Debian Codegraph @

lib/Crypto/SelfTest/Hash/test_Poly1305.py

raw symbols SCIP
1 #
2 # SelfTest/Hash/test_Poly1305.py: Self-test for the Poly1305 module
3 #
4 # ===================================================================
5 #
6 # Copyright (c) 2018, Helder Eijs <[email protected]>
7 # All rights reserved.
8 #
9 # Redistribution and use in source and binary forms, with or without
10 # modification, are permitted provided that the following conditions
11 # are met:
12 #
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in
17 # the documentation and/or other materials provided with the
18 # distribution.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 # ===================================================================
33
34 """Self-test suite for Crypto.Hash._Poly1305"""
35
36 import json
37 import unittest
38 from binascii import unhexlify, hexlify
39
40 from .common import make_mac_tests
41 from Crypto.SelfTest.st_common import list_test_cases
42
43 from Crypto.Hash import Poly1305
44 from Crypto.Cipher import AES, ChaCha20
45
46 from Crypto.Util.py3compat import tobytes
47 from Crypto.Util.strxor import strxor_c
48
49 # This is a list of (r+s keypair, data, result, description, keywords) tuples.
50 test_data_basic = [
51 (
52 "85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b",
53 hexlify(b"Cryptographic Forum Research Group").decode(),
54 "a8061dc1305136c6c22b8baf0c0127a9",
55 "RFC7539"
56 ),
57 (
58 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
59 "0000000000000000000000000000000000000000000000000000000000000000",
60 "49ec78090e481ec6c26b33b91ccc0307",
61 "https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-00#section-7 A",
62 ),
63 (
64 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
65 "48656c6c6f20776f726c6421",
66 "a6f745008f81c916a20dcc74eef2b2f0",
67 "https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-00#section-7 B",
68 ),
69 (
70 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
71 "",
72 "6b657920666f7220506f6c7931333035",
73 "Generated with pure Python",
74 ),
75 (
76 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
77 "FF",
78 "f7e4e0ef4c46d106219da3d1bdaeb3ff",
79 "Generated with pure Python",
80 ),
81 (
82 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
83 "FF00",
84 "7471eceeb22988fc936da1d6e838b70e",
85 "Generated with pure Python",
86 ),
87 (
88 "746869732069732033322d62797465206b657920666f7220506f6c7931333035",
89 "AA" * 17,
90 "32590bc07cb2afaccca3f67f122975fe",
91 "Generated with pure Python",
92 ),
93 (
94 "00" * 32,
95 "00" * 64,
96 "00" * 16,
97 "RFC7539 A.3 #1",
98 ),
99 (
100 "0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e",
101 hexlify(
102 b"Any submission t"
103 b"o the IETF inten"
104 b"ded by the Contr"
105 b"ibutor for publi"
106 b"cation as all or"
107 b" part of an IETF"
108 b" Internet-Draft "
109 b"or RFC and any s"
110 b"tatement made wi"
111 b"thin the context"
112 b" of an IETF acti"
113 b"vity is consider"
114 b"ed an \"IETF Cont"
115 b"ribution\". Such "
116 b"statements inclu"
117 b"de oral statemen"
118 b"ts in IETF sessi"
119 b"ons, as well as "
120 b"written and elec"
121 b"tronic communica"
122 b"tions made at an"
123 b"y time or place,"
124 b" which are addre"
125 b"ssed to").decode(),
126 "36e5f6b5c5e06070f0efca96227a863e",
127 "RFC7539 A.3 #2",
128 ),
129 (
130 "36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000",
131 hexlify(
132 b"Any submission t"
133 b"o the IETF inten"
134 b"ded by the Contr"
135 b"ibutor for publi"
136 b"cation as all or"
137 b" part of an IETF"
138 b" Internet-Draft "
139 b"or RFC and any s"
140 b"tatement made wi"
141 b"thin the context"
142 b" of an IETF acti"
143 b"vity is consider"
144 b"ed an \"IETF Cont"
145 b"ribution\". Such "
146 b"statements inclu"
147 b"de oral statemen"
148 b"ts in IETF sessi"
149 b"ons, as well as "
150 b"written and elec"
151 b"tronic communica"
152 b"tions made at an"
153 b"y time or place,"
154 b" which are addre"
155 b"ssed to").decode(),
156 "f3477e7cd95417af89a6b8794c310cf0",
157 "RFC7539 A.3 #3",
158 ),
159 (
160 "1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
161 "2754776173206272696c6c69672c2061"
162 "6e642074686520736c6974687920746f"
163 "7665730a446964206779726520616e64"
164 "2067696d626c6520696e207468652077"
165 "6162653a0a416c6c206d696d73792077"
166 "6572652074686520626f726f676f7665"
167 "732c0a416e6420746865206d6f6d6520"
168 "7261746873206f757467726162652e",
169 "4541669a7eaaee61e708dc7cbcc5eb62",
170 "RFC7539 A.3 #4",
171 ),
172 (
173 "02" + "00" * 31,
174 "FF" * 16,
175 "03" + "00" * 15,
176 "RFC7539 A.3 #5",
177 ),
178 (
179 "02" + "00" * 15 + "FF" * 16,
180 "02" + "00" * 15,
181 "03" + "00" * 15,
182 "RFC7539 A.3 #6",
183 ),
184 (
185 "01" + "00" * 31,
186 "FF" * 16 + "F0" + "FF" * 15 + "11" + "00" * 15,
187 "05" + "00" * 15,
188 "RFC7539 A.3 #7",
189 ),
190 (
191 "01" + "00" * 31,
192 "FF" * 16 + "FB" + "FE" * 15 + "01" * 16,
193 "00" * 16,
194 "RFC7539 A.3 #8",
195 ),
196 (
197 "02" + "00" * 31,
198 "FD" + "FF" * 15,
199 "FA" + "FF" * 15,
200 "RFC7539 A.3 #9",
201 ),
202 (
203 "01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00"
204 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
205 "E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00"
206 "33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00"
207 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"
208 "01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
209 "14 00 00 00 00 00 00 00 55 00 00 00 00 00 00 00",
210 "RFC7539 A.3 #10",
211 ),
212 (
213 "01 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00"
214 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
215 "E3 35 94 D7 50 5E 43 B9 00 00 00 00 00 00 00 00"
216 "33 94 D7 50 5E 43 79 CD 01 00 00 00 00 00 00 00"
217 "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00",
218 "13" + "00" * 15,
219 "RFC7539 A.3 #11",
220 ),
221 ]
222
223 # This is a list of (key(k+r), data, result, description, keywords) tuples.
224 test_data_aes = [
225 (
226 "ec074c835580741701425b623235add6851fc40c3467ac0be05cc20404f3f700",
227 "f3f6",
228 "f4c633c3044fc145f84f335cb81953de",
229 "http://cr.yp.to/mac/poly1305-20050329.pdf",
230 { 'cipher':AES, 'nonce':unhexlify("fb447350c4e868c52ac3275cf9d4327e") }
231 ),
232 (
233 "75deaa25c09f208e1dc4ce6b5cad3fbfa0f3080000f46400d0c7e9076c834403",
234 "",
235 "dd3fab2251f11ac759f0887129cc2ee7",
236 "http://cr.yp.to/mac/poly1305-20050329.pdf",
237 { 'cipher':AES, 'nonce':unhexlify("61ee09218d29b0aaed7e154a2c5509cc") }
238 ),
239 (
240 "6acb5f61a7176dd320c5c1eb2edcdc7448443d0bb0d21109c89a100b5ce2c208",
241 "663cea190ffb83d89593f3f476b6bc24"
242 "d7e679107ea26adb8caf6652d0656136",
243 "0ee1c16bb73f0f4fd19881753c01cdbe",
244 "http://cr.yp.to/mac/poly1305-20050329.pdf",
245 { 'cipher':AES, 'nonce':unhexlify("ae212a55399729595dea458bc621ff0e") }
246 ),
247 (
248 "e1a5668a4d5b66a5f68cc5424ed5982d12976a08c4426d0ce8a82407c4f48207",
249 "ab0812724a7f1e342742cbed374d94d1"
250 "36c6b8795d45b3819830f2c04491faf0"
251 "990c62e48b8018b2c3e4a0fa3134cb67"
252 "fa83e158c994d961c4cb21095c1bf9",
253 "5154ad0d2cb26e01274fc51148491f1b",
254 "http://cr.yp.to/mac/poly1305-20050329.pdf",
255 { 'cipher':AES, 'nonce':unhexlify("9ae831e743978d3a23527c7128149e3a") }
256 ),
257 ]
258
259 test_data_chacha20 = [
260 (
261 "00" * 32,
262 "FF" * 15,
263 "13cc5bbadc36b03a5163928f0bcb65aa",
264 "RFC7539 A.4 #1",
265 { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 12) }
266 ),
267 (
268 "00" * 31 + "01",
269 "FF" * 15,
270 "0baf33c1d6df211bdd50a6767e98e00a",
271 "RFC7539 A.4 #2",
272 { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 11 + "02") }
273 ),
274 (
275 "1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0"
276 "47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0",
277 "FF" * 15,
278 "e8b4c6db226cd8939e65e02eebf834ce",
279 "RFC7539 A.4 #3",
280 { 'cipher':ChaCha20, 'nonce':unhexlify("00" * 11 + "02") }
281 ),
282 (
283 "1c 92 40 a5 eb 55 d3 8a f3 33 88 86 04 f6 b5 f0"
284 "47 39 17 c1 40 2b 80 09 9d ca 5c bc 20 70 75 c0",
285 "f3 33 88 86 00 00 00 00 00 00 4e 91 00 00 00 00"
286 "64 a0 86 15 75 86 1a f4 60 f0 62 c7 9b e6 43 bd"
287 "5e 80 5c fd 34 5c f3 89 f1 08 67 0a c7 6c 8c b2"
288 "4c 6c fc 18 75 5d 43 ee a0 9e e9 4e 38 2d 26 b0"
289 "bd b7 b7 3c 32 1b 01 00 d4 f0 3b 7f 35 58 94 cf"
290 "33 2f 83 0e 71 0b 97 ce 98 c8 a8 4a bd 0b 94 81"
291 "14 ad 17 6e 00 8d 33 bd 60 f9 82 b1 ff 37 c8 55"
292 "97 97 a0 6e f4 f0 ef 61 c1 86 32 4e 2b 35 06 38"
293 "36 06 90 7b 6a 7c 02 b0 f9 f6 15 7b 53 c8 67 e4"
294 "b9 16 6c 76 7b 80 4d 46 a5 9b 52 16 cd e7 a4 e9"
295 "90 40 c5 a4 04 33 22 5e e2 82 a1 b0 a0 6c 52 3e"
296 "af 45 34 d7 f8 3f a1 15 5b 00 47 71 8c bc 54 6a"
297 "0d 07 2b 04 b3 56 4e ea 1b 42 22 73 f5 48 27 1a"
298 "0b b2 31 60 53 fa 76 99 19 55 eb d6 31 59 43 4e"
299 "ce bb 4e 46 6d ae 5a 10 73 a6 72 76 27 09 7a 10"
300 "49 e6 17 d9 1d 36 10 94 fa 68 f0 ff 77 98 71 30"
301 "30 5b ea ba 2e da 04 df 99 7b 71 4d 6c 6f 2c 29"
302 "a6 ad 5c b4 02 2b 02 70 9b 00 00 00 00 00 00 00"
303 "0c 00 00 00 00 00 00 00 09 01 00 00 00 00 00 00",
304 "ee ad 9d 67 89 0c bb 22 39 23 36 fe a1 85 1f 38",
305 "RFC7539 A.5",
306 { 'cipher':ChaCha20, 'nonce':unhexlify("000000000102030405060708") }
307 ),
308 ]
309
310
311 class Poly1305Test_AES(unittest.TestCase):
312
313 key = b'\x11' * 32
314
315 def test_new_positive(self):
316
317 data = b'r' * 100
318
319 h1 = Poly1305.new(key=self.key, cipher=AES)
320 self.assertEqual(h1.digest_size, 16)
321 self.assertEqual(len(h1.nonce), 16)
322 d1 = h1.update(data).digest()
323 self.assertEqual(len(d1), 16)
324
325 h2 = Poly1305.new(key=self.key, nonce=h1.nonce, data=data, cipher=AES)
326 d2 = h2.digest()
327 self.assertEqual(h1.nonce, h2.nonce)
328 self.assertEqual(d1, d2)
329
330 def test_new_negative(self):
331 from Crypto.Cipher import DES3
332
333 self.assertRaises(ValueError, Poly1305.new, key=self.key[:31], cipher=AES)
334 self.assertRaises(ValueError, Poly1305.new, key=self.key, cipher=DES3)
335 self.assertRaises(ValueError, Poly1305.new, key=self.key, nonce=b'1' * 15, cipher=AES)
336 self.assertRaises(TypeError, Poly1305.new, key=u"2" * 32, cipher=AES)
337 self.assertRaises(TypeError, Poly1305.new, key=self.key, data=u"2" * 100, cipher=AES)
338
339 def test_update(self):
340 pieces = [b"\x0A" * 200, b"\x14" * 300]
341 h1 = Poly1305.new(key=self.key, cipher=AES)
342 h1.update(pieces[0]).update(pieces[1])
343 d1 = h1.digest()
344
345 h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
346 h2.update(pieces[0] + pieces[1])
347 d2 = h2.digest()
348 self.assertEqual(d1, d2)
349
350 def test_update_negative(self):
351 h = Poly1305.new(key=self.key, cipher=AES)
352 self.assertRaises(TypeError, h.update, u"string")
353
354 def test_digest(self):
355 h = Poly1305.new(key=self.key, cipher=AES)
356 digest = h.digest()
357
358 # hexdigest does not change the state
359 self.assertEqual(h.digest(), digest)
360 # digest returns a byte string
361 self.assertTrue(isinstance(digest, type(b"digest")))
362
363 def test_update_after_digest(self):
364 msg=b"rrrrttt"
365
366 # Normally, update() cannot be done after digest()
367 h = Poly1305.new(key=self.key, data=msg[:4], cipher=AES)
368 h.digest()
369 self.assertRaises(TypeError, h.update, msg[4:])
370
371 def test_hex_digest(self):
372 mac = Poly1305.new(key=self.key, cipher=AES)
373 digest = mac.digest()
374 hexdigest = mac.hexdigest()
375
376 # hexdigest is equivalent to digest
377 self.assertEqual(hexlify(digest), tobytes(hexdigest))
378 # hexdigest does not change the state
379 self.assertEqual(mac.hexdigest(), hexdigest)
380 # hexdigest returns a string
381 self.assertTrue(isinstance(hexdigest, type("digest")))
382
383 def test_verify(self):
384 h = Poly1305.new(key=self.key, cipher=AES)
385 mac = h.digest()
386 h.verify(mac)
387 wrong_mac = strxor_c(mac, 255)
388 self.assertRaises(ValueError, h.verify, wrong_mac)
389
390 def test_hexverify(self):
391 h = Poly1305.new(key=self.key, cipher=AES)
392 mac = h.hexdigest()
393 h.hexverify(mac)
394 self.assertRaises(ValueError, h.hexverify, "4556")
395
396 def test_bytearray(self):
397
398 data = b"\x00\x01\x02"
399 h0 = Poly1305.new(key=self.key, data=data, cipher=AES)
400 d_ref = h0.digest()
401
402 # Data and key can be a bytearray (during initialization)
403 key_ba = bytearray(self.key)
404 data_ba = bytearray(data)
405
406 h1 = Poly1305.new(key=self.key, data=data, cipher=AES, nonce=h0.nonce)
407 h2 = Poly1305.new(key=key_ba, data=data_ba, cipher=AES, nonce=h0.nonce)
408 key_ba[:1] = b'\xFF'
409 data_ba[:1] = b'\xEE'
410
411 self.assertEqual(h1.digest(), d_ref)
412 self.assertEqual(h2.digest(), d_ref)
413
414 # Data can be a bytearray (during operation)
415 data_ba = bytearray(data)
416
417 h1 = Poly1305.new(key=self.key, cipher=AES)
418 h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
419 h1.update(data)
420 h2.update(data_ba)
421 data_ba[:1] = b'\xFF'
422
423 self.assertEqual(h1.digest(), h2.digest())
424
425 def test_memoryview(self):
426
427 data = b"\x00\x01\x02"
428
429 def get_mv_ro(data):
430 return memoryview(data)
431
432 def get_mv_rw(data):
433 return memoryview(bytearray(data))
434
435 for get_mv in (get_mv_ro, get_mv_rw):
436
437 # Data and key can be a memoryview (during initialization)
438 key_mv = get_mv(self.key)
439 data_mv = get_mv(data)
440
441 h1 = Poly1305.new(key=self.key, data=data, cipher=AES)
442 h2 = Poly1305.new(key=key_mv, data=data_mv, cipher=AES,
443 nonce=h1.nonce)
444 if not data_mv.readonly:
445 data_mv[:1] = b'\xFF'
446 key_mv[:1] = b'\xFF'
447
448 self.assertEqual(h1.digest(), h2.digest())
449
450 # Data can be a memoryview (during operation)
451 data_mv = get_mv(data)
452
453 h1 = Poly1305.new(key=self.key, cipher=AES)
454 h2 = Poly1305.new(key=self.key, cipher=AES, nonce=h1.nonce)
455 h1.update(data)
456 h2.update(data_mv)
457 if not data_mv.readonly:
458 data_mv[:1] = b'\xFF'
459
460 self.assertEqual(h1.digest(), h2.digest())
461
462
463 class Poly1305Test_ChaCha20(unittest.TestCase):
464
465 key = b'\x11' * 32
466
467 def test_new_positive(self):
468 data = b'r' * 100
469
470 h1 = Poly1305.new(key=self.key, cipher=ChaCha20)
471 self.assertEqual(h1.digest_size, 16)
472 self.assertEqual(len(h1.nonce), 12)
473
474 h2 = Poly1305.new(key=self.key, cipher=ChaCha20, nonce = b'8' * 8)
475 self.assertEqual(len(h2.nonce), 8)
476 self.assertEqual(h2.nonce, b'8' * 8)
477
478 def test_new_negative(self):
479
480 self.assertRaises(ValueError, Poly1305.new, key=self.key, nonce=b'1' * 7, cipher=ChaCha20)
481
482
483 #
484 # make_mac_tests() expect a new() function with signature new(key, data,
485 # **kwargs), and we need to adapt Poly1305's, as it only uses keywords
486 #
487 class Poly1305_New(object):
488
489 @staticmethod
490 def new(key, *data, **kwds):
491 _kwds = dict(kwds)
492 if len(data) == 1:
493 _kwds['data'] = data[0]
494 _kwds['key'] = key
495 return Poly1305.new(**_kwds)
496
497
498 class Poly1305_Basic(object):
499
500 @staticmethod
501 def new(key, *data, **kwds):
502 from Crypto.Hash.Poly1305 import Poly1305_MAC
503
504 if len(data) == 1:
505 msg = data[0]
506 else:
507 msg = None
508
509 return Poly1305_MAC(key[:16], key[16:], msg)
510
511
512 class Poly1305AES_MC(unittest.TestCase):
513
514 def runTest(self):
515 tag = unhexlify(b"fb447350c4e868c52ac3275cf9d4327e")
516
517 msg = b''
518 for msg_len in range(5000 + 1):
519 key = tag + strxor_c(tag, 0xFF)
520 nonce = tag[::-1]
521 if msg_len > 0:
522 msg = msg + tobytes(tag[0])
523 auth = Poly1305.new(key=key, nonce=nonce, cipher=AES, data=msg)
524 tag = auth.digest()
525
526 # Compare against output of original DJB's poly1305aes-20050218
527 self.assertEqual("CDFA436DDD629C7DC20E1128530BAED2", auth.hexdigest().upper())
528
529
530 def get_tests(config={}):
531 tests = make_mac_tests(Poly1305_Basic, "Poly1305", test_data_basic)
532 tests += make_mac_tests(Poly1305_New, "Poly1305", test_data_aes)
533 tests += make_mac_tests(Poly1305_New, "Poly1305", test_data_chacha20)
534 tests += [ Poly1305AES_MC() ]
535 tests += list_test_cases(Poly1305Test_AES)
536 tests += list_test_cases(Poly1305Test_ChaCha20)
537 return tests
538
539
540 if __name__ == '__main__':
541 suite = lambda: unittest.TestSuite(get_tests())
542 unittest.main(defaultTest='suite')