-
Notifications
You must be signed in to change notification settings - Fork 34
/
test_sign_inc.c
376 lines (334 loc) · 12.9 KB
/
test_sign_inc.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
/*
* This bangs on the incremental version of the signature generation logic
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hss.h"
#include "hss_sign_inc.h"
#include "test_hss.h"
static bool generate_random(void *output, size_t length) {
unsigned char *p = output;
int i = 1;
while (length--) {
*p++ = i++;
}
return true;
}
/* We have no reason to write the key updates anywhere */
static bool ignore_update(unsigned char *private_key, size_t len, void *ctx) {
return true;
}
static bool run_test(int d, param_set_t *lm_array, param_set_t *lm_ots_array,
unsigned num_iter, bool at_end) {
size_t len_private_key = hss_get_private_key_len(d, lm_array, lm_ots_array );
if (len_private_key == 0 || len_private_key > HSS_MAX_PRIVATE_KEY_LEN) {
printf( " Len private key failed\n" );
return false;
}
unsigned char private_key[HSS_MAX_PRIVATE_KEY_LEN];
unsigned len_public_key = hss_get_public_key_len(d, lm_array, lm_ots_array );
if (len_public_key == 0 || len_public_key > HSS_MAX_PUBLIC_KEY_LEN) {
printf( " Len public key failed\n" );
return false;
}
size_t len_sig = hss_get_signature_len(d, lm_array, lm_ots_array );
if (len_sig == 0) {
printf( " Len signature failed\n" );
return false;
}
unsigned char public_key[HSS_MAX_PUBLIC_KEY_LEN];
unsigned char aux_data[1000];
/* Generate the public key */
if (!hss_generate_private_key(
generate_random,
d, lm_array, lm_ots_array,
NULL, private_key,
public_key, len_public_key,
aux_data, sizeof aux_data, 0 )) {
printf( " Gen private key failed\n" );
return false;
}
/* Load the private key into memory (twice!) */
struct hss_working_key *w = hss_load_private_key(
NULL, private_key,
0, /* Minimal memory */
aux_data, sizeof aux_data, 0 );
struct hss_working_key *w2 = hss_load_private_key(
NULL, private_key,
0, /* Minimal memory */
aux_data, sizeof aux_data, 0 );
if (!w || !w2) {
printf( " *** failed loading private key\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
return false;
}
unsigned i;
unsigned char *sig_1 = malloc(len_sig);
unsigned char *sig_2 = malloc(len_sig);
if (!sig_1 || !sig_2) {
free(sig_1); free(sig_2);
return false;
}
for (i = 0; i<num_iter; i++) {
/* Generate a signature using the standard API */
unsigned char message[3] = "ABC";
if (!hss_generate_signature( w, ignore_update, NULL,
message, sizeof message,
sig_1, len_sig, 0 )) {
printf( " *** failed normal signature\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
/* Now, do the same using the incremental API */
struct hss_sign_inc ctx;
struct hss_extra_info info;
hss_init_extra_info( &info );
if (!hss_sign_init(&ctx, w2, ignore_update, NULL,
sig_2, len_sig, &info )) {
printf( " *** failed signature init\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
/* Check if the hit-end flag we were returned is what we should */
/* expect */
if (hss_extra_info_test_last_signature(&info) !=
(at_end && (i+1 == num_iter))) {
printf( " *** at-end flag not correct\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
if (!hss_sign_update( &ctx, "A", 1) ||
!hss_sign_update( &ctx, "BC", 2)) {
printf( " *** failed signature update\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
if (!hss_sign_finalize( &ctx, w2, sig_2, 0)) {
printf( " *** failed signature finalize\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
/* Check if the two signatures are the same */
if (0 != memcmp( sig_1, sig_2, len_sig )) {
printf( " *** Generated different signatures\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
}
/* If we're supposed to be at the end, make sure asking for another */
/* signature fails */
if (at_end) {
struct hss_sign_inc ctx;
struct hss_extra_info info = { 0 };
if (hss_sign_init(&ctx, w2, ignore_update, NULL,
sig_2, len_sig, &info )) {
printf( " *** signinit succeeded when it should have failed\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
if (hss_extra_info_test_error_code(&info) != hss_error_private_key_expired) {
printf( " *** signinit gave incorrect error code\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return false;
}
}
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig_1); free(sig_2);
return true;
}
static bool run_test_2(int d, param_set_t *lm_array, param_set_t *lm_ots_array,
unsigned num_iter) {
size_t len_private_key = hss_get_private_key_len(d, lm_array, lm_ots_array );
if (len_private_key == 0 || len_private_key > HSS_MAX_PRIVATE_KEY_LEN) {
printf( " Len private key failed\n" );
return false;
}
unsigned char private_key[HSS_MAX_PRIVATE_KEY_LEN];
unsigned len_public_key = hss_get_public_key_len(d, lm_array, lm_ots_array );
if (len_public_key == 0 || len_public_key > HSS_MAX_PUBLIC_KEY_LEN) {
printf( " Len public key failed\n" );
return false;
}
size_t len_sig = hss_get_signature_len(d, lm_array, lm_ots_array );
if (len_sig == 0) {
printf( " Len signature failed\n" );
return false;
}
unsigned char public_key[HSS_MAX_PUBLIC_KEY_LEN];
unsigned char aux_data[1000];
/* Generate the public key */
if (!hss_generate_private_key(
generate_random,
d, lm_array, lm_ots_array,
NULL, private_key,
public_key, len_public_key,
aux_data, sizeof aux_data, 0 )) {
printf( " Gen private key failed\n" );
return false;
}
/* Load the private key into memory (twice!) */
struct hss_working_key *w = hss_load_private_key(
NULL, private_key,
0, /* Minimal memory */
aux_data, sizeof aux_data, 0 );
struct hss_working_key *w2 = hss_load_private_key(
NULL, private_key,
0, /* Minimal memory */
aux_data, sizeof aux_data, 0 );
if (!w || !w2) {
printf( " *** failed loading private key\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
return false;
}
unsigned char *sig = malloc( len_sig * num_iter );
struct hss_sign_inc *ctx = malloc(sizeof(struct hss_sign_inc) * num_iter);
unsigned char *sig_2 = malloc(len_sig);
if (!sig || !ctx || !sig_2) {
printf( " *** memory allocation failure\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
unsigned i;
for (i = 0; i<num_iter; i++) {
/* Start the signature with the incremental API */
if (!hss_sign_init(&ctx[i], w2, ignore_update, NULL,
&sig[i * len_sig], len_sig, 0 )) {
printf( " *** failed signature init\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
/* Start updating */
if (!hss_sign_update( &ctx[i], "AB", 2)) {
printf( " *** failed signature update\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
}
for (i = 0; i<num_iter; i++) {
/* Generate a signature using the standard API */
unsigned char message[3] = "ABC";
if (!hss_generate_signature( w, ignore_update, NULL,
message, sizeof message,
sig_2, len_sig, 0 )) {
printf( " *** failed normal signature\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
/* Now, finish the signature with the incremental API */
if (!hss_sign_update( &ctx[i], "C", 1)) {
printf( " *** failed signature update\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
if (!hss_sign_finalize( &ctx[i], w2, &sig[i * len_sig], 0)) {
printf( " *** failed signature finalize\n" );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
/* Check if the two signatures are the same */
if (0 != memcmp( &sig[i * len_sig], sig_2, len_sig )) {
printf( " *** Generated different signatures i = %d\n", i );
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return false;
}
}
hss_free_working_key(w);
hss_free_working_key(w2);
free(sig); free(ctx); free(sig_2);
return true;
}
bool test_sign_inc(bool fast_flag, bool quiet_flag) {
/*
* First set of tests; for several different parameter sets, create the
* same key with two different working_keys; generate signatures with both
* (one using the standard API, and one with the incremental, and see if
* they match
*/
{
int d = 1;
param_set_t lm_array[1] = { LMS_SHA256_N32_H5 };
param_set_t lm_ots_array[1] = { LMOTS_SHA256_N32_W8 };
if (!run_test( d, lm_array, lm_ots_array, 32, true )) return false;
}
{
int d = 1;
param_set_t lm_array[1] = { LMS_SHA256_N32_H10 };
param_set_t lm_ots_array[1] = { LMOTS_SHA256_N32_W4 };
if (!run_test( d, lm_array, lm_ots_array, 1024, true )) return false;
}
{
int d = 2;
param_set_t lm_array[2] = { LMS_SHA256_N32_H5, LMS_SHA256_N32_H5 };
param_set_t lm_ots_array[2] = { LMOTS_SHA256_N32_W4, LMOTS_SHA256_N32_W2 };
if (!run_test( d, lm_array, lm_ots_array, 1024, true )) return false;
}
{
int d = 2;
param_set_t lm_array[2] = { LMS_SHA256_N32_H10, LMS_SHA256_N32_H5 };
param_set_t lm_ots_array[2] = { LMOTS_SHA256_N32_W8, LMOTS_SHA256_N32_W2 };
if (!run_test( d, lm_array, lm_ots_array, 100, false )) return false;
}
{
int d = 3;
param_set_t lm_array[3] = {
LMS_SHA256_N32_H10, LMS_SHA256_N32_H5, LMS_SHA256_N32_H5 };
param_set_t lm_ots_array[3] = {
LMOTS_SHA256_N32_W8, LMOTS_SHA256_N32_W4, LMOTS_SHA256_N32_W2 };
if (!run_test( d, lm_array, lm_ots_array, 2000, false )) return false;
}
/*
* Second test; for one particular parm set, initiate a large number of
* signature ops, but don't close them out; then, close them out in
* order, and see if they match the normal signature. This verifies that
* stepping the tree past where the original auth path was doesn't mess
* things up
* In slow mode, we make sure to step past the first penultimate Merkle
* tree (level 2 in this case), to make sure that we don't need the
* original tree there to be valid; it takes too long for fast mode
*/
{
int d = 3;
param_set_t lm_array[3] = {
LMS_SHA256_N32_H10, LMS_SHA256_N32_H5, LMS_SHA256_N32_H5 };
param_set_t lm_ots_array[3] = {
LMOTS_SHA256_N32_W8, LMOTS_SHA256_N32_W8, LMOTS_SHA256_N32_W8 };
int num_iter;
if (fast_flag) num_iter = 100; else num_iter = 10000;
if (!run_test_2( d, lm_array, lm_ots_array, num_iter )) return false;
}
return true;
}