blake2_rfc/
blake2b.rs

1// Copyright 2015 blake2-rfc Developers
2// Copyright 2017 Google Inc.
3//
4// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
5// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
6// http://opensource.org/licenses/MIT>, at your option. This file may not be
7// copied, modified, or distributed except according to those terms.
8
9//! The BLAKE2b hash function.
10//!
11//! # Examples
12//!
13//! ```
14//! use blake2_rfc::blake2b::{Blake2b, blake2b};
15//!
16//! // Using the convenience function.
17//! let hash = blake2b(64, &[], b"The quick brown fox jumps over the lazy dog");
18//!
19//! // Using the state context.
20//! let mut context = Blake2b::new(64);
21//! context.update(b"The quick brown fox jumps over the lazy dog");
22//! let hash = context.finalize();
23//!
24//! // Using the convenience function, with a key.
25//! let hash = blake2b(64, b"key", b"The quick brown fox jumps over the lazy dog");
26//!
27//! // Using the state context, with a key.
28//! let mut context = Blake2b::with_key(64, b"key");
29//! context.update(b"The quick brown fox jumps over the lazy dog");
30//! let hash = context.finalize();
31//! ```
32//!
33//! The returned hash is a `Blake2bResult`, which can be compared with
34//! a byte string (the comparison will take constant time), or converted
35//! into a byte string.
36
37blake2_impl!(Blake2b, Blake2bResult, blake2b, u64, u64x4, 64, 32, 24, 16, 63, [
38    0x6A09E667F3BCC908, 0xBB67AE8584CAA73B,
39    0x3C6EF372FE94F82B, 0xA54FF53A5F1D36F1,
40    0x510E527FADE682D1, 0x9B05688C2B3E6C1F,
41    0x1F83D9ABFB41BD6B, 0x5BE0CD19137E2179,
42]);
43
44blake2_selftest_impl!(Blake2b, blake2b, [
45    0xC2, 0x3A, 0x78, 0x00, 0xD9, 0x81, 0x23, 0xBD,
46    0x10, 0xF5, 0x06, 0xC6, 0x1E, 0x29, 0xDA, 0x56,
47    0x03, 0xD7, 0x63, 0xB8, 0xBB, 0xAD, 0x2E, 0x73,
48    0x7F, 0x5E, 0x76, 0x5A, 0x7B, 0xCC, 0xD4, 0x75,
49], [ 20, 32, 48, 64 ], [ 0, 3, 128, 129, 255, 1024 ]);
50
51blake2_bench_impl!(Blake2b, 64);
52
53#[cfg(test)]
54mod tests {
55    #![cfg_attr(feature = "clippy", allow(result_unwrap_used))]
56
57    extern crate data_encoding;
58    use self::data_encoding::HEXUPPER;
59
60    use blake2::selftest_seq;
61    use super::{Blake2b, blake2b};
62
63    #[test]
64    fn test_empty() {
65        assert_eq!(&blake2b(64, &[], b""), &HEXUPPER.decode(
66            b"786A02F742015903C6C6FD852552D272912F4740E15847618A86E217F71F5419D25E1031AFEE585313896444934EB04B903A685B1448B755D56F701AFE9BE2CE")
67            .unwrap()[..]);
68    }
69
70    #[test]
71    fn selftest() {
72        super::selftest();
73    }
74
75    #[test]
76    fn test_split() {
77        let data = selftest_seq(512);
78
79        let mut ctx = Blake2b::new(64);
80        ctx.update(&data[..32]);
81        ctx.update(&data[32..64]);
82        ctx.update(&data[64..448]);
83        ctx.update(&data[448..]);
84
85        assert_eq!(&ctx.finalize(), &blake2b(64, &[], &data));
86    }
87
88    #[cfg(feature = "std")]
89    #[test]
90    fn test_write() {
91        use std::io::prelude::*;
92
93        let data = selftest_seq(1024);
94
95        let mut ctx = Blake2b::new(64);
96        ctx.update(&data[..]);
97
98        let mut writer = Blake2b::new(64);
99        writer.write_all(&data[..]).unwrap();
100
101        assert_eq!(&writer.finalize(), &ctx.finalize());
102    }
103
104    #[cfg_attr(debug_assertions, ignore)]
105    #[test]
106    fn test_4g() {
107        const ZEROS: [u8; 4096] = [0; 4096];
108
109        let mut state = Blake2b::new(64);
110        for _ in 0..1048576 {
111            state.update(&ZEROS);
112        }
113        assert_eq!(&state.finalize(), &HEXUPPER.decode(
114            b"645572CA5756F9104329ED543735FC11904F0C18C4DF8ADF930F22D07F3094919A519FF34FD240AE3F5D5B4C8042225C109FB951036FDC99E7D2CD0C1D36B267")
115            .unwrap()[..]);
116    }
117}