Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_KangarooTwelve.py

raw symbols SCIP
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
27 """Self-test suite for Crypto.Hash.KangarooTwelve"""
28
29 import unittest
30 from binascii import unhexlify
31
32 from Crypto.SelfTest.st_common import list_test_cases
33
34 from Crypto.Hash import KangarooTwelve as K12
35 from Crypto.Util.py3compat import b, bchr
36
37
38 class KangarooTwelveTest(unittest.TestCase):
39
40 def test_length_encode(self):
41 self.assertEqual(K12._length_encode(0), b'\x00')
42 self.assertEqual(K12._length_encode(12), b'\x0C\x01')
43 self.assertEqual(K12._length_encode(65538), b'\x01\x00\x02\x03')
44
45 def test_new_positive(self):
46
47 xof1 = K12.new()
48 xof2 = K12.new(data=b("90"))
49 xof3 = K12.new().update(b("90"))
50
51 self.assertNotEqual(xof1.read(10), xof2.read(10))
52 xof3.read(10)
53 self.assertEqual(xof2.read(10), xof3.read(10))
54
55 xof1 = K12.new()
56 ref = xof1.read(10)
57 xof2 = K12.new(custom=b(""))
58 xof3 = K12.new(custom=b("foo"))
59
60 self.assertEqual(ref, xof2.read(10))
61 self.assertNotEqual(ref, xof3.read(10))
62
63 xof1 = K12.new(custom=b("foo"))
64 xof2 = K12.new(custom=b("foo"), data=b("90"))
65 xof3 = K12.new(custom=b("foo")).update(b("90"))
66
67 self.assertNotEqual(xof1.read(10), xof2.read(10))
68 xof3.read(10)
69 self.assertEqual(xof2.read(10), xof3.read(10))
70
71 def test_update(self):
72 pieces = [bchr(10) * 200, bchr(20) * 300]
73 h = K12.new()
74 h.update(pieces[0]).update(pieces[1])
75 digest = h.read(10)
76 h = K12.new()
77 h.update(pieces[0] + pieces[1])
78 self.assertEqual(h.read(10), digest)
79
80 def test_update_negative(self):
81 h = K12.new()
82 self.assertRaises(TypeError, h.update, u"string")
83
84 def test_digest(self):
85 h = K12.new()
86 digest = h.read(90)
87
88 # read returns a byte string of the right length
89 self.assertTrue(isinstance(digest, type(b("digest"))))
90 self.assertEqual(len(digest), 90)
91
92 def test_update_after_read(self):
93 mac = K12.new()
94 mac.update(b("rrrr"))
95 mac.read(90)
96 self.assertRaises(TypeError, mac.update, b("ttt"))
97
98
99 def txt2bin(txt):
100 clean = txt.replace(" ", "").replace("\n", "").replace("\r", "")
101 return unhexlify(clean)
102
103
104 def ptn(n):
105 res = bytearray(n)
106 pattern = b"".join([bchr(x) for x in range(0, 0xFB)])
107 for base in range(0, n - 0xFB, 0xFB):
108 res[base:base + 0xFB] = pattern
109 remain = n % 0xFB
110 if remain:
111 base = (n // 0xFB) * 0xFB
112 res[base:] = pattern[:remain]
113 assert(len(res) == n)
114 return res
115
116
117 def chunked(source, size):
118 for i in range(0, len(source), size):
119 yield source[i:i+size]
120
121
122 class KangarooTwelveTV(unittest.TestCase):
123
124 # https://github.com/XKCP/XKCP/blob/master/tests/TestVectors/KangarooTwelve.txt
125
126 def test_zero_1(self):
127 tv = """1A C2 D4 50 FC 3B 42 05 D1 9D A7 BF CA 1B 37 51
128 3C 08 03 57 7A C7 16 7F 06 FE 2C E1 F0 EF 39 E5"""
129
130 btv = txt2bin(tv)
131 res = K12.new().read(32)
132 self.assertEqual(res, btv)
133
134 def test_zero_2(self):
135 tv = """1A C2 D4 50 FC 3B 42 05 D1 9D A7 BF CA 1B 37 51
136 3C 08 03 57 7A C7 16 7F 06 FE 2C E1 F0 EF 39 E5
137 42 69 C0 56 B8 C8 2E 48 27 60 38 B6 D2 92 96 6C
138 C0 7A 3D 46 45 27 2E 31 FF 38 50 81 39 EB 0A 71"""
139
140 btv = txt2bin(tv)
141 res = K12.new().read(64)
142 self.assertEqual(res, btv)
143
144 def test_zero_3(self):
145 tv = """E8 DC 56 36 42 F7 22 8C 84 68 4C 89 84 05 D3 A8
146 34 79 91 58 C0 79 B1 28 80 27 7A 1D 28 E2 FF 6D"""
147
148 btv = txt2bin(tv)
149 res = K12.new().read(10032)
150 self.assertEqual(res[-32:], btv)
151
152 def test_ptn_1(self):
153 tv = """2B DA 92 45 0E 8B 14 7F 8A 7C B6 29 E7 84 A0 58
154 EF CA 7C F7 D8 21 8E 02 D3 45 DF AA 65 24 4A 1F"""
155
156 btv = txt2bin(tv)
157 res = K12.new(data=ptn(1)).read(32)
158 self.assertEqual(res, btv)
159
160 def test_ptn_17(self):
161 tv = """6B F7 5F A2 23 91 98 DB 47 72 E3 64 78 F8 E1 9B
162 0F 37 12 05 F6 A9 A9 3A 27 3F 51 DF 37 12 28 88"""
163
164 btv = txt2bin(tv)
165 res = K12.new(data=ptn(17)).read(32)
166 self.assertEqual(res, btv)
167
168 def test_ptn_17_2(self):
169 tv = """0C 31 5E BC DE DB F6 14 26 DE 7D CF 8F B7 25 D1
170 E7 46 75 D7 F5 32 7A 50 67 F3 67 B1 08 EC B6 7C"""
171
172 btv = txt2bin(tv)
173 res = K12.new(data=ptn(17**2)).read(32)
174 self.assertEqual(res, btv)
175
176 def test_ptn_17_3(self):
177 tv = """CB 55 2E 2E C7 7D 99 10 70 1D 57 8B 45 7D DF 77
178 2C 12 E3 22 E4 EE 7F E4 17 F9 2C 75 8F 0D 59 D0"""
179
180 btv = txt2bin(tv)
181 res = K12.new(data=ptn(17**3)).read(32)
182 self.assertEqual(res, btv)
183
184 def test_ptn_17_4(self):
185 tv = """87 01 04 5E 22 20 53 45 FF 4D DA 05 55 5C BB 5C
186 3A F1 A7 71 C2 B8 9B AE F3 7D B4 3D 99 98 B9 FE"""
187
188 btv = txt2bin(tv)
189 data = ptn(17**4)
190
191 # All at once
192 res = K12.new(data=data).read(32)
193 self.assertEqual(res, btv)
194
195 # Byte by byte
196 k12 = K12.new()
197 for x in data:
198 k12.update(bchr(x))
199 res = k12.read(32)
200 self.assertEqual(res, btv)
201
202 # Chunks of various prime sizes
203 for chunk_size in (13, 17, 19, 23, 31):
204 k12 = K12.new()
205 for x in chunked(data, chunk_size):
206 k12.update(x)
207 res = k12.read(32)
208 self.assertEqual(res, btv)
209
210 def test_ptn_17_5(self):
211 tv = """84 4D 61 09 33 B1 B9 96 3C BD EB 5A E3 B6 B0 5C
212 C7 CB D6 7C EE DF 88 3E B6 78 A0 A8 E0 37 16 82"""
213
214 btv = txt2bin(tv)
215 data = ptn(17**5)
216
217 # All at once
218 res = K12.new(data=data).read(32)
219 self.assertEqual(res, btv)
220
221 # Chunks
222 k12 = K12.new()
223 for chunk in chunked(data, 8192):
224 k12.update(chunk)
225 res = k12.read(32)
226 self.assertEqual(res, btv)
227
228 def test_ptn_17_6(self):
229 tv = """3C 39 07 82 A8 A4 E8 9F A6 36 7F 72 FE AA F1 32
230 55 C8 D9 58 78 48 1D 3C D8 CE 85 F5 8E 88 0A F8"""
231
232 btv = txt2bin(tv)
233 data = ptn(17**6)
234
235 # All at once
236 res = K12.new(data=data).read(32)
237 self.assertEqual(res, btv)
238
239 def test_ptn_c_1(self):
240 tv = """FA B6 58 DB 63 E9 4A 24 61 88 BF 7A F6 9A 13 30
241 45 F4 6E E9 84 C5 6E 3C 33 28 CA AF 1A A1 A5 83"""
242
243 btv = txt2bin(tv)
244 custom = ptn(1)
245
246 # All at once
247 res = K12.new(custom=custom).read(32)
248 self.assertEqual(res, btv)
249
250 def test_ptn_c_41(self):
251 tv = """D8 48 C5 06 8C ED 73 6F 44 62 15 9B 98 67 FD 4C
252 20 B8 08 AC C3 D5 BC 48 E0 B0 6B A0 A3 76 2E C4"""
253
254 btv = txt2bin(tv)
255 custom = ptn(41)
256
257 # All at once
258 res = K12.new(data=b'\xFF', custom=custom).read(32)
259 self.assertEqual(res, btv)
260
261 def test_ptn_c_41_2(self):
262 tv = """C3 89 E5 00 9A E5 71 20 85 4C 2E 8C 64 67 0A C0
263 13 58 CF 4C 1B AF 89 44 7A 72 42 34 DC 7C ED 74"""
264
265 btv = txt2bin(tv)
266 custom = ptn(41**2)
267
268 # All at once
269 res = K12.new(data=b'\xFF' * 3, custom=custom).read(32)
270 self.assertEqual(res, btv)
271
272 def test_ptn_c_41_3(self):
273 tv = """75 D2 F8 6A 2E 64 45 66 72 6B 4F BC FC 56 57 B9
274 DB CF 07 0C 7B 0D CA 06 45 0A B2 91 D7 44 3B CF"""
275
276 btv = txt2bin(tv)
277 custom = ptn(41**3)
278
279 # All at once
280 res = K12.new(data=b'\xFF' * 7, custom=custom).read(32)
281 self.assertEqual(res, btv)
282
283 # https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/
284
285 def test_ptn_8191(self):
286 tv = """1B 57 76 36 F7 23 64 3E 99 0C C7 D6 A6 59 83 74
287 36 FD 6A 10 36 26 60 0E B8 30 1C D1 DB E5 53 D6"""
288
289 btv = txt2bin(tv)
290
291 # All at once
292 res = K12.new(data=ptn(8191)).read(32)
293 self.assertEqual(res, btv)
294
295 def test_ptn_8192(self):
296 tv = """48 F2 56 F6 77 2F 9E DF B6 A8 B6 61 EC 92 DC 93
297 B9 5E BD 05 A0 8A 17 B3 9A E3 49 08 70 C9 26 C3"""
298
299 btv = txt2bin(tv)
300
301 # All at once
302 res = K12.new(data=ptn(8192)).read(32)
303 self.assertEqual(res, btv)
304
305 def test_ptn_8192_8189(self):
306 tv = """3E D1 2F 70 FB 05 DD B5 86 89 51 0A B3 E4 D2 3C
307 6C 60 33 84 9A A0 1E 1D 8C 22 0A 29 7F ED CD 0B"""
308
309 btv = txt2bin(tv)
310
311 # All at once
312 res = K12.new(data=ptn(8192), custom=ptn(8189)).read(32)
313 self.assertEqual(res, btv)
314
315 def test_ptn_8192_8190(self):
316 tv = """6A 7C 1B 6A 5C D0 D8 C9 CA 94 3A 4A 21 6C C6 46
317 04 55 9A 2E A4 5F 78 57 0A 15 25 3D 67 BA 00 AE"""
318
319 btv = txt2bin(tv)
320
321 # All at once
322 res = K12.new(data=ptn(8192), custom=ptn(8190)).read(32)
323 self.assertEqual(res, btv)
324
325 ###
326
327 def test_1(self):
328 tv = "fd608f91d81904a9916e78a18f65c157a78d63f93d8f6367db0524526a5ea2bb"
329
330 btv = txt2bin(tv)
331 res = K12.new(data=b'', custom=ptn(100)).read(32)
332 self.assertEqual(res, btv)
333
334 def test_2(self):
335 tv4 = "5a4ec9a649f81916d4ce1553492962f7868abf8dd1ceb2f0cb3682ea95cda6a6"
336 tv3 = "441688fe4fe4ae9425eb3105eb445eb2b3a6f67b66eff8e74ebfbc49371f6d4c"
337 tv2 = "17269a57759af0214c84a0fd9bc851f4d95f80554cfed4e7da8a6ee1ff080131"
338 tv1 = "33826990c09dc712ba7224f0d9be319e2720de95a4c1afbd2211507dae1c703a"
339 tv0 = "9f4d3aba908ddc096e4d3a71da954f917b9752f05052b9d26d916a6fbc75bf3e"
340
341 res = K12.new(data=b'A' * (8192 - 4), custom=b'B').read(32)
342 self.assertEqual(res, txt2bin(tv4))
343
344 res = K12.new(data=b'A' * (8192 - 3), custom=b'B').read(32)
345 self.assertEqual(res, txt2bin(tv3))
346
347 res = K12.new(data=b'A' * (8192 - 2), custom=b'B').read(32)
348 self.assertEqual(res, txt2bin(tv2))
349
350 res = K12.new(data=b'A' * (8192 - 1), custom=b'B').read(32)
351 self.assertEqual(res, txt2bin(tv1))
352
353 res = K12.new(data=b'A' * (8192 - 0), custom=b'B').read(32)
354 self.assertEqual(res, txt2bin(tv0))
355
356
357 def get_tests(config={}):
358 tests = []
359 tests += list_test_cases(KangarooTwelveTest)
360 tests += list_test_cases(KangarooTwelveTV)
361 return tests
362
363
364 if __name__ == '__main__':
365 def suite():
366 return unittest.TestSuite(get_tests())
367 unittest.main(defaultTest='suite')