Skip to content

Commit a256a7e

Browse files
committed
Migrate to universal-hash 0.5
This commit just switches to the new traits, and pretends that the ideal number of parallel blocks is 1 (i.e. no faster than before).
1 parent 30902e3 commit a256a7e

File tree

21 files changed

+226
-153
lines changed

21 files changed

+226
-153
lines changed

Cargo.lock

Lines changed: 14 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ members = [
55
"polyval"
66
]
77
resolver = "2"
8+
9+
[patch.crates-io]
10+
universal-hash = { git = "https://github.com/RustCrypto/traits", rev = "74ce6e7a9ab1243f574b6c37e747a6e54c01f376" }

ghash/benches/ghash.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate test;
44

55
use ghash::{
6-
universal_hash::{NewUniversalHash, UniversalHash},
6+
universal_hash::{KeyInit, UniversalHash},
77
GHash,
88
};
99
use test::Bencher;

ghash/src/lib.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@
3333
pub use polyval::universal_hash;
3434

3535
use polyval::Polyval;
36-
use universal_hash::{consts::U16, NewUniversalHash, UniversalHash};
36+
use universal_hash::{
37+
consts::U16,
38+
crypto_common::{BlockSizeUser, KeySizeUser, ParBlocksSizeUser},
39+
KeyInit, UhfBackend, UniversalHash,
40+
};
3741

3842
#[cfg(feature = "zeroize")]
3943
use zeroize::Zeroize;
@@ -45,7 +49,7 @@ pub type Key = universal_hash::Key<GHash>;
4549
pub type Block = universal_hash::Block<GHash>;
4650

4751
/// GHASH tags (16-bytes)
48-
pub type Tag = universal_hash::Output<GHash>;
52+
pub type Tag = universal_hash::Block<GHash>;
4953

5054
/// **GHASH**: universal hash over GF(2^128) used by AES-GCM.
5155
///
@@ -54,9 +58,11 @@ pub type Tag = universal_hash::Output<GHash>;
5458
#[derive(Clone)]
5559
pub struct GHash(Polyval);
5660

57-
impl NewUniversalHash for GHash {
61+
impl KeySizeUser for GHash {
5862
type KeySize = U16;
63+
}
5964

65+
impl KeyInit for GHash {
6066
/// Initialize GHASH with the given `H` field element
6167
#[inline]
6268
fn new(h: &Key) -> Self {
@@ -79,29 +85,36 @@ impl NewUniversalHash for GHash {
7985
}
8086
}
8187

82-
impl UniversalHash for GHash {
88+
impl BlockSizeUser for GHash {
8389
type BlockSize = U16;
90+
}
8491

85-
/// Input a field element `X` to be authenticated
86-
#[inline]
87-
fn update(&mut self, x: &Block) {
92+
impl ParBlocksSizeUser for GHash {
93+
type ParBlocksSize = U16;
94+
}
95+
96+
impl UhfBackend for GHash {
97+
fn proc_block(&mut self, x: &Block) {
8898
let mut x = *x;
8999
x.reverse();
90-
self.0.update(&x);
100+
self.0.proc_block(&x);
91101
}
102+
}
92103

93-
/// Reset internal state
94-
#[inline]
95-
fn reset(&mut self) {
96-
self.0.reset();
104+
impl UniversalHash for GHash {
105+
fn update_with_backend(
106+
&mut self,
107+
f: impl universal_hash::UhfClosure<BlockSize = Self::BlockSize>,
108+
) {
109+
f.call(self);
97110
}
98111

99112
/// Get GHASH output
100113
#[inline]
101114
fn finalize(self) -> Tag {
102-
let mut output = self.0.finalize().into_bytes();
115+
let mut output = self.0.finalize();
103116
output.reverse();
104-
Tag::new(output)
117+
output.into()
105118
}
106119
}
107120

ghash/tests/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use ghash::{
2-
universal_hash::{NewUniversalHash, UniversalHash},
2+
universal_hash::{KeyInit, UniversalHash},
33
GHash,
44
};
55
use hex_literal::hex;
@@ -19,9 +19,8 @@ const GHASH_RESULT: [u8; 16] = hex!("bd9b3997046731fb96251b91f9c99d7a");
1919
#[test]
2020
fn ghash_test_vector() {
2121
let mut ghash = GHash::new(&H.into());
22-
ghash.update(&X_1.into());
23-
ghash.update(&X_2.into());
22+
ghash.update(&[X_1.into(), X_2.into()]);
2423

2524
let result = ghash.finalize();
26-
assert_eq!(&GHASH_RESULT[..], result.into_bytes().as_slice());
25+
assert_eq!(&GHASH_RESULT[..], result.as_slice());
2726
}

poly1305/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ edition = "2021"
1414

1515
[dependencies]
1616
opaque-debug = "0.3"
17-
universal-hash = { version = "0.4", default-features = false }
17+
universal-hash = { version = "0.5", default-features = false }
1818
zeroize = { version = "1", optional = true, default-features = false }
1919

2020
[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dependencies]

poly1305/benches/poly1305.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
extern crate test;
44

55
use poly1305::{
6-
universal_hash::{NewUniversalHash, UniversalHash},
6+
universal_hash::{KeyInit, UniversalHash},
77
Poly1305,
88
};
99
use test::Bencher;

poly1305/src/backend/autodetect.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ impl State {
3535
Self { inner, token }
3636
}
3737

38-
/// Reset internal state
39-
#[inline]
40-
pub(crate) fn reset(&mut self) {
41-
if self.token.get() {
42-
unsafe { (*self.inner.avx2).reset() }
43-
} else {
44-
unsafe { (*self.inner.soft).reset() }
45-
}
46-
}
47-
4838
/// Compute a Poly1305 block
4939
#[inline]
5040
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {

poly1305/src/backend/avx2.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,6 @@ impl State {
6060
}
6161
}
6262

63-
/// Reset internal state
64-
pub(crate) fn reset(&mut self) {
65-
self.initialized = None;
66-
self.num_cached_blocks = 0;
67-
}
68-
6963
/// Compute a Poly1305 block
7064
#[target_feature(enable = "avx2")]
7165
pub(crate) unsafe fn compute_block(&mut self, block: &Block, partial: bool) {
@@ -152,6 +146,6 @@ impl State {
152146
};
153147
tag_int.write(tag.as_mut_slice());
154148

155-
Tag::new(tag)
149+
tag.into()
156150
}
157151
}

poly1305/src/backend/soft.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ impl State {
4141
poly
4242
}
4343

44-
/// Reset internal state
45-
pub(crate) fn reset(&mut self) {
46-
self.h = Default::default();
47-
}
48-
4944
/// Compute a Poly1305 block
5045
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {
5146
let hibit = if partial { 0 } else { 1 << 24 };
@@ -227,7 +222,7 @@ impl State {
227222
tag[8..12].copy_from_slice(&h2.to_le_bytes());
228223
tag[12..16].copy_from_slice(&h3.to_le_bytes());
229224

230-
Tag::new(tag)
225+
tag.into()
231226
}
232227
}
233228

0 commit comments

Comments
 (0)