Rust wrapper: restrict RNG generic type parameters to be integers

Fixes F-3350
This commit is contained in:
Josh Holtrop
2026-04-27 07:53:39 -04:00
parent 6bb8f8f5cd
commit 81435c8a01
3 changed files with 21 additions and 3 deletions
+16
View File
@@ -22,6 +22,12 @@ dependencies = [
"memchr",
]
[[package]]
name = "autocfg"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8"
[[package]]
name = "base64ct"
version = "1.8.3"
@@ -262,6 +268,15 @@ dependencies = [
"minimal-lexical",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "password-hash"
version = "0.6.1"
@@ -478,6 +493,7 @@ dependencies = [
"digest",
"hybrid-array",
"kem",
"num-traits",
"password-hash",
"rand_core 0.10.0",
"regex",
@@ -27,6 +27,7 @@ aead = { version = "0.5", optional = true, default-features = false }
cipher = { version = "0.5", optional = true, default-features = false }
digest = { version = "0.11", optional = true, default-features = false, features = ["block-api"] }
signature = { version = "2.2", optional = true, default-features = false }
num-traits = { version = "0.2", default-features = false }
zeroize = { version = "1.3", default-features = false, features = ["derive"] }
password-hash = { version = "0.6.1", optional = true, default-features = false }
kem = { version = "0.3", optional = true, default-features = false }
+4 -3
View File
@@ -46,6 +46,7 @@ rng.generate_block(&mut buffer).expect("Failed to generate a block");
use crate::sys;
use core::mem::{size_of_val, MaybeUninit};
use num_traits::PrimInt;
/// A cryptographically secure random number generator based on the wolfSSL
/// library.
@@ -127,7 +128,7 @@ impl RNG {
///
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
/// library return code on failure.
pub fn new_with_nonce<T>(nonce: &mut [T]) -> Result<Self, i32> {
pub fn new_with_nonce<T: PrimInt>(nonce: &mut [T]) -> Result<Self, i32> {
RNG::new_with_nonce_ex(nonce, None, None)
}
@@ -146,7 +147,7 @@ impl RNG {
///
/// A Result which is Ok(RNG) on success or an Err containing the wolfSSL
/// library return code on failure.
pub fn new_with_nonce_ex<T>(nonce: &mut [T], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
pub fn new_with_nonce_ex<T: PrimInt>(nonce: &mut [T], heap: Option<*mut core::ffi::c_void>, dev_id: Option<i32>) -> Result<Self, i32> {
#[cfg(fips)]
{
let rc = unsafe {
@@ -338,7 +339,7 @@ impl RNG {
///
/// A `Result` which is `Ok(())` on success or an `Err` with the wolfssl
/// library return code on failure.
pub fn generate_block<T>(&mut self, buf: &mut [T]) -> Result<(), i32> {
pub fn generate_block<T: PrimInt>(&mut self, buf: &mut [T]) -> Result<(), i32> {
let ptr = buf.as_mut_ptr() as *mut u8;
let size = crate::buffer_len_to_u32(size_of_val(buf))?;
let rc = unsafe { sys::wc_RNG_GenerateBlock(&mut self.wc_rng, ptr, size) };