test_CCM.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 Crypto.SelfTest.st_common import list_test_cases
  32. from Crypto.Util.py3compat import unhexlify, tobytes, bchr, b
  33. from Crypto.Cipher import AES
  34. from Crypto.Hash import SHAKE128
  35. def get_tag_random(tag, length):
  36. return SHAKE128.new(data=tobytes(tag)).read(length)
  37. class CcmTests(unittest.TestCase):
  38. key_128 = get_tag_random("key_128", 16)
  39. nonce_96 = get_tag_random("nonce_128", 12)
  40. data_128 = get_tag_random("data_128", 16)
  41. def test_loopback_128(self):
  42. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  43. pt = get_tag_random("plaintext", 16 * 100)
  44. ct = cipher.encrypt(pt)
  45. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  46. pt2 = cipher.decrypt(ct)
  47. self.assertEqual(pt, pt2)
  48. def test_nonce(self):
  49. # If not passed, the nonce is created randomly
  50. cipher = AES.new(self.key_128, AES.MODE_CCM)
  51. nonce1 = cipher.nonce
  52. cipher = AES.new(self.key_128, AES.MODE_CCM)
  53. nonce2 = cipher.nonce
  54. self.assertEqual(len(nonce1), 11)
  55. self.assertNotEqual(nonce1, nonce2)
  56. cipher = AES.new(self.key_128, AES.MODE_CCM, self.nonce_96)
  57. ct = cipher.encrypt(self.data_128)
  58. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  59. self.assertEquals(ct, cipher.encrypt(self.data_128))
  60. def test_nonce_must_be_bytes(self):
  61. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
  62. nonce=u'test12345678')
  63. def test_nonce_length(self):
  64. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
  65. nonce=b(""))
  66. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
  67. nonce=bchr(1) * 6)
  68. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
  69. nonce=bchr(1) * 14)
  70. for x in range(7, 13 + 1):
  71. AES.new(self.key_128, AES.MODE_CCM, nonce=bchr(1) * x)
  72. def test_block_size(self):
  73. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  74. self.assertEqual(cipher.block_size, AES.block_size)
  75. def test_nonce_attribute(self):
  76. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  77. self.assertEqual(cipher.nonce, self.nonce_96)
  78. # By default, a 11 bytes long nonce is randomly generated
  79. nonce1 = AES.new(self.key_128, AES.MODE_CCM).nonce
  80. nonce2 = AES.new(self.key_128, AES.MODE_CCM).nonce
  81. self.assertEqual(len(nonce1), 11)
  82. self.assertNotEqual(nonce1, nonce2)
  83. def test_unknown_parameters(self):
  84. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
  85. self.nonce_96, 7)
  86. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_CCM,
  87. nonce=self.nonce_96, unknown=7)
  88. # But some are only known by the base cipher
  89. # (e.g. use_aesni consumed by the AES module)
  90. AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  91. use_aesni=False)
  92. def test_null_encryption_decryption(self):
  93. for func in "encrypt", "decrypt":
  94. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  95. result = getattr(cipher, func)(b(""))
  96. self.assertEqual(result, b(""))
  97. def test_either_encrypt_or_decrypt(self):
  98. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  99. cipher.encrypt(b(""))
  100. self.assertRaises(TypeError, cipher.decrypt, b(""))
  101. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  102. cipher.decrypt(b(""))
  103. self.assertRaises(TypeError, cipher.encrypt, b(""))
  104. def test_data_must_be_bytes(self):
  105. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  106. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  107. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  108. self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*')
  109. def test_mac_len(self):
  110. # Invalid MAC length
  111. for mac_len in xrange(3, 17 + 1, 2):
  112. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CCM,
  113. nonce=self.nonce_96, mac_len=mac_len)
  114. # Valid MAC length
  115. for mac_len in xrange(4, 16 + 1, 2):
  116. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  117. mac_len=mac_len)
  118. _, mac = cipher.encrypt_and_digest(self.data_128)
  119. self.assertEqual(len(mac), mac_len)
  120. # Default MAC length
  121. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  122. _, mac = cipher.encrypt_and_digest(self.data_128)
  123. self.assertEqual(len(mac), 16)
  124. def test_invalid_mac(self):
  125. from Crypto.Util.strxor import strxor_c
  126. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  127. ct, mac = cipher.encrypt_and_digest(self.data_128)
  128. invalid_mac = strxor_c(mac, 0x01)
  129. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  130. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  131. invalid_mac)
  132. def test_hex_mac(self):
  133. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  134. mac_hex = cipher.hexdigest()
  135. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  136. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  137. cipher.hexverify(mac_hex)
  138. def test_longer_assoc_data_than_declared(self):
  139. # More than zero
  140. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  141. assoc_len=0)
  142. self.assertRaises(ValueError, cipher.update, b("1"))
  143. # Too large
  144. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  145. assoc_len=15)
  146. self.assertRaises(ValueError, cipher.update, self.data_128)
  147. def test_shorter_assoc_data_than_expected(self):
  148. # With plaintext
  149. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  150. assoc_len=17)
  151. cipher.update(self.data_128)
  152. self.assertRaises(ValueError, cipher.encrypt, self.data_128)
  153. # With empty plaintext
  154. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  155. assoc_len=17)
  156. cipher.update(self.data_128)
  157. self.assertRaises(ValueError, cipher.digest)
  158. # With ciphertext
  159. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  160. assoc_len=17)
  161. cipher.update(self.data_128)
  162. self.assertRaises(ValueError, cipher.decrypt, self.data_128)
  163. # With empty ciphertext
  164. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  165. cipher.update(self.data_128)
  166. mac = cipher.digest()
  167. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  168. assoc_len=17)
  169. cipher.update(self.data_128)
  170. self.assertRaises(ValueError, cipher.verify, mac)
  171. def test_shorter_and_longer_plaintext_than_declared(self):
  172. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  173. msg_len=17)
  174. cipher.encrypt(self.data_128)
  175. self.assertRaises(ValueError, cipher.digest)
  176. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  177. msg_len=15)
  178. self.assertRaises(ValueError, cipher.encrypt, self.data_128)
  179. def test_shorter_ciphertext_than_declared(self):
  180. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  181. ct, mac = cipher.encrypt_and_digest(self.data_128)
  182. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  183. msg_len=17)
  184. cipher.decrypt(ct)
  185. self.assertRaises(ValueError, cipher.verify, mac)
  186. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  187. msg_len=15)
  188. self.assertRaises(ValueError, cipher.decrypt, ct)
  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. auth_data = get_tag_random("authenticated data", 127)
  193. plaintext = get_tag_random("plaintext", 127)
  194. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  195. cipher.update(auth_data)
  196. ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext)
  197. def break_up(data, chunk_length):
  198. return [data[i:i+chunk_length] for i in range(0, len(data),
  199. chunk_length)]
  200. # Encryption
  201. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  202. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  203. msg_len=127, assoc_len=127)
  204. for chunk in break_up(auth_data, chunk_length):
  205. cipher.update(chunk)
  206. pt2 = b("")
  207. for chunk in break_up(ciphertext, chunk_length):
  208. pt2 += cipher.decrypt(chunk)
  209. self.assertEqual(plaintext, pt2)
  210. cipher.verify(ref_mac)
  211. # Decryption
  212. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  213. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96,
  214. msg_len=127, assoc_len=127)
  215. for chunk in break_up(auth_data, chunk_length):
  216. cipher.update(chunk)
  217. ct2 = b("")
  218. for chunk in break_up(plaintext, chunk_length):
  219. ct2 += cipher.encrypt(chunk)
  220. self.assertEqual(ciphertext, ct2)
  221. self.assertEquals(cipher.digest(), ref_mac)
  222. class CcmFSMTests(unittest.TestCase):
  223. key_128 = get_tag_random("key_128", 16)
  224. nonce_96 = get_tag_random("nonce_128", 12)
  225. data_128 = get_tag_random("data_128", 16)
  226. def test_valid_init_encrypt_decrypt_digest_verify(self):
  227. # No authenticated data, fixed plaintext
  228. for assoc_len in (None, 0):
  229. for msg_len in (None, len(self.data_128)):
  230. # Verify path INIT->ENCRYPT->DIGEST
  231. cipher = AES.new(self.key_128, AES.MODE_CCM,
  232. nonce=self.nonce_96,
  233. assoc_len=assoc_len,
  234. msg_len=msg_len)
  235. ct = cipher.encrypt(self.data_128)
  236. mac = cipher.digest()
  237. # Verify path INIT->DECRYPT->VERIFY
  238. cipher = AES.new(self.key_128, AES.MODE_CCM,
  239. nonce=self.nonce_96,
  240. assoc_len=assoc_len,
  241. msg_len=msg_len)
  242. cipher.decrypt(ct)
  243. cipher.verify(mac)
  244. def test_valid_init_update_digest_verify(self):
  245. # No plaintext, fixed authenticated data
  246. for assoc_len in (None, len(self.data_128)):
  247. for msg_len in (None, 0):
  248. # Verify path INIT->UPDATE->DIGEST
  249. cipher = AES.new(self.key_128, AES.MODE_CCM,
  250. nonce=self.nonce_96,
  251. assoc_len=assoc_len,
  252. msg_len=msg_len)
  253. cipher.update(self.data_128)
  254. mac = cipher.digest()
  255. # Verify path INIT->UPDATE->VERIFY
  256. cipher = AES.new(self.key_128, AES.MODE_CCM,
  257. nonce=self.nonce_96,
  258. assoc_len=assoc_len,
  259. msg_len=msg_len)
  260. cipher.update(self.data_128)
  261. cipher.verify(mac)
  262. def test_valid_full_path(self):
  263. # Fixed authenticated data, fixed plaintext
  264. for assoc_len in (None, len(self.data_128)):
  265. for msg_len in (None, len(self.data_128)):
  266. # Verify path INIT->UPDATE->ENCRYPT->DIGEST
  267. cipher = AES.new(self.key_128, AES.MODE_CCM,
  268. nonce=self.nonce_96,
  269. assoc_len=assoc_len,
  270. msg_len=msg_len)
  271. cipher.update(self.data_128)
  272. ct = cipher.encrypt(self.data_128)
  273. mac = cipher.digest()
  274. # Verify path INIT->UPDATE->DECRYPT->VERIFY
  275. cipher = AES.new(self.key_128, AES.MODE_CCM,
  276. nonce=self.nonce_96,
  277. assoc_len=assoc_len,
  278. msg_len=msg_len)
  279. cipher.update(self.data_128)
  280. cipher.decrypt(ct)
  281. cipher.verify(mac)
  282. def test_valid_init_digest(self):
  283. # Verify path INIT->DIGEST
  284. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  285. cipher.digest()
  286. def test_valid_init_verify(self):
  287. # Verify path INIT->VERIFY
  288. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  289. mac = cipher.digest()
  290. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  291. cipher.verify(mac)
  292. def test_valid_multiple_encrypt_or_decrypt(self):
  293. # Only possible if msg_len is declared in advance
  294. for method_name in "encrypt", "decrypt":
  295. for auth_data in (None, b("333"), self.data_128,
  296. self.data_128 + b("3")):
  297. if auth_data is None:
  298. assoc_len = None
  299. else:
  300. assoc_len = len(auth_data)
  301. cipher = AES.new(self.key_128, AES.MODE_CCM,
  302. nonce=self.nonce_96,
  303. msg_len=64,
  304. assoc_len=assoc_len)
  305. if auth_data is not None:
  306. cipher.update(auth_data)
  307. method = getattr(cipher, method_name)
  308. method(self.data_128)
  309. method(self.data_128)
  310. method(self.data_128)
  311. method(self.data_128)
  312. def test_valid_multiple_digest_or_verify(self):
  313. # Multiple calls to digest
  314. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  315. cipher.update(self.data_128)
  316. first_mac = cipher.digest()
  317. for x in xrange(4):
  318. self.assertEqual(first_mac, cipher.digest())
  319. # Multiple calls to verify
  320. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  321. cipher.update(self.data_128)
  322. for x in xrange(5):
  323. cipher.verify(first_mac)
  324. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  325. # encrypt_and_digest
  326. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  327. cipher.update(self.data_128)
  328. ct, mac = cipher.encrypt_and_digest(self.data_128)
  329. # decrypt_and_verify
  330. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  331. cipher.update(self.data_128)
  332. pt = cipher.decrypt_and_verify(ct, mac)
  333. self.assertEqual(self.data_128, pt)
  334. def test_invalid_multiple_encrypt_decrypt_without_msg_len(self):
  335. # Once per method, with or without assoc. data
  336. for method_name in "encrypt", "decrypt":
  337. for assoc_data_present in (True, False):
  338. cipher = AES.new(self.key_128, AES.MODE_CCM,
  339. nonce=self.nonce_96)
  340. if assoc_data_present:
  341. cipher.update(self.data_128)
  342. method = getattr(cipher, method_name)
  343. method(self.data_128)
  344. self.assertRaises(TypeError, method, self.data_128)
  345. def test_invalid_mixing_encrypt_decrypt(self):
  346. # Once per method, with or without assoc. data
  347. for method1_name, method2_name in (("encrypt", "decrypt"),
  348. ("decrypt", "encrypt")):
  349. for assoc_data_present in (True, False):
  350. cipher = AES.new(self.key_128, AES.MODE_CCM,
  351. nonce=self.nonce_96,
  352. msg_len=32)
  353. if assoc_data_present:
  354. cipher.update(self.data_128)
  355. getattr(cipher, method1_name)(self.data_128)
  356. self.assertRaises(TypeError, getattr(cipher, method2_name),
  357. self.data_128)
  358. def test_invalid_encrypt_or_update_after_digest(self):
  359. for method_name in "encrypt", "update":
  360. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  361. cipher.encrypt(self.data_128)
  362. cipher.digest()
  363. self.assertRaises(TypeError, getattr(cipher, method_name),
  364. self.data_128)
  365. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  366. cipher.encrypt_and_digest(self.data_128)
  367. def test_invalid_decrypt_or_update_after_verify(self):
  368. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  369. ct = cipher.encrypt(self.data_128)
  370. mac = cipher.digest()
  371. for method_name in "decrypt", "update":
  372. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  373. cipher.decrypt(ct)
  374. cipher.verify(mac)
  375. self.assertRaises(TypeError, getattr(cipher, method_name),
  376. self.data_128)
  377. cipher = AES.new(self.key_128, AES.MODE_CCM, nonce=self.nonce_96)
  378. cipher.decrypt_and_verify(ct, mac)
  379. self.assertRaises(TypeError, getattr(cipher, method_name),
  380. self.data_128)
  381. class TestVectors(unittest.TestCase):
  382. """Class exercising the CCM test vectors found in Appendix C
  383. of NIST SP 800-38C and in RFC 3610"""
  384. # List of test vectors, each made up of:
  385. # - authenticated data
  386. # - plaintext
  387. # - ciphertext
  388. # - MAC
  389. # - AES key
  390. # - nonce
  391. test_vectors = [
  392. # NIST SP 800 38C
  393. ( '0001020304050607',
  394. '20212223',
  395. '7162015b',
  396. '4dac255d',
  397. '404142434445464748494a4b4c4d4e4f',
  398. '10111213141516'),
  399. ( '000102030405060708090a0b0c0d0e0f',
  400. '202122232425262728292a2b2c2d2e2f',
  401. 'd2a1f0e051ea5f62081a7792073d593d',
  402. '1fc64fbfaccd',
  403. '404142434445464748494a4b4c4d4e4f',
  404. '1011121314151617'),
  405. ( '000102030405060708090a0b0c0d0e0f10111213',
  406. '202122232425262728292a2b2c2d2e2f3031323334353637',
  407. 'e3b201a9f5b71a7a9b1ceaeccd97e70b6176aad9a4428aa5',
  408. '484392fbc1b09951',
  409. '404142434445464748494a4b4c4d4e4f',
  410. '101112131415161718191a1b'),
  411. ( (''.join(["%02X" % (x*16+y) for x in xrange(0,16) for y in xrange(0,16)]))*256,
  412. '202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f',
  413. '69915dad1e84c6376a68c2967e4dab615ae0fd1faec44cc484828529463ccf72',
  414. 'b4ac6bec93e8598e7f0dadbcea5b',
  415. '404142434445464748494a4b4c4d4e4f',
  416. '101112131415161718191a1b1c'),
  417. # RFC3610
  418. ( '0001020304050607',
  419. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e',
  420. '588c979a61c663d2f066d0c2c0f989806d5f6b61dac384',
  421. '17e8d12cfdf926e0',
  422. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  423. '00000003020100a0a1a2a3a4a5'),
  424. (
  425. '0001020304050607',
  426. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  427. '72c91a36e135f8cf291ca894085c87e3cc15c439c9e43a3b',
  428. 'a091d56e10400916',
  429. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  430. '00000004030201a0a1a2a3a4a5'),
  431. ( '0001020304050607',
  432. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20',
  433. '51b1e5f44a197d1da46b0f8e2d282ae871e838bb64da859657',
  434. '4adaa76fbd9fb0c5',
  435. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  436. '00000005040302A0A1A2A3A4A5'),
  437. ( '000102030405060708090a0b',
  438. '0c0d0e0f101112131415161718191a1b1c1d1e',
  439. 'a28c6865939a9a79faaa5c4c2a9d4a91cdac8c',
  440. '96c861b9c9e61ef1',
  441. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  442. '00000006050403a0a1a2a3a4a5'),
  443. ( '000102030405060708090a0b',
  444. '0c0d0e0f101112131415161718191a1b1c1d1e1f',
  445. 'dcf1fb7b5d9e23fb9d4e131253658ad86ebdca3e',
  446. '51e83f077d9c2d93',
  447. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  448. '00000007060504a0a1a2a3a4a5'),
  449. ( '000102030405060708090a0b',
  450. '0c0d0e0f101112131415161718191a1b1c1d1e1f20',
  451. '6fc1b011f006568b5171a42d953d469b2570a4bd87',
  452. '405a0443ac91cb94',
  453. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  454. '00000008070605a0a1a2a3a4a5'),
  455. ( '0001020304050607',
  456. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e',
  457. '0135d1b2c95f41d5d1d4fec185d166b8094e999dfed96c',
  458. '048c56602c97acbb7490',
  459. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  460. '00000009080706a0a1a2a3a4a5'),
  461. ( '0001020304050607',
  462. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f',
  463. '7b75399ac0831dd2f0bbd75879a2fd8f6cae6b6cd9b7db24',
  464. 'c17b4433f434963f34b4',
  465. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  466. '0000000a090807a0a1a2a3a4a5'),
  467. ( '0001020304050607',
  468. '08090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20',
  469. '82531a60cc24945a4b8279181ab5c84df21ce7f9b73f42e197',
  470. 'ea9c07e56b5eb17e5f4e',
  471. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  472. '0000000b0a0908a0a1a2a3a4a5'),
  473. ( '000102030405060708090a0b',
  474. '0c0d0e0f101112131415161718191a1b1c1d1e',
  475. '07342594157785152b074098330abb141b947b',
  476. '566aa9406b4d999988dd',
  477. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  478. '0000000c0b0a09a0a1a2a3a4a5'),
  479. ( '000102030405060708090a0b',
  480. '0c0d0e0f101112131415161718191a1b1c1d1e1f',
  481. '676bb20380b0e301e8ab79590a396da78b834934',
  482. 'f53aa2e9107a8b6c022c',
  483. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  484. '0000000d0c0b0aa0a1a2a3a4a5'),
  485. ( '000102030405060708090a0b',
  486. '0c0d0e0f101112131415161718191a1b1c1d1e1f20',
  487. 'c0ffa0d6f05bdb67f24d43a4338d2aa4bed7b20e43',
  488. 'cd1aa31662e7ad65d6db',
  489. 'c0c1c2c3c4c5c6c7c8c9cacbcccdcecf',
  490. '0000000e0d0c0ba0a1a2a3a4a5'),
  491. ( '0be1a88bace018b1',
  492. '08e8cf97d820ea258460e96ad9cf5289054d895ceac47c',
  493. '4cb97f86a2a4689a877947ab8091ef5386a6ffbdd080f8',
  494. 'e78cf7cb0cddd7b3',
  495. 'd7828d13b2b0bdc325a76236df93cc6b',
  496. '00412b4ea9cdbe3c9696766cfa'),
  497. ( '63018f76dc8a1bcb',
  498. '9020ea6f91bdd85afa0039ba4baff9bfb79c7028949cd0ec',
  499. '4ccb1e7ca981befaa0726c55d378061298c85c92814abc33',
  500. 'c52ee81d7d77c08a',
  501. 'd7828d13b2b0bdc325a76236df93cc6b',
  502. '0033568ef7b2633c9696766cfa'),
  503. ( 'aa6cfa36cae86b40',
  504. 'b916e0eacc1c00d7dcec68ec0b3bbb1a02de8a2d1aa346132e',
  505. 'b1d23a2220ddc0ac900d9aa03c61fcf4a559a4417767089708',
  506. 'a776796edb723506',
  507. 'd7828d13b2b0bdc325a76236df93cc6b',
  508. '00103fe41336713c9696766cfa'),
  509. ( 'd0d0735c531e1becf049c244',
  510. '12daac5630efa5396f770ce1a66b21f7b2101c',
  511. '14d253c3967b70609b7cbb7c49916028324526',
  512. '9a6f49975bcadeaf',
  513. 'd7828d13b2b0bdc325a76236df93cc6b',
  514. '00764c63b8058e3c9696766cfa'),
  515. ( '77b60f011c03e1525899bcae',
  516. 'e88b6a46c78d63e52eb8c546efb5de6f75e9cc0d',
  517. '5545ff1a085ee2efbf52b2e04bee1e2336c73e3f',
  518. '762c0c7744fe7e3c',
  519. 'd7828d13b2b0bdc325a76236df93cc6b',
  520. '00f8b678094e3b3c9696766cfa'),
  521. ( 'cd9044d2b71fdb8120ea60c0',
  522. '6435acbafb11a82e2f071d7ca4a5ebd93a803ba87f',
  523. '009769ecabdf48625594c59251e6035722675e04c8',
  524. '47099e5ae0704551',
  525. 'd7828d13b2b0bdc325a76236df93cc6b',
  526. '00d560912d3f703c9696766cfa'),
  527. ( 'd85bc7e69f944fb8',
  528. '8a19b950bcf71a018e5e6701c91787659809d67dbedd18',
  529. 'bc218daa947427b6db386a99ac1aef23ade0b52939cb6a',
  530. '637cf9bec2408897c6ba',
  531. 'd7828d13b2b0bdc325a76236df93cc6b',
  532. '0042fff8f1951c3c9696766cfa'),
  533. ( '74a0ebc9069f5b37',
  534. '1761433c37c5a35fc1f39f406302eb907c6163be38c98437',
  535. '5810e6fd25874022e80361a478e3e9cf484ab04f447efff6',
  536. 'f0a477cc2fc9bf548944',
  537. 'd7828d13b2b0bdc325a76236df93cc6b',
  538. '00920f40e56cdc3c9696766cfa'),
  539. ( '44a3aa3aae6475ca',
  540. 'a434a8e58500c6e41530538862d686ea9e81301b5ae4226bfa',
  541. 'f2beed7bc5098e83feb5b31608f8e29c38819a89c8e776f154',
  542. '4d4151a4ed3a8b87b9ce',
  543. 'd7828d13b2b0bdc325a76236df93cc6b',
  544. '0027ca0c7120bc3c9696766cfa'),
  545. ( 'ec46bb63b02520c33c49fd70',
  546. 'b96b49e21d621741632875db7f6c9243d2d7c2',
  547. '31d750a09da3ed7fddd49a2032aabf17ec8ebf',
  548. '7d22c8088c666be5c197',
  549. 'd7828d13b2b0bdc325a76236df93cc6b',
  550. '005b8ccbcd9af83c9696766cfa'),
  551. ( '47a65ac78b3d594227e85e71',
  552. 'e2fcfbb880442c731bf95167c8ffd7895e337076',
  553. 'e882f1dbd38ce3eda7c23f04dd65071eb41342ac',
  554. 'df7e00dccec7ae52987d',
  555. 'd7828d13b2b0bdc325a76236df93cc6b',
  556. '003ebe94044b9a3c9696766cfa'),
  557. ( '6e37a6ef546d955d34ab6059',
  558. 'abf21c0b02feb88f856df4a37381bce3cc128517d4',
  559. 'f32905b88a641b04b9c9ffb58cc390900f3da12ab1',
  560. '6dce9e82efa16da62059',
  561. 'd7828d13b2b0bdc325a76236df93cc6b',
  562. '008d493b30ae8b3c9696766cfa'),
  563. ]
  564. for index, tv in enumerate(test_vectors):
  565. test_vectors[index] = (unhexlify(x) for x in tv)
  566. def runTest(self):
  567. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  568. # Encrypt
  569. cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=len(mac))
  570. cipher.update(assoc_data)
  571. ct2, mac2 = cipher.encrypt_and_digest(pt)
  572. self.assertEqual(ct, ct2)
  573. self.assertEqual(mac, mac2)
  574. # Decrypt
  575. cipher = AES.new(key, AES.MODE_CCM, nonce, mac_len=len(mac))
  576. cipher.update(assoc_data)
  577. pt2 = cipher.decrypt_and_verify(ct, mac)
  578. self.assertEqual(pt, pt2)
  579. def get_tests(config={}):
  580. tests = []
  581. tests += list_test_cases(CcmTests)
  582. tests += list_test_cases(CcmFSMTests)
  583. tests += [TestVectors()]
  584. return tests
  585. if __name__ == '__main__':
  586. suite = lambda: unittest.TestSuite(get_tests())
  587. unittest.main(defaultTest='suite')