test_SHAKE.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Crypto.SelfTest.loader import load_tests
  34. from Crypto.SelfTest.st_common import list_test_cases
  35. from StringIO import StringIO
  36. from Crypto.Hash import SHAKE128, SHAKE256
  37. from Crypto.Util.py3compat import b, bchr, bord, tobytes
  38. class SHAKETest(unittest.TestCase):
  39. def test_new_positive(self):
  40. xof1 = self.shake.new()
  41. xof2 = self.shake.new(data=b("90"))
  42. xof3 = self.shake.new().update(b("90"))
  43. self.assertNotEqual(xof1.read(10), xof2.read(10))
  44. xof3.read(10)
  45. self.assertEqual(xof2.read(10), xof3.read(10))
  46. def test_update(self):
  47. pieces = [bchr(10) * 200, bchr(20) * 300]
  48. h = self.shake.new()
  49. h.update(pieces[0]).update(pieces[1])
  50. digest = h.read(10)
  51. h = self.shake.new()
  52. h.update(pieces[0] + pieces[1])
  53. self.assertEqual(h.read(10), digest)
  54. def test_update_negative(self):
  55. h = self.shake.new()
  56. self.assertRaises(TypeError, h.update, u"string")
  57. def test_digest(self):
  58. h = self.shake.new()
  59. digest = h.read(90)
  60. # read returns a byte string of the right length
  61. self.failUnless(isinstance(digest, type(b("digest"))))
  62. self.assertEqual(len(digest), 90)
  63. def test_update_after_read(self):
  64. mac = self.shake.new()
  65. mac.update(b("rrrr"))
  66. mac.read(90)
  67. self.assertRaises(TypeError, mac.update, b("ttt"))
  68. class SHAKE128Test(SHAKETest):
  69. shake = SHAKE128
  70. class SHAKE256Test(SHAKETest):
  71. shake = SHAKE256
  72. class SHAKEVectors(unittest.TestCase):
  73. pass
  74. test_vectors_128 = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA3"),
  75. "ShortMsgKAT_SHAKE128.txt",
  76. "Short Messages KAT SHAKE128",
  77. { "len" : lambda x: int(x) } )
  78. for idx, tv in enumerate(test_vectors_128):
  79. if tv.len == 0:
  80. data = b("")
  81. else:
  82. data = tobytes(tv.msg)
  83. def new_test(self, data=data, result=tv.md):
  84. hobj = SHAKE128.new(data=data)
  85. digest = hobj.read(len(result))
  86. self.assertEqual(digest, result)
  87. setattr(SHAKEVectors, "test_128_%d" % idx, new_test)
  88. test_vectors_256 = load_tests(("Crypto", "SelfTest", "Hash", "test_vectors", "SHA3"),
  89. "ShortMsgKAT_SHAKE256.txt",
  90. "Short Messages KAT SHAKE256",
  91. { "len" : lambda x: int(x) } )
  92. for idx, tv in enumerate(test_vectors_256):
  93. if tv.len == 0:
  94. data = b("")
  95. else:
  96. data = tobytes(tv.msg)
  97. def new_test(self, data=data, result=tv.md):
  98. hobj = SHAKE256.new(data=data)
  99. digest = hobj.read(len(result))
  100. self.assertEqual(digest, result)
  101. setattr(SHAKEVectors, "test_256_%d" % idx, new_test)
  102. def get_tests(config={}):
  103. tests = []
  104. tests += list_test_cases(SHAKE128Test)
  105. tests += list_test_cases(SHAKE256Test)
  106. tests += list_test_cases(SHAKEVectors)
  107. return tests
  108. if __name__ == '__main__':
  109. import unittest
  110. suite = lambda: unittest.TestSuite(get_tests())
  111. unittest.main(defaultTest='suite')