diff options
Diffstat (limited to 'nacl/crypto_box/try.c')
-rw-r--r-- | nacl/crypto_box/try.c | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/nacl/crypto_box/try.c b/nacl/crypto_box/try.c new file mode 100644 index 00000000..f7029909 --- /dev/null +++ b/nacl/crypto_box/try.c | |||
@@ -0,0 +1,195 @@ | |||
1 | /* | ||
2 | * crypto_box/try.c version 20090118 | ||
3 | * D. J. Bernstein | ||
4 | * Public domain. | ||
5 | */ | ||
6 | |||
7 | #include "crypto_box.h" | ||
8 | |||
9 | extern unsigned char *alignedcalloc(unsigned long long); | ||
10 | |||
11 | const char *primitiveimplementation = crypto_box_IMPLEMENTATION; | ||
12 | |||
13 | #define MAXTEST_BYTES 10000 | ||
14 | #define CHECKSUM_BYTES 4096 | ||
15 | #define TUNE_BYTES 1536 | ||
16 | |||
17 | static unsigned char *ska; | ||
18 | static unsigned char *pka; | ||
19 | static unsigned char *skb; | ||
20 | static unsigned char *pkb; | ||
21 | static unsigned char *s; | ||
22 | static unsigned char *n; | ||
23 | static unsigned char *m; | ||
24 | static unsigned char *c; | ||
25 | static unsigned char *t; | ||
26 | static unsigned char *ska2; | ||
27 | static unsigned char *pka2; | ||
28 | static unsigned char *skb2; | ||
29 | static unsigned char *pkb2; | ||
30 | static unsigned char *s2; | ||
31 | static unsigned char *n2; | ||
32 | static unsigned char *m2; | ||
33 | static unsigned char *c2; | ||
34 | static unsigned char *t2; | ||
35 | |||
36 | #define sklen crypto_box_SECRETKEYBYTES | ||
37 | #define pklen crypto_box_PUBLICKEYBYTES | ||
38 | #define nlen crypto_box_NONCEBYTES | ||
39 | #define slen crypto_box_BEFORENMBYTES | ||
40 | |||
41 | void preallocate(void) | ||
42 | { | ||
43 | } | ||
44 | |||
45 | void allocate(void) | ||
46 | { | ||
47 | ska = alignedcalloc(sklen); | ||
48 | pka = alignedcalloc(pklen); | ||
49 | skb = alignedcalloc(sklen); | ||
50 | pkb = alignedcalloc(pklen); | ||
51 | n = alignedcalloc(nlen); | ||
52 | m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
53 | c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
54 | t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
55 | s = alignedcalloc(slen); | ||
56 | ska2 = alignedcalloc(sklen); | ||
57 | pka2 = alignedcalloc(pklen); | ||
58 | skb2 = alignedcalloc(sklen); | ||
59 | pkb2 = alignedcalloc(pklen); | ||
60 | n2 = alignedcalloc(nlen); | ||
61 | m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
62 | c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
63 | t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES); | ||
64 | s2 = alignedcalloc(slen); | ||
65 | } | ||
66 | |||
67 | void predoit(void) | ||
68 | { | ||
69 | } | ||
70 | |||
71 | void doit(void) | ||
72 | { | ||
73 | crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb); | ||
74 | crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska); | ||
75 | } | ||
76 | |||
77 | char checksum[nlen * 2 + 1]; | ||
78 | |||
79 | const char *checksum_compute(void) | ||
80 | { | ||
81 | long long i; | ||
82 | long long j; | ||
83 | |||
84 | if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero"; | ||
85 | if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero"; | ||
86 | |||
87 | for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0; | ||
88 | |||
89 | for (i = 0;i < CHECKSUM_BYTES;++i) { | ||
90 | long long mlen = i + crypto_box_ZEROBYTES; | ||
91 | long long tlen = i + crypto_box_ZEROBYTES; | ||
92 | long long clen = i + crypto_box_ZEROBYTES; | ||
93 | |||
94 | for (j = -16;j < 0;++j) ska[j] = random(); | ||
95 | for (j = -16;j < 0;++j) skb[j] = random(); | ||
96 | for (j = -16;j < 0;++j) pka[j] = random(); | ||
97 | for (j = -16;j < 0;++j) pkb[j] = random(); | ||
98 | for (j = -16;j < 0;++j) m[j] = random(); | ||
99 | for (j = -16;j < 0;++j) n[j] = random(); | ||
100 | |||
101 | for (j = sklen;j < sklen + 16;++j) ska[j] = random(); | ||
102 | for (j = sklen;j < sklen + 16;++j) skb[j] = random(); | ||
103 | for (j = pklen;j < pklen + 16;++j) pka[j] = random(); | ||
104 | for (j = pklen;j < pklen + 16;++j) pkb[j] = random(); | ||
105 | for (j = mlen;j < mlen + 16;++j) m[j] = random(); | ||
106 | for (j = nlen;j < nlen + 16;++j) n[j] = random(); | ||
107 | |||
108 | for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j]; | ||
109 | for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j]; | ||
110 | for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j]; | ||
111 | for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j]; | ||
112 | for (j = -16;j < mlen + 16;++j) m2[j] = m[j]; | ||
113 | for (j = -16;j < nlen + 16;++j) n2[j] = n[j]; | ||
114 | for (j = -16;j < clen + 16;++j) c2[j] = c[j] = random(); | ||
115 | |||
116 | if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero"; | ||
117 | |||
118 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m"; | ||
119 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n"; | ||
120 | for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output"; | ||
121 | for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output"; | ||
122 | for (j = 0;j < crypto_box_BOXZEROBYTES;++j) | ||
123 | if (c[j] != 0) return "crypto_box does not clear extra bytes"; | ||
124 | |||
125 | for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska"; | ||
126 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb"; | ||
127 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka"; | ||
128 | for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb"; | ||
129 | |||
130 | for (j = -16;j < 0;++j) c[j] = random(); | ||
131 | for (j = clen;j < clen + 16;++j) c[j] = random(); | ||
132 | for (j = -16;j < clen + 16;++j) c2[j] = c[j]; | ||
133 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
134 | |||
135 | if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero"; | ||
136 | |||
137 | for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c"; | ||
138 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n"; | ||
139 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output"; | ||
140 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output"; | ||
141 | for (j = 0;j < crypto_box_ZEROBYTES;++j) | ||
142 | if (t[j] != 0) return "crypto_box_open does not clear extra bytes"; | ||
143 | |||
144 | for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska"; | ||
145 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb"; | ||
146 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka"; | ||
147 | for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb"; | ||
148 | |||
149 | for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match"; | ||
150 | |||
151 | for (j = -16;j < slen + 16;++j) s2[j] = s[j] = random(); | ||
152 | if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero"; | ||
153 | for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk"; | ||
154 | for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk"; | ||
155 | for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output"; | ||
156 | for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output"; | ||
157 | |||
158 | for (j = -16;j < slen + 16;++j) s2[j] = s[j]; | ||
159 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
160 | if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero"; | ||
161 | for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s"; | ||
162 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m"; | ||
163 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n"; | ||
164 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output"; | ||
165 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output"; | ||
166 | for (j = 0;j < crypto_box_BOXZEROBYTES;++j) | ||
167 | if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes"; | ||
168 | for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box"; | ||
169 | |||
170 | if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero"; | ||
171 | |||
172 | for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = random(); | ||
173 | if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero"; | ||
174 | for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s"; | ||
175 | for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m"; | ||
176 | for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n"; | ||
177 | for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output"; | ||
178 | for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output"; | ||
179 | for (j = 0;j < crypto_box_ZEROBYTES;++j) | ||
180 | if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes"; | ||
181 | for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open"; | ||
182 | |||
183 | for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES]; | ||
184 | if (i == 0) m[crypto_box_ZEROBYTES] = 0; | ||
185 | m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES]; | ||
186 | for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES]; | ||
187 | } | ||
188 | |||
189 | for (i = 0;i < nlen;++i) { | ||
190 | checksum[2 * i] = "0123456789abcdef"[15 & (n[i] >> 4)]; | ||
191 | checksum[2 * i + 1] = "0123456789abcdef"[15 & n[i]]; | ||
192 | } | ||
193 | checksum[2 * i] = 0; | ||
194 | return 0; | ||
195 | } | ||