Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_SHAKE.py

raw symbols SCIP
1 # ===================================================================
2 #
3 # Copyright (c) 2015, Legrandin <[email protected]>
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
31 """Self-test suite for Crypto.Hash.SHAKE128 and SHAKE256"""
32
33 import unittest
34 from binascii import hexlify, unhexlify
35
36 from Crypto.SelfTest.loader import load_test_vectors
37 from Crypto.SelfTest.st_common import list_test_cases
38
39 from Crypto.Hash import SHAKE128, SHAKE256
40 from Crypto.Util.py3compat import b, bchr, bord, tobytes
41
42 class SHAKETest(unittest.TestCase):
43
44 def test_new_positive(self):
45
46 xof1 = self.shake.new()
47 xof2 = self.shake.new(data=b("90"))
48 xof3 = self.shake.new().update(b("90"))
49
50 self.assertNotEqual(xof1.read(10), xof2.read(10))
51 xof3.read(10)
52 self.assertEqual(xof2.read(10), xof3.read(10))
53
54 def test_update(self):
55 pieces = [bchr(10) * 200, bchr(20) * 300]
56 h = self.shake.new()
57 h.update(pieces[0]).update(pieces[1])
58 digest = h.read(10)
59 h = self.shake.new()
60 h.update(pieces[0] + pieces[1])
61 self.assertEqual(h.read(10), digest)
62
63 def test_update_negative(self):
64 h = self.shake.new()
65 self.assertRaises(TypeError, h.update, u"string")
66
67 def test_digest(self):
68 h = self.shake.new()
69 digest = h.read(90)
70
71 # read returns a byte string of the right length
72 self.assertTrue(isinstance(digest, type(b("digest"))))
73 self.assertEqual(len(digest), 90)
74
75 def test_update_after_read(self):
76 mac = self.shake.new()
77 mac.update(b("rrrr"))
78 mac.read(90)
79 self.assertRaises(TypeError, mac.update, b("ttt"))
80
81
82 class SHAKE128Test(SHAKETest):
83 shake = SHAKE128
84
85
86 class SHAKE256Test(SHAKETest):
87 shake = SHAKE256
88
89
90 class SHAKEVectors(unittest.TestCase):
91 pass
92
93
94 test_vectors_128 = load_test_vectors(("Hash", "SHA3"),
95 "ShortMsgKAT_SHAKE128.txt",
96 "Short Messages KAT SHAKE128",
97 { "len" : lambda x: int(x) } ) or []
98
99 for idx, tv in enumerate(test_vectors_128):
100 if tv.len == 0:
101 data = b("")
102 else:
103 data = tobytes(tv.msg)
104
105 def new_test(self, data=data, result=tv.md):
106 hobj = SHAKE128.new(data=data)
107 digest = hobj.read(len(result))
108 self.assertEqual(digest, result)
109
110 setattr(SHAKEVectors, "test_128_%d" % idx, new_test)
111
112
113 test_vectors_256 = load_test_vectors(("Hash", "SHA3"),
114 "ShortMsgKAT_SHAKE256.txt",
115 "Short Messages KAT SHAKE256",
116 { "len" : lambda x: int(x) } ) or []
117
118 for idx, tv in enumerate(test_vectors_256):
119 if tv.len == 0:
120 data = b("")
121 else:
122 data = tobytes(tv.msg)
123
124 def new_test(self, data=data, result=tv.md):
125 hobj = SHAKE256.new(data=data)
126 digest = hobj.read(len(result))
127 self.assertEqual(digest, result)
128
129 setattr(SHAKEVectors, "test_256_%d" % idx, new_test)
130
131
132 def get_tests(config={}):
133 tests = []
134 tests += list_test_cases(SHAKE128Test)
135 tests += list_test_cases(SHAKE256Test)
136 tests += list_test_cases(SHAKEVectors)
137 return tests
138
139
140 if __name__ == '__main__':
141 import unittest
142 suite = lambda: unittest.TestSuite(get_tests())
143 unittest.main(defaultTest='suite')