test_OCB.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2014, 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 os
  31. import re
  32. import unittest
  33. from binascii import hexlify, unhexlify
  34. from Cryptodome.Util.py3compat import b, tobytes, bchr, _memoryview
  35. from Cryptodome.Util.strxor import strxor_c
  36. from Cryptodome.Util.number import long_to_bytes
  37. from Cryptodome.SelfTest.st_common import list_test_cases
  38. from Cryptodome.Cipher import AES
  39. from Cryptodome.Hash import SHAKE128
  40. def get_tag_random(tag, length):
  41. return SHAKE128.new(data=tobytes(tag)).read(length)
  42. class OcbTests(unittest.TestCase):
  43. key_128 = get_tag_random("key_128", 16)
  44. nonce_96 = get_tag_random("nonce_128", 12)
  45. data_128 = get_tag_random("data_128", 16)
  46. def test_loopback_128(self):
  47. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  48. pt = get_tag_random("plaintext", 16 * 100)
  49. ct, mac = cipher.encrypt_and_digest(pt)
  50. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  51. pt2 = cipher.decrypt_and_verify(ct, mac)
  52. self.assertEqual(pt, pt2)
  53. def test_nonce(self):
  54. # Nonce is optional
  55. AES.new(self.key_128, AES.MODE_OCB)
  56. cipher = AES.new(self.key_128, AES.MODE_OCB, self.nonce_96)
  57. ct = cipher.encrypt(self.data_128)
  58. cipher = AES.new(self.key_128, AES.MODE_OCB, 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_OCB,
  62. nonce=u'test12345678')
  63. def test_nonce_length(self):
  64. # nonce cannot be empty
  65. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB,
  66. nonce=b(""))
  67. # nonce can be up to 15 bytes long
  68. for length in range(1, 16):
  69. AES.new(self.key_128, AES.MODE_OCB, nonce=self.data_128[:length])
  70. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB,
  71. nonce=self.data_128)
  72. def test_block_size_128(self):
  73. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  74. self.assertEqual(cipher.block_size, AES.block_size)
  75. # By default, a 15 bytes long nonce is randomly generated
  76. nonce1 = AES.new(self.key_128, AES.MODE_OCB).nonce
  77. nonce2 = AES.new(self.key_128, AES.MODE_OCB).nonce
  78. self.assertEqual(len(nonce1), 15)
  79. self.assertNotEqual(nonce1, nonce2)
  80. def test_nonce_attribute(self):
  81. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  82. self.assertEqual(cipher.nonce, self.nonce_96)
  83. # By default, a 15 bytes long nonce is randomly generated
  84. nonce1 = AES.new(self.key_128, AES.MODE_OCB).nonce
  85. nonce2 = AES.new(self.key_128, AES.MODE_OCB).nonce
  86. self.assertEqual(len(nonce1), 15)
  87. self.assertNotEqual(nonce1, nonce2)
  88. def test_unknown_parameters(self):
  89. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_OCB,
  90. self.nonce_96, 7)
  91. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_OCB,
  92. nonce=self.nonce_96, unknown=7)
  93. # But some are only known by the base cipher
  94. # (e.g. use_aesni consumed by the AES module)
  95. AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96,
  96. use_aesni=False)
  97. def test_null_encryption_decryption(self):
  98. for func in "encrypt", "decrypt":
  99. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  100. result = getattr(cipher, func)(b(""))
  101. self.assertEqual(result, b(""))
  102. def test_either_encrypt_or_decrypt(self):
  103. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  104. cipher.encrypt(b("xyz"))
  105. self.assertRaises(TypeError, cipher.decrypt, b("xyz"))
  106. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  107. cipher.decrypt(b("xyz"))
  108. self.assertRaises(TypeError, cipher.encrypt, b("xyz"))
  109. def test_data_must_be_bytes(self):
  110. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  111. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  112. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  113. self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*')
  114. def test_mac_len(self):
  115. # Invalid MAC length
  116. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB,
  117. nonce=self.nonce_96, mac_len=7)
  118. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_OCB,
  119. nonce=self.nonce_96, mac_len=16+1)
  120. # Valid MAC length
  121. for mac_len in range(8, 16 + 1):
  122. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96,
  123. mac_len=mac_len)
  124. _, mac = cipher.encrypt_and_digest(self.data_128)
  125. self.assertEqual(len(mac), mac_len)
  126. # Default MAC length
  127. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  128. _, mac = cipher.encrypt_and_digest(self.data_128)
  129. self.assertEqual(len(mac), 16)
  130. def test_invalid_mac(self):
  131. from Cryptodome.Util.strxor import strxor_c
  132. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  133. ct, mac = cipher.encrypt_and_digest(self.data_128)
  134. invalid_mac = strxor_c(mac, 0x01)
  135. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  136. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  137. invalid_mac)
  138. def test_hex_mac(self):
  139. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  140. mac_hex = cipher.hexdigest()
  141. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  142. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  143. cipher.hexverify(mac_hex)
  144. def test_message_chunks(self):
  145. # Validate that both associated data and plaintext/ciphertext
  146. # can be broken up in chunks of arbitrary length
  147. auth_data = get_tag_random("authenticated data", 127)
  148. plaintext = get_tag_random("plaintext", 127)
  149. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  150. cipher.update(auth_data)
  151. ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext)
  152. def break_up(data, chunk_length):
  153. return [data[i:i+chunk_length] for i in range(0, len(data),
  154. chunk_length)]
  155. # Encryption
  156. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  157. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  158. for chunk in break_up(auth_data, chunk_length):
  159. cipher.update(chunk)
  160. pt2 = b("")
  161. for chunk in break_up(ciphertext, chunk_length):
  162. pt2 += cipher.decrypt(chunk)
  163. pt2 += cipher.decrypt()
  164. self.assertEqual(plaintext, pt2)
  165. cipher.verify(ref_mac)
  166. # Decryption
  167. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  168. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  169. for chunk in break_up(auth_data, chunk_length):
  170. cipher.update(chunk)
  171. ct2 = b("")
  172. for chunk in break_up(plaintext, chunk_length):
  173. ct2 += cipher.encrypt(chunk)
  174. ct2 += cipher.encrypt()
  175. self.assertEqual(ciphertext, ct2)
  176. self.assertEquals(cipher.digest(), ref_mac)
  177. def test_bytearray(self):
  178. # Encrypt
  179. key_ba = bytearray(self.key_128)
  180. nonce_ba = bytearray(self.nonce_96)
  181. header_ba = bytearray(self.data_128)
  182. data_ba = bytearray(self.data_128)
  183. cipher1 = AES.new(self.key_128,
  184. AES.MODE_OCB,
  185. nonce=self.nonce_96)
  186. cipher1.update(self.data_128)
  187. ct = cipher1.encrypt(self.data_128) + cipher1.encrypt()
  188. tag = cipher1.digest()
  189. cipher2 = AES.new(key_ba,
  190. AES.MODE_OCB,
  191. nonce=nonce_ba)
  192. key_ba[:3] = b"\xFF\xFF\xFF"
  193. nonce_ba[:3] = b"\xFF\xFF\xFF"
  194. cipher2.update(header_ba)
  195. header_ba[:3] = b"\xFF\xFF\xFF"
  196. ct_test = cipher2.encrypt(data_ba) + cipher2.encrypt()
  197. data_ba[:3] = b"\xFF\xFF\xFF"
  198. tag_test = cipher2.digest()
  199. self.assertEqual(ct, ct_test)
  200. self.assertEqual(tag, tag_test)
  201. self.assertEqual(cipher1.nonce, cipher2.nonce)
  202. # Decrypt
  203. key_ba = bytearray(self.key_128)
  204. nonce_ba = bytearray(self.nonce_96)
  205. header_ba = bytearray(self.data_128)
  206. del data_ba
  207. cipher4 = AES.new(key_ba,
  208. AES.MODE_OCB,
  209. nonce=nonce_ba)
  210. key_ba[:3] = b"\xFF\xFF\xFF"
  211. nonce_ba[:3] = b"\xFF\xFF\xFF"
  212. cipher4.update(header_ba)
  213. header_ba[:3] = b"\xFF\xFF\xFF"
  214. pt_test = cipher4.decrypt_and_verify(bytearray(ct_test), bytearray(tag_test))
  215. self.assertEqual(self.data_128, pt_test)
  216. def test_memoryview(self):
  217. # Encrypt
  218. key_mv = memoryview(bytearray(self.key_128))
  219. nonce_mv = memoryview(bytearray(self.nonce_96))
  220. header_mv = memoryview(bytearray(self.data_128))
  221. data_mv = memoryview(bytearray(self.data_128))
  222. cipher1 = AES.new(self.key_128,
  223. AES.MODE_OCB,
  224. nonce=self.nonce_96)
  225. cipher1.update(self.data_128)
  226. ct = cipher1.encrypt(self.data_128) + cipher1.encrypt()
  227. tag = cipher1.digest()
  228. cipher2 = AES.new(key_mv,
  229. AES.MODE_OCB,
  230. nonce=nonce_mv)
  231. key_mv[:3] = b"\xFF\xFF\xFF"
  232. nonce_mv[:3] = b"\xFF\xFF\xFF"
  233. cipher2.update(header_mv)
  234. header_mv[:3] = b"\xFF\xFF\xFF"
  235. ct_test = cipher2.encrypt(data_mv) + cipher2.encrypt()
  236. data_mv[:3] = b"\xFF\xFF\xFF"
  237. tag_test = cipher2.digest()
  238. self.assertEqual(ct, ct_test)
  239. self.assertEqual(tag, tag_test)
  240. self.assertEqual(cipher1.nonce, cipher2.nonce)
  241. # Decrypt
  242. key_mv = memoryview(bytearray(self.key_128))
  243. nonce_mv = memoryview(bytearray(self.nonce_96))
  244. header_mv = memoryview(bytearray(self.data_128))
  245. del data_mv
  246. cipher4 = AES.new(key_mv,
  247. AES.MODE_OCB,
  248. nonce=nonce_mv)
  249. key_mv[:3] = b"\xFF\xFF\xFF"
  250. nonce_mv[:3] = b"\xFF\xFF\xFF"
  251. cipher4.update(header_mv)
  252. header_mv[:3] = b"\xFF\xFF\xFF"
  253. pt_test = cipher4.decrypt_and_verify(memoryview(ct_test), memoryview(tag_test))
  254. self.assertEqual(self.data_128, pt_test)
  255. import sys
  256. if sys.version[:3] == "2.6":
  257. del test_memoryview
  258. class OcbFSMTests(unittest.TestCase):
  259. key_128 = get_tag_random("key_128", 16)
  260. nonce_96 = get_tag_random("nonce_128", 12)
  261. data_128 = get_tag_random("data_128", 16)
  262. def test_valid_init_encrypt_decrypt_digest_verify(self):
  263. # No authenticated data, fixed plaintext
  264. # Verify path INIT->ENCRYPT->ENCRYPT(NONE)->DIGEST
  265. cipher = AES.new(self.key_128, AES.MODE_OCB,
  266. nonce=self.nonce_96)
  267. ct = cipher.encrypt(self.data_128)
  268. ct += cipher.encrypt()
  269. mac = cipher.digest()
  270. # Verify path INIT->DECRYPT->DECRYPT(NONCE)->VERIFY
  271. cipher = AES.new(self.key_128, AES.MODE_OCB,
  272. nonce=self.nonce_96)
  273. cipher.decrypt(ct)
  274. cipher.decrypt()
  275. cipher.verify(mac)
  276. def test_invalid_init_encrypt_decrypt_digest_verify(self):
  277. # No authenticated data, fixed plaintext
  278. # Verify path INIT->ENCRYPT->DIGEST
  279. cipher = AES.new(self.key_128, AES.MODE_OCB,
  280. nonce=self.nonce_96)
  281. ct = cipher.encrypt(self.data_128)
  282. self.assertRaises(TypeError, cipher.digest)
  283. # Verify path INIT->DECRYPT->VERIFY
  284. cipher = AES.new(self.key_128, AES.MODE_OCB,
  285. nonce=self.nonce_96)
  286. cipher.decrypt(ct)
  287. self.assertRaises(TypeError, cipher.verify)
  288. def test_valid_init_update_digest_verify(self):
  289. # No plaintext, fixed authenticated data
  290. # Verify path INIT->UPDATE->DIGEST
  291. cipher = AES.new(self.key_128, AES.MODE_OCB,
  292. nonce=self.nonce_96)
  293. cipher.update(self.data_128)
  294. mac = cipher.digest()
  295. # Verify path INIT->UPDATE->VERIFY
  296. cipher = AES.new(self.key_128, AES.MODE_OCB,
  297. nonce=self.nonce_96)
  298. cipher.update(self.data_128)
  299. cipher.verify(mac)
  300. def test_valid_full_path(self):
  301. # Fixed authenticated data, fixed plaintext
  302. # Verify path INIT->UPDATE->ENCRYPT->ENCRYPT(NONE)->DIGEST
  303. cipher = AES.new(self.key_128, AES.MODE_OCB,
  304. nonce=self.nonce_96)
  305. cipher.update(self.data_128)
  306. ct = cipher.encrypt(self.data_128)
  307. ct += cipher.encrypt()
  308. mac = cipher.digest()
  309. # Verify path INIT->UPDATE->DECRYPT->DECRYPT(NONE)->VERIFY
  310. cipher = AES.new(self.key_128, AES.MODE_OCB,
  311. nonce=self.nonce_96)
  312. cipher.update(self.data_128)
  313. cipher.decrypt(ct)
  314. cipher.decrypt()
  315. cipher.verify(mac)
  316. def test_invalid_encrypt_after_final(self):
  317. cipher = AES.new(self.key_128, AES.MODE_OCB,
  318. nonce=self.nonce_96)
  319. cipher.update(self.data_128)
  320. cipher.encrypt(self.data_128)
  321. cipher.encrypt()
  322. self.assertRaises(TypeError, cipher.encrypt, self.data_128)
  323. def test_invalid_decrypt_after_final(self):
  324. cipher = AES.new(self.key_128, AES.MODE_OCB,
  325. nonce=self.nonce_96)
  326. cipher.update(self.data_128)
  327. cipher.decrypt(self.data_128)
  328. cipher.decrypt()
  329. self.assertRaises(TypeError, cipher.decrypt, self.data_128)
  330. def test_valid_init_digest(self):
  331. # Verify path INIT->DIGEST
  332. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  333. cipher.digest()
  334. def test_valid_init_verify(self):
  335. # Verify path INIT->VERIFY
  336. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  337. mac = cipher.digest()
  338. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  339. cipher.verify(mac)
  340. def test_valid_multiple_encrypt_or_decrypt(self):
  341. for method_name in "encrypt", "decrypt":
  342. for auth_data in (None, b("333"), self.data_128,
  343. self.data_128 + b("3")):
  344. if auth_data is None:
  345. assoc_len = None
  346. else:
  347. assoc_len = len(auth_data)
  348. cipher = AES.new(self.key_128, AES.MODE_OCB,
  349. nonce=self.nonce_96)
  350. if auth_data is not None:
  351. cipher.update(auth_data)
  352. method = getattr(cipher, method_name)
  353. method(self.data_128)
  354. method(self.data_128)
  355. method(self.data_128)
  356. method(self.data_128)
  357. method()
  358. def test_valid_multiple_digest_or_verify(self):
  359. # Multiple calls to digest
  360. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  361. cipher.update(self.data_128)
  362. first_mac = cipher.digest()
  363. for x in range(4):
  364. self.assertEqual(first_mac, cipher.digest())
  365. # Multiple calls to verify
  366. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  367. cipher.update(self.data_128)
  368. for x in range(5):
  369. cipher.verify(first_mac)
  370. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  371. # encrypt_and_digest
  372. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  373. cipher.update(self.data_128)
  374. ct, mac = cipher.encrypt_and_digest(self.data_128)
  375. # decrypt_and_verify
  376. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  377. cipher.update(self.data_128)
  378. pt = cipher.decrypt_and_verify(ct, mac)
  379. self.assertEqual(self.data_128, pt)
  380. def test_invalid_mixing_encrypt_decrypt(self):
  381. # Once per method, with or without assoc. data
  382. for method1_name, method2_name in (("encrypt", "decrypt"),
  383. ("decrypt", "encrypt")):
  384. for assoc_data_present in (True, False):
  385. cipher = AES.new(self.key_128, AES.MODE_OCB,
  386. nonce=self.nonce_96)
  387. if assoc_data_present:
  388. cipher.update(self.data_128)
  389. getattr(cipher, method1_name)(self.data_128)
  390. self.assertRaises(TypeError, getattr(cipher, method2_name),
  391. self.data_128)
  392. def test_invalid_encrypt_or_update_after_digest(self):
  393. for method_name in "encrypt", "update":
  394. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  395. cipher.encrypt(self.data_128)
  396. cipher.encrypt()
  397. cipher.digest()
  398. self.assertRaises(TypeError, getattr(cipher, method_name),
  399. self.data_128)
  400. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  401. cipher.encrypt_and_digest(self.data_128)
  402. def test_invalid_decrypt_or_update_after_verify(self):
  403. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  404. ct = cipher.encrypt(self.data_128)
  405. ct += cipher.encrypt()
  406. mac = cipher.digest()
  407. for method_name in "decrypt", "update":
  408. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  409. cipher.decrypt(ct)
  410. cipher.decrypt()
  411. cipher.verify(mac)
  412. self.assertRaises(TypeError, getattr(cipher, method_name),
  413. self.data_128)
  414. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  415. cipher.decrypt_and_verify(ct, mac)
  416. self.assertRaises(TypeError, getattr(cipher, method_name),
  417. self.data_128)
  418. class OcbRfc7253Test(unittest.TestCase):
  419. # Tuple with
  420. # - nonce
  421. # - authenticated data
  422. # - plaintext
  423. # - ciphertext and 16 byte MAC tag
  424. tv1_key = "000102030405060708090A0B0C0D0E0F"
  425. tv1 = (
  426. (
  427. "BBAA99887766554433221100",
  428. "",
  429. "",
  430. "785407BFFFC8AD9EDCC5520AC9111EE6"
  431. ),
  432. (
  433. "BBAA99887766554433221101",
  434. "0001020304050607",
  435. "0001020304050607",
  436. "6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009"
  437. ),
  438. (
  439. "BBAA99887766554433221102",
  440. "0001020304050607",
  441. "",
  442. "81017F8203F081277152FADE694A0A00"
  443. ),
  444. (
  445. "BBAA99887766554433221103",
  446. "",
  447. "0001020304050607",
  448. "45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9"
  449. ),
  450. (
  451. "BBAA99887766554433221104",
  452. "000102030405060708090A0B0C0D0E0F",
  453. "000102030405060708090A0B0C0D0E0F",
  454. "571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5"
  455. "701C1CCEC8FC3358"
  456. ),
  457. (
  458. "BBAA99887766554433221105",
  459. "000102030405060708090A0B0C0D0E0F",
  460. "",
  461. "8CF761B6902EF764462AD86498CA6B97"
  462. ),
  463. (
  464. "BBAA99887766554433221106",
  465. "",
  466. "000102030405060708090A0B0C0D0E0F",
  467. "5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436B"
  468. "DF06D8FA1ECA343D"
  469. ),
  470. (
  471. "BBAA99887766554433221107",
  472. "000102030405060708090A0B0C0D0E0F1011121314151617",
  473. "000102030405060708090A0B0C0D0E0F1011121314151617",
  474. "1CA2207308C87C010756104D8840CE1952F09673A448A122"
  475. "C92C62241051F57356D7F3C90BB0E07F"
  476. ),
  477. (
  478. "BBAA99887766554433221108",
  479. "000102030405060708090A0B0C0D0E0F1011121314151617",
  480. "",
  481. "6DC225A071FC1B9F7C69F93B0F1E10DE"
  482. ),
  483. (
  484. "BBAA99887766554433221109",
  485. "",
  486. "000102030405060708090A0B0C0D0E0F1011121314151617",
  487. "221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3C"
  488. "E725F32494B9F914D85C0B1EB38357FF"
  489. ),
  490. (
  491. "BBAA9988776655443322110A",
  492. "000102030405060708090A0B0C0D0E0F1011121314151617"
  493. "18191A1B1C1D1E1F",
  494. "000102030405060708090A0B0C0D0E0F1011121314151617"
  495. "18191A1B1C1D1E1F",
  496. "BD6F6C496201C69296C11EFD138A467ABD3C707924B964DE"
  497. "AFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240"
  498. ),
  499. (
  500. "BBAA9988776655443322110B",
  501. "000102030405060708090A0B0C0D0E0F1011121314151617"
  502. "18191A1B1C1D1E1F",
  503. "",
  504. "FE80690BEE8A485D11F32965BC9D2A32"
  505. ),
  506. (
  507. "BBAA9988776655443322110C",
  508. "",
  509. "000102030405060708090A0B0C0D0E0F1011121314151617"
  510. "18191A1B1C1D1E1F",
  511. "2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF4"
  512. "6040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF"
  513. ),
  514. (
  515. "BBAA9988776655443322110D",
  516. "000102030405060708090A0B0C0D0E0F1011121314151617"
  517. "18191A1B1C1D1E1F2021222324252627",
  518. "000102030405060708090A0B0C0D0E0F1011121314151617"
  519. "18191A1B1C1D1E1F2021222324252627",
  520. "D5CA91748410C1751FF8A2F618255B68A0A12E093FF45460"
  521. "6E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483"
  522. "A7035490C5769E60"
  523. ),
  524. (
  525. "BBAA9988776655443322110E",
  526. "000102030405060708090A0B0C0D0E0F1011121314151617"
  527. "18191A1B1C1D1E1F2021222324252627",
  528. "",
  529. "C5CD9D1850C141E358649994EE701B68"
  530. ),
  531. (
  532. "BBAA9988776655443322110F",
  533. "",
  534. "000102030405060708090A0B0C0D0E0F1011121314151617"
  535. "18191A1B1C1D1E1F2021222324252627",
  536. "4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15"
  537. "A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95"
  538. "A98CA5F3000B1479"
  539. )
  540. )
  541. # Tuple with
  542. # - key
  543. # - nonce
  544. # - authenticated data
  545. # - plaintext
  546. # - ciphertext and 12 byte MAC tag
  547. tv2 = (
  548. "0F0E0D0C0B0A09080706050403020100",
  549. "BBAA9988776655443322110D",
  550. "000102030405060708090A0B0C0D0E0F1011121314151617"
  551. "18191A1B1C1D1E1F2021222324252627",
  552. "000102030405060708090A0B0C0D0E0F1011121314151617"
  553. "18191A1B1C1D1E1F2021222324252627",
  554. "1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1"
  555. "A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FD"
  556. "AC4F02AA"
  557. )
  558. # Tuple with
  559. # - key length
  560. # - MAC tag length
  561. # - Expected output
  562. tv3 = (
  563. (128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2"),
  564. (192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17"),
  565. (256, 128, "D90EB8E9C977C88B79DD793D7FFA161C"),
  566. (128, 96, "77A3D8E73589158D25D01209"),
  567. (192, 96, "05D56EAD2752C86BE6932C5E"),
  568. (256, 96, "5458359AC23B0CBA9E6330DD"),
  569. (128, 64, "192C9B7BD90BA06A"),
  570. (192, 64, "0066BC6E0EF34E24"),
  571. (256, 64, "7D4EA5D445501CBE"),
  572. )
  573. def test1(self):
  574. key = unhexlify(b(self.tv1_key))
  575. for tv in self.tv1:
  576. nonce, aad, pt, ct = [ unhexlify(b(x)) for x in tv ]
  577. ct, mac_tag = ct[:-16], ct[-16:]
  578. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  579. cipher.update(aad)
  580. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  581. self.assertEquals(ct, ct2)
  582. self.assertEquals(mac_tag, cipher.digest())
  583. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  584. cipher.update(aad)
  585. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  586. self.assertEquals(pt, pt2)
  587. cipher.verify(mac_tag)
  588. def test2(self):
  589. key, nonce, aad, pt, ct = [ unhexlify(b(x)) for x in self.tv2 ]
  590. ct, mac_tag = ct[:-12], ct[-12:]
  591. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  592. cipher.update(aad)
  593. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  594. self.assertEquals(ct, ct2)
  595. self.assertEquals(mac_tag, cipher.digest())
  596. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  597. cipher.update(aad)
  598. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  599. self.assertEquals(pt, pt2)
  600. cipher.verify(mac_tag)
  601. def test3(self):
  602. for keylen, taglen, result in self.tv3:
  603. key = bchr(0) * (keylen // 8 - 1) + bchr(taglen)
  604. C = b("")
  605. for i in range(128):
  606. S = bchr(0) * i
  607. N = long_to_bytes(3 * i + 1, 12)
  608. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  609. cipher.update(S)
  610. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  611. N = long_to_bytes(3 * i + 2, 12)
  612. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  613. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  614. N = long_to_bytes(3 * i + 3, 12)
  615. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  616. cipher.update(S)
  617. C += cipher.encrypt() + cipher.digest()
  618. N = long_to_bytes(385, 12)
  619. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  620. cipher.update(C)
  621. result2 = cipher.encrypt() + cipher.digest()
  622. self.assertEquals(unhexlify(b(result)), result2)
  623. def get_tests(config={}):
  624. tests = []
  625. tests += list_test_cases(OcbTests)
  626. tests += list_test_cases(OcbFSMTests)
  627. tests += list_test_cases(OcbRfc7253Test)
  628. return tests
  629. if __name__ == '__main__':
  630. import unittest
  631. suite = lambda: unittest.TestSuite(get_tests())
  632. unittest.main(defaultTest='suite')