1 /* Copyright 2016 OpenMarket Ltd
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "olm/outbound_group_session.h"
20 #include "olm/base64.h"
21 #include "olm/cipher.h"
22 #include "olm/crypto.h"
23 #include "olm/error.h"
24 #include "olm/megolm.h"
25 #include "olm/memory.h"
26 #include "olm/message.h"
27 #include "olm/pickle.h"
28 #include "olm/pickle_encoding.h"
30 #define OLM_PROTOCOL_VERSION 3
31 #define GROUP_SESSION_ID_LENGTH ED25519_PUBLIC_KEY_LENGTH
32 #define PICKLE_VERSION 1
33 #define SESSION_KEY_VERSION 2
35 struct OlmOutboundGroupSession {
36 /** the Megolm ratchet providing the encryption keys */
39 /** The ed25519 keypair used for signing the messages */
40 struct _olm_ed25519_key_pair signing_key;
42 enum OlmErrorCode last_error;
46 size_t olm_outbound_group_session_size(void) {
47 return sizeof(OlmOutboundGroupSession);
50 OlmOutboundGroupSession * olm_outbound_group_session(
53 OlmOutboundGroupSession *session = memory;
54 olm_clear_outbound_group_session(session);
58 const char *olm_outbound_group_session_last_error(
59 const OlmOutboundGroupSession *session
61 return _olm_error_to_string(session->last_error);
64 enum OlmErrorCode olm_outbound_group_session_last_error_code(
65 const OlmOutboundGroupSession *session
67 return session->last_error;
70 size_t olm_clear_outbound_group_session(
71 OlmOutboundGroupSession *session
73 _olm_unset(session, sizeof(OlmOutboundGroupSession));
74 return sizeof(OlmOutboundGroupSession);
77 static size_t raw_pickle_length(
78 const OlmOutboundGroupSession *session
81 length += _olm_pickle_uint32_length(PICKLE_VERSION);
82 length += megolm_pickle_length(&(session->ratchet));
83 length += _olm_pickle_ed25519_key_pair_length(&(session->signing_key));
87 size_t olm_pickle_outbound_group_session_length(
88 const OlmOutboundGroupSession *session
90 return _olm_enc_output_length(raw_pickle_length(session));
93 size_t olm_pickle_outbound_group_session(
94 OlmOutboundGroupSession *session,
95 void const * key, size_t key_length,
96 void * pickled, size_t pickled_length
98 size_t raw_length = raw_pickle_length(session);
101 if (pickled_length < _olm_enc_output_length(raw_length)) {
102 session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
107 pos = _olm_enc_output_pos(pickled, raw_length);
112 pos = _olm_pickle_uint32(pos, PICKLE_VERSION);
113 pos = megolm_pickle(&(session->ratchet), pos);
114 pos = _olm_pickle_ed25519_key_pair(pos, &(session->signing_key));
117 return _olm_enc_output(key, key_length, pickled, raw_length);
123 size_t olm_unpickle_outbound_group_session(
124 OlmOutboundGroupSession *session,
125 void const * key, size_t key_length,
126 void * pickled, size_t pickled_length
130 uint32_t pickle_version;
133 size_t raw_length = _olm_enc_input(
134 key, key_length, pickled, pickled_length, &(session->last_error)
137 size_t raw_length = pickled_length;
140 if (raw_length == (size_t)-1) {
145 end = pos + raw_length;
147 pos = _olm_unpickle_uint32(pos, end, &pickle_version);
148 FAIL_ON_CORRUPTED_PICKLE(pos, session);
150 if (pickle_version != PICKLE_VERSION) {
151 session->last_error = OLM_UNKNOWN_PICKLE_VERSION;
155 pos = megolm_unpickle(&(session->ratchet), pos, end);
156 FAIL_ON_CORRUPTED_PICKLE(pos, session);
158 pos = _olm_unpickle_ed25519_key_pair(pos, end, &(session->signing_key));
159 FAIL_ON_CORRUPTED_PICKLE(pos, session);
162 /* Input was longer than expected. */
163 session->last_error = OLM_PICKLE_EXTRA_DATA;
167 return pickled_length;
171 size_t olm_init_outbound_group_session_random_length(
172 const OlmOutboundGroupSession *session
174 /* we need data to initialize the megolm ratchet, plus some more for the
177 return MEGOLM_RATCHET_LENGTH +
178 ED25519_RANDOM_LENGTH;
181 size_t olm_init_outbound_group_session(
182 OlmOutboundGroupSession *session,
183 uint8_t *random, size_t random_length
185 const uint8_t *random_ptr = random;
187 if (random_length < olm_init_outbound_group_session_random_length(session)) {
188 /* Insufficient random data for new session */
189 session->last_error = OLM_NOT_ENOUGH_RANDOM;
193 megolm_init(&(session->ratchet), random_ptr, 0);
194 random_ptr += MEGOLM_RATCHET_LENGTH;
196 _olm_crypto_ed25519_generate_key(random_ptr, &(session->signing_key));
197 random_ptr += ED25519_RANDOM_LENGTH;
199 _olm_unset(random, random_length);
203 static size_t raw_message_length(
204 OlmOutboundGroupSession *session,
205 size_t plaintext_length)
207 size_t ciphertext_length, mac_length;
209 ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
210 megolm_cipher, plaintext_length
213 mac_length = megolm_cipher->ops->mac_length(megolm_cipher);
215 return _olm_encode_group_message_length(
216 session->ratchet.counter,
217 ciphertext_length, mac_length, ED25519_SIGNATURE_LENGTH
221 size_t olm_group_encrypt_message_length(
222 OlmOutboundGroupSession *session,
223 size_t plaintext_length
225 size_t message_length = raw_message_length(session, plaintext_length);
226 return _olm_encode_base64_length(message_length);
229 /** write an un-base64-ed message to the buffer */
230 static size_t _encrypt(
231 OlmOutboundGroupSession *session, uint8_t const * plaintext, size_t plaintext_length,
234 size_t ciphertext_length, mac_length, message_length;
236 uint8_t *ciphertext_ptr;
238 ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
243 mac_length = megolm_cipher->ops->mac_length(megolm_cipher);
245 /* first we build the message structure, then we encrypt
246 * the plaintext into it.
248 message_length = _olm_encode_group_message(
249 OLM_PROTOCOL_VERSION,
250 session->ratchet.counter,
255 message_length += mac_length;
257 result = megolm_cipher->ops->encrypt(
259 megolm_get_data(&(session->ratchet)), MEGOLM_RATCHET_LENGTH,
260 plaintext, plaintext_length,
261 ciphertext_ptr, ciphertext_length,
262 buffer, message_length
265 if (result == (size_t)-1) {
269 megolm_advance(&(session->ratchet));
271 /* sign the whole thing with the ed25519 key. */
272 _olm_crypto_ed25519_sign(
273 &(session->signing_key),
274 buffer, message_length,
275 buffer + message_length
281 size_t olm_group_encrypt(
282 OlmOutboundGroupSession *session,
283 uint8_t const * plaintext, size_t plaintext_length,
284 uint8_t * message, size_t max_message_length
288 uint8_t *message_pos;
290 rawmsglen = raw_message_length(session, plaintext_length);
292 if (max_message_length < _olm_encode_base64_length(rawmsglen)) {
293 session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
297 /* we construct the message at the end of the buffer, so that
298 * we have room to base64-encode it once we're done.
300 message_pos = message + _olm_encode_base64_length(rawmsglen) - rawmsglen;
302 /* write the message, and encrypt it, at message_pos */
303 result = _encrypt(session, plaintext, plaintext_length, message_pos);
304 if (result == (size_t)-1) {
308 /* bas64-encode it */
309 return _olm_encode_base64(
310 message_pos, rawmsglen, message
315 size_t olm_outbound_group_session_id_length(
316 const OlmOutboundGroupSession *session
318 return _olm_encode_base64_length(GROUP_SESSION_ID_LENGTH);
321 size_t olm_outbound_group_session_id(
322 OlmOutboundGroupSession *session,
323 uint8_t * id, size_t id_length
325 if (id_length < olm_outbound_group_session_id_length(session)) {
326 session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
330 return _olm_encode_base64(
331 session->signing_key.public_key.public_key, GROUP_SESSION_ID_LENGTH, id
335 uint32_t olm_outbound_group_session_message_index(
336 OlmOutboundGroupSession *session
338 return session->ratchet.counter;
341 #define SESSION_KEY_RAW_LENGTH \
342 (1 + 4 + MEGOLM_RATCHET_LENGTH + ED25519_PUBLIC_KEY_LENGTH\
343 + ED25519_SIGNATURE_LENGTH)
345 size_t olm_outbound_group_session_key_length(
346 const OlmOutboundGroupSession *session
348 return _olm_encode_base64_length(SESSION_KEY_RAW_LENGTH);
351 size_t olm_outbound_group_session_key(
352 OlmOutboundGroupSession *session,
353 uint8_t * key, size_t key_length
357 size_t encoded_length = olm_outbound_group_session_key_length(session);
359 if (key_length < encoded_length) {
360 session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL;
364 /* put the raw data at the end of the output buffer. */
365 raw = ptr = key + encoded_length - SESSION_KEY_RAW_LENGTH;
366 *ptr++ = SESSION_KEY_VERSION;
368 uint32_t counter = session->ratchet.counter;
369 // Encode counter as a big endian 32-bit number.
370 for (unsigned i = 0; i < 4; i++) {
371 *ptr++ = 0xFF & (counter >> 24); counter <<= 8;
374 memcpy(ptr, megolm_get_data(&session->ratchet), MEGOLM_RATCHET_LENGTH);
375 ptr += MEGOLM_RATCHET_LENGTH;
378 ptr, session->signing_key.public_key.public_key,
379 ED25519_PUBLIC_KEY_LENGTH
381 ptr += ED25519_PUBLIC_KEY_LENGTH;
383 /* sign the whole thing with the ed25519 key. */
384 _olm_crypto_ed25519_sign(
385 &(session->signing_key),
389 return _olm_encode_base64(raw, SESSION_KEY_RAW_LENGTH, key);