📄 shark_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++: shark.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>shark.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// shark.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">#ifdef WORD64_AVAILABLE</span>00006 <span class="preprocessor"></span>00007 <span class="preprocessor">#include "<a class="code" href="shark_8h.html">shark.h</a>"</span>00008 <span class="preprocessor">#include "misc.h"</span>00009 <span class="preprocessor">#include "<a class="code" href="modes_8h.html">modes.h</a>"</span>00010 <span class="preprocessor">#include "gf256.h"</span>00011 00012 NAMESPACE_BEGIN(CryptoPP)00013 00014 <span class="keyword">static</span> word64 SHARKTransform(word64 a)00015 {00016 <span class="keyword">static</span> <span class="keyword">const</span> byte iG[8][8] = {00017 0xe7, 0x30, 0x90, 0x85, 0xd0, 0x4b, 0x91, 0x41, 00018 0x53, 0x95, 0x9b, 0xa5, 0x96, 0xbc, 0xa1, 0x68, 00019 0x02, 0x45, 0xf7, 0x65, 0x5c, 0x1f, 0xb6, 0x52, 00020 0xa2, 0xca, 0x22, 0x94, 0x44, 0x63, 0x2a, 0xa2, 00021 0xfc, 0x67, 0x8e, 0x10, 0x29, 0x75, 0x85, 0x71, 00022 0x24, 0x45, 0xa2, 0xcf, 0x2f, 0x22, 0xc1, 0x0e, 00023 0xa1, 0xf1, 0x71, 0x40, 0x91, 0x27, 0x18, 0xa5, 00024 0x56, 0xf4, 0xaf, 0x32, 0xd2, 0xa4, 0xdc, 0x71, 00025 };00026 00027 word64 result=0;00028 <a class="code" href="class_g_f256.html">GF256</a> gf256(0xf5);00029 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<8; i++)00030 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> j=0; j<8; j++) 00031 result ^= word64(gf256.<a class="code" href="class_g_f256.html#_g_f256a11">Multiply</a>(iG[i][j], a>>(56-8*j))) << (56-8*i);00032 <span class="keywordflow">return</span> result;00033 }00034 00035 <span class="keywordtype">void</span> SHARK::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keyLen, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> rounds)00036 {00037 AssertValidKeyLength(keyLen);00038 AssertValidRounds(rounds);00039 00040 m_rounds = rounds;00041 m_roundKeys.New(m_rounds+1);00042 00043 <span class="comment">// concatenate key enought times to fill a</span>00044 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<(m_rounds+1)*8; i++)00045 ((byte *)m_roundKeys.begin())[i] = key[i%keyLen];00046 00047 <a class="code" href="class_block_cipher_template.html">SHARK::Encryption</a> e;00048 e.InitForKeySetup();00049 byte <a class="code" href="namespace_name.html#a4">IV</a>[8] = {0,0,0,0,0,0,0,0};00050 <a class="code" href="class_cipher_mode_final_template___external_cipher.html">CFB_Mode_ExternalCipher::Encryption</a> cfb(e, IV);00051 00052 cfb.ProcessString((byte *)m_roundKeys.begin(), (m_rounds+1)*8);00053 00054 ConditionalByteReverse(BIG_ENDIAN_ORDER, m_roundKeys.begin(), m_roundKeys.begin(), (m_rounds+1)*8);00055 00056 m_roundKeys[m_rounds] = SHARKTransform(m_roundKeys[m_rounds]);00057 00058 <span class="keywordflow">if</span> (dir == DECRYPTION)00059 {00060 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i;00061 00062 <span class="comment">// transform encryption round keys into decryption round keys</span>00063 <span class="keywordflow">for</span> (i=0; i<m_rounds/2; i++)00064 std::swap(m_roundKeys[i], m_roundKeys[m_rounds-i]);00065 00066 <span class="keywordflow">for</span> (i=1; i<m_rounds; i++)00067 m_roundKeys[i] = SHARKTransform(m_roundKeys[i]);00068 }00069 00070 <span class="preprocessor">#ifdef IS_LITTLE_ENDIAN</span>00071 <span class="preprocessor"></span> m_roundKeys[0] = ByteReverse(m_roundKeys[0]);00072 m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]);00073 <span class="preprocessor">#endif</span>00074 <span class="preprocessor"></span>}00075 00076 <span class="comment">// construct an SHARK_Enc object with fixed round keys, to be used to initialize actual round keys</span>00077 <span class="keywordtype">void</span> SHARK::Enc::InitForKeySetup()00078 {00079 m_rounds = DEFAULT_ROUNDS;00080 m_roundKeys.New(DEFAULT_ROUNDS+1);00081 00082 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<DEFAULT_ROUNDS; i++)00083 m_roundKeys[i] = cbox[0][i];00084 00085 m_roundKeys[DEFAULT_ROUNDS] = SHARKTransform(cbox[0][DEFAULT_ROUNDS]);00086 00087 <span class="preprocessor">#ifdef IS_LITTLE_ENDIAN</span>00088 <span class="preprocessor"></span> m_roundKeys[0] = ByteReverse(m_roundKeys[0]);00089 m_roundKeys[m_rounds] = ByteReverse(m_roundKeys[m_rounds]);00090 <span class="preprocessor">#endif</span>00091 <span class="preprocessor"></span>}00092 00093 <span class="keyword">typedef</span> word64 ArrayOf256Word64s[256];00094 00095 <span class="keyword">template</span> <const byte *sbox, const ArrayOf256Word64s *cbox>00096 <span class="keyword">struct </span>SharkProcessAndXorBlock{ <span class="comment">// VC60 workaround: problem with template functions</span>00097 <span class="keyword">inline</span> SharkProcessAndXorBlock(<span class="keyword">const</span> word64 *roundKeys, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> rounds, <span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)00098 {00099 word64 tmp = *(word64 *)inBlock ^ roundKeys[0];00100 00101 ByteOrder order = GetNativeByteOrder();00102 tmp = cbox[0][GetByte(order, tmp, 0)] ^ cbox[1][GetByte(order, tmp, 1)] 00103 ^ cbox[2][GetByte(order, tmp, 2)] ^ cbox[3][GetByte(order, tmp, 3)] 00104 ^ cbox[4][GetByte(order, tmp, 4)] ^ cbox[5][GetByte(order, tmp, 5)] 00105 ^ cbox[6][GetByte(order, tmp, 6)] ^ cbox[7][GetByte(order, tmp, 7)]00106 ^ roundKeys[1];00107 00108 <span class="keywordflow">for</span>(<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=2; i<rounds; i++) 00109 {00110 tmp = cbox[0][GETBYTE(tmp, 7)] ^ cbox[1][GETBYTE(tmp, 6)] 00111 ^ cbox[2][GETBYTE(tmp, 5)] ^ cbox[3][GETBYTE(tmp, 4)] 00112 ^ cbox[4][GETBYTE(tmp, 3)] ^ cbox[5][GETBYTE(tmp, 2)] 00113 ^ cbox[6][GETBYTE(tmp, 1)] ^ cbox[7][GETBYTE(tmp, 0)]00114 ^ roundKeys[i];00115 }00116 00117 PutBlock<byte, BigEndian>(xorBlock, outBlock)00118 (sbox[GETBYTE(tmp, 7)])00119 (sbox[GETBYTE(tmp, 6)])00120 (sbox[GETBYTE(tmp, 5)])00121 (sbox[GETBYTE(tmp, 4)])00122 (sbox[GETBYTE(tmp, 3)])00123 (sbox[GETBYTE(tmp, 2)])00124 (sbox[GETBYTE(tmp, 1)])00125 (sbox[GETBYTE(tmp, 0)]);00126 00127 *(word64 *)outBlock ^= roundKeys[rounds];00128 }};00129 00130 <span class="keywordtype">void</span> SHARK::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00131 <span class="keyword"></span>{00132 SharkProcessAndXorBlock<sbox, cbox>(m_roundKeys, m_rounds, inBlock, xorBlock, outBlock);00133 }00134 00135 <span class="keywordtype">void</span> SHARK::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00136 <span class="keyword"></span>{00137 SharkProcessAndXorBlock<sbox, cbox>(m_roundKeys, m_rounds, inBlock, xorBlock, outBlock);00138 }00139 00140 NAMESPACE_END00141 00142 <span class="preprocessor">#endif // WORD64_AVAILABLE</span></pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:25 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 + -