Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 2aa62c5bbb658df1fece777472a7bcf2 > files > 2394

barry-devel-docs-0.17-0.3.20100730git.fc14.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>Barry: sha1.cc Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<!-- Generated by Doxygen 1.6.2-20100208 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Main&nbsp;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li><a href="dirs.html"><span>Directories</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.html"><span>File&nbsp;List</span></a></li>
    </ul>
  </div>
  <div class="navpath"><a class="el" href="dir_65781e9408669bb8d7d9c025fdb5fe86.html">src</a>
  </div>
</div>
<div class="contents">
<h1>sha1.cc</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span>
<a name="l00002"></a>00002 <span class="comment"> * The contents of this file are subject to the Mozilla Public</span>
<a name="l00003"></a>00003 <span class="comment"> * License Version 1.1 (the &quot;License&quot;); you may not use this file</span>
<a name="l00004"></a>00004 <span class="comment"> * except in compliance with the License. You may obtain a copy of</span>
<a name="l00005"></a>00005 <span class="comment"> * the License at http://www.mozilla.org/MPL/</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Software distributed under the License is distributed on an &quot;AS</span>
<a name="l00008"></a>00008 <span class="comment"> * IS&quot; basis, WITHOUT WARRANTY OF ANY KIND, either express or</span>
<a name="l00009"></a>00009 <span class="comment"> * implied. See the License for the specific language governing</span>
<a name="l00010"></a>00010 <span class="comment"> * rights and limitations under the License.</span>
<a name="l00011"></a>00011 <span class="comment"> *</span>
<a name="l00012"></a>00012 <span class="comment"> * The Original Code is SHA 180-1 Reference Implementation (Compact version)</span>
<a name="l00013"></a>00013 <span class="comment"> *</span>
<a name="l00014"></a>00014 <span class="comment"> * The Initial Developer of the Original Code is Paul Kocher of</span>
<a name="l00015"></a>00015 <span class="comment"> * Cryptography Research.  Portions created by Paul Kocher are</span>
<a name="l00016"></a>00016 <span class="comment"> * Copyright (C) 1995-9 by Cryptography Research, Inc.  All</span>
<a name="l00017"></a>00017 <span class="comment"> * Rights Reserved.</span>
<a name="l00018"></a>00018 <span class="comment"> *</span>
<a name="l00019"></a>00019 <span class="comment"> * Contributor(s):</span>
<a name="l00020"></a>00020 <span class="comment"> *</span>
<a name="l00021"></a>00021 <span class="comment"> *     Paul Kocher</span>
<a name="l00022"></a>00022 <span class="comment"> *</span>
<a name="l00023"></a>00023 <span class="comment"> * Alternatively, the contents of this file may be used under the</span>
<a name="l00024"></a>00024 <span class="comment"> * terms of the GNU General Public License Version 2 or later (the</span>
<a name="l00025"></a>00025 <span class="comment"> * &quot;GPL&quot;), in which case the provisions of the GPL are applicable</span>
<a name="l00026"></a>00026 <span class="comment"> * instead of those above.  If you wish to allow use of your</span>
<a name="l00027"></a>00027 <span class="comment"> * version of this file only under the terms of the GPL and not to</span>
<a name="l00028"></a>00028 <span class="comment"> * allow others to use your version of this file under the MPL,</span>
<a name="l00029"></a>00029 <span class="comment"> * indicate your decision by deleting the provisions above and</span>
<a name="l00030"></a>00030 <span class="comment"> * replace them with the notice and other provisions required by</span>
<a name="l00031"></a>00031 <span class="comment"> * the GPL.  If you do not delete the provisions above, a recipient</span>
<a name="l00032"></a>00032 <span class="comment"> * may use your version of this file under either the MPL or the</span>
<a name="l00033"></a>00033 <span class="comment"> * GPL.</span>
<a name="l00034"></a>00034 <span class="comment"> */</span>
<a name="l00035"></a>00035 
<a name="l00036"></a>00036 <span class="comment">/*</span>
<a name="l00037"></a>00037 <span class="comment">Copied from the git sources, with the following revision history:</span>
<a name="l00038"></a>00038 <span class="comment"></span>
<a name="l00039"></a>00039 <span class="comment">commit 77ab8798d3f8df39877235be17bb6e70077aaba2</span>
<a name="l00040"></a>00040 <span class="comment">Author: Junio C Hamano &lt;junkio@cox.net&gt;</span>
<a name="l00041"></a>00041 <span class="comment">Date:   Tue Nov 1 10:56:03 2005 -0800</span>
<a name="l00042"></a>00042 <span class="comment"></span>
<a name="l00043"></a>00043 <span class="comment">    Fix constness of input in mozilla-sha1/sha1.c::SHA1_Update().</span>
<a name="l00044"></a>00044 <span class="comment"></span>
<a name="l00045"></a>00045 <span class="comment">    Among the three of our own implementations, only this one lacked</span>
<a name="l00046"></a>00046 <span class="comment">    &quot;const&quot; from the second argument.</span>
<a name="l00047"></a>00047 <span class="comment"></span>
<a name="l00048"></a>00048 <span class="comment">    Signed-off-by: Junio C Hamano &lt;junkio@cox.net&gt;</span>
<a name="l00049"></a>00049 <span class="comment"></span>
<a name="l00050"></a>00050 <span class="comment">commit cef661fc799a3a13ffdea4a3f69f1acd295de53d</span>
<a name="l00051"></a>00051 <span class="comment">Author: Linus Torvalds &lt;torvalds@ppc970.osdl.org&gt;</span>
<a name="l00052"></a>00052 <span class="comment">Date:   Thu Apr 21 12:33:22 2005 -0700</span>
<a name="l00053"></a>00053 <span class="comment"></span>
<a name="l00054"></a>00054 <span class="comment">    Add support for alternate SHA1 library implementations.</span>
<a name="l00055"></a>00055 <span class="comment"></span>
<a name="l00056"></a>00056 <span class="comment">    This one includes the Mozilla SHA1 implementation sent in by Edgar Toernig.</span>
<a name="l00057"></a>00057 <span class="comment">    It&#39;s dual-licenced under MPL-1.1 or GPL, so in the context of git, we</span>
<a name="l00058"></a>00058 <span class="comment">    obviously use the GPL version.</span>
<a name="l00059"></a>00059 <span class="comment"></span>
<a name="l00060"></a>00060 <span class="comment">    Side note: the Mozilla SHA1 implementation is about twice as fast as the</span>
<a name="l00061"></a>00061 <span class="comment">    default openssl one on my G5, but the default openssl one has optimized</span>
<a name="l00062"></a>00062 <span class="comment">    x86 assembly language on x86. So choose wisely.</span>
<a name="l00063"></a>00063 <span class="comment"></span>
<a name="l00064"></a>00064 <span class="comment">*/</span>
<a name="l00065"></a>00065 
<a name="l00066"></a>00066 <span class="preprocessor">#include &quot;sha1.h&quot;</span>
<a name="l00067"></a>00067 
<a name="l00068"></a>00068 <span class="keyword">namespace </span>Barry {
<a name="l00069"></a>00069 
<a name="l00070"></a>00070 <span class="keyword">static</span> <span class="keywordtype">void</span> shaHashBlock(SHA_CTX *ctx);
<a name="l00071"></a>00071 
<a name="l00072"></a>00072 <span class="keywordtype">void</span> SHA1(<span class="keyword">const</span> <span class="keywordtype">void</span> *dataIn, <span class="keywordtype">int</span> len, <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *hashout)
<a name="l00073"></a>00073 {
<a name="l00074"></a>00074         SHA_CTX ctx;
<a name="l00075"></a>00075         SHA1_Init(&amp;ctx);
<a name="l00076"></a>00076         SHA1_Update(&amp;ctx, dataIn, len);
<a name="l00077"></a>00077         SHA1_Final(hashout, &amp;ctx);
<a name="l00078"></a>00078 }
<a name="l00079"></a>00079 
<a name="l00080"></a>00080 <span class="keywordtype">void</span> SHA1_Init(SHA_CTX *ctx) {
<a name="l00081"></a>00081   <span class="keywordtype">int</span> i;
<a name="l00082"></a>00082 
<a name="l00083"></a>00083   ctx-&gt;lenW = 0;
<a name="l00084"></a>00084   ctx-&gt;sizeHi = ctx-&gt;sizeLo = 0;
<a name="l00085"></a>00085 
<a name="l00086"></a>00086   <span class="comment">/* Initialize H with the magic constants (see FIPS180 for constants)</span>
<a name="l00087"></a>00087 <span class="comment">   */</span>
<a name="l00088"></a>00088   ctx-&gt;H[0] = 0x67452301;
<a name="l00089"></a>00089   ctx-&gt;H[1] = 0xefcdab89;
<a name="l00090"></a>00090   ctx-&gt;H[2] = 0x98badcfe;
<a name="l00091"></a>00091   ctx-&gt;H[3] = 0x10325476;
<a name="l00092"></a>00092   ctx-&gt;H[4] = 0xc3d2e1f0;
<a name="l00093"></a>00093 
<a name="l00094"></a>00094   <span class="keywordflow">for</span> (i = 0; i &lt; 80; i++)
<a name="l00095"></a>00095     ctx-&gt;W[i] = 0;
<a name="l00096"></a>00096 }
<a name="l00097"></a>00097 
<a name="l00098"></a>00098 
<a name="l00099"></a>00099 <span class="keywordtype">void</span> SHA1_Update(SHA_CTX *ctx, <span class="keyword">const</span> <span class="keywordtype">void</span> *_dataIn, <span class="keywordtype">int</span> len) {
<a name="l00100"></a>00100   <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *dataIn = (<span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> *) _dataIn;
<a name="l00101"></a>00101   <span class="keywordtype">int</span> i;
<a name="l00102"></a>00102 
<a name="l00103"></a>00103   <span class="comment">/* Read the data into W and process blocks as they get full</span>
<a name="l00104"></a>00104 <span class="comment">   */</span>
<a name="l00105"></a>00105   <span class="keywordflow">for</span> (i = 0; i &lt; len; i++) {
<a name="l00106"></a>00106     ctx-&gt;W[ctx-&gt;lenW / 4] &lt;&lt;= 8;
<a name="l00107"></a>00107     ctx-&gt;W[ctx-&gt;lenW / 4] |= (<span class="keywordtype">unsigned</span> int)dataIn[i];
<a name="l00108"></a>00108     <span class="keywordflow">if</span> ((++ctx-&gt;lenW) % 64 == 0) {
<a name="l00109"></a>00109       shaHashBlock(ctx);
<a name="l00110"></a>00110       ctx-&gt;lenW = 0;
<a name="l00111"></a>00111     }
<a name="l00112"></a>00112     ctx-&gt;sizeLo += 8;
<a name="l00113"></a>00113     ctx-&gt;sizeHi += (ctx-&gt;sizeLo &lt; 8);
<a name="l00114"></a>00114   }
<a name="l00115"></a>00115 }
<a name="l00116"></a>00116 
<a name="l00117"></a>00117 
<a name="l00118"></a>00118 <span class="keywordtype">void</span> SHA1_Final(<span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> hashout[20], SHA_CTX *ctx) {
<a name="l00119"></a>00119   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> pad0x80 = 0x80;
<a name="l00120"></a>00120   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> pad0x00 = 0x00;
<a name="l00121"></a>00121   <span class="keywordtype">unsigned</span> <span class="keywordtype">char</span> padlen[8];
<a name="l00122"></a>00122   <span class="keywordtype">int</span> i;
<a name="l00123"></a>00123 
<a name="l00124"></a>00124   <span class="comment">/* Pad with a binary 1 (e.g. 0x80), then zeroes, then length</span>
<a name="l00125"></a>00125 <span class="comment">   */</span>
<a name="l00126"></a>00126   padlen[0] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeHi &gt;&gt; 24) &amp; 255);
<a name="l00127"></a>00127   padlen[1] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeHi &gt;&gt; 16) &amp; 255);
<a name="l00128"></a>00128   padlen[2] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeHi &gt;&gt; 8) &amp; 255);
<a name="l00129"></a>00129   padlen[3] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeHi &gt;&gt; 0) &amp; 255);
<a name="l00130"></a>00130   padlen[4] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeLo &gt;&gt; 24) &amp; 255);
<a name="l00131"></a>00131   padlen[5] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeLo &gt;&gt; 16) &amp; 255);
<a name="l00132"></a>00132   padlen[6] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeLo &gt;&gt; 8) &amp; 255);
<a name="l00133"></a>00133   padlen[7] = (<span class="keywordtype">unsigned</span> char)((ctx-&gt;sizeLo &gt;&gt; 0) &amp; 255);
<a name="l00134"></a>00134   SHA1_Update(ctx, &amp;pad0x80, 1);
<a name="l00135"></a>00135   <span class="keywordflow">while</span> (ctx-&gt;lenW != 56)
<a name="l00136"></a>00136     SHA1_Update(ctx, &amp;pad0x00, 1);
<a name="l00137"></a>00137   SHA1_Update(ctx, padlen, 8);
<a name="l00138"></a>00138 
<a name="l00139"></a>00139   <span class="comment">/* Output hash</span>
<a name="l00140"></a>00140 <span class="comment">   */</span>
<a name="l00141"></a>00141   <span class="keywordflow">for</span> (i = 0; i &lt; 20; i++) {
<a name="l00142"></a>00142     hashout[i] = (<span class="keywordtype">unsigned</span> char)(ctx-&gt;H[i / 4] &gt;&gt; 24);
<a name="l00143"></a>00143     ctx-&gt;H[i / 4] &lt;&lt;= 8;
<a name="l00144"></a>00144   }
<a name="l00145"></a>00145 
<a name="l00146"></a>00146   <span class="comment">/*</span>
<a name="l00147"></a>00147 <span class="comment">   *  Re-initialize the context (also zeroizes contents)</span>
<a name="l00148"></a>00148 <span class="comment">   */</span>
<a name="l00149"></a>00149   SHA1_Init(ctx);
<a name="l00150"></a>00150 }
<a name="l00151"></a>00151 
<a name="l00152"></a>00152 
<a name="l00153"></a>00153 <span class="preprocessor">#define SHA_ROT(X,n) (((X) &lt;&lt; (n)) | ((X) &gt;&gt; (32-(n))))</span>
<a name="l00154"></a>00154 <span class="preprocessor"></span>
<a name="l00155"></a>00155 <span class="keyword">static</span> <span class="keywordtype">void</span> shaHashBlock(SHA_CTX *ctx) {
<a name="l00156"></a>00156   <span class="keywordtype">int</span> t;
<a name="l00157"></a>00157   <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> A,B,C,D,E,TEMP;
<a name="l00158"></a>00158 
<a name="l00159"></a>00159   <span class="keywordflow">for</span> (t = 16; t &lt;= 79; t++)
<a name="l00160"></a>00160     ctx-&gt;W[t] =
<a name="l00161"></a>00161       SHA_ROT(ctx-&gt;W[t-3] ^ ctx-&gt;W[t-8] ^ ctx-&gt;W[t-14] ^ ctx-&gt;W[t-16], 1);
<a name="l00162"></a>00162 
<a name="l00163"></a>00163   A = ctx-&gt;H[0];
<a name="l00164"></a>00164   B = ctx-&gt;H[1];
<a name="l00165"></a>00165   C = ctx-&gt;H[2];
<a name="l00166"></a>00166   D = ctx-&gt;H[3];
<a name="l00167"></a>00167   E = ctx-&gt;H[4];
<a name="l00168"></a>00168 
<a name="l00169"></a>00169   <span class="keywordflow">for</span> (t = 0; t &lt;= 19; t++) {
<a name="l00170"></a>00170     TEMP = SHA_ROT(A,5) + (((C^D)&amp;B)^D)     + E + ctx-&gt;W[t] + 0x5a827999;
<a name="l00171"></a>00171     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
<a name="l00172"></a>00172   }
<a name="l00173"></a>00173   <span class="keywordflow">for</span> (t = 20; t &lt;= 39; t++) {
<a name="l00174"></a>00174     TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx-&gt;W[t] + 0x6ed9eba1;
<a name="l00175"></a>00175     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
<a name="l00176"></a>00176   }
<a name="l00177"></a>00177   <span class="keywordflow">for</span> (t = 40; t &lt;= 59; t++) {
<a name="l00178"></a>00178     TEMP = SHA_ROT(A,5) + ((B&amp;C)|(D&amp;(B|C))) + E + ctx-&gt;W[t] + 0x8f1bbcdc;
<a name="l00179"></a>00179     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
<a name="l00180"></a>00180   }
<a name="l00181"></a>00181   <span class="keywordflow">for</span> (t = 60; t &lt;= 79; t++) {
<a name="l00182"></a>00182     TEMP = SHA_ROT(A,5) + (B^C^D)           + E + ctx-&gt;W[t] + 0xca62c1d6;
<a name="l00183"></a>00183     E = D; D = C; C = SHA_ROT(B, 30); B = A; A = TEMP;
<a name="l00184"></a>00184   }
<a name="l00185"></a>00185 
<a name="l00186"></a>00186   ctx-&gt;H[0] += A;
<a name="l00187"></a>00187   ctx-&gt;H[1] += B;
<a name="l00188"></a>00188   ctx-&gt;H[2] += C;
<a name="l00189"></a>00189   ctx-&gt;H[3] += D;
<a name="l00190"></a>00190   ctx-&gt;H[4] += E;
<a name="l00191"></a>00191 }
<a name="l00192"></a>00192 
<a name="l00193"></a>00193 }
<a name="l00194"></a>00194 
</pre></div></div>
<hr class="footer"/><address style="text-align: right;"><small>Generated by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2-20100208 </small></address>
</body>
</html>