summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2019-02-13 12:27:26 +0100
committerJustus Winter <justus@sequoia-pgp.org>2019-02-13 12:27:26 +0100
commit906c5c106ebd13656c81304af8ad3b9a4c15ca50 (patch)
treed696649ef147947d91aafae40c9ac84e761eb66a
parentf2cd1cf10eef430214d2df3d2dc7283b94fc1949 (diff)
openpgp: Introduce crypto::Hash.
- This trait formalizes the hashing of OpenPGP packets and related types. - Fixes #183.
-rw-r--r--openpgp/src/crypto/hash.rs26
-rw-r--r--openpgp/src/crypto/mod.rs2
-rw-r--r--openpgp/src/crypto/mpis.rs35
-rw-r--r--openpgp/src/packet/signature/mod.rs1
-rw-r--r--openpgp/src/parse/key.rs1
-rw-r--r--openpgp/src/parse/parse.rs2
-rw-r--r--openpgp/src/tpk/mod.rs2
-rw-r--r--sqv/src/sqv.rs1
8 files changed, 46 insertions, 24 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")
}
}
diff --git a/openpgp/src/packet/signature/mod.rs b/openpgp/src/packet/signature/mod.rs
index 0a3f7cba..6c216c92 100644
--- a/openpgp/src/packet/signature/mod.rs
+++ b/openpgp/src/packet/signature/mod.rs
@@ -8,6 +8,7 @@ use Error;
use Result;
use crypto::{
mpis,
+ Hash,
Signer,
};
use HashAlgorithm;
diff --git a/openpgp/src/parse/key.rs b/openpgp/src/parse/key.rs
index 76afeb01..36c252ba 100644
--- a/openpgp/src/parse/key.rs
+++ b/openpgp/src/parse/key.rs
@@ -4,6 +4,7 @@ use nettle::Hash as NettleHash;
use nettle::hash::insecure_do_not_use::Sha1;
use {
+ crypto::Hash,
packet::Key,
KeyID,
Fingerprint,
diff --git a/openpgp/src/parse/parse.rs b/openpgp/src/parse/parse.rs
index be146c2e..ce0ffa86 100644
--- a/openpgp/src/parse/parse.rs
+++ b/openpgp/src/parse/parse.rs
@@ -15,7 +15,7 @@ use nettle;
use ::buffered_reader::*;
use {
- crypto::aead,
+ crypto::{aead, Hash},
Result,
CTB,
BodyLength,
diff --git a/openpgp/src/tpk/mod.rs b/openpgp/src/tpk/mod.rs
index 15cbf2ab..789c5114 100644
--- a/openpgp/src/tpk/mod.rs
+++ b/openpgp/src/tpk/mod.rs
@@ -11,7 +11,7 @@ use time;
use failure;
use {
- crypto::Signer,
+ crypto::{Hash, Signer},
Error,
Result,
RevocationStatus,
diff --git a/sqv/src/sqv.rs b/sqv/src/sqv.rs
index fc8a98a3..c9cc65c6 100644
--- a/sqv/src/sqv.rs
+++ b/sqv/src/sqv.rs
@@ -16,6 +16,7 @@ use std::collections::{HashMap, HashSet};
use openpgp::{TPK, Packet, packet::Signature, KeyID, RevocationStatus};
use openpgp::constants::HashAlgorithm;
+use openpgp::crypto::Hash;
use openpgp::parse::{Parse, PacketParserResult, PacketParser};
use openpgp::tpk::TPKParser;