2015-08-11 11:08:27 +01:00
|
|
|
#include <stdlib.h>
|
2017-06-02 13:45:16 +01:00
|
|
|
#include <string.h>
|
2016-07-11 10:15:16 +01:00
|
|
|
#include <stdint.h>
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-06-02 13:45:16 +01:00
|
|
|
#include "hash.h"
|
|
|
|
#include "hash_address.h"
|
|
|
|
#include "params.h"
|
2017-08-03 16:38:34 +01:00
|
|
|
#include "wots.h"
|
|
|
|
#include "xmss_commons.h"
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/**
|
|
|
|
* Converts the value of 'in' to 'len' bytes in big-endian byte order.
|
|
|
|
*/
|
|
|
|
void ull_to_bytes(unsigned char *out, unsigned long long in, uint32_t len)
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
int i;
|
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
for (i = len - 1; i >= 0; i--) {
|
2017-08-03 16:38:34 +01:00
|
|
|
out[i] = in & 0xff;
|
|
|
|
in = in >> 8;
|
|
|
|
}
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2017-06-02 13:45:16 +01:00
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* 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.
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
void gen_leaf_wots(const xmss_params *params, unsigned char *leaf,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *sk_seed, const unsigned char *pub_seed,
|
|
|
|
uint32_t ltree_addr[8], uint32_t ots_addr[8])
|
2017-06-02 13:45:16 +01:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char seed[params->n];
|
|
|
|
unsigned char pk[params->wots_keysize];
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
get_seed(params, seed, sk_seed, ots_addr);
|
|
|
|
wots_pkgen(params, pk, seed, pub_seed, ots_addr);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
l_tree(params, leaf, pk, pub_seed, ltree_addr);
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* Used for pseudo-random key generation.
|
|
|
|
* Generates the seed for the WOTS key pair at address 'addr'.
|
2017-06-02 13:45:16 +01:00
|
|
|
*
|
2017-10-23 13:10:39 +01:00
|
|
|
* Takes n-byte sk_seed and returns n-byte seed using 32 byte address 'addr'.
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
void get_seed(const xmss_params *params, unsigned char *seed,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *sk_seed, uint32_t addr[8])
|
2017-06-02 13:45:16 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned char bytes[32];
|
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Make sure that chain addr, hash addr, and key bit are zeroed. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_chain_addr(addr, 0);
|
|
|
|
set_hash_addr(addr, 0);
|
|
|
|
set_key_and_mask(addr, 0);
|
2017-10-23 13:10:39 +01:00
|
|
|
|
|
|
|
/* Generate seed. */
|
|
|
|
addr_to_bytes(bytes, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, seed, bytes, sk_seed, params->n);
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* Computes a leaf node from a WOTS public key using an L-tree.
|
|
|
|
* Note that this destroys the used WOTS public key.
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-23 13:10:39 +01:00
|
|
|
void l_tree(const xmss_params *params,
|
|
|
|
unsigned char *leaf, unsigned char *wots_pk,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2017-06-02 13:45:16 +01:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned int l = params->wots_len;
|
2017-10-23 13:10:39 +01:00
|
|
|
unsigned int parent_nodes;
|
|
|
|
uint32_t i;
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t height = 0;
|
|
|
|
|
|
|
|
set_tree_height(addr, height);
|
|
|
|
|
|
|
|
while (l > 1) {
|
2017-10-23 13:10:39 +01:00
|
|
|
parent_nodes = l >> 1;
|
|
|
|
for (i = 0; i < parent_nodes; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
set_tree_index(addr, i);
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Hashes the nodes at (i*2)*params->n and (i*2)*params->n + 1 */
|
|
|
|
hash_h(params, wots_pk + i*params->n, wots_pk + (i*2)*params->n, pub_seed, addr);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-23 13:10:39 +01:00
|
|
|
/* If the row contained an odd number of nodes, the last node was not hashed.
|
|
|
|
Instead, we pull it up to the next layer. */
|
2017-08-03 16:38:34 +01:00
|
|
|
if (l & 1) {
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(wots_pk + (l >> 1)*params->n, wots_pk + (l - 1)*params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
l = (l >> 1) + 1;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
l = l >> 1;
|
|
|
|
}
|
|
|
|
height++;
|
|
|
|
set_tree_height(addr, height);
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(leaf, wots_pk, params->n);
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-23 13:10:39 +01:00
|
|
|
* Computes a root node given a leaf and an auth path
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
static void validate_authpath(const xmss_params *params, unsigned char *root,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *leaf, unsigned long leafidx,
|
|
|
|
const unsigned char *authpath,
|
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2017-06-02 13:45:16 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t i, j;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char buffer[2*params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* If leafidx is odd (last bit = 1), current path element is a right child
|
|
|
|
and authpath has to go left. Otherwise it is the other way around. */
|
2017-08-03 16:38:34 +01:00
|
|
|
if (leafidx & 1) {
|
2017-10-16 10:58:45 +01:00
|
|
|
for (j = 0; j < params->n; j++) {
|
|
|
|
buffer[params->n + j] = leaf[j];
|
2017-08-03 16:38:34 +01:00
|
|
|
buffer[j] = authpath[j];
|
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-10-16 10:58:45 +01:00
|
|
|
for (j = 0; j < params->n; j++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
buffer[j] = leaf[j];
|
2017-10-16 10:58:45 +01:00
|
|
|
buffer[params->n + j] = authpath[j];
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
authpath += params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->tree_height-1; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
set_tree_height(addr, i);
|
|
|
|
leafidx >>= 1;
|
|
|
|
set_tree_index(addr, leafidx);
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Pick the right or left neighbor, depending on parity of the node. */
|
2017-08-03 16:38:34 +01:00
|
|
|
if (leafidx & 1) {
|
2017-10-16 10:58:45 +01:00
|
|
|
hash_h(params, buffer + params->n, buffer, pub_seed, addr);
|
|
|
|
for (j = 0; j < params->n; j++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
buffer[j] = authpath[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-10-16 10:58:45 +01:00
|
|
|
hash_h(params, buffer, buffer, pub_seed, addr);
|
|
|
|
for (j = 0; j < params->n; j++) {
|
|
|
|
buffer[j + params->n] = authpath[j];
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
authpath += params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
set_tree_height(addr, params->tree_height - 1);
|
2017-08-03 16:38:34 +01:00
|
|
|
leafidx >>= 1;
|
|
|
|
set_tree_index(addr, leafidx);
|
2017-10-16 10:58:45 +01:00
|
|
|
hash_h(params, root, buffer, pub_seed, addr);
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies a given message signature pair under a given public key.
|
2017-10-23 13:10:39 +01:00
|
|
|
* Note that this assumes a pk without an OID, i.e. [root || PUB_SEED]
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmss_core_sign_open(const xmss_params *params,
|
|
|
|
unsigned char *m, unsigned long long *mlen,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *sm, unsigned long long smlen,
|
|
|
|
const unsigned char *pk)
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned long long i;
|
|
|
|
unsigned long idx = 0;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char wots_pk[params->wots_keysize];
|
|
|
|
unsigned char pkhash[params->n];
|
|
|
|
unsigned char root[params->n];
|
|
|
|
unsigned char msg_h[params->n];
|
|
|
|
unsigned char hash_key[3*params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char pub_seed[params->n];
|
|
|
|
memcpy(pub_seed, pk + params->n, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
uint32_t ots_addr[8] = {0};
|
|
|
|
uint32_t ltree_addr[8] = {0};
|
|
|
|
uint32_t node_addr[8] = {0};
|
|
|
|
|
|
|
|
set_type(ots_addr, 0);
|
|
|
|
set_type(ltree_addr, 1);
|
|
|
|
set_type(node_addr, 2);
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
*mlen = smlen - params->bytes;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Convert the index bytes from the signature to an integer. */
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
|
|
|
idx |= ((unsigned long long)sm[i]) << (8*(params->index_len - 1 - i));
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Prepare the hash key, of the form [R || root || idx]. */
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(hash_key, sm + params->index_len, params->n);
|
|
|
|
memcpy(hash_key + params->n, pk, params->n);
|
2017-10-23 13:10:39 +01:00
|
|
|
ull_to_bytes(hash_key + 2*params->n, idx, params->n);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the message hash. */
|
2017-10-16 10:58:45 +01:00
|
|
|
h_msg(params, msg_h, sm + params->bytes, *mlen, hash_key, 3*params->n);
|
|
|
|
sm += params->index_len + params->n;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* The WOTS public key is only correct if the signature was correct. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ots_addr(ots_addr, idx);
|
2017-10-16 10:58:45 +01:00
|
|
|
wots_pk_from_sig(params, wots_pk, sm, msg_h, pub_seed, ots_addr);
|
|
|
|
sm += params->wots_keysize;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the leaf node using the WOTS public key. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ltree_addr(ltree_addr, idx);
|
2017-10-16 10:58:45 +01:00
|
|
|
l_tree(params, pkhash, wots_pk, pub_seed, ltree_addr);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the root node. */
|
2017-10-16 10:58:45 +01:00
|
|
|
validate_authpath(params, root, pkhash, idx, sm, pub_seed, node_addr);
|
|
|
|
sm += params->tree_height*params->n;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Check if the root node equals the root node in the public key. */
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
if (root[i] != pk[i]) {
|
|
|
|
for (i = 0; i < *mlen; i++) {
|
|
|
|
m[i] = 0;
|
|
|
|
}
|
|
|
|
*mlen = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* If verification was successful, copy the message from the signature. */
|
2017-08-03 16:38:34 +01:00
|
|
|
for (i = 0; i < *mlen; i++) {
|
|
|
|
m[i] = sm[i];
|
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies a given message signature pair under a given public key.
|
2017-10-23 13:10:39 +01:00
|
|
|
* Note that this assumes a pk without an OID, i.e. [root || PUB_SEED]
|
2017-06-02 13:45:16 +01:00
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int xmssmt_core_sign_open(const xmss_params *params,
|
|
|
|
unsigned char *m, unsigned long long *mlen,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *sm, unsigned long long smlen,
|
|
|
|
const unsigned char *pk)
|
2017-06-02 13:45:16 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
uint32_t idx_leaf;
|
|
|
|
unsigned long long i;
|
|
|
|
unsigned long long idx = 0;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char wots_pk[params->wots_keysize];
|
|
|
|
unsigned char pkhash[params->n];
|
|
|
|
unsigned char root[params->n];
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned char *msg_h = root;
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char hash_key[3*params->n];
|
|
|
|
const unsigned char *pub_seed = pk + params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
uint32_t ots_addr[8] = {0};
|
|
|
|
uint32_t ltree_addr[8] = {0};
|
|
|
|
uint32_t node_addr[8] = {0};
|
|
|
|
|
|
|
|
set_type(ots_addr, 0);
|
|
|
|
set_type(ltree_addr, 1);
|
|
|
|
set_type(node_addr, 2);
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
*mlen = smlen - params->bytes;
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Convert the index bytes from the signature to an integer. */
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->index_len; i++) {
|
|
|
|
idx |= ((unsigned long long)sm[i]) << (8*(params->index_len - 1 - i));
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Prepare the hash key, of the form [R || root || idx]. */
|
2017-10-16 10:58:45 +01:00
|
|
|
memcpy(hash_key, sm + params->index_len, params->n);
|
|
|
|
memcpy(hash_key + params->n, pk, params->n);
|
2017-10-23 13:10:39 +01:00
|
|
|
ull_to_bytes(hash_key + 2*params->n, idx, params->n);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the message hash. */
|
2017-10-16 10:58:45 +01:00
|
|
|
h_msg(params, msg_h, sm + params->bytes, *mlen, hash_key, 3*params->n);
|
|
|
|
sm += params->index_len + params->n;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* For each subtree.. */
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->d; i++) {
|
|
|
|
idx_leaf = (idx & ((1 << params->tree_height)-1));
|
|
|
|
idx = idx >> params->tree_height;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
set_layer_addr(ots_addr, i);
|
|
|
|
set_layer_addr(ltree_addr, i);
|
|
|
|
set_layer_addr(node_addr, i);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
set_tree_addr(ltree_addr, idx);
|
|
|
|
set_tree_addr(ots_addr, idx);
|
|
|
|
set_tree_addr(node_addr, idx);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* The WOTS public key is only correct if the signature was correct. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ots_addr(ots_addr, idx_leaf);
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Initially, root = msg_h, 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_pk_from_sig(params, wots_pk, sm, root, pub_seed, ots_addr);
|
|
|
|
sm += params->wots_keysize;
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the leaf node using the WOTS public key. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_ltree_addr(ltree_addr, idx_leaf);
|
2017-10-16 10:58:45 +01:00
|
|
|
l_tree(params, pkhash, wots_pk, pub_seed, ltree_addr);
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Compute the root node of this subtree. */
|
2017-10-16 10:58:45 +01:00
|
|
|
validate_authpath(params, root, pkhash, idx_leaf, sm, pub_seed, node_addr);
|
|
|
|
sm += params->tree_height*params->n;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Check if the final root node equals the root node in the public key. */
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
if (root[i] != pk[i]) {
|
|
|
|
for (i = 0; i < *mlen; i++) {
|
|
|
|
m[i] = 0;
|
|
|
|
}
|
|
|
|
*mlen = -1;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* If verification was successful, copy the message from the signature. */
|
2017-08-03 16:38:34 +01:00
|
|
|
for (i = 0; i < *mlen; i++) {
|
|
|
|
m[i] = sm[i];
|
|
|
|
}
|
2017-06-02 13:45:16 +01:00
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
return 0;
|
2017-06-02 13:45:16 +01:00
|
|
|
}
|