#[cfg(test)]
mod tests;
use crate::platform::{le_bytes_from_words_32, words_from_le_bytes_32};
use crate::{
BLOCK_LEN, CHUNK_END, CHUNK_LEN, CHUNK_START, CVWords, DERIVE_KEY_CONTEXT, DERIVE_KEY_MATERIAL,
IV, KEY_LEN, KEYED_HASH, OUT_LEN, ROOT,
};
use blake3::platform::Platform;
#[inline(always)]
fn hash_chunk(input: &[u8], key: CVWords, flags: u8) -> Option<[u8; OUT_LEN]> {
if input.len() > CHUNK_LEN {
return None;
}
let mut cv = key;
let platform = Platform::detect();
let (blocks, remainder) = input.as_chunks();
let num_blocks = blocks.len() + (!remainder.is_empty()) as usize;
for (block_index, block) in blocks.iter().enumerate() {
let mut block_flags = flags;
if block_index == 0 {
block_flags |= CHUNK_START;
}
if block_index + 1 == num_blocks {
block_flags |= CHUNK_END | ROOT;
}
platform.compress_in_place(&mut cv, block, BLOCK_LEN as u8, 0, block_flags);
}
if !remainder.is_empty() || num_blocks == 0 {
let mut block_flags = flags | CHUNK_END | ROOT;
if num_blocks <= 1 {
block_flags |= CHUNK_START;
}
let mut buf = [0; BLOCK_LEN];
buf[..remainder.len()].copy_from_slice(remainder);
platform.compress_in_place(&mut cv, &buf, remainder.len() as u8, 0, block_flags);
}
Some(*le_bytes_from_words_32(&cv))
}
#[inline]
#[cfg_attr(feature = "no-panic", no_panic::no_panic)]
pub fn single_chunk_hash(input: &[u8]) -> Option<[u8; OUT_LEN]> {
hash_chunk(input, *IV, 0)
}
#[inline]
#[cfg_attr(feature = "no-panic", no_panic::no_panic)]
pub fn single_chunk_keyed_hash(key: &[u8; KEY_LEN], input: &[u8]) -> Option<[u8; OUT_LEN]> {
let key_words = words_from_le_bytes_32(key);
hash_chunk(input, key_words, KEYED_HASH)
}
#[inline]
#[cfg_attr(feature = "no-panic", no_panic::no_panic)]
pub fn single_chunk_derive_key(context: &str, key_material: &[u8]) -> Option<[u8; OUT_LEN]> {
let context_key = hash_chunk(context.as_bytes(), *IV, DERIVE_KEY_CONTEXT)?;
let context_key_words = words_from_le_bytes_32(&context_key);
hash_chunk(key_material, context_key_words, DERIVE_KEY_MATERIAL)
}