Commit
Author: Dirkjan Ochtman [dirkjan@ochtman.nl]
Hash: 01a48f62db6dc40ab94e978e8007d4c8407f7dd4
Timestamp: Tue, 13 Dec 2022 10:40:45 +0000 (1 year ago)

+13 -9 +/-3 browse
Avoid unnecessary allocation for hash output
1diff --git a/src/common/crypto.rs b/src/common/crypto.rs
2index 0879594..e37d859 100644
3--- a/src/common/crypto.rs
4+++ b/src/common/crypto.rs
5 @@ -165,7 +165,7 @@ impl VerifyingKey for RsaPublicKey {
6 self.inner
7 .verify(
8 PaddingScheme::new_pkcs1v15_sign::<Sha256>(),
9- &hash,
10+ hash.as_ref(),
11 signature,
12 )
13 .map_err(|_| Error::FailedVerification)
14 @@ -173,7 +173,11 @@ impl VerifyingKey for RsaPublicKey {
15 Algorithm::RsaSha1 => {
16 let hash = canonicalization.hash_headers::<Sha1>(headers);
17 self.inner
18- .verify(PaddingScheme::new_pkcs1v15_sign::<Sha1>(), &hash, signature)
19+ .verify(
20+ PaddingScheme::new_pkcs1v15_sign::<Sha1>(),
21+ hash.as_ref(),
22+ signature,
23+ )
24 .map_err(|_| Error::FailedVerification)
25 }
26 Algorithm::Ed25519Sha256 => Err(Error::IncompatibleAlgorithms),
27 @@ -200,7 +204,7 @@ impl VerifyingKey for Ed25519PublicKey {
28 let hash = canonicalization.hash_headers::<Sha256>(headers);
29 self.inner
30 .verify_strict(
31- &hash,
32+ hash.as_ref(),
33 &ed25519_dalek::Signature::from_bytes(signature)
34 .map_err(|err| Error::CryptoError(err.to_string()))?,
35 )
36 diff --git a/src/common/message.rs b/src/common/message.rs
37index b7423c2..dd1ab6f 100644
38--- a/src/common/message.rs
39+++ b/src/common/message.rs
40 @@ -147,8 +147,8 @@ impl<'x> AuthenticatedMessage<'x> {
41 // Calculate body hashes
42 for (cb, ha, l, bh) in &mut message.body_hashes {
43 *bh = match ha {
44- HashAlgorithm::Sha256 => cb.hash_body::<Sha256>(message.body, *l),
45- HashAlgorithm::Sha1 => cb.hash_body::<Sha1>(message.body, *l),
46+ HashAlgorithm::Sha256 => cb.hash_body::<Sha256>(message.body, *l).as_ref().to_vec(),
47+ HashAlgorithm::Sha1 => cb.hash_body::<Sha1>(message.body, *l).as_ref().to_vec(),
48 };
49 }
50
51 diff --git a/src/dkim/canonicalize.rs b/src/dkim/canonicalize.rs
52index 49e0300..143fd6e 100644
53--- a/src/dkim/canonicalize.rs
54+++ b/src/dkim/canonicalize.rs
55 @@ -119,13 +119,13 @@ impl Canonicalization {
56 pub fn hash_headers<'x, T: Digest + Writer>(
57 &self,
58 headers: &mut dyn Iterator<Item = (&'x [u8], &'x [u8])>,
59- ) -> Vec<u8> {
60+ ) -> impl AsRef<[u8]> {
61 let mut hasher = T::new();
62 self.canonicalize_headers(headers, &mut hasher);
63- hasher.finalize().to_vec()
64+ hasher.finalize()
65 }
66
67- pub fn hash_body<T: Digest + Writer>(&self, body: &[u8], l: u64) -> Vec<u8> {
68+ pub fn hash_body<T: Digest + Writer>(&self, body: &[u8], l: u64) -> impl AsRef<[u8]> {
69 let mut hasher = T::new();
70 self.canonicalize_body(
71 if l == 0 || body.is_empty() {
72 @@ -135,7 +135,7 @@ impl Canonicalization {
73 },
74 &mut hasher,
75 );
76- hasher.finalize().to_vec()
77+ hasher.finalize()
78 }
79
80 pub fn serialize_name(&self, writer: &mut impl Writer) {