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

📄 twofish_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++: twofish.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>twofish.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// twofish.cpp - modified by Wei Dai from Matthew Skala's twofish.c</span>00002 <span class="comment">// The original code and all modifications are in the public domain.</span>00003 00004 <span class="preprocessor">#include "pch.h"</span>00005 <span class="preprocessor">#include "<a class="code" href="twofish_8h.html">twofish.h</a>"</span>00006 <span class="preprocessor">#include "misc.h"</span>00007 00008 NAMESPACE_BEGIN(CryptoPP)00009 00010 <span class="comment">// compute (c * x^4) mod (x^4 + (a + 1/a) * x^3 + a * x^2 + (a + 1/a) * x + 1)</span>00011 <span class="comment">// over GF(256)</span>00012 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> Mod(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> c)00013 {00014         <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> modulus = 0x14d;00015         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> c2 = (c&lt;&lt;1) ^ ((c &amp; 0x80) ? modulus : 0);00016         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> c1 = c2 ^ (c&gt;&gt;1) ^ ((c &amp; 1) ? (modulus&gt;&gt;1) : 0);00017         <span class="keywordflow">return</span> c | (c1 &lt;&lt; 8) | (c2 &lt;&lt; 16) | (c1 &lt;&lt; 24);00018 }00019 00020 <span class="comment">// compute RS(12,8) code with the above polynomial as generator</span>00021 <span class="comment">// this is equivalent to multiplying by the RS matrix</span>00022 <span class="keyword">static</span> word32 ReedSolomon(word32 high, word32 low)00023 {00024         <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;8; i++)00025         {00026                 high = Mod(high&gt;&gt;24) ^ (high&lt;&lt;8) ^ (low&gt;&gt;24);00027                 low &lt;&lt;= 8;00028         }00029         <span class="keywordflow">return</span> high;00030 }00031 00032 <span class="keyword">inline</span> word32 Twofish::Base::h0(word32 x, <span class="keyword">const</span> word32 *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> kLen)00033 {00034         x = x | (x&lt;&lt;8) | (x&lt;&lt;16) | (x&lt;&lt;24);00035         <span class="keywordflow">switch</span>(kLen)00036         {00037 <span class="preprocessor">#define Q(a, b, c, d, t) q[a][GETBYTE(t,0)] ^ (q[b][GETBYTE(t,1)] &lt;&lt; 8) ^ (q[c][GETBYTE(t,2)] &lt;&lt; 16) ^ (q[d][GETBYTE(t,3)] &lt;&lt; 24)</span>00038 <span class="preprocessor"></span>        <span class="keywordflow">case</span> 4: x = Q(1, 0, 0, 1, x) ^ key[6];00039         <span class="keywordflow">case</span> 3: x = Q(1, 1, 0, 0, x) ^ key[4];00040         <span class="keywordflow">case</span> 2: x = Q(0, 1, 0, 1, x) ^ key[2];00041                         x = Q(0, 0, 1, 1, x) ^ key[0];00042         }00043         <span class="keywordflow">return</span> x;00044 }00045 00046 <span class="keyword">inline</span> word32 Twofish::Base::h(word32 x, <span class="keyword">const</span> word32 *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> kLen)00047 {00048         x = h0(x, key, kLen);00049         <span class="keywordflow">return</span> mds[0][GETBYTE(x,0)] ^ mds[1][GETBYTE(x,1)] ^ mds[2][GETBYTE(x,2)] ^ mds[3][GETBYTE(x,3)];00050 }00051 00052 <span class="keywordtype">void</span> Twofish::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylength)00053 {00054         AssertValidKeyLength(keylength);00055 00056         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = (keylength &lt;= 16 ? 2 : (keylength &lt;= 24 ? 3 : 4));00057         <a class="code" href="class_sec_block.html">SecBlock&lt;word32&gt;</a> key(len*2);00058         GetUserKey(LITTLE_ENDIAN_ORDER, key.<a class="code" href="class_sec_block.html#_sec_block_with_hinta7">begin</a>(), len*2, userKey, keylength);00059 00060         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00061         <span class="keywordflow">for</span> (i=0; i&lt;40; i+=2)00062         {00063                 word32 a = h(i, key, len);00064                 word32 b = rotlFixed(h(i+1, key+1, len), 8);00065                 m_k[i] = a+b;00066                 m_k[i+1] = rotlFixed(a+2*b, 9);00067         }00068 00069         <a class="code" href="class_sec_block.html">SecBlock&lt;word32&gt;</a> svec(2*len);00070         <span class="keywordflow">for</span> (i=0; i&lt;len; i++)00071                 svec[2*(len-i-1)] = ReedSolomon(key[2*i+1], key[2*i]);00072         <span class="keywordflow">for</span> (i=0; i&lt;256; i++)00073         {00074                 word32 t = h0(i, svec, len);00075                 m_s[0][i] = mds[0][GETBYTE(t, 0)];00076                 m_s[1][i] = mds[1][GETBYTE(t, 1)];00077                 m_s[2][i] = mds[2][GETBYTE(t, 2)];00078                 m_s[3][i] = mds[3][GETBYTE(t, 3)];00079         }00080 }00081 00082 <span class="preprocessor">#define G1(x) (m_s[0][GETBYTE(x,0)] ^ m_s[1][GETBYTE(x,1)] ^ m_s[2][GETBYTE(x,2)] ^ m_s[3][GETBYTE(x,3)])</span>00083 <span class="preprocessor"></span><span class="preprocessor">#define G2(x) (m_s[0][GETBYTE(x,3)] ^ m_s[1][GETBYTE(x,0)] ^ m_s[2][GETBYTE(x,1)] ^ m_s[3][GETBYTE(x,2)])</span>00084 <span class="preprocessor"></span>00085 <span class="preprocessor">#define ENCROUND(n, a, b, c, d) \</span>00086 <span class="preprocessor">        x = G1 (a); y = G2 (b); \</span>00087 <span class="preprocessor">        x += y; y += x + k[2 * (n) + 1]; \</span>00088 <span class="preprocessor">        (c) ^= x + k[2 * (n)]; \</span>00089 <span class="preprocessor">        (c) = rotrFixed(c, 1); \</span>00090 <span class="preprocessor">        (d) = rotlFixed(d, 1) ^ y</span>00091 <span class="preprocessor"></span>00092 <span class="preprocessor">#define ENCCYCLE(n) \</span>00093 <span class="preprocessor">        ENCROUND (2 * (n), a, b, c, d); \</span>00094 <span class="preprocessor">        ENCROUND (2 * (n) + 1, c, d, a, b)</span>00095 <span class="preprocessor"></span>00096 <span class="preprocessor">#define DECROUND(n, a, b, c, d) \</span>00097 <span class="preprocessor">        x = G1 (a); y = G2 (b); \</span>00098 <span class="preprocessor">        x += y; y += x; \</span>00099 <span class="preprocessor">        (d) ^= y + k[2 * (n) + 1]; \</span>00100 <span class="preprocessor">        (d) = rotrFixed(d, 1); \</span>00101 <span class="preprocessor">        (c) = rotlFixed(c, 1); \</span>00102 <span class="preprocessor">        (c) ^= (x + k[2 * (n)])</span>00103 <span class="preprocessor"></span>00104 <span class="preprocessor">#define DECCYCLE(n) \</span>00105 <span class="preprocessor">        DECROUND (2 * (n) + 1, c, d, a, b); \</span>00106 <span class="preprocessor">        DECROUND (2 * (n), a, b, c, d)</span>00107 <span class="preprocessor"></span>00108 <span class="keyword">typedef</span> BlockGetAndPut&lt;word32, LittleEndian&gt; Block;00109 00110 <span class="keywordtype">void</span> Twofish::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00111 <span class="keyword"></span>{00112         word32 x, y, a, b, c, d;00113 00114         Block::Get(inBlock)(a)(b)(c)(d);00115 00116         a ^= m_k[0];00117         b ^= m_k[1];00118         c ^= m_k[2];00119         d ^= m_k[3];00120 00121         <span class="keyword">const</span> word32 *k = m_k+8;00122         ENCCYCLE (0);00123         ENCCYCLE (1);00124         ENCCYCLE (2);00125         ENCCYCLE (3);00126         ENCCYCLE (4);00127         ENCCYCLE (5);00128         ENCCYCLE (6);00129         ENCCYCLE (7);00130 00131         c ^= m_k[4];00132         d ^= m_k[5];00133         a ^= m_k[6];00134         b ^= m_k[7]; 00135 00136         Block::Put(xorBlock, outBlock)(c)(d)(a)(b);00137 }00138 00139 <span class="keywordtype">void</span> Twofish::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00140 <span class="keyword"></span>{00141         word32 x, y, a, b, c, d;00142 00143         Block::Get(inBlock)(c)(d)(a)(b);00144 00145         c ^= m_k[4];00146         d ^= m_k[5];00147         a ^= m_k[6];00148         b ^= m_k[7];00149 00150         <span class="keyword">const</span> word32 *k = m_k+8;00151         DECCYCLE (7);00152         DECCYCLE (6);00153         DECCYCLE (5);00154         DECCYCLE (4);00155         DECCYCLE (3);00156         DECCYCLE (2);00157         DECCYCLE (1);00158         DECCYCLE (0);00159 00160         a ^= m_k[0];00161         b ^= m_k[1];00162         c ^= m_k[2];00163         d ^= m_k[3];00164 00165         Block::Put(xorBlock, outBlock)(a)(b)(c)(d);00166 }00167 00168 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:27 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 + -