| 1 | """Make unit test for poly1305_accumulate()""" |
| 2 | |
| 3 | import struct |
| 4 | from hashlib import sha1 |
| 5 | |
| 6 | |
| 7 | from common import counter, make_main, split32 |
| 8 | |
| 9 | |
| 10 | def make_test(term1, term2): |
| 11 | |
| 12 | assert term1 < 2**(32*5) |
| 13 | assert term2 < 2**(32*5) |
| 14 | |
| 15 | t1_split = split32(term1, 5) |
| 16 | t2_split = split32(term2, 5) |
| 17 | res_split = split32(term1 + term2, 5) |
| 18 | |
| 19 | print("") |
| 20 | print("void test_%d(void) {" % next(counter)) |
| 21 | print(" uint32_t h[5] = {" + ",".join(t1_split) + "};") |
| 22 | print(" uint32_t m[5] = {" + ",".join(t2_split) + "};") |
| 23 | print(" uint32_t w[5] = {" + ",".join(res_split) + "};") |
| 24 | |
| 25 | print(" poly1305_accumulate(h, m);") |
| 26 | print(" assert(0 == memcmp(h, w, sizeof(h)));") |
| 27 | print("}") |
| 28 | print("") |
| 29 | |
| 30 | |
| 31 | print("#ifdef NDEBUG") |
| 32 | print("#undef NDEBUG") |
| 33 | print("#endif") |
| 34 | print("#include <assert.h>") |
| 35 | print("#include <string.h>") |
| 36 | print("#include <stdint.h>") |
| 37 | print("#include <stdio.h>") |
| 38 | print() |
| 39 | print("void poly1305_accumulate(uint32_t h[5], const uint32_t m[5]);") |
| 40 | |
| 41 | make_test(0, 0xFFFFFFFFFFFFFFFFFFF) |
| 42 | make_test(0xFFFFFFFFFFFFFFFFFFF, 0) |
| 43 | make_test(2**(32*5)-2, 1) |
| 44 | make_test(1, 2**(32*5)-2) |
| 45 | |
| 46 | for i in range(100): |
| 47 | prng1 = sha1(struct.pack('<II', 0, i)).digest() |
| 48 | prng2 = sha1(struct.pack('<II', 1, i)).digest() |
| 49 | term1 = term2 = 0 |
| 50 | for piece in range(5): |
| 51 | term1 = (term1 << 32) + struct.unpack('<I', prng1[piece*4:(piece+1)*4])[0] |
| 52 | term2 = (term2 << 32) + struct.unpack('<I', prng2[piece*4:(piece+1)*4])[0] |
| 53 | if term1 + term2 >= 2**(32*5): |
| 54 | continue |
| 55 | make_test(term1, term2) |
| 56 | |
| 57 | make_main() |