END_TEST START_TEST(test_unique_signature_vector) { uint8_t publicKey[] = { 0x05, 0x21, 0xf7, 0x34, 0x5f, 0x56, 0xd9, 0x60, 0x2f, 0x15, 0x23, 0x29, 0x8f, 0x4f, 0x6f, 0xce, 0xcb, 0x14, 0xdd, 0xe2, 0xd5, 0xb9, 0xa9, 0xb4, 0x8b, 0xca, 0x82, 0x42, 0x68, 0x14, 0x92, 0xb9, 0x20}; uint8_t privateKey[] = { 0x38, 0x61, 0x1d, 0x25, 0x3b, 0xea, 0x85, 0xa2, 0x03, 0x80, 0x53, 0x43, 0xb7, 0x4a, 0x93, 0x6d, 0x3b, 0x13, 0xb9, 0xe3, 0x12, 0x14, 0x53, 0xe9, 0x74, 0x0b, 0x6b, 0x82, 0x7e, 0x33, 0x7e, 0x5d}; uint8_t message[] = { 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, 0x75, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x2e}; uint8_t vrf[] = { 0x75, 0xad, 0x49, 0xbc, 0x95, 0x5f, 0x38, 0xdc, 0xf6, 0x5f, 0xb6, 0x72, 0x08, 0x6b, 0xd5, 0x09, 0xcb, 0x4b, 0x4c, 0x41, 0x04, 0x7d, 0xb1, 0x7e, 0xfd, 0xaf, 0xee, 0xbc, 0x33, 0x03, 0x71, 0xe6}; int result; ec_public_key *public_key = 0; ec_private_key *private_key = 0; signal_buffer *signature = 0; signal_buffer *vrf_output = 0; result = curve_decode_point(&public_key, publicKey, sizeof(publicKey), global_context); ck_assert_int_eq(result, 0); result = curve_decode_private_point(&private_key, privateKey, sizeof(privateKey), global_context); ck_assert_int_eq(result, 0); result = curve_calculate_vrf_signature(global_context, &signature, private_key, message, sizeof(message)); ck_assert_int_eq(result, 0); result = curve_verify_vrf_signature(global_context, &vrf_output, public_key, message, sizeof(message), signal_buffer_data(signature), signal_buffer_len(signature)); ck_assert_int_eq(result, 0); ck_assert_int_eq(signal_buffer_len(vrf_output), sizeof(vrf)); ck_assert_int_eq(memcmp(signal_buffer_data(vrf_output), vrf, sizeof(vrf)), 0); /* Cleanup */ signal_buffer_free(signature); signal_buffer_free(vrf_output); SIGNAL_UNREF(public_key); SIGNAL_UNREF(private_key); }
END_TEST START_TEST(test_curve25519_generate_public) { int result; uint8_t alicePublic[] = { 0x05, 0x1b, 0xb7, 0x59, 0x66, 0xf2, 0xe9, 0x3a, 0x36, 0x91, 0xdf, 0xff, 0x94, 0x2b, 0xb2, 0xa4, 0x66, 0xa1, 0xc0, 0x8b, 0x8d, 0x78, 0xca, 0x3f, 0x4d, 0x6d, 0xf8, 0xb8, 0xbf, 0xa2, 0xe4, 0xee, 0x28}; uint8_t alicePrivate[] = { 0xc8, 0x06, 0x43, 0x9d, 0xc9, 0xd2, 0xc4, 0x76, 0xff, 0xed, 0x8f, 0x25, 0x80, 0xc0, 0x88, 0x8d, 0x58, 0xab, 0x40, 0x6b, 0xf7, 0xae, 0x36, 0x98, 0x87, 0x90, 0x21, 0xb9, 0x6b, 0xb4, 0xbf, 0x59}; ec_private_key *alice_private_key = 0; ec_public_key *alice_expected_public_key = 0; ec_public_key *alice_public_key = 0; /* Initialize Alice's private key */ result = curve_decode_private_point(&alice_private_key, alicePrivate, sizeof(alicePrivate), global_context); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_private_key, 0); /* Initialize Alice's expected public key */ result = curve_decode_point(&alice_expected_public_key, alicePublic, sizeof(alicePublic), global_context); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_expected_public_key, 0); /* Generate Alice's actual public key */ result = curve_generate_public_key(&alice_public_key, alice_private_key); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_public_key, 0); /* Assert that expected and actual public keys match */ ck_assert_int_eq(ec_public_key_compare(alice_expected_public_key, alice_public_key), 0); /* Cleanup */ SIGNAL_UNREF(alice_public_key); SIGNAL_UNREF(alice_expected_public_key); SIGNAL_UNREF(alice_private_key); }
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; ec_private_key *private_key = 0; ratchet_identity_key_pair *result_pair = 0; Textsecure__IdentityKeyPairStructure *key_structure = 0; key_structure = textsecure__identity_key_pair_structure__unpack(0, len, data); if(!key_structure) { result = SG_ERR_INVALID_PROTO_BUF; goto complete; } if(!key_structure->has_publickey || !key_structure->has_privatekey) { result = SG_ERR_INVALID_KEY; goto complete; } result = curve_decode_point( &public_key, key_structure->publickey.data, key_structure->publickey.len, global_context); if(result < 0) { goto complete; } result = curve_decode_private_point( &private_key, key_structure->privatekey.data, key_structure->privatekey.len, global_context); if(result < 0) { goto complete; } result = ratchet_identity_key_pair_create(&result_pair, public_key, private_key); complete: SIGNAL_UNREF(public_key); SIGNAL_UNREF(private_key); if(key_structure) { textsecure__identity_key_pair_structure__free_unpacked(key_structure, 0); } if(result >= 0) { *key_pair = result_pair; } return result; }
END_TEST START_TEST(test_vectors) { int result = 0; ec_public_key *alice_identity_key = 0; ec_public_key *bob_identity_key = 0; fingerprint_generator *generator = 0; fingerprint *alice_fingerprint = 0; fingerprint *bob_fingerprint = 0; signal_buffer *alice_buffer = 0; signal_buffer *bob_buffer = 0; uint8_t aliceIdentity[] = { 0x05, 0x06, 0x86, 0x3b, 0xc6, 0x6d, 0x02, 0xb4, 0x0d, 0x27, 0xb8, 0xd4, 0x9c, 0xa7, 0xc0, 0x9e, 0x92, 0x39, 0x23, 0x6f, 0x9d, 0x7d, 0x25, 0xd6, 0xfc, 0xca, 0x5c, 0xe1, 0x3c, 0x70, 0x64, 0xd8, 0x68 }; uint8_t bobIdentity[] = { 0x05, 0xf7, 0x81, 0xb6, 0xfb, 0x32, 0xfe, 0xd9, 0xba, 0x1c, 0xf2, 0xde, 0x97, 0x8d, 0x4d, 0x5d, 0xa2, 0x8d, 0xc3, 0x40, 0x46, 0xae, 0x81, 0x44, 0x02, 0xb5, 0xc0, 0xdb, 0xd9, 0x6f, 0xda, 0x90, 0x7b }; const char *displayableFingerprint = "300354477692869396892869876765458257569162576843440918079131"; uint8_t aliceScannableFingerprint[] = { 0x08, 0x00, 0x12, 0x31, 0x0a, 0x21, 0x05, 0x06, 0x86, 0x3b, 0xc6, 0x6d, 0x02, 0xb4, 0x0d, 0x27, 0xb8, 0xd4, 0x9c, 0xa7, 0xc0, 0x9e, 0x92, 0x39, 0x23, 0x6f, 0x9d, 0x7d, 0x25, 0xd6, 0xfc, 0xca, 0x5c, 0xe1, 0x3c, 0x70, 0x64, 0xd8, 0x68, 0x12, 0x0c, 0x2b, 0x31, 0x34, 0x31, 0x35, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x1a, 0x31, 0x0a, 0x21, 0x05, 0xf7, 0x81, 0xb6, 0xfb, 0x32, 0xfe, 0xd9, 0xba, 0x1c, 0xf2, 0xde, 0x97, 0x8d, 0x4d, 0x5d, 0xa2, 0x8d, 0xc3, 0x40, 0x46, 0xae, 0x81, 0x44, 0x02, 0xb5, 0xc0, 0xdb, 0xd9, 0x6f, 0xda, 0x90, 0x7b, 0x12, 0x0c, 0x2b, 0x31, 0x34, 0x31, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 }; uint8_t bobScannableFingerprint[] = { 0x08, 0x00, 0x12, 0x31, 0x0a, 0x21, 0x05, 0xf7, 0x81, 0xb6, 0xfb, 0x32, 0xfe, 0xd9, 0xba, 0x1c, 0xf2, 0xde, 0x97, 0x8d, 0x4d, 0x5d, 0xa2, 0x8d, 0xc3, 0x40, 0x46, 0xae, 0x81, 0x44, 0x02, 0xb5, 0xc0, 0xdb, 0xd9, 0x6f, 0xda, 0x90, 0x7b, 0x12, 0x0c, 0x2b, 0x31, 0x34, 0x31, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x1a, 0x31, 0x0a, 0x21, 0x05, 0x06, 0x86, 0x3b, 0xc6, 0x6d, 0x02, 0xb4, 0x0d, 0x27, 0xb8, 0xd4, 0x9c, 0xa7, 0xc0, 0x9e, 0x92, 0x39, 0x23, 0x6f, 0x9d, 0x7d, 0x25, 0xd6, 0xfc, 0xca, 0x5c, 0xe1, 0x3c, 0x70, 0x64, 0xd8, 0x68, 0x12, 0x0c, 0x2b, 0x31, 0x34, 0x31, 0x35, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32 }; result = curve_decode_point(&alice_identity_key, aliceIdentity, sizeof(aliceIdentity), global_context); ck_assert_int_eq(result, 0); result = curve_decode_point(&bob_identity_key, bobIdentity, sizeof(bobIdentity), global_context); ck_assert_int_eq(result, 0); result = fingerprint_generator_create(&generator, 5200, global_context); ck_assert_int_eq(result, 0); result = fingerprint_generator_create_for(generator, "+14152222222", alice_identity_key, "+14153333333", bob_identity_key, &alice_fingerprint); ck_assert_int_eq(result, 0); result = fingerprint_generator_create_for(generator, "+14153333333", bob_identity_key, "+14152222222", alice_identity_key, &bob_fingerprint); ck_assert_int_eq(result, 0); displayable_fingerprint *alice_displayable = fingerprint_get_displayable(alice_fingerprint); ck_assert_str_eq( displayable_fingerprint_text(alice_displayable), displayableFingerprint); displayable_fingerprint *bob_displayable = fingerprint_get_displayable(bob_fingerprint); ck_assert_str_eq( displayable_fingerprint_text(bob_displayable), displayableFingerprint); scannable_fingerprint *alice_scannable = fingerprint_get_scannable(alice_fingerprint); scannable_fingerprint_serialize(&alice_buffer, alice_scannable); ck_assert_int_eq(result, 0); ck_assert_int_eq(signal_buffer_len(alice_buffer), sizeof(aliceScannableFingerprint)); ck_assert_int_eq(memcmp(signal_buffer_data(alice_buffer), aliceScannableFingerprint, sizeof(aliceScannableFingerprint)), 0); scannable_fingerprint *bob_scannable = fingerprint_get_scannable(bob_fingerprint); scannable_fingerprint_serialize(&bob_buffer, bob_scannable); ck_assert_int_eq(result, 0); ck_assert_int_eq(signal_buffer_len(bob_buffer), sizeof(bobScannableFingerprint)); ck_assert_int_eq(memcmp(signal_buffer_data(bob_buffer), bobScannableFingerprint, sizeof(bobScannableFingerprint)), 0); /* Cleanup */ signal_buffer_free(alice_buffer); signal_buffer_free(bob_buffer); fingerprint_generator_free(generator); SIGNAL_UNREF(alice_identity_key); SIGNAL_UNREF(bob_identity_key); SIGNAL_UNREF(alice_fingerprint); SIGNAL_UNREF(bob_fingerprint); }
END_TEST START_TEST(test_expected_fingerprints) { int result = 0; ec_public_key *alice_identity_key = 0; ec_public_key *bob_identity_key = 0; fingerprint_generator *generator = 0; fingerprint *alice_fingerprint = 0; uint8_t alicePublic[] = { 0x05, 0xBB, 0x9D, 0xAD, 0xC3, 0xF2, 0x91, 0x72, 0x6F, 0x91, 0xB7, 0x64, 0xA0, 0x2D, 0x9A, 0x2C, 0x3A, 0x3C, 0xD0, 0xE1, 0x3D, 0x6A, 0x52, 0x70, 0x88, 0x9A, 0x65, 0xE7, 0x17, 0xF5, 0xDB, 0xE5, 0x17}; uint8_t bobPublic[] = { 0x05, 0x20, 0x83, 0x88, 0xDC, 0xF7, 0x23, 0x68, 0xAA, 0xF7, 0x87, 0xC3, 0xF5, 0xD0, 0x08, 0xAF, 0x3D, 0xFC, 0xB0, 0x20, 0xC3, 0xF6, 0x81, 0xC2, 0x84, 0x51, 0x1C, 0x23, 0xB8, 0x16, 0x71, 0x50, 0x05}; const char *expectedDisplayText = "170425132130519178537525891870742040077993076320374280808288"; uint8_t expectedScannableBytes[] = { 0x08, 0x00, 0x12, 0x31, 0x0A, 0x21, 0x05, 0xBB, 0x9D, 0xAD, 0xC3, 0xF2, 0x91, 0x72, 0x6F, 0x91, 0xB7, 0x64, 0xA0, 0x2D, 0x9A, 0x2C, 0x3A, 0x3C, 0xD0, 0xE1, 0x3D, 0x6A, 0x52, 0x70, 0x88, 0x9A, 0x65, 0xE7, 0x17, 0xF5, 0xDB, 0xE5, 0x17, 0x12, 0x0C, 0x2B, 0x31, 0x34, 0x31, 0x35, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x32, 0x1A, 0x31, 0x0A, 0x21, 0x05, 0x20, 0x83, 0x88, 0xDC, 0xF7, 0x23, 0x68, 0xAA, 0xF7, 0x87, 0xC3, 0xF5, 0xD0, 0x08, 0xAF, 0x3D, 0xFC, 0xB0, 0x20, 0xC3, 0xF6, 0x81, 0xC2, 0x84, 0x51, 0x1C, 0x23, 0xB8, 0x16, 0x71, 0x50, 0x05, 0x12, 0x0C, 0x2B, 0x31, 0x34, 0x31, 0x35, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33 }; result = curve_decode_point(&alice_identity_key, alicePublic, sizeof(alicePublic), global_context); ck_assert_int_eq(result, 0); result = curve_decode_point(&bob_identity_key, bobPublic, sizeof(bobPublic), global_context); ck_assert_int_eq(result, 0); result = fingerprint_generator_create(&generator, 1024, global_context); ck_assert_int_eq(result, 0); result = fingerprint_generator_create_for(generator, "+14152222222", alice_identity_key, "+14153333333", bob_identity_key, &alice_fingerprint); ck_assert_int_eq(result, 0); displayable_fingerprint *alice_displayable = fingerprint_get_displayable(alice_fingerprint); ck_assert_str_eq( displayable_fingerprint_text(alice_displayable), expectedDisplayText); scannable_fingerprint *alice_scannable = fingerprint_get_scannable(alice_fingerprint); axolotl_buffer *buffer = 0; scannable_fingerprint_serialize(&buffer, alice_scannable); ck_assert_int_eq(result, 0); ck_assert_int_eq(axolotl_buffer_len(buffer), sizeof(expectedScannableBytes)); ck_assert_int_eq(memcmp(axolotl_buffer_data(buffer), expectedScannableBytes, sizeof(expectedScannableBytes)), 0); /* Cleanup */ axolotl_buffer_free(buffer); fingerprint_generator_free(generator); AXOLOTL_UNREF(alice_identity_key); AXOLOTL_UNREF(bob_identity_key); AXOLOTL_UNREF(alice_fingerprint); }
int scannable_fingerprint_deserialize(scannable_fingerprint **scannable, const uint8_t *data, size_t len, signal_context *global_context) { int result = 0; Textsecure__CombinedFingerprint *combined_fingerprint = 0; uint32_t version = 0; char *local_stable_identifier = 0; ec_public_key *local_identity_key = 0; char *remote_stable_identifier = 0; ec_public_key *remote_identity_key = 0; combined_fingerprint = textsecure__combined_fingerprint__unpack(0, len, data); if(!combined_fingerprint) { result = SG_ERR_INVALID_PROTO_BUF; goto complete; } if(combined_fingerprint->has_version) { version = combined_fingerprint->version; } if(combined_fingerprint->localfingerprint) { if(combined_fingerprint->localfingerprint->has_identifier) { local_stable_identifier = signal_protocol_str_deserialize_protobuf(&combined_fingerprint->localfingerprint->identifier); if(!local_stable_identifier) { result = SG_ERR_NOMEM; goto complete; } } if(combined_fingerprint->localfingerprint->has_publickey) { result = curve_decode_point(&local_identity_key, combined_fingerprint->localfingerprint->publickey.data, combined_fingerprint->localfingerprint->publickey.len, global_context); if(result < 0) { goto complete; } } } if(combined_fingerprint->remotefingerprint) { if(combined_fingerprint->remotefingerprint->has_identifier) { remote_stable_identifier = signal_protocol_str_deserialize_protobuf(&combined_fingerprint->remotefingerprint->identifier); if(!remote_stable_identifier) { result = SG_ERR_NOMEM; goto complete; } } if(combined_fingerprint->remotefingerprint->has_publickey) { result = curve_decode_point(&remote_identity_key, combined_fingerprint->remotefingerprint->publickey.data, combined_fingerprint->remotefingerprint->publickey.len, global_context); if(result < 0) { goto complete; } } } result = scannable_fingerprint_create(scannable, version, local_stable_identifier, local_identity_key, remote_stable_identifier, remote_identity_key); complete: if(combined_fingerprint) { textsecure__combined_fingerprint__free_unpacked(combined_fingerprint, 0); } if(local_stable_identifier) { free(local_stable_identifier); } if(local_identity_key) { SIGNAL_UNREF(local_identity_key); } if(remote_stable_identifier) { free(remote_stable_identifier); } if(remote_identity_key) { SIGNAL_UNREF(remote_identity_key); } return result; }
END_TEST START_TEST(test_curve25519_signature) { int result; uint8_t aliceIdentityPrivate[] = { 0xc0, 0x97, 0x24, 0x84, 0x12, 0xe5, 0x8b, 0xf0, 0x5d, 0xf4, 0x87, 0x96, 0x82, 0x05, 0x13, 0x27, 0x94, 0x17, 0x8e, 0x36, 0x76, 0x37, 0xf5, 0x81, 0x8f, 0x81, 0xe0, 0xe6, 0xce, 0x73, 0xe8, 0x65}; uint8_t aliceIdentityPublic[] = { 0x05, 0xab, 0x7e, 0x71, 0x7d, 0x4a, 0x16, 0x3b, 0x7d, 0x9a, 0x1d, 0x80, 0x71, 0xdf, 0xe9, 0xdc, 0xf8, 0xcd, 0xcd, 0x1c, 0xea, 0x33, 0x39, 0xb6, 0x35, 0x6b, 0xe8, 0x4d, 0x88, 0x7e, 0x32, 0x2c, 0x64}; uint8_t aliceEphemeralPublic[] = { 0x05, 0xed, 0xce, 0x9d, 0x9c, 0x41, 0x5c, 0xa7, 0x8c, 0xb7, 0x25, 0x2e, 0x72, 0xc2, 0xc4, 0xa5, 0x54, 0xd3, 0xeb, 0x29, 0x48, 0x5a, 0x0e, 0x1d, 0x50, 0x31, 0x18, 0xd1, 0xa8, 0x2d, 0x99, 0xfb, 0x4a}; uint8_t aliceSignature[] = { 0x5d, 0xe8, 0x8c, 0xa9, 0xa8, 0x9b, 0x4a, 0x11, 0x5d, 0xa7, 0x91, 0x09, 0xc6, 0x7c, 0x9c, 0x74, 0x64, 0xa3, 0xe4, 0x18, 0x02, 0x74, 0xf1, 0xcb, 0x8c, 0x63, 0xc2, 0x98, 0x4e, 0x28, 0x6d, 0xfb, 0xed, 0xe8, 0x2d, 0xeb, 0x9d, 0xcd, 0x9f, 0xae, 0x0b, 0xfb, 0xb8, 0x21, 0x56, 0x9b, 0x3d, 0x90, 0x01, 0xbd, 0x81, 0x30, 0xcd, 0x11, 0xd4, 0x86, 0xce, 0xf0, 0x47, 0xbd, 0x60, 0xb8, 0x6e, 0x88}; ec_private_key *alice_private_key = 0; ec_public_key *alice_public_key = 0; ec_public_key *alice_ephemeral = 0; /* Initialize Alice's private key */ result = curve_decode_private_point(&alice_private_key, aliceIdentityPrivate, sizeof(aliceIdentityPrivate), global_context); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_private_key, 0); /* Initialize Alice's public key */ result = curve_decode_point(&alice_public_key, aliceIdentityPublic, sizeof(aliceIdentityPublic), global_context); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_public_key, 0); /* Initialize Alice's ephemeral key */ result = curve_decode_point(&alice_ephemeral, aliceEphemeralPublic, sizeof(aliceEphemeralPublic), global_context); ck_assert_int_eq(result, 0); ck_assert_ptr_ne(alice_ephemeral, 0); result = curve_verify_signature(alice_public_key, aliceEphemeralPublic, sizeof(aliceEphemeralPublic), aliceSignature, sizeof(aliceSignature)); ck_assert_msg(result == 1, "signature verification failed"); uint8_t modifiedSignature[sizeof(aliceSignature)]; int i; for(i = 0; i < sizeof(aliceSignature); i++) { memcpy(modifiedSignature, aliceSignature, sizeof(aliceSignature)); modifiedSignature[i] ^= 0x01; result = curve_verify_signature(alice_public_key, aliceEphemeralPublic, sizeof(aliceEphemeralPublic), modifiedSignature, sizeof(modifiedSignature)); ck_assert_msg(result != 1, "signature verification succeeded"); } /* Cleanup */ SIGNAL_UNREF(alice_private_key); SIGNAL_UNREF(alice_public_key); SIGNAL_UNREF(alice_ephemeral); }