diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_base64.cpp | 38 | ||||
-rw-r--r-- | tests/test_crypto.cpp | 87 | ||||
-rw-r--r-- | tests/test_group_session.cpp | 240 | ||||
-rw-r--r-- | tests/test_megolm.cpp | 134 | ||||
-rw-r--r-- | tests/test_message.cpp | 49 | ||||
-rw-r--r-- | tests/test_olm.cpp | 29 | ||||
-rw-r--r-- | tests/test_olm_decrypt.cpp | 43 | ||||
-rw-r--r-- | tests/test_olm_sha256.cpp | 2 | ||||
-rw-r--r-- | tests/test_olm_signature.cpp | 2 | ||||
-rw-r--r-- | tests/test_olm_using_malloc.cpp | 2 | ||||
-rw-r--r-- | tests/test_ratchet.cpp | 21 | ||||
-rw-r--r-- | tests/test_session.cpp | 144 |
12 files changed, 701 insertions, 90 deletions
diff --git a/tests/test_base64.cpp b/tests/test_base64.cpp index 5bae2f9..c95e3c9 100644 --- a/tests/test_base64.cpp +++ b/tests/test_base64.cpp @@ -1,10 +1,11 @@ #include "olm/base64.hh" +#include "olm/base64.h" #include "unittest.hh" int main() { { /* Base64 encode test */ -TestCase test_case("Base64 encode test"); +TestCase test_case("Base64 C++ binding encode test"); std::uint8_t input[] = "Hello World"; std::uint8_t expected_output[] = "SGVsbG8gV29ybGQ"; @@ -18,8 +19,24 @@ olm::encode_base64(input, input_length, output); assert_equals(expected_output, output, output_length); } +{ +TestCase test_case("Base64 C binding encode test"); + +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 = ::_olm_encode_base64_length(input_length); +assert_equals(std::size_t(15), output_length); + +std::uint8_t output[output_length]; +output_length = ::_olm_encode_base64(input, input_length, output); +assert_equals(std::size_t(15), output_length); +assert_equals(expected_output, output, output_length); +} + { /* Base64 decode test */ -TestCase test_case("Base64 decode test"); +TestCase test_case("Base64 C++ binding decode test"); std::uint8_t input[] = "SGVsbG8gV29ybGQ"; std::uint8_t expected_output[] = "Hello World"; @@ -33,4 +50,21 @@ olm::decode_base64(input, input_length, output); assert_equals(expected_output, output, output_length); } +{ +TestCase test_case("Base64 C binding decode test"); + +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 = ::_olm_decode_base64_length(input_length); +assert_equals(std::size_t(11), output_length); + +std::uint8_t output[output_length]; +output_length = ::_olm_decode_base64(input, input_length, output); +assert_equals(std::size_t(11), output_length); +assert_equals(expected_output, output, output_length); +} + + } diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp index 4606c52..7dad892 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 "olm/crypto.hh" +#include "olm/crypto.h" #include "unittest.hh" @@ -58,60 +58,31 @@ std::uint8_t expected_agreement[32] = { 0x76, 0xF0, 0x9B, 0x3C, 0x1E, 0x16, 0x17, 0x42 }; -olm::Curve25519KeyPair alice_pair; -olm::curve25519_generate_key(alice_private, alice_pair); +_olm_curve25519_key_pair alice_pair; +_olm_crypto_curve25519_generate_key(alice_private, &alice_pair); -assert_equals(alice_private, alice_pair.private_key, 32); -assert_equals(alice_public, alice_pair.public_key, 32); +assert_equals(alice_private, alice_pair.private_key.private_key, 32); +assert_equals(alice_public, alice_pair.public_key.public_key, 32); -olm::Curve25519KeyPair bob_pair; -olm::curve25519_generate_key(bob_private, bob_pair); +_olm_curve25519_key_pair bob_pair; +_olm_crypto_curve25519_generate_key(bob_private, &bob_pair); -assert_equals(bob_private, bob_pair.private_key, 32); -assert_equals(bob_public, bob_pair.public_key, 32); +assert_equals(bob_private, bob_pair.private_key.private_key, 32); +assert_equals(bob_public, bob_pair.public_key.public_key, 32); -std::uint8_t actual_agreement[olm::KEY_LENGTH] = {}; +std::uint8_t actual_agreement[CURVE25519_SHARED_SECRET_LENGTH] = {}; -olm::curve25519_shared_secret(alice_pair, bob_pair, actual_agreement); +_olm_crypto_curve25519_shared_secret(&alice_pair, &bob_pair.public_key, actual_agreement); assert_equals(expected_agreement, actual_agreement, 32); -olm::curve25519_shared_secret(bob_pair, alice_pair, actual_agreement); +_olm_crypto_curve25519_shared_secret(&bob_pair, &alice_pair.public_key, actual_agreement); assert_equals(expected_agreement, actual_agreement, 32); } /* Curve25529 Test Case 1 */ -{ /* Curve25519 Signature Test Case 1 */ -TestCase test_case("Curve25519 Signature Test Case 1"); - -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; - -olm::Curve25519KeyPair key_pair; -olm::curve25519_generate_key(private_key, key_pair); - -std::uint8_t signature[64]; - -olm::curve25519_sign( - key_pair, message, message_length, signature -); - -bool result = olm::curve25519_verify( - key_pair, message, message_length, signature -); -assert_equals(true, result); - -message[0] = 'n'; -result = olm::curve25519_verify( - key_pair, message, message_length, signature -); -assert_equals(false, result); - -} /* Curve25519 Signature Test Case 1 */ - { TestCase test_case("Ed25519 Signature Test Case 1"); std::uint8_t private_key[33] = "This key is a string of 32 bytes"; @@ -119,22 +90,22 @@ std::uint8_t private_key[33] = "This key is a string of 32 bytes"; std::uint8_t message[] = "Hello, World"; std::size_t message_length = sizeof(message) - 1; -olm::Ed25519KeyPair key_pair; -olm::ed25519_generate_key(private_key, key_pair); +_olm_ed25519_key_pair key_pair; +_olm_crypto_ed25519_generate_key(private_key, &key_pair); std::uint8_t signature[64]; -olm::ed25519_sign( - key_pair, message, message_length, signature +_olm_crypto_ed25519_sign( + &key_pair, message, message_length, signature ); -bool result = olm::ed25519_verify( - key_pair, message, message_length, signature +bool result = _olm_crypto_ed25519_verify( + &key_pair.public_key, message, message_length, signature ); assert_equals(true, result); message[0] = 'n'; -result = olm::ed25519_verify( - key_pair, message, message_length, signature +result = _olm_crypto_ed25519_verify( + &key_pair.public_key, message, message_length, signature ); assert_equals(false, result); } @@ -144,8 +115,8 @@ assert_equals(false, result); TestCase test_case("AES Test Case 1"); -olm::Aes256Key key = {}; -olm::Aes256Iv iv = {}; +_olm_aes256_key key = {}; +_olm_aes256_iv iv = {}; std::uint8_t input[16] = {}; std::uint8_t expected[32] = { @@ -155,16 +126,16 @@ std::uint8_t expected[32] = { 0x4B, 0xAE, 0xDF, 0xFC, 0x3D, 0x21, 0x4C, 0x38 }; -std::size_t length = olm::aes_encrypt_cbc_length(sizeof(input)); +std::size_t length = _olm_crypto_aes_encrypt_cbc_length(sizeof(input)); assert_equals(std::size_t(32), length); std::uint8_t actual[32] = {}; -olm::aes_encrypt_cbc(key, iv, input, sizeof(input), actual); +_olm_crypto_aes_encrypt_cbc(&key, &iv, input, sizeof(input), actual); assert_equals(expected, actual, 32); -length = olm::aes_decrypt_cbc(key, iv, expected, sizeof(expected), actual); +length = _olm_crypto_aes_decrypt_cbc(&key, &iv, expected, sizeof(expected), actual); assert_equals(std::size_t(16), length); assert_equals(input, actual, length); @@ -186,7 +157,7 @@ std::uint8_t expected[32] = { std::uint8_t actual[32]; -olm::sha256(input, sizeof(input), actual); +_olm_crypto_sha256(input, sizeof(input), actual); assert_equals(expected, actual, 32); @@ -207,7 +178,7 @@ std::uint8_t expected[32] = { std::uint8_t actual[32]; -olm::hmac_sha256(input, sizeof(input), input, sizeof(input), actual); +_olm_crypto_hmac_sha256(input, sizeof(input), input, sizeof(input), actual); assert_equals(expected, actual, 32); @@ -242,7 +213,7 @@ std::uint8_t hmac_expected_output[32] = { std::uint8_t hmac_actual_output[32] = {}; -olm::hmac_sha256( +_olm_crypto_hmac_sha256( salt, sizeof(salt), input, sizeof(input), hmac_actual_output @@ -261,7 +232,7 @@ std::uint8_t hkdf_expected_output[42] = { std::uint8_t hkdf_actual_output[42] = {}; -olm::hkdf_sha256( +_olm_crypto_hkdf_sha256( input, sizeof(input), salt, sizeof(salt), info, sizeof(info), diff --git a/tests/test_group_session.cpp b/tests/test_group_session.cpp new file mode 100644 index 0000000..9930927 --- /dev/null +++ b/tests/test_group_session.cpp @@ -0,0 +1,240 @@ +/* Copyright 2016 OpenMarket Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "olm/inbound_group_session.h" +#include "olm/outbound_group_session.h" +#include "unittest.hh" + + +int main() { + +{ + TestCase test_case("Pickle outbound group session"); + + size_t size = olm_outbound_group_session_size(); + uint8_t memory[size]; + OlmOutboundGroupSession *session = olm_outbound_group_session(memory); + + size_t pickle_length = olm_pickle_outbound_group_session_length(session); + uint8_t pickle1[pickle_length]; + olm_pickle_outbound_group_session(session, + "secret_key", 10, + pickle1, pickle_length); + uint8_t pickle2[pickle_length]; + memcpy(pickle2, pickle1, pickle_length); + + uint8_t buffer2[size]; + OlmOutboundGroupSession *session2 = olm_outbound_group_session(buffer2); + size_t res = olm_unpickle_outbound_group_session(session2, + "secret_key", 10, + pickle2, pickle_length); + assert_not_equals((size_t)-1, res); + assert_equals(pickle_length, + olm_pickle_outbound_group_session_length(session2)); + olm_pickle_outbound_group_session(session2, + "secret_key", 10, + pickle2, pickle_length); + + assert_equals(pickle1, pickle2, pickle_length); +} + + +{ + TestCase test_case("Pickle inbound group session"); + + size_t size = olm_inbound_group_session_size(); + uint8_t memory[size]; + OlmInboundGroupSession *session = olm_inbound_group_session(memory); + + size_t pickle_length = olm_pickle_inbound_group_session_length(session); + uint8_t pickle1[pickle_length]; + olm_pickle_inbound_group_session(session, + "secret_key", 10, + pickle1, pickle_length); + uint8_t pickle2[pickle_length]; + memcpy(pickle2, pickle1, pickle_length); + + uint8_t buffer2[size]; + OlmInboundGroupSession *session2 = olm_inbound_group_session(buffer2); + size_t res = olm_unpickle_inbound_group_session(session2, + "secret_key", 10, + pickle2, pickle_length); + assert_not_equals((size_t)-1, res); + assert_equals(pickle_length, + olm_pickle_inbound_group_session_length(session2)); + olm_pickle_inbound_group_session(session2, + "secret_key", 10, + pickle2, pickle_length); + + assert_equals(pickle1, pickle2, pickle_length); +} + +{ + TestCase test_case("Group message send/receive"); + + uint8_t random_bytes[] = + "0123456789ABDEF0123456789ABCDEF" + "0123456789ABDEF0123456789ABCDEF" + "0123456789ABDEF0123456789ABCDEF" + "0123456789ABDEF0123456789ABCDEF" + "0123456789ABDEF0123456789ABCDEF" + "0123456789ABDEF0123456789ABCDEF"; + + + /* build the outbound session */ + size_t size = olm_outbound_group_session_size(); + uint8_t memory[size]; + OlmOutboundGroupSession *session = olm_outbound_group_session(memory); + + assert_equals((size_t)160, + olm_init_outbound_group_session_random_length(session)); + + size_t res = olm_init_outbound_group_session( + session, random_bytes, sizeof(random_bytes)); + assert_equals((size_t)0, res); + + assert_equals(0U, olm_outbound_group_session_message_index(session)); + size_t session_key_len = olm_outbound_group_session_key_length(session); + uint8_t session_key[session_key_len]; + olm_outbound_group_session_key(session, session_key, session_key_len); + + /* encode the message */ + uint8_t plaintext[] = "Message"; + size_t plaintext_length = sizeof(plaintext) - 1; + + size_t msglen = olm_group_encrypt_message_length( + session, plaintext_length); + + uint8_t msg[msglen]; + res = olm_group_encrypt(session, plaintext, plaintext_length, + msg, msglen); + assert_equals(msglen, res); + assert_equals(1U, olm_outbound_group_session_message_index(session)); + + + /* build the inbound session */ + size = olm_inbound_group_session_size(); + uint8_t inbound_session_memory[size]; + OlmInboundGroupSession *inbound_session = + olm_inbound_group_session(inbound_session_memory); + + res = olm_init_inbound_group_session( + inbound_session, session_key, session_key_len); + assert_equals((size_t)0, res); + + + /* Check the session ids */ + + size_t out_session_id_len = olm_outbound_group_session_id_length(session); + uint8_t out_session_id[out_session_id_len]; + assert_equals(out_session_id_len, olm_outbound_group_session_id( + session, out_session_id, out_session_id_len + )); + + size_t in_session_id_len = olm_inbound_group_session_id_length( + inbound_session + ); + uint8_t in_session_id[in_session_id_len]; + assert_equals(in_session_id_len, olm_inbound_group_session_id( + inbound_session, in_session_id, in_session_id_len + )); + + assert_equals(in_session_id_len, out_session_id_len); + assert_equals(out_session_id, in_session_id, in_session_id_len); + + /* decode the message */ + + /* olm_group_decrypt_max_plaintext_length destroys the input so we have to + copy it. */ + uint8_t msgcopy[msglen]; + memcpy(msgcopy, msg, msglen); + size = olm_group_decrypt_max_plaintext_length(inbound_session, msgcopy, msglen); + uint8_t plaintext_buf[size]; + res = olm_group_decrypt(inbound_session, msg, msglen, + plaintext_buf, size); + assert_equals(plaintext_length, res); + assert_equals(plaintext, plaintext_buf, res); +} + +{ + TestCase test_case("Invalid signature group message"); + + uint8_t plaintext[] = "Message"; + size_t plaintext_length = sizeof(plaintext) - 1; + + uint8_t session_key[] = + "AgAAAAAwMTIzNDU2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMzQ1Njc4OUFCREVGM" + "DEyMzQ1Njc4OUFCQ0RFRjAxMjM0NTY3ODlBQkRFRjAxMjM0NTY3ODlBQkNERUYwMTIzND" + "U2Nzg5QUJERUYwMTIzNDU2Nzg5QUJDREVGMDEyMztqJ7zOtqQtYqOo0CpvDXNlMhV3HeJ" + "DpjrASKGLWdop4lx1cSN3Xv1TgfLPW8rhGiW+hHiMxd36nRuxscNv9k4oJA/KP+o0mi1w" + "v44StrEJ1wwx9WZHBUIWkQbaBSuBDw"; + + uint8_t message[] = + "AwgAEhAcbh6UpbByoyZxufQ+h2B+8XHMjhR69G8nP4pNZGl/3QMgrzCZPmP+F2aPLyKPz" + "xRPBMUkeXRJ6Iqm5NeOdx2eERgTW7P20CM+lL3Xpk+ZUOOPvsSQNaAL"; + size_t msglen = sizeof(message)-1; + + /* build the inbound session */ + size_t size = olm_inbound_group_session_size(); + uint8_t inbound_session_memory[size]; + OlmInboundGroupSession *inbound_session = + olm_inbound_group_session(inbound_session_memory); + + size_t res = olm_init_inbound_group_session( + inbound_session, session_key, sizeof(session_key)-1 + ); + assert_equals((size_t)0, res); + + /* decode the message */ + + /* olm_group_decrypt_max_plaintext_length destroys the input so we have to + copy it. */ + uint8_t msgcopy[msglen]; + memcpy(msgcopy, message, msglen); + size = olm_group_decrypt_max_plaintext_length( + inbound_session, msgcopy, msglen + ); + + memcpy(msgcopy, message, msglen); + uint8_t plaintext_buf[size]; + res = olm_group_decrypt( + inbound_session, msgcopy, msglen, plaintext_buf, size + ); + assert_equals(plaintext_length, res); + assert_equals(plaintext, plaintext_buf, res); + + /* now twiddle the signature */ + message[msglen-1] = 'E'; + memcpy(msgcopy, message, msglen); + assert_equals( + size, + olm_group_decrypt_max_plaintext_length( + inbound_session, msgcopy, msglen + ) + ); + + memcpy(msgcopy, message, msglen); + res = olm_group_decrypt( + inbound_session, msgcopy, msglen, + plaintext_buf, size + ); + assert_equals((size_t)-1, res); + assert_equals( + std::string("BAD_SIGNATURE"), + std::string(olm_inbound_group_session_last_error(inbound_session)) + ); +} + + +} diff --git a/tests/test_megolm.cpp b/tests/test_megolm.cpp new file mode 100644 index 0000000..3048fa3 --- /dev/null +++ b/tests/test_megolm.cpp @@ -0,0 +1,134 @@ +/* Copyright 2016 OpenMarket Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "olm/megolm.h" +#include "olm/memory.hh" + +#include "unittest.hh" + + +int main() { + +std::uint8_t random_bytes[] = + "0123456789ABCDEF0123456789ABCDEF" + "0123456789ABCDEF0123456789ABCDEF" + "0123456789ABCDEF0123456789ABCDEF" + "0123456789ABCDEF0123456789ABCDEF"; + +{ + TestCase test_case("Megolm::advance"); + + Megolm mr; + + megolm_init(&mr, random_bytes, 0); + // single-step advance + megolm_advance(&mr); + const std::uint8_t expected1[] = { + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, + 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8, + 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a + }; + assert_equals(1U, mr.counter); + assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); + + // repeat with complex advance + megolm_init(&mr, random_bytes, 0); + megolm_advance_to(&mr, 1); + assert_equals(1U, mr.counter); + assert_equals(expected1, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); + + megolm_advance_to(&mr, 0x1000000); + const std::uint8_t expected2[] = { + 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9, + 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c, + 0x70, 0x04, 0xc0, 0x1e, 0xe4, 0x9b, 0xd6, 0xef, 0xe0, 0x07, 0x35, 0x25, 0xaf, 0x9b, 0x16, 0x32, + 0xc5, 0xbe, 0x72, 0x6d, 0x12, 0x34, 0x9c, 0xc5, 0xbd, 0x47, 0x2b, 0xdc, 0x2d, 0xf6, 0x54, 0x0f, + 0x31, 0x12, 0x59, 0x11, 0x94, 0xfd, 0xa6, 0x17, 0xe5, 0x68, 0xc6, 0x83, 0x10, 0x1e, 0xae, 0xcd, + 0x7e, 0xdd, 0xd6, 0xde, 0x1f, 0xbc, 0x07, 0x67, 0xae, 0x34, 0xda, 0x1a, 0x09, 0xa5, 0x4e, 0xab, + 0xba, 0x9c, 0xd9, 0x55, 0x74, 0x1d, 0x1c, 0x16, 0x23, 0x23, 0xec, 0x82, 0x5e, 0x7c, 0x5c, 0xe8, + 0x89, 0xbb, 0xb4, 0x23, 0xa1, 0x8f, 0x23, 0x82, 0x8f, 0xb2, 0x09, 0x0d, 0x6e, 0x2a, 0xf8, 0x6a, + }; + assert_equals(0x1000000U, mr.counter); + assert_equals(expected2, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); + + megolm_advance_to(&mr, 0x1041506); + const std::uint8_t expected3[] = { + 0x54, 0x02, 0x2d, 0x7d, 0xc0, 0x29, 0x8e, 0x16, 0x37, 0xe2, 0x1c, 0x97, 0x15, 0x30, 0x92, 0xf9, + 0x33, 0xc0, 0x56, 0xff, 0x74, 0xfe, 0x1b, 0x92, 0x2d, 0x97, 0x1f, 0x24, 0x82, 0xc2, 0x85, 0x9c, + 0x55, 0x58, 0x8d, 0xf5, 0xb7, 0xa4, 0x88, 0x78, 0x42, 0x89, 0x27, 0x86, 0x81, 0x64, 0x58, 0x9f, + 0x36, 0x63, 0x44, 0x7b, 0x51, 0xed, 0xc3, 0x59, 0x5b, 0x03, 0x6c, 0xa6, 0x04, 0xc4, 0x6d, 0xcd, + 0x5c, 0x54, 0x85, 0x0b, 0xfa, 0x98, 0xa1, 0xfd, 0x79, 0xa9, 0xdf, 0x1c, 0xbe, 0x8f, 0xc5, 0x68, + 0x19, 0x37, 0xd3, 0x0c, 0x85, 0xc8, 0xc3, 0x1f, 0x7b, 0xb8, 0x28, 0x81, 0x6c, 0xf9, 0xff, 0x3b, + 0x95, 0x6c, 0xbf, 0x80, 0x7e, 0x65, 0x12, 0x6a, 0x49, 0x55, 0x8d, 0x45, 0xc8, 0x4a, 0x2e, 0x4c, + 0xd5, 0x6f, 0x03, 0xe2, 0x44, 0x16, 0xb9, 0x8e, 0x1c, 0xfd, 0x97, 0xc2, 0x06, 0xaa, 0x90, 0x7a + }; + assert_equals(0x1041506U, mr.counter); + assert_equals(expected3, megolm_get_data(&mr), MEGOLM_RATCHET_LENGTH); +} + +{ + TestCase test_case("Megolm::advance wraparound"); + + Megolm mr1, mr2; + + megolm_init(&mr1, random_bytes, 0xffffffffUL); + megolm_advance_to(&mr1, 0x1000000); + assert_equals(0x1000000U, mr1.counter); + + megolm_init(&mr2, random_bytes, 0); + megolm_advance_to(&mr2, 0x2000000); + assert_equals(0x2000000U, mr2.counter); + + assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); +} + +{ + TestCase test_case("Megolm::advance overflow by one"); + + Megolm mr1, mr2; + + megolm_init(&mr1, random_bytes, 0xffffffffUL); + megolm_advance_to(&mr1, 0x0); + assert_equals(0x0U, mr1.counter); + + megolm_init(&mr2, random_bytes, 0xffffffffUL); + megolm_advance(&mr2); + assert_equals(0x0U, mr2.counter); + + assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); +} + +{ + TestCase test_case("Megolm::advance overflow"); + + Megolm mr1, mr2; + + megolm_init(&mr1, random_bytes, 0x1UL); + megolm_advance_to(&mr1, 0x80000000UL); + megolm_advance_to(&mr1, 0x0); + assert_equals(0x0U, mr1.counter); + + megolm_init(&mr2, random_bytes, 0x1UL); + megolm_advance_to(&mr2, 0x0UL); + assert_equals(0x0U, mr2.counter); + + assert_equals(megolm_get_data(&mr2), megolm_get_data(&mr1), MEGOLM_RATCHET_LENGTH); +} + +} diff --git a/tests/test_message.cpp b/tests/test_message.cpp index ff14649..25693f5 100644 --- a/tests/test_message.cpp +++ b/tests/test_message.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 OpenMarket Ltd +/* Copyright 2015-2016 OpenMarket Ltd * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -62,4 +62,51 @@ assert_equals(message2, output, 35); } /* Message encode test */ + +{ /* group message encode test */ + + TestCase test_case("Group message encode test"); + + size_t length = _olm_encode_group_message_length(200, 10, 8, 64); + size_t expected_length = 1 + (1+2) + (2+10) + 8 + 64; + assert_equals(expected_length, length); + + uint8_t output[50]; + uint8_t *ciphertext_ptr; + + _olm_encode_group_message( + 3, + 200, // counter + 10, // ciphertext length + output, + &ciphertext_ptr + ); + + uint8_t expected[] = + "\x03" + "\x08\xC8\x01" + "\x12\x0A"; + + assert_equals(expected, output, sizeof(expected)-1); + assert_equals(output+sizeof(expected)-1, ciphertext_ptr); +} /* group message encode test */ + +{ + TestCase test_case("Group message decode test"); + + struct _OlmDecodeGroupMessageResults results; + std::uint8_t message[] = + "\x03" + "\x08\xC8\x01" + "\x12\x0A" "ciphertext" + "hmacsha2" + "ed25519signature"; + + _olm_decode_group_message(message, sizeof(message)-1, 8, 16, &results); + assert_equals(std::uint8_t(3), results.version); + assert_equals(1, results.has_message_index); + assert_equals(std::uint32_t(200), results.message_index); + assert_equals(std::size_t(10), results.ciphertext_length); + assert_equals(ciphertext, results.ciphertext, 10); +} /* group message decode test */ } diff --git a/tests/test_olm.cpp b/tests/test_olm.cpp index fbc14cf..af2c9f7 100644 --- a/tests/test_olm.cpp +++ b/tests/test_olm.cpp @@ -1,4 +1,4 @@ -#include "olm/olm.hh" +#include "olm/olm.h" #include "unittest.hh" #include <cstddef> @@ -65,6 +65,33 @@ assert_equals(pickle1, pickle2, pickle_length); } + +{ + TestCase test_case("Old account unpickle test"); + + // this uses the old pickle format, which did not use enough space + // for the Ed25519 key. We should reject it. + std::uint8_t pickle[] = + "x3h9er86ygvq56pM1yesdAxZou4ResPQC9Rszk/fhEL9JY/umtZ2N/foL/SUgVXS" + "v0IxHHZTafYjDdzJU9xr8dQeBoOTGfV9E/lCqDGBnIlu7SZndqjEKXtzGyQr4sP4" + "K/A/8TOu9iK2hDFszy6xETiousHnHgh2ZGbRUh4pQx+YMm8ZdNZeRnwFGLnrWyf9" + "O5TmXua1FcU"; + + std::uint8_t account_buffer[::olm_account_size()]; + ::OlmAccount *account = ::olm_account(account_buffer); + assert_equals( + std::size_t(-1), + ::olm_unpickle_account( + account, "", 0, pickle, sizeof(pickle)-1 + ) + ); + assert_equals( + std::string("BAD_LEGACY_ACCOUNT_PICKLE"), + std::string(::olm_account_last_error(account)) + ); +} + + { /** Pickle session test */ TestCase test_case("Pickle session test"); diff --git a/tests/test_olm_decrypt.cpp b/tests/test_olm_decrypt.cpp index 2a2db98..4a1fb97 100644 --- a/tests/test_olm_decrypt.cpp +++ b/tests/test_olm_decrypt.cpp @@ -1,11 +1,16 @@ -#include "olm/olm.hh" +#include "olm/olm.h" #include "unittest.hh" -const char * test_cases[] = { - "41776f", - "7fff6f0101346d671201", - "ee776f41496f674177804177778041776f6716670a677d6f670a67c2677d", - "e9e9c9c1e9e9c9e9c9c1e9e9c9c1", +struct test_case { + const char *msghex; + const char *expected_error; +}; + +const test_case test_cases[] = { + { "41776f", "BAD_MESSAGE_FORMAT" }, + { "7fff6f0101346d671201", "BAD_MESSAGE_FORMAT" }, + { "ee776f41496f674177804177778041776f6716670a677d6f670a67c2677d", "BAD_MESSAGE_FORMAT" }, + { "e9e9c9c1e9e9c9e9c9c1e9e9c9c1", "BAD_MESSAGE_FORMAT" }, }; @@ -31,29 +36,39 @@ void decode_hex( } } -void decrypt_case(int message_type, const char * test_case) { +void decrypt_case(int message_type, const test_case * test_case) { std::uint8_t session_memory[olm_session_size()]; ::OlmSession * session = ::olm_session(session_memory); std::uint8_t pickled[strlen(session_data)]; ::memcpy(pickled, session_data, sizeof(pickled)); - ::olm_unpickle_session(session, "", 0, pickled, sizeof(pickled)); + assert_not_equals( + ::olm_error(), + ::olm_unpickle_session(session, "", 0, pickled, sizeof(pickled)) + ); - std::size_t message_length = strlen(test_case) / 2; + std::size_t message_length = strlen(test_case->msghex) / 2; std::uint8_t * message = (std::uint8_t *) ::malloc(message_length); - decode_hex(test_case, message, message_length); + decode_hex(test_case->msghex, message, message_length); size_t max_length = olm_decrypt_max_plaintext_length( session, message_type, message, message_length ); - if (max_length == std::size_t(-1)) { + if (test_case->expected_error) { + assert_equals(::olm_error(), max_length); + assert_equals( + std::string(test_case->expected_error), + std::string(::olm_session_last_error(session)) + ); free(message); return; } + assert_not_equals(::olm_error(), max_length); + uint8_t plaintext[max_length]; - decode_hex(test_case, message, message_length); + decode_hex(test_case->msghex, message, message_length); olm_decrypt( session, message_type, message, message_length, @@ -67,8 +82,8 @@ int main() { { TestCase my_test("Olm decrypt test"); -for (int i = 0; i < sizeof(test_cases)/ sizeof(const char *); ++i) { - decrypt_case(0, test_cases[i]); +for (unsigned int i = 0; i < sizeof(test_cases)/ sizeof(test_cases[0]); ++i) { + decrypt_case(0, &test_cases[i]); } } diff --git a/tests/test_olm_sha256.cpp b/tests/test_olm_sha256.cpp index fe5bf42..c6d0242 100644 --- a/tests/test_olm_sha256.cpp +++ b/tests/test_olm_sha256.cpp @@ -1,4 +1,4 @@ -#include "olm/olm.hh" +#include "olm/olm.h" #include "unittest.hh" int main() { diff --git a/tests/test_olm_signature.cpp b/tests/test_olm_signature.cpp index a7cce63..d7259de 100644 --- a/tests/test_olm_signature.cpp +++ b/tests/test_olm_signature.cpp @@ -1,4 +1,4 @@ -#include "olm/olm.hh" +#include "olm/olm.h" #include "unittest.hh" #include <cstddef> diff --git a/tests/test_olm_using_malloc.cpp b/tests/test_olm_using_malloc.cpp index 84fbbd7..fff3ea2 100644 --- a/tests/test_olm_using_malloc.cpp +++ b/tests/test_olm_using_malloc.cpp @@ -1,4 +1,4 @@ -#include "olm/olm.hh" +#include "olm/olm.h" #include "unittest.hh" #include <cstddef> diff --git a/tests/test_ratchet.cpp b/tests/test_ratchet.cpp index cbb3c52..fb60ba9 100644 --- a/tests/test_ratchet.cpp +++ b/tests/test_ratchet.cpp @@ -13,7 +13,7 @@ * limitations under the License. */ #include "olm/ratchet.hh" -#include "olm/cipher.hh" +#include "olm/cipher.h" #include "unittest.hh" @@ -28,13 +28,12 @@ olm::KdfInfo kdf_info = { ratchet_info, sizeof(ratchet_info) - 1 }; -olm::CipherAesSha256 cipher( - message_info, sizeof(message_info) - 1 -); +_olm_cipher_aes_sha_256 cipher0 = OLM_CIPHER_INIT_AES_SHA_256(message_info); +_olm_cipher *cipher = OLM_CIPHER_BASE(&cipher0); std::uint8_t random_bytes[] = "0123456789ABDEF0123456789ABCDEF"; -olm::Curve25519KeyPair alice_key; -olm::curve25519_generate_key(random_bytes, alice_key); +_olm_curve25519_key_pair alice_key; +_olm_crypto_curve25519_generate_key(random_bytes, &alice_key); std::uint8_t shared_secret[] = "A secret"; @@ -45,7 +44,7 @@ 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); +bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); std::uint8_t plaintext[] = "Message"; std::size_t plaintext_length = sizeof(plaintext) - 1; @@ -114,7 +113,7 @@ 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); +bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); std::uint8_t plaintext_1[] = "First Message"; std::size_t plaintext_1_length = sizeof(plaintext_1) - 1; @@ -186,7 +185,7 @@ 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); +bob.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, alice_key.public_key); std::uint8_t plaintext[] = "These 15 bytes"; assert_equals(std::size_t(15), sizeof(plaintext)); @@ -195,7 +194,7 @@ std::uint8_t random[] = "This is a random 32 byte string"; for (unsigned i = 0; i < 8; ++i) { { std::uint8_t msg[alice.encrypt_output_length(sizeof(plaintext))]; - std::uint8_t encrypt_length = alice.encrypt( + alice.encrypt( plaintext, 15, random, 32, msg, sizeof(msg) ); std::uint8_t output[bob.decrypt_max_plaintext_length(msg, sizeof(msg))]; @@ -206,7 +205,7 @@ for (unsigned i = 0; i < 8; ++i) { random[31]++; { std::uint8_t msg[bob.encrypt_output_length(sizeof(plaintext))]; - std::uint8_t encrypt_length = bob.encrypt( + bob.encrypt( plaintext, 15, random, 32, msg, sizeof(msg) ); std::uint8_t output[alice.decrypt_max_plaintext_length(msg, sizeof(msg))]; diff --git a/tests/test_session.cpp b/tests/test_session.cpp new file mode 100644 index 0000000..e2c3199 --- /dev/null +++ b/tests/test_session.cpp @@ -0,0 +1,144 @@ +#include "olm/session.hh" +#include "olm/pickle_encoding.h" + +#include "unittest.hh" + +/* decode into a buffer, which is returned */ +std::uint8_t *decode_hex( + const char * input +) { + static std::uint8_t buf[256]; + std::uint8_t *p = buf; + while (*input != '\0') { + char high = *(input++); + char low = *(input++); + if (high >= 'a') high -= 'a' - ('9' + 1); + if (low >= 'a') low -= 'a' - ('9' + 1); + uint8_t value = ((high - '0') << 4) | (low - '0'); + *p++ = value; + } + return buf; +} + +void check_session(const olm::Session &session) { + assert_equals( + decode_hex("49d640dc96b80176694af69fc4b8ca9fac49aecbd697d01fd8bee1ed2693b6c9"), + session.ratchet.root_key, 32 + ); + + assert_equals( + std::size_t(1), + session.ratchet.sender_chain.size() + ); + + assert_equals( + decode_hex("f77a03eaa9b301fa7d2a5aa6b50286906de12cc96044f526dbbcb12839ad7003"), + session.ratchet.sender_chain[0].ratchet_key.public_key.public_key, 32 + ); + + assert_equals( + decode_hex("d945c6ed4c7c277117adf11fb133a7936d287afe97c0b3ac989644b4490d4f31"), + session.ratchet.sender_chain[0].ratchet_key.private_key.private_key, 32 + ); + + assert_equals( + std::uint32_t(0), + session.ratchet.sender_chain[0].chain_key.index + ); + + assert_equals( + std::size_t(0), + session.ratchet.receiver_chains.size() + ); + + assert_equals( + std::size_t(0), + session.ratchet.skipped_message_keys.size() + ); + + assert_equals(OLM_SUCCESS, session.last_error); + assert_equals(false, session.received_message); + + assert_equals( + decode_hex("7326b58623a3f7bd8da11a1bab51f432c02a7430241b326e9fc8916a21eb257e"), + session.alice_identity_key.public_key, 32 + ); + + assert_equals( + decode_hex("0ab4b30bde20bd374ceccc72861660f0fd046f7516900796c3e5de41c598316c"), + session.alice_base_key.public_key, 32 + ); + + assert_equals( + decode_hex("585dba930b10d90d81702c715f4085d07c42b0cd2d676010bb6086c86c4cc618"), + session.bob_one_time_key.public_key, 32 + ); +} + +int main() { + +{ + TestCase test_case("V1 session pickle"); + + const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key"; + uint8_t pickled[] = + "wkEpwMgiAqD7B1/Lw2cKYYDcUZVOd9QHes7ZroWxr/Rp/nWEAySgRsIu/a54YhO67rwitr" + "Lpos7tFxxK9IZ7pKB1qrR1coVWIt78V9lp9WgmBAvxHBSY+tu1lkL/JjLi963/yFdPancZ" + "+WHMVfaKlV3gWGpo7EfNK6qAOxI1Ea/eCsE2sYrsHEDvLLGlKAA9E56rmmoe2w6TKzsQjs" + "ZM2/XT2eJ82EgMO9pL02iLElXWmGNv72Ut7DouR0pQIT50HIEEKcFxYcoTb3WCfJD76Coe" + "sE4kx+TA6d45Xu1bwQNNkTGF+nCCu/GmKY+sECXbz9U6WhxG0YdF9Z4T8YkWYAgpKNS0FW" + "RV"; + size_t pickle_len = _olm_enc_input( + PICKLE_KEY, strlen((char *)PICKLE_KEY), + pickled, strlen((char *)pickled), NULL + ); + + olm::Session session; + const uint8_t *unpickle_res = olm::unpickle(pickled, pickled+sizeof(pickled), session); + assert_equals( + pickle_len, (size_t)(unpickle_res - pickled) + ); + + check_session(session); + +#if 0 + size_t rawlen = olm::pickle_length(session); + uint8_t *r1 = _olm_enc_output_pos(pickled, rawlen); + olm::pickle(r1, session); + _olm_enc_output( + PICKLE_KEY, strlen((char *)PICKLE_KEY), + pickled, rawlen); + printf("%s\n", pickled); +#endif +} + +{ + TestCase test_case("V2 session pickle"); + + const uint8_t *PICKLE_KEY=(uint8_t *)"secret_key"; + uint8_t pickled[] = + "m+DS/q34MXpw2xp50ZD0B7val1mlMpQXo0mx+VPje0weFYRRuuZQBdJgcFPEpi2MVSpA4c" + "qgqHyj2/bU7/lz+BXkEBrCFVx0BJidxXfOLDW4TNtRhLS1YHJNGP8GvTg1+dCytBTLsCdm" + "5f945Eq1U/pY3Cg96YTUufFP6EYrfRoDbAsRHc+h+wKKftQv+W44yUmRhcCemGHtpxk3UQ" + "AMCI7EBv9BvveyZMy3p9qZ3xvFK34Hef+R7gjtFycz7Nk/4UF46sT3cTmUlXz9iFW4uz2F" + "rTI1Wjym+l0DadsbSpHSUjmp9zt4qRP2UjwfZ5QNLv+cdObIfqFsiThGu/PlKigdF4SLHr" + "nG"; + + size_t pickle_len = _olm_enc_input( + PICKLE_KEY, strlen((char *)PICKLE_KEY), + pickled, strlen((char *)pickled), NULL + ); + + olm::Session session; + const uint8_t *unpickle_res = olm::unpickle(pickled, pickled+sizeof(pickled), session); + assert_equals( + pickle_len, (size_t)(unpickle_res - pickled) + ); + + check_session(session); +} + + + +return 0; +} |