Move utils for pickling into a separate file

This commit is contained in:
Mark Haines 2015-06-12 16:15:37 +01:00
parent 28541dd82a
commit 6fe3b7eb73
8 changed files with 561 additions and 131 deletions

View file

@ -41,24 +41,26 @@ struct Account {
LocalKey const * lookup_key( LocalKey const * lookup_key(
std::uint32_t id 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
);
/** 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::size_t pickle_length(
Account const & value
);
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 } // namespace axolotl
#endif /* AXOLOTL_ACCOUNT_HH_ */ #endif /* AXOLOTL_ACCOUNT_HH_ */

164
include/axolotl/pickle.hh Normal file
View file

@ -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 */

View file

@ -114,21 +114,6 @@ struct Ratchet {
Curve25519KeyPair const & our_ratchet_key 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 /** The number of bytes of output the encrypt method will write for
* a given message length. */ * a given message length. */
std::size_t encrypt_output_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 } // namespace axolotl

View file

@ -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 } // namespace axolotl
#endif /* AXOLOTL_SESSION_HH_ */ #endif /* AXOLOTL_SESSION_HH_ */

105
src/account.cpp Normal file
View file

@ -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;
}

66
src/pickle.cpp Normal file
View file

@ -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;
}

View file

@ -16,6 +16,8 @@
#include "axolotl/message.hh" #include "axolotl/message.hh"
#include "axolotl/memory.hh" #include "axolotl/memory.hh"
#include "axolotl/cipher.hh" #include "axolotl/cipher.hh"
#include "axolotl/pickle.hh"
#include <cstring> #include <cstring>
@ -214,137 +216,165 @@ void axolotl::Ratchet::initialise_as_alice(
axolotl::unset(derived_secrets); 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;
}
namespace { static std::size_t pickle_length(
const axolotl::SharedKey & value
std::uint8_t * pickle_counter(
std::uint8_t * output, std::uint32_t value
) { ) {
unsigned i = 4; return KEY_LENGTH;
output += 4;
while (i--) { *(--output) = value; value >>= 8; }
return output + 4;
} }
std::uint8_t * unpickle_counter(
std::uint8_t *input, std::uint32_t &value static std::uint8_t * pickle(
std::uint8_t * pos,
const axolotl::SharedKey & value
) { ) {
unsigned i = 4; return axolotl::pickle_bytes(pos, value, KEY_LENGTH);
value = 0;
while (i--) { value <<= 8; value |= *(input++); }
return input;
} }
std::uint8_t * pickle_bytes(
std::uint8_t * output, std::size_t count, std::uint8_t const * bytes static std::uint8_t const * unpickle(
std::uint8_t const * pos, std::uint8_t const * end,
axolotl::SharedKey & value
) { ) {
std::memcpy(output, bytes, count); return axolotl::unpickle_bytes(pos, end, value, KEY_LENGTH);
return output + count;
} }
std::uint8_t * unpickle_bytes(
std::uint8_t * input, std::size_t count, std::uint8_t * bytes static std::size_t pickle_length(
const axolotl::SenderChain & value
) { ) {
std::memcpy(bytes, input, count); std::size_t length = 0;
return input + count; 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;
} }
} // namespace
static std::uint8_t * pickle(
std::size_t axolotl::Ratchet::pickle( std::uint8_t * pos,
std::uint8_t * output, std::size_t output_length const axolotl::SenderChain & value
) { ) {
std::uint8_t * pos = output; pos = axolotl::pickle(pos, value.ratchet_key);
if (output_length < pickle_length()) { pos = axolotl::pickle(pos, value.chain_key.key);
last_error = axolotl::ErrorCode::OUTPUT_BUFFER_TOO_SMALL; pos = axolotl::pickle(pos, value.chain_key.index);
return std::size_t(-1); return pos;
} }
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;
}
std::size_t axolotl::Ratchet::unpickle( static std::uint8_t const * unpickle(
std::uint8_t * input, std::size_t input_length std::uint8_t const * pos, std::uint8_t const * end,
axolotl::SenderChain & value
) { ) {
pos = axolotl::unpickle(pos, end, value.ratchet_key);
std::uint8_t * pos = input; pos = axolotl::unpickle(pos, end, value.chain_key.key);
std::uint8_t * end = input + input_length; pos = axolotl::unpickle(pos, end, value.chain_key.index);
std::uint32_t send_chain_num, recv_chain_num, skipped_num; return pos;
if (end - pos < 4 * 3 + 32) {} // input too small.
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.
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. static std::size_t pickle_length(
const axolotl::ReceiverChain & value
while (recv_chain_num--) { ) {
axolotl::ReceiverChain & chain = *receiver_chains.insert( std::size_t length = 0;
receiver_chains.end() length += axolotl::pickle_length(value.ratchet_key);
); length += axolotl::pickle_length(value.chain_key.key);
pos = unpickle_counter(pos, chain.chain_key.index); length += axolotl::pickle_length(value.chain_key.index);
pos = unpickle_bytes(pos, 32, chain.chain_key.key); return length;
pos = unpickle_bytes(pos, 32, chain.ratchet_key.public_key);
} }
if (end - pos < skipped_num * (32 * 3 + 16 + 4)) {} // input too small.
while (skipped_num--) { static std::uint8_t * pickle(
axolotl::SkippedMessageKey &key = *skipped_message_keys.insert( std::uint8_t * pos,
skipped_message_keys.end() const axolotl::ReceiverChain & value
); ) {
pos = unpickle_counter(pos, key.message_key.index); pos = axolotl::pickle(pos, value.ratchet_key);
pos = unpickle_bytes(pos, 32, key.message_key.key); pos = axolotl::pickle(pos, value.chain_key.key);
pos = unpickle_bytes(pos, 32, key.ratchet_key.public_key); pos = axolotl::pickle(pos, value.chain_key.index);
return pos;
} }
return pos - input;
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;
}
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;
}
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;
}
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;
}
} // namespace axolotl
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;
}
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;
} }

View file

@ -4,6 +4,7 @@
#include "axolotl/account.hh" #include "axolotl/account.hh"
#include "axolotl/memory.hh" #include "axolotl/memory.hh"
#include "axolotl/message.hh" #include "axolotl/message.hh"
#include "axolotl/pickle.hh"
#include <cstring> #include <cstring>
@ -334,3 +335,46 @@ std::size_t axolotl::Session::decrypt(
} }
return result; 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;
}