libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_stream / try.c
blob3af36e3e3d99ff53f2934116927bd2c5dd4f0d44
1 /*
2 * crypto_stream/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_stream.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_stream_IMPLEMENTATION;
15 #define MAXTEST_BYTES 10000
16 #define CHECKSUM_BYTES 4096
17 #define TUNE_BYTES 1536
19 static unsigned char *k;
20 static unsigned char *n;
21 static unsigned char *m;
22 static unsigned char *c;
23 static unsigned char *s;
24 static unsigned char *k2;
25 static unsigned char *n2;
26 static unsigned char *m2;
27 static unsigned char *c2;
28 static unsigned char *s2;
30 void preallocate(void)
34 void allocate(void)
36 k = alignedcalloc(crypto_stream_KEYBYTES);
37 n = alignedcalloc(crypto_stream_NONCEBYTES);
38 m = alignedcalloc(MAXTEST_BYTES);
39 c = alignedcalloc(MAXTEST_BYTES);
40 s = alignedcalloc(MAXTEST_BYTES);
41 k2 = alignedcalloc(crypto_stream_KEYBYTES);
42 n2 = alignedcalloc(crypto_stream_NONCEBYTES);
43 m2 = alignedcalloc(MAXTEST_BYTES);
44 c2 = alignedcalloc(MAXTEST_BYTES);
45 s2 = alignedcalloc(MAXTEST_BYTES);
48 void predoit(void)
52 void doit(void)
54 crypto_stream_xor(c,m,TUNE_BYTES,n,k);
57 char checksum[crypto_stream_KEYBYTES * 2 + 1];
59 const char *checksum_compute(void)
61 long long i;
62 long long j;
64 for (i = 0;i < CHECKSUM_BYTES;++i) {
65 long long mlen = i;
66 long long clen = i;
67 long long slen = i;
68 long long klen = crypto_stream_KEYBYTES;
69 long long nlen = crypto_stream_NONCEBYTES;
70 for (j = -16;j < 0;++j) m[j] = rand();
71 for (j = -16;j < 0;++j) c[j] = rand();
72 for (j = -16;j < 0;++j) s[j] = rand();
73 for (j = -16;j < 0;++j) n[j] = rand();
74 for (j = -16;j < 0;++j) k[j] = rand();
75 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
76 for (j = clen;j < clen + 16;++j) c[j] = rand();
77 for (j = slen;j < slen + 16;++j) s[j] = rand();
78 for (j = nlen;j < nlen + 16;++j) n[j] = rand();
79 for (j = klen;j < klen + 16;++j) k[j] = rand();
80 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
81 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
82 for (j = -16;j < slen + 16;++j) s2[j] = s[j];
83 for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
84 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
86 crypto_stream_xor(c,m,mlen,n,k);
88 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_stream_xor overwrites m";
89 for (j = -16;j < slen + 16;++j) if (s[j] != s2[j]) return "crypto_stream_xor overwrites s";
90 for (j = -16;j < nlen + 16;++j) if (n[j] != n2[j]) return "crypto_stream_xor overwrites n";
91 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_stream_xor overwrites k";
92 for (j = -16;j < 0;++j) if (c[j] != c2[j]) return "crypto_stream_xor writes before output";
93 for (j = clen;j < clen + 16;++j) if (c[j] != c2[j]) return "crypto_stream_xor writes after output";
95 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
97 crypto_stream(s,slen,n,k);
99 for (j = -16;j < mlen + 16;++j) if (m[j] != m2[j]) return "crypto_stream overwrites m";
100 for (j = -16;j < clen + 16;++j) if (c[j] != c2[j]) return "crypto_stream overwrites c";
101 for (j = -16;j < nlen + 16;++j) if (n[j] != n2[j]) return "crypto_stream overwrites n";
102 for (j = -16;j < klen + 16;++j) if (k[j] != k2[j]) return "crypto_stream overwrites k";
103 for (j = -16;j < 0;++j) if (s[j] != s2[j]) return "crypto_stream writes before output";
104 for (j = slen;j < slen + 16;++j) if (s[j] != s2[j]) return "crypto_stream writes after output";
106 for (j = 0;j < mlen;++j)
107 if ((s[j] ^ m[j]) != c[j]) return "crypto_stream_xor does not match crypto_stream";
109 for (j = 0;j < clen;++j) k[j % klen] ^= c[j];
110 crypto_stream_xor(m,c,clen,n,k);
111 crypto_stream(s,slen,n,k);
112 for (j = 0;j < mlen;++j)
113 if ((s[j] ^ m[j]) != c[j]) return "crypto_stream_xor does not match crypto_stream";
114 for (j = 0;j < mlen;++j) n[j % nlen] ^= m[j];
115 m[mlen] = 0;
118 for (i = 0;i < crypto_stream_KEYBYTES;++i) {
119 checksum[2 * i] = "0123456789abcdef"[15 & (k[i] >> 4)];
120 checksum[2 * i + 1] = "0123456789abcdef"[15 & k[i]];
122 checksum[2 * i] = 0;
124 return 0;