From 09d4125ff164f5ca686d12ccb0790c35ce721a6b Mon Sep 17 00:00:00 2001 From: Matthew Hodgson Date: Sat, 27 Jun 2015 01:15:23 +0200 Subject: Rename axolotlpp as olm to avoid confusion with Axolotl-the-spec and Axolotl-the-OWS-libraries at moxie's request --- tests/test_axolotl.cpp | 269 ------------------------------------------------- tests/test_base64.cpp | 10 +- tests/test_crypto.cpp | 44 ++++---- tests/test_list.cpp | 6 +- tests/test_message.cpp | 12 +-- tests/test_olm.cpp | 269 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_ratchet.cpp | 36 +++---- 7 files changed, 323 insertions(+), 323 deletions(-) delete mode 100644 tests/test_axolotl.cpp create mode 100644 tests/test_olm.cpp (limited to 'tests') diff --git a/tests/test_axolotl.cpp b/tests/test_axolotl.cpp deleted file mode 100644 index e8bfcc8..0000000 --- a/tests/test_axolotl.cpp +++ /dev/null @@ -1,269 +0,0 @@ -#include "axolotl/axolotl.hh" -#include "unittest.hh" - -#include -#include -#include - -struct MockRandom { - MockRandom(std::uint8_t tag, std::uint8_t offset = 0) - : tag(tag), current(offset) {} - void operator()( - std::uint8_t * bytes, std::size_t length - ) { - while (length > 32) { - bytes[0] = tag; - std::memset(bytes + 1, current, 31); - length -= 32; - bytes += 32; - current += 1; - } - if (length) { - bytes[0] = tag; - std::memset(bytes + 1, current, length - 1); - current += 1; - } - } - std::uint8_t tag; - std::uint8_t current; -}; - -int main() { - -{ /** Pickle account test */ - -TestCase test_case("Pickle account test"); -MockRandom mock_random('P'); - - -std::uint8_t account_buffer[::axolotl_account_size()]; -::AxolotlAccount *account = ::axolotl_account(account_buffer); -std::uint8_t random[::axolotl_create_account_random_length(account)]; -mock_random(random, sizeof(random)); -::axolotl_create_account(account, random, sizeof(random)); -std::size_t pickle_length = ::axolotl_pickle_account_length(account); -std::uint8_t pickle1[pickle_length]; -::axolotl_pickle_account(account, "secret_key", 10, pickle1, pickle_length); -std::uint8_t pickle2[pickle_length]; -std::memcpy(pickle2, pickle1, pickle_length); - -std::uint8_t account_buffer2[::axolotl_account_size()]; -::AxolotlAccount *account2 = ::axolotl_account(account_buffer2); -::axolotl_unpickle_account(account2, "secret_key", 10, pickle2, pickle_length); -assert_equals(pickle_length, ::axolotl_pickle_account_length(account2)); -::axolotl_pickle_account(account2, "secret_key", 10, pickle2, pickle_length); - -assert_equals(pickle1, pickle2, pickle_length); - -} - -{ /** Loopback test */ - -TestCase test_case("Loopback test"); -MockRandom mock_random_a('A', 0x00); -MockRandom mock_random_b('B', 0x80); - -std::uint8_t a_account_buffer[::axolotl_account_size()]; -::AxolotlAccount *a_account = ::axolotl_account(a_account_buffer); -std::uint8_t a_random[::axolotl_create_account_random_length(a_account)]; -mock_random_a(a_random, sizeof(a_random)); -::axolotl_create_account(a_account, a_random, sizeof(a_random)); - -std::uint8_t b_account_buffer[::axolotl_account_size()]; -::AxolotlAccount *b_account = ::axolotl_account(b_account_buffer); -std::uint8_t b_random[::axolotl_create_account_random_length(b_account)]; -mock_random_b(b_random, sizeof(b_random)); -::axolotl_create_account(b_account, b_random, sizeof(b_random)); - -std::uint8_t b_id_keys[::axolotl_account_identity_keys_length(b_account)]; -std::uint8_t b_ot_keys[::axolotl_account_one_time_keys_length(b_account)]; -::axolotl_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys)); -::axolotl_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys)); - -std::uint8_t a_session_buffer[::axolotl_session_size()]; -::AxolotlSession *a_session = ::axolotl_session(a_session_buffer); -std::uint8_t a_rand[::axolotl_create_outbound_session_random_length(a_session)]; -mock_random_a(a_rand, sizeof(a_rand)); -assert_not_equals(std::size_t(-1), ::axolotl_create_outbound_session( - a_session, a_account, - b_id_keys + 14, 43, - ::atol((char *)(b_ot_keys + 62)), b_ot_keys + 74, 43, - a_rand, sizeof(a_rand) -)); - -std::uint8_t plaintext[] = "Hello, World"; -std::uint8_t message_1[::axolotl_encrypt_message_length(a_session, 12)]; -std::uint8_t a_message_random[::axolotl_encrypt_random_length(a_session)]; -mock_random_a(a_message_random, sizeof(a_message_random)); -assert_equals(std::size_t(0), ::axolotl_encrypt_message_type(a_session)); -assert_not_equals(std::size_t(-1), ::axolotl_encrypt( - a_session, - plaintext, 12, - a_message_random, sizeof(a_message_random), - message_1, sizeof(message_1) -)); - - -std::uint8_t tmp_message_1[sizeof(message_1)]; -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -std::uint8_t b_session_buffer[::axolotl_account_size()]; -::AxolotlSession *b_session = ::axolotl_session(b_session_buffer); -::axolotl_create_inbound_session( - b_session, b_account, tmp_message_1, sizeof(message_1) -); - -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -std::uint8_t plaintext_1[::axolotl_decrypt_max_plaintext_length( - b_session, 0, tmp_message_1, sizeof(message_1) -)]; -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -assert_equals(std::size_t(12), ::axolotl_decrypt( - b_session, 0, - tmp_message_1, sizeof(message_1), - plaintext_1, sizeof(plaintext_1) -)); - -assert_equals(plaintext, plaintext_1, 12); - -std::uint8_t message_2[::axolotl_encrypt_message_length(b_session, 12)]; -std::uint8_t b_message_random[::axolotl_encrypt_random_length(b_session)]; -mock_random_b(b_message_random, sizeof(b_message_random)); -assert_equals(std::size_t(1), ::axolotl_encrypt_message_type(b_session)); -assert_not_equals(std::size_t(-1), ::axolotl_encrypt( - b_session, - plaintext, 12, - b_message_random, sizeof(b_message_random), - message_2, sizeof(message_2) -)); - -std::uint8_t tmp_message_2[sizeof(message_2)]; -std::memcpy(tmp_message_2, message_2, sizeof(message_2)); -std::uint8_t plaintext_2[::axolotl_decrypt_max_plaintext_length( - a_session, 1, tmp_message_2, sizeof(message_2) -)]; -std::memcpy(tmp_message_2, message_2, sizeof(message_2)); -assert_equals(std::size_t(12), ::axolotl_decrypt( - a_session, 1, - tmp_message_2, sizeof(message_2), - plaintext_2, sizeof(plaintext_2) -)); - -assert_equals(plaintext, plaintext_2, 12); - -std::memcpy(tmp_message_2, message_2, sizeof(message_2)); -assert_equals(std::size_t(-1), ::axolotl_decrypt( - a_session, 1, - tmp_message_2, sizeof(message_2), - plaintext_2, sizeof(plaintext_2) -)); - -} - -{ /** More messages test */ - -TestCase test_case("More messages test"); -MockRandom mock_random_a('A', 0x00); -MockRandom mock_random_b('B', 0x80); - -std::uint8_t a_account_buffer[::axolotl_account_size()]; -::AxolotlAccount *a_account = ::axolotl_account(a_account_buffer); -std::uint8_t a_random[::axolotl_create_account_random_length(a_account)]; -mock_random_a(a_random, sizeof(a_random)); -::axolotl_create_account(a_account, a_random, sizeof(a_random)); - -std::uint8_t b_account_buffer[::axolotl_account_size()]; -::AxolotlAccount *b_account = ::axolotl_account(b_account_buffer); -std::uint8_t b_random[::axolotl_create_account_random_length(b_account)]; -mock_random_b(b_random, sizeof(b_random)); -::axolotl_create_account(b_account, b_random, sizeof(b_random)); - -std::uint8_t b_id_keys[::axolotl_account_identity_keys_length(b_account)]; -std::uint8_t b_ot_keys[::axolotl_account_one_time_keys_length(b_account)]; -::axolotl_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys)); -::axolotl_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys)); - -std::uint8_t a_session_buffer[::axolotl_session_size()]; -::AxolotlSession *a_session = ::axolotl_session(a_session_buffer); -std::uint8_t a_rand[::axolotl_create_outbound_session_random_length(a_session)]; -mock_random_a(a_rand, sizeof(a_rand)); -assert_not_equals(std::size_t(-1), ::axolotl_create_outbound_session( - a_session, a_account, - b_id_keys + 14, 43, - ::atol((char *)(b_ot_keys + 62)), b_ot_keys + 74, 43, - a_rand, sizeof(a_rand) -)); - -std::uint8_t plaintext[] = "Hello, World"; -std::uint8_t message_1[::axolotl_encrypt_message_length(a_session, 12)]; -std::uint8_t a_message_random[::axolotl_encrypt_random_length(a_session)]; -mock_random_a(a_message_random, sizeof(a_message_random)); -assert_equals(std::size_t(0), ::axolotl_encrypt_message_type(a_session)); -assert_not_equals(std::size_t(-1), ::axolotl_encrypt( - a_session, - plaintext, 12, - a_message_random, sizeof(a_message_random), - message_1, sizeof(message_1) -)); - -std::uint8_t tmp_message_1[sizeof(message_1)]; -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -std::uint8_t b_session_buffer[::axolotl_account_size()]; -::AxolotlSession *b_session = ::axolotl_session(b_session_buffer); -::axolotl_create_inbound_session( - b_session, b_account, tmp_message_1, sizeof(message_1) -); - -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -std::uint8_t plaintext_1[::axolotl_decrypt_max_plaintext_length( - b_session, 0, tmp_message_1, sizeof(message_1) -)]; -std::memcpy(tmp_message_1, message_1, sizeof(message_1)); -assert_equals(std::size_t(12), ::axolotl_decrypt( - b_session, 0, - tmp_message_1, sizeof(message_1), - plaintext_1, sizeof(plaintext_1) -)); - -for (unsigned i = 0; i < 8; ++i) { - { - std::uint8_t msg_a[::axolotl_encrypt_message_length(a_session, 12)]; - std::uint8_t rnd_a[::axolotl_encrypt_random_length(a_session)]; - mock_random_a(rnd_a, sizeof(rnd_a)); - std::size_t type_a = ::axolotl_encrypt_message_type(a_session); - assert_not_equals(std::size_t(-1), ::axolotl_encrypt( - a_session, plaintext, 12, rnd_a, sizeof(rnd_a), msg_a, sizeof(msg_a) - )); - - std::uint8_t tmp_a[sizeof(msg_a)]; - std::memcpy(tmp_a, msg_a, sizeof(msg_a)); - std::uint8_t out_a[::axolotl_decrypt_max_plaintext_length( - b_session, type_a, tmp_a, sizeof(tmp_a) - )]; - std::memcpy(tmp_a, msg_a, sizeof(msg_a)); - assert_equals(std::size_t(12), ::axolotl_decrypt( - b_session, type_a, msg_a, sizeof(msg_a), out_a, sizeof(out_a) - )); - } - { - std::uint8_t msg_b[::axolotl_encrypt_message_length(b_session, 12)]; - std::uint8_t rnd_b[::axolotl_encrypt_random_length(b_session)]; - mock_random_b(rnd_b, sizeof(rnd_b)); - std::size_t type_b = ::axolotl_encrypt_message_type(b_session); - assert_not_equals(std::size_t(-1), ::axolotl_encrypt( - b_session, plaintext, 12, rnd_b, sizeof(rnd_b), msg_b, sizeof(msg_b) - )); - - std::uint8_t tmp_b[sizeof(msg_b)]; - std::memcpy(tmp_b, msg_b, sizeof(msg_b)); - std::uint8_t out_b[::axolotl_decrypt_max_plaintext_length( - a_session, type_b, tmp_b, sizeof(tmp_b) - )]; - std::memcpy(tmp_b, msg_b, sizeof(msg_b)); - assert_equals(std::size_t(12), ::axolotl_decrypt( - a_session, type_b, msg_b, sizeof(msg_b), out_b, sizeof(out_b) - )); - } -} -} - -} diff --git a/tests/test_base64.cpp b/tests/test_base64.cpp index fbd9e19..e6b3710 100644 --- a/tests/test_base64.cpp +++ b/tests/test_base64.cpp @@ -1,4 +1,4 @@ -#include "axolotl/base64.hh" +#include "olm/base64.hh" #include "unittest.hh" int main() { @@ -10,11 +10,11 @@ std::uint8_t input[] = "Hello World"; std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ"; std::size_t input_length = sizeof(input) - 1; -std::size_t output_length = axolotl::encode_base64_length(input_length); +std::size_t output_length = olm::encode_base64_length(input_length); assert_equals(std::size_t(15), output_length); std::uint8_t output[output_length]; -axolotl::encode_base64(input, input_length, output); +olm::encode_base64(input, input_length, output); assert_equals(expected_output, output, output_length); } @@ -25,11 +25,11 @@ std::uint8_t input[] = "SGVsbG8gV29ybGQ"; std::uint8_t expected_output[] = "Hello World"; std::size_t input_length = sizeof(input) - 1; -std::size_t output_length = axolotl::decode_base64_length(input_length); +std::size_t output_length = olm::decode_base64_length(input_length); assert_equals(std::size_t(11), output_length); std::uint8_t output[output_length]; -axolotl::decode_base64(input, input_length, output); +olm::decode_base64(input, input_length, output); assert_equals(expected_output, output, output_length); } diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp index 3ec5360..9b7637b 100644 --- a/tests/test_crypto.cpp +++ b/tests/test_crypto.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "axolotl/crypto.hh" +#include "olm/crypto.hh" #include "unittest.hh" @@ -58,25 +58,25 @@ std::uint8_t expected_agreement[32] = { 0x76, 0xF0, 0x9B, 0x3C, 0x1E, 0x16, 0x17, 0x42 }; -axolotl::Curve25519KeyPair alice_pair; -axolotl::generate_key(alice_private, alice_pair); +olm::Curve25519KeyPair alice_pair; +olm::generate_key(alice_private, alice_pair); assert_equals(alice_private, alice_pair.private_key, 32); assert_equals(alice_public, alice_pair.public_key, 32); -axolotl::Curve25519KeyPair bob_pair; -axolotl::generate_key(bob_private, bob_pair); +olm::Curve25519KeyPair bob_pair; +olm::generate_key(bob_private, bob_pair); assert_equals(bob_private, bob_pair.private_key, 32); assert_equals(bob_public, bob_pair.public_key, 32); -std::uint8_t actual_agreement[axolotl::CURVE25519_SHARED_SECRET_LENGTH] = {}; +std::uint8_t actual_agreement[olm::CURVE25519_SHARED_SECRET_LENGTH] = {}; -axolotl::curve25519_shared_secret(alice_pair, bob_pair, actual_agreement); +olm::curve25519_shared_secret(alice_pair, bob_pair, actual_agreement); assert_equals(expected_agreement, actual_agreement, 32); -axolotl::curve25519_shared_secret(bob_pair, alice_pair, actual_agreement); +olm::curve25519_shared_secret(bob_pair, alice_pair, actual_agreement); assert_equals(expected_agreement, actual_agreement, 32); @@ -90,22 +90,22 @@ std::uint8_t private_key[33] = "This key is a string of 32 bytes"; std::uint8_t message[] = "message"; std::size_t message_length = sizeof(message) - 1; -axolotl::Curve25519KeyPair key_pair; -axolotl::generate_key(private_key, key_pair); +olm::Curve25519KeyPair key_pair; +olm::generate_key(private_key, key_pair); std::uint8_t signature[64]; -axolotl::curve25519_sign( +olm::curve25519_sign( key_pair, message, message_length, signature ); -bool result = axolotl::curve25519_verify( +bool result = olm::curve25519_verify( key_pair, message, message_length, signature ); assert_equals(true, result); message[0] = 'n'; -result = axolotl::curve25519_verify( +result = olm::curve25519_verify( key_pair, message, message_length, signature ); assert_equals(false, result); @@ -116,8 +116,8 @@ assert_equals(false, result); TestCase test_case("AES Test Case 1"); -axolotl::Aes256Key key = {}; -axolotl::Aes256Iv iv = {}; +olm::Aes256Key key = {}; +olm::Aes256Iv iv = {}; std::uint8_t input[16] = {}; std::uint8_t expected[32] = { @@ -127,16 +127,16 @@ std::uint8_t expected[32] = { 0x4B, 0xAE, 0xDF, 0xFC, 0x3D, 0x21, 0x4C, 0x38 }; -std::size_t length = axolotl::aes_encrypt_cbc_length(sizeof(input)); +std::size_t length = olm::aes_encrypt_cbc_length(sizeof(input)); assert_equals(std::size_t(32), length); std::uint8_t actual[32] = {}; -axolotl::aes_encrypt_cbc(key, iv, input, sizeof(input), actual); +olm::aes_encrypt_cbc(key, iv, input, sizeof(input), actual); assert_equals(expected, actual, 32); -length = axolotl::aes_decrypt_cbc(key, iv, expected, sizeof(expected), actual); +length = olm::aes_decrypt_cbc(key, iv, expected, sizeof(expected), actual); assert_equals(std::size_t(16), length); assert_equals(input, actual, length); @@ -158,7 +158,7 @@ std::uint8_t expected[32] = { std::uint8_t actual[32]; -axolotl::sha256(input, sizeof(input), actual); +olm::sha256(input, sizeof(input), actual); assert_equals(expected, actual, 32); @@ -179,7 +179,7 @@ std::uint8_t expected[32] = { std::uint8_t actual[32]; -axolotl::hmac_sha256(input, sizeof(input), input, sizeof(input), actual); +olm::hmac_sha256(input, sizeof(input), input, sizeof(input), actual); assert_equals(expected, actual, 32); @@ -214,7 +214,7 @@ std::uint8_t hmac_expected_output[32] = { std::uint8_t hmac_actual_output[32] = {}; -axolotl::hmac_sha256( +olm::hmac_sha256( salt, sizeof(salt), input, sizeof(input), hmac_actual_output @@ -233,7 +233,7 @@ std::uint8_t hkdf_expected_output[42] = { std::uint8_t hkdf_actual_output[42] = {}; -axolotl::hkdf_sha256( +olm::hkdf_sha256( input, sizeof(input), salt, sizeof(salt), info, sizeof(info), diff --git a/tests/test_list.cpp b/tests/test_list.cpp index 2cf23dc..c6d9a9a 100644 --- a/tests/test_list.cpp +++ b/tests/test_list.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "axolotl/list.hh" +#include "olm/list.hh" #include "unittest.hh" int main() { @@ -21,7 +21,7 @@ int main() { TestCase test_case("List insert"); -axolotl::List test_list; +olm::List test_list; assert_equals(std::size_t(0), test_list.size()); @@ -47,7 +47,7 @@ assert_equals(4, test_list[3]); { /** List erase test **/ TestCase test_case("List erase"); -axolotl::List test_list; +olm::List test_list; assert_equals(std::size_t(0), test_list.size()); for (int i = 0; i < 4; ++i) { diff --git a/tests/test_message.cpp b/tests/test_message.cpp index b46b15a..ff14649 100644 --- a/tests/test_message.cpp +++ b/tests/test_message.cpp @@ -12,7 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "axolotl/message.hh" +#include "olm/message.hh" #include "unittest.hh" int main() { @@ -27,8 +27,8 @@ std::uint8_t hmacsha2[9] = "hmacsha2"; TestCase test_case("Message decode test"); -axolotl::MessageReader reader; -axolotl::decode_message(reader, message1, 35, 8); +olm::MessageReader reader; +olm::decode_message(reader, message1, 35, 8); assert_equals(std::uint8_t(3), reader.version); assert_equals(true, reader.has_counter); @@ -46,13 +46,13 @@ assert_equals(ciphertext, reader.ciphertext, 10); TestCase test_case("Message encode test"); -std::size_t length = axolotl::encode_message_length(1, 10, 10, 8); +std::size_t length = olm::encode_message_length(1, 10, 10, 8); assert_equals(std::size_t(35), length); std::uint8_t output[length]; -axolotl::MessageWriter writer; -axolotl::encode_message(writer, 3, 1, 10, 10, output); +olm::MessageWriter writer; +olm::encode_message(writer, 3, 1, 10, 10, output); std::memcpy(writer.ratchet_key, ratchetkey, 10); std::memcpy(writer.ciphertext, ciphertext, 10); diff --git a/tests/test_olm.cpp b/tests/test_olm.cpp new file mode 100644 index 0000000..74d19e7 --- /dev/null +++ b/tests/test_olm.cpp @@ -0,0 +1,269 @@ +#include "olm/olm.hh" +#include "unittest.hh" + +#include +#include +#include + +struct MockRandom { + MockRandom(std::uint8_t tag, std::uint8_t offset = 0) + : tag(tag), current(offset) {} + void operator()( + std::uint8_t * bytes, std::size_t length + ) { + while (length > 32) { + bytes[0] = tag; + std::memset(bytes + 1, current, 31); + length -= 32; + bytes += 32; + current += 1; + } + if (length) { + bytes[0] = tag; + std::memset(bytes + 1, current, length - 1); + current += 1; + } + } + std::uint8_t tag; + std::uint8_t current; +}; + +int main() { + +{ /** Pickle account test */ + +TestCase test_case("Pickle account test"); +MockRandom mock_random('P'); + + +std::uint8_t account_buffer[::olm_account_size()]; +::OlmAccount *account = ::olm_account(account_buffer); +std::uint8_t random[::olm_create_account_random_length(account)]; +mock_random(random, sizeof(random)); +::olm_create_account(account, random, sizeof(random)); +std::size_t pickle_length = ::olm_pickle_account_length(account); +std::uint8_t pickle1[pickle_length]; +::olm_pickle_account(account, "secret_key", 10, pickle1, pickle_length); +std::uint8_t pickle2[pickle_length]; +std::memcpy(pickle2, pickle1, pickle_length); + +std::uint8_t account_buffer2[::olm_account_size()]; +::OlmAccount *account2 = ::olm_account(account_buffer2); +::olm_unpickle_account(account2, "secret_key", 10, pickle2, pickle_length); +assert_equals(pickle_length, ::olm_pickle_account_length(account2)); +::olm_pickle_account(account2, "secret_key", 10, pickle2, pickle_length); + +assert_equals(pickle1, pickle2, pickle_length); + +} + +{ /** Loopback test */ + +TestCase test_case("Loopback test"); +MockRandom mock_random_a('A', 0x00); +MockRandom mock_random_b('B', 0x80); + +std::uint8_t a_account_buffer[::olm_account_size()]; +::OlmAccount *a_account = ::olm_account(a_account_buffer); +std::uint8_t a_random[::olm_create_account_random_length(a_account)]; +mock_random_a(a_random, sizeof(a_random)); +::olm_create_account(a_account, a_random, sizeof(a_random)); + +std::uint8_t b_account_buffer[::olm_account_size()]; +::OlmAccount *b_account = ::olm_account(b_account_buffer); +std::uint8_t b_random[::olm_create_account_random_length(b_account)]; +mock_random_b(b_random, sizeof(b_random)); +::olm_create_account(b_account, b_random, sizeof(b_random)); + +std::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)]; +std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)]; +::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys)); +::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys)); + +std::uint8_t a_session_buffer[::olm_session_size()]; +::OlmSession *a_session = ::olm_session(a_session_buffer); +std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)]; +mock_random_a(a_rand, sizeof(a_rand)); +assert_not_equals(std::size_t(-1), ::olm_create_outbound_session( + a_session, a_account, + b_id_keys + 14, 43, + ::atol((char *)(b_ot_keys + 62)), b_ot_keys + 74, 43, + a_rand, sizeof(a_rand) +)); + +std::uint8_t plaintext[] = "Hello, World"; +std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)]; +std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)]; +mock_random_a(a_message_random, sizeof(a_message_random)); +assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session)); +assert_not_equals(std::size_t(-1), ::olm_encrypt( + a_session, + plaintext, 12, + a_message_random, sizeof(a_message_random), + message_1, sizeof(message_1) +)); + + +std::uint8_t tmp_message_1[sizeof(message_1)]; +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +std::uint8_t b_session_buffer[::olm_account_size()]; +::OlmSession *b_session = ::olm_session(b_session_buffer); +::olm_create_inbound_session( + b_session, b_account, tmp_message_1, sizeof(message_1) +); + +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length( + b_session, 0, tmp_message_1, sizeof(message_1) +)]; +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +assert_equals(std::size_t(12), ::olm_decrypt( + b_session, 0, + tmp_message_1, sizeof(message_1), + plaintext_1, sizeof(plaintext_1) +)); + +assert_equals(plaintext, plaintext_1, 12); + +std::uint8_t message_2[::olm_encrypt_message_length(b_session, 12)]; +std::uint8_t b_message_random[::olm_encrypt_random_length(b_session)]; +mock_random_b(b_message_random, sizeof(b_message_random)); +assert_equals(std::size_t(1), ::olm_encrypt_message_type(b_session)); +assert_not_equals(std::size_t(-1), ::olm_encrypt( + b_session, + plaintext, 12, + b_message_random, sizeof(b_message_random), + message_2, sizeof(message_2) +)); + +std::uint8_t tmp_message_2[sizeof(message_2)]; +std::memcpy(tmp_message_2, message_2, sizeof(message_2)); +std::uint8_t plaintext_2[::olm_decrypt_max_plaintext_length( + a_session, 1, tmp_message_2, sizeof(message_2) +)]; +std::memcpy(tmp_message_2, message_2, sizeof(message_2)); +assert_equals(std::size_t(12), ::olm_decrypt( + a_session, 1, + tmp_message_2, sizeof(message_2), + plaintext_2, sizeof(plaintext_2) +)); + +assert_equals(plaintext, plaintext_2, 12); + +std::memcpy(tmp_message_2, message_2, sizeof(message_2)); +assert_equals(std::size_t(-1), ::olm_decrypt( + a_session, 1, + tmp_message_2, sizeof(message_2), + plaintext_2, sizeof(plaintext_2) +)); + +} + +{ /** More messages test */ + +TestCase test_case("More messages test"); +MockRandom mock_random_a('A', 0x00); +MockRandom mock_random_b('B', 0x80); + +std::uint8_t a_account_buffer[::olm_account_size()]; +::OlmAccount *a_account = ::olm_account(a_account_buffer); +std::uint8_t a_random[::olm_create_account_random_length(a_account)]; +mock_random_a(a_random, sizeof(a_random)); +::olm_create_account(a_account, a_random, sizeof(a_random)); + +std::uint8_t b_account_buffer[::olm_account_size()]; +::OlmAccount *b_account = ::olm_account(b_account_buffer); +std::uint8_t b_random[::olm_create_account_random_length(b_account)]; +mock_random_b(b_random, sizeof(b_random)); +::olm_create_account(b_account, b_random, sizeof(b_random)); + +std::uint8_t b_id_keys[::olm_account_identity_keys_length(b_account)]; +std::uint8_t b_ot_keys[::olm_account_one_time_keys_length(b_account)]; +::olm_account_identity_keys(b_account, b_id_keys, sizeof(b_id_keys)); +::olm_account_one_time_keys(b_account, b_ot_keys, sizeof(b_ot_keys)); + +std::uint8_t a_session_buffer[::olm_session_size()]; +::OlmSession *a_session = ::olm_session(a_session_buffer); +std::uint8_t a_rand[::olm_create_outbound_session_random_length(a_session)]; +mock_random_a(a_rand, sizeof(a_rand)); +assert_not_equals(std::size_t(-1), ::olm_create_outbound_session( + a_session, a_account, + b_id_keys + 14, 43, + ::atol((char *)(b_ot_keys + 62)), b_ot_keys + 74, 43, + a_rand, sizeof(a_rand) +)); + +std::uint8_t plaintext[] = "Hello, World"; +std::uint8_t message_1[::olm_encrypt_message_length(a_session, 12)]; +std::uint8_t a_message_random[::olm_encrypt_random_length(a_session)]; +mock_random_a(a_message_random, sizeof(a_message_random)); +assert_equals(std::size_t(0), ::olm_encrypt_message_type(a_session)); +assert_not_equals(std::size_t(-1), ::olm_encrypt( + a_session, + plaintext, 12, + a_message_random, sizeof(a_message_random), + message_1, sizeof(message_1) +)); + +std::uint8_t tmp_message_1[sizeof(message_1)]; +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +std::uint8_t b_session_buffer[::olm_account_size()]; +::OlmSession *b_session = ::olm_session(b_session_buffer); +::olm_create_inbound_session( + b_session, b_account, tmp_message_1, sizeof(message_1) +); + +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +std::uint8_t plaintext_1[::olm_decrypt_max_plaintext_length( + b_session, 0, tmp_message_1, sizeof(message_1) +)]; +std::memcpy(tmp_message_1, message_1, sizeof(message_1)); +assert_equals(std::size_t(12), ::olm_decrypt( + b_session, 0, + tmp_message_1, sizeof(message_1), + plaintext_1, sizeof(plaintext_1) +)); + +for (unsigned i = 0; i < 8; ++i) { + { + std::uint8_t msg_a[::olm_encrypt_message_length(a_session, 12)]; + std::uint8_t rnd_a[::olm_encrypt_random_length(a_session)]; + mock_random_a(rnd_a, sizeof(rnd_a)); + std::size_t type_a = ::olm_encrypt_message_type(a_session); + assert_not_equals(std::size_t(-1), ::olm_encrypt( + a_session, plaintext, 12, rnd_a, sizeof(rnd_a), msg_a, sizeof(msg_a) + )); + + std::uint8_t tmp_a[sizeof(msg_a)]; + std::memcpy(tmp_a, msg_a, sizeof(msg_a)); + std::uint8_t out_a[::olm_decrypt_max_plaintext_length( + b_session, type_a, tmp_a, sizeof(tmp_a) + )]; + std::memcpy(tmp_a, msg_a, sizeof(msg_a)); + assert_equals(std::size_t(12), ::olm_decrypt( + b_session, type_a, msg_a, sizeof(msg_a), out_a, sizeof(out_a) + )); + } + { + std::uint8_t msg_b[::olm_encrypt_message_length(b_session, 12)]; + std::uint8_t rnd_b[::olm_encrypt_random_length(b_session)]; + mock_random_b(rnd_b, sizeof(rnd_b)); + std::size_t type_b = ::olm_encrypt_message_type(b_session); + assert_not_equals(std::size_t(-1), ::olm_encrypt( + b_session, plaintext, 12, rnd_b, sizeof(rnd_b), msg_b, sizeof(msg_b) + )); + + std::uint8_t tmp_b[sizeof(msg_b)]; + std::memcpy(tmp_b, msg_b, sizeof(msg_b)); + std::uint8_t out_b[::olm_decrypt_max_plaintext_length( + a_session, type_b, tmp_b, sizeof(tmp_b) + )]; + std::memcpy(tmp_b, msg_b, sizeof(msg_b)); + assert_equals(std::size_t(12), ::olm_decrypt( + a_session, type_b, msg_b, sizeof(msg_b), out_b, sizeof(out_b) + )); + } +} +} + +} diff --git a/tests/test_ratchet.cpp b/tests/test_ratchet.cpp index fbc5031..b2db8e7 100644 --- a/tests/test_ratchet.cpp +++ b/tests/test_ratchet.cpp @@ -12,37 +12,37 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include "axolotl/ratchet.hh" -#include "axolotl/cipher.hh" +#include "olm/ratchet.hh" +#include "olm/cipher.hh" #include "unittest.hh" int main() { -std::uint8_t root_info[] = "Axolotl"; -std::uint8_t ratchet_info[] = "AxolotlRatchet"; -std::uint8_t message_info[] = "AxolotlMessageKeys"; +std::uint8_t root_info[] = "Olm"; +std::uint8_t ratchet_info[] = "OlmRatchet"; +std::uint8_t message_info[] = "OlmMessageKeys"; -axolotl::KdfInfo kdf_info = { +olm::KdfInfo kdf_info = { root_info, sizeof(root_info) - 1, ratchet_info, sizeof(ratchet_info) - 1 }; -axolotl::CipherAesSha256 cipher( +olm::CipherAesSha256 cipher( message_info, sizeof(message_info) - 1 ); std::uint8_t random_bytes[] = "0123456789ABDEF0123456789ABCDEF"; -axolotl::Curve25519KeyPair alice_key; -axolotl::generate_key(random_bytes, alice_key); +olm::Curve25519KeyPair alice_key; +olm::generate_key(random_bytes, alice_key); std::uint8_t shared_secret[] = "A secret"; { /* Send/Receive test case */ -TestCase test_case("Axolotl Send/Receive"); +TestCase test_case("Olm Send/Receive"); -axolotl::Ratchet alice(kdf_info, cipher); -axolotl::Ratchet bob(kdf_info, cipher); +olm::Ratchet alice(kdf_info, cipher); +olm::Ratchet bob(kdf_info, cipher); alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key); @@ -108,10 +108,10 @@ std::size_t encrypt_length, decrypt_length; { /* Out of order test case */ -TestCase test_case("Axolotl Out of Order"); +TestCase test_case("Olm Out of Order"); -axolotl::Ratchet alice(kdf_info, cipher); -axolotl::Ratchet bob(kdf_info, cipher); +olm::Ratchet alice(kdf_info, cipher); +olm::Ratchet bob(kdf_info, cipher); alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key); @@ -180,10 +180,10 @@ std::size_t encrypt_length, decrypt_length; { /* More messages */ -TestCase test_case("Axolotl More Messages"); +TestCase test_case("Olm More Messages"); -axolotl::Ratchet alice(kdf_info, cipher); -axolotl::Ratchet bob(kdf_info, cipher); +olm::Ratchet alice(kdf_info, cipher); +olm::Ratchet bob(kdf_info, cipher); alice.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, alice_key); bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key); -- cgit v1.2.3