test_CCM.py 34 KB

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