aboutsummaryrefslogtreecommitdiff
path: root/src/inbound_group_session.c
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2016-05-24 16:23:19 +0100
committerRichard van der Hoff <richard@matrix.org>2016-05-24 16:23:19 +0100
commit1b15465c42a88f750a960a0e73f186245f9bba33 (patch)
treee0ecb976022b606e2872c490718ad5dd182be0d9 /src/inbound_group_session.c
parenta919a149fbb192e3fae7aba921ca28e02d9c0d10 (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/inbound_group_session.c')
-rw-r--r--src/inbound_group_session.c64
1 files changed, 46 insertions, 18 deletions
diff --git a/src/inbound_group_session.c b/src/inbound_group_session.c
index b6894c1..e171205 100644
--- a/src/inbound_group_session.c
+++ b/src/inbound_group_session.c
@@ -163,19 +163,15 @@ size_t olm_unpickle_inbound_group_session(
return pickled_length;
}
-size_t olm_group_decrypt_max_plaintext_length(
+/**
+ * get the max plaintext length in an un-base64-ed message
+ */
+static size_t _decrypt_max_plaintext_length(
OlmInboundGroupSession *session,
uint8_t * message, size_t message_length
) {
- size_t r;
struct _OlmDecodeGroupMessageResults decoded_results;
- r = _olm_decode_base64(message, message_length, message);
- if (r == (size_t)-1) {
- session->last_error = OLM_INVALID_BASE64;
- return r;
- }
-
_olm_decode_group_message(
message, message_length,
megolm_cipher->ops->mac_length(megolm_cipher),
@@ -195,25 +191,38 @@ size_t olm_group_decrypt_max_plaintext_length(
megolm_cipher, decoded_results.ciphertext_length);
}
+size_t olm_group_decrypt_max_plaintext_length(
+ OlmInboundGroupSession *session,
+ uint8_t * message, size_t message_length
+) {
+ size_t raw_length;
-size_t olm_group_decrypt(
+ raw_length = _olm_decode_base64(message, message_length, message);
+ if (raw_length == (size_t)-1) {
+ session->last_error = OLM_INVALID_BASE64;
+ return (size_t)-1;
+ }
+
+ return _decrypt_max_plaintext_length(
+ session, message, raw_length
+ );
+}
+
+/**
+ * decrypt an un-base64-ed message
+ */
+static size_t _decrypt(
OlmInboundGroupSession *session,
uint8_t * message, size_t message_length,
uint8_t * plaintext, size_t max_plaintext_length
) {
struct _OlmDecodeGroupMessageResults decoded_results;
- size_t max_length, raw_message_length, r;
+ size_t max_length, r;
Megolm *megolm;
Megolm tmp_megolm;
- raw_message_length = _olm_decode_base64(message, message_length, message);
- if (raw_message_length == (size_t)-1) {
- session->last_error = OLM_INVALID_BASE64;
- return (size_t)-1;
- }
-
_olm_decode_group_message(
- message, raw_message_length,
+ message, message_length,
megolm_cipher->ops->mac_length(megolm_cipher),
&decoded_results);
@@ -259,7 +268,7 @@ size_t olm_group_decrypt(
r = megolm_cipher->ops->decrypt(
megolm_cipher,
megolm_get_data(megolm), MEGOLM_RATCHET_LENGTH,
- message, raw_message_length,
+ message, message_length,
decoded_results.ciphertext, decoded_results.ciphertext_length,
plaintext, max_plaintext_length
);
@@ -272,3 +281,22 @@ size_t olm_group_decrypt(
return r;
}
+
+size_t olm_group_decrypt(
+ OlmInboundGroupSession *session,
+ uint8_t * message, size_t message_length,
+ uint8_t * plaintext, size_t max_plaintext_length
+) {
+ size_t raw_message_length;
+
+ raw_message_length = _olm_decode_base64(message, message_length, message);
+ if (raw_message_length == (size_t)-1) {
+ session->last_error = OLM_INVALID_BASE64;
+ return (size_t)-1;
+ }
+
+ return _decrypt(
+ session, message, raw_message_length,
+ plaintext, max_plaintext_length
+ );
+}