blake2_rfc/
blake2s.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 BLAKE2s hash function.
10//!
11//! # Examples
12//!
13//! ```
14//! use blake2_rfc::blake2s::{Blake2s, blake2s};
15//!
16//! // Using the convenience function.
17//! let hash = blake2s(32, &[], b"The quick brown fox jumps over the lazy dog");
18//!
19//! // Using the state context.
20//! let mut context = Blake2s::new(32);
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 = blake2s(32, 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 = Blake2s::with_key(32, 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 `Blake2sResult`, 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!(Blake2s, Blake2sResult, blake2s, u32, u32x4, 32, 16, 12, 8, 7, [
38    0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A,
39    0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19,
40]);
41
42blake2_selftest_impl!(Blake2s, blake2s, [
43    0x6A, 0x41, 0x1F, 0x08, 0xCE, 0x25, 0xAD, 0xCD,
44    0xFB, 0x02, 0xAB, 0xA6, 0x41, 0x45, 0x1C, 0xEC,
45    0x53, 0xC5, 0x98, 0xB2, 0x4F, 0x4F, 0xC7, 0x87,
46    0xFB, 0xDC, 0x88, 0x79, 0x7F, 0x4C, 0x1D, 0xFE,
47], [ 16, 20, 28, 32 ], [ 0,  3,  64, 65, 255, 1024 ]);
48
49blake2_bench_impl!(Blake2s, 32);
50
51#[cfg(test)]
52mod tests {
53    #![cfg_attr(feature = "clippy", allow(result_unwrap_used))]
54
55    extern crate data_encoding;
56    use self::data_encoding::HEXUPPER;
57
58    use blake2::selftest_seq;
59    use super::{Blake2s, blake2s};
60
61    #[test]
62    fn test_empty() {
63        assert_eq!(&blake2s(32, &[], b""), &HEXUPPER.decode(
64            b"69217A3079908094E11121D042354A7C1F55B6482CA1A51E1B250DFD1ED0EEF9")
65            .unwrap()[..]);
66    }
67
68    #[test]
69    fn selftest() {
70        super::selftest();
71    }
72
73    #[test]
74    fn test_split() {
75        let data = selftest_seq(256);
76
77        let mut ctx = Blake2s::new(32);
78        ctx.update(&data[..16]);
79        ctx.update(&data[16..32]);
80        ctx.update(&data[32..224]);
81        ctx.update(&data[224..]);
82
83        assert_eq!(&ctx.finalize(), &blake2s(32, &[], &data));
84    }
85
86    #[cfg(feature = "std")]
87    #[test]
88    fn test_write() {
89        use std::io::prelude::*;
90
91        let data = selftest_seq(1024);
92
93        let mut ctx = Blake2s::new(32);
94        ctx.update(&data[..]);
95
96        let mut writer = Blake2s::new(32);
97        writer.write_all(&data[..]).unwrap();
98
99        assert_eq!(&writer.finalize(), &ctx.finalize());
100    }
101
102    #[cfg_attr(debug_assertions, ignore)]
103    #[test]
104    fn test_4g() {
105        const ZEROS: [u8; 4096] = [0; 4096];
106
107        let mut state = Blake2s::new(32);
108        for _ in 0..1048576 {
109            state.update(&ZEROS);
110        }
111        assert_eq!(&state.finalize(), &HEXUPPER.decode(
112            b"2A8E26830310DA3EF7F7032B7B1AF11B989ABA44A3713A22F539F69BD2CE4A87")
113            .unwrap()[..]);
114    }
115}