test_SHA256.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/test_SHA256.py: Self-test for the SHA-256 hash function
  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.Hash.SHA256"""
  25. import unittest
  26. from tls.Crypto.Util.py3compat import *
  27. class LargeSHA256Test(unittest.TestCase):
  28. def runTest(self):
  29. """SHA256: 512/520 MiB test"""
  30. from tls.Crypto.Hash import SHA256
  31. zeros = bchr(0x00) * (1024*1024)
  32. h = SHA256.new(zeros)
  33. for i in range(511):
  34. h.update(zeros)
  35. # This test vector is from PyCrypto's old testdata.py file.
  36. self.assertEqual('9acca8e8c22201155389f65abbf6bc9723edc7384ead80503839f49dcc56d767', h.hexdigest()) # 512 MiB
  37. for i in range(8):
  38. h.update(zeros)
  39. # This test vector is from PyCrypto's old testdata.py file.
  40. self.assertEqual('abf51ad954b246009dfe5a50ecd582fd5b8f1b8b27f30393853c3ef721e7fa6e', h.hexdigest()) # 520 MiB
  41. def get_tests(config={}):
  42. # Test vectors from FIPS PUB 180-2
  43. # This is a list of (expected_result, input[, description]) tuples.
  44. test_data = [
  45. # FIPS PUB 180-2, B.1 - "One-Block Message"
  46. ('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
  47. 'abc'),
  48. # FIPS PUB 180-2, B.2 - "Multi-Block Message"
  49. ('248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1',
  50. 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'),
  51. # FIPS PUB 180-2, B.3 - "Long Message"
  52. ('cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0',
  53. 'a' * 10**6,
  54. '"a" * 10**6'),
  55. # Test for an old PyCrypto bug.
  56. ('f7fd017a3c721ce7ff03f3552c0813adcc48b7f33f07e5e2ba71e23ea393d103',
  57. 'This message is precisely 55 bytes long, to test a bug.',
  58. 'Length = 55 (mod 64)'),
  59. # Example from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
  60. ('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', ''),
  61. ('d32b568cd1b96d459e7291ebf4b25d007f275c9f13149beeb782fac0716613f8',
  62. 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
  63. ]
  64. from tls.Crypto.Hash import SHA256
  65. from .common import make_hash_tests
  66. tests = make_hash_tests(SHA256, "SHA256", test_data,
  67. digest_size=32,
  68. oid="2.16.840.1.101.3.4.2.1")
  69. if config.get('slow_tests'):
  70. tests += [LargeSHA256Test()]
  71. return tests
  72. if __name__ == '__main__':
  73. import unittest
  74. suite = lambda: unittest.TestSuite(get_tests())
  75. unittest.main(defaultTest='suite')
  76. # vim:set ts=4 sw=4 sts=4 expandtab: