Skip to content

Commit 88196bd

Browse files
committed
Merge #312: Display and Debug for secret keys prints a hash
6810c2b Dedicated display_secret fn for secret-containing types (Dr Maxim Orlovsky) 635a6ae Add to_hex converter and add tests for hex conversion (Elichai Turkel) Pull request description: Extract of concept ACK part of #311 related to changing the way secret keys are displayed/printed out ACKs for top commit: apoelstra: ACK 6810c2b thomaseizinger: ACK 6810c2b Tree-SHA512: 22ad7b22f47b177e299ec133129d607f8c3ced1970c4c9bea6e81e49506534c7e15b4fb1d745ba1d3f85f27715f7793c6fef0b93f258037665b7f740b967afe5
2 parents 24a9c9c + 6810c2b commit 88196bd

File tree

5 files changed

+225
-27
lines changed

5 files changed

+225
-27
lines changed

src/key.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,15 @@ use core::{fmt, str};
2121

2222
use super::{from_hex, Secp256k1};
2323
use super::Error::{self, InvalidPublicKey, InvalidPublicKeySum, InvalidSecretKey};
24-
use Signing;
24+
use ::{Signing};
2525
use Verification;
2626
use constants;
2727
use ffi::{self, CPtr};
2828

2929
/// Secret 256-bit key used as `x` in an ECDSA signature
3030
pub struct SecretKey([u8; constants::SECRET_KEY_SIZE]);
3131
impl_array_newtype!(SecretKey, u8, constants::SECRET_KEY_SIZE);
32-
impl_pretty_debug!(SecretKey);
33-
34-
impl fmt::LowerHex for SecretKey {
35-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36-
for ch in &self.0[..] {
37-
write!(f, "{:02x}", *ch)?;
38-
}
39-
Ok(())
40-
}
41-
}
42-
43-
impl fmt::Display for SecretKey {
44-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45-
fmt::LowerHex::fmt(self, f)
46-
}
47-
}
32+
impl_display_secret!(SecretKey);
4833

4934
impl str::FromStr for SecretKey {
5035
type Err = Error;
@@ -164,6 +149,12 @@ impl SecretKey {
164149
SecretKey(sk)
165150
}
166151

152+
/// Serialize the secret key as byte value
153+
#[inline]
154+
pub fn serialize_secret(&self) -> [u8; constants::SECRET_KEY_SIZE] {
155+
self.0
156+
}
157+
167158
#[inline]
168159
/// Negates one secret key.
169160
pub fn negate_assign(
@@ -233,7 +224,8 @@ impl SecretKey {
233224
impl ::serde::Serialize for SecretKey {
234225
fn serialize<S: ::serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
235226
if s.is_human_readable() {
236-
s.collect_str(self)
227+
let mut buf = [0u8; 64];
228+
s.serialize_str(::to_hex(&self.0, &mut buf).expect("fixed-size hex serialization"))
237229
} else {
238230
s.serialize_bytes(&self[..])
239231
}
@@ -516,7 +508,7 @@ impl Ord for PublicKey {
516508
#[cfg(test)]
517509
mod test {
518510
use Secp256k1;
519-
use from_hex;
511+
use {from_hex, to_hex};
520512
use super::super::Error::{InvalidPublicKey, InvalidSecretKey};
521513
use super::{PublicKey, SecretKey};
522514
use super::super::constants;
@@ -710,7 +702,11 @@ mod test {
710702
let (sk, _) = s.generate_keypair(&mut DumbRng(0));
711703

712704
assert_eq!(&format!("{:?}", sk),
713-
"SecretKey(0100000000000000020000000000000003000000000000000400000000000000)");
705+
"SecretKey(#d3e0c51a23169bb5)");
706+
707+
let mut buf = [0u8; constants::SECRET_KEY_SIZE * 2];
708+
assert_eq!(to_hex(&sk[..], &mut buf).unwrap(),
709+
"0100000000000000020000000000000003000000000000000400000000000000");
714710
}
715711

716712
#[test]
@@ -733,7 +729,7 @@ mod test {
733729
let pk = PublicKey::from_slice(&[0x02, 0x18, 0x84, 0x57, 0x81, 0xf6, 0x31, 0xc4, 0x8f, 0x1c, 0x97, 0x09, 0xe2, 0x30, 0x92, 0x06, 0x7d, 0x06, 0x83, 0x7f, 0x30, 0xaa, 0x0c, 0xd0, 0x54, 0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66]).expect("pk");
734730

735731
assert_eq!(
736-
sk.to_string(),
732+
sk.display_secret().to_string(),
737733
"01010101010101010001020304050607ffff0000ffff00006363636363636363"
738734
);
739735
assert_eq!(

src/lib.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ pub use secp256k1_sys as ffi;
137137
#[cfg(all(test, target_arch = "wasm32"))] extern crate wasm_bindgen_test;
138138
#[cfg(feature = "alloc")] extern crate alloc;
139139

140-
use core::{fmt, ptr, str};
141140

142141
#[macro_use]
143142
mod macros;
143+
#[macro_use]
144+
mod secret;
144145
mod context;
145146
pub mod constants;
146147
pub mod ecdh;
@@ -156,7 +157,7 @@ pub use key::PublicKey;
156157
pub use context::*;
157158
use core::marker::PhantomData;
158159
use core::ops::Deref;
159-
use core::mem;
160+
use core::{mem, fmt, ptr, str};
160161
use ffi::{CPtr, types::AlignedType};
161162

162163
#[cfg(feature = "global-context-less-secure")]
@@ -851,6 +852,28 @@ fn from_hex(hex: &str, target: &mut [u8]) -> Result<usize, ()> {
851852
Ok(idx / 2)
852853
}
853854

855+
/// Utility function used to encode hex into a target u8 buffer. Returns
856+
/// a reference to the target buffer as an str. Returns an error if the target
857+
/// buffer isn't big enough.
858+
#[inline]
859+
fn to_hex<'a>(src: &[u8], target: &'a mut [u8]) -> Result<&'a str, ()> {
860+
let hex_len = src.len() * 2;
861+
if target.len() < hex_len {
862+
return Err(());
863+
}
864+
const HEX_TABLE: [u8; 16] = *b"0123456789abcdef";
865+
866+
let mut i = 0;
867+
for &b in src {
868+
target[i] = HEX_TABLE[usize::from(b >> 4)];
869+
target[i+1] = HEX_TABLE[usize::from(b & 0b00001111)];
870+
i +=2 ;
871+
}
872+
let result = &target[..hex_len];
873+
debug_assert!(str::from_utf8(result).is_ok());
874+
return unsafe { Ok(str::from_utf8_unchecked(result)) };
875+
}
876+
854877

855878
#[cfg(test)]
856879
mod tests {
@@ -859,7 +882,7 @@ mod tests {
859882
use std::marker::PhantomData;
860883

861884
use key::{SecretKey, PublicKey};
862-
use super::from_hex;
885+
use super::{from_hex, to_hex};
863886
use super::constants;
864887
use super::{Secp256k1, Signature, Message};
865888
use super::Error::{InvalidMessage, IncorrectSignature, InvalidSignature};
@@ -1186,6 +1209,32 @@ mod tests {
11861209
assert!(Message::from_slice(&[1; constants::MESSAGE_SIZE]).is_ok());
11871210
}
11881211

1212+
#[test]
1213+
fn test_hex() {
1214+
let mut rng = thread_rng();
1215+
const AMOUNT: usize = 1024;
1216+
for i in 0..AMOUNT {
1217+
// 255 isn't a valid utf8 character.
1218+
let mut hex_buf = [255u8; AMOUNT*2];
1219+
let mut src_buf = [0u8; AMOUNT];
1220+
let mut result_buf = [0u8; AMOUNT];
1221+
let src = &mut src_buf[0..i];
1222+
rng.fill_bytes(src);
1223+
1224+
let hex = to_hex(src, &mut hex_buf).unwrap();
1225+
assert_eq!(from_hex(hex, &mut result_buf).unwrap(), i);
1226+
assert_eq!(src, &result_buf[..i]);
1227+
}
1228+
1229+
1230+
assert!(to_hex(&[1;2], &mut [0u8; 3]).is_err());
1231+
assert!(to_hex(&[1;2], &mut [0u8; 4]).is_ok());
1232+
assert!(from_hex("deadbeaf", &mut [0u8; 3]).is_err());
1233+
assert!(from_hex("deadbeaf", &mut [0u8; 4]).is_ok());
1234+
assert!(from_hex("a", &mut [0u8; 4]).is_err());
1235+
assert!(from_hex("ag", &mut [0u8; 4]).is_err());
1236+
}
1237+
11891238
#[test]
11901239
#[cfg(not(fuzzing))] // fixed sig vectors can't work with fuzz-sigs
11911240
fn test_low_s() {

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ macro_rules! impl_pretty_debug {
1818
impl ::core::fmt::Debug for $thing {
1919
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
2020
write!(f, "{}(", stringify!($thing))?;
21-
for i in self[..].iter().cloned() {
21+
for i in &self[..] {
2222
write!(f, "{:02x}", i)?;
2323
}
24-
write!(f, ")")
24+
f.write_str(")")
2525
}
2626
}
2727
}

src/schnorrsig.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ impl str::FromStr for Signature {
7676
}
7777

7878
/// Opaque data structure that holds a keypair consisting of a secret and a public key.
79-
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]
79+
#[derive(Clone)]
8080
pub struct KeyPair(ffi::KeyPair);
81+
impl_display_secret!(KeyPair);
8182

8283
/// A Schnorr public key, used for verification of Schnorr signatures
8384
#[derive(Copy, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, Hash)]

src/secret.rs

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Bitcoin secp256k1 bindings
2+
// Written in 2021 by
3+
// Maxim Orlovsky <[email protected]>
4+
//
5+
// To the extent possible under law, the author(s) have dedicated all
6+
// copyright and related and neighboring rights to this software to
7+
// the public domain worldwide. This software is distributed without
8+
// any warranty.
9+
//
10+
// You should have received a copy of the CC0 Public Domain Dedication
11+
// along with this software.
12+
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
13+
//
14+
15+
//! Helpers for displaying secret values
16+
17+
use ::core::fmt;
18+
use ::{SecretKey, schnorrsig::KeyPair, to_hex};
19+
use constants::SECRET_KEY_SIZE;
20+
21+
macro_rules! impl_display_secret {
22+
// Default hasher exists only in standard library and not alloc
23+
($thing:ident) => {
24+
#[cfg(feature = "std")]
25+
impl ::core::fmt::Debug for $thing {
26+
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
27+
use ::core::hash::Hasher;
28+
const DEBUG_HASH_TAG: &[u8] = &[
29+
0x66, 0xa6, 0x77, 0x1b, 0x9b, 0x6d, 0xae, 0xa1, 0xb2, 0xee, 0x4e, 0x07, 0x49,
30+
0x4a, 0xac, 0x87, 0xa9, 0xb8, 0x5b, 0x4b, 0x35, 0x02, 0xaa, 0x6d, 0x0f, 0x79,
31+
0xcb, 0x63, 0xe6, 0xf8, 0x66, 0x22
32+
]; // =SHA256(b"rust-secp256k1DEBUG");
33+
34+
let mut hasher = ::std::collections::hash_map::DefaultHasher::new();
35+
36+
hasher.write(DEBUG_HASH_TAG);
37+
hasher.write(DEBUG_HASH_TAG);
38+
hasher.write(&self.serialize_secret());
39+
let hash = hasher.finish();
40+
41+
f.debug_tuple(stringify!($thing))
42+
.field(&format_args!("#{:016x}", hash))
43+
.finish()
44+
}
45+
}
46+
}
47+
}
48+
49+
/// Helper struct for safely printing secrets (like [`SecretKey`] value).
50+
/// Formats the explicit byte value of the secret kept inside the type as a
51+
/// little-endian hexadecimal string using the provided formatter.
52+
///
53+
/// Secrets should not implement neither [`Debug`] and [`Display`] traits directly,
54+
/// and instead provide `fn display_secret<'a>(&'a self) -> DisplaySecret<'a>`
55+
/// function to be used in different display contexts (see "examples" below).
56+
///
57+
/// [`Display`]: fmt::Display
58+
/// [`Debug`]: fmt::Debug
59+
pub struct DisplaySecret {
60+
secret: [u8; SECRET_KEY_SIZE]
61+
}
62+
63+
impl fmt::Debug for DisplaySecret {
64+
#[inline]
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
let mut slice = [0u8; 64];
67+
let hex = to_hex(&self.secret, &mut slice).expect("fixed-size hex serializer failed");
68+
f.debug_tuple("DisplaySecret")
69+
.field(&hex)
70+
.finish()
71+
}
72+
}
73+
74+
impl fmt::Display for DisplaySecret {
75+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76+
for i in &self.secret {
77+
write!(f, "{:02x}", i)?;
78+
}
79+
Ok(())
80+
}
81+
}
82+
83+
impl SecretKey {
84+
/// Formats the explicit byte value of the secret key kept inside the type as a
85+
/// little-endian hexadecimal string using the provided formatter.
86+
///
87+
/// This is the only method that outputs the actual secret key value, and, thus,
88+
/// should be used with extreme precaution.
89+
///
90+
/// # Example
91+
///
92+
/// ```
93+
/// use secp256k1::key::ONE_KEY;
94+
/// let key = ONE_KEY;
95+
/// // Normal display hides value
96+
/// assert_eq!(
97+
/// "SecretKey(#2518682f7819fb2d)",
98+
/// format!("{:?}", key)
99+
/// );
100+
/// // Here we explicitly display the secret value:
101+
/// assert_eq!(
102+
/// "0000000000000000000000000000000000000000000000000000000000000001",
103+
/// format!("{}", key.display_secret())
104+
/// );
105+
/// assert_eq!(
106+
/// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
107+
/// format!("{:?}", key.display_secret())
108+
/// );
109+
/// ```
110+
#[inline]
111+
pub fn display_secret(&self) -> DisplaySecret {
112+
DisplaySecret { secret: self.serialize_secret() }
113+
}
114+
}
115+
116+
impl KeyPair {
117+
/// Formats the explicit byte value of the secret key kept inside the type as a
118+
/// little-endian hexadecimal string using the provided formatter.
119+
///
120+
/// This is the only method that outputs the actual secret key value, and, thus,
121+
/// should be used with extreme precaution.
122+
///
123+
/// # Example
124+
///
125+
/// ```
126+
/// use secp256k1::key::ONE_KEY;
127+
/// use secp256k1::schnorrsig::KeyPair;
128+
/// use secp256k1::Secp256k1;
129+
///
130+
/// let secp = Secp256k1::new();
131+
/// let key = ONE_KEY;
132+
/// let key = KeyPair::from_secret_key(&secp, key);
133+
///
134+
/// // Normal display hides value
135+
/// assert_eq!(
136+
/// "KeyPair(#2518682f7819fb2d)",
137+
/// format!("{:?}", key)
138+
/// );
139+
/// // Here we explicitly display the secret value:
140+
/// assert_eq!(
141+
/// "0000000000000000000000000000000000000000000000000000000000000001",
142+
/// format!("{}", key.display_secret())
143+
/// );
144+
/// assert_eq!(
145+
/// "DisplaySecret(\"0000000000000000000000000000000000000000000000000000000000000001\")",
146+
/// format!("{:?}", key.display_secret())
147+
/// );
148+
#[inline]
149+
pub fn display_secret(&self) -> DisplaySecret {
150+
DisplaySecret { secret: self.serialize_secret() }
151+
}
152+
}

0 commit comments

Comments
 (0)