zeros/chacha/
word_array.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Zeros
5
6Copyright (C) 2019-2025  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2019-2025".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27//! # Kit for `WordArray`
28
29use super::{Key, Nonce};
30
31#[cfg(test)]
32mod tests;
33
34/// # Word
35pub (crate) type Word = u32;
36
37/// # Word Array length
38pub (super) const WORD_ARRAY_LEN: usize = 16;
39
40/// # Word Array
41pub (super) type WordArray = [Word; WORD_ARRAY_LEN];
42
43/// # Word size
44pub (crate) const WORD_SIZE: usize = size_of::<Word>();
45
46const SIGMA_LEN: usize = 16;
47const SIGMA: [u8; SIGMA_LEN] = *b"expand 32-byte k";
48#[allow(dead_code)]
49const TAU: [u8; SIGMA_LEN] = *b"expand 16-byte k";
50
51/// # Make a `WordArray` from key and nonce
52pub (crate) fn new<K, N>(key: K, nonce: N) -> WordArray where K: Into<Key>, N: Into<Nonce> {
53    let key = key.into();
54    let nonce = nonce.into();
55
56    macro_rules! form { ($src: ident, $i: literal) => {{
57        Word::from_le_bytes([$src[$i], $src[$i + 1], $src[$i + 2], $src[$i + 3]])
58    }}}
59
60    [
61        form!(SIGMA,  0),
62        form!(SIGMA,  4),
63        form!(SIGMA,  8),
64        form!(SIGMA, 12),
65
66        form!(key,  0),
67        form!(key,  4),
68        form!(key,  8),
69        form!(key, 12),
70        form!(key, 16),
71        form!(key, 20),
72        form!(key, 24),
73        form!(key, 28),
74
75        form!(nonce,  0),
76        form!(nonce,  4),
77        form!(nonce,  8),
78        form!(nonce, 12),
79    ]
80}