1#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
8use aesni;
9
10use aessafe;
11use blockmodes::{PaddingProcessor, EcbEncryptor, EcbDecryptor, CbcEncryptor, CbcDecryptor, CtrMode,
12 CtrModeX8};
13use symmetriccipher::{Encryptor, Decryptor, SynchronousStreamCipher};
14use util;
15
16#[derive(Clone, Copy)]
18pub enum KeySize {
19 KeySize128,
20 KeySize192,
21 KeySize256
22}
23
24#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
26pub fn ecb_encryptor<X: PaddingProcessor + Send + 'static>(
27 key_size: KeySize,
28 key: &[u8],
29 padding: X) -> Box<Encryptor> {
30 if util::supports_aesni() {
31 let aes_enc = aesni::AesNiEncryptor::new(key_size, key);
32 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
33 enc
34 } else {
35 match key_size {
36 KeySize::KeySize128 => {
37 let aes_enc = aessafe::AesSafe128Encryptor::new(key);
38 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
39 enc
40 }
41 KeySize::KeySize192 => {
42 let aes_enc = aessafe::AesSafe192Encryptor::new(key);
43 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
44 enc
45 }
46 KeySize::KeySize256 => {
47 let aes_enc = aessafe::AesSafe256Encryptor::new(key);
48 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
49 enc
50 }
51 }
52 }
53}
54
55#[cfg(all(not(target_arch = "x86"), not(target_arch = "x86_64")))]
57pub fn ecb_encryptor<X: PaddingProcessor + Send + 'static>(
58 key_size: KeySize,
59 key: &[u8],
60 padding: X) -> Box<Encryptor> {
61 match key_size {
62 KeySize::KeySize128 => {
63 let aes_enc = aessafe::AesSafe128Encryptor::new(key);
64 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
65 enc
66 }
67 KeySize::KeySize192 => {
68 let aes_enc = aessafe::AesSafe192Encryptor::new(key);
69 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
70 enc
71 }
72 KeySize::KeySize256 => {
73 let aes_enc = aessafe::AesSafe256Encryptor::new(key);
74 let enc = Box::new(EcbEncryptor::new(aes_enc, padding));
75 enc
76 }
77 }
78}
79
80#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
82pub fn ecb_decryptor<X: PaddingProcessor + Send + 'static>(
83 key_size: KeySize,
84 key: &[u8],
85 padding: X) -> Box<Decryptor> {
86 if util::supports_aesni() {
87 let aes_dec = aesni::AesNiDecryptor::new(key_size, key);
88 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
89 dec
90 } else {
91 match key_size {
92 KeySize::KeySize128 => {
93 let aes_dec = aessafe::AesSafe128Decryptor::new(key);
94 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
95 dec
96 }
97 KeySize::KeySize192 => {
98 let aes_dec = aessafe::AesSafe192Decryptor::new(key);
99 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
100 dec
101 }
102 KeySize::KeySize256 => {
103 let aes_dec = aessafe::AesSafe256Decryptor::new(key);
104 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
105 dec
106 }
107 }
108 }
109}
110
111#[cfg(all(not(target_arch = "x86"), not(target_arch = "x86_64")))]
113pub fn ecb_decryptor<X: PaddingProcessor + Send + 'static>(
114 key_size: KeySize,
115 key: &[u8],
116 padding: X) -> Box<Decryptor> {
117 match key_size {
118 KeySize::KeySize128 => {
119 let aes_dec = aessafe::AesSafe128Decryptor::new(key);
120 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
121 dec
122 }
123 KeySize::KeySize192 => {
124 let aes_dec = aessafe::AesSafe192Decryptor::new(key);
125 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
126 dec
127 }
128 KeySize::KeySize256 => {
129 let aes_dec = aessafe::AesSafe256Decryptor::new(key);
130 let dec = Box::new(EcbDecryptor::new(aes_dec, padding));
131 dec
132 }
133 }
134}
135
136#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
138pub fn cbc_encryptor<X: PaddingProcessor + Send + 'static>(
139 key_size: KeySize,
140 key: &[u8],
141 iv: &[u8],
142 padding: X) -> Box<Encryptor + 'static> {
143 if util::supports_aesni() {
144 let aes_enc = aesni::AesNiEncryptor::new(key_size, key);
145 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
146 enc
147 } else {
148 match key_size {
149 KeySize::KeySize128 => {
150 let aes_enc = aessafe::AesSafe128Encryptor::new(key);
151 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
152 enc
153 }
154 KeySize::KeySize192 => {
155 let aes_enc = aessafe::AesSafe192Encryptor::new(key);
156 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
157 enc
158 }
159 KeySize::KeySize256 => {
160 let aes_enc = aessafe::AesSafe256Encryptor::new(key);
161 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
162 enc
163 }
164 }
165 }
166}
167
168#[cfg(all(not(target_arch = "x86"), not(target_arch = "x86_64")))]
170pub fn cbc_encryptor<X: PaddingProcessor + Send + 'static>(
171 key_size: KeySize,
172 key: &[u8],
173 iv: &[u8],
174 padding: X) -> Box<Encryptor + 'static> {
175 match key_size {
176 KeySize::KeySize128 => {
177 let aes_enc = aessafe::AesSafe128Encryptor::new(key);
178 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
179 enc
180 }
181 KeySize::KeySize192 => {
182 let aes_enc = aessafe::AesSafe192Encryptor::new(key);
183 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
184 enc
185 }
186 KeySize::KeySize256 => {
187 let aes_enc = aessafe::AesSafe256Encryptor::new(key);
188 let enc = Box::new(CbcEncryptor::new(aes_enc, padding, iv.to_vec()));
189 enc
190 }
191 }
192}
193
194#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
196pub fn cbc_decryptor<X: PaddingProcessor + Send + 'static>(
197 key_size: KeySize,
198 key: &[u8],
199 iv: &[u8],
200 padding: X) -> Box<Decryptor + 'static> {
201 if util::supports_aesni() {
202 let aes_dec = aesni::AesNiDecryptor::new(key_size, key);
203 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
204 dec
205 } else {
206 match key_size {
207 KeySize::KeySize128 => {
208 let aes_dec = aessafe::AesSafe128Decryptor::new(key);
209 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
210 dec
211 }
212 KeySize::KeySize192 => {
213 let aes_dec = aessafe::AesSafe192Decryptor::new(key);
214 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
215 dec
216 }
217 KeySize::KeySize256 => {
218 let aes_dec = aessafe::AesSafe256Decryptor::new(key);
219 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
220 dec
221 }
222 }
223 }
224}
225
226#[cfg(all(not(target_arch = "x86"), not(target_arch = "x86_64")))]
228pub fn cbc_decryptor<X: PaddingProcessor + Send + 'static>(
229 key_size: KeySize,
230 key: &[u8],
231 iv: &[u8],
232 padding: X) -> Box<Decryptor + 'static> {
233 match key_size {
234 KeySize::KeySize128 => {
235 let aes_dec = aessafe::AesSafe128Decryptor::new(key);
236 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
237 dec as Box<Decryptor + 'static>
238 }
239 KeySize::KeySize192 => {
240 let aes_dec = aessafe::AesSafe192Decryptor::new(key);
241 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
242 dec as Box<Decryptor + 'static>
243 }
244 KeySize::KeySize256 => {
245 let aes_dec = aessafe::AesSafe256Decryptor::new(key);
246 let dec = Box::new(CbcDecryptor::new(aes_dec, padding, iv.to_vec()));
247 dec as Box<Decryptor + 'static>
248 }
249 }
250}
251
252#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
254pub fn ctr(
255 key_size: KeySize,
256 key: &[u8],
257 iv: &[u8]) -> Box<SynchronousStreamCipher + 'static> {
258 if util::supports_aesni() {
259 let aes_dec = aesni::AesNiEncryptor::new(key_size, key);
260 let dec = Box::new(CtrMode::new(aes_dec, iv.to_vec()));
261 dec
262 } else {
263 match key_size {
264 KeySize::KeySize128 => {
265 let aes_dec = aessafe::AesSafe128EncryptorX8::new(key);
266 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
267 dec
268 }
269 KeySize::KeySize192 => {
270 let aes_dec = aessafe::AesSafe192EncryptorX8::new(key);
271 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
272 dec
273 }
274 KeySize::KeySize256 => {
275 let aes_dec = aessafe::AesSafe256EncryptorX8::new(key);
276 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
277 dec
278 }
279 }
280 }
281}
282
283#[cfg(all(not(target_arch = "x86"), not(target_arch = "x86_64")))]
285pub fn ctr(
286 key_size: KeySize,
287 key: &[u8],
288 iv: &[u8]) -> Box<SynchronousStreamCipher + 'static> {
289 match key_size {
290 KeySize::KeySize128 => {
291 let aes_dec = aessafe::AesSafe128EncryptorX8::new(key);
292 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
293 dec as Box<SynchronousStreamCipher>
294 }
295 KeySize::KeySize192 => {
296 let aes_dec = aessafe::AesSafe192EncryptorX8::new(key);
297 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
298 dec as Box<SynchronousStreamCipher>
299 }
300 KeySize::KeySize256 => {
301 let aes_dec = aessafe::AesSafe256EncryptorX8::new(key);
302 let dec = Box::new(CtrModeX8::new(aes_dec, iv));
303 dec as Box<SynchronousStreamCipher>
304 }
305 }
306}
307
308#[cfg(test)]
309mod test {
310 use std::iter::repeat;
311
312 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
313 use aesni;
314
315 use aessafe;
316 use symmetriccipher::{BlockEncryptor, BlockDecryptor, BlockEncryptorX8, BlockDecryptorX8,
317 SynchronousStreamCipher};
318 use util;
319 use aes;
320 use aes::KeySize::{KeySize128, KeySize192, KeySize256};
321
322 struct Test {
326 key: Vec<u8>,
327 data: Vec<TestData>
328 }
329
330 struct TestData {
331 plain: Vec<u8>,
332 cipher: Vec<u8>
333 }
334
335 fn tests128() -> Vec<Test> {
336 vec![
337 Test {
338 key: vec![0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
339 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c],
340 data: vec![
341 TestData {
342 plain: vec![0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
343 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a],
344 cipher: vec![0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60,
345 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97]
346 },
347 TestData {
348 plain: vec![0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
349 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51],
350 cipher: vec![0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d,
351 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf]
352 },
353 TestData {
354 plain: vec![0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
355 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef],
356 cipher: vec![0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23,
357 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88]
358 },
359 TestData {
360 plain: vec![0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
361 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
362 cipher: vec![0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f,
363 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4]
364 }
365 ]
366 }
367 ]
368 }
369
370 fn tests192() -> Vec<Test> {
371 vec![
372 Test {
373 key: vec![0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b,
374 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b],
375 data: vec![
376 TestData {
377 plain: vec![0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
378 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a],
379 cipher: vec![0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f,
380 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc]
381 },
382 TestData {
383 plain: vec![0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
384 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51],
385 cipher: vec![0x97, 0x41, 0x04, 0x84, 0x6d, 0x0a, 0xd3, 0xad,
386 0x77, 0x34, 0xec, 0xb3, 0xec, 0xee, 0x4e, 0xef]
387 },
388 TestData {
389 plain: vec![0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
390 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef],
391 cipher: vec![0xef, 0x7a, 0xfd, 0x22, 0x70, 0xe2, 0xe6, 0x0a,
392 0xdc, 0xe0, 0xba, 0x2f, 0xac, 0xe6, 0x44, 0x4e]
393 },
394 TestData {
395 plain: vec![0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
396 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
397 cipher: vec![0x9a, 0x4b, 0x41, 0xba, 0x73, 0x8d, 0x6c, 0x72,
398 0xfb, 0x16, 0x69, 0x16, 0x03, 0xc1, 0x8e, 0x0e]
399 }
400 ]
401 }
402 ]
403 }
404
405 fn tests256() -> Vec<Test> {
406 vec![
407 Test {
408 key: vec![0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
409 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
410 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
411 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4],
412 data: vec![
413 TestData {
414 plain: vec![0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
415 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a],
416 cipher: vec![0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
417 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8]
418 },
419 TestData {
420 plain: vec![0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
421 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51],
422 cipher: vec![0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26,
423 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70]
424 },
425 TestData {
426 plain: vec![0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
427 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef],
428 cipher: vec![0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9,
429 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d]
430 },
431 TestData {
432 plain: vec![0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
433 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10],
434 cipher: vec![0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff,
435 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7]
436 }
437 ]
438 }
439 ]
440 }
441
442 struct CtrTest {
443 key: Vec<u8>,
444 ctr: Vec<u8>,
445 plain: Vec<u8>,
446 cipher: Vec<u8>
447 }
448
449 fn aes_ctr_tests() -> Vec<CtrTest> {
450 vec![
451 CtrTest {
452 key: repeat(1).take(16).collect(),
453 ctr: repeat(3).take(16).collect(),
454 plain: repeat(2).take(33).collect(),
455 cipher: vec![
456 0x64, 0x3e, 0x05, 0x19, 0x79, 0x78, 0xd7, 0x45,
457 0xa9, 0x10, 0x5f, 0xd8, 0x4c, 0xd7, 0xe6, 0xb1,
458 0x5f, 0x66, 0xc6, 0x17, 0x4b, 0x25, 0xea, 0x24,
459 0xe6, 0xf9, 0x19, 0x09, 0xb7, 0xdd, 0x84, 0xfb,
460 0x86 ]
461 }
462 ]
463 }
464
465 fn run_test<E: BlockEncryptor, D: BlockDecryptor>(enc: &mut E, dec: &mut D, test: &Test) {
466 let mut tmp = [0u8; 16];
467 for data in test.data.iter() {
468 enc.encrypt_block(&data.plain[..], &mut tmp);
469 assert!(tmp[..] == data.cipher[..]);
470 dec.decrypt_block(&data.cipher[..], &mut tmp);
471 assert!(tmp[..] == data.plain[..]);
472 }
473 }
474
475 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
476 #[test]
477 fn test_aesni_128() {
478 if util::supports_aesni() {
479 let tests = tests128();
480 for t in tests.iter() {
481 let mut enc = aesni::AesNiEncryptor::new(KeySize128, &t.key[..]);
482 let mut dec = aesni::AesNiDecryptor::new(KeySize128, &t.key[..]);
483 run_test(&mut enc, &mut dec, t);
484 }
485 }
486 }
487
488 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
489 #[test]
490 fn test_aesni_192() {
491 if util::supports_aesni() {
492 let tests = tests192();
493 for t in tests.iter() {
494 let mut enc = aesni::AesNiEncryptor::new(KeySize192, &t.key[..]);
495 let mut dec = aesni::AesNiDecryptor::new(KeySize192, &t.key[..]);
496 run_test(&mut enc, &mut dec, t);
497 }
498 }
499 }
500
501 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
502 #[test]
503 fn test_aesni_256() {
504 if util::supports_aesni() {
505 let tests = tests256();
506 for t in tests.iter() {
507 let mut enc = aesni::AesNiEncryptor::new(KeySize256, &t.key[..]);
508 let mut dec = aesni::AesNiDecryptor::new(KeySize256, &t.key[..]);
509 run_test(&mut enc, &mut dec, t);
510 }
511 }
512 }
513
514 #[test]
515 fn test_aessafe_128() {
516 let tests = tests128();
517 for t in tests.iter() {
518 let mut enc = aessafe::AesSafe128Encryptor::new(&t.key[..]);
519 let mut dec = aessafe::AesSafe128Decryptor::new(&t.key[..]);
520 run_test(&mut enc, &mut dec, t);
521 }
522 }
523
524 #[test]
525 fn test_aessafe_192() {
526 let tests = tests192();
527 for t in tests.iter() {
528 let mut enc = aessafe::AesSafe192Encryptor::new(&t.key[..]);
529 let mut dec = aessafe::AesSafe192Decryptor::new(&t.key[..]);
530 run_test(&mut enc, &mut dec, t);
531 }
532 }
533
534 #[test]
535 fn test_aessafe_256() {
536 let tests = tests256();
537 for t in tests.iter() {
538 let mut enc = aessafe::AesSafe256Encryptor::new(&t.key[..]);
539 let mut dec = aessafe::AesSafe256Decryptor::new(&t.key[..]);
540 run_test(&mut enc, &mut dec, t);
541 }
542 }
543
544 #[test]
547 fn test_aessafe_128_x8() {
548 let key: [u8; 16] = [
549 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6,
550 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c ];
551 let plain: [u8; 128] = [
552 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
553 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
554 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
555 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
556 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
557 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
558 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
559 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
560 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
561 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
562 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
563 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
564 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
565 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
566 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
567 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ];
568 let cipher: [u8; 128] = [
569 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60,
570 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97,
571 0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d,
572 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf,
573 0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23,
574 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88,
575 0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f,
576 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4,
577 0x3a, 0xd7, 0x7b, 0xb4, 0x0d, 0x7a, 0x36, 0x60,
578 0xa8, 0x9e, 0xca, 0xf3, 0x24, 0x66, 0xef, 0x97,
579 0xf5, 0xd3, 0xd5, 0x85, 0x03, 0xb9, 0x69, 0x9d,
580 0xe7, 0x85, 0x89, 0x5a, 0x96, 0xfd, 0xba, 0xaf,
581 0x43, 0xb1, 0xcd, 0x7f, 0x59, 0x8e, 0xce, 0x23,
582 0x88, 0x1b, 0x00, 0xe3, 0xed, 0x03, 0x06, 0x88,
583 0x7b, 0x0c, 0x78, 0x5e, 0x27, 0xe8, 0xad, 0x3f,
584 0x82, 0x23, 0x20, 0x71, 0x04, 0x72, 0x5d, 0xd4 ];
585
586 let enc = aessafe::AesSafe128EncryptorX8::new(&key);
587 let dec = aessafe::AesSafe128DecryptorX8::new(&key);
588 let mut tmp = [0u8; 128];
589 enc.encrypt_block_x8(&plain, &mut tmp);
590 assert!(tmp[..] == cipher[..]);
591 dec.decrypt_block_x8(&cipher, &mut tmp);
592 assert!(tmp[..] == plain[..]);
593 }
594
595 #[test]
596 fn test_aessafe_192_x8() {
597 let key: [u8; 24] = [
598 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b,
599 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b ];
600 let plain: [u8; 128] = [
601 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
602 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
603 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
604 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
605 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
606 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
607 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
608 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
609 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
610 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
611 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
612 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
613 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
614 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
615 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
616 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ];
617 let cipher: [u8; 128] = [
618 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f,
619 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc,
620 0x97, 0x41, 0x04, 0x84, 0x6d, 0x0a, 0xd3, 0xad,
621 0x77, 0x34, 0xec, 0xb3, 0xec, 0xee, 0x4e, 0xef,
622 0xef, 0x7a, 0xfd, 0x22, 0x70, 0xe2, 0xe6, 0x0a,
623 0xdc, 0xe0, 0xba, 0x2f, 0xac, 0xe6, 0x44, 0x4e,
624 0x9a, 0x4b, 0x41, 0xba, 0x73, 0x8d, 0x6c, 0x72,
625 0xfb, 0x16, 0x69, 0x16, 0x03, 0xc1, 0x8e, 0x0e,
626 0xbd, 0x33, 0x4f, 0x1d, 0x6e, 0x45, 0xf2, 0x5f,
627 0xf7, 0x12, 0xa2, 0x14, 0x57, 0x1f, 0xa5, 0xcc,
628 0x97, 0x41, 0x04, 0x84, 0x6d, 0x0a, 0xd3, 0xad,
629 0x77, 0x34, 0xec, 0xb3, 0xec, 0xee, 0x4e, 0xef,
630 0xef, 0x7a, 0xfd, 0x22, 0x70, 0xe2, 0xe6, 0x0a,
631 0xdc, 0xe0, 0xba, 0x2f, 0xac, 0xe6, 0x44, 0x4e,
632 0x9a, 0x4b, 0x41, 0xba, 0x73, 0x8d, 0x6c, 0x72,
633 0xfb, 0x16, 0x69, 0x16, 0x03, 0xc1, 0x8e, 0x0e ];
634
635 let enc = aessafe::AesSafe192EncryptorX8::new(&key);
636 let dec = aessafe::AesSafe192DecryptorX8::new(&key);
637 let mut tmp = [0u8; 128];
638 enc.encrypt_block_x8(&plain, &mut tmp);
639 assert!(tmp[..] == cipher[..]);
640 dec.decrypt_block_x8(&cipher, &mut tmp);
641 assert!(tmp[..] == plain[..]);
642 }
643
644 #[test]
645 fn test_aessafe_256_x8() {
646 let key: [u8; 32] = [
647 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
648 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
649 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
650 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 ];
651 let plain: [u8; 128] = [
652 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
653 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
654 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
655 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
656 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
657 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
658 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
659 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10,
660 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
661 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
662 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
663 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
664 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
665 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
666 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
667 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ];
668 let cipher: [u8; 128] = [
669 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
670 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
671 0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26,
672 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70,
673 0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9,
674 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d,
675 0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff,
676 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7,
677 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
678 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
679 0x59, 0x1c, 0xcb, 0x10, 0xd4, 0x10, 0xed, 0x26,
680 0xdc, 0x5b, 0xa7, 0x4a, 0x31, 0x36, 0x28, 0x70,
681 0xb6, 0xed, 0x21, 0xb9, 0x9c, 0xa6, 0xf4, 0xf9,
682 0xf1, 0x53, 0xe7, 0xb1, 0xbe, 0xaf, 0xed, 0x1d,
683 0x23, 0x30, 0x4b, 0x7a, 0x39, 0xf9, 0xf3, 0xff,
684 0x06, 0x7d, 0x8d, 0x8f, 0x9e, 0x24, 0xec, 0xc7 ];
685
686 let enc = aessafe::AesSafe256EncryptorX8::new(&key);
687 let dec = aessafe::AesSafe256DecryptorX8::new(&key);
688 let mut tmp = [0u8; 128];
689 enc.encrypt_block_x8(&plain, &mut tmp);
690 assert!(tmp[..] == cipher[..]);
691 dec.decrypt_block_x8(&cipher, &mut tmp);
692 assert!(tmp[..] == plain[..]);
693 }
694
695 #[test]
696 fn aes_ctr_box() {
697 let tests = aes_ctr_tests();
698 for test in tests.iter() {
699 let mut aes_enc = aes::ctr(aes::KeySize::KeySize128, &test.key[..], &test.ctr[..]);
700 let mut result: Vec<u8> = repeat(0).take(test.plain.len()).collect();
701 aes_enc.process(&test.plain[..], &mut result[..]);
702 let res: &[u8] = result.as_ref();
703 assert!(res == &test.cipher[..]);
704 }
705 }
706}
707
708#[cfg(all(test, feature = "with-bench"))]
709mod bench {
710 use test::Bencher;
711
712 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
713 use aesni;
714
715 use aessafe;
716 use symmetriccipher::{BlockEncryptor, BlockEncryptorX8};
717 use util;
718 use aes::KeySize::{self, KeySize128, KeySize192, KeySize256};
719
720 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
721 #[bench]
722 pub fn aesni_128_bench(bh: &mut Bencher) {
723 aesni_bench(bh, KeySize128);
724 }
725
726 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
727 #[bench]
728 pub fn aesni_192_bench(bh: &mut Bencher) {
729 aesni_bench(bh, KeySize192);
730 }
731
732 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
733 #[bench]
734 pub fn aesni_256_bench(bh: &mut Bencher) {
735 aesni_bench(bh, KeySize256);
736 }
737
738 #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
739 fn aesni_bench(bh: &mut Bencher, key_size: KeySize) {
740 if util::supports_aesni() {
741 let key: [u8; 16] = [1u8; 16];
742 let plain: [u8; 16] = [2u8; 16];
743
744 let a = aesni::AesNiEncryptor::new(key_size, &key);
745
746 let mut tmp = [0u8; 16];
747
748 bh.iter( || {
749 a.encrypt_block(&plain, &mut tmp);
750 });
751
752 bh.bytes = (plain.len()) as u64;
753 }
754 }
755
756 #[bench]
757 pub fn aes_safe_bench(bh: &mut Bencher) {
758 let key: [u8; 16] = [1u8; 16];
759 let plain: [u8; 16] = [2u8; 16];
760
761 let a = aessafe::AesSafe128Encryptor::new(&key);
762
763 let mut tmp = [0u8; 16];
764
765 bh.iter( || {
766 a.encrypt_block(&plain, &mut tmp);
767 });
768
769 bh.bytes = (plain.len()) as u64;
770 }
771
772 #[bench]
773 pub fn aes_safe_x8_bench(bh: &mut Bencher) {
774 let key: [u8; 16] = [1u8; 16];
775 let plain: [u8; 128] = [2u8; 128];
776
777 let a = aessafe::AesSafe128EncryptorX8::new(&key);
778
779 let mut tmp = [0u8; 128];
780
781 bh.iter( || {
782 a.encrypt_block_x8(&plain, &mut tmp);
783 });
784
785 bh.bytes = (plain.len()) as u64;
786 }
787}