Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_keccak.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.keccak"""
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 keccak
40 from Crypto.Util.py3compat import b, tobytes, bchr
41
42 class KeccakTest(unittest.TestCase):
43
44 def test_new_positive(self):
45
46 for digest_bits in (224, 256, 384, 512):
47 hobj = keccak.new(digest_bits=digest_bits)
48 self.assertEqual(hobj.digest_size, digest_bits // 8)
49
50 hobj2 = hobj.new()
51 self.assertEqual(hobj2.digest_size, digest_bits // 8)
52
53 for digest_bytes in (28, 32, 48, 64):
54 hobj = keccak.new(digest_bytes=digest_bytes)
55 self.assertEqual(hobj.digest_size, digest_bytes)
56
57 hobj2 = hobj.new()
58 self.assertEqual(hobj2.digest_size, digest_bytes)
59
60 def test_new_positive2(self):
61
62 digest1 = keccak.new(data=b("\x90"), digest_bytes=64).digest()
63 digest2 = keccak.new(digest_bytes=64).update(b("\x90")).digest()
64 self.assertEqual(digest1, digest2)
65
66 def test_new_negative(self):
67
68 # keccak.new needs digest size
69 self.assertRaises(TypeError, keccak.new)
70
71 h = keccak.new(digest_bits=512)
72
73 # Either bits or bytes can be specified
74 self.assertRaises(TypeError, keccak.new,
75 digest_bytes=64,
76 digest_bits=512)
77
78 # Range
79 self.assertRaises(ValueError, keccak.new, digest_bytes=0)
80 self.assertRaises(ValueError, keccak.new, digest_bytes=1)
81 self.assertRaises(ValueError, keccak.new, digest_bytes=65)
82 self.assertRaises(ValueError, keccak.new, digest_bits=0)
83 self.assertRaises(ValueError, keccak.new, digest_bits=1)
84 self.assertRaises(ValueError, keccak.new, digest_bits=513)
85
86 def test_update(self):
87 pieces = [bchr(10) * 200, bchr(20) * 300]
88 h = keccak.new(digest_bytes=64)
89 h.update(pieces[0]).update(pieces[1])
90 digest = h.digest()
91 h = keccak.new(digest_bytes=64)
92 h.update(pieces[0] + pieces[1])
93 self.assertEqual(h.digest(), digest)
94
95 def test_update_negative(self):
96 h = keccak.new(digest_bytes=64)
97 self.assertRaises(TypeError, h.update, u"string")
98
99 def test_digest(self):
100 h = keccak.new(digest_bytes=64)
101 digest = h.digest()
102
103 # hexdigest does not change the state
104 self.assertEqual(h.digest(), digest)
105 # digest returns a byte string
106 self.assertTrue(isinstance(digest, type(b("digest"))))
107
108 def test_hex_digest(self):
109 mac = keccak.new(digest_bits=512)
110 digest = mac.digest()
111 hexdigest = mac.hexdigest()
112
113 # hexdigest is equivalent to digest
114 self.assertEqual(hexlify(digest), tobytes(hexdigest))
115 # hexdigest does not change the state
116 self.assertEqual(mac.hexdigest(), hexdigest)
117 # hexdigest returns a string
118 self.assertTrue(isinstance(hexdigest, type("digest")))
119
120 def test_update_after_digest(self):
121 msg=b("rrrrttt")
122
123 # Normally, update() cannot be done after digest()
124 h = keccak.new(digest_bits=512, data=msg[:4])
125 dig1 = h.digest()
126 self.assertRaises(TypeError, h.update, msg[4:])
127 dig2 = keccak.new(digest_bits=512, data=msg).digest()
128
129 # With the proper flag, it is allowed
130 h = keccak.new(digest_bits=512, data=msg[:4], update_after_digest=True)
131 self.assertEqual(h.digest(), dig1)
132 # ... and the subsequent digest applies to the entire message
133 # up to that point
134 h.update(msg[4:])
135 self.assertEqual(h.digest(), dig2)
136
137
138 class KeccakVectors(unittest.TestCase):
139 pass
140
141 # TODO: add ExtremelyLong tests
142
143
144 test_vectors_224 = load_test_vectors(("Hash", "keccak"),
145 "ShortMsgKAT_224.txt",
146 "Short Messages KAT 224",
147 {"len": lambda x: int(x)}) or []
148
149 test_vectors_224 += load_test_vectors(("Hash", "keccak"),
150 "LongMsgKAT_224.txt",
151 "Long Messages KAT 224",
152 {"len": lambda x: int(x)}) or []
153
154 for idx, tv in enumerate(test_vectors_224):
155 if tv.len == 0:
156 data = b("")
157 else:
158 data = tobytes(tv.msg)
159
160 def new_test(self, data=data, result=tv.md):
161 hobj = keccak.new(digest_bits=224, data=data)
162 self.assertEqual(hobj.digest(), result)
163
164 setattr(KeccakVectors, "test_224_%d" % idx, new_test)
165
166 # ---
167
168 test_vectors_256 = load_test_vectors(("Hash", "keccak"),
169 "ShortMsgKAT_256.txt",
170 "Short Messages KAT 256",
171 { "len" : lambda x: int(x) } ) or []
172
173 test_vectors_256 += load_test_vectors(("Hash", "keccak"),
174 "LongMsgKAT_256.txt",
175 "Long Messages KAT 256",
176 { "len" : lambda x: int(x) } ) or []
177
178 for idx, tv in enumerate(test_vectors_256):
179 if tv.len == 0:
180 data = b("")
181 else:
182 data = tobytes(tv.msg)
183
184 def new_test(self, data=data, result=tv.md):
185 hobj = keccak.new(digest_bits=256, data=data)
186 self.assertEqual(hobj.digest(), result)
187
188 setattr(KeccakVectors, "test_256_%d" % idx, new_test)
189
190
191 # ---
192
193 test_vectors_384 = load_test_vectors(("Hash", "keccak"),
194 "ShortMsgKAT_384.txt",
195 "Short Messages KAT 384",
196 {"len": lambda x: int(x)}) or []
197
198 test_vectors_384 += load_test_vectors(("Hash", "keccak"),
199 "LongMsgKAT_384.txt",
200 "Long Messages KAT 384",
201 {"len": lambda x: int(x)}) or []
202
203 for idx, tv in enumerate(test_vectors_384):
204 if tv.len == 0:
205 data = b("")
206 else:
207 data = tobytes(tv.msg)
208
209 def new_test(self, data=data, result=tv.md):
210 hobj = keccak.new(digest_bits=384, data=data)
211 self.assertEqual(hobj.digest(), result)
212
213 setattr(KeccakVectors, "test_384_%d" % idx, new_test)
214
215 # ---
216
217 test_vectors_512 = load_test_vectors(("Hash", "keccak"),
218 "ShortMsgKAT_512.txt",
219 "Short Messages KAT 512",
220 {"len": lambda x: int(x)}) or []
221
222 test_vectors_512 += load_test_vectors(("Hash", "keccak"),
223 "LongMsgKAT_512.txt",
224 "Long Messages KAT 512",
225 {"len": lambda x: int(x)}) or []
226
227 for idx, tv in enumerate(test_vectors_512):
228 if tv.len == 0:
229 data = b("")
230 else:
231 data = tobytes(tv.msg)
232
233 def new_test(self, data=data, result=tv.md):
234 hobj = keccak.new(digest_bits=512, data=data)
235 self.assertEqual(hobj.digest(), result)
236
237 setattr(KeccakVectors, "test_512_%d" % idx, new_test)
238
239
240 def get_tests(config={}):
241 tests = []
242 tests += list_test_cases(KeccakTest)
243 tests += list_test_cases(KeccakVectors)
244 return tests
245
246
247 if __name__ == '__main__':
248 import unittest
249 suite = lambda: unittest.TestSuite(get_tests())
250 unittest.main(defaultTest='suite')