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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
This is part of WHY2
Copyright (C) 2022-2026 Václav Šmejkal
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//! REX Encrypter
//!
//! This module defines the full encryption pipeline for WHY2, including Grid shaping
//! and round-based mixing. It transforms raw data into encrypted Grid chunks using
//! a symmetric key.
//!
//! # Overview
//! WHY2 encrypts data by converting it into fixed-size grids ([`Grid`]) and applying
//! nonlinear and linear transformations across multiple rounds in CTR mode. The process includes:
//!
//! 1. **[`Grid`] Shaping**: Input is padded and split into [`Grid`] chunks.
//! 2. **Key Handling**: A symmetric key is either provided or securely generated.
//! 3. **Nonce Generation**: A random nonce is generated for CTR mode.
//! 4. **CTR Mode Encryption**: Each plaintext [`Grid`] is XORed with a keystream
//! block derived from encrypting the nonce plus block counter.
use Zeroizing;
use crate::
;
/// Encrypts a vector of `i64` values.
///
/// This function transforms the input into fixed-size grids ([`Grid`]) and performs
/// round-based encryption using nonlinear and linear mixing.
///
/// # Parameters
/// - `input`: A slice of `i64` values representing the data.
/// - `key`: An optional symmetric key. If `None`, a secure key is generated automatically.
/// If provided, it must be exactly $2 \times W \times H$ elements long.
///
/// # Returns
/// An [`EncryptedData`] struct containing:
/// - `output`: A vector of encrypted [`Grid`]s.
/// - `key`: The key [`Grid`] used for encryption.
///
/// # Behavior
/// - Splits the input into [`Grid`] chunks.
/// - Generates a random nonce for CTR mode.
/// - Applies CTR mode encryption using the WHY2 block cipher.
///
/// Each plaintext block $P_i$ is encrypted using CTR mode:
/// $$ C_i = P_i \oplus E_K(\text{Nonce} + i) $$
///
/// where $E_K$ denotes the WHY2 block cipher keyed with $K$, and $i$ is the block counter.
/// Encrypts a string.
///
/// This function encodes the input string into `i64` values by packing two `char`s
/// into each 64-bit integer.
///
/// # Parameters
/// - `input`: A string slice to encrypt.
/// - `key`: An optional symmetric key. If `None`, a secure key is generated automatically.
/// If provided, it must be exactly $2 \times W \times H$ elements long.