Repository navigation
Expand file tree
/
Copy pathkat_test.c
More file actions
172 lines (135 loc) · 3.92 KB
/
Copy pathkat_test.c
File metadata and controls
172 lines (135 loc) · 3.92 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
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
// kat_test.c
// Markku-Juhani O. Saarinen <mjos@iki.fi>. See LICENSE.
// === KAT Testing for SLH-DSA
#ifndef SLOTH
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "slh_dsa.h"
#include "kat_drbg.h"
#ifndef KATNUM
#define KATNUM 100
#endif
// fake test drbg state
aes256_ctr_drbg_t kat_drbg, iut_drbg;
// for the callback interface
int iut_randombytes(uint8_t *x, size_t xlen)
{
aes256ctr_xof(&iut_drbg, x, xlen);
return 0;
}
static void kat_hex(FILE *fh, const char *label,
const uint8_t *x, size_t xlen)
{
size_t i;
fprintf(fh, "%s = ", label);
for (i = 0; i < xlen; i++) {
fprintf(fh, "%02X", x[i]);
}
fprintf(fh, "\n");
}
int kat_test(const slh_param_t *iut, int katnum)
{
int fail = 0;
char fn[256];
FILE *fh;
uint8_t seed[48] = { 0 };
uint8_t msg[33 * KATNUM] = { 0 };
size_t msg_sz= 0, pk_sz = 0, sk_sz = 0, sig_sz = 0, sm_sz = 0;
uint8_t pk[2 * 32] = { 0 };
uint8_t sk[4 * 32] = { 0 };
uint8_t sm[50000 + 33 * KATNUM];
snprintf(fn, sizeof(fn), "%s-%d.rsp", slh_alg_id(iut), katnum);
fh = fopen(fn, "w");
if (fh == NULL) {
perror(fn);
fail++;
return fail;
}
fprintf(fh, "# SPHINCS+\n\n");
// initialize kat seed drbg
for (int i = 0; i < 48; i++) {
seed[i] = i;
}
aes256ctr_xof_init(&kat_drbg, seed);
pk_sz = slh_pk_sz(iut);
assert(sizeof(pk) >= pk_sz);
sk_sz = slh_sk_sz(iut);
assert(sizeof(sk) >= sk_sz);
sig_sz = slh_sig_sz(iut);
for (int count = 0; count < katnum; count++) {
printf("[KAT] (%d) %s\n", count, fn);
fflush(stdout);
fprintf(fh, "count = %d\n", count);
aes256ctr_xof(&kat_drbg, seed, 48);
kat_hex(fh, "seed", seed, 48);
msg_sz = (count + 1) * 33;
fprintf(fh, "mlen = %zu\n", msg_sz);
assert(sizeof(sm) >= sig_sz + msg_sz);
aes256ctr_xof(&kat_drbg, msg, msg_sz);
kat_hex(fh, "msg", msg, msg_sz);
// initialize target drbg
aes256ctr_xof_init(&iut_drbg, seed);
slh_keygen(pk, sk, &iut_randombytes, iut);
kat_hex(fh, "pk", pk, pk_sz);
kat_hex(fh, "sk", sk, sk_sz);
sm_sz = slh_sign(sm, msg, msg_sz, sk, &iut_randombytes, iut);
memcpy(sm + sm_sz, msg, msg_sz);
sm_sz += msg_sz;
fprintf(fh, "smlen = %zu\n", sm_sz);
kat_hex(fh, "sm", sm, sm_sz);
fprintf(fh, "\n");
assert(sm_sz == sig_sz + msg_sz);
if (!slh_verify(sm + sig_sz, msg_sz, sm, pk, iut)) {
fail++;
fprintf(stderr, "[FAIL] slh_verify() fails.\n");
}
// flip random bit
uint32_t xbit = ((uint32_t) seed[4]) +
(((uint32_t) seed[5]) << 8) +
(((uint32_t) seed[6]) << 16) +
(((uint32_t) seed[7]) << 24);
xbit %= (8 * sm_sz);
sm[xbit >> 3] ^= 1 << (xbit & 7);
if (slh_verify(sm + sig_sz, msg_sz, sm, pk, iut)) {
fail++;
fprintf(stderr, "[FAIL] slh_verify() forgery bit= %u.\n", xbit);
}
}
fclose(fh);
return fail;
}
// test targets
const slh_param_t *test_iut[] = {
&slh_dsa_shake_128s,
&slh_dsa_shake_128f,
&slh_dsa_shake_192s,
&slh_dsa_shake_192f,
&slh_dsa_shake_256s,
&slh_dsa_shake_256f,
&slh_dsa_sha2_128s,
&slh_dsa_sha2_128f,
&slh_dsa_sha2_192s,
&slh_dsa_sha2_192f,
&slh_dsa_sha2_256s,
&slh_dsa_sha2_256f,
NULL
};
int main(int argc, char **argv)
{
int fail = 0;
int iut_n = 0;
if (argc == 2 &&
(iut_n = atoi(argv[1])) >= 0 &&
iut_n < 12) {
fail += kat_test(test_iut[iut_n], 1);
} else {
for (iut_n = 0; test_iut[iut_n] != NULL; iut_n++) {
fail += kat_test(test_iut[iut_n], KATNUM);
}
}
printf("[INFO] test_slh_dsa() fail= %d\n", fail);
return fail;
}
#endif