2015-08-11 11:23:30 +01:00
|
|
|
/*
|
2016-07-11 10:15:16 +01:00
|
|
|
hash.c version 20160708
|
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"
|
2015-08-11 11:08:27 +01:00
|
|
|
|
|
|
|
#include <stddef.h>
|
2016-07-11 10:15:16 +01:00
|
|
|
#include <stdint.h>
|
2016-02-02 13:06:43 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-08-11 11:08:27 +01:00
|
|
|
#include <openssl/sha.h>
|
|
|
|
#include <openssl/hmac.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
|
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
int core_hash_SHA2(unsigned char *out, const unsigned int type, const unsigned char *key, unsigned int keylen, const unsigned char *in, unsigned long long inlen, unsigned int n){
|
|
|
|
unsigned long long i = 0;
|
|
|
|
unsigned char buf[inlen + n + keylen];
|
|
|
|
|
|
|
|
// Input is (toByte(X, 32) || KEY || M)
|
|
|
|
|
|
|
|
// set toByte
|
|
|
|
to_byte(buf, type, n);
|
2016-02-16 15:31:18 +00:00
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i=0; i < keylen; i++) {
|
2016-07-11 10:15:16 +01:00
|
|
|
buf[i+n] = key[i];
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
2016-07-11 10:15:16 +01:00
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i=0; i < inlen; i++) {
|
2016-07-11 10:15:16 +01:00
|
|
|
buf[keylen + n + i] = in[i];
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
if (n == 32) {
|
|
|
|
SHA256(buf, inlen + keylen + n, out);
|
2015-08-11 11:08:27 +01:00
|
|
|
return 0;
|
2016-02-02 13:06:43 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-07-11 10:15:16 +01:00
|
|
|
if (n == 64) {
|
|
|
|
SHA512(buf, inlen + keylen + n, out);
|
2015-08-11 11:08:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
/**
|
|
|
|
* Implements PRF
|
|
|
|
*/
|
|
|
|
int prf(unsigned char *out, const unsigned char *in, const unsigned char *key, unsigned int keylen)
|
|
|
|
{
|
|
|
|
size_t inlen = 32;
|
|
|
|
return core_hash_SHA2(out, 3, key, keylen, in, inlen, keylen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Implemts H_msg
|
|
|
|
*/
|
|
|
|
int h_msg(unsigned char *out, const unsigned char *in, unsigned long long inlen, const unsigned char *key, const unsigned int keylen, const unsigned int n)
|
|
|
|
{
|
|
|
|
if (keylen != 3*n){
|
|
|
|
fprintf(stderr, "H_msg takes 3n-bit keys, we got n=%d but a keylength of %d.\n", n, keylen);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return core_hash_SHA2(out, 2, key, keylen, in, inlen, n);
|
|
|
|
}
|
|
|
|
|
2015-08-11 11:08:27 +01:00
|
|
|
/**
|
|
|
|
* We assume the left half is in in[0]...in[n-1]
|
|
|
|
*/
|
2016-07-11 10:15:16 +01:00
|
|
|
int hash_h(unsigned char *out, const unsigned char *in, const unsigned char *pub_seed, uint32_t addr[8], const unsigned int n)
|
2015-08-11 11:08:27 +01:00
|
|
|
{
|
2016-02-02 13:06:43 +00:00
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
unsigned char buf[2*n];
|
2015-08-11 11:08:27 +01:00
|
|
|
unsigned char key[n];
|
|
|
|
unsigned char bitmask[2*n];
|
2016-02-02 13:06:43 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
setKeyAndMask(addr, 0);
|
|
|
|
prf(key, (unsigned char *)addr, pub_seed, n);
|
2015-08-11 11:08:27 +01:00
|
|
|
// Use MSB order
|
2016-07-11 10:15:16 +01:00
|
|
|
setKeyAndMask(addr, 1);
|
|
|
|
prf(bitmask, (unsigned char *)addr, pub_seed, n);
|
|
|
|
setKeyAndMask(addr, 2);
|
|
|
|
prf(bitmask+n, (unsigned char *)addr, pub_seed, n);
|
|
|
|
for (i = 0; i < 2*n; i++) {
|
|
|
|
buf[i] = in[i] ^ bitmask[i];
|
2015-08-12 16:59:29 +01:00
|
|
|
}
|
2016-07-11 10:15:16 +01:00
|
|
|
return core_hash_SHA2(out, 1, key, n, buf, 2*n, n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
int hash_f(unsigned char *out, const unsigned char *in, const unsigned char *pub_seed, uint32_t addr[8], const unsigned int n)
|
2016-02-02 13:06:43 +00:00
|
|
|
{
|
2016-07-11 10:15:16 +01:00
|
|
|
unsigned char buf[n];
|
2015-08-11 11:08:27 +01:00
|
|
|
unsigned char key[n];
|
|
|
|
unsigned char bitmask[n];
|
2016-02-02 13:06:43 +00:00
|
|
|
unsigned int i;
|
|
|
|
|
2016-07-11 10:15:16 +01:00
|
|
|
setKeyAndMask(addr, 0);
|
|
|
|
prf(key, (unsigned char *)addr, pub_seed, n);
|
|
|
|
// Use MSB order
|
|
|
|
setKeyAndMask(addr, 1);
|
|
|
|
prf(bitmask, (unsigned char *)addr, pub_seed, n);
|
|
|
|
|
2016-02-02 13:06:43 +00:00
|
|
|
for (i = 0; i < n; i++) {
|
2016-07-11 10:15:16 +01:00
|
|
|
buf[i] = in[i] ^ bitmask[i];
|
2015-08-12 16:59:29 +01:00
|
|
|
}
|
2016-07-11 10:15:16 +01:00
|
|
|
return core_hash_SHA2(out, 0, key, n, buf, n, n);
|
2015-08-11 11:08:27 +01:00
|
|
|
}
|