2015-08-11 11:23:30 +01:00
|
|
|
/*
|
2016-09-22 15:31:41 +01:00
|
|
|
hash.c version 20160722
|
2015-08-11 11:23:30 +01:00
|
|
|
Andreas Hülsing
|
2016-02-16 15:31:18 +00:00
|
|
|
Joost Rijneveld
|
2015-08-11 11:23:30 +01:00
|
|
|
Public domain.
|
|
|
|
*/
|
|
|
|
|
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>
|
2016-02-02 13:06:43 +00:00
|
|
|
#include <string.h>
|
2015-08-11 11:08:27 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
|
2017-06-02 16:29:14 +01:00
|
|
|
unsigned char* addr_to_byte(unsigned char *bytes, const uint32_t addr[8])
|
|
|
|
{
|
|
|
|
#if IS_LITTLE_ENDIAN==1
|
2017-08-03 16:38:34 +01:00
|
|
|
int i;
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
to_byte(bytes + i*4, addr[i], 4);
|
|
|
|
}
|
2016-07-12 15:19:42 +01:00
|
|
|
#else
|
2017-08-03 16:38:34 +01:00
|
|
|
memcpy(bytes, addr, 32);
|
2017-06-02 16:29:14 +01:00
|
|
|
#endif
|
2017-08-03 16:38:34 +01:00
|
|
|
return bytes;
|
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];
|
|
|
|
|
|
|
|
/* Input is of the form (toByte(X, 32) || KEY || M). */
|
|
|
|
|
|
|
|
to_byte(buf, type, n);
|
|
|
|
|
|
|
|
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-08-03 16:38:34 +01:00
|
|
|
unsigned char byte_addr[32];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
set_key_and_mask(addr, 0);
|
|
|
|
addr_to_byte(byte_addr, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, key, byte_addr, pub_seed, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
// Use MSB order
|
|
|
|
set_key_and_mask(addr, 1);
|
|
|
|
addr_to_byte(byte_addr, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, bitmask, byte_addr, pub_seed, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
set_key_and_mask(addr, 2);
|
|
|
|
addr_to_byte(byte_addr, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, bitmask+params->n, byte_addr, pub_seed, params->n);
|
|
|
|
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-08-03 16:38:34 +01:00
|
|
|
unsigned char byte_addr[32];
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
set_key_and_mask(addr, 0);
|
|
|
|
addr_to_byte(byte_addr, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, key, byte_addr, pub_seed, params->n);
|
2017-08-03 16:38:34 +01:00
|
|
|
|
|
|
|
set_key_and_mask(addr, 1);
|
|
|
|
addr_to_byte(byte_addr, addr);
|
2017-10-16 10:58:45 +01:00
|
|
|
prf(params, bitmask, byte_addr, 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
|
|
|
}
|