blake2/
lib.rs

1//! An implementation of the [BLAKE2][1] hash functions.
2//!
3//! # Usage
4//!
5//! [`Blake2b512`] and [`Blake2s256`] can be used in the following way:
6//!
7//! ```rust
8//! use blake2::{Blake2b512, Blake2s256, Digest};
9//! use hex_literal::hex;
10//!
11//! // create a Blake2b512 object
12//! let mut hasher = Blake2b512::new();
13//!
14//! // write input message
15//! hasher.update(b"hello world");
16//!
17//! // read hash digest and consume hasher
18//! let res = hasher.finalize();
19//! assert_eq!(res[..], hex!("
20//!     021ced8799296ceca557832ab941a50b4a11f83478cf141f51f933f653ab9fbc
21//!     c05a037cddbed06e309bf334942c4e58cdf1a46e237911ccd7fcf9787cbc7fd0
22//! ")[..]);
23//!
24//! // same example for Blake2s256:
25//! let mut hasher = Blake2s256::new();
26//! hasher.update(b"hello world");
27//! let res = hasher.finalize();
28//! assert_eq!(res[..], hex!("
29//!     9aec6806794561107e594b1f6a8a6b0c92a0cba9acf5e5e93cca06f781813b0b
30//! ")[..]);
31//! ```
32//!
33//! Also see [RustCrypto/hashes](https://github.com/RustCrypto/hashes) readme.
34//!
35//! ## Variable output size
36//!
37//! This implementation supports run and compile time variable sizes.
38//!
39//! Run time variable output example:
40//! ```rust
41//! use blake2::Blake2bVar;
42//! use blake2::digest::{Update, VariableOutput};
43//! use hex_literal::hex;
44//!
45//! let mut hasher = Blake2bVar::new(10).unwrap();
46//! hasher.update(b"my_input");
47//! let mut buf = [0u8; 10];
48//! hasher.finalize_variable(&mut buf).unwrap();
49//! assert_eq!(buf, hex!("2cc55c84e416924e6400"));
50//! ```
51//!
52//! Compile time variable output example:
53//! ```rust
54//! use blake2::{Blake2b, Digest, digest::consts::U10};
55//! use hex_literal::hex;
56//!
57//! type Blake2b80 = Blake2b<U10>;
58//!
59//! let mut hasher = Blake2b80::new();
60//! hasher.update(b"my_input");
61//! let res = hasher.finalize();
62//! assert_eq!(res[..], hex!("2cc55c84e416924e6400")[..]);
63//! ```
64//!
65//! # Acknowledgment
66//! Based on the [blake2-rfc][2] crate.
67//!
68//! [1]: https://en.wikipedia.org/wiki/BLAKE_(hash_function)#BLAKE2
69//! [2]: https://github.com/cesarb/blake2-rfc
70
71#![no_std]
72#![doc(
73    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
74    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
75)]
76#![warn(missing_docs, rust_2018_idioms)]
77#![cfg_attr(feature = "simd", feature(platform_intrinsics, repr_simd))]
78#![cfg_attr(feature = "simd", allow(incomplete_features))]
79
80#[cfg(feature = "std")]
81extern crate std;
82
83pub use digest::{self, Digest};
84
85use core::{convert::TryInto, fmt, marker::PhantomData, ops::Div};
86use digest::{
87    block_buffer::{Lazy, LazyBuffer},
88    consts::{U128, U32, U4, U64},
89    core_api::{
90        AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper,
91        CtVariableCoreWrapper, OutputSizeUser, RtVariableCoreWrapper, TruncSide, UpdateCore,
92        VariableOutputCore,
93    },
94    crypto_common::{InvalidLength, Key, KeyInit, KeySizeUser},
95    generic_array::{ArrayLength, GenericArray},
96    typenum::{IsLessOrEqual, LeEq, NonZero, Unsigned},
97    FixedOutput, HashMarker, InvalidOutputSize, MacMarker, Output, Update,
98};
99#[cfg(feature = "reset")]
100use digest::{FixedOutputReset, Reset};
101
102mod as_bytes;
103mod consts;
104
105mod simd;
106
107#[macro_use]
108mod macros;
109
110use as_bytes::AsBytes;
111use consts::{BLAKE2B_IV, BLAKE2S_IV};
112use simd::{u32x4, u64x4, Vector4};
113
114blake2_impl!(
115    Blake2bVarCore,
116    "Blake2b",
117    u64,
118    u64x4,
119    U64,
120    U128,
121    32,
122    24,
123    16,
124    63,
125    BLAKE2B_IV,
126    "Blake2b instance with a variable output.",
127    "Blake2b instance with a fixed output.",
128);
129
130/// BLAKE2b which allows to choose output size at runtime.
131pub type Blake2bVar = RtVariableCoreWrapper<Blake2bVarCore>;
132/// Core hasher state of BLAKE2b generic over output size.
133pub type Blake2bCore<OutSize> = CtVariableCoreWrapper<Blake2bVarCore, OutSize>;
134/// BLAKE2b generic over output size.
135pub type Blake2b<OutSize> = CoreWrapper<Blake2bCore<OutSize>>;
136/// BLAKE2b-512 hasher state.
137pub type Blake2b512 = Blake2b<U64>;
138
139blake2_mac_impl!(Blake2bMac, Blake2bVarCore, U64, "Blake2b MAC function");
140
141/// BLAKE2b-512 MAC state.
142pub type Blake2bMac512 = Blake2bMac<U64>;
143
144blake2_impl!(
145    Blake2sVarCore,
146    "Blake2s",
147    u32,
148    u32x4,
149    U32,
150    U64,
151    16,
152    12,
153    8,
154    7,
155    BLAKE2S_IV,
156    "Blake2s instance with a variable output.",
157    "Blake2s instance with a fixed output.",
158);
159
160/// BLAKE2s which allows to choose output size at runtime.
161pub type Blake2sVar = RtVariableCoreWrapper<Blake2sVarCore>;
162/// Core hasher state of BLAKE2s generic over output size.
163pub type Blake2sCore<OutSize> = CtVariableCoreWrapper<Blake2sVarCore, OutSize>;
164/// BLAKE2s generic over output size.
165pub type Blake2s<OutSize> = CoreWrapper<Blake2sCore<OutSize>>;
166/// BLAKE2s-256 hasher state.
167pub type Blake2s256 = Blake2s<U32>;
168
169blake2_mac_impl!(Blake2sMac, Blake2sVarCore, U32, "Blake2s MAC function");
170
171/// BLAKE2s-256 MAC state.
172pub type Blake2sMac256 = Blake2sMac<U32>;