Skip to content

Commit 09c7fac

Browse files
Rust wrapper: address Copilot review comments
1 parent e2d09f3 commit 09c7fac

4 files changed

Lines changed: 30 additions & 5 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1219,7 +1219,7 @@ impl ECC {
12191219
/// }
12201220
/// ```
12211221
pub fn rs_hex_to_sig(r: &[u8], s: &[u8], dout: &mut [u8]) -> Result<usize, i32> {
1222-
if r[r.len() - 1] != 0 || s[s.len() - 1] != 0 {
1222+
if r.is_empty() || s.is_empty() || r[r.len() - 1] != 0 || s[s.len() - 1] != 0 {
12231223
return Err(sys::wolfCrypt_ErrorCodes_BAD_FUNC_ARG);
12241224
}
12251225
let mut dout_size = crate::buffer_len_to_u32(dout.len())?;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,15 @@ pub mod hmac_mac;
6262
pub mod kdf;
6363
pub mod lms;
6464
pub mod mlkem;
65-
#[cfg(feature = "kem")]
65+
#[cfg(all(feature = "kem", mlkem))]
6666
pub mod mlkem_kem;
6767
pub mod prf;
6868
pub mod random;
6969
pub mod rsa;
7070
#[cfg(feature = "signature")]
7171
pub mod rsa_pkcs1v15;
7272
pub mod sha;
73-
#[cfg(feature = "password-hash")]
73+
#[cfg(all(feature = "password-hash", hmac, kdf_pbkdf2))]
7474
pub mod pbkdf2_password_hash;
7575
#[cfg(feature = "digest")]
7676
pub mod sha_digest;

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ impl TryFrom<&PasswordHash> for Params {
148148

149149
let output_len = if let Some(ref h) = hash.hash {
150150
h.len()
151-
} else if let Some(l) = hash.params.get_decimal("l") {
151+
} else if let Some(l) = hash.params.get_decimal("l") &&
152+
0 < l && (l as usize) <= Output::MAX_LENGTH {
152153
l as usize
153154
} else {
154155
return Err(Error::ParamInvalid { name: "l" });
@@ -216,7 +217,7 @@ impl password_hash::CustomizedPasswordHasher<PasswordHash> for Pbkdf2 {
216217
None => self.algorithm,
217218
};
218219

219-
if params.rounds < MIN_ROUNDS {
220+
if params.rounds < MIN_ROUNDS || params.output_len > Output::MAX_LENGTH {
220221
return Err(Error::ParamInvalid { name: "i" });
221222
}
222223

wrapper/rust/wolfssl-wolfcrypt/tests/test_ecc.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use wolfssl_wolfcrypt::random::RNG;
1111
#[test]
1212
#[cfg(random)]
1313
fn test_ecc_generate() {
14+
common::setup();
15+
1416
let mut rng = RNG::new().expect("Failed to create RNG");
1517
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
1618
ecc.check().expect("Error with check()");
@@ -19,6 +21,8 @@ fn test_ecc_generate() {
1921
#[test]
2022
#[cfg(random)]
2123
fn test_ecc_generate_ex() {
24+
common::setup();
25+
2226
let mut rng = RNG::new().expect("Failed to create RNG");
2327
let curve_id = ECC::SECP256R1;
2428
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -30,6 +34,8 @@ fn test_ecc_generate_ex() {
3034
#[test]
3135
#[cfg(all(ecc_import, ecc_export, random))]
3236
fn test_ecc_import_x963() {
37+
common::setup();
38+
3339
let mut rng = RNG::new().expect("Failed to create RNG");
3440
let curve_id = ECC::SECP256R1;
3541
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -47,6 +53,8 @@ fn test_ecc_import_x963() {
4753
#[test]
4854
#[cfg(random)]
4955
fn test_ecc_generate_ex2() {
56+
common::setup();
57+
5058
let mut rng = RNG::new().expect("Failed to create RNG");
5159
let curve_id = ECC::SECP256R1;
5260
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -58,6 +66,8 @@ fn test_ecc_generate_ex2() {
5866
#[test]
5967
#[cfg(all(ecc_import, ecc_export, ecc_sign, ecc_verify, random))]
6068
fn test_ecc_import_export_sign_verify() {
69+
common::setup();
70+
6171
let mut rng = RNG::new().expect("Failed to create RNG");
6272
let key_path = "../../../certs/ecc-client-key.der";
6373
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
@@ -242,6 +252,8 @@ fn test_ecc_import_export_private_ex() {
242252
#[test]
243253
#[cfg(all(ecc_export, random))]
244254
fn test_ecc_export_public() {
255+
common::setup();
256+
245257
let mut rng = RNG::new().expect("Failed to create RNG");
246258
let mut ecc = ECC::generate(32, &mut rng, None, None).expect("Error with generate()");
247259
let mut qx = [0u8; 32];
@@ -281,6 +293,8 @@ fn test_ecc_import_unsigned() {
281293
#[test]
282294
#[cfg(random)]
283295
fn test_ecc_make_pub() {
296+
common::setup();
297+
284298
let mut rng = RNG::new().expect("Failed to create RNG");
285299
let key_path = "../../../certs/ecc-client-key.der";
286300
let der: Vec<u8> = fs::read(key_path).expect("Error reading key file");
@@ -294,6 +308,8 @@ fn test_ecc_make_pub() {
294308
#[test]
295309
#[cfg(all(ecc_export, random))]
296310
fn test_ecc_point() {
311+
common::setup();
312+
297313
let mut rng = RNG::new().expect("Failed to create RNG");
298314
let curve_id = ECC::SECP256R1;
299315
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -308,6 +324,8 @@ fn test_ecc_point() {
308324
#[test]
309325
#[cfg(all(all(ecc_import, ecc_export, random)))]
310326
fn test_ecc_point_import() {
327+
common::setup();
328+
311329
let mut rng = RNG::new().expect("Failed to create RNG");
312330
let curve_id = ECC::SECP256R1;
313331
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -323,6 +341,8 @@ fn test_ecc_point_import() {
323341
#[test]
324342
#[cfg(all(ecc_import, ecc_export, ecc_comp_key, random))]
325343
fn test_ecc_point_import_compressed() {
344+
common::setup();
345+
326346
let mut rng = RNG::new().expect("Failed to create RNG");
327347
let curve_id = ECC::SECP256R1;
328348
let curve_size = ECC::get_curve_size_from_id(curve_id).expect("Error with get_curve_size_from_id()");
@@ -336,6 +356,8 @@ fn test_ecc_point_import_compressed() {
336356
#[test]
337357
#[cfg(ecc_import)]
338358
fn test_ecc_import() {
359+
common::setup();
360+
339361
let qx = b"7a4e287890a1a47ad3457e52f2f76a83ce46cbc947616d0cbaa82323818a793d\0";
340362
let qy = b"eec4084f5b29ebf29c44cce3b3059610922f8b30ea6e8811742ac7238fe87308\0";
341363
let d = b"8c14b793cb19137e323a6d2e2a870bca2e7a493ec1153b3a95feb8a4873f8d08\0";
@@ -345,6 +367,8 @@ fn test_ecc_import() {
345367

346368
#[test]
347369
fn test_ecc_rs_hex_to_sig_not_null_terminated() {
370+
common::setup();
371+
348372
let r_hex = b"AABB\0";
349373
let s_hex = b"CCDD\0";
350374
let r_hex_no_nul = b"AABB";

0 commit comments

Comments
 (0)