2015-08-11 11:08:27 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "hash.h"
|
2016-02-16 15:31:18 +00:00
|
|
|
#include "hash_address.h"
|
2017-06-02 13:10:24 +01:00
|
|
|
#include "params.h"
|
2017-08-03 16:38:34 +01:00
|
|
|
#include "randombytes.h"
|
|
|
|
#include "wots.h"
|
|
|
|
#include "xmss_commons.h"
|
|
|
|
#include "xmss_core.h"
|
2015-08-11 11:08:27 +01:00
|
|
|
|
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* Merkle's TreeHash algorithm. The address only needs to initialize the first
|
|
|
|
* 78 bits of addr. Everything else will be set by treehash.
|
2015-08-11 11:08:27 +01:00
|
|
|
* Currently only used for key generation.
|
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
static void treehash(const xmss_params *params, unsigned char *node, uint32_t index, const unsigned char *sk_seed, const unsigned char *pub_seed, const uint32_t addr[8])
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t idx = index;
|
2017-10-23 13:10:39 +01:00
|
|
|
// Use three different addresses because at this point we use all three formats in parallel
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t ots_addr[8];
|
|
|
|
uint32_t ltree_addr[8];
|
|
|
|
uint32_t node_addr[8];
|
|
|
|
// only copy layer and tree address parts
|
|
|
|
memcpy(ots_addr, addr, 12);
|
|
|
|
// type = ots
|
|
|
|
set_type(ots_addr, 0);
|
|
|
|
memcpy(ltree_addr, addr, 12);
|
|
|
|
set_type(ltree_addr, 1);
|
|
|
|
memcpy(node_addr, addr, 12);
|
|
|
|
set_type(node_addr, 2);
|
|
|
|
|
|
|
|
uint32_t lastnode, i;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char stack[(params->tree_height+1)*params->n];
|
|
|
|
uint16_t stacklevels[params->tree_height+1];
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned int stackoffset=0;
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
lastnode = idx+(1 << params->tree_height);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
for (; idx < lastnode; idx++) {
|
|
|
|
set_ltree_addr(ltree_addr, idx);
|
|
|
|
set_ots_addr(ots_addr, idx);
|
2017-10-16 10:58:45 +01:00
|
|
|
gen_leaf_wots(params, stack+stackoffset*params->n, sk_seed, pub_seed, ltree_addr, ots_addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
stacklevels[stackoffset] = 0;
|
|
|
|
stackoffset++;
|
|
|
|
while (stackoffset>1 && stacklevels[stackoffset-1] == stacklevels[stackoffset-2]) {
|
|
|
|
set_tree_height(node_addr, stacklevels[stackoffset-1]);
|
|
|
|
set_tree_index(node_addr, (idx >> (stacklevels[stackoffset-1]+1)));
|
2017-10-16 10:58:45 +01:00
|
|
|
hash_h(params, stack+(stackoffset-2)*params->n, stack+(stackoffset-2)*params->n, pub_seed, node_addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
stacklevels[stackoffset-2]++;
|
|
|
|
stackoffset--;
|
|
|
|
}
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
node[i] = stack[i];
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* Computes the authpath and the root. This method is using a lot of space as we
|
|
|
|
* build the whole tree and then select the authpath nodes. For more efficient
|
|
|
|
* algorithms see e.g. the chapter on hash-based signatures in Bernstein,
|
|
|
|
* Buchmann, Dahmen. "Post-quantum Cryptography", Springer 2009.
|
|
|
|
*
|
|
|
|
* Returns the authpath in "authpath" with the node on level 0 at index 0.
|
2015-08-11 11:08:27 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
static void compute_authpath_wots(const xmss_params *params, unsigned char *root, unsigned char *authpath, unsigned long leaf_idx, const unsigned char *sk_seed, unsigned char *pub_seed, uint32_t addr[8])
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t i, j, level;
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char tree[2*(1 << params->tree_height)*params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
uint32_t ots_addr[8];
|
|
|
|
uint32_t ltree_addr[8];
|
|
|
|
uint32_t node_addr[8];
|
|
|
|
|
|
|
|
memcpy(ots_addr, addr, 12);
|
|
|
|
set_type(ots_addr, 0);
|
|
|
|
memcpy(ltree_addr, addr, 12);
|
|
|
|
set_type(ltree_addr, 1);
|
|
|
|
memcpy(node_addr, addr, 12);
|
|
|
|
set_type(node_addr, 2);
|
|
|
|
|
|
|
|
// Compute all leaves
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < (1U << params->tree_height); i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ltree_addr(ltree_addr, i);
|
|
|
|
set_ots_addr(ots_addr, i);
|
2017-10-16 10:58:45 +01:00
|
|
|
gen_leaf_wots(params, tree+((1 << params->tree_height)*params->n + i*params->n), sk_seed, pub_seed, ltree_addr, ots_addr);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
level = 0;
|
|
|
|
// Compute tree:
|
|
|
|
// Outer loop: For each inner layer
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = (1 << params->tree_height); i > 1; i>>=1) {
|
2017-08-03 16:38:34 +01:00
|
|
|
set_tree_height(node_addr, level);
|
|
|
|
// Inner loop: for each pair of sibling nodes
|
|
|
|
for (j = 0; j < i; j+=2) {
|
|
|
|
set_tree_index(node_addr, j>>1);
|
2017-10-16 10:58:45 +01:00
|
|
|
hash_h(params, tree + (i>>1)*params->n + (j>>1) * params->n, tree + i*params->n + j*params->n, pub_seed, node_addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy authpath
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->tree_height; i++) {
|
|
|
|
memcpy(authpath + i*params->n, tree + ((1 << params->tree_height)>>i)*params->n + ((leaf_idx >> i) ^ 1) * params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// copy root
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(root, tree+params->n, params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates a XMSS key pair for a given parameter set.
|
2016-07-11 10:15:16 +01:00
|
|
|
* Format sk: [(32bit) idx || SK_SEED || SK_PRF || PUB_SEED || root]
|
2015-08-11 11:08:27 +01:00
|
|
|
* Format pk: [root || PUB_SEED] omitting algo oid.
|
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmss_core_keypair(const xmss_params *params, unsigned char *pk, unsigned char *sk)
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
// Set idx = 0
|
|
|
|
sk[0] = 0;
|
|
|
|
sk[1] = 0;
|
|
|
|
sk[2] = 0;
|
|
|
|
sk[3] = 0;
|
2017-10-16 10:58:45 +01:00
|
|
|
// Init SK_SEED (params->n byte), SK_PRF (params->n byte), and PUB_SEED (params->n byte)
|
|
|
|
randombytes(sk+4, 3*params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Copy PUB_SEED to public key
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(pk+params->n, sk+4+2*params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
// Compute root
|
2017-10-16 10:58:45 +01:00
|
|
|
treehash(params, pk, 0, sk+4, sk+4+2*params->n, addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
// copy root to sk
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(sk+4+3*params->n, pk, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs a message.
|
2016-02-02 13:06:43 +00:00
|
|
|
* Returns
|
2015-08-11 11:08:27 +01:00
|
|
|
* 1. an array containing the signature followed by the message AND
|
|
|
|
* 2. an updated secret key!
|
2016-02-02 13:06:43 +00:00
|
|
|
*
|
2015-08-11 11:08:27 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmss_core_sign(const xmss_params *params, unsigned char *sk, unsigned char *sm, unsigned long long *smlen, const unsigned char *m, unsigned long long mlen)
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint16_t i = 0;
|
|
|
|
|
|
|
|
// Extract SK
|
|
|
|
uint32_t idx = ((unsigned long)sk[0] << 24) | ((unsigned long)sk[1] << 16) | ((unsigned long)sk[2] << 8) | sk[3];
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char sk_seed[params->n];
|
|
|
|
unsigned char sk_prf[params->n];
|
|
|
|
unsigned char pub_seed[params->n];
|
|
|
|
unsigned char hash_key[3*params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// index as 32 bytes string
|
|
|
|
unsigned char idx_bytes_32[32];
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(idx_bytes_32, 32, idx);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(sk_seed, sk+4, params->n);
|
|
|
|
memcpy(sk_prf, sk+4+params->n, params->n);
|
|
|
|
memcpy(pub_seed, sk+4+2*params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Update SK
|
|
|
|
sk[0] = ((idx + 1) >> 24) & 255;
|
|
|
|
sk[1] = ((idx + 1) >> 16) & 255;
|
|
|
|
sk[2] = ((idx + 1) >> 8) & 255;
|
|
|
|
sk[3] = (idx + 1) & 255;
|
2017-10-23 13:10:39 +01:00
|
|
|
// Secret key for this non-forward-secure version is now updated.
|
|
|
|
// A production implementation should consider using a file handle instead,
|
|
|
|
// and write the updated secret key at this point!
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Init working params
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char R[params->n];
|
|
|
|
unsigned char msg_h[params->n];
|
|
|
|
unsigned char root[params->n];
|
|
|
|
unsigned char ots_seed[params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
|
|
|
|
// ---------------------------------
|
|
|
|
// Message Hashing
|
|
|
|
// ---------------------------------
|
|
|
|
|
|
|
|
// Message Hash:
|
|
|
|
// First compute pseudorandom value
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, R, idx_bytes_32, sk_prf, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Generate hash key (R || root || idx)
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(hash_key, R, params->n);
|
|
|
|
memcpy(hash_key+params->n, sk+4+3*params->n, params->n);
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(hash_key+2*params->n, params->n, idx);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Then use it for message digest
|
2017-10-16 10:58:45 +01:00
|
|
|
h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Start collecting signature
|
|
|
|
*smlen = 0;
|
|
|
|
|
|
|
|
// Copy index to signature
|
|
|
|
sm[0] = (idx >> 24) & 255;
|
|
|
|
sm[1] = (idx >> 16) & 255;
|
|
|
|
sm[2] = (idx >> 8) & 255;
|
|
|
|
sm[3] = idx & 255;
|
|
|
|
|
|
|
|
sm += 4;
|
|
|
|
*smlen += 4;
|
|
|
|
|
|
|
|
// Copy R to signature
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++)
|
2017-06-02 16:29:14 +01:00
|
|
|
sm[i] = R[i];
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->n;
|
|
|
|
*smlen += params->n;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// ----------------------------------
|
|
|
|
// Now we start to "really sign"
|
|
|
|
// ----------------------------------
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Prepare Address
|
|
|
|
set_type(ots_addr, 0);
|
|
|
|
set_ots_addr(ots_addr, idx);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Compute seed for OTS key pair
|
2017-10-16 10:58:45 +01:00
|
|
|
get_seed(params, ots_seed, sk_seed, ots_addr);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Compute WOTS signature
|
2017-10-16 10:58:45 +01:00
|
|
|
wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->wots_keysize;
|
|
|
|
*smlen += params->wots_keysize;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
compute_authpath_wots(params, root, sm, idx, sk_seed, pub_seed, ots_addr);
|
|
|
|
sm += params->tree_height*params->n;
|
|
|
|
*smlen += params->tree_height*params->n;
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
memcpy(sm, m, mlen);
|
|
|
|
*smlen += mlen;
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2015-08-12 13:37:49 +01:00
|
|
|
/*
|
|
|
|
* Generates a XMSSMT key pair for a given parameter set.
|
|
|
|
* Format sk: [(ceil(h/8) bit) idx || SK_SEED || SK_PRF || PUB_SEED]
|
|
|
|
* Format pk: [root || PUB_SEED] omitting algo oid.
|
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmssmt_core_keypair(const xmss_params *params, unsigned char *pk, unsigned char *sk)
|
2015-08-12 13:37:49 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint16_t i;
|
|
|
|
// Set idx = 0
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
sk[i] = 0;
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
// Init SK_SEED (params->n byte), SK_PRF (params->n byte), and PUB_SEED (params->n byte)
|
|
|
|
randombytes(sk+params->index_len, 3*params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Copy PUB_SEED to public key
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(pk+params->n, sk+params->index_len+2*params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Set address to point on the single tree on layer d-1
|
|
|
|
uint32_t addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
2017-10-16 10:58:45 +01:00
|
|
|
set_layer_addr(addr, (params->d-1));
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Compute root
|
2017-10-16 10:58:45 +01:00
|
|
|
treehash(params, pk, 0, sk+params->index_len, pk+params->n, addr);
|
|
|
|
memcpy(sk+params->index_len+3*params->n, pk, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2015-08-12 13:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Signs a message.
|
2016-02-02 13:06:43 +00:00
|
|
|
* Returns
|
2015-08-12 13:37:49 +01:00
|
|
|
* 1. an array containing the signature followed by the message AND
|
|
|
|
* 2. an updated secret key!
|
2016-02-02 13:06:43 +00:00
|
|
|
*
|
2015-08-12 13:37:49 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmssmt_core_sign(const xmss_params *params, unsigned char *sk, unsigned char *sm, unsigned long long *smlen, const unsigned char *m, unsigned long long mlen)
|
2015-08-12 13:37:49 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint64_t idx_tree;
|
|
|
|
uint32_t idx_leaf;
|
|
|
|
uint64_t i;
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char sk_seed[params->n];
|
|
|
|
unsigned char sk_prf[params->n];
|
|
|
|
unsigned char pub_seed[params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
// Init working params
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char R[params->n];
|
|
|
|
unsigned char hash_key[3*params->n];
|
|
|
|
unsigned char msg_h[params->n];
|
|
|
|
unsigned char root[params->n];
|
|
|
|
unsigned char ots_seed[params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t ots_addr[8] = {0, 0, 0, 0, 0, 0, 0, 0};
|
|
|
|
unsigned char idx_bytes_32[32];
|
|
|
|
|
|
|
|
// Extract SK
|
|
|
|
unsigned long long idx = 0;
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
|
|
|
idx |= ((unsigned long long)sk[i]) << 8*(params->index_len - 1 - i);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2015-08-12 13:37:49 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(sk_seed, sk+params->index_len, params->n);
|
|
|
|
memcpy(sk_prf, sk+params->index_len+params->n, params->n);
|
|
|
|
memcpy(pub_seed, sk+params->index_len+2*params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Update SK
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
|
|
|
sk[i] = ((idx + 1) >> 8*(params->index_len - 1 - i)) & 255;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-23 13:10:39 +01:00
|
|
|
// Secret key for this non-forward-secure version is now updated.
|
|
|
|
// A production implementation should consider using a file handle instead,
|
|
|
|
// and write the updated secret key at this point!
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// ---------------------------------
|
|
|
|
// Message Hashing
|
|
|
|
// ---------------------------------
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Message Hash:
|
|
|
|
// First compute pseudorandom value
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(idx_bytes_32, 32, idx);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, R, idx_bytes_32, sk_prf, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Generate hash key (R || root || idx)
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(hash_key, R, params->n);
|
|
|
|
memcpy(hash_key+params->n, sk+params->index_len+3*params->n, params->n);
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(hash_key+2*params->n, params->n, idx);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Then use it for message digest
|
2017-10-16 10:58:45 +01:00
|
|
|
h_msg(params, msg_h, m, mlen, hash_key, 3*params->n);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Start collecting signature
|
|
|
|
*smlen = 0;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Copy index to signature
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
|
|
|
sm[i] = (idx >> 8*(params->index_len - 1 - i)) & 255;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2015-08-12 13:37:49 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->index_len;
|
|
|
|
*smlen += params->index_len;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Copy R to signature
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
sm[i] = R[i];
|
|
|
|
}
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->n;
|
|
|
|
*smlen += params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// ----------------------------------
|
|
|
|
// Now we start to "really sign"
|
|
|
|
// ----------------------------------
|
|
|
|
|
|
|
|
// Handle lowest layer separately as it is slightly different...
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-08-12 13:37:49 +01:00
|
|
|
// Prepare Address
|
2017-08-03 16:38:34 +01:00
|
|
|
set_type(ots_addr, 0);
|
2017-10-16 10:58:45 +01:00
|
|
|
idx_tree = idx >> params->tree_height;
|
|
|
|
idx_leaf = (idx & ((1 << params->tree_height)-1));
|
2017-08-03 16:38:34 +01:00
|
|
|
set_layer_addr(ots_addr, 0);
|
|
|
|
set_tree_addr(ots_addr, idx_tree);
|
|
|
|
set_ots_addr(ots_addr, idx_leaf);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-08-12 13:37:49 +01:00
|
|
|
// Compute seed for OTS key pair
|
2017-10-16 10:58:45 +01:00
|
|
|
get_seed(params, ots_seed, sk_seed, ots_addr);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2015-08-12 13:37:49 +01:00
|
|
|
// Compute WOTS signature
|
2017-10-16 10:58:45 +01:00
|
|
|
wots_sign(params, sm, msg_h, ots_seed, pub_seed, ots_addr);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->wots_keysize;
|
|
|
|
*smlen += params->wots_keysize;
|
2015-08-12 13:37:49 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
compute_authpath_wots(params, root, sm, idx_leaf, sk_seed, pub_seed, ots_addr);
|
|
|
|
sm += params->tree_height*params->n;
|
|
|
|
*smlen += params->tree_height*params->n;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
// Now loop over remaining layers...
|
|
|
|
unsigned int j;
|
2017-10-16 10:58:45 +01:00
|
|
|
for (j = 1; j < params->d; j++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
// Prepare Address
|
2017-10-16 10:58:45 +01:00
|
|
|
idx_leaf = (idx_tree & ((1 << params->tree_height)-1));
|
|
|
|
idx_tree = idx_tree >> params->tree_height;
|
2017-08-03 16:38:34 +01:00
|
|
|
set_layer_addr(ots_addr, j);
|
|
|
|
set_tree_addr(ots_addr, idx_tree);
|
|
|
|
set_ots_addr(ots_addr, idx_leaf);
|
|
|
|
|
|
|
|
// Compute seed for OTS key pair
|
2017-10-16 10:58:45 +01:00
|
|
|
get_seed(params, ots_seed, sk_seed, ots_addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
// Compute WOTS signature
|
2017-10-16 10:58:45 +01:00
|
|
|
wots_sign(params, sm, root, ots_seed, pub_seed, ots_addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->wots_keysize;
|
|
|
|
*smlen += params->wots_keysize;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
compute_authpath_wots(params, root, sm, idx_leaf, sk_seed, pub_seed, ots_addr);
|
|
|
|
sm += params->tree_height*params->n;
|
|
|
|
*smlen += params->tree_height*params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(sm, m, mlen);
|
|
|
|
*smlen += mlen;
|
2015-08-12 13:37:49 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2015-08-12 13:37:49 +01:00
|
|
|
}
|