chiark / gitweb /
@@@ extra
[mLib] / hash / t / siphash-mktv.c
1 #include <stdio.h>
2
3 #include "siphash.h"
4
5 #define TAP 0x8299
6
7 static void step(int *rng_inout)
8 {
9   unsigned i;
10   int x = *rng_inout;
11
12   for (i = 0; i < 8; i++) x = (x << 1) ^ ((x >> 31)&TAP);
13   *rng_inout = x;
14 }
15
16 static void fill(int *rng_inout, unsigned char *p, size_t sz)
17   { while (sz--) { *p++ = *rng_inout&0xff; step(rng_inout); } }
18
19 static void dump(const char *name, const unsigned char *p, size_t sz)
20 {
21   size_t i;
22
23   printf("%s = ", name);
24   if (!sz)
25     fputs("\"\"", stdout);
26   else for (i = 0; i < sz; i++) {
27     if (!i || i%4) ;
28     else if (!(i%32)) fputs("\n    ", stdout);
29     else if (!(i%4)) fputc(' ', stdout);
30     printf("%02x", *p++);
31   }
32   fputc('\n', stdout);
33 }
34
35 int main(void)
36 {
37   unsigned char k[16], h[8];
38   unsigned char msg[1024];
39   int rng = 0xe6161f4d;
40   unsigned i;
41
42   for (i = 0; i <= sizeof(msg); i++) {
43     if (i) fputc('\n', stdout);
44     fill(&rng, k, sizeof(k)); dump("k", k, sizeof(k));
45     fill(&rng, msg, i); dump("m", msg, i);
46     siphash(msg, i, k, h, sizeof(h)); dump("h", h, sizeof(h));
47   }
48   return (0);
49 }