Skip to main content

libcrux_blake2/impl_hacl/
mod.rs

1extern crate alloc;
2
3mod blake2b;
4mod blake2s;
5mod error;
6mod lengths;
7
8pub use blake2b::{Blake2b, Blake2bBuilder};
9pub use blake2s::{Blake2s, Blake2sBuilder};
10pub use error::Error;
11pub use lengths::{SupportsKeyLen, SupportsOutLen};
12
13/// Type that holds the constants in case both key length and digest length are known at compile
14/// time.
15pub struct ConstKeyLenConstDigestLen<const KEY_LEN: usize, const OUT_LEN: usize>;
16
17/// Type that holds the constant in case just the key length is known at compile time.
18pub struct ConstKeyLen<const KEY_LEN: usize>;
19
20/// Type that holds the constant in case just the digest length is known at compile time.
21pub struct ConstDigestLen<const OUT_LEN: usize>;
22
23/// Type that holds the constant in case neither the key length nor the digest length is known at
24/// compile time.
25pub struct Dynamic;
26
27/// Type that is used to bound the implementations to valid key and output lengths.
28///
29/// We implement [`SupportsKeyLen`] and [`SupportsOutLen`] for [`Blake2s<LengthBounds>`] and
30/// [`Blake2b<LengthBounds>`] with the appropriate lengths. This can be used by callers to bound
31/// their own implementations to only accept valid lengths.
32///
33/// # Example
34///
35/// ```
36/// use libcrux_blake2::{Blake2b, Blake2bBuilder, LengthBounds, SupportsKeyLen, SupportsOutLen};
37///
38/// // A function that does a keyed Blake2b and only accepts valid key lengths
39/// fn keyed_hash<const KEY_LEN: usize>(key: &[u8; KEY_LEN], msg: &[u8]) -> [u8; 32]
40/// where
41///     Blake2b<LengthBounds>: SupportsKeyLen<KEY_LEN>,
42/// {
43///     let mut hasher = Blake2bBuilder::new_keyed_const(key)
44///         .build_const_digest_len::<32>();
45///     hasher.update(msg);
46///     let mut output = [0u8; 32];
47///     hasher.finalize(&mut output);
48///     output
49/// }
50///
51/// // This compiles because Blake2b supports 32 byte keys
52/// let key = [0; 32]; // this should actually be random
53/// let msg = b"a test message";
54/// let result = keyed_hash(&key, msg);
55///
56/// ```
57pub struct LengthBounds;