test_EAX.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  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. import unittest
  31. from binascii import unhexlify
  32. from Crypto.SelfTest.st_common import list_test_cases
  33. from Crypto.SelfTest.loader import load_test_vectors_wycheproof
  34. from Crypto.Util.py3compat import tobytes, bchr
  35. from Crypto.Cipher import AES, DES3
  36. from Crypto.Hash import SHAKE128
  37. from Crypto.Util.strxor import strxor
  38. def get_tag_random(tag, length):
  39. return SHAKE128.new(data=tobytes(tag)).read(length)
  40. class EaxTests(unittest.TestCase):
  41. key_128 = get_tag_random("key_128", 16)
  42. key_192 = get_tag_random("key_192", 16)
  43. nonce_96 = get_tag_random("nonce_128", 12)
  44. data_128 = get_tag_random("data_128", 16)
  45. def test_loopback_128(self):
  46. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  47. pt = get_tag_random("plaintext", 16 * 100)
  48. ct = cipher.encrypt(pt)
  49. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  50. pt2 = cipher.decrypt(ct)
  51. self.assertEqual(pt, pt2)
  52. def test_loopback_64(self):
  53. cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96)
  54. pt = get_tag_random("plaintext", 8 * 100)
  55. ct = cipher.encrypt(pt)
  56. cipher = DES3.new(self.key_192, DES3.MODE_EAX, nonce=self.nonce_96)
  57. pt2 = cipher.decrypt(ct)
  58. self.assertEqual(pt, pt2)
  59. def test_nonce(self):
  60. # If not passed, the nonce is created randomly
  61. cipher = AES.new(self.key_128, AES.MODE_EAX)
  62. nonce1 = cipher.nonce
  63. cipher = AES.new(self.key_128, AES.MODE_EAX)
  64. nonce2 = cipher.nonce
  65. self.assertEqual(len(nonce1), 16)
  66. self.assertNotEqual(nonce1, nonce2)
  67. cipher = AES.new(self.key_128, AES.MODE_EAX, self.nonce_96)
  68. ct = cipher.encrypt(self.data_128)
  69. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  70. self.assertEquals(ct, cipher.encrypt(self.data_128))
  71. def test_nonce_must_be_bytes(self):
  72. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
  73. nonce=u'test12345678')
  74. def test_nonce_length(self):
  75. # nonce can be of any length (but not empty)
  76. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
  77. nonce=b"")
  78. for x in range(1, 128):
  79. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=bchr(1) * x)
  80. cipher.encrypt(bchr(1))
  81. def test_block_size_128(self):
  82. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  83. self.assertEqual(cipher.block_size, AES.block_size)
  84. def test_block_size_64(self):
  85. cipher = DES3.new(self.key_192, AES.MODE_EAX, nonce=self.nonce_96)
  86. self.assertEqual(cipher.block_size, DES3.block_size)
  87. def test_nonce_attribute(self):
  88. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  89. self.assertEqual(cipher.nonce, self.nonce_96)
  90. # By default, a 16 bytes long nonce is randomly generated
  91. nonce1 = AES.new(self.key_128, AES.MODE_EAX).nonce
  92. nonce2 = AES.new(self.key_128, AES.MODE_EAX).nonce
  93. self.assertEqual(len(nonce1), 16)
  94. self.assertNotEqual(nonce1, nonce2)
  95. def test_unknown_parameters(self):
  96. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
  97. self.nonce_96, 7)
  98. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_EAX,
  99. nonce=self.nonce_96, unknown=7)
  100. # But some are only known by the base cipher
  101. # (e.g. use_aesni consumed by the AES module)
  102. AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96,
  103. use_aesni=False)
  104. def test_null_encryption_decryption(self):
  105. for func in "encrypt", "decrypt":
  106. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  107. result = getattr(cipher, func)(b"")
  108. self.assertEqual(result, b"")
  109. def test_either_encrypt_or_decrypt(self):
  110. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  111. cipher.encrypt(b"")
  112. self.assertRaises(TypeError, cipher.decrypt, b"")
  113. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  114. cipher.decrypt(b"")
  115. self.assertRaises(TypeError, cipher.encrypt, b"")
  116. def test_data_must_be_bytes(self):
  117. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  118. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  119. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  120. self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*')
  121. def test_mac_len(self):
  122. # Invalid MAC length
  123. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
  124. nonce=self.nonce_96, mac_len=3)
  125. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_EAX,
  126. nonce=self.nonce_96, mac_len=16+1)
  127. # Valid MAC length
  128. for mac_len in range(5, 16 + 1):
  129. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96,
  130. mac_len=mac_len)
  131. _, mac = cipher.encrypt_and_digest(self.data_128)
  132. self.assertEqual(len(mac), mac_len)
  133. # Default MAC length
  134. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  135. _, mac = cipher.encrypt_and_digest(self.data_128)
  136. self.assertEqual(len(mac), 16)
  137. def test_invalid_mac(self):
  138. from Crypto.Util.strxor import strxor_c
  139. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  140. ct, mac = cipher.encrypt_and_digest(self.data_128)
  141. invalid_mac = strxor_c(mac, 0x01)
  142. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  143. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  144. invalid_mac)
  145. def test_hex_mac(self):
  146. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  147. mac_hex = cipher.hexdigest()
  148. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  149. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  150. cipher.hexverify(mac_hex)
  151. def test_message_chunks(self):
  152. # Validate that both associated data and plaintext/ciphertext
  153. # can be broken up in chunks of arbitrary length
  154. auth_data = get_tag_random("authenticated data", 127)
  155. plaintext = get_tag_random("plaintext", 127)
  156. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  157. cipher.update(auth_data)
  158. ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext)
  159. def break_up(data, chunk_length):
  160. return [data[i:i+chunk_length] for i in range(0, len(data),
  161. chunk_length)]
  162. # Encryption
  163. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  164. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  165. for chunk in break_up(auth_data, chunk_length):
  166. cipher.update(chunk)
  167. pt2 = b""
  168. for chunk in break_up(ciphertext, chunk_length):
  169. pt2 += cipher.decrypt(chunk)
  170. self.assertEqual(plaintext, pt2)
  171. cipher.verify(ref_mac)
  172. # Decryption
  173. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  174. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  175. for chunk in break_up(auth_data, chunk_length):
  176. cipher.update(chunk)
  177. ct2 = b""
  178. for chunk in break_up(plaintext, chunk_length):
  179. ct2 += cipher.encrypt(chunk)
  180. self.assertEqual(ciphertext, ct2)
  181. self.assertEquals(cipher.digest(), ref_mac)
  182. def test_bytearray(self):
  183. # Encrypt
  184. key_ba = bytearray(self.key_128)
  185. nonce_ba = bytearray(self.nonce_96)
  186. header_ba = bytearray(self.data_128)
  187. data_ba = bytearray(self.data_128)
  188. cipher1 = AES.new(self.key_128,
  189. AES.MODE_EAX,
  190. nonce=self.nonce_96)
  191. cipher1.update(self.data_128)
  192. ct = cipher1.encrypt(self.data_128)
  193. tag = cipher1.digest()
  194. cipher2 = AES.new(key_ba,
  195. AES.MODE_EAX,
  196. nonce=nonce_ba)
  197. key_ba[:3] = b'\xFF\xFF\xFF'
  198. nonce_ba[:3] = b'\xFF\xFF\xFF'
  199. cipher2.update(header_ba)
  200. header_ba[:3] = b'\xFF\xFF\xFF'
  201. ct_test = cipher2.encrypt(data_ba)
  202. data_ba[:3] = b'\x99\x99\x99'
  203. tag_test = cipher2.digest()
  204. self.assertEqual(ct, ct_test)
  205. self.assertEqual(tag, tag_test)
  206. self.assertEqual(cipher1.nonce, cipher2.nonce)
  207. # Decrypt
  208. key_ba = bytearray(self.key_128)
  209. nonce_ba = bytearray(self.nonce_96)
  210. header_ba = bytearray(self.data_128)
  211. ct_ba = bytearray(ct)
  212. tag_ba = bytearray(tag)
  213. del data_ba
  214. cipher3 = AES.new(key_ba,
  215. AES.MODE_EAX,
  216. nonce=nonce_ba)
  217. key_ba[:3] = b'\xFF\xFF\xFF'
  218. nonce_ba[:3] = b'\xFF\xFF\xFF'
  219. cipher3.update(header_ba)
  220. header_ba[:3] = b'\xFF\xFF\xFF'
  221. pt_test = cipher3.decrypt(ct_ba)
  222. ct_ba[:3] = b'\xFF\xFF\xFF'
  223. cipher3.verify(tag_ba)
  224. self.assertEqual(pt_test, self.data_128)
  225. def test_memoryview(self):
  226. # Encrypt
  227. key_mv = memoryview(bytearray(self.key_128))
  228. nonce_mv = memoryview(bytearray(self.nonce_96))
  229. header_mv = memoryview(bytearray(self.data_128))
  230. data_mv = memoryview(bytearray(self.data_128))
  231. cipher1 = AES.new(self.key_128,
  232. AES.MODE_EAX,
  233. nonce=self.nonce_96)
  234. cipher1.update(self.data_128)
  235. ct = cipher1.encrypt(self.data_128)
  236. tag = cipher1.digest()
  237. cipher2 = AES.new(key_mv,
  238. AES.MODE_EAX,
  239. nonce=nonce_mv)
  240. key_mv[:3] = b'\xFF\xFF\xFF'
  241. nonce_mv[:3] = b'\xFF\xFF\xFF'
  242. cipher2.update(header_mv)
  243. header_mv[:3] = b'\xFF\xFF\xFF'
  244. ct_test = cipher2.encrypt(data_mv)
  245. data_mv[:3] = b'\x99\x99\x99'
  246. tag_test = cipher2.digest()
  247. self.assertEqual(ct, ct_test)
  248. self.assertEqual(tag, tag_test)
  249. self.assertEqual(cipher1.nonce, cipher2.nonce)
  250. # Decrypt
  251. key_mv = memoryview(bytearray(self.key_128))
  252. nonce_mv = memoryview(bytearray(self.nonce_96))
  253. header_mv = memoryview(bytearray(self.data_128))
  254. ct_mv = memoryview(bytearray(ct))
  255. tag_mv = memoryview(bytearray(tag))
  256. del data_mv
  257. cipher3 = AES.new(key_mv,
  258. AES.MODE_EAX,
  259. nonce=nonce_mv)
  260. key_mv[:3] = b'\xFF\xFF\xFF'
  261. nonce_mv[:3] = b'\xFF\xFF\xFF'
  262. cipher3.update(header_mv)
  263. header_mv[:3] = b'\xFF\xFF\xFF'
  264. pt_test = cipher3.decrypt(ct_mv)
  265. ct_mv[:3] = b'\x99\x99\x99'
  266. cipher3.verify(tag_mv)
  267. self.assertEqual(pt_test, self.data_128)
  268. def test_output_param(self):
  269. pt = b'5' * 16
  270. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  271. ct = cipher.encrypt(pt)
  272. tag = cipher.digest()
  273. output = bytearray(16)
  274. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  275. res = cipher.encrypt(pt, output=output)
  276. self.assertEqual(ct, output)
  277. self.assertEqual(res, None)
  278. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  279. res = cipher.decrypt(ct, output=output)
  280. self.assertEqual(pt, output)
  281. self.assertEqual(res, None)
  282. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  283. res, tag_out = cipher.encrypt_and_digest(pt, output=output)
  284. self.assertEqual(ct, output)
  285. self.assertEqual(res, None)
  286. self.assertEqual(tag, tag_out)
  287. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  288. res = cipher.decrypt_and_verify(ct, tag, output=output)
  289. self.assertEqual(pt, output)
  290. self.assertEqual(res, None)
  291. def test_output_param_memoryview(self):
  292. pt = b'5' * 16
  293. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  294. ct = cipher.encrypt(pt)
  295. output = memoryview(bytearray(16))
  296. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  297. cipher.encrypt(pt, output=output)
  298. self.assertEqual(ct, output)
  299. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  300. cipher.decrypt(ct, output=output)
  301. self.assertEqual(pt, output)
  302. def test_output_param_neg(self):
  303. pt = b'5' * 16
  304. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  305. ct = cipher.encrypt(pt)
  306. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  307. self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
  308. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  309. self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)
  310. shorter_output = bytearray(15)
  311. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  312. self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
  313. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  314. self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
  315. class EaxFSMTests(unittest.TestCase):
  316. key_128 = get_tag_random("key_128", 16)
  317. nonce_96 = get_tag_random("nonce_128", 12)
  318. data_128 = get_tag_random("data_128", 16)
  319. def test_valid_init_encrypt_decrypt_digest_verify(self):
  320. # No authenticated data, fixed plaintext
  321. # Verify path INIT->ENCRYPT->DIGEST
  322. cipher = AES.new(self.key_128, AES.MODE_EAX,
  323. nonce=self.nonce_96)
  324. ct = cipher.encrypt(self.data_128)
  325. mac = cipher.digest()
  326. # Verify path INIT->DECRYPT->VERIFY
  327. cipher = AES.new(self.key_128, AES.MODE_EAX,
  328. nonce=self.nonce_96)
  329. cipher.decrypt(ct)
  330. cipher.verify(mac)
  331. def test_valid_init_update_digest_verify(self):
  332. # No plaintext, fixed authenticated data
  333. # Verify path INIT->UPDATE->DIGEST
  334. cipher = AES.new(self.key_128, AES.MODE_EAX,
  335. nonce=self.nonce_96)
  336. cipher.update(self.data_128)
  337. mac = cipher.digest()
  338. # Verify path INIT->UPDATE->VERIFY
  339. cipher = AES.new(self.key_128, AES.MODE_EAX,
  340. nonce=self.nonce_96)
  341. cipher.update(self.data_128)
  342. cipher.verify(mac)
  343. def test_valid_full_path(self):
  344. # Fixed authenticated data, fixed plaintext
  345. # Verify path INIT->UPDATE->ENCRYPT->DIGEST
  346. cipher = AES.new(self.key_128, AES.MODE_EAX,
  347. nonce=self.nonce_96)
  348. cipher.update(self.data_128)
  349. ct = cipher.encrypt(self.data_128)
  350. mac = cipher.digest()
  351. # Verify path INIT->UPDATE->DECRYPT->VERIFY
  352. cipher = AES.new(self.key_128, AES.MODE_EAX,
  353. nonce=self.nonce_96)
  354. cipher.update(self.data_128)
  355. cipher.decrypt(ct)
  356. cipher.verify(mac)
  357. def test_valid_init_digest(self):
  358. # Verify path INIT->DIGEST
  359. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  360. cipher.digest()
  361. def test_valid_init_verify(self):
  362. # Verify path INIT->VERIFY
  363. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  364. mac = cipher.digest()
  365. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  366. cipher.verify(mac)
  367. def test_valid_multiple_encrypt_or_decrypt(self):
  368. for method_name in "encrypt", "decrypt":
  369. for auth_data in (None, b"333", self.data_128,
  370. self.data_128 + b"3"):
  371. if auth_data is None:
  372. assoc_len = None
  373. else:
  374. assoc_len = len(auth_data)
  375. cipher = AES.new(self.key_128, AES.MODE_EAX,
  376. nonce=self.nonce_96)
  377. if auth_data is not None:
  378. cipher.update(auth_data)
  379. method = getattr(cipher, method_name)
  380. method(self.data_128)
  381. method(self.data_128)
  382. method(self.data_128)
  383. method(self.data_128)
  384. def test_valid_multiple_digest_or_verify(self):
  385. # Multiple calls to digest
  386. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  387. cipher.update(self.data_128)
  388. first_mac = cipher.digest()
  389. for x in range(4):
  390. self.assertEqual(first_mac, cipher.digest())
  391. # Multiple calls to verify
  392. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  393. cipher.update(self.data_128)
  394. for x in range(5):
  395. cipher.verify(first_mac)
  396. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  397. # encrypt_and_digest
  398. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  399. cipher.update(self.data_128)
  400. ct, mac = cipher.encrypt_and_digest(self.data_128)
  401. # decrypt_and_verify
  402. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  403. cipher.update(self.data_128)
  404. pt = cipher.decrypt_and_verify(ct, mac)
  405. self.assertEqual(self.data_128, pt)
  406. def test_invalid_mixing_encrypt_decrypt(self):
  407. # Once per method, with or without assoc. data
  408. for method1_name, method2_name in (("encrypt", "decrypt"),
  409. ("decrypt", "encrypt")):
  410. for assoc_data_present in (True, False):
  411. cipher = AES.new(self.key_128, AES.MODE_EAX,
  412. nonce=self.nonce_96)
  413. if assoc_data_present:
  414. cipher.update(self.data_128)
  415. getattr(cipher, method1_name)(self.data_128)
  416. self.assertRaises(TypeError, getattr(cipher, method2_name),
  417. self.data_128)
  418. def test_invalid_encrypt_or_update_after_digest(self):
  419. for method_name in "encrypt", "update":
  420. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  421. cipher.encrypt(self.data_128)
  422. cipher.digest()
  423. self.assertRaises(TypeError, getattr(cipher, method_name),
  424. self.data_128)
  425. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  426. cipher.encrypt_and_digest(self.data_128)
  427. def test_invalid_decrypt_or_update_after_verify(self):
  428. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  429. ct = cipher.encrypt(self.data_128)
  430. mac = cipher.digest()
  431. for method_name in "decrypt", "update":
  432. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  433. cipher.decrypt(ct)
  434. cipher.verify(mac)
  435. self.assertRaises(TypeError, getattr(cipher, method_name),
  436. self.data_128)
  437. cipher = AES.new(self.key_128, AES.MODE_EAX, nonce=self.nonce_96)
  438. cipher.decrypt_and_verify(ct, mac)
  439. self.assertRaises(TypeError, getattr(cipher, method_name),
  440. self.data_128)
  441. class TestVectorsPaper(unittest.TestCase):
  442. """Class exercising the EAX test vectors found in
  443. http://www.cs.ucdavis.edu/~rogaway/papers/eax.pdf"""
  444. test_vectors_hex = [
  445. ( '6bfb914fd07eae6b',
  446. '',
  447. '',
  448. 'e037830e8389f27b025a2d6527e79d01',
  449. '233952dee4d5ed5f9b9c6d6ff80ff478',
  450. '62EC67F9C3A4A407FCB2A8C49031A8B3'
  451. ),
  452. (
  453. 'fa3bfd4806eb53fa',
  454. 'f7fb',
  455. '19dd',
  456. '5c4c9331049d0bdab0277408f67967e5',
  457. '91945d3f4dcbee0bf45ef52255f095a4',
  458. 'BECAF043B0A23D843194BA972C66DEBD'
  459. ),
  460. ( '234a3463c1264ac6',
  461. '1a47cb4933',
  462. 'd851d5bae0',
  463. '3a59f238a23e39199dc9266626c40f80',
  464. '01f74ad64077f2e704c0f60ada3dd523',
  465. '70C3DB4F0D26368400A10ED05D2BFF5E'
  466. ),
  467. (
  468. '33cce2eabff5a79d',
  469. '481c9e39b1',
  470. '632a9d131a',
  471. 'd4c168a4225d8e1ff755939974a7bede',
  472. 'd07cf6cbb7f313bdde66b727afd3c5e8',
  473. '8408DFFF3C1A2B1292DC199E46B7D617'
  474. ),
  475. (
  476. 'aeb96eaebe2970e9',
  477. '40d0c07da5e4',
  478. '071dfe16c675',
  479. 'cb0677e536f73afe6a14b74ee49844dd',
  480. '35b6d0580005bbc12b0587124557d2c2',
  481. 'FDB6B06676EEDC5C61D74276E1F8E816'
  482. ),
  483. (
  484. 'd4482d1ca78dce0f',
  485. '4de3b35c3fc039245bd1fb7d',
  486. '835bb4f15d743e350e728414',
  487. 'abb8644fd6ccb86947c5e10590210a4f',
  488. 'bd8e6e11475e60b268784c38c62feb22',
  489. '6EAC5C93072D8E8513F750935E46DA1B'
  490. ),
  491. (
  492. '65d2017990d62528',
  493. '8b0a79306c9ce7ed99dae4f87f8dd61636',
  494. '02083e3979da014812f59f11d52630da30',
  495. '137327d10649b0aa6e1c181db617d7f2',
  496. '7c77d6e813bed5ac98baa417477a2e7d',
  497. '1A8C98DCD73D38393B2BF1569DEEFC19'
  498. ),
  499. (
  500. '54b9f04e6a09189a',
  501. '1bda122bce8a8dbaf1877d962b8592dd2d56',
  502. '2ec47b2c4954a489afc7ba4897edcdae8cc3',
  503. '3b60450599bd02c96382902aef7f832a',
  504. '5fff20cafab119ca2fc73549e20f5b0d',
  505. 'DDE59B97D722156D4D9AFF2BC7559826'
  506. ),
  507. (
  508. '899a175897561d7e',
  509. '6cf36720872b8513f6eab1a8a44438d5ef11',
  510. '0de18fd0fdd91e7af19f1d8ee8733938b1e8',
  511. 'e7f6d2231618102fdb7fe55ff1991700',
  512. 'a4a4782bcffd3ec5e7ef6d8c34a56123',
  513. 'B781FCF2F75FA5A8DE97A9CA48E522EC'
  514. ),
  515. (
  516. '126735fcc320d25a',
  517. 'ca40d7446e545ffaed3bd12a740a659ffbbb3ceab7',
  518. 'cb8920f87a6c75cff39627b56e3ed197c552d295a7',
  519. 'cfc46afc253b4652b1af3795b124ab6e',
  520. '8395fcf1e95bebd697bd010bc766aac3',
  521. '22E7ADD93CFC6393C57EC0B3C17D6B44'
  522. ),
  523. ]
  524. test_vectors = [[unhexlify(x) for x in tv] for tv in test_vectors_hex]
  525. def runTest(self):
  526. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  527. # Encrypt
  528. cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac))
  529. cipher.update(assoc_data)
  530. ct2, mac2 = cipher.encrypt_and_digest(pt)
  531. self.assertEqual(ct, ct2)
  532. self.assertEqual(mac, mac2)
  533. # Decrypt
  534. cipher = AES.new(key, AES.MODE_EAX, nonce, mac_len=len(mac))
  535. cipher.update(assoc_data)
  536. pt2 = cipher.decrypt_and_verify(ct, mac)
  537. self.assertEqual(pt, pt2)
  538. class TestVectorsWycheproof(unittest.TestCase):
  539. def __init__(self, wycheproof_warnings):
  540. unittest.TestCase.__init__(self)
  541. self._wycheproof_warnings = wycheproof_warnings
  542. self._id = "None"
  543. def setUp(self):
  544. def filter_tag(group):
  545. return group['tagSize'] // 8
  546. self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
  547. "aes_eax_test.json",
  548. "Wycheproof EAX",
  549. group_tag={'tag_size': filter_tag})
  550. def shortDescription(self):
  551. return self._id
  552. def warn(self, tv):
  553. if tv.warning and self._wycheproof_warnings:
  554. import warnings
  555. warnings.warn("Wycheproof warning: %s (%s)" % (self._id, tv.comment))
  556. def test_encrypt(self, tv):
  557. self._id = "Wycheproof Encrypt EAX Test #" + str(tv.id)
  558. try:
  559. cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
  560. except ValueError as e:
  561. assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e)
  562. return
  563. cipher.update(tv.aad)
  564. ct, tag = cipher.encrypt_and_digest(tv.msg)
  565. if tv.valid:
  566. self.assertEqual(ct, tv.ct)
  567. self.assertEqual(tag, tv.tag)
  568. self.warn(tv)
  569. def test_decrypt(self, tv):
  570. self._id = "Wycheproof Decrypt EAX Test #" + str(tv.id)
  571. try:
  572. cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
  573. except ValueError as e:
  574. assert len(tv.iv) == 0 and "Nonce cannot be empty" in str(e)
  575. return
  576. cipher.update(tv.aad)
  577. try:
  578. pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
  579. except ValueError:
  580. assert not tv.valid
  581. else:
  582. assert tv.valid
  583. self.assertEqual(pt, tv.msg)
  584. self.warn(tv)
  585. def test_corrupt_decrypt(self, tv):
  586. self._id = "Wycheproof Corrupt Decrypt EAX Test #" + str(tv.id)
  587. if len(tv.iv) == 0 or len(tv.ct) < 1:
  588. return
  589. cipher = AES.new(tv.key, AES.MODE_EAX, tv.iv, mac_len=tv.tag_size)
  590. cipher.update(tv.aad)
  591. ct_corrupt = strxor(tv.ct, b"\x00" * (len(tv.ct) - 1) + b"\x01")
  592. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct_corrupt, tv.tag)
  593. def runTest(self):
  594. for tv in self.tv:
  595. self.test_encrypt(tv)
  596. self.test_decrypt(tv)
  597. self.test_corrupt_decrypt(tv)
  598. class TestOtherCiphers(unittest.TestCase):
  599. @classmethod
  600. def create_test(cls, name, factory, key_size):
  601. def test_template(self, factory=factory, key_size=key_size):
  602. cipher = factory.new(get_tag_random("cipher", key_size),
  603. factory.MODE_EAX,
  604. nonce=b"nonce")
  605. ct, mac = cipher.encrypt_and_digest(b"plaintext")
  606. cipher = factory.new(get_tag_random("cipher", key_size),
  607. factory.MODE_EAX,
  608. nonce=b"nonce")
  609. pt2 = cipher.decrypt_and_verify(ct, mac)
  610. self.assertEqual(b"plaintext", pt2)
  611. setattr(cls, "test_" + name, test_template)
  612. from Crypto.Cipher import DES, DES3, ARC2, CAST, Blowfish
  613. TestOtherCiphers.create_test("DES_" + str(DES.key_size), DES, DES.key_size)
  614. for ks in DES3.key_size:
  615. TestOtherCiphers.create_test("DES3_" + str(ks), DES3, ks)
  616. for ks in ARC2.key_size:
  617. TestOtherCiphers.create_test("ARC2_" + str(ks), ARC2, ks)
  618. for ks in CAST.key_size:
  619. TestOtherCiphers.create_test("CAST_" + str(ks), CAST, ks)
  620. for ks in Blowfish.key_size:
  621. TestOtherCiphers.create_test("Blowfish_" + str(ks), Blowfish, ks)
  622. def get_tests(config={}):
  623. wycheproof_warnings = config.get('wycheproof_warnings')
  624. tests = []
  625. tests += list_test_cases(EaxTests)
  626. tests += list_test_cases(EaxFSMTests)
  627. tests += [ TestVectorsPaper() ]
  628. tests += [ TestVectorsWycheproof(wycheproof_warnings) ]
  629. tests += list_test_cases(TestOtherCiphers)
  630. return tests
  631. if __name__ == '__main__':
  632. suite = lambda: unittest.TestSuite(get_tests())
  633. unittest.main(defaultTest='suite')