Skip to content

Commit 9759cb0

Browse files
committed
Replace SharedSecret with a more generic alternative
1 parent e3aaf00 commit 9759cb0

File tree

4 files changed

+81
-71
lines changed

4 files changed

+81
-71
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 2 additions & 21 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,

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 {

src/ecdh.rs

Lines changed: 61 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,83 +16,94 @@
1616
//! Support for shared secret computations
1717
//!
1818
19-
use core::{ops, ptr};
19+
use core::ptr;
20+
use core::ops::Deref;
2021

2122
use key::{SecretKey, PublicKey};
2223
use ffi::{self, CPtr};
2324

2425
/// A tag used for recovering the public key from a compact signature
25-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
26-
pub struct SharedSecret(ffi::SharedSecret);
26+
#[derive(Copy, Clone)]
27+
pub struct SharedSecret {
28+
data: [u8; 256],
29+
len: usize,
30+
}
31+
impl_raw_debug!(SharedSecret);
32+
33+
34+
// This implementes `From<N>` for all `[u8; N]` arrays from 128bits(16 byte) to 2048bits allowing known hash lengths.
35+
// Lower than 128 bits isn't resistant to collisions any more.
36+
impl_from_array_len!(SharedSecret, 256, (16 20 28 32 48 64 96 128 256));
2737

2838
impl SharedSecret {
29-
/// Creates a new shared secret from a pubkey and secret key
30-
#[inline]
31-
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
32-
unsafe {
33-
let mut ss = ffi::SharedSecret::new();
34-
let res = ffi::secp256k1_ecdh(
35-
ffi::secp256k1_context_no_precomp,
36-
&mut ss,
37-
point.as_c_ptr(),
38-
scalar.as_c_ptr(),
39-
ffi::secp256k1_ecdh_hash_function_default,
40-
ptr::null_mut(),
41-
);
42-
debug_assert_eq!(res, 1);
43-
SharedSecret(ss)
39+
40+
/// Create an empty SharedSecret
41+
pub(crate) fn empty() -> SharedSecret {
42+
SharedSecret {
43+
data: [0u8; 256],
44+
len: 0,
4445
}
4546
}
4647

47-
/// Obtains a raw pointer suitable for use with FFI functions
48-
#[inline]
49-
pub fn as_ptr(&self) -> *const ffi::SharedSecret {
50-
&self.0 as *const _
48+
/// Get a pointer to the underlying data with the specified capacity.
49+
pub(crate) fn get_data_mut_ptr(&mut self) -> *mut u8 {
50+
self.data.as_mut_ptr()
5151
}
52-
}
5352

54-
/// Creates a new shared secret from a FFI shared secret
55-
impl From<ffi::SharedSecret> for SharedSecret {
56-
#[inline]
57-
fn from(ss: ffi::SharedSecret) -> SharedSecret {
58-
SharedSecret(ss)
53+
/// Get the capacity of the underlying data buffer.
54+
pub fn capacity(&self) -> usize {
55+
self.data.len()
5956
}
60-
}
6157

58+
/// Get the len of the used data.
59+
pub fn len(&self) -> usize {
60+
self.len
61+
}
6262

63-
impl ops::Index<usize> for SharedSecret {
64-
type Output = u8;
65-
66-
#[inline]
67-
fn index(&self, index: usize) -> &u8 {
68-
&self.0[index]
63+
/// Set the length of the object.
64+
pub(crate) fn set_len(&mut self, len: usize) {
65+
self.len = len;
6966
}
7067
}
7168

72-
impl ops::Index<ops::Range<usize>> for SharedSecret {
73-
type Output = [u8];
74-
75-
#[inline]
76-
fn index(&self, index: ops::Range<usize>) -> &[u8] {
77-
&self.0[index]
69+
impl PartialEq for SharedSecret {
70+
fn eq(&self, other: &SharedSecret) -> bool {
71+
&self.data[..self.len] == &other.data[..other.len]
7872
}
7973
}
8074

81-
impl ops::Index<ops::RangeFrom<usize>> for SharedSecret {
82-
type Output = [u8];
75+
impl AsRef<[u8]> for SharedSecret {
76+
fn as_ref(&self) -> &[u8] {
77+
&self.data[..self.len]
78+
}
79+
}
8380

84-
#[inline]
85-
fn index(&self, index: ops::RangeFrom<usize>) -> &[u8] {
86-
&self.0[index.start..]
81+
impl Deref for SharedSecret {
82+
type Target = [u8];
83+
fn deref(&self) -> &[u8] {
84+
&self.data[..self.len]
8785
}
8886
}
8987

90-
impl ops::Index<ops::RangeFull> for SharedSecret {
91-
type Output = [u8];
9288

89+
impl SharedSecret {
90+
/// Creates a new shared secret from a pubkey and secret key
9391
#[inline]
94-
fn index(&self, _: ops::RangeFull) -> &[u8] {
95-
&self.0[..]
92+
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
93+
let mut ss = SharedSecret::empty();
94+
let res = unsafe {
95+
ffi::secp256k1_ecdh(
96+
ffi::secp256k1_context_no_precomp,
97+
ss.get_data_mut_ptr(),
98+
point.as_c_ptr(),
99+
scalar.as_c_ptr(),
100+
ffi::secp256k1_ecdh_hash_function_default,
101+
ptr::null_mut(),
102+
)
103+
};
104+
debug_assert_eq!(res, 1); // The default `secp256k1_ecdh_hash_function_default` should always return 1.
105+
ss.set_len(32); // The default hash function is SHA256, which is 32 bytes long.
106+
ss
96107
}
97108
}
98109

src/macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,23 @@ macro_rules! impl_pretty_debug {
2727
}
2828
}
2929

30+
macro_rules! impl_from_array_len {
31+
($thing:ident, $capacity:tt, ($($N:tt)+)) => {
32+
$(
33+
impl From<[u8; $N]> for $thing {
34+
fn from(arr: [u8; $N]) -> Self {
35+
let mut data = [0u8; $capacity];
36+
data[..$N].copy_from_slice(&arr);
37+
$thing {
38+
data,
39+
len: $N,
40+
}
41+
}
42+
}
43+
)+
44+
}
45+
}
46+
3047
#[cfg(feature="serde")]
3148
/// Implements `Serialize` and `Deserialize` for a type `$t` which represents
3249
/// a newtype over a byte-slice over length `$len`. Type `$t` must implement

0 commit comments

Comments
 (0)