hash_whirlpool.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Michael Wallner <mike@php.net> |
  16. | Sara Golemon <pollita@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php_hash.h"
  20. /*
  21. * TODO: simplify Update and Final, those look ridiculously complex
  22. * Mike, 2005-11-23
  23. */
  24. #include "php_hash_whirlpool.h"
  25. #include "php_hash_whirlpool_tables.h"
  26. #define DIGESTBYTES 64
  27. #define DIGESTBITS (8*DIGESTBYTES) /* 512 */
  28. #define WBLOCKBYTES 64
  29. #define WBLOCKBITS (8*WBLOCKBYTES) /* 512 */
  30. #define LENGTHBYTES 32
  31. #define LENGTHBITS (8*LENGTHBYTES) /* 256 */
  32. static void WhirlpoolTransform(PHP_WHIRLPOOL_CTX *context)
  33. {
  34. int i, r;
  35. uint64_t K[8]; /* the round key */
  36. uint64_t block[8]; /* mu(buffer) */
  37. uint64_t state[8]; /* the cipher state */
  38. uint64_t L[8];
  39. unsigned char *buffer = context->buffer.data;
  40. /*
  41. * map the buffer to a block:
  42. */
  43. for (i = 0; i < 8; i++, buffer += 8) {
  44. block[i] =
  45. (((uint64_t)buffer[0] ) << 56) ^
  46. (((uint64_t)buffer[1] & 0xffL) << 48) ^
  47. (((uint64_t)buffer[2] & 0xffL) << 40) ^
  48. (((uint64_t)buffer[3] & 0xffL) << 32) ^
  49. (((uint64_t)buffer[4] & 0xffL) << 24) ^
  50. (((uint64_t)buffer[5] & 0xffL) << 16) ^
  51. (((uint64_t)buffer[6] & 0xffL) << 8) ^
  52. (((uint64_t)buffer[7] & 0xffL) );
  53. }
  54. /*
  55. * compute and apply K^0 to the cipher state:
  56. */
  57. state[0] = block[0] ^ (K[0] = context->state[0]);
  58. state[1] = block[1] ^ (K[1] = context->state[1]);
  59. state[2] = block[2] ^ (K[2] = context->state[2]);
  60. state[3] = block[3] ^ (K[3] = context->state[3]);
  61. state[4] = block[4] ^ (K[4] = context->state[4]);
  62. state[5] = block[5] ^ (K[5] = context->state[5]);
  63. state[6] = block[6] ^ (K[6] = context->state[6]);
  64. state[7] = block[7] ^ (K[7] = context->state[7]);
  65. /*
  66. * iterate over all rounds:
  67. */
  68. for (r = 1; r <= R; r++) {
  69. /*
  70. * compute K^r from K^{r-1}:
  71. */
  72. L[0] =
  73. C0[(int)(K[0] >> 56) ] ^
  74. C1[(int)(K[7] >> 48) & 0xff] ^
  75. C2[(int)(K[6] >> 40) & 0xff] ^
  76. C3[(int)(K[5] >> 32) & 0xff] ^
  77. C4[(int)(K[4] >> 24) & 0xff] ^
  78. C5[(int)(K[3] >> 16) & 0xff] ^
  79. C6[(int)(K[2] >> 8) & 0xff] ^
  80. C7[(int)(K[1] ) & 0xff] ^
  81. rc[r];
  82. L[1] =
  83. C0[(int)(K[1] >> 56) ] ^
  84. C1[(int)(K[0] >> 48) & 0xff] ^
  85. C2[(int)(K[7] >> 40) & 0xff] ^
  86. C3[(int)(K[6] >> 32) & 0xff] ^
  87. C4[(int)(K[5] >> 24) & 0xff] ^
  88. C5[(int)(K[4] >> 16) & 0xff] ^
  89. C6[(int)(K[3] >> 8) & 0xff] ^
  90. C7[(int)(K[2] ) & 0xff];
  91. L[2] =
  92. C0[(int)(K[2] >> 56) ] ^
  93. C1[(int)(K[1] >> 48) & 0xff] ^
  94. C2[(int)(K[0] >> 40) & 0xff] ^
  95. C3[(int)(K[7] >> 32) & 0xff] ^
  96. C4[(int)(K[6] >> 24) & 0xff] ^
  97. C5[(int)(K[5] >> 16) & 0xff] ^
  98. C6[(int)(K[4] >> 8) & 0xff] ^
  99. C7[(int)(K[3] ) & 0xff];
  100. L[3] =
  101. C0[(int)(K[3] >> 56) ] ^
  102. C1[(int)(K[2] >> 48) & 0xff] ^
  103. C2[(int)(K[1] >> 40) & 0xff] ^
  104. C3[(int)(K[0] >> 32) & 0xff] ^
  105. C4[(int)(K[7] >> 24) & 0xff] ^
  106. C5[(int)(K[6] >> 16) & 0xff] ^
  107. C6[(int)(K[5] >> 8) & 0xff] ^
  108. C7[(int)(K[4] ) & 0xff];
  109. L[4] =
  110. C0[(int)(K[4] >> 56) ] ^
  111. C1[(int)(K[3] >> 48) & 0xff] ^
  112. C2[(int)(K[2] >> 40) & 0xff] ^
  113. C3[(int)(K[1] >> 32) & 0xff] ^
  114. C4[(int)(K[0] >> 24) & 0xff] ^
  115. C5[(int)(K[7] >> 16) & 0xff] ^
  116. C6[(int)(K[6] >> 8) & 0xff] ^
  117. C7[(int)(K[5] ) & 0xff];
  118. L[5] =
  119. C0[(int)(K[5] >> 56) ] ^
  120. C1[(int)(K[4] >> 48) & 0xff] ^
  121. C2[(int)(K[3] >> 40) & 0xff] ^
  122. C3[(int)(K[2] >> 32) & 0xff] ^
  123. C4[(int)(K[1] >> 24) & 0xff] ^
  124. C5[(int)(K[0] >> 16) & 0xff] ^
  125. C6[(int)(K[7] >> 8) & 0xff] ^
  126. C7[(int)(K[6] ) & 0xff];
  127. L[6] =
  128. C0[(int)(K[6] >> 56) ] ^
  129. C1[(int)(K[5] >> 48) & 0xff] ^
  130. C2[(int)(K[4] >> 40) & 0xff] ^
  131. C3[(int)(K[3] >> 32) & 0xff] ^
  132. C4[(int)(K[2] >> 24) & 0xff] ^
  133. C5[(int)(K[1] >> 16) & 0xff] ^
  134. C6[(int)(K[0] >> 8) & 0xff] ^
  135. C7[(int)(K[7] ) & 0xff];
  136. L[7] =
  137. C0[(int)(K[7] >> 56) ] ^
  138. C1[(int)(K[6] >> 48) & 0xff] ^
  139. C2[(int)(K[5] >> 40) & 0xff] ^
  140. C3[(int)(K[4] >> 32) & 0xff] ^
  141. C4[(int)(K[3] >> 24) & 0xff] ^
  142. C5[(int)(K[2] >> 16) & 0xff] ^
  143. C6[(int)(K[1] >> 8) & 0xff] ^
  144. C7[(int)(K[0] ) & 0xff];
  145. K[0] = L[0];
  146. K[1] = L[1];
  147. K[2] = L[2];
  148. K[3] = L[3];
  149. K[4] = L[4];
  150. K[5] = L[5];
  151. K[6] = L[6];
  152. K[7] = L[7];
  153. /*
  154. * apply the r-th round transformation:
  155. */
  156. L[0] =
  157. C0[(int)(state[0] >> 56) ] ^
  158. C1[(int)(state[7] >> 48) & 0xff] ^
  159. C2[(int)(state[6] >> 40) & 0xff] ^
  160. C3[(int)(state[5] >> 32) & 0xff] ^
  161. C4[(int)(state[4] >> 24) & 0xff] ^
  162. C5[(int)(state[3] >> 16) & 0xff] ^
  163. C6[(int)(state[2] >> 8) & 0xff] ^
  164. C7[(int)(state[1] ) & 0xff] ^
  165. K[0];
  166. L[1] =
  167. C0[(int)(state[1] >> 56) ] ^
  168. C1[(int)(state[0] >> 48) & 0xff] ^
  169. C2[(int)(state[7] >> 40) & 0xff] ^
  170. C3[(int)(state[6] >> 32) & 0xff] ^
  171. C4[(int)(state[5] >> 24) & 0xff] ^
  172. C5[(int)(state[4] >> 16) & 0xff] ^
  173. C6[(int)(state[3] >> 8) & 0xff] ^
  174. C7[(int)(state[2] ) & 0xff] ^
  175. K[1];
  176. L[2] =
  177. C0[(int)(state[2] >> 56) ] ^
  178. C1[(int)(state[1] >> 48) & 0xff] ^
  179. C2[(int)(state[0] >> 40) & 0xff] ^
  180. C3[(int)(state[7] >> 32) & 0xff] ^
  181. C4[(int)(state[6] >> 24) & 0xff] ^
  182. C5[(int)(state[5] >> 16) & 0xff] ^
  183. C6[(int)(state[4] >> 8) & 0xff] ^
  184. C7[(int)(state[3] ) & 0xff] ^
  185. K[2];
  186. L[3] =
  187. C0[(int)(state[3] >> 56) ] ^
  188. C1[(int)(state[2] >> 48) & 0xff] ^
  189. C2[(int)(state[1] >> 40) & 0xff] ^
  190. C3[(int)(state[0] >> 32) & 0xff] ^
  191. C4[(int)(state[7] >> 24) & 0xff] ^
  192. C5[(int)(state[6] >> 16) & 0xff] ^
  193. C6[(int)(state[5] >> 8) & 0xff] ^
  194. C7[(int)(state[4] ) & 0xff] ^
  195. K[3];
  196. L[4] =
  197. C0[(int)(state[4] >> 56) ] ^
  198. C1[(int)(state[3] >> 48) & 0xff] ^
  199. C2[(int)(state[2] >> 40) & 0xff] ^
  200. C3[(int)(state[1] >> 32) & 0xff] ^
  201. C4[(int)(state[0] >> 24) & 0xff] ^
  202. C5[(int)(state[7] >> 16) & 0xff] ^
  203. C6[(int)(state[6] >> 8) & 0xff] ^
  204. C7[(int)(state[5] ) & 0xff] ^
  205. K[4];
  206. L[5] =
  207. C0[(int)(state[5] >> 56) ] ^
  208. C1[(int)(state[4] >> 48) & 0xff] ^
  209. C2[(int)(state[3] >> 40) & 0xff] ^
  210. C3[(int)(state[2] >> 32) & 0xff] ^
  211. C4[(int)(state[1] >> 24) & 0xff] ^
  212. C5[(int)(state[0] >> 16) & 0xff] ^
  213. C6[(int)(state[7] >> 8) & 0xff] ^
  214. C7[(int)(state[6] ) & 0xff] ^
  215. K[5];
  216. L[6] =
  217. C0[(int)(state[6] >> 56) ] ^
  218. C1[(int)(state[5] >> 48) & 0xff] ^
  219. C2[(int)(state[4] >> 40) & 0xff] ^
  220. C3[(int)(state[3] >> 32) & 0xff] ^
  221. C4[(int)(state[2] >> 24) & 0xff] ^
  222. C5[(int)(state[1] >> 16) & 0xff] ^
  223. C6[(int)(state[0] >> 8) & 0xff] ^
  224. C7[(int)(state[7] ) & 0xff] ^
  225. K[6];
  226. L[7] =
  227. C0[(int)(state[7] >> 56) ] ^
  228. C1[(int)(state[6] >> 48) & 0xff] ^
  229. C2[(int)(state[5] >> 40) & 0xff] ^
  230. C3[(int)(state[4] >> 32) & 0xff] ^
  231. C4[(int)(state[3] >> 24) & 0xff] ^
  232. C5[(int)(state[2] >> 16) & 0xff] ^
  233. C6[(int)(state[1] >> 8) & 0xff] ^
  234. C7[(int)(state[0] ) & 0xff] ^
  235. K[7];
  236. state[0] = L[0];
  237. state[1] = L[1];
  238. state[2] = L[2];
  239. state[3] = L[3];
  240. state[4] = L[4];
  241. state[5] = L[5];
  242. state[6] = L[6];
  243. state[7] = L[7];
  244. }
  245. /*
  246. * apply the Miyaguchi-Preneel compression function:
  247. */
  248. context->state[0] ^= state[0] ^ block[0];
  249. context->state[1] ^= state[1] ^ block[1];
  250. context->state[2] ^= state[2] ^ block[2];
  251. context->state[3] ^= state[3] ^ block[3];
  252. context->state[4] ^= state[4] ^ block[4];
  253. context->state[5] ^= state[5] ^ block[5];
  254. context->state[6] ^= state[6] ^ block[6];
  255. context->state[7] ^= state[7] ^ block[7];
  256. ZEND_SECURE_ZERO(state, sizeof(state));
  257. }
  258. PHP_HASH_API void PHP_WHIRLPOOLInit(PHP_WHIRLPOOL_CTX *context)
  259. {
  260. memset(context, 0, sizeof(*context));
  261. }
  262. PHP_HASH_API void PHP_WHIRLPOOLUpdate(PHP_WHIRLPOOL_CTX *context, const unsigned char *input, size_t len)
  263. {
  264. uint64_t sourceBits = len * 8;
  265. int sourcePos = 0; /* index of leftmost source unsigned char containing data (1 to 8 bits). */
  266. int sourceGap = (8 - ((int)sourceBits & 7)) & 7; /* space on source[sourcePos]. */
  267. int bufferRem = context->buffer.bits & 7; /* occupied bits on buffer[bufferPos]. */
  268. const unsigned char *source = input;
  269. unsigned char *buffer = context->buffer.data;
  270. unsigned char *bitLength = context->bitlength;
  271. int bufferBits = context->buffer.bits;
  272. int bufferPos = context->buffer.pos;
  273. uint32_t b, carry;
  274. int i;
  275. /*
  276. * tally the length of the added data:
  277. */
  278. uint64_t value = sourceBits;
  279. for (i = 31, carry = 0; i >= 0 && (carry != 0 || value != L64(0)); i--) {
  280. carry += bitLength[i] + ((uint32_t)value & 0xff);
  281. bitLength[i] = (unsigned char)carry;
  282. carry >>= 8;
  283. value >>= 8;
  284. }
  285. /*
  286. * process data in chunks of 8 bits (a more efficient approach would be to take whole-word chunks):
  287. */
  288. while (sourceBits > 8) {
  289. /* N.B. at least source[sourcePos] and source[sourcePos+1] contain data. */
  290. /*
  291. * take a byte from the source:
  292. */
  293. b = ((source[sourcePos] << sourceGap) & 0xff) |
  294. ((source[sourcePos + 1] & 0xff) >> (8 - sourceGap));
  295. /*
  296. * process this byte:
  297. */
  298. buffer[bufferPos++] |= (unsigned char)(b >> bufferRem);
  299. bufferBits += 8 - bufferRem; /* bufferBits = 8*bufferPos; */
  300. if (bufferBits == DIGESTBITS) {
  301. /*
  302. * process data block:
  303. */
  304. WhirlpoolTransform(context);
  305. /*
  306. * reset buffer:
  307. */
  308. bufferBits = bufferPos = 0;
  309. }
  310. buffer[bufferPos] = (unsigned char) (b << (8 - bufferRem));
  311. bufferBits += bufferRem;
  312. /*
  313. * proceed to remaining data:
  314. */
  315. sourceBits -= 8;
  316. sourcePos++;
  317. }
  318. /* now 0 <= sourceBits <= 8;
  319. * furthermore, all data (if any is left) is in source[sourcePos].
  320. */
  321. if (sourceBits > 0) {
  322. b = (source[sourcePos] << sourceGap) & 0xff; /* bits are left-justified on b. */
  323. /*
  324. * process the remaining bits:
  325. */
  326. buffer[bufferPos] |= b >> bufferRem;
  327. } else {
  328. b = 0;
  329. }
  330. if (bufferRem + sourceBits < 8) {
  331. /*
  332. * all remaining data fits on buffer[bufferPos],
  333. * and there still remains some space.
  334. */
  335. bufferBits += (int) sourceBits;
  336. } else {
  337. /*
  338. * buffer[bufferPos] is full:
  339. */
  340. bufferPos++;
  341. bufferBits += 8 - bufferRem; /* bufferBits = 8*bufferPos; */
  342. sourceBits -= 8 - bufferRem;
  343. /* now 0 <= sourceBits < 8;
  344. * furthermore, all data (if any is left) is in source[sourcePos].
  345. */
  346. if (bufferBits == DIGESTBITS) {
  347. /*
  348. * process data block:
  349. */
  350. WhirlpoolTransform(context);
  351. /*
  352. * reset buffer:
  353. */
  354. bufferBits = bufferPos = 0;
  355. }
  356. buffer[bufferPos] = (unsigned char) (b << (8 - bufferRem));
  357. bufferBits += (int)sourceBits;
  358. }
  359. context->buffer.bits = bufferBits;
  360. context->buffer.pos = bufferPos;
  361. }
  362. PHP_HASH_API void PHP_WHIRLPOOLFinal(unsigned char digest[64], PHP_WHIRLPOOL_CTX *context)
  363. {
  364. int i;
  365. unsigned char *buffer = context->buffer.data;
  366. unsigned char *bitLength = context->bitlength;
  367. int bufferBits = context->buffer.bits;
  368. int bufferPos = context->buffer.pos;
  369. /*
  370. * append a '1'-bit:
  371. */
  372. buffer[bufferPos] |= 0x80U >> (bufferBits & 7);
  373. bufferPos++; /* all remaining bits on the current unsigned char are set to zero. */
  374. /*
  375. * pad with zero bits to complete (N*WBLOCKBITS - LENGTHBITS) bits:
  376. */
  377. if (bufferPos > WBLOCKBYTES - LENGTHBYTES) {
  378. if (bufferPos < WBLOCKBYTES) {
  379. memset(&buffer[bufferPos], 0, WBLOCKBYTES - bufferPos);
  380. }
  381. /*
  382. * process data block:
  383. */
  384. WhirlpoolTransform(context);
  385. /*
  386. * reset buffer:
  387. */
  388. bufferPos = 0;
  389. }
  390. if (bufferPos < WBLOCKBYTES - LENGTHBYTES) {
  391. memset(&buffer[bufferPos], 0, (WBLOCKBYTES - LENGTHBYTES) - bufferPos);
  392. }
  393. bufferPos = WBLOCKBYTES - LENGTHBYTES;
  394. /*
  395. * append bit length of hashed data:
  396. */
  397. memcpy(&buffer[WBLOCKBYTES - LENGTHBYTES], bitLength, LENGTHBYTES);
  398. /*
  399. * process data block:
  400. */
  401. WhirlpoolTransform(context);
  402. /*
  403. * return the completed message digest:
  404. */
  405. for (i = 0; i < DIGESTBYTES/8; i++) {
  406. digest[0] = (unsigned char)(context->state[i] >> 56);
  407. digest[1] = (unsigned char)(context->state[i] >> 48);
  408. digest[2] = (unsigned char)(context->state[i] >> 40);
  409. digest[3] = (unsigned char)(context->state[i] >> 32);
  410. digest[4] = (unsigned char)(context->state[i] >> 24);
  411. digest[5] = (unsigned char)(context->state[i] >> 16);
  412. digest[6] = (unsigned char)(context->state[i] >> 8);
  413. digest[7] = (unsigned char)(context->state[i] );
  414. digest += 8;
  415. }
  416. ZEND_SECURE_ZERO(context, sizeof(*context));
  417. }
  418. const php_hash_ops php_hash_whirlpool_ops = {
  419. (php_hash_init_func_t) PHP_WHIRLPOOLInit,
  420. (php_hash_update_func_t) PHP_WHIRLPOOLUpdate,
  421. (php_hash_final_func_t) PHP_WHIRLPOOLFinal,
  422. (php_hash_copy_func_t) php_hash_copy,
  423. 64,
  424. 64,
  425. sizeof(PHP_WHIRLPOOL_CTX),
  426. 1
  427. };
  428. /*
  429. * Local variables:
  430. * tab-width: 4
  431. * c-basic-offset: 4
  432. * End:
  433. * vim600: sw=4 ts=4 fdm=marker
  434. * vim<600: sw=4 ts=4
  435. */