aboutsummaryrefslogtreecommitdiff
path: root/src/session.cpp
diff options
context:
space:
mode:
authorMark Haines <mjark@negativecurvature.net>2015-06-22 14:30:46 +0100
committerMark Haines <mark.haines@matrix.org>2015-06-22 14:30:46 +0100
commit28541dd82a9e4a99661ba32905b8488d35e70fe3 (patch)
treedbf27f2a2b57d3b33ccaf5e9227c2a3f424b548f /src/session.cpp
parent6ecea67718803e96e00a18f97ae8abc83ecaa1c2 (diff)
Implement the session key exchange
Diffstat (limited to 'src/session.cpp')
-rw-r--r--src/session.cpp45
1 files changed, 31 insertions, 14 deletions
diff --git a/src/session.cpp b/src/session.cpp
index be2bce7..8c4b0b5 100644
--- a/src/session.cpp
+++ b/src/session.cpp
@@ -37,7 +37,7 @@ axolotl::Session::Session(
std::size_t axolotl::Session::new_outbound_session_random_length() {
- return KEY_LENGTH;
+ return KEY_LENGTH * 2;
}
@@ -55,27 +55,31 @@ std::size_t axolotl::Session::new_outbound_session(
Curve25519KeyPair base_key;
axolotl::generate_key(random, base_key);
+ Curve25519KeyPair ratchet_key;
+ axolotl::generate_key(random + 32, ratchet_key);
+
received_message = false;
alice_identity_key.id = local_account.identity_key.id;
alice_identity_key.key = local_account.identity_key.key;
alice_base_key = base_key;
bob_one_time_key_id = one_time_key.id;
- std::uint8_t shared_secret[160];
- std::memset(shared_secret, 0xFF, 32);
+ std::uint8_t shared_secret[96];
axolotl::curve25519_shared_secret(
+ local_account.identity_key.key, one_time_key.key, shared_secret
);
axolotl::curve25519_shared_secret(
- base_key, identity_key, shared_secret + 64
- );
- axolotl::curve25519_shared_secret(
+ base_key, identity_key, shared_secret + 32
);
axolotl::curve25519_shared_secret(
- base_key, one_time_key.key, shared_secret + 128
+ base_key, one_time_key.key, shared_secret + 64
);
+ ratchet.initialise_as_alice(shared_secret, 96, ratchet_key);
+
axolotl::unset(base_key);
+ axolotl::unset(ratchet_key);
axolotl::unset(shared_secret);
return std::size_t(0);
@@ -112,34 +116,47 @@ std::size_t axolotl::Session::new_inbound_session(
return std::size_t(-1);
}
+ axolotl::MessageReader message_reader;
+ decode_message(
+ message_reader, reader.message, reader.message_length,
+ ratchet.ratchet_cipher.mac_length()
+ );
+
+ if (!message_reader.ratchet_key
+ || message_reader.ratchet_key_length != KEY_LENGTH) {
+ last_error = axolotl::ErrorCode::BAD_MESSAGE_FORMAT;
+ return std::size_t(-1);
+ }
+
alice_identity_key.id = reader.registration_id;
std::memcpy(alice_identity_key.key.public_key, reader.identity_key, 32);
std::memcpy(alice_base_key.public_key, reader.base_key, 32);
bob_one_time_key_id = reader.one_time_key_id;
-
+ axolotl::Curve25519PublicKey ratchet_key;
+ std::memcpy(ratchet_key.public_key, message_reader.ratchet_key, 32);
axolotl::LocalKey const * bob_one_time_key = local_account.lookup_key(
bob_one_time_key_id
);
+ if (!bob_one_time_key) {
last_error = axolotl::ErrorCode::BAD_MESSAGE_KEY_ID;
return std::size_t(-1);
}
- std::uint8_t shared_secret[160];
- std::memset(shared_secret, 0xFF, 32);
+ std::uint8_t shared_secret[96];
axolotl::curve25519_shared_secret(
+ bob_one_time_key->key, alice_identity_key.key, shared_secret
);
axolotl::curve25519_shared_secret(
- local_account.identity_key.key, alice_base_key, shared_secret + 64
- );
- axolotl::curve25519_shared_secret(
+ local_account.identity_key.key, alice_base_key, shared_secret + 32
);
axolotl::curve25519_shared_secret(
- bob_one_time_key->key, alice_base_key, shared_secret + 128
+ bob_one_time_key->key, alice_base_key, shared_secret + 64
);
+ ratchet.initialise_as_bob(shared_secret, 96, ratchet_key);
return std::size_t(0);
}