test_CFB.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 unittest
  31. from binascii import unhexlify
  32. from tls.Crypto.SelfTest.loader import load_tests
  33. from tls.Crypto.SelfTest.st_common import list_test_cases
  34. from tls.Crypto.Util.py3compat import tobytes, is_string
  35. from tls.Crypto.Cipher import AES, DES3, DES
  36. from tls.Crypto.Hash import SHAKE128
  37. def get_tag_random(tag, length):
  38. return SHAKE128.new(data=tobytes(tag)).read(length)
  39. from tls.Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests
  40. class CfbTests(BlockChainingTests):
  41. aes_mode = AES.MODE_CFB
  42. des3_mode = DES3.MODE_CFB
  43. # Redefine test_unaligned_data_128/64
  44. def test_unaligned_data_128(self):
  45. plaintexts = [ b"7777777" ] * 100
  46. cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8)
  47. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  48. cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=8)
  49. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  50. cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128)
  51. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  52. cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128, segment_size=128)
  53. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  54. def test_unaligned_data_64(self):
  55. plaintexts = [ b"7777777" ] * 100
  56. cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8)
  57. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  58. cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=8)
  59. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  60. cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64)
  61. ciphertexts = [ cipher.encrypt(x) for x in plaintexts ]
  62. cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64, segment_size=64)
  63. self.assertEqual(b"".join(ciphertexts), cipher.encrypt(b"".join(plaintexts)))
  64. # Extra
  65. def test_segment_size_128(self):
  66. for bits in range(8, 129, 8):
  67. cipher = AES.new(self.key_128, AES.MODE_CFB, self.iv_128,
  68. segment_size=bits)
  69. for bits in 0, 7, 9, 127, 129:
  70. self.assertRaises(ValueError, AES.new, self.key_128, AES.MODE_CFB,
  71. self.iv_128,
  72. segment_size=bits)
  73. def test_segment_size_64(self):
  74. for bits in range(8, 65, 8):
  75. cipher = DES3.new(self.key_192, DES3.MODE_CFB, self.iv_64,
  76. segment_size=bits)
  77. for bits in 0, 7, 9, 63, 65:
  78. self.assertRaises(ValueError, DES3.new, self.key_192, AES.MODE_CFB,
  79. self.iv_64,
  80. segment_size=bits)
  81. class NistCfbVectors(unittest.TestCase):
  82. def _do_kat_aes_test(self, file_name, segment_size):
  83. test_vectors = load_tests(("Crypto", "SelfTest", "Cipher", "test_vectors", "AES"),
  84. file_name,
  85. "AES CFB%d KAT" % segment_size,
  86. { "count" : lambda x: int(x) } )
  87. assert(test_vectors)
  88. direction = None
  89. for tv in test_vectors:
  90. # The test vector file contains some directive lines
  91. if is_string(tv):
  92. direction = tv
  93. continue
  94. self.description = tv.desc
  95. cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv,
  96. segment_size=segment_size)
  97. if direction == "[ENCRYPT]":
  98. self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext)
  99. elif direction == "[DECRYPT]":
  100. self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
  101. else:
  102. assert False
  103. # See Section 6.4.5 in AESAVS
  104. def _do_mct_aes_test(self, file_name, segment_size):
  105. test_vectors = load_tests(("Crypto", "SelfTest", "Cipher", "test_vectors", "AES"),
  106. file_name,
  107. "AES CFB%d Montecarlo" % segment_size,
  108. { "count" : lambda x: int(x) } )
  109. assert(test_vectors)
  110. assert(segment_size in (8, 128))
  111. direction = None
  112. for tv in test_vectors:
  113. # The test vector file contains some directive lines
  114. if is_string(tv):
  115. direction = tv
  116. continue
  117. self.description = tv.desc
  118. cipher = AES.new(tv.key, AES.MODE_CFB, tv.iv,
  119. segment_size=segment_size)
  120. def get_input(input_text, output_seq, j):
  121. # CFB128
  122. if segment_size == 128:
  123. if j >= 2:
  124. return output_seq[-2]
  125. return [input_text, tv.iv][j]
  126. # CFB8
  127. if j == 0:
  128. return input_text
  129. elif j <= 16:
  130. return tv.iv[j - 1:j]
  131. return output_seq[j - 17]
  132. if direction == '[ENCRYPT]':
  133. cts = []
  134. for j in range(1000):
  135. plaintext = get_input(tv.plaintext, cts, j)
  136. cts.append(cipher.encrypt(plaintext))
  137. self.assertEqual(cts[-1], tv.ciphertext)
  138. elif direction == '[DECRYPT]':
  139. pts = []
  140. for j in range(1000):
  141. ciphertext = get_input(tv.ciphertext, pts, j)
  142. pts.append(cipher.decrypt(ciphertext))
  143. self.assertEqual(pts[-1], tv.plaintext)
  144. else:
  145. assert False
  146. def _do_tdes_test(self, file_name, segment_size):
  147. test_vectors = load_tests(("Crypto", "SelfTest", "Cipher", "test_vectors", "TDES"),
  148. file_name,
  149. "AES CFB%d KAT" % segment_size,
  150. { "count" : lambda x: int(x) } )
  151. assert(test_vectors)
  152. direction = None
  153. for tv in test_vectors:
  154. # The test vector file contains some directive lines
  155. if is_string(tv):
  156. direction = tv
  157. continue
  158. self.description = tv.desc
  159. if hasattr(tv, "keys"):
  160. cipher = DES.new(tv.keys, DES.MODE_CFB, tv.iv,
  161. segment_size=segment_size)
  162. else:
  163. if tv.key1 != tv.key3:
  164. key = tv.key1 + tv.key2 + tv.key3 # Option 3
  165. else:
  166. key = tv.key1 + tv.key2 # Option 2
  167. cipher = DES3.new(key, DES3.MODE_CFB, tv.iv,
  168. segment_size=segment_size)
  169. if direction == "[ENCRYPT]":
  170. self.assertEqual(cipher.encrypt(tv.plaintext), tv.ciphertext)
  171. elif direction == "[DECRYPT]":
  172. self.assertEqual(cipher.decrypt(tv.ciphertext), tv.plaintext)
  173. else:
  174. assert False
  175. # Create one test method per file
  176. nist_aes_kat_mmt_files = (
  177. # KAT
  178. "CFB?GFSbox128.rsp",
  179. "CFB?GFSbox192.rsp",
  180. "CFB?GFSbox256.rsp",
  181. "CFB?KeySbox128.rsp",
  182. "CFB?KeySbox192.rsp",
  183. "CFB?KeySbox256.rsp",
  184. "CFB?VarKey128.rsp",
  185. "CFB?VarKey192.rsp",
  186. "CFB?VarKey256.rsp",
  187. "CFB?VarTxt128.rsp",
  188. "CFB?VarTxt192.rsp",
  189. "CFB?VarTxt256.rsp",
  190. # MMT
  191. "CFB?MMT128.rsp",
  192. "CFB?MMT192.rsp",
  193. "CFB?MMT256.rsp",
  194. )
  195. nist_aes_mct_files = (
  196. "CFB?MCT128.rsp",
  197. "CFB?MCT192.rsp",
  198. "CFB?MCT256.rsp",
  199. )
  200. for file_gen_name in nist_aes_kat_mmt_files:
  201. for bits in "8", "128":
  202. file_name = file_gen_name.replace("?", bits)
  203. def new_func(self, file_name=file_name, bits=bits):
  204. self._do_kat_aes_test(file_name, int(bits))
  205. setattr(NistCfbVectors, "test_AES_" + file_name, new_func)
  206. for file_gen_name in nist_aes_mct_files:
  207. for bits in "8", "128":
  208. file_name = file_gen_name.replace("?", bits)
  209. def new_func(self, file_name=file_name, bits=bits):
  210. self._do_mct_aes_test(file_name, int(bits))
  211. setattr(NistCfbVectors, "test_AES_" + file_name, new_func)
  212. del file_name, new_func
  213. nist_tdes_files = (
  214. "TCFB?MMT2.rsp", # 2TDES
  215. "TCFB?MMT3.rsp", # 3TDES
  216. "TCFB?invperm.rsp", # Single DES
  217. "TCFB?permop.rsp",
  218. "TCFB?subtab.rsp",
  219. "TCFB?varkey.rsp",
  220. "TCFB?vartext.rsp",
  221. )
  222. for file_gen_name in nist_tdes_files:
  223. for bits in "8", "64":
  224. file_name = file_gen_name.replace("?", bits)
  225. def new_func(self, file_name=file_name, bits=bits):
  226. self._do_tdes_test(file_name, int(bits))
  227. setattr(NistCfbVectors, "test_TDES_" + file_name, new_func)
  228. # END OF NIST CBC TEST VECTORS
  229. class SP800TestVectors(unittest.TestCase):
  230. """Class exercising the CFB test vectors found in Section F.3
  231. of NIST SP 800-3A"""
  232. def test_aes_128_cfb8(self):
  233. plaintext = '6bc1bee22e409f96e93d7e117393172aae2d'
  234. ciphertext = '3b79424c9c0dd436bace9e0ed4586a4f32b9'
  235. key = '2b7e151628aed2a6abf7158809cf4f3c'
  236. iv = '000102030405060708090a0b0c0d0e0f'
  237. key = unhexlify(key)
  238. iv = unhexlify(iv)
  239. plaintext = unhexlify(plaintext)
  240. ciphertext = unhexlify(ciphertext)
  241. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  242. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  243. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  244. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  245. def test_aes_192_cfb8(self):
  246. plaintext = '6bc1bee22e409f96e93d7e117393172aae2d'
  247. ciphertext = 'cda2521ef0a905ca44cd057cbf0d47a0678a'
  248. key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
  249. iv = '000102030405060708090a0b0c0d0e0f'
  250. key = unhexlify(key)
  251. iv = unhexlify(iv)
  252. plaintext = unhexlify(plaintext)
  253. ciphertext = unhexlify(ciphertext)
  254. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  255. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  256. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  257. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  258. def test_aes_256_cfb8(self):
  259. plaintext = '6bc1bee22e409f96e93d7e117393172aae2d'
  260. ciphertext = 'dc1f1a8520a64db55fcc8ac554844e889700'
  261. key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
  262. iv = '000102030405060708090a0b0c0d0e0f'
  263. key = unhexlify(key)
  264. iv = unhexlify(iv)
  265. plaintext = unhexlify(plaintext)
  266. ciphertext = unhexlify(ciphertext)
  267. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  268. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  269. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=8)
  270. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  271. def test_aes_128_cfb128(self):
  272. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  273. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  274. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  275. 'f69f2445df4f9b17ad2b417be66c3710'
  276. ciphertext = '3b3fd92eb72dad20333449f8e83cfb4a' +\
  277. 'c8a64537a0b3a93fcde3cdad9f1ce58b' +\
  278. '26751f67a3cbb140b1808cf187a4f4df' +\
  279. 'c04b05357c5d1c0eeac4c66f9ff7f2e6'
  280. key = '2b7e151628aed2a6abf7158809cf4f3c'
  281. iv = '000102030405060708090a0b0c0d0e0f'
  282. key = unhexlify(key)
  283. iv = unhexlify(iv)
  284. plaintext = unhexlify(plaintext)
  285. ciphertext = unhexlify(ciphertext)
  286. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  287. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  288. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  289. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  290. def test_aes_192_cfb128(self):
  291. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  292. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  293. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  294. 'f69f2445df4f9b17ad2b417be66c3710'
  295. ciphertext = 'cdc80d6fddf18cab34c25909c99a4174' +\
  296. '67ce7f7f81173621961a2b70171d3d7a' +\
  297. '2e1e8a1dd59b88b1c8e60fed1efac4c9' +\
  298. 'c05f9f9ca9834fa042ae8fba584b09ff'
  299. key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
  300. iv = '000102030405060708090a0b0c0d0e0f'
  301. key = unhexlify(key)
  302. iv = unhexlify(iv)
  303. plaintext = unhexlify(plaintext)
  304. ciphertext = unhexlify(ciphertext)
  305. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  306. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  307. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  308. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  309. def test_aes_256_cfb128(self):
  310. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  311. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  312. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  313. 'f69f2445df4f9b17ad2b417be66c3710'
  314. ciphertext = 'dc7e84bfda79164b7ecd8486985d3860' +\
  315. '39ffed143b28b1c832113c6331e5407b' +\
  316. 'df10132415e54b92a13ed0a8267ae2f9' +\
  317. '75a385741ab9cef82031623d55b1e471'
  318. key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
  319. iv = '000102030405060708090a0b0c0d0e0f'
  320. key = unhexlify(key)
  321. iv = unhexlify(iv)
  322. plaintext = unhexlify(plaintext)
  323. ciphertext = unhexlify(ciphertext)
  324. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  325. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  326. cipher = AES.new(key, AES.MODE_CFB, iv, segment_size=128)
  327. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  328. def get_tests(config={}):
  329. tests = []
  330. tests += list_test_cases(CfbTests)
  331. if config.get('slow_tests'):
  332. tests += list_test_cases(NistCfbVectors)
  333. tests += list_test_cases(SP800TestVectors)
  334. return tests
  335. if __name__ == '__main__':
  336. suite = lambda: unittest.TestSuite(get_tests())
  337. unittest.main(defaultTest='suite')