From 0ba8be8d63184d0f11bc7ae6bbdf13f1b377abf6 Mon Sep 17 00:00:00 2001 From: moana Date: Mon, 19 Feb 2024 17:08:03 +0100 Subject: [PATCH] Zeroize approach with referencing the inner array --- src/keys/secret.rs | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/src/keys/secret.rs b/src/keys/secret.rs index 06ec094..93e6df5 100644 --- a/src/keys/secret.rs +++ b/src/keys/secret.rs @@ -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); } }