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

/ext/standard/sha1.c

https://github.com/ifeghali/php-src
C | 434 lines | 288 code | 56 blank | 90 comment | 27 complexity | 9d0bb8d97832d2b9639fc104ecd7c132 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-2009 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. | Author: Stefan Esser <sesser@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. /* $Id$ */
  19. #include "php.h"
  20. #include "ext/standard/file.h"
  21. /* This code is heavily based on the PHP md5 implementation */
  22. #include "sha1.h"
  23. #include "md5.h"
  24. PHPAPI void make_sha1_digest(char *sha1str, unsigned char *digest) /* {{{ */
  25. {
  26. make_digest_ex(sha1str, digest, 20);
  27. }
  28. /* }}} */
  29. /* {{{ proto string sha1(string str [, bool raw_output]) U
  30. Calculate the sha1 hash of a string */
  31. PHP_FUNCTION(sha1)
  32. {
  33. char *arg;
  34. int arg_len;
  35. zend_uchar arg_type;
  36. zend_bool raw_output = 0;
  37. char sha1str[41];
  38. PHP_SHA1_CTX context;
  39. unsigned char digest[20];
  40. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
  41. return;
  42. }
  43. if (arg_type == IS_UNICODE) {
  44. arg = zend_unicode_to_ascii((UChar*)arg, arg_len TSRMLS_CC);
  45. if (!arg) {
  46. php_error_docref(NULL TSRMLS_CC, E_WARNING, "Binary or ASCII-Unicode string expected, non-ASCII-Unicode string received");
  47. RETURN_FALSE;
  48. }
  49. }
  50. sha1str[0] = '\0';
  51. PHP_SHA1Init(&context);
  52. PHP_SHA1Update(&context, (unsigned char*)arg, arg_len);
  53. PHP_SHA1Final(digest, &context);
  54. if (raw_output) {
  55. RETVAL_STRINGL((char*)digest, 20, 1);
  56. } else {
  57. make_digest_ex(sha1str, digest, 20);
  58. RETVAL_ASCII_STRING(sha1str, ZSTR_DUPLICATE);
  59. }
  60. if (arg_type == IS_UNICODE) {
  61. efree(arg);
  62. }
  63. }
  64. /* }}} */
  65. /* {{{ proto string sha1_file(string filename [, bool raw_output]) U
  66. Calculate the sha1 hash of given filename */
  67. PHP_FUNCTION(sha1_file)
  68. {
  69. char *arg;
  70. int arg_len;
  71. zend_uchar arg_type;
  72. zend_bool raw_output = 0;
  73. char sha1str[41];
  74. unsigned char buf[1024];
  75. unsigned char digest[20];
  76. PHP_SHA1_CTX context;
  77. int n;
  78. php_stream *stream;
  79. if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "t|b", &arg, &arg_len, &arg_type, &raw_output) == FAILURE) {
  80. return;
  81. }
  82. if (arg_type == IS_UNICODE) {
  83. if (php_stream_path_encode(NULL, &arg, &arg_len, (UChar*)arg, arg_len, REPORT_ERRORS, FG(default_context)) == FAILURE) {
  84. RETURN_FALSE;
  85. }
  86. }
  87. stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
  88. if (arg_type == IS_UNICODE) {
  89. efree(arg);
  90. }
  91. if (!stream) {
  92. RETURN_FALSE;
  93. }
  94. PHP_SHA1Init(&context);
  95. while ((n = php_stream_read(stream, (char*)buf, sizeof(buf))) > 0) {
  96. PHP_SHA1Update(&context, buf, n);
  97. }
  98. PHP_SHA1Final(digest, &context);
  99. php_stream_close(stream);
  100. if (n<0) {
  101. RETURN_FALSE;
  102. }
  103. if (raw_output) {
  104. RETURN_STRINGL((char*)digest, 20, 1);
  105. } else {
  106. make_digest_ex(sha1str, digest, 20);
  107. RETVAL_ASCII_STRING(sha1str, ZSTR_DUPLICATE);
  108. }
  109. }
  110. /* }}} */
  111. static void SHA1Transform(php_uint32[5], const unsigned char[64]);
  112. static void SHA1Encode(unsigned char *, php_uint32 *, unsigned int);
  113. static void SHA1Decode(php_uint32 *, const unsigned char *, unsigned int);
  114. static unsigned char PADDING[64] =
  115. {
  116. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  117. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  118. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  119. };
  120. /* F, G, H and I are basic SHA1 functions.
  121. */
  122. #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
  123. #define G(x, y, z) ((x) ^ (y) ^ (z))
  124. #define H(x, y, z) (((x) & (y)) | ((z) & ((x) | (y))))
  125. #define I(x, y, z) ((x) ^ (y) ^ (z))
  126. /* ROTATE_LEFT rotates x left n bits.
  127. */
  128. #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
  129. /* W[i]
  130. */
  131. #define W(i) ( tmp=x[(i-3)&15]^x[(i-8)&15]^x[(i-14)&15]^x[i&15], \
  132. (x[i&15]=ROTATE_LEFT(tmp, 1)) )
  133. /* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  134. */
  135. #define FF(a, b, c, d, e, w) { \
  136. (e) += F ((b), (c), (d)) + (w) + (php_uint32)(0x5A827999); \
  137. (e) += ROTATE_LEFT ((a), 5); \
  138. (b) = ROTATE_LEFT((b), 30); \
  139. }
  140. #define GG(a, b, c, d, e, w) { \
  141. (e) += G ((b), (c), (d)) + (w) + (php_uint32)(0x6ED9EBA1); \
  142. (e) += ROTATE_LEFT ((a), 5); \
  143. (b) = ROTATE_LEFT((b), 30); \
  144. }
  145. #define HH(a, b, c, d, e, w) { \
  146. (e) += H ((b), (c), (d)) + (w) + (php_uint32)(0x8F1BBCDC); \
  147. (e) += ROTATE_LEFT ((a), 5); \
  148. (b) = ROTATE_LEFT((b), 30); \
  149. }
  150. #define II(a, b, c, d, e, w) { \
  151. (e) += I ((b), (c), (d)) + (w) + (php_uint32)(0xCA62C1D6); \
  152. (e) += ROTATE_LEFT ((a), 5); \
  153. (b) = ROTATE_LEFT((b), 30); \
  154. }
  155. /* {{{ PHP_SHA1Init
  156. * SHA1 initialization. Begins an SHA1 operation, writing a new context.
  157. */
  158. PHPAPI void PHP_SHA1Init(PHP_SHA1_CTX * context)
  159. {
  160. context->count[0] = context->count[1] = 0;
  161. /* Load magic initialization constants.
  162. */
  163. context->state[0] = 0x67452301;
  164. context->state[1] = 0xefcdab89;
  165. context->state[2] = 0x98badcfe;
  166. context->state[3] = 0x10325476;
  167. context->state[4] = 0xc3d2e1f0;
  168. }
  169. /* }}} */
  170. /* {{{ PHP_SHA1Update
  171. SHA1 block update operation. Continues an SHA1 message-digest
  172. operation, processing another message block, and updating the
  173. context.
  174. */
  175. PHPAPI void PHP_SHA1Update(PHP_SHA1_CTX * context, const unsigned char *input,
  176. unsigned int inputLen)
  177. {
  178. unsigned int i, index, partLen;
  179. /* Compute number of bytes mod 64 */
  180. index = (unsigned int) ((context->count[0] >> 3) & 0x3F);
  181. /* Update number of bits */
  182. if ((context->count[0] += ((php_uint32) inputLen << 3))
  183. < ((php_uint32) inputLen << 3))
  184. context->count[1]++;
  185. context->count[1] += ((php_uint32) inputLen >> 29);
  186. partLen = 64 - index;
  187. /* Transform as many times as possible.
  188. */
  189. if (inputLen >= partLen) {
  190. memcpy
  191. ((unsigned char*) & context->buffer[index], (unsigned char*) input, partLen);
  192. SHA1Transform(context->state, context->buffer);
  193. for (i = partLen; i + 63 < inputLen; i += 64)
  194. SHA1Transform(context->state, &input[i]);
  195. index = 0;
  196. } else
  197. i = 0;
  198. /* Buffer remaining input */
  199. memcpy
  200. ((unsigned char*) & context->buffer[index], (unsigned char*) & input[i],
  201. inputLen - i);
  202. }
  203. /* }}} */
  204. /* {{{ PHP_SHA1Final
  205. SHA1 finalization. Ends an SHA1 message-digest operation, writing the
  206. the message digest and zeroizing the context.
  207. */
  208. PHPAPI void PHP_SHA1Final(unsigned char digest[20], PHP_SHA1_CTX * context)
  209. {
  210. unsigned char bits[8];
  211. unsigned int index, padLen;
  212. /* Save number of bits */
  213. bits[7] = context->count[0] & 0xFF;
  214. bits[6] = (context->count[0] >> 8) & 0xFF;
  215. bits[5] = (context->count[0] >> 16) & 0xFF;
  216. bits[4] = (context->count[0] >> 24) & 0xFF;
  217. bits[3] = context->count[1] & 0xFF;
  218. bits[2] = (context->count[1] >> 8) & 0xFF;
  219. bits[1] = (context->count[1] >> 16) & 0xFF;
  220. bits[0] = (context->count[1] >> 24) & 0xFF;
  221. /* Pad out to 56 mod 64.
  222. */
  223. index = (unsigned int) ((context->count[0] >> 3) & 0x3f);
  224. padLen = (index < 56) ? (56 - index) : (120 - index);
  225. PHP_SHA1Update(context, PADDING, padLen);
  226. /* Append length (before padding) */
  227. PHP_SHA1Update(context, bits, 8);
  228. /* Store state in digest */
  229. SHA1Encode(digest, context->state, 20);
  230. /* Zeroize sensitive information.
  231. */
  232. memset((unsigned char*) context, 0, sizeof(*context));
  233. }
  234. /* }}} */
  235. /* {{{ SHA1Transform
  236. * SHA1 basic transformation. Transforms state based on block.
  237. */
  238. static void SHA1Transform(state, block)
  239. php_uint32 state[5];
  240. const unsigned char block[64];
  241. {
  242. php_uint32 a = state[0], b = state[1], c = state[2];
  243. php_uint32 d = state[3], e = state[4], x[16], tmp;
  244. SHA1Decode(x, block, 64);
  245. /* Round 1 */
  246. FF(a, b, c, d, e, x[0]); /* 1 */
  247. FF(e, a, b, c, d, x[1]); /* 2 */
  248. FF(d, e, a, b, c, x[2]); /* 3 */
  249. FF(c, d, e, a, b, x[3]); /* 4 */
  250. FF(b, c, d, e, a, x[4]); /* 5 */
  251. FF(a, b, c, d, e, x[5]); /* 6 */
  252. FF(e, a, b, c, d, x[6]); /* 7 */
  253. FF(d, e, a, b, c, x[7]); /* 8 */
  254. FF(c, d, e, a, b, x[8]); /* 9 */
  255. FF(b, c, d, e, a, x[9]); /* 10 */
  256. FF(a, b, c, d, e, x[10]); /* 11 */
  257. FF(e, a, b, c, d, x[11]); /* 12 */
  258. FF(d, e, a, b, c, x[12]); /* 13 */
  259. FF(c, d, e, a, b, x[13]); /* 14 */
  260. FF(b, c, d, e, a, x[14]); /* 15 */
  261. FF(a, b, c, d, e, x[15]); /* 16 */
  262. FF(e, a, b, c, d, W(16)); /* 17 */
  263. FF(d, e, a, b, c, W(17)); /* 18 */
  264. FF(c, d, e, a, b, W(18)); /* 19 */
  265. FF(b, c, d, e, a, W(19)); /* 20 */
  266. /* Round 2 */
  267. GG(a, b, c, d, e, W(20)); /* 21 */
  268. GG(e, a, b, c, d, W(21)); /* 22 */
  269. GG(d, e, a, b, c, W(22)); /* 23 */
  270. GG(c, d, e, a, b, W(23)); /* 24 */
  271. GG(b, c, d, e, a, W(24)); /* 25 */
  272. GG(a, b, c, d, e, W(25)); /* 26 */
  273. GG(e, a, b, c, d, W(26)); /* 27 */
  274. GG(d, e, a, b, c, W(27)); /* 28 */
  275. GG(c, d, e, a, b, W(28)); /* 29 */
  276. GG(b, c, d, e, a, W(29)); /* 30 */
  277. GG(a, b, c, d, e, W(30)); /* 31 */
  278. GG(e, a, b, c, d, W(31)); /* 32 */
  279. GG(d, e, a, b, c, W(32)); /* 33 */
  280. GG(c, d, e, a, b, W(33)); /* 34 */
  281. GG(b, c, d, e, a, W(34)); /* 35 */
  282. GG(a, b, c, d, e, W(35)); /* 36 */
  283. GG(e, a, b, c, d, W(36)); /* 37 */
  284. GG(d, e, a, b, c, W(37)); /* 38 */
  285. GG(c, d, e, a, b, W(38)); /* 39 */
  286. GG(b, c, d, e, a, W(39)); /* 40 */
  287. /* Round 3 */
  288. HH(a, b, c, d, e, W(40)); /* 41 */
  289. HH(e, a, b, c, d, W(41)); /* 42 */
  290. HH(d, e, a, b, c, W(42)); /* 43 */
  291. HH(c, d, e, a, b, W(43)); /* 44 */
  292. HH(b, c, d, e, a, W(44)); /* 45 */
  293. HH(a, b, c, d, e, W(45)); /* 46 */
  294. HH(e, a, b, c, d, W(46)); /* 47 */
  295. HH(d, e, a, b, c, W(47)); /* 48 */
  296. HH(c, d, e, a, b, W(48)); /* 49 */
  297. HH(b, c, d, e, a, W(49)); /* 50 */
  298. HH(a, b, c, d, e, W(50)); /* 51 */
  299. HH(e, a, b, c, d, W(51)); /* 52 */
  300. HH(d, e, a, b, c, W(52)); /* 53 */
  301. HH(c, d, e, a, b, W(53)); /* 54 */
  302. HH(b, c, d, e, a, W(54)); /* 55 */
  303. HH(a, b, c, d, e, W(55)); /* 56 */
  304. HH(e, a, b, c, d, W(56)); /* 57 */
  305. HH(d, e, a, b, c, W(57)); /* 58 */
  306. HH(c, d, e, a, b, W(58)); /* 59 */
  307. HH(b, c, d, e, a, W(59)); /* 60 */
  308. /* Round 4 */
  309. II(a, b, c, d, e, W(60)); /* 61 */
  310. II(e, a, b, c, d, W(61)); /* 62 */
  311. II(d, e, a, b, c, W(62)); /* 63 */
  312. II(c, d, e, a, b, W(63)); /* 64 */
  313. II(b, c, d, e, a, W(64)); /* 65 */
  314. II(a, b, c, d, e, W(65)); /* 66 */
  315. II(e, a, b, c, d, W(66)); /* 67 */
  316. II(d, e, a, b, c, W(67)); /* 68 */
  317. II(c, d, e, a, b, W(68)); /* 69 */
  318. II(b, c, d, e, a, W(69)); /* 70 */
  319. II(a, b, c, d, e, W(70)); /* 71 */
  320. II(e, a, b, c, d, W(71)); /* 72 */
  321. II(d, e, a, b, c, W(72)); /* 73 */
  322. II(c, d, e, a, b, W(73)); /* 74 */
  323. II(b, c, d, e, a, W(74)); /* 75 */
  324. II(a, b, c, d, e, W(75)); /* 76 */
  325. II(e, a, b, c, d, W(76)); /* 77 */
  326. II(d, e, a, b, c, W(77)); /* 78 */
  327. II(c, d, e, a, b, W(78)); /* 79 */
  328. II(b, c, d, e, a, W(79)); /* 80 */
  329. state[0] += a;
  330. state[1] += b;
  331. state[2] += c;
  332. state[3] += d;
  333. state[4] += e;
  334. /* Zeroize sensitive information. */
  335. memset((unsigned char*) x, 0, sizeof(x));
  336. }
  337. /* }}} */
  338. /* {{{ SHA1Encode
  339. Encodes input (php_uint32) into output (unsigned char). Assumes len is
  340. a multiple of 4.
  341. */
  342. static void SHA1Encode(output, input, len)
  343. unsigned char *output;
  344. php_uint32 *input;
  345. unsigned int len;
  346. {
  347. unsigned int i, j;
  348. for (i = 0, j = 0; j < len; i++, j += 4) {
  349. output[j] = (unsigned char) ((input[i] >> 24) & 0xff);
  350. output[j + 1] = (unsigned char) ((input[i] >> 16) & 0xff);
  351. output[j + 2] = (unsigned char) ((input[i] >> 8) & 0xff);
  352. output[j + 3] = (unsigned char) (input[i] & 0xff);
  353. }
  354. }
  355. /* }}} */
  356. /* {{{ SHA1Decode
  357. Decodes input (unsigned char) into output (php_uint32). Assumes len is
  358. a multiple of 4.
  359. */
  360. static void SHA1Decode(output, input, len)
  361. php_uint32 *output;
  362. const unsigned char *input;
  363. unsigned int len;
  364. {
  365. unsigned int i, j;
  366. for (i = 0, j = 0; j < len; i++, j += 4)
  367. output[i] = ((php_uint32) input[j + 3]) | (((php_uint32) input[j + 2]) << 8) |
  368. (((php_uint32) input[j + 1]) << 16) | (((php_uint32) input[j]) << 24);
  369. }
  370. /* }}} */
  371. /*
  372. * Local variables:
  373. * tab-width: 4
  374. * c-basic-offset: 4
  375. * End:
  376. * vim600: sw=4 ts=4 fdm=marker
  377. * vim<600: sw=4 ts=4
  378. */