Skip to content

Commit eb30dc0

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 eb30dc0

21 files changed

Lines changed: 305 additions & 175 deletions

File tree

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.

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: 44 additions & 16 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, UhfClosure, 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,51 @@ impl NewUniversalHash for GHash {
7985
}
8086
}
8187

82-
impl UniversalHash for GHash {
83-
type BlockSize = U16;
88+
struct GHashBackend<'b, B: UhfBackend>(&'b mut B);
8489

85-
/// Input a field element `X` to be authenticated
86-
#[inline]
87-
fn update(&mut self, x: &Block) {
88-
let mut x = *x;
90+
impl<'b, B: UhfBackend> BlockSizeUser for GHashBackend<'b, B> {
91+
type BlockSize = B::BlockSize;
92+
}
93+
94+
impl<'b, B: UhfBackend> ParBlocksSizeUser for GHashBackend<'b, B> {
95+
type ParBlocksSize = B::ParBlocksSize;
96+
}
97+
98+
impl<'b, B: UhfBackend> UhfBackend for GHashBackend<'b, B> {
99+
fn proc_block(&mut self, x: &universal_hash::Block<B>) {
100+
let mut x = x.clone();
89101
x.reverse();
90-
self.0.update(&x);
102+
self.0.proc_block(&x);
91103
}
104+
}
92105

93-
/// Reset internal state
94-
#[inline]
95-
fn reset(&mut self) {
96-
self.0.reset();
106+
impl BlockSizeUser for GHash {
107+
type BlockSize = U16;
108+
}
109+
110+
impl UniversalHash for GHash {
111+
fn update_with_backend(&mut self, f: impl UhfClosure<BlockSize = Self::BlockSize>) {
112+
struct GHashClosure<C: UhfClosure>(C);
113+
114+
impl<C: UhfClosure> BlockSizeUser for GHashClosure<C> {
115+
type BlockSize = C::BlockSize;
116+
}
117+
118+
impl<C: UhfClosure> UhfClosure for GHashClosure<C> {
119+
fn call<B: UhfBackend<BlockSize = Self::BlockSize>>(self, backend: &mut B) {
120+
self.0.call(&mut GHashBackend(backend));
121+
}
122+
}
123+
124+
self.0.update_with_backend(GHashClosure(f));
97125
}
98126

99127
/// Get GHASH output
100128
#[inline]
101129
fn finalize(self) -> Tag {
102-
let mut output = self.0.finalize().into_bytes();
130+
let mut output = self.0.finalize();
103131
output.reverse();
104-
Tag::new(output)
132+
output
105133
}
106134
}
107135

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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@ 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]
2121
cpufeatures = "0.2"
2222

23+
[dependencies.universal-hash]
24+
git = "https://github.com/RustCrypto/traits"
25+
rev = "74ce6e7a9ab1243f574b6c37e747a6e54c01f376"
26+
default-features = false
27+
2328
[dev-dependencies]
2429
hex-literal = "0.3"
2530

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: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Autodetection support for AVX2 CPU intrinsics on x86 CPUs, with fallback
22
//! to the "soft" backend when it's unavailable.
33
4+
use universal_hash::{consts::U16, crypto_common::BlockSizeUser, UniversalHash};
5+
46
use crate::{backend, Block, Key, Tag};
57
use core::mem::ManuallyDrop;
68

@@ -16,6 +18,10 @@ union Inner {
1618
soft: ManuallyDrop<backend::soft::State>,
1719
}
1820

21+
impl BlockSizeUser for State {
22+
type BlockSize = U16;
23+
}
24+
1925
impl State {
2026
/// Initialize Poly1305 [`State`] with the given key
2127
#[inline]
@@ -35,33 +41,36 @@ impl State {
3541
Self { inner, token }
3642
}
3743

38-
/// Reset internal state
44+
/// Compute a Poly1305 block
3945
#[inline]
40-
pub(crate) fn reset(&mut self) {
46+
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {
4147
if self.token.get() {
42-
unsafe { (*self.inner.avx2).reset() }
48+
unsafe { (*self.inner.avx2).compute_block(block, partial) }
4349
} else {
44-
unsafe { (*self.inner.soft).reset() }
50+
unsafe { (*self.inner.soft).compute_block(block, partial) }
4551
}
4652
}
53+
}
4754

48-
/// Compute a Poly1305 block
49-
#[inline]
50-
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {
55+
impl UniversalHash for State {
56+
fn update_with_backend(
57+
&mut self,
58+
f: impl universal_hash::UhfClosure<BlockSize = Self::BlockSize>,
59+
) {
5160
if self.token.get() {
52-
unsafe { (*self.inner.avx2).compute_block(block, partial) }
61+
unsafe { f.call(&mut *self.inner.avx2) }
5362
} else {
54-
unsafe { (*self.inner.soft).compute_block(block, partial) }
63+
unsafe { f.call(&mut *self.inner.soft) }
5564
}
5665
}
5766

5867
/// Finalize output producing a [`Tag`]
5968
#[inline]
60-
pub(crate) fn finalize(&mut self) -> Tag {
69+
fn finalize(mut self) -> Tag {
6170
if self.token.get() {
6271
unsafe { (*self.inner.avx2).finalize() }
6372
} else {
64-
unsafe { (*self.inner.soft).finalize() }
73+
unsafe { (*self.inner.soft).finalize_mut() }
6574
}
6675
}
6776
}

poly1305/src/backend/avx2.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
// optimisations provided by Bhattacharyya and Sarkar. The latter require the message
1616
// length to be known, which is incompatible with the streaming API of UniversalHash.
1717

18-
use universal_hash::generic_array::GenericArray;
18+
use universal_hash::{
19+
consts::{U1, U16},
20+
crypto_common::{BlockSizeUser, ParBlocksSizeUser},
21+
generic_array::GenericArray,
22+
UhfBackend,
23+
};
1924

2025
use crate::{Block, Key, Tag};
2126

@@ -60,12 +65,6 @@ impl State {
6065
}
6166
}
6267

63-
/// Reset internal state
64-
pub(crate) fn reset(&mut self) {
65-
self.initialized = None;
66-
self.num_cached_blocks = 0;
67-
}
68-
6968
/// Compute a Poly1305 block
7069
#[target_feature(enable = "avx2")]
7170
pub(crate) unsafe fn compute_block(&mut self, block: &Block, partial: bool) {
@@ -152,6 +151,20 @@ impl State {
152151
};
153152
tag_int.write(tag.as_mut_slice());
154153

155-
Tag::new(tag)
154+
tag
155+
}
156+
}
157+
158+
impl BlockSizeUser for State {
159+
type BlockSize = U16;
160+
}
161+
162+
impl ParBlocksSizeUser for State {
163+
type ParBlocksSize = U1;
164+
}
165+
166+
impl UhfBackend for State {
167+
fn proc_block(&mut self, block: &Block) {
168+
unsafe { self.compute_block(block, false) };
156169
}
157170
}

poly1305/src/backend/soft.rs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
// ...and was originally a port of Andrew Moons poly1305-donna
1313
// https://github.com/floodyberry/poly1305-donna
1414

15+
use universal_hash::{
16+
consts::{U1, U16},
17+
crypto_common::{BlockSizeUser, ParBlocksSizeUser},
18+
UhfBackend, UniversalHash,
19+
};
20+
1521
use crate::{Block, Key, Tag};
1622

1723
#[derive(Clone, Default)]
@@ -41,11 +47,6 @@ impl State {
4147
poly
4248
}
4349

44-
/// Reset internal state
45-
pub(crate) fn reset(&mut self) {
46-
self.h = Default::default();
47-
}
48-
4950
/// Compute a Poly1305 block
5051
pub(crate) fn compute_block(&mut self, block: &Block, partial: bool) {
5152
let hibit = if partial { 0 } else { 1 << 24 };
@@ -139,7 +140,7 @@ impl State {
139140
}
140141

141142
/// Finalize output producing a [`Tag`]
142-
pub(crate) fn finalize(&mut self) -> Tag {
143+
pub(crate) fn finalize_mut(&mut self) -> Tag {
143144
// fully carry h
144145
let mut h0 = self.h[0];
145146
let mut h1 = self.h[1];
@@ -227,7 +228,7 @@ impl State {
227228
tag[8..12].copy_from_slice(&h2.to_le_bytes());
228229
tag[12..16].copy_from_slice(&h3.to_le_bytes());
229230

230-
Tag::new(tag)
231+
tag
231232
}
232233
}
233234

@@ -240,3 +241,31 @@ impl Drop for State {
240241
self.pad.zeroize();
241242
}
242243
}
244+
245+
impl BlockSizeUser for State {
246+
type BlockSize = U16;
247+
}
248+
249+
impl ParBlocksSizeUser for State {
250+
type ParBlocksSize = U1;
251+
}
252+
253+
impl UhfBackend for State {
254+
fn proc_block(&mut self, block: &Block) {
255+
self.compute_block(block, false);
256+
}
257+
}
258+
259+
impl UniversalHash for State {
260+
fn update_with_backend(
261+
&mut self,
262+
f: impl universal_hash::UhfClosure<BlockSize = Self::BlockSize>,
263+
) {
264+
f.call(self);
265+
}
266+
267+
/// Finalize output producing a [`Tag`]
268+
fn finalize(mut self) -> Tag {
269+
self.finalize_mut()
270+
}
271+
}

0 commit comments

Comments
 (0)