/opensource.apple.com/source/OpenSSL096/OpenSSL096-15/openssl/crypto/md32_common.h

# · C++ Header · 632 lines · 431 code · 38 blank · 163 comment · 10 complexity · 7b6d99a351b1a615be11b48345d5f946 MD5 · raw file

  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>md32_common.h</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">md32_common.h&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">/* crypto/md32_common.h */</span>
  24. <span class="enscript-comment">/* ====================================================================
  25. * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions
  29. * are met:
  30. *
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. *
  34. * 2. Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in
  36. * the documentation and/or other materials provided with the
  37. * distribution.
  38. *
  39. * 3. All advertising materials mentioning features or use of this
  40. * software must display the following acknowledgment:
  41. * &quot;This product includes software developed by the OpenSSL Project
  42. * for use in the OpenSSL Toolkit. (<a href="http://www.OpenSSL.org/">http://www.OpenSSL.org/</a>)&quot;
  43. *
  44. * 4. The names &quot;OpenSSL Toolkit&quot; and &quot;OpenSSL Project&quot; must not be used to
  45. * endorse or promote products derived from this software without
  46. * prior written permission. For written permission, please contact
  47. * <a href="mailto:licensing@OpenSSL.org">licensing@OpenSSL.org</a>.
  48. *
  49. * 5. Products derived from this software may not be called &quot;OpenSSL&quot;
  50. * nor may &quot;OpenSSL&quot; appear in their names without prior written
  51. * permission of the OpenSSL Project.
  52. *
  53. * 6. Redistributions of any form whatsoever must retain the following
  54. * acknowledgment:
  55. * &quot;This product includes software developed by the OpenSSL Project
  56. * for use in the OpenSSL Toolkit (<a href="http://www.OpenSSL.org/">http://www.OpenSSL.org/</a>)&quot;
  57. *
  58. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  59. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  60. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  61. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  62. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  63. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  64. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  65. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  66. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  67. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  68. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  69. * OF THE POSSIBILITY OF SUCH DAMAGE.
  70. * ====================================================================
  71. *
  72. * This product includes cryptographic software written by Eric Young
  73. * (<a href="mailto:eay@cryptsoft.com">eay@cryptsoft.com</a>). This product includes software written by Tim
  74. * Hudson (<a href="mailto:tjh@cryptsoft.com">tjh@cryptsoft.com</a>).
  75. *
  76. */</span>
  77. <span class="enscript-comment">/*
  78. * This is a generic 32 bit &quot;collector&quot; for message digest algorithms.
  79. * Whenever needed it collects input character stream into chunks of
  80. * 32 bit values and invokes a block function that performs actual hash
  81. * calculations.
  82. *
  83. * Porting guide.
  84. *
  85. * Obligatory macros:
  86. *
  87. * DATA_ORDER_IS_BIG_ENDIAN or DATA_ORDER_IS_LITTLE_ENDIAN
  88. * this macro defines byte order of input stream.
  89. * HASH_CBLOCK
  90. * size of a unit chunk HASH_BLOCK operates on.
  91. * HASH_LONG
  92. * has to be at lest 32 bit wide, if it's wider, then
  93. * HASH_LONG_LOG2 *has to* be defined along
  94. * HASH_CTX
  95. * context structure that at least contains following
  96. * members:
  97. * typedef struct {
  98. * ...
  99. * HASH_LONG Nl,Nh;
  100. * HASH_LONG data[HASH_LBLOCK];
  101. * int num;
  102. * ...
  103. * } HASH_CTX;
  104. * HASH_UPDATE
  105. * name of &quot;Update&quot; function, implemented here.
  106. * HASH_TRANSFORM
  107. * name of &quot;Transform&quot; function, implemented here.
  108. * HASH_FINAL
  109. * name of &quot;Final&quot; function, implemented here.
  110. * HASH_BLOCK_HOST_ORDER
  111. * name of &quot;block&quot; function treating *aligned* input message
  112. * in host byte order, implemented externally.
  113. * HASH_BLOCK_DATA_ORDER
  114. * name of &quot;block&quot; function treating *unaligned* input message
  115. * in original (data) byte order, implemented externally (it
  116. * actually is optional if data and host are of the same
  117. * &quot;endianess&quot;).
  118. * HASH_MAKE_STRING
  119. * macro convering context variables to an ASCII hash string.
  120. *
  121. * Optional macros:
  122. *
  123. * B_ENDIAN or L_ENDIAN
  124. * defines host byte-order.
  125. * HASH_LONG_LOG2
  126. * defaults to 2 if not states otherwise.
  127. * HASH_LBLOCK
  128. * assumed to be HASH_CBLOCK/4 if not stated otherwise.
  129. * HASH_BLOCK_DATA_ORDER_ALIGNED
  130. * alternative &quot;block&quot; function capable of treating
  131. * aligned input message in original (data) order,
  132. * implemented externally.
  133. *
  134. * MD5 example:
  135. *
  136. * #define DATA_ORDER_IS_LITTLE_ENDIAN
  137. *
  138. * #define HASH_LONG MD5_LONG
  139. * #define HASH_LONG_LOG2 MD5_LONG_LOG2
  140. * #define HASH_CTX MD5_CTX
  141. * #define HASH_CBLOCK MD5_CBLOCK
  142. * #define HASH_LBLOCK MD5_LBLOCK
  143. * #define HASH_UPDATE MD5_Update
  144. * #define HASH_TRANSFORM MD5_Transform
  145. * #define HASH_FINAL MD5_Final
  146. * #define HASH_BLOCK_HOST_ORDER md5_block_host_order
  147. * #define HASH_BLOCK_DATA_ORDER md5_block_data_order
  148. *
  149. * &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt;
  150. */</span>
  151. #<span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_BIG_ENDIAN</span>) &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_LITTLE_ENDIAN</span>)
  152. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;DATA_ORDER must be defined!&quot;</span>
  153. #<span class="enscript-reference">endif</span>
  154. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_CBLOCK</span>
  155. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_CBLOCK must be defined!&quot;</span>
  156. #<span class="enscript-reference">endif</span>
  157. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_LONG</span>
  158. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_LONG must be defined!&quot;</span>
  159. #<span class="enscript-reference">endif</span>
  160. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_CTX</span>
  161. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_CTX must be defined!&quot;</span>
  162. #<span class="enscript-reference">endif</span>
  163. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_UPDATE</span>
  164. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_UPDATE must be defined!&quot;</span>
  165. #<span class="enscript-reference">endif</span>
  166. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_TRANSFORM</span>
  167. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_TRANSFORM must be defined!&quot;</span>
  168. #<span class="enscript-reference">endif</span>
  169. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_FINAL</span>
  170. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_FINAL must be defined!&quot;</span>
  171. #<span class="enscript-reference">endif</span>
  172. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_BLOCK_HOST_ORDER</span>
  173. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_BLOCK_HOST_ORDER must be defined!&quot;</span>
  174. #<span class="enscript-reference">endif</span>
  175. #<span class="enscript-reference">if</span> 0
  176. <span class="enscript-comment">/*
  177. * Moved below as it's required only if HASH_BLOCK_DATA_ORDER_ALIGNED
  178. * isn't defined.
  179. */</span>
  180. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>
  181. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_BLOCK_DATA_ORDER must be defined!&quot;</span>
  182. #<span class="enscript-reference">endif</span>
  183. #<span class="enscript-reference">endif</span>
  184. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_LBLOCK</span>
  185. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">HASH_LBLOCK</span> (HASH_CBLOCK/4)
  186. #<span class="enscript-reference">endif</span>
  187. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_LONG_LOG2</span>
  188. #<span class="enscript-reference">define</span> <span class="enscript-variable-name">HASH_LONG_LOG2</span> 2
  189. #<span class="enscript-reference">endif</span>
  190. <span class="enscript-comment">/*
  191. * Engage compiler specific rotate intrinsic function if available.
  192. */</span>
  193. #<span class="enscript-reference">undef</span> <span class="enscript-variable-name">ROTATE</span>
  194. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">PEDANTIC</span>
  195. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">_MSC_VER</span>)
  196. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) _lrotl(a,n)
  197. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__MWERKS__</span>)
  198. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__POWERPC__</span>)
  199. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) __rlwinm(a,n,0,31)
  200. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__MC68K__</span>)
  201. <span class="enscript-comment">/* Motorola specific tweak. &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt; */</span>
  202. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) ( n&lt;24 ? __rol(a,n) : __ror(a,32-n) )
  203. # <span class="enscript-reference">else</span>
  204. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) __rol(a,n)
  205. # <span class="enscript-reference">endif</span>
  206. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__GNUC__</span>) &amp;&amp; <span class="enscript-variable-name">__GNUC__</span>&gt;=2 &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">NO_ASM</span>) &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">NO_INLINE_ASM</span>)
  207. <span class="enscript-comment">/*
  208. * Some GNU C inline assembler templates. Note that these are
  209. * rotates by *constant* number of bits! But that's exactly
  210. * what we need here...
  211. *
  212. * &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt;
  213. */</span>
  214. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__i386</span>) || <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__i386__</span>)
  215. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) ({ register unsigned int ret; \
  216. asm ( \
  217. <span class="enscript-string">&quot;roll %1,%0&quot;</span> \
  218. : <span class="enscript-string">&quot;=r&quot;</span>(ret) \
  219. : <span class="enscript-string">&quot;I&quot;</span>(n), <span class="enscript-string">&quot;0&quot;</span>(a) \
  220. : <span class="enscript-string">&quot;cc&quot;</span>); \
  221. ret; \
  222. })
  223. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__powerpc</span>) || <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__ppc</span>)
  224. # <span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) ({ register unsigned int ret; \
  225. asm ( \
  226. <span class="enscript-string">&quot;rlwinm %0,%1,%2,0,31&quot;</span> \
  227. : <span class="enscript-string">&quot;=r&quot;</span>(ret) \
  228. : <span class="enscript-string">&quot;r&quot;</span>(a), <span class="enscript-string">&quot;I&quot;</span>(n)); \
  229. ret; \
  230. })
  231. # <span class="enscript-reference">endif</span>
  232. # <span class="enscript-reference">endif</span>
  233. <span class="enscript-comment">/*
  234. * Engage compiler specific &quot;fetch in reverse byte order&quot;
  235. * intrinsic function if available.
  236. */</span>
  237. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__GNUC__</span>) &amp;&amp; <span class="enscript-variable-name">__GNUC__</span>&gt;=2 &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">NO_ASM</span>) &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">NO_INLINE_ASM</span>)
  238. <span class="enscript-comment">/* some GNU C inline assembler templates by &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt; */</span>
  239. # <span class="enscript-reference">if</span> (<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__i386</span>) || <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__i386__</span>)) &amp;&amp; !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">I386_ONLY</span>)
  240. # <span class="enscript-reference">define</span> <span class="enscript-function-name">BE_FETCH32</span>(a) ({ register unsigned int l=(a);\
  241. asm ( \
  242. <span class="enscript-string">&quot;bswapl %0&quot;</span> \
  243. : <span class="enscript-string">&quot;=r&quot;</span>(l) : <span class="enscript-string">&quot;0&quot;</span>(l)); \
  244. l; \
  245. })
  246. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__powerpc</span>)
  247. # <span class="enscript-reference">define</span> <span class="enscript-function-name">LE_FETCH32</span>(a) ({ register unsigned int l; \
  248. asm ( \
  249. <span class="enscript-string">&quot;lwbrx %0,0,%1&quot;</span> \
  250. : <span class="enscript-string">&quot;=r&quot;</span>(l) \
  251. : <span class="enscript-string">&quot;r&quot;</span>(a)); \
  252. l; \
  253. })
  254. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">__sparc</span>) &amp;&amp; <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">ULTRASPARC</span>)
  255. # <span class="enscript-reference">define</span> <span class="enscript-function-name">LE_FETCH32</span>(a) ({ register unsigned int l; \
  256. asm ( \
  257. <span class="enscript-string">&quot;lda [%1]#ASI_PRIMARY_LITTLE,%0&quot;</span>\
  258. : <span class="enscript-string">&quot;=r&quot;</span>(l) \
  259. : <span class="enscript-string">&quot;r&quot;</span>(a)); \
  260. l; \
  261. })
  262. # <span class="enscript-reference">endif</span>
  263. # <span class="enscript-reference">endif</span>
  264. #<span class="enscript-reference">endif</span> <span class="enscript-comment">/* PEDANTIC */</span>
  265. #<span class="enscript-reference">if</span> <span class="enscript-variable-name">HASH_LONG_LOG2</span>==2 <span class="enscript-comment">/* Engage only if sizeof(HASH_LONG)== 4 */</span>
  266. <span class="enscript-comment">/* A nice byte order reversal from Wei Dai &lt;<a href="mailto:weidai@eskimo.com">weidai@eskimo.com</a>&gt; */</span>
  267. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">ROTATE</span>
  268. <span class="enscript-comment">/* 5 instructions with rotate instruction, else 9 */</span>
  269. #<span class="enscript-reference">define</span> <span class="enscript-function-name">REVERSE_FETCH32</span>(a,l) ( \
  270. l=*(<span class="enscript-type">const</span> HASH_LONG *)(a), \
  271. ((ROTATE(l,8)&amp;0x00FF00FF)|(ROTATE((l&amp;0x00FF00FF),24))) \
  272. )
  273. #<span class="enscript-reference">else</span>
  274. <span class="enscript-comment">/* 6 instructions with rotate instruction, else 8 */</span>
  275. #<span class="enscript-reference">define</span> <span class="enscript-function-name">REVERSE_FETCH32</span>(a,l) ( \
  276. l=*(<span class="enscript-type">const</span> HASH_LONG *)(a), \
  277. l=(((l&gt;&gt;8)&amp;0x00FF00FF)|((l&amp;0x00FF00FF)&lt;&lt;8)), \
  278. ROTATE(l,16) \
  279. )
  280. <span class="enscript-comment">/*
  281. * Originally the middle line started with l=(((l&amp;0xFF00FF00)&gt;&gt;8)|...
  282. * It's rewritten as above for two reasons:
  283. * - RISCs aren't good at long constants and have to explicitely
  284. * compose 'em with several (well, usually 2) instructions in a
  285. * register before performing the actual operation and (as you
  286. * already realized:-) having same constant should inspire the
  287. * compiler to permanently allocate the only register for it;
  288. * - most modern CPUs have two ALUs, but usually only one has
  289. * circuitry for shifts:-( this minor tweak inspires compiler
  290. * to schedule shift instructions in a better way...
  291. *
  292. * &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt;
  293. */</span>
  294. #<span class="enscript-reference">endif</span>
  295. #<span class="enscript-reference">endif</span>
  296. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">ROTATE</span>
  297. #<span class="enscript-reference">define</span> <span class="enscript-function-name">ROTATE</span>(a,n) (((a)&lt;&lt;(n))|(((a)&amp;0xffffffff)&gt;&gt;(32-(n))))
  298. #<span class="enscript-reference">endif</span>
  299. <span class="enscript-comment">/*
  300. * Make some obvious choices. E.g., HASH_BLOCK_DATA_ORDER_ALIGNED
  301. * and HASH_BLOCK_HOST_ORDER ought to be the same if input data
  302. * and host are of the same &quot;endianess&quot;. It's possible to mask
  303. * this with blank #define HASH_BLOCK_DATA_ORDER though...
  304. *
  305. * &lt;<a href="mailto:appro@fy.chalmers.se">appro@fy.chalmers.se</a>&gt;
  306. */</span>
  307. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">B_ENDIAN</span>)
  308. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_BIG_ENDIAN</span>)
  309. # <span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span>) &amp;&amp; <span class="enscript-variable-name">HASH_LONG_LOG2</span>==2
  310. # <span class="enscript-reference">define</span> <span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span> HASH_BLOCK_HOST_ORDER
  311. # <span class="enscript-reference">endif</span>
  312. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_LITTLE_ENDIAN</span>)
  313. # <span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HOST_FETCH32</span>
  314. # <span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">LE_FETCH32</span>
  315. # <span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_FETCH32</span>(p,l) LE_FETCH32(p)
  316. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">REVERSE_FETCH32</span>)
  317. # <span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_FETCH32</span>(p,l) REVERSE_FETCH32(p,l)
  318. # <span class="enscript-reference">endif</span>
  319. # <span class="enscript-reference">endif</span>
  320. # <span class="enscript-reference">endif</span>
  321. #<span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">L_ENDIAN</span>)
  322. # <span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_LITTLE_ENDIAN</span>)
  323. # <span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span>) &amp;&amp; <span class="enscript-variable-name">HASH_LONG_LOG2</span>==2
  324. # <span class="enscript-reference">define</span> <span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span> HASH_BLOCK_HOST_ORDER
  325. # <span class="enscript-reference">endif</span>
  326. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_BIG_ENDIAN</span>)
  327. # <span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HOST_FETCH32</span>
  328. # <span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">BE_FETCH32</span>
  329. # <span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_FETCH32</span>(p,l) BE_FETCH32(p)
  330. # <span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">REVERSE_FETCH32</span>)
  331. # <span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_FETCH32</span>(p,l) REVERSE_FETCH32(p,l)
  332. # <span class="enscript-reference">endif</span>
  333. # <span class="enscript-reference">endif</span>
  334. # <span class="enscript-reference">endif</span>
  335. #<span class="enscript-reference">endif</span>
  336. #<span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span>)
  337. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>
  338. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_BLOCK_DATA_ORDER must be defined!&quot;</span>
  339. #<span class="enscript-reference">endif</span>
  340. #<span class="enscript-reference">endif</span>
  341. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_BIG_ENDIAN</span>)
  342. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_c2l</span>(c,l) (l =(((unsigned long)(*((c)++)))&lt;&lt;24), \
  343. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16), \
  344. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8), \
  345. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++))) ), \
  346. l)
  347. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_p_c2l</span>(c,l,n) { \
  348. <span class="enscript-keyword">switch</span> (n) { \
  349. <span class="enscript-keyword">case</span> <span class="enscript-reference">0</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;24; \
  350. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16; \
  351. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8; \
  352. <span class="enscript-keyword">case</span> <span class="enscript-reference">3</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++))); \
  353. } }
  354. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_p_c2l_p</span>(c,l,sc,len) { \
  355. <span class="enscript-keyword">switch</span> (sc) { \
  356. <span class="enscript-keyword">case</span> <span class="enscript-reference">0</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;24; \
  357. <span class="enscript-keyword">if</span> (--len == 0) <span class="enscript-keyword">break</span>; \
  358. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16; \
  359. <span class="enscript-keyword">if</span> (--len == 0) <span class="enscript-keyword">break</span>; \
  360. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8; \
  361. } }
  362. <span class="enscript-comment">/* NOTE the pointer is not incremented at the end of this */</span>
  363. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_c2l_p</span>(c,l,n) { \
  364. l=0; (c)+=n; \
  365. <span class="enscript-keyword">switch</span> (n) { \
  366. <span class="enscript-keyword">case</span> <span class="enscript-reference">3</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c))))&lt;&lt; 8; \
  367. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c))))&lt;&lt;16; \
  368. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c))))&lt;&lt;24; \
  369. } }
  370. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_l2c</span>(l,c) (*((c)++)=(unsigned char)(((l)&gt;&gt;24)&amp;0xff), \
  371. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l)&gt;&gt;16)&amp;0xff), \
  372. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l)&gt;&gt; 8)&amp;0xff), \
  373. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l) )&amp;0xff), \
  374. l)
  375. #<span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_LITTLE_ENDIAN</span>)
  376. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_c2l</span>(c,l) (l =(((unsigned long)(*((c)++))) ), \
  377. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8), \
  378. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16), \
  379. l|=(((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;24), \
  380. l)
  381. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_p_c2l</span>(c,l,n) { \
  382. <span class="enscript-keyword">switch</span> (n) { \
  383. <span class="enscript-keyword">case</span> <span class="enscript-reference">0</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++))); \
  384. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8; \
  385. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16; \
  386. <span class="enscript-keyword">case</span> <span class="enscript-reference">3</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;24; \
  387. } }
  388. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_p_c2l_p</span>(c,l,sc,len) { \
  389. <span class="enscript-keyword">switch</span> (sc) { \
  390. <span class="enscript-keyword">case</span> <span class="enscript-reference">0</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++))); \
  391. <span class="enscript-keyword">if</span> (--len == 0) <span class="enscript-keyword">break</span>; \
  392. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt; 8; \
  393. <span class="enscript-keyword">if</span> (--len == 0) <span class="enscript-keyword">break</span>; \
  394. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*((c)++)))&lt;&lt;16; \
  395. } }
  396. <span class="enscript-comment">/* NOTE the pointer is not incremented at the end of this */</span>
  397. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_c2l_p</span>(c,l,n) { \
  398. l=0; (c)+=n; \
  399. <span class="enscript-keyword">switch</span> (n) { \
  400. <span class="enscript-keyword">case</span> <span class="enscript-reference">3</span>: l =((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c))))&lt;&lt;16; \
  401. <span class="enscript-keyword">case</span> <span class="enscript-reference">2</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c))))&lt;&lt; 8; \
  402. <span class="enscript-keyword">case</span> <span class="enscript-reference">1</span>: l|=((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)(*(--(c)))); \
  403. } }
  404. #<span class="enscript-reference">define</span> <span class="enscript-function-name">HOST_l2c</span>(l,c) (*((c)++)=(unsigned char)(((l) )&amp;0xff), \
  405. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l)&gt;&gt; 8)&amp;0xff), \
  406. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l)&gt;&gt;16)&amp;0xff), \
  407. *((c)++)=(<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span>)(((l)&gt;&gt;24)&amp;0xff), \
  408. l)
  409. #<span class="enscript-reference">endif</span>
  410. <span class="enscript-comment">/*
  411. * Time for some action:-)
  412. */</span>
  413. <span class="enscript-type">void</span> <span class="enscript-function-name">HASH_UPDATE</span> (HASH_CTX *c, <span class="enscript-type">const</span> <span class="enscript-type">void</span> *data_, <span class="enscript-type">unsigned</span> <span class="enscript-type">long</span> len)
  414. {
  415. <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *data=data_;
  416. <span class="enscript-type">register</span> HASH_LONG * p;
  417. <span class="enscript-type">register</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">long</span> l;
  418. <span class="enscript-type">int</span> sw,sc,ew,ec;
  419. <span class="enscript-keyword">if</span> (len==0) <span class="enscript-keyword">return</span>;
  420. l=(c-&gt;Nl+(len&lt;&lt;3))&amp;0xffffffffL;
  421. <span class="enscript-comment">/* 95-05-24 eay Fixed a bug with the overflow handling, thanks to
  422. * Wei Dai &lt;<a href="mailto:weidai@eskimo.com">weidai@eskimo.com</a>&gt; for pointing it out. */</span>
  423. <span class="enscript-keyword">if</span> (l &lt; c-&gt;Nl) <span class="enscript-comment">/* overflow */</span>
  424. c-&gt;Nh++;
  425. c-&gt;Nh+=(len&gt;&gt;29);
  426. c-&gt;Nl=l;
  427. <span class="enscript-keyword">if</span> (c-&gt;num != 0)
  428. {
  429. p=c-&gt;data;
  430. sw=c-&gt;num&gt;&gt;2;
  431. sc=c-&gt;num&amp;0x03;
  432. <span class="enscript-keyword">if</span> ((c-&gt;num+len) &gt;= HASH_CBLOCK)
  433. {
  434. l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
  435. <span class="enscript-keyword">for</span> (; sw&lt;HASH_LBLOCK; sw++)
  436. {
  437. HOST_c2l(data,l); p[sw]=l;
  438. }
  439. HASH_BLOCK_HOST_ORDER (c,p,1);
  440. len-=(HASH_CBLOCK-c-&gt;num);
  441. c-&gt;num=0;
  442. <span class="enscript-comment">/* drop through and do the rest */</span>
  443. }
  444. <span class="enscript-keyword">else</span>
  445. {
  446. c-&gt;num+=len;
  447. <span class="enscript-keyword">if</span> ((sc+len) &lt; 4) <span class="enscript-comment">/* ugly, add char's to a word */</span>
  448. {
  449. l=p[sw]; HOST_p_c2l_p(data,l,sc,len); p[sw]=l;
  450. }
  451. <span class="enscript-keyword">else</span>
  452. {
  453. ew=(c-&gt;num&gt;&gt;2);
  454. ec=(c-&gt;num&amp;0x03);
  455. l=p[sw]; HOST_p_c2l(data,l,sc); p[sw++]=l;
  456. <span class="enscript-keyword">for</span> (; sw &lt; ew; sw++)
  457. {
  458. HOST_c2l(data,l); p[sw]=l;
  459. }
  460. <span class="enscript-keyword">if</span> (ec)
  461. {
  462. HOST_c2l_p(data,l,ec); p[sw]=l;
  463. }
  464. }
  465. <span class="enscript-keyword">return</span>;
  466. }
  467. }
  468. sw=len/HASH_CBLOCK;
  469. <span class="enscript-keyword">if</span> (sw &gt; 0)
  470. {
  471. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span>)
  472. <span class="enscript-comment">/*
  473. * Note that HASH_BLOCK_DATA_ORDER_ALIGNED gets defined
  474. * only if sizeof(HASH_LONG)==4.
  475. */</span>
  476. <span class="enscript-keyword">if</span> ((((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)data)%4) == 0)
  477. {
  478. <span class="enscript-comment">/* data is properly aligned so that we can cast it: */</span>
  479. HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,sw);
  480. sw*=HASH_CBLOCK;
  481. data+=sw;
  482. len-=sw;
  483. }
  484. <span class="enscript-keyword">else</span>
  485. #<span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>)
  486. <span class="enscript-keyword">while</span> (sw--)
  487. {
  488. memcpy (p=c-&gt;data,data,HASH_CBLOCK);
  489. HASH_BLOCK_DATA_ORDER_ALIGNED(c,p,1);
  490. data+=HASH_CBLOCK;
  491. len-=HASH_CBLOCK;
  492. }
  493. #<span class="enscript-reference">endif</span>
  494. #<span class="enscript-reference">endif</span>
  495. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>)
  496. {
  497. HASH_BLOCK_DATA_ORDER(c,data,sw);
  498. sw*=HASH_CBLOCK;
  499. data+=sw;
  500. len-=sw;
  501. }
  502. #<span class="enscript-reference">endif</span>
  503. }
  504. <span class="enscript-keyword">if</span> (len!=0)
  505. {
  506. p = c-&gt;data;
  507. c-&gt;num = len;
  508. ew=len&gt;&gt;2; <span class="enscript-comment">/* words to copy */</span>
  509. ec=len&amp;0x03;
  510. <span class="enscript-keyword">for</span> (; ew; ew--,p++)
  511. {
  512. HOST_c2l(data,l); *p=l;
  513. }
  514. HOST_c2l_p(data,l,ec);
  515. *p=l;
  516. }
  517. }
  518. <span class="enscript-type">void</span> <span class="enscript-function-name">HASH_TRANSFORM</span> (HASH_CTX *c, <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *data)
  519. {
  520. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER_ALIGNED</span>)
  521. <span class="enscript-keyword">if</span> ((((<span class="enscript-type">unsigned</span> <span class="enscript-type">long</span>)data)%4) == 0)
  522. <span class="enscript-comment">/* data is properly aligned so that we can cast it: */</span>
  523. HASH_BLOCK_DATA_ORDER_ALIGNED (c,(HASH_LONG *)data,1);
  524. <span class="enscript-keyword">else</span>
  525. #<span class="enscript-reference">if</span> !<span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>)
  526. {
  527. memcpy (c-&gt;data,data,HASH_CBLOCK);
  528. HASH_BLOCK_DATA_ORDER_ALIGNED (c,c-&gt;data,1);
  529. }
  530. #<span class="enscript-reference">endif</span>
  531. #<span class="enscript-reference">endif</span>
  532. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">HASH_BLOCK_DATA_ORDER</span>)
  533. HASH_BLOCK_DATA_ORDER (c,data,1);
  534. #<span class="enscript-reference">endif</span>
  535. }
  536. <span class="enscript-type">void</span> <span class="enscript-function-name">HASH_FINAL</span> (<span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *md, HASH_CTX *c)
  537. {
  538. <span class="enscript-type">register</span> HASH_LONG *p;
  539. <span class="enscript-type">register</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">long</span> l;
  540. <span class="enscript-type">register</span> <span class="enscript-type">int</span> i,j;
  541. <span class="enscript-type">static</span> <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> end[4]={0x80,0x00,0x00,0x00};
  542. <span class="enscript-type">const</span> <span class="enscript-type">unsigned</span> <span class="enscript-type">char</span> *cp=end;
  543. <span class="enscript-comment">/* c-&gt;num should definitly have room for at least one more byte. */</span>
  544. p=c-&gt;data;
  545. i=c-&gt;num&gt;&gt;2;
  546. j=c-&gt;num&amp;0x03;
  547. #<span class="enscript-reference">if</span> 0
  548. <span class="enscript-comment">/* purify often complains about the following line as an
  549. * Uninitialized Memory Read. While this can be true, the
  550. * following p_c2l macro will reset l when that case is true.
  551. * This is because j&amp;0x03 contains the number of 'valid' bytes
  552. * already in p[i]. If and only if j&amp;0x03 == 0, the UMR will
  553. * occur but this is also the only time p_c2l will do
  554. * l= *(cp++) instead of l|= *(cp++)
  555. * Many thanks to Alex Tang &lt;<a href="mailto:altitude@cic.net">altitude@cic.net</a>&gt; for pickup this
  556. * 'potential bug' */</span>
  557. #<span class="enscript-reference">ifdef</span> <span class="enscript-variable-name">PURIFY</span>
  558. <span class="enscript-keyword">if</span> (j==0) p[i]=0; <span class="enscript-comment">/* Yeah, but that's not the way to fix it:-) */</span>
  559. #<span class="enscript-reference">endif</span>
  560. l=p[i];
  561. #<span class="enscript-reference">else</span>
  562. l = (j==0) ? 0 : p[i];
  563. #<span class="enscript-reference">endif</span>
  564. HOST_p_c2l(cp,l,j); p[i++]=l; <span class="enscript-comment">/* i is the next 'undefined word' */</span>
  565. <span class="enscript-keyword">if</span> (i&gt;(HASH_LBLOCK-2)) <span class="enscript-comment">/* save room for Nl and Nh */</span>
  566. {
  567. <span class="enscript-keyword">if</span> (i&lt;HASH_LBLOCK) p[i]=0;
  568. HASH_BLOCK_HOST_ORDER (c,p,1);
  569. i=0;
  570. }
  571. <span class="enscript-keyword">for</span> (; i&lt;(HASH_LBLOCK-2); i++)
  572. p[i]=0;
  573. #<span class="enscript-reference">if</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_BIG_ENDIAN</span>)
  574. p[HASH_LBLOCK-2]=c-&gt;Nh;
  575. p[HASH_LBLOCK-1]=c-&gt;Nl;
  576. #<span class="enscript-reference">elif</span> <span class="enscript-reference">defined</span>(<span class="enscript-variable-name">DATA_ORDER_IS_LITTLE_ENDIAN</span>)
  577. p[HASH_LBLOCK-2]=c-&gt;Nl;
  578. p[HASH_LBLOCK-1]=c-&gt;Nh;
  579. #<span class="enscript-reference">endif</span>
  580. HASH_BLOCK_HOST_ORDER (c,p,1);
  581. #<span class="enscript-reference">ifndef</span> <span class="enscript-variable-name">HASH_MAKE_STRING</span>
  582. #<span class="enscript-reference">error</span> <span class="enscript-string">&quot;HASH_MAKE_STRING must be defined!&quot;</span>
  583. #<span class="enscript-reference">else</span>
  584. HASH_MAKE_STRING(c,md);
  585. #<span class="enscript-reference">endif</span>
  586. c-&gt;num=0;
  587. <span class="enscript-comment">/* clear stuff, HASH_BLOCK may be leaving some stuff on the stack
  588. * but I'm not worried :-)
  589. OPENSSL_cleanse((void *)c,sizeof(HASH_CTX));
  590. */</span>
  591. }
  592. </pre>
  593. <hr />
  594. </body></html>