Sophie

Sophie

distrib > Mandriva > 9.1 > ppc > by-pkgid > a74ec78bdb789d910d054e3918f3f007 > files > 464

libsword1-devel-1.5.5-2mdk.ppc.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>sapphire.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.2.15 -->
<center>
<a class="qindex" href="index.html">Main Page</a> &nbsp; <a class="qindex" href="namespaces.html">Namespace List</a> &nbsp; <a class="qindex" href="hierarchy.html">Class Hierarchy</a> &nbsp; <a class="qindex" href="classes.html">Alphabetical List</a> &nbsp; <a class="qindex" href="annotated.html">Compound List</a> &nbsp; <a class="qindex" href="files.html">File List</a> &nbsp; <a class="qindex" href="functions.html">Compound Members</a> &nbsp; </center>
<hr><h1>sapphire.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/* sapphire.cpp -- the Saphire II stream cipher class.</font>
00002 <font class="comment">   Dedicated to the Public Domain the author and inventor:</font>
00003 <font class="comment">   (Michael Paul Johnson).  This code comes with no warranty.</font>
00004 <font class="comment">   Use it at your own risk.</font>
00005 <font class="comment">   Ported from the Pascal implementation of the Sapphire Stream</font>
00006 <font class="comment">   Cipher 9 December 1994.</font>
00007 <font class="comment">   Added hash pre- and post-processing 27 December 1994.</font>
00008 <font class="comment">   Modified initialization to make index variables key dependent,</font>
00009 <font class="comment">   made the output function more resistant to cryptanalysis,</font>
00010 <font class="comment">   and renamed to Sapphire II 2 January 1995</font>
00011 <font class="comment">*/</font>
00012 
00013 
00014 <font class="preprocessor">#ifdef WIN32</font>
00015 <font class="preprocessor"></font><font class="preprocessor">#include &lt;memory.h&gt;</font>
00016 <font class="preprocessor">#endif</font>
00017 <font class="preprocessor"></font>
00018 <font class="preprocessor">#ifdef UNIX</font>
00019 <font class="preprocessor"></font><font class="preprocessor">#include &lt;memory.h&gt;</font>
00020 <font class="preprocessor">#include &lt;unistd.h&gt;</font>
00021 <font class="preprocessor">#else</font>
00022 <font class="preprocessor"></font><font class="preprocessor">#ifndef _MSC_VER</font>
00023 <font class="preprocessor"></font><font class="preprocessor">#include &lt;mem.h&gt;</font>
00024 <font class="preprocessor">#endif</font>
00025 <font class="preprocessor"></font><font class="preprocessor">#endif</font>
00026 <font class="preprocessor"></font>
00027 <font class="preprocessor">#ifdef _WIN32_WCE</font>
00028 <font class="preprocessor"></font><font class="preprocessor">#include &lt;string.h&gt;</font>
00029 <font class="preprocessor">#endif</font>
00030 <font class="preprocessor"></font>
00031 <font class="preprocessor">#include "sapphire.h"</font>
00032 
00033 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> sapphire::keyrand(<font class="keywordtype">int</font> limit,
00034                                 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *user_key,
00035                                 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> keysize,
00036                                 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *rsum,
00037                                 <font class="keywordtype">unsigned</font> *keypos)
00038     {
00039     <font class="keywordtype">unsigned</font> u,             <font class="comment">// Value from 0 to limit to return.</font>
00040         retry_limiter,      <font class="comment">// No infinite loops allowed.</font>
00041         mask;               <font class="comment">// Select just enough bits.</font>
00042 
00043     <font class="keywordflow">if</font> (!limit) <font class="keywordflow">return</font> 0;   <font class="comment">// Avoid divide by zero error.</font>
00044     retry_limiter = 0;
00045     mask = 1;               <font class="comment">// Fill mask with enough bits to cover</font>
00046     <font class="keywordflow">while</font> (mask &lt; (unsigned)limit)    <font class="comment">// the desired range.</font>
00047         mask = (mask &lt;&lt; 1) + 1;
00048     <font class="keywordflow">do</font>
00049         {
00050         *rsum = cards[*rsum] + user_key[(*keypos)++];
00051         <font class="keywordflow">if</font> (*keypos &gt;= keysize)
00052             {
00053             *keypos = 0;            <font class="comment">// Recycle the user key.</font>
00054             *rsum += keysize;   <font class="comment">// key "aaaa" != key "aaaaaaaa"</font>
00055             }
00056         u = mask &amp; *rsum;
00057         <font class="keywordflow">if</font> (++retry_limiter &gt; 11)
00058             u %= limit;     <font class="comment">// Prevent very rare long loops.</font>
00059         }
00060     <font class="keywordflow">while</font> (u &gt; (unsigned)limit);
00061     <font class="keywordflow">return</font> u;
00062     }
00063 
00064 <font class="keywordtype">void</font> sapphire::initialize(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *key, <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> keysize)
00065     {
00066     <font class="comment">// Key size may be up to 256 bytes.</font>
00067     <font class="comment">// Pass phrases may be used directly, with longer length</font>
00068     <font class="comment">// compensating for the low entropy expected in such keys.</font>
00069     <font class="comment">// Alternatively, shorter keys hashed from a pass phrase or</font>
00070     <font class="comment">// generated randomly may be used. For random keys, lengths</font>
00071     <font class="comment">// of from 4 to 16 bytes are recommended, depending on how</font>
00072     <font class="comment">// secure you want this to be.</font>
00073 
00074     <font class="keywordtype">int</font> i;
00075     <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> toswap, swaptemp, rsum;
00076     <font class="keywordtype">unsigned</font> keypos;
00077 
00078     <font class="comment">// If we have been given no key, assume the default hash setup.</font>
00079 
00080     <font class="keywordflow">if</font> (keysize &lt; 1)
00081         {
00082         hash_init();
00083         <font class="keywordflow">return</font>;
00084         }
00085 
00086     <font class="comment">// Start with cards all in order, one of each.</font>
00087 
00088     <font class="keywordflow">for</font> (i=0;i&lt;256;i++)
00089         cards[i] = i;
00090 
00091     <font class="comment">// Swap the card at each position with some other card.</font>
00092 
00093     toswap = 0;
00094     keypos = 0;         <font class="comment">// Start with first byte of user key.</font>
00095     rsum = 0;
00096     <font class="keywordflow">for</font> (i=255;i&gt;=0;i--)
00097         {
00098         toswap = keyrand(i, key, keysize, &amp;rsum, &amp;keypos);
00099         swaptemp = cards[i];
00100         cards[i] = cards[toswap];
00101         cards[toswap] = swaptemp;
00102         }
00103 
00104     <font class="comment">// Initialize the indices and data dependencies.</font>
00105     <font class="comment">// Indices are set to different values instead of all 0</font>
00106     <font class="comment">// to reduce what is known about the state of the cards</font>
00107     <font class="comment">// when the first byte is emitted.</font>
00108 
00109     rotor = cards[1];
00110     ratchet = cards[3];
00111     avalanche = cards[5];
00112     last_plain = cards[7];
00113     last_cipher = cards[rsum];
00114 
00115     toswap = swaptemp = rsum = 0;
00116     keypos = 0;
00117     }
00118 
00119 <font class="keywordtype">void</font> sapphire::hash_init(<font class="keywordtype">void</font>)
00120     {
00121     <font class="comment">// This function is used to initialize non-keyed hash</font>
00122     <font class="comment">// computation.</font>
00123 
00124     <font class="keywordtype">int</font> i, j;
00125 
00126     <font class="comment">// Initialize the indices and data dependencies.</font>
00127 
00128     rotor = 1;
00129     ratchet = 3;
00130     avalanche = 5;
00131     last_plain = 7;
00132     last_cipher = 11;
00133 
00134     <font class="comment">// Start with cards all in inverse order.</font>
00135 
00136     <font class="keywordflow">for</font> (i=0, j=255;i&lt;256;i++,j--)
00137         cards[i] = (<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>) j;
00138     }
00139 
00140 sapphire::sapphire(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *key, <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> keysize)
00141     {
00142     <font class="keywordflow">if</font> (key &amp;&amp; keysize)
00143         initialize(key, keysize);
00144     }
00145 
00146 <font class="keywordtype">void</font> sapphire::burn(<font class="keywordtype">void</font>)
00147     {
00148     <font class="comment">// Destroy the key and state information in RAM.</font>
00149     memset(cards, 0, 256);
00150     rotor = ratchet = avalanche = last_plain = last_cipher = 0;
00151     }
00152 
00153 sapphire::~sapphire()
00154     {
00155     burn();
00156     }
00157 
00158 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> sapphire::encrypt(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> b)
00159     {
00160 <font class="preprocessor">#ifdef USBINARY</font>
00161 <font class="preprocessor"></font>    <font class="comment">// Picture a single enigma rotor with 256 positions, rewired</font>
00162     <font class="comment">// on the fly by card-shuffling.</font>
00163 
00164     <font class="comment">// This cipher is a variant of one invented and written</font>
00165     <font class="comment">// by Michael Paul Johnson in November, 1993.</font>
00166 
00167     <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> swaptemp;
00168 
00169     <font class="comment">// Shuffle the deck a little more.</font>
00170 
00171     ratchet += cards[rotor++];
00172     swaptemp = cards[last_cipher];
00173     cards[last_cipher] = cards[ratchet];
00174     cards[ratchet] = cards[last_plain];
00175     cards[last_plain] = cards[rotor];
00176     cards[rotor] = swaptemp;
00177     avalanche += cards[swaptemp];
00178 
00179     <font class="comment">// Output one byte from the state in such a way as to make it</font>
00180     <font class="comment">// very hard to figure out which one you are looking at.</font>
00181 
00182     last_cipher = b^cards[(cards[ratchet] + cards[rotor]) &amp; 0xFF] ^
00183                   cards[cards[(cards[last_plain] +
00184                                cards[last_cipher] +
00185                                cards[avalanche])&amp;0xFF]];
00186     last_plain = b;
00187     <font class="keywordflow">return</font> last_cipher;
00188 <font class="preprocessor">#else</font>
00189 <font class="preprocessor"></font>    <font class="keywordflow">return</font> b;
00190 <font class="preprocessor">#endif</font>
00191 <font class="preprocessor"></font>    }
00192 
00193 <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> sapphire::decrypt(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> b)
00194     {
00195     <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> swaptemp;
00196 
00197     <font class="comment">// Shuffle the deck a little more.</font>
00198 
00199     ratchet += cards[rotor++];
00200     swaptemp = cards[last_cipher];
00201     cards[last_cipher] = cards[ratchet];
00202     cards[ratchet] = cards[last_plain];
00203     cards[last_plain] = cards[rotor];
00204     cards[rotor] = swaptemp;
00205     avalanche += cards[swaptemp];
00206 
00207     <font class="comment">// Output one byte from the state in such a way as to make it</font>
00208     <font class="comment">// very hard to figure out which one you are looking at.</font>
00209 
00210     last_plain = b^cards[(cards[ratchet] + cards[rotor]) &amp; 0xFF] ^
00211                    cards[cards[(cards[last_plain] +
00212                                 cards[last_cipher] +
00213                                 cards[avalanche])&amp;0xFF]];
00214     last_cipher = b;
00215     <font class="keywordflow">return</font> last_plain;
00216     }
00217 
00218 <font class="keywordtype">void</font> sapphire::hash_final(<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> *hash,      <font class="comment">// Destination</font>
00219                           <font class="keywordtype">unsigned</font> <font class="keywordtype">char</font> hashlength) <font class="comment">// Size of hash.</font>
00220     {
00221     <font class="keywordtype">int</font> i;
00222 
00223     <font class="keywordflow">for</font> (i=255;i&gt;=0;i--)
00224         encrypt((<font class="keywordtype">unsigned</font> <font class="keywordtype">char</font>) i);
00225     <font class="keywordflow">for</font> (i=0;i&lt;hashlength;i++)
00226         hash[i] = encrypt(0);
00227     }
00228 
</pre></div><hr><address align="right"><small>Generated on Thu Jun 20 22:13:00 2002 for The Sword Project by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 
width=110 height=53></a>1.2.15 </small></address>
</body>
</html>