Skip to content

Commit 5c10fe9

Browse files
Rust wrapper: BLAKE2: check for non-empty hash buffer in finalize()
This is related to F-1070 but not the same. We do not need to check that hash_size being passed in matches the initialized digest size because the C function will use the passed-in size as long as it is non-zero.
1 parent 56c20ae commit 5c10fe9

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ impl BLAKE2b {
157157
/// ```
158158
pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> {
159159
let hash_size = hash.len() as u32;
160+
if hash_size == 0 {
161+
// The C function uses the internal state configured digest size
162+
// if hash_size is passed in as 0. We do not want to allow a
163+
// buffer overrun, so do not allow an empty hash buffer here.
164+
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
165+
}
160166
let rc = unsafe {
161167
sys::wc_Blake2bFinal(&mut self.wc_blake2b, hash.as_mut_ptr(), hash_size)
162168
};
@@ -434,6 +440,12 @@ impl BLAKE2s {
434440
/// ```
435441
pub fn finalize(&mut self, hash: &mut [u8]) -> Result<(), i32> {
436442
let hash_size = hash.len() as u32;
443+
if hash_size == 0 {
444+
// The C function uses the internal state configured digest size
445+
// if hash_size is passed in as 0. We do not want to allow a
446+
// buffer overrun, so do not allow an empty hash buffer here.
447+
return Err(sys::wolfCrypt_ErrorCodes_BUFFER_E);
448+
}
437449
let rc = unsafe {
438450
sys::wc_Blake2sFinal(&mut self.wc_blake2s, hash.as_mut_ptr(), hash_size)
439451
};

0 commit comments

Comments
 (0)