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