Commit 6fe3b7eb authored by Mark Haines's avatar Mark Haines
Browse files

Move utils for pickling into a separate file

parent 28541dd8
......@@ -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
......
#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 */
......@@ -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
......@@ -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_ */
#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;
}
#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;
}
......@@ -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);