diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-02-13 12:27:26 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-02-13 12:27:26 +0100 |
commit | 906c5c106ebd13656c81304af8ad3b9a4c15ca50 (patch) | |
tree | d696649ef147947d91aafae40c9ac84e761eb66a /openpgp/src/crypto | |
parent | f2cd1cf10eef430214d2df3d2dc7283b94fc1949 (diff) |
openpgp: Introduce crypto::Hash.
- This trait formalizes the hashing of OpenPGP packets and related
types.
- Fixes #183.
Diffstat (limited to 'openpgp/src/crypto')
-rw-r--r-- | openpgp/src/crypto/hash.rs | 26 | ||||
-rw-r--r-- | openpgp/src/crypto/mod.rs | 2 | ||||
-rw-r--r-- | openpgp/src/crypto/mpis.rs | 35 |
3 files changed, 41 insertions, 22 deletions
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index 26f9fda6..fdde1976 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -134,9 +134,15 @@ impl nettle::Hash for HashDumper { } } -impl UserID { +/// Hashes OpenPGP packets and related types. +pub trait Hash { + /// Updates the given hash with this object. + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H); +} + +impl Hash for UserID { /// Update the Hash with a hash of the user id. - pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { let mut header = [0; 5]; header[0] = 0xB4; @@ -151,9 +157,9 @@ impl UserID { } } -impl UserAttribute { +impl Hash for UserAttribute { /// Update the Hash with a hash of the user attribute. - pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { let mut header = [0; 5]; header[0] = 0xD1; @@ -168,9 +174,9 @@ impl UserAttribute { } } -impl Key { +impl Hash for Key { /// Update the Hash with a hash of the key. - pub fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { // We hash 8 bytes plus the MPIs. But, the len doesn't // include the tag (1 byte) or the length (2 bytes). let len = (9 - 3) + self.mpis().serialized_len(); @@ -205,16 +211,16 @@ impl Key { } } -impl Signature { +impl Hash for Signature { /// Adds the `Signature` to the provided hash context. - pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { self.fields.hash(hash); } } -impl signature::Builder { +impl Hash for signature::Builder { /// Adds the `Signature` to the provided hash context. - pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { // A version 4 signature packet is laid out as follows: // // version - 1 byte \ diff --git a/openpgp/src/crypto/mod.rs b/openpgp/src/crypto/mod.rs index 5f94e3b4..052be2d4 100644 --- a/openpgp/src/crypto/mod.rs +++ b/openpgp/src/crypto/mod.rs @@ -25,6 +25,8 @@ pub use self::asymmetric::{ KeyPair, }; +pub use self::hash::Hash; + /// Holds a session key. /// /// The session key is cleared when dropped. diff --git a/openpgp/src/crypto/mpis.rs b/openpgp/src/crypto/mpis.rs index f749d90f..24e4d479 100644 --- a/openpgp/src/crypto/mpis.rs +++ b/openpgp/src/crypto/mpis.rs @@ -13,6 +13,7 @@ use constants::{ PublicKeyAlgorithm, SymmetricAlgorithm, }; +use crypto::Hash; use serialize::Serialize; use nettle; @@ -48,14 +49,6 @@ impl MPI { } } - /// Update the Hash with a hash of the MPIs. - pub fn hash<H: nettle::Hash>(&self, hash: &mut H) { - let len = &[(self.bits >> 8) as u8 & 0xFF, self.bits as u8]; - - hash.update(len); - hash.update(&self.value); - } - fn secure_memzero(&mut self) { unsafe { ::memsec::memzero(self.value.as_mut_ptr(), self.value.len()); @@ -87,6 +80,16 @@ impl fmt::Debug for MPI { } } +impl Hash for MPI { + /// Update the Hash with a hash of the MPIs. + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + let len = &[(self.bits >> 8) as u8 & 0xFF, self.bits as u8]; + + hash.update(len); + hash.update(&self.value); + } +} + impl Arbitrary for MPI { fn arbitrary<G: Gen>(g: &mut G) -> Self { loop { @@ -254,9 +257,11 @@ impl PublicKey { &Unknown { .. } => 0, } } +} +impl Hash for PublicKey { /// Update the Hash with a hash of the MPIs. - pub fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { self.serialize(hash).expect("hashing does not fail") } } @@ -554,9 +559,11 @@ impl SecretKey { + rest.len(), } } +} +impl Hash for SecretKey { /// Update the Hash with a hash of the MPIs. - pub fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { self.serialize(hash).expect("hashing does not fail") } } @@ -674,9 +681,11 @@ impl Ciphertext { &Unknown { .. } => None, } } +} +impl Hash for Ciphertext { /// Update the Hash with a hash of the MPIs. - pub fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { self.serialize(hash).expect("hashing does not fail") } } @@ -784,9 +793,11 @@ impl Signature { + rest.len(), } } +} +impl Hash for Signature { /// Update the Hash with a hash of the MPIs. - pub fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { + fn hash<H: nettle::Hash + Write>(&self, hash: &mut H) { self.serialize(hash).expect("hashing does not fail") } } |