allow specifying the info parameter for the HKDF

Hubert Chathi 2018-12-28 13:41:25 -05:00
parent ded55f5053
commit 17a989db6c
5 changed files with 21 additions and 11 deletions

View File

@ -62,6 +62,7 @@ size_t olm_sas_set_their_key(
size_t olm_sas_generate_bytes(
OlmSAS * sas,
const void * info, size_t info_length,
void * output, size_t output_length
);
@ -72,6 +73,7 @@ size_t olm_sas_mac_length(
size_t olm_sas_calculate_mac(
OlmSAS * sas,
void * input, size_t input_length,
const void * info, size_t info_length,
void * mac, size_t mac_length
);

View File

@ -42,10 +42,13 @@ SAS.prototype['set_their_key'] = restore_stack(function(their_key) {
);
});
SAS.prototype['generate_bytes'] = restore_stack(function(length) {
SAS.prototype['generate_bytes'] = restore_stack(function(info, length) {
var info_array = array_from_string(info);
var info_buffer = stack(info_array);
var output_buffer = stack(length);
sas_method(Module['_olm_sas_generate_bytes'])(
this.ptr,
info_buffer, info_array.length,
output_buffer, length
);
// The inner Uint8Array creates a view of the buffer. The outer Uint8Array
@ -57,14 +60,17 @@ SAS.prototype['generate_bytes'] = restore_stack(function(length) {
return output_arr;
});
SAS.prototype['calculate_mac'] = restore_stack(function(input) {
SAS.prototype['calculate_mac'] = restore_stack(function(input, info) {
var input_array = array_from_string(input);
var input_buffer = stack(input_array)
var input_buffer = stack(input_array);
var info_array = array_from_string(info);
var info_buffer = stack(info_array);
var mac_length = sas_method(Module['_olm_sas_mac_length'])(this.ptr);
var mac_buffer = stack(mac_length + NULL_BYTE_PADDING_LENGTH);
sas_method(Module['_olm_sas_calculate_mac'])(
this.ptr,
input_buffer, input_array.length,
info_buffer, info_array.length,
mac_buffer, mac_length
);
return Pointer_stringify(mac_buffer);

View File

@ -42,12 +42,12 @@ describe("sas", function() {
it('should create matching SAS bytes', function () {
alice.set_their_key(bob.get_pubkey());
bob.set_their_key(alice.get_pubkey());
expect(alice.generate_bytes(5).toString()).toEqual(bob.generate_bytes(5).toString());
expect(alice.generate_bytes("SAS", 5).toString()).toEqual(bob.generate_bytes("SAS", 5).toString());
});
it('should create matching MACs', function () {
alice.set_their_key(bob.get_pubkey());
bob.set_their_key(alice.get_pubkey());
expect(alice.calculate_mac("test").toString()).toEqual(bob.calculate_mac("test").toString());
expect(alice.calculate_mac("test", "MAC").toString()).toEqual(bob.calculate_mac("test", "MAC").toString());
});
});

View File

@ -100,12 +100,13 @@ size_t olm_sas_set_their_key(
size_t olm_sas_generate_bytes(
OlmSAS * sas,
const void * info, size_t info_length,
void * output, size_t output_length
) {
_olm_crypto_hkdf_sha256(
sas->secret, sizeof(sas->secret),
NULL, 0,
(const uint8_t *) "SAS", 3,
(const uint8_t *) info, info_length,
output, output_length
);
return 0;
@ -120,6 +121,7 @@ size_t olm_sas_mac_length(
size_t olm_sas_calculate_mac(
OlmSAS * sas,
void * input, size_t input_length,
const void * info, size_t info_length,
void * mac, size_t mac_length
) {
if (mac_length < olm_sas_mac_length(sas)) {
@ -131,7 +133,7 @@ size_t olm_sas_calculate_mac(
_olm_crypto_hkdf_sha256(
sas->secret, sizeof(sas->secret),
NULL, 0,
(const uint8_t *) "MAC", 3,
(const uint8_t *) info, info_length,
key, 256
);
_olm_crypto_hmac_sha256(key, 256, input, input_length, mac);

View File

@ -55,8 +55,8 @@ olm_sas_set_their_key(alice_sas, pubkey, olm_sas_pubkey_length(alice_sas));
std::uint8_t alice_bytes[6];
std::uint8_t bob_bytes[6];
olm_sas_generate_bytes(alice_sas, alice_bytes, 6);
olm_sas_generate_bytes(bob_sas, bob_bytes, 6);
olm_sas_generate_bytes(alice_sas, "SAS", 3, alice_bytes, 6);
olm_sas_generate_bytes(bob_sas, "SAS", 3, bob_bytes, 6);
assert_equals(alice_bytes, bob_bytes, 6);
@ -108,8 +108,8 @@ olm_sas_set_their_key(alice_sas, pubkey, olm_sas_pubkey_length(alice_sas));
std::uint8_t alice_mac[olm_sas_mac_length(alice_sas)];
std::uint8_t bob_mac[olm_sas_mac_length(bob_sas)];
olm_sas_calculate_mac(alice_sas, (void *) "Hello world!", 12, alice_mac, olm_sas_mac_length(alice_sas));
olm_sas_calculate_mac(bob_sas, (void *) "Hello world!", 12, bob_mac, olm_sas_mac_length(bob_sas));
olm_sas_calculate_mac(alice_sas, (void *) "Hello world!", 12, "MAC", 3, alice_mac, olm_sas_mac_length(alice_sas));
olm_sas_calculate_mac(bob_sas, (void *) "Hello world!", 12, "MAC", 3, bob_mac, olm_sas_mac_length(bob_sas));
assert_equals(alice_mac, bob_mac, olm_sas_mac_length(alice_sas));