-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_sign.cpp
285 lines (256 loc) · 9.8 KB
/
test_sign.cpp
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
#include <cstdio>
#include <stdbool.h>
#include <string.h>
#include <exception>
#include "api.h"
#include "test_sphincs.h"
//
// This tests out the signing process
// There isn't a great deal to check out (do we generate valid
// signatures? Do the various APIs work as intended?), but we
// do what we can...
typedef bool (*random_function)( void *target, size_t num_bytes );
//
// This is an 'RNG' that always gives a fixed pattern
static bool fixed_rng( void *target, size_t num_bytes ) {
unsigned char *p = (unsigned char *)target;
while (num_bytes) {
*p++ = num_bytes--;
}
return true;
}
//
// This is an 'RNG' that always a different pattern each time
// This uses CRC-16; not exactly cryptographicall secure
// especially since we advance the CRC only once for each
// byte output), but good enough for our purposes
static bool good_rng( void *target, size_t num_bytes ) {
unsigned char *p = (unsigned char *)target;
static uint32_t seed = 0x01;
while (num_bytes--) {
*p++ = seed & 0xff;
uint32_t feedback = (1&(seed >> 15)) * 0x12F15;
seed = (seed << 1) ^ feedback;
}
return true;
}
class sign_test {
bool fast_flag;
enum noise_level level;
public:
sign_test( bool flg, enum noise_level lev ) {
fast_flag = flg;
level = lev;
}
bool run( sphincs_plus::key& k, sphincs_plus::key& v, const char *name, bool always );
};
//
// We have two different signature APIs; these give a copy
// interface to both
// We also try it with the default opt_rand and having the caller
// explicitly pass one in
static bool sign1( sphincs_plus::key& k,
const unsigned char *msg, size_t len_msg,
unsigned char *sig_buffer) {
return k.sign( sig_buffer, k.len_signature(),
msg, len_msg ) &&
k.verify( sig_buffer, k.len_signature(), msg, len_msg );
}
static bool sign1( sphincs_plus::key& k,
const unsigned char *msg, size_t len_msg,
unsigned char *sig_buffer,
random_function rand ) {
return k.sign( sig_buffer, k.len_signature(),
msg, len_msg, rand ) &&
k.verify( sig_buffer, k.len_signature(), msg, len_msg );
}
static bool sign2( sphincs_plus::key& k,
const unsigned char *msg, size_t len_msg,
unsigned char *sig_buffer) {
try {
auto sig = k.sign( msg, len_msg );
memcpy( sig_buffer, sig.get(), k.len_signature() );
return k.verify( sig_buffer, k.len_signature(), msg, len_msg );
} catch(std::exception& e) {
return false;
}
}
static bool sign2( sphincs_plus::key& k,
const unsigned char *msg, size_t len_msg,
unsigned char *sig_buffer,
random_function rand ) {
try {
auto sig = k.sign( msg, len_msg, rand );
memcpy( sig_buffer, sig.get(), k.len_signature() );
return k.verify( sig_buffer, k.len_signature(), msg, len_msg );
} catch(std::exception& e) {
return false;
}
}
bool sign_test::run( sphincs_plus::key& k, sphincs_plus::key& p,
const char* parameter_set_name, bool always ) {
// If we're running in fast mode, skip any parameter set that is not
// marked as always
if (fast_flag && !always) return true;
if (level == loud) {
printf( " Checking %s\n", parameter_set_name);
}
// Allocate the two signature buffers we'll use below
unsigned len_sig = k.len_signature();
std::unique_ptr<unsigned char[]>sig( new unsigned char[len_sig] );
std::unique_ptr<unsigned char[]>sig2( new unsigned char[len_sig] );
// Make sure that signing with an uninitialized key doesn't work
static const unsigned char msg[1] = { 'x' }; /* Marks the spot */
if (sign1( k, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH UNINITIALIZED KEY WORKED\n" );
return false;
}
if (sign2( k, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH UNINITIALIZED KEY WORKED\n" );
return false;
}
// Now initialize k with a private key
if (!k.generate_key_pair()) {
printf( "*** KEY GENERATION FAILURE\n" );
return false;
}
// Make sure that signing with that private key works
if (!sign1( k, msg, sizeof msg, sig.get())) {
printf( "*** SIGNATURE GENERATION FAILURE 1a\n" );
return false;
}
if (!sign2( k, msg, sizeof msg, sig.get())) {
printf( "*** SIGNATURE GENERATION FAILURE 1b\n" );
return false;
}
// Now try the same with a key initialized with a public key
p.set_public_key( k.get_public_key() );
if (sign1( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH PUBLIC KEY WORKED\n" );
return false;
}
if (sign2( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH PUBLIC KEY WORKED\n" );
return false;
}
// Now pass the private key to the other key, and make sure that
// works
p.set_private_key( k.get_private_key() );
if (!sign1( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION FAILURE 2\n" );
return false;
}
if (!sign2( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION FAILURE r3\n" );
return false;
}
// Now we have checks on whether the signature is consistent when
// it should be
if (!sign1( k, msg, sizeof msg, sig.get(), 0 ) ||
!sign2( p, msg, sizeof msg, sig2.get(), 0 )) {
printf( "*** SIGNATURE GENERATION FAILURE 4\n" );
return false;
}
if (0 != memcmp( sig.get(), sig2.get(), len_sig )) {
printf( "*** DETERMANISTIC SIGNATURE GENERATION GAVE TWO DIFFERENT SIGNATURES\n" );
return false;
}
if (!sign1( k, msg, sizeof msg, sig.get(), fixed_rng ) ||
!sign2( p, msg, sizeof msg, sig2.get(), fixed_rng )) {
printf( "*** SIGNATURE GENERATION FAILURE 5\n" );
return false;
}
if (0 != memcmp( sig.get(), sig2.get(), len_sig )) {
printf( "*** SIGNATURE GENERATION REPEATING RNG GAVE TWO DIFFERENT SIGNATURES\n" );
return false;
}
// Now we have checks to see that the signature is different when it
// should be
if (!sign1( k, msg, sizeof msg, sig.get() ) ||
!sign2( p, msg, sizeof msg, sig2.get() )) {
printf( "*** SIGNATURE GENERATION FAILURE 6\n" );
return false;
}
if (0 == memcmp( sig.get(), sig2.get(), len_sig )) {
printf( "*** RANDOM SIGNATURE GENERATION GAVE THE SAME SIGNATURE\n" );
return false;
}
if (!sign1( k, msg, sizeof msg, sig.get(), good_rng ) ||
!sign2( p, msg, sizeof msg, sig2.get(), good_rng )) {
printf( "*** SIGNATURE GENERATION FAILURE 7\n" );
return false;
}
if (0 == memcmp( sig.get(), sig2.get(), len_sig )) {
printf( "*** RANDOM SIGNATURE GENERATION GAVE THE SAME SIGNATURE\n" );
return false;
}
// Check that we fail if the signature buffer isn't long enough
if (k.sign( sig.get(), len_sig - 1, msg, sizeof msg )) {
printf( "*** SIGNATURE GENERATION WITH TOO SMALL BUFFER WORKED\n" );
return false;
}
// Now initialize p (that had the private key) with the public key,
// make sure it lost the right to sign
p.set_public_key( k.get_public_key() );
if (sign1( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH PUBLIC KEY WORKED\n" );
return false;
}
if (sign2( p, msg, sizeof msg, sig.get() )) {
printf( "*** SIGNATURE GENERATION WITH PUBLIC KEY WORKED\n" );
return false;
}
return true;
}
#define CONCAT( A, B ) A##B
#define RUN_TEST(PARM_SET, always) { \
CONCAT( sphincs_plus::key_, PARM_SET) k, k2; \
if (!s.run( k, k2, #PARM_SET, always )) { \
return false; \
} \
}
bool test_sign(bool fast_flag, enum noise_level level) {
sign_test s( fast_flag, level );
// By default, we check all the 'F' parameter sets (they're fast)
// and selected 'S' parameter sets
// L1 parameter sets
RUN_TEST( sha256_128s_simple, false );
RUN_TEST( sha256_128f_simple, true );
RUN_TEST( sha256_128s_robust, false );
RUN_TEST( sha256_128f_robust, true );
RUN_TEST( shake256_128s_simple, false );
RUN_TEST( shake256_128f_simple, true );
RUN_TEST( shake256_128s_robust, false );
RUN_TEST( shake256_128f_robust, true );
RUN_TEST( haraka_128s_simple, true );
RUN_TEST( haraka_128f_simple, true );
RUN_TEST( haraka_128s_robust, false );
RUN_TEST( haraka_128f_robust, true );
// L3 parameter sets
RUN_TEST( sha256_192s_simple, false );
RUN_TEST( sha256_192f_simple, true );
RUN_TEST( sha256_192s_robust, false );
RUN_TEST( sha256_192f_robust, true );
RUN_TEST( shake256_192s_simple, false );
RUN_TEST( shake256_192f_simple, true );
RUN_TEST( shake256_192s_robust, false );
RUN_TEST( shake256_192f_robust, true );
RUN_TEST( haraka_192s_simple, false );
RUN_TEST( haraka_192f_simple, true );
RUN_TEST( haraka_192s_robust, false );
RUN_TEST( haraka_192f_robust, true );
// L5 parameter sets
RUN_TEST( sha256_256s_simple, false );
RUN_TEST( sha256_256f_simple, true );
RUN_TEST( sha256_256s_robust, false );
RUN_TEST( sha256_256f_robust, true );
RUN_TEST( shake256_256s_simple, false );
RUN_TEST( shake256_256f_simple, true );
RUN_TEST( shake256_256s_robust, false );
RUN_TEST( shake256_256f_robust, true );
RUN_TEST( haraka_256s_simple, false );
RUN_TEST( haraka_256f_simple, true );
RUN_TEST( haraka_256s_robust, false );
RUN_TEST( haraka_256f_robust, true );
return true;
}