Skip to content

Commit a9ef4f6

Browse files
authored
digest: add Mac::verify_reset and Mac::verify_slice_reset methods (#1154)
1 parent f6ebf83 commit a9ef4f6

File tree

4 files changed

+95
-42
lines changed

4 files changed

+95
-42
lines changed

Cargo.lock

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

digest/CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## 0.10.6 (2022-11-17)
9+
### Added
10+
- `Mac::verify_reset` and `Mac::verify_slice_reset` methods ([#1154])
11+
12+
[#1154]: https://github.com/RustCrypto/traits/pull/1154
13+
814
## 0.10.5 (2022-09-16)
915
### Fixed
10-
- MSRV build. ([#1117])
16+
- MSRV build ([#1117])
1117

1218
[#1117]: https://github.com/RustCrypto/traits/pull/1117
1319

digest/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "digest"
3-
description = "Traits for cryptographic hash functions"
4-
version = "0.10.5"
3+
description = "Traits for cryptographic hash functions and message authentication codes"
4+
version = "0.10.6"
55
authors = ["RustCrypto Developers"]
66
license = "MIT OR Apache-2.0"
77
readme = "README.md"

digest/src/mac.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,28 @@ pub trait Mac: OutputSizeUser + Sized {
5959
/// Check if tag/code value is correct for the processed input.
6060
fn verify(self, tag: &Output<Self>) -> Result<(), MacError>;
6161

62+
/// Check if tag/code value is correct for the processed input and reset
63+
/// [`Mac`] instance.
64+
fn verify_reset(&mut self, tag: &Output<Self>) -> Result<(), MacError>
65+
where
66+
Self: FixedOutputReset;
67+
6268
/// Check truncated tag correctness using all bytes
6369
/// of calculated tag.
6470
///
6571
/// Returns `Error` if `tag` is not valid or not equal in length
6672
/// to MAC's output.
6773
fn verify_slice(self, tag: &[u8]) -> Result<(), MacError>;
6874

75+
/// Check truncated tag correctness using all bytes
76+
/// of calculated tag and reset [`Mac`] instance.
77+
///
78+
/// Returns `Error` if `tag` is not valid or not equal in length
79+
/// to MAC's output.
80+
fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>
81+
where
82+
Self: FixedOutputReset;
83+
6984
/// Check truncated tag correctness using left side bytes
7085
/// (i.e. `tag[..n]`) of calculated tag.
7186
///
@@ -137,6 +152,18 @@ impl<T: Update + FixedOutput + MacMarker> Mac for T {
137152
}
138153
}
139154

155+
#[inline]
156+
fn verify_reset(&mut self, tag: &Output<Self>) -> Result<(), MacError>
157+
where
158+
Self: FixedOutputReset,
159+
{
160+
if self.finalize_reset() == tag.into() {
161+
Ok(())
162+
} else {
163+
Err(MacError)
164+
}
165+
}
166+
140167
#[inline]
141168
fn verify_slice(self, tag: &[u8]) -> Result<(), MacError> {
142169
let n = tag.len();
@@ -151,6 +178,23 @@ impl<T: Update + FixedOutput + MacMarker> Mac for T {
151178
}
152179
}
153180

181+
#[inline]
182+
fn verify_slice_reset(&mut self, tag: &[u8]) -> Result<(), MacError>
183+
where
184+
Self: FixedOutputReset,
185+
{
186+
let n = tag.len();
187+
if n != Self::OutputSize::USIZE {
188+
return Err(MacError);
189+
}
190+
let choice = self.finalize_fixed_reset().ct_eq(tag);
191+
if choice.into() {
192+
Ok(())
193+
} else {
194+
Err(MacError)
195+
}
196+
}
197+
154198
fn verify_truncated_left(self, tag: &[u8]) -> Result<(), MacError> {
155199
let n = tag.len();
156200
if n == 0 || n > Self::OutputSize::USIZE {

0 commit comments

Comments
 (0)