blob: 5e3a54314cd60039fb36ec54479b1c103646e425 [file] [log] [blame]
Darren Tuckera10d8552018-02-27 14:45:17 +11001#ifdef WITH_XMSS
markus@openbsd.org1b11ea72018-02-23 15:58:37 +00002/*
3xmss_fast.c version 20160722
4Andreas Hülsing
5Joost Rijneveld
6Public domain.
7*/
8
Damien Millerf8854742018-02-26 12:18:14 +11009#include "includes.h"
10
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000011#include "xmss_fast.h"
12#include <stdlib.h>
13#include <string.h>
Darren Tuckerc7ef4a32018-02-26 17:42:56 +110014#ifdef HAVE_STDINT_H
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000015#include <stdint.h>
Darren Tuckerc7ef4a32018-02-26 17:42:56 +110016#endif
markus@openbsd.org1b11ea72018-02-23 15:58:37 +000017
18#include "crypto_api.h"
19#include "xmss_wots.h"
20#include "xmss_hash.h"
21
22#include "xmss_commons.h"
23#include "xmss_hash_address.h"
24// For testing
25#include "stdio.h"
26
27
28
29/**
30 * Used for pseudorandom keygeneration,
31 * generates the seed for the WOTS keypair at address addr
32 *
33 * takes n byte sk_seed and returns n byte seed using 32 byte address addr.
34 */
35static void get_seed(unsigned char *seed, const unsigned char *sk_seed, int n, uint32_t addr[8])
36{
37 unsigned char bytes[32];
38 // Make sure that chain addr, hash addr, and key bit are 0!
39 setChainADRS(addr,0);
40 setHashADRS(addr,0);
41 setKeyAndMask(addr,0);
42 // Generate pseudorandom value
43 addr_to_byte(bytes, addr);
44 prf(seed, bytes, sk_seed, n);
45}
46
47/**
48 * Initialize xmss params struct
49 * parameter names are the same as in the draft
50 * parameter k is K as used in the BDS algorithm
51 */
52int xmss_set_params(xmss_params *params, int n, int h, int w, int k)
53{
54 if (k >= h || k < 2 || (h - k) % 2) {
55 fprintf(stderr, "For BDS traversal, H - K must be even, with H > K >= 2!\n");
56 return 1;
57 }
58 params->h = h;
59 params->n = n;
60 params->k = k;
61 wots_params wots_par;
62 wots_set_params(&wots_par, n, w);
63 params->wots_par = wots_par;
64 return 0;
65}
66
67/**
68 * Initialize BDS state struct
69 * parameter names are the same as used in the description of the BDS traversal
70 */
71void xmss_set_bds_state(bds_state *state, unsigned char *stack, int stackoffset, unsigned char *stacklevels, unsigned char *auth, unsigned char *keep, treehash_inst *treehash, unsigned char *retain, int next_leaf)
72{
73 state->stack = stack;
74 state->stackoffset = stackoffset;
75 state->stacklevels = stacklevels;
76 state->auth = auth;
77 state->keep = keep;
78 state->treehash = treehash;
79 state->retain = retain;
80 state->next_leaf = next_leaf;
81}
82
83/**
84 * Initialize xmssmt_params struct
85 * parameter names are the same as in the draft
86 *
87 * Especially h is the total tree height, i.e. the XMSS trees have height h/d
88 */
89int xmssmt_set_params(xmssmt_params *params, int n, int h, int d, int w, int k)
90{
91 if (h % d) {
92 fprintf(stderr, "d must divide h without remainder!\n");
93 return 1;
94 }
95 params->h = h;
96 params->d = d;
97 params->n = n;
98 params->index_len = (h + 7) / 8;
99 xmss_params xmss_par;
100 if (xmss_set_params(&xmss_par, n, (h/d), w, k)) {
101 return 1;
102 }
103 params->xmss_par = xmss_par;
104 return 0;
105}
106
107/**
108 * Computes a leaf from a WOTS public key using an L-tree.
109 */
110static void l_tree(unsigned char *leaf, unsigned char *wots_pk, const xmss_params *params, const unsigned char *pub_seed, uint32_t addr[8])
111{
112 unsigned int l = params->wots_par.len;
113 unsigned int n = params->n;
114 uint32_t i = 0;
115 uint32_t height = 0;
116 uint32_t bound;
117
118 //ADRS.setTreeHeight(0);
119 setTreeHeight(addr, height);
120
121 while (l > 1) {
122 bound = l >> 1; //floor(l / 2);
123 for (i = 0; i < bound; i++) {
124 //ADRS.setTreeIndex(i);
125 setTreeIndex(addr, i);
126 //wots_pk[i] = RAND_HASH(pk[2i], pk[2i + 1], SEED, ADRS);
127 hash_h(wots_pk+i*n, wots_pk+i*2*n, pub_seed, addr, n);
128 }
129 //if ( l % 2 == 1 ) {
130 if (l & 1) {
131 //pk[floor(l / 2) + 1] = pk[l];
132 memcpy(wots_pk+(l>>1)*n, wots_pk+(l-1)*n, n);
133 //l = ceil(l / 2);
134 l=(l>>1)+1;
135 }
136 else {
137 //l = ceil(l / 2);
138 l=(l>>1);
139 }
140 //ADRS.setTreeHeight(ADRS.getTreeHeight() + 1);
141 height++;
142 setTreeHeight(addr, height);
143 }
144 //return pk[0];
145 memcpy(leaf, wots_pk, n);
146}
147
148/**
149 * Computes the leaf at a given address. First generates the WOTS key pair, then computes leaf using l_tree. As this happens position independent, we only require that addr encodes the right ltree-address.
150 */
151static void gen_leaf_wots(unsigned char *leaf, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, uint32_t ltree_addr[8], uint32_t ots_addr[8])
152{
153 unsigned char seed[params->n];
154 unsigned char pk[params->wots_par.keysize];
155
156 get_seed(seed, sk_seed, params->n, ots_addr);
157 wots_pkgen(pk, seed, &(params->wots_par), pub_seed, ots_addr);
158
159 l_tree(leaf, pk, params, pub_seed, ltree_addr);
160}
161
162static int treehash_minheight_on_stack(bds_state* state, const xmss_params *params, const treehash_inst *treehash) {
163 unsigned int r = params->h, i;
164 for (i = 0; i < treehash->stackusage; i++) {
165 if (state->stacklevels[state->stackoffset - i - 1] < r) {
166 r = state->stacklevels[state->stackoffset - i - 1];
167 }
168 }
169 return r;
170}
171
172/**
173 * Merkle's TreeHash algorithm. The address only needs to initialize the first 78 bits of addr. Everything else will be set by treehash.
174 * Currently only used for key generation.
175 *
176 */
177static void treehash_setup(unsigned char *node, int height, int index, bds_state *state, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const uint32_t addr[8])
178{
179 unsigned int idx = index;
180 unsigned int n = params->n;
181 unsigned int h = params->h;
182 unsigned int k = params->k;
183 // use three different addresses because at this point we use all three formats in parallel
184 uint32_t ots_addr[8];
185 uint32_t ltree_addr[8];
186 uint32_t node_addr[8];
187 // only copy layer and tree address parts
188 memcpy(ots_addr, addr, 12);
189 // type = ots
190 setType(ots_addr, 0);
191 memcpy(ltree_addr, addr, 12);
192 setType(ltree_addr, 1);
193 memcpy(node_addr, addr, 12);
194 setType(node_addr, 2);
195
196 uint32_t lastnode, i;
197 unsigned char stack[(height+1)*n];
198 unsigned int stacklevels[height+1];
199 unsigned int stackoffset=0;
200 unsigned int nodeh;
201
202 lastnode = idx+(1<<height);
203
204 for (i = 0; i < h-k; i++) {
205 state->treehash[i].h = i;
206 state->treehash[i].completed = 1;
207 state->treehash[i].stackusage = 0;
208 }
209
210 i = 0;
211 for (; idx < lastnode; idx++) {
212 setLtreeADRS(ltree_addr, idx);
213 setOTSADRS(ots_addr, idx);
214 gen_leaf_wots(stack+stackoffset*n, sk_seed, params, pub_seed, ltree_addr, ots_addr);
215 stacklevels[stackoffset] = 0;
216 stackoffset++;
217 if (h - k > 0 && i == 3) {
218 memcpy(state->treehash[0].node, stack+stackoffset*n, n);
219 }
220 while (stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2])
221 {
222 nodeh = stacklevels[stackoffset-1];
223 if (i >> nodeh == 1) {
224 memcpy(state->auth + nodeh*n, stack+(stackoffset-1)*n, n);
225 }
226 else {
227 if (nodeh < h - k && i >> nodeh == 3) {
228 memcpy(state->treehash[nodeh].node, stack+(stackoffset-1)*n, n);
229 }
230 else if (nodeh >= h - k) {
231 memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((i >> nodeh) - 3) >> 1)) * n, stack+(stackoffset-1)*n, n);
232 }
233 }
234 setTreeHeight(node_addr, stacklevels[stackoffset-1]);
235 setTreeIndex(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
236 hash_h(stack+(stackoffset-2)*n, stack+(stackoffset-2)*n, pub_seed,
237 node_addr, n);
238 stacklevels[stackoffset-2]++;
239 stackoffset--;
240 }
241 i++;
242 }
243
244 for (i = 0; i < n; i++)
245 node[i] = stack[i];
246}
247
248static void treehash_update(treehash_inst *treehash, bds_state *state, const unsigned char *sk_seed, const xmss_params *params, const unsigned char *pub_seed, const uint32_t addr[8]) {
249 int n = params->n;
250
251 uint32_t ots_addr[8];
252 uint32_t ltree_addr[8];
253 uint32_t node_addr[8];
254 // only copy layer and tree address parts
255 memcpy(ots_addr, addr, 12);
256 // type = ots
257 setType(ots_addr, 0);
258 memcpy(ltree_addr, addr, 12);
259 setType(ltree_addr, 1);
260 memcpy(node_addr, addr, 12);
261 setType(node_addr, 2);
262
263 setLtreeADRS(ltree_addr, treehash->next_idx);
264 setOTSADRS(ots_addr, treehash->next_idx);
265
266 unsigned char nodebuffer[2 * n];
267 unsigned int nodeheight = 0;
268 gen_leaf_wots(nodebuffer, sk_seed, params, pub_seed, ltree_addr, ots_addr);
269 while (treehash->stackusage > 0 && state->stacklevels[state->stackoffset-1] == nodeheight) {
270 memcpy(nodebuffer + n, nodebuffer, n);
271 memcpy(nodebuffer, state->stack + (state->stackoffset-1)*n, n);
272 setTreeHeight(node_addr, nodeheight);
273 setTreeIndex(node_addr, (treehash->next_idx >> (nodeheight+1)));
274 hash_h(nodebuffer, nodebuffer, pub_seed, node_addr, n);
275 nodeheight++;
276 treehash->stackusage--;
277 state->stackoffset--;
278 }
279 if (nodeheight == treehash->h) { // this also implies stackusage == 0
280 memcpy(treehash->node, nodebuffer, n);
281 treehash->completed = 1;
282 }
283 else {
284 memcpy(state->stack + state->stackoffset*n, nodebuffer, n);
285 treehash->stackusage++;
286 state->stacklevels[state->stackoffset] = nodeheight;
287 state->stackoffset++;
288 treehash->next_idx++;
289 }
290}
291
292/**
293 * Computes a root node given a leaf and an authapth
294 */
295static void validate_authpath(unsigned char *root, const unsigned char *leaf, unsigned long leafidx, const unsigned char *authpath, const xmss_params *params, const unsigned char *pub_seed, uint32_t addr[8])
296{
297 unsigned int n = params->n;
298
299 uint32_t i, j;
300 unsigned char buffer[2*n];
301
302 // If leafidx is odd (last bit = 1), current path element is a right child and authpath has to go to the left.
303 // Otherwise, it is the other way around
304 if (leafidx & 1) {
305 for (j = 0; j < n; j++)
306 buffer[n+j] = leaf[j];
307 for (j = 0; j < n; j++)
308 buffer[j] = authpath[j];
309 }
310 else {
311 for (j = 0; j < n; j++)
312 buffer[j] = leaf[j];
313 for (j = 0; j < n; j++)
314 buffer[n+j] = authpath[j];
315 }
316 authpath += n;
317
318 for (i=0; i < params->h-1; i++) {
319 setTreeHeight(addr, i);
320 leafidx >>= 1;
321 setTreeIndex(addr, leafidx);
322 if (leafidx&1) {
323 hash_h(buffer+n, buffer, pub_seed, addr, n);
324 for (j = 0; j < n; j++)
325 buffer[j] = authpath[j];
326 }
327 else {
328 hash_h(buffer, buffer, pub_seed, addr, n);
329 for (j = 0; j < n; j++)
330 buffer[j+n] = authpath[j];
331 }
332 authpath += n;
333 }
334 setTreeHeight(addr, (params->h-1));
335 leafidx >>= 1;
336 setTreeIndex(addr, leafidx);
337 hash_h(root, buffer, pub_seed, addr, n);
338}
339
340/**
341 * Performs one treehash update on the instance that needs it the most.
342 * Returns 1 if such an instance was not found
343 **/
344static char bds_treehash_update(bds_state *state, unsigned int updates, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, const uint32_t addr[8]) {
345 uint32_t i, j;
346 unsigned int level, l_min, low;
347 unsigned int h = params->h;
348 unsigned int k = params->k;
349 unsigned int used = 0;
350
351 for (j = 0; j < updates; j++) {
352 l_min = h;
353 level = h - k;
354 for (i = 0; i < h - k; i++) {
355 if (state->treehash[i].completed) {
356 low = h;
357 }
358 else if (state->treehash[i].stackusage == 0) {
359 low = i;
360 }
361 else {
362 low = treehash_minheight_on_stack(state, params, &(state->treehash[i]));
363 }
364 if (low < l_min) {
365 level = i;
366 l_min = low;
367 }
368 }
369 if (level == h - k) {
370 break;
371 }
372 treehash_update(&(state->treehash[level]), state, sk_seed, params, pub_seed, addr);
373 used++;
374 }
375 return updates - used;
376}
377
378/**
379 * Updates the state (typically NEXT_i) by adding a leaf and updating the stack
380 * Returns 1 if all leaf nodes have already been processed
381 **/
382static char bds_state_update(bds_state *state, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, const uint32_t addr[8]) {
383 uint32_t ltree_addr[8];
384 uint32_t node_addr[8];
385 uint32_t ots_addr[8];
386
387 int n = params->n;
388 int h = params->h;
389 int k = params->k;
390
391 int nodeh;
392 int idx = state->next_leaf;
393 if (idx == 1 << h) {
394 return 1;
395 }
396
397 // only copy layer and tree address parts
398 memcpy(ots_addr, addr, 12);
399 // type = ots
400 setType(ots_addr, 0);
401 memcpy(ltree_addr, addr, 12);
402 setType(ltree_addr, 1);
403 memcpy(node_addr, addr, 12);
404 setType(node_addr, 2);
405
406 setOTSADRS(ots_addr, idx);
407 setLtreeADRS(ltree_addr, idx);
408
409 gen_leaf_wots(state->stack+state->stackoffset*n, sk_seed, params, pub_seed, ltree_addr, ots_addr);
410
411 state->stacklevels[state->stackoffset] = 0;
412 state->stackoffset++;
413 if (h - k > 0 && idx == 3) {
414 memcpy(state->treehash[0].node, state->stack+state->stackoffset*n, n);
415 }
416 while (state->stackoffset>1 && state->stacklevels[state->stackoffset-1] == state->stacklevels[state->stackoffset-2]) {
417 nodeh = state->stacklevels[state->stackoffset-1];
418 if (idx >> nodeh == 1) {
419 memcpy(state->auth + nodeh*n, state->stack+(state->stackoffset-1)*n, n);
420 }
421 else {
422 if (nodeh < h - k && idx >> nodeh == 3) {
423 memcpy(state->treehash[nodeh].node, state->stack+(state->stackoffset-1)*n, n);
424 }
425 else if (nodeh >= h - k) {
426 memcpy(state->retain + ((1 << (h - 1 - nodeh)) + nodeh - h + (((idx >> nodeh) - 3) >> 1)) * n, state->stack+(state->stackoffset-1)*n, n);
427 }
428 }
429 setTreeHeight(node_addr, state->stacklevels[state->stackoffset-1]);
430 setTreeIndex(node_addr, (idx >> (state->stacklevels[state->stackoffset-1]+1)));
431 hash_h(state->stack+(state->stackoffset-2)*n, state->stack+(state->stackoffset-2)*n, pub_seed, node_addr, n);
432
433 state->stacklevels[state->stackoffset-2]++;
434 state->stackoffset--;
435 }
436 state->next_leaf++;
437 return 0;
438}
439
440/**
441 * Returns the auth path for node leaf_idx and computes the auth path for the
442 * next leaf node, using the algorithm described by Buchmann, Dahmen and Szydlo
443 * in "Post Quantum Cryptography", Springer 2009.
444 */
445static void bds_round(bds_state *state, const unsigned long leaf_idx, const unsigned char *sk_seed, const xmss_params *params, unsigned char *pub_seed, uint32_t addr[8])
446{
447 unsigned int i;
448 unsigned int n = params->n;
449 unsigned int h = params->h;
450 unsigned int k = params->k;
451
452 unsigned int tau = h;
453 unsigned int startidx;
454 unsigned int offset, rowidx;
455 unsigned char buf[2 * n];
456
457 uint32_t ots_addr[8];
458 uint32_t ltree_addr[8];
459 uint32_t node_addr[8];
460 // only copy layer and tree address parts
461 memcpy(ots_addr, addr, 12);
462 // type = ots
463 setType(ots_addr, 0);
464 memcpy(ltree_addr, addr, 12);
465 setType(ltree_addr, 1);
466 memcpy(node_addr, addr, 12);
467 setType(node_addr, 2);
468
469 for (i = 0; i < h; i++) {
470 if (! ((leaf_idx >> i) & 1)) {
471 tau = i;
472 break;
473 }
474 }
475
476 if (tau > 0) {
477 memcpy(buf, state->auth + (tau-1) * n, n);
478 // we need to do this before refreshing state->keep to prevent overwriting
479 memcpy(buf + n, state->keep + ((tau-1) >> 1) * n, n);
480 }
481 if (!((leaf_idx >> (tau + 1)) & 1) && (tau < h - 1)) {
482 memcpy(state->keep + (tau >> 1)*n, state->auth + tau*n, n);
483 }
484 if (tau == 0) {
485 setLtreeADRS(ltree_addr, leaf_idx);
486 setOTSADRS(ots_addr, leaf_idx);
487 gen_leaf_wots(state->auth, sk_seed, params, pub_seed, ltree_addr, ots_addr);
488 }
489 else {
490 setTreeHeight(node_addr, (tau-1));
491 setTreeIndex(node_addr, leaf_idx >> tau);
492 hash_h(state->auth + tau * n, buf, pub_seed, node_addr, n);
493 for (i = 0; i < tau; i++) {
494 if (i < h - k) {
495 memcpy(state->auth + i * n, state->treehash[i].node, n);
496 }
497 else {
498 offset = (1 << (h - 1 - i)) + i - h;
499 rowidx = ((leaf_idx >> i) - 1) >> 1;
500 memcpy(state->auth + i * n, state->retain + (offset + rowidx) * n, n);
501 }
502 }
503
504 for (i = 0; i < ((tau < h - k) ? tau : (h - k)); i++) {
505 startidx = leaf_idx + 1 + 3 * (1 << i);
506 if (startidx < 1U << h) {
507 state->treehash[i].h = i;
508 state->treehash[i].next_idx = startidx;
509 state->treehash[i].completed = 0;
510 state->treehash[i].stackusage = 0;
511 }
512 }
513 }
514}
515
516/*
517 * Generates a XMSS key pair for a given parameter set.
518 * Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
519 * Format pk: [root || PUB_SEED] omitting algo oid.
520 */
521int xmss_keypair(unsigned char *pk, unsigned char *sk, bds_state *state, xmss_params *params)
522{
523 unsigned int n = params->n;
524 // Set idx = 0
525 sk[0] = 0;
526 sk[1] = 0;
527 sk[2] = 0;
528 sk[3] = 0;
529 // Init SK_SEED (n byte), SK_PRF (n byte), and PUB_SEED (n byte)
530 randombytes(sk+4, 3*n);
531 // Copy PUB_SEED to public key
532 memcpy(pk+n, sk+4+2*n, n);
533
534 uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
535
536 // Compute root
537 treehash_setup(pk, params->h, 0, state, sk+4, params, sk+4+2*n, addr);
538 // copy root to sk
539 memcpy(sk+4+3*n, pk, n);
540 return 0;
541}
542
543/**
544 * Signs a message.
545 * Returns
546 * 1. an array containing the signature followed by the message AND
547 * 2. an updated secret key!
548 *
549 */
550int xmss_sign(unsigned char *sk, bds_state *state, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmss_params *params)
551{
552 unsigned int h = params->h;
553 unsigned int n = params->n;
554 unsigned int k = params->k;
555 uint16_t i = 0;
556
557 // Extract SK
558 unsigned long idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
559 unsigned char sk_seed[n];
560 memcpy(sk_seed, sk+4, n);
561 unsigned char sk_prf[n];
562 memcpy(sk_prf, sk+4+n, n);
563 unsigned char pub_seed[n];
564 memcpy(pub_seed, sk+4+2*n, n);
565
566 // index as 32 bytes string
567 unsigned char idx_bytes_32[32];
568 to_byte(idx_bytes_32, idx, 32);
569
570 unsigned char hash_key[3*n];
571
572 // Update SK
573 sk[0] = ((idx + 1) >> 24) & 255;
574 sk[1] = ((idx + 1) >> 16) & 255;
575 sk[2] = ((idx + 1) >> 8) & 255;
576 sk[3] = (idx + 1) & 255;
577 // -- Secret key for this non-forward-secure version is now updated.
578 // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
579
580 // Init working params
581 unsigned char R[n];
582 unsigned char msg_h[n];
583 unsigned char ots_seed[n];
584 uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
585
586 // ---------------------------------
587 // Message Hashing
588 // ---------------------------------
589
590 // Message Hash:
591 // First compute pseudorandom value
592 prf(R, idx_bytes_32, sk_prf, n);
593 // Generate hash key (R || root || idx)
594 memcpy(hash_key, R, n);
595 memcpy(hash_key+n, sk+4+3*n, n);
596 to_byte(hash_key+2*n, idx, n);
597 // Then use it for message digest
598 h_msg(msg_h, msg, msglen, hash_key, 3*n, n);
599
600 // Start collecting signature
601 *sig_msg_len = 0;
602
603 // Copy index to signature
604 sig_msg[0] = (idx >> 24) & 255;
605 sig_msg[1] = (idx >> 16) & 255;
606 sig_msg[2] = (idx >> 8) & 255;
607 sig_msg[3] = idx & 255;
608
609 sig_msg += 4;
610 *sig_msg_len += 4;
611
612 // Copy R to signature
613 for (i = 0; i < n; i++)
614 sig_msg[i] = R[i];
615
616 sig_msg += n;
617 *sig_msg_len += n;
618
619 // ----------------------------------
620 // Now we start to "really sign"
621 // ----------------------------------
622
623 // Prepare Address
624 setType(ots_addr, 0);
625 setOTSADRS(ots_addr, idx);
626
627 // Compute seed for OTS key pair
628 get_seed(ots_seed, sk_seed, n, ots_addr);
629
630 // Compute WOTS signature
631 wots_sign(sig_msg, msg_h, ots_seed, &(params->wots_par), pub_seed, ots_addr);
632
633 sig_msg += params->wots_par.keysize;
634 *sig_msg_len += params->wots_par.keysize;
635
636 // the auth path was already computed during the previous round
637 memcpy(sig_msg, state->auth, h*n);
638
639 if (idx < (1U << h) - 1) {
640 bds_round(state, idx, sk_seed, params, pub_seed, ots_addr);
641 bds_treehash_update(state, (h - k) >> 1, sk_seed, params, pub_seed, ots_addr);
642 }
643
644/* TODO: save key/bds state here! */
645
646 sig_msg += params->h*n;
647 *sig_msg_len += params->h*n;
648
649 //Whipe secret elements?
650 //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
651
652
653 memcpy(sig_msg, msg, msglen);
654 *sig_msg_len += msglen;
655
656 return 0;
657}
658
659/**
660 * Verifies a given message signature pair under a given public key.
661 */
662int xmss_sign_open(unsigned char *msg, unsigned long long *msglen, const unsigned char *sig_msg, unsigned long long sig_msg_len, const unsigned char *pk, const xmss_params *params)
663{
664 unsigned int n = params->n;
665
666 unsigned long long i, m_len;
667 unsigned long idx=0;
668 unsigned char wots_pk[params->wots_par.keysize];
669 unsigned char pkhash[n];
670 unsigned char root[n];
671 unsigned char msg_h[n];
672 unsigned char hash_key[3*n];
673
674 unsigned char pub_seed[n];
675 memcpy(pub_seed, pk+n, n);
676
677 // Init addresses
678 uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
679 uint32_t ltree_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
680 uint32_t node_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
681
682 setType(ots_addr, 0);
683 setType(ltree_addr, 1);
684 setType(node_addr, 2);
685
686 // Extract index
687 idx = ((unsigned long)sig_msg[0] << 24) | ((unsigned long)sig_msg[1] << 16) | ((unsigned long)sig_msg[2] << 8) | sig_msg[3];
688 printf("verify:: idx = %lu\n", idx);
689
690 // Generate hash key (R || root || idx)
691 memcpy(hash_key, sig_msg+4,n);
692 memcpy(hash_key+n, pk, n);
693 to_byte(hash_key+2*n, idx, n);
694
695 sig_msg += (n+4);
696 sig_msg_len -= (n+4);
697
698 // hash message
699 unsigned long long tmp_sig_len = params->wots_par.keysize+params->h*n;
700 m_len = sig_msg_len - tmp_sig_len;
701 h_msg(msg_h, sig_msg + tmp_sig_len, m_len, hash_key, 3*n, n);
702
703 //-----------------------
704 // Verify signature
705 //-----------------------
706
707 // Prepare Address
708 setOTSADRS(ots_addr, idx);
709 // Check WOTS signature
710 wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->wots_par), pub_seed, ots_addr);
711
712 sig_msg += params->wots_par.keysize;
713 sig_msg_len -= params->wots_par.keysize;
714
715 // Compute Ltree
716 setLtreeADRS(ltree_addr, idx);
717 l_tree(pkhash, wots_pk, params, pub_seed, ltree_addr);
718
719 // Compute root
720 validate_authpath(root, pkhash, idx, sig_msg, params, pub_seed, node_addr);
721
722 sig_msg += params->h*n;
723 sig_msg_len -= params->h*n;
724
725 for (i = 0; i < n; i++)
726 if (root[i] != pk[i])
727 goto fail;
728
729 *msglen = sig_msg_len;
730 for (i = 0; i < *msglen; i++)
731 msg[i] = sig_msg[i];
732
733 return 0;
734
735
736fail:
737 *msglen = sig_msg_len;
738 for (i = 0; i < *msglen; i++)
739 msg[i] = 0;
740 *msglen = -1;
741 return -1;
742}
743
744/*
745 * Generates a XMSSMT key pair for a given parameter set.
746 * Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
747 * Format pk: [root || PUB_SEED] omitting algo oid.
748 */
749int xmssmt_keypair(unsigned char *pk, unsigned char *sk, bds_state *states, unsigned char *wots_sigs, xmssmt_params *params)
750{
751 unsigned int n = params->n;
752 unsigned int i;
753 unsigned char ots_seed[params->n];
754 // Set idx = 0
755 for (i = 0; i < params->index_len; i++) {
756 sk[i] = 0;
757 }
758 // Init SK_SEED (n byte), SK_PRF (n byte), and PUB_SEED (n byte)
759 randombytes(sk+params->index_len, 3*n);
760 // Copy PUB_SEED to public key
761 memcpy(pk+n, sk+params->index_len+2*n, n);
762
763 // Set address to point on the single tree on layer d-1
764 uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
765 setLayerADRS(addr, (params->d-1));
766 // Set up state and compute wots signatures for all but topmost tree root
767 for (i = 0; i < params->d - 1; i++) {
768 // Compute seed for OTS key pair
769 treehash_setup(pk, params->xmss_par.h, 0, states + i, sk+params->index_len, &(params->xmss_par), pk+n, addr);
770 setLayerADRS(addr, (i+1));
771 get_seed(ots_seed, sk+params->index_len, n, addr);
772 wots_sign(wots_sigs + i*params->xmss_par.wots_par.keysize, pk, ots_seed, &(params->xmss_par.wots_par), pk+n, addr);
773 }
774 treehash_setup(pk, params->xmss_par.h, 0, states + i, sk+params->index_len, &(params->xmss_par), pk+n, addr);
775 memcpy(sk+params->index_len+3*n, pk, n);
776 return 0;
777}
778
779/**
780 * Signs a message.
781 * Returns
782 * 1. an array containing the signature followed by the message AND
783 * 2. an updated secret key!
784 *
785 */
786int xmssmt_sign(unsigned char *sk, bds_state *states, unsigned char *wots_sigs, unsigned char *sig_msg, unsigned long long *sig_msg_len, const unsigned char *msg, unsigned long long msglen, const xmssmt_params *params)
787{
788 unsigned int n = params->n;
789
790 unsigned int tree_h = params->xmss_par.h;
791 unsigned int h = params->h;
792 unsigned int k = params->xmss_par.k;
793 unsigned int idx_len = params->index_len;
794 uint64_t idx_tree;
795 uint32_t idx_leaf;
796 uint64_t i, j;
797 int needswap_upto = -1;
798 unsigned int updates;
799
800 unsigned char sk_seed[n];
801 unsigned char sk_prf[n];
802 unsigned char pub_seed[n];
803 // Init working params
804 unsigned char R[n];
805 unsigned char msg_h[n];
806 unsigned char hash_key[3*n];
807 unsigned char ots_seed[n];
808 uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
809 uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
810 unsigned char idx_bytes_32[32];
811 bds_state tmp;
812
813 // Extract SK
814 unsigned long long idx = 0;
815 for (i = 0; i < idx_len; i++) {
816 idx |= ((unsigned long long)sk[i]) << 8*(idx_len - 1 - i);
817 }
818
819 memcpy(sk_seed, sk+idx_len, n);
820 memcpy(sk_prf, sk+idx_len+n, n);
821 memcpy(pub_seed, sk+idx_len+2*n, n);
822
823 // Update SK
824 for (i = 0; i < idx_len; i++) {
825 sk[i] = ((idx + 1) >> 8*(idx_len - 1 - i)) & 255;
826 }
827 // -- Secret key for this non-forward-secure version is now updated.
828 // -- A productive implementation should use a file handle instead and write the updated secret key at this point!
829
830
831 // ---------------------------------
832 // Message Hashing
833 // ---------------------------------
834
835 // Message Hash:
836 // First compute pseudorandom value
837 to_byte(idx_bytes_32, idx, 32);
838 prf(R, idx_bytes_32, sk_prf, n);
839 // Generate hash key (R || root || idx)
840 memcpy(hash_key, R, n);
841 memcpy(hash_key+n, sk+idx_len+3*n, n);
842 to_byte(hash_key+2*n, idx, n);
843
844 // Then use it for message digest
845 h_msg(msg_h, msg, msglen, hash_key, 3*n, n);
846
847 // Start collecting signature
848 *sig_msg_len = 0;
849
850 // Copy index to signature
851 for (i = 0; i < idx_len; i++) {
852 sig_msg[i] = (idx >> 8*(idx_len - 1 - i)) & 255;
853 }
854
855 sig_msg += idx_len;
856 *sig_msg_len += idx_len;
857
858 // Copy R to signature
859 for (i = 0; i < n; i++)
860 sig_msg[i] = R[i];
861
862 sig_msg += n;
863 *sig_msg_len += n;
864
865 // ----------------------------------
866 // Now we start to "really sign"
867 // ----------------------------------
868
869 // Handle lowest layer separately as it is slightly different...
870
871 // Prepare Address
872 setType(ots_addr, 0);
873 idx_tree = idx >> tree_h;
874 idx_leaf = (idx & ((1 << tree_h)-1));
875 setLayerADRS(ots_addr, 0);
876 setTreeADRS(ots_addr, idx_tree);
877 setOTSADRS(ots_addr, idx_leaf);
878
879 // Compute seed for OTS key pair
880 get_seed(ots_seed, sk_seed, n, ots_addr);
881
882 // Compute WOTS signature
883 wots_sign(sig_msg, msg_h, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
884
885 sig_msg += params->xmss_par.wots_par.keysize;
886 *sig_msg_len += params->xmss_par.wots_par.keysize;
887
888 memcpy(sig_msg, states[0].auth, tree_h*n);
889 sig_msg += tree_h*n;
890 *sig_msg_len += tree_h*n;
891
892 // prepare signature of remaining layers
893 for (i = 1; i < params->d; i++) {
894 // put WOTS signature in place
895 memcpy(sig_msg, wots_sigs + (i-1)*params->xmss_par.wots_par.keysize, params->xmss_par.wots_par.keysize);
896
897 sig_msg += params->xmss_par.wots_par.keysize;
898 *sig_msg_len += params->xmss_par.wots_par.keysize;
899
900 // put AUTH nodes in place
901 memcpy(sig_msg, states[i].auth, tree_h*n);
902 sig_msg += tree_h*n;
903 *sig_msg_len += tree_h*n;
904 }
905
906 updates = (tree_h - k) >> 1;
907
908 setTreeADRS(addr, (idx_tree + 1));
909 // mandatory update for NEXT_0 (does not count towards h-k/2) if NEXT_0 exists
910 if ((1 + idx_tree) * (1 << tree_h) + idx_leaf < (1ULL << h)) {
911 bds_state_update(&states[params->d], sk_seed, &(params->xmss_par), pub_seed, addr);
912 }
913
914 for (i = 0; i < params->d; i++) {
915 // check if we're not at the end of a tree
916 if (! (((idx + 1) & ((1ULL << ((i+1)*tree_h)) - 1)) == 0)) {
917 idx_leaf = (idx >> (tree_h * i)) & ((1 << tree_h)-1);
918 idx_tree = (idx >> (tree_h * (i+1)));
919 setLayerADRS(addr, i);
920 setTreeADRS(addr, idx_tree);
921 if (i == (unsigned int) (needswap_upto + 1)) {
922 bds_round(&states[i], idx_leaf, sk_seed, &(params->xmss_par), pub_seed, addr);
923 }
924 updates = bds_treehash_update(&states[i], updates, sk_seed, &(params->xmss_par), pub_seed, addr);
925 setTreeADRS(addr, (idx_tree + 1));
926 // if a NEXT-tree exists for this level;
927 if ((1 + idx_tree) * (1 << tree_h) + idx_leaf < (1ULL << (h - tree_h * i))) {
928 if (i > 0 && updates > 0 && states[params->d + i].next_leaf < (1ULL << h)) {
929 bds_state_update(&states[params->d + i], sk_seed, &(params->xmss_par), pub_seed, addr);
930 updates--;
931 }
932 }
933 }
934 else if (idx < (1ULL << h) - 1) {
935 memcpy(&tmp, states+params->d + i, sizeof(bds_state));
936 memcpy(states+params->d + i, states + i, sizeof(bds_state));
937 memcpy(states + i, &tmp, sizeof(bds_state));
938
939 setLayerADRS(ots_addr, (i+1));
940 setTreeADRS(ots_addr, ((idx + 1) >> ((i+2) * tree_h)));
941 setOTSADRS(ots_addr, (((idx >> ((i+1) * tree_h)) + 1) & ((1 << tree_h)-1)));
942
943 get_seed(ots_seed, sk+params->index_len, n, ots_addr);
944 wots_sign(wots_sigs + i*params->xmss_par.wots_par.keysize, states[i].stack, ots_seed, &(params->xmss_par.wots_par), pub_seed, ots_addr);
945
946 states[params->d + i].stackoffset = 0;
947 states[params->d + i].next_leaf = 0;
948
949 updates--; // WOTS-signing counts as one update
950 needswap_upto = i;
951 for (j = 0; j < tree_h-k; j++) {
952 states[i].treehash[j].completed = 1;
953 }
954 }
955 }
956
957 //Whipe secret elements?
958 //zerobytes(tsk, CRYPTO_SECRETKEYBYTES);
959
960 memcpy(sig_msg, msg, msglen);
961 *sig_msg_len += msglen;
962
963 return 0;
964}
965
966/**
967 * Verifies a given message signature pair under a given public key.
968 */
969int xmssmt_sign_open(unsigned char *msg, unsigned long long *msglen, const unsigned char *sig_msg, unsigned long long sig_msg_len, const unsigned char *pk, const xmssmt_params *params)
970{
971 unsigned int n = params->n;
972
973 unsigned int tree_h = params->xmss_par.h;
974 unsigned int idx_len = params->index_len;
975 uint64_t idx_tree;
976 uint32_t idx_leaf;
977
978 unsigned long long i, m_len;
979 unsigned long long idx=0;
980 unsigned char wots_pk[params->xmss_par.wots_par.keysize];
981 unsigned char pkhash[n];
982 unsigned char root[n];
983 unsigned char msg_h[n];
984 unsigned char hash_key[3*n];
985
986 unsigned char pub_seed[n];
987 memcpy(pub_seed, pk+n, n);
988
989 // Init addresses
990 uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
991 uint32_t ltree_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
992 uint32_t node_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
993
994 // Extract index
995 for (i = 0; i < idx_len; i++) {
996 idx |= ((unsigned long long)sig_msg[i]) << (8*(idx_len - 1 - i));
997 }
998 printf("verify:: idx = %llu\n", idx);
999 sig_msg += idx_len;
1000 sig_msg_len -= idx_len;
1001
1002 // Generate hash key (R || root || idx)
1003 memcpy(hash_key, sig_msg,n);
1004 memcpy(hash_key+n, pk, n);
1005 to_byte(hash_key+2*n, idx, n);
1006
1007 sig_msg += n;
1008 sig_msg_len -= n;
1009
1010
1011 // hash message (recall, R is now on pole position at sig_msg
1012 unsigned long long tmp_sig_len = (params->d * params->xmss_par.wots_par.keysize) + (params->h * n);
1013 m_len = sig_msg_len - tmp_sig_len;
1014 h_msg(msg_h, sig_msg + tmp_sig_len, m_len, hash_key, 3*n, n);
1015
1016
1017 //-----------------------
1018 // Verify signature
1019 //-----------------------
1020
1021 // Prepare Address
1022 idx_tree = idx >> tree_h;
1023 idx_leaf = (idx & ((1 << tree_h)-1));
1024 setLayerADRS(ots_addr, 0);
1025 setTreeADRS(ots_addr, idx_tree);
1026 setType(ots_addr, 0);
1027
1028 memcpy(ltree_addr, ots_addr, 12);
1029 setType(ltree_addr, 1);
1030
1031 memcpy(node_addr, ltree_addr, 12);
1032 setType(node_addr, 2);
1033
1034 setOTSADRS(ots_addr, idx_leaf);
1035
1036 // Check WOTS signature
1037 wots_pkFromSig(wots_pk, sig_msg, msg_h, &(params->xmss_par.wots_par), pub_seed, ots_addr);
1038
1039 sig_msg += params->xmss_par.wots_par.keysize;
1040 sig_msg_len -= params->xmss_par.wots_par.keysize;
1041
1042 // Compute Ltree
1043 setLtreeADRS(ltree_addr, idx_leaf);
1044 l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
1045
1046 // Compute root
1047 validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
1048
1049 sig_msg += tree_h*n;
1050 sig_msg_len -= tree_h*n;
1051
1052 for (i = 1; i < params->d; i++) {
1053 // Prepare Address
1054 idx_leaf = (idx_tree & ((1 << tree_h)-1));
1055 idx_tree = idx_tree >> tree_h;
1056
1057 setLayerADRS(ots_addr, i);
1058 setTreeADRS(ots_addr, idx_tree);
1059 setType(ots_addr, 0);
1060
1061 memcpy(ltree_addr, ots_addr, 12);
1062 setType(ltree_addr, 1);
1063
1064 memcpy(node_addr, ltree_addr, 12);
1065 setType(node_addr, 2);
1066
1067 setOTSADRS(ots_addr, idx_leaf);
1068
1069 // Check WOTS signature
1070 wots_pkFromSig(wots_pk, sig_msg, root, &(params->xmss_par.wots_par), pub_seed, ots_addr);
1071
1072 sig_msg += params->xmss_par.wots_par.keysize;
1073 sig_msg_len -= params->xmss_par.wots_par.keysize;
1074
1075 // Compute Ltree
1076 setLtreeADRS(ltree_addr, idx_leaf);
1077 l_tree(pkhash, wots_pk, &(params->xmss_par), pub_seed, ltree_addr);
1078
1079 // Compute root
1080 validate_authpath(root, pkhash, idx_leaf, sig_msg, &(params->xmss_par), pub_seed, node_addr);
1081
1082 sig_msg += tree_h*n;
1083 sig_msg_len -= tree_h*n;
1084
1085 }
1086
1087 for (i = 0; i < n; i++)
1088 if (root[i] != pk[i])
1089 goto fail;
1090
1091 *msglen = sig_msg_len;
1092 for (i = 0; i < *msglen; i++)
1093 msg[i] = sig_msg[i];
1094
1095 return 0;
1096
1097
1098fail:
1099 *msglen = sig_msg_len;
1100 for (i = 0; i < *msglen; i++)
1101 msg[i] = 0;
1102 *msglen = -1;
1103 return -1;
1104}
Darren Tuckera10d8552018-02-27 14:45:17 +11001105#endif /* WITH_XMSS */