hash.c 1.71 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
#include"auxFormat.h"


#define hash_RATE (32 / 8)

#define PRH_ROUNDS 33
//#define PRH_ROUNDS 68
unsigned char  constant7Format[68] = {
/*constant7_hash_256v1:*/
		0x1,
			0x10,
			0x2,
			0x20,
			0x4,
			0x40,
			0x9,
			0x11,
			0x12,
			0x22,
			0x24,
			0x44,
			0x49,
			0x18,
			0x3,
			0x30,
			0x6,
			0x60,
			0xd,
			0x51,
			0x1b,
			0x33,
			0x36,
			0x66,
			0x6d,
			0x5c,
			0x4a,
			0x28,
			0x5,
			0x50,
			0xb,
			0x31,
			0x16,
			0x62,
			0x2d,
			0x55,
			0x5b,
			0x3a,
			0x27,
			0x74,
			0x4f,
			0x78,
			0xe,
			0x61,
			0x1d,
			0x53,
			0x3b,
			0x37,
			0x76,
			0x6f,
			0x7c,
			0x4e,
			0x68,
			0xc,
			0x41,
			0x19,
			0x13,
			0x32,
			0x26,
			0x64,
			0x4d,
			0x58,
			0xa,
			0x21,
			0x14,
			0x42,
			0x29,
			0x15,
};
int crypto_hash(unsigned char *out, const unsigned char *in,
	unsigned long long inlen) {
	u32 dataFormat[2] = { 0 };
	// initialization
	u32 s[8] = { 0 };
	u8 tempData[32];
	//absorb
	//RATE=4
	while (inlen >= hash_RATE) {
		getU32Format(dataFormat, in);
		s[0] ^= dataFormat[0] >>16;
		s[1] ^= dataFormat[0] &0xffff;
		P256(s, constant7Format, PRH_ROUNDS);
		inlen -= hash_RATE;
		in += hash_RATE;
	}
	memset(tempData, 0, sizeof(tempData));
	memcpy(tempData, in, inlen * sizeof(unsigned char));
	tempData[inlen] = 0x01;
	getU32Format(dataFormat, tempData);
	s[0] ^= dataFormat[0] >> 16;
	s[1] ^= dataFormat[0] & 0xffff;

	P256(s, constant7Format, PRH_ROUNDS);
	//sequeez
	unpackFormat(out, s);
	unpackFormat((out + 8), (s + 2));

	P256(s, constant7Format, PRH_ROUNDS);
	out += CRYPTO_BYTES / 2;
	unpackFormat(out, s);
	unpackFormat((out + 8), (s + 2));
	return 0;
}