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

📄 square_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++: square.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>square.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// square.cpp - written and placed in the public domain by Wei Dai</span>00002 <span class="comment">// Based on Paulo S.L.M. Barreto's public domain implementation</span>00003 00004 <span class="preprocessor">#include "pch.h"</span>00005 <span class="preprocessor">#include "<a class="code" href="square_8h.html">square.h</a>"</span>00006 <span class="preprocessor">#include "misc.h"</span>00007 <span class="preprocessor">#include "gf256.h"</span>00008 00009 NAMESPACE_BEGIN(CryptoPP)00010 00011 <span class="comment">// apply theta to a roundkey</span>00012 <span class="keyword">static</span> <span class="keywordtype">void</span> SquareTransform (word32 in[4], word32 out[4])00013 {00014         <span class="keyword">static</span> <span class="keyword">const</span> byte G[4][4] = 00015         {00016                 0x02U, 0x01U, 0x01U, 0x03U, 00017                 0x03U, 0x02U, 0x01U, 0x01U, 00018                 0x01U, 0x03U, 0x02U, 0x01U, 00019                 0x01U, 0x01U, 0x03U, 0x02U00020         };00021 00022         <a class="code" href="class_g_f256.html">GF256</a> gf256(0xf5);00023 00024         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; 4; i++)00025         {00026                 word32 temp = 0;00027                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; 4; j++)00028                         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> k = 0; k &lt; 4; k++)00029                                 temp ^= (word32)gf256.<a class="code" href="class_g_f256.html#_g_f256a11">Multiply</a>(GETBYTE(in[i], 3-k), G[k][j]) &lt;&lt; ((3-j)*8);00030                 out[i] = temp;00031         }00032 }00033 00034 <span class="keywordtype">void</span> Square::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00035 {00036         AssertValidKeyLength(length);00037 00038         <span class="keyword">static</span> <span class="keyword">const</span> word32 offset[ROUNDS] = {00039         0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,00040         0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL,00041         };00042 00043         GetUserKey(BIG_ENDIAN_ORDER, roundkeys[0], KEYLENGTH/4, userKey, KEYLENGTH);00044 00045         <span class="comment">/* apply the key evolution function */</span>00046         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt; ROUNDS+1; i++)00047         {00048                 roundkeys[i][0] = roundkeys[i-1][0] ^ rotlFixed(roundkeys[i-1][3], 8U) ^ offset[i-1];00049                 roundkeys[i][1] = roundkeys[i-1][1] ^ roundkeys[i][0];00050                 roundkeys[i][2] = roundkeys[i-1][2] ^ roundkeys[i][1];00051                 roundkeys[i][3] = roundkeys[i-1][3] ^ roundkeys[i][2];00052         }  00053 00054         <span class="comment">/* produce the round keys */</span>00055         <span class="keywordflow">if</span> (dir == ENCRYPTION)00056         {00057                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ROUNDS; i++)00058                         SquareTransform (roundkeys[i], roundkeys[i]);00059         }00060         <span class="keywordflow">else</span>00061         {00062                 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; ROUNDS/2; i++)00063                         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> j = 0; j &lt; 4; j++)00064                                 std::swap(roundkeys[i][j], roundkeys[ROUNDS-i][j]);00065                 SquareTransform (roundkeys[ROUNDS], roundkeys[ROUNDS]);00066         }00067 }00068 00069 <span class="preprocessor">#define MSB(x) (((x) &gt;&gt; 24) &amp; 0xffU)    </span><span class="comment">/* most  significant byte */</span>00070 <span class="preprocessor">#define SSB(x) (((x) &gt;&gt; 16) &amp; 0xffU)    </span><span class="comment">/* second in significance */</span>00071 <span class="preprocessor">#define TSB(x) (((x) &gt;&gt;  8) &amp; 0xffU)    </span><span class="comment">/* third  in significance */</span>00072 <span class="preprocessor">#define LSB(x) (((x)      ) &amp; 0xffU)    </span><span class="comment">/* least significant byte */</span>00073 00074 <span class="preprocessor">#define squareRound(text, temp, T0, T1, T2, T3, roundkey) \</span>00075 <span class="preprocessor">{ \</span>00076 <span class="preprocessor">        temp[0] = T0[MSB (text[0])] \</span>00077 <span class="preprocessor">                        ^ T1[MSB (text[1])] \</span>00078 <span class="preprocessor">                        ^ T2[MSB (text[2])] \</span>00079 <span class="preprocessor">                        ^ T3[MSB (text[3])] \</span>00080 <span class="preprocessor">                        ^ roundkey[0]; \</span>00081 <span class="preprocessor">        temp[1] = T0[SSB (text[0])] \</span>00082 <span class="preprocessor">                        ^ T1[SSB (text[1])] \</span>00083 <span class="preprocessor">                        ^ T2[SSB (text[2])] \</span>00084 <span class="preprocessor">                        ^ T3[SSB (text[3])] \</span>00085 <span class="preprocessor">                        ^ roundkey[1]; \</span>00086 <span class="preprocessor">        temp[2] = T0[TSB (text[0])] \</span>00087 <span class="preprocessor">                        ^ T1[TSB (text[1])] \</span>00088 <span class="preprocessor">                        ^ T2[TSB (text[2])] \</span>00089 <span class="preprocessor">                        ^ T3[TSB (text[3])] \</span>00090 <span class="preprocessor">                        ^ roundkey[2]; \</span>00091 <span class="preprocessor">        temp[3] = T0[LSB (text[0])] \</span>00092 <span class="preprocessor">                        ^ T1[LSB (text[1])] \</span>00093 <span class="preprocessor">                        ^ T2[LSB (text[2])] \</span>00094 <span class="preprocessor">                        ^ T3[LSB (text[3])] \</span>00095 <span class="preprocessor">                        ^ roundkey[3]; \</span>00096 <span class="preprocessor">} </span><span class="comment">/* squareRound */</span>00097 00098 <span class="preprocessor">#define squareFinal(text, temp, S, roundkey) \</span>00099 <span class="preprocessor">{ \</span>00100 <span class="preprocessor">        text[0] = ((word32) (S[MSB (temp[0])]) &lt;&lt; 24) \</span>00101 <span class="preprocessor">                        ^ ((word32) (S[MSB (temp[1])]) &lt;&lt; 16) \</span>00102 <span class="preprocessor">                        ^ ((word32) (S[MSB (temp[2])]) &lt;&lt;  8) \</span>00103 <span class="preprocessor">                        ^  (word32) (S[MSB (temp[3])]) \</span>00104 <span class="preprocessor">                        ^ roundkey[0]; \</span>00105 <span class="preprocessor">        text[1] = ((word32) (S[SSB (temp[0])]) &lt;&lt; 24) \</span>00106 <span class="preprocessor">                        ^ ((word32) (S[SSB (temp[1])]) &lt;&lt; 16) \</span>00107 <span class="preprocessor">                        ^ ((word32) (S[SSB (temp[2])]) &lt;&lt;  8) \</span>00108 <span class="preprocessor">                        ^  (word32) (S[SSB (temp[3])]) \</span>00109 <span class="preprocessor">                        ^ roundkey[1]; \</span>00110 <span class="preprocessor">        text[2] = ((word32) (S[TSB (temp[0])]) &lt;&lt; 24) \</span>00111 <span class="preprocessor">                        ^ ((word32) (S[TSB (temp[1])]) &lt;&lt; 16) \</span>00112 <span class="preprocessor">                        ^ ((word32) (S[TSB (temp[2])]) &lt;&lt;  8) \</span>00113 <span class="preprocessor">                        ^  (word32) (S[TSB (temp[3])]) \</span>00114 <span class="preprocessor">                        ^ roundkey[2]; \</span>00115 <span class="preprocessor">        text[3] = ((word32) (S[LSB (temp[0])]) &lt;&lt; 24) \</span>00116 <span class="preprocessor">                        ^ ((word32) (S[LSB (temp[1])]) &lt;&lt; 16) \</span>00117 <span class="preprocessor">                        ^ ((word32) (S[LSB (temp[2])]) &lt;&lt;  8) \</span>00118 <span class="preprocessor">                        ^  (word32) (S[LSB (temp[3])]) \</span>00119 <span class="preprocessor">                        ^ roundkey[3]; \</span>00120 <span class="preprocessor">} </span><span class="comment">/* squareFinal */</span>00121 00122 <span class="keyword">typedef</span> BlockGetAndPut&lt;word32, BigEndian&gt; Block;00123 00124 <span class="keywordtype">void</span> Square::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00125 <span class="keyword"></span>{00126         word32 text[4], temp[4];00127         Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);00128    00129         <span class="comment">/* initial key addition */</span>00130         text[0] ^= roundkeys[0][0];00131         text[1] ^= roundkeys[0][1];00132         text[2] ^= roundkeys[0][2];00133         text[3] ^= roundkeys[0][3];00134  00135         <span class="comment">/* ROUNDS - 1 full rounds */</span>00136         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=1; i+1&lt;ROUNDS; i+=2)00137         {00138                 squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys[i]);00139                 squareRound (temp, text, Te[0], Te[1], Te[2], Te[3], roundkeys[i+1]);00140         }00141         squareRound (text, temp, Te[0], Te[1], Te[2], Te[3], roundkeys[ROUNDS-1]);00142 00143         <span class="comment">/* last round (diffusion becomes only transposition) */</span>00144         squareFinal (text, temp, Se, roundkeys[ROUNDS]);00145 00146         Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);00147 }00148 00149 <span class="keywordtype">void</span> Square::Dec::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00150 <span class="keyword"></span>{00151         word32 text[4], temp[4];00152         Block::Get(inBlock)(text[0])(text[1])(text[2])(text[3]);00153    00154         <span class="comment">/* initial key addition */</span>00155         text[0] ^= roundkeys[0][0];00156         text[1] ^= roundkeys[0][1];00157         text[2] ^= roundkeys[0][2];00158         text[3] ^= roundkeys[0][3];00159  00160         <span class="comment">/* ROUNDS - 1 full rounds */</span>00161         <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=1; i+1&lt;ROUNDS; i+=2)00162         {00163                 squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys[i]);00164                 squareRound (temp, text, Td[0], Td[1], Td[2], Td[3], roundkeys[i+1]);00165         }00166         squareRound (text, temp, Td[0], Td[1], Td[2], Td[3], roundkeys[ROUNDS-1]);00167 00168         <span class="comment">/* last round (diffusion becomes only transposition) */</span>00169         squareFinal (text, temp, Sd, roundkeys[ROUNDS]);00170 00171         Block::Put(xorBlock, outBlock)(text[0])(text[1])(text[2])(text[3]);00172 }00173 00174 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:26 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 + -