Skip to content

Commit 87dede2

Browse files
Rust wrapper: add mac feature and implement digest/mac traits
1 parent cc77cbb commit 87dede2

8 files changed

Lines changed: 382 additions & 2 deletions

File tree

wrapper/rust/wolfssl-wolfcrypt/Cargo.lock

Lines changed: 1 addition & 0 deletions
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ std = []
1515
rand_core = ["dep:rand_core"]
1616
aead = ["dep:aead"]
1717
cipher = ["dep:cipher"]
18+
mac = ["digest/mac"]
1819
digest = ["dep:digest"]
1920
signature = ["dep:signature"]
2021
password-hash = ["dep:password-hash", "password-hash/phc"]
@@ -34,7 +35,7 @@ hybrid-array = { version = "0.4.7", optional = true, default-features = false }
3435
[dev-dependencies]
3536
aead = { version = "0.5", features = ["alloc", "dev"] }
3637
cipher = "0.5"
37-
digest = { version = "0.11", features = ["dev"] }
38+
digest = { version = "0.11", features = ["dev", "mac"] }
3839
signature = "2.2"
3940
password-hash = { version = "0.6.1", features = ["phc"] }
4041
kem = "0.3"

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,digest,signature,password-hash,kem
1+
FEATURES := rand_core,aead,cipher,digest,mac,signature,password-hash,kem
22
CARGO_FEATURE_FLAGS := --features $(FEATURES)
33

44
.PHONY: all
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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::Mac` trait implementations for the wolfCrypt CMAC types.
23+
24+
This module provides typed AES-CMAC wrappers with implementations of the
25+
traits from the `digest` crate (`MacMarker`, `KeyInit`, `Update`,
26+
`FixedOutput`) for each AES key size (128, 192, 256). With these
27+
implementations the `digest::Mac` trait becomes available via its blanket
28+
implementation, allowing these CMAC types to be used anywhere a RustCrypto
29+
`Mac` is accepted.
30+
31+
Any failure returned by the underlying wolfCrypt call in a trait method will
32+
result in a panic, matching the infallible signatures required by the
33+
RustCrypto traits.
34+
*/
35+
36+
use digest::consts::{U16, U24, U32};
37+
38+
macro_rules! impl_cmac_mac {
39+
(
40+
$(#[$attr:meta])*
41+
$name:ident, key = $key_size:ty
42+
) => {
43+
$(#[$attr])*
44+
pub struct $name {
45+
cmac: crate::cmac::CMAC,
46+
}
47+
48+
$(#[$attr])*
49+
impl digest::MacMarker for $name {}
50+
51+
$(#[$attr])*
52+
impl digest::OutputSizeUser for $name {
53+
type OutputSize = U16;
54+
}
55+
56+
$(#[$attr])*
57+
impl digest::common::KeySizeUser for $name {
58+
type KeySize = $key_size;
59+
}
60+
61+
$(#[$attr])*
62+
impl digest::KeyInit for $name {
63+
fn new(key: &digest::Key<Self>) -> Self {
64+
Self {
65+
cmac: crate::cmac::CMAC::new(key.as_slice())
66+
.expect("wolfCrypt CMAC init failed"),
67+
}
68+
}
69+
}
70+
71+
$(#[$attr])*
72+
impl digest::Update for $name {
73+
fn update(&mut self, data: &[u8]) {
74+
crate::cmac::CMAC::update(&mut self.cmac, data)
75+
.expect("wolfCrypt CMAC update failed");
76+
}
77+
}
78+
79+
$(#[$attr])*
80+
impl digest::FixedOutput for $name {
81+
fn finalize_into(self, out: &mut digest::Output<Self>) {
82+
self.cmac.finalize(out.as_mut_slice())
83+
.expect("wolfCrypt CMAC finalize failed");
84+
}
85+
}
86+
};
87+
}
88+
89+
impl_cmac_mac! {
90+
CmacAes128, key = U16
91+
}
92+
93+
impl_cmac_mac! {
94+
CmacAes192, key = U24
95+
}
96+
97+
impl_cmac_mac! {
98+
CmacAes256, key = U32
99+
}
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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::Mac` trait implementations for the wolfCrypt HMAC types.
23+
24+
This module provides typed HMAC wrappers with implementations of the traits
25+
from the `digest` crate (`MacMarker`, `KeyInit`, `Update`, `FixedOutput`)
26+
for each supported hash algorithm. With these implementations the
27+
`digest::Mac` trait becomes available via its blanket implementation,
28+
allowing these HMAC types to be used anywhere a RustCrypto `Mac` is accepted.
29+
30+
Any failure returned by the underlying wolfCrypt call in a trait method will
31+
result in a panic, matching the infallible signatures required by the
32+
RustCrypto traits.
33+
*/
34+
35+
use digest::consts::{
36+
U20, U28, U32, U48, U64, U72, U104, U128, U136, U144,
37+
};
38+
39+
macro_rules! impl_hmac_mac {
40+
(
41+
$(#[$attr:meta])*
42+
$name:ident, hmac_type = $hmac_type:expr, key = $key_size:ty, out = $out_size:ty
43+
) => {
44+
$(#[$attr])*
45+
pub struct $name {
46+
hmac: crate::hmac::HMAC,
47+
}
48+
49+
$(#[$attr])*
50+
impl digest::MacMarker for $name {}
51+
52+
$(#[$attr])*
53+
impl digest::OutputSizeUser for $name {
54+
type OutputSize = $out_size;
55+
}
56+
57+
$(#[$attr])*
58+
impl digest::common::KeySizeUser for $name {
59+
type KeySize = $key_size;
60+
}
61+
62+
$(#[$attr])*
63+
impl digest::KeyInit for $name {
64+
fn new(key: &digest::Key<Self>) -> Self {
65+
Self {
66+
hmac: crate::hmac::HMAC::new($hmac_type, key.as_slice())
67+
.expect("wolfCrypt HMAC init failed"),
68+
}
69+
}
70+
71+
fn new_from_slice(key: &[u8]) -> Result<Self, digest::InvalidLength> {
72+
crate::hmac::HMAC::new($hmac_type, key)
73+
.map(|hmac| Self { hmac })
74+
.map_err(|_| digest::InvalidLength)
75+
}
76+
}
77+
78+
$(#[$attr])*
79+
impl digest::Update for $name {
80+
fn update(&mut self, data: &[u8]) {
81+
crate::hmac::HMAC::update(&mut self.hmac, data)
82+
.expect("wolfCrypt HMAC update failed");
83+
}
84+
}
85+
86+
$(#[$attr])*
87+
impl digest::FixedOutput for $name {
88+
fn finalize_into(mut self, out: &mut digest::Output<Self>) {
89+
crate::hmac::HMAC::finalize(&mut self.hmac, out.as_mut_slice())
90+
.expect("wolfCrypt HMAC finalize failed");
91+
}
92+
}
93+
};
94+
}
95+
96+
impl_hmac_mac! {
97+
#[cfg(sha)]
98+
HmacSha, hmac_type = crate::hmac::HMAC::TYPE_SHA, key = U64, out = U20
99+
}
100+
101+
impl_hmac_mac! {
102+
#[cfg(sha224)]
103+
HmacSha224, hmac_type = crate::hmac::HMAC::TYPE_SHA224, key = U64, out = U28
104+
}
105+
106+
impl_hmac_mac! {
107+
#[cfg(sha256)]
108+
HmacSha256, hmac_type = crate::hmac::HMAC::TYPE_SHA256, key = U64, out = U32
109+
}
110+
111+
impl_hmac_mac! {
112+
#[cfg(sha384)]
113+
HmacSha384, hmac_type = crate::hmac::HMAC::TYPE_SHA384, key = U128, out = U48
114+
}
115+
116+
impl_hmac_mac! {
117+
#[cfg(sha512)]
118+
HmacSha512, hmac_type = crate::hmac::HMAC::TYPE_SHA512, key = U128, out = U64
119+
}
120+
121+
#[cfg(sha512_224)]
122+
impl_hmac_mac! {
123+
#[cfg(sha512_224)]
124+
HmacSha512_224, hmac_type = crate::hmac::HMAC::TYPE_SHA512_224, key = U128, out = U28
125+
}
126+
127+
#[cfg(sha512_256)]
128+
impl_hmac_mac! {
129+
#[cfg(sha512_256)]
130+
HmacSha512_256, hmac_type = crate::hmac::HMAC::TYPE_SHA512_256, key = U128, out = U32
131+
}
132+
133+
impl_hmac_mac! {
134+
#[cfg(sha3)]
135+
HmacSha3_224, hmac_type = crate::hmac::HMAC::TYPE_SHA3_224, key = U144, out = U28
136+
}
137+
138+
impl_hmac_mac! {
139+
#[cfg(sha3)]
140+
HmacSha3_256, hmac_type = crate::hmac::HMAC::TYPE_SHA3_256, key = U136, out = U32
141+
}
142+
143+
impl_hmac_mac! {
144+
#[cfg(sha3)]
145+
HmacSha3_384, hmac_type = crate::hmac::HMAC::TYPE_SHA3_384, key = U104, out = U48
146+
}
147+
148+
impl_hmac_mac! {
149+
#[cfg(sha3)]
150+
HmacSha3_512, hmac_type = crate::hmac::HMAC::TYPE_SHA3_512, key = U72, out = U64
151+
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod aes;
4444
pub mod blake2;
4545
pub mod chacha20_poly1305;
4646
pub mod cmac;
47+
#[cfg(all(cmac, feature = "mac"))]
48+
pub mod cmac_mac;
4749
pub mod curve25519;
4850
pub mod dh;
4951
pub mod dilithium;
@@ -55,6 +57,8 @@ pub mod ed448;
5557
pub mod fips;
5658
pub mod hkdf;
5759
pub mod hmac;
60+
#[cfg(all(hmac, feature = "mac"))]
61+
pub mod hmac_mac;
5862
pub mod kdf;
5963
pub mod lms;
6064
pub mod mlkem;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#![cfg(all(cmac, feature = "mac"))]
2+
3+
use digest::{KeyInit, Mac};
4+
use wolfssl_wolfcrypt::cmac_mac::CmacAes128;
5+
6+
#[test]
7+
fn test_cmac_aes128_mac_trait() {
8+
let key = [
9+
0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
10+
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
11+
];
12+
let message = [
13+
0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
14+
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
15+
];
16+
let expected = [
17+
0x07u8, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
18+
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
19+
];
20+
21+
let mut mac = CmacAes128::new_from_slice(&key)
22+
.expect("CMAC init failed");
23+
mac.update(&message);
24+
mac.verify_slice(&expected).expect("CMAC verification failed");
25+
}
26+
27+
#[test]
28+
fn test_cmac_aes128_mac_finalize() {
29+
let key = [
30+
0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
31+
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
32+
];
33+
let message = [
34+
0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
35+
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
36+
];
37+
let expected: &[u8] = &[
38+
0x07u8, 0x0a, 0x16, 0xb4, 0x6b, 0x4d, 0x41, 0x44,
39+
0xf7, 0x9b, 0xdd, 0x9d, 0xd0, 0x4a, 0x28, 0x7c
40+
];
41+
42+
let mac = CmacAes128::new_from_slice(&key)
43+
.expect("CMAC init failed")
44+
.chain_update(&message);
45+
let result = mac.finalize();
46+
assert_eq!(result.as_bytes().as_slice(), expected);
47+
}
48+
49+
#[test]
50+
fn test_cmac_aes128_mac_verify_fail() {
51+
let key = [
52+
0x2bu8, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
53+
0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
54+
];
55+
let message = [
56+
0x6bu8, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
57+
0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
58+
];
59+
let wrong_tag = [0u8; 16];
60+
61+
let mut mac = CmacAes128::new_from_slice(&key)
62+
.expect("CMAC init failed");
63+
mac.update(&message);
64+
assert!(mac.verify_slice(&wrong_tag).is_err());
65+
}
66+
67+
#[test]
68+
fn test_cmac_aes128_wrong_key_size() {
69+
let bad_key = [0u8; 15]; // wrong size for AES-128
70+
assert!(CmacAes128::new_from_slice(&bad_key).is_err());
71+
}

0 commit comments

Comments
 (0)