aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMark Haines <mark.haines@matrix.org>2015-06-12 16:15:37 +0100
committerMark Haines <mark.haines@matrix.org>2015-06-12 16:15:37 +0100
commit6fe3b7eb73fb11e1cba27b28add5b14430b66259 (patch)
treee2e526e4f5593cb8d978f1b8955b80c9c2d88b4e /src
parent28541dd82a9e4a99661ba32905b8488d35e70fe3 (diff)
Move utils for pickling into a separate file
Diffstat (limited to 'src')
-rw-r--r--src/account.cpp105
-rw-r--r--src/pickle.cpp66
-rw-r--r--src/ratchet.cpp228
-rw-r--r--src/session.cpp44
4 files changed, 344 insertions, 99 deletions
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;
+}
+