test_SHA512.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/test_SHA512.py: Self-test for the SHA-512 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.SHA512"""
  25. from binascii import hexlify
  26. from tls.Crypto.Hash import SHA512
  27. from .common import make_hash_tests
  28. from tls.Crypto.SelfTest.loader import load_tests
  29. # Test vectors from various sources
  30. # This is a list of (expected_result, input[, description]) tuples.
  31. test_data_512_other = [
  32. # RFC 4634: Section Page 8.4, "Test 1"
  33. ('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f', 'abc'),
  34. # RFC 4634: Section Page 8.4, "Test 2.1"
  35. ('8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909', 'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu'),
  36. # RFC 4634: Section Page 8.4, "Test 3"
  37. ('e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b', 'a' * 10**6, "'a' * 10**6"),
  38. # Taken from http://de.wikipedia.org/wiki/Secure_Hash_Algorithm
  39. ('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', ''),
  40. ('af9ed2de700433b803240a552b41b5a472a6ef3fe1431a722b2063c75e9f07451f67a28e37d09cde769424c96aea6f8971389db9e1993d6c565c3c71b855723c', 'Franz jagt im komplett verwahrlosten Taxi quer durch Bayern'),
  41. ]
  42. def get_tests_SHA512():
  43. test_vectors = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA2"),
  44. "SHA512ShortMsg.rsp",
  45. "KAT SHA-512",
  46. { "len" : lambda x: int(x) } )
  47. test_data = test_data_512_other[:]
  48. for tv in test_vectors:
  49. try:
  50. if tv.startswith('['):
  51. continue
  52. except AttributeError:
  53. pass
  54. if tv.len == 0:
  55. tv.msg = b""
  56. test_data.append((hexlify(tv.md), tv.msg, tv.desc))
  57. tests = make_hash_tests(SHA512, "SHA512", test_data,
  58. digest_size=64,
  59. oid="2.16.840.1.101.3.4.2.3")
  60. return tests
  61. def get_tests_SHA512_224():
  62. test_vectors = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA2"),
  63. "SHA512_224ShortMsg.rsp",
  64. "KAT SHA-512/224",
  65. { "len" : lambda x: int(x) } )
  66. test_data = []
  67. for tv in test_vectors:
  68. try:
  69. if tv.startswith('['):
  70. continue
  71. except AttributeError:
  72. pass
  73. if tv.len == 0:
  74. tv.msg = b""
  75. test_data.append((hexlify(tv.md), tv.msg, tv.desc))
  76. tests = make_hash_tests(SHA512, "SHA512/224", test_data,
  77. digest_size=28,
  78. oid="2.16.840.1.101.3.4.2.5",
  79. extra_params={ "truncate" : "224" })
  80. return tests
  81. def get_tests_SHA512_256():
  82. test_vectors = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA2"),
  83. "SHA512_256ShortMsg.rsp",
  84. "KAT SHA-512/256",
  85. { "len" : lambda x: int(x) } )
  86. test_data = []
  87. for tv in test_vectors:
  88. try:
  89. if tv.startswith('['):
  90. continue
  91. except AttributeError:
  92. pass
  93. if tv.len == 0:
  94. tv.msg = b""
  95. test_data.append((hexlify(tv.md), tv.msg, tv.desc))
  96. tests = make_hash_tests(SHA512, "SHA512/256", test_data,
  97. digest_size=32,
  98. oid="2.16.840.1.101.3.4.2.6",
  99. extra_params={ "truncate" : "256" })
  100. return tests
  101. def get_tests(config={}):
  102. tests = []
  103. tests += get_tests_SHA512()
  104. tests += get_tests_SHA512_224()
  105. tests += get_tests_SHA512_256()
  106. return tests
  107. if __name__ == '__main__':
  108. import unittest
  109. suite = lambda: unittest.TestSuite(get_tests())
  110. unittest.main(defaultTest='suite')
  111. # vim:set ts=4 sw=4 sts=4 expandtab: