Skip to content

Commit 478a4ad

Browse files
authored
Merge pull request #26 from gasbytes/2025-03-18-varios-fixes
Various fixes reported from analyzer
2 parents dfcdbfd + b7765d0 commit 478a4ad

25 files changed

Lines changed: 333 additions & 230 deletions

File tree

.github/workflows/macos-build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ jobs:
5656
- name: Run clippy
5757
run: |
5858
cd wolfcrypt-rs
59-
cargo clippy -- -D warnings
59+
cargo clippy -- -D warnings -A unnecessary-transmutes
6060
cd ../rustls-wolfcrypt-provider
6161
cargo clippy -- -D warnings

rustls-wolfcrypt-provider/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ anyhow = "1.0.95"
3131
num_cpus = "1.16.0"
3232
lazy_static = "1.5.0"
3333
hex-literal = "0.4.1"
34+
zeroize = { version = "1", default-features = false, features = ["alloc", "derive"] }
3435

3536

3637
[dev-dependencies]

rustls-wolfcrypt-provider/src/aead/aes128gcm.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustls::crypto::cipher::{
1111
UnsupportedOperationError,
1212
};
1313
use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
14+
use zeroize::Zeroizing;
1415

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

3132
Box::new(WCTls12Encrypter {
3233
iv: iv_as_array.into(),
33-
key: key_as_slice.to_vec(),
34+
key: Zeroizing::new(key_as_slice.to_vec()),
3435
})
3536
}
3637

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

4647
Box::new(WCTls12Decrypter {
4748
implicit_iv: iv_implicit_as_array,
48-
key: key_as_slice.to_vec(),
49+
key: Zeroizing::new(key_as_slice.to_vec()),
4950
})
5051
}
5152

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

68-
iv_as_vec.copy_from_slice(iv);
69-
iv_as_vec.copy_from_slice(explicit);
69+
iv_as_vec[..4].copy_from_slice(iv);
70+
iv_as_vec[4..].copy_from_slice(explicit);
7071

7172
Ok(ConnectionTrafficSecrets::Aes128Gcm {
7273
key,
@@ -80,12 +81,12 @@ impl Tls12AeadAlgorithm for Aes128Gcm {
8081
// We separate the structs for the implementation.
8182
pub struct WCTls12Encrypter {
8283
iv: Iv,
83-
key: Vec<u8>,
84+
key: Zeroizing<Vec<u8>>,
8485
}
8586

8687
pub struct WCTls12Decrypter {
8788
implicit_iv: [u8; 4],
88-
key: Vec<u8>,
89+
key: Zeroizing<Vec<u8>>,
8990
}
9091

9192
impl MessageEncrypter for WCTls12Encrypter {
@@ -237,14 +238,14 @@ impl MessageDecrypter for WCTls12Decrypter {
237238
impl Tls13AeadAlgorithm for Aes128Gcm {
238239
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
239240
Box::new(WCTls13Cipher {
240-
key: key.as_ref().into(),
241+
key: Zeroizing::new(key.as_ref().into()),
241242
iv,
242243
})
243244
}
244245

245246
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
246247
Box::new(WCTls13Cipher {
247-
key: key.as_ref().into(),
248+
key: Zeroizing::new(key.as_ref().into()),
248249
iv,
249250
})
250251
}
@@ -263,7 +264,7 @@ impl Tls13AeadAlgorithm for Aes128Gcm {
263264
}
264265

265266
pub struct WCTls13Cipher {
266-
key: Vec<u8>,
267+
key: Zeroizing<Vec<u8>>,
267268
iv: Iv,
268269
}
269270

rustls-wolfcrypt-provider/src/aead/aes256gcm.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustls::crypto::cipher::{
1111
UnsupportedOperationError,
1212
};
1313
use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
14+
use zeroize::Zeroizing;
1415

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

3132
Box::new(WCTls12Encrypter {
3233
iv: iv_as_array.into(),
33-
key: key_as_slice.to_vec(),
34+
key: Zeroizing::new(key_as_slice.to_vec()),
3435
})
3536
}
3637

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

4647
Box::new(WCTls12Decrypter {
4748
implicit_iv: iv_implicit_as_array,
48-
key: key_as_slice.to_vec(),
49+
key: Zeroizing::new(key_as_slice.to_vec()),
4950
})
5051
}
5152

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

68-
iv_as_vec.copy_from_slice(iv);
69-
iv_as_vec.copy_from_slice(explicit);
69+
iv_as_vec[..4].copy_from_slice(iv);
70+
iv_as_vec[4..].copy_from_slice(explicit);
7071

7172
Ok(ConnectionTrafficSecrets::Aes256Gcm {
7273
key,
@@ -80,12 +81,12 @@ impl Tls12AeadAlgorithm for Aes256Gcm {
8081
// We separate the structs for the implementation.
8182
pub struct WCTls12Encrypter {
8283
iv: Iv,
83-
key: Vec<u8>,
84+
key: Zeroizing<Vec<u8>>,
8485
}
8586

8687
pub struct WCTls12Decrypter {
8788
implicit_iv: [u8; 4],
88-
key: Vec<u8>,
89+
key: Zeroizing<Vec<u8>>,
8990
}
9091

9192
impl MessageEncrypter for WCTls12Encrypter {
@@ -237,14 +238,14 @@ impl MessageDecrypter for WCTls12Decrypter {
237238
impl Tls13AeadAlgorithm for Aes256Gcm {
238239
fn encrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageEncrypter> {
239240
Box::new(WCTls13Cipher {
240-
key: key.as_ref().into(),
241+
key: Zeroizing::new(key.as_ref().into()),
241242
iv,
242243
})
243244
}
244245

245246
fn decrypter(&self, key: AeadKey, iv: Iv) -> Box<dyn MessageDecrypter> {
246247
Box::new(WCTls13Cipher {
247-
key: key.as_ref().into(),
248+
key: Zeroizing::new(key.as_ref().into()),
248249
iv,
249250
})
250251
}
@@ -263,7 +264,7 @@ impl Tls13AeadAlgorithm for Aes256Gcm {
263264
}
264265

265266
pub struct WCTls13Cipher {
266-
key: Vec<u8>,
267+
key: Zeroizing<Vec<u8>>,
267268
iv: Iv,
268269
}
269270

rustls-wolfcrypt-provider/src/aead/chacha20.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ use rustls::{ConnectionTrafficSecrets, ContentType, ProtocolVersion};
1313
use wolfcrypt_rs::*;
1414

1515
use crate::error::check_if_zero;
16+
use zeroize::Zeroizing;
1617

1718
const CHACHAPOLY1305_OVERHEAD: usize = 16;
1819

1920
pub struct Chacha20Poly1305;
2021

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

2627
Box::new(WCTls12Cipher {
@@ -30,7 +31,7 @@ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
3031
}
3132

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

3637
Box::new(WCTls12Cipher {
@@ -63,7 +64,7 @@ impl Tls12AeadAlgorithm for Chacha20Poly1305 {
6364
}
6465

6566
pub struct WCTls12Cipher {
66-
key: Vec<u8>,
67+
key: Zeroizing<Vec<u8>>,
6768
iv: Iv,
6869
}
6970

@@ -175,7 +176,7 @@ impl MessageDecrypter for WCTls12Cipher {
175176

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

181182
Box::new(WCTls13Cipher {
@@ -185,7 +186,7 @@ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
185186
}
186187

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

191192
Box::new(WCTls13Cipher {
@@ -208,7 +209,7 @@ impl Tls13AeadAlgorithm for Chacha20Poly1305 {
208209
}
209210

210211
pub struct WCTls13Cipher {
211-
key: [u8; 32],
212+
key: Zeroizing<[u8; 32]>,
212213
iv: Iv,
213214
}
214215

rustls-wolfcrypt-provider/src/hash/sha256.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,21 @@ impl hash::Context for WCSha256Context {
9191
unsafe impl Sync for WCHasher256 {}
9292
unsafe impl Send for WCHasher256 {}
9393
impl Clone for WCHasher256 {
94-
// Clone implementation.
95-
// Returns a copy of the WCHasher256 struct.
9694
fn clone(&self) -> WCHasher256 {
97-
WCHasher256 {
98-
sha256_c_type: self.sha256_c_type,
95+
let mut new_hasher = WCHasher256 {
96+
sha256_c_type: unsafe { mem::zeroed() },
9997
hash: self.hash,
100-
}
98+
};
99+
let ret = unsafe { wc_InitSha256(&mut new_hasher.sha256_c_type) };
100+
check_if_zero(ret).unwrap();
101+
let ret = unsafe {
102+
wc_Sha256Copy(
103+
&self.sha256_c_type as *const wc_Sha256 as *mut wc_Sha256,
104+
&mut new_hasher.sha256_c_type,
105+
)
106+
};
107+
check_if_zero(ret).unwrap();
108+
new_hasher
101109
}
102110
}
103111

rustls-wolfcrypt-provider/src/hash/sha384.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,20 @@ mod tests {
109109
unsafe impl Sync for WCHasher384 {}
110110
unsafe impl Send for WCHasher384 {}
111111
impl Clone for WCHasher384 {
112-
// Clone implementation.
113-
// Returns a copy of the WCHasher256 struct.
114112
fn clone(&self) -> WCHasher384 {
115-
WCHasher384 {
116-
sha384_c_type: self.sha384_c_type,
113+
let mut new_hasher = WCHasher384 {
114+
sha384_c_type: unsafe { mem::zeroed() },
117115
hash: self.hash,
118-
}
116+
};
117+
let ret = unsafe { wc_InitSha384(&mut new_hasher.sha384_c_type) };
118+
check_if_zero(ret).unwrap();
119+
let ret = unsafe {
120+
wc_Sha384Copy(
121+
&self.sha384_c_type as *const wc_Sha384 as *mut wc_Sha384,
122+
&mut new_hasher.sha384_c_type,
123+
)
124+
};
125+
check_if_zero(ret).unwrap();
126+
new_hasher
119127
}
120128
}

rustls-wolfcrypt-provider/src/hkdf.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use wolfcrypt_rs::*;
77

88
use crate::error::check_if_zero;
99
use crate::hmac::WCShaHmac;
10+
use zeroize::Zeroizing;
1011

1112
pub struct WCHkdfUsingHmac(pub WCShaHmac);
1213

@@ -43,7 +44,7 @@ impl RustlsHkdf for WCHkdfUsingHmac {
4344
check_if_zero(ret).unwrap();
4445

4546
Box::new(WolfHkdfExpander::new(
46-
extracted_key,
47+
Zeroizing::new(extracted_key),
4748
self.0.hash_type().try_into().unwrap(),
4849
self.0.hash_len(),
4950
))
@@ -54,7 +55,7 @@ impl RustlsHkdf for WCHkdfUsingHmac {
5455
okm: &rustls::crypto::tls13::OkmBlock,
5556
) -> Box<dyn rustls::crypto::tls13::HkdfExpander> {
5657
Box::new(WolfHkdfExpander {
57-
extracted_key: okm.as_ref().to_vec(),
58+
extracted_key: Zeroizing::new(okm.as_ref().to_vec()),
5859
hash_type: self.0.hash_type().try_into().unwrap(),
5960
hash_len: self.0.hash_len(),
6061
})
@@ -85,21 +86,20 @@ impl RustlsHkdf for WCHkdfUsingHmac {
8586
check_if_zero(ret).unwrap();
8687

8788
unsafe { wc_HmacFree(&mut hmac_ctx) };
88-
check_if_zero(ret).unwrap();
8989

9090
rustls::crypto::hmac::Tag::new(&hmac)
9191
}
9292
}
9393

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

101101
impl WolfHkdfExpander {
102-
fn new(extracted_key: Vec<u8>, hash_type: i32, hash_len: usize) -> Self {
102+
fn new(extracted_key: Zeroizing<Vec<u8>>, hash_type: i32, hash_len: usize) -> Self {
103103
Self {
104104
extracted_key,
105105
hash_type,
@@ -120,7 +120,7 @@ impl tls13::HkdfExpander for WolfHkdfExpander {
120120
return Err(tls13::OutputLengthError);
121121
}
122122

123-
unsafe {
123+
let ret = unsafe {
124124
wc_HKDF_Expand(
125125
self.hash_type,
126126
self.extracted_key.as_ptr(),
@@ -129,8 +129,9 @@ impl tls13::HkdfExpander for WolfHkdfExpander {
129129
info_concat.len() as u32,
130130
output.as_mut_ptr(),
131131
output.len() as u32,
132-
);
133-
}
132+
)
133+
};
134+
check_if_zero(ret).map_err(|_| tls13::OutputLengthError)?;
134135

135136
Ok(())
136137
}

0 commit comments

Comments
 (0)