Skip to content

Commit d7807bf

Browse files
authored
digest: add buffering macros (#1799)
The new macros create buffered wrapper types and replace the old wrapper types (except `CtVariableCoreWrapper`).
1 parent 3d8039a commit d7807bf

File tree

15 files changed

+1378
-604
lines changed

15 files changed

+1378
-604
lines changed

digest/CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,28 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## 0.11.0 (UNRELEASED)
99
### Added
10-
- `CustomizedInit` trait ([#1334]).
10+
- `CustomizedInit` trait ([#1334])
11+
- `buffer_fixed`, `buffer_ct_variable`, `buffer_rt_variable`, and `buffer_xof` macros ([#1799])
12+
- `CollisionResistance` trait ([#1820])
1113

1214
### Changed
1315
- `crypto-common` dependency bumped to v0.2 ([#1173])
1416
- Edition changed to 2024 and MSRV bumped to 1.85 ([#1759])
17+
- `CtVariableCoreWrapper` renamed to `CtOutWrapper` ([#1799])
18+
- Removed the OID type parameter from `CtOutWrapper` ([#1799])
1519

1620
### Removed
1721
- `Mac::new`, `Mac::new_from_slice`, and `Mac::generate_key` methods ([#1173])
22+
- `CoreWrapper`, `RtVariableCoreWrapper`, and `XofReaderCoreWrapper` types ([#1799])
1823
- `HashReader` and `HashWriter` are moved to the `digest-io` crate ([#1809])
1924
- `io::Write/Read` implementations in favor of the `digest_io::IoWrapper` type ([#1809])
2025

2126
[#1173]: https://github.com/RustCrypto/traits/pull/1173
2227
[#1334]: https://github.com/RustCrypto/traits/pull/1334
2328
[#1759]: https://github.com/RustCrypto/traits/pull/1759
29+
[#1799]: https://github.com/RustCrypto/traits/pull/1799
2430
[#1809]: https://github.com/RustCrypto/traits/pull/1809
31+
[#1820]: https://github.com/RustCrypto/traits/pull/1820
2532

2633
## 0.10.7 (2023-05-19)
2734
### Changed

digest/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ const-oid = { version = "0.10", optional = true }
2323
zeroize = { version = "1.7", optional = true, default-features = false }
2424

2525
[features]
26-
default = ["core-api"]
27-
core-api = ["block-buffer"] # Enable Core API traits
26+
default = ["block-api"]
27+
block-api = ["block-buffer"] # Enable block API traits
2828
mac = ["subtle"] # Enable MAC traits
2929
rand_core = ["crypto-common/rand_core"] # Enable random key generation methods
3030
os_rng = ["crypto-common/rand_core", "rand_core"]

digest/README.md

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,25 +64,6 @@ let hash = Sha256::digest(b"my message");
6464
println!("Result: {:x}", hash);
6565
```
6666

67-
### Hashing `Read`-able objects
68-
69-
If you want to hash data from [`Read`][3] trait (e.g. from file) you can rely on
70-
implementation of [`Write`][4] trait (requires enabled-by-default `std` feature):
71-
72-
```rust
73-
use sha2::{Sha256, Digest};
74-
use std::{fs, io};
75-
76-
let mut file = fs::File::open(&path)?;
77-
let mut hasher = Sha256::new();
78-
let n = io::copy(&mut file, &mut hasher)?;
79-
let hash = hasher.finalize();
80-
81-
println!("Path: {}", path);
82-
println!("Bytes processed: {}", n);
83-
println!("Hash value: {:x}", hash);
84-
```
85-
8667
### Generic code
8768

8869
You can write generic code over `Digest` (or other traits from `digest` crate)
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,14 @@
55
//! higher-level traits.
66
use crate::InvalidOutputSize;
77

8+
pub use block_buffer::{Eager, Lazy};
89
pub use crypto_common::{AlgorithmName, Block, BlockSizeUser, OutputSizeUser, Reset};
910

1011
use block_buffer::{BlockBuffer, BufferKind};
1112
use crypto_common::Output;
1213

1314
mod ct_variable;
14-
mod rt_variable;
15-
mod wrapper;
16-
mod xof_reader;
17-
18-
pub use ct_variable::CtVariableCoreWrapper;
19-
pub use rt_variable::RtVariableCoreWrapper;
20-
pub use wrapper::{CoreProxy, CoreWrapper};
21-
pub use xof_reader::XofReaderCoreWrapper;
15+
pub use ct_variable::CtOutWrapper;
2216

2317
/// Buffer type used by type which implements [`BufferKindUser`].
2418
pub type Buffer<S> =
@@ -102,3 +96,9 @@ pub enum TruncSide {
10296
/// Truncate right side, i.e. `&out[m..]`.
10397
Right,
10498
}
99+
100+
/// A proxy trait to a core type.
101+
pub trait CoreProxy {
102+
/// Wrapped block-level type.
103+
type Core;
104+
}
Lines changed: 35 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use super::{
55
#[cfg(feature = "mac")]
66
use crate::MacMarker;
77
use crate::{CollisionResistance, CustomizedInit, HashMarker, VarOutputCustomized};
8-
#[cfg(feature = "oid")]
9-
use const_oid::{AssociatedOid, ObjectIdentifier};
108
use core::{
119
fmt,
1210
marker::PhantomData,
@@ -16,97 +14,82 @@ use crypto_common::{
1614
Block, BlockSizeUser, OutputSizeUser,
1715
array::{Array, ArraySize},
1816
hazmat::{DeserializeStateError, SerializableState, SerializedState, SubSerializedStateSize},
19-
typenum::{IsLess, IsLessOrEqual, Le, LeEq, NonZero, Sum, U1, U256},
17+
typenum::{IsLess, IsLessOrEqual, Le, NonZero, Sum, True, U1, U256},
2018
};
2119

22-
/// Dummy type used with [`CtVariableCoreWrapper`] in cases when
23-
/// resulting hash does not have a known OID.
24-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
25-
pub struct NoOid;
26-
27-
/// Wrapper around [`VariableOutputCore`] which selects output size
28-
/// at compile time.
20+
/// Wrapper around [`VariableOutputCore`] which selects output size at compile time.
2921
#[derive(Clone)]
30-
pub struct CtVariableCoreWrapper<T, OutSize, O = NoOid>
22+
pub struct CtOutWrapper<T, OutSize>
3123
where
3224
T: VariableOutputCore,
33-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
34-
LeEq<OutSize, T::OutputSize>: NonZero,
25+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
3526
{
3627
inner: T,
37-
_out: PhantomData<(OutSize, O)>,
28+
_out: PhantomData<OutSize>,
3829
}
3930

40-
impl<T, OutSize, O> HashMarker for CtVariableCoreWrapper<T, OutSize, O>
31+
impl<T, OutSize> HashMarker for CtOutWrapper<T, OutSize>
4132
where
4233
T: VariableOutputCore + HashMarker,
43-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
44-
LeEq<OutSize, T::OutputSize>: NonZero,
34+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
4535
{
4636
}
4737

4838
#[cfg(feature = "mac")]
49-
impl<T, OutSize, O> MacMarker for CtVariableCoreWrapper<T, OutSize, O>
39+
impl<T, OutSize> MacMarker for CtOutWrapper<T, OutSize>
5040
where
5141
T: VariableOutputCore + MacMarker,
52-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
53-
LeEq<OutSize, T::OutputSize>: NonZero,
42+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
5443
{
5544
}
5645

57-
impl<T, OutSize> CollisionResistance for CtVariableCoreWrapper<T, OutSize>
46+
impl<T, OutSize> CollisionResistance for CtOutWrapper<T, OutSize>
5847
where
5948
T: VariableOutputCore + CollisionResistance,
60-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
61-
LeEq<OutSize, T::OutputSize>: NonZero,
49+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
6250
{
6351
type CollisionResistance = T::CollisionResistance;
6452
}
6553

66-
impl<T, OutSize, O> BlockSizeUser for CtVariableCoreWrapper<T, OutSize, O>
54+
impl<T, OutSize> BlockSizeUser for CtOutWrapper<T, OutSize>
6755
where
6856
T: VariableOutputCore,
69-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
70-
LeEq<OutSize, T::OutputSize>: NonZero,
57+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
7158
{
7259
type BlockSize = T::BlockSize;
7360
}
7461

75-
impl<T, OutSize, O> UpdateCore for CtVariableCoreWrapper<T, OutSize, O>
62+
impl<T, OutSize> UpdateCore for CtOutWrapper<T, OutSize>
7663
where
7764
T: VariableOutputCore,
78-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
79-
LeEq<OutSize, T::OutputSize>: NonZero,
65+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
8066
{
8167
#[inline]
8268
fn update_blocks(&mut self, blocks: &[Block<Self>]) {
8369
self.inner.update_blocks(blocks);
8470
}
8571
}
8672

87-
impl<T, OutSize, O> OutputSizeUser for CtVariableCoreWrapper<T, OutSize, O>
73+
impl<T, OutSize> OutputSizeUser for CtOutWrapper<T, OutSize>
8874
where
8975
T: VariableOutputCore,
90-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
91-
LeEq<OutSize, T::OutputSize>: NonZero,
76+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
9277
{
9378
type OutputSize = OutSize;
9479
}
9580

96-
impl<T, OutSize, O> BufferKindUser for CtVariableCoreWrapper<T, OutSize, O>
81+
impl<T, OutSize> BufferKindUser for CtOutWrapper<T, OutSize>
9782
where
9883
T: VariableOutputCore,
99-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
100-
LeEq<OutSize, T::OutputSize>: NonZero,
84+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
10185
{
10286
type BufferKind = T::BufferKind;
10387
}
10488

105-
impl<T, OutSize, O> FixedOutputCore for CtVariableCoreWrapper<T, OutSize, O>
89+
impl<T, OutSize> FixedOutputCore for CtOutWrapper<T, OutSize>
10690
where
10791
T: VariableOutputCore,
108-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
109-
LeEq<OutSize, T::OutputSize>: NonZero,
92+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
11093
{
11194
#[inline]
11295
fn finalize_fixed_core(
@@ -125,11 +108,10 @@ where
125108
}
126109
}
127110

128-
impl<T, OutSize, O> Default for CtVariableCoreWrapper<T, OutSize, O>
111+
impl<T, OutSize> Default for CtOutWrapper<T, OutSize>
129112
where
130113
T: VariableOutputCore,
131-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
132-
LeEq<OutSize, T::OutputSize>: NonZero,
114+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
133115
{
134116
#[inline]
135117
fn default() -> Self {
@@ -140,11 +122,10 @@ where
140122
}
141123
}
142124

143-
impl<T, OutSize, O> CustomizedInit for CtVariableCoreWrapper<T, OutSize, O>
125+
impl<T, OutSize> CustomizedInit for CtOutWrapper<T, OutSize>
144126
where
145127
T: VariableOutputCore + VarOutputCustomized,
146-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
147-
LeEq<OutSize, T::OutputSize>: NonZero,
128+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
148129
{
149130
#[inline]
150131
fn new_customized(customization: &[u8]) -> Self {
@@ -155,23 +136,21 @@ where
155136
}
156137
}
157138

158-
impl<T, OutSize, O> Reset for CtVariableCoreWrapper<T, OutSize, O>
139+
impl<T, OutSize> Reset for CtOutWrapper<T, OutSize>
159140
where
160141
T: VariableOutputCore,
161-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
162-
LeEq<OutSize, T::OutputSize>: NonZero,
142+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
163143
{
164144
#[inline]
165145
fn reset(&mut self) {
166146
*self = Default::default();
167147
}
168148
}
169149

170-
impl<T, OutSize, O> AlgorithmName for CtVariableCoreWrapper<T, OutSize, O>
150+
impl<T, OutSize> AlgorithmName for CtOutWrapper<T, OutSize>
171151
where
172152
T: VariableOutputCore + AlgorithmName,
173-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
174-
LeEq<OutSize, T::OutputSize>: NonZero,
153+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
175154
{
176155
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
177156
T::write_alg_name(f)?;
@@ -180,61 +159,31 @@ where
180159
}
181160
}
182161

183-
#[cfg(feature = "oid")]
184-
impl<T, OutSize, O> AssociatedOid for CtVariableCoreWrapper<T, OutSize, O>
185-
where
186-
T: VariableOutputCore,
187-
O: AssociatedOid,
188-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
189-
LeEq<OutSize, T::OutputSize>: NonZero,
190-
{
191-
const OID: ObjectIdentifier = O::OID;
192-
}
193-
194162
#[cfg(feature = "zeroize")]
195-
impl<T, OutSize, O> zeroize::ZeroizeOnDrop for CtVariableCoreWrapper<T, OutSize, O>
163+
impl<T, OutSize> zeroize::ZeroizeOnDrop for CtOutWrapper<T, OutSize>
196164
where
197165
T: VariableOutputCore + zeroize::ZeroizeOnDrop,
198-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
199-
LeEq<OutSize, T::OutputSize>: NonZero,
166+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
200167
{
201168
}
202169

203-
impl<T, OutSize, O> fmt::Debug for CtVariableCoreWrapper<T, OutSize, O>
170+
impl<T, OutSize> fmt::Debug for CtOutWrapper<T, OutSize>
204171
where
205172
T: VariableOutputCore + AlgorithmName,
206-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
207-
LeEq<OutSize, T::OutputSize>: NonZero,
173+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
208174
{
209175
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
210176
Self::write_alg_name(f)
211177
}
212178
}
213179

214-
/// Implement dummy type with hidden docs which is used to "carry" hasher
215-
/// OID for [`CtVariableCoreWrapper`].
216-
#[macro_export]
217-
macro_rules! impl_oid_carrier {
218-
($name:ident, $oid:literal) => {
219-
#[doc(hidden)]
220-
#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)]
221-
pub struct $name;
222-
223-
#[cfg(feature = "oid")]
224-
impl AssociatedOid for $name {
225-
const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap($oid);
226-
}
227-
};
228-
}
229-
230180
type CtVariableCoreWrapperSerializedStateSize<T> =
231181
Sum<<T as SerializableState>::SerializedStateSize, U1>;
232182

233-
impl<T, OutSize, O> SerializableState for CtVariableCoreWrapper<T, OutSize, O>
183+
impl<T, OutSize> SerializableState for CtOutWrapper<T, OutSize>
234184
where
235185
T: VariableOutputCore + SerializableState,
236-
OutSize: ArraySize + IsLessOrEqual<T::OutputSize>,
237-
LeEq<OutSize, T::OutputSize>: NonZero,
186+
OutSize: ArraySize + IsLessOrEqual<T::OutputSize, Output = True>,
238187
T::BlockSize: IsLess<U256>,
239188
Le<T::BlockSize, U256>: NonZero,
240189
T::SerializedStateSize: Add<U1>,

digest/src/buffer_macros.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mod fixed;
2+
mod variable_ct;
3+
mod variable_rt;
4+
mod xof;

0 commit comments

Comments
 (0)