Skip to content
This repository has been archived by the owner on Oct 17, 2022. It is now read-only.

Commit

Permalink
[crypto] zeroize bls12381 secrets (#733)
Browse files Browse the repository at this point in the history
* [crypto] implement zeroize on bls12381 secrets
  • Loading branch information
punwai authored and huitseeker committed Aug 12, 2022
1 parent bc21b32 commit 2237554
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
32 changes: 31 additions & 1 deletion crypto/src/bls12381.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use blst::min_sig as blst;

use once_cell::sync::OnceCell;
use rand::{rngs::OsRng, RngCore};
use zeroize::Zeroize;

use crate::{
pubkey_bytes::PublicKeyBytes,
Expand Down Expand Up @@ -403,7 +404,7 @@ impl KeyPair for BLS12381KeyPair {
}

fn private(self) -> Self::PrivKey {
self.secret
BLS12381PrivateKey::from_bytes(self.secret.as_ref()).unwrap()
}

fn generate<R: rand::CryptoRng + rand::RngCore>(rng: &mut R) -> Self {
Expand Down Expand Up @@ -595,3 +596,32 @@ impl From<&BLS12381PublicKey> for BLS12381PublicKeyBytes {
BLS12381PublicKeyBytes::from_bytes(pk.as_ref()).unwrap()
}
}

impl zeroize::Zeroize for BLS12381PrivateKey {
fn zeroize(&mut self) {
self.bytes.take().zeroize();
self.privkey.zeroize();
}
}

impl zeroize::ZeroizeOnDrop for BLS12381PrivateKey {}

impl Drop for BLS12381PrivateKey {
fn drop(&mut self) {
self.zeroize();
}
}

impl zeroize::Zeroize for BLS12381KeyPair {
fn zeroize(&mut self) {
self.secret.zeroize()
}
}

impl zeroize::ZeroizeOnDrop for BLS12381KeyPair {}

impl Drop for BLS12381KeyPair {
fn drop(&mut self) {
self.zeroize();
}
}
40 changes: 39 additions & 1 deletion crypto/src/tests/bls12381_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use crate::{
BLS12381PublicKeyBytes, BLS12381Signature,
},
hkdf::hkdf_generate_from_ikm,
traits::{AggregateAuthenticator, EncodeDecodeBase64, KeyPair, ToFromBytes, VerifyingKey},
traits::{
AggregateAuthenticator, EncodeDecodeBase64, KeyPair, SigningKey, ToFromBytes, VerifyingKey,
},
};
use rand::{rngs::StdRng, SeedableRng as _};
use sha3::Sha3_256;
Expand Down Expand Up @@ -400,3 +402,39 @@ async fn signature_service() {
// Verify the signature we received.
assert!(pk.verify(digest.as_ref(), &signature).is_ok());
}

// Checks if the private keys zeroed out
#[test]
fn test_sk_zeroization_on_drop() {
let ptr: *const u8;
let bytes_ptr: *const u8;

let mut sk_bytes = Vec::new();

{
let mut rng = StdRng::from_seed([9; 32]);
let kp = BLS12381KeyPair::generate(&mut rng);
let sk = kp.private();
sk_bytes.extend_from_slice(sk.as_ref());

ptr = std::ptr::addr_of!(sk.privkey) as *const u8;
bytes_ptr = &sk.as_ref()[0] as *const u8;

let sk_memory: &[u8] =
unsafe { ::std::slice::from_raw_parts(bytes_ptr, BLS12381PrivateKey::LENGTH) };
// Assert that this is equal to sk_bytes before deletion
assert_eq!(sk_memory, &sk_bytes[..]);
}

// Check that self.privkey is zeroized
unsafe {
for i in 0..BLS12381PrivateKey::LENGTH {
assert!(*ptr.add(i) == 0);
}
}

// Check that self.bytes is zeroized
let sk_memory: &[u8] =
unsafe { ::std::slice::from_raw_parts(bytes_ptr, BLS12381PrivateKey::LENGTH) };
assert_ne!(sk_memory, &sk_bytes[..]);
}

0 comments on commit 2237554

Please sign in to comment.