Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
3d50bbc
check stat instead of only checking ret when verifying signatures
gasbytes Mar 18, 2026
1a3db7b
remove panic when signatures are invalid (verify/ecdsa.rs)
gasbytes Mar 18, 2026
ef80b36
copy_from_slice could panic when source and destination lengths differ,
gasbytes Mar 18, 2026
1cd5139
add missing RSA_PSS_SHA512 and RSA_PKCS1_SHA512 to the all array, which
gasbytes Mar 18, 2026
5b2f5cb
(aes256gcm) copy_from_slice could panic when source and destination l…
gasbytes Mar 18, 2026
5c0eb94
minor error in including ed448 as signature scheme in eddsa.rs
gasbytes Mar 18, 2026
81d18aa
match on any Err variant not just WCError when checking signatures in
gasbytes Mar 18, 2026
3654c59
proper error mapping and return in sign/eddsa.rs instead of panic
gasbytes Mar 18, 2026
e7d5059
minor typo of the digest size in hmac_final (changed from sha3_384 to
gasbytes Mar 18, 2026
9476a30
remove hmacobject dangling pointer, heap allocate hmac struct via box,
gasbytes Mar 19, 2026
a91c8f1
check returned value of wc_HKDF_Expand with proper error propagation
gasbytes Mar 19, 2026
9dc39df
- added drop implementations for the missing foreign types to prevent
gasbytes Mar 19, 2026
d0f6ed0
use the appropriate copy function in the clone implementations of sha256
gasbytes Mar 19, 2026
b154c1f
add lengths checks when deriving the secret in the p-* apis, to validate
gasbytes Mar 19, 2026
3d83b34
removing redudant check_if_zero, this it was re-chcking the wc_hmacfinal
gasbytes Mar 19, 2026
254ec63
add zeroize crate to wipe key material from memory on drop (this applies
gasbytes Mar 19, 2026
233cc87
formatting issues fixed via cargo fmt --all
gasbytes Mar 19, 2026
66ecb84
clippy fixes
gasbytes Mar 19, 2026
3938e1a
more fmt fixes
gasbytes Mar 19, 2026
b7765d0
surpress unnecessary-transmutes on macOS (newest toolchain)
gasbytes Mar 19, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/macos-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ jobs:
- name: Run clippy
run: |
cd wolfcrypt-rs
cargo clippy -- -D warnings
cargo clippy -- -D warnings -A unnecessary-transmutes
cd ../rustls-wolfcrypt-provider
cargo clippy -- -D warnings
1 change: 1 addition & 0 deletions rustls-wolfcrypt-provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ anyhow = "1.0.95"
num_cpus = "1.16.0"
lazy_static = "1.5.0"
hex-literal = "0.4.1"
zeroize = { version = "1", default-features = false, features = ["alloc", "derive"] }


[dev-dependencies]
Expand Down
19 changes: 10 additions & 9 deletions rustls-wolfcrypt-provider/src/aead/aes128gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustls::crypto::cipher::{
UnsupportedOperationError,
};
use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
use zeroize::Zeroizing;

use alloc::vec::Vec;
use core::ptr;
Expand All @@ -30,7 +31,7 @@ impl Tls12AeadAlgorithm for Aes128Gcm {

Box::new(WCTls12Encrypter {
iv: iv_as_array.into(),
key: key_as_slice.to_vec(),
key: Zeroizing::new(key_as_slice.to_vec()),
})
}

Expand All @@ -45,7 +46,7 @@ impl Tls12AeadAlgorithm for Aes128Gcm {

Box::new(WCTls12Decrypter {
implicit_iv: iv_implicit_as_array,
key: key_as_slice.to_vec(),
key: Zeroizing::new(key_as_slice.to_vec()),
})
}

Expand All @@ -65,8 +66,8 @@ impl Tls12AeadAlgorithm for Aes128Gcm {
) -> Result<ConnectionTrafficSecrets, UnsupportedOperationError> {
let mut iv_as_vec = vec![0u8; GCM_NONCE_LENGTH];

iv_as_vec.copy_from_slice(iv);
iv_as_vec.copy_from_slice(explicit);
iv_as_vec[..4].copy_from_slice(iv);
iv_as_vec[4..].copy_from_slice(explicit);

Ok(ConnectionTrafficSecrets::Aes128Gcm {
key,
Expand All @@ -80,12 +81,12 @@ impl Tls12AeadAlgorithm for Aes128Gcm {
// We separate the structs for the implementation.
pub struct WCTls12Encrypter {
iv: Iv,
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
}

pub struct WCTls12Decrypter {
implicit_iv: [u8; 4],
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
}

impl MessageEncrypter for WCTls12Encrypter {
Expand Down Expand Up @@ -237,14 +238,14 @@ impl MessageDecrypter for WCTls12Decrypter {
impl Tls13AeadAlgorithm for Aes128Gcm {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
Box::new(WCTls13Cipher {
key: key.as_ref().into(),
key: Zeroizing::new(key.as_ref().into()),
iv,
})
}

fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
Box::new(WCTls13Cipher {
key: key.as_ref().into(),
key: Zeroizing::new(key.as_ref().into()),
iv,
})
}
Expand All @@ -263,7 +264,7 @@ impl Tls13AeadAlgorithm for Aes128Gcm {
}

pub struct WCTls13Cipher {
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
iv: Iv,
}

Expand Down
19 changes: 10 additions & 9 deletions rustls-wolfcrypt-provider/src/aead/aes256gcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use rustls::crypto::cipher::{
UnsupportedOperationError,
};
use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
use zeroize::Zeroizing;

use alloc::vec::Vec;
use core::ptr;
Expand All @@ -30,7 +31,7 @@ impl Tls12AeadAlgorithm for Aes256Gcm {

Box::new(WCTls12Encrypter {
iv: iv_as_array.into(),
key: key_as_slice.to_vec(),
key: Zeroizing::new(key_as_slice.to_vec()),
})
}

Expand All @@ -45,7 +46,7 @@ impl Tls12AeadAlgorithm for Aes256Gcm {

Box::new(WCTls12Decrypter {
implicit_iv: iv_implicit_as_array,
key: key_as_slice.to_vec(),
key: Zeroizing::new(key_as_slice.to_vec()),
})
}

Expand All @@ -65,8 +66,8 @@ impl Tls12AeadAlgorithm for Aes256Gcm {
) -> Result<ConnectionTrafficSecrets, UnsupportedOperationError> {
let mut iv_as_vec = vec![0u8; GCM_NONCE_LENGTH];

iv_as_vec.copy_from_slice(iv);
iv_as_vec.copy_from_slice(explicit);
iv_as_vec[..4].copy_from_slice(iv);
iv_as_vec[4..].copy_from_slice(explicit);

Ok(ConnectionTrafficSecrets::Aes256Gcm {
key,
Expand All @@ -80,12 +81,12 @@ impl Tls12AeadAlgorithm for Aes256Gcm {
// We separate the structs for the implementation.
pub struct WCTls12Encrypter {
iv: Iv,
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
}

pub struct WCTls12Decrypter {
implicit_iv: [u8; 4],
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
}

impl MessageEncrypter for WCTls12Encrypter {
Expand Down Expand Up @@ -237,14 +238,14 @@ impl MessageDecrypter for WCTls12Decrypter {
impl Tls13AeadAlgorithm for Aes256Gcm {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
Box::new(WCTls13Cipher {
key: key.as_ref().into(),
key: Zeroizing::new(key.as_ref().into()),
iv,
})
}

fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
Box::new(WCTls13Cipher {
key: key.as_ref().into(),
key: Zeroizing::new(key.as_ref().into()),
iv,
})
}
Expand All @@ -263,7 +264,7 @@ impl Tls13AeadAlgorithm for Aes256Gcm {
}

pub struct WCTls13Cipher {
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
iv: Iv,
}

Expand Down
13 changes: 7 additions & 6 deletions rustls-wolfcrypt-provider/src/aead/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
use wolfcrypt_rs::*;

use crate::error::check_if_zero;
use zeroize::Zeroizing;

const CHACHAPOLY1305_OVERHEAD: usize = 16;

pub struct Chacha20Poly1305;

impl Tls12AeadAlgorithm for Chacha20Poly1305 {
fn encrypter(&self, key: AeadKey, iv: &[u8], _: &[u8]) -> Box<dyn MessageEncrypter> {
let mut key_as_vec = vec![0u8; 32];
let mut key_as_vec = Zeroizing::new(vec![0u8; 32]);
key_as_vec.copy_from_slice(key.as_ref());

Box::new(WCTls12Cipher {
Expand All @@ -30,7 +31,7 @@ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
}

fn decrypter(&self, key: AeadKey, iv: &[u8]) -> Box<dyn MessageDecrypter> {
let mut key_as_vec = vec![0u8; 32];
let mut key_as_vec = Zeroizing::new(vec![0u8; 32]);
key_as_vec.copy_from_slice(key.as_ref());

Box::new(WCTls12Cipher {
Expand Down Expand Up @@ -63,7 +64,7 @@ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
}

pub struct WCTls12Cipher {
key: Vec<u8>,
key: Zeroizing<Vec<u8>>,
iv: Iv,
}

Expand Down Expand Up @@ -175,7 +176,7 @@ impl MessageDecrypter for WCTls12Cipher {

impl Tls13AeadAlgorithm for Chacha20Poly1305 {
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
let mut key_as_array = [0u8; 32];
let mut key_as_array = Zeroizing::new([0u8; 32]);
key_as_array[..32].copy_from_slice(key.as_ref());

Box::new(WCTls13Cipher {
Expand All @@ -185,7 +186,7 @@ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
}

fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
let mut key_as_array = [0u8; 32];
let mut key_as_array = Zeroizing::new([0u8; 32]);
key_as_array[..32].copy_from_slice(key.as_ref());

Box::new(WCTls13Cipher {
Expand All @@ -208,7 +209,7 @@ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
}

pub struct WCTls13Cipher {
key: [u8; 32],
key: Zeroizing<[u8; 32]>,
iv: Iv,
}

Expand Down
18 changes: 13 additions & 5 deletions rustls-wolfcrypt-provider/src/hash/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,21 @@ impl hash::Context for WCSha256Context {
unsafe impl Sync for WCHasher256 {}
unsafe impl Send for WCHasher256 {}
impl Clone for WCHasher256 {
// Clone implementation.
// Returns a copy of the WCHasher256 struct.
fn clone(&self) -> WCHasher256 {
WCHasher256 {
sha256_c_type: self.sha256_c_type,
let mut new_hasher = WCHasher256 {
sha256_c_type: unsafe { mem::zeroed() },
hash: self.hash,
}
};
let ret = unsafe { wc_InitSha256(&mut new_hasher.sha256_c_type) };
check_if_zero(ret).unwrap();
let ret = unsafe {
wc_Sha256Copy(
&self.sha256_c_type as *const wc_Sha256 as *mut wc_Sha256,
&mut new_hasher.sha256_c_type,
)
};
check_if_zero(ret).unwrap();
new_hasher
}
}

Expand Down
18 changes: 13 additions & 5 deletions rustls-wolfcrypt-provider/src/hash/sha384.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,20 @@ mod tests {
unsafe impl Sync for WCHasher384 {}
unsafe impl Send for WCHasher384 {}
impl Clone for WCHasher384 {
// Clone implementation.
// Returns a copy of the WCHasher256 struct.
fn clone(&self) -> WCHasher384 {
WCHasher384 {
sha384_c_type: self.sha384_c_type,
let mut new_hasher = WCHasher384 {
sha384_c_type: unsafe { mem::zeroed() },
hash: self.hash,
}
};
let ret = unsafe { wc_InitSha384(&mut new_hasher.sha384_c_type) };
check_if_zero(ret).unwrap();
let ret = unsafe {
wc_Sha384Copy(
&self.sha384_c_type as *const wc_Sha384 as *mut wc_Sha384,
&mut new_hasher.sha384_c_type,
)
};
check_if_zero(ret).unwrap();
new_hasher
}
}
21 changes: 11 additions & 10 deletions rustls-wolfcrypt-provider/src/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use wolfcrypt_rs::*;

use crate::error::check_if_zero;
use crate::hmac::WCShaHmac;
use zeroize::Zeroizing;

pub struct WCHkdfUsingHmac(pub WCShaHmac);

Expand Down Expand Up @@ -43,7 +44,7 @@ impl RustlsHkdf for WCHkdfUsingHmac {
check_if_zero(ret).unwrap();

Box::new(WolfHkdfExpander::new(
extracted_key,
Zeroizing::new(extracted_key),
self.0.hash_type().try_into().unwrap(),
self.0.hash_len(),
))
Expand All @@ -54,7 +55,7 @@ impl RustlsHkdf for WCHkdfUsingHmac {
okm: &rustls::crypto::tls13::OkmBlock,
) -> Box<dyn rustls::crypto::tls13::HkdfExpander> {
Box::new(WolfHkdfExpander {
extracted_key: okm.as_ref().to_vec(),
extracted_key: Zeroizing::new(okm.as_ref().to_vec()),
hash_type: self.0.hash_type().try_into().unwrap(),
hash_len: self.0.hash_len(),
})
Expand Down Expand Up @@ -85,21 +86,20 @@ impl RustlsHkdf for WCHkdfUsingHmac {
check_if_zero(ret).unwrap();

unsafe { wc_HmacFree(&mut hmac_ctx) };
check_if_zero(ret).unwrap();

rustls::crypto::hmac::Tag::new(&hmac)
}
}

/// Expander implementation that holds the extracted key material from HKDF extract phase
struct WolfHkdfExpander {
extracted_key: Vec<u8>, // The pseudorandom key (PRK) output from HKDF-Extract
hash_type: i32, // The wolfSSL hash algorithm identifier
hash_len: usize, // Length of the hash function output
extracted_key: Zeroizing<Vec<u8>>, // The pseudorandom key (PRK) output from HKDF-Extract
hash_type: i32, // The wolfSSL hash algorithm identifier
hash_len: usize, // Length of the hash function output
}

impl WolfHkdfExpander {
fn new(extracted_key: Vec<u8>, hash_type: i32, hash_len: usize) -> Self {
fn new(extracted_key: Zeroizing<Vec<u8>>, hash_type: i32, hash_len: usize) -> Self {
Self {
extracted_key,
hash_type,
Expand All @@ -120,7 +120,7 @@ impl tls13::HkdfExpander for WolfHkdfExpander {
return Err(tls13::OutputLengthError);
}

unsafe {
let ret = unsafe {
wc_HKDF_Expand(
self.hash_type,
self.extracted_key.as_ptr(),
Expand All @@ -129,8 +129,9 @@ impl tls13::HkdfExpander for WolfHkdfExpander {
info_concat.len() as u32,
output.as_mut_ptr(),
output.len() as u32,
);
}
)
};
check_if_zero(ret).map_err(|_| tls13::OutputLengthError)?;

Ok(())
}
Expand Down
Loading
Loading