📄 3way_8cpp-source.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++: 3way.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 Page</a> | <a class="qindex" href="namespaces.html">Namespace List</a> | <a class="qindex" href="hierarchy.html">Class Hierarchy</a> | <a class="qindex" href="classes.html">Alphabetical List</a> | <a class="qindex" href="annotated.html">Compound List</a> | <a class="qindex" href="files.html">File List</a> | <a class="qindex" href="namespacemembers.html">Namespace Members</a> | <a class="qindex" href="functions.html">Compound Members</a> | <a class="qindex" href="globals.html">File Members</a></div><h1>3way.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// 3way.cpp - modifed by Wei Dai from Joan Daemen's 3way.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="3way_8h.html">3way.h</a>"</span>00006 <span class="preprocessor">#include "misc.h"</span>00007 00008 NAMESPACE_BEGIN(CryptoPP)00009 00010 <span class="keywordtype">void</span> ThreeWay_TestInstantiations()00011 {00012 <a class="code" href="class_block_cipher_template.html">ThreeWay::Encryption</a> x1;00013 <a class="code" href="class_block_cipher_template.html">ThreeWay::Decryption</a> x2;00014 }00015 00016 <span class="keyword">static</span> <span class="keyword">const</span> word32 START_E = 0x0b0b; <span class="comment">// round constant of first encryption round</span>00017 <span class="keyword">static</span> <span class="keyword">const</span> word32 START_D = 0xb1b1; <span class="comment">// round constant of first decryption round</span>00018 <span class="keyword">static</span> <span class="keyword">const</span> word32 RC_MODULUS = 0x11011;00019 00020 <span class="keyword">static</span> <span class="keyword">inline</span> word32 reverseBits(word32 a)00021 {00022 a = ((a & 0xAAAAAAAA) >> 1) | ((a & 0x55555555) << 1);00023 a = ((a & 0xCCCCCCCC) >> 2) | ((a & 0x33333333) << 2);00024 <span class="keywordflow">return</span> ((a & 0xF0F0F0F0) >> 4) | ((a & 0x0F0F0F0F) << 4);00025 }00026 00027 <span class="preprocessor">#define mu(a0, a1, a2) \</span>00028 <span class="preprocessor">{ \</span>00029 <span class="preprocessor"> a1 = reverseBits(a1); \</span>00030 <span class="preprocessor"> word32 t = reverseBits(a0); \</span>00031 <span class="preprocessor"> a0 = reverseBits(a2); \</span>00032 <span class="preprocessor"> a2 = t; \</span>00033 <span class="preprocessor">}</span>00034 <span class="preprocessor"></span>00035 <span class="preprocessor">#define pi_gamma_pi(a0, a1, a2) \</span>00036 <span class="preprocessor">{ \</span>00037 <span class="preprocessor"> word32 b0, b2; \</span>00038 <span class="preprocessor"> b2 = rotlFixed(a2, 1U); \</span>00039 <span class="preprocessor"> b0 = rotlFixed(a0, 22U); \</span>00040 <span class="preprocessor"> a0 = rotlFixed(b0 ^ (a1|(~b2)), 1U); \</span>00041 <span class="preprocessor"> a2 = rotlFixed(b2 ^ (b0|(~a1)), 22U);\</span>00042 <span class="preprocessor"> a1 ^= (b2|(~b0)); \</span>00043 <span class="preprocessor">}</span>00044 <span class="preprocessor"></span>00045 <span class="comment">// thanks to Paulo Barreto for this optimized theta()</span>00046 <span class="preprocessor">#define theta(a0, a1, a2) \</span>00047 <span class="preprocessor">{ \</span>00048 <span class="preprocessor"> word32 b0, b1, c; \</span>00049 <span class="preprocessor"> c = a0 ^ a1 ^ a2; \</span>00050 <span class="preprocessor"> c = rotlFixed(c, 16U) ^ rotlFixed(c, 8U); \</span>00051 <span class="preprocessor"> b0 = (a0 << 24) ^ (a2 >> 8) ^ (a1 << 8) ^ (a0 >> 24); \</span>00052 <span class="preprocessor"> b1 = (a1 << 24) ^ (a0 >> 8) ^ (a2 << 8) ^ (a1 >> 24); \</span>00053 <span class="preprocessor"> a0 ^= c ^ b0; \</span>00054 <span class="preprocessor"> a1 ^= c ^ b1; \</span>00055 <span class="preprocessor"> a2 ^= c ^ (b0 >> 16) ^ (b1 << 16); \</span>00056 <span class="preprocessor">} </span>00057 <span class="preprocessor"></span>00058 <span class="preprocessor">#define rho(a0, a1, a2) \</span>00059 <span class="preprocessor">{ \</span>00060 <span class="preprocessor"> theta(a0, a1, a2); \</span>00061 <span class="preprocessor"> pi_gamma_pi(a0, a1, a2); \</span>00062 <span class="preprocessor">} </span>00063 <span class="preprocessor"></span>00064 <span class="keywordtype">void</span> ThreeWay::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *uk, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> r)00065 {00066 AssertValidKeyLength(length);00067 AssertValidRounds(r);00068 00069 m_rounds = r;00070 00071 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<3; i++)00072 m_k[i] = (word32)uk[4*i+3] | ((word32)uk[4*i+2]<<8) | ((word32)uk[4*i+1]<<16) | ((word32)uk[4*i]<<24);00073 00074 <span class="keywordflow">if</span> (dir == DECRYPTION)00075 {00076 theta(m_k[0], m_k[1], m_k[2]);00077 mu(m_k[0], m_k[1], m_k[2]);00078 m_k[0] = ByteReverse(m_k[0]);00079 m_k[1] = ByteReverse(m_k[1]);00080 m_k[2] = ByteReverse(m_k[2]);00081 }00082 }00083 00084 <span class="keywordtype">void</span> ThreeWay::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00085 <span class="keyword"></span>{00086 <span class="keyword">typedef</span> BlockGetAndPut<word32, BigEndian> Block;00087 00088 word32 a0, a1, a2;00089 Block::Get(inBlock)(a0)(a1)(a2);00090 00091 word32 rc = START_E;00092 00093 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i=0; i<m_rounds; i++)00094 {00095 a0 ^= m_k[0] ^ (rc<<16);00096 a1 ^= m_k[1];00097 a2 ^= m_k[2] ^ rc;00098 rho(a0, a1, a2);00099 00100 rc <<= 1;00101 <span class="keywordflow">if</span> (rc&0x10000) rc ^= 0x11011;00102 }00103 a0 ^= m_k[0] ^ (rc<<16);00104 a1 ^= m_k[1];00105 a2 ^= m_k[2] ^ rc;00106 theta(a0, a1, a2);00107 00108 Block::Put(xorBlock, outBlock)(a0)(a1)(a2);00109 }00110 00111 <span class="keywordtype">void</span> ThreeWay::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00112 <span class="keyword"></span>{00113 <span class="keyword">typedef</span> BlockGetAndPut<word32, LittleEndian> Block;00114 00115 word32 a0, a1, a2;00116 Block::Get(inBlock)(a0)(a1)(a2);00117 00118 word32 rc = START_D;00119 00120 mu(a0, a1, a2);00121 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> i=0; i<m_rounds; i++)00122 {00123 a0 ^= m_k[0] ^ (rc<<16);00124 a1 ^= m_k[1];00125 a2 ^= m_k[2] ^ rc;00126 rho(a0, a1, a2);00127 00128 rc <<= 1;00129 <span class="keywordflow">if</span> (rc&0x10000) rc ^= 0x11011;00130 }00131 a0 ^= m_k[0] ^ (rc<<16);00132 a1 ^= m_k[1];00133 a2 ^= m_k[2] ^ rc;00134 theta(a0, a1, a2);00135 mu(a0, a1, a2);00136 00137 Block::Put(xorBlock, outBlock)(a0)(a1)(a2);00138 }00139 00140 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:07 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 + -