diff options
author | Mark Haines <mark.haines@matrix.org> | 2015-06-12 16:15:37 +0100 |
---|---|---|
committer | Mark Haines <mark.haines@matrix.org> | 2015-06-12 16:15:37 +0100 |
commit | 6fe3b7eb73fb11e1cba27b28add5b14430b66259 (patch) | |
tree | e2e526e4f5593cb8d978f1b8955b80c9c2d88b4e | |
parent | 28541dd82a9e4a99661ba32905b8488d35e70fe3 (diff) |
Move utils for pickling into a separate file
-rw-r--r-- | include/axolotl/account.hh | 28 | ||||
-rw-r--r-- | include/axolotl/pickle.hh | 164 | ||||
-rw-r--r-- | include/axolotl/ratchet.hh | 32 | ||||
-rw-r--r-- | include/axolotl/session.hh | 17 | ||||
-rw-r--r-- | src/account.cpp | 105 | ||||
-rw-r--r-- | src/pickle.cpp | 66 | ||||
-rw-r--r-- | src/ratchet.cpp | 228 | ||||
-rw-r--r-- | src/session.cpp | 44 |
8 files changed, 557 insertions, 127 deletions
diff --git a/include/axolotl/account.hh b/include/axolotl/account.hh index dd9c819..0e65a7e 100644 --- a/include/axolotl/account.hh +++ b/include/axolotl/account.hh @@ -41,22 +41,24 @@ struct Account { LocalKey const * lookup_key( std::uint32_t id ); +}; - /** The number of bytes needed to persist this account. */ - std::size_t pickle_length(); - /** Persists an account as a sequence of bytes - * Returns the number of output bytes used. */ - std::size_t pickle( - std::uint8_t * output, std::size_t output_length - ); +std::size_t pickle_length( + Account const & value +); - /** Loads an account from a sequence of bytes. - * Returns 0 on success, or std::size_t(-1) on failure. */ - std::size_t unpickle( - std::uint8_t * input, std::size_t input_length - ); -}; + +std::uint8_t * pickle( + std::uint8_t * pos, + Account const & value +); + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Account & value +); } // namespace axolotl diff --git a/include/axolotl/pickle.hh b/include/axolotl/pickle.hh new file mode 100644 index 0000000..a2de4f0 --- /dev/null +++ b/include/axolotl/pickle.hh @@ -0,0 +1,164 @@ +#ifndef AXOLOTL_PICKLE_HH_ +#define AXOLOTL_PICKLE_HH_ + +#include "axolotl/list.hh" +#include "axolotl/crypto.hh" + +#include <cstring> +#include <cstdint> + +namespace axolotl { + +static std::size_t pickle_length( + const std::uint32_t & value +) { + return 4; +} + + +static std::uint8_t * pickle( + std::uint8_t * pos, + std::uint32_t value +) { + pos += 4; + for (unsigned i = 4; i--;) { *(--pos) = value; value >>= 8; } + return pos + 4; +} + + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + std::uint32_t & value +) { + value = 0; + if (end - pos < 4) return end; + for (unsigned i = 4; i--;) { value <<= 8; value |= *(pos++); } + return pos; +} + +static std::size_t pickle_length( + const bool & value +) { + return 1; +} + + +static std::uint8_t * pickle( + std::uint8_t * pos, + bool value +) { + *(pos++) = value ? 1 : 0; + return pos; +} + + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + bool & value +) { + if (pos == end) return end; + value = *(pos++); + return pos; +} + + + +template<typename T, std::size_t max_size> +std::size_t pickle_length( + axolotl::List<T, max_size> const & list +) { + std::size_t length = pickle_length(std::uint32_t(list.size())); + for (auto const & value : list) { + length += pickle_length(value); + } + return length; +} + + +template<typename T, std::size_t max_size> +std::uint8_t * pickle( + std::uint8_t * pos, + axolotl::List<T, max_size> const & list +) { + pos = pickle(pos, std::uint32_t(list.size())); + for (auto const & value : list) { + pos = pickle(pos, value); + } + return pos; +} + + +template<typename T, std::size_t max_size> +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::List<T, max_size> & list +) { + std::uint32_t size; + pos = unpickle(pos, end, size); + while (size--) { + T * value = list.insert(list.end()); + pos = unpickle(pos, end, *value); + } + return pos; +} + + +static std::uint8_t * pickle_bytes( + std::uint8_t * pos, + std::uint8_t const * bytes, std::size_t bytes_length +) { + std::memcpy(pos, bytes, bytes_length); + return pos + bytes_length; +} + + +static std::uint8_t const * unpickle_bytes( + std::uint8_t const * pos, std::uint8_t const * end, + std::uint8_t * bytes, std::size_t bytes_length +) { + if (end - pos < bytes_length) return end; + std::memcpy(bytes, pos, bytes_length); + return pos + bytes_length; +} + + +std::size_t pickle_length( + const Curve25519PublicKey & value +); + + +std::uint8_t * pickle( + std::uint8_t * pos, + const Curve25519PublicKey & value +); + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Curve25519PublicKey & value +); + + +std::size_t pickle_length( + const Curve25519KeyPair & value +); + + +std::uint8_t * pickle( + std::uint8_t * pos, + const Curve25519KeyPair & value +); + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Curve25519KeyPair & value +); + + +} // namespace axolotl + + + + +#endif /* AXOLOTL_PICKLE_HH */ diff --git a/include/axolotl/ratchet.hh b/include/axolotl/ratchet.hh index eaf1352..0874cf0 100644 --- a/include/axolotl/ratchet.hh +++ b/include/axolotl/ratchet.hh @@ -114,21 +114,6 @@ struct Ratchet { Curve25519KeyPair const & our_ratchet_key ); - /** The number of bytes needed to persist the current session. */ - std::size_t pickle_length(); - - /** Persists a session as a sequence of bytes - * Returns the number of output bytes used. */ - std::size_t pickle( - std::uint8_t * output, std::size_t output_length - ); - - /** Loads a session from a sequence of bytes. - * Returns 0 on success, or std::size_t(-1) on failure. */ - std::size_t unpickle( - std::uint8_t * input, std::size_t input_length - ); - /** The number of bytes of output the encrypt method will write for * a given message length. */ std::size_t encrypt_output_length( @@ -172,4 +157,21 @@ struct Ratchet { }; +std::size_t pickle_length( + Ratchet const & value +); + + +std::uint8_t * pickle( + std::uint8_t * pos, + Ratchet const & value +); + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Ratchet & value +); + + } // namespace axolotl diff --git a/include/axolotl/session.hh b/include/axolotl/session.hh index 1c3395a..93c0b64 100644 --- a/include/axolotl/session.hh +++ b/include/axolotl/session.hh @@ -78,6 +78,23 @@ struct Session { }; +std::size_t pickle_length( + Session const & value +); + + +std::uint8_t * pickle( + std::uint8_t * pos, + Session const & value +); + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Session & value +); + + } // namespace axolotl #endif /* AXOLOTL_SESSION_HH_ */ diff --git a/src/account.cpp b/src/account.cpp new file mode 100644 index 0000000..c73ef1b --- /dev/null +++ b/src/account.cpp @@ -0,0 +1,105 @@ +#include "axolotl/account.hh" +#include "axolotl/pickle.hh" + + + +axolotl::LocalKey const * axolotl::Account::lookup_key( + std::uint32_t id +) { + for (axolotl::LocalKey const & key : one_time_keys) { + if (key.id == id) return &key; + } + return 0; +} + +namespace axolotl { + + +static std::size_t pickle_length( + axolotl::LocalKey const & value +) { + return axolotl::pickle_length(value.id) + axolotl::pickle_length(value.key); +} + + +static std::uint8_t * pickle( + std::uint8_t * pos, + axolotl::LocalKey const & value +) { + pos = axolotl::pickle(pos, value.id); + pos = axolotl::pickle(pos, value.key); + return pos; +} + + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::LocalKey & value +) { + pos = axolotl::unpickle(pos, end, value.id); + pos = axolotl::unpickle(pos, end, value.key); + return pos; +} + + +static std::size_t pickle_length( + axolotl::SignedKey const & value +) { + return axolotl::pickle_length((axolotl::LocalKey const &) value) + 64; +} + + +static std::uint8_t * pickle( + std::uint8_t * pos, + axolotl::SignedKey const & value +) { + pos = axolotl::pickle(pos, (axolotl::LocalKey const &) value); + pos = axolotl::pickle_bytes(pos, value.signature, 64); + return pos; +} + + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::SignedKey & value +) { + pos = axolotl::unpickle(pos, end, (axolotl::LocalKey &) value); + pos = axolotl::unpickle_bytes(pos, end, value.signature, 64); + return pos; +} + + +} // namespace axolotl + + +std::size_t pickle_length( + axolotl::Account const & value +) { + std::size_t length = 0; + length += axolotl::pickle_length(value.identity_key); + length += axolotl::pickle_length(value.last_resort_one_time_key); + length += axolotl::pickle_length(value.one_time_keys); + return length; +} + + +std::uint8_t * pickle( + std::uint8_t * pos, + axolotl::Account const & value +) { + pos = axolotl::pickle(pos, value.identity_key); + pos = axolotl::pickle(pos, value.last_resort_one_time_key); + pos = axolotl::pickle(pos, value.one_time_keys); + return pos; +} + + +std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::Account & value +) { + pos = axolotl::unpickle(pos, end, value.identity_key); + pos = axolotl::unpickle(pos, end, value.last_resort_one_time_key); + pos = axolotl::unpickle(pos, end, value.one_time_keys); + return pos; +} diff --git a/src/pickle.cpp b/src/pickle.cpp new file mode 100644 index 0000000..43ef49c --- /dev/null +++ b/src/pickle.cpp @@ -0,0 +1,66 @@ +#include "axolotl/pickle.hh" + + +std::size_t axolotl::pickle_length( + const axolotl::Curve25519PublicKey & value +) { + return sizeof(value.public_key); +} + + +std::uint8_t * axolotl::pickle( + std::uint8_t * pos, + const axolotl::Curve25519PublicKey & value +) { + pos = axolotl::pickle_bytes( + pos, value.public_key, sizeof(value.public_key) + ); + return pos; +} + + +std::uint8_t const * axolotl::unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::Curve25519PublicKey & value +) { + pos = axolotl::unpickle_bytes( + pos, end, value.public_key, sizeof(value.public_key) + ); + return pos; + +} + + +std::size_t axolotl::pickle_length( + const axolotl::Curve25519KeyPair & value +) { + return sizeof(value.public_key) + sizeof(value.private_key); +} + + +std::uint8_t * axolotl::pickle( + std::uint8_t * pos, + const axolotl::Curve25519KeyPair & value +) { + pos = axolotl::pickle_bytes( + pos, value.public_key, sizeof(value.public_key) + ); + pos = axolotl::pickle_bytes( + pos, value.private_key, sizeof(value.private_key) + ); + return pos; +} + + +std::uint8_t const * axolotl::unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::Curve25519KeyPair & value +) { + pos = axolotl::unpickle_bytes( + pos, end, value.public_key, sizeof(value.public_key) + ); + pos = axolotl::unpickle_bytes( + pos, end, value.private_key, sizeof(value.private_key) + ); + return pos; +} diff --git a/src/ratchet.cpp b/src/ratchet.cpp index 87d79b7..24f0ac2 100644 --- a/src/ratchet.cpp +++ b/src/ratchet.cpp @@ -16,6 +16,8 @@ #include "axolotl/message.hh" #include "axolotl/memory.hh" #include "axolotl/cipher.hh" +#include "axolotl/pickle.hh" + #include <cstring> @@ -214,137 +216,165 @@ void axolotl::Ratchet::initialise_as_alice( axolotl::unset(derived_secrets); } +namespace axolotl { + -std::size_t axolotl::Ratchet::pickle_length() { - std::size_t counter_length = 4; - std::size_t send_chain_length = counter_length + 64 + 32; - std::size_t recv_chain_length = counter_length + 32 + 32; - std::size_t skip_key_length = counter_length + 32 + 32; - std::size_t pickle_length = 3 * counter_length + 32; - pickle_length += sender_chain.size() * send_chain_length; - pickle_length += receiver_chains.size() * recv_chain_length; - pickle_length += skipped_message_keys.size() * skip_key_length; - return pickle_length; +static std::size_t pickle_length( + const axolotl::SharedKey & value +) { + return KEY_LENGTH; } -namespace { -std::uint8_t * pickle_counter( - std::uint8_t * output, std::uint32_t value +static std::uint8_t * pickle( + std::uint8_t * pos, + const axolotl::SharedKey & value ) { - unsigned i = 4; - output += 4; - while (i--) { *(--output) = value; value >>= 8; } - return output + 4; + return axolotl::pickle_bytes(pos, value, KEY_LENGTH); } -std::uint8_t * unpickle_counter( - std::uint8_t *input, std::uint32_t &value + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::SharedKey & value ) { - unsigned i = 4; - value = 0; - while (i--) { value <<= 8; value |= *(input++); } - return input; + return axolotl::unpickle_bytes(pos, end, value, KEY_LENGTH); } -std::uint8_t * pickle_bytes( - std::uint8_t * output, std::size_t count, std::uint8_t const * bytes + +static std::size_t pickle_length( + const axolotl::SenderChain & value ) { - std::memcpy(output, bytes, count); - return output + count; + std::size_t length = 0; + length += axolotl::pickle_length(value.ratchet_key); + length += axolotl::pickle_length(value.chain_key.key); + length += axolotl::pickle_length(value.chain_key.index); + return length; } -std::uint8_t * unpickle_bytes( - std::uint8_t * input, std::size_t count, std::uint8_t * bytes + +static std::uint8_t * pickle( + std::uint8_t * pos, + const axolotl::SenderChain & value ) { - std::memcpy(bytes, input, count); - return input + count; + pos = axolotl::pickle(pos, value.ratchet_key); + pos = axolotl::pickle(pos, value.chain_key.key); + pos = axolotl::pickle(pos, value.chain_key.index); + return pos; } -} // namespace +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::SenderChain & value +) { + pos = axolotl::unpickle(pos, end, value.ratchet_key); + pos = axolotl::unpickle(pos, end, value.chain_key.key); + pos = axolotl::unpickle(pos, end, value.chain_key.index); + return pos; +} -std::size_t axolotl::Ratchet::pickle( - std::uint8_t * output, std::size_t output_length +static std::size_t pickle_length( + const axolotl::ReceiverChain & value ) { - std::uint8_t * pos = output; - if (output_length < pickle_length()) { - last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; - return std::size_t(-1); - } + std::size_t length = 0; + length += axolotl::pickle_length(value.ratchet_key); + length += axolotl::pickle_length(value.chain_key.key); + length += axolotl::pickle_length(value.chain_key.index); + return length; +} - pos = pickle_counter(pos, sender_chain.size()); - pos = pickle_counter(pos, receiver_chains.size()); - pos = pickle_counter(pos, skipped_message_keys.size()); - pos = pickle_bytes(pos, 32, root_key); - for (const axolotl::SenderChain &chain : sender_chain) { - pos = pickle_counter(pos, chain.chain_key.index); - pos = pickle_bytes(pos, 32, chain.chain_key.key); - pos = pickle_bytes(pos, 32, chain.ratchet_key.public_key); - pos = pickle_bytes(pos, 32, chain.ratchet_key.private_key); - } - for (const axolotl::ReceiverChain &chain : receiver_chains) { - pos = pickle_counter(pos, chain.chain_key.index); - pos = pickle_bytes(pos, 32, chain.chain_key.key); - pos = pickle_bytes(pos, 32, chain.ratchet_key.public_key); - } - for (const axolotl::SkippedMessageKey &key : skipped_message_keys) { - pos = pickle_counter(pos, key.message_key.index); - pos = pickle_bytes(pos, 32, key.message_key.key); - pos = pickle_bytes(pos, 32, key.ratchet_key.public_key); - } - return pos - output; + +static std::uint8_t * pickle( + std::uint8_t * pos, + const axolotl::ReceiverChain & value +) { + pos = axolotl::pickle(pos, value.ratchet_key); + pos = axolotl::pickle(pos, value.chain_key.key); + pos = axolotl::pickle(pos, value.chain_key.index); + return pos; +} + + +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::ReceiverChain & value +) { + pos = axolotl::unpickle(pos, end, value.ratchet_key); + pos = axolotl::unpickle(pos, end, value.chain_key.key); + pos = axolotl::unpickle(pos, end, value.chain_key.index); + return pos; } -std::size_t axolotl::Ratchet::unpickle( - std::uint8_t * input, std::size_t input_length + +static std::size_t pickle_length( + const axolotl::SkippedMessageKey & value ) { + std::size_t length = 0; + length += axolotl::pickle_length(value.ratchet_key); + length += axolotl::pickle_length(value.message_key.key); + length += axolotl::pickle_length(value.message_key.index); + return length; +} - std::uint8_t * pos = input; - std::uint8_t * end = input + input_length; - std::uint32_t send_chain_num, recv_chain_num, skipped_num; - if (end - pos < 4 * 3 + 32) {} // input too small. +static std::uint8_t * pickle( + std::uint8_t * pos, + const axolotl::SkippedMessageKey & value +) { + pos = axolotl::pickle(pos, value.ratchet_key); + pos = axolotl::pickle(pos, value.message_key.key); + pos = axolotl::pickle(pos, value.message_key.index); + return pos; +} - pos = unpickle_counter(pos, send_chain_num); - pos = unpickle_counter(pos, recv_chain_num); - pos = unpickle_counter(pos, skipped_num); - pos = unpickle_bytes(pos, 32, root_key); - if (end - pos < send_chain_num * (32 * 3 + 4)) {} // input too small. +static std::uint8_t const * unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::SkippedMessageKey & value +) { + pos = axolotl::unpickle(pos, end, value.ratchet_key); + pos = axolotl::unpickle(pos, end, value.message_key.key); + pos = axolotl::unpickle(pos, end, value.message_key.index); + return pos; +} - while (send_chain_num--) { - axolotl::SenderChain & chain = *sender_chain.insert( - sender_chain.end() - ); - pos = unpickle_counter(pos, chain.chain_key.index); - pos = unpickle_bytes(pos, 32, chain.chain_key.key); - pos = unpickle_bytes(pos, 32, chain.ratchet_key.public_key); - pos = unpickle_bytes(pos, 32, chain.ratchet_key.private_key); - } - if (end - pos < recv_chain_num * (32 * 2 + 4)) {} // input too small. +} // namespace axolotl - while (recv_chain_num--) { - axolotl::ReceiverChain & chain = *receiver_chains.insert( - receiver_chains.end() - ); - pos = unpickle_counter(pos, chain.chain_key.index); - pos = unpickle_bytes(pos, 32, chain.chain_key.key); - pos = unpickle_bytes(pos, 32, chain.ratchet_key.public_key); - } - if (end - pos < skipped_num * (32 * 3 + 16 + 4)) {} // input too small. +std::size_t axolotl::pickle_length( + axolotl::Ratchet const & value +) { + std::size_t length = 0; + length += KEY_LENGTH; + length += axolotl::pickle_length(value.sender_chain); + length += axolotl::pickle_length(value.receiver_chains); + length += axolotl::pickle_length(value.skipped_message_keys); + return length; +} - while (skipped_num--) { - axolotl::SkippedMessageKey &key = *skipped_message_keys.insert( - skipped_message_keys.end() - ); - pos = unpickle_counter(pos, key.message_key.index); - pos = unpickle_bytes(pos, 32, key.message_key.key); - pos = unpickle_bytes(pos, 32, key.ratchet_key.public_key); - } - return pos - input; +std::uint8_t * axolotl::pickle( + std::uint8_t * pos, + axolotl::Ratchet const & value +) { + pos = pickle(pos, value.root_key); + pos = pickle(pos, value.sender_chain); + pos = pickle(pos, value.receiver_chains); + pos = pickle(pos, value.skipped_message_keys); + return pos; +} + + +std::uint8_t const * axolotl::unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + axolotl::Ratchet & value +) { + pos = unpickle(pos, end, value.root_key); + pos = unpickle(pos, end, value.sender_chain); + pos = unpickle(pos, end, value.receiver_chains); + pos = unpickle(pos, end, value.skipped_message_keys); + return pos; } diff --git a/src/session.cpp b/src/session.cpp index 8c4b0b5..db8097f 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -4,6 +4,7 @@ #include "axolotl/account.hh" #include "axolotl/memory.hh" #include "axolotl/message.hh" +#include "axolotl/pickle.hh" #include <cstring> @@ -334,3 +335,46 @@ std::size_t axolotl::Session::decrypt( } return result; } + + +std::size_t axolotl::pickle_length( + Session const & value +) { + std::size_t length = 0; + length += axolotl::pickle_length(value.received_message); + length += axolotl::pickle_length(value.alice_identity_key.id); + length += axolotl::pickle_length(value.alice_identity_key.key); + length += axolotl::pickle_length(value.alice_base_key); + length += axolotl::pickle_length(value.bob_one_time_key_id); + length += axolotl::pickle_length(value.ratchet); + return length; +} + + +std::uint8_t * axolotl::pickle( + std::uint8_t * pos, + Session const & value +) { + pos = axolotl::pickle(pos, value.received_message); + pos = axolotl::pickle(pos, value.alice_identity_key.id); + pos = axolotl::pickle(pos, value.alice_identity_key.key); + pos = axolotl::pickle(pos, value.alice_base_key); + pos = axolotl::pickle(pos, value.bob_one_time_key_id); + pos = axolotl::pickle(pos, value.ratchet); + return pos; +} + + +std::uint8_t const * axolotl::unpickle( + std::uint8_t const * pos, std::uint8_t const * end, + Session & value +) { + pos = axolotl::unpickle(pos, end, value.received_message); + pos = axolotl::unpickle(pos, end, value.alice_identity_key.id); + pos = axolotl::unpickle(pos, end, value.alice_identity_key.key); + pos = axolotl::unpickle(pos, end, value.alice_base_key); + pos = axolotl::unpickle(pos, end, value.bob_one_time_key_id); + pos = axolotl::unpickle(pos, end, value.ratchet); + return pos; +} + |