diff options
author | Richard van der Hoff <richard@matrix.org> | 2016-05-24 16:23:19 +0100 |
---|---|---|
committer | Richard van der Hoff <richard@matrix.org> | 2016-05-24 16:23:19 +0100 |
commit | 1b15465c42a88f750a960a0e73f186245f9bba33 (patch) | |
tree | e0ecb976022b606e2872c490718ad5dd182be0d9 /src/outbound_group_session.c | |
parent | a919a149fbb192e3fae7aba921ca28e02d9c0d10 (diff) |
Separate base64ing from the rest of msg encoding
Factor the actual message encoding/decoding and encrypting/decrypting out to
separate functions from the top-level functions which do the base64-wrangling.
This is particularly helpful in the 'outbound' code-path where the offsets
required to allow room to base64-encode make the flow hard to see when it's all inline.
Diffstat (limited to 'src/outbound_group_session.c')
-rw-r--r-- | src/outbound_group_session.c | 65 |
1 files changed, 43 insertions, 22 deletions
diff --git a/src/outbound_group_session.c b/src/outbound_group_session.c index 9f36ad8..9b2298a 100644 --- a/src/outbound_group_session.c +++ b/src/outbound_group_session.c @@ -199,51 +199,41 @@ size_t olm_group_encrypt_message_length( return _olm_encode_base64_length(message_length); } - -size_t olm_group_encrypt( - OlmOutboundGroupSession *session, - uint8_t const * plaintext, size_t plaintext_length, - uint8_t * message, size_t max_message_length +/** write an un-base64-ed message to the buffer */ +static size_t _encrypt( + OlmOutboundGroupSession *session, uint8_t const * plaintext, size_t plaintext_length, + uint8_t * buffer ) { - size_t ciphertext_length; - size_t rawmsglen; + size_t ciphertext_length, mac_length, message_length; size_t result; - uint8_t *ciphertext_ptr, *message_pos; - - rawmsglen = raw_message_length(session, plaintext_length); - - if (max_message_length < _olm_encode_base64_length(rawmsglen)) { - session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL; - return (size_t)-1; - } + uint8_t *ciphertext_ptr; ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length( megolm_cipher, plaintext_length ); - /* we construct the message at the end of the buffer, so that - * we have room to base64-encode it once we're done. - */ - message_pos = message + _olm_encode_base64_length(rawmsglen) - rawmsglen; + mac_length = megolm_cipher->ops->mac_length(megolm_cipher); /* first we build the message structure, then we encrypt * the plaintext into it. */ - _olm_encode_group_message( + message_length = _olm_encode_group_message( OLM_PROTOCOL_VERSION, session->session_id, GROUP_SESSION_ID_LENGTH, session->ratchet.counter, ciphertext_length, - message_pos, + buffer, &ciphertext_ptr); + message_length += mac_length; + result = megolm_cipher->ops->encrypt( megolm_cipher, megolm_get_data(&(session->ratchet)), MEGOLM_RATCHET_LENGTH, plaintext, plaintext_length, ciphertext_ptr, ciphertext_length, - message_pos, rawmsglen + buffer, message_length ); if (result == (size_t)-1) { @@ -252,6 +242,37 @@ size_t olm_group_encrypt( megolm_advance(&(session->ratchet)); + return result; +} + +size_t olm_group_encrypt( + OlmOutboundGroupSession *session, + uint8_t const * plaintext, size_t plaintext_length, + uint8_t * message, size_t max_message_length +) { + size_t rawmsglen; + size_t result; + uint8_t *message_pos; + + rawmsglen = raw_message_length(session, plaintext_length); + + if (max_message_length < _olm_encode_base64_length(rawmsglen)) { + session->last_error = OLM_OUTPUT_BUFFER_TOO_SMALL; + return (size_t)-1; + } + + /* we construct the message at the end of the buffer, so that + * we have room to base64-encode it once we're done. + */ + message_pos = message + _olm_encode_base64_length(rawmsglen) - rawmsglen; + + /* write the message, and encrypt it, at message_pos */ + result = _encrypt(session, plaintext, plaintext_length, message_pos); + if (result == (size_t)-1) { + return result; + } + + /* bas64-encode it */ return _olm_encode_base64( message_pos, rawmsglen, message ); |