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
/*
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 Decrypter
//!
//! This module defines the core decryption for WHY2, including round-key reversal
//! and Grid unmixing. It reconstructs the original data from encrypted Grid
//! chunks using a symmetric key.
//!
//! # Overview
//! WHY2 encrypts data by transforming it into fixed-size grids ([`Grid`]) using
//! CTR mode with a block cipher. Decryption reverses this process:
//!
//! 1. **Round Key Generation**: Reconstructs round keys from the master key using chained SHA-256 seeds.
//! 2. **CTR Mode Decryption**: Each ciphertext [`Grid`] is XORed with the keystream block (the nonce plus block counter encrypted with WHY2).
//!
//! Since CTR mode is symmetric, the same encryption function is used for both encryption and decryption.
use Zeroizing;
use crate::
;
/// Decrypts a WHY2-encrypted data into raw `i64` values.
///
/// This function reverses the full WHY2 encryption pipeline using CTR mode:
///
/// $$ P_i = C_i \oplus E_K(\text{Nonce} + i) $$
///
/// where $E_K$ is the WHY2 block cipher and $i$ is the block counter.
///
/// - Generates round keys from the master key
/// - Applies CTR mode decryption (XOR with keystream blocks)
///
/// # Parameters
/// - `input`: An [`EncryptedData`] struct containing the encrypted grids and key grid.
///
/// - Ok([`DecryptedData`]) struct containing:
/// - `output`: A vector of decrypted `i64` values
/// - `key`: The original key [`Grid`] flattened into a vector
/// - Err(String) if [`Grid`] area is 1
/// Decrypts a WHY2-encrypted data and reconstructs the original string.
///
/// This function performs full decryption using [`decrypt`],
/// then interprets each `i64` value as two concatenated `u32` characters. The first 4 bytes
/// represent the high character, and the next 4 bytes represent the low character. If the low
/// character is zero, it is omitted.
///
/// # Parameters
/// - `input`: An [`EncryptedData`] struct containing encrypted grids and key.
///
/// # Returns
/// - Ok(`String`) reconstructed from the decrypted values.
/// - Err(`String`) if Grid area is 1
///
/// # Notes
/// - Uses CTR mode for decryption (same as encryption).
/// - Uses big-endian decoding for each `i64` value.
/// - Each decrypted value contributes up to two Unicode scalar values.