test_OCB.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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
  34. from Crypto.Util.py3compat import b, tobytes, bchr, unhexlify
  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 xrange(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 xrange(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. class OcbFSMTests(unittest.TestCase):
  178. key_128 = get_tag_random("key_128", 16)
  179. nonce_96 = get_tag_random("nonce_128", 12)
  180. data_128 = get_tag_random("data_128", 16)
  181. def test_valid_init_encrypt_decrypt_digest_verify(self):
  182. # No authenticated data, fixed plaintext
  183. # Verify path INIT->ENCRYPT->ENCRYPT(NONE)->DIGEST
  184. cipher = AES.new(self.key_128, AES.MODE_OCB,
  185. nonce=self.nonce_96)
  186. ct = cipher.encrypt(self.data_128)
  187. ct += cipher.encrypt()
  188. mac = cipher.digest()
  189. # Verify path INIT->DECRYPT->DECRYPT(NONCE)->VERIFY
  190. cipher = AES.new(self.key_128, AES.MODE_OCB,
  191. nonce=self.nonce_96)
  192. cipher.decrypt(ct)
  193. cipher.decrypt()
  194. cipher.verify(mac)
  195. def test_invalid_init_encrypt_decrypt_digest_verify(self):
  196. # No authenticated data, fixed plaintext
  197. # Verify path INIT->ENCRYPT->DIGEST
  198. cipher = AES.new(self.key_128, AES.MODE_OCB,
  199. nonce=self.nonce_96)
  200. ct = cipher.encrypt(self.data_128)
  201. self.assertRaises(TypeError, cipher.digest)
  202. # Verify path INIT->DECRYPT->VERIFY
  203. cipher = AES.new(self.key_128, AES.MODE_OCB,
  204. nonce=self.nonce_96)
  205. cipher.decrypt(ct)
  206. self.assertRaises(TypeError, cipher.verify)
  207. def test_valid_init_update_digest_verify(self):
  208. # No plaintext, fixed authenticated data
  209. # Verify path INIT->UPDATE->DIGEST
  210. cipher = AES.new(self.key_128, AES.MODE_OCB,
  211. nonce=self.nonce_96)
  212. cipher.update(self.data_128)
  213. mac = cipher.digest()
  214. # Verify path INIT->UPDATE->VERIFY
  215. cipher = AES.new(self.key_128, AES.MODE_OCB,
  216. nonce=self.nonce_96)
  217. cipher.update(self.data_128)
  218. cipher.verify(mac)
  219. def test_valid_full_path(self):
  220. # Fixed authenticated data, fixed plaintext
  221. # Verify path INIT->UPDATE->ENCRYPT->ENCRYPT(NONE)->DIGEST
  222. cipher = AES.new(self.key_128, AES.MODE_OCB,
  223. nonce=self.nonce_96)
  224. cipher.update(self.data_128)
  225. ct = cipher.encrypt(self.data_128)
  226. ct += cipher.encrypt()
  227. mac = cipher.digest()
  228. # Verify path INIT->UPDATE->DECRYPT->DECRYPT(NONE)->VERIFY
  229. cipher = AES.new(self.key_128, AES.MODE_OCB,
  230. nonce=self.nonce_96)
  231. cipher.update(self.data_128)
  232. cipher.decrypt(ct)
  233. cipher.decrypt()
  234. cipher.verify(mac)
  235. def test_invalid_encrypt_after_final(self):
  236. cipher = AES.new(self.key_128, AES.MODE_OCB,
  237. nonce=self.nonce_96)
  238. cipher.update(self.data_128)
  239. cipher.encrypt(self.data_128)
  240. cipher.encrypt()
  241. self.assertRaises(TypeError, cipher.encrypt, self.data_128)
  242. def test_invalid_decrypt_after_final(self):
  243. cipher = AES.new(self.key_128, AES.MODE_OCB,
  244. nonce=self.nonce_96)
  245. cipher.update(self.data_128)
  246. cipher.decrypt(self.data_128)
  247. cipher.decrypt()
  248. self.assertRaises(TypeError, cipher.decrypt, self.data_128)
  249. def test_valid_init_digest(self):
  250. # Verify path INIT->DIGEST
  251. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  252. cipher.digest()
  253. def test_valid_init_verify(self):
  254. # Verify path INIT->VERIFY
  255. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  256. mac = cipher.digest()
  257. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  258. cipher.verify(mac)
  259. def test_valid_multiple_encrypt_or_decrypt(self):
  260. for method_name in "encrypt", "decrypt":
  261. for auth_data in (None, b("333"), self.data_128,
  262. self.data_128 + b("3")):
  263. if auth_data is None:
  264. assoc_len = None
  265. else:
  266. assoc_len = len(auth_data)
  267. cipher = AES.new(self.key_128, AES.MODE_OCB,
  268. nonce=self.nonce_96)
  269. if auth_data is not None:
  270. cipher.update(auth_data)
  271. method = getattr(cipher, method_name)
  272. method(self.data_128)
  273. method(self.data_128)
  274. method(self.data_128)
  275. method(self.data_128)
  276. method()
  277. def test_valid_multiple_digest_or_verify(self):
  278. # Multiple calls to digest
  279. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  280. cipher.update(self.data_128)
  281. first_mac = cipher.digest()
  282. for x in xrange(4):
  283. self.assertEqual(first_mac, cipher.digest())
  284. # Multiple calls to verify
  285. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  286. cipher.update(self.data_128)
  287. for x in xrange(5):
  288. cipher.verify(first_mac)
  289. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  290. # encrypt_and_digest
  291. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  292. cipher.update(self.data_128)
  293. ct, mac = cipher.encrypt_and_digest(self.data_128)
  294. # decrypt_and_verify
  295. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  296. cipher.update(self.data_128)
  297. pt = cipher.decrypt_and_verify(ct, mac)
  298. self.assertEqual(self.data_128, pt)
  299. def test_invalid_mixing_encrypt_decrypt(self):
  300. # Once per method, with or without assoc. data
  301. for method1_name, method2_name in (("encrypt", "decrypt"),
  302. ("decrypt", "encrypt")):
  303. for assoc_data_present in (True, False):
  304. cipher = AES.new(self.key_128, AES.MODE_OCB,
  305. nonce=self.nonce_96)
  306. if assoc_data_present:
  307. cipher.update(self.data_128)
  308. getattr(cipher, method1_name)(self.data_128)
  309. self.assertRaises(TypeError, getattr(cipher, method2_name),
  310. self.data_128)
  311. def test_invalid_encrypt_or_update_after_digest(self):
  312. for method_name in "encrypt", "update":
  313. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  314. cipher.encrypt(self.data_128)
  315. cipher.encrypt()
  316. cipher.digest()
  317. self.assertRaises(TypeError, getattr(cipher, method_name),
  318. self.data_128)
  319. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  320. cipher.encrypt_and_digest(self.data_128)
  321. def test_invalid_decrypt_or_update_after_verify(self):
  322. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  323. ct = cipher.encrypt(self.data_128)
  324. ct += cipher.encrypt()
  325. mac = cipher.digest()
  326. for method_name in "decrypt", "update":
  327. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  328. cipher.decrypt(ct)
  329. cipher.decrypt()
  330. cipher.verify(mac)
  331. self.assertRaises(TypeError, getattr(cipher, method_name),
  332. self.data_128)
  333. cipher = AES.new(self.key_128, AES.MODE_OCB, nonce=self.nonce_96)
  334. cipher.decrypt_and_verify(ct, mac)
  335. self.assertRaises(TypeError, getattr(cipher, method_name),
  336. self.data_128)
  337. class OcbRfc7253Test(unittest.TestCase):
  338. # Tuple with
  339. # - nonce
  340. # - authenticated data
  341. # - plaintext
  342. # - ciphertext and 16 byte MAC tag
  343. tv1_key = "000102030405060708090A0B0C0D0E0F"
  344. tv1 = (
  345. (
  346. "BBAA99887766554433221100",
  347. "",
  348. "",
  349. "785407BFFFC8AD9EDCC5520AC9111EE6"
  350. ),
  351. (
  352. "BBAA99887766554433221101",
  353. "0001020304050607",
  354. "0001020304050607",
  355. "6820B3657B6F615A5725BDA0D3B4EB3A257C9AF1F8F03009"
  356. ),
  357. (
  358. "BBAA99887766554433221102",
  359. "0001020304050607",
  360. "",
  361. "81017F8203F081277152FADE694A0A00"
  362. ),
  363. (
  364. "BBAA99887766554433221103",
  365. "",
  366. "0001020304050607",
  367. "45DD69F8F5AAE72414054CD1F35D82760B2CD00D2F99BFA9"
  368. ),
  369. (
  370. "BBAA99887766554433221104",
  371. "000102030405060708090A0B0C0D0E0F",
  372. "000102030405060708090A0B0C0D0E0F",
  373. "571D535B60B277188BE5147170A9A22C3AD7A4FF3835B8C5"
  374. "701C1CCEC8FC3358"
  375. ),
  376. (
  377. "BBAA99887766554433221105",
  378. "000102030405060708090A0B0C0D0E0F",
  379. "",
  380. "8CF761B6902EF764462AD86498CA6B97"
  381. ),
  382. (
  383. "BBAA99887766554433221106",
  384. "",
  385. "000102030405060708090A0B0C0D0E0F",
  386. "5CE88EC2E0692706A915C00AEB8B2396F40E1C743F52436B"
  387. "DF06D8FA1ECA343D"
  388. ),
  389. (
  390. "BBAA99887766554433221107",
  391. "000102030405060708090A0B0C0D0E0F1011121314151617",
  392. "000102030405060708090A0B0C0D0E0F1011121314151617",
  393. "1CA2207308C87C010756104D8840CE1952F09673A448A122"
  394. "C92C62241051F57356D7F3C90BB0E07F"
  395. ),
  396. (
  397. "BBAA99887766554433221108",
  398. "000102030405060708090A0B0C0D0E0F1011121314151617",
  399. "",
  400. "6DC225A071FC1B9F7C69F93B0F1E10DE"
  401. ),
  402. (
  403. "BBAA99887766554433221109",
  404. "",
  405. "000102030405060708090A0B0C0D0E0F1011121314151617",
  406. "221BD0DE7FA6FE993ECCD769460A0AF2D6CDED0C395B1C3C"
  407. "E725F32494B9F914D85C0B1EB38357FF"
  408. ),
  409. (
  410. "BBAA9988776655443322110A",
  411. "000102030405060708090A0B0C0D0E0F1011121314151617"
  412. "18191A1B1C1D1E1F",
  413. "000102030405060708090A0B0C0D0E0F1011121314151617"
  414. "18191A1B1C1D1E1F",
  415. "BD6F6C496201C69296C11EFD138A467ABD3C707924B964DE"
  416. "AFFC40319AF5A48540FBBA186C5553C68AD9F592A79A4240"
  417. ),
  418. (
  419. "BBAA9988776655443322110B",
  420. "000102030405060708090A0B0C0D0E0F1011121314151617"
  421. "18191A1B1C1D1E1F",
  422. "",
  423. "FE80690BEE8A485D11F32965BC9D2A32"
  424. ),
  425. (
  426. "BBAA9988776655443322110C",
  427. "",
  428. "000102030405060708090A0B0C0D0E0F1011121314151617"
  429. "18191A1B1C1D1E1F",
  430. "2942BFC773BDA23CABC6ACFD9BFD5835BD300F0973792EF4"
  431. "6040C53F1432BCDFB5E1DDE3BC18A5F840B52E653444D5DF"
  432. ),
  433. (
  434. "BBAA9988776655443322110D",
  435. "000102030405060708090A0B0C0D0E0F1011121314151617"
  436. "18191A1B1C1D1E1F2021222324252627",
  437. "000102030405060708090A0B0C0D0E0F1011121314151617"
  438. "18191A1B1C1D1E1F2021222324252627",
  439. "D5CA91748410C1751FF8A2F618255B68A0A12E093FF45460"
  440. "6E59F9C1D0DDC54B65E8628E568BAD7AED07BA06A4A69483"
  441. "A7035490C5769E60"
  442. ),
  443. (
  444. "BBAA9988776655443322110E",
  445. "000102030405060708090A0B0C0D0E0F1011121314151617"
  446. "18191A1B1C1D1E1F2021222324252627",
  447. "",
  448. "C5CD9D1850C141E358649994EE701B68"
  449. ),
  450. (
  451. "BBAA9988776655443322110F",
  452. "",
  453. "000102030405060708090A0B0C0D0E0F1011121314151617"
  454. "18191A1B1C1D1E1F2021222324252627",
  455. "4412923493C57D5DE0D700F753CCE0D1D2D95060122E9F15"
  456. "A5DDBFC5787E50B5CC55EE507BCB084E479AD363AC366B95"
  457. "A98CA5F3000B1479"
  458. )
  459. )
  460. # Tuple with
  461. # - key
  462. # - nonce
  463. # - authenticated data
  464. # - plaintext
  465. # - ciphertext and 12 byte MAC tag
  466. tv2 = (
  467. "0F0E0D0C0B0A09080706050403020100",
  468. "BBAA9988776655443322110D",
  469. "000102030405060708090A0B0C0D0E0F1011121314151617"
  470. "18191A1B1C1D1E1F2021222324252627",
  471. "000102030405060708090A0B0C0D0E0F1011121314151617"
  472. "18191A1B1C1D1E1F2021222324252627",
  473. "1792A4E31E0755FB03E31B22116E6C2DDF9EFD6E33D536F1"
  474. "A0124B0A55BAE884ED93481529C76B6AD0C515F4D1CDD4FD"
  475. "AC4F02AA"
  476. )
  477. # Tuple with
  478. # - key length
  479. # - MAC tag length
  480. # - Expected output
  481. tv3 = (
  482. (128, 128, "67E944D23256C5E0B6C61FA22FDF1EA2"),
  483. (192, 128, "F673F2C3E7174AAE7BAE986CA9F29E17"),
  484. (256, 128, "D90EB8E9C977C88B79DD793D7FFA161C"),
  485. (128, 96, "77A3D8E73589158D25D01209"),
  486. (192, 96, "05D56EAD2752C86BE6932C5E"),
  487. (256, 96, "5458359AC23B0CBA9E6330DD"),
  488. (128, 64, "192C9B7BD90BA06A"),
  489. (192, 64, "0066BC6E0EF34E24"),
  490. (256, 64, "7D4EA5D445501CBE"),
  491. )
  492. def test1(self):
  493. key = unhexlify(b(self.tv1_key))
  494. for tv in self.tv1:
  495. nonce, aad, pt, ct = [ unhexlify(b(x)) for x in tv ]
  496. ct, mac_tag = ct[:-16], ct[-16:]
  497. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  498. cipher.update(aad)
  499. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  500. self.assertEquals(ct, ct2)
  501. self.assertEquals(mac_tag, cipher.digest())
  502. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce)
  503. cipher.update(aad)
  504. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  505. self.assertEquals(pt, pt2)
  506. cipher.verify(mac_tag)
  507. def test2(self):
  508. key, nonce, aad, pt, ct = [ unhexlify(b(x)) for x in self.tv2 ]
  509. ct, mac_tag = ct[:-12], ct[-12:]
  510. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  511. cipher.update(aad)
  512. ct2 = cipher.encrypt(pt) + cipher.encrypt()
  513. self.assertEquals(ct, ct2)
  514. self.assertEquals(mac_tag, cipher.digest())
  515. cipher = AES.new(key, AES.MODE_OCB, nonce=nonce, mac_len=12)
  516. cipher.update(aad)
  517. pt2 = cipher.decrypt(ct) + cipher.decrypt()
  518. self.assertEquals(pt, pt2)
  519. cipher.verify(mac_tag)
  520. def test3(self):
  521. for keylen, taglen, result in self.tv3:
  522. key = bchr(0) * (keylen // 8 - 1) + bchr(taglen)
  523. C = b("")
  524. for i in xrange(128):
  525. S = bchr(0) * i
  526. N = long_to_bytes(3 * i + 1, 12)
  527. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  528. cipher.update(S)
  529. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  530. N = long_to_bytes(3 * i + 2, 12)
  531. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  532. C += cipher.encrypt(S) + cipher.encrypt() + cipher.digest()
  533. N = long_to_bytes(3 * i + 3, 12)
  534. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  535. cipher.update(S)
  536. C += cipher.encrypt() + cipher.digest()
  537. N = long_to_bytes(385, 12)
  538. cipher = AES.new(key, AES.MODE_OCB, nonce=N, mac_len=taglen // 8)
  539. cipher.update(C)
  540. result2 = cipher.encrypt() + cipher.digest()
  541. self.assertEquals(unhexlify(b(result)), result2)
  542. def get_tests(config={}):
  543. tests = []
  544. tests += list_test_cases(OcbTests)
  545. tests += list_test_cases(OcbFSMTests)
  546. tests += list_test_cases(OcbRfc7253Test)
  547. return tests
  548. if __name__ == '__main__':
  549. import unittest
  550. suite = lambda: unittest.TestSuite(get_tests())
  551. unittest.main(defaultTest='suite')