Skip to content

Commit 69d3bc3

Browse files
Rust wrapper: check for NUL-terminated slice in ECC::rs_hex_to_sig
Fixes F-3092
1 parent 52c867e commit 69d3bc3

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 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[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;

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,3 +342,24 @@ fn test_ecc_import() {
342342
ECC::import_raw(qx, qy, d, b"SECP256R1\0", None, None).expect("Error with import_raw()");
343343
ECC::import_raw_ex(qx, qy, d, ECC::SECP256R1, None, None).expect("Error with import_raw_ex()");
344344
}
345+
346+
#[test]
347+
fn test_ecc_rs_hex_to_sig_not_null_terminated() {
348+
let r_hex = b"AABB\0";
349+
let s_hex = b"CCDD\0";
350+
let r_hex_no_nul = b"AABB";
351+
let s_hex_no_nul = b"CCDD";
352+
let mut sig_out = [0u8; 128];
353+
354+
// Both null-terminated should succeed
355+
assert!(ECC::rs_hex_to_sig(r_hex, s_hex, &mut sig_out).is_ok());
356+
357+
// r not null-terminated should fail
358+
assert!(ECC::rs_hex_to_sig(r_hex_no_nul, s_hex, &mut sig_out).is_err());
359+
360+
// s not null-terminated should fail
361+
assert!(ECC::rs_hex_to_sig(r_hex, s_hex_no_nul, &mut sig_out).is_err());
362+
363+
// Both not null-terminated should fail
364+
assert!(ECC::rs_hex_to_sig(r_hex_no_nul, s_hex_no_nul, &mut sig_out).is_err());
365+
}

0 commit comments

Comments
 (0)