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

📄 rijndael_8cpp-source.html

📁 Crypto++是一个非常强大的密码学库,主要是功能全
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!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++: rijndael.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>rijndael.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// rijndael.cpp - modified by Chris Morgan &lt;cmorgan@wpi.edu&gt;</span>00002 <span class="comment">// and Wei Dai from Paulo Baretto's Rijndael implementation</span>00003 <span class="comment">// The original code and all modifications are in the public domain.</span>00004 00005 <span class="comment">// This is the original introductory comment:</span>00006 <span class="comment"></span>00007 <span class="comment">/**</span>00008 <span class="comment"> * version 3.0 (December 2000)</span>00009 <span class="comment"> *</span>00010 <span class="comment"> * Optimised ANSI C code for the Rijndael cipher (now AES)</span>00011 <span class="comment"> *</span>00012 <span class="comment"> * author Vincent Rijmen &lt;vincent.rijmen@esat.kuleuven.ac.be&gt;</span>00013 <span class="comment"> * author Antoon Bosselaers &lt;antoon.bosselaers@esat.kuleuven.ac.be&gt;</span>00014 <span class="comment"> * author Paulo Barreto &lt;paulo.barreto@terra.com.br&gt;</span>00015 <span class="comment"> *</span>00016 <span class="comment"> * This code is hereby placed in the public domain.</span>00017 <span class="comment"> *</span>00018 <span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS</span>00019 <span class="comment"> * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED</span>00020 <span class="comment"> * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE</span>00021 <span class="comment"> * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE</span>00022 <span class="comment"> * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR</span>00023 <span class="comment"> * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF</span>00024 <span class="comment"> * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR</span>00025 <span class="comment"> * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,</span>00026 <span class="comment"> * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE</span>00027 <span class="comment"> * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,</span>00028 <span class="comment"> * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>00029 <span class="comment"> */</span>00030 00031 <span class="preprocessor">#include "pch.h"</span>00032 00033 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00034 <span class="preprocessor"></span>00035 <span class="preprocessor">#include "<a class="code" href="rijndael_8h.html">rijndael.h</a>"</span>00036 <span class="preprocessor">#include "misc.h"</span>00037 00038 NAMESPACE_BEGIN(CryptoPP)00039 00040 <span class="keywordtype">void</span> Rijndael::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *userKey, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> keylen)00041 {00042         AssertValidKeyLength(keylen);00043 00044         m_rounds = keylen/4 + 6;00045         m_key.New(4*(m_rounds+1));00046 00047         word32 temp, *rk = m_key;00048         <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0;00049 00050         GetUserKey(BIG_ENDIAN_ORDER, rk, keylen/4, userKey, keylen);00051 00052         <span class="keywordflow">switch</span>(keylen)00053         {00054         <span class="keywordflow">case</span> 16:00055                 <span class="keywordflow">while</span> (<span class="keyword">true</span>)00056                 {00057                         temp  = rk[3];00058                         rk[4] = rk[0] ^00059                                 (Te4[GETBYTE(temp, 2)] &amp; 0xff000000) ^00060                                 (Te4[GETBYTE(temp, 1)] &amp; 0x00ff0000) ^00061                                 (Te4[GETBYTE(temp, 0)] &amp; 0x0000ff00) ^00062                                 (Te4[GETBYTE(temp, 3)] &amp; 0x000000ff) ^00063                                 rcon[i];00064                         rk[5] = rk[1] ^ rk[4];00065                         rk[6] = rk[2] ^ rk[5];00066                         rk[7] = rk[3] ^ rk[6];00067                         <span class="keywordflow">if</span> (++i == 10)00068                                 <span class="keywordflow">break</span>;00069                         rk += 4;00070                 }00071                 <span class="keywordflow">break</span>;00072 00073         <span class="keywordflow">case</span> 24:00074                 <span class="keywordflow">while</span> (<span class="keyword">true</span>)    <span class="comment">// for (;;) here triggers a bug in VC60 SP4 w/ Processor Pack</span>00075                 {00076                         temp = rk[ 5];00077                         rk[ 6] = rk[ 0] ^00078                                 (Te4[GETBYTE(temp, 2)] &amp; 0xff000000) ^00079                                 (Te4[GETBYTE(temp, 1)] &amp; 0x00ff0000) ^00080                                 (Te4[GETBYTE(temp, 0)] &amp; 0x0000ff00) ^00081                                 (Te4[GETBYTE(temp, 3)] &amp; 0x000000ff) ^00082                                 rcon[i];00083                         rk[ 7] = rk[ 1] ^ rk[ 6];00084                         rk[ 8] = rk[ 2] ^ rk[ 7];00085                         rk[ 9] = rk[ 3] ^ rk[ 8];00086                         <span class="keywordflow">if</span> (++i == 8)00087                                 <span class="keywordflow">break</span>;00088                         rk[10] = rk[ 4] ^ rk[ 9];00089                         rk[11] = rk[ 5] ^ rk[10];00090                         rk += 6;00091                 }00092                 <span class="keywordflow">break</span>;00093 00094         <span class="keywordflow">case</span> 32:00095         <span class="keywordflow">while</span> (<span class="keyword">true</span>)00096                 {00097                 temp = rk[ 7];00098                 rk[ 8] = rk[ 0] ^00099                         (Te4[GETBYTE(temp, 2)] &amp; 0xff000000) ^00100                         (Te4[GETBYTE(temp, 1)] &amp; 0x00ff0000) ^00101                         (Te4[GETBYTE(temp, 0)] &amp; 0x0000ff00) ^00102                         (Te4[GETBYTE(temp, 3)] &amp; 0x000000ff) ^00103                         rcon[i];00104                 rk[ 9] = rk[ 1] ^ rk[ 8];00105                 rk[10] = rk[ 2] ^ rk[ 9];00106                 rk[11] = rk[ 3] ^ rk[10];00107                         <span class="keywordflow">if</span> (++i == 7)00108                                 <span class="keywordflow">break</span>;00109                 temp = rk[11];00110                 rk[12] = rk[ 4] ^00111                         (Te4[GETBYTE(temp, 3)] &amp; 0xff000000) ^00112                         (Te4[GETBYTE(temp, 2)] &amp; 0x00ff0000) ^00113                         (Te4[GETBYTE(temp, 1)] &amp; 0x0000ff00) ^00114                         (Te4[GETBYTE(temp, 0)] &amp; 0x000000ff);00115                 rk[13] = rk[ 5] ^ rk[12];00116                 rk[14] = rk[ 6] ^ rk[13];00117                 rk[15] = rk[ 7] ^ rk[14];00118 00119                         rk += 8;00120         }00121                 <span class="keywordflow">break</span>;00122         }00123 00124         <span class="keywordflow">if</span> (dir == DECRYPTION)00125         {00126                 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i, j;00127                 rk = m_key;00128 00129                 <span class="comment">/* invert the order of the round keys: */</span>00130                 <span class="keywordflow">for</span> (i = 0, j = 4*m_rounds; i &lt; j; i += 4, j -= 4) {00131                         temp = rk[i    ]; rk[i    ] = rk[j    ]; rk[j    ] = temp;00132                         temp = rk[i + 1]; rk[i + 1] = rk[j + 1]; rk[j + 1] = temp;00133                         temp = rk[i + 2]; rk[i + 2] = rk[j + 2]; rk[j + 2] = temp;00134                         temp = rk[i + 3]; rk[i + 3] = rk[j + 3]; rk[j + 3] = temp;00135                 }00136                 <span class="comment">/* apply the inverse MixColumn transform to all round keys but the first and the last: */</span>00137                 <span class="keywordflow">for</span> (i = 1; i &lt; m_rounds; i++) {00138                         rk += 4;00139                         rk[0] =00140                                 Td0[Te4[GETBYTE(rk[0], 3)] &amp; 0xff] ^00141                                 Td1[Te4[GETBYTE(rk[0], 2)] &amp; 0xff] ^00142                                 Td2[Te4[GETBYTE(rk[0], 1)] &amp; 0xff] ^00143                                 Td3[Te4[GETBYTE(rk[0], 0)] &amp; 0xff];00144                         rk[1] =00145                                 Td0[Te4[GETBYTE(rk[1], 3)] &amp; 0xff] ^00146                                 Td1[Te4[GETBYTE(rk[1], 2)] &amp; 0xff] ^00147                                 Td2[Te4[GETBYTE(rk[1], 1)] &amp; 0xff] ^00148                                 Td3[Te4[GETBYTE(rk[1], 0)] &amp; 0xff];00149                         rk[2] =00150                                 Td0[Te4[GETBYTE(rk[2], 3)] &amp; 0xff] ^00151                                 Td1[Te4[GETBYTE(rk[2], 2)] &amp; 0xff] ^00152                                 Td2[Te4[GETBYTE(rk[2], 1)] &amp; 0xff] ^00153                                 Td3[Te4[GETBYTE(rk[2], 0)] &amp; 0xff];00154                         rk[3] =00155                                 Td0[Te4[GETBYTE(rk[3], 3)] &amp; 0xff] ^00156                                 Td1[Te4[GETBYTE(rk[3], 2)] &amp; 0xff] ^00157                                 Td2[Te4[GETBYTE(rk[3], 1)] &amp; 0xff] ^00158                                 Td3[Te4[GETBYTE(rk[3], 0)] &amp; 0xff];00159                 }00160         }00161 }00162 00163 <span class="keyword">typedef</span> BlockGetAndPut&lt;word32, BigEndian&gt; Block;00164 00165 <span class="keywordtype">void</span> Rijndael::Enc::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00166 <span class="keyword"></span>{00167         word32 s0, s1, s2, s3, t0, t1, t2, t3;00168         <span class="keyword">const</span> word32 *rk = m_key;00169 00170     <span class="comment">/*</span>00171 <span class="comment">         * map byte array block to cipher state</span>00172 <span class="comment">         * and add initial round key:</span>00173 <span class="comment">         */</span>00174         Block::Get(inBlock)(s0)(s1)(s2)(s3);00175         s0 ^= rk[0];00176         s1 ^= rk[1];00177         s2 ^= rk[2];00178         s3 ^= rk[3];00179     <span class="comment">/*</span>00180 <span class="comment">         * Nr - 1 full rounds:</span>00181 <span class="comment">         */</span>00182     <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> r = m_rounds &gt;&gt; 1;00183     <span class="keywordflow">for</span> (;;) {00184         t0 =00185             Te0[GETBYTE(s0, 3)] ^00186             Te1[GETBYTE(s1, 2)] ^00187             Te2[GETBYTE(s2, 1)] ^00188             Te3[GETBYTE(s3, 0)] ^00189             rk[4];00190         t1 =

⌨️ 快捷键说明

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