| 1 | """Make unit test for poly1305_load_r()""" |
| 2 | |
| 3 | import struct |
| 4 | |
| 5 | from common import counter, make_main, split64 |
| 6 | |
| 7 | |
| 8 | def make_test(secret): |
| 9 | |
| 10 | assert len(secret) == 16 |
| 11 | |
| 12 | clamped = bytearray(secret) |
| 13 | for idx in 3, 7, 11, 15: |
| 14 | clamped[idx] &= 15 |
| 15 | for idx in 4, 8, 12: |
| 16 | clamped[idx] &= 252 |
| 17 | |
| 18 | split = struct.unpack('<IIII', bytes(clamped)) |
| 19 | r_out = [ "0x%08xUL" % x for x in split ] |
| 20 | rr_out = [ "0x%08xUL" % ((x>>2)*5) for x in split ] |
| 21 | |
| 22 | print("") |
| 23 | print("void test_%d(void) {" % next(counter)) |
| 24 | print(" uint8_t secret[16] = {" + ",".join([str(x) for x in secret]) + "};") |
| 25 | print(" uint32_t r[5] = { 0 };") |
| 26 | print(" uint32_t rr[5] = { 0 };") |
| 27 | print(" const uint32_t expected_r[5] = {" + ", ".join(r_out) + "};") |
| 28 | print(" const uint32_t expected_rr[5] = {" + ", ".join(rr_out) + "};") |
| 29 | print("") |
| 30 | print(" poly1305_load_r(r, rr, secret);") |
| 31 | print(" assert(memcmp(r, expected_r, sizeof(r)) == 0);") |
| 32 | print(" assert(memcmp(rr, expected_rr, sizeof(rr)) == 0);") |
| 33 | print("}") |
| 34 | print("") |
| 35 | |
| 36 | |
| 37 | print("#ifdef NDEBUG") |
| 38 | print("#undef NDEBUG") |
| 39 | print("#endif") |
| 40 | print("#include <assert.h>") |
| 41 | print("#include <string.h>") |
| 42 | print("#include <stdint.h>") |
| 43 | print("#include <stdio.h>") |
| 44 | print() |
| 45 | print("void poly1305_load_r(uint32_t r[4], uint32_t rr[4], const uint8_t secret[]);") |
| 46 | |
| 47 | make_test(b"\xaa" * 16) |
| 48 | make_test(b"\xcc" * 16) |
| 49 | make_test(b"\xff" * 16) |
| 50 | make_test(b"12\x00kjskskjp39027") |
| 51 | make_main() |