Skip to main content

blake2/
lib.rs

1#![no_std]
2#![doc = include_str!("../README.md")]
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
5    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
6)]
7#![cfg_attr(docsrs, feature(doc_cfg))]
8
9pub use digest::{self, Digest};
10
11use core::{fmt, marker::PhantomData, ops::Div};
12use digest::{
13    CustomizedInit, FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update,
14    array::{Array, ArraySize},
15    block_api::{
16        AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, OutputSizeUser, TruncSide,
17        UpdateCore, VariableOutputCore, VariableOutputCoreCustomized,
18    },
19    block_buffer::{Lazy, LazyBuffer},
20    common::{InvalidLength, Key, KeyInit, KeySizeUser},
21    consts::{U4, U16, U32, U64, U128},
22    typenum::{IsLessOrEqual, True, Unsigned},
23};
24#[cfg(feature = "reset")]
25use digest::{FixedOutputReset, Reset};
26
27#[cfg(feature = "zeroize")]
28use digest::zeroize::{Zeroize, ZeroizeOnDrop};
29
30mod consts;
31
32mod simd;
33
34#[macro_use]
35mod macros;
36
37use consts::{BLAKE2B_IV, BLAKE2S_IV};
38use simd::{u32x4, u64x4};
39
40blake2_impl!(
41    Blake2bVarCore,
42    "Blake2b",
43    u64,
44    u64x4,
45    U64,
46    U128,
47    32,
48    24,
49    16,
50    63,
51    BLAKE2B_IV,
52    "Blake2b instance with a variable output.",
53    "Blake2b instance with a fixed output.",
54);
55
56digest::buffer_ct_variable!(
57    /// BLAKE2b generic over output size.
58    pub struct Blake2b<OutSize>(Blake2bVarCore);
59    exclude: SerializableState;
60    max_size: U64;
61);
62
63// TODO: impl in the macro
64impl<OutSize> CustomizedInit for Blake2b<OutSize>
65where
66    OutSize: ArraySize + IsLessOrEqual<U64, Output = True>,
67{
68    fn new_customized(customization: &[u8]) -> Self {
69        Self {
70            core: CustomizedInit::new_customized(customization),
71            buffer: Default::default(),
72        }
73    }
74}
75
76/// BLAKE2b-128 hasher state.
77pub type Blake2b128 = Blake2b<U16>;
78/// BLAKE2b-256 hasher state.
79pub type Blake2b256 = Blake2b<U32>;
80/// BLAKE2b-512 hasher state.
81pub type Blake2b512 = Blake2b<U64>;
82
83blake2_mac_impl!(Blake2bMac, Blake2bVarCore, U64, "Blake2b MAC function");
84
85/// BLAKE2b-512 MAC state.
86pub type Blake2bMac512 = Blake2bMac<U64>;
87
88blake2_impl!(
89    Blake2sVarCore,
90    "Blake2s",
91    u32,
92    u32x4,
93    U32,
94    U64,
95    16,
96    12,
97    8,
98    7,
99    BLAKE2S_IV,
100    "Blake2s instance with a variable output.",
101    "Blake2s instance with a fixed output.",
102);
103
104digest::buffer_ct_variable!(
105    /// BLAKE2s generic over output size.
106    pub struct Blake2s<OutSize>(Blake2sVarCore);
107    exclude: SerializableState;
108    max_size: U32;
109);
110
111// TODO: impl in the macro
112impl<OutSize> CustomizedInit for Blake2s<OutSize>
113where
114    OutSize: ArraySize + IsLessOrEqual<U32, Output = True>,
115{
116    fn new_customized(customization: &[u8]) -> Self {
117        Self {
118            core: CustomizedInit::new_customized(customization),
119            buffer: Default::default(),
120        }
121    }
122}
123
124/// BLAKE2s-128 hasher state.
125pub type Blake2s128 = Blake2s<U16>;
126/// BLAKE2s-256 hasher state.
127pub type Blake2s256 = Blake2s<U32>;
128
129blake2_mac_impl!(Blake2sMac, Blake2sVarCore, U32, "Blake2s MAC function");
130
131/// BLAKE2s-256 MAC state.
132pub type Blake2sMac256 = Blake2sMac<U32>;