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-24 14:49:36 +01:00
|
|
|
* For a given leaf index, computes the authentication path and the resulting
|
|
|
|
* root node using Merkle's TreeHash algorithm.
|
|
|
|
* Expects the layer and tree parts of subtree_addr to be set.
|
2015-08-11 11:08:27 +01:00
|
|
|
*/
|
2017-10-24 10:53:45 +01:00
|
|
|
static void treehash(const xmss_params *params,
|
|
|
|
unsigned char *root, unsigned char *auth_path,
|
|
|
|
const unsigned char *sk_seed,
|
|
|
|
const unsigned char *pub_seed,
|
|
|
|
uint32_t leaf_idx, const uint32_t subtree_addr[8])
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-10-23 16:31:01 +01:00
|
|
|
unsigned char stack[(params->tree_height+1)*params->n];
|
|
|
|
unsigned int heights[params->tree_height+1];
|
|
|
|
unsigned int offset = 0;
|
|
|
|
|
|
|
|
/* The subtree has at most 2^20 leafs, so uint32_t suffices. */
|
|
|
|
uint32_t idx;
|
2017-10-24 10:53:45 +01:00
|
|
|
uint32_t tree_idx;
|
2017-10-23 16:31:01 +01:00
|
|
|
|
|
|
|
/* We need all three types of addresses in parallel. */
|
|
|
|
uint32_t ots_addr[8] = {0};
|
|
|
|
uint32_t ltree_addr[8] = {0};
|
|
|
|
uint32_t node_addr[8] = {0};
|
|
|
|
|
2017-10-24 10:53:45 +01:00
|
|
|
/* Select the required subtree. */
|
|
|
|
copy_subtree_addr(ots_addr, subtree_addr);
|
|
|
|
copy_subtree_addr(ltree_addr, subtree_addr);
|
|
|
|
copy_subtree_addr(node_addr, subtree_addr);
|
2017-10-23 16:31:01 +01:00
|
|
|
|
2017-10-24 15:20:31 +01:00
|
|
|
set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
|
|
|
|
set_type(ltree_addr, XMSS_ADDR_TYPE_LTREE);
|
|
|
|
set_type(node_addr, XMSS_ADDR_TYPE_HASHTREE);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-23 16:31:01 +01:00
|
|
|
for (idx = 0; idx < (uint32_t)(1 << params->tree_height); idx++) {
|
|
|
|
/* Add the next leaf node to the stack. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ltree_addr(ltree_addr, idx);
|
|
|
|
set_ots_addr(ots_addr, idx);
|
2017-10-23 16:31:01 +01:00
|
|
|
gen_leaf_wots(params, stack + offset*params->n,
|
|
|
|
sk_seed, pub_seed, ltree_addr, ots_addr);
|
|
|
|
offset++;
|
2017-10-24 10:53:45 +01:00
|
|
|
heights[offset - 1] = 0;
|
|
|
|
|
|
|
|
/* If this is a node we need for the auth path.. */
|
|
|
|
if ((leaf_idx ^ 0x1) == idx) {
|
|
|
|
memcpy(auth_path, stack + (offset - 1)*params->n, params->n);
|
|
|
|
}
|
2017-10-23 16:31:01 +01:00
|
|
|
|
|
|
|
/* While the top-most nodes are of equal height.. */
|
|
|
|
while (offset >= 2 && heights[offset - 1] == heights[offset - 2]) {
|
2017-10-24 10:53:45 +01:00
|
|
|
/* Compute index of the new node, in the next layer. */
|
|
|
|
tree_idx = (idx >> (heights[offset - 1] + 1));
|
|
|
|
|
2017-10-23 16:31:01 +01:00
|
|
|
/* Hash the top-most nodes from the stack together. */
|
2017-10-24 10:53:45 +01:00
|
|
|
/* Note that tree height is the 'lower' layer, even though we use
|
|
|
|
the index of the new node on the 'higher' layer. This follows
|
|
|
|
from the fact that we address the hash function calls. */
|
2017-10-23 16:31:01 +01:00
|
|
|
set_tree_height(node_addr, heights[offset - 1]);
|
2017-10-24 10:53:45 +01:00
|
|
|
set_tree_index(node_addr, tree_idx);
|
2017-10-23 16:31:01 +01:00
|
|
|
hash_h(params, stack + (offset-2)*params->n,
|
|
|
|
stack + (offset-2)*params->n, pub_seed, node_addr);
|
|
|
|
offset--;
|
2017-10-24 10:53:45 +01:00
|
|
|
/* Note that the top-most node is now one layer higher. */
|
|
|
|
heights[offset - 1]++;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 10:53:45 +01:00
|
|
|
/* If this is a node we need for the auth path.. */
|
|
|
|
if (((leaf_idx >> heights[offset - 1]) ^ 0x1) == tree_idx) {
|
|
|
|
memcpy(auth_path + heights[offset - 1]*params->n,
|
|
|
|
stack + (offset - 1)*params->n, params->n);
|
|
|
|
}
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 10:53:45 +01:00
|
|
|
memcpy(root, stack, params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generates a XMSS key pair for a given parameter set.
|
2017-10-23 16:31:01 +01:00
|
|
|
* Format sk: [(32bit) index || SK_SEED || SK_PRF || PUB_SEED || root]
|
|
|
|
* Format pk: [root || PUB_SEED], omitting algorithm OID.
|
2015-08-11 11:08:27 +01:00
|
|
|
*/
|
2017-10-23 16:31:01 +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-10-24 11:02:42 +01:00
|
|
|
/* The key generation procedure of XMSS and XMSSMT is exactly the same.
|
|
|
|
The only important detail is that the right subtree must be selected;
|
|
|
|
this requires us to correctly set the d=1 parameter for XMSS. */
|
|
|
|
return xmssmt_core_keypair(params, pk, sk);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-24 14:49:36 +01:00
|
|
|
* Signs a message. Returns an array containing the signature followed by the
|
|
|
|
* message and an updated secret key.
|
2015-08-11 11:08:27 +01:00
|
|
|
*/
|
2017-10-24 14:49:36 +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-10-24 14:49:36 +01:00
|
|
|
const unsigned char *sk_seed = sk + params->index_len;
|
|
|
|
const unsigned char *sk_prf = sk + params->index_len + params->n;
|
|
|
|
const unsigned char *pub_seed = sk + params->index_len + 2*params->n;
|
|
|
|
const unsigned char *pub_root = sk + params->index_len + 3*params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char root[params->n];
|
2017-10-24 14:49:36 +01:00
|
|
|
unsigned char mhash[params->n];
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char ots_seed[params->n];
|
2017-10-24 14:49:36 +01:00
|
|
|
unsigned long idx;
|
|
|
|
unsigned char idx_bytes_32[32];
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
uint32_t ots_addr[8] = {0};
|
2017-10-24 15:20:31 +01:00
|
|
|
set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Read and use the current index from the secret key. */
|
|
|
|
idx = (unsigned long)bytes_to_ull(sk, params->index_len);
|
|
|
|
memcpy(sm, sk, params->index_len);
|
|
|
|
sm += params->index_len;
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* THIS IS WHERE PRODUCTION IMPLEMENTATIONS WOULD UPDATE THE SECRET KEY. *
|
|
|
|
*************************************************************************/
|
|
|
|
/* Increment the index in the secret key. */
|
|
|
|
ull_to_bytes(sk, params->index_len, idx + 1);
|
|
|
|
|
|
|
|
/* Compute the digest randomization value. */
|
|
|
|
ull_to_bytes(idx_bytes_32, 32, idx);
|
|
|
|
prf(params, sm, idx_bytes_32, sk_prf, params->n);
|
|
|
|
|
|
|
|
/* Compute the message hash. */
|
|
|
|
hash_message(params, mhash, sm, pub_root, idx, m, mlen);
|
|
|
|
sm += params->n;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ots_addr(ots_addr, idx);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Get a seed for the WOTS keypair. */
|
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-10-24 14:49:36 +01:00
|
|
|
/* Compute a WOTS signature on the message hash. */
|
|
|
|
wots_sign(params, sm, mhash, ots_seed, pub_seed, ots_addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->wots_keysize;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Compute the authentication path for the used WOTS leaf. */
|
2017-10-24 10:53:45 +01:00
|
|
|
treehash(params, root, sm, sk_seed, pub_seed, idx, ots_addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += 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);
|
2017-10-24 14:49:36 +01:00
|
|
|
*smlen = params->bytes + 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.
|
2017-10-23 16:31:01 +01:00
|
|
|
* Format sk: [(ceil(h/8) bit) index || SK_SEED || SK_PRF || PUB_SEED]
|
|
|
|
* Format pk: [root || PUB_SEED] omitting algorithm OID.
|
2015-08-12 13:37:49 +01:00
|
|
|
*/
|
2017-10-24 14:49:36 +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-10-24 10:53:45 +01:00
|
|
|
/* We do not need the auth path in key generation, but it simplifies the
|
|
|
|
code to have just one treehash routine that computes both root and path
|
|
|
|
in one function. */
|
|
|
|
unsigned char auth_path[params->tree_height * params->n];
|
|
|
|
uint32_t top_tree_addr[8] = {0};
|
|
|
|
set_layer_addr(top_tree_addr, params->d - 1);
|
|
|
|
|
2017-10-23 16:31:01 +01:00
|
|
|
/* Initialize index to 0. */
|
|
|
|
memset(sk, 0, params->index_len);
|
2017-10-24 16:21:16 +01:00
|
|
|
sk += params->index_len;
|
2017-10-23 16:31:01 +01:00
|
|
|
|
|
|
|
/* Initialize SK_SEED, SK_PRF and PUB_SEED. */
|
|
|
|
randombytes(sk, 3 * params->n);
|
|
|
|
memcpy(pk + params->n, sk + 2*params->n, params->n);
|
|
|
|
|
|
|
|
/* Compute root node of the top-most subtree. */
|
2017-10-24 10:53:45 +01:00
|
|
|
treehash(params, pk, auth_path, sk, pk + params->n, 0, top_tree_addr);
|
2017-10-23 16:31:01 +01:00
|
|
|
memcpy(sk + 3*params->n, pk, params->n);
|
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2015-08-12 13:37:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-24 14:49:36 +01:00
|
|
|
* Signs a message. Returns an array containing the signature followed by the
|
|
|
|
* message and an updated secret key.
|
2015-08-12 13:37:49 +01:00
|
|
|
*/
|
2017-10-24 14:49:36 +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-10-24 14:49:36 +01:00
|
|
|
const unsigned char *sk_seed = sk + params->index_len;
|
|
|
|
const unsigned char *sk_prf = sk + params->index_len + params->n;
|
|
|
|
const unsigned char *pub_seed = sk + params->index_len + 2*params->n;
|
|
|
|
const unsigned char *pub_root = sk + params->index_len + 3*params->n;
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char root[params->n];
|
2017-10-24 14:49:36 +01:00
|
|
|
unsigned char *mhash = root;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char ots_seed[params->n];
|
2017-10-24 14:49:36 +01:00
|
|
|
unsigned long long idx;
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned char idx_bytes_32[32];
|
2017-10-24 14:49:36 +01:00
|
|
|
unsigned int i;
|
|
|
|
uint32_t idx_leaf;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
uint32_t ots_addr[8] = {0};
|
2017-10-24 15:20:31 +01:00
|
|
|
set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Read and use the current index from the secret key. */
|
|
|
|
idx = (unsigned long)bytes_to_ull(sk, params->index_len);
|
|
|
|
memcpy(sm, sk, params->index_len);
|
|
|
|
sm += params->index_len;
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/*************************************************************************
|
|
|
|
* THIS IS WHERE PRODUCTION IMPLEMENTATIONS WOULD UPDATE THE SECRET KEY. *
|
|
|
|
*************************************************************************/
|
|
|
|
/* Increment the index in the secret key. */
|
|
|
|
ull_to_bytes(sk, params->index_len, idx + 1);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Compute the digest randomization value. */
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(idx_bytes_32, 32, idx);
|
2017-10-24 14:49:36 +01:00
|
|
|
prf(params, sm, idx_bytes_32, sk_prf, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Compute the message hash. */
|
|
|
|
hash_message(params, mhash, sm, pub_root, idx, m, mlen);
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-24 15:20:31 +01:00
|
|
|
set_type(ots_addr, XMSS_ADDR_TYPE_OTS);
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
for (i = 0; i < params->d; i++) {
|
|
|
|
idx_leaf = (idx & ((1 << params->tree_height)-1));
|
|
|
|
idx = idx >> params->tree_height;
|
2015-08-12 13:37:49 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
set_layer_addr(ots_addr, i);
|
|
|
|
set_tree_addr(ots_addr, idx);
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ots_addr(ots_addr, idx_leaf);
|
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Get a seed for the WOTS keypair. */
|
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
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Compute a WOTS signature. */
|
|
|
|
/* Initially, root = mhash, but on subsequent iterations it is the root
|
|
|
|
of the subtree below the currently processed subtree. */
|
2017-10-16 10:58:45 +01:00
|
|
|
wots_sign(params, sm, root, ots_seed, pub_seed, ots_addr);
|
|
|
|
sm += params->wots_keysize;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-24 14:49:36 +01:00
|
|
|
/* Compute the authentication path for the used WOTS leaf. */
|
2017-10-24 10:53:45 +01:00
|
|
|
treehash(params, root, sm, sk_seed, pub_seed, idx_leaf, ots_addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
sm += params->tree_height*params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(sm, m, mlen);
|
2017-10-24 14:49:36 +01:00
|
|
|
*smlen = params->bytes + 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
|
|
|
}
|