2017-10-30 16:24:10 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2016-02-16 15:31:18 +00:00
|
|
|
#include "hash_address.h"
|
2016-07-11 10:15:16 +01:00
|
|
|
#include "xmss_commons.h"
|
2017-06-02 16:29:14 +01:00
|
|
|
#include "params.h"
|
2016-07-12 15:19:42 +01:00
|
|
|
#include "hash.h"
|
2017-06-02 16:29:14 +01:00
|
|
|
#include "fips202.h"
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
void addr_to_bytes(unsigned char *bytes, const uint32_t addr[8])
|
2017-06-02 16:29:14 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 8; i++) {
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(bytes + i*4, 4, addr[i]);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2016-07-12 15:19:42 +01:00
|
|
|
}
|
2015-08-11 11:08:27 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
static int core_hash(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned int type,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *key, unsigned int keylen,
|
|
|
|
const unsigned char *in, unsigned long long inlen, int n)
|
2017-06-02 16:29:14 +01:00
|
|
|
{
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned char buf[inlen + n + keylen];
|
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* We arrange the input into the hash function to be of the form:
|
2017-10-30 16:24:10 +00:00
|
|
|
* toByte(X, 32) || KEY || M */
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(buf, n, type);
|
2017-10-30 16:24:10 +00:00
|
|
|
memcpy(buf + n, key, keylen);
|
|
|
|
memcpy(buf + keylen + n, in, inlen);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
if (n == 32 && params->func == XMSS_SHA2) {
|
2017-08-03 16:38:34 +01:00
|
|
|
SHA256(buf, inlen + keylen + n, out);
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
else if (n == 32 && params->func == XMSS_SHAKE) {
|
2017-08-03 16:38:34 +01:00
|
|
|
shake128(out, 32, buf, inlen + keylen + n);
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
else if (n == 64 && params->func == XMSS_SHA2) {
|
2017-08-03 16:38:34 +01:00
|
|
|
SHA512(buf, inlen + keylen + n, out);
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
else if (n == 64 && params->func == XMSS_SHAKE) {
|
2017-08-03 16:38:34 +01:00
|
|
|
shake256(out, 64, buf, inlen + keylen + n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
int prf(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *key, unsigned int keylen)
|
2017-06-02 16:29:14 +01:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
return core_hash(params, out, 3, key, keylen, in, 32, keylen);
|
2016-07-11 10:15:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
int h_msg(const xmss_params *params,
|
|
|
|
unsigned char *out,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *in, unsigned long long inlen,
|
|
|
|
const unsigned char *key, const unsigned int keylen)
|
2016-07-11 10:15:16 +01:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
return core_hash(params, out, 2, key, keylen, in, inlen, params->n);
|
2016-07-11 10:15:16 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 16:24:10 +00:00
|
|
|
/*
|
|
|
|
* Computes the message hash using R, the public root, the index of the leaf
|
|
|
|
* node, and the message. Notably, it requires m_with_prefix to have 4*n bytes
|
|
|
|
* of space before the message, to use for the prefix. This is necessary to
|
|
|
|
* prevent having to move the message around (and thus allocate memory for it).
|
|
|
|
*/
|
|
|
|
int hash_message(const xmss_params *params, unsigned char *out,
|
|
|
|
const unsigned char *R, const unsigned char *root,
|
|
|
|
unsigned long long idx,
|
|
|
|
unsigned char *m_with_prefix, unsigned long long mlen)
|
|
|
|
{
|
|
|
|
/* We're creating a hash using input of the form:
|
|
|
|
toByte(X, 32) || R || root || index || M */
|
|
|
|
ull_to_bytes(m_with_prefix, params->n, 2);
|
|
|
|
memcpy(m_with_prefix + params->n, R, params->n);
|
|
|
|
memcpy(m_with_prefix + 2 * params->n, root, params->n);
|
|
|
|
ull_to_bytes(m_with_prefix + 3 * params->n, params->n, idx);
|
|
|
|
|
|
|
|
/* Since the message can be bigger than the stack, this cannot use the
|
|
|
|
* core_hash function. */
|
|
|
|
if (params->n == 32 && params->func == XMSS_SHA2) {
|
|
|
|
SHA256(m_with_prefix, mlen + 4*params->n, out);
|
|
|
|
}
|
|
|
|
else if (params->n == 32 && params->func == XMSS_SHAKE) {
|
|
|
|
shake128(out, 32, m_with_prefix, mlen + 4*params->n);
|
|
|
|
}
|
|
|
|
else if (params->n == 64 && params->func == XMSS_SHA2) {
|
|
|
|
SHA512(m_with_prefix, mlen + 4*params->n, out);
|
|
|
|
}
|
|
|
|
else if (params->n == 64 && params->func == XMSS_SHAKE) {
|
|
|
|
shake256(out, 64, m_with_prefix, mlen + 4*params->n);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:08:27 +01:00
|
|
|
/**
|
|
|
|
* We assume the left half is in in[0]...in[n-1]
|
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int hash_h(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char buf[2*params->n];
|
|
|
|
unsigned char key[params->n];
|
|
|
|
unsigned char bitmask[2*params->n];
|
2017-10-23 13:10:39 +01:00
|
|
|
unsigned char addr_as_bytes[32];
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned int i;
|
|
|
|
|
2017-10-23 13:10:39 +01:00
|
|
|
/* Generate the n-byte key. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_key_and_mask(addr, 0);
|
2017-10-23 13:10:39 +01:00
|
|
|
addr_to_bytes(addr_as_bytes, addr);
|
|
|
|
prf(params, key, addr_as_bytes, pub_seed, params->n);
|
|
|
|
|
|
|
|
/* Generate the 2n-byte mask. */
|
2017-08-03 16:38:34 +01:00
|
|
|
set_key_and_mask(addr, 1);
|
2017-10-23 13:10:39 +01:00
|
|
|
addr_to_bytes(addr_as_bytes, addr);
|
|
|
|
prf(params, bitmask, addr_as_bytes, pub_seed, params->n);
|
|
|
|
|
2017-08-03 16:38:34 +01:00
|
|
|
set_key_and_mask(addr, 2);
|
2017-10-23 13:10:39 +01:00
|
|
|
addr_to_bytes(addr_as_bytes, addr);
|
|
|
|
prf(params, bitmask + params->n, addr_as_bytes, pub_seed, params->n);
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < 2*params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
buf[i] = in[i] ^ bitmask[i];
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
return core_hash(params, out, 1, key, params->n, buf, 2*params->n, params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
int hash_f(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
2017-08-03 16:38:34 +01:00
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2016-02-02 13:06:43 +00:00
|
|
|
{
|
2017-10-16 10:58:45 +01:00
|
|
|
unsigned char buf[params->n];
|
|
|
|
unsigned char key[params->n];
|
|
|
|
unsigned char bitmask[params->n];
|
2017-10-23 13:10:39 +01:00
|
|
|
unsigned char addr_as_bytes[32];
|
2017-08-03 16:38:34 +01:00
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
set_key_and_mask(addr, 0);
|
2017-10-23 13:10:39 +01:00
|
|
|
addr_to_bytes(addr_as_bytes, addr);
|
|
|
|
prf(params, key, addr_as_bytes, pub_seed, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
set_key_and_mask(addr, 1);
|
2017-10-23 13:10:39 +01:00
|
|
|
addr_to_bytes(addr_as_bytes, addr);
|
|
|
|
prf(params, bitmask, addr_as_bytes, pub_seed, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2017-08-03 16:38:34 +01:00
|
|
|
buf[i] = in[i] ^ bitmask[i];
|
|
|
|
}
|
2017-10-16 10:58:45 +01:00
|
|
|
return core_hash(params, out, 0, key, params->n, buf, params->n, params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|