⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 idea_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
字号:
<!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>Crypto++: idea.cpp Source File</title><link href="doxygen.css" rel="stylesheet" type="text/css"></head><body><!-- Generated by Doxygen 1.3.2 --><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">Compound&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">Compound&nbsp;Members</a> | <a class="qindex" href="globals.html">File&nbsp;Members</a></div><h1>idea.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// idea.cpp - written and placed in the public domain by Wei Dai</span>00002 00003 <span class="preprocessor">#include "pch.h"</span>00004 <span class="preprocessor">#include "<a class="code" href="idea_8h.html">idea.h</a>"</span>00005 <span class="preprocessor">#include "misc.h"</span>00006 00007 NAMESPACE_BEGIN(CryptoPP)00008 00009 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> IDEA_KEYLEN=(6*IDEA::ROUNDS+4);  <span class="comment">// key schedule length in # of word16s</span>00010 00011 <span class="preprocessor">#define low16(x) ((x)&amp;0xffff)   // compiler should be able to optimize this away if word is 16 bits</span>00012 <span class="preprocessor"></span><span class="preprocessor">#define high16(x) ((x)&gt;&gt;16)</span>00013 <span class="preprocessor"></span>00014 <span class="comment">// should use an inline function but macros are still faster in MSVC 4.0</span>00015 <span class="preprocessor">#define DirectMUL(a,b)                                  \</span>00016 <span class="preprocessor">{                                                                               \</span>00017 <span class="preprocessor">        assert(b &lt;= 0xffff);                            \</span>00018 <span class="preprocessor">                                                                                \</span>00019 <span class="preprocessor">        word32 p=(word32)low16(a)*b;            \</span>00020 <span class="preprocessor">                                                                                \</span>00021 <span class="preprocessor">        if (p)                                                          \</span>00022 <span class="preprocessor">        {                                                                       \</span>00023 <span class="preprocessor">                p = low16(p) - high16(p);               \</span>00024 <span class="preprocessor">                a = (word)p - (word)high16(p);  \</span>00025 <span class="preprocessor">        }                                                                       \</span>00026 <span class="preprocessor">        else                                                            \</span>00027 <span class="preprocessor">                a = 1-a-b;                                              \</span>00028 <span class="preprocessor">}</span>00029 <span class="preprocessor"></span>00030 <span class="preprocessor">#ifdef IDEA_LARGECACHE</span>00031 <span class="preprocessor"></span><span class="keywordtype">bool</span> IDEA::Base::tablesBuilt = <span class="keyword">false</span>;00032 word16 IDEA::Base::log[0x10000];00033 word16 IDEA::Base::antilog[0x10000];00034 00035 <span class="keywordtype">void</span> IDEA::Base::BuildLogTables()00036 {00037         <span class="keywordflow">if</span> (tablesBuilt)00038                 <span class="keywordflow">return</span>;00039         <span class="keywordflow">else</span>00040         {00041                 tablesBuilt = <span class="keyword">true</span>;00042                 00043                 word x=1;00044                 word32 i;00045                 00046                 <span class="keywordflow">for</span> (i=0; i&lt;0x10000; i++)00047                 {00048                         antilog[i] = (word16)x;00049                         DirectMUL(x, 3);00050                 }00051                 00052                 <span class="keywordflow">for</span> (i=0; i&lt;0x10000; i++)00053                         log[antilog[i]] = (word16)i;00054         }00055 }00056 00057 <span class="keywordtype">void</span> IDEA::Base::LookupKeyLogs()00058 {00059         word* Z=key;00060         <span class="keywordtype">int</span> r=ROUNDS;00061         <span class="keywordflow">do</span>00062         {00063                 Z[0] = log[Z[0]];00064                 Z[3] = log[Z[3]];00065                 Z[4] = log[Z[4]];00066                 Z[5] = log[Z[5]];00067                 Z+=6;00068         } <span class="keywordflow">while</span> (--r);00069         Z[0] = log[Z[0]];00070         Z[3] = log[Z[3]];00071 }00072 00073 <span class="keyword">inline</span> <span class="keywordtype">void</span> IDEA::Base::LookupMUL(word &amp;a, word b)00074 {00075         a = antilog[low16(log[low16(a)]+b)];00076 }00077 <span class="preprocessor">#endif // IDEA_LARGECACHE</span>00078 <span class="preprocessor"></span>00079 <span class="keywordtype">void</span> IDEA::Base::UncheckedSetKey(CipherDir direction, <span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00080 {00081         AssertValidKeyLength(length);00082         00083 <span class="preprocessor">#ifdef IDEA_LARGECACHE</span>00084 <span class="preprocessor"></span>        BuildLogTables();00085 <span class="preprocessor">#endif</span>00086 <span class="preprocessor"></span>        00087         EnKey(userKey);00088         00089         <span class="keywordflow">if</span> (direction==DECRYPTION)00090                 DeKey();00091         00092 <span class="preprocessor">#ifdef IDEA_LARGECACHE</span>00093 <span class="preprocessor"></span>        LookupKeyLogs();00094 <span class="preprocessor">#endif</span>00095 <span class="preprocessor"></span>}00096 00097 <span class="keywordtype">void</span> IDEA::Base::EnKey (<span class="keyword">const</span> byte *userKey)00098 {00099         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00100         00101         <span class="keywordflow">for</span> (i=0; i&lt;8; i++)00102                 m_key[i] = ((word)userKey[2*i]&lt;&lt;8) | userKey[2*i+1];00103         00104         <span class="keywordflow">for</span> (; i&lt;IDEA_KEYLEN; i++)00105         {00106                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j = RoundDownToMultipleOf(i,8U)-8;00107                 m_key[i] = low16((m_key[j+(i+1)%8] &lt;&lt; 9) | (m_key[j+(i+2)%8] &gt;&gt; 7));00108         }00109 }00110 00111 <span class="keyword">static</span> word MulInv(word x)00112 {00113         word y=x;00114         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i&lt;15; i++)00115         {00116                 DirectMUL(y,low16(y));00117                 DirectMUL(y,x);00118         }00119         <span class="keywordflow">return</span> low16(y);00120 }00121 00122 <span class="keyword">static</span> <span class="keyword">inline</span> word AddInv(word x)00123 {00124         <span class="keywordflow">return</span> low16(0-x);00125 }00126 00127 <span class="keywordtype">void</span> IDEA::Base::DeKey()00128 {00129         FixedSizeSecBlock&lt;word, 6*ROUNDS+4&gt; tempkey;00130         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00131 00132         <span class="keywordflow">for</span> (i=0; i&lt;ROUNDS; i++)00133         {00134                 tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);00135                 tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1+(i&gt;0)]);00136                 tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2-(i&gt;0)]);00137                 tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);00138                 tempkey[i*6+4] =        m_key[(ROUNDS-1-i)*6+4];00139                 tempkey[i*6+5] =        m_key[(ROUNDS-1-i)*6+5];00140         }00141 00142         tempkey[i*6+0] = MulInv(m_key[(ROUNDS-i)*6+0]);00143         tempkey[i*6+1] = AddInv(m_key[(ROUNDS-i)*6+1]);00144         tempkey[i*6+2] = AddInv(m_key[(ROUNDS-i)*6+2]);00145         tempkey[i*6+3] = MulInv(m_key[(ROUNDS-i)*6+3]);00146 00147         m_key = tempkey;00148 }00149 00150 <span class="preprocessor">#ifdef IDEA_LARGECACHE</span>00151 <span class="preprocessor"></span><span class="preprocessor">#define MUL(a,b) LookupMUL(a,b)</span>00152 <span class="preprocessor"></span><span class="preprocessor">#else</span>00153 <span class="preprocessor"></span><span class="preprocessor">#define MUL(a,b) DirectMUL(a,b)</span>00154 <span class="preprocessor"></span><span class="preprocessor">#endif</span>00155 <span class="preprocessor"></span>00156 <span class="keywordtype">void</span> IDEA::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00157 <span class="keyword"></span>{00158         <span class="keyword">typedef</span> BlockGetAndPut&lt;word16, BigEndian&gt; Block;00159 00160         <span class="keyword">const</span> word *key = m_key;00161         word x0,x1,x2,x3,t0,t1;00162         Block::Get(inBlock)(x0)(x1)(x2)(x3);00163 00164         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;ROUNDS; i++)00165         {00166                 MUL(x0, key[i*6+0]);00167                 x1 += key[i*6+1];00168                 x2 += key[i*6+2];00169                 MUL(x3, key[i*6+3]);00170                 t0 = x0^x2; 00171                 MUL(t0, key[i*6+4]);00172                 t1 = t0 + (x1^x3);00173                 MUL(t1, key[i*6+5]);00174                 t0 += t1;00175                 x0 ^= t1;00176                 x3 ^= t0;00177                 t0 ^= x1;00178                 x1 = x2^t1;00179                 x2 = t0;00180         }00181 00182         MUL(x0, key[ROUNDS*6+0]);00183         x2 += key[ROUNDS*6+1];00184         x1 += key[ROUNDS*6+2];00185         MUL(x3, key[ROUNDS*6+3]);00186 00187         Block::Put(xorBlock, outBlock)(x0)(x2)(x1)(x3);00188 }00189 00190 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:16 2003 for Crypto++ by<a href="http://www.doxygen.org/index.html"><img src="doxygen.png" alt="doxygen" align="middle" border=0 > </a>1.3.2 </small></address></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -