forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec_self_tests.c
262 lines (240 loc) · 6.8 KB
/
ec_self_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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "../external_deps/print.h"
#include "../utils/utils.h"
#include "../libsig.h"
/*
* Use extern declarations to avoid including
* ec_self_tests_core.h, which has all fixed
* test vectors definitions. We only need the
* three functions below.
*/
extern int perform_known_test_vectors_test(const char *sig, const char *hash, const char *curve);
extern int perform_random_sig_verif_test(const char *sig, const char *hash, const char *curve);
extern int perform_performance_test(const char *sig, const char *hash, const char *curve);
/* Tests kinds */
#define KNOWN_TEST_VECTORS (1)
#define RANDOM_SIG_VERIF (1 << 2)
#define PERFORMANCE (1 << 3)
typedef struct {
const char *type_name;
const char *type_help;
unsigned int type_mask;
} test_type;
static const test_type test_types[] = {
{
.type_name = "vectors",
.type_help = "Perform known test vectors",
.type_mask = KNOWN_TEST_VECTORS,
},
{
.type_name = "rand",
.type_help = "Perform random sign/verify tests",
.type_mask = RANDOM_SIG_VERIF,
},
{
.type_name = "perf",
.type_help = "Performance tests",
.type_mask = PERFORMANCE,
},
};
static int perform_tests(unsigned int tests, const char *sig, const char *hash, const char *curve)
{
/* KNOWN_TEST_VECTORS tests */
if (tests & KNOWN_TEST_VECTORS) {
if (perform_known_test_vectors_test(sig, hash, curve)) {
goto err;
}
}
/* RANDOM_SIG_VERIF tests */
if (tests & RANDOM_SIG_VERIF) {
if (perform_random_sig_verif_test(sig, hash, curve)) {
goto err;
}
}
/* PERFORMANCE tests */
if (tests & PERFORMANCE) {
if (perform_performance_test(sig, hash, curve)) {
goto err;
}
}
return 0;
err:
return -1;
}
static void print_curves(void)
{
u8 i;
/* Print all the available curves */
for (i = 0; i < EC_CURVES_NUM; i++) {
ext_printf("%s ", (const char *)(ec_maps[i].params->name->buf));
}
return;
}
static void print_hash_algs(void)
{
int i;
/* Print all the available hash functions */
for (i = 0; hash_maps[i].type != UNKNOWN_HASH_ALG; i++) {
ext_printf("%s ", hash_maps[i].name);
}
return;
}
static void print_sig_algs(void)
{
int i;
/* Print all the available signature schemes */
for (i = 0; ec_sig_maps[i].type != UNKNOWN_SIG_ALG; i++) {
ext_printf("%s ", ec_sig_maps[i].name);
}
return;
}
static void print_help(const char *bad_arg)
{
int j;
if(bad_arg != NULL){
ext_printf("Argument %s is unknown. Possible args are:\n", bad_arg);
}
for (j = 0; j < (int)(sizeof(test_types) / sizeof(test_type)); j++) {
ext_printf("\t%20s:\t%s\n", test_types[j].type_name,
test_types[j].type_help);
}
ext_printf("-------------------\n");
ext_printf("NOTE: you can filter signatures with 'sign=', hash algorithms with 'hash=', curves with 'curve='\n");
ext_printf("\tExample: sign=ECDSA hash=SHA256 hash=SHA512 curve=FRP256V1\n");
ext_printf("\tPossible signatures: ");
print_sig_algs();
ext_printf("\n\tPossible hash algorithms: ");
print_hash_algs();
ext_printf("\n\tPossible curves: ");
print_curves();
ext_printf("\n");
}
#define MAX_FILTERS 100
int main(int argc, char *argv[])
{
int ret;
unsigned int tests_to_do;
const char *sign_filters[MAX_FILTERS] = { NULL };
const char *hash_filters[MAX_FILTERS] = { NULL };
const char *curve_filters[MAX_FILTERS] = { NULL };
int sign_filters_num = 0, hash_filters_num = 0, curve_filters_num = 0;
int i, j, k;
/* By default, perform all tests */
tests_to_do = KNOWN_TEST_VECTORS | RANDOM_SIG_VERIF | PERFORMANCE;
/* Sanity check */
if(MAX_FILTERS < 1){
ext_printf("Error: MAX_FILTERS too small\n");
return -1;
}
/* If we have one or more arguments, only perform specific test */
if (argc > 1) {
unsigned char found = 0, found_filter = 0;
unsigned int found_ops = 0;
/* Check of the args */
for (i = 1; i < argc; i++) {
found = found_filter = 0;
for (j = 0;
j < (int)(sizeof(test_types) / sizeof(test_type));
j++) {
if (are_equal
(argv[i], test_types[j].type_name,
local_strlen(test_types[j].type_name) +
1)) {
found_ops++;
found = 1;
break;
}
if(are_equal(argv[i], "sign=", sizeof("sign=")-1)){
if(sign_filters_num >= MAX_FILTERS){
ext_printf("Maximum number of sign filters %d exceeded!\n", sign_filters_num);
return -1;
}
sign_filters[sign_filters_num++] = argv[i]+sizeof("sign=")-1;
found_filter = 1;
break;
}
if(are_equal(argv[i], "hash=", sizeof("hash=")-1)){
if(hash_filters_num >= MAX_FILTERS){
ext_printf("Maximum number of hash filters %d exceeded!\n", hash_filters_num);
return -1;
}
hash_filters[hash_filters_num++] = argv[i]+sizeof("hash=")-1;
found_filter = 1;
break;
}
if(are_equal(argv[i], "curve=", sizeof("curve=")-1)){
if(curve_filters_num >= MAX_FILTERS){
ext_printf("Maximum number of curve filters %d exceeded!\n", curve_filters_num);
return -1;
}
curve_filters[curve_filters_num++] = argv[i]+sizeof("curve=")-1;
found_filter = 1;
break;
}
}
if ((found == 0) && (found_filter == 0)) {
print_help(argv[i]);
return -1;
}
}
if (found_ops == 0) {
if(found_filter == 0){
ext_printf("Error: no operation asked ...\n");
print_help(NULL);
return -1;
}
}
else{
tests_to_do = 0;
for (i = 1; i < argc; i++) {
for (j = 0;
j < (int)(sizeof(test_types) / sizeof(test_type));
j++) {
if (are_equal
(argv[i], test_types[j].type_name,
local_strlen(test_types[j].type_name) +
1)) {
tests_to_do |= test_types[j].type_mask;
}
}
}
}
}
/* If we do not have filters, we put NULL to tell that we do not filter */
if(sign_filters_num == 0){
sign_filters_num = 1;
sign_filters[0] = NULL;
}
if(hash_filters_num == 0){
hash_filters_num = 1;
hash_filters[0] = NULL;
}
if(curve_filters_num == 0){
curve_filters_num = 1;
curve_filters[0] = NULL;
}
for(i = 0; i < sign_filters_num; i++){
for(j = 0; j < hash_filters_num; j++){
for(k = 0; k < curve_filters_num; k++){
if((ret = perform_tests(tests_to_do, sign_filters[i], hash_filters[j], curve_filters[k]))){
ext_printf("Test for %s/%s/%s failed!\n", sign_filters[i], hash_filters[j], curve_filters[k]);
return ret;
}
}
}
}
}