Skip to content

Commit 4b74091

Browse files
authored
Merge pull request #180 from elichai/2019-11-SharedSecret
Alternative: Passing custom hash functions to ECDH
2 parents e7f0974 + 92c42ca commit 4b74091

File tree

6 files changed

+264
-77
lines changed

6 files changed

+264
-77
lines changed

no_std_test/src/main.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use core::panic::PanicInfo;
5555
use secp256k1::rand::{self, RngCore};
5656
use secp256k1::serde::Serialize;
5757
use secp256k1::*;
58+
use secp256k1::ecdh::SharedSecret;
5859

5960
use serde_cbor::de;
6061
use serde_cbor::ser::SliceWrite;
@@ -102,6 +103,16 @@ fn start(_argc: isize, _argv: *const *const u8) -> isize {
102103
let new_sig: Signature = de::from_mut_slice(&mut cbor_ser[..size]).unwrap();
103104
assert_eq!(sig, new_sig);
104105

106+
let _ = SharedSecret::new(&public_key, &secret_key);
107+
let mut x_arr = [0u8; 32];
108+
let y_arr = unsafe { SharedSecret::new_with_hash_no_panic(&public_key, &secret_key, |x,y| {
109+
x_arr = x;
110+
y.into()
111+
})}.unwrap();
112+
assert_ne!(x_arr, [0u8; 32]);
113+
assert_ne!(&y_arr[..], &[0u8; 32][..]);
114+
115+
105116
unsafe { libc::printf("Verified Successfully!\n\0".as_ptr() as _) };
106117
0
107118
}

secp256k1-sys/src/lib.rs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ pub type EcdhHashFn = unsafe extern "C" fn(
7272
x: *const c_uchar,
7373
y: *const c_uchar,
7474
data: *mut c_void,
75-
);
75+
) -> c_int;
7676

7777
/// A Secp256k1 context, containing various precomputed values and such
7878
/// needed to do elliptic curve computations. If you create one of these
@@ -134,25 +134,6 @@ impl Default for Signature {
134134
}
135135
}
136136

137-
/// Library-internal representation of an ECDH shared secret
138-
#[repr(C)]
139-
pub struct SharedSecret([c_uchar; 32]);
140-
impl_array_newtype!(SharedSecret, c_uchar, 32);
141-
impl_raw_debug!(SharedSecret);
142-
143-
impl SharedSecret {
144-
/// Create a new (zeroed) signature usable for the FFI interface
145-
pub fn new() -> SharedSecret { SharedSecret([0; 32]) }
146-
/// Create a new (uninitialized) signature usable for the FFI interface
147-
#[deprecated(since = "0.15.3", note = "Please use the new function instead")]
148-
pub unsafe fn blank() -> SharedSecret { SharedSecret::new() }
149-
}
150-
151-
impl Default for SharedSecret {
152-
fn default() -> Self {
153-
SharedSecret::new()
154-
}
155-
}
156137

157138
#[cfg(not(feature = "fuzztarget"))]
158139
extern "C" {
@@ -296,7 +277,7 @@ extern "C" {
296277
#[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdh")]
297278
pub fn secp256k1_ecdh(
298279
cx: *const Context,
299-
output: *mut SharedSecret,
280+
output: *mut c_uchar,
300281
pubkey: *const PublicKey,
301282
privkey: *const c_uchar,
302283
hashfp: EcdhHashFn,
@@ -459,7 +440,7 @@ mod fuzz_dummy {
459440
use self::std::{ptr, mem};
460441
use self::std::boxed::Box;
461442
use types::*;
462-
use ::{Signature, Context, NonceFn, EcdhHashFn, PublicKey, SharedSecret,
443+
use ::{Signature, Context, NonceFn, EcdhHashFn, PublicKey,
463444
SECP256K1_START_NONE, SECP256K1_START_VERIFY, SECP256K1_START_SIGN,
464445
SECP256K1_SER_COMPRESSED, SECP256K1_SER_UNCOMPRESSED};
465446

@@ -788,7 +769,7 @@ mod fuzz_dummy {
788769
/// Sets out to point[0..16]||scalar[0..16]
789770
pub unsafe fn secp256k1_ecdh(
790771
cx: *const Context,
791-
out: *mut SharedSecret,
772+
out: *mut c_uchar,
792773
point: *const PublicKey,
793774
scalar: *const c_uchar,
794775
_hashfp: EcdhHashFn,
@@ -801,13 +782,13 @@ mod fuzz_dummy {
801782
ptr::copy(scalar, scalar_prefix[..].as_mut_ptr(), 16);
802783

803784
if (*point).0[0..16] > scalar_prefix[0..16] {
804-
(*out).0[0..16].copy_from_slice(&(*point).0[0..16]);
805-
ptr::copy(scalar, (*out).0[16..32].as_mut_ptr(), 16);
785+
ptr::copy((*point).as_ptr(), out, 16);
786+
ptr::copy(scalar, out.offset(16), 16);
806787
} else {
807-
ptr::copy(scalar, (*out).0[0..16].as_mut_ptr(), 16);
808-
(*out).0[16..32].copy_from_slice(&(*point).0[0..16]);
788+
ptr::copy(scalar, out, 16);
789+
ptr::copy((*point).as_ptr(), out.offset(16), 16);
809790
}
810-
(*out).0[16] = 0x00; // result should always be a valid secret key
791+
(*out.offset(16)) = 0x00; // result should always be a valid secret key
811792
1
812793
}
813794
}

secp256k1-sys/src/macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ macro_rules! impl_array_newtype {
144144
}
145145
}
146146

147+
#[macro_export]
147148
macro_rules! impl_raw_debug {
148149
($thing:ident) => {
149150
impl ::core::fmt::Debug for $thing {

0 commit comments

Comments
 (0)