Sophie

Sophie

distrib > Mandriva > 10.2 > i586 > media > contrib > by-pkgid > 7457b841ac8136d3a1a9d3d960c5252e > files > 1424

libcryptopp-doc-5.2.1-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Crypto++: strciphr.cpp Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.3.7 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical&nbsp;List</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="namespacemembers.html">Namespace&nbsp;Members</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div>
<h1>strciphr.cpp</h1><pre class="fragment"><div>00001 <span class="comment">// strciphr.cpp - written and placed in the public domain by Wei Dai</span>
00002 
00003 <span class="preprocessor">#include "pch.h"</span>
00004 
00005 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>
00006 <span class="preprocessor"></span>
00007 <span class="preprocessor">#include "<a class="code" href="strciphr_8h.html">strciphr.h</a>"</span>
00008 
00009 NAMESPACE_BEGIN(CryptoPP)
00010 
00011 template &lt;class S&gt;
00012 byte AdditiveCipherTemplate&lt;S&gt;::GenerateByte()
00013 {
00014         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00015 
00016         <span class="keywordflow">if</span> (m_leftOver == 0)
00017         {
00018                 policy.WriteKeystream(m_buffer, policy.GetIterationsToBuffer());
00019                 m_leftOver = policy.GetBytesPerIteration();
00020         }
00021 
00022         <span class="keywordflow">return</span> *(KeystreamBufferEnd()-m_leftOver--);
00023 }
00024 
00025 <span class="keyword">template</span> &lt;<span class="keyword">class</span> S&gt;
00026 <span class="keyword">inline</span> <span class="keywordtype">void</span> AdditiveCipherTemplate&lt;S&gt;::ProcessData(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)
00027 {
00028         <span class="keywordflow">if</span> (m_leftOver &gt; 0)
00029         {
00030                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = STDMIN(m_leftOver, length);
00031                 xorbuf(outString, inString, KeystreamBufferEnd()-m_leftOver, len);
00032                 length -= len;
00033                 m_leftOver -= len;
00034                 inString += len;
00035                 outString += len;
00036         }
00037 
00038         <span class="keywordflow">if</span> (!length)
00039                 <span class="keywordflow">return</span>;
00040 
00041         assert(m_leftOver == 0);
00042 
00043         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00044         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bytesPerIteration = policy.GetBytesPerIteration();
00045         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alignment = policy.GetAlignment();
00046 
00047         <span class="keywordflow">if</span> (policy.CanOperateKeystream() &amp;&amp; length &gt;= bytesPerIteration &amp;&amp; IsAlignedOn(outString, alignment))
00048         {
00049                 <span class="keywordflow">if</span> (IsAlignedOn(inString, alignment))
00050                         policy.OperateKeystream(XOR_KEYSTREAM, outString, inString, length / bytesPerIteration);
00051                 <span class="keywordflow">else</span>
00052                 {
00053                         memcpy(outString, inString, length);
00054                         policy.OperateKeystream(XOR_KEYSTREAM_INPLACE, outString, outString, length / bytesPerIteration);
00055                 }
00056                 inString += length - length % bytesPerIteration;
00057                 outString += length - length % bytesPerIteration;
00058                 length %= bytesPerIteration;
00059 
00060                 <span class="keywordflow">if</span> (!length)
00061                         <span class="keywordflow">return</span>;
00062         }
00063 
00064         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferByteSize = GetBufferByteSize(policy);
00065         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bufferIterations = policy.GetIterationsToBuffer();
00066 
00067         <span class="keywordflow">while</span> (length &gt;= bufferByteSize)
00068         {
00069                 policy.WriteKeystream(m_buffer, bufferIterations);
00070                 xorbuf(outString, inString, KeystreamBufferBegin(), bufferByteSize);
00071                 length -= bufferByteSize;
00072                 inString += bufferByteSize;
00073                 outString += bufferByteSize;
00074         }
00075 
00076         <span class="keywordflow">if</span> (length &gt; 0)
00077         {
00078                 policy.WriteKeystream(m_buffer, bufferIterations);
00079                 xorbuf(outString, inString, KeystreamBufferBegin(), length);
00080                 m_leftOver = bytesPerIteration - length;
00081         }
00082 }
00083 
00084 <span class="keyword">template</span> &lt;<span class="keyword">class</span> S&gt;
00085 <span class="keywordtype">void</span> AdditiveCipherTemplate&lt;S&gt;::Resynchronize(<span class="keyword">const</span> byte *iv)
00086 {
00087         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00088         m_leftOver = 0;
00089         m_buffer.New(GetBufferByteSize(policy));
00090         policy.CipherResynchronize(m_buffer, iv);
00091 }
00092 
00093 <span class="keyword">template</span> &lt;<span class="keyword">class</span> BASE&gt;
00094 <span class="keywordtype">void</span> AdditiveCipherTemplate&lt;BASE&gt;::Seek(lword position)
00095 {
00096         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00097         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bytesPerIteration = policy.GetBytesPerIteration();
00098 
00099         policy.SeekToIteration(position / bytesPerIteration);
00100         position %= bytesPerIteration;
00101 
00102         <span class="keywordflow">if</span> (position &gt; 0)
00103         {
00104                 policy.WriteKeystream(m_buffer, 1);
00105                 m_leftOver = bytesPerIteration - (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)position;
00106         }
00107         <span class="keywordflow">else</span>
00108                 m_leftOver = 0;
00109 }
00110 
00111 <span class="keyword">template</span> &lt;<span class="keyword">class</span> BASE&gt;
00112 <span class="keywordtype">void</span> CFB_CipherTemplate&lt;BASE&gt;::Resynchronize(<span class="keyword">const</span> byte *iv)
00113 {
00114         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00115         policy.CipherResynchronize(iv);
00116         m_leftOver = policy.GetBytesPerIteration();
00117 }
00118 
00119 <span class="keyword">template</span> &lt;<span class="keyword">class</span> BASE&gt;
00120 <span class="keywordtype">void</span> CFB_CipherTemplate&lt;BASE&gt;::ProcessData(byte *outString, <span class="keyword">const</span> byte *inString, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)
00121 {
00122         assert(length % this-&gt;MandatoryBlockSize() == 0);
00123 
00124         PolicyInterface &amp;policy = this-&gt;AccessPolicy();
00125         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> bytesPerIteration = policy.GetBytesPerIteration();
00126         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> alignment = policy.GetAlignment();
00127         byte *reg = policy.GetRegisterBegin();
00128 
00129         <span class="keywordflow">if</span> (m_leftOver)
00130         {
00131                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = STDMIN(m_leftOver, length);
00132                 CombineMessageAndShiftRegister(outString, reg + bytesPerIteration - m_leftOver, inString, len);
00133                 m_leftOver -= len;
00134                 length -= len;
00135                 inString += len;
00136                 outString += len;
00137         }
00138 
00139         <span class="keywordflow">if</span> (!length)
00140                 <span class="keywordflow">return</span>;
00141 
00142         assert(m_leftOver == 0);
00143 
00144         <span class="keywordflow">if</span> (policy.CanIterate() &amp;&amp; length &gt;= bytesPerIteration &amp;&amp; IsAlignedOn(outString, alignment))
00145         {
00146                 <span class="keywordflow">if</span> (IsAlignedOn(inString, alignment))
00147                         policy.Iterate(outString, inString, GetCipherDir(*<span class="keyword">this</span>), length / bytesPerIteration);
00148                 <span class="keywordflow">else</span>
00149                 {
00150                         memcpy(outString, inString, length);
00151                         policy.Iterate(outString, outString, GetCipherDir(*<span class="keyword">this</span>), length / bytesPerIteration);
00152                 }
00153                 inString += length - length % bytesPerIteration;
00154                 outString += length - length % bytesPerIteration;
00155                 length %= bytesPerIteration;
00156         }
00157 
00158         <span class="keywordflow">while</span> (length &gt;= bytesPerIteration)
00159         {
00160                 policy.TransformRegister();
00161                 CombineMessageAndShiftRegister(outString, reg, inString, bytesPerIteration);
00162                 length -= bytesPerIteration;
00163                 inString += bytesPerIteration;
00164                 outString += bytesPerIteration;
00165         }
00166 
00167         <span class="keywordflow">if</span> (length &gt; 0)
00168         {
00169                 policy.TransformRegister();
00170                 CombineMessageAndShiftRegister(outString, reg, inString, length);
00171                 m_leftOver = bytesPerIteration - length;
00172         }
00173 }
00174 
00175 <span class="keyword">template</span> &lt;<span class="keyword">class</span> BASE&gt;
00176 <span class="keywordtype">void</span> CFB_EncryptionTemplate&lt;BASE&gt;::CombineMessageAndShiftRegister(byte *output, byte *reg, <span class="keyword">const</span> byte *message, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)
00177 {
00178         xorbuf(reg, message, length);
00179         memcpy(output, reg, length);
00180 }
00181 
00182 <span class="keyword">template</span> &lt;<span class="keyword">class</span> BASE&gt;
00183 <span class="keywordtype">void</span> CFB_DecryptionTemplate&lt;BASE&gt;::CombineMessageAndShiftRegister(byte *output, byte *reg, <span class="keyword">const</span> byte *message, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)
00184 {
00185         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;length; i++)
00186         {
00187                 byte b = message[i];
00188                 output[i] = reg[i] ^ b;
00189                 reg[i] = b;
00190         }
00191 }
00192 
00193 NAMESPACE_END
00194 
00195 <span class="preprocessor">#endif</span>
</div></pre><hr size="1"><address style="align: right;"><small>Generated on Sun Nov 7 08:23:59 2004 for Crypto++ by
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border=0 ></a> 1.3.7 </small></address>
</body>
</html>