test_OCB.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 Crypto.Util.py3compat import b, tobytes, bchr
  35. from Crypto.Util.strxor import strxor_c
  36. from Crypto.Util.number import long_to_bytes
  37. from Crypto.SelfTest.st_common import list_test_cases
  38. from Crypto.Cipher import AES
  39. from Crypto.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 Crypto.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. class OcbFSMTests(unittest.TestCase):
  256. key_128 = get_tag_random("key_128", 16)
  257. nonce_96 = get_tag_random("nonce_128", 12)
  258. data_128 = get_tag_random("data_128", 16)
  259. def test_valid_init_encrypt_decrypt_digest_verify(self):
  260. # No authenticated data, fixed plaintext
  261. # Verify path INIT->ENCRYPT->ENCRYPT(NONE)->DIGEST
  262. cipher = AES.new(self.key_128, AES.MODE_OCB,
  263. nonce=self.nonce_96)
  264. ct = cipher.encrypt(self.data_128)
  265. ct += cipher.encrypt()
  266. mac = cipher.digest()
  267. # Verify path INIT->DECRYPT->DECRYPT(NONCE)->VERIFY
  268. cipher = AES.new(self.key_128, AES.MODE_OCB,
  269. nonce=self.nonce_96)
  270. cipher.decrypt(ct)
  271. cipher.decrypt()
  272. cipher.verify(mac)
  273. def test_invalid_init_encrypt_decrypt_digest_verify(self):
  274. # No authenticated data, fixed plaintext
  275. # Verify path INIT->ENCRYPT->DIGEST
  276. cipher = AES.new(self.key_128, AES.MODE_OCB,
  277. nonce=self.nonce_96)
  278. ct = cipher.encrypt(self.data_128)
  279. self.assertRaises(TypeError, cipher.digest)
  280. # Verify path INIT->DECRYPT->VERIFY
  281. cipher = AES.new(self.key_128, AES.MODE_OCB,
  282. nonce=self.nonce_96)
  283. cipher.decrypt(ct)
  284. self.assertRaises(TypeError, cipher.verify)
  285. def test_valid_init_update_digest_verify(self):
  286. # No plaintext, fixed authenticated data
  287. # Verify path INIT->UPDATE->DIGEST
  288. cipher = AES.new(self.key_128, AES.MODE_OCB,
  289. nonce=self.nonce_96)
  290. cipher.update(self.data_128)
  291. mac = cipher.digest()
  292. # Verify path INIT->UPDATE->VERIFY
  293. cipher = AES.new(self.key_128, AES.MODE_OCB,
  294. nonce=self.nonce_96)
  295. cipher.update(self.data_128)
  296. cipher.verify(mac)
  297. def test_valid_full_path(self):
  298. # Fixed authenticated data, fixed plaintext
  299. # Verify path INIT->UPDATE->ENCRYPT->ENCRYPT(NONE)->DIGEST
  300. cipher = AES.new(self.key_128, AES.MODE_OCB,
  301. nonce=self.nonce_96)
  302. cipher.update(self.data_128)
  303. ct = cipher.encrypt(self.data_128)
  304. ct += cipher.encrypt()
  305. mac = cipher.digest()
  306. # Verify path INIT->UPDATE->DECRYPT->DECRYPT(NONE)->VERIFY
  307. cipher = AES.new(self.key_128, AES.MODE_OCB,
  308. nonce=self.nonce_96)
  309. cipher.update(self.data_128)
  310. cipher.decrypt(ct)
  311. cipher.decrypt()
  312. cipher.verify(mac)
  313. def test_invalid_encrypt_after_final(self):
  314. cipher = AES.new(self.key_128, AES.MODE_OCB,
  315. nonce=self.nonce_96)
  316. cipher.update(self.data_128)
  317. cipher.encrypt(self.data_128)
  318. cipher.encrypt()
  319. self.assertRaises(TypeError, cipher.encrypt, self.data_128)
  320. def test_invalid_decrypt_after_final(self):
  321. cipher = AES.new(self.key_128, AES.MODE_OCB,
  322. nonce=self.nonce_96)
  323. cipher.update(self.data_128)
  324. cipher.decrypt(self.data_128)
  325. cipher.decrypt()
  326. self.assertRaises(TypeError, cipher.decrypt, self.data_128)
  327. def test_valid_init_digest(self):
  328. # Verify path INIT->DIGEST
  329. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  330. cipher.digest()
  331. def test_valid_init_verify(self):
  332. # Verify path INIT->VERIFY
  333. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  334. mac = cipher.digest()
  335. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  336. cipher.verify(mac)
  337. def test_valid_multiple_encrypt_or_decrypt(self):
  338. for method_name in "encrypt", "decrypt":
  339. for auth_data in (None, b("333"), self.data_128,
  340. self.data_128 + b("3")):
  341. if auth_data is None:
  342. assoc_len = None
  343. else:
  344. assoc_len = len(auth_data)
  345. cipher = AES.new(self.key_128, AES.MODE_OCB,
  346. nonce=self.nonce_96)
  347. if auth_data is not None:
  348. cipher.update(auth_data)
  349. method = getattr(cipher, method_name)
  350. method(self.data_128)
  351. method(self.data_128)
  352. method(self.data_128)
  353. method(self.data_128)
  354. method()
  355. def test_valid_multiple_digest_or_verify(self):
  356. # Multiple calls to digest
  357. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  358. cipher.update(self.data_128)
  359. first_mac = cipher.digest()
  360. for x in range(4):
  361. self.assertEqual(first_mac, cipher.digest())
  362. # Multiple calls to verify
  363. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  364. cipher.update(self.data_128)
  365. for x in range(5):
  366. cipher.verify(first_mac)
  367. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  368. # encrypt_and_digest
  369. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  370. cipher.update(self.data_128)
  371. ct, mac = cipher.encrypt_and_digest(self.data_128)
  372. # decrypt_and_verify
  373. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  374. cipher.update(self.data_128)
  375. pt = cipher.decrypt_and_verify(ct, mac)
  376. self.assertEqual(self.data_128, pt)
  377. def test_invalid_mixing_encrypt_decrypt(self):
  378. # Once per method, with or without assoc. data
  379. for method1_name, method2_name in (("encrypt", "decrypt"),
  380. ("decrypt", "encrypt")):
  381. for assoc_data_present in (True, False):
  382. cipher = AES.new(self.key_128, AES.MODE_OCB,
  383. nonce=self.nonce_96)
  384. if assoc_data_present:
  385. cipher.update(self.data_128)
  386. getattr(cipher, method1_name)(self.data_128)
  387. self.assertRaises(TypeError, getattr(cipher, method2_name),
  388. self.data_128)
  389. def test_invalid_encrypt_or_update_after_digest(self):
  390. for method_name in "encrypt", "update":
  391. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  392. cipher.encrypt(self.data_128)
  393. cipher.encrypt()
  394. cipher.digest()
  395. self.assertRaises(TypeError, getattr(cipher, method_name),
  396. self.data_128)
  397. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  398. cipher.encrypt_and_digest(self.data_128)
  399. def test_invalid_decrypt_or_update_after_verify(self):
  400. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  401. ct = cipher.encrypt(self.data_128)
  402. ct += cipher.encrypt()
  403. mac = cipher.digest()
  404. for method_name in "decrypt", "update":
  405. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  406. cipher.decrypt(ct)
  407. cipher.decrypt()
  408. cipher.verify(mac)
  409. self.assertRaises(TypeError, getattr(cipher, method_name),
  410. self.data_128)
  411. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  412. cipher.decrypt_and_verify(ct, mac)
  413. self.assertRaises(TypeError, getattr(cipher, method_name),
  414. self.data_128)
  415. class OcbRfc7253Test(unittest.TestCase):
  416. # Tuple with
  417. # - nonce
  418. # - authenticated data
  419. # - plaintext
  420. # - ciphertext and 16 byte MAC tag
  421. tv1_key = "000102030405060708090A0B0C0D0E0F"
  422. tv1 = (
  423. (
  424. "BBAA99887766554433221100",
  425. "",
  426. "",
  427. "785407BFFFC8AD9EDCC5520AC9111EE6"
  428. ),
  429. (
  430. "BBAA99887766554433221101",
  431. "0001020304050607",
  432. "0001020304050607",
  433. "6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009"
  434. ),
  435. (
  436. "BBAA99887766554433221102",
  437. "0001020304050607",
  438. "",
  439. "81017F8203F081277152FADE694A0A00"
  440. ),
  441. (
  442. "BBAA99887766554433221103",
  443. "",
  444. "0001020304050607",
  445. "45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9"
  446. ),
  447. (
  448. "BBAA99887766554433221104",
  449. "000102030405060708090A0B0C0D0E0F",
  450. "000102030405060708090A0B0C0D0E0F",
  451. "571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5"
  452. "701C1CCEC8FC3358"
  453. ),
  454. (
  455. "BBAA99887766554433221105",
  456. "000102030405060708090A0B0C0D0E0F",
  457. "",
  458. "8CF761B6902EF764462AD86498CA6B97"
  459. ),
  460. (
  461. "BBAA99887766554433221106",
  462. "",
  463. "000102030405060708090A0B0C0D0E0F",
  464. "5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436B"
  465. "DF06D8FA1ECA343D"
  466. ),
  467. (
  468. "BBAA99887766554433221107",
  469. "000102030405060708090A0B0C0D0E0F1011121314151617",
  470. "000102030405060708090A0B0C0D0E0F1011121314151617",
  471. "1CA2207308C87C010756104D8840CE1952F09673A448A122"
  472. "C92C62241051F57356D7F3C90BB0E07F"
  473. ),
  474. (
  475. "BBAA99887766554433221108",
  476. "000102030405060708090A0B0C0D0E0F1011121314151617",
  477. "",
  478. "6DC225A071FC1B9F7C69F93B0F1E10DE"
  479. ),
  480. (
  481. "BBAA99887766554433221109",
  482. "",
  483. "000102030405060708090A0B0C0D0E0F1011121314151617",
  484. "221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3C"
  485. "E725F32494B9F914D85C0B1EB38357FF"
  486. ),
  487. (
  488. "BBAA9988776655443322110A",
  489. "000102030405060708090A0B0C0D0E0F1011121314151617"
  490. "18191A1B1C1D1E1F",
  491. "000102030405060708090A0B0C0D0E0F1011121314151617"
  492. "18191A1B1C1D1E1F",
  493. "BD6F6C496201C69296C11EFD138A467ABD3C707924B964DE"
  494. "AFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240"
  495. ),
  496. (
  497. "BBAA9988776655443322110B",
  498. "000102030405060708090A0B0C0D0E0F1011121314151617"
  499. "18191A1B1C1D1E1F",
  500. "",
  501. "FE80690BEE8A485D11F32965BC9D2A32"
  502. ),
  503. (
  504. "BBAA9988776655443322110C",
  505. "",
  506. "000102030405060708090A0B0C0D0E0F1011121314151617"
  507. "18191A1B1C1D1E1F",
  508. "2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF4"
  509. "6040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF"
  510. ),
  511. (
  512. "BBAA9988776655443322110D",
  513. "000102030405060708090A0B0C0D0E0F1011121314151617"
  514. "18191A1B1C1D1E1F2021222324252627",
  515. "000102030405060708090A0B0C0D0E0F1011121314151617"
  516. "18191A1B1C1D1E1F2021222324252627",
  517. "D5CA91748410C1751FF8A2F618255B68A0A12E093FF45460"
  518. "6E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483"
  519. "A7035490C5769E60"
  520. ),
  521. (
  522. "BBAA9988776655443322110E",
  523. "000102030405060708090A0B0C0D0E0F1011121314151617"
  524. "18191A1B1C1D1E1F2021222324252627",
  525. "",
  526. "C5CD9D1850C141E358649994EE701B68"
  527. ),
  528. (
  529. "BBAA9988776655443322110F",
  530. "",
  531. "000102030405060708090A0B0C0D0E0F1011121314151617"
  532. "18191A1B1C1D1E1F2021222324252627",
  533. "4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15"
  534. "A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95"
  535. "A98CA5F3000B1479"
  536. )
  537. )
  538. # Tuple with
  539. # - key
  540. # - nonce
  541. # - authenticated data
  542. # - plaintext
  543. # - ciphertext and 12 byte MAC tag
  544. tv2 = (
  545. "0F0E0D0C0B0A09080706050403020100",
  546. "BBAA9988776655443322110D",
  547. "000102030405060708090A0B0C0D0E0F1011121314151617"
  548. "18191A1B1C1D1E1F2021222324252627",
  549. "000102030405060708090A0B0C0D0E0F1011121314151617"
  550. "18191A1B1C1D1E1F2021222324252627",
  551. "1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1"
  552. "A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FD"
  553. "AC4F02AA"
  554. )
  555. # Tuple with
  556. # - key length
  557. # - MAC tag length
  558. # - Expected output
  559. tv3 = (
  560. (128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2"),
  561. (192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17"),
  562. (256, 128, "D90EB8E9C977C88B79DD793D7FFA161C"),
  563. (128, 96, "77A3D8E73589158D25D01209"),
  564. (192, 96, "05D56EAD2752C86BE6932C5E"),
  565. (256, 96, "5458359AC23B0CBA9E6330DD"),
  566. (128, 64, "192C9B7BD90BA06A"),
  567. (192, 64, "0066BC6E0EF34E24"),
  568. (256, 64, "7D4EA5D445501CBE"),
  569. )
  570. def test1(self):
  571. key = unhexlify(b(self.tv1_key))
  572. for tv in self.tv1:
  573. nonce, aad, pt, ct = [ unhexlify(b(x)) for x in tv ]
  574. ct, mac_tag = ct[:-16], ct[-16:]
  575. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  576. cipher.update(aad)
  577. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  578. self.assertEquals(ct, ct2)
  579. self.assertEquals(mac_tag, cipher.digest())
  580. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  581. cipher.update(aad)
  582. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  583. self.assertEquals(pt, pt2)
  584. cipher.verify(mac_tag)
  585. def test2(self):
  586. key, nonce, aad, pt, ct = [ unhexlify(b(x)) for x in self.tv2 ]
  587. ct, mac_tag = ct[:-12], ct[-12:]
  588. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  589. cipher.update(aad)
  590. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  591. self.assertEquals(ct, ct2)
  592. self.assertEquals(mac_tag, cipher.digest())
  593. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  594. cipher.update(aad)
  595. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  596. self.assertEquals(pt, pt2)
  597. cipher.verify(mac_tag)
  598. def test3(self):
  599. for keylen, taglen, result in self.tv3:
  600. key = bchr(0) * (keylen // 8 - 1) + bchr(taglen)
  601. C = b("")
  602. for i in range(128):
  603. S = bchr(0) * i
  604. N = long_to_bytes(3 * i + 1, 12)
  605. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  606. cipher.update(S)
  607. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  608. N = long_to_bytes(3 * i + 2, 12)
  609. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  610. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  611. N = long_to_bytes(3 * i + 3, 12)
  612. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  613. cipher.update(S)
  614. C += cipher.encrypt() + cipher.digest()
  615. N = long_to_bytes(385, 12)
  616. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  617. cipher.update(C)
  618. result2 = cipher.encrypt() + cipher.digest()
  619. self.assertEquals(unhexlify(b(result)), result2)
  620. def get_tests(config={}):
  621. tests = []
  622. tests += list_test_cases(OcbTests)
  623. tests += list_test_cases(OcbFSMTests)
  624. tests += list_test_cases(OcbRfc7253Test)
  625. return tests
  626. if __name__ == '__main__':
  627. import unittest
  628. suite = lambda: unittest.TestSuite(get_tests())
  629. unittest.main(defaultTest='suite')