Skip to content

Commit 9fc6e03

Browse files
stratosphersipa
andcommitted
[test/crypto] Add Poly1305 python implementation
Co-authored-by: Pieter Wuille <pieter.wuille@gmail.com>
1 parent fec2ca6 commit 9fc6e03

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2022 The Bitcoin Core developers
3+
# Distributed under the MIT software license, see the accompanying
4+
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5+
6+
"""Test-only implementation of Poly1305 authenticator
7+
8+
It is designed for ease of understanding, not performance.
9+
10+
WARNING: This code is slow and trivially vulnerable to side channel attacks. Do not use for
11+
anything but tests.
12+
"""
13+
14+
import unittest
15+
16+
17+
class Poly1305:
18+
"""Class representing a running poly1305 computation."""
19+
MODULUS = 2**130 - 5
20+
21+
def __init__(self, key):
22+
self.r = int.from_bytes(key[:16], 'little') & 0xffffffc0ffffffc0ffffffc0fffffff
23+
self.s = int.from_bytes(key[16:], 'little')
24+
25+
def tag(self, data):
26+
"""Compute the poly1305 tag."""
27+
acc, length = 0, len(data)
28+
for i in range((length + 15) // 16):
29+
chunk = data[i * 16:min(length, (i + 1) * 16)]
30+
val = int.from_bytes(chunk, 'little') + 256**len(chunk)
31+
acc = (self.r * (acc + val)) % Poly1305.MODULUS
32+
return ((acc + self.s) & 0xffffffffffffffffffffffffffffffff).to_bytes(16, 'little')
33+
34+
35+
# Test vectors from RFC7539/8439 consisting of message to be authenticated, 32 byte key and computed 16 byte tag
36+
POLY1305_TESTS = [
37+
# RFC 7539, section 2.5.2.
38+
["43727970746f6772617068696320466f72756d2052657365617263682047726f7570",
39+
"85d6be7857556d337f4452fe42d506a80103808afb0db2fd4abff6af4149f51b",
40+
"a8061dc1305136c6c22b8baf0c0127a9"],
41+
# RFC 7539, section A.3.
42+
["00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
43+
"000000000000000000000000000",
44+
"0000000000000000000000000000000000000000000000000000000000000000",
45+
"00000000000000000000000000000000"],
46+
["416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e747269627"
47+
"5746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465"
48+
"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686"
49+
"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e2022494554"
50+
"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c20737461746"
51+
"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c65"
52+
"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207"
53+
"768696368206172652061646472657373656420746f",
54+
"0000000000000000000000000000000036e5f6b5c5e06070f0efca96227a863e",
55+
"36e5f6b5c5e06070f0efca96227a863e"],
56+
["416e79207375626d697373696f6e20746f20746865204945544620696e74656e6465642062792074686520436f6e747269627"
57+
"5746f7220666f72207075626c69636174696f6e20617320616c6c206f722070617274206f6620616e204945544620496e7465"
58+
"726e65742d4472616674206f722052464320616e6420616e792073746174656d656e74206d6164652077697468696e2074686"
59+
"520636f6e74657874206f6620616e204945544620616374697669747920697320636f6e7369646572656420616e2022494554"
60+
"4620436f6e747269627574696f6e222e20537563682073746174656d656e747320696e636c756465206f72616c20737461746"
61+
"56d656e747320696e20494554462073657373696f6e732c2061732077656c6c206173207772697474656e20616e6420656c65"
62+
"6374726f6e696320636f6d6d756e69636174696f6e73206d61646520617420616e792074696d65206f7220706c6163652c207"
63+
"768696368206172652061646472657373656420746f",
64+
"36e5f6b5c5e06070f0efca96227a863e00000000000000000000000000000000",
65+
"f3477e7cd95417af89a6b8794c310cf0"],
66+
["2754776173206272696c6c69672c20616e642074686520736c6974687920746f7665730a446964206779726520616e6420676"
67+
"96d626c6520696e2074686520776162653a0a416c6c206d696d737920776572652074686520626f726f676f7665732c0a416e"
68+
"6420746865206d6f6d65207261746873206f757467726162652e",
69+
"1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0",
70+
"4541669a7eaaee61e708dc7cbcc5eb62"],
71+
["ffffffffffffffffffffffffffffffff",
72+
"0200000000000000000000000000000000000000000000000000000000000000",
73+
"03000000000000000000000000000000"],
74+
["02000000000000000000000000000000",
75+
"02000000000000000000000000000000ffffffffffffffffffffffffffffffff",
76+
"03000000000000000000000000000000"],
77+
["fffffffffffffffffffffffffffffffff0ffffffffffffffffffffffffffffff11000000000000000000000000000000",
78+
"0100000000000000000000000000000000000000000000000000000000000000",
79+
"05000000000000000000000000000000"],
80+
["fffffffffffffffffffffffffffffffffbfefefefefefefefefefefefefefefe01010101010101010101010101010101",
81+
"0100000000000000000000000000000000000000000000000000000000000000",
82+
"00000000000000000000000000000000"],
83+
["fdffffffffffffffffffffffffffffff",
84+
"0200000000000000000000000000000000000000000000000000000000000000",
85+
"faffffffffffffffffffffffffffffff"],
86+
["e33594d7505e43b900000000000000003394d7505e4379cd01000000000000000000000000000000000000000000000001000000000000000000000000000000",
87+
"0100000000000000040000000000000000000000000000000000000000000000",
88+
"14000000000000005500000000000000"],
89+
["e33594d7505e43b900000000000000003394d7505e4379cd010000000000000000000000000000000000000000000000",
90+
"0100000000000000040000000000000000000000000000000000000000000000",
91+
"13000000000000000000000000000000"],
92+
]
93+
94+
95+
class TestFrameworkPoly1305(unittest.TestCase):
96+
def test_poly1305(self):
97+
"""Poly1305 test vectors."""
98+
for test_vector in POLY1305_TESTS:
99+
hex_message, hex_key, hex_tag = test_vector
100+
message = bytes.fromhex(hex_message)
101+
key = bytes.fromhex(hex_key)
102+
tag = bytes.fromhex(hex_tag)
103+
comp_tag = Poly1305(key).tag(message)
104+
self.assertEqual(tag, comp_tag)

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"key",
8080
"messages",
8181
"crypto.muhash",
82+
"crypto.poly1305",
8283
"crypto.ripemd160",
8384
"script",
8485
"segwit_addr",

0 commit comments

Comments
 (0)