Skip to content

Commit

Permalink
Zeroize approach with referencing the inner array
Browse files Browse the repository at this point in the history
  • Loading branch information
moCello committed Feb 19, 2024
1 parent dee7b94 commit 0ba8be8
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/keys/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,30 @@ impl ZeroizeOnDrop for SecretKey {}
#[cfg(test)]
mod tests {
use super::{BlsScalar, SecretKey};

impl SecretKey {
pub fn as_ptr(&self) -> *const BlsScalar {
&*self.0
}
}
extern crate std;
use std::println;

#[test]
fn zeroize() {
let sk = SecretKey::from(BlsScalar::from(42));
let ptr = sk.as_ptr();
let ptr: *const u64 = sk.as_ref().0.as_ptr();

// sanity check that the raw pointer points to the correct data
let scalar = unsafe { core::slice::from_raw_parts(ptr, 1)[0] };
assert_eq!(scalar, BlsScalar::from(42));
// let scalar = unsafe { core::slice::from_raw_parts(ptr, 1) };
// assert_eq!(scalar, [BlsScalar::from(42)]);
let bytes = unsafe { core::slice::from_raw_parts(ptr, 5) };
// assert_eq!(bytes, BlsScalar::from(42).0);
println!("bytes: {:?}", bytes);

// drop the struct which should trigger the zeroizing of the memory
// drop the struct which triggers the zeroizing of the memory
drop(sk);

// check that the memory is erased after the struct was dropped
let scalar = unsafe { core::slice::from_raw_parts(ptr, 1)[0] };
assert_eq!(scalar, BlsScalar::zero());
// let scalar = unsafe { core::slice::from_raw_parts(ptr, 1) };
// assert_eq!(scalar, [BlsScalar::zero()]);
let bytes = unsafe { core::slice::from_raw_parts(ptr, 5) };
println!("bytes: {:?}", bytes);
assert_eq!(bytes, [0; 5]);
// assert_eq!(bytes, BlsScalar::zero().0);
}
}

0 comments on commit 0ba8be8

Please sign in to comment.