PageRenderTime 47ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/2.0/Source/ExtSupport/PHP4/Digest.cpp

#
C++ | 689 lines | 411 code | 89 blank | 189 comment | 12 complexity | 45d810fa7e77687f2f37ebbd7b451371 MD5 | raw file
Possible License(s): CPL-1.0, GPL-2.0, CC-BY-SA-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. //
  2. // ExtSupport.PHP4 - substitute for php4ts.dll
  3. //
  4. // Digest.cpp
  5. // - slightly modified md5.c and sha1.c, originally PHP source files
  6. //
  7. /*
  8. +----------------------------------------------------------------------+
  9. | PHP Version 4 |
  10. +----------------------------------------------------------------------+
  11. | Copyright (c) 1997-2003 The PHP Group |
  12. +----------------------------------------------------------------------+
  13. | This source file is subject to version 3.0 of the PHP license, |
  14. | that is bundled with this package in the file LICENSE, and is |
  15. | available through the world-wide-web at the following url: |
  16. | http://www.php.net/license/3_0.txt. |
  17. | If you did not receive a copy of the PHP license and are unable to |
  18. | obtain it through the world-wide-web, please send a note to |
  19. | license@php.net so we can mail you a copy immediately. |
  20. +----------------------------------------------------------------------+
  21. | Author: Lachlan Roche |
  22. +----------------------------------------------------------------------+
  23. */
  24. /* $Id: Digest.cpp,v 1.1.2.2 2006/04/15 20:19:12 prosl0am Exp $ */
  25. /*
  26. * md5.c - Copyright 1997 Lachlan Roche
  27. * md5_file() added by Alessandro Astarita <aleast@capri.it>
  28. */
  29. #include "stdafx.h"
  30. #include <stdio.h>
  31. //#include "php.h"
  32. #include "Digest.h"
  33. #pragma unmanaged
  34. ZEND_API void make_digest(char *md5str, unsigned char *digest)
  35. {
  36. int i;
  37. for (i = 0; i < 16; i++) {
  38. sprintf(md5str, "%02x", digest[i]);
  39. md5str += 2;
  40. }
  41. *md5str = '\0';
  42. }
  43. /*
  44. * The remaining code is the reference MD5 code (md5c.c) from rfc1321
  45. */
  46. /* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  47. */
  48. /* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  49. rights reserved.
  50. License to copy and use this software is granted provided that it
  51. is identified as the "RSA Data Security, Inc. MD5 Message-Digest
  52. Algorithm" in all material mentioning or referencing this software
  53. or this function.
  54. License is also granted to make and use derivative works provided
  55. that such works are identified as "derived from the RSA Data
  56. Security, Inc. MD5 Message-Digest Algorithm" in all material
  57. mentioning or referencing the derived work.
  58. RSA Data Security, Inc. makes no representations concerning either
  59. the merchantability of this software or the suitability of this
  60. software for any particular purpose. It is provided "as is"
  61. without express or implied warranty of any kind.
  62. These notices must be retained in any copies of any part of this
  63. documentation and/or software.
  64. */
  65. /* Constants for MD5Transform routine.
  66. */
  67. #define S11 7
  68. #define S12 12
  69. #define S13 17
  70. #define S14 22
  71. #define S21 5
  72. #define S22 9
  73. #define S23 14
  74. #define S24 20
  75. #define S31 4
  76. #define S32 11
  77. #define S33 16
  78. #define S34 23
  79. #define S41 6
  80. #define S42 10
  81. #define S43 15
  82. #define S44 21
  83. static void MD5Transform(php_uint32[4], const unsigned char[64]);
  84. static void Encode(unsigned char *, php_uint32 *, unsigned int);
  85. static void Decode(php_uint32 *, const unsigned char *, unsigned int);
  86. static unsigned char PADDING[64] =
  87. {
  88. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  89. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  90. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  91. };
  92. /* F, G, H and I are basic MD5 functions.
  93. */
  94. #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
  95. #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
  96. #define H(x, y, z) ((x) ^ (y) ^ (z))
  97. #define I(x, y, z) ((y) ^ ((x) | (~z)))
  98. /* ROTATE_LEFT rotates x left n bits.
  99. */
  100. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  101. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  102. Rotation is separate from addition to prevent recomputation.
  103. */
  104. #define FF(a, b, c, d, x, s, ac) { \
  105. (a) += F ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  106. (a) = ROTATE_LEFT ((a), (s)); \
  107. (a) += (b); \
  108. }
  109. #define GG(a, b, c, d, x, s, ac) { \
  110. (a) += G ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  111. (a) = ROTATE_LEFT ((a), (s)); \
  112. (a) += (b); \
  113. }
  114. #define HH(a, b, c, d, x, s, ac) { \
  115. (a) += H ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  116. (a) = ROTATE_LEFT ((a), (s)); \
  117. (a) += (b); \
  118. }
  119. #define II(a, b, c, d, x, s, ac) { \
  120. (a) += I ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  121. (a) = ROTATE_LEFT ((a), (s)); \
  122. (a) += (b); \
  123. }
  124. /* {{{ PHP_MD5Init
  125. * MD5 initialization. Begins an MD5 operation, writing a new context.
  126. */
  127. ZEND_API void PHP_MD5Init(PHP_MD5_CTX * context)
  128. {
  129. context->count[0] = context->count[1] = 0;
  130. /* Load magic initialization constants.
  131. */
  132. context->state[0] = 0x67452301;
  133. context->state[1] = 0xefcdab89;
  134. context->state[2] = 0x98badcfe;
  135. context->state[3] = 0x10325476;
  136. }
  137. /* }}} */
  138. /* {{{ PHP_MD5Update
  139. MD5 block update operation. Continues an MD5 message-digest
  140. operation, processing another message block, and updating the
  141. context.
  142. */
  143. ZEND_API void PHP_MD5Update(PHP_MD5_CTX * context, const unsigned char *input,
  144. unsigned int inputLen)
  145. {
  146. unsigned int i, index, partLen;
  147. /* Compute number of bytes mod 64 */
  148. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  149. /* Update number of bits */
  150. if ((context->count[0] += ((php_uint32) inputLen << 3))
  151. < ((php_uint32) inputLen << 3))
  152. context->count[1]++;
  153. context->count[1] += ((php_uint32) inputLen >> 29);
  154. partLen = 64 - index;
  155. /* Transform as many times as possible.
  156. */
  157. if (inputLen >= partLen) {
  158. memcpy
  159. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  160. MD5Transform(context->state, context->buffer);
  161. for (i = partLen; i + 63 < inputLen; i += 64)
  162. MD5Transform(context->state, &input[i]);
  163. index = 0;
  164. } else
  165. i = 0;
  166. /* Buffer remaining input */
  167. memcpy
  168. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  169. inputLen - i);
  170. }
  171. /* }}} */
  172. /* {{{ PHP_MD5Final
  173. MD5 finalization. Ends an MD5 message-digest operation, writing the
  174. the message digest and zeroizing the context.
  175. */
  176. ZEND_API void PHP_MD5Final(unsigned char digest[16], PHP_MD5_CTX * context)
  177. {
  178. unsigned char bits[8];
  179. unsigned int index, padLen;
  180. /* Save number of bits */
  181. Encode(bits, context->count, 8);
  182. /* Pad out to 56 mod 64.
  183. */
  184. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  185. padLen = (index < 56) ? (56 - index) : (120 - index);
  186. PHP_MD5Update(context, PADDING, padLen);
  187. /* Append length (before padding) */
  188. PHP_MD5Update(context, bits, 8);
  189. /* Store state in digest */
  190. Encode(digest, context->state, 16);
  191. /* Zeroize sensitive information.
  192. */
  193. memset((unsigned char*) context, 0, sizeof(*context));
  194. }
  195. /* }}} */
  196. /* {{{ MD5Transform
  197. * MD5 basic transformation. Transforms state based on block.
  198. */
  199. static void MD5Transform(php_uint32 state[4], const unsigned char block[64])
  200. {
  201. php_uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  202. Decode(x, block, 64);
  203. /* Round 1 */
  204. FF(a, b, c, d, x[0], S11, 0xd76aa478); /* 1 */
  205. FF(d, a, b, c, x[1], S12, 0xe8c7b756); /* 2 */
  206. FF(c, d, a, b, x[2], S13, 0x242070db); /* 3 */
  207. FF(b, c, d, a, x[3], S14, 0xc1bdceee); /* 4 */
  208. FF(a, b, c, d, x[4], S11, 0xf57c0faf); /* 5 */
  209. FF(d, a, b, c, x[5], S12, 0x4787c62a); /* 6 */
  210. FF(c, d, a, b, x[6], S13, 0xa8304613); /* 7 */
  211. FF(b, c, d, a, x[7], S14, 0xfd469501); /* 8 */
  212. FF(a, b, c, d, x[8], S11, 0x698098d8); /* 9 */
  213. FF(d, a, b, c, x[9], S12, 0x8b44f7af); /* 10 */
  214. FF(c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
  215. FF(b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
  216. FF(a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
  217. FF(d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
  218. FF(c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
  219. FF(b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
  220. /* Round 2 */
  221. GG(a, b, c, d, x[1], S21, 0xf61e2562); /* 17 */
  222. GG(d, a, b, c, x[6], S22, 0xc040b340); /* 18 */
  223. GG(c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
  224. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); /* 20 */
  225. GG(a, b, c, d, x[5], S21, 0xd62f105d); /* 21 */
  226. GG(d, a, b, c, x[10], S22, 0x2441453); /* 22 */
  227. GG(c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
  228. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); /* 24 */
  229. GG(a, b, c, d, x[9], S21, 0x21e1cde6); /* 25 */
  230. GG(d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
  231. GG(c, d, a, b, x[3], S23, 0xf4d50d87); /* 27 */
  232. GG(b, c, d, a, x[8], S24, 0x455a14ed); /* 28 */
  233. GG(a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
  234. GG(d, a, b, c, x[2], S22, 0xfcefa3f8); /* 30 */
  235. GG(c, d, a, b, x[7], S23, 0x676f02d9); /* 31 */
  236. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
  237. /* Round 3 */
  238. HH(a, b, c, d, x[5], S31, 0xfffa3942); /* 33 */
  239. HH(d, a, b, c, x[8], S32, 0x8771f681); /* 34 */
  240. HH(c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
  241. HH(b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
  242. HH(a, b, c, d, x[1], S31, 0xa4beea44); /* 37 */
  243. HH(d, a, b, c, x[4], S32, 0x4bdecfa9); /* 38 */
  244. HH(c, d, a, b, x[7], S33, 0xf6bb4b60); /* 39 */
  245. HH(b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
  246. HH(a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
  247. HH(d, a, b, c, x[0], S32, 0xeaa127fa); /* 42 */
  248. HH(c, d, a, b, x[3], S33, 0xd4ef3085); /* 43 */
  249. HH(b, c, d, a, x[6], S34, 0x4881d05); /* 44 */
  250. HH(a, b, c, d, x[9], S31, 0xd9d4d039); /* 45 */
  251. HH(d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
  252. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
  253. HH(b, c, d, a, x[2], S34, 0xc4ac5665); /* 48 */
  254. /* Round 4 */
  255. II(a, b, c, d, x[0], S41, 0xf4292244); /* 49 */
  256. II(d, a, b, c, x[7], S42, 0x432aff97); /* 50 */
  257. II(c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
  258. II(b, c, d, a, x[5], S44, 0xfc93a039); /* 52 */
  259. II(a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
  260. II(d, a, b, c, x[3], S42, 0x8f0ccc92); /* 54 */
  261. II(c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
  262. II(b, c, d, a, x[1], S44, 0x85845dd1); /* 56 */
  263. II(a, b, c, d, x[8], S41, 0x6fa87e4f); /* 57 */
  264. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
  265. II(c, d, a, b, x[6], S43, 0xa3014314); /* 59 */
  266. II(b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
  267. II(a, b, c, d, x[4], S41, 0xf7537e82); /* 61 */
  268. II(d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
  269. II(c, d, a, b, x[2], S43, 0x2ad7d2bb); /* 63 */
  270. II(b, c, d, a, x[9], S44, 0xeb86d391); /* 64 */
  271. state[0] += a;
  272. state[1] += b;
  273. state[2] += c;
  274. state[3] += d;
  275. /* Zeroize sensitive information. */
  276. memset((unsigned char*) x, 0, sizeof(x));
  277. }
  278. /* }}} */
  279. /* {{{ Encode
  280. Encodes input (php_uint32) into output (unsigned char). Assumes len is
  281. a multiple of 4.
  282. */
  283. static void Encode(unsigned char *output, php_uint32 *input, unsigned int len)
  284. {
  285. unsigned int i, j;
  286. for (i = 0, j = 0; j < len; i++, j += 4) {
  287. output[j] = (unsigned char) (input[i] & 0xff);
  288. output[j + 1] = (unsigned char) ((input[i] >> 8) & 0xff);
  289. output[j + 2] = (unsigned char) ((input[i] >> 16) & 0xff);
  290. output[j + 3] = (unsigned char) ((input[i] >> 24) & 0xff);
  291. }
  292. }
  293. /* }}} */
  294. /* {{{ Decode
  295. Decodes input (unsigned char) into output (php_uint32). Assumes len is
  296. a multiple of 4.
  297. */
  298. static void Decode(php_uint32 *output, const unsigned char *input, unsigned int len)
  299. {
  300. unsigned int i, j;
  301. for (i = 0, j = 0; j < len; i++, j += 4)
  302. output[i] = ((php_uint32) input[j]) | (((php_uint32) input[j + 1]) << 8) |
  303. (((php_uint32) input[j + 2]) << 16) | (((php_uint32) input[j + 3]) << 24);
  304. }
  305. /* }}} */
  306. #undef F
  307. #undef G
  308. #undef H
  309. #undef I
  310. #undef FF
  311. #undef GG
  312. #undef HH
  313. #undef II
  314. /*
  315. +----------------------------------------------------------------------+
  316. | PHP Version 4 |
  317. +----------------------------------------------------------------------+
  318. | Copyright (c) 1997-2003 The PHP Group |
  319. +----------------------------------------------------------------------+
  320. | This source file is subject to version 3.0 of the PHP license, |
  321. | that is bundled with this package in the file LICENSE, and is |
  322. | available through the world-wide-web at the following url: |
  323. | http://www.php.net/license/3_0.txt. |
  324. | If you did not receive a copy of the PHP license and are unable to |
  325. | obtain it through the world-wide-web, please send a note to |
  326. | license@php.net so we can mail you a copy immediately. |
  327. +----------------------------------------------------------------------+
  328. | Author: Stefan Esser <sesser@php.net> |
  329. +----------------------------------------------------------------------+
  330. */
  331. /* $Id: Digest.cpp,v 1.1.2.2 2006/04/15 20:19:12 prosl0am Exp $ */
  332. #include <stdio.h>
  333. //#include "php.h"
  334. /* This code is heavily based on the PHP md5 implementation */
  335. //#include "sha1.h"
  336. ZEND_API void make_sha1_digest(char *sha1str, unsigned char *digest)
  337. {
  338. int i;
  339. for (i = 0; i < 20; i++) {
  340. sprintf(sha1str, "%02x", digest[i]);
  341. sha1str += 2;
  342. }
  343. *sha1str = '\0';
  344. }
  345. static void SHA1Transform(php_uint32[5], const unsigned char[64]);
  346. static void SHA1Encode(unsigned char *, php_uint32 *, unsigned int);
  347. static void SHA1Decode(php_uint32 *, const unsigned char *, unsigned int);
  348. static unsigned char PADDING_[64] =
  349. {
  350. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  351. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  352. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  353. };
  354. /* F, G, H and I are basic SHA1 functions.
  355. */
  356. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  357. #define G(x, y, z) ((x) ^ (y) ^ (z))
  358. #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  359. #define I(x, y, z) ((x) ^ (y) ^ (z))
  360. /* ROTATE_LEFT rotates x left n bits.
  361. */
  362. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  363. /* W[i]
  364. */
  365. #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
  366. (x[i&15]=ROTATE_LEFT(tmp, 1)) )
  367. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  368. */
  369. #define FF(a, b, c, d, e, w) { \
  370. (e) += F ((b), (c), (d)) + (w) + (php_uint32)(0x5A827999); \
  371. (e) += ROTATE_LEFT ((a), 5); \
  372. (b) = ROTATE_LEFT((b), 30); \
  373. }
  374. #define GG(a, b, c, d, e, w) { \
  375. (e) += G ((b), (c), (d)) + (w) + (php_uint32)(0x6ED9EBA1); \
  376. (e) += ROTATE_LEFT ((a), 5); \
  377. (b) = ROTATE_LEFT((b), 30); \
  378. }
  379. #define HH(a, b, c, d, e, w) { \
  380. (e) += H ((b), (c), (d)) + (w) + (php_uint32)(0x8F1BBCDC); \
  381. (e) += ROTATE_LEFT ((a), 5); \
  382. (b) = ROTATE_LEFT((b), 30); \
  383. }
  384. #define II(a, b, c, d, e, w) { \
  385. (e) += I ((b), (c), (d)) + (w) + (php_uint32)(0xCA62C1D6); \
  386. (e) += ROTATE_LEFT ((a), 5); \
  387. (b) = ROTATE_LEFT((b), 30); \
  388. }
  389. /* {{{ PHP_SHA1Init
  390. * SHA1 initialization. Begins an SHA1 operation, writing a new context.
  391. */
  392. ZEND_API void PHP_SHA1Init(PHP_SHA1_CTX * context)
  393. {
  394. context->count[0] = context->count[1] = 0;
  395. /* Load magic initialization constants.
  396. */
  397. context->state[0] = 0x67452301;
  398. context->state[1] = 0xefcdab89;
  399. context->state[2] = 0x98badcfe;
  400. context->state[3] = 0x10325476;
  401. context->state[4] = 0xc3d2e1f0;
  402. }
  403. /* }}} */
  404. /* {{{ PHP_SHA1Update
  405. SHA1 block update operation. Continues an SHA1 message-digest
  406. operation, processing another message block, and updating the
  407. context.
  408. */
  409. ZEND_API void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
  410. unsigned int inputLen)
  411. {
  412. unsigned int i, index, partLen;
  413. /* Compute number of bytes mod 64 */
  414. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  415. /* Update number of bits */
  416. if ((context->count[0] += ((php_uint32) inputLen << 3))
  417. < ((php_uint32) inputLen << 3))
  418. context->count[1]++;
  419. context->count[1] += ((php_uint32) inputLen >> 29);
  420. partLen = 64 - index;
  421. /* Transform as many times as possible.
  422. */
  423. if (inputLen >= partLen) {
  424. memcpy
  425. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  426. SHA1Transform(context->state, context->buffer);
  427. for (i = partLen; i + 63 < inputLen; i += 64)
  428. SHA1Transform(context->state, &input[i]);
  429. index = 0;
  430. } else
  431. i = 0;
  432. /* Buffer remaining input */
  433. memcpy
  434. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  435. inputLen - i);
  436. }
  437. /* }}} */
  438. /* {{{ PHP_SHA1Final
  439. SHA1 finalization. Ends an SHA1 message-digest operation, writing the
  440. the message digest and zeroizing the context.
  441. */
  442. ZEND_API void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
  443. {
  444. unsigned char bits[8];
  445. unsigned int index, padLen;
  446. /* Save number of bits */
  447. bits[7] = context->count[0] & 0xFF;
  448. bits[6] = (context->count[0] >> 8) & 0xFF;
  449. bits[5] = (context->count[0] >> 16) & 0xFF;
  450. bits[4] = (context->count[0] >> 24) & 0xFF;
  451. bits[3] = context->count[1] & 0xFF;
  452. bits[2] = (context->count[1] >> 8) & 0xFF;
  453. bits[1] = (context->count[1] >> 16) & 0xFF;
  454. bits[0] = (context->count[1] >> 24) & 0xFF;
  455. /* Pad out to 56 mod 64.
  456. */
  457. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  458. padLen = (index < 56) ? (56 - index) : (120 - index);
  459. PHP_SHA1Update(context, PADDING_, padLen);
  460. /* Append length (before padding) */
  461. PHP_SHA1Update(context, bits, 8);
  462. /* Store state in digest */
  463. SHA1Encode(digest, context->state, 20);
  464. /* Zeroize sensitive information.
  465. */
  466. memset((unsigned char*) context, 0, sizeof(*context));
  467. }
  468. /* }}} */
  469. /* {{{ SHA1Transform
  470. * SHA1 basic transformation. Transforms state based on block.
  471. */
  472. static void SHA1Transform(php_uint32 state[5], const unsigned char block[64])
  473. {
  474. php_uint32 a = state[0], b = state[1], c = state[2];
  475. php_uint32 d = state[3], e = state[4], x[16], tmp;
  476. SHA1Decode(x, block, 64);
  477. /* Round 1 */
  478. FF(a, b, c, d, e, x[0]); /* 1 */
  479. FF(e, a, b, c, d, x[1]); /* 2 */
  480. FF(d, e, a, b, c, x[2]); /* 3 */
  481. FF(c, d, e, a, b, x[3]); /* 4 */
  482. FF(b, c, d, e, a, x[4]); /* 5 */
  483. FF(a, b, c, d, e, x[5]); /* 6 */
  484. FF(e, a, b, c, d, x[6]); /* 7 */
  485. FF(d, e, a, b, c, x[7]); /* 8 */
  486. FF(c, d, e, a, b, x[8]); /* 9 */
  487. FF(b, c, d, e, a, x[9]); /* 10 */
  488. FF(a, b, c, d, e, x[10]); /* 11 */
  489. FF(e, a, b, c, d, x[11]); /* 12 */
  490. FF(d, e, a, b, c, x[12]); /* 13 */
  491. FF(c, d, e, a, b, x[13]); /* 14 */
  492. FF(b, c, d, e, a, x[14]); /* 15 */
  493. FF(a, b, c, d, e, x[15]); /* 16 */
  494. FF(e, a, b, c, d, W(16)); /* 17 */
  495. FF(d, e, a, b, c, W(17)); /* 18 */
  496. FF(c, d, e, a, b, W(18)); /* 19 */
  497. FF(b, c, d, e, a, W(19)); /* 20 */
  498. /* Round 2 */
  499. GG(a, b, c, d, e, W(20)); /* 21 */
  500. GG(e, a, b, c, d, W(21)); /* 22 */
  501. GG(d, e, a, b, c, W(22)); /* 23 */
  502. GG(c, d, e, a, b, W(23)); /* 24 */
  503. GG(b, c, d, e, a, W(24)); /* 25 */
  504. GG(a, b, c, d, e, W(25)); /* 26 */
  505. GG(e, a, b, c, d, W(26)); /* 27 */
  506. GG(d, e, a, b, c, W(27)); /* 28 */
  507. GG(c, d, e, a, b, W(28)); /* 29 */
  508. GG(b, c, d, e, a, W(29)); /* 30 */
  509. GG(a, b, c, d, e, W(30)); /* 31 */
  510. GG(e, a, b, c, d, W(31)); /* 32 */
  511. GG(d, e, a, b, c, W(32)); /* 33 */
  512. GG(c, d, e, a, b, W(33)); /* 34 */
  513. GG(b, c, d, e, a, W(34)); /* 35 */
  514. GG(a, b, c, d, e, W(35)); /* 36 */
  515. GG(e, a, b, c, d, W(36)); /* 37 */
  516. GG(d, e, a, b, c, W(37)); /* 38 */
  517. GG(c, d, e, a, b, W(38)); /* 39 */
  518. GG(b, c, d, e, a, W(39)); /* 40 */
  519. /* Round 3 */
  520. HH(a, b, c, d, e, W(40)); /* 41 */
  521. HH(e, a, b, c, d, W(41)); /* 42 */
  522. HH(d, e, a, b, c, W(42)); /* 43 */
  523. HH(c, d, e, a, b, W(43)); /* 44 */
  524. HH(b, c, d, e, a, W(44)); /* 45 */
  525. HH(a, b, c, d, e, W(45)); /* 46 */
  526. HH(e, a, b, c, d, W(46)); /* 47 */
  527. HH(d, e, a, b, c, W(47)); /* 48 */
  528. HH(c, d, e, a, b, W(48)); /* 49 */
  529. HH(b, c, d, e, a, W(49)); /* 50 */
  530. HH(a, b, c, d, e, W(50)); /* 51 */
  531. HH(e, a, b, c, d, W(51)); /* 52 */
  532. HH(d, e, a, b, c, W(52)); /* 53 */
  533. HH(c, d, e, a, b, W(53)); /* 54 */
  534. HH(b, c, d, e, a, W(54)); /* 55 */
  535. HH(a, b, c, d, e, W(55)); /* 56 */
  536. HH(e, a, b, c, d, W(56)); /* 57 */
  537. HH(d, e, a, b, c, W(57)); /* 58 */
  538. HH(c, d, e, a, b, W(58)); /* 59 */
  539. HH(b, c, d, e, a, W(59)); /* 60 */
  540. /* Round 4 */
  541. II(a, b, c, d, e, W(60)); /* 61 */
  542. II(e, a, b, c, d, W(61)); /* 62 */
  543. II(d, e, a, b, c, W(62)); /* 63 */
  544. II(c, d, e, a, b, W(63)); /* 64 */
  545. II(b, c, d, e, a, W(64)); /* 65 */
  546. II(a, b, c, d, e, W(65)); /* 66 */
  547. II(e, a, b, c, d, W(66)); /* 67 */
  548. II(d, e, a, b, c, W(67)); /* 68 */
  549. II(c, d, e, a, b, W(68)); /* 69 */
  550. II(b, c, d, e, a, W(69)); /* 70 */
  551. II(a, b, c, d, e, W(70)); /* 71 */
  552. II(e, a, b, c, d, W(71)); /* 72 */
  553. II(d, e, a, b, c, W(72)); /* 73 */
  554. II(c, d, e, a, b, W(73)); /* 74 */
  555. II(b, c, d, e, a, W(74)); /* 75 */
  556. II(a, b, c, d, e, W(75)); /* 76 */
  557. II(e, a, b, c, d, W(76)); /* 77 */
  558. II(d, e, a, b, c, W(77)); /* 78 */
  559. II(c, d, e, a, b, W(78)); /* 79 */
  560. II(b, c, d, e, a, W(79)); /* 80 */
  561. state[0] += a;
  562. state[1] += b;
  563. state[2] += c;
  564. state[3] += d;
  565. state[4] += e;
  566. /* Zeroize sensitive information. */
  567. memset((unsigned char*) x, 0, sizeof(x));
  568. }
  569. /* }}} */
  570. /* {{{ SHA1Encode
  571. Encodes input (php_uint32) into output (unsigned char). Assumes len is
  572. a multiple of 4.
  573. */
  574. static void SHA1Encode(unsigned char *output, php_uint32 *input, unsigned int len)
  575. {
  576. unsigned int i, j;
  577. for (i = 0, j = 0; j < len; i++, j += 4) {
  578. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  579. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  580. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  581. output[j + 3] = (unsigned char) (input[i] & 0xff);
  582. }
  583. }
  584. /* }}} */
  585. /* {{{ SHA1Decode
  586. Decodes input (unsigned char) into output (php_uint32). Assumes len is
  587. a multiple of 4.
  588. */
  589. static void SHA1Decode(php_uint32 *output, const unsigned char *input, unsigned int len)
  590. {
  591. unsigned int i, j;
  592. for (i = 0, j = 0; j < len; i++, j += 4)
  593. output[i] = ((php_uint32) input[j + 3]) | (((php_uint32) input[j + 2]) << 8) |
  594. (((php_uint32) input[j + 1]) << 16) | (((php_uint32) input[j]) << 24);
  595. }
  596. /* }}} */