1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--

Zeros

Copyright (C) 2019-2023  Anonymous

There are several releases over multiple years,
they are listed as ranges, such as: "2019-2023".

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
*/

//! # Kit for `WordArray`

use {
    core::mem,
    super::Key,
};

/// # Word
pub (super) type Word = u32;

/// # Word size in bytes
pub (super) const WORD_SIZE_IN_BYTES: usize = mem::size_of::<Word>() / mem::size_of::<u8>();

/// # Word Array length
pub (super) const WORD_ARRAY_LEN: usize = 16;

/// # Word Array
pub (super) type WordArray = [Word; WORD_ARRAY_LEN];

const SIGMA: [u8; 16] = *b"expand 32-byte k";
#[allow(dead_code)]
const TAU: [u8; 16] = *b"expand 16-byte k";

#[test]
fn tests() {
    assert_eq!(WORD_SIZE_IN_BYTES, (Word::BITS / u8::BITS).try_into().unwrap());
}

/// # Make a `WordArray` from key and IV
pub (crate) fn new<K, IV>(key: K, iv: IV) -> WordArray where K: Into<Key>, IV: Into<super::IV> {
    let key = key.into();
    let iv = iv.into();
    [
        Word::from_le_bytes([SIGMA[0], SIGMA[1], SIGMA[2], SIGMA[3]]),
        Word::from_le_bytes([SIGMA[4], SIGMA[5], SIGMA[6], SIGMA[7]]),
        Word::from_le_bytes([SIGMA[8], SIGMA[9], SIGMA[10], SIGMA[11]]),
        Word::from_le_bytes([SIGMA[12], SIGMA[13], SIGMA[14], SIGMA[15]]),

        Word::from_le_bytes([key[0], key[1], key[2], key[3]]),
        Word::from_le_bytes([key[4], key[5], key[6], key[7]]),
        Word::from_le_bytes([key[8], key[9], key[10], key[11]]),
        Word::from_le_bytes([key[12], key[13], key[14], key[15]]),
        Word::from_le_bytes([key[16], key[17], key[18], key[19]]),
        Word::from_le_bytes([key[20], key[21], key[22], key[23]]),
        Word::from_le_bytes([key[24], key[25], key[26], key[27]]),
        Word::from_le_bytes([key[28], key[29], key[30], key[31]]),

        Word::from_le_bytes([iv[0], iv[1], iv[2], iv[3]]),
        Word::from_le_bytes([iv[4], iv[5], iv[6], iv[7]]),
        Word::from_le_bytes([iv[8], iv[9], iv[10], iv[11]]),
        Word::from_le_bytes([iv[12], iv[13], iv[14], iv[15]]),
    ]
}