PageRenderTime 2810ms CodeModel.GetById 45ms RepoModel.GetById 1ms app.codeStats 0ms

/Windows/Python3.8/WPy64-3830/WPy64-3830/python-3.8.3.amd64/Lib/site-packages/Crypto/SelfTest/Cipher/test_Blowfish.py

https://gitlab.com/abhi1tb/build
Python | 162 lines | 109 code | 23 blank | 30 comment | 2 complexity | e68c5a7157e6558fc0660387aecb20c4 MD5 | raw file
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Cipher/test_Blowfish.py: Self-test for the Blowfish 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.Blowfish"""
  25. import unittest
  26. from Crypto.Util.py3compat import bchr
  27. from Crypto.Cipher import Blowfish
  28. # This is a list of (plaintext, ciphertext, key) tuples.
  29. test_data = [
  30. # Test vectors from http://www.schneier.com/code/vectors.txt
  31. ('0000000000000000', '4ef997456198dd78', '0000000000000000'),
  32. ('ffffffffffffffff', '51866fd5b85ecb8a', 'ffffffffffffffff'),
  33. ('1000000000000001', '7d856f9a613063f2', '3000000000000000'),
  34. ('1111111111111111', '2466dd878b963c9d', '1111111111111111'),
  35. ('1111111111111111', '61f9c3802281b096', '0123456789abcdef'),
  36. ('0123456789abcdef', '7d0cc630afda1ec7', '1111111111111111'),
  37. ('0000000000000000', '4ef997456198dd78', '0000000000000000'),
  38. ('0123456789abcdef', '0aceab0fc6a0a28d', 'fedcba9876543210'),
  39. ('01a1d6d039776742', '59c68245eb05282b', '7ca110454a1a6e57'),
  40. ('5cd54ca83def57da', 'b1b8cc0b250f09a0', '0131d9619dc1376e'),
  41. ('0248d43806f67172', '1730e5778bea1da4', '07a1133e4a0b2686'),
  42. ('51454b582ddf440a', 'a25e7856cf2651eb', '3849674c2602319e'),
  43. ('42fd443059577fa2', '353882b109ce8f1a', '04b915ba43feb5b6'),
  44. ('059b5e0851cf143a', '48f4d0884c379918', '0113b970fd34f2ce'),
  45. ('0756d8e0774761d2', '432193b78951fc98', '0170f175468fb5e6'),
  46. ('762514b829bf486a', '13f04154d69d1ae5', '43297fad38e373fe'),
  47. ('3bdd119049372802', '2eedda93ffd39c79', '07a7137045da2a16'),
  48. ('26955f6835af609a', 'd887e0393c2da6e3', '04689104c2fd3b2f'),
  49. ('164d5e404f275232', '5f99d04f5b163969', '37d06bb516cb7546'),
  50. ('6b056e18759f5cca', '4a057a3b24d3977b', '1f08260d1ac2465e'),
  51. ('004bd6ef09176062', '452031c1e4fada8e', '584023641aba6176'),
  52. ('480d39006ee762f2', '7555ae39f59b87bd', '025816164629b007'),
  53. ('437540c8698f3cfa', '53c55f9cb49fc019', '49793ebc79b3258f'),
  54. ('072d43a077075292', '7a8e7bfa937e89a3', '4fb05e1515ab73a7'),
  55. ('02fe55778117f12a', 'cf9c5d7a4986adb5', '49e95d6d4ca229bf'),
  56. ('1d9d5c5018f728c2', 'd1abb290658bc778', '018310dc409b26d6'),
  57. ('305532286d6f295a', '55cb3774d13ef201', '1c587f1c13924fef'),
  58. ('0123456789abcdef', 'fa34ec4847b268b2', '0101010101010101'),
  59. ('0123456789abcdef', 'a790795108ea3cae', '1f1f1f1f0e0e0e0e'),
  60. ('0123456789abcdef', 'c39e072d9fac631d', 'e0fee0fef1fef1fe'),
  61. ('ffffffffffffffff', '014933e0cdaff6e4', '0000000000000000'),
  62. ('0000000000000000', 'f21e9a77b71c49bc', 'ffffffffffffffff'),
  63. ('0000000000000000', '245946885754369a', '0123456789abcdef'),
  64. ('ffffffffffffffff', '6b5c5a9c5d9e0a5a', 'fedcba9876543210'),
  65. #('fedcba9876543210', 'f9ad597c49db005e', 'f0'),
  66. #('fedcba9876543210', 'e91d21c1d961a6d6', 'f0e1'),
  67. #('fedcba9876543210', 'e9c2b70a1bc65cf3', 'f0e1d2'),
  68. ('fedcba9876543210', 'be1e639408640f05', 'f0e1d2c3'),
  69. ('fedcba9876543210', 'b39e44481bdb1e6e', 'f0e1d2c3b4'),
  70. ('fedcba9876543210', '9457aa83b1928c0d', 'f0e1d2c3b4a5'),
  71. ('fedcba9876543210', '8bb77032f960629d', 'f0e1d2c3b4a596'),
  72. ('fedcba9876543210', 'e87a244e2cc85e82', 'f0e1d2c3b4a59687'),
  73. ('fedcba9876543210', '15750e7a4f4ec577', 'f0e1d2c3b4a5968778'),
  74. ('fedcba9876543210', '122ba70b3ab64ae0', 'f0e1d2c3b4a596877869'),
  75. ('fedcba9876543210', '3a833c9affc537f6', 'f0e1d2c3b4a5968778695a'),
  76. ('fedcba9876543210', '9409da87a90f6bf2', 'f0e1d2c3b4a5968778695a4b'),
  77. ('fedcba9876543210', '884f80625060b8b4', 'f0e1d2c3b4a5968778695a4b3c'),
  78. ('fedcba9876543210', '1f85031c19e11968', 'f0e1d2c3b4a5968778695a4b3c2d'),
  79. ('fedcba9876543210', '79d9373a714ca34f', 'f0e1d2c3b4a5968778695a4b3c2d1e'),
  80. ('fedcba9876543210', '93142887ee3be15c',
  81. 'f0e1d2c3b4a5968778695a4b3c2d1e0f'),
  82. ('fedcba9876543210', '03429e838ce2d14b',
  83. 'f0e1d2c3b4a5968778695a4b3c2d1e0f00'),
  84. ('fedcba9876543210', 'a4299e27469ff67b',
  85. 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011'),
  86. ('fedcba9876543210', 'afd5aed1c1bc96a8',
  87. 'f0e1d2c3b4a5968778695a4b3c2d1e0f001122'),
  88. ('fedcba9876543210', '10851c0e3858da9f',
  89. 'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233'),
  90. ('fedcba9876543210', 'e6f51ed79b9db21f',
  91. 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344'),
  92. ('fedcba9876543210', '64a6e14afd36b46f',
  93. 'f0e1d2c3b4a5968778695a4b3c2d1e0f001122334455'),
  94. ('fedcba9876543210', '80c7d7d45a5479ad',
  95. 'f0e1d2c3b4a5968778695a4b3c2d1e0f00112233445566'),
  96. ('fedcba9876543210', '05044b62fa52d080',
  97. 'f0e1d2c3b4a5968778695a4b3c2d1e0f0011223344556677'),
  98. ]
  99. class KeyLength(unittest.TestCase):
  100. def runTest(self):
  101. self.assertRaises(ValueError, Blowfish.new, bchr(0) * 3,
  102. Blowfish.MODE_ECB)
  103. self.assertRaises(ValueError, Blowfish.new, bchr(0) * 57,
  104. Blowfish.MODE_ECB)
  105. class TestOutput(unittest.TestCase):
  106. def runTest(self):
  107. # Encrypt/Decrypt data and test output parameter
  108. cipher = Blowfish.new(b'4'*16, Blowfish.MODE_ECB)
  109. pt = b'5' * 16
  110. ct = cipher.encrypt(pt)
  111. output = bytearray(16)
  112. res = cipher.encrypt(pt, output=output)
  113. self.assertEqual(ct, output)
  114. self.assertEqual(res, None)
  115. res = cipher.decrypt(ct, output=output)
  116. self.assertEqual(pt, output)
  117. self.assertEqual(res, None)
  118. import sys
  119. if sys.version[:3] != '2.6':
  120. output = memoryview(bytearray(16))
  121. cipher.encrypt(pt, output=output)
  122. self.assertEqual(ct, output)
  123. cipher.decrypt(ct, output=output)
  124. self.assertEqual(pt, output)
  125. self.assertRaises(TypeError, cipher.encrypt, pt, output=b'0'*16)
  126. self.assertRaises(TypeError, cipher.decrypt, ct, output=b'0'*16)
  127. shorter_output = bytearray(7)
  128. self.assertRaises(ValueError, cipher.encrypt, pt, output=shorter_output)
  129. self.assertRaises(ValueError, cipher.decrypt, ct, output=shorter_output)
  130. def get_tests(config={}):
  131. from .common import make_block_tests
  132. tests = make_block_tests(Blowfish, "Blowfish", test_data)
  133. tests.append(KeyLength())
  134. tests += [TestOutput()]
  135. return tests
  136. if __name__ == '__main__':
  137. import unittest
  138. suite = lambda: unittest.TestSuite(get_tests())
  139. unittest.main(defaultTest='suite')