Skip to content

Commit 81912e6

Browse files
committed
kmac: Use the new chake const-generic API
1 parent 291f4b8 commit 81912e6

2 files changed

Lines changed: 46 additions & 27 deletions

File tree

Cargo.lock

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

kmac/src/lib.rs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,54 @@
1313
mod encoding;
1414

1515
use crate::encoding::{left_encode, right_encode};
16-
use cshake::{CShake, CShakeReader};
17-
use digest::block_buffer::BlockSizes;
16+
use cshake::CShakeReader;
1817
use digest::consts::{U136, U168};
1918
pub use digest::{self, ExtendableOutput, FixedOutput, KeyInit, Mac, XofReader};
2019
use digest::{InvalidLength, MacMarker, Output, OutputSizeUser, Update};
2120

2221
mod sealed {
22+
use cshake::{CShake128, CShake256, CShakeReader};
2323
use digest::array::ArraySize;
2424
use digest::consts::{U32, U64, U136, U168};
25+
use digest::{ExtendableOutput, Update, XofReader};
2526

26-
pub trait KmacParams {
27+
pub trait KmacParams: ArraySize {
2728
type OutputSize: ArraySize;
29+
type CShake: Clone + Update + ExtendableOutput<Reader = Self::Reader>;
30+
type Reader: XofReader;
31+
32+
fn new_cshake(function_name: &[u8], customization: &[u8]) -> Self::CShake;
2833
}
2934

3035
impl KmacParams for U168 {
3136
type OutputSize = U32;
37+
type CShake = CShake128;
38+
type Reader = CShakeReader<168>;
39+
40+
#[inline]
41+
fn new_cshake(function_name: &[u8], customization: &[u8]) -> Self::CShake {
42+
CShake128::new_with_function_name(function_name, customization)
43+
}
3244
}
3345

3446
impl KmacParams for U136 {
3547
type OutputSize = U64;
48+
type CShake = CShake256;
49+
type Reader = CShakeReader<136>;
50+
51+
#[inline]
52+
fn new_cshake(function_name: &[u8], customization: &[u8]) -> Self::CShake {
53+
CShake256::new_with_function_name(function_name, customization)
54+
}
3655
}
3756
}
3857

3958
/// KMAC implementation as per Section 4 of [NIST SP 800-185].
4059
///
4160
/// [NIST SP 800-185]: https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf
4261
#[derive(Clone)]
43-
pub struct Kmac<Rate: BlockSizes + sealed::KmacParams> {
44-
cshake: CShake<Rate>,
62+
pub struct Kmac<Rate: sealed::KmacParams> {
63+
cshake: <Rate as sealed::KmacParams>::CShake,
4564
}
4665

4766
/// KMAC128: KMAC with 128-bit security strength, as defined in Section 4 of
@@ -89,24 +108,24 @@ pub type Kmac256 = Kmac<U136>;
89108
/// KMACXOF128 reader, returned by calling [`ExtendableOutput::finalize_xof`] on [`Kmac128`].
90109
///
91110
/// Implements [`XofReader`] to produce an arbitrary-length output stream (KMACXOF128).
92-
pub type Kmac128Reader = CShakeReader<U168>;
111+
pub type Kmac128Reader = CShakeReader<168>;
93112

94113
/// KMACXOF256 reader, returned by calling [`ExtendableOutput::finalize_xof`] on [`Kmac256`].
95114
///
96115
/// Implements [`XofReader`] to produce an arbitrary-length output stream (KMACXOF256).
97-
pub type Kmac256Reader = CShakeReader<U136>;
116+
pub type Kmac256Reader = CShakeReader<136>;
98117

99-
impl<Rate: BlockSizes + sealed::KmacParams> MacMarker for Kmac<Rate> {}
118+
impl<Rate: sealed::KmacParams> MacMarker for Kmac<Rate> {}
100119

101-
impl<Rate: BlockSizes + sealed::KmacParams> OutputSizeUser for Kmac<Rate> {
120+
impl<Rate: sealed::KmacParams> OutputSizeUser for Kmac<Rate> {
102121
type OutputSize = <Rate as sealed::KmacParams>::OutputSize;
103122
}
104123

105-
impl<Rate: BlockSizes + sealed::KmacParams> digest::common::KeySizeUser for Kmac<Rate> {
124+
impl<Rate: sealed::KmacParams> digest::common::KeySizeUser for Kmac<Rate> {
106125
type KeySize = Rate;
107126
}
108127

109-
impl<Rate: BlockSizes + sealed::KmacParams> KeyInit for Kmac<Rate> {
128+
impl<Rate: sealed::KmacParams> KeyInit for Kmac<Rate> {
110129
#[inline]
111130
fn new(key: &digest::Key<Self>) -> Self {
112131
Self::new_customization_inner(key.as_slice(), &[])
@@ -118,22 +137,22 @@ impl<Rate: BlockSizes + sealed::KmacParams> KeyInit for Kmac<Rate> {
118137
}
119138
}
120139

121-
impl<Rate: BlockSizes + sealed::KmacParams> Update for Kmac<Rate> {
140+
impl<Rate: sealed::KmacParams> Update for Kmac<Rate> {
122141
#[inline(always)]
123142
fn update(&mut self, data: &[u8]) {
124143
self.cshake.update(data);
125144
}
126145
}
127146

128-
impl<Rate: BlockSizes + sealed::KmacParams> FixedOutput for Kmac<Rate> {
147+
impl<Rate: sealed::KmacParams> FixedOutput for Kmac<Rate> {
129148
#[inline(always)]
130149
fn finalize_into(self, out: &mut Output<Self>) {
131150
self.finalize_fixed_inner(out.as_mut_slice());
132151
}
133152
}
134153

135-
impl<Rate: BlockSizes + sealed::KmacParams> ExtendableOutput for Kmac<Rate> {
136-
type Reader = CShakeReader<Rate>;
154+
impl<Rate: sealed::KmacParams> ExtendableOutput for Kmac<Rate> {
155+
type Reader = <Rate as sealed::KmacParams>::Reader;
137156

138157
// Finalize as KMACXOF, a variable-length (extendable) output stream, as defined in
139158
// Section 4.3.1 (KMAC with Arbitrary-Length Output) of [NIST SP 800-185].
@@ -143,7 +162,7 @@ impl<Rate: BlockSizes + sealed::KmacParams> ExtendableOutput for Kmac<Rate> {
143162
}
144163
}
145164

146-
impl<Rate: BlockSizes + sealed::KmacParams> Kmac<Rate> {
165+
impl<Rate: sealed::KmacParams> Kmac<Rate> {
147166
/// Create a new KMAC with the given key and customisation.
148167
///
149168
/// Section 4.2 of [NIST SP 800-185] specifies that KMAC takes both a key (K) and an
@@ -180,7 +199,7 @@ impl<Rate: BlockSizes + sealed::KmacParams> Kmac<Rate> {
180199

181200
#[inline(always)]
182201
fn new_customization_inner(key: &[u8], customisation: &[u8]) -> Self {
183-
let mut cshake = CShake::<Rate>::new_with_function_name(b"KMAC", customisation);
202+
let mut cshake = <Rate as sealed::KmacParams>::new_cshake(b"KMAC", customisation);
184203
let block_size = Rate::USIZE;
185204
let mut encode_buffer = [0u8; 9];
186205

@@ -226,7 +245,7 @@ impl<Rate: BlockSizes + sealed::KmacParams> Kmac<Rate> {
226245

227246
/// Finalizes the KMAC for extendable output (XOF).
228247
#[inline(always)]
229-
fn finalize_xof_inner(mut self) -> CShakeReader<Rate> {
248+
fn finalize_xof_inner(mut self) -> <Rate as sealed::KmacParams>::Reader {
230249
// right_encode(0), as L = 0 for extendable output
231250
let mut encode_buffer = [0u8; 9];
232251
let re = right_encode(0, &mut encode_buffer);

0 commit comments

Comments
 (0)