PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/hash/hash_sha.c

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