hash_whirlpool.c 16 KB

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