test_SIV.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 unittest
  31. from Crypto.SelfTest.st_common import list_test_cases
  32. from Crypto.Util.py3compat import unhexlify, tobytes, bchr, b
  33. from Crypto.Cipher import AES
  34. from Crypto.Hash import SHAKE128
  35. def get_tag_random(tag, length):
  36. return SHAKE128.new(data=tobytes(tag)).read(length)
  37. class SivTests(unittest.TestCase):
  38. key_256 = get_tag_random("key_256", 32)
  39. key_384 = get_tag_random("key_384", 48)
  40. key_512 = get_tag_random("key_512", 64)
  41. nonce_96 = get_tag_random("nonce_128", 12)
  42. data_128 = get_tag_random("data_128", 16)
  43. def test_loopback_128(self):
  44. for key in self.key_256, self.key_384, self.key_512:
  45. cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
  46. pt = get_tag_random("plaintext", 16 * 100)
  47. ct, mac = cipher.encrypt_and_digest(pt)
  48. cipher = AES.new(key, AES.MODE_SIV, nonce=self.nonce_96)
  49. pt2 = cipher.decrypt_and_verify(ct, mac)
  50. self.assertEqual(pt, pt2)
  51. def test_nonce(self):
  52. # Deterministic encryption
  53. AES.new(self.key_256, AES.MODE_SIV)
  54. cipher = AES.new(self.key_256, AES.MODE_SIV, self.nonce_96)
  55. ct = cipher.encrypt(self.data_128)
  56. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  57. self.assertEquals(ct, cipher.encrypt(self.data_128))
  58. def test_nonce_must_be_bytes(self):
  59. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  60. nonce=u'test12345678')
  61. def test_nonce_length(self):
  62. # nonce can be of any length (but not empty)
  63. self.assertRaises(ValueError, AES.new, self.key_256, AES.MODE_SIV,
  64. nonce=b(""))
  65. for x in range(1, 128):
  66. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=bchr(1) * x)
  67. cipher.encrypt(bchr(1))
  68. def test_block_size_128(self):
  69. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  70. self.assertEqual(cipher.block_size, AES.block_size)
  71. def test_nonce_attribute(self):
  72. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  73. self.assertEqual(cipher.nonce, self.nonce_96)
  74. # By default, no nonce is randomly generated
  75. self.failIf(hasattr(AES.new(self.key_256, AES.MODE_SIV), "nonce"))
  76. def test_unknown_parameters(self):
  77. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  78. self.nonce_96, 7)
  79. self.assertRaises(TypeError, AES.new, self.key_256, AES.MODE_SIV,
  80. nonce=self.nonce_96, unknown=7)
  81. # But some are only known by the base cipher
  82. # (e.g. use_aesni consumed by the AES module)
  83. AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96,
  84. use_aesni=False)
  85. def test_invalid_null_encryption(self):
  86. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  87. self.assertRaises(ValueError, cipher.encrypt, b(""))
  88. def test_invalid_null_component(self):
  89. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  90. self.assertRaises(ValueError, cipher.update, b(""))
  91. def test_encrypt_excludes_decrypt(self):
  92. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  93. cipher.encrypt(self.data_128)
  94. self.assertRaises(TypeError, cipher.decrypt, self.data_128)
  95. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  96. cipher.encrypt(self.data_128)
  97. self.assertRaises(TypeError, cipher.decrypt_and_verify,
  98. self.data_128, self.data_128)
  99. def test_data_must_be_bytes(self):
  100. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  101. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  102. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  103. self.assertRaises(TypeError, cipher.decrypt_and_verify,
  104. u'test1234567890-*', b("xxxx"))
  105. def test_mac_len(self):
  106. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  107. _, mac = cipher.encrypt_and_digest(self.data_128)
  108. self.assertEqual(len(mac), 16)
  109. def test_invalid_mac(self):
  110. from Crypto.Util.strxor import strxor_c
  111. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  112. ct, mac = cipher.encrypt_and_digest(self.data_128)
  113. invalid_mac = strxor_c(mac, 0x01)
  114. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  115. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  116. invalid_mac)
  117. def test_hex_mac(self):
  118. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  119. mac_hex = cipher.hexdigest()
  120. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  121. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  122. cipher.hexverify(mac_hex)
  123. class SivFSMTests(unittest.TestCase):
  124. key_256 = get_tag_random("key_256", 32)
  125. nonce_96 = get_tag_random("nonce_96", 12)
  126. data_128 = get_tag_random("data_128", 16)
  127. def test_valid_init_encrypt_decrypt_verify(self):
  128. # No authenticated data, fixed plaintext
  129. # Verify path INIT->ENCRYPT->DIGEST
  130. cipher = AES.new(self.key_256, AES.MODE_SIV,
  131. nonce=self.nonce_96)
  132. ct = cipher.encrypt(self.data_128)
  133. mac = cipher.digest()
  134. # Verify path INIT->DECRYPT_AND_VERIFY
  135. cipher = AES.new(self.key_256, AES.MODE_SIV,
  136. nonce=self.nonce_96)
  137. cipher.decrypt_and_verify(ct, mac)
  138. def test_invalid_init_decrypt(self):
  139. # Path INIT->DECRYPT fails
  140. cipher = AES.new(self.key_256, AES.MODE_SIV,
  141. nonce=self.nonce_96)
  142. self.assertRaises(TypeError, cipher.decrypt, b("xxx"))
  143. def test_valid_init_update_digest_verify(self):
  144. # No plaintext, fixed authenticated data
  145. # Verify path INIT->UPDATE->DIGEST
  146. cipher = AES.new(self.key_256, AES.MODE_SIV,
  147. nonce=self.nonce_96)
  148. cipher.update(self.data_128)
  149. mac = cipher.digest()
  150. # Verify path INIT->UPDATE->VERIFY
  151. cipher = AES.new(self.key_256, AES.MODE_SIV,
  152. nonce=self.nonce_96)
  153. cipher.update(self.data_128)
  154. cipher.verify(mac)
  155. def test_valid_full_path(self):
  156. # Fixed authenticated data, fixed plaintext
  157. # Verify path INIT->UPDATE->ENCRYPT->DIGEST
  158. cipher = AES.new(self.key_256, AES.MODE_SIV,
  159. nonce=self.nonce_96)
  160. cipher.update(self.data_128)
  161. ct = cipher.encrypt(self.data_128)
  162. mac = cipher.digest()
  163. # Verify path INIT->UPDATE->DECRYPT_AND_VERIFY
  164. cipher = AES.new(self.key_256, AES.MODE_SIV,
  165. nonce=self.nonce_96)
  166. cipher.update(self.data_128)
  167. cipher.decrypt_and_verify(ct, mac)
  168. def test_valid_init_digest(self):
  169. # Verify path INIT->DIGEST
  170. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  171. cipher.digest()
  172. def test_valid_init_verify(self):
  173. # Verify path INIT->VERIFY
  174. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  175. mac = cipher.digest()
  176. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  177. cipher.verify(mac)
  178. def test_invalid_multiple_encrypt(self):
  179. # Without AAD
  180. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  181. cipher.encrypt(b("xxx"))
  182. self.assertRaises(TypeError, cipher.encrypt, b("xxx"))
  183. # With AAD
  184. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  185. cipher.update(b("yyy"))
  186. cipher.encrypt(b("xxx"))
  187. self.assertRaises(TypeError, cipher.encrypt, b("xxx"))
  188. def test_valid_multiple_digest_or_verify(self):
  189. # Multiple calls to digest
  190. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  191. cipher.update(self.data_128)
  192. first_mac = cipher.digest()
  193. for x in xrange(4):
  194. self.assertEqual(first_mac, cipher.digest())
  195. # Multiple calls to verify
  196. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  197. cipher.update(self.data_128)
  198. for x in xrange(5):
  199. cipher.verify(first_mac)
  200. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  201. # encrypt_and_digest
  202. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  203. cipher.update(self.data_128)
  204. ct, mac = cipher.encrypt_and_digest(self.data_128)
  205. # decrypt_and_verify
  206. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  207. cipher.update(self.data_128)
  208. pt = cipher.decrypt_and_verify(ct, mac)
  209. self.assertEqual(self.data_128, pt)
  210. def test_invalid_encrypt_or_update_after_digest(self):
  211. for method_name in "encrypt", "update":
  212. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  213. cipher.encrypt(self.data_128)
  214. cipher.digest()
  215. self.assertRaises(TypeError, getattr(cipher, method_name),
  216. self.data_128)
  217. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  218. cipher.encrypt_and_digest(self.data_128)
  219. def test_invalid_decrypt_or_update_after_verify(self):
  220. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  221. ct = cipher.encrypt(self.data_128)
  222. mac = cipher.digest()
  223. for method_name in "decrypt", "update":
  224. cipher = AES.new(self.key_256, AES.MODE_SIV, nonce=self.nonce_96)
  225. cipher.decrypt_and_verify(ct, mac)
  226. self.assertRaises(TypeError, getattr(cipher, method_name),
  227. self.data_128)
  228. class TestVectors(unittest.TestCase):
  229. """Class exercising the SIV test vectors found in RFC5297"""
  230. # This is a list of tuples with 5 items:
  231. #
  232. # 1. Header + '|' + plaintext
  233. # 2. Header + '|' + ciphertext + '|' + MAC
  234. # 3. AES-128 key
  235. # 4. Description
  236. # 5. Dictionary of parameters to be passed to AES.new().
  237. # It must include the nonce.
  238. #
  239. # A "Header" is a dash ('-') separated sequece of components.
  240. #
  241. test_vectors = [
  242. (
  243. '101112131415161718191a1b1c1d1e1f2021222324252627',
  244. '112233445566778899aabbccddee',
  245. '40c02b9690c4dc04daef7f6afe5c',
  246. '85632d07c6e8f37f950acd320a2ecc93',
  247. 'fffefdfcfbfaf9f8f7f6f5f4f3f2f1f0f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff',
  248. None
  249. ),
  250. (
  251. '00112233445566778899aabbccddeeffdeaddadadeaddadaffeeddccbbaa9988' +
  252. '7766554433221100-102030405060708090a0',
  253. '7468697320697320736f6d6520706c61696e7465787420746f20656e63727970' +
  254. '74207573696e67205349562d414553',
  255. 'cb900f2fddbe404326601965c889bf17dba77ceb094fa663b7a3f748ba8af829' +
  256. 'ea64ad544a272e9c485b62a3fd5c0d',
  257. '7bdb6e3b432667eb06f4d14bff2fbd0f',
  258. '7f7e7d7c7b7a79787776757473727170404142434445464748494a4b4c4d4e4f',
  259. '09f911029d74e35bd84156c5635688c0'
  260. ),
  261. ]
  262. for index, tv in enumerate(test_vectors):
  263. test_vectors[index] = [[unhexlify(x) for x in tv[0].split("-")]]
  264. test_vectors[index] += [unhexlify(x) for x in tv[1:5]]
  265. if tv[5]:
  266. nonce = unhexlify(tv[5])
  267. else:
  268. nonce = None
  269. test_vectors[index].append(nonce)
  270. def runTest(self):
  271. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  272. # Encrypt
  273. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  274. for x in assoc_data:
  275. cipher.update(x)
  276. ct2, mac2 = cipher.encrypt_and_digest(pt)
  277. self.assertEqual(ct, ct2)
  278. self.assertEqual(mac, mac2)
  279. # Decrypt
  280. cipher = AES.new(key, AES.MODE_SIV, nonce=nonce)
  281. for x in assoc_data:
  282. cipher.update(x)
  283. pt2 = cipher.decrypt_and_verify(ct, mac)
  284. self.assertEqual(pt, pt2)
  285. def get_tests(config={}):
  286. tests = []
  287. tests += list_test_cases(SivTests)
  288. tests += list_test_cases(SivFSMTests)
  289. tests += [TestVectors()]
  290. return tests
  291. if __name__ == '__main__':
  292. suite = lambda: unittest.TestSuite(get_tests())
  293. unittest.main(defaultTest='suite')