Update megolm_cipher as a global struct
Initialise megolm_cipher via the preprocessor macro, instead of with a function.
This commit is contained in:
parent
fa1e9446ac
commit
a919a149fb
4 changed files with 20 additions and 33 deletions
|
@ -49,11 +49,11 @@ typedef struct Megolm {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the cipher used in megolm-backed conversations
|
* The cipher used in megolm-backed conversations
|
||||||
*
|
*
|
||||||
* (AES256 + SHA256, with keys based on an HKDF with info of MEGOLM_KEYS)
|
* (AES256 + SHA256, with keys based on an HKDF with info of MEGOLM_KEYS)
|
||||||
*/
|
*/
|
||||||
const struct _olm_cipher *megolm_cipher();
|
extern const struct _olm_cipher *megolm_cipher;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* initialize the megolm ratchet. random_data should be at least
|
* initialize the megolm ratchet. random_data should be at least
|
||||||
|
|
|
@ -168,7 +168,6 @@ size_t olm_group_decrypt_max_plaintext_length(
|
||||||
uint8_t * message, size_t message_length
|
uint8_t * message, size_t message_length
|
||||||
) {
|
) {
|
||||||
size_t r;
|
size_t r;
|
||||||
const struct _olm_cipher *cipher = megolm_cipher();
|
|
||||||
struct _OlmDecodeGroupMessageResults decoded_results;
|
struct _OlmDecodeGroupMessageResults decoded_results;
|
||||||
|
|
||||||
r = _olm_decode_base64(message, message_length, message);
|
r = _olm_decode_base64(message, message_length, message);
|
||||||
|
@ -179,7 +178,7 @@ size_t olm_group_decrypt_max_plaintext_length(
|
||||||
|
|
||||||
_olm_decode_group_message(
|
_olm_decode_group_message(
|
||||||
message, message_length,
|
message, message_length,
|
||||||
cipher->ops->mac_length(cipher),
|
megolm_cipher->ops->mac_length(megolm_cipher),
|
||||||
&decoded_results);
|
&decoded_results);
|
||||||
|
|
||||||
if (decoded_results.version != OLM_PROTOCOL_VERSION) {
|
if (decoded_results.version != OLM_PROTOCOL_VERSION) {
|
||||||
|
@ -192,8 +191,8 @@ size_t olm_group_decrypt_max_plaintext_length(
|
||||||
return (size_t)-1;
|
return (size_t)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return cipher->ops->decrypt_max_plaintext_length(
|
return megolm_cipher->ops->decrypt_max_plaintext_length(
|
||||||
cipher, decoded_results.ciphertext_length);
|
megolm_cipher, decoded_results.ciphertext_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -203,7 +202,6 @@ size_t olm_group_decrypt(
|
||||||
uint8_t * plaintext, size_t max_plaintext_length
|
uint8_t * plaintext, size_t max_plaintext_length
|
||||||
) {
|
) {
|
||||||
struct _OlmDecodeGroupMessageResults decoded_results;
|
struct _OlmDecodeGroupMessageResults decoded_results;
|
||||||
const struct _olm_cipher *cipher = megolm_cipher();
|
|
||||||
size_t max_length, raw_message_length, r;
|
size_t max_length, raw_message_length, r;
|
||||||
Megolm *megolm;
|
Megolm *megolm;
|
||||||
Megolm tmp_megolm;
|
Megolm tmp_megolm;
|
||||||
|
@ -216,7 +214,7 @@ size_t olm_group_decrypt(
|
||||||
|
|
||||||
_olm_decode_group_message(
|
_olm_decode_group_message(
|
||||||
message, raw_message_length,
|
message, raw_message_length,
|
||||||
cipher->ops->mac_length(cipher),
|
megolm_cipher->ops->mac_length(megolm_cipher),
|
||||||
&decoded_results);
|
&decoded_results);
|
||||||
|
|
||||||
if (decoded_results.version != OLM_PROTOCOL_VERSION) {
|
if (decoded_results.version != OLM_PROTOCOL_VERSION) {
|
||||||
|
@ -231,8 +229,8 @@ size_t olm_group_decrypt(
|
||||||
return (size_t)-1;
|
return (size_t)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
max_length = cipher->ops->decrypt_max_plaintext_length(
|
max_length = megolm_cipher->ops->decrypt_max_plaintext_length(
|
||||||
cipher,
|
megolm_cipher,
|
||||||
decoded_results.ciphertext_length
|
decoded_results.ciphertext_length
|
||||||
);
|
);
|
||||||
if (max_plaintext_length < max_length) {
|
if (max_plaintext_length < max_length) {
|
||||||
|
@ -258,8 +256,8 @@ size_t olm_group_decrypt(
|
||||||
megolm_advance_to(megolm, decoded_results.message_index);
|
megolm_advance_to(megolm, decoded_results.message_index);
|
||||||
|
|
||||||
/* now try checking the mac, and decrypting */
|
/* now try checking the mac, and decrypting */
|
||||||
r = cipher->ops->decrypt(
|
r = megolm_cipher->ops->decrypt(
|
||||||
cipher,
|
megolm_cipher,
|
||||||
megolm_get_data(megolm), MEGOLM_RATCHET_LENGTH,
|
megolm_get_data(megolm), MEGOLM_RATCHET_LENGTH,
|
||||||
message, raw_message_length,
|
message, raw_message_length,
|
||||||
decoded_results.ciphertext, decoded_results.ciphertext_length,
|
decoded_results.ciphertext, decoded_results.ciphertext_length,
|
||||||
|
|
15
src/megolm.c
15
src/megolm.c
|
@ -22,18 +22,9 @@
|
||||||
#include "olm/crypto.h"
|
#include "olm/crypto.h"
|
||||||
#include "olm/pickle.h"
|
#include "olm/pickle.h"
|
||||||
|
|
||||||
const struct _olm_cipher *megolm_cipher() {
|
static const struct _olm_cipher_aes_sha_256 MEGOLM_CIPHER =
|
||||||
static const uint8_t CIPHER_KDF_INFO[] = "MEGOLM_KEYS";
|
OLM_CIPHER_INIT_AES_SHA_256("MEGOLM_KEYS");
|
||||||
static struct _olm_cipher *cipher;
|
const struct _olm_cipher *megolm_cipher = OLM_CIPHER_BASE(&MEGOLM_CIPHER);
|
||||||
static struct _olm_cipher_aes_sha_256 OLM_CIPHER;
|
|
||||||
if (!cipher) {
|
|
||||||
cipher = _olm_cipher_aes_sha_256_init(
|
|
||||||
&OLM_CIPHER,
|
|
||||||
CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) - 1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return cipher;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* the seeds used in the HMAC-SHA-256 functions for each part of the ratchet.
|
/* the seeds used in the HMAC-SHA-256 functions for each part of the ratchet.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -179,13 +179,12 @@ static size_t raw_message_length(
|
||||||
size_t plaintext_length)
|
size_t plaintext_length)
|
||||||
{
|
{
|
||||||
size_t ciphertext_length, mac_length;
|
size_t ciphertext_length, mac_length;
|
||||||
const struct _olm_cipher *cipher = megolm_cipher();
|
|
||||||
|
|
||||||
ciphertext_length = cipher->ops->encrypt_ciphertext_length(
|
ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
|
||||||
cipher, plaintext_length
|
megolm_cipher, plaintext_length
|
||||||
);
|
);
|
||||||
|
|
||||||
mac_length = cipher->ops->mac_length(cipher);
|
mac_length = megolm_cipher->ops->mac_length(megolm_cipher);
|
||||||
|
|
||||||
return _olm_encode_group_message_length(
|
return _olm_encode_group_message_length(
|
||||||
GROUP_SESSION_ID_LENGTH, session->ratchet.counter,
|
GROUP_SESSION_ID_LENGTH, session->ratchet.counter,
|
||||||
|
@ -210,7 +209,6 @@ size_t olm_group_encrypt(
|
||||||
size_t rawmsglen;
|
size_t rawmsglen;
|
||||||
size_t result;
|
size_t result;
|
||||||
uint8_t *ciphertext_ptr, *message_pos;
|
uint8_t *ciphertext_ptr, *message_pos;
|
||||||
const struct _olm_cipher *cipher = megolm_cipher();
|
|
||||||
|
|
||||||
rawmsglen = raw_message_length(session, plaintext_length);
|
rawmsglen = raw_message_length(session, plaintext_length);
|
||||||
|
|
||||||
|
@ -219,8 +217,8 @@ size_t olm_group_encrypt(
|
||||||
return (size_t)-1;
|
return (size_t)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ciphertext_length = cipher->ops->encrypt_ciphertext_length(
|
ciphertext_length = megolm_cipher->ops->encrypt_ciphertext_length(
|
||||||
cipher,
|
megolm_cipher,
|
||||||
plaintext_length
|
plaintext_length
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -240,8 +238,8 @@ size_t olm_group_encrypt(
|
||||||
message_pos,
|
message_pos,
|
||||||
&ciphertext_ptr);
|
&ciphertext_ptr);
|
||||||
|
|
||||||
result = cipher->ops->encrypt(
|
result = megolm_cipher->ops->encrypt(
|
||||||
cipher,
|
megolm_cipher,
|
||||||
megolm_get_data(&(session->ratchet)), MEGOLM_RATCHET_LENGTH,
|
megolm_get_data(&(session->ratchet)), MEGOLM_RATCHET_LENGTH,
|
||||||
plaintext, plaintext_length,
|
plaintext, plaintext_length,
|
||||||
ciphertext_ptr, ciphertext_length,
|
ciphertext_ptr, ciphertext_length,
|
||||||
|
|
Loading…
Reference in a new issue