Skip to content

Commit

Permalink
Add ANSI-X9.63-KDF support: use alloc_zeroed for input overflow test …
Browse files Browse the repository at this point in the history
…case
  • Loading branch information
nemynm committed Oct 5, 2024
1 parent 33c8994 commit 0a9087e
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions ansi-x963-kdf/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,16 +190,15 @@ fn test_errors() {
// other_info has a length that causes input overflow.
#[cfg(target_pointer_width = "64")]
{
let hashmaxlen = Sha224::output_size() * (u32::MAX as usize);
// Secret
let secret = [0u8; 42]; // Empty secret
// Calculate the required length for other_info to cause an input overflow.
let other_info_len = hashmaxlen - secret.len() - 4;
let secret = [0u8; 42];
// Calculate the required length for other_info to cause an input overflow.
let other_info_len = Sha224::output_size() * (u32::MAX as usize) - secret.len() - 4;
// Create a layout for allocation.
let layout = std::alloc::Layout::from_size_align(other_info_len, 1).unwrap();
unsafe {
// Allocate memory without initializing.
let p = std::alloc::alloc(layout);
// We assume that OS will not allocate physical memory for this buffer
let p = std::alloc::alloc_zeroed(layout);
if p.is_null() {
panic!("Failed to allocate memory");
}
Expand All @@ -220,11 +219,10 @@ fn test_errors() {

// Create a slice from the allocated memory.
let other_info = std::slice::from_raw_parts(p, other_info_len);

// Attempt to derive the key, which should result in an InputOverflow error.
let res = ansi_x963_kdf::derive_key_into::<Sha224>(&secret, other_info, &mut [0u8; 42]);

assert_eq!(res, Err(ansi_x963_kdf::Error::InputOverflow));
assert_eq!(
ansi_x963_kdf::derive_key_into::<Sha224>(&secret, other_info, &mut [0u8; 42]),
Err(ansi_x963_kdf::Error::InputOverflow)
);
}
}

Expand Down

0 comments on commit 0a9087e

Please sign in to comment.