-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_thread.cpp
More file actions
114 lines (94 loc) · 3.34 KB
/
test_thread.cpp
File metadata and controls
114 lines (94 loc) · 3.34 KB
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
#include <cstdio>
#include <stdbool.h>
#include <string.h>
#include <exception>
#include "api.h"
#include "test_sphincs.h"
//
// This tests out the various thread options
class thread_test {
bool fast_flag;
enum noise_level level;
public:
thread_test( bool flg, enum noise_level lev ) {
fast_flag = flg;
level = lev;
}
bool run( slh_dsa::key& k, const char *name, bool always );
};
// We need to generate the signatures in determanistic mode
static bool sign( slh_dsa::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, 0, 0, 0 ) &&
k.verify( sig_buffer, k.len_signature(), msg, len_msg );
}
bool thread_test::run( slh_dsa::key& k,
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] );
// Now initialize k with a private key
if (!k.generate_key_pair()) {
printf( "*** KEY GENERATION FAILURE\n" );
return false;
}
static const unsigned char msg[1] = { '@' }; /* Right here */
// Generate the signature with one thread
k.set_num_thread(1);
if (!sign( k, msg, sizeof msg, sig.get())) {
printf( "*** SIGNATURE GENERATION FAILURE\n" );
return false;
}
// Iterate through the possible number of threads, and make
// sure they generate the same signature
for (int thread = 2; thread <= 16; thread++) {
k.set_num_thread(thread);
memset( sig2.get(), 0, len_sig );
if (!sign( k, msg, sizeof msg, sig2.get())) {
printf( "*** SIGNATURE GENERATION FAILURE\n" );
return false;
}
if (0 != memcmp( sig.get(), sig2.get(), len_sig )) {
printf( "*** SIGNATURE GENERATION INCONSISTENT\n" );
return false;
}
}
return true;
}
#define CONCAT( A, B ) A##B
#define RUN_TEST(PARM_SET, always) { \
CONCAT( slh_dsa::key_, PARM_SET) k; \
if (!s.run( k, #PARM_SET, always )) { \
return false; \
} \
}
bool test_thread(bool fast_flag, enum noise_level level) {
thread_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( sha2_128s, true );
RUN_TEST( sha2_128f, true );
RUN_TEST( shake_128s, false );
RUN_TEST( shake_128f, true );
// L3 parameter sets
RUN_TEST( sha2_192s, false );
RUN_TEST( sha2_192f, true );
RUN_TEST( shake_192s, false );
RUN_TEST( shake_192f, true );
// L5 parameter sets
RUN_TEST( sha2_256s, false );
RUN_TEST( sha2_256f, true );
RUN_TEST( shake_256s, false );
RUN_TEST( shake_256f, true );
return true;
}