• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

CMakeModules/H28-Mar-2020-

protobuf/H28-Mar-2020-

src/H03-May-2022-

tests/H03-May-2022-

.gitattributesH A D28-Mar-2020250

.gitignoreH A D28-Mar-2020147

ISSUE_TEMPLATE.mdH A D28-Mar-20201.5 KiB

LICENSEH A D28-Mar-202031.7 KiB

README.mdH A D28-Mar-20209.7 KiB

README.md

1# Overview
2
3This is a ratcheting forward secrecy protocol that works in synchronous and asynchronous messaging
4environments. See the [Java library](https://github.com/whispersystems/libsignal-protocol-java) for more details.
5
6# Building libsignal-protocol-c
7
8## Development host setup
9
10### Build dependencies
11
12* [CMake](https://cmake.org/) 2.8.4 or higher
13* [Check *1](https://libcheck.github.io/check/)
14* [OpenSSL *1](https://www.openssl.org/) 1.0 or higher
15 * On MacOS X, [Common Crypto](https://developer.apple.com/library/content/documentation/Security/Conceptual/cryptoservices/GeneralPurposeCrypto/GeneralPurposeCrypto.html) is used instead of OpenSSL
16* [LCOV *2](http://ltp.sourceforge.net/coverage/lcov.php)
17
18Most of these dependencies are required just for the unit test suite and
19development of the library itself. When integrating into actual applications,
20you should not need anything beyond CMake. Alternatively, you may integrate
21the code using a build system of your choice.
22Items marked with *1 are required for tests, with *2 are additionally required for code coverage.
23
24### Setting up a fresh source tree
25
26    $ cd /path/to/libsignal-protocol-c
27    $ mkdir build
28    $ cd build
29    $ cmake -DCMAKE_BUILD_TYPE=Debug ..
30    $ make
31
32### Running the unit tests
33
34    $ cd /path/to/libsignal-protocol-c/build
35    $ cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=1 ..
36    $ cd tests
37    $ make
38    $ cd ..
39    $ ctest
40
41### Creating the code coverage report
42
43    $ cd /path/to/libsignal-protocol-c/build
44    $ cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=1 -DCOVERAGE=1 ..
45    $ make coverage
46
47The generated code coverage report can be found in:
48`/path/to/libsignal-protocol-c/build/coverage`
49
50### Eclipse project setup
51
52CMake provides a tutorial on Eclipse project setup here:
53https://cmake.org/Wiki/CMake:Eclipse_UNIX_Tutorial
54
55It is recommended to follow the more manual "Option 2," since the Eclipse
56project generator built into CMake tends to be outdated and leads you toward
57a very awkward and occasionally broken project configuration.
58
59### Protocol Buffers compiler
60
61This project uses serialization code based on [Protocol Buffers](https://github.com/google/protobuf).
62Since the official library does not support C, the [protobuf-c](https://github.com/protobuf-c/protobuf-c)
63generator is used instead. For the sake of convenience, the generated code and its dependencies are
64included in the source tree. The generated code can be regenerated at any time by installing the two
65mentioned packages and running "make" in the "protobuf/" subdirectory.
66
67## Target platforms
68
69CMake toolchain files have been included from the following sources:
70
71* [iOS](https://code.google.com/archive/p/ios-cmake)
72* [BlackBerry 10](https://github.com/blackberry/OGRE/blob/master/src/CMake/toolchain/blackberry.toolchain.cmake)
73
74# Using libsignal-protocol-c
75
76## Library initialization
77
78Before using the library, a libsignal-protocol-c client needs to initialize a global
79context. This global context is used to provide callbacks for implementations
80of functions used across the library that need client-specific implementations.
81Refer to "signal_protocol.h" for detailed documentation on these functions, and the unit
82tests for example implementations.
83
84```c
85signal_context *global_context;
86signal_context_create(&global_context, user_data);
87signal_context_set_crypto_provider(global_context, &provider);
88signal_context_set_locking_functions(global_context, lock_function, unlock_function);
89```
90
91## Client install time
92
93At install time, a libsignal-protocol-c client needs to generate its identity keys,
94registration id, and prekeys.
95
96```c
97ratchet_identity_key_pair *identity_key_pair;
98uint32_t registration_id;
99signal_protocol_key_helper_pre_key_list_node *pre_keys_head;
100session_signed_pre_key *signed_pre_key;
101
102signal_protocol_key_helper_generate_identity_key_pair(&identity_key_pair, global_context);
103signal_protocol_key_helper_generate_registration_id(&registration_id, 0, global_context);
104signal_protocol_key_helper_generate_pre_keys(&pre_keys_head, start_id, 100, global_context);
105signal_protocol_key_helper_generate_signed_pre_key(&signed_pre_key, identity_key_pair, 5, timestamp, global_context);
106
107/* Store identity_key_pair somewhere durable and safe. */
108/* Store registration_id somewhere durable and safe. */
109
110/* Store pre keys in the pre key store. */
111/* Store signed pre key in the signed pre key store. */
112```
113
114The above example is simplified for the sake of clarity. All of these functions return errors
115on failure, and those errors should be checked for in real usage.
116
117There are also iteration and serialization methods for the above types that should
118be used as appropriate.
119
120## Building a session
121
122A libsignal-protocol-c client needs to implement four data store callback interfaces:
123`signal_protocol_identity_key_store`, `signal_protocol_pre_key_store`,
124`signal_protocol_signed_pre_key_store`, and `signal_protocol_session_store`.
125These will manage loading and storing of identity, prekeys, signed prekeys,
126and session state.
127
128These callback interfaces are designed such that implementations should treat
129all data flowing through them as opaque binary blobs. Anything necessary for
130referencing that data will be provided as separate function arguments to those
131callbacks. If it is ever necessary for clients to directly access stored data
132in terms of library data structures, they should use the accessor functions
133declared in "signal_protocol.h" for these data stores.
134
135Once the callbacks for these data stores are implemented, building a session
136is fairly straightforward:
137
138```c
139/* Create the data store context, and add all the callbacks to it */
140signal_protocol_store_context *store_context;
141signal_protocol_store_context_create(&store_context, context);
142signal_protocol_store_context_set_session_store(store_context, &session_store);
143signal_protocol_store_context_set_pre_key_store(store_context, &pre_key_store);
144signal_protocol_store_context_set_signed_pre_key_store(store_context, &signed_pre_key_store);
145signal_protocol_store_context_set_identity_key_store(store_context, &identity_key_store);
146
147/* Instantiate a session_builder for a recipient address. */
148signal_protocol_address address = {
149    "+14159998888", 12, 1
150};
151session_builder *builder;
152session_builder_create(&builder, store_context, &address, global_context);
153
154/* Build a session with a pre key retrieved from the server. */
155session_builder_process_pre_key_bundle(builder, retrieved_pre_key);
156
157/* Create the session cipher and encrypt the message */
158session_cipher *cipher;
159session_cipher_create(&cipher, store_context, &address, global_context);
160
161ciphertext_message *encrypted_message;
162session_cipher_encrypt(cipher, message, message_len, &encrypted_message);
163
164/* Get the serialized content and deliver it */
165signal_buffer *serialized = ciphertext_message_get_serialized(encrypted_message);
166
167deliver(signal_buffer_data(serialized), signal_buffer_len(serialized));
168
169/* Cleanup */
170SIGNAL_UNREF(encrypted_message);
171session_cipher_free(cipher);
172session_builder_free(builder);
173signal_protocol_store_context_destroy(store_context);
174```
175
176The above example is simplified for the sake of clarity. All of these functions return errors
177on failure, and those errors should be checked for in real usage.
178
179## Memory management notes
180
181For every custom data type that the libsignal-protocol-c library can allocate and
182return, a corresponding way of deallocating an instance of that data type
183is provided.
184
185The more basic and higher level data types provide a type-specific free or
186destroy function. These types include `signal_context`,
187`signal_protocol_store_context`, `signal_buffer`, `signal_buffer_list`,
188`signal_int_list`, `signal_protocol_key_helper_pre_key_list_node`, `session_builder`,
189`session_cipher`, `group_session_builder`, `group_cipher`, and
190`fingerprint_generator`.
191
192Most of the other data types, including everything internal, use a reference
193counting mechanism. If you are going to hold onto a reference to one of these
194types, use the `SIGNAL_REF(x)` macro to increment its count. If you are done
195with a reference, use `SIGNAL_UNREF(x)` to decrement its count. When the count
196reaches 0, the type's destructor function is called.
197
198# Legal things
199## Cryptography Notice
200
201This distribution includes cryptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software.
202BEFORE using any encryption software, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted.
203See <http://www.wassenaar.org/> for more information.
204
205The U.S. Government Department of Commerce, Bureau of Industry and Security (BIS), has classified this software as Export Commodity Control Number (ECCN) 5D002.C.1, which includes information security software using or performing cryptographic functions with asymmetric algorithms.
206The form and manner of this distribution makes it eligible for export under the License Exception ENC Technology Software Unrestricted (TSU) exception (see the BIS Export Administration Regulations, Section 740.13) for both object code and source code.
207
208## License
209
210Copyright 2015-2016 Open Whisper Systems
211
212Licensed under the GPLv3: http://www.gnu.org/licenses/gpl-3.0.html
213
214Additional Permissions For Submission to Apple App Store: Provided that you are otherwise in compliance with the GPLv3 for each covered work you convey (including without limitation making the Corresponding Source available in compliance with Section 6 of the GPLv3), Open Whisper Systems also grants you the additional permission to convey through the Apple App Store non-source executable versions of the Program as incorporated into each applicable covered work as Executable Versions only under the Mozilla Public License version 2.0 (https://www.mozilla.org/en-US/MPL/2.0/).
215
216