diff --git a/src/lib.rs b/src/lib.rs index 656818a..24bf5a3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,7 +368,13 @@ impl Default for SecretKey { impl Into for SecretKey { fn into(self) -> Scalar { - self.0 + self.0.clone() + } +} + +impl Drop for SecretKey { + fn drop(&mut self) { + self.0.clear(); } } @@ -573,6 +579,7 @@ impl SharedSecret { Ok(SharedSecret(inner)) } + } impl AsRef<[u8]> for SharedSecret { @@ -581,6 +588,14 @@ impl AsRef<[u8]> for SharedSecret { } } +impl Drop for SharedSecret { + fn drop(&mut self) { + unsafe { + core::ptr::write_volatile(&mut self.0, [0u8; 32]); + } + } +} + /// Check signature is a valid message signed by public key. pub fn verify(message: &Message, signature: &Signature, pubkey: &PublicKey) -> bool { ECMULT_CONTEXT.verify_raw(&signature.r, &signature.s, &pubkey.0, &message.0) diff --git a/src/scalar.rs b/src/scalar.rs index 79fad36..e59eb2b 100644 --- a/src/scalar.rs +++ b/src/scalar.rs @@ -31,7 +31,9 @@ pub struct Scalar(pub [u32; 8]); impl Scalar { /// Clear a scalar to prevent the leak of sensitive data. pub fn clear(&mut self) { - self.0 = [0u32; 8]; + unsafe { + core::ptr::write_volatile(&mut self.0, [0u32; 8]); + } } /// Set a scalar to an unsigned integer.