test_SHA3_384.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # -*- coding: utf-8 -*-
  2. #
  3. # SelfTest/Hash/test_SHA3_384.py: Self-test for the SHA-3/384 hash function
  4. #
  5. # ===================================================================
  6. # The contents of this file are dedicated to the public domain. To
  7. # the extent that dedication to the public domain is not available,
  8. # everyone is granted a worldwide, perpetual, royalty-free,
  9. # non-exclusive license to exercise all rights associated with the
  10. # contents of this file for any purpose whatsoever.
  11. # No rights are reserved.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  17. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  18. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. # ===================================================================
  22. """Self-test suite for Cryptodome.Hash.SHA3_384"""
  23. import unittest
  24. from binascii import hexlify
  25. from Cryptodome.SelfTest.loader import load_test_vectors
  26. from Cryptodome.SelfTest.st_common import list_test_cases
  27. from Cryptodome.Hash import SHA3_384 as SHA3
  28. from Cryptodome.Util.py3compat import b
  29. class APITest(unittest.TestCase):
  30. def test_update_after_digest(self):
  31. msg=b("rrrrttt")
  32. # Normally, update() cannot be done after digest()
  33. h = SHA3.new(data=msg[:4])
  34. dig1 = h.digest()
  35. self.assertRaises(TypeError, h.update, msg[4:])
  36. dig2 = SHA3.new(data=msg).digest()
  37. # With the proper flag, it is allowed
  38. h = SHA3.new(data=msg[:4], update_after_digest=True)
  39. self.assertEquals(h.digest(), dig1)
  40. # ... and the subsequent digest applies to the entire message
  41. # up to that point
  42. h.update(msg[4:])
  43. self.assertEquals(h.digest(), dig2)
  44. def get_tests(config={}):
  45. from .common import make_hash_tests
  46. tests = []
  47. test_vectors = load_test_vectors(("Hash", "SHA3"),
  48. "ShortMsgKAT_SHA3-384.txt",
  49. "KAT SHA-3 384",
  50. { "len" : lambda x: int(x) } ) or []
  51. test_data = []
  52. for tv in test_vectors:
  53. if tv.len == 0:
  54. tv.msg = b("")
  55. test_data.append((hexlify(tv.md), tv.msg, tv.desc))
  56. tests += make_hash_tests(SHA3, "SHA3_384", test_data,
  57. digest_size=SHA3.digest_size,
  58. oid="2.16.840.1.101.3.4.2.9")
  59. tests += list_test_cases(APITest)
  60. return tests
  61. if __name__ == '__main__':
  62. import unittest
  63. suite = lambda: unittest.TestSuite(get_tests())
  64. unittest.main(defaultTest='suite')