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
//! Low-level utility functions used by the encryption and decryption pipelines.
//!
//! Most callers will not need anything from this module directly; the helpers
//! are exposed because they are useful when composing custom v0–v2 read flows
//! ([`utf8_to_utf16le`]) or custom CBC pipelines ([`xor_blocks`]).
use crateAescryptError;
/// Re-encodes UTF-8 password bytes as a UTF-16-LE byte vector.
///
/// AES Crypt v0–v2 ACKDF hashes passwords as little-endian UTF-16 code units;
/// this helper performs the conversion inside [`crate::derive_ackdf_key`] but
/// is also useful for callers building custom legacy decryption flows.
///
/// The output `Vec<u8>` length is always twice the number of UTF-16 code units
/// produced from `input_utf8` — i.e. **bytes**, not code units.
///
/// # Errors
///
/// - [`AescryptError::Crypto`] — `input_utf8` is not valid UTF-8.
///
/// # Panics
///
/// Never panics.
///
/// # Security
///
/// Returns a plain `Vec<u8>` (not a [`secure-gate`] alias). When this function
/// is called from [`crate::derive_ackdf_key`] the output is immediately wrapped
/// in `Dynamic<Vec<u8>>`. External callers that pass through real passwords
/// should also wrap the output in a zeroizing container before letting it
/// drop, otherwise the UTF-16-LE password copy lingers on the heap until the
/// allocator overwrites it.
///
/// # Examples
///
/// ```
/// use aescrypt_rs::utilities::utf8_to_utf16le;
///
/// let utf8_bytes = b"Hello";
/// let utf16le = utf8_to_utf16le(utf8_bytes)?;
/// assert_eq!(
/// utf16le,
/// [0x48, 0x00, 0x65, 0x00, 0x6C, 0x00, 0x6C, 0x00, 0x6F, 0x00]
/// );
/// # Ok::<(), aescrypt_rs::AescryptError>(())
/// ```
///
/// [`secure-gate`]: https://github.com/Slurp9187/secure-gate
/// XORs the first 16 bytes of `block_a` and `block_b` into the first 16 bytes
/// of `output`.
///
/// Used by both encryption and decryption paths for AES-CBC chaining. The
/// fixed length of 16 makes this function easy to inline as a tight loop on
/// every supported target.
///
/// # Errors
///
/// Infallible.
///
/// # Panics
///
/// Panics if any of `block_a`, `block_b`, or `output` is shorter than 16
/// bytes (Rust's normal slice-bounds panic; this is **not** undefined
/// behavior).
///
/// # Compatibility
///
/// This function is `pub fn`, **not** `pub const fn`, because the MSRV (1.70)
/// does not yet stabilize mutable references in `const fn` for this access
/// pattern. The signature is intentionally identical to the eventual `const`
/// variant so the change can land as a non-breaking minor upgrade once MSRV
/// is bumped. See `CHANGELOG.md` (0.2.0-rc.8) for the rationale.
///
/// # Examples
///
/// ```
/// use aescrypt_rs::utilities::xor_blocks;
///
/// let block_a = [0xFF; 16];
/// let block_b = [0xAA; 16];
/// let mut output = [0u8; 16];
///
/// xor_blocks(&block_a, &block_b, &mut output);
/// assert_eq!(output, [0x55; 16]); // 0xFF ^ 0xAA = 0x55
/// ```
pub