From 70dec8c78ab04207158a67aaf7ca21270af5d473 Mon Sep 17 00:00:00 2001 From: "Neal H. Walfield" Date: Fri, 12 Jan 2024 16:11:28 +0100 Subject: openpgp: Use ahash instead of xxhash. - According to the `ahash` `README`, it performs better than `xxhash`. - `ahash` is also used by some of our dependencies, `xxhash` isn't. - Fixes #1074. --- Cargo.lock | 41 ++++++++++++++++++++++++++++++++++------- openpgp/Cargo.toml | 2 +- openpgp/src/packet/container.rs | 16 ++++++++-------- openpgp/src/parse.rs | 13 +++++++------ 4 files changed, 50 insertions(+), 22 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f5c4b61d..b21c388d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -53,6 +53,19 @@ dependencies = [ "subtle", ] +[[package]] +name = "ahash" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01" +dependencies = [ + "cfg-if", + "getrandom", + "once_cell", + "version_check", + "zerocopy", +] + [[package]] name = "aho-corasick" version = "1.1.2" @@ -2601,6 +2614,7 @@ version = "1.17.0" dependencies = [ "aes", "aes-gcm", + "ahash", "anyhow", "base64", "block-padding", @@ -2655,7 +2669,6 @@ dependencies = [ "win-crypto-ng", "winapi", "x25519-dalek", - "xxhash-rust", ] [[package]] @@ -3377,18 +3390,32 @@ dependencies = [ "zeroize", ] -[[package]] -name = "xxhash-rust" -version = "0.8.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9828b178da53440fa9c766a3d2f73f7cf5d0ac1fe3980c1e5018d899fd19e07b" - [[package]] name = "z-base-32" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80a0d98613370af88e15bd2047702d7c78c8c6aba44403eb227c8ad706871f92" +[[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "zeroize" version = "1.7.0" diff --git a/openpgp/Cargo.toml b/openpgp/Cargo.toml index de75a852..847b5733 100644 --- a/openpgp/Cargo.toml +++ b/openpgp/Cargo.toml @@ -44,7 +44,7 @@ regex = "1" regex-syntax = "0.8" sha1collisiondetection = { version = "0.3.1", default-features = false, features = ["std"] } thiserror = "1.0.2" -xxhash-rust = { version = "0.8", features = ["xxh3"] } +ahash = { version = "0.8", features = ["std"] } # At least 0.10.55 is needed due `no-ocb` check: # https://github.com/sfackler/rust-openssl/blob/master/openssl/CHANGELOG.md diff --git a/openpgp/src/packet/container.rs b/openpgp/src/packet/container.rs index fd8c0f2f..fb713979 100644 --- a/openpgp/src/packet/container.rs +++ b/openpgp/src/packet/container.rs @@ -8,7 +8,7 @@ use std::hash::{Hash, Hasher}; use std::slice; use std::vec; -use xxhash_rust::xxh3::Xxh3; +use ahash::AHasher; use crate::{ Packet, @@ -287,8 +287,8 @@ impl Container { use Body::*; let mut h = Self::make_body_hash(); match &body { - Unprocessed(bytes) => h.update(bytes), - Processed(bytes) => h.update(bytes), + Unprocessed(bytes) => h.write(bytes), + Processed(bytes) => h.write(bytes), Structured(_) => (), } self.set_body_hash(h); @@ -299,7 +299,7 @@ impl Container { fn empty_body_digest() -> u64 { lazy_static::lazy_static!{ static ref DIGEST: u64 = { - Container::make_body_hash().digest() + Container::make_body_hash().finish() }; } @@ -308,14 +308,14 @@ impl Container { /// Creates a hash context for hashing the body. pub(crate) // For parse.rs - fn make_body_hash() -> Box { - Box::new(Xxh3::new()) + fn make_body_hash() -> Box { + Box::new(AHasher::default()) } /// Hashes content that has been streamed. pub(crate) // For parse.rs - fn set_body_hash(&mut self, h: Box) { - self.body_digest = h.digest(); + fn set_body_hash(&mut self, h: Box) { + self.body_digest = h.finish(); } pub(crate) diff --git a/openpgp/src/parse.rs b/openpgp/src/parse.rs index 4e18acee..510d1cfd 100644 --- a/openpgp/src/parse.rs +++ b/openpgp/src/parse.rs @@ -177,13 +177,14 @@ use std::io; use std::io::prelude::*; use std::convert::TryFrom; use std::cmp; +use std::hash::Hasher; use std::str; use std::mem; use std::fmt; use std::path::Path; use std::result::Result as StdResult; -use xxhash_rust::xxh3::Xxh3; +use ahash::AHasher; // Re-export buffered_reader. // @@ -3547,7 +3548,7 @@ pub struct PacketParser<'a> { /// We compute a hashsum over the body to implement comparison on /// containers that have been streamed. - body_hash: Option>, + body_hash: Option>, state: PacketParserState, } @@ -5140,7 +5141,7 @@ impl <'a> PacketParser<'a> { if !b.is_empty() { assert!(self.body_hash.is_some()); if let Some(h) = self.body_hash.as_mut() { - h.update(b); + h.write(b); } self.content_was_read = true; } @@ -5277,7 +5278,7 @@ impl<'a> BufferedReader for PacketParser<'a> { if let Some(mut body_hash) = self.body_hash.take() { let data = self.data_hard(amount) .expect("It is an error to consume more than data returns"); - body_hash.update(&data[..amount]); + body_hash.write(&data[..amount]); self.body_hash = Some(body_hash); self.content_was_read |= amount > 0; } else { @@ -5292,7 +5293,7 @@ impl<'a> BufferedReader for PacketParser<'a> { if let Some(mut body_hash) = self.body_hash.take() { let data = self.data(amount)?; amount = cmp::min(data.len(), amount); - body_hash.update(&data[..amount]); + body_hash.write(&data[..amount]); self.body_hash = Some(body_hash); self.content_was_read |= amount > 0; } else { @@ -5306,7 +5307,7 @@ impl<'a> BufferedReader for PacketParser<'a> { // This is awkward. Juggle mutable references around. if let Some(mut body_hash) = self.body_hash.take() { let data = self.data_hard(amount)?; - body_hash.update(&data[..amount]); + body_hash.write(&data[..amount]); self.body_hash = Some(body_hash); self.content_was_read |= amount > 0; } else { -- cgit v1.2.3