aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/axolotl/axolotl.hh114
-rw-r--r--include/axolotl/crypto.hh90
-rw-r--r--include/axolotl/list.hh100
-rw-r--r--include/axolotl/message.hh62
-rw-r--r--lib/curve25519-donna/.gitignore (renamed from .gitignore)0
-rw-r--r--lib/curve25519-donna/LICENSE.md (renamed from LICENSE.md)0
-rw-r--r--lib/curve25519-donna/Makefile (renamed from Makefile)0
-rw-r--r--lib/curve25519-donna/README (renamed from README)0
-rw-r--r--lib/curve25519-donna/contrib/Curve25519Donna.c (renamed from contrib/Curve25519Donna.c)0
-rw-r--r--lib/curve25519-donna/contrib/Curve25519Donna.h (renamed from contrib/Curve25519Donna.h)0
-rw-r--r--lib/curve25519-donna/contrib/Curve25519Donna.java (renamed from contrib/Curve25519Donna.java)0
-rw-r--r--lib/curve25519-donna/contrib/make-snippets (renamed from contrib/make-snippets)0
-rw-r--r--lib/curve25519-donna/curve25519-donna-c64.c (renamed from curve25519-donna-c64.c)0
-rw-r--r--lib/curve25519-donna/curve25519-donna.c (renamed from curve25519-donna.c)0
-rw-r--r--lib/curve25519-donna/curve25519-donna.podspec (renamed from curve25519-donna.podspec)0
-rw-r--r--lib/curve25519-donna/python-src/curve25519/__init__.py (renamed from python-src/curve25519/__init__.py)0
-rw-r--r--lib/curve25519-donna/python-src/curve25519/curve25519module.c (renamed from python-src/curve25519/curve25519module.c)0
-rw-r--r--lib/curve25519-donna/python-src/curve25519/keys.py (renamed from python-src/curve25519/keys.py)0
-rw-r--r--lib/curve25519-donna/python-src/curve25519/test/__init__.py (renamed from python-src/curve25519/test/__init__.py)0
-rwxr-xr-xlib/curve25519-donna/python-src/curve25519/test/test_curve25519.py (renamed from python-src/curve25519/test/test_curve25519.py)0
-rwxr-xr-xlib/curve25519-donna/python-src/curve25519/test/test_speed.py (renamed from python-src/curve25519/test/test_speed.py)0
-rwxr-xr-xlib/curve25519-donna/setup.py (renamed from setup.py)0
-rw-r--r--lib/curve25519-donna/speed-curve25519.c (renamed from speed-curve25519.c)0
-rw-r--r--lib/curve25519-donna/test-curve25519.c (renamed from test-curve25519.c)0
-rw-r--r--lib/curve25519-donna/test-noncanon.c (renamed from test-noncanon.c)0
-rw-r--r--lib/curve25519-donna/test-sc-curve25519.c (renamed from test-sc-curve25519.c)0
-rw-r--r--lib/curve25519-donna/test-sc-curve25519.s (renamed from test-sc-curve25519.s)0
-rw-r--r--src/axolotl.cpp432
-rw-r--r--src/crypto.cpp234
-rw-r--r--src/libs.cpp5
-rw-r--r--src/message.cpp170
-rw-r--r--test.py17
-rw-r--r--tests/include/unittest.hh55
-rw-r--r--tests/test_crypto.cpp204
-rw-r--r--tests/test_list.cpp56
-rw-r--r--tests/test_message.cpp51
36 files changed, 1590 insertions, 0 deletions
diff --git a/include/axolotl/axolotl.hh b/include/axolotl/axolotl.hh
new file mode 100644
index 0000000..ead52fc
--- /dev/null
+++ b/include/axolotl/axolotl.hh
@@ -0,0 +1,114 @@
+
+#include "axolotl/crypto.hh"
+#include "axolotl/list.hh"
+
+namespace axolotl {
+
+typedef std::uint8_t SharedKey[32];
+
+
+struct ChainKey {
+ std::uint32_t index;
+ SharedKey key;
+};
+
+
+struct MessageKey {
+ std::uint32_t index;
+ Aes256Key cipher_key;
+ SharedKey mac_key;
+ Aes256Iv iv;
+};
+
+
+struct SenderChain {
+ Curve25519KeyPair ratchet_key;
+ ChainKey chain_key;
+};
+
+
+struct ReceiverChain {
+ Curve25519PublicKey ratchet_key;
+ ChainKey chain_key;
+};
+
+
+struct SkippedMessageKey {
+ Curve25519PublicKey ratchet_key;
+ MessageKey message_key;
+};
+
+
+enum struct ErrorCode {
+ SUCCESS = 0, /*!< There wasn't an error */
+ NOT_ENOUGH_RANDOM = 1, /*!< Not enough entropy was supplied */
+ OUTPUT_BUFFER_TOO_SMALL = 2, /*!< Supplied output buffer is too small */
+ BAD_MESSAGE_VERSION = 3, /*!< The message version is unsupported */
+ BAD_MESSAGE_FORMAT = 4, /*!< The message couldn't be decoded */
+ BAD_MESSAGE_MAC = 5, /*!< The message couldn't be decrypted */
+};
+
+
+static std::size_t const MAX_RECEIVER_CHAINS = 5;
+static std::size_t const MAX_SKIPPED_MESSAGE_KEYS = 40;
+
+
+struct KdfInfo {
+ std::uint8_t const * root_info;
+ std::size_t root_info_length;
+ std::uint8_t const * ratchet_info;
+ std::size_t ratchet_info_length;
+ std::uint8_t const * message_info;
+ std::size_t message_info_length;
+};
+
+
+struct Session {
+
+ Session(
+ KdfInfo const & kdf_info
+ );
+
+ /** A pair of string to feed into the KDF identifing the application */
+ KdfInfo kdf_info;
+ /** The last error that happened encypting or decrypting a message */
+ ErrorCode last_error;
+ SharedKey root_key;
+ List<SenderChain, 1> sender_chain;
+ List<ReceiverChain, MAX_RECEIVER_CHAINS> receiver_chains;
+ List<SkippedMessageKey, MAX_SKIPPED_MESSAGE_KEYS> skipped_message_keys;
+
+ void initialise_as_bob(
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
+ Curve25519PublicKey const & their_ratchet_key
+ );
+
+ void initialise_as_alice(
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
+ Curve25519KeyPair const & our_ratchet_key
+ );
+
+ std::size_t encrypt_max_output_length(
+ std::size_t plaintext_length
+ );
+
+ std::size_t encrypt_random_length();
+
+ std::size_t encrypt(
+ std::uint8_t const * plaintext, std::size_t plaintext_length,
+ std::uint8_t const * random, std::size_t random_length,
+ std::uint8_t * output, std::size_t max_output_length
+ );
+
+ std::size_t decrypt_max_plaintext_length(
+ std::size_t input_length
+ );
+
+ std::size_t decrypt(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * plaintext, std::size_t max_plaintext_length
+ );
+};
+
+
+} // namespace axololt
diff --git a/include/axolotl/crypto.hh b/include/axolotl/crypto.hh
new file mode 100644
index 0000000..f1e81ac
--- /dev/null
+++ b/include/axolotl/crypto.hh
@@ -0,0 +1,90 @@
+#include <cstdint>
+#include <cstddef>
+
+namespace axolotl {
+
+
+struct Curve25519PublicKey {
+ static const int LENGTH = 32;
+ std::uint8_t public_key[32];
+};
+
+
+struct Curve25519KeyPair : public Curve25519PublicKey {
+ std::uint8_t private_key[32];
+};
+
+
+void generate_key(
+ std::uint8_t const * random_32_bytes,
+ Curve25519KeyPair & key_pair
+);
+
+
+const std::size_t CURVE25519_SHARED_SECRET_LENGTH = 32;
+
+
+void curve25519_shared_secret(
+ Curve25519KeyPair const & our_key,
+ Curve25519PublicKey const & their_key,
+ std::uint8_t * output
+);
+
+
+struct Aes256Key {
+ static const int LENGTH = 32;
+ std::uint8_t key[32];
+};
+
+
+struct Aes256Iv {
+ static const int LENGTH = 16;
+ std::uint8_t iv[16];
+};
+
+
+std::size_t aes_encrypt_cbc_length(
+ std::size_t input_length
+);
+
+
+void aes_encrypt_cbc(
+ Aes256Key const & key,
+ Aes256Iv const & iv,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+);
+
+
+std::size_t aes_decrypt_cbc(
+ Aes256Key const & key,
+ Aes256Iv const & iv,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+);
+
+
+void sha256(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+);
+
+
+const std::size_t HMAC_SHA256_OUTPUT_LENGTH = 32;
+
+
+void hmac_sha256(
+ std::uint8_t const * key, std::size_t key_length,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+);
+
+
+void hkdf_sha256(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t const * info, std::size_t info_length,
+ std::uint8_t const * salt, std::size_t salt_length,
+ std::uint8_t * output, std::size_t output_length
+);
+
+} // namespace axolotl
diff --git a/include/axolotl/list.hh b/include/axolotl/list.hh
new file mode 100644
index 0000000..4c87630
--- /dev/null
+++ b/include/axolotl/list.hh
@@ -0,0 +1,100 @@
+#include <cstddef>
+
+namespace axolotl {
+
+template<typename T, std::size_t max_size>
+class List {
+public:
+ List() : _end(_data) {}
+
+ typedef T * iterator;
+ typedef T const * const_iterator;
+
+ T * begin() { return _data; }
+ T * end() { return _end; }
+ T const * begin() const { return _data; }
+ T const * end() const { return _end; }
+
+ /**
+ * Is the list empty?
+ */
+ bool empty() const { return _end == _data; }
+
+ /**
+ * The number of items in the list.
+ */
+ std::size_t size() const { return _end - _data; }
+
+ T & operator[](std::size_t index) { return _data[index]; }
+
+ T const & operator[](std::size_t index) const { return _data[index]; }
+
+ /**
+ * Erase the item from the list at the given position.
+ */
+ void erase(T * pos) {
+ --_end;
+ while (pos != _end) {
+ *pos = *(pos + 1);
+ ++pos;
+ }
+ }
+
+ /**
+ * Make space for an item in the list at a given position.
+ * If inserting the item makes the list longer than max_size then
+ * the end of the list is discarded.
+ * Returns the where the item is inserted.
+ */
+ T * insert(T * pos) {
+ if (_end != _data + max_size) {
+ ++_end;
+ } else if (pos == _end) {
+ --pos;
+ }
+ T * tmp = pos;
+ while (tmp != _end - 1) {
+ *(tmp + 1) = *tmp;
+ ++tmp;
+ }
+ return pos;
+ }
+
+ /**
+ * Make space for an item in the list at the start of the list
+ */
+ T * insert() { return insert(begin()); }
+
+ /**
+ * Insert an item into the list at a given position.
+ * If inserting the item makes the list longer than max_size then
+ * the end of the list is discarded.
+ * Returns the where the item is inserted.
+ */
+ T * insert(T * pos, T const & value) {
+ pos = insert(pos);
+ *pos = value;
+ return pos;
+ }
+
+ List<T, max_size> & operator=(List<T, max_size> const & other) {
+ if (this = &other) {
+ return *this;
+ }
+ T * this_pos = _data;
+ T * const other_pos = other._data;
+ while (other_pos != other._end) {
+ *this_pos = *other;
+ ++this_pos;
+ ++other_pos;
+ }
+ _end = this_pos;
+ return *this;
+ }
+
+private:
+ T * _end;
+ T _data[max_size];
+};
+
+} // namespace axolotl
diff --git a/include/axolotl/message.hh b/include/axolotl/message.hh
new file mode 100644
index 0000000..ac4b3e0
--- /dev/null
+++ b/include/axolotl/message.hh
@@ -0,0 +1,62 @@
+#include <cstddef>
+#include <cstdint>
+
+
+namespace axolotl {
+
+/**
+ * The length of the buffer needed to hold a message.
+ */
+std::size_t encode_message_length(
+ std::uint32_t counter,
+ std::size_t ratchet_key_length,
+ std::size_t ciphertext_length,
+ std::size_t mac_length
+);
+
+
+struct MessageWriter {
+ std::size_t body_length;
+ std::uint8_t * ratchet_key;
+ std::uint8_t * ciphertext;
+ std::uint8_t * mac;
+};
+
+
+struct MessageReader {
+ std::size_t body_length;
+ std::uint8_t version;
+ std::uint32_t counter;
+ std::size_t ratchet_key_length;
+ std::size_t ciphertext_length;
+ std::uint8_t const * ratchet_key;
+ std::uint8_t const * ciphertext;
+ std::uint8_t const * mac;
+};
+
+
+/**
+ * Writes the message headers into the output buffer.
+ * Returns a writer struct populated with pointers into the output buffer.
+ */
+MessageWriter encode_message(
+ std::uint8_t version,
+ std::uint32_t counter,
+ std::size_t ratchet_key_length,
+ std::size_t ciphertext_length,
+ std::uint8_t * output
+);
+
+
+/**
+ * Reads the message headers from the input buffer.
+ * Returns a reader struct populated with pointers into the input buffer.
+ * On failure the returned body_length will be 0.
+ */
+MessageReader decode_message(
+ std::uint8_t const * input, std::size_t input_length,
+ std::size_t mac_length
+);
+
+
+} // namespace axolotl
diff --git a/.gitignore b/lib/curve25519-donna/.gitignore
index ccabede..ccabede 100644
--- a/.gitignore
+++ b/lib/curve25519-donna/.gitignore
diff --git a/LICENSE.md b/lib/curve25519-donna/LICENSE.md
index 33a3240..33a3240 100644
--- a/LICENSE.md
+++ b/lib/curve25519-donna/LICENSE.md
diff --git a/Makefile b/lib/curve25519-donna/Makefile
index e31fcca..e31fcca 100644
--- a/Makefile
+++ b/lib/curve25519-donna/Makefile
diff --git a/README b/lib/curve25519-donna/README
index 9adf9be..9adf9be 100644
--- a/README
+++ b/lib/curve25519-donna/README
diff --git a/contrib/Curve25519Donna.c b/lib/curve25519-donna/contrib/Curve25519Donna.c
index 71b816c..71b816c 100644
--- a/contrib/Curve25519Donna.c
+++ b/lib/curve25519-donna/contrib/Curve25519Donna.c
diff --git a/contrib/Curve25519Donna.h b/lib/curve25519-donna/contrib/Curve25519Donna.h
index 3cd4ca0..3cd4ca0 100644
--- a/contrib/Curve25519Donna.h
+++ b/lib/curve25519-donna/contrib/Curve25519Donna.h
diff --git a/contrib/Curve25519Donna.java b/lib/curve25519-donna/contrib/Curve25519Donna.java
index e28cb53..e28cb53 100644
--- a/contrib/Curve25519Donna.java
+++ b/lib/curve25519-donna/contrib/Curve25519Donna.java
diff --git a/contrib/make-snippets b/lib/curve25519-donna/contrib/make-snippets
index 4568721..4568721 100644
--- a/contrib/make-snippets
+++ b/lib/curve25519-donna/contrib/make-snippets
diff --git a/curve25519-donna-c64.c b/lib/curve25519-donna/curve25519-donna-c64.c
index 9ebd8a1..9ebd8a1 100644
--- a/curve25519-donna-c64.c
+++ b/lib/curve25519-donna/curve25519-donna-c64.c
diff --git a/curve25519-donna.c b/lib/curve25519-donna/curve25519-donna.c
index ed15d6c..ed15d6c 100644
--- a/curve25519-donna.c
+++ b/lib/curve25519-donna/curve25519-donna.c
diff --git a/curve25519-donna.podspec b/lib/curve25519-donna/curve25519-donna.podspec
index 0f2f31a..0f2f31a 100644
--- a/curve25519-donna.podspec
+++ b/lib/curve25519-donna/curve25519-donna.podspec
diff --git a/python-src/curve25519/__init__.py b/lib/curve25519-donna/python-src/curve25519/__init__.py
index 873ff57..873ff57 100644
--- a/python-src/curve25519/__init__.py
+++ b/lib/curve25519-donna/python-src/curve25519/__init__.py
diff --git a/python-src/curve25519/curve25519module.c b/lib/curve25519-donna/python-src/curve25519/curve25519module.c
index e309ec0..e309ec0 100644
--- a/python-src/curve25519/curve25519module.c
+++ b/lib/curve25519-donna/python-src/curve25519/curve25519module.c
diff --git a/python-src/curve25519/keys.py b/lib/curve25519-donna/python-src/curve25519/keys.py
index e131dac..e131dac 100644
--- a/python-src/curve25519/keys.py
+++ b/lib/curve25519-donna/python-src/curve25519/keys.py
diff --git a/python-src/curve25519/test/__init__.py b/lib/curve25519-donna/python-src/curve25519/test/__init__.py
index e69de29..e69de29 100644
--- a/python-src/curve25519/test/__init__.py
+++ b/lib/curve25519-donna/python-src/curve25519/test/__init__.py
diff --git a/python-src/curve25519/test/test_curve25519.py b/lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py
index 2ecbd47..2ecbd47 100755
--- a/python-src/curve25519/test/test_curve25519.py
+++ b/lib/curve25519-donna/python-src/curve25519/test/test_curve25519.py
diff --git a/python-src/curve25519/test/test_speed.py b/lib/curve25519-donna/python-src/curve25519/test/test_speed.py
index 87952fa..87952fa 100755
--- a/python-src/curve25519/test/test_speed.py
+++ b/lib/curve25519-donna/python-src/curve25519/test/test_speed.py
diff --git a/setup.py b/lib/curve25519-donna/setup.py
index dc1b8eb..dc1b8eb 100755
--- a/setup.py
+++ b/lib/curve25519-donna/setup.py
diff --git a/speed-curve25519.c b/lib/curve25519-donna/speed-curve25519.c
index d945d48..d945d48 100644
--- a/speed-curve25519.c
+++ b/lib/curve25519-donna/speed-curve25519.c
diff --git a/test-curve25519.c b/lib/curve25519-donna/test-curve25519.c
index 591d871..591d871 100644
--- a/test-curve25519.c
+++ b/lib/curve25519-donna/test-curve25519.c
diff --git a/test-noncanon.c b/lib/curve25519-donna/test-noncanon.c
index 6de4e8d..6de4e8d 100644
--- a/test-noncanon.c
+++ b/lib/curve25519-donna/test-noncanon.c
diff --git a/test-sc-curve25519.c b/lib/curve25519-donna/test-sc-curve25519.c
index 14a7e3c..14a7e3c 100644
--- a/test-sc-curve25519.c
+++ b/lib/curve25519-donna/test-sc-curve25519.c
diff --git a/test-sc-curve25519.s b/lib/curve25519-donna/test-sc-curve25519.s
index 1da4f68..1da4f68 100644
--- a/test-sc-curve25519.s
+++ b/lib/curve25519-donna/test-sc-curve25519.s
diff --git a/src/axolotl.cpp b/src/axolotl.cpp
new file mode 100644
index 0000000..7384d79
--- /dev/null
+++ b/src/axolotl.cpp
@@ -0,0 +1,432 @@
+#include "axolotl/axolotl.hh"
+#include "axolotl/message.hh"
+
+#include <cstring>
+
+namespace {
+
+std::uint8_t PROTOCOL_VERSION = 3;
+std::size_t MAC_LENGTH = 8;
+std::size_t KEY_LENGTH = axolotl::Curve25519PublicKey::LENGTH;
+std::uint8_t MESSAGE_KEY_SEED[1] = {0x01};
+std::uint8_t CHAIN_KEY_SEED[1] = {0x02};
+std::size_t MAX_MESSAGE_GAP = 2000;
+
+template<typename T>
+void unset(
+ T & value
+) {
+ std::memset(&value, 0, sizeof(T));
+}
+
+
+void create_chain_key(
+ axolotl::SharedKey const & root_key,
+ axolotl::Curve25519KeyPair const & our_key,
+ axolotl::Curve25519PublicKey const & their_key,
+ axolotl::KdfInfo const & info,
+ axolotl::SharedKey & new_root_key,
+ axolotl::ChainKey & new_chain_key
+) {
+ axolotl::SharedKey secret;
+ axolotl::curve25519_shared_secret(our_key, their_key, secret);
+ std::uint8_t derived_secrets[64];
+ axolotl::hkdf_sha256(
+ secret, sizeof(secret),
+ root_key, sizeof(root_key),
+ info.ratchet_info, info.ratchet_info_length,
+ derived_secrets, sizeof(derived_secrets)
+ );
+ std::memcpy(new_root_key, derived_secrets, 32);
+ std::memcpy(new_chain_key.key, derived_secrets + 32, 32);
+ new_chain_key.index = 0;
+ unset(derived_secrets);
+ unset(secret);
+}
+
+
+void advance_chain_key(
+ axolotl::ChainKey const & chain_key,
+ axolotl::ChainKey & new_chain_key
+) {
+ axolotl::hmac_sha256(
+ chain_key.key, sizeof(chain_key.key),
+ CHAIN_KEY_SEED, sizeof(CHAIN_KEY_SEED),
+ new_chain_key.key
+ );
+ new_chain_key.index = chain_key.index + 1;
+}
+
+
+void create_message_keys(
+ axolotl::ChainKey const & chain_key,
+ axolotl::KdfInfo const & info,
+ axolotl::MessageKey & message_key
+) {
+ axolotl::SharedKey secret;
+ axolotl::hmac_sha256(
+ chain_key.key, sizeof(chain_key.key),
+ MESSAGE_KEY_SEED, sizeof(MESSAGE_KEY_SEED),
+ secret
+ );
+ std::uint8_t derived_secrets[80];
+ axolotl::hkdf_sha256(
+ secret, sizeof(secret),
+ NULL, 0,
+ info.message_info, info.message_info_length,
+ derived_secrets, sizeof(derived_secrets)
+ );
+ std::memcpy(message_key.cipher_key.key, derived_secrets, 32);
+ std::memcpy(message_key.mac_key, derived_secrets + 32, 32);
+ std::memcpy(message_key.iv.iv, derived_secrets + 64, 16);
+ message_key.index = chain_key.index;
+ unset(derived_secrets);
+ unset(secret);
+}
+
+
+bool verify_mac(
+ axolotl::MessageKey const & message_key,
+ std::uint8_t const * input,
+ axolotl::MessageReader const & reader
+) {
+ std::uint8_t mac[axolotl::HMAC_SHA256_OUTPUT_LENGTH];
+ axolotl::hmac_sha256(
+ message_key.mac_key, sizeof(message_key.mac_key),
+ input, reader.body_length,
+ mac
+ );
+
+ bool result = std::memcmp(mac, reader.mac, MAC_LENGTH) == 0;
+ unset(mac);
+ return result;
+}
+
+
+bool verify_mac_for_existing_chain(
+ axolotl::Session const & session,
+ axolotl::ChainKey const & chain,
+ std::uint8_t const * input,
+ axolotl::MessageReader const & reader
+) {
+ if (reader.counter < chain.index) {
+ return false;
+ }
+
+ /* Limit the number of hashes we're prepared to compute */
+ if (reader.counter - chain.index > MAX_MESSAGE_GAP) {
+ return false;
+ }
+
+ axolotl::ChainKey new_chain = chain;
+
+ while (new_chain.index < reader.counter) {
+ advance_chain_key(new_chain, new_chain);
+ }
+
+ axolotl::MessageKey message_key;
+ create_message_keys(new_chain, session.kdf_info, message_key);
+
+ bool result = verify_mac(message_key, input, reader);
+ unset(new_chain);
+ return result;
+}
+
+
+bool verify_mac_for_new_chain(
+ axolotl::Session const & session,
+ std::uint8_t const * input,
+ axolotl::MessageReader const & reader
+) {
+ axolotl::SharedKey new_root_key;
+ axolotl::ReceiverChain new_chain;
+
+ /* They shouldn't move to a new chain until we've sent them a message
+ * acknowledging the last one */
+ if (session.sender_chain.empty()) {
+ return false;
+ }
+
+ /* Limit the number of hashes we're prepared to compute */
+ if (reader.counter > MAX_MESSAGE_GAP) {
+ return false;
+ }
+ std::memcpy(
+ new_chain.ratchet_key.public_key, reader.ratchet_key, KEY_LENGTH
+ );
+
+ create_chain_key(
+ session.root_key, session.sender_chain[0].ratchet_key,
+ new_chain.ratchet_key, session.kdf_info,
+ new_root_key, new_chain.chain_key
+ );
+
+ bool result = verify_mac_for_existing_chain(
+ session, new_chain.chain_key, input, reader
+ );
+ unset(new_root_key);
+ unset(new_chain);
+ return result;
+}
+
+} // namespace
+
+
+axolotl::Session::Session(
+ axolotl::KdfInfo const & kdf_info
+) : kdf_info(kdf_info), last_error(axolotl::ErrorCode::SUCCESS) {
+}
+
+
+void axolotl::Session::initialise_as_bob(
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
+ axolotl::Curve25519PublicKey const & their_ratchet_key
+) {
+ std::uint8_t derived_secrets[64];
+ axolotl::hkdf_sha256(
+ shared_secret, shared_secret_length,
+ NULL, 0,
+ kdf_info.root_info, kdf_info.root_info_length,
+ derived_secrets, sizeof(derived_secrets)
+ );
+ receiver_chains.insert();
+ std::memcpy(root_key, derived_secrets, 32);
+ std::memcpy(receiver_chains[0].chain_key.key, derived_secrets + 32, 32);
+ receiver_chains[0].ratchet_key = their_ratchet_key;
+ unset(derived_secrets);
+}
+
+
+void axolotl::Session::initialise_as_alice(
+ std::uint8_t const * shared_secret, std::size_t shared_secret_length,
+ axolotl::Curve25519KeyPair const & our_ratchet_key
+) {
+ std::uint8_t derived_secrets[64];
+ axolotl::hkdf_sha256(
+ shared_secret, shared_secret_length,
+ NULL, 0,
+ kdf_info.root_info, kdf_info.root_info_length,
+ derived_secrets, sizeof(derived_secrets)
+ );
+ sender_chain.insert();
+ std::memcpy(root_key, derived_secrets, 32);
+ std::memcpy(sender_chain[0].chain_key.key, derived_secrets + 32, 32);
+ sender_chain[0].ratchet_key = our_ratchet_key;
+ unset(derived_secrets);
+}
+
+
+std::size_t axolotl::Session::encrypt_max_output_length(
+ std::size_t plaintext_length
+) {
+ std::size_t counter = 0;
+ if (!sender_chain.empty()) {
+ counter = sender_chain[0].chain_key.index;
+ }
+ std::size_t padded = axolotl::aes_encrypt_cbc_length(plaintext_length);
+ return axolotl::encode_message_length(
+ counter, KEY_LENGTH, padded, MAC_LENGTH
+ );
+}
+
+
+std::size_t axolotl::Session::encrypt_random_length() {
+ return sender_chain.empty() ? KEY_LENGTH : 0;
+}
+
+
+std::size_t axolotl::Session::encrypt(
+ std::uint8_t const * plaintext, std::size_t plaintext_length,
+ std::uint8_t const * random, std::size_t random_length,
+ std::uint8_t * output, std::size_t max_output_length
+) {
+ if (random_length < encrypt_random_length()) {
+ last_error = axolotl::ErrorCode::NOT_ENOUGH_RANDOM;
+ return std::size_t(-1);
+ }
+ if (max_output_length < encrypt_max_output_length(plaintext_length)) {
+ last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
+ return std::size_t(-1);
+ }
+
+ if (sender_chain.empty()) {
+ sender_chain.insert();
+ axolotl::generate_key(random, sender_chain[0].ratchet_key);
+ create_chain_key(
+ root_key,
+ sender_chain[0].ratchet_key,
+ receiver_chains[0].ratchet_key,
+ kdf_info,
+ root_key, sender_chain[0].chain_key
+ );
+ }
+
+ MessageKey keys;
+ create_message_keys(sender_chain[0].chain_key, kdf_info, keys);
+ advance_chain_key(sender_chain[0].chain_key, sender_chain[0].chain_key);
+
+ std::size_t padded = axolotl::aes_encrypt_cbc_length(plaintext_length);
+ std::uint32_t counter = keys.index;
+ const Curve25519PublicKey &ratchet_key = sender_chain[0].ratchet_key;
+
+ axolotl::MessageWriter writer(axolotl::encode_message(
+ PROTOCOL_VERSION, counter, KEY_LENGTH, padded, output
+ ));
+
+ std::memcpy(writer.ratchet_key, ratchet_key.public_key, KEY_LENGTH);
+
+ axolotl::aes_encrypt_cbc(
+ keys.cipher_key, keys.iv,
+ plaintext, plaintext_length,
+ writer.ciphertext
+ );
+
+ std::uint8_t mac[axolotl::HMAC_SHA256_OUTPUT_LENGTH];
+ axolotl::hmac_sha256(
+ keys.mac_key, sizeof(keys.mac_key),
+ output, writer.body_length,
+ mac
+ );
+ std::memcpy(writer.mac, mac, MAC_LENGTH);
+
+ unset(keys);
+ return writer.body_length + MAC_LENGTH;
+}
+
+
+std::size_t axolotl::Session::decrypt_max_plaintext_length(
+ std::size_t input_length
+) {
+ return input_length;
+}
+
+
+std::size_t axolotl::Session::decrypt(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * plaintext, std::size_t max_plaintext_length
+) {
+ if (max_plaintext_length < decrypt_max_plaintext_length(input_length)) {
+ last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL;
+ return std::size_t(-1);
+ }
+
+ axolotl::MessageReader reader(axolotl::decode_message(
+ input, input_length, MAC_LENGTH
+ ));
+
+ if (reader.version != PROTOCOL_VERSION) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_VERSION;
+ return std::size_t(-1);
+ }
+
+ if (reader.body_length == 0 || reader.ratchet_key_length != KEY_LENGTH) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_FORMAT;
+ return std::size_t(-1);
+ }
+
+ ReceiverChain * chain = NULL;
+ for (axolotl::ReceiverChain & receiver_chain : receiver_chains) {
+ if (0 == std::memcmp(
+ receiver_chain.ratchet_key.public_key, reader.ratchet_key,
+ KEY_LENGTH
+ )) {
+ chain = &receiver_chain;
+ break;
+ }
+ }
+
+ if (!chain) {
+ if (!verify_mac_for_new_chain(*this, input, reader)) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return std::size_t(-1);
+ }
+ } else {
+ if (chain->chain_key.index > reader.counter) {
+ /* Chain already advanced beyond the key for this message
+ * Check if the message keys are in the skipped key list. */
+ for (axolotl::SkippedMessageKey & skipped : skipped_message_keys) {
+ if (reader.counter == skipped.message_key.index
+ && 0 == std::memcmp(
+ skipped.ratchet_key.public_key, reader.ratchet_key,
+ KEY_LENGTH
+ )
+ ) {
+ /* Found the key for this message. Check the MAC. */
+ if (!verify_mac(skipped.message_key, input, reader)) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return std::size_t(-1);
+ }
+
+ std::size_t result = axolotl::aes_decrypt_cbc(
+ skipped.message_key.cipher_key,
+ skipped.message_key.iv,
+ reader.ciphertext, reader.ciphertext_length,
+ plaintext
+ );
+
+ if (result == std::size_t(-1)) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return result;
+ }
+
+ /* Remove the key from the skipped keys now that we've
+ * decoded the message it corresponds to. */
+ unset(skipped);
+ skipped_message_keys.erase(&skipped);
+ return result;
+ }
+ }
+ /* No matching keys for the message, fail with bad mac */
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return std::size_t(-1);
+ } else if (!verify_mac_for_existing_chain(
+ *this, chain->chain_key, input, reader
+ )) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return std::size_t(-1);
+ }
+ }
+
+ if (!chain) {
+ /* They have started using a new empheral ratchet key.
+ * We need to derive a new set of chain keys.
+ * We can discard our previous empheral ratchet key.
+ * We will generate a new key when we send the next message. */
+ chain = receiver_chains.insert();
+ std::memcpy(
+ chain->ratchet_key.public_key, reader.ratchet_key, KEY_LENGTH
+ );
+ create_chain_key(
+ root_key, sender_chain[0].ratchet_key, chain->ratchet_key,
+ kdf_info, root_key, chain->chain_key
+ );
+ unset(sender_chain[0]);
+ sender_chain.erase(sender_chain.begin());
+ }
+
+ while (chain->chain_key.index < reader.counter) {
+ axolotl::SkippedMessageKey & key = *skipped_message_keys.insert();
+ create_message_keys(chain->chain_key, kdf_info, key.message_key);
+ key.ratchet_key = chain->ratchet_key;
+ advance_chain_key(chain->chain_key, chain->chain_key);
+ }
+
+ axolotl::MessageKey message_key;
+ create_message_keys(chain->chain_key, kdf_info, message_key);
+ std::size_t result = axolotl::aes_decrypt_cbc(
+ message_key.cipher_key,
+ message_key.iv,
+ reader.ciphertext, reader.ciphertext_length,
+ plaintext
+ );
+ unset(message_key);
+
+ advance_chain_key(chain->chain_key, chain->chain_key);
+
+ if (result == std::size_t(-1)) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_MAC;
+ return std::size_t(-1);
+ } else {
+ return result;
+ }
+}
diff --git a/src/crypto.cpp b/src/crypto.cpp
new file mode 100644
index 0000000..2397d7c
--- /dev/null
+++ b/src/crypto.cpp
@@ -0,0 +1,234 @@
+#include "axolotl/crypto.hh"
+#include <cstring>
+
+extern "C" {
+
+int curve25519_donna(
+ uint8_t * output,
+ const uint8_t * secret,
+ const uint8_t * basepoint
+);
+
+#include "crypto-algorithms/aes.h"
+#include "crypto-algorithms/sha256.h"
+
+}
+
+
+namespace {
+
+static const std::uint8_t CURVE25519_BASEPOINT[32] = {9};
+static const std::size_t AES_BLOCK_LENGTH = 16;
+static const std::size_t SHA256_HASH_LENGTH = 32;
+static const std::size_t SHA256_BLOCK_LENGTH = 64;
+static const std::uint8_t HKDF_DEFAULT_SALT[32] = {};
+
+
+template<std::size_t block_size>
+inline static void xor_block(
+ std::uint8_t * block,
+ std::uint8_t const * input
+) {
+ for (std::size_t i = 0; i < block_size; ++i) {
+ block[i] ^= input[i];
+ }
+}
+
+
+inline static void hmac_sha256_key(
+ std::uint8_t const * input_key, std::size_t input_key_length,
+ std::uint8_t * hmac_key
+) {
+ std::memset(hmac_key, 0, SHA256_BLOCK_LENGTH);
+ if (input_key_length > SHA256_BLOCK_LENGTH) {
+ ::SHA256_CTX context;
+ ::sha256_init(&context);
+ ::sha256_update(&context, input_key, input_key_length);
+ ::sha256_final(&context, hmac_key);
+ } else {
+ std::memcpy(hmac_key, input_key, input_key_length);
+ }
+}
+
+
+inline void hmac_sha256_init(
+ ::SHA256_CTX * context,
+ std::uint8_t const * hmac_key
+) {
+ std::uint8_t i_pad[SHA256_BLOCK_LENGTH];
+ std::memcpy(i_pad, hmac_key, SHA256_BLOCK_LENGTH);
+ for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) {
+ i_pad[i] ^= 0x36;
+ }
+ ::sha256_init(context);
+ ::sha256_update(context, i_pad, SHA256_BLOCK_LENGTH);
+ std::memset(i_pad, 0, sizeof(i_pad));
+}
+
+
+inline void hmac_sha256_final(
+ ::SHA256_CTX * context,
+ std::uint8_t const * hmac_key,
+ std::uint8_t * output
+) {
+ std::uint8_t o_pad[SHA256_BLOCK_LENGTH + SHA256_HASH_LENGTH];
+ std::memcpy(o_pad, hmac_key, SHA256_BLOCK_LENGTH);
+ for (std::size_t i = 0; i < SHA256_BLOCK_LENGTH; ++i) {
+ o_pad[i] ^= 0x5C;
+ }
+ ::sha256_final(context, o_pad + SHA256_BLOCK_LENGTH);
+ ::SHA256_CTX final_context;
+ ::sha256_init(&final_context);
+ ::sha256_update(&final_context, o_pad, sizeof(o_pad));
+ ::sha256_final(&final_context, output);
+ std::memset(o_pad, 0, sizeof(o_pad));
+}
+
+} // namespace
+
+
+void axolotl::generate_key(
+ std::uint8_t const * random_32_bytes,
+ axolotl::Curve25519KeyPair & key_pair
+) {
+ std::memcpy(key_pair.private_key, random_32_bytes, 32);
+ ::curve25519_donna(
+ key_pair.public_key, key_pair.private_key, CURVE25519_BASEPOINT
+ );
+}
+
+
+void axolotl::curve25519_shared_secret(
+ axolotl::Curve25519KeyPair const & our_key,
+ axolotl::Curve25519PublicKey const & their_key,
+ std::uint8_t * output
+) {
+ ::curve25519_donna(output, our_key.private_key, their_key.public_key);
+}
+
+
+std::size_t axolotl::aes_encrypt_cbc_length(
+ std::size_t input_length
+) {
+ return input_length + AES_BLOCK_LENGTH - input_length % AES_BLOCK_LENGTH;
+}
+
+
+void axolotl::aes_encrypt_cbc(
+ axolotl::Aes256Key const & key,
+ axolotl::Aes256Iv const & iv,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+) {
+ std::uint32_t key_schedule[60];
+ ::aes_key_setup(key.key, key_schedule, 256);
+ std::uint8_t input_block[AES_BLOCK_LENGTH];
+ std::memcpy(input_block, iv.iv, AES_BLOCK_LENGTH);
+ while (input_length >= AES_BLOCK_LENGTH) {
+ xor_block<AES_BLOCK_LENGTH>(input_block, input);
+ ::aes_encrypt(input_block, output, key_schedule, 256);
+ std::memcpy(input_block, output, AES_BLOCK_LENGTH);
+ input += AES_BLOCK_LENGTH;
+ output += AES_BLOCK_LENGTH;
+ input_length -= AES_BLOCK_LENGTH;
+ }
+ std::size_t i = 0;
+ for (; i < input_length; ++i) {
+ input_block[i] ^= input[i];
+ }
+ for (; i < AES_BLOCK_LENGTH; ++i) {
+ input_block[i] ^= AES_BLOCK_LENGTH - input_length;
+ }
+ ::aes_encrypt(input_block, output, key_schedule, 256);
+ std::memset(key_schedule, 0, sizeof(key_schedule));
+ std::memset(input_block, 0, sizeof(AES_BLOCK_LENGTH));
+}
+
+
+std::size_t axolotl::aes_decrypt_cbc(
+ axolotl::Aes256Key const & key,
+ axolotl::Aes256Iv const & iv,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+) {
+ std::uint32_t key_schedule[60];
+ ::aes_key_setup(key.key, key_schedule, 256);
+ for (std::size_t i = 0; i < input_length; i += AES_BLOCK_LENGTH) {
+ ::aes_decrypt(&input[i], &output[i], key_schedule, 256);
+ if (i == 0) {
+ xor_block<AES_BLOCK_LENGTH>(&output[i], iv.iv);
+ } else {
+ xor_block<AES_BLOCK_LENGTH>(&output[i], &input[i - AES_BLOCK_LENGTH]);
+ }
+ }
+ std::memset(key_schedule, 0, sizeof(key_schedule));
+ std::size_t padding = output[input_length - 1];
+ return (padding > input_length) ? std::size_t(-1) : (input_length - padding);
+}
+
+
+void axolotl::sha256(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+) {
+ ::SHA256_CTX context;
+ ::sha256_init(&context);
+ ::sha256_update(&context, input, input_length);
+ ::sha256_final(&context, output);
+}
+
+void axolotl::hmac_sha256(
+ std::uint8_t const * key, std::size_t key_length,
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t * output
+) {
+ std::uint8_t hmac_key[SHA256_BLOCK_LENGTH];
+ ::SHA256_CTX context;
+ hmac_sha256_key(key, key_length, hmac_key);
+ hmac_sha256_init(&context, hmac_key);
+ ::sha256_update(&context, input, input_length);
+ hmac_sha256_final(&context, hmac_key, output);
+ std::memset(hmac_key, 0, sizeof(hmac_key));
+}
+
+
+void axolotl::hkdf_sha256(
+ std::uint8_t const * input, std::size_t input_length,
+ std::uint8_t const * salt, std::size_t salt_length,
+ std::uint8_t const * info, std::size_t info_length,
+ std::uint8_t * output, std::size_t output_length
+) {
+ ::SHA256_CTX context;
+ std::uint8_t hmac_key[SHA256_BLOCK_LENGTH];
+ std::uint8_t step_result[SHA256_HASH_LENGTH];
+ std::size_t bytes_remaining = output_length;
+ std::uint8_t iteration = 1;
+ if (!salt) {
+ salt = HKDF_DEFAULT_SALT;
+ salt_length = sizeof(HKDF_DEFAULT_SALT);
+ }
+ /* Expand */
+ hmac_sha256_key(salt, salt_length, hmac_key);
+ hmac_sha256_init(&context, hmac_key);
+ ::sha256_update(&context, input, input_length);
+ hmac_sha256_final(&context, hmac_key, step_result);
+ hmac_sha256_key(step_result, SHA256_HASH_LENGTH, hmac_key);
+
+ /* Extract */
+ hmac_sha256_init(&context, hmac_key);
+ ::sha256_update(&context, info, info_length);
+ ::sha256_update(&context, &iteration, 1);
+ hmac_sha256_final(&context, hmac_key, step_result);
+ while (bytes_remaining > SHA256_HASH_LENGTH) {
+ std::memcpy(output, step_result, SHA256_HASH_LENGTH);
+ output += SHA256_HASH_LENGTH;
+ bytes_remaining -= SHA256_HASH_LENGTH;
+ iteration ++;
+ hmac_sha256_init(&context, hmac_key);
+ ::sha256_update(&context, step_result, SHA256_HASH_LENGTH);
+ ::sha256_update(&context, info, info_length);
+ ::sha256_update(&context, &iteration, 1);
+ hmac_sha256_final(&context, hmac_key, step_result);
+ }
+ std::memcpy(output, step_result, bytes_remaining);
+}
diff --git a/src/libs.cpp b/src/libs.cpp
new file mode 100644
index 0000000..5b9c2aa
--- /dev/null
+++ b/src/libs.cpp
@@ -0,0 +1,5 @@
+extern "C" {
+#include "crypto-algorithms/sha256.c"
+#include "crypto-algorithms/aes.c"
+#include "curve25519-donna/curve25519-donna.c"
+}
diff --git a/src/message.cpp b/src/message.cpp
new file mode 100644
index 0000000..18dbe0e
--- /dev/null
+++ b/src/message.cpp
@@ -0,0 +1,170 @@
+#include "axolotl/message.hh"
+
+namespace {
+
+template<typename T>
+std::size_t varint_length(
+ T value
+) {
+ std::size_t result = 1;
+ while (value > 128U) {
+ ++result;
+ value >>= 7;
+ }
+ return result;
+}
+
+
+template<typename T>
+std::uint8_t * varint_encode(
+ std::uint8_t * output,
+ T value
+) {
+ while (value > 128U) {
+ *(output++) = (0x7F & value) | 0x80;
+ }
+ (*output++) = value;
+ return output;
+}
+
+
+template<typename T>
+T varint_decode(
+ std::uint8_t const * varint_start,
+ std::uint8_t const * varint_end
+) {
+ T value = 0;
+ do {
+ value <<= 7;
+ value |= 0x7F & *(--varint_end);
+ } while (varint_end != varint_start);
+ return value;
+}
+
+
+std::uint8_t const * varint_skip(
+ std::uint8_t const * input,
+ std::uint8_t const * input_end
+) {
+ while (input != input_end) {
+ std::uint8_t tmp = *(input++);
+ if ((tmp & 0x80) == 0) {
+ return input;
+ }
+ }
+ return input;
+}
+
+
+std::size_t varstring_length(
+ std::size_t string_length
+) {
+ return varint_length(string_length) + string_length;
+}
+
+static std::size_t const VERSION_LENGTH = 1;
+static std::uint8_t const RATCHET_KEY_TAG = 012;
+static std::uint8_t const COUNTER_TAG = 020;
+static std::uint8_t const CIPHERTEXT_TAG = 042;
+
+} // namespace
+
+
+std::size_t axolotl::encode_message_length(
+ std::uint32_t counter,
+ std::size_t ratchet_key_length,
+ std::size_t ciphertext_length,
+ std::size_t mac_length
+) {
+ std::size_t length = VERSION_LENGTH;
+ length += 1 + varstring_length(ratchet_key_length);
+ length += 1 + varint_length(counter);
+ length += 1 + varstring_length(ciphertext_length);
+ return length + mac_length;
+}
+
+
+axolotl::MessageWriter axolotl::encode_message(
+ std::uint8_t version,
+ std::uint32_t counter,
+ std::size_t ratchet_key_length,
+ std::size_t ciphertext_length,
+ std::uint8_t * output
+) {
+ axolotl::MessageWriter result;
+ std::uint8_t * pos = output;
+ *(pos++) = version;
+ *(pos++) = COUNTER_TAG;
+ pos = varint_encode(pos, counter);
+ *(pos++) = RATCHET_KEY_TAG;
+ pos = varint_encode(pos, ratchet_key_length);
+ result.ratchet_key = pos;
+ pos += ratchet_key_length;
+ *(pos++) = CIPHERTEXT_TAG;
+ pos = varint_encode(pos, ciphertext_length);
+ result.ciphertext = pos;
+ pos += ciphertext_length;
+ result.body_length = pos - output;
+ result.mac = pos;
+ return result;
+}
+
+
+axolotl::MessageReader axolotl::decode_message(
+ std::uint8_t const * input, std::size_t input_length,
+ std::size_t mac_length
+) {
+ axolotl::MessageReader result;
+ result.body_length = 0;
+ std::uint8_t const * pos = input;
+ std::uint8_t const * end = input + input_length - mac_length;
+ std::uint8_t flags = 0;
+ result.mac = end;
+ if (pos == end) return result;
+ result.version = *(pos++);
+ while (pos != end) {
+ uint8_t tag = *(pos);
+ if (tag == COUNTER_TAG) {
+ ++pos;
+ std::uint8_t const * counter_start = pos;
+ pos = varint_skip(pos, end);
+ result.counter = varint_decode<std::uint32_t>(counter_start, pos);
+ flags |= 1;
+ } else if (tag == RATCHET_KEY_TAG) {
+ ++pos;
+ std::uint8_t const * len_start = pos;
+ pos = varint_skip(pos, end);
+ std::size_t len = varint_decode<std::size_t>(len_start, pos);
+ if (len > end - pos) return result;
+ result.ratchet_key_length = len;
+ result.ratchet_key = pos;
+ pos += len;
+ flags |= 2;
+ } else if (tag == CIPHERTEXT_TAG) {
+ ++pos;
+ std::uint8_t const * len_start = pos;
+ pos = varint_skip(pos, end);
+ std::size_t len = varint_decode<std::size_t>(len_start, pos);
+ if (len > end - pos) return result;
+ result.ciphertext_length = len;
+ result.ciphertext = pos;
+ pos += len;
+ flags |= 4;
+ } else if (tag & 0x7 == 0) {
+ pos = varint_skip(pos, end);
+ pos = varint_skip(pos, end);
+ } else if (tag & 0x7 == 2) {
+ std::uint8_t const * len_start = pos;
+ pos = varint_skip(pos, end);
+ std::size_t len = varint_decode<std::size_t>(len_start, pos);
+ if (len > end - pos) return result;
+ pos += len;
+ } else {
+ return result;
+ }
+ }
+ if (flags == 0x7) {
+ result.body_length = end - input;
+ }
+ return result;
+}
diff --git a/test.py b/test.py
new file mode 100644
index 0000000..250f43e
--- /dev/null
+++ b/test.py
@@ -0,0 +1,17 @@
+import subprocess
+import glob
+import os
+
+if not os.path.exists("build"):
+ os.mkdir("build")
+
+test_files = glob.glob("tests/test_*.cpp")
+source_files = glob.glob("src/*.cpp")
+
+compile_args = "g++ -Itests/include -Iinclude -Ilib --std=c++11".split()
+compile_args += source_files
+
+for test_file in test_files:
+ exe_file = "build/" + test_file[:4]
+ subprocess.check_call(compile_args + [test_file, "-o", exe_file])
+ subprocess.check_call([exe_file])
diff --git a/tests/include/unittest.hh b/tests/include/unittest.hh
new file mode 100644
index 0000000..1ba64df
--- /dev/null
+++ b/tests/include/unittest.hh
@@ -0,0 +1,55 @@
+#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(
+ T const & expected,
+ T const & actual
+) {
+ if (expected != actual) {
+ std::cout << "FAILED: " << TEST_CASE << std::endl;
+ std::cout << "Expected: " << expected << std::endl;
+ std::cout << "Actual: " << actual << std::endl;
+ std::exit(1);
+ }
+}
+
+
+void assert_equals(
+ 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;
+ print_hex(std::cout << "Expected: ", expected, length) << std::endl;
+ print_hex(std::cout << "Actual: ", actual, length) << std::endl;
+ std::exit(1);
+ }
+}
+
+
+class TestCase {
+public:
+ TestCase(const char *name) { TEST_CASE = name; }
+ ~TestCase() { std::cout << "PASSED: " << TEST_CASE << std::endl; }
+};
diff --git a/tests/test_crypto.cpp b/tests/test_crypto.cpp
new file mode 100644
index 0000000..1b9947b
--- /dev/null
+++ b/tests/test_crypto.cpp
@@ -0,0 +1,204 @@
+#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..27b3d58
--- /dev/null
+++ b/tests/test_list.cpp
@@ -0,0 +1,56 @@
+#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..242243b
--- /dev/null
+++ b/tests/test_message.cpp
@@ -0,0 +1,51 @@
+#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 */
+
+}