Changes to update Tomato RAF.
[tomato.git] / release / src / router / dnscrypt / src / libnacl / crypto_core / try.c
blob6f5534e7d73284c441209f0718234c4a93c0ced3
1 /*
2 * crypto_core/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_core.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_core_IMPLEMENTATION;
15 static unsigned char *h;
16 static unsigned char *n;
17 static unsigned char *k;
18 static unsigned char *c;
19 static unsigned char *h2;
20 static unsigned char *n2;
21 static unsigned char *k2;
22 static unsigned char *c2;
24 #define hlen crypto_core_OUTPUTBYTES
25 #define nlen crypto_core_INPUTBYTES
26 #define klen crypto_core_KEYBYTES
27 #define clen crypto_core_CONSTBYTES
29 void preallocate(void)
33 void allocate(void)
35 h = alignedcalloc(hlen);
36 n = alignedcalloc(nlen);
37 k = alignedcalloc(klen);
38 c = alignedcalloc(clen);
39 h2 = alignedcalloc(hlen);
40 n2 = alignedcalloc(nlen + crypto_core_OUTPUTBYTES);
41 k2 = alignedcalloc(klen + crypto_core_OUTPUTBYTES);
42 c2 = alignedcalloc(clen + crypto_core_OUTPUTBYTES);
45 void predoit(void)
49 void doit(void)
51 crypto_core(h,n,k,c);
54 static unsigned char newbyte(void)
56 unsigned long long x;
57 long long j;
58 x = 8675309;
59 for (j = 0;j < hlen;++j) { x += h[j]; x *= x; x += (x >> 31); }
60 for (j = 0;j < nlen;++j) { x += n[j]; x *= x; x += (x >> 31); }
61 for (j = 0;j < klen;++j) { x += k[j]; x *= x; x += (x >> 31); }
62 for (j = 0;j < clen;++j) { x += c[j]; x *= x; x += (x >> 31); }
63 for (j = 0;j < 100;++j) { x += j ; x *= x; x += (x >> 31); }
64 return x;
67 char checksum[hlen * 2 + 1];
69 const char *checksum_compute(void)
71 long long i;
72 long long j;
74 for (i = 0;i < 100;++i) {
75 for (j = -16;j < 0;++j) h[j] = random();
76 for (j = hlen;j < hlen + 16;++j) h[j] = random();
77 for (j = -16;j < hlen + 16;++j) h2[j] = h[j];
78 for (j = -16;j < 0;++j) n[j] = random();
79 for (j = nlen;j < nlen + 16;++j) n[j] = random();
80 for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
81 for (j = -16;j < 0;++j) k[j] = random();
82 for (j = klen;j < klen + 16;++j) k[j] = random();
83 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
84 for (j = -16;j < 0;++j) c[j] = random();
85 for (j = clen;j < clen + 16;++j) c[j] = random();
86 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
87 if (crypto_core(h,n,k,c) != 0) return "crypto_core returns nonzero";
88 for (j = -16;j < 0;++j) if (h2[j] != h[j]) return "crypto_core writes before output";
89 for (j = hlen;j < hlen + 16;++j) if (h2[j] != h[j]) return "crypto_core writes after output";
90 for (j = -16;j < klen + 16;++j) if (k2[j] != k[j]) return "crypto_core writes to k";
91 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_core writes to n";
92 for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_core writes to c";
94 if (crypto_core(n2,n2,k,c) != 0) return "crypto_core returns nonzero";
95 for (j = 0;j < hlen;++j) if (h[j] != n2[j]) return "crypto_core does not handle n overlap";
96 for (j = 0;j < hlen;++j) n2[j] = n[j];
97 if (crypto_core(k2,n2,k2,c) != 0) return "crypto_core returns nonzero";
98 for (j = 0;j < hlen;++j) if (h[j] != k2[j]) return "crypto_core does not handle k overlap";
99 for (j = 0;j < hlen;++j) k2[j] = k[j];
100 if (crypto_core(c2,n2,k2,c2) != 0) return "crypto_core returns nonzero";
101 for (j = 0;j < hlen;++j) if (h[j] != c2[j]) return "crypto_core does not handle c overlap";
102 for (j = 0;j < hlen;++j) c2[j] = c[j];
104 for (j = 0;j < nlen;++j) n[j] = newbyte();
105 if (crypto_core(h,n,k,c) != 0) return "crypto_core returns nonzero";
106 for (j = 0;j < klen;++j) k[j] = newbyte();
107 if (crypto_core(h,n,k,c) != 0) return "crypto_core returns nonzero";
108 for (j = 0;j < clen;++j) c[j] = newbyte();
111 for (i = 0;i < hlen;++i) {
112 checksum[2 * i] = "0123456789abcdef"[15 & (h[i] >> 4)];
113 checksum[2 * i + 1] = "0123456789abcdef"[15 & h[i]];
115 checksum[2 * i] = 0;
116 return 0;