diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-11-17 13:37:43 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-12-07 16:28:40 +0100 |
commit | f444c789247fb7d96a825b8f66fde6a44c576584 (patch) | |
tree | 6902c153e19c39246be4aa892140e544cb72989b | |
parent | b222796002d9276f55a2b961aabfac6890d9b527 (diff) |
openpgp: Add Digeset::algo.
-rw-r--r-- | openpgp/src/crypto/backend/cng/hash.rs | 25 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/nettle/hash.rs | 20 | ||||
-rw-r--r-- | openpgp/src/crypto/backend/sha1cd.rs | 4 | ||||
-rw-r--r-- | openpgp/src/crypto/hash.rs | 9 |
4 files changed, 48 insertions, 10 deletions
diff --git a/openpgp/src/crypto/backend/cng/hash.rs b/openpgp/src/crypto/backend/cng/hash.rs index 3c797797..1f58d4cc 100644 --- a/openpgp/src/crypto/backend/cng/hash.rs +++ b/openpgp/src/crypto/backend/cng/hash.rs @@ -1,4 +1,4 @@ -use core::convert::TryFrom; +use core::convert::{TryFrom, TryInto}; use std::sync::Mutex; use crate::crypto::hash::Digest; @@ -22,6 +22,13 @@ impl Clone for Hash { } impl Digest for Hash { + fn algo(&self) -> HashAlgorithm { + self.0.lock().expect("Mutex not to be poisoned") + .hash_algorithm().expect("CNG to not fail internally") + .try_into() + .expect("We created the object, algo is representable") + } + fn digest_size(&self) -> usize { self.0.lock().expect("Mutex not to be poisoned") .hash_size().expect("CNG to not fail internally") @@ -69,6 +76,22 @@ impl TryFrom<HashAlgorithm> for cng::HashAlgorithmId { } } +impl TryFrom<cng::HashAlgorithmId> for HashAlgorithm { + type Error = Error; + + fn try_from(value: cng::HashAlgorithmId) -> std::result::Result<Self, Self::Error> { + Ok(match value { + cng::HashAlgorithmId::Sha1 => HashAlgorithm::SHA1, + cng::HashAlgorithmId::Sha256 => HashAlgorithm::SHA256, + cng::HashAlgorithmId::Sha384 => HashAlgorithm::SHA384, + cng::HashAlgorithmId::Sha512 => HashAlgorithm::SHA512, + cng::HashAlgorithmId::Md5 => HashAlgorithm::MD5, + algo => Err(Error::InvalidArgument( + format!("Algorithm {:?} not representable", algo)))?, + }) + } +} + impl HashAlgorithm { /// Whether Sequoia supports this algorithm. pub fn is_supported(self) -> bool { diff --git a/openpgp/src/crypto/backend/nettle/hash.rs b/openpgp/src/crypto/backend/nettle/hash.rs index acfdf3a8..aa05a9e4 100644 --- a/openpgp/src/crypto/backend/nettle/hash.rs +++ b/openpgp/src/crypto/backend/nettle/hash.rs @@ -3,8 +3,12 @@ use crate::{Error, Result}; use crate::types::{HashAlgorithm}; macro_rules! impl_digest_for { - ($t: path) => { + ($t: path, $algo: ident) => { impl Digest for $t { + fn algo(&self) -> crate::types::HashAlgorithm { + crate::types::HashAlgorithm::$algo + } + fn digest_size(&self) -> usize { nettle::hash::Hash::digest_size(self) } @@ -21,13 +25,13 @@ macro_rules! impl_digest_for { } } -impl_digest_for!(nettle::hash::Sha224); -impl_digest_for!(nettle::hash::Sha256); -impl_digest_for!(nettle::hash::Sha384); -impl_digest_for!(nettle::hash::Sha512); -impl_digest_for!(nettle::hash::insecure_do_not_use::Sha1); -impl_digest_for!(nettle::hash::insecure_do_not_use::Md5); -impl_digest_for!(nettle::hash::insecure_do_not_use::Ripemd160); +impl_digest_for!(nettle::hash::Sha224, SHA224); +impl_digest_for!(nettle::hash::Sha256, SHA256); +impl_digest_for!(nettle::hash::Sha384, SHA384); +impl_digest_for!(nettle::hash::Sha512, SHA512); +impl_digest_for!(nettle::hash::insecure_do_not_use::Sha1, SHA1); +impl_digest_for!(nettle::hash::insecure_do_not_use::Md5, MD5); +impl_digest_for!(nettle::hash::insecure_do_not_use::Ripemd160, RipeMD); impl HashAlgorithm { /// Whether Sequoia supports this algorithm. diff --git a/openpgp/src/crypto/backend/sha1cd.rs b/openpgp/src/crypto/backend/sha1cd.rs index 2e00652d..7daf8ac1 100644 --- a/openpgp/src/crypto/backend/sha1cd.rs +++ b/openpgp/src/crypto/backend/sha1cd.rs @@ -10,6 +10,10 @@ pub(crate) fn build() -> sha1collisiondetection::Sha1CD { } impl Digest for sha1collisiondetection::Sha1CD { + fn algo(&self) -> crate::types::HashAlgorithm { + crate::types::HashAlgorithm::SHA1 + } + fn digest_size(&self) -> usize { 20 } diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs index 8f594e99..410d9eae 100644 --- a/openpgp/src/crypto/hash.rs +++ b/openpgp/src/crypto/hash.rs @@ -31,7 +31,10 @@ use std::io::{self, Write}; const DUMP_HASHED_VALUES: Option<&str> = None; /// Hasher capable of calculating a digest for the input byte stream. -pub(crate) trait Digest: DynClone + Send + Sync { +pub(crate) trait Digest: DynClone + Send + Sync { + /// Returns the algorithm. + fn algo(&self) -> HashAlgorithm; + /// Size of the digest in bytes fn digest_size(&self) -> usize; @@ -209,6 +212,10 @@ impl Drop for HashDumper { } impl Digest for HashDumper { + fn algo(&self) -> HashAlgorithm { + self.hasher.algo() + } + fn digest_size(&self) -> usize { self.hasher.digest_size() } |