test_OFB.py 9.1 KB

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