1 /* $OpenBSD: xmss_hash.c,v 1.3 2022/04/20 16:00:25 millert Exp $ */
3 hash.c version 20160722
12 #include "xmss_hash_address.h"
13 #include "xmss_commons.h"
14 #include "xmss_hash.h"
23 int core_hash_SHA2(unsigned char *, const unsigned int, const unsigned char *,
24 unsigned int, const unsigned char *, unsigned long long, unsigned int);
26 unsigned char* addr_to_byte(unsigned char *bytes, const uint32_t addr[8]){
27 #if IS_LITTLE_ENDIAN==1
30 to_byte(bytes+i*4, addr[i],4);
33 memcpy(bytes, addr, 32);
38 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){
39 unsigned long long i = 0;
40 unsigned char buf[inlen + n + keylen];
42 // Input is (toByte(X, 32) || KEY || M)
45 to_byte(buf, type, n);
47 for (i=0; i < keylen; i++) {
51 for (i=0; i < inlen; i++) {
52 buf[keylen + n + i] = in[i];
56 SHA256(buf, inlen + keylen + n, out);
61 SHA512(buf, inlen + keylen + n, out);
71 int prf(unsigned char *out, const unsigned char *in, const unsigned char *key, unsigned int keylen)
73 return core_hash_SHA2(out, 3, key, keylen, in, 32, keylen);
79 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)
82 // H_msg takes 3n-bit keys, but n does not match the keylength of keylen
85 return core_hash_SHA2(out, 2, key, keylen, in, inlen, n);
89 * We assume the left half is in in[0]...in[n-1]
91 int hash_h(unsigned char *out, const unsigned char *in, const unsigned char *pub_seed, uint32_t addr[8], const unsigned int n)
94 unsigned char buf[2*n];
96 unsigned char bitmask[2*n];
97 unsigned char byte_addr[32];
100 setKeyAndMask(addr, 0);
101 addr_to_byte(byte_addr, addr);
102 prf(key, byte_addr, pub_seed, n);
104 setKeyAndMask(addr, 1);
105 addr_to_byte(byte_addr, addr);
106 prf(bitmask, byte_addr, pub_seed, n);
107 setKeyAndMask(addr, 2);
108 addr_to_byte(byte_addr, addr);
109 prf(bitmask+n, byte_addr, pub_seed, n);
110 for (i = 0; i < 2*n; i++) {
111 buf[i] = in[i] ^ bitmask[i];
113 return core_hash_SHA2(out, 1, key, n, buf, 2*n, n);
116 int hash_f(unsigned char *out, const unsigned char *in, const unsigned char *pub_seed, uint32_t addr[8], const unsigned int n)
118 unsigned char buf[n];
119 unsigned char key[n];
120 unsigned char bitmask[n];
121 unsigned char byte_addr[32];
124 setKeyAndMask(addr, 0);
125 addr_to_byte(byte_addr, addr);
126 prf(key, byte_addr, pub_seed, n);
128 setKeyAndMask(addr, 1);
129 addr_to_byte(byte_addr, addr);
130 prf(bitmask, byte_addr, pub_seed, n);
132 for (i = 0; i < n; i++) {
133 buf[i] = in[i] ^ bitmask[i];
135 return core_hash_SHA2(out, 0, key, n, buf, n, n);
137 #endif /* WITH_XMSS */