test_GCM.py 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  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 GcmTests(unittest.TestCase):
  38. key_128 = get_tag_random("key_128", 16)
  39. nonce_96 = get_tag_random("nonce_128", 12)
  40. data_128 = get_tag_random("data_128", 16)
  41. def test_loopback_128(self):
  42. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  43. pt = get_tag_random("plaintext", 16 * 100)
  44. ct = cipher.encrypt(pt)
  45. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  46. pt2 = cipher.decrypt(ct)
  47. self.assertEqual(pt, pt2)
  48. def test_nonce(self):
  49. # Nonce is optional (a random one will be created)
  50. AES.new(self.key_128, AES.MODE_GCM)
  51. cipher = AES.new(self.key_128, AES.MODE_GCM, self.nonce_96)
  52. ct = cipher.encrypt(self.data_128)
  53. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  54. self.assertEquals(ct, cipher.encrypt(self.data_128))
  55. def test_nonce_must_be_bytes(self):
  56. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM,
  57. nonce=u'test12345678')
  58. def test_nonce_length(self):
  59. # nonce can be of any length (but not empty)
  60. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM,
  61. nonce=b(""))
  62. for x in range(1, 128):
  63. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=bchr(1) * x)
  64. cipher.encrypt(bchr(1))
  65. def test_block_size_128(self):
  66. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  67. self.assertEqual(cipher.block_size, AES.block_size)
  68. def test_nonce_attribute(self):
  69. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  70. self.assertEqual(cipher.nonce, self.nonce_96)
  71. # By default, a 15 bytes long nonce is randomly generated
  72. nonce1 = AES.new(self.key_128, AES.MODE_GCM).nonce
  73. nonce2 = AES.new(self.key_128, AES.MODE_GCM).nonce
  74. self.assertEqual(len(nonce1), 16)
  75. self.assertNotEqual(nonce1, nonce2)
  76. def test_unknown_parameters(self):
  77. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM,
  78. self.nonce_96, 7)
  79. self.assertRaises(TypeError, AES.new, self.key_128, AES.MODE_GCM,
  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_128, AES.MODE_GCM, nonce=self.nonce_96,
  84. use_aesni=False)
  85. def test_null_encryption_decryption(self):
  86. for func in "encrypt", "decrypt":
  87. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  88. result = getattr(cipher, func)(b(""))
  89. self.assertEqual(result, b(""))
  90. def test_either_encrypt_or_decrypt(self):
  91. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  92. cipher.encrypt(b(""))
  93. self.assertRaises(TypeError, cipher.decrypt, b(""))
  94. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  95. cipher.decrypt(b(""))
  96. self.assertRaises(TypeError, cipher.encrypt, b(""))
  97. def test_data_must_be_bytes(self):
  98. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  99. self.assertRaises(TypeError, cipher.encrypt, u'test1234567890-*')
  100. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  101. self.assertRaises(TypeError, cipher.decrypt, u'test1234567890-*')
  102. def test_mac_len(self):
  103. # Invalid MAC length
  104. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM,
  105. nonce=self.nonce_96, mac_len=3)
  106. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_GCM,
  107. nonce=self.nonce_96, mac_len=16+1)
  108. # Valid MAC length
  109. for mac_len in xrange(5, 16 + 1):
  110. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96,
  111. mac_len=mac_len)
  112. _, mac = cipher.encrypt_and_digest(self.data_128)
  113. self.assertEqual(len(mac), mac_len)
  114. # Default MAC length
  115. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  116. _, mac = cipher.encrypt_and_digest(self.data_128)
  117. self.assertEqual(len(mac), 16)
  118. def test_invalid_mac(self):
  119. from Crypto.Util.strxor import strxor_c
  120. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  121. ct, mac = cipher.encrypt_and_digest(self.data_128)
  122. invalid_mac = strxor_c(mac, 0x01)
  123. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  124. self.assertRaises(ValueError, cipher.decrypt_and_verify, ct,
  125. invalid_mac)
  126. def test_hex_mac(self):
  127. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  128. mac_hex = cipher.hexdigest()
  129. self.assertEqual(cipher.digest(), unhexlify(mac_hex))
  130. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  131. cipher.hexverify(mac_hex)
  132. def test_message_chunks(self):
  133. # Validate that both associated data and plaintext/ciphertext
  134. # can be broken up in chunks of arbitrary length
  135. auth_data = get_tag_random("authenticated data", 127)
  136. plaintext = get_tag_random("plaintext", 127)
  137. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  138. cipher.update(auth_data)
  139. ciphertext, ref_mac = cipher.encrypt_and_digest(plaintext)
  140. def break_up(data, chunk_length):
  141. return [data[i:i+chunk_length] for i in range(0, len(data),
  142. chunk_length)]
  143. # Encryption
  144. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  145. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  146. for chunk in break_up(auth_data, chunk_length):
  147. cipher.update(chunk)
  148. pt2 = b("")
  149. for chunk in break_up(ciphertext, chunk_length):
  150. pt2 += cipher.decrypt(chunk)
  151. self.assertEqual(plaintext, pt2)
  152. cipher.verify(ref_mac)
  153. # Decryption
  154. for chunk_length in 1, 2, 3, 7, 10, 13, 16, 40, 80, 128:
  155. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  156. for chunk in break_up(auth_data, chunk_length):
  157. cipher.update(chunk)
  158. ct2 = b("")
  159. for chunk in break_up(plaintext, chunk_length):
  160. ct2 += cipher.encrypt(chunk)
  161. self.assertEqual(ciphertext, ct2)
  162. self.assertEquals(cipher.digest(), ref_mac)
  163. class GcmFSMTests(unittest.TestCase):
  164. key_128 = get_tag_random("key_128", 16)
  165. nonce_96 = get_tag_random("nonce_128", 12)
  166. data_128 = get_tag_random("data_128", 16)
  167. def test_valid_init_encrypt_decrypt_digest_verify(self):
  168. # No authenticated data, fixed plaintext
  169. # Verify path INIT->ENCRYPT->DIGEST
  170. cipher = AES.new(self.key_128, AES.MODE_GCM,
  171. nonce=self.nonce_96)
  172. ct = cipher.encrypt(self.data_128)
  173. mac = cipher.digest()
  174. # Verify path INIT->DECRYPT->VERIFY
  175. cipher = AES.new(self.key_128, AES.MODE_GCM,
  176. nonce=self.nonce_96)
  177. cipher.decrypt(ct)
  178. cipher.verify(mac)
  179. def test_valid_init_update_digest_verify(self):
  180. # No plaintext, fixed authenticated data
  181. # Verify path INIT->UPDATE->DIGEST
  182. cipher = AES.new(self.key_128, AES.MODE_GCM,
  183. nonce=self.nonce_96)
  184. cipher.update(self.data_128)
  185. mac = cipher.digest()
  186. # Verify path INIT->UPDATE->VERIFY
  187. cipher = AES.new(self.key_128, AES.MODE_GCM,
  188. nonce=self.nonce_96)
  189. cipher.update(self.data_128)
  190. cipher.verify(mac)
  191. def test_valid_full_path(self):
  192. # Fixed authenticated data, fixed plaintext
  193. # Verify path INIT->UPDATE->ENCRYPT->DIGEST
  194. cipher = AES.new(self.key_128, AES.MODE_GCM,
  195. nonce=self.nonce_96)
  196. cipher.update(self.data_128)
  197. ct = cipher.encrypt(self.data_128)
  198. mac = cipher.digest()
  199. # Verify path INIT->UPDATE->DECRYPT->VERIFY
  200. cipher = AES.new(self.key_128, AES.MODE_GCM,
  201. nonce=self.nonce_96)
  202. cipher.update(self.data_128)
  203. cipher.decrypt(ct)
  204. cipher.verify(mac)
  205. def test_valid_init_digest(self):
  206. # Verify path INIT->DIGEST
  207. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  208. cipher.digest()
  209. def test_valid_init_verify(self):
  210. # Verify path INIT->VERIFY
  211. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  212. mac = cipher.digest()
  213. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  214. cipher.verify(mac)
  215. def test_valid_multiple_encrypt_or_decrypt(self):
  216. for method_name in "encrypt", "decrypt":
  217. for auth_data in (None, b("333"), self.data_128,
  218. self.data_128 + b("3")):
  219. if auth_data is None:
  220. assoc_len = None
  221. else:
  222. assoc_len = len(auth_data)
  223. cipher = AES.new(self.key_128, AES.MODE_GCM,
  224. nonce=self.nonce_96)
  225. if auth_data is not None:
  226. cipher.update(auth_data)
  227. method = getattr(cipher, method_name)
  228. method(self.data_128)
  229. method(self.data_128)
  230. method(self.data_128)
  231. method(self.data_128)
  232. def test_valid_multiple_digest_or_verify(self):
  233. # Multiple calls to digest
  234. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  235. cipher.update(self.data_128)
  236. first_mac = cipher.digest()
  237. for x in xrange(4):
  238. self.assertEqual(first_mac, cipher.digest())
  239. # Multiple calls to verify
  240. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  241. cipher.update(self.data_128)
  242. for x in xrange(5):
  243. cipher.verify(first_mac)
  244. def test_valid_encrypt_and_digest_decrypt_and_verify(self):
  245. # encrypt_and_digest
  246. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  247. cipher.update(self.data_128)
  248. ct, mac = cipher.encrypt_and_digest(self.data_128)
  249. # decrypt_and_verify
  250. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  251. cipher.update(self.data_128)
  252. pt = cipher.decrypt_and_verify(ct, mac)
  253. self.assertEqual(self.data_128, pt)
  254. def test_invalid_mixing_encrypt_decrypt(self):
  255. # Once per method, with or without assoc. data
  256. for method1_name, method2_name in (("encrypt", "decrypt"),
  257. ("decrypt", "encrypt")):
  258. for assoc_data_present in (True, False):
  259. cipher = AES.new(self.key_128, AES.MODE_GCM,
  260. nonce=self.nonce_96)
  261. if assoc_data_present:
  262. cipher.update(self.data_128)
  263. getattr(cipher, method1_name)(self.data_128)
  264. self.assertRaises(TypeError, getattr(cipher, method2_name),
  265. self.data_128)
  266. def test_invalid_encrypt_or_update_after_digest(self):
  267. for method_name in "encrypt", "update":
  268. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  269. cipher.encrypt(self.data_128)
  270. cipher.digest()
  271. self.assertRaises(TypeError, getattr(cipher, method_name),
  272. self.data_128)
  273. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  274. cipher.encrypt_and_digest(self.data_128)
  275. def test_invalid_decrypt_or_update_after_verify(self):
  276. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  277. ct = cipher.encrypt(self.data_128)
  278. mac = cipher.digest()
  279. for method_name in "decrypt", "update":
  280. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  281. cipher.decrypt(ct)
  282. cipher.verify(mac)
  283. self.assertRaises(TypeError, getattr(cipher, method_name),
  284. self.data_128)
  285. cipher = AES.new(self.key_128, AES.MODE_GCM, nonce=self.nonce_96)
  286. cipher.decrypt_and_verify(ct, mac)
  287. self.assertRaises(TypeError, getattr(cipher, method_name),
  288. self.data_128)
  289. class TestVectors(unittest.TestCase):
  290. """Class exercising the GCM test vectors found in
  291. http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/gcm/gcm-revised-spec.pdf"""
  292. # List of test vectors, each made up of:
  293. # - authenticated data
  294. # - plaintext
  295. # - ciphertext
  296. # - MAC
  297. # - AES key
  298. # - nonce
  299. test_vectors = [
  300. (
  301. '',
  302. '',
  303. '',
  304. '58e2fccefa7e3061367f1d57a4e7455a',
  305. '00000000000000000000000000000000',
  306. '000000000000000000000000'
  307. ),
  308. (
  309. '',
  310. '00000000000000000000000000000000',
  311. '0388dace60b6a392f328c2b971b2fe78',
  312. 'ab6e47d42cec13bdf53a67b21257bddf',
  313. '00000000000000000000000000000000',
  314. '000000000000000000000000'
  315. ),
  316. (
  317. '',
  318. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  319. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
  320. '42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e' +
  321. '21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091473f5985',
  322. '4d5c2af327cd64a62cf35abd2ba6fab4',
  323. 'feffe9928665731c6d6a8f9467308308',
  324. 'cafebabefacedbaddecaf888'
  325. ),
  326. (
  327. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  328. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  329. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  330. '42831ec2217774244b7221b784d0d49ce3aa212f2c02a4e035c17e2329aca12e' +
  331. '21d514b25466931c7d8f6a5aac84aa051ba30b396a0aac973d58e091',
  332. '5bc94fbc3221a5db94fae95ae7121a47',
  333. 'feffe9928665731c6d6a8f9467308308',
  334. 'cafebabefacedbaddecaf888'
  335. ),
  336. (
  337. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  338. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  339. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  340. '61353b4c2806934a777ff51fa22a4755699b2a714fcdc6f83766e5f97b6c7423' +
  341. '73806900e49f24b22b097544d4896b424989b5e1ebac0f07c23f4598',
  342. '3612d2e79e3b0785561be14aaca2fccb',
  343. 'feffe9928665731c6d6a8f9467308308',
  344. 'cafebabefacedbad'
  345. ),
  346. (
  347. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  348. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  349. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  350. '8ce24998625615b603a033aca13fb894be9112a5c3a211a8ba262a3cca7e2ca7' +
  351. '01e4a9a4fba43c90ccdcb281d48c7c6fd62875d2aca417034c34aee5',
  352. '619cc5aefffe0bfa462af43c1699d050',
  353. 'feffe9928665731c6d6a8f9467308308',
  354. '9313225df88406e555909c5aff5269aa' +
  355. '6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254'+
  356. '16aedbf5a0de6a57a637b39b'
  357. ),
  358. (
  359. '',
  360. '',
  361. '',
  362. 'cd33b28ac773f74ba00ed1f312572435',
  363. '000000000000000000000000000000000000000000000000',
  364. '000000000000000000000000'
  365. ),
  366. (
  367. '',
  368. '00000000000000000000000000000000',
  369. '98e7247c07f0fe411c267e4384b0f600',
  370. '2ff58d80033927ab8ef4d4587514f0fb',
  371. '000000000000000000000000000000000000000000000000',
  372. '000000000000000000000000'
  373. ),
  374. (
  375. '',
  376. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  377. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
  378. '3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c' +
  379. '7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710acade256',
  380. '9924a7c8587336bfb118024db8674a14',
  381. 'feffe9928665731c6d6a8f9467308308feffe9928665731c',
  382. 'cafebabefacedbaddecaf888'
  383. ),
  384. (
  385. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  386. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  387. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  388. '3980ca0b3c00e841eb06fac4872a2757859e1ceaa6efd984628593b40ca1e19c' +
  389. '7d773d00c144c525ac619d18c84a3f4718e2448b2fe324d9ccda2710',
  390. '2519498e80f1478f37ba55bd6d27618c',
  391. 'feffe9928665731c6d6a8f9467308308feffe9928665731c',
  392. 'cafebabefacedbaddecaf888'
  393. ),
  394. (
  395. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  396. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  397. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  398. '0f10f599ae14a154ed24b36e25324db8c566632ef2bbb34f8347280fc4507057' +
  399. 'fddc29df9a471f75c66541d4d4dad1c9e93a19a58e8b473fa0f062f7',
  400. '65dcc57fcf623a24094fcca40d3533f8',
  401. 'feffe9928665731c6d6a8f9467308308feffe9928665731c',
  402. 'cafebabefacedbad'
  403. ),
  404. (
  405. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  406. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  407. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  408. 'd27e88681ce3243c4830165a8fdcf9ff1de9a1d8e6b447ef6ef7b79828666e45' +
  409. '81e79012af34ddd9e2f037589b292db3e67c036745fa22e7e9b7373b',
  410. 'dcf566ff291c25bbb8568fc3d376a6d9',
  411. 'feffe9928665731c6d6a8f9467308308feffe9928665731c',
  412. '9313225df88406e555909c5aff5269aa' +
  413. '6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254' +
  414. '16aedbf5a0de6a57a637b39b'
  415. ),
  416. (
  417. '',
  418. '',
  419. '',
  420. '530f8afbc74536b9a963b4f1c4cb738b',
  421. '0000000000000000000000000000000000000000000000000000000000000000',
  422. '000000000000000000000000'
  423. ),
  424. (
  425. '',
  426. '00000000000000000000000000000000',
  427. 'cea7403d4d606b6e074ec5d3baf39d18',
  428. 'd0d1c8a799996bf0265b98b5d48ab919',
  429. '0000000000000000000000000000000000000000000000000000000000000000',
  430. '000000000000000000000000'
  431. ),
  432. ( '',
  433. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  434. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b391aafd255',
  435. '522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa' +
  436. '8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662898015ad',
  437. 'b094dac5d93471bdec1a502270e3cc6c',
  438. 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
  439. 'cafebabefacedbaddecaf888'
  440. ),
  441. (
  442. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  443. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  444. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  445. '522dc1f099567d07f47f37a32a84427d643a8cdcbfe5c0c97598a2bd2555d1aa' +
  446. '8cb08e48590dbb3da7b08b1056828838c5f61e6393ba7a0abcc9f662',
  447. '76fc6ece0f4e1768cddf8853bb2d551b',
  448. 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
  449. 'cafebabefacedbaddecaf888'
  450. ),
  451. (
  452. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  453. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  454. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  455. 'c3762df1ca787d32ae47c13bf19844cbaf1ae14d0b976afac52ff7d79bba9de0' +
  456. 'feb582d33934a4f0954cc2363bc73f7862ac430e64abe499f47c9b1f',
  457. '3a337dbf46a792c45e454913fe2ea8f2',
  458. 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
  459. 'cafebabefacedbad'
  460. ),
  461. (
  462. 'feedfacedeadbeeffeedfacedeadbeefabaddad2',
  463. 'd9313225f88406e5a55909c5aff5269a86a7a9531534f7da2e4c303d8a318a72' +
  464. '1c3c0c95956809532fcf0e2449a6b525b16aedf5aa0de657ba637b39',
  465. '5a8def2f0c9e53f1f75d7853659e2a20eeb2b22aafde6419a058ab4f6f746bf4' +
  466. '0fc0c3b780f244452da3ebf1c5d82cdea2418997200ef82e44ae7e3f',
  467. 'a44a8266ee1c8eb0c8b5d4cf5ae9f19a',
  468. 'feffe9928665731c6d6a8f9467308308feffe9928665731c6d6a8f9467308308',
  469. '9313225df88406e555909c5aff5269aa' +
  470. '6a7a9538534f7da1e4c303d2a318a728c3c0c95156809539fcf0e2429a6b5254'+
  471. '16aedbf5a0de6a57a637b39b'
  472. )
  473. ]
  474. for index, tv in enumerate(test_vectors):
  475. test_vectors[index] = (unhexlify(x) for x in tv)
  476. def runTest(self):
  477. for assoc_data, pt, ct, mac, key, nonce in self.test_vectors:
  478. # Encrypt
  479. cipher = AES.new(key, AES.MODE_GCM, nonce, mac_len=len(mac))
  480. cipher.update(assoc_data)
  481. ct2, mac2 = cipher.encrypt_and_digest(pt)
  482. self.assertEqual(ct, ct2)
  483. self.assertEqual(mac, mac2)
  484. # Decrypt
  485. cipher = AES.new(key, AES.MODE_GCM, nonce, mac_len=len(mac))
  486. cipher.update(assoc_data)
  487. pt2 = cipher.decrypt_and_verify(ct, mac)
  488. self.assertEqual(pt, pt2)
  489. class TestVectorsGueronKrasnov(unittest.TestCase):
  490. """Class exercising the GCM test vectors found in
  491. 'The fragility of AES-GCM authentication algorithm', Gueron, Krasnov
  492. https://eprint.iacr.org/2013/157.pdf"""
  493. def test_1(self):
  494. key = unhexlify("3da6c536d6295579c0959a7043efb503")
  495. iv = unhexlify("2b926197d34e091ef722db94")
  496. aad = unhexlify("00000000000000000000000000000000" +
  497. "000102030405060708090a0b0c0d0e0f" +
  498. "101112131415161718191a1b1c1d1e1f" +
  499. "202122232425262728292a2b2c2d2e2f" +
  500. "303132333435363738393a3b3c3d3e3f")
  501. digest = unhexlify("69dd586555ce3fcc89663801a71d957b")
  502. cipher = AES.new(key, AES.MODE_GCM, iv).update(aad)
  503. self.assertEqual(digest, cipher.digest())
  504. def test_2(self):
  505. key = unhexlify("843ffcf5d2b72694d19ed01d01249412")
  506. iv = unhexlify("dbcca32ebf9b804617c3aa9e")
  507. aad = unhexlify("00000000000000000000000000000000" +
  508. "101112131415161718191a1b1c1d1e1f")
  509. pt = unhexlify("000102030405060708090a0b0c0d0e0f" +
  510. "101112131415161718191a1b1c1d1e1f" +
  511. "202122232425262728292a2b2c2d2e2f" +
  512. "303132333435363738393a3b3c3d3e3f" +
  513. "404142434445464748494a4b4c4d4e4f")
  514. ct = unhexlify("6268c6fa2a80b2d137467f092f657ac0" +
  515. "4d89be2beaa623d61b5a868c8f03ff95" +
  516. "d3dcee23ad2f1ab3a6c80eaf4b140eb0" +
  517. "5de3457f0fbc111a6b43d0763aa422a3" +
  518. "013cf1dc37fe417d1fbfc449b75d4cc5")
  519. digest = unhexlify("3b629ccfbc1119b7319e1dce2cd6fd6d")
  520. cipher = AES.new(key, AES.MODE_GCM, iv).update(aad)
  521. ct2, digest2 = cipher.encrypt_and_digest(pt)
  522. self.assertEqual(ct, ct2)
  523. self.assertEqual(digest, digest2)
  524. from Crypto.SelfTest.loader import load_tests
  525. class NISTTestVectorsGCM(unittest.TestCase):
  526. pass
  527. test_vectors_nist = load_tests(
  528. ("Crypto", "SelfTest", "Cipher", "test_vectors", "AES"),
  529. "gcmDecrypt128.rsp",
  530. "GCM decrypt",
  531. { "count" : lambda x: int(x) })
  532. test_vectors_nist += load_tests(
  533. ("Crypto", "SelfTest", "Cipher", "test_vectors", "AES"),
  534. "gcmEncryptExtIV128.rsp",
  535. "GCM encrypt",
  536. { "count" : lambda x: int(x) })
  537. for idx, tv in enumerate(test_vectors_nist):
  538. # The test vector file contains some directive lines
  539. if isinstance(tv, basestring):
  540. continue
  541. def single_test(self, tv=tv):
  542. self.description = tv.desc
  543. cipher = AES.new(tv.key, AES.MODE_GCM, nonce=tv.iv,
  544. mac_len=len(tv.tag))
  545. cipher.update(tv.aad)
  546. if "FAIL" in tv.others:
  547. self.assertRaises(ValueError, cipher.decrypt_and_verify,
  548. tv.ct, tv.tag)
  549. else:
  550. pt = cipher.decrypt_and_verify(tv.ct, tv.tag)
  551. self.assertEqual(pt, tv.pt)
  552. setattr(NISTTestVectorsGCM, "test_%d" % idx, single_test)
  553. def get_tests(config={}):
  554. tests = []
  555. tests += list_test_cases(GcmTests)
  556. tests += list_test_cases(GcmFSMTests)
  557. tests += [TestVectors()]
  558. tests += list_test_cases(TestVectorsGueronKrasnov)
  559. tests += list_test_cases(NISTTestVectorsGCM)
  560. return tests
  561. if __name__ == '__main__':
  562. suite = lambda: unittest.TestSuite(get_tests())
  563. unittest.main(defaultTest='suite')