Hash.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. #define ZERO_BUFFERS 0 // if zero helper buffers after hash calculation, to clear sensitive information
  4. #include "../../../ThirdPartyLibs/begin.h"
  5. #define XXH_STATIC_LINKING_ONLY
  6. #include "../../../ThirdPartyLibs/LZ4/xxhash.h"
  7. #define SpookyHash _SpookyHash
  8. #include "../../../ThirdPartyLibs/Spooky Hash/SpookyV2.cpp"
  9. #undef SpookyHash
  10. #define MetroHash64 _MetroHash64
  11. #define MetroHash128 _MetroHash128
  12. #include "../../../ThirdPartyLibs/Metro Hash/metrohash64.cpp"
  13. #include "../../../ThirdPartyLibs/Metro Hash/metrohash128.cpp"
  14. #undef MetroHash64
  15. #undef MetroHash128
  16. #include "../../../ThirdPartyLibs/end.h"
  17. /******************************************************************************/
  18. namespace EE{
  19. static const Byte HashPadding[64]=
  20. {
  21. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. };
  26. /******************************************************************************/
  27. // CRC32
  28. /******************************************************************************/
  29. static UInt crc32[256];
  30. static UInt CRC32Reflect(UInt reflect, Int bits)
  31. {
  32. UInt value=0;
  33. for(Int i=1; i<=bits; i++)
  34. {
  35. if(reflect&1)value|=(1<<(bits-i));
  36. reflect>>=1;
  37. }
  38. return value;
  39. }
  40. static void CRC32Init()
  41. {
  42. const UInt polynominal=0x04C11DB7; // this is compatible with Zip, Png - https://en.wikipedia.org/wiki/Cyclic_redundancy_check#Standards_and_common_use
  43. FREP(256)
  44. {
  45. UInt c=(CRC32Reflect(i, 8)<<24);
  46. FREPD(j, 8)c=((c<<1)^((c&(1<<31)) ? polynominal : 0));
  47. crc32[i]=CRC32Reflect(c, 32);
  48. }
  49. }
  50. void CRC32::reset ( ) { _crc=0xFFFFFFFF;}
  51. UInt CRC32::operator()( )C {return _crc^0xFFFFFFFF;}
  52. void CRC32::update (CPtr data, Int size)
  53. {
  54. #if 0 // this version is correct, but it is slower
  55. UInt *u=(UInt*)data;
  56. REP(size/4)
  57. {
  58. _crc^=*u++;
  59. _crc=((_crc>>8)^crc32[_crc&0xFF]);
  60. _crc=((_crc>>8)^crc32[_crc&0xFF]);
  61. _crc=((_crc>>8)^crc32[_crc&0xFF]);
  62. _crc=((_crc>>8)^crc32[_crc&0xFF]);
  63. }
  64. Byte *b=(Byte*)u;
  65. #else
  66. Byte *b=(Byte*)data;
  67. REP(size/4)
  68. {
  69. _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]);
  70. _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]);
  71. _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]);
  72. _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]);
  73. }
  74. #endif
  75. switch(size%4)
  76. {
  77. case 3: _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]); // !! no break on purpose !!
  78. case 2: _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]); // !! no break on purpose !!
  79. case 1: _crc=((_crc>>8)^crc32[(_crc&0xFF) ^ (*b++)]); // !! no break on purpose !!
  80. }
  81. }
  82. /******************************************************************************/
  83. // xxHash
  84. /******************************************************************************/
  85. void xxHash32::reset()
  86. {
  87. _finalized=false;
  88. ASSERT(SIZE(_buffer)==SIZE(XXH32_state_t));
  89. XXH32_reset((XXH32_state_t*)_buffer, 0);
  90. }
  91. void xxHash64::reset()
  92. {
  93. _finalized=false;
  94. #if IOS || ANDROID
  95. ASSERT(SIZE(_buffer)>=SIZE(XXH64_state_t)); // needed for iOS and Android
  96. #else
  97. ASSERT(SIZE(_buffer)==SIZE(XXH64_state_t));
  98. #endif
  99. XXH64_reset((XXH64_state_t*)_buffer, 0);
  100. }
  101. void xxHash32::update(CPtr data, Int size) {XXH32_update((XXH32_state_t*)_buffer, data, size);}
  102. void xxHash64::update(CPtr data, Int size) {XXH64_update((XXH64_state_t*)_buffer, data, size);}
  103. INLINE void xxHash32::finalize() {if(!_finalized){_finalized=true; _hash=XXH32_digest((XXH32_state_t*)_buffer); if(ZERO_BUFFERS)Zero(_buffer);}}
  104. INLINE void xxHash64::finalize() {if(!_finalized){_finalized=true; _hash=XXH64_digest((XXH64_state_t*)_buffer); if(ZERO_BUFFERS)Zero(_buffer);}}
  105. UInt xxHash32::operator()() {finalize(); return _hash;}
  106. UInt xxHash64::hash32 () {finalize(); return _hash;}
  107. ULong xxHash64::hash64 () {finalize(); return _hash;}
  108. /******************************************************************************/
  109. // SPOOKY HASH
  110. /******************************************************************************/
  111. void SpookyHash::reset()
  112. {
  113. _finalized=false;
  114. ASSERT(SIZE(_data)==SIZE(_SpookyHash));
  115. ((_SpookyHash&)_data).Init(0, 0);
  116. }
  117. void SpookyHash::update(CPtr data, Int size) {((_SpookyHash&)_data).Update(data, size);}
  118. INLINE void SpookyHash::finalize()
  119. {
  120. if(!_finalized)
  121. {
  122. _finalized=true;
  123. ((_SpookyHash&)_data).Final(&_hash.l[0], &_hash.l[1]);
  124. if(ZERO_BUFFERS)Zero(_data);
  125. }
  126. }
  127. UInt SpookyHash::hash32 () {finalize(); return _hash.i[0];}
  128. ULong SpookyHash::hash64 () {finalize(); return _hash.l[0];}
  129. C UID& SpookyHash::hash128() {finalize(); return _hash ;}
  130. /******************************************************************************/
  131. // METRO HASH
  132. /******************************************************************************/
  133. void MetroHash64 ::reset() {_finalized=false; ASSERT(SIZE(_data)==SIZE(_MetroHash64 )); ((_MetroHash64 &)_data).Initialize();}
  134. void MetroHash128::reset() {_finalized=false; ASSERT(SIZE(_data)==SIZE(_MetroHash128)); ((_MetroHash128&)_data).Initialize();}
  135. void MetroHash64 ::update(CPtr data, Int size) {((_MetroHash64 &)_data).Update((Byte*)data, size);}
  136. void MetroHash128::update(CPtr data, Int size) {((_MetroHash128&)_data).Update((Byte*)data, size);}
  137. ULong MetroHash64 ::operator()() {if(!_finalized){_finalized=true; ((_MetroHash64 &)_data).Finalize((Byte*)&_hash ); if(ZERO_BUFFERS)Zero(_data);} return _hash;}
  138. C UID& MetroHash128::operator()() {if(!_finalized){_finalized=true; ((_MetroHash128&)_data).Finalize( _hash.b); if(ZERO_BUFFERS)Zero(_data);} return _hash;}
  139. /******************************************************************************/
  140. // MD5
  141. /******************************************************************************/
  142. void MD5::reset()
  143. {
  144. _finalized=false;
  145. _count[0]=0;
  146. _count[1]=0;
  147. _hash.i[0]=0x67452301;
  148. _hash.i[1]=0xefcdab89;
  149. _hash.i[2]=0x98badcfe;
  150. _hash.i[3]=0x10325476;
  151. }
  152. /******************************************************************************/
  153. static void MD5Decode(UInt output[], const Byte input[], Int len) // decodes input (Byte) into output (UInt). Assumes 'len' is a multiple of 4
  154. {
  155. for(Int i=0, j=0; j<len; i++, j+=4)
  156. output[i]=((UInt)input[j]) | (((UInt)input[j+1])<<8) | (((UInt)input[j+2])<<16) | (((UInt)input[j+3])<<24);
  157. }
  158. static void MD5Encode(Byte output[], const UInt input[], Int len) // encodes input (UInt) into output (Byte). Assumes 'len' is a multiple of 4
  159. {
  160. for(Int i=0, j=0; j<len; i++, j+=4)
  161. {
  162. output[j ]= input[i] &0xFF;
  163. output[j+1]=(input[i]>> 8)&0xFF;
  164. output[j+2]=(input[i]>>16)&0xFF;
  165. output[j+3]=(input[i]>>24)&0xFF;
  166. }
  167. }
  168. #define S11 7
  169. #define S12 12
  170. #define S13 17
  171. #define S14 22
  172. #define S21 5
  173. #define S22 9
  174. #define S23 14
  175. #define S24 20
  176. #define S31 4
  177. #define S32 11
  178. #define S33 16
  179. #define S34 23
  180. #define S41 6
  181. #define S42 10
  182. #define S43 15
  183. #define S44 21
  184. static INLINE UInt F(UInt x, UInt y, UInt z) {return x&y | ~x&z;}
  185. static INLINE UInt G(UInt x, UInt y, UInt z) {return x&z | y&~z;}
  186. static INLINE UInt H(UInt x, UInt y, UInt z) {return x^y^z;}
  187. static INLINE UInt I(UInt x, UInt y, UInt z) {return y ^ (x | ~z);}
  188. static INLINE UInt ROL(UInt x, int n) {return (x<<n) | (x>>(32-n));}
  189. static INLINE void FF(UInt &a, UInt b, UInt c, UInt d, UInt x, UInt s, UInt ac) {a=ROL(a + F(b,c,d) + x + ac, s) + b;}
  190. static INLINE void GG(UInt &a, UInt b, UInt c, UInt d, UInt x, UInt s, UInt ac) {a=ROL(a + G(b,c,d) + x + ac, s) + b;}
  191. static INLINE void HH(UInt &a, UInt b, UInt c, UInt d, UInt x, UInt s, UInt ac) {a=ROL(a + H(b,c,d) + x + ac, s) + b;}
  192. static INLINE void II(UInt &a, UInt b, UInt c, UInt d, UInt x, UInt s, UInt ac) {a=ROL(a + I(b,c,d) + x + ac, s) + b;}
  193. void MD5::transform(const Byte block[64])
  194. {
  195. UInt a=_hash.i[0], b=_hash.i[1], c=_hash.i[2], d=_hash.i[3];
  196. #if 1
  197. UInt *x=(UInt*)block;
  198. #else // big-endian
  199. UInt x[16]; MD5Decode(x, block, 64);
  200. #endif
  201. // Round 1
  202. FF(a, b, c, d, x[ 0], S11, 0xd76aa478); // 1
  203. FF(d, a, b, c, x[ 1], S12, 0xe8c7b756); // 2
  204. FF(c, d, a, b, x[ 2], S13, 0x242070db); // 3
  205. FF(b, c, d, a, x[ 3], S14, 0xc1bdceee); // 4
  206. FF(a, b, c, d, x[ 4], S11, 0xf57c0faf); // 5
  207. FF(d, a, b, c, x[ 5], S12, 0x4787c62a); // 6
  208. FF(c, d, a, b, x[ 6], S13, 0xa8304613); // 7
  209. FF(b, c, d, a, x[ 7], S14, 0xfd469501); // 8
  210. FF(a, b, c, d, x[ 8], S11, 0x698098d8); // 9
  211. FF(d, a, b, c, x[ 9], S12, 0x8b44f7af); // 10
  212. FF(c, d, a, b, x[10], S13, 0xffff5bb1); // 11
  213. FF(b, c, d, a, x[11], S14, 0x895cd7be); // 12
  214. FF(a, b, c, d, x[12], S11, 0x6b901122); // 13
  215. FF(d, a, b, c, x[13], S12, 0xfd987193); // 14
  216. FF(c, d, a, b, x[14], S13, 0xa679438e); // 15
  217. FF(b, c, d, a, x[15], S14, 0x49b40821); // 16
  218. // Round 2
  219. GG(a, b, c, d, x[ 1], S21, 0xf61e2562); // 17
  220. GG(d, a, b, c, x[ 6], S22, 0xc040b340); // 18
  221. GG(c, d, a, b, x[11], S23, 0x265e5a51); // 19
  222. GG(b, c, d, a, x[ 0], S24, 0xe9b6c7aa); // 20
  223. GG(a, b, c, d, x[ 5], S21, 0xd62f105d); // 21
  224. GG(d, a, b, c, x[10], S22, 0x2441453); // 22
  225. GG(c, d, a, b, x[15], S23, 0xd8a1e681); // 23
  226. GG(b, c, d, a, x[ 4], S24, 0xe7d3fbc8); // 24
  227. GG(a, b, c, d, x[ 9], S21, 0x21e1cde6); // 25
  228. GG(d, a, b, c, x[14], S22, 0xc33707d6); // 26
  229. GG(c, d, a, b, x[ 3], S23, 0xf4d50d87); // 27
  230. GG(b, c, d, a, x[ 8], S24, 0x455a14ed); // 28
  231. GG(a, b, c, d, x[13], S21, 0xa9e3e905); // 29
  232. GG(d, a, b, c, x[ 2], S22, 0xfcefa3f8); // 30
  233. GG(c, d, a, b, x[ 7], S23, 0x676f02d9); // 31
  234. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); // 32
  235. // Round 3
  236. HH(a, b, c, d, x[ 5], S31, 0xfffa3942); // 33
  237. HH(d, a, b, c, x[ 8], S32, 0x8771f681); // 34
  238. HH(c, d, a, b, x[11], S33, 0x6d9d6122); // 35
  239. HH(b, c, d, a, x[14], S34, 0xfde5380c); // 36
  240. HH(a, b, c, d, x[ 1], S31, 0xa4beea44); // 37
  241. HH(d, a, b, c, x[ 4], S32, 0x4bdecfa9); // 38
  242. HH(c, d, a, b, x[ 7], S33, 0xf6bb4b60); // 39
  243. HH(b, c, d, a, x[10], S34, 0xbebfbc70); // 40
  244. HH(a, b, c, d, x[13], S31, 0x289b7ec6); // 41
  245. HH(d, a, b, c, x[ 0], S32, 0xeaa127fa); // 42
  246. HH(c, d, a, b, x[ 3], S33, 0xd4ef3085); // 43
  247. HH(b, c, d, a, x[ 6], S34, 0x4881d05); // 44
  248. HH(a, b, c, d, x[ 9], S31, 0xd9d4d039); // 45
  249. HH(d, a, b, c, x[12], S32, 0xe6db99e5); // 46
  250. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); // 47
  251. HH(b, c, d, a, x[ 2], S34, 0xc4ac5665); // 48
  252. // Round 4
  253. II(a, b, c, d, x[ 0], S41, 0xf4292244); // 49
  254. II(d, a, b, c, x[ 7], S42, 0x432aff97); // 50
  255. II(c, d, a, b, x[14], S43, 0xab9423a7); // 51
  256. II(b, c, d, a, x[ 5], S44, 0xfc93a039); // 52
  257. II(a, b, c, d, x[12], S41, 0x655b59c3); // 53
  258. II(d, a, b, c, x[ 3], S42, 0x8f0ccc92); // 54
  259. II(c, d, a, b, x[10], S43, 0xffeff47d); // 55
  260. II(b, c, d, a, x[ 1], S44, 0x85845dd1); // 56
  261. II(a, b, c, d, x[ 8], S41, 0x6fa87e4f); // 57
  262. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); // 58
  263. II(c, d, a, b, x[ 6], S43, 0xa3014314); // 59
  264. II(b, c, d, a, x[13], S44, 0x4e0811a1); // 60
  265. II(a, b, c, d, x[ 4], S41, 0xf7537e82); // 61
  266. II(d, a, b, c, x[11], S42, 0xbd3af235); // 62
  267. II(c, d, a, b, x[ 2], S43, 0x2ad7d2bb); // 63
  268. II(b, c, d, a, x[ 9], S44, 0xeb86d391); // 64
  269. _hash.i[0]+=a;
  270. _hash.i[1]+=b;
  271. _hash.i[2]+=c;
  272. _hash.i[3]+=d;
  273. }
  274. void MD5::update(CPtr data, Int size)
  275. {
  276. if(size>0)
  277. {
  278. // compute number of bytes mod 64
  279. Int index=_count[0]/8 % 64;
  280. // Update number of bits
  281. if((_count[0]+=(size<<3))<(size<<3))_count[1]++;
  282. _count[1]+=(size>>29);
  283. // number of bytes we need to fill in buffer
  284. Int firstpart=64-index;
  285. Int i;
  286. if(size>=firstpart) // transform as many times as possible.
  287. {
  288. // fill buffer first, transform
  289. CopyFast(&_buffer[index], data, firstpart);
  290. transform(_buffer);
  291. // transform chunks of 64
  292. for(i=firstpart; i+64<=size; i+=64)transform((Byte*)data+i);
  293. index=0;
  294. }else i=0;
  295. // buffer remaining input
  296. CopyFast(&_buffer[index], (Byte*)data+i, size-i);
  297. }
  298. }
  299. INLINE void MD5::finalize()
  300. {
  301. if(!_finalized)
  302. {
  303. // Save number of bits
  304. Byte bits[8]; MD5Encode(bits, _count, 8);
  305. // pad out to 56 mod 64
  306. Int index=_count[0]/8 % 64, padLen=(index<56) ? 56-index : 120-index;
  307. update(HashPadding, padLen);
  308. // Append length
  309. update(bits, 8);
  310. // clear sensitive information
  311. if(ZERO_BUFFERS)
  312. {
  313. Zero(_buffer);
  314. Zero(_count );
  315. }
  316. _finalized=true;
  317. }
  318. }
  319. C UID& MD5::operator()()
  320. {
  321. finalize(); return _hash;
  322. }
  323. /******************************************************************************/
  324. // SHA1-160
  325. /******************************************************************************/
  326. #define ROL(bits, word) (((word)<<(bits)) | ((word)>>(32-(bits))))
  327. #undef C
  328. void SHA1::reset()
  329. {
  330. _finalized=false;
  331. _hash.i[0]=0x67452301;
  332. _hash.i[1]=0xEFCDAB89;
  333. _hash.i[2]=0x98BADCFE;
  334. _hash.i[3]=0x10325476;
  335. _hash.i[4]=0xC3D2E1F0;
  336. _count[0]=0;
  337. _count[1]=0;
  338. }
  339. void SHA1::transform(const Byte block[64])
  340. {
  341. const UInt K[]=
  342. {
  343. 0x5A827999,
  344. 0x6ED9EBA1,
  345. 0x8F1BBCDC,
  346. 0xCA62C1D6,
  347. };
  348. Int t; // Loop counter
  349. UInt temp, // Temporary word value
  350. W[80], // Word sequence
  351. A, B, C, D, E; // Word buffers
  352. for(t=0; t<16; t++)
  353. {
  354. W[t] =(block[t*4 ]<<24);
  355. W[t]|=(block[t*4+1]<<16);
  356. W[t]|=(block[t*4+2]<< 8);
  357. W[t]|=(block[t*4+3] );
  358. }
  359. for(t=16; t<80; t++)W[t]=ROL(1, W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
  360. A=_hash.i[0];
  361. B=_hash.i[1];
  362. C=_hash.i[2];
  363. D=_hash.i[3];
  364. E=_hash.i[4];
  365. for(t=0; t<20; t++)
  366. {
  367. temp=ROL(5, A) + ((B & C) | ((~B) & D)) + E + W[t] + K[0];
  368. E=D;
  369. D=C;
  370. C=ROL(30, B);
  371. B=A;
  372. A=temp;
  373. }
  374. for(t=20; t<40; t++)
  375. {
  376. temp=ROL(5, A) + (B ^ C ^ D) + E + W[t] + K[1];
  377. E=D;
  378. D=C;
  379. C=ROL(30, B);
  380. B=A;
  381. A=temp;
  382. }
  383. for(t=40; t<60; t++)
  384. {
  385. temp=ROL(5, A) + ((B & C) | (B & D) | (C & D)) + E + W[t] + K[2];
  386. E=D;
  387. D=C;
  388. C=ROL(30, B);
  389. B=A;
  390. A=temp;
  391. }
  392. for(t=60; t<80; t++)
  393. {
  394. temp=ROL(5, A) + (B ^ C ^ D) + E + W[t] + K[3];
  395. E=D;
  396. D=C;
  397. C=ROL(30, B);
  398. B=A;
  399. A=temp;
  400. }
  401. _hash.i[0]+=A;
  402. _hash.i[1]+=B;
  403. _hash.i[2]+=C;
  404. _hash.i[3]+=D;
  405. _hash.i[4]+=E;
  406. }
  407. void SHA1::update(CPtr data, Int size)
  408. {
  409. if(size>0)
  410. {
  411. Int left=_count[0]&0x3F,
  412. fill=64-left;
  413. _count[0]+=size; if(_count[0]<size)_count[1]++;
  414. if(left && size>=fill)
  415. {
  416. CopyFast(_buffer+left, data, fill);
  417. transform(_buffer);
  418. size-= fill;
  419. data =(Byte*)data + fill;
  420. left = 0;
  421. }
  422. while(size>=64)
  423. {
  424. transform((Byte*)data);
  425. size-=64;
  426. data =(Byte*)data+64;
  427. }
  428. if(size)CopyFast(_buffer+left, data, size);
  429. }
  430. }
  431. INLINE void SHA1::finalize()
  432. {
  433. if(!_finalized)
  434. {
  435. UInt size[2]=
  436. {
  437. (_count[0]>>29)|(_count[1]<<3),
  438. (_count[0]<< 3),
  439. };
  440. UInt last=(_count[0]&0x3F),
  441. padn=(last<56) ? 56-last : 120-last;
  442. SwapEndian(size[0]);
  443. SwapEndian(size[1]);
  444. update(HashPadding, padn );
  445. update(size , SIZE(size));
  446. REPA(_hash.i)SwapEndian(_hash.i[i]);
  447. // clear sensitive information
  448. if(ZERO_BUFFERS)
  449. {
  450. Zero(_buffer);
  451. Zero(_count );
  452. }
  453. _finalized=true;
  454. }
  455. }
  456. #define C const
  457. C SHA1::Hash& SHA1::operator()()
  458. {
  459. finalize(); return _hash;
  460. }
  461. #undef ROL
  462. /******************************************************************************/
  463. // SHA2-256
  464. /******************************************************************************/
  465. #undef C
  466. void SHA2::reset()
  467. {
  468. _finalized=false;
  469. _hash.i[0]=0x6A09E667;
  470. _hash.i[1]=0xBB67AE85;
  471. _hash.i[2]=0x3C6EF372;
  472. _hash.i[3]=0xA54FF53A;
  473. _hash.i[4]=0x510E527F;
  474. _hash.i[5]=0x9B05688C;
  475. _hash.i[6]=0x1F83D9AB;
  476. _hash.i[7]=0x5BE0CD19;
  477. _count[0]=0;
  478. _count[1]=0;
  479. }
  480. #define GET_UINT32(n, b, i) \
  481. { \
  482. (n)=((UInt)(b)[(i) ]<<24) \
  483. |((UInt)(b)[(i)+1]<<16) \
  484. |((UInt)(b)[(i)+2]<< 8) \
  485. |((UInt)(b)[(i)+3] );\
  486. }
  487. #define PUT_UINT32(n, b, i) \
  488. { \
  489. (b)[(i) ]=(Byte)((n)>>24);\
  490. (b)[(i)+1]=(Byte)((n)>>16);\
  491. (b)[(i)+2]=(Byte)((n)>> 8);\
  492. (b)[(i)+3]=(Byte)((n) );\
  493. }
  494. void SHA2::transform(const Byte data[64])
  495. {
  496. UInt temp1, temp2;
  497. UInt W[64];
  498. #if 1
  499. GET_UINT32(W[0], data, 0);
  500. GET_UINT32(W[1], data, 4);
  501. GET_UINT32(W[2], data, 8);
  502. GET_UINT32(W[3], data, 12);
  503. GET_UINT32(W[4], data, 16);
  504. GET_UINT32(W[5], data, 20);
  505. GET_UINT32(W[6], data, 24);
  506. GET_UINT32(W[7], data, 28);
  507. GET_UINT32(W[8], data, 32);
  508. GET_UINT32(W[9], data, 36);
  509. GET_UINT32(W[10], data, 40);
  510. GET_UINT32(W[11], data, 44);
  511. GET_UINT32(W[12], data, 48);
  512. GET_UINT32(W[13], data, 52);
  513. GET_UINT32(W[14], data, 56);
  514. GET_UINT32(W[15], data, 60);
  515. #else
  516. CopyFast(W, data, 64);
  517. #endif
  518. #define SHR(x,n) ((x&0xFFFFFFFF)>>n)
  519. #define ROTR(x,n) (SHR(x,n) | (x<<(32-n)))
  520. #define S0(x) (ROTR(x, 7) ^ ROTR(x,18) ^ SHR(x, 3))
  521. #define S1(x) (ROTR(x,17) ^ ROTR(x,19) ^ SHR(x,10))
  522. #define S2(x) (ROTR(x, 2) ^ ROTR(x,13) ^ ROTR(x,22))
  523. #define S3(x) (ROTR(x, 6) ^ ROTR(x,11) ^ ROTR(x,25))
  524. #define F0(x,y,z) ((x&y)|(z&(x|y)))
  525. #define F1(x,y,z) (z^(x&(y^z)))
  526. #define R(t) \
  527. ( \
  528. W[t]=S1(W[t- 2])+W[t- 7]+\
  529. S0(W[t-15])+W[t-16] \
  530. )
  531. #define P(a,b,c,d,e,f,g,h,x,K) \
  532. { \
  533. temp1=h+S3(e)+F1(e,f,g)+K+x;\
  534. temp2= S2(a)+F0(a,b,c) ;\
  535. d+=temp1; h=temp1+temp2; \
  536. }
  537. UInt A=_hash.i[0],
  538. B=_hash.i[1],
  539. C=_hash.i[2],
  540. D=_hash.i[3],
  541. E=_hash.i[4],
  542. F=_hash.i[5],
  543. G=_hash.i[6],
  544. H=_hash.i[7];
  545. P(A, B, C, D, E, F, G, H, W[ 0], 0x428A2F98);
  546. P(H, A, B, C, D, E, F, G, W[ 1], 0x71374491);
  547. P(G, H, A, B, C, D, E, F, W[ 2], 0xB5C0FBCF);
  548. P(F, G, H, A, B, C, D, E, W[ 3], 0xE9B5DBA5);
  549. P(E, F, G, H, A, B, C, D, W[ 4], 0x3956C25B);
  550. P(D, E, F, G, H, A, B, C, W[ 5], 0x59F111F1);
  551. P(C, D, E, F, G, H, A, B, W[ 6], 0x923F82A4);
  552. P(B, C, D, E, F, G, H, A, W[ 7], 0xAB1C5ED5);
  553. P(A, B, C, D, E, F, G, H, W[ 8], 0xD807AA98);
  554. P(H, A, B, C, D, E, F, G, W[ 9], 0x12835B01);
  555. P(G, H, A, B, C, D, E, F, W[10], 0x243185BE);
  556. P(F, G, H, A, B, C, D, E, W[11], 0x550C7DC3);
  557. P(E, F, G, H, A, B, C, D, W[12], 0x72BE5D74);
  558. P(D, E, F, G, H, A, B, C, W[13], 0x80DEB1FE);
  559. P(C, D, E, F, G, H, A, B, W[14], 0x9BDC06A7);
  560. P(B, C, D, E, F, G, H, A, W[15], 0xC19BF174);
  561. P(A, B, C, D, E, F, G, H, R(16), 0xE49B69C1);
  562. P(H, A, B, C, D, E, F, G, R(17), 0xEFBE4786);
  563. P(G, H, A, B, C, D, E, F, R(18), 0x0FC19DC6);
  564. P(F, G, H, A, B, C, D, E, R(19), 0x240CA1CC);
  565. P(E, F, G, H, A, B, C, D, R(20), 0x2DE92C6F);
  566. P(D, E, F, G, H, A, B, C, R(21), 0x4A7484AA);
  567. P(C, D, E, F, G, H, A, B, R(22), 0x5CB0A9DC);
  568. P(B, C, D, E, F, G, H, A, R(23), 0x76F988DA);
  569. P(A, B, C, D, E, F, G, H, R(24), 0x983E5152);
  570. P(H, A, B, C, D, E, F, G, R(25), 0xA831C66D);
  571. P(G, H, A, B, C, D, E, F, R(26), 0xB00327C8);
  572. P(F, G, H, A, B, C, D, E, R(27), 0xBF597FC7);
  573. P(E, F, G, H, A, B, C, D, R(28), 0xC6E00BF3);
  574. P(D, E, F, G, H, A, B, C, R(29), 0xD5A79147);
  575. P(C, D, E, F, G, H, A, B, R(30), 0x06CA6351);
  576. P(B, C, D, E, F, G, H, A, R(31), 0x14292967);
  577. P(A, B, C, D, E, F, G, H, R(32), 0x27B70A85);
  578. P(H, A, B, C, D, E, F, G, R(33), 0x2E1B2138);
  579. P(G, H, A, B, C, D, E, F, R(34), 0x4D2C6DFC);
  580. P(F, G, H, A, B, C, D, E, R(35), 0x53380D13);
  581. P(E, F, G, H, A, B, C, D, R(36), 0x650A7354);
  582. P(D, E, F, G, H, A, B, C, R(37), 0x766A0ABB);
  583. P(C, D, E, F, G, H, A, B, R(38), 0x81C2C92E);
  584. P(B, C, D, E, F, G, H, A, R(39), 0x92722C85);
  585. P(A, B, C, D, E, F, G, H, R(40), 0xA2BFE8A1);
  586. P(H, A, B, C, D, E, F, G, R(41), 0xA81A664B);
  587. P(G, H, A, B, C, D, E, F, R(42), 0xC24B8B70);
  588. P(F, G, H, A, B, C, D, E, R(43), 0xC76C51A3);
  589. P(E, F, G, H, A, B, C, D, R(44), 0xD192E819);
  590. P(D, E, F, G, H, A, B, C, R(45), 0xD6990624);
  591. P(C, D, E, F, G, H, A, B, R(46), 0xF40E3585);
  592. P(B, C, D, E, F, G, H, A, R(47), 0x106AA070);
  593. P(A, B, C, D, E, F, G, H, R(48), 0x19A4C116);
  594. P(H, A, B, C, D, E, F, G, R(49), 0x1E376C08);
  595. P(G, H, A, B, C, D, E, F, R(50), 0x2748774C);
  596. P(F, G, H, A, B, C, D, E, R(51), 0x34B0BCB5);
  597. P(E, F, G, H, A, B, C, D, R(52), 0x391C0CB3);
  598. P(D, E, F, G, H, A, B, C, R(53), 0x4ED8AA4A);
  599. P(C, D, E, F, G, H, A, B, R(54), 0x5B9CCA4F);
  600. P(B, C, D, E, F, G, H, A, R(55), 0x682E6FF3);
  601. P(A, B, C, D, E, F, G, H, R(56), 0x748F82EE);
  602. P(H, A, B, C, D, E, F, G, R(57), 0x78A5636F);
  603. P(G, H, A, B, C, D, E, F, R(58), 0x84C87814);
  604. P(F, G, H, A, B, C, D, E, R(59), 0x8CC70208);
  605. P(E, F, G, H, A, B, C, D, R(60), 0x90BEFFFA);
  606. P(D, E, F, G, H, A, B, C, R(61), 0xA4506CEB);
  607. P(C, D, E, F, G, H, A, B, R(62), 0xBEF9A3F7);
  608. P(B, C, D, E, F, G, H, A, R(63), 0xC67178F2);
  609. _hash.i[0]+=A;
  610. _hash.i[1]+=B;
  611. _hash.i[2]+=C;
  612. _hash.i[3]+=D;
  613. _hash.i[4]+=E;
  614. _hash.i[5]+=F;
  615. _hash.i[6]+=G;
  616. _hash.i[7]+=H;
  617. }
  618. void SHA2::update(CPtr data, Int size)
  619. {
  620. if(size>0)
  621. {
  622. Int left=_count[0]&0x3F,
  623. fill=64-left;
  624. _count[0]+=size; if(_count[0]<size)_count[1]++;
  625. if(left && size>=fill)
  626. {
  627. CopyFast(_buffer+left, data, fill);
  628. transform(_buffer);
  629. size-= fill;
  630. data =(Byte*)data + fill;
  631. left = 0;
  632. }
  633. while(size>=64)
  634. {
  635. transform((Byte*)data);
  636. size-=64;
  637. data =(Byte*)data+64;
  638. }
  639. if(size)CopyFast(_buffer+left, data, size);
  640. }
  641. }
  642. INLINE void SHA2::finalize()
  643. {
  644. if(!_finalized)
  645. {
  646. UInt size[2]=
  647. {
  648. (_count[0]>>29)|(_count[1]<<3),
  649. (_count[0]<< 3),
  650. };
  651. UInt last=(_count[0]&0x3F),
  652. padn=(last<56) ? 56-last : 120-last;
  653. SwapEndian(size[0]);
  654. SwapEndian(size[1]);
  655. update(HashPadding, padn );
  656. update(size , SIZE(size));
  657. REPA(_hash.i)SwapEndian(_hash.i[i]);
  658. // clear sensitive information
  659. if(ZERO_BUFFERS)
  660. {
  661. Zero(_buffer);
  662. Zero(_count );
  663. }
  664. _finalized=true;
  665. }
  666. }
  667. #define C const
  668. C SHA2::Hash& SHA2::operator()()
  669. {
  670. finalize(); return _hash;
  671. }
  672. /******************************************************************************/
  673. // MAIN
  674. /******************************************************************************/
  675. UInt CRC32Mem(CPtr data, Int size) {CRC32 hash; hash.update(data, size); return hash();}
  676. UInt xxHash32Mem(CPtr data, Int size) {return XXH32(data, size, 0);}
  677. UInt xxHash64_32Mem(CPtr data, Int size) {return XXH64(data, size, 0);}
  678. ULong xxHash64Mem(CPtr data, Int size) {return XXH64(data, size, 0);}
  679. UInt SpookyHash32Mem(CPtr data, Int size) {return _SpookyHash ::Hash32 (data, size, 0);}
  680. ULong SpookyHash64Mem(CPtr data, Int size) {return _SpookyHash ::Hash64 (data, size, 0);}
  681. UID SpookyHash128Mem(CPtr data, Int size) {UID hash(0, 0); _SpookyHash ::Hash128(data, size, &hash.l[0], &hash.l[1]); return hash;} // !! need to initialize 'hash' to zero, because 'Hash128' treats it as seed !!
  682. ULong MetroHash64Mem(CPtr data, Int size) {ULong hash; _MetroHash64 ::Hash ((Byte*)data, size, (Byte*)&hash ); return hash;}
  683. UID MetroHash128Mem(CPtr data, Int size) {UID hash; _MetroHash128::Hash ((Byte*)data, size, hash.b ); return hash;}
  684. UID MD5Mem(CPtr data, Int size) {MD5 hash; hash.update(data, size); return hash();}
  685. SHA1::Hash SHA1Mem(CPtr data, Int size) {SHA1 hash; hash.update(data, size); return hash();}
  686. SHA2::Hash SHA2Mem(CPtr data, Int size) {SHA2 hash; hash.update(data, size); return hash();}
  687. /******************************************************************************/
  688. SHA2::Hash HMAC_SHA2(CPtr key, Int key_size, CPtr data, Int data_size)
  689. {
  690. SHA2 sha;
  691. if(key_size>64) // too long keys need to be converted to hash
  692. {
  693. sha.update(key, key_size);
  694. key=&sha(); // store pointer to hash result
  695. key_size=SIZE(SHA2::Hash);
  696. }
  697. Byte i_key_pad[64], o_key_pad[64]; // set 'i_key_pad/o_key_pad' before resetting 'sha', because they may operate on 'key' which points to 'sha.hash' !!
  698. Int i=0;
  699. for(; i<key_size; i++)
  700. {
  701. Byte k=((Byte*)key)[i];
  702. i_key_pad[i]=k^0x36;
  703. o_key_pad[i]=k^0x5C;
  704. }
  705. for(; i<64; i++)
  706. {
  707. i_key_pad[i]=0x36;
  708. o_key_pad[i]=0x5C;
  709. }
  710. sha.reset(); // !! reset 'sha' only after 'i_key_pad/o_key_pad' have been set, because they may operate on 'key' which points to 'sha.hash' !!
  711. sha.update(i_key_pad, SIZE(i_key_pad));
  712. sha.update(data , data_size );
  713. SHA2::Hash hash=sha();
  714. sha.reset();
  715. sha.update(o_key_pad, SIZE(o_key_pad));
  716. sha.update(&hash , SIZE(hash ));
  717. return sha();
  718. }
  719. /******************************************************************************/
  720. void InitHash()
  721. {
  722. CRC32Init();
  723. DEBUG_ASSERT(_MetroHash64 ::ImplementationVerified(), "MetroHash64 fail" );
  724. DEBUG_ASSERT(_MetroHash128::ImplementationVerified(), "MetroHash128 fail");
  725. }
  726. /******************************************************************************/
  727. }
  728. /******************************************************************************/