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
//! src/decryption/read.rs
//! Header / extension / iteration-count parsers for the AES Crypt v0–v3 read path.
//!
//! Every parser in this module reads into a [`SpanBuffer<N>`](crate::aliases::SpanBuffer),
//! the [`secure-gate`] auto-zeroizing fixed-size buffer, so that even
//! pre-authentication header bytes never linger on the stack after the call
//! returns. The parsers are sequenced by [`crate::decrypt()`]; they are
//! exposed publicly so that callers driving custom containers can rebuild the
//! read pipeline themselves.
//!
//! # Security
//!
//! These parsers run **before** the session HMAC is verified, so any allocation
//! or work they perform is attacker-influenceable. They are deliberately kept
//! to fixed-size reads with hard-coded upper bounds (e.g. `MAX_EXTENSIONS`,
//! [`PBKDF2_MAX_ITER`](crate::constants::PBKDF2_MAX_ITER)).
//!
//! [`secure-gate`]: https://github.com/Slurp9187/secure-gate
use crateSpanBuffer;
use crateAescryptError;
use ;
use Read;
/// Reads exactly `N` bytes from `reader` into a fresh auto-zeroizing
/// [`SpanBuffer<N>`](crate::aliases::SpanBuffer).
///
/// `read_exact_span` is the primary stream reader for the constant-memory
/// decryption path. The returned buffer lives on the stack inside a
/// [`secure-gate`] wrapper so its contents are wiped on drop — important
/// because pre-authentication header bytes pass through this function.
///
/// # Errors
///
/// - [`AescryptError::Io`] — `reader.read_exact` returned an error, including
/// premature EOF.
///
/// # Panics
///
/// Never panics. EOF is surfaced as [`AescryptError::Io`], not a panic.
///
/// # Security
///
/// Output buffer auto-zeroizes via [`secure-gate`] regardless of which
/// branch of the caller eventually returns.
///
/// [`secure-gate`]: https://github.com/Slurp9187/secure-gate
/// Reads and validates the 5-byte AES Crypt file header.
///
/// Returns `(version, modulo_or_reserved)` where:
///
/// - `version` is the file format version (`0..=3`).
/// - `modulo_or_reserved` is the 5th header byte:
/// - For v0: the **modulo** byte (any value; passed to
/// [`StreamConfig::V0`](crate::decryption::StreamConfig::V0) for final
/// plaintext-length recovery).
/// - For v1–v3: the **reserved** byte; an error is returned unless it is
/// `0x00`.
///
/// This is the strict counterpart to [`crate::read_version`], which only
/// reads as many bytes as needed and is permissive about short v0 stubs.
///
/// # Errors
///
/// - [`AescryptError::Io`] — premature EOF or other reader error.
/// - [`AescryptError::Header`] — magic bytes are not `b"AES"`, or the v1–v3
/// reserved byte is not `0x00`.
/// - [`AescryptError::UnsupportedVersion`] — version byte is `> 3`.
///
/// # Security
///
/// Reads exactly 5 bytes regardless of input, capping pre-authentication
/// effort. The output is plain `(u8, u8)` — there is nothing secret to
/// zeroize.
/// Maximum number of extensions accepted before returning an error.
///
/// A crafted file with an unbounded number of small extensions could consume
/// proportional CPU/IO before the HMAC check. Limiting to 256 extensions is
/// well above any legitimate use while capping the pre-auth work.
const MAX_EXTENSIONS: usize = 256;
/// Consumes all v2/v3 extension blocks from `reader`, stopping at the
/// zero-length terminator.
///
/// For `version < 2`, this is a no-op (v0/v1 files have no extension section).
/// For v2/v3, each extension is parsed as a `u16` big-endian length followed by
/// `length` payload bytes, and is discarded. The loop stops when a zero-length
/// extension is encountered.
///
/// # Errors
///
/// - [`AescryptError::Io`] — reader error or premature EOF inside an
/// extension.
/// - [`AescryptError::Header`] — more than 256 extension blocks encountered
/// (`"too many extensions (limit: 256)"`).
///
/// # Security
///
/// Capped at 256 extensions to bound CPU/I/O on attacker-controlled files.
/// The discard buffer is fixed at 256 bytes and reused across reads.
/// Reads the 4-byte big-endian PBKDF2 iteration count from a v3 file header.
///
/// Returns `0` for `version < 3` (v0/v1/v2 do not store an iteration count;
/// they use the fixed [`ACKDF_ITERATIONS`](crate::kdf::ackdf::ACKDF_ITERATIONS)
/// instead). For v3, the value is validated against an internal upper bound of
/// 5 000 000 iterations (matching
/// [`PBKDF2_MAX_ITER`](crate::constants::PBKDF2_MAX_ITER)) and rejected if
/// zero.
///
/// # Errors
///
/// - [`AescryptError::Io`] — reader error or premature EOF.
/// - [`AescryptError::Header`] — iteration count is `0`
/// (`"KDF iterations cannot be zero"`) or exceeds 5 000 000
/// (`"KDF iterations unreasonably high (>5M)"`).
///
/// # Security
///
/// The 5 000 000 ceiling is enforced before any password-dependent work to
/// prevent denial-of-service via crafted headers with `iterations = u32::MAX`.