hash_sha.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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: Steffan Esser <sesser@php.net> |
  14. | Sara Golemon <pollita@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. #include "php_hash.h"
  18. #include "php_hash_sha.h"
  19. static const unsigned char PADDING[128] =
  20. {
  21. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  22. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  24. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  25. 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. };
  30. /* {{{ SHAEncode32
  31. Encodes input (uint32_t) into output (unsigned char). Assumes len is
  32. a multiple of 4.
  33. */
  34. static void SHAEncode32(unsigned char *output, uint32_t *input, unsigned int len)
  35. {
  36. unsigned int i, j;
  37. for (i = 0, j = 0; j < len; i++, j += 4) {
  38. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  39. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  40. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  41. output[j + 3] = (unsigned char) (input[i] & 0xff);
  42. }
  43. }
  44. /* }}} */
  45. /* {{{ SHADecode32
  46. Decodes input (unsigned char) into output (uint32_t). Assumes len is
  47. a multiple of 4.
  48. */
  49. static void SHADecode32(uint32_t *output, const unsigned char *input, unsigned int len)
  50. {
  51. unsigned int i, j;
  52. for (i = 0, j = 0; j < len; i++, j += 4)
  53. output[i] = ((uint32_t) input[j + 3]) | (((uint32_t) input[j + 2]) << 8) |
  54. (((uint32_t) input[j + 1]) << 16) | (((uint32_t) input[j]) << 24);
  55. }
  56. /* }}} */
  57. const php_hash_ops php_hash_sha1_ops = {
  58. "sha1",
  59. (php_hash_init_func_t) PHP_SHA1InitArgs,
  60. (php_hash_update_func_t) PHP_SHA1Update,
  61. (php_hash_final_func_t) PHP_SHA1Final,
  62. php_hash_copy,
  63. php_hash_serialize,
  64. php_hash_unserialize,
  65. PHP_SHA1_SPEC,
  66. 20,
  67. 64,
  68. sizeof(PHP_SHA1_CTX),
  69. 1
  70. };
  71. /* sha224/sha256 */
  72. const php_hash_ops php_hash_sha256_ops = {
  73. "sha256",
  74. (php_hash_init_func_t) PHP_SHA256InitArgs,
  75. (php_hash_update_func_t) PHP_SHA256Update,
  76. (php_hash_final_func_t) PHP_SHA256Final,
  77. php_hash_copy,
  78. php_hash_serialize,
  79. php_hash_unserialize,
  80. PHP_SHA256_SPEC,
  81. 32,
  82. 64,
  83. sizeof(PHP_SHA256_CTX),
  84. 1
  85. };
  86. const php_hash_ops php_hash_sha224_ops = {
  87. "sha224",
  88. (php_hash_init_func_t) PHP_SHA224InitArgs,
  89. (php_hash_update_func_t) PHP_SHA224Update,
  90. (php_hash_final_func_t) PHP_SHA224Final,
  91. php_hash_copy,
  92. php_hash_serialize,
  93. php_hash_unserialize,
  94. PHP_SHA224_SPEC,
  95. 28,
  96. 64,
  97. sizeof(PHP_SHA224_CTX),
  98. 1
  99. };
  100. #define ROTR32(b,x) ((x >> b) | (x << (32 - b)))
  101. #define ROTR64(b,x) ((x >> b) | (x << (64 - b)))
  102. #define SHR(b, x) (x >> b)
  103. /* Ch */
  104. #define SHA256_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  105. /* Maj */
  106. #define SHA256_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  107. /* SUM0 */
  108. #define SHA256_F2(x) (ROTR32( 2,(x)) ^ ROTR32(13,(x)) ^ ROTR32(22,(x)))
  109. /* SUM1 */
  110. #define SHA256_F3(x) (ROTR32( 6,(x)) ^ ROTR32(11,(x)) ^ ROTR32(25,(x)))
  111. /* OM0 */
  112. #define SHA256_F4(x) (ROTR32( 7,(x)) ^ ROTR32(18,(x)) ^ SHR( 3,(x)))
  113. /* OM1 */
  114. #define SHA256_F5(x) (ROTR32(17,(x)) ^ ROTR32(19,(x)) ^ SHR(10,(x)))
  115. static const uint32_t SHA256_K[64] = {
  116. 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
  117. 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
  118. 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
  119. 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
  120. 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
  121. 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
  122. 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
  123. 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2 };
  124. /* {{{ PHP_SHA256InitArgs
  125. * SHA256 initialization. Begins an SHA256 operation, writing a new context.
  126. */
  127. PHP_HASH_API void PHP_SHA256InitArgs(PHP_SHA256_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  128. {
  129. context->count[0] = context->count[1] = 0;
  130. /* Load magic initialization constants.
  131. */
  132. context->state[0] = 0x6a09e667;
  133. context->state[1] = 0xbb67ae85;
  134. context->state[2] = 0x3c6ef372;
  135. context->state[3] = 0xa54ff53a;
  136. context->state[4] = 0x510e527f;
  137. context->state[5] = 0x9b05688c;
  138. context->state[6] = 0x1f83d9ab;
  139. context->state[7] = 0x5be0cd19;
  140. }
  141. /* }}} */
  142. /* {{{ SHA256Transform
  143. * SHA256 basic transformation. Transforms state based on block.
  144. */
  145. static void SHA256Transform(uint32_t state[8], const unsigned char block[64])
  146. {
  147. uint32_t a = state[0], b = state[1], c = state[2], d = state[3];
  148. uint32_t e = state[4], f = state[5], g = state[6], h = state[7];
  149. uint32_t x[16], T1, T2, W[64];
  150. int i;
  151. SHADecode32(x, block, 64);
  152. /* Schedule */
  153. for(i = 0; i < 16; i++) {
  154. W[i] = x[i];
  155. }
  156. for(i = 16; i < 64; i++) {
  157. W[i] = SHA256_F5(W[i-2]) + W[i-7] + SHA256_F4(W[i-15]) + W[i-16];
  158. }
  159. for (i = 0; i < 64; i++) {
  160. T1 = h + SHA256_F3(e) + SHA256_F0(e,f,g) + SHA256_K[i] + W[i];
  161. T2 = SHA256_F2(a) + SHA256_F1(a,b,c);
  162. h = g; g = f; f = e; e = d + T1;
  163. d = c; c = b; b = a; a = T1 + T2;
  164. }
  165. state[0] += a;
  166. state[1] += b;
  167. state[2] += c;
  168. state[3] += d;
  169. state[4] += e;
  170. state[5] += f;
  171. state[6] += g;
  172. state[7] += h;
  173. /* Zeroize sensitive information. */
  174. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  175. }
  176. /* }}} */
  177. /* {{{ PHP_SHA224InitArgs
  178. * SHA224 initialization. Begins an SHA224 operation, writing a new context.
  179. */
  180. PHP_HASH_API void PHP_SHA224InitArgs(PHP_SHA224_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  181. {
  182. context->count[0] = context->count[1] = 0;
  183. /* Load magic initialization constants.
  184. */
  185. context->state[0] = 0xc1059ed8;
  186. context->state[1] = 0x367cd507;
  187. context->state[2] = 0x3070dd17;
  188. context->state[3] = 0xf70e5939;
  189. context->state[4] = 0xffc00b31;
  190. context->state[5] = 0x68581511;
  191. context->state[6] = 0x64f98fa7;
  192. context->state[7] = 0xbefa4fa4;
  193. }
  194. /* }}} */
  195. /* {{{ PHP_SHA224Update
  196. SHA224 block update operation. Continues an SHA224 message-digest
  197. operation, processing another message block, and updating the
  198. context.
  199. */
  200. PHP_HASH_API void PHP_SHA224Update(PHP_SHA224_CTX * context, const unsigned char *input, size_t inputLen)
  201. {
  202. unsigned int i, index, partLen;
  203. /* Compute number of bytes mod 64 */
  204. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  205. /* Update number of bits */
  206. if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
  207. context->count[1]++;
  208. }
  209. context->count[1] += ((uint32_t) inputLen >> 29);
  210. partLen = 64 - index;
  211. /* Transform as many times as possible.
  212. */
  213. if (inputLen >= partLen) {
  214. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  215. SHA256Transform(context->state, context->buffer);
  216. for (i = partLen; i + 63 < inputLen; i += 64) {
  217. SHA256Transform(context->state, &input[i]);
  218. }
  219. index = 0;
  220. } else {
  221. i = 0;
  222. }
  223. /* Buffer remaining input */
  224. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  225. }
  226. /* }}} */
  227. /* {{{ PHP_SHA224Final
  228. SHA224 finalization. Ends an SHA224 message-digest operation, writing the
  229. the message digest and zeroizing the context.
  230. */
  231. PHP_HASH_API void PHP_SHA224Final(unsigned char digest[28], PHP_SHA224_CTX * context)
  232. {
  233. unsigned char bits[8];
  234. unsigned int index, padLen;
  235. /* Save number of bits */
  236. bits[7] = (unsigned char) (context->count[0] & 0xFF);
  237. bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  238. bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  239. bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  240. bits[3] = (unsigned char) (context->count[1] & 0xFF);
  241. bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  242. bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  243. bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  244. /* Pad out to 56 mod 64.
  245. */
  246. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  247. padLen = (index < 56) ? (56 - index) : (120 - index);
  248. PHP_SHA224Update(context, PADDING, padLen);
  249. /* Append length (before padding) */
  250. PHP_SHA224Update(context, bits, 8);
  251. /* Store state in digest */
  252. SHAEncode32(digest, context->state, 28);
  253. /* Zeroize sensitive information.
  254. */
  255. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  256. }
  257. /* }}} */
  258. /* {{{ PHP_SHA256Update
  259. SHA256 block update operation. Continues an SHA256 message-digest
  260. operation, processing another message block, and updating the
  261. context.
  262. */
  263. PHP_HASH_API void PHP_SHA256Update(PHP_SHA256_CTX * context, const unsigned char *input, size_t inputLen)
  264. {
  265. unsigned int i, index, partLen;
  266. /* Compute number of bytes mod 64 */
  267. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  268. /* Update number of bits */
  269. if ((context->count[0] += ((uint32_t) inputLen << 3)) < ((uint32_t) inputLen << 3)) {
  270. context->count[1]++;
  271. }
  272. context->count[1] += ((uint32_t) inputLen >> 29);
  273. partLen = 64 - index;
  274. /* Transform as many times as possible.
  275. */
  276. if (inputLen >= partLen) {
  277. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  278. SHA256Transform(context->state, context->buffer);
  279. for (i = partLen; i + 63 < inputLen; i += 64) {
  280. SHA256Transform(context->state, &input[i]);
  281. }
  282. index = 0;
  283. } else {
  284. i = 0;
  285. }
  286. /* Buffer remaining input */
  287. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  288. }
  289. /* }}} */
  290. /* {{{ PHP_SHA256Final
  291. SHA256 finalization. Ends an SHA256 message-digest operation, writing the
  292. the message digest and zeroizing the context.
  293. */
  294. PHP_HASH_API void PHP_SHA256Final(unsigned char digest[32], PHP_SHA256_CTX * context)
  295. {
  296. unsigned char bits[8];
  297. unsigned int index, padLen;
  298. /* Save number of bits */
  299. bits[7] = (unsigned char) (context->count[0] & 0xFF);
  300. bits[6] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  301. bits[5] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  302. bits[4] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  303. bits[3] = (unsigned char) (context->count[1] & 0xFF);
  304. bits[2] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  305. bits[1] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  306. bits[0] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  307. /* Pad out to 56 mod 64.
  308. */
  309. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  310. padLen = (index < 56) ? (56 - index) : (120 - index);
  311. PHP_SHA256Update(context, PADDING, padLen);
  312. /* Append length (before padding) */
  313. PHP_SHA256Update(context, bits, 8);
  314. /* Store state in digest */
  315. SHAEncode32(digest, context->state, 32);
  316. /* Zeroize sensitive information.
  317. */
  318. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  319. }
  320. /* }}} */
  321. /* sha384/sha512 */
  322. /* Ch */
  323. #define SHA512_F0(x,y,z) (((x) & (y)) ^ ((~(x)) & (z)))
  324. /* Maj */
  325. #define SHA512_F1(x,y,z) (((x) & (y)) ^ ((x) & (z)) ^ ((y) & (z)))
  326. /* SUM0 */
  327. #define SHA512_F2(x) (ROTR64(28, x) ^ ROTR64(34, x) ^ ROTR64(39, x))
  328. /* SUM1 */
  329. #define SHA512_F3(x) (ROTR64(14, x) ^ ROTR64(18, x) ^ ROTR64(41, x))
  330. /* OM0 */
  331. #define SHA512_F4(x) (ROTR64( 1, x) ^ ROTR64( 8, x) ^ SHR(7, x))
  332. /* OM1 */
  333. #define SHA512_F5(x) (ROTR64(19, x) ^ ROTR64(61, x) ^ SHR(6, x))
  334. static const uint64_t SHA512_K[128] = {
  335. L64(0x428a2f98d728ae22), L64(0x7137449123ef65cd), L64(0xb5c0fbcfec4d3b2f), L64(0xe9b5dba58189dbbc),
  336. L64(0x3956c25bf348b538), L64(0x59f111f1b605d019), L64(0x923f82a4af194f9b), L64(0xab1c5ed5da6d8118),
  337. L64(0xd807aa98a3030242), L64(0x12835b0145706fbe), L64(0x243185be4ee4b28c), L64(0x550c7dc3d5ffb4e2),
  338. L64(0x72be5d74f27b896f), L64(0x80deb1fe3b1696b1), L64(0x9bdc06a725c71235), L64(0xc19bf174cf692694),
  339. L64(0xe49b69c19ef14ad2), L64(0xefbe4786384f25e3), L64(0x0fc19dc68b8cd5b5), L64(0x240ca1cc77ac9c65),
  340. L64(0x2de92c6f592b0275), L64(0x4a7484aa6ea6e483), L64(0x5cb0a9dcbd41fbd4), L64(0x76f988da831153b5),
  341. L64(0x983e5152ee66dfab), L64(0xa831c66d2db43210), L64(0xb00327c898fb213f), L64(0xbf597fc7beef0ee4),
  342. L64(0xc6e00bf33da88fc2), L64(0xd5a79147930aa725), L64(0x06ca6351e003826f), L64(0x142929670a0e6e70),
  343. L64(0x27b70a8546d22ffc), L64(0x2e1b21385c26c926), L64(0x4d2c6dfc5ac42aed), L64(0x53380d139d95b3df),
  344. L64(0x650a73548baf63de), L64(0x766a0abb3c77b2a8), L64(0x81c2c92e47edaee6), L64(0x92722c851482353b),
  345. L64(0xa2bfe8a14cf10364), L64(0xa81a664bbc423001), L64(0xc24b8b70d0f89791), L64(0xc76c51a30654be30),
  346. L64(0xd192e819d6ef5218), L64(0xd69906245565a910), L64(0xf40e35855771202a), L64(0x106aa07032bbd1b8),
  347. L64(0x19a4c116b8d2d0c8), L64(0x1e376c085141ab53), L64(0x2748774cdf8eeb99), L64(0x34b0bcb5e19b48a8),
  348. L64(0x391c0cb3c5c95a63), L64(0x4ed8aa4ae3418acb), L64(0x5b9cca4f7763e373), L64(0x682e6ff3d6b2b8a3),
  349. L64(0x748f82ee5defb2fc), L64(0x78a5636f43172f60), L64(0x84c87814a1f0ab72), L64(0x8cc702081a6439ec),
  350. L64(0x90befffa23631e28), L64(0xa4506cebde82bde9), L64(0xbef9a3f7b2c67915), L64(0xc67178f2e372532b),
  351. L64(0xca273eceea26619c), L64(0xd186b8c721c0c207), L64(0xeada7dd6cde0eb1e), L64(0xf57d4f7fee6ed178),
  352. L64(0x06f067aa72176fba), L64(0x0a637dc5a2c898a6), L64(0x113f9804bef90dae), L64(0x1b710b35131c471b),
  353. L64(0x28db77f523047d84), L64(0x32caab7b40c72493), L64(0x3c9ebe0a15c9bebc), L64(0x431d67c49c100d4c),
  354. L64(0x4cc5d4becb3e42b6), L64(0x597f299cfc657e2a), L64(0x5fcb6fab3ad6faec), L64(0x6c44198c4a475817) };
  355. /* {{{ SHAEncode64
  356. Encodes input (uint64_t) into output (unsigned char). Assumes len is
  357. a multiple of 8.
  358. */
  359. static void SHAEncode64(unsigned char *output, uint64_t *input, unsigned int len)
  360. {
  361. unsigned int i, j;
  362. for (i = 0, j = 0; j < len; i++, j += 8) {
  363. output[j] = (unsigned char) ((input[i] >> 56) & 0xff);
  364. output[j + 1] = (unsigned char) ((input[i] >> 48) & 0xff);
  365. output[j + 2] = (unsigned char) ((input[i] >> 40) & 0xff);
  366. output[j + 3] = (unsigned char) ((input[i] >> 32) & 0xff);
  367. output[j + 4] = (unsigned char) ((input[i] >> 24) & 0xff);
  368. output[j + 5] = (unsigned char) ((input[i] >> 16) & 0xff);
  369. output[j + 6] = (unsigned char) ((input[i] >> 8) & 0xff);
  370. output[j + 7] = (unsigned char) (input[i] & 0xff);
  371. }
  372. }
  373. /* }}} */
  374. /* {{{ SHADecode64
  375. Decodes input (unsigned char) into output (uint64_t). Assumes len is
  376. a multiple of 8.
  377. */
  378. static void SHADecode64(uint64_t *output, const unsigned char *input, unsigned int len)
  379. {
  380. unsigned int i, j;
  381. for (i = 0, j = 0; j < len; i++, j += 8)
  382. output[i] =
  383. ((uint64_t) input[j + 7]) | (((uint64_t) input[j + 6]) << 8) |
  384. (((uint64_t) input[j + 5]) << 16) | (((uint64_t) input[j + 4]) << 24) |
  385. (((uint64_t) input[j + 3]) << 32) | (((uint64_t) input[j + 2]) << 40) |
  386. (((uint64_t) input[j + 1]) << 48) | (((uint64_t) input[j]) << 56);
  387. }
  388. /* }}} */
  389. /* {{{ PHP_SHA384InitArgs
  390. * SHA384 initialization. Begins an SHA384 operation, writing a new context.
  391. */
  392. PHP_HASH_API void PHP_SHA384InitArgs(PHP_SHA384_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  393. {
  394. context->count[0] = context->count[1] = 0;
  395. /* Load magic initialization constants.
  396. */
  397. context->state[0] = L64(0xcbbb9d5dc1059ed8);
  398. context->state[1] = L64(0x629a292a367cd507);
  399. context->state[2] = L64(0x9159015a3070dd17);
  400. context->state[3] = L64(0x152fecd8f70e5939);
  401. context->state[4] = L64(0x67332667ffc00b31);
  402. context->state[5] = L64(0x8eb44a8768581511);
  403. context->state[6] = L64(0xdb0c2e0d64f98fa7);
  404. context->state[7] = L64(0x47b5481dbefa4fa4);
  405. }
  406. /* }}} */
  407. /* {{{ SHA512Transform
  408. * SHA512 basic transformation. Transforms state based on block.
  409. * SHA384 uses the exact same algorithm
  410. */
  411. static void SHA512Transform(uint64_t state[8], const unsigned char block[128])
  412. {
  413. uint64_t a = state[0], b = state[1], c = state[2], d = state[3];
  414. uint64_t e = state[4], f = state[5], g = state[6], h = state[7];
  415. uint64_t x[16], T1, T2, W[80];
  416. int i;
  417. SHADecode64(x, block, 128);
  418. /* Schedule */
  419. for(i = 0; i < 16; i++) {
  420. W[i] = x[i];
  421. }
  422. for(i = 16; i < 80; i++) {
  423. W[i] = SHA512_F5(W[i-2]) + W[i-7] + SHA512_F4(W[i-15]) + W[i-16];
  424. }
  425. for (i = 0; i < 80; i++) {
  426. T1 = h + SHA512_F3(e) + SHA512_F0(e,f,g) + SHA512_K[i] + W[i];
  427. T2 = SHA512_F2(a) + SHA512_F1(a,b,c);
  428. h = g; g = f; f = e; e = d + T1;
  429. d = c; c = b; b = a; a = T1 + T2;
  430. }
  431. state[0] += a;
  432. state[1] += b;
  433. state[2] += c;
  434. state[3] += d;
  435. state[4] += e;
  436. state[5] += f;
  437. state[6] += g;
  438. state[7] += h;
  439. /* Zeroize sensitive information. */
  440. ZEND_SECURE_ZERO((unsigned char*) x, sizeof(x));
  441. }
  442. /* }}} */
  443. /* {{{ PHP_SHA384Update
  444. SHA384 block update operation. Continues an SHA384 message-digest
  445. operation, processing another message block, and updating the
  446. context.
  447. */
  448. PHP_HASH_API void PHP_SHA384Update(PHP_SHA384_CTX * context, const unsigned char *input, size_t inputLen)
  449. {
  450. unsigned int i = 0, index, partLen;
  451. /* Compute number of bytes mod 128 */
  452. index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
  453. /* Update number of bits */
  454. if ((context->count[0] += ((uint64_t) inputLen << 3)) < ((uint64_t) inputLen << 3)) {
  455. context->count[1]++;
  456. }
  457. context->count[1] += ((uint64_t) inputLen >> 61);
  458. partLen = 128 - index;
  459. /* Transform as many times as possible.
  460. */
  461. if (inputLen >= partLen) {
  462. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  463. SHA512Transform(context->state, context->buffer);
  464. for (i = partLen; i + 127 < inputLen; i += 128) {
  465. SHA512Transform(context->state, &input[i]);
  466. }
  467. index = 0;
  468. }
  469. /* Buffer remaining input */
  470. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  471. }
  472. /* }}} */
  473. /* {{{ PHP_SHA384Final
  474. SHA384 finalization. Ends an SHA384 message-digest operation, writing the
  475. the message digest and zeroizing the context.
  476. */
  477. PHP_HASH_API void PHP_SHA384Final(unsigned char digest[48], PHP_SHA384_CTX * context)
  478. {
  479. unsigned char bits[16];
  480. unsigned int index, padLen;
  481. /* Save number of bits */
  482. bits[15] = (unsigned char) (context->count[0] & 0xFF);
  483. bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  484. bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  485. bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  486. bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
  487. bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
  488. bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
  489. bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
  490. bits[7] = (unsigned char) (context->count[1] & 0xFF);
  491. bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  492. bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  493. bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  494. bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
  495. bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
  496. bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
  497. bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
  498. /* Pad out to 112 mod 128.
  499. */
  500. index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
  501. padLen = (index < 112) ? (112 - index) : (240 - index);
  502. PHP_SHA384Update(context, PADDING, padLen);
  503. /* Append length (before padding) */
  504. PHP_SHA384Update(context, bits, 16);
  505. /* Store state in digest */
  506. SHAEncode64(digest, context->state, 48);
  507. /* Zeroize sensitive information.
  508. */
  509. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  510. }
  511. /* }}} */
  512. const php_hash_ops php_hash_sha384_ops = {
  513. "sha384",
  514. (php_hash_init_func_t) PHP_SHA384InitArgs,
  515. (php_hash_update_func_t) PHP_SHA384Update,
  516. (php_hash_final_func_t) PHP_SHA384Final,
  517. php_hash_copy,
  518. php_hash_serialize,
  519. php_hash_unserialize,
  520. PHP_SHA384_SPEC,
  521. 48,
  522. 128,
  523. sizeof(PHP_SHA384_CTX),
  524. 1
  525. };
  526. /* {{{ PHP_SHA512InitArgs
  527. * SHA512 initialization. Begins an SHA512 operation, writing a new context.
  528. */
  529. PHP_HASH_API void PHP_SHA512InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  530. {
  531. context->count[0] = context->count[1] = 0;
  532. /* Load magic initialization constants.
  533. */
  534. context->state[0] = L64(0x6a09e667f3bcc908);
  535. context->state[1] = L64(0xbb67ae8584caa73b);
  536. context->state[2] = L64(0x3c6ef372fe94f82b);
  537. context->state[3] = L64(0xa54ff53a5f1d36f1);
  538. context->state[4] = L64(0x510e527fade682d1);
  539. context->state[5] = L64(0x9b05688c2b3e6c1f);
  540. context->state[6] = L64(0x1f83d9abfb41bd6b);
  541. context->state[7] = L64(0x5be0cd19137e2179);
  542. }
  543. /* }}} */
  544. /* {{{ PHP_SHA512_256InitArgs
  545. * SHA512/245 initialization. Identical algorithm to SHA512, using alternate initval and truncation
  546. */
  547. PHP_HASH_API void PHP_SHA512_256InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  548. {
  549. context->count[0] = context->count[1] = 0;
  550. context->state[0] = L64(0x22312194FC2BF72C);
  551. context->state[1] = L64(0x9F555FA3C84C64C2);
  552. context->state[2] = L64(0x2393B86B6F53B151);
  553. context->state[3] = L64(0x963877195940EABD);
  554. context->state[4] = L64(0x96283EE2A88EFFE3);
  555. context->state[5] = L64(0xBE5E1E2553863992);
  556. context->state[6] = L64(0x2B0199FC2C85B8AA);
  557. context->state[7] = L64(0x0EB72DDC81C52CA2);
  558. }
  559. /* }}} */
  560. /* {{{ PHP_SHA512_224InitArgs
  561. * SHA512/224 initialization. Identical algorithm to SHA512, using alternate initval and truncation
  562. */
  563. PHP_HASH_API void PHP_SHA512_224InitArgs(PHP_SHA512_CTX * context, ZEND_ATTRIBUTE_UNUSED HashTable *args)
  564. {
  565. context->count[0] = context->count[1] = 0;
  566. context->state[0] = L64(0x8C3D37C819544DA2);
  567. context->state[1] = L64(0x73E1996689DCD4D6);
  568. context->state[2] = L64(0x1DFAB7AE32FF9C82);
  569. context->state[3] = L64(0x679DD514582F9FCF);
  570. context->state[4] = L64(0x0F6D2B697BD44DA8);
  571. context->state[5] = L64(0x77E36F7304C48942);
  572. context->state[6] = L64(0x3F9D85A86A1D36C8);
  573. context->state[7] = L64(0x1112E6AD91D692A1);
  574. }
  575. /* }}} */
  576. /* {{{ PHP_SHA512Update
  577. SHA512 block update operation. Continues an SHA512 message-digest
  578. operation, processing another message block, and updating the
  579. context.
  580. */
  581. PHP_HASH_API void PHP_SHA512Update(PHP_SHA512_CTX * context, const unsigned char *input, size_t inputLen)
  582. {
  583. unsigned int i, index, partLen;
  584. /* Compute number of bytes mod 128 */
  585. index = (unsigned int) ((context->count[0] >> 3) & 0x7F);
  586. /* Update number of bits */
  587. if ((context->count[0] += ((uint64_t) inputLen << 3)) < ((uint64_t) inputLen << 3)) {
  588. context->count[1]++;
  589. }
  590. context->count[1] += ((uint64_t) inputLen >> 61);
  591. partLen = 128 - index;
  592. /* Transform as many times as possible.
  593. */
  594. if (inputLen >= partLen) {
  595. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  596. SHA512Transform(context->state, context->buffer);
  597. for (i = partLen; i + 127 < inputLen; i += 128) {
  598. SHA512Transform(context->state, &input[i]);
  599. }
  600. index = 0;
  601. } else {
  602. i = 0;
  603. }
  604. /* Buffer remaining input */
  605. memcpy((unsigned char*) & context->buffer[index], (unsigned char*) & input[i], inputLen - i);
  606. }
  607. /* }}} */
  608. /* {{{ PHP_SHA512Final
  609. SHA512 finalization. Ends an SHA512 message-digest operation, writing the
  610. the message digest and zeroizing the context.
  611. */
  612. PHP_HASH_API void PHP_SHA512Final(unsigned char digest[64], PHP_SHA512_CTX * context)
  613. {
  614. unsigned char bits[16];
  615. unsigned int index, padLen;
  616. /* Save number of bits */
  617. bits[15] = (unsigned char) (context->count[0] & 0xFF);
  618. bits[14] = (unsigned char) ((context->count[0] >> 8) & 0xFF);
  619. bits[13] = (unsigned char) ((context->count[0] >> 16) & 0xFF);
  620. bits[12] = (unsigned char) ((context->count[0] >> 24) & 0xFF);
  621. bits[11] = (unsigned char) ((context->count[0] >> 32) & 0xFF);
  622. bits[10] = (unsigned char) ((context->count[0] >> 40) & 0xFF);
  623. bits[9] = (unsigned char) ((context->count[0] >> 48) & 0xFF);
  624. bits[8] = (unsigned char) ((context->count[0] >> 56) & 0xFF);
  625. bits[7] = (unsigned char) (context->count[1] & 0xFF);
  626. bits[6] = (unsigned char) ((context->count[1] >> 8) & 0xFF);
  627. bits[5] = (unsigned char) ((context->count[1] >> 16) & 0xFF);
  628. bits[4] = (unsigned char) ((context->count[1] >> 24) & 0xFF);
  629. bits[3] = (unsigned char) ((context->count[1] >> 32) & 0xFF);
  630. bits[2] = (unsigned char) ((context->count[1] >> 40) & 0xFF);
  631. bits[1] = (unsigned char) ((context->count[1] >> 48) & 0xFF);
  632. bits[0] = (unsigned char) ((context->count[1] >> 56) & 0xFF);
  633. /* Pad out to 112 mod 128.
  634. */
  635. index = (unsigned int) ((context->count[0] >> 3) & 0x7f);
  636. padLen = (index < 112) ? (112 - index) : (240 - index);
  637. PHP_SHA512Update(context, PADDING, padLen);
  638. /* Append length (before padding) */
  639. PHP_SHA512Update(context, bits, 16);
  640. /* Store state in digest */
  641. SHAEncode64(digest, context->state, 64);
  642. /* Zeroize sensitive information.
  643. */
  644. ZEND_SECURE_ZERO((unsigned char*) context, sizeof(*context));
  645. }
  646. /* }}} */
  647. /* {{{ PHP_SHA512_256Final
  648. SHA512/256 finalization. Identical to SHA512Final, but with truncation
  649. */
  650. PHP_HASH_API void PHP_SHA512_256Final(unsigned char digest[32], PHP_SHA512_CTX * context)
  651. {
  652. unsigned char full_digest[64];
  653. PHP_SHA512Final(full_digest, context);
  654. memcpy(digest, full_digest, 32);
  655. }
  656. /* }}} */
  657. /* {{{ PHP_SHA512_224Final
  658. SHA512/224 finalization. Identical to SHA512Final, but with truncation
  659. */
  660. PHP_HASH_API void PHP_SHA512_224Final(unsigned char digest[28], PHP_SHA512_CTX * context)
  661. {
  662. unsigned char full_digest[64];
  663. PHP_SHA512Final(full_digest, context);
  664. memcpy(digest, full_digest, 28);
  665. }
  666. /* }}} */
  667. const php_hash_ops php_hash_sha512_ops = {
  668. "sha512",
  669. (php_hash_init_func_t) PHP_SHA512InitArgs,
  670. (php_hash_update_func_t) PHP_SHA512Update,
  671. (php_hash_final_func_t) PHP_SHA512Final,
  672. php_hash_copy,
  673. php_hash_serialize,
  674. php_hash_unserialize,
  675. PHP_SHA512_SPEC,
  676. 64,
  677. 128,
  678. sizeof(PHP_SHA512_CTX),
  679. 1
  680. };
  681. const php_hash_ops php_hash_sha512_256_ops = {
  682. "sha512/256",
  683. (php_hash_init_func_t) PHP_SHA512_256InitArgs,
  684. (php_hash_update_func_t) PHP_SHA512_256Update,
  685. (php_hash_final_func_t) PHP_SHA512_256Final,
  686. php_hash_copy,
  687. php_hash_serialize,
  688. php_hash_unserialize,
  689. PHP_SHA512_SPEC,
  690. 32,
  691. 128,
  692. sizeof(PHP_SHA512_CTX),
  693. 1
  694. };
  695. const php_hash_ops php_hash_sha512_224_ops = {
  696. "sha512/224",
  697. (php_hash_init_func_t) PHP_SHA512_224InitArgs,
  698. (php_hash_update_func_t) PHP_SHA512_224Update,
  699. (php_hash_final_func_t) PHP_SHA512_224Final,
  700. php_hash_copy,
  701. php_hash_serialize,
  702. php_hash_unserialize,
  703. PHP_SHA512_SPEC,
  704. 28,
  705. 128,
  706. sizeof(PHP_SHA512_CTX),
  707. 1
  708. };