Skip to content

Commit fad91b8

Browse files
tarcieristr4d
andauthoredJul 17, 2022
Migrate to universal-hash v0.5.0-pre (#155)
Migrate to `universal-hash` v0.5.0-pre New prerelease with breaking API changes that supports parallel block processing: RustCrypto/traits#1051 Co-authored-by: Jack Grigg <[email protected]>
1 parent 4c9581a commit fad91b8

File tree

25 files changed

+367
-217
lines changed

25 files changed

+367
-217
lines changed
 

‎.github/workflows/poly1305.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -153,25 +153,16 @@ jobs:
153153
strategy:
154154
matrix:
155155
include:
156-
# ARM64
157156
- target: aarch64-unknown-linux-gnu
158-
rust: 1.56.1 # MSRV
159-
- target: aarch64-unknown-linux-gnu
160-
rust: stable
161-
162-
# PPC32
163157
- target: powerpc-unknown-linux-gnu
164-
rust: 1.56.1 # MSRV
165-
- target: powerpc-unknown-linux-gnu
166-
rust: stable
167158

168159
runs-on: ubuntu-latest
169160
steps:
170161
- uses: actions/checkout@v1
171162
- run: ${{ matrix.deps }}
172163
- uses: actions-rs/toolchain@v1
173164
with:
174-
toolchain: ${{ matrix.rust }}
165+
toolchain: stable
175166
target: ${{ matrix.target }}
176167
profile: minimal
177168
override: true

‎.github/workflows/polyval.yml

+1-10
Original file line numberDiff line numberDiff line change
@@ -154,25 +154,16 @@ jobs:
154154
strategy:
155155
matrix:
156156
include:
157-
# ARM64
158157
- target: aarch64-unknown-linux-gnu
159-
rust: 1.56.1 # MSRV
160-
- target: aarch64-unknown-linux-gnu
161-
rust: stable
162-
163-
# PPC32
164158
- target: powerpc-unknown-linux-gnu
165-
rust: 1.56.1 # MSRV
166-
- target: powerpc-unknown-linux-gnu
167-
rust: stable
168159

169160
runs-on: ubuntu-latest
170161
steps:
171162
- uses: actions/checkout@v1
172163
- run: ${{ matrix.deps }}
173164
- uses: actions-rs/toolchain@v1
174165
with:
175-
toolchain: ${{ matrix.rust }}
166+
toolchain: stable
176167
target: ${{ matrix.target }}
177168
profile: minimal
178169
override: true

‎Cargo.lock

+18-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎ghash/Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ghash"
3-
version = "0.4.4" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.5.0-pre" # Also update html_root_url in lib.rs when bumping this
44
authors = ["RustCrypto Developers"]
55
license = "Apache-2.0 OR MIT"
66
description = """
@@ -17,7 +17,7 @@ edition = "2021"
1717

1818
[dependencies]
1919
opaque-debug = "0.3"
20-
polyval = { version = "0.5.1", path = "../polyval" }
20+
polyval = { version = "=0.6.0-pre", path = "../polyval" }
2121

2222
# optional dependencies
2323
zeroize = { version = "1", optional = true, default-features = false }

‎ghash/benches/ghash.rs

+1-1
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

+45-18
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,18 @@
2525
#![no_std]
2626
#![doc(
2727
html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
28-
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg",
29-
html_root_url = "https://docs.rs/ghash/0.4.3"
28+
html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/8f1a9894/logo.svg"
3029
)]
3130
#![warn(missing_docs, rust_2018_idioms)]
3231

3332
pub use polyval::universal_hash;
3433

3534
use polyval::Polyval;
36-
use universal_hash::{consts::U16, NewUniversalHash, UniversalHash};
35+
use universal_hash::{
36+
consts::U16,
37+
crypto_common::{BlockSizeUser, KeySizeUser, ParBlocksSizeUser},
38+
KeyInit, UhfBackend, UhfClosure, UniversalHash,
39+
};
3740

3841
#[cfg(feature = "zeroize")]
3942
use zeroize::Zeroize;
@@ -45,7 +48,7 @@ pub type Key = universal_hash::Key<GHash>;
4548
pub type Block = universal_hash::Block<GHash>;
4649

4750
/// GHASH tags (16-bytes)
48-
pub type Tag = universal_hash::Output<GHash>;
51+
pub type Tag = universal_hash::Block<GHash>;
4952

5053
/// **GHASH**: universal hash over GF(2^128) used by AES-GCM.
5154
///
@@ -54,9 +57,11 @@ pub type Tag = universal_hash::Output<GHash>;
5457
#[derive(Clone)]
5558
pub struct GHash(Polyval);
5659

57-
impl NewUniversalHash for GHash {
60+
impl KeySizeUser for GHash {
5861
type KeySize = U16;
62+
}
5963

64+
impl KeyInit for GHash {
6065
/// Initialize GHASH with the given `H` field element
6166
#[inline]
6267
fn new(h: &Key) -> Self {
@@ -79,29 +84,51 @@ impl NewUniversalHash for GHash {
7984
}
8085
}
8186

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

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

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

99126
/// Get GHASH output
100127
#[inline]
101128
fn finalize(self) -> Tag {
102-
let mut output = self.0.finalize().into_bytes();
129+
let mut output = self.0.finalize();
103130
output.reverse();
104-
Tag::new(output)
131+
output
105132
}
106133
}
107134

‎ghash/tests/lib.rs

+3-4
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "poly1305"
3-
version = "0.7.2" # Also update html_root_url in lib.rs when bumping this
3+
version = "0.8.0-pre" # Also update html_root_url in lib.rs when bumping this
44
authors = ["RustCrypto Developers"]
55
license = "Apache-2.0 OR MIT"
66
description = "The Poly1305 universal hash function and message authentication code"
@@ -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.0-pre", 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

+1-1
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

+20-11
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
}

0 commit comments

Comments
 (0)
Please sign in to comment.