PageRenderTime 72ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/opensource.apple.com/source/apache_mod_php/apache_mod_php-44.2/php/ext/standard/md5.c

#
C | 465 lines | 353 code | 57 blank | 55 comment | 2 complexity | a827e92dbc49739eaa658b61eac4f6e1 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0, GPL-2.0, ISC, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause, WTFPL, MIT, AGPL-1.0, AGPL-3.0
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <title>md5.c</title>
  6. <style type="text/css">
  7. .enscript-comment { font-style: italic; color: rgb(178,34,34); }
  8. .enscript-function-name { font-weight: bold; color: rgb(0,0,255); }
  9. .enscript-variable-name { font-weight: bold; color: rgb(184,134,11); }
  10. .enscript-keyword { font-weight: bold; color: rgb(160,32,240); }
  11. .enscript-reference { font-weight: bold; color: rgb(95,158,160); }
  12. .enscript-string { font-weight: bold; color: rgb(188,143,143); }
  13. .enscript-builtin { font-weight: bold; color: rgb(218,112,214); }
  14. .enscript-type { font-weight: bold; color: rgb(34,139,34); }
  15. .enscript-highlight { text-decoration: underline; color: 0; }
  16. </style>
  17. </head>
  18. <body id="top">
  19. <h1 style="margin:8px;" id="f1">md5.c&nbsp;&nbsp;&nbsp;<span style="font-weight: normal; font-size: 0.5em;">[<a href="?txt">plain text</a>]</span></h1>
  20. <hr/>
  21. <div></div>
  22. <pre>
  23. <span class="enscript-comment">/*
  24. +----------------------------------------------------------------------+
  25. | PHP Version 5 |
  26. +----------------------------------------------------------------------+
  27. | Copyright (c) 1997-2008 The PHP Group |
  28. +----------------------------------------------------------------------+
  29. | This source file is subject to version 3.01 of the PHP license, |
  30. | that is bundled with this package in the file LICENSE, and is |
  31. | available through the world-wide-web at the following url: |
  32. | <a href="http://www.php.net/license/3_01.txt">http://www.php.net/license/3_01.txt</a> |
  33. | If you did not receive a copy of the PHP license and are unable to |
  34. | obtain it through the world-wide-web, please send a note to |
  35. | <a href="mailto:license@php.net">license@php.net</a> so we can mail you a copy immediately. |
  36. +----------------------------------------------------------------------+
  37. | Author: Lachlan Roche |
  38. +----------------------------------------------------------------------+
  39. */</span>
  40. <span class="enscript-comment">/* $Id: md5.c,v 1.39.2.1.2.5 2007/12/31 07:20:13 sebastian Exp $ */</span>
  41. <span class="enscript-comment">/*
  42. * md5.c - Copyright 1997 Lachlan Roche
  43. * md5_file() added by Alessandro Astarita &lt;<a href="mailto:aleast@capri.it">aleast@capri.it</a>&gt;
  44. */</span>
  45. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;php.h&quot;</span>
  46. #<span class="enscript-reference">include</span> <span class="enscript-string">&quot;md5.h&quot;</span>
  47. PHPAPI <span class="enscript-type">void</span> <span class="enscript-function-name">make_digest</span>(<span class="enscript-type">char</span> *md5str, <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *digest)
  48. {
  49. make_digest_ex(md5str, digest, 16);
  50. }
  51. PHPAPI <span class="enscript-type">void</span> <span class="enscript-function-name">make_digest_ex</span>(<span class="enscript-type">char</span> *md5str, <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *digest, <span class="enscript-type">int</span> len)
  52. {
  53. <span class="enscript-type">static</span> <span class="enscript-type">const</span> <span class="enscript-type">char</span> hexits[17] = <span class="enscript-string">&quot;0123456789abcdef&quot;</span>;
  54. <span class="enscript-type">int</span> i;
  55. <span class="enscript-keyword">for</span> (i = 0; i &lt; len; i++) {
  56. md5str[i * 2] = hexits[digest[i] &gt;&gt; 4];
  57. md5str[(i * 2) + 1] = hexits[digest[i] &amp; 0x0F];
  58. }
  59. md5str[len * 2] = <span class="enscript-string">'\0'</span>;
  60. }
  61. <span class="enscript-comment">/* {{{ proto string md5(string str, [ bool raw_output])
  62. Calculate the md5 hash of a string */</span>
  63. <span class="enscript-function-name">PHP_NAMED_FUNCTION</span>(php_if_md5)
  64. {
  65. <span class="enscript-type">char</span> *arg;
  66. <span class="enscript-type">int</span> arg_len;
  67. zend_bool raw_output = 0;
  68. <span class="enscript-type">char</span> md5str[33];
  69. PHP_MD5_CTX context;
  70. <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> digest[16];
  71. <span class="enscript-keyword">if</span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, <span class="enscript-string">&quot;s|b&quot;</span>, &amp;arg, &amp;arg_len, &amp;raw_output) == FAILURE) {
  72. <span class="enscript-keyword">return</span>;
  73. }
  74. md5str[0] = <span class="enscript-string">'\0'</span>;
  75. PHP_MD5Init(&amp;context);
  76. PHP_MD5Update(&amp;context, arg, arg_len);
  77. PHP_MD5Final(digest, &amp;context);
  78. <span class="enscript-keyword">if</span> (raw_output) {
  79. RETURN_STRINGL(digest, 16, 1);
  80. } <span class="enscript-keyword">else</span> {
  81. make_digest_ex(md5str, digest, 16);
  82. RETVAL_STRING(md5str, 1);
  83. }
  84. }
  85. <span class="enscript-comment">/* }}} */</span>
  86. <span class="enscript-comment">/* {{{ proto string md5_file(string filename [, bool raw_output])
  87. Calculate the md5 hash of given filename */</span>
  88. <span class="enscript-function-name">PHP_NAMED_FUNCTION</span>(php_if_md5_file)
  89. {
  90. <span class="enscript-type">char</span> *arg;
  91. <span class="enscript-type">int</span> arg_len;
  92. zend_bool raw_output = 0;
  93. <span class="enscript-type">char</span> md5str[33];
  94. <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> buf[1024];
  95. <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> digest[16];
  96. PHP_MD5_CTX context;
  97. <span class="enscript-type">int</span> n;
  98. php_stream *stream;
  99. <span class="enscript-keyword">if</span> (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, <span class="enscript-string">&quot;s|b&quot;</span>, &amp;arg, &amp;arg_len, &amp;raw_output) == FAILURE) {
  100. <span class="enscript-keyword">return</span>;
  101. }
  102. stream = php_stream_open_wrapper(arg, <span class="enscript-string">&quot;rb&quot;</span>, REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
  103. <span class="enscript-keyword">if</span> (!stream) {
  104. RETURN_FALSE;
  105. }
  106. PHP_MD5Init(&amp;context);
  107. <span class="enscript-keyword">while</span> ((n = php_stream_read(stream, buf, <span class="enscript-keyword">sizeof</span>(buf))) &gt; 0) {
  108. PHP_MD5Update(&amp;context, buf, n);
  109. }
  110. PHP_MD5Final(digest, &amp;context);
  111. php_stream_close(stream);
  112. <span class="enscript-keyword">if</span> (n&lt;0) {
  113. RETURN_FALSE;
  114. }
  115. <span class="enscript-keyword">if</span> (raw_output) {
  116. RETURN_STRINGL(digest, 16, 1);
  117. } <span class="enscript-keyword">else</span> {
  118. make_digest_ex(md5str, digest, 16);
  119. RETVAL_STRING(md5str, 1);
  120. }
  121. }
  122. <span class="enscript-comment">/* }}} */</span>
  123. <span class="enscript-comment">/*
  124. * The remaining code is the reference MD5 code (md5c.c) from rfc1321
  125. */</span>
  126. <span class="enscript-comment">/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
  127. */</span>
  128. <span class="enscript-comment">/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
  129. rights reserved.
  130. License to copy and use this software is granted provided that it
  131. is identified as the &quot;RSA Data Security, Inc. MD5 Message-Digest
  132. Algorithm&quot; in all material mentioning or referencing this software
  133. or this function.
  134. License is also granted to make and use derivative works provided
  135. that such works are identified as &quot;derived from the RSA Data
  136. Security, Inc. MD5 Message-Digest Algorithm&quot; in all material
  137. mentioning or referencing the derived work.
  138. RSA Data Security, Inc. makes no representations concerning either
  139. the merchantability of this software or the suitability of this
  140. software for any particular purpose. It is provided &quot;as is&quot;
  141. without express or implied warranty of any kind.
  142. These notices must be retained in any copies of any part of this
  143. documentation and/or software.
  144. */</span>
  145. <span class="enscript-comment">/* Constants for MD5Transform routine.
  146. */</span>
  147. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S11</span> 7
  148. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S12</span> 12
  149. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S13</span> 17
  150. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S14</span> 22
  151. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S21</span> 5
  152. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S22</span> 9
  153. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S23</span> 14
  154. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S24</span> 20
  155. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S31</span> 4
  156. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S32</span> 11
  157. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S33</span> 16
  158. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S34</span> 23
  159. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S41</span> 6
  160. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S42</span> 10
  161. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S43</span> 15
  162. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">S44</span> 21
  163. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">MD5Transform</span>(php_uint32[4], <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>[64]);
  164. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">Encode</span>(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *, php_uint32 *, <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span>);
  165. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">Decode</span>(php_uint32 *, <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *, <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span>);
  166. <span class="enscript-type">static</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> PADDING[64] =
  167. {
  168. 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  169. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  170. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
  171. };
  172. <span class="enscript-comment">/* F, G, H and I are basic MD5 functions.
  173. */</span>
  174. #<span class="enscript-reference">define</span> <span class="enscript-function-name">F</span>(x, y, z) (((x) &amp; (y)) | ((~x) &amp; (z)))
  175. #<span class="enscript-reference">define</span> <span class="enscript-function-name">G</span>(x, y, z) (((x) &amp; (z)) | ((y) &amp; (~z)))
  176. #<span class="enscript-reference">define</span> <span class="enscript-function-name">H</span>(x, y, z) ((x) ^ (y) ^ (z))
  177. #<span class="enscript-reference">define</span> <span class="enscript-function-name">I</span>(x, y, z) ((y) ^ ((x) | (~z)))
  178. <span class="enscript-comment">/* ROTATE_LEFT rotates x left n bits.
  179. */</span>
  180. #<span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE_LEFT</span>(x, n) (((x) &lt;&lt; (n)) | ((x) &gt;&gt; (32-(n))))
  181. <span class="enscript-comment">/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
  182. Rotation is separate from addition to prevent recomputation.
  183. */</span>
  184. #<span class="enscript-reference">define</span> <span class="enscript-function-name">FF</span>(a, b, c, d, x, s, ac) { \
  185. (a) += F ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  186. (a) = ROTATE_LEFT ((a), (s)); \
  187. (a) += (b); \
  188. }
  189. #<span class="enscript-reference">define</span> <span class="enscript-function-name">GG</span>(a, b, c, d, x, s, ac) { \
  190. (a) += G ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  191. (a) = ROTATE_LEFT ((a), (s)); \
  192. (a) += (b); \
  193. }
  194. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HH</span>(a, b, c, d, x, s, ac) { \
  195. (a) += H ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  196. (a) = ROTATE_LEFT ((a), (s)); \
  197. (a) += (b); \
  198. }
  199. #<span class="enscript-reference">define</span> <span class="enscript-function-name">II</span>(a, b, c, d, x, s, ac) { \
  200. (a) += I ((b), (c), (d)) + (x) + (php_uint32)(ac); \
  201. (a) = ROTATE_LEFT ((a), (s)); \
  202. (a) += (b); \
  203. }
  204. <span class="enscript-comment">/* {{{ PHP_MD5Init
  205. * MD5 initialization. Begins an MD5 operation, writing a new context.
  206. */</span>
  207. PHPAPI <span class="enscript-type">void</span> <span class="enscript-function-name">PHP_MD5Init</span>(PHP_MD5_CTX * context)
  208. {
  209. context-&gt;count[0] = context-&gt;count[1] = 0;
  210. <span class="enscript-comment">/* Load magic initialization constants.
  211. */</span>
  212. context-&gt;state[0] = 0x67452301;
  213. context-&gt;state[1] = 0xefcdab89;
  214. context-&gt;state[2] = 0x98badcfe;
  215. context-&gt;state[3] = 0x10325476;
  216. }
  217. <span class="enscript-comment">/* }}} */</span>
  218. <span class="enscript-comment">/* {{{ PHP_MD5Update
  219. MD5 block update operation. Continues an MD5 message-digest
  220. operation, processing another message block, and updating the
  221. context.
  222. */</span>
  223. PHPAPI <span class="enscript-type">void</span> <span class="enscript-function-name">PHP_MD5Update</span>(PHP_MD5_CTX * context, <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *input,
  224. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> inputLen)
  225. {
  226. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> i, index, partLen;
  227. <span class="enscript-comment">/* Compute number of bytes mod 64 */</span>
  228. index = (<span class="enscript-type">unsigned</span> <span class="enscript-type">int</span>) ((context-&gt;count[0] &gt;&gt; 3) &amp; 0x3F);
  229. <span class="enscript-comment">/* Update number of bits */</span>
  230. <span class="enscript-keyword">if</span> ((context-&gt;count[0] += ((php_uint32) inputLen &lt;&lt; 3))
  231. &lt; ((php_uint32) inputLen &lt;&lt; 3))
  232. context-&gt;count[1]++;
  233. context-&gt;count[1] += ((php_uint32) inputLen &gt;&gt; 29);
  234. partLen = 64 - index;
  235. <span class="enscript-comment">/* Transform as many times as possible.
  236. */</span>
  237. <span class="enscript-keyword">if</span> (inputLen &gt;= partLen) {
  238. memcpy
  239. ((<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) &amp; context-&gt;buffer[index], (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) input, partLen);
  240. MD5Transform(context-&gt;state, context-&gt;buffer);
  241. <span class="enscript-keyword">for</span> (i = partLen; i + 63 &lt; inputLen; i += 64)
  242. MD5Transform(context-&gt;state, &amp;input[i]);
  243. index = 0;
  244. } <span class="enscript-keyword">else</span>
  245. i = 0;
  246. <span class="enscript-comment">/* Buffer remaining input */</span>
  247. memcpy
  248. ((<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) &amp; context-&gt;buffer[index], (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) &amp; input[i],
  249. inputLen - i);
  250. }
  251. <span class="enscript-comment">/* }}} */</span>
  252. <span class="enscript-comment">/* {{{ PHP_MD5Final
  253. MD5 finalization. Ends an MD5 message-digest operation, writing the
  254. the message digest and zeroizing the context.
  255. */</span>
  256. PHPAPI <span class="enscript-type">void</span> <span class="enscript-function-name">PHP_MD5Final</span>(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> digest[16], PHP_MD5_CTX * context)
  257. {
  258. <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> bits[8];
  259. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> index, padLen;
  260. <span class="enscript-comment">/* Save number of bits */</span>
  261. Encode(bits, context-&gt;count, 8);
  262. <span class="enscript-comment">/* Pad out to 56 mod 64.
  263. */</span>
  264. index = (<span class="enscript-type">unsigned</span> <span class="enscript-type">int</span>) ((context-&gt;count[0] &gt;&gt; 3) &amp; 0x3f);
  265. padLen = (index &lt; 56) ? (56 - index) : (120 - index);
  266. PHP_MD5Update(context, PADDING, padLen);
  267. <span class="enscript-comment">/* Append length (before padding) */</span>
  268. PHP_MD5Update(context, bits, 8);
  269. <span class="enscript-comment">/* Store state in digest */</span>
  270. Encode(digest, context-&gt;state, 16);
  271. <span class="enscript-comment">/* Zeroize sensitive information.
  272. */</span>
  273. memset((<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) context, 0, <span class="enscript-keyword">sizeof</span>(*context));
  274. }
  275. <span class="enscript-comment">/* }}} */</span>
  276. <span class="enscript-comment">/* {{{ MD5Transform
  277. * MD5 basic transformation. Transforms state based on block.
  278. */</span>
  279. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">MD5Transform</span>(state, block)
  280. php_uint32 state[4];
  281. <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> block[64];
  282. {
  283. php_uint32 a = state[0], b = state[1], c = state[2], d = state[3], x[16];
  284. Decode(x, block, 64);
  285. <span class="enscript-comment">/* Round 1 */</span>
  286. FF(a, b, c, d, x[0], S11, 0xd76aa478); <span class="enscript-comment">/* 1 */</span>
  287. FF(d, a, b, c, x[1], S12, 0xe8c7b756); <span class="enscript-comment">/* 2 */</span>
  288. FF(c, d, a, b, x[2], S13, 0x242070db); <span class="enscript-comment">/* 3 */</span>
  289. FF(b, c, d, a, x[3], S14, 0xc1bdceee); <span class="enscript-comment">/* 4 */</span>
  290. FF(a, b, c, d, x[4], S11, 0xf57c0faf); <span class="enscript-comment">/* 5 */</span>
  291. FF(d, a, b, c, x[5], S12, 0x4787c62a); <span class="enscript-comment">/* 6 */</span>
  292. FF(c, d, a, b, x[6], S13, 0xa8304613); <span class="enscript-comment">/* 7 */</span>
  293. FF(b, c, d, a, x[7], S14, 0xfd469501); <span class="enscript-comment">/* 8 */</span>
  294. FF(a, b, c, d, x[8], S11, 0x698098d8); <span class="enscript-comment">/* 9 */</span>
  295. FF(d, a, b, c, x[9], S12, 0x8b44f7af); <span class="enscript-comment">/* 10 */</span>
  296. FF(c, d, a, b, x[10], S13, 0xffff5bb1); <span class="enscript-comment">/* 11 */</span>
  297. FF(b, c, d, a, x[11], S14, 0x895cd7be); <span class="enscript-comment">/* 12 */</span>
  298. FF(a, b, c, d, x[12], S11, 0x6b901122); <span class="enscript-comment">/* 13 */</span>
  299. FF(d, a, b, c, x[13], S12, 0xfd987193); <span class="enscript-comment">/* 14 */</span>
  300. FF(c, d, a, b, x[14], S13, 0xa679438e); <span class="enscript-comment">/* 15 */</span>
  301. FF(b, c, d, a, x[15], S14, 0x49b40821); <span class="enscript-comment">/* 16 */</span>
  302. <span class="enscript-comment">/* Round 2 */</span>
  303. GG(a, b, c, d, x[1], S21, 0xf61e2562); <span class="enscript-comment">/* 17 */</span>
  304. GG(d, a, b, c, x[6], S22, 0xc040b340); <span class="enscript-comment">/* 18 */</span>
  305. GG(c, d, a, b, x[11], S23, 0x265e5a51); <span class="enscript-comment">/* 19 */</span>
  306. GG(b, c, d, a, x[0], S24, 0xe9b6c7aa); <span class="enscript-comment">/* 20 */</span>
  307. GG(a, b, c, d, x[5], S21, 0xd62f105d); <span class="enscript-comment">/* 21 */</span>
  308. GG(d, a, b, c, x[10], S22, 0x2441453); <span class="enscript-comment">/* 22 */</span>
  309. GG(c, d, a, b, x[15], S23, 0xd8a1e681); <span class="enscript-comment">/* 23 */</span>
  310. GG(b, c, d, a, x[4], S24, 0xe7d3fbc8); <span class="enscript-comment">/* 24 */</span>
  311. GG(a, b, c, d, x[9], S21, 0x21e1cde6); <span class="enscript-comment">/* 25 */</span>
  312. GG(d, a, b, c, x[14], S22, 0xc33707d6); <span class="enscript-comment">/* 26 */</span>
  313. GG(c, d, a, b, x[3], S23, 0xf4d50d87); <span class="enscript-comment">/* 27 */</span>
  314. GG(b, c, d, a, x[8], S24, 0x455a14ed); <span class="enscript-comment">/* 28 */</span>
  315. GG(a, b, c, d, x[13], S21, 0xa9e3e905); <span class="enscript-comment">/* 29 */</span>
  316. GG(d, a, b, c, x[2], S22, 0xfcefa3f8); <span class="enscript-comment">/* 30 */</span>
  317. GG(c, d, a, b, x[7], S23, 0x676f02d9); <span class="enscript-comment">/* 31 */</span>
  318. GG(b, c, d, a, x[12], S24, 0x8d2a4c8a); <span class="enscript-comment">/* 32 */</span>
  319. <span class="enscript-comment">/* Round 3 */</span>
  320. HH(a, b, c, d, x[5], S31, 0xfffa3942); <span class="enscript-comment">/* 33 */</span>
  321. HH(d, a, b, c, x[8], S32, 0x8771f681); <span class="enscript-comment">/* 34 */</span>
  322. HH(c, d, a, b, x[11], S33, 0x6d9d6122); <span class="enscript-comment">/* 35 */</span>
  323. HH(b, c, d, a, x[14], S34, 0xfde5380c); <span class="enscript-comment">/* 36 */</span>
  324. HH(a, b, c, d, x[1], S31, 0xa4beea44); <span class="enscript-comment">/* 37 */</span>
  325. HH(d, a, b, c, x[4], S32, 0x4bdecfa9); <span class="enscript-comment">/* 38 */</span>
  326. HH(c, d, a, b, x[7], S33, 0xf6bb4b60); <span class="enscript-comment">/* 39 */</span>
  327. HH(b, c, d, a, x[10], S34, 0xbebfbc70); <span class="enscript-comment">/* 40 */</span>
  328. HH(a, b, c, d, x[13], S31, 0x289b7ec6); <span class="enscript-comment">/* 41 */</span>
  329. HH(d, a, b, c, x[0], S32, 0xeaa127fa); <span class="enscript-comment">/* 42 */</span>
  330. HH(c, d, a, b, x[3], S33, 0xd4ef3085); <span class="enscript-comment">/* 43 */</span>
  331. HH(b, c, d, a, x[6], S34, 0x4881d05); <span class="enscript-comment">/* 44 */</span>
  332. HH(a, b, c, d, x[9], S31, 0xd9d4d039); <span class="enscript-comment">/* 45 */</span>
  333. HH(d, a, b, c, x[12], S32, 0xe6db99e5); <span class="enscript-comment">/* 46 */</span>
  334. HH(c, d, a, b, x[15], S33, 0x1fa27cf8); <span class="enscript-comment">/* 47 */</span>
  335. HH(b, c, d, a, x[2], S34, 0xc4ac5665); <span class="enscript-comment">/* 48 */</span>
  336. <span class="enscript-comment">/* Round 4 */</span>
  337. II(a, b, c, d, x[0], S41, 0xf4292244); <span class="enscript-comment">/* 49 */</span>
  338. II(d, a, b, c, x[7], S42, 0x432aff97); <span class="enscript-comment">/* 50 */</span>
  339. II(c, d, a, b, x[14], S43, 0xab9423a7); <span class="enscript-comment">/* 51 */</span>
  340. II(b, c, d, a, x[5], S44, 0xfc93a039); <span class="enscript-comment">/* 52 */</span>
  341. II(a, b, c, d, x[12], S41, 0x655b59c3); <span class="enscript-comment">/* 53 */</span>
  342. II(d, a, b, c, x[3], S42, 0x8f0ccc92); <span class="enscript-comment">/* 54 */</span>
  343. II(c, d, a, b, x[10], S43, 0xffeff47d); <span class="enscript-comment">/* 55 */</span>
  344. II(b, c, d, a, x[1], S44, 0x85845dd1); <span class="enscript-comment">/* 56 */</span>
  345. II(a, b, c, d, x[8], S41, 0x6fa87e4f); <span class="enscript-comment">/* 57 */</span>
  346. II(d, a, b, c, x[15], S42, 0xfe2ce6e0); <span class="enscript-comment">/* 58 */</span>
  347. II(c, d, a, b, x[6], S43, 0xa3014314); <span class="enscript-comment">/* 59 */</span>
  348. II(b, c, d, a, x[13], S44, 0x4e0811a1); <span class="enscript-comment">/* 60 */</span>
  349. II(a, b, c, d, x[4], S41, 0xf7537e82); <span class="enscript-comment">/* 61 */</span>
  350. II(d, a, b, c, x[11], S42, 0xbd3af235); <span class="enscript-comment">/* 62 */</span>
  351. II(c, d, a, b, x[2], S43, 0x2ad7d2bb); <span class="enscript-comment">/* 63 */</span>
  352. II(b, c, d, a, x[9], S44, 0xeb86d391); <span class="enscript-comment">/* 64 */</span>
  353. state[0] += a;
  354. state[1] += b;
  355. state[2] += c;
  356. state[3] += d;
  357. <span class="enscript-comment">/* Zeroize sensitive information. */</span>
  358. memset((<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>*) x, 0, <span class="enscript-keyword">sizeof</span>(x));
  359. }
  360. <span class="enscript-comment">/* }}} */</span>
  361. <span class="enscript-comment">/* {{{ Encode
  362. Encodes input (php_uint32) into output (unsigned char). Assumes len is
  363. a multiple of 4.
  364. */</span>
  365. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">Encode</span>(output, input, len)
  366. <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *output;
  367. php_uint32 *input;
  368. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> len;
  369. {
  370. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> i, j;
  371. <span class="enscript-keyword">for</span> (i = 0, j = 0; j &lt; len; i++, j += 4) {
  372. output[j] = (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>) (input[i] &amp; 0xff);
  373. output[j + 1] = (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>) ((input[i] &gt;&gt; 8) &amp; 0xff);
  374. output[j + 2] = (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>) ((input[i] &gt;&gt; 16) &amp; 0xff);
  375. output[j + 3] = (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>) ((input[i] &gt;&gt; 24) &amp; 0xff);
  376. }
  377. }
  378. <span class="enscript-comment">/* }}} */</span>
  379. <span class="enscript-comment">/* {{{ Decode
  380. Decodes input (unsigned char) into output (php_uint32). Assumes len is
  381. a multiple of 4.
  382. */</span>
  383. <span class="enscript-type">static</span> <span class="enscript-type">void</span> <span class="enscript-function-name">Decode</span>(output, input, len)
  384. php_uint32 *output;
  385. <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *input;
  386. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> len;
  387. {
  388. <span class="enscript-type">unsigned</span> <span class="enscript-type">int</span> i, j;
  389. <span class="enscript-keyword">for</span> (i = 0, j = 0; j &lt; len; i++, j += 4)
  390. output[i] = ((php_uint32) input[j]) | (((php_uint32) input[j + 1]) &lt;&lt; 8) |
  391. (((php_uint32) input[j + 2]) &lt;&lt; 16) | (((php_uint32) input[j + 3]) &lt;&lt; 24);
  392. }
  393. <span class="enscript-comment">/* }}} */</span>
  394. <span class="enscript-comment">/*
  395. * Local variables:
  396. * tab-width: 4
  397. * c-basic-offset: 4
  398. * End:
  399. * vim600: sw=4 ts=4 fdm=marker
  400. * vim&lt;600: sw=4 ts=4
  401. */</span>
  402. </pre>
  403. <hr />
  404. </body></html>