forked from open-quantum-safe/liboqs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sig_stfl.c
1233 lines (1038 loc) · 37.2 KB
/
test_sig_stfl.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: MIT
#if defined(_WIN32)
#pragma warning(disable : 4244 4293)
#endif
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#if defined(_WIN32)
#include <string.h>
#define strcasecmp _stricmp
#else
#include <strings.h>
#endif
#include <time.h>
#include <oqs/oqs.h>
#include "tmp_store.c"
#include "system_info.c"
#if OQS_USE_PTHREADS_IN_TESTS
#include <pthread.h>
static pthread_mutex_t *test_sk_lock = NULL;
static pthread_mutex_t *sk_lock = NULL;
#endif
#ifdef OQS_ENABLE_TEST_CONSTANT_TIME
#include <valgrind/memcheck.h>
#define OQS_TEST_CT_CLASSIFY(addr, len) VALGRIND_MAKE_MEM_UNDEFINED(addr, len)
#define OQS_TEST_CT_DECLASSIFY(addr, len) VALGRIND_MAKE_MEM_DEFINED(addr, len)
#else
#define OQS_TEST_CT_CLASSIFY(addr, len)
#define OQS_TEST_CT_DECLASSIFY(addr, len)
#endif
#ifdef __GNUC__
#define UNUSED __attribute__((unused))
#else
#define UNUSED
#endif
/*
* For stateful signature, we skip key generation because it can takes hours to complete.
* So the ReadHex and and FindMarker serve the purpose of reading pre-generate keypair from KATs.
*/
#define MAX_MARKER_LEN 50
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int FindMarker(FILE *infile, const char *marker) {
char line[MAX_MARKER_LEN];
unsigned long i, len;
int curr_line;
memset(line, 0, MAX_MARKER_LEN);
len = strlen(marker);
if (len > MAX_MARKER_LEN - 1) {
len = MAX_MARKER_LEN - 1;
}
for (i = 0; i < len; i++) {
curr_line = fgetc(infile);
line[i] = (char)curr_line;
if (curr_line == EOF) {
return 0;
}
}
line[len] = '\0';
while (1) {
if (!strncmp(line, marker, len)) {
return 1;
}
for (i = 0; i < len - 1; i++) {
line[i] = line[i + 1];
}
curr_line = fgetc(infile);
line[len - 1] = (char)curr_line;
if (curr_line == EOF) {
return 0;
}
line[len] = '\0';
}
// shouldn't get here
return 0;
}
//
// ALLOW TO READ HEXADECIMAL ENTRY (KEYS, DATA, TEXT, etc.)
//
int ReadHex(FILE *infile, unsigned char *a, unsigned long Length, char *str) {
int ch, started;
unsigned long i;
unsigned char ich;
if (Length == 0) {
a[0] = 0x00;
return 1;
}
memset(a, 0x00, Length);
started = 0;
if (FindMarker(infile, str))
while ((ch = fgetc(infile)) != EOF) {
if (!isxdigit(ch)) {
if (!started) {
if (ch == '\n') {
break;
} else {
continue;
}
} else {
break;
}
}
started = 1;
if ((ch >= '0') && (ch <= '9')) {
ich = (unsigned char)ch - '0';
} else if ((ch >= 'A') && (ch <= 'F')) {
ich = (unsigned char)ch - 'A' + 10;
} else if ((ch >= 'a') && (ch <= 'f')) {
ich = (unsigned char)ch - 'a' + 10;
} else {
// shouldn't ever get here
ich = 0;
}
for (i = 0; i < Length - 1; i++) {
a[i] = (unsigned char) (a[i] << 4) | (unsigned char) (a[i + 1] >> 4);
}
a[Length - 1] = (unsigned char) (a[Length - 1] << 4) | (unsigned char) ich;
} else {
return 0;
}
return 1;
}
/*
* Write stateful secret keys to disk.
*/
static OQS_STATUS save_secret_key(uint8_t *key_buf, size_t buf_len, void *context) {
if (key_buf == NULL || buf_len == 0 || context == NULL) {
return OQS_ERROR;
}
const char *context_char = context;
if (oqs_fstore("sk", context_char, key_buf, buf_len) == OQS_SUCCESS) {
printf("\n================================================================================\n");
printf("Updated STFL SK <%s>.\n", context_char);
printf("================================================================================\n");
return OQS_SUCCESS;
}
return OQS_ERROR;
}
#if OQS_USE_PTHREADS_IN_TESTS
static OQS_SIG_STFL_SECRET_KEY *lock_test_sk = NULL;
static OQS_SIG_STFL *lock_test_sig_obj = NULL;
static uint8_t *lock_test_public_key = NULL;
static char *lock_test_context = NULL;
static uint8_t *signature_1 = NULL;
static uint8_t *signature_2 = NULL;
static size_t signature_len_1;
static size_t signature_len_2;
static uint8_t message_1[] = "The quick brown fox ...";
static uint8_t message_2[] = "The quick brown fox jumped from the tree.";
static OQS_STATUS lock_sk_key(void *mutex) {
if (mutex == NULL) {
return OQS_ERROR;
}
if (pthread_mutex_lock((pthread_mutex_t *)mutex)) {
return OQS_ERROR;
}
return OQS_SUCCESS;
}
static OQS_STATUS unlock_sk_key(void *mutex) {
if (mutex == NULL) {
return OQS_ERROR;
}
if (pthread_mutex_unlock((pthread_mutex_t *)mutex)) {
return OQS_ERROR;
}
return OQS_SUCCESS;
}
#else
static OQS_STATUS lock_sk_key(UNUSED void *mutex) {
return OQS_SUCCESS;
}
static OQS_STATUS unlock_sk_key(UNUSED void *mutex) {
return OQS_SUCCESS;
}
#endif
OQS_STATUS sig_stfl_keypair_from_keygen(OQS_SIG_STFL *sig, uint8_t *public_key, OQS_SIG_STFL_SECRET_KEY *secret_key) {
OQS_STATUS rc;
rc = OQS_SIG_STFL_keypair(sig, public_key, secret_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
return OQS_ERROR;
}
return OQS_SUCCESS;
}
OQS_STATUS sig_stfl_keypair_from_KATs(OQS_SIG_STFL *sig, uint8_t *public_key, OQS_SIG_STFL_SECRET_KEY *secret_key, const char *katfile) {
OQS_STATUS ret = OQS_ERROR;
FILE *fp_rsp = NULL;
if ((fp_rsp = fopen(katfile, "r")) == NULL) {
fprintf(stderr, "Couldn't open <%s> for read\n", katfile);
goto err;
}
// Grab the pk and sk from KAT file
if (!ReadHex(fp_rsp, public_key, sig->length_public_key, (char *)"pk = ")) {
fprintf(stderr, "ERROR: unable to read 'pk' from <%s>\n", katfile);
goto err;
}
if (!ReadHex(fp_rsp, secret_key->secret_key_data, sig->length_secret_key, (char *)"sk = ")) {
fprintf(stderr, "ERROR: unable to read 'sk' from <%s>\n", katfile);
goto err;
}
// We are done reading, clean up and exit
ret = OQS_SUCCESS;
goto cleanup;
err:
ret = OQS_ERROR;
cleanup:
fclose(fp_rsp);
return ret;
}
/*
* We read from KATs these parameters:
* XMSS-SHA2_16_256
* XMSS-SHA2_20_256
* XMSS-SHAKE_16_256
* XMSS-SHAKE_20_256
* XMSSMT-SHA2_40/2_256
* XMSSMT-SHA2_60/3_256
* XMSSMT-SHAKE_40/2_256
* XMSSMT-SHAKE_60/3_256
*/
OQS_STATUS sig_stfl_KATs_keygen(OQS_SIG_STFL *sig, uint8_t *public_key, OQS_SIG_STFL_SECRET_KEY *secret_key, const char *katfile) {
if (sig == NULL || public_key == NULL || secret_key == NULL ) {
return OQS_ERROR;
}
#ifdef OQS_ENABLE_SIG_STFL_XMSS
if (0) {
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha256_h16
} else if (strcmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha256_h16) == 0) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha256_h20
} else if (strcmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha256_h20) == 0) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake128_h16
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake128_h16)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake128_h20
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake128_h20)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha512_h16
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha512_h16)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha512_h20
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha512_h20)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h16
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h16)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h20
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h20)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha256_h16_192
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha256_h16_192)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_sha256_h20_192
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_sha256_h20_192)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h16_192
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h16_192)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h20_192
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h20_192)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h16_256
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h16_256)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmss_shake256_h20_256
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmss_shake256_h20_256)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmssmt_sha256_h40_2
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h40_2)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmssmt_sha256_h60_3
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h60_3)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmssmt_shake128_h40_2
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h40_2)) {
goto from_kats;
#endif
#ifdef OQS_ENABLE_SIG_STFL_xmssmt_shake128_h60_3
} else if (0 == strcasecmp(sig->method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h60_3)) {
goto from_kats;
#endif
} else {
goto from_keygen;
}
from_kats:
return sig_stfl_keypair_from_KATs(sig, public_key, secret_key, katfile);
from_keygen:
#endif //OQS_ENABLE_SIG_STFL_XMSS
(void)(katfile);
return sig_stfl_keypair_from_keygen(sig, public_key, secret_key);
}
typedef struct magic_s {
uint8_t val[31];
} magic_t;
static char *convert_method_name_to_file_name(const char *method_name) {
if (method_name == NULL) {
return NULL;
}
const char *file_store = NULL;
if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h20_2) == 0) {
file_store = "XMSSMT-SHA2_20-2_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h20_4) == 0) {
file_store = "XMSSMT-SHA2_20-4_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h40_2) == 0) {
file_store = "XMSSMT-SHA2_40-2_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h40_4) == 0) {
file_store = "XMSSMT-SHA2_40-4_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h40_8) == 0) {
file_store = "XMSSMT-SHA2_40-8_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h60_3) == 0) {
file_store = "XMSSMT-SHA2_60-3_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h60_6) == 0) {
file_store = "XMSSMT-SHA2_60-6_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_sha256_h60_12) == 0) {
file_store = "XMSSMT-SHA2_60-12_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h20_2) == 0) {
file_store = "XMSSMT-SHAKE_20-2_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h20_4) == 0) {
file_store = "XMSSMT-SHAKE_20-4_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h40_2) == 0) {
file_store = "XMSSMT-SHAKE_40-2_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h40_4) == 0) {
file_store = "XMSSMT-SHAKE_40-4_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h40_8) == 0) {
file_store = "XMSSMT-SHAKE_40-8_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h60_3) == 0) {
file_store = "XMSSMT-SHAKE_60-3_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h60_6) == 0) {
file_store = "XMSSMT-SHAKE_60-6_256";
} else if (strcmp(method_name, OQS_SIG_STFL_alg_xmssmt_shake128_h60_12) == 0) {
file_store = "XMSSMT-SHAKE_60-12_256";
} else {
file_store = method_name;
}
return strdup(file_store);
}
static OQS_STATUS sig_stfl_test_correctness(const char *method_name, const char *katfile) {
OQS_SIG_STFL *sig = NULL;
uint8_t *public_key = NULL;
OQS_SIG_STFL_SECRET_KEY *secret_key = NULL;
OQS_SIG_STFL_SECRET_KEY *secret_key_rd = NULL;
uint8_t *message = NULL;
size_t message_len = 100;
uint8_t *signature = NULL;
size_t signature_len;
uint8_t *sk_buf = NULL;
uint8_t *read_pk_buf = NULL;
char *context = NULL;
char *file_store = NULL;
size_t sk_buf_len = 0;
size_t read_pk_len = 0;
magic_t magic;
OQS_STATUS rc, ret = OQS_ERROR;
//The magic numbers are random values.
//The length of the magic number was chosen to be 31 to break alignment
OQS_randombytes(magic.val, sizeof(magic_t));
sig = OQS_SIG_STFL_new(method_name);
if (sig == NULL) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed\n");
goto err;
}
printf("================================================================================\n");
printf("Sample computation for stateful signature %s\n", sig->method_name);
printf("================================================================================\n");
secret_key = OQS_SIG_STFL_SECRET_KEY_new(sig->method_name);
secret_key_rd = OQS_SIG_STFL_SECRET_KEY_new(sig->method_name);
OQS_SIG_STFL_SECRET_KEY_SET_lock(secret_key, lock_sk_key);
OQS_SIG_STFL_SECRET_KEY_SET_unlock(secret_key, unlock_sk_key);
file_store = convert_method_name_to_file_name(sig->method_name);
if (file_store == NULL) {
fprintf(stderr, "%s: file_store is null\n", __func__);
goto err;
}
/* set context and secure store callback */
context = strdup(((file_store)));
OQS_SIG_STFL_SECRET_KEY_SET_store_cb(secret_key, save_secret_key, (void *)context);
#if OQS_USE_PTHREADS_IN_TESTS
OQS_SIG_STFL_SECRET_KEY_SET_mutex(secret_key, sk_lock);
#endif
public_key = malloc(sig->length_public_key + 2 * sizeof(magic_t));
message = malloc(message_len + 2 * sizeof(magic_t));
signature = malloc(sig->length_signature + 2 * sizeof(magic_t));
if ((public_key == NULL) || (secret_key == NULL) || (message == NULL) || (signature == NULL)) {
fprintf(stderr, "ERROR: malloc failed\n");
goto err;
}
//Set the magic numbers before
memcpy(public_key, magic.val, sizeof(magic_t));
memcpy(message, magic.val, sizeof(magic_t));
memcpy(signature, magic.val, sizeof(magic_t));
public_key += sizeof(magic_t);
message += sizeof(magic_t);
signature += sizeof(magic_t);
// and after
memcpy(public_key + sig->length_public_key, magic.val, sizeof(magic_t));
memcpy(message + message_len, magic.val, sizeof(magic_t));
memcpy(signature + sig->length_signature, magic.val, sizeof(magic_t));
OQS_randombytes(message, message_len);
OQS_TEST_CT_DECLASSIFY(message, message_len);
/*
* Some keypair generation is fast, so we only read keypair from KATs for slow XMSS parameters
*/
rc = sig_stfl_KATs_keygen(sig, public_key, secret_key, katfile);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_keypair failed\n");
goto err;
}
rc = OQS_SIG_STFL_SECRET_KEY_serialize(&sk_buf, &sk_buf_len, secret_key);
if (rc != OQS_SUCCESS) {
goto err;
}
/* write key pair to disk */
if (oqs_fstore("sk", file_store, sk_buf, sk_buf_len) != OQS_SUCCESS) {
goto err;
}
if (oqs_fstore("pk", file_store, public_key, sig->length_public_key) != OQS_SUCCESS) {
goto err;
}
rc = OQS_SIG_STFL_sign(sig, signature, &signature_len, message, message_len, secret_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_sign failed\n");
goto err;
}
OQS_TEST_CT_DECLASSIFY(public_key, sig->length_public_key);
OQS_TEST_CT_DECLASSIFY(signature, signature_len);
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify failed\n");
goto err;
}
/* Read public key and re-test verify.*/
read_pk_buf = malloc(sig->length_public_key);
if (oqs_fload("pk", file_store, read_pk_buf, sig->length_public_key, &read_pk_len) != OQS_SUCCESS) {
goto err;
}
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, read_pk_buf);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: 2nd Verify with restored public key OQS_SIG_STFL_verify failed\n");
}
/* modify the signature to invalidate it */
OQS_randombytes(signature, signature_len);
OQS_TEST_CT_DECLASSIFY(signature, signature_len);
rc = OQS_SIG_STFL_verify(sig, message, message_len, signature, signature_len, public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_ERROR) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_verify should have failed!\n");
goto err;
}
#ifndef OQS_ENABLE_TEST_CONSTANT_TIME
/* check magic values */
int rv = memcmp(public_key + sig->length_public_key, magic.val, sizeof(magic_t));
rv |= memcmp(message + message_len, magic.val, sizeof(magic_t));
rv |= memcmp(signature + sig->length_signature, magic.val, sizeof(magic_t));
rv |= memcmp(public_key - sizeof(magic_t), magic.val, sizeof(magic_t));
rv |= memcmp(message - sizeof(magic_t), magic.val, sizeof(magic_t));
rv |= memcmp(signature - sizeof(magic_t), magic.val, sizeof(magic_t));
if (rv) {
fprintf(stderr, "ERROR: Magic numbers do not match\n");
goto err;
}
#endif
ret = OQS_SUCCESS;
goto cleanup;
err:
ret = OQS_ERROR;
cleanup:
OQS_SIG_STFL_SECRET_KEY_free(secret_key);
OQS_SIG_STFL_SECRET_KEY_free(secret_key_rd);
if (public_key) {
OQS_MEM_insecure_free(public_key - sizeof(magic_t));
}
if (message) {
OQS_MEM_insecure_free(message - sizeof(magic_t));
}
if (signature) {
OQS_MEM_insecure_free(signature - sizeof(magic_t));
}
OQS_MEM_secure_free(sk_buf, sk_buf_len);
OQS_SIG_STFL_free(sig);
OQS_MEM_insecure_free(read_pk_buf);
OQS_MEM_insecure_free(context);
OQS_MEM_insecure_free(file_store);
return ret;
}
static OQS_STATUS sig_stfl_test_secret_key(const char *method_name, const char *katfile) {
OQS_STATUS rc = OQS_SUCCESS;
OQS_SIG_STFL_SECRET_KEY *sk = NULL;
OQS_SIG_STFL_SECRET_KEY *sk_from_file = NULL;
unsigned long long num_sig_left = 0, max_num_sigs = 0;
OQS_SIG_STFL *sig_obj = NULL;
uint8_t *public_key = NULL;
uint8_t *from_file_sk_buf = NULL;
uint8_t *to_file_sk_buf = NULL;
size_t from_file_sk_len = 0;
size_t to_file_sk_len = 0;
char *context = NULL;
char *context_2 = NULL;
char *file_store_name = NULL;
/*
* Temporarily skip algs with long key generation times.
*/
printf("================================================================================\n");
printf("Create stateful Signature %s\n", method_name);
printf("================================================================================\n");
sig_obj = OQS_SIG_STFL_new(method_name);
if (sig_obj == NULL) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed\n");
goto err;
}
public_key = malloc(sig_obj->length_public_key * sizeof(uint8_t));
printf("================================================================================\n");
printf("Create stateful Secret Key %s\n", method_name);
printf("================================================================================\n");
sk = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (sk == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed\n");
goto err;
}
printf("================================================================================\n");
printf("Generate keypair %s\n", method_name);
printf("================================================================================\n");
rc = sig_stfl_KATs_keygen(sig_obj, public_key, sk, katfile);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key gen failed.\n");
goto err;
}
/*
* Get max num signature and the amount remaining
*/
rc = OQS_SIG_STFL_sigs_total((const OQS_SIG_STFL *)sig_obj, &max_num_sigs, (const OQS_SIG_STFL_SECRET_KEY *)sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get max number of sig from %s.\n", method_name);
goto err;
}
rc = OQS_SIG_STFL_sigs_remaining((const OQS_SIG_STFL *)sig_obj, &num_sig_left, (const OQS_SIG_STFL_SECRET_KEY *)sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get the remaining number of sig from %s.\n", method_name);
goto err;
}
/* write sk key to disk */
rc = OQS_SIG_STFL_SECRET_KEY_serialize(&to_file_sk_buf, &to_file_sk_len, sk);
if (rc != OQS_SUCCESS) {
goto err;
}
file_store_name = convert_method_name_to_file_name(sig_obj->method_name);
if (oqs_fstore("sk", file_store_name, to_file_sk_buf, to_file_sk_len) != OQS_SUCCESS) {
goto err;
}
if (sk->secret_key_data == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete.\n");
goto err;
}
/* set context and secure store callback */
if (sk->set_scrt_key_store_cb != NULL) {
context = strdup(file_store_name);
sk->set_scrt_key_store_cb(sk, save_secret_key, (void *)context);
}
/* read secret key from disk */
from_file_sk_buf = malloc(to_file_sk_len);
if (oqs_fload("sk", file_store_name, from_file_sk_buf, to_file_sk_len, &from_file_sk_len) != OQS_SUCCESS) {
goto err;
}
if (to_file_sk_len != from_file_sk_len) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new stored length not equal read length\n");
goto err;
}
sk_from_file = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (sk_from_file == NULL) {
fprintf(stderr, "ERROR: 2nd OQS_SECRET_KEY_new failed\n");
goto err;
}
context_2 = strdup(file_store_name);
rc = OQS_SIG_STFL_SECRET_KEY_deserialize(sk_from_file, from_file_sk_buf, from_file_sk_len, (void *)context_2);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS restore %s from file failed.\n", method_name);
goto err;
}
rc = OQS_SUCCESS;
goto cleanup;
err:
rc = OQS_ERROR;
cleanup:
OQS_SIG_STFL_SECRET_KEY_free(sk);
OQS_SIG_STFL_SECRET_KEY_free(sk_from_file);
OQS_MEM_insecure_free(public_key);
OQS_MEM_secure_free(to_file_sk_buf, to_file_sk_len);
OQS_MEM_secure_free(from_file_sk_buf, from_file_sk_len);
OQS_SIG_STFL_free(sig_obj);
OQS_MEM_insecure_free(context);
OQS_MEM_insecure_free(context_2);
OQS_MEM_insecure_free(file_store_name);
return rc;
}
#ifdef OQS_ENABLE_TEST_CONSTANT_TIME
static void TEST_SIG_STFL_randombytes(uint8_t *random_array, size_t bytes_to_read) {
// We can't make direct calls to the system randombytes on some platforms,
// so we have to swap out the OQS_randombytes provider.
OQS_randombytes_switch_algorithm("system");
OQS_randombytes(random_array, bytes_to_read);
OQS_randombytes_custom_algorithm(&TEST_SIG_STFL_randombytes);
// OQS_TEST_CT_CLASSIFY tells Valgrind's memcheck tool to issue a warning if
// the program branches on any byte that depends on random_array. This helps us
// identify timing side-channels, as these bytes often contain secret data.
OQS_TEST_CT_CLASSIFY(random_array, bytes_to_read);
}
#endif
#if OQS_USE_PTHREADS_IN_TESTS
static OQS_STATUS sig_stfl_test_query_key(const char *method_name) {
OQS_STATUS rc = OQS_SUCCESS;
size_t message_len_1 = sizeof(message_1);
size_t message_len_2 = sizeof(message_2);
/*
* Temporarily skip algs with long key generation times.
*/
printf("================================================================================\n");
printf("Testing stateful Signature Verification %s\n", method_name);
printf("================================================================================\n");
if ( lock_test_sk == NULL || lock_test_sig_obj == NULL ||
signature_1 == NULL || signature_2 == NULL ||
lock_test_public_key == NULL) {
return OQS_ERROR;
}
printf("================================================================================\n");
printf("Sig Verify 1 %s\n", method_name);
printf("================================================================================\n");
rc = OQS_SIG_STFL_verify(lock_test_sig_obj, message_1, message_len_1, signature_1, signature_len_1, lock_test_public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_verify failed\n");
goto err;
}
printf("================================================================================\n");
printf("Sig Verify 2 %s\n", method_name);
printf("================================================================================\n");
rc = OQS_SIG_STFL_verify(lock_test_sig_obj, message_2, message_len_2, signature_2, signature_len_2, lock_test_public_key);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_verify failed\n");
goto err;
}
printf("================================================================================\n");
printf("Stateful Signature Verification %s Passed.\n", method_name);
printf("================================================================================\n");
return OQS_SUCCESS;
err:
return OQS_ERROR;
}
static OQS_STATUS sig_stfl_test_sig_gen(const char *method_name) {
OQS_STATUS rc = OQS_SUCCESS;
size_t message_len_1 = sizeof(message_1);
size_t message_len_2 = sizeof(message_2);
char *context = NULL;
char *key_store_name = NULL;
/*
* Temporarily skip algs with long key generation times.
*/
printf("================================================================================\n");
printf("Testing stateful Signature Generation %s\n", method_name);
printf("================================================================================\n");
if ( lock_test_sk == NULL || lock_test_sig_obj == NULL) {
return OQS_ERROR;
}
key_store_name = convert_method_name_to_file_name(method_name);
/* set context and secure store callback */
context = strdup(((key_store_name)));
OQS_SIG_STFL_SECRET_KEY_SET_store_cb(lock_test_sk, save_secret_key, (void *)context);
/*
* Get max num signature and the amount remaining
*/
unsigned long long num_sig_left = 0, max_num_sigs = 0;
rc = OQS_SIG_STFL_sigs_total((const OQS_SIG_STFL *)lock_test_sig_obj, &max_num_sigs, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get max number of sig from %s.\n", method_name);
goto err;
}
rc = OQS_SIG_STFL_sigs_remaining((const OQS_SIG_STFL *)lock_test_sig_obj, &num_sig_left, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get the remaining number of sig from %s.\n", method_name);
goto err;
}
printf("================================================================================\n");
printf("Sig Gen 1 %s\n", method_name);
printf("================================================================================\n");
signature_1 = malloc(lock_test_sig_obj->length_signature);
rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_1, &signature_len_1, message_1, message_len_1, lock_test_sk);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed\n");
goto err;
}
/*
* Get max num signature and the amount remaining
*/
num_sig_left = 0, max_num_sigs = 0;
rc = OQS_SIG_STFL_sigs_total((const OQS_SIG_STFL *)lock_test_sig_obj, &max_num_sigs, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get max number of sig from %s.\n", method_name);
goto err;
}
rc = OQS_SIG_STFL_sigs_remaining((const OQS_SIG_STFL *)lock_test_sig_obj, &num_sig_left, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get the remaining number of sig from %s.\n", method_name);
goto err;
}
printf("================================================================================\n");
printf("Sig Gen 2 %s\n", method_name);
printf("================================================================================\n");
signature_2 = malloc(lock_test_sig_obj->length_signature);
rc = OQS_SIG_STFL_sign(lock_test_sig_obj, signature_2, &signature_len_2, message_2, message_len_2, lock_test_sk);
OQS_TEST_CT_DECLASSIFY(&rc, sizeof rc);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "ERROR: lock thread test OQS_SIG_STFL_sign failed\n");
goto err;
}
printf("================================================================================\n");
printf("Stateful Key Gen %s Passed.\n", method_name);
printf("================================================================================\n");
/*
* Get max num signature and the amount remaining
*/
num_sig_left = 0, max_num_sigs = 0;
rc = OQS_SIG_STFL_sigs_total((const OQS_SIG_STFL *)lock_test_sig_obj, &max_num_sigs, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get max number of sig from %s.\n", method_name);
goto err;
}
rc = OQS_SIG_STFL_sigs_remaining((const OQS_SIG_STFL *)lock_test_sig_obj, &num_sig_left, (const OQS_SIG_STFL_SECRET_KEY *)lock_test_sk);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key: Failed to get the remaining number of sig from %s.\n", method_name);
goto err;
}
rc = OQS_SUCCESS;
goto cleanup;
err:
rc = OQS_ERROR;
cleanup:
OQS_MEM_insecure_free(context);
OQS_MEM_insecure_free(key_store_name);
return rc;
}
static OQS_STATUS sig_stfl_test_secret_key_lock(const char *method_name, const char *katfile) {
OQS_STATUS rc = OQS_SUCCESS;
printf("================================================================================\n");
printf("Testing stateful Signature locks %s\n", method_name);
printf("================================================================================\n");
/*
* Temporarily skip algs with long key generation times.
*/
printf("================================================================================\n");
printf("Create stateful Signature %s\n", method_name);
printf("================================================================================\n");
lock_test_sig_obj = OQS_SIG_STFL_new(method_name);
if (lock_test_sig_obj == NULL) {
fprintf(stderr, "ERROR: OQS_SIG_STFL_new failed\n");
goto err;
}
lock_test_public_key = malloc(lock_test_sig_obj->length_public_key * sizeof(uint8_t));
printf("================================================================================\n");
printf("Create stateful Secret Key %s\n", method_name);
printf("================================================================================\n");
lock_test_sk = OQS_SIG_STFL_SECRET_KEY_new(method_name);
if (lock_test_sk == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new failed\n");
goto err;
}
OQS_SIG_STFL_SECRET_KEY_SET_lock(lock_test_sk, lock_sk_key);
OQS_SIG_STFL_SECRET_KEY_SET_unlock(lock_test_sk, unlock_sk_key);
OQS_SIG_STFL_SECRET_KEY_SET_mutex(lock_test_sk, test_sk_lock);
printf("================================================================================\n");
printf("Generate keypair %s\n", method_name);
printf("================================================================================\n");
rc = sig_stfl_KATs_keygen(lock_test_sig_obj, lock_test_public_key, lock_test_sk, katfile);
if (rc != OQS_SUCCESS) {
fprintf(stderr, "OQS STFL key gen failed.\n");
goto err;
}
if (lock_test_sk->secret_key_data == NULL) {
fprintf(stderr, "ERROR: OQS_SECRET_KEY_new incomplete.\n");
goto err;
}
/* set context and secure store callback */
if (lock_test_sk->set_scrt_key_store_cb) {
lock_test_context = convert_method_name_to_file_name(method_name);
lock_test_sk->set_scrt_key_store_cb(lock_test_sk, save_secret_key, (void *)lock_test_context);
}
return OQS_SUCCESS;
err:
return OQS_ERROR;
}
typedef struct thread_data {
const char *alg_name;
const char *katfile;
OQS_STATUS rc;
// OQS_STATUS rc1;
} thread_data_t;
typedef struct lock_test_data {
const char *alg_name;
const char *katfile;
OQS_STATUS rc;
} lock_test_data_t;
void *test_query_key(void *arg) {
struct lock_test_data *td = arg;
printf("\n%s: Start Query Stateful Key info\n", __func__);
td->rc = sig_stfl_test_query_key(td->alg_name);
printf("%s: End Query Stateful Key info\n\n", __func__);
return NULL;
}
void *test_sig_gen(void *arg) {
struct lock_test_data *td = arg;
printf("\n%s: Start Generate Stateful Signature\n", __func__);
td->rc = sig_stfl_test_sig_gen(td->alg_name);
printf("%s: End Generate Stateful Signature\n\n", __func__);
return NULL;