test_SIV.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 json
  31. import unittest
  32. from binascii import unhexlify
  33. from tls.Crypto.SelfTest.st_common import list_test_cases
  34. from tls.Crypto.Util.py3compat import tobytes, bchr
  35. from tls.Crypto.Cipher import AES
  36. from tls.Crypto.Hash import SHAKE128
  37. from tls.Crypto.Util._file_system import pycryptodome_filename
  38. from tls.Crypto.Util.strxor import strxor
  39. def get_tag_random(tag, length):
  40. return SHAKE128.new(data=tobytes(tag)).read(length)
  41. class SivTests(unittest.TestCase):
  42. key_256 = get_tag_random("key_256", 32)
  43. key_384 = get_tag_random("key_384", 48)
  44. key_512 = get_tag_random("key_512", 64)
  45. nonce_96 = get_tag_random("nonce_128", 12)
  46. data_128 = get_tag_random("data_128", 16)
  47. def test_loopback_128(self):
  48. for key in self.key_256, self.key_384, self.key_512:
  49. cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
  50. pt = get_tag_random("plaintext", 16 * 100)
  51. ct, mac = cipher.encrypt_and_digest(pt)
  52. cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
  53. pt2 = cipher.decrypt_and_verify(ct, mac)
  54. self.assertEqual(pt, pt2)
  55. def test_nonce(self):
  56. # Deterministic encryption
  57. AES.new(self.key_256, AES.MODE_SIV)
  58. cipher = AES.new(self.key_256, AES.MODE_SIV, self.nonce_96)
  59. ct1, tag1 = cipher.encrypt_and_digest(self.data_128)
  60. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  61. ct2, tag2 = cipher.encrypt_and_digest(self.data_128)
  62. self.assertEquals(ct1 + tag1, ct2 + tag2)
  63. def test_nonce_must_be_bytes(self):
  64. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  65. nonce=u'test12345678')
  66. def test_nonce_length(self):
  67. # nonce can be of any length (but not empty)
  68. self.assertRaises(ValueError, AES.new, self.key_256, AES.MODE_SIV,
  69. nonce=b"")
  70. for x in range(1, 128):
  71. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=bchr(1) * x)
  72. cipher.encrypt_and_digest(b'\x01')
  73. def test_block_size_128(self):
  74. cipher = AES.new(self.key_256, AES.MODE_SIV, 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_256, AES.MODE_SIV, nonce=self.nonce_96)
  78. self.assertEqual(cipher.nonce, self.nonce_96)
  79. # By default, no nonce is randomly generated
  80. self.failIf(hasattr(AES.new(self.key_256, AES.MODE_SIV), "nonce"))
  81. def test_unknown_parameters(self):
  82. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  83. self.nonce_96, 7)
  84. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  85. nonce=self.nonce_96, unknown=7)
  86. # But some are only known by the base cipher
  87. # (e.g. use_aesni consumed by the AES module)
  88. AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96,
  89. use_aesni=False)
  90. def test_encrypt_excludes_decrypt(self):
  91. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  92. cipher.encrypt_and_digest(self.data_128)
  93. self.assertRaises(TypeError, cipher.decrypt, self.data_128)
  94. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  95. cipher.encrypt_and_digest(self.data_128)
  96. self.assertRaises(TypeError, cipher.decrypt_and_verify,
  97. self.data_128, self.data_128)
  98. def test_data_must_be_bytes(self):
  99. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  100. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  101. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  102. self.assertRaises(TypeError, cipher.decrypt_and_verify,
  103. u'test1234567890-*', b"xxxx")
  104. def test_mac_len(self):
  105. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  106. _, mac = cipher.encrypt_and_digest(self.data_128)
  107. self.assertEqual(len(mac), 16)
  108. def test_invalid_mac(self):
  109. from tls.Crypto.Util.strxor import strxor_c
  110. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  111. ct, mac = cipher.encrypt_and_digest(self.data_128)
  112. invalid_mac = strxor_c(mac, 0x01)
  113. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  114. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  115. invalid_mac)
  116. def test_hex_mac(self):
  117. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  118. mac_hex = cipher.hexdigest()
  119. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  120. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  121. cipher.hexverify(mac_hex)
  122. def test_bytearray(self):
  123. # Encrypt
  124. key = bytearray(self.key_256)
  125. nonce = bytearray(self.nonce_96)
  126. data = bytearray(self.data_128)
  127. header = bytearray(self.data_128)
  128. cipher1 = AES.new(self.key_256,
  129. AES.MODE_SIV,
  130. nonce=self.nonce_96)
  131. cipher1.update(self.data_128)
  132. ct, tag = cipher1.encrypt_and_digest(self.data_128)
  133. cipher2 = AES.new(key,
  134. AES.MODE_SIV,
  135. nonce=nonce)
  136. key[:3] = b'\xFF\xFF\xFF'
  137. nonce[:3] = b'\xFF\xFF\xFF'
  138. cipher2.update(header)
  139. header[:3] = b'\xFF\xFF\xFF'
  140. ct_test, tag_test = cipher2.encrypt_and_digest(data)
  141. self.assertEqual(ct, ct_test)
  142. self.assertEqual(tag, tag_test)
  143. self.assertEqual(cipher1.nonce, cipher2.nonce)
  144. # Decrypt
  145. key = bytearray(self.key_256)
  146. nonce = bytearray(self.nonce_96)
  147. header = bytearray(self.data_128)
  148. ct_ba = bytearray(ct)
  149. tag_ba = bytearray(tag)
  150. cipher3 = AES.new(key,
  151. AES.MODE_SIV,
  152. nonce=nonce)
  153. key[:3] = b'\xFF\xFF\xFF'
  154. nonce[:3] = b'\xFF\xFF\xFF'
  155. cipher3.update(header)
  156. header[:3] = b'\xFF\xFF\xFF'
  157. pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba)
  158. self.assertEqual(self.data_128, pt_test)
  159. def test_memoryview(self):
  160. # Encrypt
  161. key = memoryview(bytearray(self.key_256))
  162. nonce = memoryview(bytearray(self.nonce_96))
  163. data = memoryview(bytearray(self.data_128))
  164. header = memoryview(bytearray(self.data_128))
  165. cipher1 = AES.new(self.key_256,
  166. AES.MODE_SIV,
  167. nonce=self.nonce_96)
  168. cipher1.update(self.data_128)
  169. ct, tag = cipher1.encrypt_and_digest(self.data_128)
  170. cipher2 = AES.new(key,
  171. AES.MODE_SIV,
  172. nonce=nonce)
  173. key[:3] = b'\xFF\xFF\xFF'
  174. nonce[:3] = b'\xFF\xFF\xFF'
  175. cipher2.update(header)
  176. header[:3] = b'\xFF\xFF\xFF'
  177. ct_test, tag_test= cipher2.encrypt_and_digest(data)
  178. self.assertEqual(ct, ct_test)
  179. self.assertEqual(tag, tag_test)
  180. self.assertEqual(cipher1.nonce, cipher2.nonce)
  181. # Decrypt
  182. key = memoryview(bytearray(self.key_256))
  183. nonce = memoryview(bytearray(self.nonce_96))
  184. header = memoryview(bytearray(self.data_128))
  185. ct_ba = memoryview(bytearray(ct))
  186. tag_ba = memoryview(bytearray(tag))
  187. cipher3 = AES.new(key,
  188. AES.MODE_SIV,
  189. nonce=nonce)
  190. key[:3] = b'\xFF\xFF\xFF'
  191. nonce[:3] = b'\xFF\xFF\xFF'
  192. cipher3.update(header)
  193. header[:3] = b'\xFF\xFF\xFF'
  194. pt_test = cipher3.decrypt_and_verify(ct_ba, tag_ba)
  195. self.assertEqual(self.data_128, pt_test)
  196. def test_output_param(self):
  197. pt = b'5' * 16
  198. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  199. ct, tag = cipher.encrypt_and_digest(pt)
  200. output = bytearray(16)
  201. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  202. res, tag_out = cipher.encrypt_and_digest(pt, output=output)
  203. self.assertEqual(ct, output)
  204. self.assertEqual(res, None)
  205. self.assertEqual(tag, tag_out)
  206. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  207. res = cipher.decrypt_and_verify(ct, tag, output=output)
  208. self.assertEqual(pt, output)
  209. self.assertEqual(res, None)
  210. def test_output_param_memoryview(self):
  211. pt = b'5' * 16
  212. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  213. ct, tag = cipher.encrypt_and_digest(pt)
  214. output = memoryview(bytearray(16))
  215. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  216. cipher.encrypt_and_digest(pt, output=output)
  217. self.assertEqual(ct, output)
  218. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  219. cipher.decrypt_and_verify(ct, tag, output=output)
  220. self.assertEqual(pt, output)
  221. def test_output_param_neg(self):
  222. pt = b'5' * 16
  223. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  224. ct, tag = cipher.encrypt_and_digest(pt)
  225. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  226. self.assertRaises(TypeError, cipher.encrypt_and_digest, pt, output=b'0'*16)
  227. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  228. self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag, output=b'0'*16)
  229. shorter_output = bytearray(15)
  230. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  231. self.assertRaises(ValueError, cipher.encrypt_and_digest, pt, output=shorter_output)
  232. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  233. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct, tag, output=shorter_output)
  234. import sys
  235. if sys.version[:3] == "2.6":
  236. del test_memoryview
  237. del test_output_param_memoryview
  238. class SivFSMTests(unittest.TestCase):
  239. key_256 = get_tag_random("key_256", 32)
  240. nonce_96 = get_tag_random("nonce_96", 12)
  241. data_128 = get_tag_random("data_128", 16)
  242. def test_invalid_init_encrypt(self):
  243. # Path INIT->ENCRYPT fails
  244. cipher = AES.new(self.key_256, AES.MODE_SIV,
  245. nonce=self.nonce_96)
  246. self.assertRaises(TypeError, cipher.encrypt, b"xxx")
  247. def test_invalid_init_decrypt(self):
  248. # Path INIT->DECRYPT fails
  249. cipher = AES.new(self.key_256, AES.MODE_SIV,
  250. nonce=self.nonce_96)
  251. self.assertRaises(TypeError, cipher.decrypt, b"xxx")
  252. def test_valid_init_update_digest_verify(self):
  253. # No plaintext, fixed authenticated data
  254. # Verify path INIT->UPDATE->DIGEST
  255. cipher = AES.new(self.key_256, AES.MODE_SIV,
  256. nonce=self.nonce_96)
  257. cipher.update(self.data_128)
  258. mac = cipher.digest()
  259. # Verify path INIT->UPDATE->VERIFY
  260. cipher = AES.new(self.key_256, AES.MODE_SIV,
  261. nonce=self.nonce_96)
  262. cipher.update(self.data_128)
  263. cipher.verify(mac)
  264. def test_valid_init_digest(self):
  265. # Verify path INIT->DIGEST
  266. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  267. cipher.digest()
  268. def test_valid_init_verify(self):
  269. # Verify path INIT->VERIFY
  270. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  271. mac = cipher.digest()
  272. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  273. cipher.verify(mac)
  274. def test_valid_multiple_digest_or_verify(self):
  275. # Multiple calls to digest
  276. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  277. cipher.update(self.data_128)
  278. first_mac = cipher.digest()
  279. for x in range(4):
  280. self.assertEqual(first_mac, cipher.digest())
  281. # Multiple calls to verify
  282. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  283. cipher.update(self.data_128)
  284. for x in range(5):
  285. cipher.verify(first_mac)
  286. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  287. # encrypt_and_digest
  288. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  289. cipher.update(self.data_128)
  290. ct, mac = cipher.encrypt_and_digest(self.data_128)
  291. # decrypt_and_verify
  292. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  293. cipher.update(self.data_128)
  294. pt = cipher.decrypt_and_verify(ct, mac)
  295. self.assertEqual(self.data_128, pt)
  296. def test_invalid_multiple_encrypt_and_digest(self):
  297. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  298. ct, tag = cipher.encrypt_and_digest(self.data_128)
  299. self.assertRaises(TypeError, cipher.encrypt_and_digest, b'')
  300. def test_invalid_multiple_decrypt_and_verify(self):
  301. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  302. ct, tag = cipher.encrypt_and_digest(self.data_128)
  303. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  304. cipher.decrypt_and_verify(ct, tag)
  305. self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag)
  306. def transform(tv):
  307. new_tv = [[unhexlify(x) for x in tv[0].split("-")]]
  308. new_tv += [ unhexlify(x) for x in tv[1:5]]
  309. if tv[5]:
  310. nonce = unhexlify(tv[5])
  311. else:
  312. nonce = None
  313. new_tv += [ nonce ]
  314. return new_tv
  315. class TestVectors(unittest.TestCase):
  316. """Class exercising the SIV test vectors found in RFC5297"""
  317. # This is a list of tuples with 5 items:
  318. #
  319. # 1. Header + '|' + plaintext
  320. # 2. Header + '|' + ciphertext + '|' + MAC
  321. # 3. AES-128 key
  322. # 4. Description
  323. # 5. Dictionary of parameters to be passed to AES.new().
  324. # It must include the nonce.
  325. #
  326. # A "Header" is a dash ('-') separated sequece of components.
  327. #
  328. test_vectors_hex = [
  329. (
  330. '101112131415161718191a1b1c1d1e1f2021222324252627',
  331. '112233445566778899aabbccddee',
  332. '40c02b9690c4dc04daef7f6afe5c',
  333. '85632d07c6e8f37f950acd320a2ecc93',
  334. 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff',
  335. None
  336. ),
  337. (
  338. '00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988' +
  339. '7766554433221100-102030405060708090a0',
  340. '7468697320697320736f6d6520706c61696e7465787420746f20656e63727970' +
  341. '74207573696e67205349562d414553',
  342. 'cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829' +
  343. 'ea64ad544a272e9c485b62a3fd5c0d',
  344. '7bdb6e3b432667eb06f4d14bff2fbd0f',
  345. '7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4f',
  346. '09f911029d74e35bd84156c5635688c0'
  347. ),
  348. ]
  349. test_vectors = [ transform(tv) for tv in test_vectors_hex ]
  350. def runTest(self):
  351. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  352. # Encrypt
  353. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  354. for x in assoc_data:
  355. cipher.update(x)
  356. ct2, mac2 = cipher.encrypt_and_digest(pt)
  357. self.assertEqual(ct, ct2)
  358. self.assertEqual(mac, mac2)
  359. # Decrypt
  360. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  361. for x in assoc_data:
  362. cipher.update(x)
  363. pt2 = cipher.decrypt_and_verify(ct, mac)
  364. self.assertEqual(pt, pt2)
  365. class TestVectorsWycheproof(unittest.TestCase):
  366. def __init__(self):
  367. unittest.TestCase.__init__(self)
  368. self._id = "None"
  369. def setUp(self):
  370. comps = "Crypto.SelfTest.Cipher.test_vectors.wycheproof".split(".")
  371. with open(pycryptodome_filename(comps, "aes_siv_cmac_test.json"), "rt") as file_in:
  372. tv_tree = json.load(file_in)
  373. class TestVector(object):
  374. pass
  375. self.tv = []
  376. for group in tv_tree['testGroups']:
  377. for test in group['tests']:
  378. tv = TestVector()
  379. tv.id = test['tcId']
  380. for attr in 'key', 'aad', 'msg', 'ct':
  381. setattr(tv, attr, unhexlify(test[attr]))
  382. tv.valid = test['result'] != "invalid"
  383. self.tv.append(tv)
  384. def shortDescription(self):
  385. return self._id
  386. def test_encrypt(self, tv):
  387. self._id = "Wycheproof Encrypt AES-SIV Test #" + str(tv.id)
  388. cipher = AES.new(tv.key, AES.MODE_SIV)
  389. cipher.update(tv.aad)
  390. ct, tag = cipher.encrypt_and_digest(tv.msg)
  391. if tv.valid:
  392. self.assertEqual(tag + ct, tv.ct)
  393. def test_decrypt(self, tv):
  394. self._id = "Wycheproof Decrypt AES_SIV Test #" + str(tv.id)
  395. cipher = AES.new(tv.key, AES.MODE_SIV)
  396. cipher.update(tv.aad)
  397. try:
  398. pt = cipher.decrypt_and_verify(tv.ct[16:], tv.ct[:16])
  399. except ValueError:
  400. assert not tv.valid
  401. else:
  402. assert tv.valid
  403. self.assertEqual(pt, tv.msg)
  404. def runTest(self):
  405. for tv in self.tv:
  406. self.test_encrypt(tv)
  407. self.test_decrypt(tv)
  408. class TestVectorsWycheproof2(unittest.TestCase):
  409. def __init__(self):
  410. unittest.TestCase.__init__(self)
  411. self._id = "None"
  412. def setUp(self):
  413. comps = "Crypto.SelfTest.Cipher.test_vectors.wycheproof".split(".")
  414. with open(pycryptodome_filename(comps, "aead_aes_siv_cmac_test.json"), "rt") as file_in:
  415. tv_tree = json.load(file_in)
  416. class TestVector(object):
  417. pass
  418. self.tv = []
  419. for group in tv_tree['testGroups']:
  420. for test in group['tests']:
  421. tv = TestVector()
  422. tv.id = test['tcId']
  423. for attr in 'key', 'iv', 'aad', 'msg', 'ct', 'tag':
  424. setattr(tv, attr, unhexlify(test[attr]))
  425. tv.valid = test['result'] != "invalid"
  426. self.tv.append(tv)
  427. def shortDescription(self):
  428. return self._id
  429. def test_encrypt(self, tv):
  430. self._id = "Wycheproof Encrypt AEAD-AES-SIV Test #" + str(tv.id)
  431. cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
  432. cipher.update(tv.aad)
  433. ct, tag = cipher.encrypt_and_digest(tv.msg)
  434. if tv.valid:
  435. self.assertEqual(ct, tv.ct)
  436. self.assertEqual(tag, tv.tag)
  437. def test_decrypt(self, tv):
  438. self._id = "Wycheproof Decrypt AEAD-AES-SIV Test #" + str(tv.id)
  439. cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
  440. cipher.update(tv.aad)
  441. try:
  442. pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
  443. except ValueError:
  444. assert not tv.valid
  445. else:
  446. assert tv.valid
  447. self.assertEqual(pt, tv.msg)
  448. def runTest(self):
  449. for tv in self.tv:
  450. self.test_encrypt(tv)
  451. self.test_decrypt(tv)
  452. def get_tests(config={}):
  453. wycheproof_warnings = config.get('wycheproof_warnings')
  454. tests = []
  455. tests += list_test_cases(SivTests)
  456. tests += list_test_cases(SivFSMTests)
  457. tests += [ TestVectors() ]
  458. tests += [ TestVectorsWycheproof() ]
  459. tests += [ TestVectorsWycheproof2() ]
  460. return tests
  461. if __name__ == '__main__':
  462. suite = lambda: unittest.TestSuite(get_tests())
  463. unittest.main(defaultTest='suite')