diff options
Diffstat (limited to 'src/account.cpp')
-rw-r--r-- | src/account.cpp | 63 |
1 files changed, 14 insertions, 49 deletions
diff --git a/src/account.cpp b/src/account.cpp index cf6f0cb..43033c8 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -15,6 +15,7 @@ #include "olm/account.hh" #include "olm/base64.hh" #include "olm/pickle.hh" +#include "olm/memory.hh" olm::Account::Account( ) : next_one_time_key_id(0), @@ -26,7 +27,7 @@ olm::OneTimeKey const * olm::Account::lookup_key( olm::Curve25519PublicKey const & public_key ) { for (olm::OneTimeKey const & key : one_time_keys) { - if (0 == memcmp(key.key.public_key, public_key.public_key, 32)) { + if (olm::array_equal(key.key.public_key, public_key.public_key)) { return &key; } } @@ -38,7 +39,7 @@ std::size_t olm::Account::remove_key( ) { OneTimeKey * i; for (i = one_time_keys.begin(); i != one_time_keys.end(); ++i) { - if (0 == memcmp(i->key.public_key, public_key.public_key, 32)) { + if (olm::array_equal(i->key.public_key, public_key.public_key)) { std::uint32_t id = i->id; one_time_keys.erase(i); return id; @@ -48,7 +49,7 @@ std::size_t olm::Account::remove_key( } std::size_t olm::Account::new_account_random_length() { - return 2 * 32; + return 2 * olm::KEY_LENGTH; } std::size_t olm::Account::new_account( @@ -60,35 +61,19 @@ std::size_t olm::Account::new_account( } olm::ed25519_generate_key(random, identity_keys.ed25519_key); - random += 32; + random += KEY_LENGTH; olm::curve25519_generate_key(random, identity_keys.curve25519_key); - random += 32; return 0; } namespace { - -namespace { uint8_t KEY_JSON_ED25519[] = "\"ed25519\":"; uint8_t KEY_JSON_CURVE25519[] = "\"curve25519\":"; -} - - -std::size_t count_digits( - std::uint64_t value -) { - std::size_t digits = 0; - do { - digits++; - value /= 10; - } while (value); - return digits; -} template<typename T> -std::uint8_t * write_string( +static std::uint8_t * write_string( std::uint8_t * pos, T const & value ) { @@ -96,27 +81,6 @@ std::uint8_t * write_string( return pos + (sizeof(T) - 1); } -std::uint8_t * write_string( - std::uint8_t * pos, - std::uint8_t const * value, std::size_t value_length -) { - std::memcpy(pos, value, value_length); - return pos + value_length; -} - -std::uint8_t * write_digits( - std::uint8_t * pos, - std::uint64_t value -) { - size_t digits = count_digits(value); - pos += digits; - do { - *(--pos) = '0' + (value % 10); - value /= 10; - } while (value); - return pos + digits; -} - } @@ -143,7 +107,6 @@ std::size_t olm::Account::get_identity_json( std::uint8_t * identity_json, std::size_t identity_json_length ) { std::uint8_t * pos = identity_json; - std::uint8_t signature[64]; size_t expected_length = get_identity_json_length(); if (identity_json_length < expected_length) { @@ -174,7 +137,7 @@ std::size_t olm::Account::get_identity_json( std::size_t olm::Account::signature_length( ) { - return 64; + return olm::SIGNATURE_LENGTH; } @@ -196,19 +159,20 @@ std::size_t olm::Account::sign( std::size_t olm::Account::get_one_time_keys_json_length( ) { std::size_t length = 0; + bool is_empty = true; for (auto const & key : one_time_keys) { if (key.published) { continue; } + is_empty = false; length += 2; /* {" */ length += olm::encode_base64_length(olm::pickle_length(key.id)); length += 3; /* ":" */ length += olm::encode_base64_length(sizeof(key.key.public_key)); length += 1; /* " */ } - if (length == 0) { - /* The list was empty. Add a byte for the opening '{' */ - length = 1; + if (is_empty) { + length += 1; /* { */ } length += 3; /* }{} */ length += sizeof(KEY_JSON_CURVE25519) - 1; @@ -244,6 +208,7 @@ std::size_t olm::Account::get_one_time_keys_json( sep = ','; } if (sep != ',') { + /* The list was empty */ *(pos++) = sep; } *(pos++) = '}'; @@ -273,7 +238,7 @@ std::size_t olm::Account::max_number_of_one_time_keys( std::size_t olm::Account::generate_one_time_keys_random_length( std::size_t number_of_keys ) { - return 32 * number_of_keys; + return olm::KEY_LENGTH * number_of_keys; } std::size_t olm::Account::generate_one_time_keys( @@ -289,7 +254,7 @@ std::size_t olm::Account::generate_one_time_keys( key.id = ++next_one_time_key_id; key.published = false; olm::curve25519_generate_key(random, key.key); - random += 32; + random += olm::KEY_LENGTH; } return number_of_keys; } |