-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.cpp
511 lines (384 loc) · 9.73 KB
/
main.cpp
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// main.cpp
// 2019-09-03 Markku-Juhani O. Saarinen <[email protected]>
// Simple testing main() for algorithms.
#include "mbed.h"
#include "stm32f4xx_hal.h"
// mbed API forces us the be in C++, even though the code is really C
extern "C" {
// NIST API and algorithm details
#include "api.h"
// randombytes.c
void my_random_seed(int seed);
}
// Algorithm name
#if !defined(CRYPTO_ALGNAME) && defined(crypto_kem_PRIMITIVE)
#define CRYPTO_ALGNAME crypto_kem_PRIMITIVE
#endif
#ifndef CRYPTO_ALGNAME
#define CRYPTO_ALGNAME "undefined"
#endif
// KEM or Signature algorithm ?
#ifdef CRYPTO_CIPHERTEXTBYTES
#define TEST_KEM
#else
#define TEST_SIGN
#define MESSAGE_BYTES sizeof(uint64_t)
#endif
uint64_t scc_ms = 96000; // clocks per millisecond
uint64_t ofl_ms = 44739; // milliseconds per overflow
#define CYCLES_VARS \
int ms; \
uint32_t t;
#define CYCLES_START { \
ms = timer.read_ms(); \
t = DWT->CYCCNT; \
}
#define CYCLES_ADD(cc) { \
t = DWT->CYCCNT - t; \
cc += (uint64_t) t; \
ms = timer.read_ms() - ms; \
ms -= ((uint64_t) t) / scc_ms; \
cc += ((uint64_t) (ms / ofl_ms)) << 32; \
}
// mbed
Serial ser(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
DigitalOut pin_d7(D7);
Timer timer;
// print
void hexbytes(const uint8_t *data, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
ser.printf("%02X", data[i]);
ser.printf("\n");
}
// delay
void zzz_ms(int ms_time)
{
int ms;
ser.printf("\n[ZZZ] zzz_ms(%d)\n", ms_time);
timer.reset();
timer.start();
pin_d7 = 1; // trigger measurement
pin_d7 = 0;
do {
ms = timer.read_ms();
} while (ms < ms_time);
// wait_ms(ms_time);
ser.printf("\n[END] wakey wakey\n");
}
// ==== SIGNATURE ALGORITHMS ====
#ifdef TEST_SIGN
// signing state
static uint8_t m[MESSAGE_BYTES];
static uint8_t pk[CRYPTO_PUBLICKEYBYTES];
static uint8_t sk[CRYPTO_SECRETKEYBYTES];
static uint8_t sm[CRYPTO_BYTES + MESSAGE_BYTES];
static size_t sm_len = 0;
static size_t m_len = MESSAGE_BYTES;
static int init_sign = 0;
int test_alg(int ms_time)
{
int i, r, ms;
ser.printf("\n[TEST] test_alg(%d)\n", ms_time);
timer.reset();
timer.start();
r = 0;
ms = 0;
for (i = 0; i < 999999999; i++) {
my_random_seed(i);
memset(m, 0x11 + i, sizeof(m));
memset(pk, 0x22 + i, sizeof(pk));
memset(sk, 0x33 + i, sizeof(sk));
memset(sm, 0x44 + i, sizeof(sm));
// keygen
r = crypto_sign_keypair(pk, sk);
if (r != 0) {
ser.printf("i=%d: crypto_sign_keypair() returned %d\n", i, r);
break;
}
// sign message
r = crypto_sign(sm, &sm_len, m, MESSAGE_BYTES, sk);
if (r != 0) {
ser.printf("i=%d: crypto_sign() returned %d\n", i, r);
break;
}
if (sm_len > CRYPTO_BYTES + MESSAGE_BYTES) {
ser.printf("i=%d: (Signature of %d bytes is %d bytes.)\n",
i, MESSAGE_BYTES, sm_len);
}
// verify message
r = crypto_sign_open(m, &m_len, sm, sm_len, pk);
if (r != 0) {
ser.printf("i=%d: crypto_sign_open() returned %d\n", i, r);
break;
}
// try to verify corrupt message
sm[0]++;
r = crypto_sign_open(m, &m_len, sm, sm_len, pk);
if (r == 0) {
ser.printf("i=%d: crypto_sign_open() success for bad sign\n", i);
r = -1;
break;
}
r = 0;
// timeout
ms = timer.read_ms();
if (ms > ms_time)
break;
}
timer.stop();
ser.printf("[END] %s(%d), %d milliseconds (i=%d)\n",
r == 0 ? "PASS" : "FAIL", r, ms, i);
init_sign = 1;
return r;
}
int measure(int ms_time, int do_kg, int do_sg, int do_so)
{
int r;
uint64_t n, kg, sg, so;
CYCLES_VARS
// make sure that there is a keypair
if (!do_kg && !init_sign)
test_alg(0);
ser.printf("\n[START] measure(%d%s%s%s)\n", ms_time,
do_kg ? ",kg" : "", do_sg ? ",sign" : "", do_so ? ",ver" : "");
r = 0;
n = kg = sg = so = 0;
timer.reset();
timer.start();
pin_d7 = 1; // trigger measurement
pin_d7 = 0;
do {
n++;
memcpy(m, &n, sizeof(n));
if (do_kg) {
CYCLES_START
if (crypto_sign_keypair(pk, sk))
r++;
CYCLES_ADD(kg)
}
if (do_sg) {
CYCLES_START
if (crypto_sign(sm, &sm_len, m, MESSAGE_BYTES, sk))
r++;
CYCLES_ADD(sg)
}
if (do_so) {
CYCLES_START
if (crypto_sign_open(m, &m_len, sm, sm_len, pk))
r++;
CYCLES_ADD(so)
}
ms = timer.read_ms();
} while (ms < ms_time);
timer.stop();
ser.printf("[END] %d milliseconds, n=%llu (%d errors)\n", ms, n, r);
if (n == 0)
return r;
if (kg > 0)
ser.printf("*** KeyGen %12llu %s\n", kg / n, CRYPTO_ALGNAME);
if (sg > 0)
ser.printf("*** Sign %12llu %s\n", sg / n, CRYPTO_ALGNAME);
if (so > 0)
ser.printf("*** Verify %12llu %s\n", so / n, CRYPTO_ALGNAME);
ser.printf("*** Total %12llu %s\n", (kg+sg+so) / n, CRYPTO_ALGNAME);
if (do_kg)
init_sign = 1;
return r;
}
#endif /* TEST_SIGN */
// ==== KEY ENCAPSULATION ALGORITHMS ====
#ifdef TEST_KEM
// KEM state
static uint8_t pk[CRYPTO_PUBLICKEYBYTES];
static uint8_t sk[CRYPTO_SECRETKEYBYTES];
static uint8_t ct[CRYPTO_CIPHERTEXTBYTES];
static uint8_t s1[CRYPTO_BYTES];
static uint8_t s2[CRYPTO_BYTES];
static int init_kem = 0;
int test_alg(int ms_time)
{
int i, r, ms;
ser.printf("\n[TEST] test_alg(%d)\n", ms_time);
timer.reset();
timer.start();
// check that it works correctly
r = 0;
ms = 0;
for (i = 0; i < 999999999; i++) {
my_random_seed(i);
memset(pk, 0x11 + i, sizeof(pk));
memset(sk, 0x22 + i, sizeof(sk));
memset(ct, 0x33 + i, sizeof(ct));
memset(s1, 0x44 + i, sizeof(s1));
memset(s2, 0x55 + i, sizeof(s2));
r = crypto_kem_keypair(pk, sk);
if (r != 0) {
ser.printf("i=%d: crypto_kem_keypair() returned %d\n", i, r);
break;
}
r = crypto_kem_enc(ct, s1, pk);
if (r != 0) {
ser.printf("i=%d: crypto_kem_enc() returned %d\n", i, r);
break;
}
r = crypto_kem_dec(s2, ct, sk);
if (r != 0) {
ser.printf("i=%d: crypto_kem_dec() returned %d\n", i, r);
break;
}
if (memcmp(s1, s2, CRYPTO_BYTES) != 0) {
ser.printf("i=%d: Shared secret mismatch!\n", i);
ser.printf("s1 = ");
hexbytes(s1, sizeof(s1));
ser.printf("s2 = ");
hexbytes(s1, sizeof(s2));
r = -1;
break;
}
// timeout
ms = timer.read_ms();
if (ms > ms_time)
break;
}
timer.stop();
ser.printf("[END] %s(%d), %d milliseconds (i=%d)\n",
r == 0 ? "PASS" : "FAIL", r, ms, i);
init_kem = 1;
return r;
}
int measure(int ms_time, int do_kg, int do_enc, int do_dec)
{
int r;
uint64_t n, kg, enc, dec;
CYCLES_VARS
// make sure that there is a keypair
if (!do_kg && !init_kem)
test_alg(0);
// main test
ser.printf("\n[START] measure(%d%s%s%s)\n", ms_time,
do_kg ? ",kg" : "", do_enc ? ",enc" : "", do_dec ? ",dec" : "");
r = 0;
n = kg = enc = dec = 0;
timer.reset();
timer.start();
pin_d7 = 1; // trigger measurement
pin_d7 = 0;
do {
n++;
if (do_kg) {
CYCLES_START
if (crypto_kem_keypair(pk, sk))
r++;
CYCLES_ADD(kg)
}
if (do_enc) {
CYCLES_START
if (crypto_kem_enc(ct, s1, pk))
r++;
CYCLES_ADD(enc)
}
if (do_dec) {
CYCLES_START
if (crypto_kem_dec(s2, ct, sk))
r++;
CYCLES_ADD(dec)
}
ms = timer.read_ms();
} while (ms < ms_time);
ser.printf("[END] %d milliseconds, n=%llu (%d errors)\n", ms, n, r);
timer.stop();
if (n == 0)
return r;
if (kg > 0)
ser.printf("*** KeyGen %12llu %s\n", kg / n, CRYPTO_ALGNAME);
if (enc > 0)
ser.printf("*** Encaps %12llu %s\n", enc / n, CRYPTO_ALGNAME);
if (dec > 0)
ser.printf("*** Decaps %12llu %s\n", dec / n, CRYPTO_ALGNAME);
ser.printf("*** Total %12llu %s\n", (kg+enc+dec) / n, CRYPTO_ALGNAME);
if (do_kg)
init_kem = 1;
return r;
}
#endif /* TEST_KEM */
int main()
{
int ch;
ser.baud(115200);
ser.printf("\n[RESET] This is PQPowerSandwich! Welcome.\n\n");
ser.printf("SystemCoreClock %lu\n", SystemCoreClock);
scc_ms = SystemCoreClock / 1000;
ofl_ms = 4294967295 / scc_ms;
// set so that cycle counter can be read from DWT->CYCCNT
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
// reset trigger
pin_d7 = 0;
ser.printf("CRYPTO_ALGNAME %s\n", CRYPTO_ALGNAME);
ser.printf("CRYPTO_SECRETKEYBYTES %d\n", CRYPTO_SECRETKEYBYTES);
ser.printf("CRYPTO_PUBLICKEYBYTES %d\n", CRYPTO_PUBLICKEYBYTES);
#ifdef TEST_KEM
ser.printf("CRYPTO_CIPHERTEXTBYTES %d\n", CRYPTO_CIPHERTEXTBYTES);
#endif
ser.printf("CRYPTO_BYTES %d\n", CRYPTO_BYTES);
do {
#ifdef TEST_KEM
ser.printf("[INPUT] a = all, k = keygen, e = encaps, d = decaps\n");
#endif
#ifdef TEST_SIGN
ser.printf("[INPUT] a = all, k = keygen, s = sign, v = verify\n");
#endif
switch (ch = ser.getc()) {
case 'a':
measure(10000, 1, 1, 1);
break;
case 'A':
measure(0, 1, 1, 1);
break;
case 'k':
measure(10000, 1, 0, 0);
break;
case 'K':
measure(0, 1, 0, 0);
break;
case 's':
case 'e':
measure(10000, 0, 1, 0);
break;
case 'S':
case 'E':
measure(0, 0, 1, 0);
break;
case 'v':
case 'd':
measure(10000, 0, 0, 1);
break;
case 'V':
case 'D':
measure(0, 0, 0, 1);
break;
case 'z':
zzz_ms(10000);
break;
case 't':
test_alg(10000);
break;
case 'T':
test_alg(0);
break;
}
// set seed
if (ch >= '0' && ch <= '9') {
my_random_seed(ch - '0');
ser.printf("\n[RANDOM] %c\n", ch);
}
} while (ch != 'x');
ser.printf("\n[DONE] sleep()\n");
while (1) {
sleep();
}
}