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