test_SHAKE.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # ===================================================================
  2. #
  3. # Copyright (c) 2015, Legrandin <helderijs@gmail.com>
  4. # All rights reserved.
  5. #
  6. # Redistribution and use in source and binary forms, with or without
  7. # modification, are permitted provided that the following conditions
  8. # are met:
  9. #
  10. # 1. Redistributions of source code must retain the above copyright
  11. # notice, this list of conditions and the following disclaimer.
  12. # 2. Redistributions in binary form must reproduce the above copyright
  13. # notice, this list of conditions and the following disclaimer in
  14. # the documentation and/or other materials provided with the
  15. # distribution.
  16. #
  17. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  25. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  27. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. # POSSIBILITY OF SUCH DAMAGE.
  29. # ===================================================================
  30. """Self-test suite for Crypto.Hash.SHAKE128 and SHAKE256"""
  31. import unittest
  32. from binascii import hexlify, unhexlify
  33. from tls.Crypto.SelfTest.loader import load_tests
  34. from tls.Crypto.SelfTest.st_common import list_test_cases
  35. from tls.Crypto.Hash import SHAKE128, SHAKE256
  36. from tls.Crypto.Util.py3compat import b, bchr, bord, tobytes
  37. class SHAKETest(unittest.TestCase):
  38. def test_new_positive(self):
  39. xof1 = self.shake.new()
  40. xof2 = self.shake.new(data=b("90"))
  41. xof3 = self.shake.new().update(b("90"))
  42. self.assertNotEqual(xof1.read(10), xof2.read(10))
  43. xof3.read(10)
  44. self.assertEqual(xof2.read(10), xof3.read(10))
  45. def test_update(self):
  46. pieces = [bchr(10) * 200, bchr(20) * 300]
  47. h = self.shake.new()
  48. h.update(pieces[0]).update(pieces[1])
  49. digest = h.read(10)
  50. h = self.shake.new()
  51. h.update(pieces[0] + pieces[1])
  52. self.assertEqual(h.read(10), digest)
  53. def test_update_negative(self):
  54. h = self.shake.new()
  55. self.assertRaises(TypeError, h.update, u"string")
  56. def test_digest(self):
  57. h = self.shake.new()
  58. digest = h.read(90)
  59. # read returns a byte string of the right length
  60. self.failUnless(isinstance(digest, type(b("digest"))))
  61. self.assertEqual(len(digest), 90)
  62. def test_update_after_read(self):
  63. mac = self.shake.new()
  64. mac.update(b("rrrr"))
  65. mac.read(90)
  66. self.assertRaises(TypeError, mac.update, b("ttt"))
  67. class SHAKE128Test(SHAKETest):
  68. shake = SHAKE128
  69. class SHAKE256Test(SHAKETest):
  70. shake = SHAKE256
  71. class SHAKEVectors(unittest.TestCase):
  72. pass
  73. test_vectors_128 = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA3"),
  74. "ShortMsgKAT_SHAKE128.txt",
  75. "Short Messages KAT SHAKE128",
  76. { "len" : lambda x: int(x) } )
  77. for idx, tv in enumerate(test_vectors_128):
  78. if tv.len == 0:
  79. data = b("")
  80. else:
  81. data = tobytes(tv.msg)
  82. def new_test(self, data=data, result=tv.md):
  83. hobj = SHAKE128.new(data=data)
  84. digest = hobj.read(len(result))
  85. self.assertEqual(digest, result)
  86. setattr(SHAKEVectors, "test_128_%d" % idx, new_test)
  87. test_vectors_256 = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA3"),
  88. "ShortMsgKAT_SHAKE256.txt",
  89. "Short Messages KAT SHAKE256",
  90. { "len" : lambda x: int(x) } )
  91. for idx, tv in enumerate(test_vectors_256):
  92. if tv.len == 0:
  93. data = b("")
  94. else:
  95. data = tobytes(tv.msg)
  96. def new_test(self, data=data, result=tv.md):
  97. hobj = SHAKE256.new(data=data)
  98. digest = hobj.read(len(result))
  99. self.assertEqual(digest, result)
  100. setattr(SHAKEVectors, "test_256_%d" % idx, new_test)
  101. def get_tests(config={}):
  102. tests = []
  103. tests += list_test_cases(SHAKE128Test)
  104. tests += list_test_cases(SHAKE256Test)
  105. tests += list_test_cases(SHAKEVectors)
  106. return tests
  107. if __name__ == '__main__':
  108. import unittest
  109. suite = lambda: unittest.TestSuite(get_tests())
  110. unittest.main(defaultTest='suite')