Skip to content

Commit 7f33de0

Browse files
Rust wrapper: add digest implementations
1 parent c4c71ee commit 7f33de0

6 files changed

Lines changed: 326 additions & 2 deletions

File tree

wrapper/rust/wolfssl-wolfcrypt/Cargo.lock

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

wrapper/rust/wolfssl-wolfcrypt/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@ std = []
1515
rand_core = ["dep:rand_core"]
1616
aead = ["dep:aead"]
1717
cipher = ["dep:cipher"]
18+
digest = ["dep:digest"]
1819

1920
[dependencies]
2021
rand_core = { version = "0.10", optional = true, default-features = false }
2122
aead = { version = "0.5", optional = true, default-features = false }
2223
cipher = { version = "0.5", optional = true, default-features = false }
24+
digest = { version = "0.11", optional = true, default-features = false, features = ["block-api"] }
2325
zeroize = { version = "1.3", default-features = false, features = ["derive"] }
2426

2527
[dev-dependencies]
2628
aead = { version = "0.5", features = ["alloc", "dev"] }
2729
cipher = "0.5"
30+
digest = { version = "0.11", features = ["dev"] }
2831

2932
[build-dependencies]
3033
bindgen = "0.72.1"

wrapper/rust/wolfssl-wolfcrypt/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FEATURES := rand_core,aead,cipher
1+
FEATURES := rand_core,aead,cipher,digest
22
CARGO_FEATURE_FLAGS := --features $(FEATURES)
33

44
.PHONY: all

wrapper/rust/wolfssl-wolfcrypt/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ pub mod prf;
6060
pub mod random;
6161
pub mod rsa;
6262
pub mod sha;
63+
#[cfg(feature = "digest")]
64+
mod sha_digest;
6365

6466
/// Convert a buffer length to `u32`, returning `BUFFER_E` if it overflows.
6567
pub(crate) fn buffer_len_to_u32(len: usize) -> Result<u32, i32> {
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
/*
2+
* Copyright (C) 2006-2026 wolfSSL Inc.
3+
*
4+
* This file is part of wolfSSL.
5+
*
6+
* wolfSSL is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* wolfSSL is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
19+
*/
20+
21+
/*!
22+
RustCrypto `digest` trait implementations for the wolfCrypt SHA-family hash
23+
types.
24+
25+
This module provides implementations of the traits from the `digest` crate
26+
(`HashMarker`, `OutputSizeUser`, `BlockSizeUser`, `Update`, `Reset`,
27+
`FixedOutput`, and `FixedOutputReset`) for the fixed-output hash types
28+
defined in [`crate::sha`]. With these implementations the `digest::Digest`
29+
trait becomes available via its blanket implementation, allowing these
30+
hashers to be used anywhere a RustCrypto `Digest` is accepted.
31+
32+
Any failure returned by the underlying wolfCrypt call in a trait method will
33+
result in a panic, matching the infallible signatures required by the
34+
RustCrypto traits.
35+
*/
36+
37+
use digest::consts::{
38+
U20, U28, U32, U48, U64, U72, U104, U128, U136, U144,
39+
};
40+
41+
macro_rules! impl_digest_traits {
42+
(
43+
$(#[$attr:meta])*
44+
$ty:path, out = $output:ty, block = $block:ty
45+
) => {
46+
$(#[$attr])*
47+
impl Default for $ty {
48+
fn default() -> Self {
49+
<$ty>::new().expect("wolfCrypt hash init failed")
50+
}
51+
}
52+
53+
$(#[$attr])*
54+
impl digest::HashMarker for $ty {}
55+
56+
$(#[$attr])*
57+
impl digest::OutputSizeUser for $ty {
58+
type OutputSize = $output;
59+
}
60+
61+
$(#[$attr])*
62+
impl digest::block_api::BlockSizeUser for $ty {
63+
type BlockSize = $block;
64+
}
65+
66+
$(#[$attr])*
67+
impl digest::Update for $ty {
68+
fn update(&mut self, data: &[u8]) {
69+
<$ty>::update(self, data).expect("wolfCrypt hash update failed");
70+
}
71+
}
72+
73+
$(#[$attr])*
74+
impl digest::Reset for $ty {
75+
fn reset(&mut self) {
76+
<$ty>::init(self).expect("wolfCrypt hash init failed");
77+
}
78+
}
79+
80+
$(#[$attr])*
81+
impl digest::FixedOutput for $ty {
82+
fn finalize_into(mut self, out: &mut digest::Output<Self>) {
83+
<$ty>::finalize(&mut self, out.as_mut_slice())
84+
.expect("wolfCrypt hash finalize failed");
85+
}
86+
}
87+
88+
$(#[$attr])*
89+
impl digest::FixedOutputReset for $ty {
90+
fn finalize_into_reset(&mut self, out: &mut digest::Output<Self>) {
91+
<$ty>::finalize(self, out.as_mut_slice())
92+
.expect("wolfCrypt hash finalize failed");
93+
<$ty>::init(self).expect("wolfCrypt hash init failed");
94+
}
95+
}
96+
};
97+
}
98+
99+
impl_digest_traits! {
100+
#[cfg(sha)]
101+
crate::sha::SHA, out = U20, block = U64
102+
}
103+
104+
impl_digest_traits! {
105+
#[cfg(sha224)]
106+
crate::sha::SHA224, out = U28, block = U64
107+
}
108+
109+
impl_digest_traits! {
110+
#[cfg(sha256)]
111+
crate::sha::SHA256, out = U32, block = U64
112+
}
113+
114+
impl_digest_traits! {
115+
#[cfg(sha384)]
116+
crate::sha::SHA384, out = U48, block = U128
117+
}
118+
119+
impl_digest_traits! {
120+
#[cfg(sha512)]
121+
crate::sha::SHA512, out = U64, block = U128
122+
}
123+
124+
impl_digest_traits! {
125+
#[cfg(sha3)]
126+
crate::sha::SHA3_224, out = U28, block = U144
127+
}
128+
129+
impl_digest_traits! {
130+
#[cfg(sha3)]
131+
crate::sha::SHA3_256, out = U32, block = U136
132+
}
133+
134+
impl_digest_traits! {
135+
#[cfg(sha3)]
136+
crate::sha::SHA3_384, out = U48, block = U104
137+
}
138+
139+
impl_digest_traits! {
140+
#[cfg(sha3)]
141+
crate::sha::SHA3_512, out = U64, block = U72
142+
}
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#![cfg(feature = "digest")]
2+
3+
use digest::{Digest, FixedOutputReset};
4+
use digest::block_api::BlockSizeUser;
5+
6+
mod common;
7+
8+
fn check_digest<D: Digest + BlockSizeUser + FixedOutputReset + Default>(
9+
input: &[u8],
10+
expected: &[u8],
11+
expected_block_size: usize,
12+
) {
13+
assert_eq!(<D as Digest>::output_size(), expected.len());
14+
assert_eq!(<D as BlockSizeUser>::block_size(), expected_block_size);
15+
16+
/* One-shot digest via associated function. */
17+
let out = D::digest(input);
18+
assert_eq!(out.as_slice(), expected);
19+
20+
/* Streaming via Digest::update and finalize. */
21+
let mut hasher = D::new();
22+
Digest::update(&mut hasher, input);
23+
let out = hasher.finalize();
24+
assert_eq!(out.as_slice(), expected);
25+
26+
/* Split update, via Default + Update + FixedOutputReset::finalize_reset. */
27+
let mut hasher = D::default();
28+
if input.len() >= 2 {
29+
let mid = input.len() / 2;
30+
Digest::update(&mut hasher, &input[..mid]);
31+
Digest::update(&mut hasher, &input[mid..]);
32+
} else {
33+
Digest::update(&mut hasher, input);
34+
}
35+
let out = hasher.finalize_reset();
36+
assert_eq!(out.as_slice(), expected);
37+
38+
/* After reset, the same hasher should produce the same result. */
39+
Digest::update(&mut hasher, input);
40+
let out = hasher.finalize();
41+
assert_eq!(out.as_slice(), expected);
42+
}
43+
44+
#[test]
45+
#[cfg(sha)]
46+
fn test_digest_sha() {
47+
use wolfssl_wolfcrypt::sha::SHA;
48+
common::setup();
49+
check_digest::<SHA>(
50+
b"abc",
51+
b"\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D",
52+
64,
53+
);
54+
}
55+
56+
#[test]
57+
#[cfg(sha224)]
58+
fn test_digest_sha224() {
59+
use wolfssl_wolfcrypt::sha::SHA224;
60+
common::setup();
61+
check_digest::<SHA224>(
62+
b"abc",
63+
b"\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7",
64+
64,
65+
);
66+
}
67+
68+
#[test]
69+
#[cfg(sha256)]
70+
fn test_digest_sha256() {
71+
use wolfssl_wolfcrypt::sha::SHA256;
72+
common::setup();
73+
check_digest::<SHA256>(
74+
b"abc",
75+
b"\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00\x15\xAD",
76+
64,
77+
);
78+
}
79+
80+
#[test]
81+
#[cfg(sha384)]
82+
fn test_digest_sha384() {
83+
use wolfssl_wolfcrypt::sha::SHA384;
84+
common::setup();
85+
check_digest::<SHA384>(
86+
b"abc",
87+
b"\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34\xc8\x25\xa7",
88+
128,
89+
);
90+
}
91+
92+
#[test]
93+
#[cfg(sha512)]
94+
fn test_digest_sha512() {
95+
use wolfssl_wolfcrypt::sha::SHA512;
96+
common::setup();
97+
check_digest::<SHA512>(
98+
b"abc",
99+
b"\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f\xa5\x4c\xa4\x9f",
100+
128,
101+
);
102+
}
103+
104+
#[test]
105+
#[cfg(sha3)]
106+
fn test_digest_sha3_224() {
107+
use wolfssl_wolfcrypt::sha::SHA3_224;
108+
common::setup();
109+
check_digest::<SHA3_224>(
110+
b"abc",
111+
b"\xe6\x42\x82\x4c\x3f\x8c\xf2\x4a\xd0\x92\x34\xee\x7d\x3c\x76\x6f\xc9\xa3\xa5\x16\x8d\x0c\x94\xad\x73\xb4\x6f\xdf",
112+
144,
113+
);
114+
}
115+
116+
#[test]
117+
#[cfg(sha3)]
118+
fn test_digest_sha3_256() {
119+
use wolfssl_wolfcrypt::sha::SHA3_256;
120+
common::setup();
121+
check_digest::<SHA3_256>(
122+
b"abc",
123+
b"\x3a\x98\x5d\xa7\x4f\xe2\x25\xb2\x04\x5c\x17\x2d\x6b\xd3\x90\xbd\x85\x5f\x08\x6e\x3e\x9d\x52\x5b\x46\xbf\xe2\x45\x11\x43\x15\x32",
124+
136,
125+
);
126+
}
127+
128+
#[test]
129+
#[cfg(sha3)]
130+
fn test_digest_sha3_384() {
131+
use wolfssl_wolfcrypt::sha::SHA3_384;
132+
common::setup();
133+
check_digest::<SHA3_384>(
134+
b"abc",
135+
b"\xec\x01\x49\x82\x88\x51\x6f\xc9\x26\x45\x9f\x58\xe2\xc6\xad\x8d\xf9\xb4\x73\xcb\x0f\xc0\x8c\x25\x96\xda\x7c\xf0\xe4\x9b\xe4\xb2\x98\xd8\x8c\xea\x92\x7a\xc7\xf5\x39\xf1\xed\xf2\x28\x37\x6d\x25",
136+
104,
137+
);
138+
}
139+
140+
#[test]
141+
#[cfg(sha3)]
142+
fn test_digest_sha3_512() {
143+
use wolfssl_wolfcrypt::sha::SHA3_512;
144+
common::setup();
145+
check_digest::<SHA3_512>(
146+
b"abc",
147+
b"\xb7\x51\x85\x0b\x1a\x57\x16\x8a\x56\x93\xcd\x92\x4b\x6b\x09\x6e\x08\xf6\x21\x82\x74\x44\xf7\x0d\x88\x4f\x5d\x02\x40\xd2\x71\x2e\x10\xe1\x16\xe9\x19\x2a\xf3\xc9\x1a\x7e\xc5\x76\x47\xe3\x93\x40\x57\x34\x0b\x4c\xf4\x08\xd5\xa5\x65\x92\xf8\x27\x4e\xec\x53\xf0",
148+
72,
149+
);
150+
}

0 commit comments

Comments
 (0)