hash.c 2.08 KB
Newer Older
Zhao Xuefeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include"auxFormat.h"

//#define PRH_ROUNDS 140  /4=35

#define PRH_ROUNDS 34
#define hash_RATE (64 / 8)
unsigned char constant8Format_hash[140] = {
/*constant8_hash_512*/
0x1, 0x4, 0x10, 0x40, 0x3, 0xd, 0x35, 0xd4, 0x52, 0x4a, 0x2b, 0xac, 0xb0, 0xc1,
		0x6, 0x19, 0x65, 0x97, 0x5c, 0x72, 0xca, 0x2a, 0xa8, 0xa0, 0x81, 0x5,
		0x14, 0x50, 0x43, 0xe, 0x38, 0xe1, 0x86, 0x18, 0x61, 0x87, 0x1c, 0x71,
		0xc7, 0x1f, 0x7c, 0xf2, 0xcb, 0x2e, 0xb8, 0xe0, 0x82, 0x8, 0x21, 0x84,
		0x11, 0x44, 0x13, 0x4d, 0x36, 0xd9, 0x67, 0x9e, 0x79, 0xe6, 0x9b, 0x6d,
		0xb6, 0xd8, 0x63, 0x8e, 0x39, 0xe5, 0x96, 0x58, 0x62, 0x8a, 0x29, 0xa5,
		0x95, 0x55, 0x57, 0x5e, 0x7b, 0xef, 0xbe, 0xf9, 0xe7, 0x9f, 0x7d, 0xf6,
		0xdb, 0x6e, 0xbb, 0xed, 0xb7, 0xdc, 0x73, 0xce, 0x3a, 0xe8, 0xa3, 0x8c,
		0x30, 0xc0, 0x2, 0x9, 0x25, 0x94, 0x51, 0x47, 0x1e, 0x78, 0xe2, 0x8b,
		0x2d, 0xb5, 0xd5, 0x56, 0x5a, 0x6b, 0xaf, 0xbd, 0xf4, 0xd2, 0x4b, 0x2f,
		0xbc, 0xf0, 0xc2, 0xb, 0x2c, 0xb1, 0xc5, 0x16, 0x59, 0x66, 0x9a, 0x69,
		0xa6, 0x98, 0x60, 0x83, 0xc, 0x31, };

int crypto_hash(unsigned char *out, const unsigned char *in,
		unsigned long long inlen) {

	u32 dataFormat[4] = { 0 };
	// initialization
	u32 s[16] = { 0 };
	u8 tempData[32];
	//absorb

	while (inlen >= hash_RATE) {
		packU64FormatToFourPacket(dataFormat, in);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		s[2] ^= dataFormat[2];
		s[3] ^= dataFormat[3];
		P512(s, constant8Format_hash, PRH_ROUNDS);
		inlen -= hash_RATE;
		in += hash_RATE;
	}
	memset(tempData, 0, sizeof(tempData));

	memcpy(tempData, in, inlen * sizeof(unsigned char));
	tempData[inlen] = 0x01;
	packU64FormatToFourPacket(dataFormat, tempData);
	s[0] ^= dataFormat[0];
	s[1] ^= dataFormat[1];
	s[2] ^= dataFormat[2];
	s[3] ^= dataFormat[3];

	P512(s, constant8Format_hash, PRH_ROUNDS);
	//sequeez

	unpackU128FormatToFourPacket(out, s);
	unpackU128FormatToFourPacket((out + 16), (s + 4));

	P512(s, constant8Format_hash, PRH_ROUNDS);
	out += CRYPTO_BYTES / 2;
	unpackU128FormatToFourPacket(out, s);
	unpackU128FormatToFourPacket((out + 16), (s + 4));
	return 0;
}