From 4f1bb49d20ff7e9583a764b5d7d6d6cd9dc26870 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 17 May 2016 12:13:57 +0100 Subject: Rename olm.hh to olm.h --- src/olm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/olm.cpp') diff --git a/src/olm.cpp b/src/olm.cpp index 4e5f215..d23ad81 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "olm/olm.hh" +#include "olm/olm.h" #include "olm/session.hh" #include "olm/account.hh" #include "olm/utility.hh" -- 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/olm.cpp | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/olm.cpp') diff --git a/src/olm.cpp b/src/olm.cpp index d23ad81..56bb11f 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -104,11 +104,11 @@ std::size_t enc_output( std::size_t enc_input( std::uint8_t const * key, std::size_t key_length, std::uint8_t * input, size_t b64_length, - olm::ErrorCode & last_error + OlmErrorCode & last_error ) { std::size_t enc_length = olm::decode_base64_length(b64_length); if (enc_length == std::size_t(-1)) { - last_error = olm::ErrorCode::INVALID_BASE64; + last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::decode_base64(input, b64_length, input); @@ -120,7 +120,7 @@ std::size_t enc_input( input, raw_length ); if (result == std::size_t(-1)) { - last_error = olm::ErrorCode::BAD_ACCOUNT_KEY; + last_error = OlmErrorCode::OLM_BAD_ACCOUNT_KEY; } return result; } @@ -150,11 +150,11 @@ std::size_t b64_output( std::size_t b64_input( std::uint8_t * input, size_t b64_length, - olm::ErrorCode & last_error + OlmErrorCode & last_error ) { std::size_t raw_length = olm::decode_base64_length(b64_length); if (raw_length == std::size_t(-1)) { - last_error = olm::ErrorCode::INVALID_BASE64; + last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::decode_base64(input, b64_length, input); @@ -312,7 +312,7 @@ size_t olm_pickle_account( olm::Account & object = *from_c(account); std::size_t raw_length = pickle_length(object); if (pickled_length < enc_output_length(raw_length)) { - object.last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return size_t(-1); } pickle(enc_output_pos(from_c(pickled), raw_length), object); @@ -328,7 +328,7 @@ size_t olm_pickle_session( olm::Session & object = *from_c(session); std::size_t raw_length = pickle_length(object); if (pickled_length < enc_output_length(raw_length)) { - object.last_error = olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + object.last_error = OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return size_t(-1); } pickle(enc_output_pos(from_c(pickled), raw_length), object); @@ -355,8 +355,8 @@ size_t olm_unpickle_account( * (pos + raw_length). On error unpickle will return (pos + raw_length + 1). */ if (end != unpickle(pos, end + 1, object)) { - if (object.last_error == olm::ErrorCode::SUCCESS) { - object.last_error = olm::ErrorCode::CORRUPTED_PICKLE; + if (object.last_error == OlmErrorCode::OLM_SUCCESS) { + object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE; } return std::size_t(-1); } @@ -384,8 +384,8 @@ size_t olm_unpickle_session( * (pos + raw_length). On error unpickle will return (pos + raw_length + 1). */ if (end != unpickle(pos, end + 1, object)) { - if (object.last_error == olm::ErrorCode::SUCCESS) { - object.last_error = olm::ErrorCode::CORRUPTED_PICKLE; + if (object.last_error == OlmErrorCode::OLM_SUCCESS) { + object.last_error = OlmErrorCode::OLM_CORRUPTED_PICKLE; } return std::size_t(-1); } @@ -442,7 +442,7 @@ size_t olm_account_sign( std::size_t raw_length = from_c(account)->signature_length(); if (signature_length < b64_output_length(raw_length)) { from_c(account)->last_error = - olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } from_c(account)->sign( @@ -528,7 +528,7 @@ size_t olm_create_outbound_session( if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH || olm::decode_base64_length(ot_key_length) != olm::KEY_LENGTH ) { - from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64; + from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::Curve25519PublicKey identity_key; @@ -573,7 +573,7 @@ size_t olm_create_inbound_session_from( std::size_t id_key_length = their_identity_key_length; if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) { - from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64; + from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::Curve25519PublicKey identity_key; @@ -605,7 +605,7 @@ size_t olm_session_id( std::size_t raw_length = from_c(session)->session_id_length(); if (id_length < b64_output_length(raw_length)) { from_c(session)->last_error = - olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } std::size_t result = from_c(session)->session_id( @@ -644,7 +644,7 @@ size_t olm_matches_inbound_session_from( std::size_t id_key_length = their_identity_key_length; if (olm::decode_base64_length(id_key_length) != olm::KEY_LENGTH) { - from_c(session)->last_error = olm::ErrorCode::INVALID_BASE64; + from_c(session)->last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::Curve25519PublicKey identity_key; @@ -671,7 +671,7 @@ size_t olm_remove_one_time_keys( from_c(session)->bob_one_time_key ); if (result == std::size_t(-1)) { - from_c(account)->last_error = olm::ErrorCode::BAD_MESSAGE_KEY_ID; + from_c(account)->last_error = OlmErrorCode::OLM_BAD_MESSAGE_KEY_ID; } return result; } @@ -712,7 +712,7 @@ size_t olm_encrypt( ); if (message_length < b64_output_length(raw_length)) { from_c(session)->last_error = - olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } std::size_t result = from_c(session)->encrypt( @@ -779,7 +779,7 @@ size_t olm_sha256( std::size_t raw_length = from_c(utility)->sha256_length(); if (output_length < b64_output_length(raw_length)) { from_c(utility)->last_error = - olm::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; + OlmErrorCode::OLM_OUTPUT_BUFFER_TOO_SMALL; return std::size_t(-1); } std::size_t result = from_c(utility)->sha256( @@ -800,7 +800,7 @@ size_t olm_ed25519_verify( void * signature, size_t signature_length ) { if (olm::decode_base64_length(key_length) != olm::KEY_LENGTH) { - from_c(utility)->last_error = olm::ErrorCode::INVALID_BASE64; + from_c(utility)->last_error = OlmErrorCode::OLM_INVALID_BASE64; return std::size_t(-1); } olm::Ed25519PublicKey verify_key; -- 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/olm.cpp | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) (limited to 'src/olm.cpp') diff --git a/src/olm.cpp b/src/olm.cpp index 56bb11f..9d84758 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -15,9 +15,9 @@ #include "olm/olm.h" #include "olm/session.hh" #include "olm/account.hh" +#include "olm/cipher.h" #include "olm/utility.hh" #include "olm/base64.hh" -#include "olm/cipher.hh" #include "olm/memory.hh" #include @@ -59,15 +59,24 @@ static std::uint8_t const * from_c(void const * bytes) { static const std::uint8_t CIPHER_KDF_INFO[] = "Pickle"; -static const olm::CipherAesSha256 PICKLE_CIPHER( - CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) -1 -); +const olm_cipher *get_pickle_cipher() { + static olm_cipher *cipher = NULL; + static olm_cipher_aes_sha_256 PICKLE_CIPHER; + if (!cipher) { + cipher = olm_cipher_aes_sha_256_init( + &PICKLE_CIPHER, + CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) - 1 + ); + } + return cipher; +} std::size_t enc_output_length( size_t raw_length ) { - std::size_t length = PICKLE_CIPHER.encrypt_ciphertext_length(raw_length); - length += PICKLE_CIPHER.mac_length(); + auto *cipher = get_pickle_cipher(); + std::size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length); + length += cipher->ops->mac_length(cipher); return olm::encode_base64_length(length); } @@ -76,8 +85,9 @@ std::uint8_t * enc_output_pos( std::uint8_t * output, size_t raw_length ) { - std::size_t length = PICKLE_CIPHER.encrypt_ciphertext_length(raw_length); - length += PICKLE_CIPHER.mac_length(); + auto *cipher = get_pickle_cipher(); + std::size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length); + length += cipher->ops->mac_length(cipher); return output + olm::encode_base64_length(length) - length; } @@ -85,13 +95,15 @@ std::size_t enc_output( std::uint8_t const * key, std::size_t key_length, std::uint8_t * output, size_t raw_length ) { - std::size_t ciphertext_length = PICKLE_CIPHER.encrypt_ciphertext_length( - raw_length + auto *cipher = get_pickle_cipher(); + std::size_t ciphertext_length = cipher->ops->encrypt_ciphertext_length( + cipher, raw_length ); - std::size_t length = ciphertext_length + PICKLE_CIPHER.mac_length(); + std::size_t length = ciphertext_length + cipher->ops->mac_length(cipher); std::size_t base64_length = olm::encode_base64_length(length); std::uint8_t * raw_output = output + base64_length - length; - PICKLE_CIPHER.encrypt( + cipher->ops->encrypt( + cipher, key, key_length, raw_output, raw_length, raw_output, ciphertext_length, @@ -112,8 +124,10 @@ std::size_t enc_input( return std::size_t(-1); } olm::decode_base64(input, b64_length, input); - std::size_t raw_length = enc_length - PICKLE_CIPHER.mac_length(); - std::size_t result = PICKLE_CIPHER.decrypt( + auto *cipher = get_pickle_cipher(); + std::size_t raw_length = enc_length - cipher->ops->mac_length(cipher); + std::size_t result = cipher->ops->decrypt( + cipher, key, key_length, input, enc_length, input, raw_length, -- 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/olm.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/olm.cpp') diff --git a/src/olm.cpp b/src/olm.cpp index 9d84758..b34a1dc 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -59,11 +59,11 @@ static std::uint8_t const * from_c(void const * bytes) { static const std::uint8_t CIPHER_KDF_INFO[] = "Pickle"; -const olm_cipher *get_pickle_cipher() { - static olm_cipher *cipher = NULL; - static olm_cipher_aes_sha_256 PICKLE_CIPHER; +const _olm_cipher *get_pickle_cipher() { + static _olm_cipher *cipher = NULL; + static _olm_cipher_aes_sha_256 PICKLE_CIPHER; if (!cipher) { - cipher = olm_cipher_aes_sha_256_init( + cipher = _olm_cipher_aes_sha_256_init( &PICKLE_CIPHER, CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) - 1 ); -- 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/olm.cpp | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) (limited to 'src/olm.cpp') diff --git a/src/olm.cpp b/src/olm.cpp index b34a1dc..fcd033a 100644 --- a/src/olm.cpp +++ b/src/olm.cpp @@ -57,24 +57,13 @@ static std::uint8_t const * from_c(void const * bytes) { return reinterpret_cast(bytes); } -static const std::uint8_t CIPHER_KDF_INFO[] = "Pickle"; - -const _olm_cipher *get_pickle_cipher() { - static _olm_cipher *cipher = NULL; - static _olm_cipher_aes_sha_256 PICKLE_CIPHER; - if (!cipher) { - cipher = _olm_cipher_aes_sha_256_init( - &PICKLE_CIPHER, - CIPHER_KDF_INFO, sizeof(CIPHER_KDF_INFO) - 1 - ); - } - return cipher; -} +static const struct _olm_cipher_aes_sha_256 PICKLE_CIPHER = + OLM_CIPHER_INIT_AES_SHA_256("Pickle"); std::size_t enc_output_length( size_t raw_length ) { - auto *cipher = get_pickle_cipher(); + auto *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER); std::size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length); length += cipher->ops->mac_length(cipher); return olm::encode_base64_length(length); @@ -85,7 +74,7 @@ std::uint8_t * enc_output_pos( std::uint8_t * output, size_t raw_length ) { - auto *cipher = get_pickle_cipher(); + auto *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER); std::size_t length = cipher->ops->encrypt_ciphertext_length(cipher, raw_length); length += cipher->ops->mac_length(cipher); return output + olm::encode_base64_length(length) - length; @@ -95,7 +84,7 @@ std::size_t enc_output( std::uint8_t const * key, std::size_t key_length, std::uint8_t * output, size_t raw_length ) { - auto *cipher = get_pickle_cipher(); + auto *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER); std::size_t ciphertext_length = cipher->ops->encrypt_ciphertext_length( cipher, raw_length ); @@ -124,7 +113,7 @@ std::size_t enc_input( return std::size_t(-1); } olm::decode_base64(input, b64_length, input); - auto *cipher = get_pickle_cipher(); + auto *cipher = OLM_CIPHER_BASE(&PICKLE_CIPHER); std::size_t raw_length = enc_length - cipher->ops->mac_length(cipher); std::size_t result = cipher->ops->decrypt( cipher, -- cgit v1.2.3