summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-09-25 15:21:05 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-09-25 15:21:05 +0200
commit66e290c2e9550c6cd02efe895fb5e0b0220444d9 (patch)
treee8c1df362886c170f12e9776e64677eaee586afb
parent7b90b8b0dbefb8c98c3eeec709a3899ed41b1089 (diff)
openpgp: Either derive both Eq and Hash, or impl both.
- crypto::mpi::SecretKeyMaterial is the sole exception to this rule, because we are trying to compare them in constant time. Add a hint for clippy that this is okay. - KeyHandle no longer implements Eq, so there is no point in implementing Hash. Simply remove it. - Implement Hash for SubpacketLength by hashing the serialized form. Manually implement Eq for consistency. - Fixes #567.
-rw-r--r--openpgp/src/crypto/mpi.rs1
-rw-r--r--openpgp/src/keyhandle.rs2
-rw-r--r--openpgp/src/packet/signature/subpacket.rs18
3 files changed, 19 insertions, 2 deletions
diff --git a/openpgp/src/crypto/mpi.rs b/openpgp/src/crypto/mpi.rs
index 5088e9f3..1c6757b2 100644
--- a/openpgp/src/crypto/mpi.rs
+++ b/openpgp/src/crypto/mpi.rs
@@ -613,6 +613,7 @@ impl Arbitrary for PublicKey {
/// extensions.
// Deriving Hash here is okay: PartialEq is manually implemented to
// ensure that secrets are compared in constant-time.
+#[allow(clippy::derive_hash_xor_eq)]
#[derive(Clone, Hash)]
pub enum SecretKeyMaterial {
/// RSA secret key.
diff --git a/openpgp/src/keyhandle.rs b/openpgp/src/keyhandle.rs
index c9b613c1..e92efd05 100644
--- a/openpgp/src/keyhandle.rs
+++ b/openpgp/src/keyhandle.rs
@@ -13,7 +13,7 @@ use crate::{
/// Identifies certificates and keys.
///
/// A `KeyHandle` is either a `Fingerprint` or a `KeyID`.
-#[derive(Debug, Clone, Hash)]
+#[derive(Debug, Clone)]
pub enum KeyHandle {
/// A Fingerprint.
Fingerprint(Fingerprint),
diff --git a/openpgp/src/packet/signature/subpacket.rs b/openpgp/src/packet/signature/subpacket.rs
index cbcb2df2..62494df9 100644
--- a/openpgp/src/packet/signature/subpacket.rs
+++ b/openpgp/src/packet/signature/subpacket.rs
@@ -1674,7 +1674,7 @@ impl Subpacket {
}
}
-#[derive(Clone, Debug, Hash, Eq)]
+#[derive(Clone, Debug)]
pub(crate) struct SubpacketLength {
/// The length.
pub(crate) len: u32,
@@ -1715,6 +1715,22 @@ impl PartialEq for SubpacketLength {
}
}
+impl Eq for SubpacketLength {}
+
+impl Hash for SubpacketLength {
+ fn hash<H: Hasher>(&self, state: &mut H) {
+ match &self.raw {
+ Some(raw) => raw.hash(state),
+ None => {
+ let l = self.serialized_len();
+ let mut raw = [0; 5];
+ self.serialize_into(&mut raw[..l]).unwrap();
+ raw[..l].hash(state);
+ },
+ }
+ }
+}
+
impl SubpacketLength {
pub(crate) fn new(len: u32, raw: Option<Vec<u8>>) -> Self {
Self { len, raw }