/
sign.c
387 lines (310 loc) · 10.5 KB
/
sign.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
377
378
379
380
381
382
383
384
385
386
387
#include "pqcrypto_sign.h"
#include <stdlib.h>
#include <string.h>
#include "api.h"
#include "params.h"
#include "wots.h"
#include "horst.h"
#include "hash.h"
#include <crypto_generichash.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#define BIGINT_BYTES ((TOTALTREE_HEIGHT-SUBTREE_HEIGHT+7)/8)
// unfortunately this cannot be much higher, as crypto_generichash_update does not
// seem to support updates of 1024 bits or more, after the first one
#define MBLOCKSIZE 64
#if (TOTALTREE_HEIGHT-SUBTREE_HEIGHT) > 64
#error "TOTALTREE_HEIGHT-SUBTREE_HEIGHT must be at most 64"
#endif
typedef struct{
int level;
unsigned long long subtree;
int subleaf;
} leafaddr;
static void get_seed(unsigned char seed[SEED_BYTES], const unsigned char *sk, const leafaddr *a)
{
#if (N_LEVELS > 15) && (N_LEVELS < 8)
#error "Need to have 8 <= N_LEVELS <= 15"
#endif
#if SUBTREE_HEIGHT != 5
#error "Need to have SUBTREE_HEIGHT == 5"
#endif
#if TOTALTREE_HEIGHT != 60
#error "Need to have TOTALTREE_HEIGHT == 60"
#endif
unsigned char buffer[SEED_BYTES+8];
unsigned long long t;
int i;
for(i=0;i<SEED_BYTES;i++)
buffer[i] = sk[i];
//4 bits to encode level
t = a->level;
//55 bits to encode subtree
t |= a->subtree << 4;
//5 bits to encode leaf
t |= (unsigned long long)a->subleaf << 59;
for(i=0;i<8;i++)
buffer[SEED_BYTES+i] = (t >> 8*i) & 0xff;
#if SEED_BYTES != HASH_BYTES
#error "Need to have SEED_BYTES == HASH_BYTES"
#endif
varlen_hash(seed,buffer,SEED_BYTES+8);
}
static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const unsigned char *masks)
{
int l = WOTS_L;
int i,j = 0;
for(i=0;i<WOTS_LOG_L;i++)
{
for(j=0 ;j < (l>>1);j++)
hash_2n_n_mask(wots_pk+j*HASH_BYTES,wots_pk+j*2*HASH_BYTES, masks+i*2*HASH_BYTES);
if(l&1)
{
memcpy(wots_pk+(l>>1)*HASH_BYTES,wots_pk+(l-1)*HASH_BYTES, HASH_BYTES);
l=(l>>1)+1;
}
else
l=(l>>1);
}
memcpy(leaf,wots_pk,HASH_BYTES);
}
static void gen_leaf_wots(unsigned char leaf[HASH_BYTES], const unsigned char *masks, const unsigned char *sk, const leafaddr *a)
{
unsigned char seed[SEED_BYTES];
unsigned char pk[WOTS_L*HASH_BYTES];
get_seed(seed, sk, a);
wots_pkgen(pk, seed, masks);
l_tree(leaf, pk, masks);
}
static void treehash(unsigned char *nodep, int height, const unsigned char *sk, const leafaddr *leaf, const unsigned char *masks)
{
leafaddr a = *leaf;
int lastnode,i;
unsigned char stack[(height+1)*HASH_BYTES];
unsigned int stacklevels[height+1];
unsigned int stackoffset=0;
unsigned int maskoffset =0;
lastnode = a.subleaf+(1<<height);
for(;a.subleaf<lastnode;a.subleaf++)
{
gen_leaf_wots(stack+stackoffset*HASH_BYTES,masks,sk,&a);
stacklevels[stackoffset] = 0;
stackoffset++;
while(stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
{
//MASKS
maskoffset = 2*(stacklevels[stackoffset-1] + WOTS_LOG_L)*HASH_BYTES;
hash_2n_n_mask(stack+(stackoffset-2)*HASH_BYTES,stack+(stackoffset-2)*HASH_BYTES,
masks+maskoffset);
stacklevels[stackoffset-2]++;
stackoffset--;
}
}
for(i=0;i<HASH_BYTES;i++)
nodep[i] = stack[i];
}
static void validate_authpath(unsigned char root[HASH_BYTES], const unsigned char leaf[HASH_BYTES], unsigned int leafidx, const unsigned char *authpath, const unsigned char *masks, unsigned int height)
{
unsigned int i,j;
unsigned char buffer[2*HASH_BYTES];
if(leafidx&1)
{
for(j=0;j<HASH_BYTES;j++)
buffer[HASH_BYTES+j] = leaf[j];
for(j=0;j<HASH_BYTES;j++)
buffer[j] = authpath[j];
}
else
{
for(j=0;j<HASH_BYTES;j++)
buffer[j] = leaf[j];
for(j=0;j<HASH_BYTES;j++)
buffer[HASH_BYTES+j] = authpath[j];
}
authpath += HASH_BYTES;
for(i=0;i<height-1;i++)
{
leafidx >>= 1;
if(leafidx&1)
{
hash_2n_n_mask(buffer+HASH_BYTES,buffer,masks+2*(WOTS_LOG_L+i)*HASH_BYTES);
for(j=0;j<HASH_BYTES;j++)
buffer[j] = authpath[j];
}
else
{
hash_2n_n_mask(buffer,buffer,masks+2*(WOTS_LOG_L+i)*HASH_BYTES);
for(j=0;j<HASH_BYTES;j++)
buffer[j+HASH_BYTES] = authpath[j];
}
authpath += HASH_BYTES;
}
hash_2n_n_mask(root,buffer,masks+2*(WOTS_LOG_L+height-1)*HASH_BYTES);
}
static void compute_authpath_wots(unsigned char **out, unsigned char root[HASH_BYTES], const leafaddr *a, const unsigned char *sk, const unsigned char *masks, unsigned int height)
{
unsigned int i, j;
int idx;
leafaddr ta = *a;
unsigned char tree[2*(1<<SUBTREE_HEIGHT)*HASH_BYTES]; // 2 * 32 * 32 = 2KB
unsigned char seed[SEED_BYTES];
unsigned char pk[WOTS_L*HASH_BYTES]; // 67 * 32 = 2144B
for(ta.subleaf = 0; ta.subleaf < (1<<SUBTREE_HEIGHT); ta.subleaf++) {
// level 0
get_seed(seed, sk, &ta);
wots_pkgen(pk, seed, masks);
l_tree(tree + (1<<SUBTREE_HEIGHT)*HASH_BYTES + ta.subleaf * HASH_BYTES,
pk, masks);
}
int level = 0;
// tree
for (i = (1<<SUBTREE_HEIGHT); i > 0; i>>=1)
{
for (j = 0; j < i; j+=2)
hash_2n_n_mask(tree + (i>>1)*HASH_BYTES + (j>>1) * HASH_BYTES,
tree + i*HASH_BYTES + j * HASH_BYTES,
masks+2*(WOTS_LOG_L + level)*HASH_BYTES);
level++;
}
idx = a->subleaf;
// copy authpath
for(i=0;i<height;i++) {
memcpy(*out, tree + ((1<<SUBTREE_HEIGHT)>>i)*HASH_BYTES + ((idx >> i) ^ 1) * HASH_BYTES, HASH_BYTES);
*out+=HASH_BYTES;
}
// copy root
memcpy(root, tree+HASH_BYTES, HASH_BYTES);
}
int pqcrypto_sign(unsigned char *sig, const unsigned char *msg, const unsigned char *sk)
{
leafaddr a;
int i;
unsigned long long leafidx;
unsigned char *R = sig;
unsigned char m_h[MSGHASH_BYTES];
unsigned char checkbuf[MSGHASH_BYTES];
unsigned char buffer[2*MBLOCKSIZE];
unsigned long long *rnd = (unsigned long long *)buffer;
unsigned char *leaves = R + MESSAGE_HASH_SEED_BYTES;
unsigned char *horst_sig = leaves + (TOTALTREE_HEIGHT+7)/8;
unsigned char *wots_sig = horst_sig + HORST_SIGBYTES;
#if 2*MBLOCKSIZE < HASH_BYTES+SEED_BYTES
#error "buffer is not large enough to hold seed and root"
#endif
// create leafidx deterministically
{
crypto_generichash_state S;
crypto_generichash_state check;
crypto_generichash_init(&S, NULL, 0, 64);
crypto_generichash_init(&check, NULL, 0, 64);
crypto_generichash_update(&S, sk + PQCRYPTO_SECRETKEYBYTES - SK_RAND_SEED_BYTES, SK_RAND_SEED_BYTES*8);
crypto_generichash_update(&S, msg, 32);
crypto_generichash_final(&S, (unsigned char*) rnd, 64);
crypto_generichash_final(&check, checkbuf, 64);
#if TOTALTREE_HEIGHT != 60
#error "Implemented for TOTALTREE_HEIGHT == 60!"
#endif
leafidx = rnd[0] & 0xfffffffffffffff;
#if MESSAGE_HASH_SEED_BYTES != 32
#error "Implemented for MESSAGE_HASH_SEED_BYTES == 32!"
#endif
memcpy(R, &rnd[2], MESSAGE_HASH_SEED_BYTES);
crypto_generichash_init(&S, NULL, 0, 64);
crypto_generichash_init(&check, NULL, 0, 64);
crypto_generichash_update(&S, R, MESSAGE_HASH_SEED_BYTES);
// construct pk
leafaddr a;
a.level = N_LEVELS - 1;
a.subtree = 0;
a.subleaf=0;
// like with the MBLOCKSIZE, this loop is needed since crypto_generichash_update does
// not seem to support updates of 1024 bits or more, after the first one
for(i=0; i<N_MASKS*HASH_BYTES; i += HASH_BYTES*2)
crypto_generichash_update(&S, sk+SEED_BYTES+i, 32*2);
treehash(buffer, SUBTREE_HEIGHT, sk, &a, sk+SEED_BYTES);
crypto_generichash_update(&S, buffer, HASH_BYTES);
crypto_generichash_update(&S, msg, 32);
crypto_generichash_final(&check, m_h, 64);
// this prevents streaming in two different messages
if (memcmp(checkbuf, m_h, MSGHASH_BYTES)) {
return -1;
}
crypto_generichash_final(&S, m_h, 64);
}
a.level = N_LEVELS; // Use unique value $d$ for HORST address.
a.subleaf = leafidx & ((1<<SUBTREE_HEIGHT)-1);
a.subtree = leafidx >> SUBTREE_HEIGHT;
for(i=0;i<(TOTALTREE_HEIGHT+7)/8;i++) {
leaves[i] = (leafidx >> 8*i) & 0xff;
}
get_seed(buffer, sk, &a);
horst_sign(horst_sig, buffer+SEED_BYTES, buffer, sk+SEED_BYTES, m_h);
unsigned char *outptr = wots_sig;
for(i=0;i<N_LEVELS;i++)
{
a.level = i;
get_seed(buffer, sk, &a);
wots_sign(&outptr, buffer+SEED_BYTES, buffer, sk+SEED_BYTES);
compute_authpath_wots(&outptr, buffer+SEED_BYTES, &a, sk, sk+SEED_BYTES,SUBTREE_HEIGHT);
a.subleaf = a.subtree & ((1<<SUBTREE_HEIGHT)-1);
a.subtree >>= SUBTREE_HEIGHT;
}
return 0;
}
int pqcrypto_sign_public_key(unsigned char *pk, unsigned char *sk)
{
leafaddr a;
// randombytes(sk,CRYPTO_SECRETKEYBYTES); STM32L100C-DISCO has no RNG
memcpy(pk,sk+SEED_BYTES,N_MASKS*HASH_BYTES);
// Initialization of top-subtree address
a.level = N_LEVELS - 1;
a.subtree = 0;
a.subleaf = 0;
// Construct top subtree
treehash(pk+(N_MASKS*HASH_BYTES), SUBTREE_HEIGHT, sk, &a, pk);
return 0;
}
int pqcrypto_sign_open(unsigned char *m, const unsigned char *sig, const unsigned char *pk) {
unsigned long long i;
unsigned long long leafidx=0;
unsigned char pkhash[HASH_BYTES];
unsigned char root[HASH_BYTES];
unsigned char tpk[PQCRYPTO_PUBLICKEYBYTES];
unsigned char m_h[MSGHASH_BYTES];
unsigned char *R = (unsigned char *) sig;
unsigned char *leaves = (unsigned char *) sig + MESSAGE_HASH_SEED_BYTES;
unsigned char *horst_sig = leaves + ((TOTALTREE_HEIGHT+7)/8);
unsigned char *wots_sig = horst_sig + HORST_SIGBYTES;
for(i=0;i<PQCRYPTO_PUBLICKEYBYTES;i++)
tpk[i] = pk[i];
crypto_generichash_state S;
crypto_generichash_init(&S, NULL, 0, 64);
crypto_generichash_update(&S, R, MESSAGE_HASH_SEED_BYTES);
crypto_generichash_update(&S, tpk, PQCRYPTO_PUBLICKEYBYTES);
crypto_generichash_update(&S, m, 32);
crypto_generichash_final(&S, (unsigned char*) m_h, 64);
for(i=0;i<(TOTALTREE_HEIGHT+7)/8;i++) {
leafidx ^= (((unsigned long long)leaves[i]) << 8*i);
}
if (horst_verify(horst_sig, root, tpk, m_h) != 0) {
goto fail;
}
for(i=0;i<N_LEVELS;i++) {
wots_verify(wots_sig+i*(WOTS_SIGBYTES + SUBTREE_HEIGHT*HASH_BYTES),
wots_sig+i*(WOTS_SIGBYTES + SUBTREE_HEIGHT*HASH_BYTES), root, tpk);
l_tree(pkhash, wots_sig+i*(WOTS_SIGBYTES + SUBTREE_HEIGHT*HASH_BYTES), tpk);
validate_authpath(root,
pkhash,
leafidx & 0x1f,
wots_sig+i*(WOTS_SIGBYTES + SUBTREE_HEIGHT*HASH_BYTES)+WOTS_SIGBYTES,
tpk,
SUBTREE_HEIGHT);
leafidx >>= 5;
}
for(i=0;i<HASH_BYTES;i++)
if(root[i] != tpk[i+N_MASKS*HASH_BYTES])
goto fail;
return 0;
fail:
return -1;
}