-
Notifications
You must be signed in to change notification settings - Fork 79
/
_crypto-test_sign_ed25519.inc
325 lines (310 loc) · 13.8 KB
/
_crypto-test_sign_ed25519.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/* ----- sign/ed25519, derived from supercop/crypto_sign/try.c */
/*
derived from djb work from lib25519/libntruprime
mj modifications:
- rename files to test-crypto.c and _crypto_<>.<>.inc
- fix compiler warnings
- include crypto.h
- use less rounds for valgrind test
- reformat using clang-format
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "crypto.h"
#define fail ((ok = 0), printf)
static const char *sign_ed25519_checksums[] = {
"ce11fd7c1eac4dd0bc5eec49b26ad1e91aef696fae50ce377dbd806dc394da01",
"2ed857f17c917a8185e6c296303a11772ae45683a5e7cb5b095489bad65fffde",
};
static int (*crypto_sign_keypair)(unsigned char *, unsigned char *);
static int (*crypto_sign)(unsigned char *, unsigned long long *,
const unsigned char *, unsigned long long,
const unsigned char *);
static int (*crypto_sign_open)(unsigned char *, unsigned long long *,
const unsigned char *, unsigned long long,
const unsigned char *);
#define crypto_sign_SECRETKEYBYTES crypto_sign_ed25519_SECRETKEYBYTES
#define crypto_sign_PUBLICKEYBYTES crypto_sign_ed25519_PUBLICKEYBYTES
#define crypto_sign_BYTES crypto_sign_ed25519_BYTES
static void *storage_sign_ed25519_p;
static unsigned char *test_sign_ed25519_p;
static void *storage_sign_ed25519_s;
static unsigned char *test_sign_ed25519_s;
static void *storage_sign_ed25519_m;
static unsigned char *test_sign_ed25519_m;
static void *storage_sign_ed25519_c;
static unsigned char *test_sign_ed25519_c;
static void *storage_sign_ed25519_t;
static unsigned char *test_sign_ed25519_t;
static void *storage_sign_ed25519_p2;
static unsigned char *test_sign_ed25519_p2;
static void *storage_sign_ed25519_s2;
static unsigned char *test_sign_ed25519_s2;
static void *storage_sign_ed25519_m2;
static unsigned char *test_sign_ed25519_m2;
static void *storage_sign_ed25519_c2;
static unsigned char *test_sign_ed25519_c2;
static void *storage_sign_ed25519_t2;
static unsigned char *test_sign_ed25519_t2;
static void test_sign_ed25519_impl(long long impl) {
unsigned char *p = test_sign_ed25519_p;
unsigned char *s = test_sign_ed25519_s;
unsigned char *m = test_sign_ed25519_m;
unsigned char *c = test_sign_ed25519_c;
unsigned char *t = test_sign_ed25519_t;
unsigned char *p2 = test_sign_ed25519_p2;
unsigned char *s2 = test_sign_ed25519_s2;
unsigned char *m2 = test_sign_ed25519_m2;
unsigned char *c2 = test_sign_ed25519_c2;
unsigned char *t2 = test_sign_ed25519_t2;
unsigned long long plen = crypto_sign_PUBLICKEYBYTES;
unsigned long long slen = crypto_sign_SECRETKEYBYTES;
unsigned long long mlen;
unsigned long long clen;
unsigned long long tlen;
if (targetn && atol(targetn) != impl) return;
crypto_sign_keypair = crypto_sign_ed25519_keypair;
crypto_sign = crypto_sign_ed25519;
crypto_sign_open = crypto_sign_ed25519_open;
for (long long checksumbig = 0; checksumbig < 2; ++checksumbig) {
long long loops = checksumbig ? 64 : 8;
long long maxtest = checksumbig ? 4096 : 128;
if (checksumbig && valgrind) break;
checksum_clear();
for (long long loop = 0; loop < loops; ++loop) {
int result;
mlen = myrandom() % (maxtest + 1);
output_prepare(p2, p, plen);
output_prepare(s2, s, slen);
crypto_sign_keypair(p, s);
public(p, plen);
public(s, slen);
checksum(p, plen);
checksum(s, slen);
output_compare(p2, p, plen, "crypto_sign_keypair");
output_compare(s2, s, slen, "crypto_sign_keypair");
clen = mlen + crypto_sign_BYTES;
output_prepare(c2, c, clen);
input_prepare(m2, m, mlen);
memcpy(s2, s, slen);
double_canary(s2, s, slen);
secret(m, mlen);
secret(s, slen);
crypto_sign(c, &clen, m, mlen, s);
public(m, mlen);
public(s, slen);
public(c, clen);
if (clen < mlen)
fail(
"failure: crypto_sign returns smaller output than input\n");
if (clen > mlen + crypto_sign_BYTES)
fail("failure: crypto_sign returns more than crypto_sign_BYTES "
"extra bytes\n");
checksum(c, clen);
output_compare(c2, c, clen, "crypto_sign");
input_compare(m2, m, mlen, "crypto_sign");
input_compare(s2, s, slen, "crypto_sign");
tlen = clen;
output_prepare(t2, t, tlen);
memcpy(c2, c, clen);
double_canary(c2, c, clen);
memcpy(p2, p, plen);
double_canary(p2, p, plen);
public(c, clen);
public(p, plen);
result = crypto_sign_open(t, &tlen, c, clen, p);
public(&result, sizeof result);
if (result != 0)
fail("failure: crypto_sign_open returns nonzero\n");
public(c, clen);
public(p, plen);
public(t, tlen);
if (tlen != mlen)
fail("failure: crypto_sign_open does not match mlen\n");
if (memcmp(t, m, mlen) != 0)
fail("failure: crypto_sign_open does not match m\n");
checksum(t, tlen);
output_compare(t2, t, clen, "crypto_sign_open");
input_compare(c2, c, clen, "crypto_sign_open");
input_compare(p2, p, plen, "crypto_sign_open");
double_canary(t2, t, tlen);
double_canary(c2, c, clen);
double_canary(p2, p, plen);
public(c2, clen);
public(p2, plen);
result = crypto_sign_open(t2, &tlen, c2, clen, p2);
public(&result, sizeof result);
if (result != 0)
fail("failure: crypto_sign_open returns nonzero\n");
public(c2, clen);
public(p2, plen);
public(t2, tlen);
if (memcmp(t2, t, tlen) != 0)
fail("failure: crypto_sign_open is nondeterministic\n");
double_canary(t2, t, tlen);
double_canary(c2, c, clen);
double_canary(p2, p, plen);
public(c2, clen);
public(p, plen);
result = crypto_sign_open(c2, &tlen, c2, clen, p);
public(&result, sizeof result);
if (result != 0)
fail("failure: crypto_sign_open with c=t overlap returns "
"nonzero\n");
public(c2, tlen);
public(p, plen);
if (memcmp(c2, t, tlen) != 0)
fail("failure: crypto_sign_open does not handle c=t overlap\n");
memcpy(c2, c, clen);
public(c, clen);
public(p2, plen);
result = crypto_sign_open(p2, &tlen, c, clen, p2);
public(&result, sizeof result);
if (result != 0)
fail("failure: crypto_sign_open with p=t overlap returns "
"nonzero\n");
public(p2, tlen);
public(c, clen);
if (memcmp(p2, t, tlen) != 0)
fail("failure: crypto_sign_open does not handle p=t overlap\n");
memcpy(p2, p, plen);
c[myrandom() % clen] += 1 + (myrandom() % 255);
if (crypto_sign_open(t, &tlen, c, clen, p) == 0)
if ((tlen != mlen) || (memcmp(t, m, mlen) != 0))
fail(
"failure: crypto_sign_open allows trivial forgeries\n");
c[myrandom() % clen] += 1 + (myrandom() % 255);
if (crypto_sign_open(t, &tlen, c, clen, p) == 0)
if ((tlen != mlen) || (memcmp(t, m, mlen) != 0))
fail(
"failure: crypto_sign_open allows trivial forgeries\n");
c[myrandom() % clen] += 1 + (myrandom() % 255);
if (crypto_sign_open(t, &tlen, c, clen, p) == 0)
if ((tlen != mlen) || (memcmp(t, m, mlen) != 0))
fail(
"failure: crypto_sign_open allows trivial forgeries\n");
}
checksum_expected(sign_ed25519_checksums[checksumbig]);
}
}
void test_sign_ed25519(void) {
long long maxalloc = 0;
if (targeto && strcmp(targeto, "sign")) return;
if (targetp && strcmp(targetp, "ed25519")) return;
storage_sign_ed25519_p =
callocplus(4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
test_sign_ed25519_p =
aligned(storage_sign_ed25519_p, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
if (4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES >
maxalloc)
maxalloc = 4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES;
storage_sign_ed25519_s =
callocplus(4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
test_sign_ed25519_s =
aligned(storage_sign_ed25519_s, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
if (4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES >
maxalloc)
maxalloc = 4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES;
storage_sign_ed25519_m =
callocplus(4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
test_sign_ed25519_m =
aligned(storage_sign_ed25519_m, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
if (4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES >
maxalloc)
maxalloc = 4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES;
storage_sign_ed25519_c =
callocplus(4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
test_sign_ed25519_c =
aligned(storage_sign_ed25519_c, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
if (4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES >
maxalloc)
maxalloc = 4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES;
storage_sign_ed25519_t =
callocplus(4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
test_sign_ed25519_t =
aligned(storage_sign_ed25519_t, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
if (4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES >
maxalloc)
maxalloc = 4096 + crypto_sign_BYTES + crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES;
storage_sign_ed25519_p2 = callocplus(maxalloc);
test_sign_ed25519_p2 =
aligned(storage_sign_ed25519_p2, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
storage_sign_ed25519_s2 = callocplus(maxalloc);
test_sign_ed25519_s2 =
aligned(storage_sign_ed25519_s2, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
storage_sign_ed25519_m2 = callocplus(maxalloc);
test_sign_ed25519_m2 =
aligned(storage_sign_ed25519_m2, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
storage_sign_ed25519_c2 = callocplus(maxalloc);
test_sign_ed25519_c2 =
aligned(storage_sign_ed25519_c2, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
storage_sign_ed25519_t2 = callocplus(maxalloc);
test_sign_ed25519_t2 =
aligned(storage_sign_ed25519_t2, 4096 + crypto_sign_BYTES +
crypto_sign_PUBLICKEYBYTES +
crypto_sign_SECRETKEYBYTES);
for (long long offset = 0; offset < 2; ++offset) {
if (targetoffset && atol(targetoffset) != offset) continue;
if (offset && valgrind) break;
printf("sign_ed25519 offset %lld\n", offset);
forked(test_sign_ed25519_impl, -1);
++test_sign_ed25519_p;
++test_sign_ed25519_s;
++test_sign_ed25519_m;
++test_sign_ed25519_c;
++test_sign_ed25519_t;
++test_sign_ed25519_p2;
++test_sign_ed25519_s2;
++test_sign_ed25519_m2;
++test_sign_ed25519_c2;
++test_sign_ed25519_t2;
}
free(storage_sign_ed25519_t2);
free(storage_sign_ed25519_c2);
free(storage_sign_ed25519_m2);
free(storage_sign_ed25519_s2);
free(storage_sign_ed25519_p2);
free(storage_sign_ed25519_t);
free(storage_sign_ed25519_c);
free(storage_sign_ed25519_m);
free(storage_sign_ed25519_s);
free(storage_sign_ed25519_p);
}
#undef crypto_sign_SECRETKEYBYTES
#undef crypto_sign_PUBLICKEYBYTES
#undef crypto_sign_BYTES