Skip to content

Commit 63bda77

Browse files
authored
Merge pull request #10305 from holtrop-wolfssl/rust-crate-updates
Rust wrapper: add password-hash, kem, mac traits; fix a few Fenrir findings
2 parents 3a1f51d + b38d7bf commit 63bda77

19 files changed

Lines changed: 1470 additions & 35 deletions

wrapper/rust/wolfssl-wolfcrypt/Cargo.lock

Lines changed: 71 additions & 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: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,31 @@ 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"]
21+
password-hash = ["dep:password-hash", "password-hash/phc"]
22+
kem = ["dep:kem", "hybrid-array/extra-sizes"]
2023

2124
[dependencies]
2225
rand_core = { version = "0.10", optional = true, default-features = false }
2326
aead = { version = "0.5", optional = true, default-features = false }
2427
cipher = { version = "0.5", optional = true, default-features = false }
2528
digest = { version = "0.11", optional = true, default-features = false, features = ["block-api"] }
2629
signature = { version = "2.2", optional = true, default-features = false }
30+
num-traits = { version = "0.2", default-features = false }
2731
zeroize = { version = "1.3", default-features = false, features = ["derive"] }
32+
password-hash = { version = "0.6.1", optional = true, default-features = false }
33+
kem = { version = "0.3", optional = true, default-features = false }
34+
hybrid-array = { version = "0.4.7", optional = true, default-features = false }
2835

2936
[dev-dependencies]
3037
aead = { version = "0.5", features = ["alloc", "dev"] }
3138
cipher = "0.5"
32-
digest = { version = "0.11", features = ["dev"] }
39+
digest = { version = "0.11", features = ["dev", "mac"] }
3340
signature = "2.2"
41+
password-hash = { version = "0.6.1", features = ["phc"] }
42+
kem = "0.3"
3443

3544
[build-dependencies]
3645
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,digest,signature
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+
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,6 +1219,9 @@ impl ECC {
12191219
/// }
12201220
/// ```
12211221
pub fn rs_hex_to_sig(r: &[u8], s: &[u8], dout: &mut [u8]) -> Result<usize, i32> {
1222+
if r.is_empty() || s.is_empty() || r[r.len() - 1] != 0 || s[s.len() - 1] != 0 {
1223+
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
1224+
}
12221225
let mut dout_size = crate::buffer_len_to_u32(dout.len())?;
12231226
let r_ptr = r.as_ptr() as *const core::ffi::c_char;
12241227
let s_ptr = s.as_ptr() as *const core::ffi::c_char;
@@ -1820,7 +1823,7 @@ impl ECC {
18201823
sys::wc_ecc_shared_secret(&mut self.wc_ecc_key,
18211824
&mut peer_key.wc_ecc_key, dout.as_mut_ptr(), &mut out_len)
18221825
};
1823-
if rc < 0 {
1826+
if rc != 0 {
18241827
return Err(rc);
18251828
}
18261829
Ok(out_len as usize)
@@ -1961,7 +1964,7 @@ impl ECC {
19611964
if rc != 0 {
19621965
return Err(rc);
19631966
}
1964-
Ok(res != 0)
1967+
Ok(res == 1)
19651968
}
19661969
}
19671970

0 commit comments

Comments
 (0)