Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
Merge pull request #15 from typfel/develop
Browse files Browse the repository at this point in the history
Add function for extracting a fingerprint from a prekey
  • Loading branch information
raphaelrobert authored Jun 13, 2019
2 parents 7c5c220 + 21bd2f3 commit ac914b5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/cbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,15 @@ CBoxResult cbox_fingerprint_remote(CBoxSession const * s, CBoxVec ** fp);

// Utilities ////////////////////////////////////////////////////////////////

// Get the public key fingerprint from a prekey
//
// The fingerprint is represented as a hex-encoded byte sequence.
// ---
// `prekey` is the byte array to extract the fingerprint from.
// `prekey_len` is the length of `prekey`.
// `fp` is the pointer to point at the fingerprint.
CBoxResult cbox_fingerprint_prekey(uint8_t const * prekey, size_t prekey_len, CBoxVec ** fp);

// Generate `len` cryptographically strong random bytes.
//
// Returns a pointer to the allocated random bytes.
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,18 @@ fn cbox_fingerprint_remote(session: *const CBoxSession<FileStore>, out: *mut *mu
})
}

#[no_mangle]
pub extern
fn cbox_fingerprint_prekey(c_prekey: *const uint8_t, c_prekey_len: size_t, out: *mut *mut Vec<u8>) -> CBoxResult {
catch_unwind(|| {
let prekey = to_slice(c_prekey, c_prekey_len);
let prekey = try_unwrap!(PreKeyBundle::deserialise(prekey));
let fp = prekey.identity_key.fingerprint().into_bytes();
assign(out, Box::into_raw(Box::new(fp)));
CBoxResult::Success
})
}

#[no_mangle]
pub extern
fn cbox_is_prekey(c_prekey: *const uint8_t, c_prekey_len: size_t, id: *mut uint16_t) -> CBoxResult {
Expand Down
29 changes: 29 additions & 0 deletions test/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,34 @@ void test_random_bytes(CBox const * b) {
printf("OK\n");
}

void test_fingerprint_prekey(CBox const * b) {
printf("test_fingerprint_prekey ... ");

CBoxResult rc = CBOX_SUCCESS;
CBoxVec * prekey = NULL;

rc = cbox_new_prekey(b, 42, &prekey);
assert(rc == CBOX_SUCCESS);

CBoxVec * fingerprint_local = NULL;
CBoxVec * fingerprint_prekey = NULL;

rc = cbox_fingerprint_local(b, &fingerprint_local);
assert(rc == CBOX_SUCCESS);

rc = cbox_fingerprint_prekey(cbox_vec_data(prekey), cbox_vec_len(prekey), &fingerprint_prekey);
assert(rc == CBOX_SUCCESS);

assert(strncmp((char const *) cbox_vec_data(fingerprint_local), (char const *) cbox_vec_data(fingerprint_prekey), cbox_vec_len(fingerprint_prekey)) == 0);

// Cleanup
cbox_vec_free(fingerprint_local);
cbox_vec_free(fingerprint_prekey);
cbox_vec_free(prekey);

printf("OK\n");
}

void test_prekey_check(CBox const * b) {
printf("test_is_prekey ... ");

Expand Down Expand Up @@ -443,6 +471,7 @@ int main() {
test_box_reopen();
test_external_identity();
test_wrong_identity();
test_fingerprint_prekey(alice_box);

// Cleanup
cbox_close(alice_box);
Expand Down

0 comments on commit ac914b5

Please sign in to comment.