summaryrefslogtreecommitdiff
path: root/libs/libaxolotl/src/ratchet.c
diff options
context:
space:
mode:
Diffstat (limited to 'libs/libaxolotl/src/ratchet.c')
-rwxr-xr-x[-rw-r--r--]libs/libaxolotl/src/ratchet.c439
1 files changed, 220 insertions, 219 deletions
diff --git a/libs/libaxolotl/src/ratchet.c b/libs/libaxolotl/src/ratchet.c
index 8b0dc87bec..bf65ee3e8b 100644..100755
--- a/libs/libaxolotl/src/ratchet.c
+++ b/libs/libaxolotl/src/ratchet.c
@@ -6,20 +6,21 @@
#include <assert.h>
#include "protobuf-c/protobuf-c.h"
-#include "axolotl_internal.h"
#include "hkdf.h"
#include "curve.h"
#include "session_state.h"
+#include "protocol.h"
#include "vpool.h"
#include "LocalStorageProtocol.pb-c.h"
+#include "signal_protocol_internal.h"
#define HASH_OUTPUT_SIZE 32
#define DERIVED_MESSAGE_SECRETS_SIZE 80
#define DERIVED_ROOT_SECRETS_SIZE 64
struct ratchet_chain_key {
- axolotl_type_base base;
- axolotl_context *global_context;
+ signal_type_base base;
+ signal_context *global_context;
hkdf_context *kdf;
uint8_t *key;
size_t key_len;
@@ -27,62 +28,62 @@ struct ratchet_chain_key {
};
struct ratchet_root_key {
- axolotl_type_base base;
- axolotl_context *global_context;
+ signal_type_base base;
+ signal_context *global_context;
hkdf_context *kdf;
uint8_t *key;
size_t key_len;
};
struct ratchet_identity_key_pair {
- axolotl_type_base base;
+ signal_type_base base;
ec_public_key *public_key;
ec_private_key *private_key;
};
-int ratchet_chain_key_create(ratchet_chain_key **chain_key, hkdf_context *kdf, uint8_t *key, size_t key_len, uint32_t index, axolotl_context *global_context)
+int ratchet_chain_key_create(ratchet_chain_key **chain_key, hkdf_context *kdf, const uint8_t *key, size_t key_len, uint32_t index, signal_context *global_context)
{
ratchet_chain_key *result = 0;
if(!kdf || !key) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
result = malloc(sizeof(ratchet_chain_key));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- AXOLOTL_INIT(result, ratchet_chain_key_destroy);
+ SIGNAL_INIT(result, ratchet_chain_key_destroy);
result->global_context = global_context;
result->kdf = kdf;
result->key = malloc(key_len);
if(!result->key) {
free(result);
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memcpy(result->key, key, key_len);
result->key_len = key_len;
result->index = index;
- AXOLOTL_REF(result->kdf);
+ SIGNAL_REF(result->kdf);
*chain_key = result;
return 0;
}
-int ratchet_chain_key_get_key(const ratchet_chain_key *chain_key, axolotl_buffer **buffer)
+int ratchet_chain_key_get_key(const ratchet_chain_key *chain_key, signal_buffer **buffer)
{
- axolotl_buffer *buf = 0;
+ signal_buffer *buf = 0;
uint8_t *data = 0;
- buf = axolotl_buffer_alloc(chain_key->key_len);
+ buf = signal_buffer_alloc(chain_key->key_len);
if(!buf) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- data = axolotl_buffer_data(buf);
+ data = signal_buffer_data(buf);
memcpy(data, chain_key->key, chain_key->key_len);
*buffer = buf;
@@ -99,7 +100,7 @@ int ratchet_chain_key_get_key_protobuf(const ratchet_chain_key *chain_key, Proto
data = malloc(chain_key->key_len);
if(!data) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memcpy(data, chain_key->key, chain_key->key_len);
@@ -117,38 +118,38 @@ uint32_t ratchet_chain_key_get_index(const ratchet_chain_key *chain_key)
ssize_t ratchet_chain_key_get_base_material(const ratchet_chain_key *chain_key, uint8_t **material, const uint8_t *seed, size_t seed_len)
{
int result = 0;
- axolotl_buffer *output_buffer = 0;
+ signal_buffer *output_buffer = 0;
uint8_t *output = 0;
size_t output_len = 0;
void *hmac_context = 0;
- result = axolotl_hmac_sha256_init(chain_key->global_context, &hmac_context, chain_key->key, chain_key->key_len);
+ result = signal_hmac_sha256_init(chain_key->global_context, &hmac_context, chain_key->key, chain_key->key_len);
if(result < 0) {
goto complete;
}
- result = axolotl_hmac_sha256_update(chain_key->global_context, hmac_context, seed, seed_len);
+ result = signal_hmac_sha256_update(chain_key->global_context, hmac_context, seed, seed_len);
if(result < 0) {
goto complete;
}
- result = axolotl_hmac_sha256_final(chain_key->global_context, hmac_context, &output_buffer);
+ result = signal_hmac_sha256_final(chain_key->global_context, hmac_context, &output_buffer);
if(result < 0) {
goto complete;
}
- output_len = axolotl_buffer_len(output_buffer);
+ output_len = signal_buffer_len(output_buffer);
output = malloc(output_len);
if(!output) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- memcpy(output, axolotl_buffer_data(output_buffer), output_len);
+ memcpy(output, signal_buffer_data(output_buffer), output_len);
complete:
- axolotl_hmac_sha256_cleanup(chain_key->global_context, hmac_context);
- axolotl_buffer_free(output_buffer);
+ signal_hmac_sha256_cleanup(chain_key->global_context, hmac_context);
+ signal_buffer_free(output_buffer);
if(result >= 0) {
*material = output;
@@ -176,7 +177,7 @@ int ratchet_chain_key_get_message_keys(ratchet_chain_key *chain_key, ratchet_mes
result_size = ratchet_chain_key_get_base_material(chain_key, &input_key_material, &message_key_seed, sizeof(message_key_seed));
if(result_size < 0) {
result = (int)result_size;
- axolotl_log(chain_key->global_context, AX_LOG_WARNING, "ratchet_chain_key_get_base_material failed");
+ signal_log(chain_key->global_context, SG_LOG_WARNING, "ratchet_chain_key_get_base_material failed");
goto complete;
}
input_key_material_len = (size_t)result_size;
@@ -191,16 +192,16 @@ int ratchet_chain_key_get_message_keys(ratchet_chain_key *chain_key, ratchet_mes
DERIVED_MESSAGE_SECRETS_SIZE);
if(result_size < 0) {
result = (int)result_size;
- axolotl_log(chain_key->global_context, AX_LOG_WARNING, "hkdf_derive_secrets failed");
+ signal_log(chain_key->global_context, SG_LOG_WARNING, "hkdf_derive_secrets failed");
goto complete;
}
key_material_data_len = (size_t)result_size;
if(key_material_data_len != RATCHET_CIPHER_KEY_LENGTH + RATCHET_MAC_KEY_LENGTH + RATCHET_IV_LENGTH) {
- axolotl_log(chain_key->global_context, AX_LOG_WARNING,
+ signal_log(chain_key->global_context, SG_LOG_WARNING,
"key_material_data length mismatch: %d != %d",
key_material_data_len, (RATCHET_CIPHER_KEY_LENGTH + RATCHET_MAC_KEY_LENGTH + RATCHET_IV_LENGTH));
- result = AX_ERR_UNKNOWN;
+ result = SG_ERR_UNKNOWN;
goto complete;
}
@@ -235,7 +236,7 @@ int ratchet_chain_key_create_next(const ratchet_chain_key *chain_key, ratchet_ch
result_size = ratchet_chain_key_get_base_material(chain_key, &next_key, &chain_key_seed, sizeof(chain_key_seed));
if(result_size < 0) {
result = (int)result_size;
- axolotl_log(chain_key->global_context, AX_LOG_WARNING, "ratchet_chain_key_get_base_material failed");
+ signal_log(chain_key->global_context, SG_LOG_WARNING, "ratchet_chain_key_get_base_material failed");
goto complete;
}
next_key_len = (size_t)result_size;
@@ -255,42 +256,42 @@ complete:
return result;
}
-void ratchet_chain_key_destroy(axolotl_type_base *type)
+void ratchet_chain_key_destroy(signal_type_base *type)
{
ratchet_chain_key *chain_key = (ratchet_chain_key *)type;
- AXOLOTL_UNREF(chain_key->kdf);
+ SIGNAL_UNREF(chain_key->kdf);
if(chain_key->key) {
- axolotl_explicit_bzero(chain_key->key, chain_key->key_len);
+ signal_explicit_bzero(chain_key->key, chain_key->key_len);
free(chain_key->key);
}
free(chain_key);
}
-int ratchet_root_key_create(ratchet_root_key **root_key, hkdf_context *kdf, const uint8_t *key, size_t key_len, axolotl_context *global_context)
+int ratchet_root_key_create(ratchet_root_key **root_key, hkdf_context *kdf, const uint8_t *key, size_t key_len, signal_context *global_context)
{
ratchet_root_key *result = 0;
if(!kdf || !key) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
result = malloc(sizeof(ratchet_root_key));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- AXOLOTL_INIT(result, ratchet_root_key_destroy);
+ SIGNAL_INIT(result, ratchet_root_key_destroy);
result->global_context = global_context;
result->kdf = kdf;
result->key = malloc(key_len);
if(!result->key) {
free(result);
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memcpy(result->key, key, key_len);
result->key_len = key_len;
- AXOLOTL_REF(result->kdf);
+ SIGNAL_REF(result->kdf);
*root_key = result;
return 0;
@@ -311,12 +312,12 @@ int ratchet_root_key_create_chain(ratchet_root_key *root_key,
ratchet_chain_key *new_chain_key_result = 0;
if(!their_ratchet_key || !our_ratchet_key_private) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
result = curve_calculate_agreement(&shared_secret, their_ratchet_key, our_ratchet_key_private);
if(result < 0) {
- axolotl_log(root_key->global_context, AX_LOG_WARNING, "curve_calculate_agreement failed");
+ signal_log(root_key->global_context, SG_LOG_WARNING, "curve_calculate_agreement failed");
goto complete;
}
shared_secret_len = (size_t)result;
@@ -328,12 +329,12 @@ int ratchet_root_key_create_chain(ratchet_root_key *root_key,
DERIVED_ROOT_SECRETS_SIZE);
if(result_size < 0) {
result = (int)result_size;
- axolotl_log(root_key->global_context, AX_LOG_WARNING, "hkdf_derive_secrets failed");
+ signal_log(root_key->global_context, SG_LOG_WARNING, "hkdf_derive_secrets failed");
goto complete;
}
else if(result_size != DERIVED_ROOT_SECRETS_SIZE) {
- result = AX_ERR_UNKNOWN;
- axolotl_log(root_key->global_context, AX_LOG_WARNING, "hkdf_derive_secrets size mismatch");
+ result = SG_ERR_UNKNOWN;
+ signal_log(root_key->global_context, SG_LOG_WARNING, "hkdf_derive_secrets size mismatch");
goto complete;
}
@@ -341,7 +342,7 @@ int ratchet_root_key_create_chain(ratchet_root_key *root_key,
derived_secret, 32,
root_key->global_context);
if(result < 0) {
- axolotl_log(root_key->global_context, AX_LOG_WARNING, "ratchet_root_key_create failed");
+ signal_log(root_key->global_context, SG_LOG_WARNING, "ratchet_root_key_create failed");
goto complete;
}
@@ -349,7 +350,7 @@ int ratchet_root_key_create_chain(ratchet_root_key *root_key,
derived_secret + 32, 32, 0,
root_key->global_context);
if(result < 0) {
- axolotl_log(root_key->global_context, AX_LOG_WARNING, "ratchet_chain_key_create failed");
+ signal_log(root_key->global_context, SG_LOG_WARNING, "ratchet_chain_key_create failed");
goto complete;
}
@@ -362,10 +363,10 @@ complete:
}
if(result < 0) {
if(new_root_key_result) {
- AXOLOTL_UNREF(new_root_key_result);
+ SIGNAL_UNREF(new_root_key_result);
}
if(new_chain_key_result) {
- AXOLOTL_UNREF(new_chain_key_result);
+ SIGNAL_UNREF(new_chain_key_result);
}
return result;
}
@@ -376,19 +377,19 @@ complete:
}
}
-int ratchet_root_key_get_key(ratchet_root_key *root_key, axolotl_buffer **buffer)
+int ratchet_root_key_get_key(ratchet_root_key *root_key, signal_buffer **buffer)
{
- axolotl_buffer *buf = 0;
+ signal_buffer *buf = 0;
uint8_t *data = 0;
assert(root_key);
- buf = axolotl_buffer_alloc(root_key->key_len);
+ buf = signal_buffer_alloc(root_key->key_len);
if(!buf) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- data = axolotl_buffer_data(buf);
+ data = signal_buffer_data(buf);
memcpy(data, root_key->key, root_key->key_len);
*buffer = buf;
@@ -405,7 +406,7 @@ int ratchet_root_key_get_key_protobuf(const ratchet_root_key *root_key, Protobuf
data = malloc(root_key->key_len);
if(!data) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
memcpy(data, root_key->key, root_key->key_len);
@@ -438,17 +439,17 @@ int ratchet_root_key_compare(const ratchet_root_key *key1, const ratchet_root_ke
return 1;
}
else {
- return axolotl_constant_memcmp(key1->key, key2->key, key1->key_len);
+ return signal_constant_memcmp(key1->key, key2->key, key1->key_len);
}
}
}
-void ratchet_root_key_destroy(axolotl_type_base *type)
+void ratchet_root_key_destroy(signal_type_base *type)
{
ratchet_root_key *root_key = (ratchet_root_key *)type;
- AXOLOTL_UNREF(root_key->kdf);
+ SIGNAL_UNREF(root_key->kdf);
if(root_key->key) {
- axolotl_explicit_bzero(root_key->key, root_key->key_len);
+ signal_explicit_bzero(root_key->key, root_key->key_len);
free(root_key->key);
}
free(root_key);
@@ -461,12 +462,12 @@ int ratchet_identity_key_pair_create(
{
ratchet_identity_key_pair *result = malloc(sizeof(ratchet_identity_key_pair));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- AXOLOTL_INIT(result, ratchet_identity_key_pair_destroy);
- AXOLOTL_REF(public_key);
- AXOLOTL_REF(private_key);
+ SIGNAL_INIT(result, ratchet_identity_key_pair_destroy);
+ SIGNAL_REF(public_key);
+ SIGNAL_REF(private_key);
result->public_key = public_key;
result->private_key = private_key;
@@ -475,15 +476,20 @@ int ratchet_identity_key_pair_create(
return 0;
}
-int ratchet_identity_key_pair_serialize(axolotl_buffer **buffer, const ratchet_identity_key_pair *key_pair)
+int ratchet_identity_key_pair_serialize(signal_buffer **buffer, const ratchet_identity_key_pair *key_pair)
{
int result = 0;
size_t result_size = 0;
- axolotl_buffer *result_buf = 0;
+ signal_buffer *result_buf = 0;
Textsecure__IdentityKeyPairStructure key_structure = TEXTSECURE__IDENTITY_KEY_PAIR_STRUCTURE__INIT;
size_t len = 0;
uint8_t *data = 0;
+ if(!key_pair) {
+ result = SG_ERR_INVAL;
+ goto complete;
+ }
+
result = ec_public_key_serialize_protobuf(&key_structure.publickey, key_pair->public_key);
if(result < 0) {
goto complete;
@@ -497,17 +503,17 @@ int ratchet_identity_key_pair_serialize(axolotl_buffer **buffer, const ratchet_i
key_structure.has_privatekey = 1;
len = textsecure__identity_key_pair_structure__get_packed_size(&key_structure);
- result_buf = axolotl_buffer_alloc(len);
+ result_buf = signal_buffer_alloc(len);
if(!result_buf) {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- data = axolotl_buffer_data(result_buf);
+ data = signal_buffer_data(result_buf);
result_size = textsecure__identity_key_pair_structure__pack(&key_structure, data);
if(result_size != len) {
- axolotl_buffer_free(result_buf);
- result = AX_ERR_INVALID_PROTO_BUF;
+ signal_buffer_free(result_buf);
+ result = SG_ERR_INVALID_PROTO_BUF;
result_buf = 0;
goto complete;
}
@@ -526,7 +532,7 @@ complete:
return result;
}
-int ratchet_identity_key_pair_deserialize(ratchet_identity_key_pair **key_pair, const uint8_t *data, size_t len, axolotl_context *global_context)
+int ratchet_identity_key_pair_deserialize(ratchet_identity_key_pair **key_pair, const uint8_t *data, size_t len, signal_context *global_context)
{
int result = 0;
ec_public_key *public_key = 0;
@@ -536,12 +542,12 @@ int ratchet_identity_key_pair_deserialize(ratchet_identity_key_pair **key_pair,
key_structure = textsecure__identity_key_pair_structure__unpack(0, len, data);
if(!key_structure) {
- result = AX_ERR_INVALID_PROTO_BUF;
+ result = SG_ERR_INVALID_PROTO_BUF;
goto complete;
}
if(!key_structure->has_publickey || !key_structure->has_privatekey) {
- result = AX_ERR_INVALID_KEY;
+ result = SG_ERR_INVALID_KEY;
goto complete;
}
@@ -567,8 +573,8 @@ int ratchet_identity_key_pair_deserialize(ratchet_identity_key_pair **key_pair,
public_key, private_key);
complete:
- AXOLOTL_UNREF(public_key);
- AXOLOTL_UNREF(private_key);
+ SIGNAL_UNREF(public_key);
+ SIGNAL_UNREF(private_key);
if(key_structure) {
textsecure__identity_key_pair_structure__free_unpacked(key_structure, 0);
}
@@ -592,17 +598,17 @@ ec_private_key *ratchet_identity_key_pair_get_private(const ratchet_identity_key
return key_pair->private_key;
}
-void ratchet_identity_key_pair_destroy(axolotl_type_base *type)
+void ratchet_identity_key_pair_destroy(signal_type_base *type)
{
ratchet_identity_key_pair *key_pair = (ratchet_identity_key_pair *)type;
- AXOLOTL_UNREF(key_pair->public_key);
- AXOLOTL_UNREF(key_pair->private_key);
+ SIGNAL_UNREF(key_pair->public_key);
+ SIGNAL_UNREF(key_pair->private_key);
free(key_pair);
}
-struct symmetric_axolotl_parameters
+struct symmetric_signal_protocol_parameters
{
- axolotl_type_base base;
+ signal_type_base base;
ratchet_identity_key_pair *our_identity_key;
ec_key_pair *our_base_key;
ec_key_pair *our_ratchet_key;
@@ -611,9 +617,9 @@ struct symmetric_axolotl_parameters
ec_public_key *their_identity_key;
};
-struct alice_axolotl_parameters
+struct alice_signal_protocol_parameters
{
- axolotl_type_base base;
+ signal_type_base base;
ratchet_identity_key_pair *our_identity_key;
ec_key_pair *our_base_key;
ec_public_key *their_identity_key;
@@ -622,9 +628,9 @@ struct alice_axolotl_parameters
ec_public_key *their_ratchet_key;
};
-struct bob_axolotl_parameters
+struct bob_signal_protocol_parameters
{
- axolotl_type_base base;
+ signal_type_base base;
ratchet_identity_key_pair *our_identity_key;
ec_key_pair *our_signed_pre_key;
ec_key_pair *our_one_time_pre_key; /* optional */
@@ -633,8 +639,8 @@ struct bob_axolotl_parameters
ec_public_key *their_base_key;
};
-int symmetric_axolotl_parameters_create(
- symmetric_axolotl_parameters **parameters,
+int symmetric_signal_protocol_parameters_create(
+ symmetric_signal_protocol_parameters **parameters,
ratchet_identity_key_pair *our_identity_key,
ec_key_pair *our_base_key,
ec_key_pair *our_ratchet_key,
@@ -642,27 +648,27 @@ int symmetric_axolotl_parameters_create(
ec_public_key *their_ratchet_key,
ec_public_key *their_identity_key)
{
- symmetric_axolotl_parameters *result = 0;
+ symmetric_signal_protocol_parameters *result = 0;
if(!our_identity_key || !our_base_key || !our_ratchet_key
|| !their_base_key || !their_ratchet_key || !their_identity_key) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
- result = malloc(sizeof(symmetric_axolotl_parameters));
+ result = malloc(sizeof(symmetric_signal_protocol_parameters));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- memset(result, 0, sizeof(symmetric_axolotl_parameters));
+ memset(result, 0, sizeof(symmetric_signal_protocol_parameters));
- AXOLOTL_INIT(result, symmetric_axolotl_parameters_destroy);
- AXOLOTL_REF(our_identity_key);
- AXOLOTL_REF(our_base_key);
- AXOLOTL_REF(our_ratchet_key);
- AXOLOTL_REF(their_base_key);
- AXOLOTL_REF(their_ratchet_key);
- AXOLOTL_REF(their_identity_key);
+ SIGNAL_INIT(result, symmetric_signal_protocol_parameters_destroy);
+ SIGNAL_REF(our_identity_key);
+ SIGNAL_REF(our_base_key);
+ SIGNAL_REF(our_ratchet_key);
+ SIGNAL_REF(their_base_key);
+ SIGNAL_REF(their_ratchet_key);
+ SIGNAL_REF(their_identity_key);
result->our_identity_key = our_identity_key;
result->our_base_key = our_base_key;
result->our_ratchet_key = our_ratchet_key;
@@ -674,58 +680,58 @@ int symmetric_axolotl_parameters_create(
return 0;
}
-ratchet_identity_key_pair *symmetric_axolotl_parameters_get_our_identity_key(const symmetric_axolotl_parameters *parameters)
+ratchet_identity_key_pair *symmetric_signal_protocol_parameters_get_our_identity_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->our_identity_key;
}
-ec_key_pair *symmetric_axolotl_parameters_get_our_base_key(const symmetric_axolotl_parameters *parameters)
+ec_key_pair *symmetric_signal_protocol_parameters_get_our_base_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->our_base_key;
}
-ec_key_pair *symmetric_axolotl_parameters_get_our_ratchet_key(const symmetric_axolotl_parameters *parameters)
+ec_key_pair *symmetric_signal_protocol_parameters_get_our_ratchet_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->our_ratchet_key;
}
-ec_public_key *symmetric_axolotl_parameters_get_their_base_key(const symmetric_axolotl_parameters *parameters)
+ec_public_key *symmetric_signal_protocol_parameters_get_their_base_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->their_base_key;
}
-ec_public_key *symmetric_axolotl_parameters_get_their_ratchet_key(const symmetric_axolotl_parameters *parameters)
+ec_public_key *symmetric_signal_protocol_parameters_get_their_ratchet_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->their_ratchet_key;
}
-ec_public_key *symmetric_axolotl_parameters_get_their_identity_key(const symmetric_axolotl_parameters *parameters)
+ec_public_key *symmetric_signal_protocol_parameters_get_their_identity_key(const symmetric_signal_protocol_parameters *parameters)
{
assert(parameters);
return parameters->their_identity_key;
}
-void symmetric_axolotl_parameters_destroy(axolotl_type_base *type)
+void symmetric_signal_protocol_parameters_destroy(signal_type_base *type)
{
- symmetric_axolotl_parameters *parameters = (symmetric_axolotl_parameters *)type;
+ symmetric_signal_protocol_parameters *parameters = (symmetric_signal_protocol_parameters *)type;
- AXOLOTL_UNREF(parameters->our_identity_key);
- AXOLOTL_UNREF(parameters->our_base_key);
- AXOLOTL_UNREF(parameters->our_ratchet_key);
- AXOLOTL_UNREF(parameters->their_base_key);
- AXOLOTL_UNREF(parameters->their_ratchet_key);
- AXOLOTL_UNREF(parameters->their_identity_key);
+ SIGNAL_UNREF(parameters->our_identity_key);
+ SIGNAL_UNREF(parameters->our_base_key);
+ SIGNAL_UNREF(parameters->our_ratchet_key);
+ SIGNAL_UNREF(parameters->their_base_key);
+ SIGNAL_UNREF(parameters->their_ratchet_key);
+ SIGNAL_UNREF(parameters->their_identity_key);
free(parameters);
}
-int alice_axolotl_parameters_create(
- alice_axolotl_parameters **parameters,
+int alice_signal_protocol_parameters_create(
+ alice_signal_protocol_parameters **parameters,
ratchet_identity_key_pair *our_identity_key,
ec_key_pair *our_base_key,
ec_public_key *their_identity_key,
@@ -733,27 +739,27 @@ int alice_axolotl_parameters_create(
ec_public_key *their_one_time_pre_key,
ec_public_key *their_ratchet_key)
{
- alice_axolotl_parameters *result = 0;
+ alice_signal_protocol_parameters *result = 0;
/* Only "their_one_time_pre_key" is allowed to be null */
if(!our_identity_key || !our_base_key || !their_identity_key
|| !their_signed_pre_key || !their_ratchet_key) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
- result = malloc(sizeof(alice_axolotl_parameters));
+ result = malloc(sizeof(alice_signal_protocol_parameters));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- memset(result, 0, sizeof(alice_axolotl_parameters));
+ memset(result, 0, sizeof(alice_signal_protocol_parameters));
- AXOLOTL_INIT(result, alice_axolotl_parameters_destroy);
- AXOLOTL_REF(our_identity_key);
- AXOLOTL_REF(our_base_key);
- AXOLOTL_REF(their_identity_key);
- AXOLOTL_REF(their_signed_pre_key);
- AXOLOTL_REF(their_ratchet_key);
+ SIGNAL_INIT(result, alice_signal_protocol_parameters_destroy);
+ SIGNAL_REF(our_identity_key);
+ SIGNAL_REF(our_base_key);
+ SIGNAL_REF(their_identity_key);
+ SIGNAL_REF(their_signed_pre_key);
+ SIGNAL_REF(their_ratchet_key);
result->our_identity_key = our_identity_key;
result->our_base_key = our_base_key;
result->their_identity_key = their_identity_key;
@@ -761,7 +767,7 @@ int alice_axolotl_parameters_create(
result->their_ratchet_key = their_ratchet_key;
if(their_one_time_pre_key) {
- AXOLOTL_REF(their_one_time_pre_key);
+ SIGNAL_REF(their_one_time_pre_key);
result->their_one_time_pre_key = their_one_time_pre_key;
}
@@ -769,25 +775,25 @@ int alice_axolotl_parameters_create(
return 0;
}
-void alice_axolotl_parameters_destroy(axolotl_type_base *type)
+void alice_signal_protocol_parameters_destroy(signal_type_base *type)
{
- alice_axolotl_parameters *parameters = (alice_axolotl_parameters *)type;
+ alice_signal_protocol_parameters *parameters = (alice_signal_protocol_parameters *)type;
- AXOLOTL_UNREF(parameters->our_identity_key);
- AXOLOTL_UNREF(parameters->our_base_key);
- AXOLOTL_UNREF(parameters->their_identity_key);
- AXOLOTL_UNREF(parameters->their_signed_pre_key);
- AXOLOTL_UNREF(parameters->their_ratchet_key);
+ SIGNAL_UNREF(parameters->our_identity_key);
+ SIGNAL_UNREF(parameters->our_base_key);
+ SIGNAL_UNREF(parameters->their_identity_key);
+ SIGNAL_UNREF(parameters->their_signed_pre_key);
+ SIGNAL_UNREF(parameters->their_ratchet_key);
if(parameters->their_one_time_pre_key) {
- AXOLOTL_UNREF(parameters->their_one_time_pre_key);
+ SIGNAL_UNREF(parameters->their_one_time_pre_key);
}
free(parameters);
}
-int bob_axolotl_parameters_create(
- bob_axolotl_parameters **parameters,
+int bob_signal_protocol_parameters_create(
+ bob_signal_protocol_parameters **parameters,
ratchet_identity_key_pair *our_identity_key,
ec_key_pair *our_signed_pre_key,
ec_key_pair *our_one_time_pre_key,
@@ -795,27 +801,27 @@ int bob_axolotl_parameters_create(
ec_public_key *their_identity_key,
ec_public_key *their_base_key)
{
- bob_axolotl_parameters *result = 0;
+ bob_signal_protocol_parameters *result = 0;
/* Only "our_one_time_pre_key" is allowed to be null */
if(!our_identity_key || !our_signed_pre_key || !our_ratchet_key
|| !their_identity_key || !their_base_key) {
- return AX_ERR_INVAL;
+ return SG_ERR_INVAL;
}
- result = malloc(sizeof(bob_axolotl_parameters));
+ result = malloc(sizeof(bob_signal_protocol_parameters));
if(!result) {
- return AX_ERR_NOMEM;
+ return SG_ERR_NOMEM;
}
- memset(result, 0, sizeof(bob_axolotl_parameters));
+ memset(result, 0, sizeof(bob_signal_protocol_parameters));
- AXOLOTL_INIT(result, bob_axolotl_parameters_destroy);
- AXOLOTL_REF(our_identity_key);
- AXOLOTL_REF(our_signed_pre_key);
- AXOLOTL_REF(our_ratchet_key);
- AXOLOTL_REF(their_identity_key);
- AXOLOTL_REF(their_base_key);
+ SIGNAL_INIT(result, bob_signal_protocol_parameters_destroy);
+ SIGNAL_REF(our_identity_key);
+ SIGNAL_REF(our_signed_pre_key);
+ SIGNAL_REF(our_ratchet_key);
+ SIGNAL_REF(their_identity_key);
+ SIGNAL_REF(their_base_key);
result->our_identity_key = our_identity_key;
result->our_signed_pre_key = our_signed_pre_key;
result->our_ratchet_key = our_ratchet_key;
@@ -823,7 +829,7 @@ int bob_axolotl_parameters_create(
result->their_base_key = their_base_key;
if(our_one_time_pre_key) {
- AXOLOTL_REF(our_one_time_pre_key);
+ SIGNAL_REF(our_one_time_pre_key);
result->our_one_time_pre_key = our_one_time_pre_key;
}
@@ -831,25 +837,25 @@ int bob_axolotl_parameters_create(
return 0;
}
-void bob_axolotl_parameters_destroy(axolotl_type_base *type)
+void bob_signal_protocol_parameters_destroy(signal_type_base *type)
{
- bob_axolotl_parameters *parameters = (bob_axolotl_parameters *)type;
+ bob_signal_protocol_parameters *parameters = (bob_signal_protocol_parameters *)type;
- AXOLOTL_UNREF(parameters->our_identity_key);
- AXOLOTL_UNREF(parameters->our_signed_pre_key);
- AXOLOTL_UNREF(parameters->our_ratchet_key);
- AXOLOTL_UNREF(parameters->their_identity_key);
- AXOLOTL_UNREF(parameters->their_base_key);
+ SIGNAL_UNREF(parameters->our_identity_key);
+ SIGNAL_UNREF(parameters->our_signed_pre_key);
+ SIGNAL_UNREF(parameters->our_ratchet_key);
+ SIGNAL_UNREF(parameters->their_identity_key);
+ SIGNAL_UNREF(parameters->their_base_key);
if(parameters->our_one_time_pre_key) {
- AXOLOTL_UNREF(parameters->our_one_time_pre_key);
+ SIGNAL_UNREF(parameters->our_one_time_pre_key);
}
free(parameters);
}
int ratcheting_session_calculate_derived_keys(ratchet_root_key **root_key, ratchet_chain_key **chain_key,
- uint32_t version, uint8_t *secret, size_t secret_len, axolotl_context *global_context)
+ uint8_t *secret, size_t secret_len, signal_context *global_context)
{
int result = 0;
ssize_t result_size = 0;
@@ -860,7 +866,7 @@ int ratcheting_session_calculate_derived_keys(ratchet_root_key **root_key, ratch
uint8_t salt[HASH_OUTPUT_SIZE];
static const char key_info[] = "WhisperText";
- result = hkdf_create(&kdf, (int)version, global_context);
+ result = hkdf_create(&kdf, 3, global_context);
if(result < 0) {
goto complete;
}
@@ -872,7 +878,7 @@ int ratcheting_session_calculate_derived_keys(ratchet_root_key **root_key, ratch
salt, sizeof(salt),
(uint8_t *)key_info, sizeof(key_info) - 1, 64);
if(result_size != 64) {
- result = AX_ERR_UNKNOWN;
+ result = SG_ERR_UNKNOWN;
goto complete;
}
@@ -888,7 +894,7 @@ int ratcheting_session_calculate_derived_keys(ratchet_root_key **root_key, ratch
complete:
if(kdf) {
- AXOLOTL_UNREF(kdf);
+ SIGNAL_UNREF(kdf);
}
if(output) {
free(output);
@@ -896,10 +902,10 @@ complete:
if(result < 0) {
if(root_key_result) {
- AXOLOTL_UNREF(root_key_result);
+ SIGNAL_UNREF(root_key_result);
}
if(chain_key_result) {
- AXOLOTL_UNREF(chain_key_result);
+ SIGNAL_UNREF(chain_key_result);
}
}
else {
@@ -910,7 +916,7 @@ complete:
return result;
}
-int ratcheting_session_symmetric_is_alice(symmetric_axolotl_parameters *parameters)
+int ratcheting_session_symmetric_is_alice(symmetric_signal_protocol_parameters *parameters)
{
//FIXME Java code checks if our_base_key < their_base_key
// This comparison may not return the same result. However, we should find
@@ -921,9 +927,9 @@ int ratcheting_session_symmetric_is_alice(symmetric_axolotl_parameters *paramete
}
int ratcheting_session_symmetric_initialize(
- session_state *state, uint32_t version,
- symmetric_axolotl_parameters *parameters,
- axolotl_context *global_context)
+ session_state *state,
+ symmetric_signal_protocol_parameters *parameters,
+ signal_context *global_context)
{
int result = 0;
@@ -932,8 +938,8 @@ int ratcheting_session_symmetric_initialize(
assert(global_context);
if(ratcheting_session_symmetric_is_alice(parameters)) {
- alice_axolotl_parameters *alice_parameters = 0;
- result = alice_axolotl_parameters_create(&alice_parameters,
+ alice_signal_protocol_parameters *alice_parameters = 0;
+ result = alice_signal_protocol_parameters_create(&alice_parameters,
parameters->our_identity_key,
parameters->our_base_key,
parameters->their_identity_key,
@@ -941,15 +947,15 @@ int ratcheting_session_symmetric_initialize(
0,
parameters->their_ratchet_key);
if(result >= 0) {
- result = ratcheting_session_alice_initialize(state, version, alice_parameters, global_context);
+ result = ratcheting_session_alice_initialize(state, alice_parameters, global_context);
}
if(alice_parameters) {
- AXOLOTL_UNREF(alice_parameters);
+ SIGNAL_UNREF(alice_parameters);
}
}
else {
- bob_axolotl_parameters *bob_parameters = 0;
- result = bob_axolotl_parameters_create(&bob_parameters,
+ bob_signal_protocol_parameters *bob_parameters = 0;
+ result = bob_signal_protocol_parameters_create(&bob_parameters,
parameters->our_identity_key,
parameters->our_base_key,
0,
@@ -957,19 +963,19 @@ int ratcheting_session_symmetric_initialize(
parameters->their_identity_key,
parameters->their_base_key);
if(result >= 0) {
- result = ratcheting_session_bob_initialize(state, version, bob_parameters, global_context);
+ result = ratcheting_session_bob_initialize(state, bob_parameters, global_context);
}
if(bob_parameters) {
- AXOLOTL_UNREF(bob_parameters);
+ SIGNAL_UNREF(bob_parameters);
}
}
return result;
}
int ratcheting_session_alice_initialize(
- session_state *state, uint32_t version,
- alice_axolotl_parameters *parameters,
- axolotl_context *global_context)
+ session_state *state,
+ alice_signal_protocol_parameters *parameters,
+ signal_context *global_context)
{
int result = 0;
uint8_t *agreement = 0;
@@ -982,6 +988,7 @@ int ratcheting_session_alice_initialize(
struct vpool vp;
uint8_t *secret = 0;
size_t secret_len = 0;
+ uint8_t discontinuity_data[32];
assert(state);
assert(parameters);
@@ -994,13 +1001,10 @@ int ratcheting_session_alice_initialize(
goto complete;
}
- if(version >= 3) {
- uint8_t discontinuity_data[32];
- memset(discontinuity_data, 0xFF, sizeof(discontinuity_data));
- if(!vpool_insert(&vp, vpool_get_length(&vp), discontinuity_data, sizeof(discontinuity_data))) {
- result = AX_ERR_NOMEM;
- goto complete;
- }
+ memset(discontinuity_data, 0xFF, sizeof(discontinuity_data));
+ if(!vpool_insert(&vp, vpool_get_length(&vp), discontinuity_data, sizeof(discontinuity_data))) {
+ result = SG_ERR_NOMEM;
+ goto complete;
}
agreement_len = curve_calculate_agreement(&agreement,
@@ -1013,7 +1017,7 @@ int ratcheting_session_alice_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
@@ -1027,7 +1031,7 @@ int ratcheting_session_alice_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
@@ -1041,11 +1045,11 @@ int ratcheting_session_alice_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- if(version >= 3 && parameters->their_one_time_pre_key) {
+ if(parameters->their_one_time_pre_key) {
agreement_len = curve_calculate_agreement(&agreement,
parameters->their_one_time_pre_key, ec_key_pair_get_private(parameters->our_base_key));
if(agreement_len < 0) {
@@ -1056,20 +1060,20 @@ int ratcheting_session_alice_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
}
if(vpool_is_empty(&vp)) {
- result = AX_ERR_UNKNOWN;
+ result = SG_ERR_UNKNOWN;
goto complete;
}
secret = vpool_get_buf(&vp);
secret_len = vpool_get_length(&vp);
- result = ratcheting_session_calculate_derived_keys(&derived_root, &derived_chain, version, secret, secret_len, global_context);
+ result = ratcheting_session_calculate_derived_keys(&derived_root, &derived_chain, secret, secret_len, global_context);
if(result < 0) {
goto complete;
}
@@ -1084,7 +1088,7 @@ int ratcheting_session_alice_initialize(
complete:
if(result >= 0) {
- session_state_set_session_version(state, version);
+ session_state_set_session_version(state, CIPHERTEXT_CURRENT_VERSION);
session_state_set_remote_identity_key(state, parameters->their_identity_key);
session_state_set_local_identity_key(state, parameters->our_identity_key->public_key);
session_state_add_receiver_chain(state, parameters->their_ratchet_key, derived_chain);
@@ -1097,28 +1101,28 @@ complete:
free(agreement);
}
if(sending_ratchet_key) {
- AXOLOTL_UNREF(sending_ratchet_key);
+ SIGNAL_UNREF(sending_ratchet_key);
}
if(derived_root) {
- AXOLOTL_UNREF(derived_root);
+ SIGNAL_UNREF(derived_root);
}
if(derived_chain) {
- AXOLOTL_UNREF(derived_chain);
+ SIGNAL_UNREF(derived_chain);
}
if(sending_chain_root) {
- AXOLOTL_UNREF(sending_chain_root);
+ SIGNAL_UNREF(sending_chain_root);
}
if(sending_chain_key) {
- AXOLOTL_UNREF(sending_chain_key);
+ SIGNAL_UNREF(sending_chain_key);
}
return result;
}
int ratcheting_session_bob_initialize(
- session_state *state, uint32_t version,
- bob_axolotl_parameters *parameters,
- axolotl_context *global_context)
+ session_state *state,
+ bob_signal_protocol_parameters *parameters,
+ signal_context *global_context)
{
int result = 0;
uint8_t *agreement = 0;
@@ -1128,7 +1132,7 @@ int ratcheting_session_bob_initialize(
struct vpool vp;
uint8_t *secret = 0;
size_t secret_len = 0;
-
+ uint8_t discontinuity_data[32];
assert(state);
assert(parameters);
@@ -1136,13 +1140,10 @@ int ratcheting_session_bob_initialize(
vpool_init(&vp, 1024, 0);
- if(version >= 3) {
- uint8_t discontinuity_data[32];
- memset(discontinuity_data, 0xFF, sizeof(discontinuity_data));
- if(!vpool_insert(&vp, vpool_get_length(&vp), discontinuity_data, sizeof(discontinuity_data))) {
- result = AX_ERR_NOMEM;
- goto complete;
- }
+ memset(discontinuity_data, 0xFF, sizeof(discontinuity_data));
+ if(!vpool_insert(&vp, vpool_get_length(&vp), discontinuity_data, sizeof(discontinuity_data))) {
+ result = SG_ERR_NOMEM;
+ goto complete;
}
agreement_len = curve_calculate_agreement(&agreement,
@@ -1155,7 +1156,7 @@ int ratcheting_session_bob_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
@@ -1169,7 +1170,7 @@ int ratcheting_session_bob_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
@@ -1183,11 +1184,11 @@ int ratcheting_session_bob_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
- if(version >= 3 && parameters->our_one_time_pre_key) {
+ if(parameters->our_one_time_pre_key) {
agreement_len = curve_calculate_agreement(&agreement,
parameters->their_base_key, ec_key_pair_get_private(parameters->our_one_time_pre_key));
if(agreement_len < 0) {
@@ -1198,24 +1199,24 @@ int ratcheting_session_bob_initialize(
free(agreement); agreement = 0; agreement_len = 0;
}
else {
- result = AX_ERR_NOMEM;
+ result = SG_ERR_NOMEM;
goto complete;
}
}
if(vpool_is_empty(&vp)) {
- result = AX_ERR_UNKNOWN;
+ result = SG_ERR_UNKNOWN;
goto complete;
}
secret = vpool_get_buf(&vp);
secret_len = vpool_get_length(&vp);
- result = ratcheting_session_calculate_derived_keys(&derived_root, &derived_chain, version, secret, secret_len, global_context);
+ result = ratcheting_session_calculate_derived_keys(&derived_root, &derived_chain, secret, secret_len, global_context);
complete:
if(result >= 0) {
- session_state_set_session_version(state, version);
+ session_state_set_session_version(state, CIPHERTEXT_CURRENT_VERSION);
session_state_set_remote_identity_key(state, parameters->their_identity_key);
session_state_set_local_identity_key(state, parameters->our_identity_key->public_key);
session_state_set_sender_chain(state, parameters->our_ratchet_key, derived_chain);
@@ -1227,10 +1228,10 @@ complete:
free(agreement);
}
if(derived_root) {
- AXOLOTL_UNREF(derived_root);
+ SIGNAL_UNREF(derived_root);
}
if(derived_chain) {
- AXOLOTL_UNREF(derived_chain);
+ SIGNAL_UNREF(derived_chain);
}
return result;