Debian Codegraph @

lib/Crypto/SelfTest/Cipher/test_EAX.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2015, Legrandin <[email protected]>
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 # POSSIBILITY OF SUCH DAMAGE.
29 # ===================================================================
30
31 import unittest
32 from binascii import unhexlify
33
34 from Crypto.SelfTest.st_common import list_test_cases
35 from Crypto.SelfTest.loader import load_test_vectors_wycheproof
36 from Crypto.Util.py3compat import tobytes, bchr
37 from Crypto.Cipher import AES, DES3
38 from Crypto.Hash import SHAKE128
39
40 from Crypto.Util.strxor import strxor
41
42
43 def get_tag_random(tag, length):
44 return SHAKE128.new(data=tobytes(tag)).read(length)
45
46
47 class EaxTests(unittest.TestCase):
48
49 key_128 = get_tag_random("key_128", 16)
50 key_192 = get_tag_random("key_192", 16)
51 nonce_96 = get_tag_random("nonce_128", 12)
52 data_128 = get_tag_random("data_128", 16)
53
54 def test_loopback_128(self):
55 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
56 pt = get_tag_random("plaintext", 16 * 100)
57 ct = cipher.encrypt(pt)
58
59 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
60 pt2 = cipher.decrypt(ct)
61 self.assertEqual(pt, pt2)
62
63 def test_loopback_64(self):
64 cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96)
65 pt = get_tag_random("plaintext", 8 * 100)
66 ct = cipher.encrypt(pt)
67
68 cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96)
69 pt2 = cipher.decrypt(ct)
70 self.assertEqual(pt, pt2)
71
72 def test_nonce(self):
73 # If not passed, the nonce is created randomly
74 cipher = AES.new(self.key_128, AES.MODE_EAX)
75 nonce1 = cipher.nonce
76 cipher = AES.new(self.key_128, AES.MODE_EAX)
77 nonce2 = cipher.nonce
78 self.assertEqual(len(nonce1), 16)
79 self.assertNotEqual(nonce1, nonce2)
80
81 cipher = AES.new(self.key_128, AES.MODE_EAX, self.nonce_96)
82 ct = cipher.encrypt(self.data_128)
83
84 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
85 self.assertEqual(ct, cipher.encrypt(self.data_128))
86
87 def test_nonce_must_be_bytes(self):
88 self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
89 nonce=u'test12345678')
90
91 def test_nonce_length(self):
92 # nonce can be of any length (but not empty)
93 self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
94 nonce=b"")
95
96 for x in range(1, 128):
97 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=bchr(1) * x)
98 cipher.encrypt(bchr(1))
99
100 def test_block_size_128(self):
101 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
102 self.assertEqual(cipher.block_size, AES.block_size)
103
104 def test_block_size_64(self):
105 cipher = DES3.new(self.key_192, AES.MODE_EAX, nonce=self.nonce_96)
106 self.assertEqual(cipher.block_size, DES3.block_size)
107
108 def test_nonce_attribute(self):
109 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
110 self.assertEqual(cipher.nonce, self.nonce_96)
111
112 # By default, a 16 bytes long nonce is randomly generated
113 nonce1 = AES.new(self.key_128, AES.MODE_EAX).nonce
114 nonce2 = AES.new(self.key_128, AES.MODE_EAX).nonce
115 self.assertEqual(len(nonce1), 16)
116 self.assertNotEqual(nonce1, nonce2)
117
118 def test_unknown_parameters(self):
119 self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
120 self.nonce_96, 7)
121 self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
122 nonce=self.nonce_96, unknown=7)
123
124 # But some are only known by the base cipher
125 # (e.g. use_aesni consumed by the AES module)
126 AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96,
127 use_aesni=False)
128
129 def test_null_encryption_decryption(self):
130 for func in "encrypt", "decrypt":
131 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
132 result = getattr(cipher, func)(b"")
133 self.assertEqual(result, b"")
134
135 def test_either_encrypt_or_decrypt(self):
136 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
137 cipher.encrypt(b"")
138 self.assertRaises(TypeError, cipher.decrypt, b"")
139
140 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
141 cipher.decrypt(b"")
142 self.assertRaises(TypeError, cipher.encrypt, b"")
143
144 def test_data_must_be_bytes(self):
145 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
146 self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
147
148 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
149 self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*')
150
151 def test_mac_len(self):
152 # Invalid MAC length
153 self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
154 nonce=self.nonce_96, mac_len=2-1)
155 self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
156 nonce=self.nonce_96, mac_len=16+1)
157
158 # Valid MAC length
159 for mac_len in range(2, 16 + 1):
160 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96,
161 mac_len=mac_len)
162 _, mac = cipher.encrypt_and_digest(self.data_128)
163 self.assertEqual(len(mac), mac_len)
164
165 # Default MAC length
166 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
167 _, mac = cipher.encrypt_and_digest(self.data_128)
168 self.assertEqual(len(mac), 16)
169
170 def test_invalid_mac(self):
171 from Crypto.Util.strxor import strxor_c
172 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
173 ct, mac = cipher.encrypt_and_digest(self.data_128)
174
175 invalid_mac = strxor_c(mac, 0x01)
176
177 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
178 self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
179 invalid_mac)
180
181 def test_hex_mac(self):
182 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
183 mac_hex = cipher.hexdigest()
184 self.assertEqual(cipher.digest(), unhexlify(mac_hex))
185
186 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
187 cipher.hexverify(mac_hex)
188
189 def test_message_chunks(self):
190 # Validate that both associated data and plaintext/ciphertext
191 # can be broken up in chunks of arbitrary length
192
193 auth_data = get_tag_random("authenticated data", 127)
194 plaintext = get_tag_random("plaintext", 127)
195
196 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
197 cipher.update(auth_data)
198 ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext)
199
200 def break_up(data, chunk_length):
201 return [data[i:i+chunk_length] for i in range(0, len(data),
202 chunk_length)]
203
204 # Encryption
205 for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
206
207 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
208
209 for chunk in break_up(auth_data, chunk_length):
210 cipher.update(chunk)
211 pt2 = b""
212 for chunk in break_up(ciphertext, chunk_length):
213 pt2 += cipher.decrypt(chunk)
214 self.assertEqual(plaintext, pt2)
215 cipher.verify(ref_mac)
216
217 # Decryption
218 for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
219
220 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
221
222 for chunk in break_up(auth_data, chunk_length):
223 cipher.update(chunk)
224 ct2 = b""
225 for chunk in break_up(plaintext, chunk_length):
226 ct2 += cipher.encrypt(chunk)
227 self.assertEqual(ciphertext, ct2)
228 self.assertEqual(cipher.digest(), ref_mac)
229
230 def test_bytearray(self):
231
232 # Encrypt
233 key_ba = bytearray(self.key_128)
234 nonce_ba = bytearray(self.nonce_96)
235 header_ba = bytearray(self.data_128)
236 data_ba = bytearray(self.data_128)
237
238 cipher1 = AES.new(self.key_128,
239 AES.MODE_EAX,
240 nonce=self.nonce_96)
241 cipher1.update(self.data_128)
242 ct = cipher1.encrypt(self.data_128)
243 tag = cipher1.digest()
244
245 cipher2 = AES.new(key_ba,
246 AES.MODE_EAX,
247 nonce=nonce_ba)
248 key_ba[:3] = b'\xFF\xFF\xFF'
249 nonce_ba[:3] = b'\xFF\xFF\xFF'
250 cipher2.update(header_ba)
251 header_ba[:3] = b'\xFF\xFF\xFF'
252 ct_test = cipher2.encrypt(data_ba)
253 data_ba[:3] = b'\x99\x99\x99'
254 tag_test = cipher2.digest()
255
256 self.assertEqual(ct, ct_test)
257 self.assertEqual(tag, tag_test)
258 self.assertEqual(cipher1.nonce, cipher2.nonce)
259
260 # Decrypt
261 key_ba = bytearray(self.key_128)
262 nonce_ba = bytearray(self.nonce_96)
263 header_ba = bytearray(self.data_128)
264 ct_ba = bytearray(ct)
265 tag_ba = bytearray(tag)
266 del data_ba
267
268 cipher3 = AES.new(key_ba,
269 AES.MODE_EAX,
270 nonce=nonce_ba)
271 key_ba[:3] = b'\xFF\xFF\xFF'
272 nonce_ba[:3] = b'\xFF\xFF\xFF'
273 cipher3.update(header_ba)
274 header_ba[:3] = b'\xFF\xFF\xFF'
275 pt_test = cipher3.decrypt(ct_ba)
276 ct_ba[:3] = b'\xFF\xFF\xFF'
277 cipher3.verify(tag_ba)
278
279 self.assertEqual(pt_test, self.data_128)
280
281 def test_memoryview(self):
282
283 # Encrypt
284 key_mv = memoryview(bytearray(self.key_128))
285 nonce_mv = memoryview(bytearray(self.nonce_96))
286 header_mv = memoryview(bytearray(self.data_128))
287 data_mv = memoryview(bytearray(self.data_128))
288
289 cipher1 = AES.new(self.key_128,
290 AES.MODE_EAX,
291 nonce=self.nonce_96)
292 cipher1.update(self.data_128)
293 ct = cipher1.encrypt(self.data_128)
294 tag = cipher1.digest()
295
296 cipher2 = AES.new(key_mv,
297 AES.MODE_EAX,
298 nonce=nonce_mv)
299 key_mv[:3] = b'\xFF\xFF\xFF'
300 nonce_mv[:3] = b'\xFF\xFF\xFF'
301 cipher2.update(header_mv)
302 header_mv[:3] = b'\xFF\xFF\xFF'
303 ct_test = cipher2.encrypt(data_mv)
304 data_mv[:3] = b'\x99\x99\x99'
305 tag_test = cipher2.digest()
306
307 self.assertEqual(ct, ct_test)
308 self.assertEqual(tag, tag_test)
309 self.assertEqual(cipher1.nonce, cipher2.nonce)
310
311 # Decrypt
312 key_mv = memoryview(bytearray(self.key_128))
313 nonce_mv = memoryview(bytearray(self.nonce_96))
314 header_mv = memoryview(bytearray(self.data_128))
315 ct_mv = memoryview(bytearray(ct))
316 tag_mv = memoryview(bytearray(tag))
317 del data_mv
318
319 cipher3 = AES.new(key_mv,
320 AES.MODE_EAX,
321 nonce=nonce_mv)
322 key_mv[:3] = b'\xFF\xFF\xFF'
323 nonce_mv[:3] = b'\xFF\xFF\xFF'
324 cipher3.update(header_mv)
325 header_mv[:3] = b'\xFF\xFF\xFF'
326 pt_test = cipher3.decrypt(ct_mv)
327 ct_mv[:3] = b'\x99\x99\x99'
328 cipher3.verify(tag_mv)
329
330 self.assertEqual(pt_test, self.data_128)
331
332 def test_output_param(self):
333
334 pt = b'5' * 128
335 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
336 ct = cipher.encrypt(pt)
337 tag = cipher.digest()
338
339 output = bytearray(128)
340 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
341 res = cipher.encrypt(pt, output=output)
342 self.assertEqual(ct, output)
343 self.assertEqual(res, None)
344
345 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
346 res = cipher.decrypt(ct, output=output)
347 self.assertEqual(pt, output)
348 self.assertEqual(res, None)
349
350 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
351 res, tag_out = cipher.encrypt_and_digest(pt, output=output)
352 self.assertEqual(ct, output)
353 self.assertEqual(res, None)
354 self.assertEqual(tag, tag_out)
355
356 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
357 res = cipher.decrypt_and_verify(ct, tag, output=output)
358 self.assertEqual(pt, output)
359 self.assertEqual(res, None)
360
361 def test_output_param_memoryview(self):
362
363 pt = b'5' * 128
364 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
365 ct = cipher.encrypt(pt)
366
367 output = memoryview(bytearray(128))
368 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
369 cipher.encrypt(pt, output=output)
370 self.assertEqual(ct, output)
371
372 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
373 cipher.decrypt(ct, output=output)
374 self.assertEqual(pt, output)
375
376 def test_output_param_neg(self):
377 LEN_PT = 16
378
379 pt = b'5' * LEN_PT
380 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
381 ct = cipher.encrypt(pt)
382
383 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
384 self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0' * LEN_PT)
385
386 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
387 self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0' * LEN_PT)
388
389 shorter_output = bytearray(LEN_PT - 1)
390 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
391 self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
392 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
393 self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
394
395
396 class EaxFSMTests(unittest.TestCase):
397
398 key_128 = get_tag_random("key_128", 16)
399 nonce_96 = get_tag_random("nonce_128", 12)
400 data_128 = get_tag_random("data_128", 16)
401
402 def test_valid_init_encrypt_decrypt_digest_verify(self):
403 # No authenticated data, fixed plaintext
404 # Verify path INIT->ENCRYPT->DIGEST
405 cipher = AES.new(self.key_128, AES.MODE_EAX,
406 nonce=self.nonce_96)
407 ct = cipher.encrypt(self.data_128)
408 mac = cipher.digest()
409
410 # Verify path INIT->DECRYPT->VERIFY
411 cipher = AES.new(self.key_128, AES.MODE_EAX,
412 nonce=self.nonce_96)
413 cipher.decrypt(ct)
414 cipher.verify(mac)
415
416 def test_valid_init_update_digest_verify(self):
417 # No plaintext, fixed authenticated data
418 # Verify path INIT->UPDATE->DIGEST
419 cipher = AES.new(self.key_128, AES.MODE_EAX,
420 nonce=self.nonce_96)
421 cipher.update(self.data_128)
422 mac = cipher.digest()
423
424 # Verify path INIT->UPDATE->VERIFY
425 cipher = AES.new(self.key_128, AES.MODE_EAX,
426 nonce=self.nonce_96)
427 cipher.update(self.data_128)
428 cipher.verify(mac)
429
430 def test_valid_full_path(self):
431 # Fixed authenticated data, fixed plaintext
432 # Verify path INIT->UPDATE->ENCRYPT->DIGEST
433 cipher = AES.new(self.key_128, AES.MODE_EAX,
434 nonce=self.nonce_96)
435 cipher.update(self.data_128)
436 ct = cipher.encrypt(self.data_128)
437 mac = cipher.digest()
438
439 # Verify path INIT->UPDATE->DECRYPT->VERIFY
440 cipher = AES.new(self.key_128, AES.MODE_EAX,
441 nonce=self.nonce_96)
442 cipher.update(self.data_128)
443 cipher.decrypt(ct)
444 cipher.verify(mac)
445
446 def test_valid_init_digest(self):
447 # Verify path INIT->DIGEST
448 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
449 cipher.digest()
450
451 def test_valid_init_verify(self):
452 # Verify path INIT->VERIFY
453 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
454 mac = cipher.digest()
455
456 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
457 cipher.verify(mac)
458
459 def test_valid_multiple_encrypt_or_decrypt(self):
460 for method_name in "encrypt", "decrypt":
461 for auth_data in (None, b"333", self.data_128,
462 self.data_128 + b"3"):
463 if auth_data is None:
464 assoc_len = None
465 else:
466 assoc_len = len(auth_data)
467 cipher = AES.new(self.key_128, AES.MODE_EAX,
468 nonce=self.nonce_96)
469 if auth_data is not None:
470 cipher.update(auth_data)
471 method = getattr(cipher, method_name)
472 method(self.data_128)
473 method(self.data_128)
474 method(self.data_128)
475 method(self.data_128)
476
477 def test_valid_multiple_digest_or_verify(self):
478 # Multiple calls to digest
479 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
480 cipher.update(self.data_128)
481 first_mac = cipher.digest()
482 for x in range(4):
483 self.assertEqual(first_mac, cipher.digest())
484
485 # Multiple calls to verify
486 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
487 cipher.update(self.data_128)
488 for x in range(5):
489 cipher.verify(first_mac)
490
491 def test_valid_encrypt_and_digest_decrypt_and_verify(self):
492 # encrypt_and_digest
493 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
494 cipher.update(self.data_128)
495 ct, mac = cipher.encrypt_and_digest(self.data_128)
496
497 # decrypt_and_verify
498 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
499 cipher.update(self.data_128)
500 pt = cipher.decrypt_and_verify(ct, mac)
501 self.assertEqual(self.data_128, pt)
502
503 def test_invalid_mixing_encrypt_decrypt(self):
504 # Once per method, with or without assoc. data
505 for method1_name, method2_name in (("encrypt", "decrypt"),
506 ("decrypt", "encrypt")):
507 for assoc_data_present in (True, False):
508 cipher = AES.new(self.key_128, AES.MODE_EAX,
509 nonce=self.nonce_96)
510 if assoc_data_present:
511 cipher.update(self.data_128)
512 getattr(cipher, method1_name)(self.data_128)
513 self.assertRaises(TypeError, getattr(cipher, method2_name),
514 self.data_128)
515
516 def test_invalid_encrypt_or_update_after_digest(self):
517 for method_name in "encrypt", "update":
518 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
519 cipher.encrypt(self.data_128)
520 cipher.digest()
521 self.assertRaises(TypeError, getattr(cipher, method_name),
522 self.data_128)
523
524 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
525 cipher.encrypt_and_digest(self.data_128)
526
527 def test_invalid_decrypt_or_update_after_verify(self):
528 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
529 ct = cipher.encrypt(self.data_128)
530 mac = cipher.digest()
531
532 for method_name in "decrypt", "update":
533 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
534 cipher.decrypt(ct)
535 cipher.verify(mac)
536 self.assertRaises(TypeError, getattr(cipher, method_name),
537 self.data_128)
538
539 cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
540 cipher.decrypt_and_verify(ct, mac)
541 self.assertRaises(TypeError, getattr(cipher, method_name),
542 self.data_128)
543
544
545 class TestVectorsPaper(unittest.TestCase):
546 """Class exercising the EAX test vectors found in
547 http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf"""
548
549 test_vectors_hex = [
550 ( '6bfb914fd07eae6b',
551 '',
552 '',
553 'e037830e8389f27b025a2d6527e79d01',
554 '233952dee4d5ed5f9b9c6d6ff80ff478',
555 '62EC67F9C3A4A407FCB2A8C49031A8B3'
556 ),
557 (
558 'fa3bfd4806eb53fa',
559 'f7fb',
560 '19dd',
561 '5c4c9331049d0bdab0277408f67967e5',
562 '91945d3f4dcbee0bf45ef52255f095a4',
563 'BECAF043B0A23D843194BA972C66DEBD'
564 ),
565 ( '234a3463c1264ac6',
566 '1a47cb4933',
567 'd851d5bae0',
568 '3a59f238a23e39199dc9266626c40f80',
569 '01f74ad64077f2e704c0f60ada3dd523',
570 '70C3DB4F0D26368400A10ED05D2BFF5E'
571 ),
572 (
573 '33cce2eabff5a79d',
574 '481c9e39b1',
575 '632a9d131a',
576 'd4c168a4225d8e1ff755939974a7bede',
577 'd07cf6cbb7f313bdde66b727afd3c5e8',
578 '8408DFFF3C1A2B1292DC199E46B7D617'
579 ),
580 (
581 'aeb96eaebe2970e9',
582 '40d0c07da5e4',
583 '071dfe16c675',
584 'cb0677e536f73afe6a14b74ee49844dd',
585 '35b6d0580005bbc12b0587124557d2c2',
586 'FDB6B06676EEDC5C61D74276E1F8E816'
587 ),
588 (
589 'd4482d1ca78dce0f',
590 '4de3b35c3fc039245bd1fb7d',
591 '835bb4f15d743e350e728414',
592 'abb8644fd6ccb86947c5e10590210a4f',
593 'bd8e6e11475e60b268784c38c62feb22',
594 '6EAC5C93072D8E8513F750935E46DA1B'
595 ),
596 (
597 '65d2017990d62528',
598 '8b0a79306c9ce7ed99dae4f87f8dd61636',
599 '02083e3979da014812f59f11d52630da30',
600 '137327d10649b0aa6e1c181db617d7f2',
601 '7c77d6e813bed5ac98baa417477a2e7d',
602 '1A8C98DCD73D38393B2BF1569DEEFC19'
603 ),
604 (
605 '54b9f04e6a09189a',
606 '1bda122bce8a8dbaf1877d962b8592dd2d56',
607 '2ec47b2c4954a489afc7ba4897edcdae8cc3',
608 '3b60450599bd02c96382902aef7f832a',
609 '5fff20cafab119ca2fc73549e20f5b0d',
610 'DDE59B97D722156D4D9AFF2BC7559826'
611 ),
612 (
613 '899a175897561d7e',
614 '6cf36720872b8513f6eab1a8a44438d5ef11',
615 '0de18fd0fdd91e7af19f1d8ee8733938b1e8',
616 'e7f6d2231618102fdb7fe55ff1991700',
617 'a4a4782bcffd3ec5e7ef6d8c34a56123',
618 'B781FCF2F75FA5A8DE97A9CA48E522EC'
619 ),
620 (
621 '126735fcc320d25a',
622 'ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7',
623 'cb8920f87a6c75cff39627b56e3ed197c552d295a7',
624 'cfc46afc253b4652b1af3795b124ab6e',
625 '8395fcf1e95bebd697bd010bc766aac3',
626 '22E7ADD93CFC6393C57EC0B3C17D6B44'
627 ),
628 ]
629
630 test_vectors = [[unhexlify(x) for x in tv] for tv in test_vectors_hex]
631
632 def runTest(self):
633 for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
634 # Encrypt
635 cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac))
636 cipher.update(assoc_data)
637 ct2, mac2 = cipher.encrypt_and_digest(pt)
638 self.assertEqual(ct, ct2)
639 self.assertEqual(mac, mac2)
640
641 # Decrypt
642 cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac))
643 cipher.update(assoc_data)
644 pt2 = cipher.decrypt_and_verify(ct, mac)
645 self.assertEqual(pt, pt2)
646
647
648 class TestVectorsWycheproof(unittest.TestCase):
649
650 def __init__(self, wycheproof_warnings):
651 unittest.TestCase.__init__(self)
652 self._wycheproof_warnings = wycheproof_warnings
653 self._id = "None"
654
655 def setUp(self):
656
657 def filter_tag(group):
658 return group['tagSize'] // 8
659
660 self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
661 "aes_eax_test.json",
662 "Wycheproof EAX",
663 group_tag={'tag_size': filter_tag})
664
665 def shortDescription(self):
666 return self._id
667
668 def warn(self, tv):
669 if tv.warning and self._wycheproof_warnings:
670 import warnings
671 warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment))
672
673 def test_encrypt(self, tv):
674 self._id = "Wycheproof Encrypt EAX Test #" + str(tv.id)
675
676 try:
677 cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
678 except ValueError as e:
679 assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e)
680 return
681
682 cipher.update(tv.aad)
683 ct, tag = cipher.encrypt_and_digest(tv.msg)
684 if tv.valid:
685 self.assertEqual(ct, tv.ct)
686 self.assertEqual(tag, tv.tag)
687 self.warn(tv)
688
689 def test_decrypt(self, tv):
690 self._id = "Wycheproof Decrypt EAX Test #" + str(tv.id)
691
692 try:
693 cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
694 except ValueError as e:
695 assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e)
696 return
697
698 cipher.update(tv.aad)
699 try:
700 pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
701 except ValueError:
702 assert not tv.valid
703 else:
704 assert tv.valid
705 self.assertEqual(pt, tv.msg)
706 self.warn(tv)
707
708 def test_corrupt_decrypt(self, tv):
709 self._id = "Wycheproof Corrupt Decrypt EAX Test #" + str(tv.id)
710 if len(tv.iv) == 0 or len(tv.ct) < 1:
711 return
712 cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
713 cipher.update(tv.aad)
714 ct_corrupt = strxor(tv.ct, b"\x00" * (len(tv.ct) - 1) + b"\x01")
715 self.assertRaises(ValueError, cipher.decrypt_and_verify, ct_corrupt, tv.tag)
716
717 def runTest(self):
718
719 for tv in self.tv:
720 self.test_encrypt(tv)
721 self.test_decrypt(tv)
722 self.test_corrupt_decrypt(tv)
723
724
725 class TestOtherCiphers(unittest.TestCase):
726
727 @classmethod
728 def create_test(cls, name, factory, key_size):
729
730 def test_template(self, factory=factory, key_size=key_size):
731 cipher = factory.new(get_tag_random("cipher", key_size),
732 factory.MODE_EAX,
733 nonce=b"nonce")
734 ct, mac = cipher.encrypt_and_digest(b"plaintext")
735
736 cipher = factory.new(get_tag_random("cipher", key_size),
737 factory.MODE_EAX,
738 nonce=b"nonce")
739 pt2 = cipher.decrypt_and_verify(ct, mac)
740
741 self.assertEqual(b"plaintext", pt2)
742
743 setattr(cls, "test_" + name, test_template)
744
745
746 from Crypto.Cipher import DES, DES3, ARC2, CAST, Blowfish
747
748 TestOtherCiphers.create_test("DES_" + str(DES.key_size), DES, DES.key_size)
749 for ks in DES3.key_size:
750 TestOtherCiphers.create_test("DES3_" + str(ks), DES3, ks)
751 for ks in ARC2.key_size:
752 TestOtherCiphers.create_test("ARC2_" + str(ks), ARC2, ks)
753 for ks in CAST.key_size:
754 TestOtherCiphers.create_test("CAST_" + str(ks), CAST, ks)
755 for ks in Blowfish.key_size:
756 TestOtherCiphers.create_test("Blowfish_" + str(ks), Blowfish, ks)
757
758
759 def get_tests(config={}):
760 wycheproof_warnings = config.get('wycheproof_warnings')
761
762 tests = []
763 tests += list_test_cases(EaxTests)
764 tests += list_test_cases(EaxFSMTests)
765 tests += [ TestVectorsPaper() ]
766 tests += [ TestVectorsWycheproof(wycheproof_warnings) ]
767 tests += list_test_cases(TestOtherCiphers)
768 return tests
769
770
771 if __name__ == '__main__':
772 suite = lambda: unittest.TestSuite(get_tests())
773 unittest.main(defaultTest='suite')