diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/include/unittest.hh | 84 | ||||
-rw-r--r-- | tests/test_axolotl.cpp | 174 | ||||
-rw-r--r-- | tests/test_crypto.cpp | 218 | ||||
-rw-r--r-- | tests/test_list.cpp | 70 | ||||
-rw-r--r-- | tests/test_message.cpp | 65 |
5 files changed, 611 insertions, 0 deletions
diff --git a/tests/include/unittest.hh b/tests/include/unittest.hh new file mode 100644 index 0000000..437ea77 --- /dev/null +++ b/tests/include/unittest.hh @@ -0,0 +1,84 @@ +/* Copyright 2015 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 <cstring> +#include <iostream> +#include <iomanip> +#include <cstdlib> + + +std::ostream & print_hex( + std::ostream & os, + std::uint8_t const * data, + std::size_t length +) { + for (std::size_t i = 0; i < length; i++) { + os << std::setw(2) << std::setfill('0') << std::right + << std::hex << (int) data[i]; + } + return os; +} + + +char const * TEST_CASE; + + +template<typename T> +void assert_equals( + const char *file, + unsigned line, + const char *expected_expr, + const char *actual_expr, + T const & expected, + T const & actual +) { + if (expected != actual) { + std::cout << "FAILED: " << TEST_CASE << std::endl; + std::cout << file << ":" << line << std::endl; + std::cout << expected_expr << " == " << actual_expr << std::endl; + std::cout << "Expected: " << expected << std::endl; + std::cout << "Actual: " << actual << std::endl; + std::exit(1); + } +} + + +void assert_equals( + const char *file, + unsigned line, + const char *expected_expr, + const char *actual_expr, + std::uint8_t const * expected, + std::uint8_t const * actual, + std::size_t length +) { + if (std::memcmp(expected, actual, length)) { + std::cout << "FAILED: " << TEST_CASE << std::endl; + std::cout << file << ":" << line << std::endl; + std::cout << expected_expr << " == " << actual_expr << std::endl; + print_hex(std::cout << "Expected: ", expected, length) << std::endl; + print_hex(std::cout << "Actual: ", actual, length) << std::endl; + std::exit(1); + } +} + +#define assert_equals(expected, actual, ...) assert_equals( \ + __FILE__, __LINE__, #expected, #actual, expected, actual, ##__VA_ARGS__ \ +) + +class TestCase { +public: + TestCase(const char *name) { TEST_CASE = name; } + ~TestCase() { std::cout << "PASSED: " << TEST_CASE << std::endl; } +}; diff --git a/tests/test_axolotl.cpp b/tests/test_axolotl.cpp new file mode 100644 index 0000000..f268f2a --- /dev/null +++ b/tests/test_axolotl.cpp @@ -0,0 +1,174 @@ +/* Copyright 2015 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 "axolotl/axolotl.hh" +#include "unittest.hh" + + +int main() { + +std::uint8_t root_info[] = "Axolotl"; +std::uint8_t ratchet_info[] = "AxolotlRatchet"; +std::uint8_t message_info[] = "AxolotlMessageKeys"; + +axolotl::KdfInfo kdf_info = { + root_info, sizeof(root_info) - 1, + ratchet_info, sizeof(ratchet_info - 1), + message_info, sizeof(ratchet_info - 1) +}; + +std::uint8_t random_bytes[] = "0123456789ABDEF0123456789ABCDEF"; +axolotl::Curve25519KeyPair bob_key; +axolotl::generate_key(random_bytes, bob_key); + +std::uint8_t shared_secret[] = "A secret"; + +{ /* Send/Receive test case */ +TestCase test_case("Axolotl Send/Receive"); + +axolotl::Session alice(kdf_info); +axolotl::Session bob(kdf_info); + +alice.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, bob_key); +bob.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, bob_key); + +std::uint8_t plaintext[] = "Message"; +std::size_t plaintext_length = sizeof(plaintext) - 1; + +std::size_t message_length, random_length, output_length; +std::size_t encrypt_length, decrypt_length; +{ + /* Bob sends Alice a message */ + message_length = bob.encrypt_max_output_length(plaintext_length); + random_length = bob.encrypt_random_length(); + assert_equals(std::size_t(0), random_length); + output_length = alice.decrypt_max_plaintext_length(message_length); + + std::uint8_t message[message_length]; + std::uint8_t output[output_length]; + + encrypt_length = bob.encrypt( + plaintext, plaintext_length, + NULL, 0, + message, message_length + ); + assert_equals(message_length, encrypt_length); + + decrypt_length = alice.decrypt( + message, message_length, + output, output_length + ); + assert_equals(plaintext_length, decrypt_length); + assert_equals(plaintext, output, decrypt_length); +} + + +{ + /* Alice sends Bob a message */ + message_length = alice.encrypt_max_output_length(plaintext_length); + random_length = alice.encrypt_random_length(); + assert_equals(std::size_t(32), random_length); + output_length = bob.decrypt_max_plaintext_length(message_length); + + std::uint8_t message[message_length]; + std::uint8_t output[output_length]; + std::uint8_t random[] = "This is a random 32 byte string."; + + encrypt_length = alice.encrypt( + plaintext, plaintext_length, + random, 32, + message, message_length + ); + assert_equals(message_length, encrypt_length); + + decrypt_length = bob.decrypt( + message, message_length, + output, output_length + ); + assert_equals(plaintext_length, decrypt_length); + assert_equals(plaintext, output, decrypt_length); +} + +} /* Send/receive message test case */ + +{ /* Out of order test case */ + +TestCase test_case("Axolotl Out of Order"); + +axolotl::Session alice(kdf_info); +axolotl::Session bob(kdf_info); + +alice.initialise_as_bob(shared_secret, sizeof(shared_secret) - 1, bob_key); +bob.initialise_as_alice(shared_secret, sizeof(shared_secret) - 1, bob_key); + +std::uint8_t plaintext_1[] = "First Message"; +std::size_t plaintext_1_length = sizeof(plaintext_1) - 1; + +std::uint8_t plaintext_2[] = "Second Messsage. A bit longer than the first."; +std::size_t plaintext_2_length = sizeof(plaintext_2) - 1; + +std::size_t message_1_length, message_2_length, random_length, output_length; +std::size_t encrypt_length, decrypt_length; + +{ + /* Alice sends Bob two messages and they arrive out of order */ + message_1_length = alice.encrypt_max_output_length(plaintext_1_length); + random_length = alice.encrypt_random_length(); + assert_equals(std::size_t(32), random_length); + + std::uint8_t message_1[message_1_length]; + std::uint8_t random[] = "This is a random 32 byte string."; + encrypt_length = alice.encrypt( + plaintext_1, plaintext_1_length, + random, 32, + message_1, message_1_length + ); + assert_equals(message_1_length, encrypt_length); + + message_2_length = alice.encrypt_max_output_length(plaintext_2_length); + random_length = alice.encrypt_random_length(); + assert_equals(std::size_t(0), random_length); + + std::uint8_t message_2[message_2_length]; + encrypt_length = alice.encrypt( + plaintext_2, plaintext_2_length, + NULL, 0, + message_2, message_2_length + ); + assert_equals(message_2_length, encrypt_length); + + output_length = bob.decrypt_max_plaintext_length(message_2_length); + std::uint8_t output_1[output_length]; + decrypt_length = bob.decrypt( + message_2, message_2_length, + output_1, output_length + ); + assert_equals(plaintext_2_length, decrypt_length); + assert_equals(plaintext_2, output_1, decrypt_length); + + output_length = bob.decrypt_max_plaintext_length(message_1_length); + std::uint8_t output_2[output_length]; + decrypt_length = bob.decrypt( + message_1, message_1_length, + output_2, output_length + ); + + assert_equals(plaintext_1_length, decrypt_length); + assert_equals(plaintext_1, output_2, decrypt_length); +} + +} /* Out of order test case */ + + +} diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp new file mode 100644 index 0000000..1c068c4 --- /dev/null +++ b/tests/test_crypto.cpp @@ -0,0 +1,218 @@ +/* Copyright 2015 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 "axolotl/crypto.hh" + +#include "unittest.hh" + +int main() { + + +{ /* Curve25529 Test Case 1 */ + +TestCase test_case("Curve25529 Test Case 1"); + +std::uint8_t alice_private[32] = { + 0x77, 0x07, 0x6D, 0x0A, 0x73, 0x18, 0xA5, 0x7D, + 0x3C, 0x16, 0xC1, 0x72, 0x51, 0xB2, 0x66, 0x45, + 0xDF, 0x4C, 0x2F, 0x87, 0xEB, 0xC0, 0x99, 0x2A, + 0xB1, 0x77, 0xFB, 0xA5, 0x1D, 0xB9, 0x2C, 0x2A +}; + +std::uint8_t alice_public[32] = { + 0x85, 0x20, 0xF0, 0x09, 0x89, 0x30, 0xA7, 0x54, + 0x74, 0x8B, 0x7D, 0xDC, 0xB4, 0x3E, 0xF7, 0x5A, + 0x0D, 0xBF, 0x3A, 0x0D, 0x26, 0x38, 0x1A, 0xF4, + 0xEB, 0xA4, 0xA9, 0x8E, 0xAA, 0x9B, 0x4E, 0x6A +}; + +std::uint8_t bob_private[32] = { + 0x5D, 0xAB, 0x08, 0x7E, 0x62, 0x4A, 0x8A, 0x4B, + 0x79, 0xE1, 0x7F, 0x8B, 0x83, 0x80, 0x0E, 0xE6, + 0x6F, 0x3B, 0xB1, 0x29, 0x26, 0x18, 0xB6, 0xFD, + 0x1C, 0x2F, 0x8B, 0x27, 0xFF, 0x88, 0xE0, 0xEB +}; + +std::uint8_t bob_public[32] = { + 0xDE, 0x9E, 0xDB, 0x7D, 0x7B, 0x7D, 0xC1, 0xB4, + 0xD3, 0x5B, 0x61, 0xC2, 0xEC, 0xE4, 0x35, 0x37, + 0x3F, 0x83, 0x43, 0xC8, 0x5B, 0x78, 0x67, 0x4D, + 0xAD, 0xFC, 0x7E, 0x14, 0x6F, 0x88, 0x2B, 0x4F +}; + +std::uint8_t expected_agreement[32] = { + 0x4A, 0x5D, 0x9D, 0x5B, 0xA4, 0xCE, 0x2D, 0xE1, + 0x72, 0x8E, 0x3B, 0xF4, 0x80, 0x35, 0x0F, 0x25, + 0xE0, 0x7E, 0x21, 0xC9, 0x47, 0xD1, 0x9E, 0x33, + 0x76, 0xF0, 0x9B, 0x3C, 0x1E, 0x16, 0x17, 0x42 +}; + +axolotl::Curve25519KeyPair alice_pair; +axolotl::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); + +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] = {}; + +axolotl::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); + +assert_equals(expected_agreement, actual_agreement, 32); + +} /* Curve25529 Test Case 1 */ + + +{ /* AES Test Case 1 */ + +TestCase test_case("AES Test Case 1"); + +axolotl::Aes256Key key = {}; +axolotl::Aes256Iv iv = {}; +std::uint8_t input[16] = {}; + +std::uint8_t expected[32] = { + 0xDC, 0x95, 0xC0, 0x78, 0xA2, 0x40, 0x89, 0x89, + 0xAD, 0x48, 0xA2, 0x14, 0x92, 0x84, 0x20, 0x87, + 0xF3, 0xC0, 0x03, 0xDD, 0xC4, 0xA7, 0xB8, 0xA9, + 0x4B, 0xAE, 0xDF, 0xFC, 0x3D, 0x21, 0x4C, 0x38 +}; + +std::size_t length = axolotl::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); +assert_equals(expected, actual, 32); + +length = axolotl::aes_decrypt_cbc(key, iv, expected, sizeof(expected), actual); +assert_equals(std::size_t(16), length); +assert_equals(input, actual, length); + +} /* AES Test Case 1 */ + + +{ /* SHA 256 Test Case 1 */ + +TestCase test_case("SHA 256 Test Case 1"); + +std::uint8_t input[0] = {}; + +std::uint8_t expected[32] = { + 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14, + 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24, + 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C, + 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55 +}; + +std::uint8_t actual[32]; + +axolotl::sha256(input, sizeof(input), actual); + +assert_equals(expected, actual, 32); + +} /* SHA 256 Test Case 1 */ + +{ /* HMAC Test Case 1 */ + +TestCase test_case("HMAC Test Case 1"); + +std::uint8_t input[0] = {}; + +std::uint8_t expected[32] = { + 0xb6, 0x13, 0x67, 0x9a, 0x08, 0x14, 0xd9, 0xec, + 0x77, 0x2f, 0x95, 0xd7, 0x78, 0xc3, 0x5f, 0xc5, + 0xff, 0x16, 0x97, 0xc4, 0x93, 0x71, 0x56, 0x53, + 0xc6, 0xc7, 0x12, 0x14, 0x42, 0x92, 0xc5, 0xad +}; + +std::uint8_t actual[32]; + +axolotl::hmac_sha256(input, sizeof(input), input, sizeof(input), actual); + +assert_equals(expected, actual, 32); + +} /* HMAC Test Case 1 */ + +{ /* HDKF Test Case 1 */ + +TestCase test_case("HDKF Test Case 1"); + +std::uint8_t input[22] = { + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b +}; + +std::uint8_t salt[13] = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c +}; + +std::uint8_t info[10] = { + 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, + 0xf8, 0xf9 +}; + +std::uint8_t hmac_expected_output[32] = { + 0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf, + 0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63, + 0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31, + 0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5, +}; + +std::uint8_t hmac_actual_output[32] = {}; + +axolotl::hmac_sha256( + salt, sizeof(salt), + input, sizeof(input), + hmac_actual_output +); + +assert_equals(hmac_expected_output, hmac_actual_output, 32); + +std::uint8_t hkdf_expected_output[42] = { + 0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a, + 0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a, + 0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c, + 0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf, + 0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18, + 0x58, 0x65 +}; + +std::uint8_t hkdf_actual_output[42] = {}; + +axolotl::hkdf_sha256( + input, sizeof(input), + salt, sizeof(salt), + info, sizeof(info), + hkdf_actual_output, sizeof(hkdf_actual_output) +); + +assert_equals(hkdf_expected_output, hkdf_actual_output, 42); + +} /* HDKF Test Case 1 */ + +} diff --git a/tests/test_list.cpp b/tests/test_list.cpp new file mode 100644 index 0000000..2cf23dc --- /dev/null +++ b/tests/test_list.cpp @@ -0,0 +1,70 @@ +/* Copyright 2015 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 "axolotl/list.hh" +#include "unittest.hh" + +int main() { + +{ /** List insert test **/ + +TestCase test_case("List insert"); + +axolotl::List<int, 4> test_list; + +assert_equals(std::size_t(0), test_list.size()); + +for (int i = 0; i < 4; ++i) { + test_list.insert(test_list.end(), i); +} + +assert_equals(std::size_t(4), test_list.size()); + +int i = 0; +for (auto item : test_list) { + assert_equals(i++, item); +} + +assert_equals(4, i); + +test_list.insert(test_list.end(), 4); + +assert_equals(4, test_list[3]); + +} /** List insert test **/ + +{ /** List erase test **/ +TestCase test_case("List erase"); + +axolotl::List<int, 4> test_list; +assert_equals(std::size_t(0), test_list.size()); + +for (int i = 0; i < 4; ++i) { + test_list.insert(test_list.end(), i); +} +assert_equals(std::size_t(4), test_list.size()); + +test_list.erase(test_list.begin()); +assert_equals(std::size_t(3), test_list.size()); + +int i = 0; +for (auto item : test_list) { + assert_equals(i + 1, item); + ++i; +} +assert_equals(3, i); + +} + +} diff --git a/tests/test_message.cpp b/tests/test_message.cpp new file mode 100644 index 0000000..ca36ff5 --- /dev/null +++ b/tests/test_message.cpp @@ -0,0 +1,65 @@ +/* Copyright 2015 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 "axolotl/message.hh" +#include "unittest.hh" + +int main() { + +std::uint8_t message1[36] = "\x03\n\nratchetkey\x10\x01\"\nciphertexthmacsha2"; +std::uint8_t message2[36] = "\x03\x10\x01\n\nratchetkey\"\nciphertexthmacsha2"; +std::uint8_t ratchetkey[11] = "ratchetkey"; +std::uint8_t ciphertext[11] = "ciphertext"; +std::uint8_t hmacsha2[9] = "hmacsha2"; + +{ /* Message decode test */ + +TestCase test_case("Message decode test"); + +axolotl::MessageReader reader(axolotl::decode_message(message1, 35, 8)); + +assert_equals(std::size_t(27), reader.body_length); +assert_equals(std::uint8_t(3), reader.version); +assert_equals(std::uint32_t(1), reader.counter); +assert_equals(std::size_t(10), reader.ratchet_key_length); +assert_equals(std::size_t(10), reader.ciphertext_length); + +assert_equals(ratchetkey, reader.ratchet_key, 10); +assert_equals(ciphertext, reader.ciphertext, 10); +assert_equals(hmacsha2, reader.mac, 8); + + +} /* Message decode test */ + +{ /* Message encode test */ + +TestCase test_case("Message encode test"); + +std::size_t length = axolotl::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(3, 1, 10, 10, output)); +assert_equals(std::size_t(27), writer.body_length); + +std::memcpy(writer.ratchet_key, ratchetkey, 10); +std::memcpy(writer.ciphertext, ciphertext, 10); +std::memcpy(writer.mac, hmacsha2, 8); + +assert_equals(message2, output, 35); + +} /* Message encode test */ + +} |