Skip to content

Commit f7b229e

Browse files
committed
Obfuscate SharedSecret when printing
Currently printing the `SharedSecret` using `Display` or `Debug` prints the real secret, this is sub-optimal. We have a solution for other secrets in the project where printing is obfuscated and we provide a `display_secret` method for explicitly printing. Mirror the logic for other secrets and obfuscate the `SharedSecret` when printing.
1 parent ee6a09d commit f7b229e

File tree

2 files changed

+54
-3
lines changed

2 files changed

+54
-3
lines changed

src/ecdh.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ use core::borrow::Borrow;
2121
use key::{SecretKey, PublicKey};
2222
use ffi::{self, CPtr};
2323
use secp256k1_sys::types::{c_int, c_uchar, c_void};
24+
use constants;
25+
26+
// The logic for displaying shared secrets relies on this (see `secret.rs`).
27+
const SHARED_SECRET_SIZE: usize = constants::SECRET_KEY_SIZE;
2428

2529
/// Enables two parties to create a shared secret without revealing their own secrets.
2630
///
@@ -39,14 +43,15 @@ use secp256k1_sys::types::{c_int, c_uchar, c_void};
3943
/// assert_eq!(sec1, sec2);
4044
/// # }
4145
// ```
42-
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
43-
pub struct SharedSecret([u8; 32]);
46+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
47+
pub struct SharedSecret([u8; SHARED_SECRET_SIZE]);
48+
impl_display_secret!(SharedSecret);
4449

4550
impl SharedSecret {
4651
/// Creates a new shared secret from a pubkey and secret key.
4752
#[inline]
4853
pub fn new(point: &PublicKey, scalar: &SecretKey) -> SharedSecret {
49-
let mut buf = [0u8; 32];
54+
let mut buf = [0u8; SHARED_SECRET_SIZE];
5055
let res = unsafe {
5156
ffi::secp256k1_ecdh(
5257
ffi::secp256k1_context_no_precomp,
@@ -62,6 +67,12 @@ impl SharedSecret {
6267
debug_assert_eq!(res, 1);
6368
SharedSecret(buf)
6469
}
70+
71+
/// Gets the shared secret as a byte value.
72+
#[inline]
73+
pub fn into_secret(&self) -> [u8; SHARED_SECRET_SIZE] {
74+
self.0
75+
}
6576
}
6677

6778
impl Borrow<[u8]> for SharedSecret {

src/secret.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
use ::core::fmt;
1818
use ::{SecretKey, KeyPair, to_hex};
19+
use ecdh::SharedSecret;
1920
use constants::SECRET_KEY_SIZE;
2021

2122
macro_rules! impl_display_secret {
@@ -182,3 +183,42 @@ impl KeyPair {
182183
DisplaySecret { secret: self.into_secret() }
183184
}
184185
}
186+
187+
impl SharedSecret {
188+
/// Formats the explicit byte value of the shared secret kept inside the type as a
189+
/// little-endian hexadecimal string using the provided formatter.
190+
///
191+
/// This is the only method that outputs the actual shared secret value, and, thus,
192+
/// should be used with extreme caution.
193+
///
194+
/// # Examples
195+
///
196+
/// ```
197+
/// # #[cfg(all(feature = "std", not(feature = "bitcoin_hashes")))] {
198+
/// # use std::str::FromStr;
199+
/// # use secp256k1::{SecretKey, PublicKey};
200+
/// use secp256k1::ecdh::SharedSecret;
201+
///
202+
/// # let pk = PublicKey::from_slice(&[3, 23, 183, 225, 206, 31, 159, 148, 195, 42, 67, 115, 146, 41, 248, 140, 11, 3, 51, 41, 111, 180, 110, 143, 114, 134, 88, 73, 198, 174, 52, 184, 78]).expect("hard coded slice should parse correctly");
203+
/// # let sk = SecretKey::from_str("57f0148f94d13095cfda539d0da0d1541304b678d8b36e243980aab4e1b7cead").unwrap();
204+
///
205+
/// let secret = SharedSecret::new(&pk, &sk);
206+
/// // Normal display hides the value.
207+
/// assert_eq!(format!("{:?}", secret), "SharedSecret(#2f7b793c9a1de4bf)");
208+
///
209+
/// // Here we explicitly display the secret value:
210+
/// assert_eq!(
211+
/// format!("{}", secret.display_secret()),
212+
/// "cf05ae7da039ddce6d56dd57d3000c6dd91c6f1695eae47e05389f11e2467043"
213+
/// );
214+
/// // Also, we can explicitly display with `Debug`:
215+
/// assert_eq!(
216+
/// format!("{:?}", secret.display_secret()),
217+
/// format!("DisplaySecret(\"{}\")", secret.display_secret()));
218+
/// # }
219+
/// ```
220+
#[inline]
221+
pub fn display_secret(&self) -> DisplaySecret {
222+
DisplaySecret { secret: self.serialize_secret() }
223+
}
224+
}

0 commit comments

Comments
 (0)