forked from microsoft/FourQlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcrypto_tests.c
368 lines (318 loc) · 12.9 KB
/
crypto_tests.c
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
/***********************************************************************************
* FourQlib: a high-performance crypto library based on the elliptic curve FourQ
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Abstract: testing code for cryptographic functions based on FourQ
************************************************************************************/
#include "../FourQ_api.h"
#include "../FourQ_params.h"
#include "test_extras.h"
#include <stdio.h>
// Benchmark and test parameters
#if defined(GENERIC_IMPLEMENTATION)
#define BENCH_LOOPS 100 // Number of iterations per bench
#define TEST_LOOPS 100 // Number of iterations per test
#else
#define BENCH_LOOPS 10000
#define TEST_LOOPS 1000
#endif
ECCRYPTO_STATUS SchnorrQ_test()
{ // Test the SchnorrQ digital signature scheme
int n, passed;
void *msg = NULL;
unsigned int len, valid = false;
unsigned char SecretKey[32], PublicKey[32], Signature[64], BlindingPoint[64];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Testing the SchnorrQ signature scheme. Includes side-channel countermeasures: \n\n");
passed = 1;
for (n = 0; n < TEST_LOOPS; n++)
{
// Signature key generation
Status = SchnorrQ_FullKeyGeneration_SCA_secure(SecretKey, PublicKey, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Signature computation
msg = "a";
len = 1;
Status = SchnorrQ_Sign_SCA_secure(SecretKey, PublicKey, msg, len, Signature, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Valid signature test
Status = SchnorrQ_Verify(PublicKey, msg, len, Signature, &valid);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
if (valid == false) {
passed = 0;
break;
}
// Invalid signature test (flipping one bit of the message)
msg = "b";
Status = SchnorrQ_Verify(PublicKey, msg, len, Signature, &valid);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
if (valid == true) {
passed = 0;
break;
}
}
if (passed==1) printf(" Signature tests.................................................................. PASSED");
else { printf(" Signature tests... FAILED"); printf("\n"); Status = ECCRYPTO_ERROR_SIGNATURE_VERIFICATION; }
printf("\n");
return Status;
}
ECCRYPTO_STATUS SchnorrQ_run()
{ // Benchmark the SchnorrQ digital signature scheme
int n;
unsigned long long nsec, nsec1, nsec2;
void *msg = NULL;
unsigned int len = 0, valid = false;
unsigned char SecretKey[32], PublicKey[32], Signature[64], BlindingPoint[64];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Benchmarking the SchnorrQ signature scheme. Includes side-channel countermeasures: \n\n");
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = SchnorrQ_FullKeyGeneration_SCA_secure(SecretKey, PublicKey, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec+(nsec2-nsec1);
}
printf(" SchnorrQ's key generation runs in ............................................... %8lld nsec", nsec/BENCH_LOOPS);
printf("\n");
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = SchnorrQ_Sign_SCA_secure(SecretKey, PublicKey, msg, len, Signature, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec+(nsec2-nsec1);
}
printf(" SchnorrQ's signing runs in ...................................................... %8lld nsec", nsec/BENCH_LOOPS);
printf("\n");
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = SchnorrQ_Verify(PublicKey, msg, len, Signature, &valid);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec+(nsec2-nsec1);
}
printf(" SchnorrQ's verification runs in ................................................. %8lld nsec", nsec/BENCH_LOOPS);
printf("\n");
return Status;
}
ECCRYPTO_STATUS compressedkex_test()
{ // Test ECDH key exchange based on FourQ
int n, passed;
unsigned int i;
unsigned char SecretKeyA[32], PublicKeyA[32], BlindingPointA[64], SecretAgreementA[32];
unsigned char SecretKeyB[32], PublicKeyB[32], BlindingPointB[64], SecretAgreementB[32];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Testing DH key exchange using compressed, 32-byte public keys. Includes side-channel countermeasures: \n\n");
passed = 1;
for (n = 0; n < TEST_LOOPS; n++)
{
// Alice's keypair generation
Status = CompressedKeyGeneration_SCA_secure(SecretKeyA, PublicKeyA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Bob's keypair generation
Status = CompressedKeyGeneration_SCA_secure(SecretKeyB, PublicKeyB, BlindingPointB);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Alice's shared secret computation
Status = CompressedSecretAgreement_SCA_secure(SecretKeyA, PublicKeyB, SecretAgreementA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Bob's shared secret computation
Status = CompressedSecretAgreement_SCA_secure(SecretKeyB, PublicKeyA, SecretAgreementB, BlindingPointB);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
for (i = 0; i < 32; i++) {
if (SecretAgreementA[i] != SecretAgreementB[i]) {
passed = 0;
break;
}
}
}
if (passed==1) printf(" DH key exchange tests............................................................ PASSED");
else { printf(" DH key exchange tests... FAILED"); printf("\n"); Status = ECCRYPTO_ERROR_SHARED_KEY; }
printf("\n");
return Status;
}
ECCRYPTO_STATUS compressedkex_run()
{ // Benchmark ECDH key exchange based on FourQ
int n;
unsigned long long nsec, nsec1, nsec2;
unsigned char SecretKeyA[32], PublicKeyA[32], BlindingPointA[64], SecretAgreementA[32];
unsigned char SecretKeyB[32], PublicKeyB[32], BlindingPointB[64];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Benchmarking DH key exchange using compressed, 32-byte public keys. Includes side-channel countermeasures: \n\n");
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = CompressedKeyGeneration_SCA_secure(SecretKeyA, PublicKeyA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec + (nsec2 - nsec1);
}
printf(" Keypair generation runs in ...................................................... %8lld nsec", nsec / BENCH_LOOPS);
printf("\n");
Status = CompressedKeyGeneration_SCA_secure(SecretKeyB, PublicKeyB, BlindingPointB);
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = CompressedSecretAgreement_SCA_secure(SecretKeyA, PublicKeyB, SecretAgreementA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec + (nsec2 - nsec1);
}
printf(" Secret agreement runs in ........................................................ %8lld nsec", nsec / BENCH_LOOPS);
printf("\n");
return Status;
}
ECCRYPTO_STATUS kex_test()
{ // Test ECDH key exchange based on FourQ
int n, passed;
unsigned int i;
unsigned char SecretKeyA[32], PublicKeyA[64], BlindingPointA[64], SecretAgreementA[32];
unsigned char SecretKeyB[32], PublicKeyB[64], BlindingPointB[64], SecretAgreementB[32];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Testing DH key exchange using uncompressed, 64-byte public keys. Includes side-channel countermeasures: \n\n");
passed = 1;
for (n = 0; n < TEST_LOOPS; n++)
{
// Alice's keypair generation
Status = KeyGeneration_SCA_secure(SecretKeyA, PublicKeyA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Bob's keypair generation
Status = KeyGeneration_SCA_secure(SecretKeyB, PublicKeyB, BlindingPointB);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Alice's shared secret computation
Status = SecretAgreement_SCA_secure(SecretKeyA, PublicKeyB, SecretAgreementA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
// Bob's shared secret computation
Status = SecretAgreement_SCA_secure(SecretKeyB, PublicKeyA, SecretAgreementB, BlindingPointB);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
for (i = 0; i < 32; i++) {
if (SecretAgreementA[i] != SecretAgreementB[i]) {
passed = 0;
break;
}
}
}
if (passed==1) printf(" DH key exchange tests............................................................ PASSED");
else { printf(" DH key exchange tests... FAILED"); printf("\n"); Status = ECCRYPTO_ERROR_SHARED_KEY; }
printf("\n");
return Status;
}
ECCRYPTO_STATUS kex_run()
{ // Benchmark ECDH key exchange based on FourQ
int n;
unsigned long long nsec, nsec1, nsec2;
unsigned char SecretKeyA[32], PublicKeyA[64], BlindingPointA[64], SecretAgreementA[32];
unsigned char SecretKeyB[32], PublicKeyB[64], BlindingPointB[64];
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
printf("\n--------------------------------------------------------------------------------------------------------\n\n");
printf("Benchmarking DH key exchange using uncompressed, 64-byte public keys. Includes side-channel countermeasures: \n\n");
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = KeyGeneration_SCA_secure(SecretKeyA, PublicKeyA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec + (nsec2 - nsec1);
}
printf(" Keypair generation runs in ...................................................... %8lld nsec", nsec / BENCH_LOOPS);
printf("\n");
Status = KeyGeneration_SCA_secure(SecretKeyB, PublicKeyB, BlindingPointB);
nsec = 0;
for (n = 0; n < BENCH_LOOPS; n++)
{
nsec1 = cpu_nseconds();
Status = SecretAgreement_SCA_secure(SecretKeyA, PublicKeyB, SecretAgreementA, BlindingPointA);
if (Status != ECCRYPTO_SUCCESS) {
return Status;
}
nsec2 = cpu_nseconds();
nsec = nsec + (nsec2 - nsec1);
}
printf(" Secret agreement runs in ........................................................ %8lld nsec", nsec / BENCH_LOOPS);
printf("\n");
return Status;
}
int main()
{
ECCRYPTO_STATUS Status = ECCRYPTO_SUCCESS;
Status = SchnorrQ_test(); // Test SchnorrQ signature scheme
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
Status = SchnorrQ_run(); // Benchmark SchnorrQ signature scheme
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
Status = compressedkex_test(); // Test Diffie-Hellman key exchange using compressed public keys
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
Status = compressedkex_run(); // Benchmark Diffie-Hellman key exchange using compressed public keys
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
Status = kex_test(); // Test Diffie-Hellman key exchange using uncompressed public keys
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
Status = kex_run(); // Benchmark Diffie-Hellman key exchange using uncompressed public keys
if (Status != ECCRYPTO_SUCCESS) {
printf("\n\n Error detected: %s \n\n", FourQ_get_error_message(Status));
return false;
}
return true;
}