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