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"
|
2017-11-01 13:59:33 +00:00
|
|
|
#include "utils.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-30 22:49:30 +00:00
|
|
|
#define XMSS_HASH_PADDING_F 0
|
|
|
|
#define XMSS_HASH_PADDING_H 1
|
|
|
|
#define XMSS_HASH_PADDING_HASH 2
|
|
|
|
#define XMSS_HASH_PADDING_PRF 3
|
2020-04-28 15:02:06 +01:00
|
|
|
#define XMSS_HASH_PADDING_PRF_KEYGEN 4
|
2017-10-30 22:49:30 +00: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,
|
2017-10-30 22:49:30 +00:00
|
|
|
unsigned char *out,
|
|
|
|
const unsigned char *in, unsigned long long inlen)
|
2017-06-02 16:29:14 +01:00
|
|
|
{
|
2017-10-30 22:49:30 +00:00
|
|
|
if (params->n == 32 && params->func == XMSS_SHA2) {
|
|
|
|
SHA256(in, inlen, out);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-30 22:49:30 +00:00
|
|
|
else if (params->n == 32 && params->func == XMSS_SHAKE) {
|
|
|
|
shake128(out, 32, in, inlen);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-30 22:49:30 +00:00
|
|
|
else if (params->n == 64 && params->func == XMSS_SHA2) {
|
|
|
|
SHA512(in, inlen, out);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2017-10-30 22:49:30 +00:00
|
|
|
else if (params->n == 64 && params->func == XMSS_SHAKE) {
|
|
|
|
shake256(out, 64, in, inlen);
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-11-01 13:33:07 +00:00
|
|
|
return -1;
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2017-10-30 22:49:30 +00:00
|
|
|
/*
|
|
|
|
* Computes PRF(key, in), for a key of params->n bytes, and a 32-byte input.
|
|
|
|
*/
|
2017-10-16 10:58:45 +01:00
|
|
|
int prf(const xmss_params *params,
|
2017-10-30 22:49:30 +00:00
|
|
|
unsigned char *out, const unsigned char in[32],
|
|
|
|
const unsigned char *key)
|
2017-06-02 16:29:14 +01:00
|
|
|
{
|
2020-04-14 20:18:01 +01:00
|
|
|
unsigned char buf[params->padding_len + params->n + 32];
|
2016-07-11 10:15:16 +01:00
|
|
|
|
2020-04-14 20:18:01 +01:00
|
|
|
ull_to_bytes(buf, params->padding_len, XMSS_HASH_PADDING_PRF);
|
|
|
|
memcpy(buf + params->padding_len, key, params->n);
|
|
|
|
memcpy(buf + params->padding_len + params->n, in, 32);
|
2017-10-30 22:49:30 +00:00
|
|
|
|
2020-04-14 20:18:01 +01:00
|
|
|
return core_hash(params, out, buf, params->padding_len + params->n + 32);
|
2016-07-11 10:15:16 +01:00
|
|
|
}
|
|
|
|
|
2020-04-28 15:02:06 +01:00
|
|
|
/*
|
|
|
|
* Computes PRF_keygen(key, in), for a key of params->n bytes, and an input
|
|
|
|
* of 32 + params->n bytes
|
|
|
|
*/
|
|
|
|
int prf_keygen(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
|
|
|
const unsigned char *key)
|
|
|
|
{
|
|
|
|
unsigned char buf[params->padding_len + 2*params->n + 32];
|
|
|
|
|
|
|
|
ull_to_bytes(buf, params->padding_len, XMSS_HASH_PADDING_PRF_KEYGEN);
|
|
|
|
memcpy(buf + params->padding_len, key, params->n);
|
|
|
|
memcpy(buf + params->padding_len + params->n, in, params->n + 32);
|
|
|
|
|
|
|
|
return core_hash(params, out, buf, params->padding_len + 2*params->n + 32);
|
|
|
|
}
|
|
|
|
|
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 */
|
2020-04-14 20:18:01 +01:00
|
|
|
ull_to_bytes(m_with_prefix, params->padding_len, XMSS_HASH_PADDING_HASH);
|
|
|
|
memcpy(m_with_prefix + params->padding_len, R, params->n);
|
|
|
|
memcpy(m_with_prefix + params->padding_len + params->n, root, params->n);
|
|
|
|
ull_to_bytes(m_with_prefix + params->padding_len + 2*params->n, params->n, idx);
|
2017-10-30 16:24:10 +00:00
|
|
|
|
2020-04-14 20:18:01 +01:00
|
|
|
return core_hash(params, out, m_with_prefix, mlen + params->padding_len + 3*params->n);
|
2017-10-30 16:24:10 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 11:08:27 +01:00
|
|
|
/**
|
|
|
|
* We assume the left half is in in[0]...in[n-1]
|
|
|
|
*/
|
2017-11-01 14:16:17 +00:00
|
|
|
int thash_h(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2020-04-14 20:18:01 +01:00
|
|
|
unsigned char buf[params->padding_len + 3 * params->n];
|
2017-10-30 22:49:30 +00:00
|
|
|
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-30 22:49:30 +00:00
|
|
|
/* Set the function padding. */
|
2020-04-14 20:18:01 +01:00
|
|
|
ull_to_bytes(buf, params->padding_len, XMSS_HASH_PADDING_H);
|
2017-10-30 22:49:30 +00:00
|
|
|
|
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);
|
2020-04-14 20:18:01 +01:00
|
|
|
prf(params, buf + params->padding_len, addr_as_bytes, pub_seed);
|
2017-10-23 13:10:39 +01:00
|
|
|
|
|
|
|
/* 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);
|
2017-10-30 22:49:30 +00:00
|
|
|
prf(params, bitmask, addr_as_bytes, pub_seed);
|
2017-10-23 13:10:39 +01:00
|
|
|
|
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);
|
2017-10-30 22:49:30 +00:00
|
|
|
prf(params, bitmask + params->n, addr_as_bytes, pub_seed);
|
2017-10-23 13:10:39 +01:00
|
|
|
|
2017-10-30 22:49:30 +00:00
|
|
|
for (i = 0; i < 2 * params->n; i++) {
|
2020-04-14 20:18:01 +01:00
|
|
|
buf[params->padding_len + params->n + i] = in[i] ^ bitmask[i];
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2020-04-14 20:18:01 +01:00
|
|
|
return core_hash(params, out, buf, params->padding_len + 3 * params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2017-11-01 14:16:17 +00:00
|
|
|
int thash_f(const xmss_params *params,
|
|
|
|
unsigned char *out, const unsigned char *in,
|
|
|
|
const unsigned char *pub_seed, uint32_t addr[8])
|
2016-02-02 13:06:43 +00:00
|
|
|
{
|
2020-04-14 20:18:01 +01:00
|
|
|
unsigned char buf[params->padding_len + 2 * params->n];
|
2017-10-16 10:58:45 +01:00
|
|
|
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;
|
|
|
|
|
2017-10-30 22:49:30 +00:00
|
|
|
/* Set the function padding. */
|
2020-04-14 20:18:01 +01:00
|
|
|
ull_to_bytes(buf, params->padding_len, XMSS_HASH_PADDING_F);
|
2017-10-30 22:49:30 +00: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);
|
2020-04-14 20:18:01 +01:00
|
|
|
prf(params, buf + params->padding_len, addr_as_bytes, pub_seed);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-30 22:49:30 +00:00
|
|
|
/* Generate the n-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);
|
2017-10-30 22:49:30 +00:00
|
|
|
prf(params, bitmask, addr_as_bytes, pub_seed);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-16 10:58:45 +01:00
|
|
|
for (i = 0; i < params->n; i++) {
|
2020-04-14 20:18:01 +01:00
|
|
|
buf[params->padding_len + params->n + i] = in[i] ^ bitmask[i];
|
2017-08-03 16:38:34 +01:00
|
|
|
}
|
2020-04-14 20:18:01 +01:00
|
|
|
return core_hash(params, out, buf, params->padding_len + 2 * params->n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|