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-06-02 16:29:14 +01:00
|
|
|
#include <stdint.h>
|
2015-08-11 11:08:27 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
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 long long i = 0;
|
|
|
|
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:
|
|
|
|
* toByte(X, 32) || KEY || M
|
|
|
|
*/
|
2017-08-03 16:38:34 +01:00
|
|
|
|
2017-10-23 15:19:16 +01:00
|
|
|
ull_to_bytes(buf, n, type);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
for (i=0; i < keylen; i++) {
|
|
|
|
buf[i+n] = key[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i=0; i < inlen; i++) {
|
|
|
|
buf[keylen + n + i] = in[i];
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|