summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2024-03-16 18:59:52 +0100
committerJustus Winter <justus@sequoia-pgp.org>2024-03-16 18:59:52 +0100
commitde54f85b0f1aa9ab5930912feac829c8392c941c (patch)
treec5178e3bd711c7f0e076ed1761bd913750429d33
parentaf16b457ae822cf7a9b5d42f9ee28a3c8dc36383 (diff)
openpgp: Avoid heap allocation when hashing keys.
- Notably, this is done during certificate canonicalization. This is expensive as-is, let's keep the allocator out of it.
-rw-r--r--openpgp/src/crypto/hash.rs33
1 files changed, 17 insertions, 16 deletions
diff --git a/openpgp/src/crypto/hash.rs b/openpgp/src/crypto/hash.rs
index 79a31f8a..c3742664 100644
--- a/openpgp/src/crypto/hash.rs
+++ b/openpgp/src/crypto/hash.rs
@@ -381,27 +381,28 @@ impl<P, R> Hash for Key4<P, R>
// include the tag (1 byte) or the length (2 bytes).
let len = (9 - 3) + self.mpis().serialized_len() as u16;
- let mut header: Vec<u8> = Vec::with_capacity(9);
+ let mut header = [
+ // Tag. Note: we use this whether
+ 0x99u8,
- // Tag. Note: we use this whether
- header.push(0x99);
+ // Length (2 bytes, big endian). Fixup later.
+ 0, 0,
- // Length (2 bytes, big endian).
- header.extend_from_slice(&len.to_be_bytes());
+ // Version.
+ 4,
- // Version.
- header.push(4);
-
- // Creation time.
- let creation_time: u32 =
- Timestamp::try_from(self.creation_time())
- .unwrap_or_else(|_| Timestamp::from(0))
- .into();
- header.extend_from_slice(&creation_time.to_be_bytes());
+ // Creation time. Fixup later.
+ 0, 0, 0, 0,
- // Algorithm.
- header.push(self.pk_algo().into());
+ // Algorithm.
+ self.pk_algo().into(),
+ ];
+ // Fixup length and creation time and hash the header.
+ header[1..3].copy_from_slice(&len.to_be_bytes());
+ header[4..8].copy_from_slice(&u32::from(
+ Timestamp::try_from(self.creation_time())
+ .unwrap_or_else(|_| Timestamp::from(0))).to_be_bytes());
hash.update(&header[..]);
// MPIs.