change impl Drop for SharedSecret to use clear directly

This commit is contained in:
DebugSteven 2018-11-16 12:48:49 -05:00
parent cb18af7c1b
commit 4c6498ec69

View file

@ -12,7 +12,6 @@
//! This implements x25519 key exchange as specified by Mike Hamburg //! This implements x25519 key exchange as specified by Mike Hamburg
//! and Adam Langley in [RFC7748](https://tools.ietf.org/html/rfc7748). //! and Adam Langley in [RFC7748](https://tools.ietf.org/html/rfc7748).
use core::mem;
use core::ops::Mul; use core::ops::Mul;
use clear_on_drop::clear::Clear; use clear_on_drop::clear::Clear;
@ -83,18 +82,15 @@ impl Ephemeral {
} }
#[repr(C)]
/// A DH SharedSecret /// A DH SharedSecret
#[repr(C)]
#[derive(Default)] // we derive Default in order to use the clear() method in Drop
pub struct SharedSecret(pub (crate) MontgomeryPoint); pub struct SharedSecret(pub (crate) MontgomeryPoint);
/// Overwrite shared secret material with null bytes when it goes out of scope. /// Overwrite shared secret material with null bytes when it goes out of scope.
impl Drop for SharedSecret { impl Drop for SharedSecret {
fn drop(&mut self) { fn drop(&mut self) {
let bytes: &mut [u8; 32] = unsafe { self.0.clear();
mem::transmute::<&mut MontgomeryPoint, &mut [u8; 32]>
(&mut self.0)
};
bytes.clear();
} }
} }