| 1 |
"""Make unit test for poly1305_multiply()""" |
| 2 |
|
| 3 |
import struct |
| 4 |
from hashlib import sha1 |
| 5 |
from binascii import unhexlify |
| 6 |
|
| 7 |
from common import counter, make_main, split32 |
| 8 |
|
| 9 |
|
| 10 |
def make_test(term, secret): |
| 11 |
|
| 12 |
assert term < 2**(32*5) |
| 13 |
assert len(secret) == 16 |
| 14 |
|
| 15 |
|
| 16 |
clamped = bytearray(secret) |
| 17 |
for idx in 3, 7, 11, 15: |
| 18 |
clamped[idx] &= 15 |
| 19 |
for idx in 4, 8, 12: |
| 20 |
clamped[idx] &= 252 |
| 21 |
|
| 22 |
|
| 23 |
r = 0 |
| 24 |
for x in clamped[::-1]: |
| 25 |
r = (r * 256) + x |
| 26 |
|
| 27 |
|
| 28 |
p = 2**130 - 5 |
| 29 |
result = (term * r) % p |
| 30 |
all_results = [] |
| 31 |
while result < 2**131: |
| 32 |
all_results.append(split32(result, 5)) |
| 33 |
result += p |
| 34 |
n_results = len(all_results) |
| 35 |
|
| 36 |
|
| 37 |
h_split = split32(term, 5) |
| 38 |
|
| 39 |
print("") |
| 40 |
print("void test_%d(void) {" % next(counter)) |
| 41 |
print(" uint8_t secret[16] = {" + ",".join([str(x) for x in secret]) + "};") |
| 42 |
print(" uint32_t r[4], rr[4];") |
| 43 |
print(" uint32_t h[5] = {" + ",".join(h_split) + "};") |
| 44 |
print(" int match;"); |
| 45 |
for x in range(n_results): |
| 46 |
y = ",".join(all_results[x]) |
| 47 |
print(" uint32_t expected_h_%d[5] = { %s };" % (x+1, y)) |
| 48 |
print("") |
| 49 |
print(" poly1305_load_r(r, rr, secret);") |
| 50 |
print(" poly1305_multiply(h, r, rr);") |
| 51 |
print(" match = !0;") |
| 52 |
for x in range(n_results): |
| 53 |
print(" match = match && memcmp(h, expected_h_%d, sizeof(h));" % (x+1)) |
| 54 |
print(" assert(match == 0);") |
| 55 |
print("}") |
| 56 |
print("") |
| 57 |
|
| 58 |
|
| 59 |
print("#ifdef NDEBUG") |
| 60 |
print("#undef NDEBUG") |
| 61 |
print("#endif") |
| 62 |
print("#include <assert.h>") |
| 63 |
print("#include <string.h>") |
| 64 |
print("#include <stdint.h>") |
| 65 |
print("#include <stdio.h>") |
| 66 |
print() |
| 67 |
print("void poly1305_load_r(uint32_t r[4], uint32_t rr[4], const uint8_t secret[16]);") |
| 68 |
print("void poly1305_multiply(uint32_t h[5], const uint32_t r[4], const uint32_t rr[4]);") |
| 69 |
|
| 70 |
p = 2**130 - 5 |
| 71 |
|
| 72 |
make_test(0, b"X" * 16) |
| 73 |
make_test(78923723423432, b"\x00" * 16) |
| 74 |
|
| 75 |
make_test(1, b"\x01" + b"\x00" * 15) |
| 76 |
for x in range(0,129,32): |
| 77 |
make_test(2**x, b"\x01" + b"\x00" * 15) |
| 78 |
make_test(1, b"\x00"*12 + b'\x04' + b'\x00'*3) |
| 79 |
|
| 80 |
make_test(p, b"\x01" + b"\x00" * 15) |
| 81 |
|
| 82 |
make_test(2**(32*5)-1, b'\xFF'*16) |
| 83 |
|
| 84 |
for i in range(100): |
| 85 |
prng = sha1(struct.pack('<II', 0, i)).digest() |
| 86 |
h = 0 |
| 87 |
for piece in range(5): |
| 88 |
h = (h << 32) + struct.unpack('<I', prng[piece*4:(piece+1)*4])[0] |
| 89 |
secret = sha1(struct.pack('<II', 1, i)).digest()[:16] |
| 90 |
make_test(h, secret) |
| 91 |
|
| 92 |
make_test(2**128, unhexlify("746869032069730030322d0278746500")) |
| 93 |
|
| 94 |
make_main() |