Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_KMAC.py

raw symbols SCIP
1 import unittest
2 from binascii import unhexlify, hexlify
3
4 from Crypto.Util.py3compat import tobytes
5 from Crypto.Util.strxor import strxor_c
6 from Crypto.SelfTest.st_common import list_test_cases
7
8 from Crypto.Hash import KMAC128, KMAC256
9
10
11 class KMACTest(unittest.TestCase):
12
13 def new(self, *args, **kwargs):
14 return self.KMAC.new(key=b'X' * (self.minimum_key_bits // 8), *args, **kwargs)
15
16 def test_new_positive(self):
17
18 key = b'X' * 32
19
20 h = self.new()
21 for new_func in self.KMAC.new, h.new:
22
23 for dbytes in range(self.minimum_bytes, 128 + 1):
24 hobj = new_func(key=key, mac_len=dbytes)
25 self.assertEqual(hobj.digest_size, dbytes)
26
27 digest1 = new_func(key=key, data=b"\x90").digest()
28 digest2 = new_func(key=key).update(b"\x90").digest()
29 self.assertEqual(digest1, digest2)
30
31 new_func(data=b"A", key=key, custom=b"g")
32
33 hobj = h.new(key=key)
34 self.assertEqual(hobj.digest_size, self.default_bytes)
35
36 def test_new_negative(self):
37
38 h = self.new()
39 for new_func in self.KMAC.new, h.new:
40 self.assertRaises(ValueError, new_func, key=b'X'*32,
41 mac_len=0)
42 self.assertRaises(ValueError, new_func, key=b'X'*32,
43 mac_len=self.minimum_bytes - 1)
44 self.assertRaises(TypeError, new_func,
45 key=u"string")
46 self.assertRaises(TypeError, new_func,
47 data=u"string")
48
49 def test_default_digest_size(self):
50 digest = self.new(data=b'abc').digest()
51 self.assertEqual(len(digest), self.default_bytes)
52
53 def test_update(self):
54 pieces = [b"\x0A" * 200, b"\x14" * 300]
55 h = self.new()
56 h.update(pieces[0]).update(pieces[1])
57 digest = h.digest()
58 h = self.new()
59 h.update(pieces[0] + pieces[1])
60 self.assertEqual(h.digest(), digest)
61
62 def test_update_negative(self):
63 h = self.new()
64 self.assertRaises(TypeError, h.update, u"string")
65
66 def test_digest(self):
67 h = self.new()
68 digest = h.digest()
69
70 # hexdigest does not change the state
71 self.assertEqual(h.digest(), digest)
72 # digest returns a byte string
73 self.assertTrue(isinstance(digest, type(b"digest")))
74
75 def test_update_after_digest(self):
76 msg = b"rrrrttt"
77
78 # Normally, update() cannot be done after digest()
79 h = self.new(mac_len=32, data=msg[:4])
80 dig1 = h.digest()
81 self.assertRaises(TypeError, h.update, dig1)
82
83 def test_hex_digest(self):
84 mac = self.new()
85 digest = mac.digest()
86 hexdigest = mac.hexdigest()
87
88 # hexdigest is equivalent to digest
89 self.assertEqual(hexlify(digest), tobytes(hexdigest))
90 # hexdigest does not change the state
91 self.assertEqual(mac.hexdigest(), hexdigest)
92 # hexdigest returns a string
93 self.assertTrue(isinstance(hexdigest, type("digest")))
94
95 def test_verify(self):
96 h = self.new()
97 mac = h.digest()
98 h.verify(mac)
99 wrong_mac = strxor_c(mac, 255)
100 self.assertRaises(ValueError, h.verify, wrong_mac)
101
102 def test_hexverify(self):
103 h = self.new()
104 mac = h.hexdigest()
105 h.hexverify(mac)
106 self.assertRaises(ValueError, h.hexverify, "4556")
107
108 def test_oid(self):
109
110 oid = "2.16.840.1.101.3.4.2." + self.oid_variant
111 h = self.new()
112 self.assertEqual(h.oid, oid)
113
114 def test_bytearray(self):
115
116 key = b'0' * 32
117 data = b"\x00\x01\x02"
118
119 # Data and key can be a bytearray (during initialization)
120 key_ba = bytearray(key)
121 data_ba = bytearray(data)
122
123 h1 = self.KMAC.new(data=data, key=key)
124 h2 = self.KMAC.new(data=data_ba, key=key_ba)
125 key_ba[:1] = b'\xFF'
126 data_ba[:1] = b'\xFF'
127
128 self.assertEqual(h1.digest(), h2.digest())
129
130 # Data can be a bytearray (during operation)
131 data_ba = bytearray(data)
132
133 h1 = self.new()
134 h2 = self.new()
135 h1.update(data)
136 h2.update(data_ba)
137 data_ba[:1] = b'\xFF'
138
139 self.assertEqual(h1.digest(), h2.digest())
140
141 def test_memoryview(self):
142
143 key = b'0' * 32
144 data = b"\x00\x01\x02"
145
146 def get_mv_ro(data):
147 return memoryview(data)
148
149 def get_mv_rw(data):
150 return memoryview(bytearray(data))
151
152 for get_mv in (get_mv_ro, get_mv_rw):
153
154 # Data and key can be a memoryview (during initialization)
155 key_mv = get_mv(key)
156 data_mv = get_mv(data)
157
158 h1 = self.KMAC.new(data=data, key=key)
159 h2 = self.KMAC.new(data=data_mv, key=key_mv)
160 if not data_mv.readonly:
161 data_mv[:1] = b'\xFF'
162 key_mv[:1] = b'\xFF'
163
164 self.assertEqual(h1.digest(), h2.digest())
165
166 # Data can be a memoryview (during operation)
167 data_mv = get_mv(data)
168
169 h1 = self.new()
170 h2 = self.new()
171 h1.update(data)
172 h2.update(data_mv)
173 if not data_mv.readonly:
174 data_mv[:1] = b'\xFF'
175
176 self.assertEqual(h1.digest(), h2.digest())
177
178
179 class KMAC128Test(KMACTest):
180
181 KMAC = KMAC128
182
183 minimum_key_bits = 128
184
185 minimum_bytes = 8
186 default_bytes = 64
187
188 oid_variant = "19"
189
190
191 class KMAC256Test(KMACTest):
192
193 KMAC = KMAC256
194
195 minimum_key_bits = 256
196
197 minimum_bytes = 8
198 default_bytes = 64
199
200 oid_variant = "20"
201
202
203 class NISTExampleTestVectors(unittest.TestCase):
204
205 # https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/KMAC_samples.pdf
206 test_data = [
207 (
208 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
209 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
210 "00 01 02 03",
211 "",
212 "E5 78 0B 0D 3E A6 F7 D3 A4 29 C5 70 6A A4 3A 00"
213 "FA DB D7 D4 96 28 83 9E 31 87 24 3F 45 6E E1 4E",
214 "Sample #1 NIST",
215 KMAC128
216 ),
217 (
218 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
219 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
220 "00 01 02 03",
221 "My Tagged Application",
222 "3B 1F BA 96 3C D8 B0 B5 9E 8C 1A 6D 71 88 8B 71"
223 "43 65 1A F8 BA 0A 70 70 C0 97 9E 28 11 32 4A A5",
224 "Sample #2 NIST",
225 KMAC128
226 ),
227 (
228 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
229 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
230 "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"
231 "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
232 "20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F"
233 "30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F"
234 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
235 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F"
236 "60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F"
237 "70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F"
238 "80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F"
239 "90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F"
240 "A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF"
241 "B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF"
242 "C0 C1 C2 C3 C4 C5 C6 C7",
243 "My Tagged Application",
244 "1F 5B 4E 6C CA 02 20 9E 0D CB 5C A6 35 B8 9A 15"
245 "E2 71 EC C7 60 07 1D FD 80 5F AA 38 F9 72 92 30",
246 "Sample #3 NIST",
247 KMAC128
248 ),
249 (
250 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
251 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
252 "00 01 02 03",
253 "My Tagged Application",
254 "20 C5 70 C3 13 46 F7 03 C9 AC 36 C6 1C 03 CB 64"
255 "C3 97 0D 0C FC 78 7E 9B 79 59 9D 27 3A 68 D2 F7"
256 "F6 9D 4C C3 DE 9D 10 4A 35 16 89 F2 7C F6 F5 95"
257 "1F 01 03 F3 3F 4F 24 87 10 24 D9 C2 77 73 A8 DD",
258 "Sample #4 NIST",
259 KMAC256
260 ),
261 (
262 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
263 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
264 "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"
265 "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
266 "20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F"
267 "30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F"
268 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
269 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F"
270 "60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F"
271 "70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F"
272 "80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F"
273 "90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F"
274 "A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF"
275 "B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF"
276 "C0 C1 C2 C3 C4 C5 C6 C7",
277 "",
278 "75 35 8C F3 9E 41 49 4E 94 97 07 92 7C EE 0A F2"
279 "0A 3F F5 53 90 4C 86 B0 8F 21 CC 41 4B CF D6 91"
280 "58 9D 27 CF 5E 15 36 9C BB FF 8B 9A 4C 2E B1 78"
281 "00 85 5D 02 35 FF 63 5D A8 25 33 EC 6B 75 9B 69",
282 "Sample #5 NIST",
283 KMAC256
284 ),
285 (
286 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
287 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F",
288 "00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"
289 "10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F"
290 "20 21 22 23 24 25 26 27 28 29 2A 2B 2C 2D 2E 2F"
291 "30 31 32 33 34 35 36 37 38 39 3A 3B 3C 3D 3E 3F"
292 "40 41 42 43 44 45 46 47 48 49 4A 4B 4C 4D 4E 4F"
293 "50 51 52 53 54 55 56 57 58 59 5A 5B 5C 5D 5E 5F"
294 "60 61 62 63 64 65 66 67 68 69 6A 6B 6C 6D 6E 6F"
295 "70 71 72 73 74 75 76 77 78 79 7A 7B 7C 7D 7E 7F"
296 "80 81 82 83 84 85 86 87 88 89 8A 8B 8C 8D 8E 8F"
297 "90 91 92 93 94 95 96 97 98 99 9A 9B 9C 9D 9E 9F"
298 "A0 A1 A2 A3 A4 A5 A6 A7 A8 A9 AA AB AC AD AE AF"
299 "B0 B1 B2 B3 B4 B5 B6 B7 B8 B9 BA BB BC BD BE BF"
300 "C0 C1 C2 C3 C4 C5 C6 C7",
301 "My Tagged Application",
302 "B5 86 18 F7 1F 92 E1 D5 6C 1B 8C 55 DD D7 CD 18"
303 "8B 97 B4 CA 4D 99 83 1E B2 69 9A 83 7D A2 E4 D9"
304 "70 FB AC FD E5 00 33 AE A5 85 F1 A2 70 85 10 C3"
305 "2D 07 88 08 01 BD 18 28 98 FE 47 68 76 FC 89 65",
306 "Sample #6 NIST",
307 KMAC256
308 ),
309 ]
310
311 def setUp(self):
312 td = []
313 for key, data, custom, mac, text, module in self.test_data:
314 ni = (
315 unhexlify(key.replace(" ", "")),
316 unhexlify(data.replace(" ", "")),
317 custom.encode(),
318 unhexlify(mac.replace(" ", "")),
319 text,
320 module
321 )
322 td.append(ni)
323 self.test_data = td
324
325 def runTest(self):
326
327 for key, data, custom, mac, text, module in self.test_data:
328 h = module.new(data=data, key=key, custom=custom, mac_len=len(mac))
329 mac_tag = h.digest()
330 self.assertEqual(mac_tag, mac, msg=text)
331
332
333 def get_tests(config={}):
334 tests = []
335
336 tests += list_test_cases(KMAC128Test)
337 tests += list_test_cases(KMAC256Test)
338 tests.append(NISTExampleTestVectors())
339
340 return tests
341
342
343 if __name__ == '__main__':
344 def suite():
345 return unittest.TestSuite(get_tests())
346 unittest.main(defaultTest='suite')