test_OFB.py 9.1 KB

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