Skip to content

Commit 4a92ee3

Browse files
Rust HMAC-BLAKE2: require exact output buffer size
1 parent e59ddb9 commit 4a92ee3

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ pub struct BLAKE2bHmac {
176176

177177
#[cfg(blake2b_hmac)]
178178
impl BLAKE2bHmac {
179+
/// HMAC-BLAKE2b digest size.
180+
pub const DIGEST_SIZE: usize = sys::WC_BLAKE2B_DIGEST_SIZE as usize;
181+
179182
/// Build a new BLAKE2bHmac instance.
180183
///
181184
/// # Parameters
@@ -264,7 +267,7 @@ impl BLAKE2bHmac {
264267
/// let mut mac = [0u8; 64];
265268
/// hmac_blake2b.finalize(&key, &mut mac).expect("Error with finalize()");
266269
/// ```
267-
pub fn finalize(&mut self, key: &[u8], mac: &mut [u8]) -> Result<(), i32> {
270+
pub fn finalize(&mut self, key: &[u8], mac: &mut [u8; Self::DIGEST_SIZE]) -> Result<(), i32> {
268271
let rc = unsafe {
269272
sys::wc_Blake2bHmacFinal(&mut self.wc_blake2b,
270273
key.as_ptr(), key.len(), mac.as_mut_ptr(), mac.len())
@@ -282,13 +285,14 @@ impl BLAKE2bHmac {
282285
///
283286
/// * `data`: Input data to create MAC from.
284287
/// * `key`: Key to use for MAC creation.
285-
/// * `out`: Buffer in which to store the computed MAC.
288+
/// * `out`: Buffer in which to store the computed MAC. It must be 64 bytes
289+
/// long.
286290
///
287291
/// # Returns
288292
///
289293
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
290294
/// library error code value.
291-
pub fn hmac(data: &[u8], key: &[u8], out: &mut [u8]) -> Result<(), i32> {
295+
pub fn hmac(data: &[u8], key: &[u8], out: &mut [u8; Self::DIGEST_SIZE]) -> Result<(), i32> {
292296
let rc = unsafe {
293297
sys::wc_Blake2bHmac(data.as_ptr(), data.len(), key.as_ptr(),
294298
key.len(), out.as_mut_ptr(), out.len())
@@ -449,6 +453,9 @@ pub struct BLAKE2sHmac {
449453

450454
#[cfg(blake2s_hmac)]
451455
impl BLAKE2sHmac {
456+
/// HMAC-BLAKE2s digest size.
457+
pub const DIGEST_SIZE: usize = sys::WC_BLAKE2S_DIGEST_SIZE as usize;
458+
452459
/// Build a new BLAKE2sHmac instance.
453460
///
454461
/// # Parameters
@@ -537,7 +544,7 @@ impl BLAKE2sHmac {
537544
/// let mut mac = [0u8; 32];
538545
/// hmac_blake2s.finalize(&key, &mut mac).expect("Error with finalize()");
539546
/// ```
540-
pub fn finalize(&mut self, key: &[u8], mac: &mut [u8]) -> Result<(), i32> {
547+
pub fn finalize(&mut self, key: &[u8], mac: &mut [u8; Self::DIGEST_SIZE]) -> Result<(), i32> {
541548
let rc = unsafe {
542549
sys::wc_Blake2sHmacFinal(&mut self.wc_blake2s,
543550
key.as_ptr(), key.len(), mac.as_mut_ptr(), mac.len())
@@ -555,13 +562,14 @@ impl BLAKE2sHmac {
555562
///
556563
/// * `data`: Input data to create MAC from.
557564
/// * `key`: Key to use for MAC creation.
558-
/// * `out`: Buffer in which to store the computed MAC.
565+
/// * `out`: Buffer in which to store the computed MAC. It must be 32 bytes
566+
/// long.
559567
///
560568
/// # Returns
561569
///
562570
/// Returns either Ok(()) on success or Err(e) containing the wolfSSL
563571
/// library error code value.
564-
pub fn hmac(data: &[u8], key: &[u8], out: &mut [u8]) -> Result<(), i32> {
572+
pub fn hmac(data: &[u8], key: &[u8], out: &mut [u8; Self::DIGEST_SIZE]) -> Result<(), i32> {
565573
let rc = unsafe {
566574
sys::wc_Blake2sHmac(data.as_ptr(), data.len(), key.as_ptr(),
567575
key.len(), out.as_mut_ptr(), out.len())

0 commit comments

Comments
 (0)