Debian Codegraph @

debian/rust-vendor/zip/src/aes_ctr.rs

raw
1 //! A counter mode (CTR) for AES to work with the encryption used in zip files.
2 //!
3 //! This was implemented since the zip specification requires the mode to not use a nonce and uses a
4 //! different byte order (little endian) than NIST (big endian).
5 //! See [`AesCtrZipKeyStream`] for more information.
6
7 use crate::result::{ZipError, ZipResult};
8 use aes::cipher::{BlockCipherEncrypt, KeyInit};
9 use core::{any, fmt};
10
11 /// Internal block size of an AES cipher.
12 const AES_BLOCK_SIZE: usize = 16;
13
14 /// AES-128.
15 #[derive(Debug)]
16 pub struct Aes128;
17 /// AES-192.
18 #[derive(Debug)]
19 pub struct Aes192;
20 /// AES-256.
21 #[derive(Debug)]
22 pub struct Aes256;
23
24 /// An AES cipher kind.
25 pub trait AesKind {
26 /// Key type.
27 type Key: AsRef<[u8]>;
28 /// Cipher used to decrypt.
29 type Cipher: KeyInit;
30 }
31
32 impl AesKind for Aes128 {
33 type Key = [u8; 16];
34 type Cipher = aes::Aes128;
35 }
36
37 impl AesKind for Aes192 {
38 type Key = [u8; 24];
39 type Cipher = aes::Aes192;
40 }
41
42 impl AesKind for Aes256 {
43 type Key = [u8; 32];
44 type Cipher = aes::Aes256;
45 }
46
47 /// An AES-CTR key stream generator.
48 ///
49 /// Implements the slightly non-standard AES-CTR variant used by `WinZip` AES encryption.
50 ///
51 /// Typical AES-CTR implementations combine a nonce with a 64 bit counter. `WinZIP` AES instead uses
52 /// no nonce and also uses a different byte order (little endian) than NIST (big endian).
53 ///
54 /// The stream implements the `Read` trait; encryption or decryption is performed by XOR-ing the
55 /// bytes from the key stream with the ciphertext/plaintext.
56 pub struct AesCtrZipKeyStream<C: AesKind> {
57 /// Current AES counter.
58 counter: u128,
59 /// AES cipher instance.
60 cipher: C::Cipher,
61 /// Stores the currently available keystream bytes.
62 buffer: [u8; AES_BLOCK_SIZE],
63 /// Number of bytes already used up from `buffer`.
64 pos: usize,
65 }
66
67 impl<C> fmt::Debug for AesCtrZipKeyStream<C>
68 where
69 C: AesKind,
70 {
71 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72 write!(
73 f,
74 "AesCtrZipKeyStream<{}>(counter: {})",
75 any::type_name::<C>(),
76 self.counter
77 )
78 }
79 }
80
81 impl<C> AesCtrZipKeyStream<C>
82 where
83 C: AesKind,
84 C::Cipher: KeyInit,
85 {
86 /// Creates a new zip variant AES-CTR key stream.
87 ///
88 /// Returns `ZipError::InvalidPassword` if `key` doesn't have the correct size for cipher `C`.
89 pub fn new(key: &[u8]) -> ZipResult<AesCtrZipKeyStream<C>> {
90 Ok(AesCtrZipKeyStream {
91 counter: 1,
92 cipher: C::Cipher::new_from_slice(key).map_err(|_| ZipError::InvalidPassword)?,
93 buffer: [0u8; AES_BLOCK_SIZE],
94 pos: AES_BLOCK_SIZE,
95 })
96 }
97 }
98
99 impl<C> AesCipher for AesCtrZipKeyStream<C>
100 where
101 C: AesKind,
102 C::Cipher: BlockCipherEncrypt,
103 {
104 /// Decrypt or encrypt `target`.
105 #[inline]
106 fn crypt_in_place(&mut self, mut target: &mut [u8]) {
107 while !target.is_empty() {
108 if self.pos == AES_BLOCK_SIZE {
109 self.buffer = self.counter.to_le_bytes();
110 #[allow(deprecated)]
111 self.cipher
112 .encrypt_block(aes::cipher::Block::<C::Cipher>::from_mut_slice(
113 &mut self.buffer,
114 ));
115 self.counter += 1;
116 self.pos = 0;
117 }
118
119 let target_len = target.len().min(AES_BLOCK_SIZE - self.pos);
120
121 xor(
122 &mut target[0..target_len],
123 &self.buffer[self.pos..(self.pos + target_len)],
124 );
125 target = &mut target[target_len..];
126 self.pos += target_len;
127 }
128 }
129 }
130
131 /// This trait allows using generic AES ciphers with different key sizes.
132 pub trait AesCipher {
133 fn crypt_in_place(&mut self, target: &mut [u8]);
134 }
135
136 /// XORs a slice in place with another slice.
137 #[inline]
138 fn xor(dest: &mut [u8], src: &[u8]) {
139 debug_assert_eq!(
140 dest.len(),
141 src.len(),
142 "XOR operation requires destination and source slices to have equal length"
143 );
144
145 for (lhs, rhs) in dest.iter_mut().zip(src.iter()) {
146 *lhs ^= *rhs;
147 }
148 }
149
150 #[cfg(test)]
151 mod tests {
152 use super::{Aes128, Aes192, Aes256, AesCipher, AesCtrZipKeyStream, AesKind};
153 use aes::cipher::{BlockCipherEncrypt, KeyInit};
154
155 /// Checks whether `crypt_in_place` produces the correct plaintext after one use and yields the
156 /// ciphertext again after applying it again.
157 fn roundtrip<Aes>(key: &[u8], ciphertext: &[u8], expected_plaintext: &[u8])
158 where
159 Aes: AesKind,
160 Aes::Cipher: KeyInit + BlockCipherEncrypt,
161 {
162 let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key).unwrap();
163
164 let mut plaintext = ciphertext.to_vec().into_boxed_slice();
165 key_stream.crypt_in_place(&mut plaintext);
166 assert_eq!(*plaintext, *expected_plaintext);
167
168 // Round-tripping should yield the ciphertext again.
169 let mut key_stream = AesCtrZipKeyStream::<Aes>::new(key).unwrap();
170 key_stream.crypt_in_place(&mut plaintext);
171 assert_eq!(*plaintext, *ciphertext);
172 }
173
174 #[test]
175 fn new_with_wrong_key_size() {
176 AesCtrZipKeyStream::<Aes128>::new(&[1, 2, 3, 4, 5])
177 .expect_err("Wrong key size should fail");
178 }
179
180 // The data used in these tests was generated with p7zip without any compression.
181 // It's not possible to recreate the exact same data, since a random salt is used for encryption.
182 // `7z a -phelloworld -mem=AES256 -mx=0 aes256_40byte.zip 40byte_data.txt`
183 #[test]
184 fn crypt_aes_256_0_byte() {
185 let ciphertext = [];
186 let expected_plaintext = &[];
187 let key = [
188 0x0b, 0xec, 0x2e, 0xf2, 0x46, 0xf0, 0x7e, 0x35, 0x16, 0x54, 0xe0, 0x98, 0x10, 0xb3,
189 0x18, 0x55, 0x24, 0xa3, 0x9e, 0x0e, 0x40, 0xe7, 0x92, 0xad, 0xb2, 0x8a, 0x48, 0xf4,
190 0x5c, 0xd0, 0xc0, 0x54,
191 ];
192
193 roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
194 }
195
196 #[test]
197 fn crypt_aes_128_5_byte() {
198 let ciphertext = [0x98, 0xa9, 0x8c, 0x26, 0x0e];
199 let expected_plaintext = b"asdf\n";
200 let key = [
201 0xe0, 0x25, 0x7b, 0x57, 0x97, 0x6a, 0xa4, 0x23, 0xab, 0x94, 0xaa, 0x44, 0xfd, 0x47,
202 0x4f, 0xa5,
203 ];
204
205 roundtrip::<Aes128>(&key, &ciphertext, expected_plaintext);
206 }
207
208 #[test]
209 fn crypt_aes_192_5_byte() {
210 let ciphertext = [0x36, 0x55, 0x5c, 0x61, 0x3c];
211 let expected_plaintext = b"asdf\n";
212 let key = [
213 0xe4, 0x4a, 0x88, 0x52, 0x8f, 0xf7, 0x0b, 0x81, 0x7b, 0x75, 0xf1, 0x74, 0x21, 0x37,
214 0x8c, 0x90, 0xad, 0xbe, 0x4a, 0x65, 0xa8, 0x96, 0x0e, 0xcc,
215 ];
216
217 roundtrip::<Aes192>(&key, &ciphertext, expected_plaintext);
218 }
219
220 #[test]
221 fn crypt_aes_256_5_byte() {
222 let ciphertext = [0xc2, 0x47, 0xc0, 0xdc, 0x56];
223 let expected_plaintext = b"asdf\n";
224 let key = [
225 0x79, 0x5e, 0x17, 0xf2, 0xc6, 0x3d, 0x28, 0x9b, 0x4b, 0x4b, 0xbb, 0xa9, 0xba, 0xc9,
226 0xa5, 0xee, 0x3a, 0x4f, 0x0f, 0x4b, 0x29, 0xbd, 0xe9, 0xb8, 0x41, 0x9c, 0x41, 0xa5,
227 0x15, 0xb2, 0x86, 0xab,
228 ];
229
230 roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
231 }
232
233 #[test]
234 fn crypt_aes_128_40_byte() {
235 let ciphertext = [
236 0xcf, 0x72, 0x6b, 0xa1, 0xb2, 0x0f, 0xdf, 0xaa, 0x10, 0xad, 0x9c, 0x7f, 0x6d, 0x1c,
237 0x8d, 0xb5, 0x16, 0x7e, 0xbb, 0x11, 0x69, 0x52, 0x8c, 0x89, 0x80, 0x32, 0xaa, 0x76,
238 0xa6, 0x18, 0x31, 0x98, 0xee, 0xdd, 0x22, 0x68, 0xb7, 0xe6, 0x77, 0xd2,
239 ];
240 let expected_plaintext = b"Lorem ipsum dolor sit amet, consectetur\n";
241 let key = [
242 0x43, 0x2b, 0x6d, 0xbe, 0x05, 0x76, 0x6c, 0x9e, 0xde, 0xca, 0x3b, 0xf8, 0xaf, 0x5d,
243 0x81, 0xb6,
244 ];
245
246 roundtrip::<Aes128>(&key, &ciphertext, expected_plaintext);
247 }
248
249 #[test]
250 fn crypt_aes_192_40_byte() {
251 let ciphertext = [
252 0xa6, 0xfc, 0x52, 0x79, 0x2c, 0x6c, 0xfe, 0x68, 0xb1, 0xa8, 0xb3, 0x07, 0x52, 0x8b,
253 0x82, 0xa6, 0x87, 0x9c, 0x72, 0x42, 0x3a, 0xf8, 0xc6, 0xa9, 0xc9, 0xfb, 0x61, 0x19,
254 0x37, 0xb9, 0x56, 0x62, 0xf4, 0xfc, 0x5e, 0x7a, 0xdd, 0x55, 0x0a, 0x48,
255 ];
256 let expected_plaintext = b"Lorem ipsum dolor sit amet, consectetur\n";
257 let key = [
258 0xac, 0x92, 0x41, 0xba, 0xde, 0xd9, 0x02, 0xfe, 0x40, 0x92, 0x20, 0xf6, 0x56, 0x03,
259 0xfe, 0xae, 0x1b, 0xba, 0x01, 0x97, 0x97, 0x79, 0xbb, 0xa6,
260 ];
261
262 roundtrip::<Aes192>(&key, &ciphertext, expected_plaintext);
263 }
264
265 #[test]
266 fn crypt_aes_256_40_byte() {
267 let ciphertext = [
268 0xa9, 0x99, 0xbd, 0xea, 0x82, 0x9b, 0x8f, 0x2f, 0xb7, 0x52, 0x2f, 0x6b, 0xd8, 0xf6,
269 0xab, 0x0e, 0x24, 0x51, 0x9e, 0x18, 0x0f, 0xc0, 0x8f, 0x54, 0x15, 0x80, 0xae, 0xbc,
270 0xa0, 0x5c, 0x8a, 0x11, 0x8d, 0x14, 0x7e, 0xc5, 0xb4, 0xae, 0xd3, 0x37,
271 ];
272 let expected_plaintext = b"Lorem ipsum dolor sit amet, consectetur\n";
273 let key = [
274 0x64, 0x7c, 0x7a, 0xde, 0xf0, 0xf2, 0x61, 0x49, 0x1c, 0xf1, 0xf1, 0xe3, 0x37, 0xfc,
275 0xe1, 0x4d, 0x4a, 0x77, 0xd4, 0xeb, 0x9e, 0x3d, 0x75, 0xce, 0x9a, 0x3e, 0x10, 0x50,
276 0xc2, 0x07, 0x36, 0xb6,
277 ];
278
279 roundtrip::<Aes256>(&key, &ciphertext, expected_plaintext);
280 }
281 }