From e533b0dc8ef606aa808b38d2f49d9baf438dae47 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 13 May 2016 12:56:23 +0100 Subject: Give SHA256 functions C bindings --- src/session.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 86ba63b..85c958c 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -192,7 +192,7 @@ std::size_t olm::Session::new_inbound_session( std::size_t olm::Session::session_id_length() { - return olm::SHA256_OUTPUT_LENGTH; + return SHA256_OUTPUT_LENGTH; } @@ -208,7 +208,7 @@ std::size_t olm::Session::session_id( pos = olm::store_array(pos, alice_identity_key.public_key); pos = olm::store_array(pos, alice_base_key.public_key); pos = olm::store_array(pos, bob_one_time_key.public_key); - olm::sha256(tmp, sizeof(tmp), id); + crypto_sha256(tmp, sizeof(tmp), id); return session_id_length(); } -- cgit v1.2.3 From f9139dfa6aea6ca8c4054a5b5fff9be484d978fa Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 16 May 2016 12:08:45 +0100 Subject: Convert error.hh to plain C --- src/session.cpp | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 85c958c..c0b6cf4 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -43,7 +43,7 @@ static const olm::KdfInfo OLM_KDF_INFO = { olm::Session::Session( ) : ratchet(OLM_KDF_INFO, OLM_CIPHER), - last_error(olm::ErrorCode::SUCCESS), + last_error(OlmErrorCode::OLM_SUCCESS), received_message(false) { } @@ -61,7 +61,7 @@ std::size_t olm::Session::new_outbound_session( std::uint8_t const * random, std::size_t random_length ) { if (random_length < new_outbound_session_random_length()) { - last_error = olm::ErrorCode::NOT_ENOUGH_RANDOM; + last_error = OlmErrorCode::OLM_NOT_ENOUGH_RANDOM; return std::size_t(-1); } @@ -128,7 +128,7 @@ std::size_t olm::Session::new_inbound_session( decode_one_time_key_message(reader, one_time_key_message, message_length); if (!check_message_fields(reader, their_identity_key)) { - last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT; return std::size_t(-1); } @@ -137,7 +137,7 @@ std::size_t olm::Session::new_inbound_session( their_identity_key->public_key, reader.identity_key, olm::KEY_LENGTH ); if (!same) { - last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID; return std::size_t(-1); } } @@ -154,7 +154,7 @@ std::size_t olm::Session::new_inbound_session( if (!message_reader.ratchet_key || message_reader.ratchet_key_length != olm::KEY_LENGTH) { - last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT; return std::size_t(-1); } @@ -166,7 +166,7 @@ std::size_t olm::Session::new_inbound_session( ); if (!our_one_time_key) { - last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID; return std::size_t(-1); } @@ -200,7 +200,7 @@ std::size_t olm::Session::session_id( std::uint8_t * id, std::size_t id_length ) { if (id_length < session_id_length()) { - last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } std::uint8_t tmp[olm::KEY_LENGTH * 3]; @@ -286,7 +286,7 @@ std::size_t olm::Session::encrypt( std::uint8_t * message, std::size_t message_length ) { if (message_length < encrypt_message_length(plaintext_length)) { - last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } std::uint8_t * message_body; @@ -321,7 +321,7 @@ std::size_t olm::Session::encrypt( if (result == std::size_t(-1)) { last_error = ratchet.last_error; - ratchet.last_error = olm::ErrorCode::SUCCESS; + ratchet.last_error = OlmErrorCode::OLM_SUCCESS; return result; } @@ -342,7 +342,7 @@ std::size_t olm::Session::decrypt_max_plaintext_length( olm::PreKeyMessageReader reader; decode_one_time_key_message(reader, message, message_length); if (!reader.message) { - last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT; return std::size_t(-1); } message_body = reader.message; @@ -355,7 +355,7 @@ std::size_t olm::Session::decrypt_max_plaintext_length( if (result == std::size_t(-1)) { last_error = ratchet.last_error; - ratchet.last_error = olm::ErrorCode::SUCCESS; + ratchet.last_error = OlmErrorCode::OLM_SUCCESS; } return result; } @@ -375,7 +375,7 @@ std::size_t olm::Session::decrypt( olm::PreKeyMessageReader reader; decode_one_time_key_message(reader, message, message_length); if (!reader.message) { - last_error = olm::ErrorCode::BAD_MESSAGE_FORMAT; + last_error = OlmErrorCode::OLM_BAD_MESSAGE_FORMAT; return std::size_t(-1); } message_body = reader.message; @@ -388,7 +388,7 @@ std::size_t olm::Session::decrypt( if (result == std::size_t(-1)) { last_error = ratchet.last_error; - ratchet.last_error = olm::ErrorCode::SUCCESS; + ratchet.last_error = OlmErrorCode::OLM_SUCCESS; return result; } @@ -435,7 +435,7 @@ std::uint8_t const * olm::unpickle( uint32_t pickle_version; pos = olm::unpickle(pos, end, pickle_version); if (pickle_version != SESSION_PICKLE_VERSION) { - value.last_error = olm::ErrorCode::UNKNOWN_PICKLE_VERSION; + value.last_error = OlmErrorCode::OLM_UNKNOWN_PICKLE_VERSION; return end; } pos = olm::unpickle(pos, end, value.received_message); -- cgit v1.2.3 From 294cf482ea49f690ac9eaad52f2574a90b2e51e6 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Mon, 16 May 2016 16:25:09 +0100 Subject: Convert cipher.hh to plain C --- src/session.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index c0b6cf4..0d9b58a 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "olm/session.hh" -#include "olm/cipher.hh" +#include "olm/cipher.h" #include "olm/crypto.hh" #include "olm/account.hh" #include "olm/memory.hh" @@ -30,19 +30,27 @@ static const std::uint8_t ROOT_KDF_INFO[] = "OLM_ROOT"; static const std::uint8_t RATCHET_KDF_INFO[] = "OLM_RATCHET"; static const std::uint8_t CIPHER_KDF_INFO[] = "OLM_KEYS"; -static const olm::CipherAesSha256 OLM_CIPHER( - CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) -1 -); - static const olm::KdfInfo OLM_KDF_INFO = { ROOT_KDF_INFO, sizeof(ROOT_KDF_INFO) - 1, RATCHET_KDF_INFO, sizeof(RATCHET_KDF_INFO) - 1 }; +const olm_cipher *get_cipher() { + static olm_cipher *cipher; + static 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; +} + } // namespace olm::Session::Session( -) : ratchet(OLM_KDF_INFO, OLM_CIPHER), +) : ratchet(OLM_KDF_INFO, get_cipher()), last_error(OlmErrorCode::OLM_SUCCESS), received_message(false) { @@ -149,7 +157,7 @@ std::size_t olm::Session::new_inbound_session( olm::MessageReader message_reader; decode_message( message_reader, reader.message, reader.message_length, - ratchet.ratchet_cipher.mac_length() + ratchet.ratchet_cipher->ops->mac_length(ratchet.ratchet_cipher) ); if (!message_reader.ratchet_key -- cgit v1.2.3 From 444ef1f70687c340ba1b0b2a22d6e63c734d5f9e Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Fri, 20 May 2016 11:59:31 +0100 Subject: Prefix for internal symbols Give a load of internal symbols "_olm_" prefixes. This better delineates the public and private interfaces in the module, and helps avoid internal symbols leaking out and possibly being abused. --- src/session.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 0d9b58a..19b9f21 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -35,11 +35,11 @@ static const olm::KdfInfo OLM_KDF_INFO = { RATCHET_KDF_INFO, sizeof(RATCHET_KDF_INFO) - 1 }; -const olm_cipher *get_cipher() { - static olm_cipher *cipher; - static olm_cipher_aes_sha_256 OLM_CIPHER; +const _olm_cipher *get_cipher() { + static _olm_cipher *cipher; + static _olm_cipher_aes_sha_256 OLM_CIPHER; if (!cipher) { - cipher = olm_cipher_aes_sha_256_init( + cipher = _olm_cipher_aes_sha_256_init( &OLM_CIPHER, CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) - 1 ); @@ -216,7 +216,7 @@ std::size_t olm::Session::session_id( pos = olm::store_array(pos, alice_identity_key.public_key); pos = olm::store_array(pos, alice_base_key.public_key); pos = olm::store_array(pos, bob_one_time_key.public_key); - crypto_sha256(tmp, sizeof(tmp), id); + _olm_crypto_sha256(tmp, sizeof(tmp), id); return session_id_length(); } -- cgit v1.2.3 From 2fd28a66824bda7b86c08b065736009c39761987 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 24 May 2016 12:06:47 +0100 Subject: Rewrite _olm_cipher_aes_sha_256 initialisation Replace the init-static-var dance with some preprocessor macros --- src/session.cpp | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'src/session.cpp') diff --git a/src/session.cpp b/src/session.cpp index 19b9f21..c148c97 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -35,22 +35,13 @@ static const olm::KdfInfo OLM_KDF_INFO = { RATCHET_KDF_INFO, sizeof(RATCHET_KDF_INFO) - 1 }; -const _olm_cipher *get_cipher() { - static _olm_cipher *cipher; - static _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; -} +static const struct _olm_cipher_aes_sha_256 OLM_CIPHER = + OLM_CIPHER_INIT_AES_SHA_256(CIPHER_KDF_INFO); } // namespace olm::Session::Session( -) : ratchet(OLM_KDF_INFO, get_cipher()), +) : ratchet(OLM_KDF_INFO, OLM_CIPHER_BASE(&OLM_CIPHER)), last_error(OlmErrorCode::OLM_SUCCESS), received_message(false) { -- cgit v1.2.3