forked from cisco/hash-sigs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_reserve.c
252 lines (230 loc) · 8.85 KB
/
test_reserve.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
/*
* This tests out the reservation; both the manual and the autoreserve
*
* The general strategy is to do reservations (both autoreserve and manual),
* maintain a separate counter tracking when we ought to update the
* private key, and see if we update it at the proper times (and by the
* expected updates)
*/
#include "test_hss.h"
#include "hss.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int rand_seed;
static int my_rand(void) {
rand_seed += rand_seed*rand_seed | 5;
return rand_seed >> 9;
}
static bool rand_1( void *output, size_t len) {
unsigned char *p = output;
while (len--) *p++ = my_rand();
return true;
}
static unsigned char priv_key[HSS_MAX_PRIVATE_KEY_LEN];
static unsigned long last_seqno;
static bool got_update;
static bool got_error;
static bool hit_end;
static bool read_private_key(unsigned char *private_key,
size_t len_private_key, void *context) {
if (len_private_key > HSS_MAX_PRIVATE_KEY_LEN) return false;
memcpy( private_key, priv_key, len_private_key );
return true;
}
static bool update_private_key(unsigned char *private_key,
size_t len_private_key, void *context) {
if (len_private_key > HSS_MAX_PRIVATE_KEY_LEN || len_private_key < 8) return false;
memcpy( priv_key, private_key, len_private_key );
got_update = true;
hit_end = false;
got_error = false;
int i;
for (i=0; i<8; i++) {
if (private_key[i] != 0xff) break;
}
if (i == 8) {
hit_end = true;
return true;
}
/* Our tests never have seqno's larger than 2**32-1 */
/* If we see any larger claimed, it's an error */
for (i=0; i<4; i++) {
if (private_key[i] != 0x00) {
got_error = true;
return true;
}
}
/* Pull out the sequence number from the private key */
last_seqno = 0;
for (i=4; i<8; i++) {
last_seqno = 256*last_seqno + private_key[i];
}
return true;
}
bool test_reserve(bool fast_flag, bool quiet_flag) {
int reserve, do_manual_res;
/* d=1 makes it esay to extract the sequence number from */
/* the signature */
int default_d = 1;
param_set_t default_lm_type[1] = { LMS_SHA256_N32_H10 };
param_set_t default_ots_type[1] = { LMOTS_SHA256_N32_W2 };
for (do_manual_res = 0; do_manual_res <= 1; do_manual_res++) {
/* In full mode, we also run the tests skipping all manual */
/* reservations; this makes sure that the autoreservations are */
/* tested in all situations */
if (fast_flag && !do_manual_res) continue;
for (reserve = 0; reserve < 40; reserve++) {
rand_seed = 2*reserve + do_manual_res;
unsigned char pub_key[ 200 ];
unsigned char aux_data[ 200 ];
if (!hss_generate_private_key( rand_1,
default_d, default_lm_type, default_ots_type,
update_private_key, NULL,
pub_key, sizeof pub_key,
aux_data, sizeof aux_data,
NULL)) {
printf( "Error: unable to create private key\n" );
return false;
}
struct hss_working_key *w = hss_load_private_key(
read_private_key, NULL, 50000,
aux_data, sizeof aux_data, NULL );
if (!w) {
printf( "Error: unable to load private key\n" );
return false;
}
if (reserve > 0) {
if (!hss_set_autoreserve(w, reserve, NULL)) {
hss_free_working_key(w);
printf( "Error: unable to do autoreseserve\n" );
return false;
}
}
unsigned i;
unsigned reserved = 0; /* Our model for how many are reserved */
for (i=0; i<=1024; i++) {
/* During the manual_res test, we randomly ask for manual */
/* reservations */
if (do_manual_res && (my_rand() & 0x1f) == 0x0d) {
unsigned manual_res = my_rand() & 0x0f;
got_update = false;
if (!hss_reserve_signature(w, update_private_key, NULL,
manual_res, NULL)) {
hss_free_working_key(w);
printf( "Error: unable to do manual reserve\n" );
return false;
}
if (got_update && got_error) {
hss_free_working_key(w);
printf( "Error: manual reservation: set private key "
"to illegal value\n" );
return false;
}
if (manual_res > 1023 - i) manual_res = 1023 - i;
if (manual_res <= reserved) {
if (got_update) {
hss_free_working_key(w);
printf( "Error: got update from manual "
"reservation: not expected\n" );
return false;
}
} else {
if (!got_update) {
hss_free_working_key(w);
printf( "Error: no update from manual "
"reservation: expected one\n" );
return false;
}
if (hit_end) {
hss_free_working_key(w);
printf( "Error: manual reservation "
"invaliated key\n" );
return false;
}
if (manual_res + i != last_seqno) {
hss_free_working_key(w);
printf( "Error: manual reservation set "
"wrong seqno\n" );
return false;
}
reserved = manual_res;
}
}
char message[ 100 ];
size_t len_message = sprintf( message, "Message #%d", i );
got_update = false;
struct hss_extra_info info = { 0 };
unsigned char signature[ 16000 ];
if (!hss_generate_signature(w, update_private_key, NULL,
message, len_message,
signature, sizeof signature,
&info )) {
hss_free_working_key(w);
printf( "Error: unable to sign %d\n", info.error_code );
return false;
}
/* Make sure that the index used in the signature is what we */
/* expect */
unsigned long sig_index = (signature[4] << 24UL) +
(signature[5] << 16UL) +
(signature[6] << 8UL) +
(signature[7] );
if (i != sig_index) {
hss_free_working_key(w);
printf( "Error: unexpected signature index\n" );
return false;
}
if (got_update && got_error) {
hss_free_working_key(w);
printf( "Error: signature set private key "
"to illegal value\n" );
return false;
}
if (reserved > 0 && i < 1023) {
if (got_update) {
hss_free_working_key(w);
printf( "Error: siganture unexpectedly set "
"private key " );
return false;
}
reserved--;
} else {
if (!got_update) {
hss_free_working_key(w);
printf( "Error: siganture did not set private key " );
return false;
}
if (i == 1023) {
if (!hit_end) {
hss_free_working_key(w);
printf( "Error: reservation at end "
"did not invaliate key\n" );
return false;
}
} else {
int expected_seqno = i + 1 + reserve;
if (expected_seqno >= 1024) expected_seqno = 1023;
if (hit_end) {
hss_free_working_key(w);
printf( "Error: reservation in middle "
"invaliated key\n" );
return false;
}
if (expected_seqno != last_seqno) {
hss_free_working_key(w);
printf( "Error: autoreservation set "
"unexpected sequence number\n" );
return false;
}
reserved = reserve;
}
}
if (hss_extra_info_test_last_signature( &info )) {
break;
}
}
hss_free_working_key(w);
} }
return true;
}