libsodium: Needed for Dnscrypto-proxy Release 1.3.0
[tomato.git] / release / src / router / libsodium / src / libsodium / crypto_box / try.c
blobb046b411bcb34262d58d088c1cf888ca4b777b4c
1 /*
2 * crypto_box/try.c version 20090118
3 * D. J. Bernstein
4 * Public domain.
5 */
7 #include <stdlib.h>
8 #include "crypto_box.h"
9 #include "windows/windows-quirks.h"
11 extern unsigned char *alignedcalloc(unsigned long long);
13 const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
15 #define MAXTEST_BYTES 10000
16 #define CHECKSUM_BYTES 4096
17 #define TUNE_BYTES 1536
19 static unsigned char *ska;
20 static unsigned char *pka;
21 static unsigned char *skb;
22 static unsigned char *pkb;
23 static unsigned char *s;
24 static unsigned char *n;
25 static unsigned char *m;
26 static unsigned char *c;
27 static unsigned char *t;
28 static unsigned char *ska2;
29 static unsigned char *pka2;
30 static unsigned char *skb2;
31 static unsigned char *pkb2;
32 static unsigned char *s2;
33 static unsigned char *n2;
34 static unsigned char *m2;
35 static unsigned char *c2;
36 static unsigned char *t2;
38 #define sklen crypto_box_SECRETKEYBYTES
39 #define pklen crypto_box_PUBLICKEYBYTES
40 #define nlen crypto_box_NONCEBYTES
41 #define slen crypto_box_BEFORENMBYTES
43 void preallocate(void)
47 void allocate(void)
49 ska = alignedcalloc(sklen);
50 pka = alignedcalloc(pklen);
51 skb = alignedcalloc(sklen);
52 pkb = alignedcalloc(pklen);
53 n = alignedcalloc(nlen);
54 m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
55 c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
56 t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
57 s = alignedcalloc(slen);
58 ska2 = alignedcalloc(sklen);
59 pka2 = alignedcalloc(pklen);
60 skb2 = alignedcalloc(sklen);
61 pkb2 = alignedcalloc(pklen);
62 n2 = alignedcalloc(nlen);
63 m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
64 c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
65 t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
66 s2 = alignedcalloc(slen);
69 void predoit(void)
73 void doit(void)
75 crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb);
76 crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska);
79 char checksum[nlen * 2 + 1];
81 const char *checksum_compute(void)
83 long long i;
84 long long j;
86 if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero";
87 if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero";
89 for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0;
91 for (i = 0;i < CHECKSUM_BYTES;++i) {
92 long long mlen = i + crypto_box_ZEROBYTES;
93 long long tlen = i + crypto_box_ZEROBYTES;
94 long long clen = i + crypto_box_ZEROBYTES;
96 for (j = -16;j < 0;++j) ska[j] = rand();
97 for (j = -16;j < 0;++j) skb[j] = rand();
98 for (j = -16;j < 0;++j) pka[j] = rand();
99 for (j = -16;j < 0;++j) pkb[j] = rand();
100 for (j = -16;j < 0;++j) m[j] = rand();
101 for (j = -16;j < 0;++j) n[j] = rand();
103 for (j = sklen;j < sklen + 16;++j) ska[j] = rand();
104 for (j = sklen;j < sklen + 16;++j) skb[j] = rand();
105 for (j = pklen;j < pklen + 16;++j) pka[j] = rand();
106 for (j = pklen;j < pklen + 16;++j) pkb[j] = rand();
107 for (j = mlen;j < mlen + 16;++j) m[j] = rand();
108 for (j = nlen;j < nlen + 16;++j) n[j] = rand();
110 for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j];
111 for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j];
112 for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j];
113 for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j];
114 for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
115 for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
116 for (j = -16;j < clen + 16;++j) c2[j] = c[j] = rand();
118 if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero";
120 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m";
121 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n";
122 for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output";
123 for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output";
124 for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
125 if (c[j] != 0) return "crypto_box does not clear extra bytes";
127 for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska";
128 for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb";
129 for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka";
130 for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb";
132 for (j = -16;j < 0;++j) c[j] = rand();
133 for (j = clen;j < clen + 16;++j) c[j] = rand();
134 for (j = -16;j < clen + 16;++j) c2[j] = c[j];
135 for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
137 if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero";
139 for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c";
140 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n";
141 for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output";
142 for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output";
143 for (j = 0;j < crypto_box_ZEROBYTES;++j)
144 if (t[j] != 0) return "crypto_box_open does not clear extra bytes";
146 for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska";
147 for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb";
148 for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka";
149 for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb";
151 for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match";
153 for (j = -16;j < slen + 16;++j) s2[j] = s[j] = rand();
154 if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero";
155 for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk";
156 for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk";
157 for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output";
158 for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output";
160 for (j = -16;j < slen + 16;++j) s2[j] = s[j];
161 for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
162 if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero";
163 for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s";
164 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m";
165 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n";
166 for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output";
167 for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output";
168 for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
169 if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes";
170 for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box";
172 if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero";
174 for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
175 if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero";
176 for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s";
177 for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m";
178 for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n";
179 for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output";
180 for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output";
181 for (j = 0;j < crypto_box_ZEROBYTES;++j)
182 if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes";
183 for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open";
185 for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES];
186 if (i == 0) m[crypto_box_ZEROBYTES] = 0;
187 m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES];
188 for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES];
191 for (i = 0;i < nlen;++i) {
192 checksum[2 * i] = "0123456789abcdef"[15 & (n[i] >> 4)];
193 checksum[2 * i + 1] = "0123456789abcdef"[15 & n[i]];
195 checksum[2 * i] = 0;
196 return 0;