libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_secretbox / try.c
blob0a62949b81c90021c846e763f8f92d758699a335
1 /*
2 * crypto_secretbox/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_secretbox.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_secretbox_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 *t;
24 static unsigned char *k2;
25 static unsigned char *n2;
26 static unsigned char *m2;
27 static unsigned char *c2;
28 static unsigned char *t2;
30 #define klen crypto_secretbox_KEYBYTES
31 #define nlen crypto_secretbox_NONCEBYTES
33 void preallocate(void)
37 void allocate(void)
39 k = alignedcalloc(klen);
40 n = alignedcalloc(nlen);
41 m = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
42 c = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
43 t = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
44 k2 = alignedcalloc(klen);
45 n2 = alignedcalloc(nlen);
46 m2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
47 c2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
48 t2 = alignedcalloc(MAXTEST_BYTES + crypto_secretbox_ZEROBYTES);
51 void predoit(void)
55 void doit(void)
57 crypto_secretbox(c,m,TUNE_BYTES + crypto_secretbox_ZEROBYTES,n,k);
58 crypto_secretbox_open(t,c,TUNE_BYTES + crypto_secretbox_ZEROBYTES,n,k);
61 char checksum[klen * 2 + 1];
63 const char *checksum_compute(void)
65 long long i;
66 long long j;
68 for (j = 0;j < crypto_secretbox_ZEROBYTES;++j) m[j] = 0;
70 for (i = 0;i < CHECKSUM_BYTES;++i) {
71 long long mlen = i + crypto_secretbox_ZEROBYTES;
72 long long tlen = i + crypto_secretbox_ZEROBYTES;
73 long long clen = i + crypto_secretbox_ZEROBYTES;
75 for (j = -16;j < 0;++j) k[j] = rand();
76 for (j = -16;j < 0;++j) n[j] = rand();
77 for (j = -16;j < 0;++j) m[j] = rand();
78 for (j = klen;j < klen + 16;++j) k[j] = rand();
79 for (j = nlen;j < nlen + 16;++j) n[j] = rand();
80 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
81 for (j = -16;j < klen + 16;++j) k2[j] = k[j];
82 for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
83 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
84 for (j = -16;j < clen + 16;++j) c2[j] = c[j] = rand();
86 if (crypto_secretbox(c,m,mlen,n,k) != 0) return "crypto_secretbox returns nonzero";
88 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_secretbox overwrites m";
89 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_secretbox overwrites n";
90 for (j = -16;j < klen + 16;++j) if (k2[j] != k[j]) return "crypto_secretbox overwrites k";
91 for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_secretbox writes before output";
92 for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_secretbox writes after output";
93 for (j = 0;j < crypto_secretbox_BOXZEROBYTES;++j)
94 if (c[j] != 0) return "crypto_secretbox does not clear extra bytes";
96 for (j = -16;j < 0;++j) c[j] = rand();
97 for (j = clen;j < clen + 16;++j) c[j] = rand();
98 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
99 for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
101 if (crypto_secretbox_open(t,c,clen,n,k) != 0) return "crypto_secretbox_open returns nonzero";
103 for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_secretbox_open overwrites c";
104 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_secretbox_open overwrites n";
105 for (j = -16;j < klen + 16;++j) if (k2[j] != k[j]) return "crypto_secretbox_open overwrites k";
106 for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_secretbox_open writes before output";
107 for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_secretbox_open writes after output";
108 for (j = 0;j < crypto_secretbox_ZEROBYTES;++j)
109 if (t[j] != 0) return "crypto_secretbox_open does not clear extra bytes";
111 for (j = 0;j < i;++j) if (t[j] != m[j]) return "plaintext does not match";
113 for (j = 0;j < i;++j)
114 k[j % klen] ^= c[j + crypto_secretbox_BOXZEROBYTES];
115 crypto_secretbox(c,m,mlen,n,k);
116 for (j = 0;j < i;++j)
117 n[j % nlen] ^= c[j + crypto_secretbox_BOXZEROBYTES];
118 crypto_secretbox(c,m,mlen,n,k);
119 if (i == 0) m[crypto_secretbox_ZEROBYTES + 0] = 0;
120 m[crypto_secretbox_ZEROBYTES + i] = m[crypto_secretbox_ZEROBYTES + 0];
121 for (j = 0;j < i;++j)
122 m[j + crypto_secretbox_ZEROBYTES] ^= c[j + crypto_secretbox_BOXZEROBYTES];
125 for (i = 0;i < klen;++i) {
126 checksum[2 * i] = "0123456789abcdef"[15 & (k[i] >> 4)];
127 checksum[2 * i + 1] = "0123456789abcdef"[15 & k[i]];
129 checksum[2 * i] = 0;
130 return 0;