hash_sha.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124
  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: Steffan Esser <sesser@php.net> |
  16. | Sara Golemon <pollita@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include "php_hash.h"
  20. #include "php_hash_sha.h"
  21. static const unsigned char PADDING[128] =
  22. {
  23. 0x80, 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. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  26. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  27. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  28. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  29. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  30. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  31. };
  32. /* {{{ SHAEncode32
  33. Encodes input (uint32_t) into output (unsigned char). Assumes len is
  34. a multiple of 4.
  35. */
  36. static void SHAEncode32(unsigned char *output, uint32_t *input, unsigned int len)
  37. {
  38. unsigned int i, j;
  39. for (i = 0, j = 0; j < len; i++, j += 4) {
  40. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  41. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  42. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  43. output[j + 3] = (unsigned char) (input[i] & 0xff);
  44. }
  45. }
  46. /* }}} */
  47. /* {{{ SHADecode32
  48. Decodes input (unsigned char) into output (uint32_t). Assumes len is
  49. a multiple of 4.
  50. */
  51. static void SHADecode32(uint32_t *output, const unsigned char *input, unsigned int len)
  52. {
  53. unsigned int i, j;
  54. for (i = 0, j = 0; j < len; i++, j += 4)
  55. output[i] = ((uint32_t) input[j + 3]) | (((uint32_t) input[j + 2]) << 8) |
  56. (((uint32_t) input[j + 1]) << 16) | (((uint32_t) input[j]) << 24);
  57. }
  58. /* }}} */
  59. const php_hash_ops php_hash_sha1_ops = {
  60. (php_hash_init_func_t) PHP_SHA1Init,
  61. (php_hash_update_func_t) PHP_SHA1Update,
  62. (php_hash_final_func_t) PHP_SHA1Final,
  63. (php_hash_copy_func_t) php_hash_copy,
  64. 20,
  65. 64,
  66. sizeof(PHP_SHA1_CTX),
  67. 1
  68. };
  69. #ifdef PHP_HASH_SHA1_NOT_IN_CORE
  70. PHP_HASH_API void make_sha1_digest(char *sha1str, unsigned char *digest)
  71. {
  72. php_hash_bin2hex(sha1str, digest, 20);
  73. sha1str[40] = '\0';
  74. }
  75. /* {{{ proto string sha1(string str [, bool raw_output])
  76. Calculate the sha1 hash of a string */
  77. PHP_FUNCTION(sha1)
  78. {
  79. char *arg;
  80. size_t arg_len;
  81. zend_bool raw_output = 0;
  82. PHP_SHA1_CTX context;
  83. unsigned char digest[20];
  84. if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|b", &arg, &arg_len, &raw_output) == FAILURE) {
  85. return;
  86. }
  87. PHP_SHA1Init(&context);
  88. PHP_SHA1Update(&context, arg, arg_len);
  89. PHP_SHA1Final(digest, &context);
  90. if (raw_output) {
  91. RETURN_STRINGL(digest, 20);
  92. } else {
  93. RETVAL_NEW_STR(zend_string_alloc(40, 0));
  94. make_sha1_digest(Z_STRVAL_P(return_value), digest);
  95. }
  96. }
  97. /* }}} */
  98. /* {{{ proto string sha1_file(string filename [, bool raw_output])
  99. Calculate the sha1 hash of given filename */
  100. PHP_FUNCTION(sha1_file)
  101. {
  102. char *arg;
  103. size_t arg_len;
  104. zend_bool raw_output = 0;
  105. unsigned char buf[1024];
  106. unsigned char digest[20];
  107. PHP_SHA1_CTX context;
  108. int n;
  109. php_stream *stream;
  110. if (zend_parse_parameters(ZEND_NUM_ARGS(), "p|b", &arg, &arg_len, &raw_output) == FAILURE) {
  111. return;
  112. }
  113. stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
  114. if (!stream) {
  115. RETURN_FALSE;
  116. }
  117. PHP_SHA1Init(&context);
  118. while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
  119. PHP_SHA1Update(&context, buf, n);
  120. }
  121. PHP_SHA1Final(digest, &context);
  122. php_stream_close(stream);
  123. if (n<0) {
  124. RETURN_FALSE;
  125. }
  126. if (raw_output) {
  127. RETURN_STRINGL(digest, 20);
  128. } else {
  129. RETVAL_NEW_STR(zend_string_alloc(40, 0));
  130. make_sha1_digest(Z_STRVAL_P(return_value), digest);
  131. }
  132. }
  133. /* }}} */
  134. /* F, G, H and I are basic SHA1 functions.
  135. */
  136. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  137. #define G(x, y, z) ((x) ^ (y) ^ (z))
  138. #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  139. #define I(x, y, z) ((x) ^ (y) ^ (z))
  140. /* ROTATE_LEFT rotates x left n bits.
  141. */
  142. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  143. /* W[i]
  144. */
  145. #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
  146. (x[i&15]=ROTATE_LEFT(tmp, 1)) )
  147. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  148. */
  149. #define FF(a, b, c, d, e, w) { \
  150. (e) += F ((b), (c), (d)) + (w) + (uint32_t)(0x5A827999); \
  151. (e) += ROTATE_LEFT ((a), 5); \
  152. (b) = ROTATE_LEFT((b), 30); \
  153. }
  154. #define GG(a, b, c, d, e, w) { \
  155. (e) += G ((b), (c), (d)) + (w) + (uint32_t)(0x6ED9EBA1); \
  156. (e) += ROTATE_LEFT ((a), 5); \
  157. (b) = ROTATE_LEFT((b), 30); \
  158. }
  159. #define HH(a, b, c, d, e, w) { \
  160. (e) += H ((b), (c), (d)) + (w) + (uint32_t)(0x8F1BBCDC); \
  161. (e) += ROTATE_LEFT ((a), 5); \
  162. (b) = ROTATE_LEFT((b), 30); \
  163. }
  164. #define II(a, b, c, d, e, w) { \
  165. (e) += I ((b), (c), (d)) + (w) + (uint32_t)(0xCA62C1D6); \
  166. (e) += ROTATE_LEFT ((a), 5); \
  167. (b) = ROTATE_LEFT((b), 30); \
  168. }
  169. /* {{{ PHP_SHA1Init
  170. * SHA1 initialization. Begins an SHA1 operation, writing a new context.
  171. */
  172. PHP_HASH_API void PHP_SHA1Init(PHP_SHA1_CTX * context)
  173. {
  174. context->count[0] = context->count[1] = 0;
  175. /* Load magic initialization constants.
  176. */
  177. context->state[0] = 0x67452301;
  178. context->state[1] = 0xefcdab89;
  179. context->state[2] = 0x98badcfe;
  180. context->state[3] = 0x10325476;
  181. context->state[4] = 0xc3d2e1f0;
  182. }
  183. /* }}} */
  184. /* {{{ SHA1Transform
  185. * SHA1 basic transformation. Transforms state based on block.
  186. */
  187. static void SHA1Transform(uint32_t state[5], const unsigned char block[64])
  188. {
  189. uint32_t a = state[0], b = state[1], c = state[2];
  190. uint32_t d = state[3], e = state[4], x[16], tmp;
  191. SHADecode32(x, block, 64);
  192. /* Round 1 */
  193. FF(a, b, c, d, e, x[0]); /* 1 */
  194. FF(e, a, b, c, d, x[1]); /* 2 */
  195. FF(d, e, a, b, c, x[2]); /* 3 */
  196. FF(c, d, e, a, b, x[3]); /* 4 */
  197. FF(b, c, d, e, a, x[4]); /* 5 */
  198. FF(a, b, c, d, e, x[5]); /* 6 */
  199. FF(e, a, b, c, d, x[6]); /* 7 */
  200. FF(d, e, a, b, c, x[7]); /* 8 */
  201. FF(c, d, e, a, b, x[8]); /* 9 */
  202. FF(b, c, d, e, a, x[9]); /* 10 */
  203. FF(a, b, c, d, e, x[10]); /* 11 */
  204. FF(e, a, b, c, d, x[11]); /* 12 */
  205. FF(d, e, a, b, c, x[12]); /* 13 */
  206. FF(c, d, e, a, b, x[13]); /* 14 */
  207. FF(b, c, d, e, a, x[14]); /* 15 */
  208. FF(a, b, c, d, e, x[15]); /* 16 */
  209. FF(e, a, b, c, d, W(16)); /* 17 */
  210. FF(d, e, a, b, c, W(17)); /* 18 */
  211. FF(c, d, e, a, b, W(18)); /* 19 */
  212. FF(b, c, d, e, a, W(19)); /* 20 */
  213. /* Round 2 */
  214. GG(a, b, c, d, e, W(20)); /* 21 */
  215. GG(e, a, b, c, d, W(21)); /* 22 */
  216. GG(d, e, a, b, c, W(22)); /* 23 */
  217. GG(c, d, e, a, b, W(23)); /* 24 */
  218. GG(b, c, d, e, a, W(24)); /* 25 */
  219. GG(a, b, c, d, e, W(25)); /* 26 */
  220. GG(e, a, b, c, d, W(26)); /* 27 */
  221. GG(d, e, a, b, c, W(27)); /* 28 */
  222. GG(c, d, e, a, b, W(28)); /* 29 */
  223. GG(b, c, d, e, a, W(29)); /* 30 */
  224. GG(a, b, c, d, e, W(30)); /* 31 */
  225. GG(e, a, b, c, d, W(31)); /* 32 */
  226. GG(d, e, a, b, c, W(32)); /* 33 */
  227. GG(c, d, e, a, b, W(33)); /* 34 */
  228. GG(b, c, d, e, a, W(34)); /* 35 */
  229. GG(a, b, c, d, e, W(35)); /* 36 */
  230. GG(e, a, b, c, d, W(36)); /* 37 */
  231. GG(d, e, a, b, c, W(37)); /* 38 */
  232. GG(c, d, e, a, b, W(38)); /* 39 */
  233. GG(b, c, d, e, a, W(39)); /* 40 */
  234. /* Round 3 */
  235. HH(a, b, c, d, e, W(40)); /* 41 */
  236. HH(e, a, b, c, d, W(41)); /* 42 */
  237. HH(d, e, a, b, c, W(42)); /* 43 */
  238. HH(c, d, e, a, b, W(43)); /* 44 */
  239. HH(b, c, d, e, a, W(44)); /* 45 */
  240. HH(a, b, c, d, e, W(45)); /* 46 */
  241. HH(e, a, b, c, d, W(46)); /* 47 */
  242. HH(d, e, a, b, c, W(47)); /* 48 */
  243. HH(c, d, e, a, b, W(48)); /* 49 */
  244. HH(b, c, d, e, a, W(49)); /* 50 */
  245. HH(a, b, c, d, e, W(50)); /* 51 */
  246. HH(e, a, b, c, d, W(51)); /* 52 */
  247. HH(d, e, a, b, c, W(52)); /* 53 */
  248. HH(c, d, e, a, b, W(53)); /* 54 */
  249. HH(b, c, d, e, a, W(54)); /* 55 */
  250. HH(a, b, c, d, e, W(55)); /* 56 */
  251. HH(e, a, b, c, d, W(56)); /* 57 */
  252. HH(d, e, a, b, c, W(57)); /* 58 */
  253. HH(c, d, e, a, b, W(58)); /* 59 */
  254. HH(b, c, d, e, a, W(59)); /* 60 */
  255. /* Round 4 */
  256. II(a, b, c, d, e, W(60)); /* 61 */
  257. II(e, a, b, c, d, W(61)); /* 62 */
  258. II(d, e, a, b, c, W(62)); /* 63 */
  259. II(c, d, e, a, b, W(63)); /* 64 */
  260. II(b, c, d, e, a, W(64)); /* 65 */
  261. II(a, b, c, d, e, W(65)); /* 66 */
  262. II(e, a, b, c, d, W(66)); /* 67 */
  263. II(d, e, a, b, c, W(67)); /* 68 */
  264. II(c, d, e, a, b, W(68)); /* 69 */
  265. II(b, c, d, e, a, W(69)); /* 70 */
  266. II(a, b, c, d, e, W(70)); /* 71 */
  267. II(e, a, b, c, d, W(71)); /* 72 */
  268. II(d, e, a, b, c, W(72)); /* 73 */
  269. II(c, d, e, a, b, W(73)); /* 74 */
  270. II(b, c, d, e, a, W(74)); /* 75 */
  271. II(a, b, c, d, e, W(75)); /* 76 */
  272. II(e, a, b, c, d, W(76)); /* 77 */
  273. II(d, e, a, b, c, W(77)); /* 78 */
  274. II(c, d, e, a, b, W(78)); /* 79 */
  275. II(b, c, d, e, a, W(79)); /* 80 */
  276. state[0] += a;
  277. state[1] += b;
  278. state[2] += c;
  279. state[3] += d;
  280. state[4] += e;
  281. /* Zeroize sensitive information. */
  282. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  283. }
  284. /* }}} */
  285. /* {{{ PHP_SHA1Update
  286. SHA1 block update operation. Continues an SHA1 message-digest
  287. operation, processing another message block, and updating the
  288. context.
  289. */
  290. PHP_HASH_API void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
  291. unsigned int inputLen)
  292. {
  293. unsigned int i, index, partLen;
  294. /* Compute number of bytes mod 64 */
  295. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  296. /* Update number of bits */
  297. if ((context->count[0] += ((uint32_t) inputLen << 3))
  298. < ((uint32_t) inputLen << 3))
  299. context->count[1]++;
  300. context->count[1] += ((uint32_t) inputLen >> 29);
  301. partLen = 64 - index;
  302. /* Transform as many times as possible.
  303. */
  304. if (inputLen >= partLen) {
  305. memcpy
  306. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  307. SHA1Transform(context->state, context->buffer);
  308. for (i = partLen; i + 63 < inputLen; i += 64)
  309. SHA1Transform(context->state, &input[i]);
  310. index = 0;
  311. } else
  312. i = 0;
  313. /* Buffer remaining input */
  314. memcpy
  315. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  316. inputLen - i);
  317. }
  318. /* }}} */
  319. /* {{{ PHP_SHA1Final
  320. SHA1 finalization. Ends an SHA1 message-digest operation, writing the
  321. the message digest and zeroizing the context.
  322. */
  323. PHP_HASH_API void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
  324. {
  325. unsigned char bits[8];
  326. unsigned int index, padLen;
  327. /* Save number of bits */
  328. bits[7] = context->count[0] & 0xFF;
  329. bits[6] = (context->count[0] >> 8) & 0xFF;
  330. bits[5] = (context->count[0] >> 16) & 0xFF;
  331. bits[4] = (context->count[0] >> 24) & 0xFF;
  332. bits[3] = context->count[1] & 0xFF;
  333. bits[2] = (context->count[1] >> 8) & 0xFF;
  334. bits[1] = (context->count[1] >> 16) & 0xFF;
  335. bits[0] = (context->count[1] >> 24) & 0xFF;
  336. /* Pad out to 56 mod 64.
  337. */
  338. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  339. padLen = (index < 56) ? (56 - index) : (120 - index);
  340. PHP_SHA1Update(context, PADDING, padLen);
  341. /* Append length (before padding) */
  342. PHP_SHA1Update(context, bits, 8);
  343. /* Store state in digest */
  344. SHAEncode32(digest, context->state, 20);
  345. /* Zeroize sensitive information.
  346. */
  347. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  348. }
  349. /* }}} */
  350. #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
  351. /* sha224/sha256 */
  352. const php_hash_ops php_hash_sha256_ops = {
  353. (php_hash_init_func_t) PHP_SHA256Init,
  354. (php_hash_update_func_t) PHP_SHA256Update,
  355. (php_hash_final_func_t) PHP_SHA256Final,
  356. (php_hash_copy_func_t) php_hash_copy,
  357. 32,
  358. 64,
  359. sizeof(PHP_SHA256_CTX),
  360. 1
  361. };
  362. const php_hash_ops php_hash_sha224_ops = {
  363. (php_hash_init_func_t) PHP_SHA224Init,
  364. (php_hash_update_func_t) PHP_SHA224Update,
  365. (php_hash_final_func_t) PHP_SHA224Final,
  366. (php_hash_copy_func_t) php_hash_copy,
  367. 28,
  368. 64,
  369. sizeof(PHP_SHA224_CTX),
  370. 1
  371. };
  372. #define ROTR32(b,x) ((x >> b) | (x << (32 - b)))
  373. #define ROTR64(b,x) ((x >> b) | (x << (64 - b)))
  374. #define SHR(b, x) (x >> b)
  375. /* Ch */
  376. #define SHA256_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  377. /* Maj */
  378. #define SHA256_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  379. /* SUM0 */
  380. #define SHA256_F2(x) (ROTR32( 2,(x)) ^ ROTR32(13,(x)) ^ ROTR32(22,(x)))
  381. /* SUM1 */
  382. #define SHA256_F3(x) (ROTR32( 6,(x)) ^ ROTR32(11,(x)) ^ ROTR32(25,(x)))
  383. /* OM0 */
  384. #define SHA256_F4(x) (ROTR32( 7,(x)) ^ ROTR32(18,(x)) ^ SHR( 3,(x)))
  385. /* OM1 */
  386. #define SHA256_F5(x) (ROTR32(17,(x)) ^ ROTR32(19,(x)) ^ SHR(10,(x)))
  387. static const uint32_t SHA256_K[64] = {
  388. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  389. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  390. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  391. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  392. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  393. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  394. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  395. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
  396. /* {{{ PHP_SHA256Init
  397. * SHA256 initialization. Begins an SHA256 operation, writing a new context.
  398. */
  399. PHP_HASH_API void PHP_SHA256Init(PHP_SHA256_CTX * context)
  400. {
  401. context->count[0] = context->count[1] = 0;
  402. /* Load magic initialization constants.
  403. */
  404. context->state[0] = 0x6a09e667;
  405. context->state[1] = 0xbb67ae85;
  406. context->state[2] = 0x3c6ef372;
  407. context->state[3] = 0xa54ff53a;
  408. context->state[4] = 0x510e527f;
  409. context->state[5] = 0x9b05688c;
  410. context->state[6] = 0x1f83d9ab;
  411. context->state[7] = 0x5be0cd19;
  412. }
  413. /* }}} */
  414. /* {{{ SHA256Transform
  415. * SHA256 basic transformation. Transforms state based on block.
  416. */
  417. static void SHA256Transform(uint32_t state[8], const unsigned char block[64])
  418. {
  419. uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
  420. uint32_t e = state[4], f = state[5], g = state[6], h = state[7];
  421. uint32_t x[16], T1, T2, W[64];
  422. int i;
  423. SHADecode32(x, block, 64);
  424. /* Schedule */
  425. for(i = 0; i < 16; i++) {
  426. W[i] = x[i];
  427. }
  428. for(i = 16; i < 64; i++) {
  429. W[i] = SHA256_F5(W[i-2]) + W[i-7] + SHA256_F4(W[i-15]) + W[i-16];
  430. }
  431. for (i = 0; i < 64; i++) {
  432. T1 = h + SHA256_F3(e) + SHA256_F0(e,f,g) + SHA256_K[i] + W[i];
  433. T2 = SHA256_F2(a) + SHA256_F1(a,b,c);
  434. h = g; g = f; f = e; e = d + T1;
  435. d = c; c = b; b = a; a = T1 + T2;
  436. }
  437. state[0] += a;
  438. state[1] += b;
  439. state[2] += c;
  440. state[3] += d;
  441. state[4] += e;
  442. state[5] += f;
  443. state[6] += g;
  444. state[7] += h;
  445. /* Zeroize sensitive information. */
  446. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  447. }
  448. /* }}} */
  449. /* {{{ PHP_SHA224Init
  450. * SHA224 initialization. Begins an SHA224 operation, writing a new context.
  451. */
  452. PHP_HASH_API void PHP_SHA224Init(PHP_SHA224_CTX * context)
  453. {
  454. context->count[0] = context->count[1] = 0;
  455. /* Load magic initialization constants.
  456. */
  457. context->state[0] = 0xc1059ed8;
  458. context->state[1] = 0x367cd507;
  459. context->state[2] = 0x3070dd17;
  460. context->state[3] = 0xf70e5939;
  461. context->state[4] = 0xffc00b31;
  462. context->state[5] = 0x68581511;
  463. context->state[6] = 0x64f98fa7;
  464. context->state[7] = 0xbefa4fa4;
  465. }
  466. /* }}} */
  467. /* {{{ PHP_SHA224Update
  468. SHA224 block update operation. Continues an SHA224 message-digest
  469. operation, processing another message block, and updating the
  470. context.
  471. */
  472. PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX * context, const unsigned char *input, unsigned int inputLen)
  473. {
  474. unsigned int i, index, partLen;
  475. /* Compute number of bytes mod 64 */
  476. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  477. /* Update number of bits */
  478. if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
  479. context->count[1]++;
  480. }
  481. context->count[1] += ((uint32_t) inputLen >> 29);
  482. partLen = 64 - index;
  483. /* Transform as many times as possible.
  484. */
  485. if (inputLen >= partLen) {
  486. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  487. SHA256Transform(context->state, context->buffer);
  488. for (i = partLen; i + 63 < inputLen; i += 64) {
  489. SHA256Transform(context->state, &input[i]);
  490. }
  491. index = 0;
  492. } else {
  493. i = 0;
  494. }
  495. /* Buffer remaining input */
  496. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  497. }
  498. /* }}} */
  499. /* {{{ PHP_SHA224Final
  500. SHA224 finalization. Ends an SHA224 message-digest operation, writing the
  501. the message digest and zeroizing the context.
  502. */
  503. PHP_HASH_API void PHP_SHA224Final(unsigned char digest[28], PHP_SHA224_CTX * context)
  504. {
  505. unsigned char bits[8];
  506. unsigned int index, padLen;
  507. /* Save number of bits */
  508. bits[7] = (unsigned char) (context->count[0] & 0xFF);
  509. bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  510. bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  511. bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  512. bits[3] = (unsigned char) (context->count[1] & 0xFF);
  513. bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  514. bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  515. bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  516. /* Pad out to 56 mod 64.
  517. */
  518. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  519. padLen = (index < 56) ? (56 - index) : (120 - index);
  520. PHP_SHA224Update(context, PADDING, padLen);
  521. /* Append length (before padding) */
  522. PHP_SHA224Update(context, bits, 8);
  523. /* Store state in digest */
  524. SHAEncode32(digest, context->state, 28);
  525. /* Zeroize sensitive information.
  526. */
  527. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  528. }
  529. /* }}} */
  530. /* {{{ PHP_SHA256Update
  531. SHA256 block update operation. Continues an SHA256 message-digest
  532. operation, processing another message block, and updating the
  533. context.
  534. */
  535. PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX * context, const unsigned char *input, unsigned int inputLen)
  536. {
  537. unsigned int i, index, partLen;
  538. /* Compute number of bytes mod 64 */
  539. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  540. /* Update number of bits */
  541. if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
  542. context->count[1]++;
  543. }
  544. context->count[1] += ((uint32_t) inputLen >> 29);
  545. partLen = 64 - index;
  546. /* Transform as many times as possible.
  547. */
  548. if (inputLen >= partLen) {
  549. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  550. SHA256Transform(context->state, context->buffer);
  551. for (i = partLen; i + 63 < inputLen; i += 64) {
  552. SHA256Transform(context->state, &input[i]);
  553. }
  554. index = 0;
  555. } else {
  556. i = 0;
  557. }
  558. /* Buffer remaining input */
  559. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  560. }
  561. /* }}} */
  562. /* {{{ PHP_SHA256Final
  563. SHA256 finalization. Ends an SHA256 message-digest operation, writing the
  564. the message digest and zeroizing the context.
  565. */
  566. PHP_HASH_API void PHP_SHA256Final(unsigned char digest[32], PHP_SHA256_CTX * context)
  567. {
  568. unsigned char bits[8];
  569. unsigned int index, padLen;
  570. /* Save number of bits */
  571. bits[7] = (unsigned char) (context->count[0] & 0xFF);
  572. bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  573. bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  574. bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  575. bits[3] = (unsigned char) (context->count[1] & 0xFF);
  576. bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  577. bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  578. bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  579. /* Pad out to 56 mod 64.
  580. */
  581. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  582. padLen = (index < 56) ? (56 - index) : (120 - index);
  583. PHP_SHA256Update(context, PADDING, padLen);
  584. /* Append length (before padding) */
  585. PHP_SHA256Update(context, bits, 8);
  586. /* Store state in digest */
  587. SHAEncode32(digest, context->state, 32);
  588. /* Zeroize sensitive information.
  589. */
  590. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  591. }
  592. /* }}} */
  593. /* sha384/sha512 */
  594. /* Ch */
  595. #define SHA512_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  596. /* Maj */
  597. #define SHA512_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  598. /* SUM0 */
  599. #define SHA512_F2(x) (ROTR64(28, x) ^ ROTR64(34, x) ^ ROTR64(39, x))
  600. /* SUM1 */
  601. #define SHA512_F3(x) (ROTR64(14, x) ^ ROTR64(18, x) ^ ROTR64(41, x))
  602. /* OM0 */
  603. #define SHA512_F4(x) (ROTR64( 1, x) ^ ROTR64( 8, x) ^ SHR(7, x))
  604. /* OM1 */
  605. #define SHA512_F5(x) (ROTR64(19, x) ^ ROTR64(61, x) ^ SHR(6, x))
  606. static const uint64_t SHA512_K[128] = {
  607. L64(0x428a2f98d728ae22), L64(0x7137449123ef65cd), L64(0xb5c0fbcfec4d3b2f), L64(0xe9b5dba58189dbbc),
  608. L64(0x3956c25bf348b538), L64(0x59f111f1b605d019), L64(0x923f82a4af194f9b), L64(0xab1c5ed5da6d8118),
  609. L64(0xd807aa98a3030242), L64(0x12835b0145706fbe), L64(0x243185be4ee4b28c), L64(0x550c7dc3d5ffb4e2),
  610. L64(0x72be5d74f27b896f), L64(0x80deb1fe3b1696b1), L64(0x9bdc06a725c71235), L64(0xc19bf174cf692694),
  611. L64(0xe49b69c19ef14ad2), L64(0xefbe4786384f25e3), L64(0x0fc19dc68b8cd5b5), L64(0x240ca1cc77ac9c65),
  612. L64(0x2de92c6f592b0275), L64(0x4a7484aa6ea6e483), L64(0x5cb0a9dcbd41fbd4), L64(0x76f988da831153b5),
  613. L64(0x983e5152ee66dfab), L64(0xa831c66d2db43210), L64(0xb00327c898fb213f), L64(0xbf597fc7beef0ee4),
  614. L64(0xc6e00bf33da88fc2), L64(0xd5a79147930aa725), L64(0x06ca6351e003826f), L64(0x142929670a0e6e70),
  615. L64(0x27b70a8546d22ffc), L64(0x2e1b21385c26c926), L64(0x4d2c6dfc5ac42aed), L64(0x53380d139d95b3df),
  616. L64(0x650a73548baf63de), L64(0x766a0abb3c77b2a8), L64(0x81c2c92e47edaee6), L64(0x92722c851482353b),
  617. L64(0xa2bfe8a14cf10364), L64(0xa81a664bbc423001), L64(0xc24b8b70d0f89791), L64(0xc76c51a30654be30),
  618. L64(0xd192e819d6ef5218), L64(0xd69906245565a910), L64(0xf40e35855771202a), L64(0x106aa07032bbd1b8),
  619. L64(0x19a4c116b8d2d0c8), L64(0x1e376c085141ab53), L64(0x2748774cdf8eeb99), L64(0x34b0bcb5e19b48a8),
  620. L64(0x391c0cb3c5c95a63), L64(0x4ed8aa4ae3418acb), L64(0x5b9cca4f7763e373), L64(0x682e6ff3d6b2b8a3),
  621. L64(0x748f82ee5defb2fc), L64(0x78a5636f43172f60), L64(0x84c87814a1f0ab72), L64(0x8cc702081a6439ec),
  622. L64(0x90befffa23631e28), L64(0xa4506cebde82bde9), L64(0xbef9a3f7b2c67915), L64(0xc67178f2e372532b),
  623. L64(0xca273eceea26619c), L64(0xd186b8c721c0c207), L64(0xeada7dd6cde0eb1e), L64(0xf57d4f7fee6ed178),
  624. L64(0x06f067aa72176fba), L64(0x0a637dc5a2c898a6), L64(0x113f9804bef90dae), L64(0x1b710b35131c471b),
  625. L64(0x28db77f523047d84), L64(0x32caab7b40c72493), L64(0x3c9ebe0a15c9bebc), L64(0x431d67c49c100d4c),
  626. L64(0x4cc5d4becb3e42b6), L64(0x597f299cfc657e2a), L64(0x5fcb6fab3ad6faec), L64(0x6c44198c4a475817) };
  627. /* {{{ SHAEncode64
  628. Encodes input (uint64_t) into output (unsigned char). Assumes len is
  629. a multiple of 8.
  630. */
  631. static void SHAEncode64(unsigned char *output, uint64_t *input, unsigned int len)
  632. {
  633. unsigned int i, j;
  634. for (i = 0, j = 0; j < len; i++, j += 8) {
  635. output[j] = (unsigned char) ((input[i] >> 56) & 0xff);
  636. output[j + 1] = (unsigned char) ((input[i] >> 48) & 0xff);
  637. output[j + 2] = (unsigned char) ((input[i] >> 40) & 0xff);
  638. output[j + 3] = (unsigned char) ((input[i] >> 32) & 0xff);
  639. output[j + 4] = (unsigned char) ((input[i] >> 24) & 0xff);
  640. output[j + 5] = (unsigned char) ((input[i] >> 16) & 0xff);
  641. output[j + 6] = (unsigned char) ((input[i] >> 8) & 0xff);
  642. output[j + 7] = (unsigned char) (input[i] & 0xff);
  643. }
  644. }
  645. /* }}} */
  646. /* {{{ SHADecode64
  647. Decodes input (unsigned char) into output (uint64_t). Assumes len is
  648. a multiple of 8.
  649. */
  650. static void SHADecode64(uint64_t *output, const unsigned char *input, unsigned int len)
  651. {
  652. unsigned int i, j;
  653. for (i = 0, j = 0; j < len; i++, j += 8)
  654. output[i] =
  655. ((uint64_t) input[j + 7]) | (((uint64_t) input[j + 6]) << 8) |
  656. (((uint64_t) input[j + 5]) << 16) | (((uint64_t) input[j + 4]) << 24) |
  657. (((uint64_t) input[j + 3]) << 32) | (((uint64_t) input[j + 2]) << 40) |
  658. (((uint64_t) input[j + 1]) << 48) | (((uint64_t) input[j]) << 56);
  659. }
  660. /* }}} */
  661. /* {{{ PHP_SHA384Init
  662. * SHA384 initialization. Begins an SHA384 operation, writing a new context.
  663. */
  664. PHP_HASH_API void PHP_SHA384Init(PHP_SHA384_CTX * context)
  665. {
  666. context->count[0] = context->count[1] = 0;
  667. /* Load magic initialization constants.
  668. */
  669. context->state[0] = L64(0xcbbb9d5dc1059ed8);
  670. context->state[1] = L64(0x629a292a367cd507);
  671. context->state[2] = L64(0x9159015a3070dd17);
  672. context->state[3] = L64(0x152fecd8f70e5939);
  673. context->state[4] = L64(0x67332667ffc00b31);
  674. context->state[5] = L64(0x8eb44a8768581511);
  675. context->state[6] = L64(0xdb0c2e0d64f98fa7);
  676. context->state[7] = L64(0x47b5481dbefa4fa4);
  677. }
  678. /* }}} */
  679. /* {{{ SHA512Transform
  680. * SHA512 basic transformation. Transforms state based on block.
  681. * SHA384 uses the exact same algorithm
  682. */
  683. static void SHA512Transform(uint64_t state[8], const unsigned char block[128])
  684. {
  685. uint64_t a = state[0], b = state[1], c = state[2], d = state[3];
  686. uint64_t e = state[4], f = state[5], g = state[6], h = state[7];
  687. uint64_t x[16], T1, T2, W[80];
  688. int i;
  689. SHADecode64(x, block, 128);
  690. /* Schedule */
  691. for(i = 0; i < 16; i++) {
  692. W[i] = x[i];
  693. }
  694. for(i = 16; i < 80; i++) {
  695. W[i] = SHA512_F5(W[i-2]) + W[i-7] + SHA512_F4(W[i-15]) + W[i-16];
  696. }
  697. for (i = 0; i < 80; i++) {
  698. T1 = h + SHA512_F3(e) + SHA512_F0(e,f,g) + SHA512_K[i] + W[i];
  699. T2 = SHA512_F2(a) + SHA512_F1(a,b,c);
  700. h = g; g = f; f = e; e = d + T1;
  701. d = c; c = b; b = a; a = T1 + T2;
  702. }
  703. state[0] += a;
  704. state[1] += b;
  705. state[2] += c;
  706. state[3] += d;
  707. state[4] += e;
  708. state[5] += f;
  709. state[6] += g;
  710. state[7] += h;
  711. /* Zeroize sensitive information. */
  712. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  713. }
  714. /* }}} */
  715. /* {{{ PHP_SHA384Update
  716. SHA384 block update operation. Continues an SHA384 message-digest
  717. operation, processing another message block, and updating the
  718. context.
  719. */
  720. PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX * context, const unsigned char *input, unsigned int inputLen)
  721. {
  722. unsigned int i, index, partLen;
  723. /* Compute number of bytes mod 128 */
  724. index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
  725. /* Update number of bits */
  726. if ((context->count[0] += ((uint64_t) inputLen << 3)) < ((uint64_t) inputLen << 3)) {
  727. context->count[1]++;
  728. }
  729. context->count[1] += ((uint64_t) inputLen >> 61);
  730. partLen = 128 - index;
  731. /* Transform as many times as possible.
  732. */
  733. if (inputLen >= partLen) {
  734. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  735. SHA512Transform(context->state, context->buffer);
  736. for (i = partLen; i + 127 < inputLen; i += 128) {
  737. SHA512Transform(context->state, &input[i]);
  738. }
  739. index = 0;
  740. } else {
  741. i = 0;
  742. }
  743. /* Buffer remaining input */
  744. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  745. }
  746. /* }}} */
  747. /* {{{ PHP_SHA384Final
  748. SHA384 finalization. Ends an SHA384 message-digest operation, writing the
  749. the message digest and zeroizing the context.
  750. */
  751. PHP_HASH_API void PHP_SHA384Final(unsigned char digest[48], PHP_SHA384_CTX * context)
  752. {
  753. unsigned char bits[16];
  754. unsigned int index, padLen;
  755. /* Save number of bits */
  756. bits[15] = (unsigned char) (context->count[0] & 0xFF);
  757. bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  758. bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  759. bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  760. bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
  761. bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
  762. bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
  763. bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
  764. bits[7] = (unsigned char) (context->count[1] & 0xFF);
  765. bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  766. bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  767. bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  768. bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
  769. bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
  770. bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
  771. bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
  772. /* Pad out to 112 mod 128.
  773. */
  774. index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
  775. padLen = (index < 112) ? (112 - index) : (240 - index);
  776. PHP_SHA384Update(context, PADDING, padLen);
  777. /* Append length (before padding) */
  778. PHP_SHA384Update(context, bits, 16);
  779. /* Store state in digest */
  780. SHAEncode64(digest, context->state, 48);
  781. /* Zeroize sensitive information.
  782. */
  783. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  784. }
  785. /* }}} */
  786. const php_hash_ops php_hash_sha384_ops = {
  787. (php_hash_init_func_t) PHP_SHA384Init,
  788. (php_hash_update_func_t) PHP_SHA384Update,
  789. (php_hash_final_func_t) PHP_SHA384Final,
  790. (php_hash_copy_func_t) php_hash_copy,
  791. 48,
  792. 128,
  793. sizeof(PHP_SHA384_CTX),
  794. 1
  795. };
  796. /* {{{ PHP_SHA512Init
  797. * SHA512 initialization. Begins an SHA512 operation, writing a new context.
  798. */
  799. PHP_HASH_API void PHP_SHA512Init(PHP_SHA512_CTX * context)
  800. {
  801. context->count[0] = context->count[1] = 0;
  802. /* Load magic initialization constants.
  803. */
  804. context->state[0] = L64(0x6a09e667f3bcc908);
  805. context->state[1] = L64(0xbb67ae8584caa73b);
  806. context->state[2] = L64(0x3c6ef372fe94f82b);
  807. context->state[3] = L64(0xa54ff53a5f1d36f1);
  808. context->state[4] = L64(0x510e527fade682d1);
  809. context->state[5] = L64(0x9b05688c2b3e6c1f);
  810. context->state[6] = L64(0x1f83d9abfb41bd6b);
  811. context->state[7] = L64(0x5be0cd19137e2179);
  812. }
  813. /* }}} */
  814. /* {{{ PHP_SHA512_256Init
  815. * SHA512/245 initialization. Identical algorithm to SHA512, using alternate initval and truncation
  816. */
  817. PHP_HASH_API void PHP_SHA512_256Init(PHP_SHA512_CTX * context)
  818. {
  819. context->count[0] = context->count[1] = 0;
  820. context->state[0] = L64(0x22312194FC2BF72C);
  821. context->state[1] = L64(0x9F555FA3C84C64C2);
  822. context->state[2] = L64(0x2393B86B6F53B151);
  823. context->state[3] = L64(0x963877195940EABD);
  824. context->state[4] = L64(0x96283EE2A88EFFE3);
  825. context->state[5] = L64(0xBE5E1E2553863992);
  826. context->state[6] = L64(0x2B0199FC2C85B8AA);
  827. context->state[7] = L64(0x0EB72DDC81C52CA2);
  828. }
  829. /* }}} */
  830. /* {{{ PHP_SHA512_224Init
  831. * SHA512/224 initialization. Identical algorithm to SHA512, using alternate initval and truncation
  832. */
  833. PHP_HASH_API void PHP_SHA512_224Init(PHP_SHA512_CTX * context)
  834. {
  835. context->count[0] = context->count[1] = 0;
  836. context->state[0] = L64(0x8C3D37C819544DA2);
  837. context->state[1] = L64(0x73E1996689DCD4D6);
  838. context->state[2] = L64(0x1DFAB7AE32FF9C82);
  839. context->state[3] = L64(0x679DD514582F9FCF);
  840. context->state[4] = L64(0x0F6D2B697BD44DA8);
  841. context->state[5] = L64(0x77E36F7304C48942);
  842. context->state[6] = L64(0x3F9D85A86A1D36C8);
  843. context->state[7] = L64(0x1112E6AD91D692A1);
  844. }
  845. /* }}} */
  846. /* {{{ PHP_SHA512Update
  847. SHA512 block update operation. Continues an SHA512 message-digest
  848. operation, processing another message block, and updating the
  849. context.
  850. */
  851. PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX * context, const unsigned char *input, unsigned int inputLen)
  852. {
  853. unsigned int i, index, partLen;
  854. /* Compute number of bytes mod 128 */
  855. index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
  856. /* Update number of bits */
  857. if ((context->count[0] += ((uint64_t) inputLen << 3)) < ((uint64_t) inputLen << 3)) {
  858. context->count[1]++;
  859. }
  860. context->count[1] += ((uint64_t) inputLen >> 61);
  861. partLen = 128 - index;
  862. /* Transform as many times as possible.
  863. */
  864. if (inputLen >= partLen) {
  865. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  866. SHA512Transform(context->state, context->buffer);
  867. for (i = partLen; i + 127 < inputLen; i += 128) {
  868. SHA512Transform(context->state, &input[i]);
  869. }
  870. index = 0;
  871. } else {
  872. i = 0;
  873. }
  874. /* Buffer remaining input */
  875. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  876. }
  877. /* }}} */
  878. /* {{{ PHP_SHA512Final
  879. SHA512 finalization. Ends an SHA512 message-digest operation, writing the
  880. the message digest and zeroizing the context.
  881. */
  882. PHP_HASH_API void PHP_SHA512Final(unsigned char digest[64], PHP_SHA512_CTX * context)
  883. {
  884. unsigned char bits[16];
  885. unsigned int index, padLen;
  886. /* Save number of bits */
  887. bits[15] = (unsigned char) (context->count[0] & 0xFF);
  888. bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  889. bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  890. bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  891. bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
  892. bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
  893. bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
  894. bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
  895. bits[7] = (unsigned char) (context->count[1] & 0xFF);
  896. bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  897. bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  898. bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  899. bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
  900. bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
  901. bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
  902. bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
  903. /* Pad out to 112 mod 128.
  904. */
  905. index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
  906. padLen = (index < 112) ? (112 - index) : (240 - index);
  907. PHP_SHA512Update(context, PADDING, padLen);
  908. /* Append length (before padding) */
  909. PHP_SHA512Update(context, bits, 16);
  910. /* Store state in digest */
  911. SHAEncode64(digest, context->state, 64);
  912. /* Zeroize sensitive information.
  913. */
  914. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  915. }
  916. /* }}} */
  917. /* {{{ PHP_SHA512_256Final
  918. SHA512/256 finalization. Identical to SHA512Final, but with truncation
  919. */
  920. PHP_HASH_API void PHP_SHA512_256Final(unsigned char digest[32], PHP_SHA512_CTX * context)
  921. {
  922. unsigned char full_digest[64];
  923. PHP_SHA512Final(full_digest, context);
  924. memcpy(digest, full_digest, 32);
  925. }
  926. /* }}} */
  927. /* {{{ PHP_SHA512_224Final
  928. SHA512/224 finalization. Identical to SHA512Final, but with truncation
  929. */
  930. PHP_HASH_API void PHP_SHA512_224Final(unsigned char digest[28], PHP_SHA512_CTX * context)
  931. {
  932. unsigned char full_digest[64];
  933. PHP_SHA512Final(full_digest, context);
  934. memcpy(digest, full_digest, 28);
  935. }
  936. /* }}} */
  937. const php_hash_ops php_hash_sha512_ops = {
  938. (php_hash_init_func_t) PHP_SHA512Init,
  939. (php_hash_update_func_t) PHP_SHA512Update,
  940. (php_hash_final_func_t) PHP_SHA512Final,
  941. (php_hash_copy_func_t) php_hash_copy,
  942. 64,
  943. 128,
  944. sizeof(PHP_SHA512_CTX),
  945. 1
  946. };
  947. const php_hash_ops php_hash_sha512_256_ops = {
  948. (php_hash_init_func_t) PHP_SHA512_256Init,
  949. (php_hash_update_func_t) PHP_SHA512_256Update,
  950. (php_hash_final_func_t) PHP_SHA512_256Final,
  951. (php_hash_copy_func_t) php_hash_copy,
  952. 32,
  953. 128,
  954. sizeof(PHP_SHA512_CTX),
  955. 1
  956. };
  957. const php_hash_ops php_hash_sha512_224_ops = {
  958. (php_hash_init_func_t) PHP_SHA512_224Init,
  959. (php_hash_update_func_t) PHP_SHA512_224Update,
  960. (php_hash_final_func_t) PHP_SHA512_224Final,
  961. (php_hash_copy_func_t) php_hash_copy,
  962. 28,
  963. 128,
  964. sizeof(PHP_SHA512_CTX),
  965. 1
  966. };
  967. /*
  968. * Local variables:
  969. * tab-width: 4
  970. * c-basic-offset: 4
  971. * End:
  972. * vim600: sw=4 ts=4 fdm=marker
  973. * vim<600: sw=4 ts=4
  974. */