test_cSHAKE.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # ===================================================================
  2. # Redistribution and use in source and binary forms, with or without
  3. # modification, are permitted provided that the following conditions
  4. # are met:
  5. #
  6. # 1. Redistributions of source code must retain the above copyright
  7. # notice, this list of conditions and the following disclaimer.
  8. # 2. Redistributions in binary form must reproduce the above copyright
  9. # notice, this list of conditions and the following disclaimer in
  10. # the documentation and/or other materials provided with the
  11. # distribution.
  12. #
  13. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  14. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  15. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  16. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  17. # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  19. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  23. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. # POSSIBILITY OF SUCH DAMAGE.
  25. # ===================================================================
  26. """Self-test suite for Cryptodome.Hash.cSHAKE128 and cSHAKE256"""
  27. import unittest
  28. from Cryptodome.SelfTest.loader import load_test_vectors
  29. from Cryptodome.SelfTest.st_common import list_test_cases
  30. from Cryptodome.Hash import cSHAKE128, cSHAKE256, SHAKE128, SHAKE256
  31. from Cryptodome.Util.py3compat import b, bchr, tobytes
  32. class cSHAKETest(unittest.TestCase):
  33. def test_left_encode(self):
  34. from Cryptodome.Hash.cSHAKE128 import _left_encode
  35. self.assertEqual(_left_encode(0), b'\x01\x00')
  36. self.assertEqual(_left_encode(1), b'\x01\x01')
  37. self.assertEqual(_left_encode(256), b'\x02\x00\x01')
  38. def test_bytepad(self):
  39. from Cryptodome.Hash.cSHAKE128 import _bytepad
  40. self.assertEqual(_bytepad(b'', 4), b'\x01\x04\x00\x00')
  41. self.assertEqual(_bytepad(b'A', 4), b'\x01\x04A\x00')
  42. self.assertEqual(_bytepad(b'AA', 4), b'\x01\x04AA')
  43. self.assertEqual(_bytepad(b'AAA', 4), b'\x01\x04AAA\x00\x00\x00')
  44. self.assertEqual(_bytepad(b'AAAA', 4), b'\x01\x04AAAA\x00\x00')
  45. self.assertEqual(_bytepad(b'AAAAA', 4), b'\x01\x04AAAAA\x00')
  46. self.assertEqual(_bytepad(b'AAAAAA', 4), b'\x01\x04AAAAAA')
  47. self.assertEqual(_bytepad(b'AAAAAAA', 4), b'\x01\x04AAAAAAA\x00\x00\x00')
  48. def test_new_positive(self):
  49. xof1 = self.cshake.new()
  50. xof2 = self.cshake.new(data=b("90"))
  51. xof3 = self.cshake.new().update(b("90"))
  52. self.assertNotEqual(xof1.read(10), xof2.read(10))
  53. xof3.read(10)
  54. self.assertEqual(xof2.read(10), xof3.read(10))
  55. xof1 = self.cshake.new()
  56. ref = xof1.read(10)
  57. xof2 = self.cshake.new(custom=b(""))
  58. xof3 = self.cshake.new(custom=b("foo"))
  59. self.assertEqual(ref, xof2.read(10))
  60. self.assertNotEqual(ref, xof3.read(10))
  61. xof1 = self.cshake.new(custom=b("foo"))
  62. xof2 = self.cshake.new(custom=b("foo"), data=b("90"))
  63. xof3 = self.cshake.new(custom=b("foo")).update(b("90"))
  64. self.assertNotEqual(xof1.read(10), xof2.read(10))
  65. xof3.read(10)
  66. self.assertEqual(xof2.read(10), xof3.read(10))
  67. def test_update(self):
  68. pieces = [bchr(10) * 200, bchr(20) * 300]
  69. h = self.cshake.new()
  70. h.update(pieces[0]).update(pieces[1])
  71. digest = h.read(10)
  72. h = self.cshake.new()
  73. h.update(pieces[0] + pieces[1])
  74. self.assertEqual(h.read(10), digest)
  75. def test_update_negative(self):
  76. h = self.cshake.new()
  77. self.assertRaises(TypeError, h.update, u"string")
  78. def test_digest(self):
  79. h = self.cshake.new()
  80. digest = h.read(90)
  81. # read returns a byte string of the right length
  82. self.failUnless(isinstance(digest, type(b("digest"))))
  83. self.assertEqual(len(digest), 90)
  84. def test_update_after_read(self):
  85. mac = self.cshake.new()
  86. mac.update(b("rrrr"))
  87. mac.read(90)
  88. self.assertRaises(TypeError, mac.update, b("ttt"))
  89. def test_shake(self):
  90. # When no customization string is passed, results must match SHAKE
  91. for digest_len in range(64):
  92. xof1 = self.cshake.new(b'TEST')
  93. xof2 = self.shake.new(b'TEST')
  94. self.assertEqual(xof1.read(digest_len), xof2.read(digest_len))
  95. class cSHAKE128Test(cSHAKETest):
  96. cshake = cSHAKE128
  97. shake = SHAKE128
  98. class cSHAKE256Test(cSHAKETest):
  99. cshake = cSHAKE256
  100. shake = SHAKE256
  101. class cSHAKEVectors(unittest.TestCase):
  102. pass
  103. vector_files = [("ShortMsgSamples_cSHAKE128.txt", "Short Message Samples cSHAKE128", "128_cshake", cSHAKE128),
  104. ("ShortMsgSamples_cSHAKE256.txt", "Short Message Samples cSHAKE256", "256_cshake", cSHAKE256)]
  105. for file, descr, tag, test_class in vector_files:
  106. test_vectors = load_test_vectors(("Hash", "SHA3"), file, descr,
  107. {"len": lambda x: int(x),
  108. "nlen": lambda x: int(x),
  109. "slen": lambda x: int(x)}) or []
  110. for idx, tv in enumerate(test_vectors):
  111. if getattr(tv, "len", 0) == 0:
  112. data = b("")
  113. else:
  114. data = tobytes(tv.msg)
  115. assert(tv.len == len(tv.msg)*8)
  116. if getattr(tv, "nlen", 0) != 0:
  117. raise ValueError("Unsupported cSHAKE test vector")
  118. if getattr(tv, "slen", 0) == 0:
  119. custom = b("")
  120. else:
  121. custom = tobytes(tv.s)
  122. assert(tv.slen == len(tv.s)*8)
  123. def new_test(self, data=data, result=tv.md, custom=custom, test_class=test_class):
  124. hobj = test_class.new(data=data, custom=custom)
  125. digest = hobj.read(len(result))
  126. self.assertEqual(digest, result)
  127. setattr(cSHAKEVectors, "test_%s_%d" % (tag, idx), new_test)
  128. def get_tests(config={}):
  129. tests = []
  130. tests += list_test_cases(cSHAKE128Test)
  131. tests += list_test_cases(cSHAKE256Test)
  132. tests += list_test_cases(cSHAKEVectors)
  133. return tests
  134. if __name__ == '__main__':
  135. import unittest
  136. suite = lambda: unittest.TestSuite(get_tests())
  137. unittest.main(defaultTest='suite')