Skip to content

Commit cdaa7c1

Browse files
committed
Improve (SigningKey|PrivateKey)::to_bytes() to use zerozing on output
1 parent 3af9abf commit cdaa7c1

File tree

8 files changed

+80
-15
lines changed

8 files changed

+80
-15
lines changed

libparsec/crates/crypto/src/rustcrypto/private.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ impl PrivateKey {
199199
Ok(SecretKey::try_from(&raw_512[..Self::SIZE]).expect("valid size"))
200200
}
201201

202-
pub fn to_bytes(&self) -> [u8; KEY_SIZE] {
203-
self.0.to_bytes()
202+
pub fn to_bytes(&self) -> zeroize::Zeroizing<[u8; KEY_SIZE]> {
203+
self.0.to_bytes().into()
204204
}
205205
}
206206

libparsec/crates/crypto/src/rustcrypto/sign.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ impl SigningKey {
5353
self.0.sign(data).to_bytes()
5454
}
5555

56-
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
57-
self.0.to_bytes()
56+
pub fn to_bytes(&self) -> zeroize::Zeroizing<[u8; Self::SIZE]> {
57+
self.0.to_bytes().into()
5858
}
5959
}
6060

@@ -86,7 +86,7 @@ impl Serialize for SigningKey {
8686
where
8787
S: serde::Serializer,
8888
{
89-
serializer.serialize_bytes(&self.to_bytes())
89+
serializer.serialize_bytes(self.to_bytes().as_ref())
9090
}
9191
}
9292

libparsec/crates/crypto/src/sodium/private.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,8 @@ impl PrivateKey {
107107
Ok(key)
108108
}
109109

110-
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
111-
self.0.as_bytes().to_owned()
110+
pub fn to_bytes(&self) -> zeroize::Zeroizing<[u8; Self::SIZE]> {
111+
self.0.as_bytes().to_owned().into()
112112
}
113113
}
114114

libparsec/crates/crypto/src/sodium/sign.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,11 @@ impl SigningKey {
5151
sign_detached(data, &self.0).expect("Cannot sign data")
5252
}
5353

54-
pub fn to_bytes(&self) -> [u8; Self::SIZE] {
54+
pub fn to_bytes(&self) -> zeroize::Zeroizing<[u8; Self::SIZE]> {
5555
// SecretKey is composed of Seed then PublicKey, we export only seed here
5656
<[u8; Self::SIZE]>::try_from(&self.0.as_bytes()[..Self::SIZE])
5757
.expect("The internal array is > Self::SIZE")
58+
.into()
5859
}
5960
}
6061

libparsec/crates/crypto/tests/unit/private.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,67 @@ fn pubkey_hash() {
186186
assert_eq!(hash(&vk1), hash(&vk1));
187187
assert_ne!(hash(&vk1), hash(&vk2));
188188
}
189+
190+
#[platform::test]
191+
fn private_key_from() {
192+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
193+
let key = PrivateKey::from(raw);
194+
195+
assert_eq!(*key.to_bytes(), raw);
196+
}
197+
198+
#[platform::test]
199+
fn private_key_try_from_array_of_u8() {
200+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
201+
let key = PrivateKey::try_from(raw.as_ref()).unwrap();
202+
203+
assert_eq!(*key.to_bytes(), raw);
204+
205+
let outcome = PrivateKey::try_from(b"<too_small>".as_ref());
206+
207+
assert_eq!(outcome, Err(CryptoError::DataSize));
208+
}
209+
210+
#[platform::test]
211+
fn private_key_try_from_serde_bytes() {
212+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
213+
let key = PrivateKey::try_from(serde_bytes::Bytes::new(&raw)).unwrap();
214+
215+
assert_eq!(*key.to_bytes(), raw);
216+
217+
let outcome = PrivateKey::try_from(serde_bytes::Bytes::new(b"<too_small>"));
218+
219+
assert_eq!(outcome, Err(CryptoError::DataSize));
220+
}
221+
222+
#[platform::test]
223+
fn publickey_key_try_from_static_array_of_u8() {
224+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
225+
let key = PublicKey::from(raw);
226+
227+
assert_eq!(key.as_ref(), raw);
228+
}
229+
230+
#[platform::test]
231+
fn publickey_key_try_from_array_of_u8() {
232+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
233+
let key = PublicKey::try_from(raw.as_ref()).unwrap();
234+
235+
assert_eq!(key.as_ref(), raw);
236+
237+
let outcome = PublicKey::try_from(b"<too_small>".as_ref());
238+
239+
assert_eq!(outcome, Err(CryptoError::DataSize));
240+
}
241+
242+
#[platform::test]
243+
fn publickey_key_try_from_serde_bytes() {
244+
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
245+
let key = PublicKey::try_from(serde_bytes::Bytes::new(&raw)).unwrap();
246+
247+
assert_eq!(key.as_ref(), raw);
248+
249+
let outcome = PublicKey::try_from(serde_bytes::Bytes::new(b"<too_small>"));
250+
251+
assert_eq!(outcome, Err(CryptoError::DataSize));
252+
}

libparsec/crates/crypto/tests/unit/sign.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,15 @@ fn signing_key_from() {
187187
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
188188
let key = SigningKey::from(raw);
189189

190-
assert_eq!(key.to_bytes(), raw);
190+
assert_eq!(*key.to_bytes(), raw);
191191
}
192192

193193
#[platform::test]
194194
fn signing_key_try_from_array_of_u8() {
195195
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
196196
let key = SigningKey::try_from(raw.as_ref()).unwrap();
197197

198-
assert_eq!(key.to_bytes(), raw);
198+
assert_eq!(*key.to_bytes(), raw);
199199

200200
let outcome = SigningKey::try_from(b"<too_small>".as_ref());
201201

@@ -207,7 +207,7 @@ fn signing_key_try_from_serde_bytes() {
207207
let raw = hex!("78958e49abad190be2d51bab73af07f87682cfcd65cceedd27e4b2a94bfd8537");
208208
let key = SigningKey::try_from(serde_bytes::Bytes::new(&raw)).unwrap();
209209

210-
assert_eq!(key.to_bytes(), raw);
210+
assert_eq!(*key.to_bytes(), raw);
211211

212212
let outcome = SigningKey::try_from(serde_bytes::Bytes::new(b"<too_small>"));
213213

libparsec/crates/testbed/src/template/crc_hash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ impl CrcHash for HashDigest {
253253
impl CrcHash for SigningKey {
254254
fn crc_hash(&self, hasher: &mut crc32fast::Hasher) {
255255
hasher.update(b"SigningKey");
256-
hasher.update(&self.to_bytes());
256+
hasher.update(self.to_bytes().as_ref());
257257
}
258258
}
259259

@@ -267,7 +267,7 @@ impl CrcHash for VerifyKey {
267267
impl CrcHash for PrivateKey {
268268
fn crc_hash(&self, hasher: &mut crc32fast::Hasher) {
269269
hasher.update(b"PrivateKey");
270-
hasher.update(&self.to_bytes());
270+
hasher.update(self.to_bytes().as_ref());
271271
}
272272
}
273273

server/src/crypto.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ impl SigningKey {
106106
}
107107

108108
fn encode<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
109-
PyBytes::new(py, &self.0.to_bytes())
109+
PyBytes::new(py, self.0.to_bytes().as_ref())
110110
}
111111
}
112112

@@ -266,7 +266,7 @@ impl PrivateKey {
266266
}
267267

268268
fn encode<'py>(&self, py: Python<'py>) -> Bound<'py, PyBytes> {
269-
PyBytes::new(py, &self.0.to_bytes())
269+
PyBytes::new(py, self.0.to_bytes().as_ref())
270270
}
271271
}
272272

0 commit comments

Comments
 (0)