diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2018-09-26 12:28:39 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2018-09-28 13:40:32 +0200 |
commit | 67a874462b716c9af52a64946a22f7456b4e1bd6 (patch) | |
tree | 4de6672d60cf548bf4827e97c793bc79976ac9f5 /openpgp | |
parent | 79ac47b3ac04bbba468fa0f3b6f149c127c88b55 (diff) |
openpgp: Zero secret keys on Drop.
Diffstat (limited to 'openpgp')
-rw-r--r-- | openpgp/Cargo.toml | 1 | ||||
-rw-r--r-- | openpgp/src/lib.rs | 1 | ||||
-rw-r--r-- | openpgp/src/mpis.rs | 36 |
3 files changed, 38 insertions, 0 deletions
diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index 58b1aff4..e9afbdfe 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -11,6 +11,7 @@ bzip2 = { version = "0.3.2", optional = true } failure = "0.1.2" flate2 = { version = "1.0.1", optional = true } lalrpop-util = "0.16" +memsec = "0.5.4" nettle = { git = "https://gitlab.com/sequoia-pgp/nettle-rs.git" } quickcheck = "0.7" rand = "0.5" diff --git a/openpgp/src/lib.rs b/openpgp/src/lib.rs index d2cabdad..690827cc 100644 --- a/openpgp/src/lib.rs +++ b/openpgp/src/lib.rs @@ -47,6 +47,7 @@ extern crate failure; extern crate buffered_reader; +extern crate memsec; extern crate nettle; #[cfg(feature = "compression-deflate")] diff --git a/openpgp/src/mpis.rs b/openpgp/src/mpis.rs index d204ec54..2d2c82d9 100644 --- a/openpgp/src/mpis.rs +++ b/openpgp/src/mpis.rs @@ -50,6 +50,12 @@ impl MPI { hash.update(len); hash.update(&self.value); } + + fn secure_memzero(&mut self) { + unsafe { + ::memsec::memzero(self.value.as_mut_ptr(), self.value.len()); + } + } } impl fmt::Debug for MPI { @@ -345,6 +351,36 @@ pub enum SecretKey { }, } +impl Drop for SecretKey { + fn drop(&mut self) { + use self::SecretKey::*; + match self { + RSA { ref mut d, ref mut p, ref mut q, ref mut u } => { + d.secure_memzero(); + p.secure_memzero(); + q.secure_memzero(); + u.secure_memzero(); + }, + DSA { ref mut x } => + x.secure_memzero(), + Elgamal { ref mut x } => + x.secure_memzero(), + EdDSA { ref mut scalar } => + scalar.secure_memzero(), + ECDSA { ref mut scalar } => + scalar.secure_memzero(), + ECDH { ref mut scalar } => + scalar.secure_memzero(), + Unknown { ref mut mpis, ref mut rest } => { + mpis.iter_mut().for_each(|m| m.secure_memzero()); + unsafe { + ::memsec::memzero(rest.as_mut_ptr(), rest.len()); + } + }, + } + } +} + impl SecretKey { /// Number of octets all MPIs of this instance occupy when serialized. pub fn serialized_len(&self) -> usize { |