test_DES3.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Cipher/DES3.py: Self-test for the Triple-DES cipher
  4. #
  5. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  6. #
  7. # ===================================================================
  8. # The contents of this file are dedicated to the public domain. To
  9. # the extent that dedication to the public domain is not available,
  10. # everyone is granted a worldwide, perpetual, royalty-free,
  11. # non-exclusive license to exercise all rights associated with the
  12. # contents of this file for any purpose whatsoever.
  13. # No rights are reserved.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. # SOFTWARE.
  23. # ===================================================================
  24. """Self-test suite for Crypto.Cipher.DES3"""
  25. import unittest
  26. from binascii import hexlify, unhexlify
  27. from tls.Crypto.Cipher import DES3
  28. from tls.Crypto.Util.strxor import strxor_c
  29. from tls.Crypto.Util.py3compat import bchr, tostr
  30. from tls.Crypto.SelfTest.loader import load_tests
  31. from tls.Crypto.SelfTest.st_common import list_test_cases
  32. # This is a list of (plaintext, ciphertext, key, description) tuples.
  33. test_data = [
  34. # Test vector from Appendix B of NIST SP 800-67
  35. # "Recommendation for the Triple Data Encryption Algorithm (TDEA) Block
  36. # Cipher"
  37. # http://csrc.nist.gov/publications/nistpubs/800-67/SP800-67.pdf
  38. ('54686520717566636b2062726f776e20666f78206a756d70',
  39. 'a826fd8ce53b855fcce21c8112256fe668d5c05dd9b6b900',
  40. '0123456789abcdef23456789abcdef01456789abcdef0123',
  41. 'NIST SP800-67 B.1'),
  42. # This test is designed to test the DES3 API, not the correctness of the
  43. # output.
  44. ('21e81b7ade88a259', '5c577d4d9b20c0f8',
  45. '9b397ebf81b1181e282f4bb8adbadc6b', 'Two-key 3DES'),
  46. ]
  47. # NIST CAVP test vectors
  48. nist_tdes_mmt_files = ("TECBMMT2.rsp", "TECBMMT3.rsp")
  49. for tdes_file in nist_tdes_mmt_files:
  50. test_vectors = load_tests(("Crypto", "SelfTest", "Cipher", "test_vectors", "TDES"),
  51. tdes_file,
  52. "TDES ECB (%s)" % tdes_file,
  53. { "count" : lambda x: int(x) } )
  54. assert(test_vectors)
  55. for index, tv in enumerate(test_vectors):
  56. # The test vector file contains some directive lines
  57. if isinstance(tv, str):
  58. continue
  59. key = tv.key1 + tv.key2 + tv.key3
  60. test_data_item = (tostr(hexlify(tv.plaintext)),
  61. tostr(hexlify(tv.ciphertext)),
  62. tostr(hexlify(key)),
  63. "%s (%s)" % (tdes_file, index))
  64. test_data.append(test_data_item)
  65. class CheckParity(unittest.TestCase):
  66. def test_parity_option2(self):
  67. before_2k = unhexlify("CABF326FA56734324FFCCABCDEFACABF")
  68. after_2k = DES3.adjust_key_parity(before_2k)
  69. self.assertEqual(after_2k,
  70. unhexlify("CBBF326EA46734324FFDCBBCDFFBCBBF"))
  71. def test_parity_option3(self):
  72. before_3k = unhexlify("AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCC")
  73. after_3k = DES3.adjust_key_parity(before_3k)
  74. self.assertEqual(after_3k,
  75. unhexlify("ABABABABABABABABBABABABABABABABACDCDCDCDCDCDCDCD"))
  76. def test_degradation(self):
  77. sub_key1 = bchr(1) * 8
  78. sub_key2 = bchr(255) * 8
  79. # K1 == K2
  80. self.assertRaises(ValueError, DES3.adjust_key_parity,
  81. sub_key1 * 2 + sub_key2)
  82. # K2 == K3
  83. self.assertRaises(ValueError, DES3.adjust_key_parity,
  84. sub_key1 + sub_key2 * 2)
  85. # K1 == K2 == K3
  86. self.assertRaises(ValueError, DES3.adjust_key_parity,
  87. sub_key1 * 3)
  88. # K1 == K2 (with different parity)
  89. self.assertRaises(ValueError, DES3.adjust_key_parity,
  90. sub_key1 + strxor_c(sub_key1, 1) + sub_key2)
  91. class DegenerateToDESTest(unittest.TestCase):
  92. def runTest(self):
  93. sub_key1 = bchr(1) * 8
  94. sub_key2 = bchr(255) * 8
  95. # K1 == K2
  96. self.assertRaises(ValueError, DES3.new,
  97. sub_key1 * 2 + sub_key2,
  98. DES3.MODE_ECB)
  99. # K2 == K3
  100. self.assertRaises(ValueError, DES3.new,
  101. sub_key1 + sub_key2 * 2,
  102. DES3.MODE_ECB)
  103. # K1 == K2 == K3
  104. self.assertRaises(ValueError, DES3.new,
  105. sub_key1 *3,
  106. DES3.MODE_ECB)
  107. # K2 == K3 (parity is ignored)
  108. self.assertRaises(ValueError, DES3.new,
  109. sub_key1 + sub_key2 + strxor_c(sub_key2, 0x1),
  110. DES3.MODE_ECB)
  111. class TestOutput(unittest.TestCase):
  112. def runTest(self):
  113. # Encrypt/Decrypt data and test output parameter
  114. cipher = DES3.new(b'4'*8 + b'G'*8 + b'T'*8, DES3.MODE_ECB)
  115. pt = b'5' * 16
  116. ct = cipher.encrypt(pt)
  117. output = bytearray(16)
  118. res = cipher.encrypt(pt, output=output)
  119. self.assertEqual(ct, output)
  120. self.assertEqual(res, None)
  121. res = cipher.decrypt(ct, output=output)
  122. self.assertEqual(pt, output)
  123. self.assertEqual(res, None)
  124. import sys
  125. if sys.version[:3] != '2.6':
  126. output = memoryview(bytearray(16))
  127. cipher.encrypt(pt, output=output)
  128. self.assertEqual(ct, output)
  129. cipher.decrypt(ct, output=output)
  130. self.assertEqual(pt, output)
  131. self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
  132. self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)
  133. shorter_output = bytearray(7)
  134. self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
  135. self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
  136. def get_tests(config={}):
  137. from .common import make_block_tests
  138. tests = []
  139. tests = make_block_tests(DES3, "DES3", test_data)
  140. tests.append(DegenerateToDESTest())
  141. tests += list_test_cases(CheckParity)
  142. tests += [TestOutput()]
  143. return tests
  144. if __name__ == '__main__':
  145. import unittest
  146. suite = lambda: unittest.TestSuite(get_tests())
  147. unittest.main(defaultTest='suite')
  148. # vim:set ts=4 sw=4 sts=4 expandtab: