test_SIV.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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 Crypto.SelfTest.st_common import list_test_cases
  34. from Crypto.SelfTest.loader import load_test_vectors_wycheproof
  35. from Crypto.Util.py3compat import tobytes, bchr
  36. from Crypto.Cipher import AES
  37. from Crypto.Hash import SHAKE128
  38. from 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 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. class SivFSMTests(unittest.TestCase):
  235. key_256 = get_tag_random("key_256", 32)
  236. nonce_96 = get_tag_random("nonce_96", 12)
  237. data_128 = get_tag_random("data_128", 16)
  238. def test_invalid_init_encrypt(self):
  239. # Path INIT->ENCRYPT fails
  240. cipher = AES.new(self.key_256, AES.MODE_SIV,
  241. nonce=self.nonce_96)
  242. self.assertRaises(TypeError, cipher.encrypt, b"xxx")
  243. def test_invalid_init_decrypt(self):
  244. # Path INIT->DECRYPT fails
  245. cipher = AES.new(self.key_256, AES.MODE_SIV,
  246. nonce=self.nonce_96)
  247. self.assertRaises(TypeError, cipher.decrypt, b"xxx")
  248. def test_valid_init_update_digest_verify(self):
  249. # No plaintext, fixed authenticated data
  250. # Verify path INIT->UPDATE->DIGEST
  251. cipher = AES.new(self.key_256, AES.MODE_SIV,
  252. nonce=self.nonce_96)
  253. cipher.update(self.data_128)
  254. mac = cipher.digest()
  255. # Verify path INIT->UPDATE->VERIFY
  256. cipher = AES.new(self.key_256, AES.MODE_SIV,
  257. nonce=self.nonce_96)
  258. cipher.update(self.data_128)
  259. cipher.verify(mac)
  260. def test_valid_init_digest(self):
  261. # Verify path INIT->DIGEST
  262. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  263. cipher.digest()
  264. def test_valid_init_verify(self):
  265. # Verify path INIT->VERIFY
  266. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  267. mac = cipher.digest()
  268. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  269. cipher.verify(mac)
  270. def test_valid_multiple_digest_or_verify(self):
  271. # Multiple calls to digest
  272. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  273. cipher.update(self.data_128)
  274. first_mac = cipher.digest()
  275. for x in range(4):
  276. self.assertEqual(first_mac, cipher.digest())
  277. # Multiple calls to verify
  278. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  279. cipher.update(self.data_128)
  280. for x in range(5):
  281. cipher.verify(first_mac)
  282. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  283. # encrypt_and_digest
  284. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  285. cipher.update(self.data_128)
  286. ct, mac = cipher.encrypt_and_digest(self.data_128)
  287. # decrypt_and_verify
  288. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  289. cipher.update(self.data_128)
  290. pt = cipher.decrypt_and_verify(ct, mac)
  291. self.assertEqual(self.data_128, pt)
  292. def test_invalid_multiple_encrypt_and_digest(self):
  293. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  294. ct, tag = cipher.encrypt_and_digest(self.data_128)
  295. self.assertRaises(TypeError, cipher.encrypt_and_digest, b'')
  296. def test_invalid_multiple_decrypt_and_verify(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. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  300. cipher.decrypt_and_verify(ct, tag)
  301. self.assertRaises(TypeError, cipher.decrypt_and_verify, ct, tag)
  302. def transform(tv):
  303. new_tv = [[unhexlify(x) for x in tv[0].split("-")]]
  304. new_tv += [ unhexlify(x) for x in tv[1:5]]
  305. if tv[5]:
  306. nonce = unhexlify(tv[5])
  307. else:
  308. nonce = None
  309. new_tv += [ nonce ]
  310. return new_tv
  311. class TestVectors(unittest.TestCase):
  312. """Class exercising the SIV test vectors found in RFC5297"""
  313. # This is a list of tuples with 5 items:
  314. #
  315. # 1. Header + '|' + plaintext
  316. # 2. Header + '|' + ciphertext + '|' + MAC
  317. # 3. AES-128 key
  318. # 4. Description
  319. # 5. Dictionary of parameters to be passed to AES.new().
  320. # It must include the nonce.
  321. #
  322. # A "Header" is a dash ('-') separated sequece of components.
  323. #
  324. test_vectors_hex = [
  325. (
  326. '101112131415161718191a1b1c1d1e1f2021222324252627',
  327. '112233445566778899aabbccddee',
  328. '40c02b9690c4dc04daef7f6afe5c',
  329. '85632d07c6e8f37f950acd320a2ecc93',
  330. 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff',
  331. None
  332. ),
  333. (
  334. '00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988' +
  335. '7766554433221100-102030405060708090a0',
  336. '7468697320697320736f6d6520706c61696e7465787420746f20656e63727970' +
  337. '74207573696e67205349562d414553',
  338. 'cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829' +
  339. 'ea64ad544a272e9c485b62a3fd5c0d',
  340. '7bdb6e3b432667eb06f4d14bff2fbd0f',
  341. '7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4f',
  342. '09f911029d74e35bd84156c5635688c0'
  343. ),
  344. ]
  345. test_vectors = [ transform(tv) for tv in test_vectors_hex ]
  346. def runTest(self):
  347. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  348. # Encrypt
  349. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  350. for x in assoc_data:
  351. cipher.update(x)
  352. ct2, mac2 = cipher.encrypt_and_digest(pt)
  353. self.assertEqual(ct, ct2)
  354. self.assertEqual(mac, mac2)
  355. # Decrypt
  356. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  357. for x in assoc_data:
  358. cipher.update(x)
  359. pt2 = cipher.decrypt_and_verify(ct, mac)
  360. self.assertEqual(pt, pt2)
  361. class TestVectorsWycheproof(unittest.TestCase):
  362. def __init__(self):
  363. unittest.TestCase.__init__(self)
  364. self._id = "None"
  365. def setUp(self):
  366. self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
  367. "aes_siv_cmac_test.json",
  368. "Wycheproof AES SIV")
  369. def shortDescription(self):
  370. return self._id
  371. def test_encrypt(self, tv):
  372. self._id = "Wycheproof Encrypt AES-SIV Test #" + str(tv.id)
  373. cipher = AES.new(tv.key, AES.MODE_SIV)
  374. cipher.update(tv.aad)
  375. ct, tag = cipher.encrypt_and_digest(tv.msg)
  376. if tv.valid:
  377. self.assertEqual(tag + ct, tv.ct)
  378. def test_decrypt(self, tv):
  379. self._id = "Wycheproof Decrypt AES_SIV Test #" + str(tv.id)
  380. cipher = AES.new(tv.key, AES.MODE_SIV)
  381. cipher.update(tv.aad)
  382. try:
  383. pt = cipher.decrypt_and_verify(tv.ct[16:], tv.ct[:16])
  384. except ValueError:
  385. assert not tv.valid
  386. else:
  387. assert tv.valid
  388. self.assertEqual(pt, tv.msg)
  389. def runTest(self):
  390. for tv in self.tv:
  391. self.test_encrypt(tv)
  392. self.test_decrypt(tv)
  393. class TestVectorsWycheproof2(unittest.TestCase):
  394. def __init__(self):
  395. unittest.TestCase.__init__(self)
  396. self._id = "None"
  397. def setUp(self):
  398. self.tv = load_test_vectors_wycheproof(("Cipher", "wycheproof"),
  399. "aead_aes_siv_cmac_test.json",
  400. "Wycheproof AEAD SIV")
  401. def shortDescription(self):
  402. return self._id
  403. def test_encrypt(self, tv):
  404. self._id = "Wycheproof Encrypt AEAD-AES-SIV Test #" + str(tv.id)
  405. cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
  406. cipher.update(tv.aad)
  407. ct, tag = cipher.encrypt_and_digest(tv.msg)
  408. if tv.valid:
  409. self.assertEqual(ct, tv.ct)
  410. self.assertEqual(tag, tv.tag)
  411. def test_decrypt(self, tv):
  412. self._id = "Wycheproof Decrypt AEAD-AES-SIV Test #" + str(tv.id)
  413. cipher = AES.new(tv.key, AES.MODE_SIV, nonce=tv.iv)
  414. cipher.update(tv.aad)
  415. try:
  416. pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
  417. except ValueError:
  418. assert not tv.valid
  419. else:
  420. assert tv.valid
  421. self.assertEqual(pt, tv.msg)
  422. def runTest(self):
  423. for tv in self.tv:
  424. self.test_encrypt(tv)
  425. self.test_decrypt(tv)
  426. def get_tests(config={}):
  427. wycheproof_warnings = config.get('wycheproof_warnings')
  428. tests = []
  429. tests += list_test_cases(SivTests)
  430. tests += list_test_cases(SivFSMTests)
  431. tests += [ TestVectors() ]
  432. tests += [ TestVectorsWycheproof() ]
  433. tests += [ TestVectorsWycheproof2() ]
  434. return tests
  435. if __name__ == '__main__':
  436. suite = lambda: unittest.TestSuite(get_tests())
  437. unittest.main(defaultTest='suite')