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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
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 Stream Processing
//!
//! This module defines the stateful interface for streamed encryption and decryption
//! in the WHY2 system. It allows seamless processing of arbitrarily long data in smaller
//! chunks, making it ideal for network communication (e.g., TCP streams) or handling large files.
//!
//! # Overview
//! Unlike the [`encrypter`](crate::encrypter) and [`decrypter`](crate::decrypter) modules,
//! which require a one-shot loading of all data into memory, the [`RexStream`] structure
//! acts as a state machine. Because CTR (Counter) mode is symmetric, the identical internal
//! logic processes both plaintext encryption and ciphertext decryption.
//!
//! The stream lifecycle consists of three steps:
//!
//! 1. **Initialization (`new`)**: Generation of round keys from the master key and allocation
//! of the internal buffer.
//! 2. **Continuous Processing (`update`)**: Incremental data consumption. Once the buffer
//! reaches the capacity of a single block ([`Grid`]), the data is transformed in parallel
//! and flushed to the output.
//! 3. **Stream Finalization (`finalize`)**: Processing of any remaining elements in the buffer
//! that did not fill an entire grid area by generating a final truncated keystream.
//!
//! # Security and Nonce Reuse
//! For stream ciphers based on CTR mode, it is critically important to ensure that an identical
//! keystream is never generated for different blocks of data under the same key.
//!
//! The internal state therefore strictly enforces counter continuity. The global `block_counter`
//! does not correspond to the number of `update` method calls, but rather the total cumulative
//! number of processed [`Grid`] blocks since the stream's initialization. The keystream for each
//! block $G_i$ within any given chunk is thus derived as:
//!
//! $$ G_i \leftarrow G_i \oplus E_K(\text{Nonce} + \text{block}_{\text{counter}} + i) $$
use slice;
use Zeroizing;
use crate::
;
//STRUCTS
/// A stateful container for incremental encryption and decryption using the REX cipher in CTR mode.
///
/// `RexStream` maintains the cryptographic context and internal buffering required to process
/// data chunks of arbitrary length. It buffers incoming elements until a complete [`Grid`]
/// can be formed, making it suitable for streaming I/O operations such as network sockets
/// or file streams.
///
/// Since CTR mode is symmetric, the same stream instance is used for both encryption
/// and decryption operations.
///
/// # Type Parameters
/// - `W`: The width of the REX [`Grid`].
/// - `H`: The height of the REX [`Grid`].
///
/// # Cryptographic Safety
/// This structure enforces strict block counter continuity across consecutive updates.
/// To maintain semantic security, a unique `nonce` must be paired with the master key
/// for every separate stream lifecycle to prevent keystream reuse.
,
const H: usize = ,
>
//IMPLEMENTATIONS