hash.c 1.12 KB
Newer Older
Wentao Zhang committed
1 2 3 4 5 6 7 8 9 10
#include"auxFormat.h"

#define hash_RATE 6
//#define hash_RATE (48 / 8)

#define PRH_ROUNDS 34
//#define PRH_ROUNDS 104   104/3=34+2

int crypto_hash(unsigned char *out, const unsigned char *in,
		unsigned long long inlen) {
Wentao Zhang committed
11
	u32 dataFormat[3] = { 0 }, t2;
Wentao Zhang committed
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
	// initialization
	u32 s[12] = { 0 };
	u8 tempData[12];
	//absorb
	while (inlen >= hash_RATE) {
		packU48FormatToThreePacket(dataFormat, in);
		s[0] ^= dataFormat[0];
		s[1] ^= dataFormat[1];
		s[2] ^= dataFormat[2];
		P384_2(s, constant7Format, PRH_ROUNDS);
		inlen -= hash_RATE;
		in += hash_RATE;
	}
	memset(tempData, 0, hash_RATE);
	memcpy(tempData, in, inlen * sizeof(unsigned char));
	tempData[inlen] = 0x01;
	packU48FormatToThreePacket(dataFormat, tempData);
	s[0] ^= dataFormat[0];
	s[1] ^= dataFormat[1];
	s[2] ^= dataFormat[2];
	P384_2(s, constant7Format, PRH_ROUNDS);
	//sequeez

	unpackU96FormatToThreePacket(out, s);
	unpackU96FormatToThreePacket(out + 12, s + 3);

	P384_2(s, constant7Format, PRH_ROUNDS);
	out += CRYPTO_BYTES / 2;
	unpackU96FormatToThreePacket(out, s);
	unpackU96FormatToThreePacket(out + 12, s + 3);
	return 0;
}