-
Notifications
You must be signed in to change notification settings - Fork 316
add posibility to create SharedSecret with applied custom hash function #164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,9 +17,12 @@ | |
| //! | ||
|
|
||
| use core::{ops, ptr}; | ||
| use core::ops::{FnMut}; | ||
| use core::slice::{from_raw_parts, from_raw_parts_mut}; | ||
|
|
||
| use key::{SecretKey, PublicKey}; | ||
| use ffi::{self, CPtr}; | ||
| use types::{c_int, c_uchar, c_void}; | ||
|
|
||
| /// A tag used for recovering the public key from a compact signature | ||
| #[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
|
|
@@ -44,6 +47,31 @@ impl SharedSecret { | |
| } | ||
| } | ||
|
|
||
| /// Creates a new shared secret from a pubkey and secret key with applied custom hash function | ||
| pub fn new_with_hash<F>(point: &PublicKey, scalar: &SecretKey, hash: &mut F) -> SharedSecret | ||
| where F: FnMut(&mut [u8], &[u8], &[u8]) -> i32 | ||
| { | ||
| extern "C" fn hash_callback<F>(output: *mut c_uchar, x: *const c_uchar, y: *const c_uchar, data: *const c_void) -> c_int | ||
| where F: FnMut(&mut [u8], &[u8], &[u8]) -> i32 | ||
| { | ||
| let callback: &mut F = unsafe { &mut *(data as *mut F) }; | ||
| unsafe { (*callback)(from_raw_parts_mut(output, 32), from_raw_parts(x, 32), from_raw_parts(y, 32)) } | ||
| } | ||
| unsafe { | ||
| let mut ss = ffi::SharedSecret::new(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please minimize the code inside the unsafe block to the minimum possible needed (i.e. the ffi call only) |
||
| let res = ffi::secp256k1_ecdh( | ||
| ffi::secp256k1_context_no_precomp, | ||
| &mut ss, | ||
| point.as_ptr(), | ||
| scalar.as_ptr(), | ||
| hash_callback::<F>, | ||
| hash as *mut F as *mut c_void, | ||
| ); | ||
| debug_assert_eq!(res, 1); | ||
| SharedSecret::from(ss) | ||
| } | ||
| } | ||
|
|
||
| /// Obtains a raw pointer suitable for use with FFI functions | ||
| #[inline] | ||
| pub fn as_ptr(&self) -> *const ffi::SharedSecret { | ||
|
|
@@ -114,6 +142,36 @@ mod tests { | |
| assert_eq!(sec1, sec2); | ||
| assert!(sec_odd != sec2); | ||
| } | ||
|
|
||
| #[test] | ||
| fn ecdh_with_hash() { | ||
| let s = Secp256k1::signing_only(); | ||
| let (sk1, pk1) = s.generate_keypair(&mut thread_rng()); | ||
| let (sk2, pk2) = s.generate_keypair(&mut thread_rng()); | ||
|
|
||
| let sec1 = SharedSecret::new_with_hash(&pk1, &sk2, &mut hash); | ||
| let sec2 = SharedSecret::new_with_hash(&pk2, &sk1, &mut hash); | ||
| let sec_odd = SharedSecret::new_with_hash(&pk1, &sk1, &mut hash); | ||
| assert_eq!(sec1, sec2); | ||
| assert!(sec_odd != sec2); | ||
| } | ||
|
|
||
| fn hash(output: &mut [u8], x: &[u8], _y: &[u8]) -> i32 { | ||
| output.copy_from_slice(x); | ||
| 1 | ||
| } | ||
|
|
||
| #[test] | ||
| fn ecdh_with_hash_callback() { | ||
| let s = Secp256k1::signing_only(); | ||
| let (sk1, pk1) = s.generate_keypair(&mut thread_rng()); | ||
| let expect_result: &[u8] = &[123u8;32]; | ||
| let result = SharedSecret::new_with_hash(&pk1, &sk1, &mut |output, _, _ | { | ||
| output.copy_from_slice(expect_result); | ||
| 1 | ||
| }); | ||
| assert_eq!(expect_result, &result[..]); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(all(test, feature = "unstable"))] | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would consider actually using
i16here. and casting it toc_intin the function. that way it should be compatible with any rust supporting architecture while being lossless.