summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-11-16 14:50:58 +0100
committerJustus Winter <justus@sequoia-pgp.org>2020-11-24 13:27:51 +0100
commit429358370ad6ec8a00b4fd97c723533ceedb4f40 (patch)
tree55713fc0bc506d638aefeaa1959b969490a7fed4
parentfbbe3cf4ca6b6b1881160925f869065e8f3df58e (diff)
openpgp: Move the compound hashing functions to SignatureFields.
-rw-r--r--openpgp/src/cert.rs7
-rw-r--r--openpgp/src/crypto/hash.rs46
-rw-r--r--openpgp/src/packet/signature.rs38
3 files changed, 40 insertions, 51 deletions
diff --git a/openpgp/src/cert.rs b/openpgp/src/cert.rs
index 65a47486..3c4a34ee 100644
--- a/openpgp/src/cert.rs
+++ b/openpgp/src/cert.rs
@@ -1484,8 +1484,7 @@ impl Cert {
// Use hash prefix as heuristic.
let key = self.primary.key();
match sig.hash_algo().context().and_then(|mut ctx| {
- Signature::$hash_method(&mut ctx, &sig, key,
- $($verify_args),*);
+ sig.$hash_method(&mut ctx, key, $($verify_args),*);
ctx.into_digest()
}) {
Ok(hash) => {
@@ -1712,8 +1711,8 @@ impl Cert {
let key = self.primary.key();
if let Ok(hash) = sig.hash_algo().context()
.and_then(|mut ctx| {
- Signature::$hash_method(&mut ctx, &sig, key,
- $($verify_args),*);
+ sig.$hash_method(&mut ctx, key,
+ $($verify_args),*);
ctx.into_digest()
})
{
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs
index 9221e070..e3311b52 100644
--- a/openpgp/src/crypto/hash.rs
+++ b/openpgp/src/crypto/hash.rs
@@ -387,36 +387,32 @@ impl Hash for signature::SignatureFields {
/// Hashing-related functionality.
///
/// <a name="hashing-functions"></a>
-impl Signature {
+impl signature::SignatureFields {
/// Computes the message digest of standalone signatures.
- pub fn hash_standalone(hash: &mut Context,
- sig: &signature::SignatureFields)
+ pub fn hash_standalone(&self, hash: &mut Context)
{
- sig.hash(hash);
+ self.hash(hash);
}
/// Computes the message digest of timestamp signatures.
- pub fn hash_timestamp(hash: &mut Context,
- sig: &signature::SignatureFields)
+ pub fn hash_timestamp(&self, hash: &mut Context)
{
- Self::hash_standalone(hash, sig);
+ self.hash_standalone(hash);
}
/// Returns the message digest of the direct key signature over
/// the specified primary key.
- pub fn hash_direct_key<P>(hash: &mut Context,
- sig: &signature::SignatureFields,
+ pub fn hash_direct_key<P>(&self, hash: &mut Context,
key: &Key<P, key::PrimaryRole>)
where P: key::KeyParts,
{
key.hash(hash);
- sig.hash(hash);
+ self.hash(hash);
}
/// Returns the message digest of the subkey binding over the
/// specified primary key and subkey.
- pub fn hash_subkey_binding<P, Q>(hash: &mut Context,
- sig: &signature::SignatureFields,
+ pub fn hash_subkey_binding<P, Q>(&self, hash: &mut Context,
key: &Key<P, key::PrimaryRole>,
subkey: &Key<Q, key::SubordinateRole>)
where P: key::KeyParts,
@@ -424,52 +420,49 @@ impl Signature {
{
key.hash(hash);
subkey.hash(hash);
- sig.hash(hash);
+ self.hash(hash);
}
/// Returns the message digest of the primary key binding over the
/// specified primary key and subkey.
- pub fn hash_primary_key_binding<P, Q>(hash: &mut Context,
- sig: &signature::SignatureFields,
+ pub fn hash_primary_key_binding<P, Q>(&self, hash: &mut Context,
key: &Key<P, key::PrimaryRole>,
subkey: &Key<Q, key::SubordinateRole>)
where P: key::KeyParts,
Q: key::KeyParts,
{
- Self::hash_subkey_binding(hash, sig, key, subkey);
+ self.hash_subkey_binding(hash, key, subkey);
}
/// Returns the message digest of the user ID binding over the
/// specified primary key, user ID, and signature.
- pub fn hash_userid_binding<P>(hash: &mut Context,
- sig: &signature::SignatureFields,
+ pub fn hash_userid_binding<P>(&self, hash: &mut Context,
key: &Key<P, key::PrimaryRole>,
userid: &UserID)
where P: key::KeyParts,
{
key.hash(hash);
userid.hash(hash);
- sig.hash(hash);
+ self.hash(hash);
}
/// Returns the message digest of the user attribute binding over
/// the specified primary key, user attribute, and signature.
pub fn hash_user_attribute_binding<P>(
+ &self,
hash: &mut Context,
- sig: &signature::SignatureFields,
key: &Key<P, key::PrimaryRole>,
ua: &UserAttribute)
where P: key::KeyParts,
{
key.hash(hash);
ua.hash(hash);
- sig.hash(hash);
+ self.hash(hash);
}
}
#[cfg(test)]
mod test {
- use super::*;
use crate::Cert;
use crate::parse::Parse;
@@ -480,9 +473,8 @@ mod test {
for (i, binding) in cert.userids().enumerate() {
for selfsig in binding.self_signatures() {
let mut hash = selfsig.hash_algo().context().unwrap();
- Signature::hash_userid_binding(
+ selfsig.hash_userid_binding(
&mut hash,
- selfsig,
cert.primary_key().key(),
binding.userid());
let h = hash.into_digest().unwrap();
@@ -500,9 +492,8 @@ mod test {
{
for selfsig in a.self_signatures() {
let mut hash = selfsig.hash_algo().context().unwrap();
- Signature::hash_user_attribute_binding(
+ selfsig.hash_user_attribute_binding(
&mut hash,
- selfsig,
cert.primary_key().key(),
a.user_attribute());
let h = hash.into_digest().unwrap();
@@ -519,9 +510,8 @@ mod test {
for (i, binding) in cert.subkeys().enumerate() {
for selfsig in binding.self_signatures() {
let mut hash = selfsig.hash_algo().context().unwrap();
- Signature::hash_subkey_binding(
+ selfsig.hash_subkey_binding(
&mut hash,
- selfsig,
cert.primary_key().key(),
binding.key());
let h = hash.into_digest().unwrap();
diff --git a/openpgp/src/packet/signature.rs b/openpgp/src/packet/signature.rs
index e0cf5d37..50f84dd2 100644
--- a/openpgp/src/packet/signature.rs
+++ b/openpgp/src/packet/signature.rs
@@ -578,7 +578,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_standalone(&mut hash, &self);
+ self.hash_standalone(&mut hash);
self.sign(signer, hash.into_digest()?)
}
@@ -691,7 +691,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_timestamp(&mut hash, &self);
+ self.hash_timestamp(&mut hash);
self.sign(signer, hash.into_digest()?)
}
@@ -814,7 +814,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_direct_key(&mut hash, &self, pk);
+ self.hash_direct_key(&mut hash, pk);
self.sign(signer, hash.into_digest()?)
}
@@ -951,7 +951,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_userid_binding(&mut hash, &self, key, userid);
+ self.hash_userid_binding(&mut hash, key, userid);
self.sign(signer, hash.into_digest()?)
}
@@ -1071,7 +1071,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_subkey_binding(&mut hash, &self, primary, subkey);
+ self.hash_subkey_binding(&mut hash, primary, subkey);
self.sign(signer, hash.into_digest()?)
}
@@ -1219,7 +1219,7 @@ impl SignatureBuilder {
self = self.pre_sign(subkey_signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_primary_key_binding(&mut hash, &self, primary, subkey);
+ self.hash_primary_key_binding(&mut hash, primary, subkey);
self.sign(subkey_signer, hash.into_digest()?)
}
@@ -1353,7 +1353,7 @@ impl SignatureBuilder {
self = self.pre_sign(signer)?;
let mut hash = self.hash_algo().context()?;
- Signature::hash_user_attribute_binding(&mut hash, &self, key, ua);
+ self.hash_user_attribute_binding(&mut hash, key, ua);
self.sign(signer, hash.into_digest()?)
}
@@ -2418,7 +2418,7 @@ impl Signature {
// Standalone signatures are like binary-signatures over the
// zero-sized string.
let mut hash = self.hash_algo().context()?;
- Signature::hash_standalone(&mut hash, &self);
+ self.hash_standalone(&mut hash);
self.verify_digest(key, &hash.into_digest()?[..])
}
@@ -2446,7 +2446,7 @@ impl Signature {
// Timestamp signatures are like binary-signatures over the
// zero-sized string.
let mut hash = self.hash_algo().context()?;
- Signature::hash_timestamp(&mut hash, &self);
+ self.hash_timestamp(&mut hash);
self.verify_digest(key, &hash.into_digest()?[..])
}
@@ -2482,7 +2482,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_direct_key(&mut hash, &self, pk);
+ self.hash_direct_key(&mut hash, pk);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2518,7 +2518,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_direct_key(&mut hash, &self, pk);
+ self.hash_direct_key(&mut hash, pk);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2562,7 +2562,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_subkey_binding(&mut hash, &self, pk, subkey);
+ self.hash_subkey_binding(&mut hash, pk, subkey);
self.verify_digest(signer, &hash.into_digest()?[..])?;
// The signature is good, but we may still need to verify the
@@ -2626,7 +2626,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_primary_key_binding(&mut hash, &self, pk, subkey);
+ self.hash_primary_key_binding(&mut hash, pk, subkey);
self.verify_digest(subkey, &hash.into_digest()?[..])
}
@@ -2665,7 +2665,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_subkey_binding(&mut hash, &self, pk, subkey);
+ self.hash_subkey_binding(&mut hash, pk, subkey);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2705,7 +2705,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_userid_binding(&mut hash, &self, pk, userid);
+ self.hash_userid_binding(&mut hash, pk, userid);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2742,7 +2742,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_userid_binding(&mut hash, &self, pk, userid);
+ self.hash_userid_binding(&mut hash, pk, userid);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2782,7 +2782,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_user_attribute_binding(&mut hash, &self, pk, ua);
+ self.hash_user_attribute_binding(&mut hash, pk, ua);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -2820,7 +2820,7 @@ impl Signature {
}
let mut hash = self.hash_algo().context()?;
- Signature::hash_user_attribute_binding(&mut hash, &self, pk, ua);
+ self.hash_user_attribute_binding(&mut hash, pk, ua);
self.verify_digest(signer, &hash.into_digest()?[..])
}
@@ -3277,7 +3277,7 @@ mod test {
"contrib/gnupg/timestamp-signature-by-alice.asc")).unwrap();
if let Packet::Signature(mut sig) = p {
let mut hash = sig.hash_algo().context().unwrap();
- Signature::hash_standalone(&mut hash, &sig);
+ sig.hash_standalone(&mut hash);
let digest = hash.into_digest().unwrap();
eprintln!("{}", crate::fmt::hex::encode(&digest));
sig.verify_timestamp(alpha.primary_key().key()).unwrap();