test_OFB.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 binascii import unhexlify
  32. from Crypto.SelfTest.st_common import list_test_cases
  33. from Crypto.Util.py3compat import tobytes
  34. from Crypto.Cipher import AES, DES3, DES
  35. from Crypto.Hash import SHAKE128
  36. from Crypto.SelfTest.loader import load_test_vectors_wycheproof
  37. def get_tag_random(tag, length):
  38. return SHAKE128.new(data=tobytes(tag)).read(length)
  39. from Crypto.SelfTest.Cipher.test_CBC import BlockChainingTests
  40. class OfbTests(BlockChainingTests):
  41. aes_mode = AES.MODE_OFB
  42. des3_mode = DES3.MODE_OFB
  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. from Crypto.SelfTest.Cipher.test_CBC import NistBlockChainingVectors
  65. class NistOfbVectors(NistBlockChainingVectors):
  66. aes_mode = AES.MODE_OFB
  67. des_mode = DES.MODE_OFB
  68. des3_mode = DES3.MODE_OFB
  69. # Create one test method per file
  70. nist_aes_kat_mmt_files = (
  71. # KAT
  72. "OFBGFSbox128.rsp",
  73. "OFBGFSbox192.rsp",
  74. "OFBGFSbox256.rsp",
  75. "OFBKeySbox128.rsp",
  76. "OFBKeySbox192.rsp",
  77. "OFBKeySbox256.rsp",
  78. "OFBVarKey128.rsp",
  79. "OFBVarKey192.rsp",
  80. "OFBVarKey256.rsp",
  81. "OFBVarTxt128.rsp",
  82. "OFBVarTxt192.rsp",
  83. "OFBVarTxt256.rsp",
  84. # MMT
  85. "OFBMMT128.rsp",
  86. "OFBMMT192.rsp",
  87. "OFBMMT256.rsp",
  88. )
  89. nist_aes_mct_files = (
  90. "OFBMCT128.rsp",
  91. "OFBMCT192.rsp",
  92. "OFBMCT256.rsp",
  93. )
  94. for file_name in nist_aes_kat_mmt_files:
  95. def new_func(self, file_name=file_name):
  96. self._do_kat_aes_test(file_name)
  97. setattr(NistOfbVectors, "test_AES_" + file_name, new_func)
  98. for file_name in nist_aes_mct_files:
  99. def new_func(self, file_name=file_name):
  100. self._do_mct_aes_test(file_name)
  101. setattr(NistOfbVectors, "test_AES_" + file_name, new_func)
  102. del file_name, new_func
  103. nist_tdes_files = (
  104. "TOFBMMT2.rsp", # 2TDES
  105. "TOFBMMT3.rsp", # 3TDES
  106. "TOFBinvperm.rsp", # Single DES
  107. "TOFBpermop.rsp",
  108. "TOFBsubtab.rsp",
  109. "TOFBvarkey.rsp",
  110. "TOFBvartext.rsp",
  111. )
  112. for file_name in nist_tdes_files:
  113. def new_func(self, file_name=file_name):
  114. self._do_tdes_test(file_name)
  115. setattr(NistOfbVectors, "test_TDES_" + file_name, new_func)
  116. # END OF NIST OFB TEST VECTORS
  117. class SP800TestVectors(unittest.TestCase):
  118. """Class exercising the OFB test vectors found in Section F.4
  119. of NIST SP 800-3A"""
  120. def test_aes_128(self):
  121. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  122. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  123. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  124. 'f69f2445df4f9b17ad2b417be66c3710'
  125. ciphertext = '3b3fd92eb72dad20333449f8e83cfb4a' +\
  126. '7789508d16918f03f53c52dac54ed825' +\
  127. '9740051e9c5fecf64344f7a82260edcc' +\
  128. '304c6528f659c77866a510d9c1d6ae5e'
  129. key = '2b7e151628aed2a6abf7158809cf4f3c'
  130. iv = '000102030405060708090a0b0c0d0e0f'
  131. key = unhexlify(key)
  132. iv = unhexlify(iv)
  133. plaintext = unhexlify(plaintext)
  134. ciphertext = unhexlify(ciphertext)
  135. cipher = AES.new(key, AES.MODE_OFB, iv)
  136. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  137. cipher = AES.new(key, AES.MODE_OFB, iv)
  138. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  139. cipher = AES.new(key, AES.MODE_OFB, iv)
  140. self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8])
  141. cipher = AES.new(key, AES.MODE_OFB, iv)
  142. self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8])
  143. def test_aes_192(self):
  144. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  145. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  146. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  147. 'f69f2445df4f9b17ad2b417be66c3710'
  148. ciphertext = 'cdc80d6fddf18cab34c25909c99a4174' +\
  149. 'fcc28b8d4c63837c09e81700c1100401' +\
  150. '8d9a9aeac0f6596f559c6d4daf59a5f2' +\
  151. '6d9f200857ca6c3e9cac524bd9acc92a'
  152. key = '8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b'
  153. iv = '000102030405060708090a0b0c0d0e0f'
  154. key = unhexlify(key)
  155. iv = unhexlify(iv)
  156. plaintext = unhexlify(plaintext)
  157. ciphertext = unhexlify(ciphertext)
  158. cipher = AES.new(key, AES.MODE_OFB, iv)
  159. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  160. cipher = AES.new(key, AES.MODE_OFB, iv)
  161. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  162. cipher = AES.new(key, AES.MODE_OFB, iv)
  163. self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8])
  164. cipher = AES.new(key, AES.MODE_OFB, iv)
  165. self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8])
  166. def test_aes_256(self):
  167. plaintext = '6bc1bee22e409f96e93d7e117393172a' +\
  168. 'ae2d8a571e03ac9c9eb76fac45af8e51' +\
  169. '30c81c46a35ce411e5fbc1191a0a52ef' +\
  170. 'f69f2445df4f9b17ad2b417be66c3710'
  171. ciphertext = 'dc7e84bfda79164b7ecd8486985d3860' +\
  172. '4febdc6740d20b3ac88f6ad82a4fb08d' +\
  173. '71ab47a086e86eedf39d1c5bba97c408' +\
  174. '0126141d67f37be8538f5a8be740e484'
  175. key = '603deb1015ca71be2b73aef0857d77811f352c073b6108d72d9810a30914dff4'
  176. iv = '000102030405060708090a0b0c0d0e0f'
  177. key = unhexlify(key)
  178. iv = unhexlify(iv)
  179. plaintext = unhexlify(plaintext)
  180. ciphertext = unhexlify(ciphertext)
  181. cipher = AES.new(key, AES.MODE_OFB, iv)
  182. self.assertEqual(cipher.encrypt(plaintext), ciphertext)
  183. cipher = AES.new(key, AES.MODE_OFB, iv)
  184. self.assertEqual(cipher.decrypt(ciphertext), plaintext)
  185. cipher = AES.new(key, AES.MODE_OFB, iv)
  186. self.assertEqual(cipher.encrypt(plaintext[:-8]), ciphertext[:-8])
  187. cipher = AES.new(key, AES.MODE_OFB, iv)
  188. self.assertEqual(cipher.decrypt(ciphertext[:-8]), plaintext[:-8])
  189. def get_tests(config={}):
  190. tests = []
  191. tests += list_test_cases(OfbTests)
  192. if config.get('slow_tests'):
  193. tests += list_test_cases(NistOfbVectors)
  194. tests += list_test_cases(SP800TestVectors)
  195. return tests
  196. if __name__ == '__main__':
  197. suite = lambda: unittest.TestSuite(get_tests())
  198. unittest.main(defaultTest='suite')