summaryrefslogtreecommitdiff
path: root/nacl/crypto_stream/try.c
diff options
context:
space:
mode:
Diffstat (limited to 'nacl/crypto_stream/try.c')
-rw-r--r--nacl/crypto_stream/try.c124
1 files changed, 124 insertions, 0 deletions
diff --git a/nacl/crypto_stream/try.c b/nacl/crypto_stream/try.c
new file mode 100644
index 00000000..9a36d760
--- /dev/null
+++ b/nacl/crypto_stream/try.c
@@ -0,0 +1,124 @@
1/*
2 * crypto_stream/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
6
7#include <stdlib.h>
8#include "crypto_stream.h"
9
10extern unsigned char *alignedcalloc(unsigned long long);
11
12const char *primitiveimplementation = crypto_stream_IMPLEMENTATION;
13
14#define MAXTEST_BYTES 10000
15#define CHECKSUM_BYTES 4096
16#define TUNE_BYTES 1536
17
18static unsigned char *k;
19static unsigned char *n;
20static unsigned char *m;
21static unsigned char *c;
22static unsigned char *s;
23static unsigned char *k2;
24static unsigned char *n2;
25static unsigned char *m2;
26static unsigned char *c2;
27static unsigned char *s2;
28
29void preallocate(void)
30{
31}
32
33void allocate(void)
34{
35 k = alignedcalloc(crypto_stream_KEYBYTES);
36 n = alignedcalloc(crypto_stream_NONCEBYTES);
37 m = alignedcalloc(MAXTEST_BYTES);
38 c = alignedcalloc(MAXTEST_BYTES);
39 s = alignedcalloc(MAXTEST_BYTES);
40 k2 = alignedcalloc(crypto_stream_KEYBYTES);
41 n2 = alignedcalloc(crypto_stream_NONCEBYTES);
42 m2 = alignedcalloc(MAXTEST_BYTES);
43 c2 = alignedcalloc(MAXTEST_BYTES);
44 s2 = alignedcalloc(MAXTEST_BYTES);
45}
46
47void predoit(void)
48{
49}
50
51void doit(void)
52{
53 crypto_stream_xor(c,m,TUNE_BYTES,n,k);
54}
55
56char checksum[crypto_stream_KEYBYTES * 2 + 1];
57
58const char *checksum_compute(void)
59{
60 long long i;
61 long long j;
62
63 for (i = 0;i < CHECKSUM_BYTES;++i) {
64 long long mlen = i;
65 long long clen = i;
66 long long slen = i;
67 long long klen = crypto_stream_KEYBYTES;
68 long long nlen = crypto_stream_NONCEBYTES;
69 for (j = -16;j < 0;++j) m[j] = random();
70 for (j = -16;j < 0;++j) c[j] = random();
71 for (j = -16;j < 0;++j) s[j] = random();
72 for (j = -16;j < 0;++j) n[j] = random();
73 for (j = -16;j < 0;++j) k[j] = random();
74 for (j = mlen;j < mlen + 16;++j) m[j] = random();
75 for (j = clen;j < clen + 16;++j) c[j] = random();
76 for (j = slen;j < slen + 16;++j) s[j] = random();
77 for (j = nlen;j < nlen + 16;++j) n[j] = random();
78 for (j = klen;j < klen + 16;++j) k[j] = random();
79 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
80 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
81 for (j = -16;j < slen + 16;++j) s2[j] = s[j];
82 for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
83 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
84
85 crypto_stream_xor(c,m,mlen,n,k);
86
87 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_stream_xor overwrites m";
88 for (j = -16;j < slen + 16;++j) if (s[j] != s2[j]) return "crypto_stream_xor overwrites s";
89 for (j = -16;j < nlen + 16;++j) if (n[j] != n2[j]) return "crypto_stream_xor overwrites n";
90 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_stream_xor overwrites k";
91 for (j = -16;j < 0;++j) if (c[j] != c2[j]) return "crypto_stream_xor writes before output";
92 for (j = clen;j < clen + 16;++j) if (c[j] != c2[j]) return "crypto_stream_xor writes after output";
93
94 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
95
96 crypto_stream(s,slen,n,k);
97
98 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_stream overwrites m";
99 for (j = -16;j < clen + 16;++j) if (c[j] != c2[j]) return "crypto_stream overwrites c";
100 for (j = -16;j < nlen + 16;++j) if (n[j] != n2[j]) return "crypto_stream overwrites n";
101 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_stream overwrites k";
102 for (j = -16;j < 0;++j) if (s[j] != s2[j]) return "crypto_stream writes before output";
103 for (j = slen;j < slen + 16;++j) if (s[j] != s2[j]) return "crypto_stream writes after output";
104
105 for (j = 0;j < mlen;++j)
106 if ((s[j] ^ m[j]) != c[j]) return "crypto_stream_xor does not match crypto_stream";
107
108 for (j = 0;j < clen;++j) k[j % klen] ^= c[j];
109 crypto_stream_xor(m,c,clen,n,k);
110 crypto_stream(s,slen,n,k);
111 for (j = 0;j < mlen;++j)
112 if ((s[j] ^ m[j]) != c[j]) return "crypto_stream_xor does not match crypto_stream";
113 for (j = 0;j < mlen;++j) n[j % nlen] ^= m[j];
114 m[mlen] = 0;
115 }
116
117 for (i = 0;i < crypto_stream_KEYBYTES;++i) {
118 checksum[2 * i] = "0123456789abcdef"[15 & (k[i] >> 4)];
119 checksum[2 * i + 1] = "0123456789abcdef"[15 & k[i]];
120 }
121 checksum[2 * i] = 0;
122
123 return 0;
124}