summaryrefslogtreecommitdiff
path: root/nacl/crypto_core/try.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
committerirungentoo <irungentoo@gmail.com>2013-07-02 09:53:34 -0400
commite2967396ac73cb7410787886cdaf072a184ffc49 (patch)
tree527a74d25a4a0705fc641994fd35bfab22662034 /nacl/crypto_core/try.c
parent8928c817df345f29aa0b194743595aa11bd6a8ba (diff)
Added NaCl crypto library.
Diffstat (limited to 'nacl/crypto_core/try.c')
-rw-r--r--nacl/crypto_core/try.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/nacl/crypto_core/try.c b/nacl/crypto_core/try.c
new file mode 100644
index 00000000..7eb1c677
--- /dev/null
+++ b/nacl/crypto_core/try.c
@@ -0,0 +1,116 @@
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
10extern unsigned char *alignedcalloc(unsigned long long);
11
12const char *primitiveimplementation = crypto_core_IMPLEMENTATION;
13
14static unsigned char *h;
15static unsigned char *n;
16static unsigned char *k;
17static unsigned char *c;
18static unsigned char *h2;
19static unsigned char *n2;
20static unsigned char *k2;
21static 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
28void preallocate(void)
29{
30}
31
32void 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
44void predoit(void)
45{
46}
47
48void doit(void)
49{
50 crypto_core(h,n,k,c);
51}
52
53static 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
66char checksum[hlen * 2 + 1];
67
68const 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}