📄 des_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++: des.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>des.cpp</h1><div class="fragment"><pre>00001 <span class="comment">// des.cpp - modified by Wei Dai from Phil Karn's des.c</span>00002 <span class="comment">// The original code and all modifications are in the public domain.</span>00003 00004 <span class="comment">/*</span>00005 <span class="comment"> * This is a major rewrite of my old public domain DES code written</span>00006 <span class="comment"> * circa 1987, which in turn borrowed heavily from Jim Gillogly's 1977</span>00007 <span class="comment"> * public domain code. I pretty much kept my key scheduling code, but</span>00008 <span class="comment"> * the actual encrypt/decrypt routines are taken from from Richard</span>00009 <span class="comment"> * Outerbridge's DES code as printed in Schneier's "Applied Cryptography."</span>00010 <span class="comment"> *</span>00011 <span class="comment"> * This code is in the public domain. I would appreciate bug reports and</span>00012 <span class="comment"> * enhancements.</span>00013 <span class="comment"> *</span>00014 <span class="comment"> * Phil Karn KA9Q, karn@unix.ka9q.ampr.org, August 1994.</span>00015 <span class="comment"> */</span>00016 00017 <span class="preprocessor">#include "pch.h"</span>00018 <span class="preprocessor">#include "misc.h"</span>00019 <span class="preprocessor">#include "<a class="code" href="des_8h.html">des.h</a>"</span>00020 00021 NAMESPACE_BEGIN(CryptoPP)00022 00023 <span class="keyword">typedef</span> BlockGetAndPut<word32, BigEndian> Block;00024 00025 <span class="comment">// Richard Outerbridge's initial permutation algorithm</span>00026 <span class="comment">/*</span>00027 <span class="comment">inline void IPERM(word32 &left, word32 &right)</span>00028 <span class="comment">{</span>00029 <span class="comment"> word32 work;</span>00030 <span class="comment"></span>00031 <span class="comment"> work = ((left >> 4) ^ right) & 0x0f0f0f0f;</span>00032 <span class="comment"> right ^= work;</span>00033 <span class="comment"> left ^= work << 4;</span>00034 <span class="comment"> work = ((left >> 16) ^ right) & 0xffff;</span>00035 <span class="comment"> right ^= work;</span>00036 <span class="comment"> left ^= work << 16;</span>00037 <span class="comment"> work = ((right >> 2) ^ left) & 0x33333333;</span>00038 <span class="comment"> left ^= work;</span>00039 <span class="comment"> right ^= (work << 2);</span>00040 <span class="comment"> work = ((right >> 8) ^ left) & 0xff00ff;</span>00041 <span class="comment"> left ^= work;</span>00042 <span class="comment"> right ^= (work << 8);</span>00043 <span class="comment"> right = rotl(right, 1);</span>00044 <span class="comment"> work = (left ^ right) & 0xaaaaaaaa;</span>00045 <span class="comment"> left ^= work;</span>00046 <span class="comment"> right ^= work;</span>00047 <span class="comment"> left = rotl(left, 1);</span>00048 <span class="comment">}</span>00049 <span class="comment">inline void FPERM(word32 &left, word32 &right)</span>00050 <span class="comment">{</span>00051 <span class="comment"> word32 work;</span>00052 <span class="comment"></span>00053 <span class="comment"> right = rotr(right, 1);</span>00054 <span class="comment"> work = (left ^ right) & 0xaaaaaaaa;</span>00055 <span class="comment"> left ^= work;</span>00056 <span class="comment"> right ^= work;</span>00057 <span class="comment"> left = rotr(left, 1);</span>00058 <span class="comment"> work = ((left >> 8) ^ right) & 0xff00ff;</span>00059 <span class="comment"> right ^= work;</span>00060 <span class="comment"> left ^= work << 8;</span>00061 <span class="comment"> work = ((left >> 2) ^ right) & 0x33333333;</span>00062 <span class="comment"> right ^= work;</span>00063 <span class="comment"> left ^= work << 2;</span>00064 <span class="comment"> work = ((right >> 16) ^ left) & 0xffff;</span>00065 <span class="comment"> left ^= work;</span>00066 <span class="comment"> right ^= work << 16;</span>00067 <span class="comment"> work = ((right >> 4) ^ left) & 0x0f0f0f0f;</span>00068 <span class="comment"> left ^= work;</span>00069 <span class="comment"> right ^= work << 4;</span>00070 <span class="comment">}</span>00071 <span class="comment">*/</span>00072 00073 <span class="comment">// Wei Dai's modification to Richard Outerbridge's initial permutation </span>00074 <span class="comment">// algorithm, this one is faster if you have access to rotate instructions </span>00075 <span class="comment">// (like in MSVC)</span>00076 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> IPERM(word32 &left, word32 &right)00077 {00078 word32 work;00079 00080 right = rotlFixed(right, 4U);00081 work = (left ^ right) & 0xf0f0f0f0;00082 left ^= work;00083 right = rotrFixed(right^work, 20U);00084 work = (left ^ right) & 0xffff0000;00085 left ^= work;00086 right = rotrFixed(right^work, 18U);00087 work = (left ^ right) & 0x33333333;00088 left ^= work;00089 right = rotrFixed(right^work, 6U);00090 work = (left ^ right) & 0x00ff00ff;00091 left ^= work;00092 right = rotlFixed(right^work, 9U);00093 work = (left ^ right) & 0xaaaaaaaa;00094 left = rotlFixed(left^work, 1U);00095 right ^= work;00096 }00097 00098 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">void</span> FPERM(word32 &left, word32 &right)00099 {00100 word32 work;00101 00102 right = rotrFixed(right, 1U);00103 work = (left ^ right) & 0xaaaaaaaa;00104 right ^= work;00105 left = rotrFixed(left^work, 9U);00106 work = (left ^ right) & 0x00ff00ff;00107 right ^= work;00108 left = rotlFixed(left^work, 6U);00109 work = (left ^ right) & 0x33333333;00110 right ^= work;00111 left = rotlFixed(left^work, 18U);00112 work = (left ^ right) & 0xffff0000;00113 right ^= work;00114 left = rotlFixed(left^work, 20U);00115 work = (left ^ right) & 0xf0f0f0f0;00116 right ^= work;00117 left = rotrFixed(left^work, 4U);00118 }00119 00120 <span class="preprocessor">#ifndef CRYPTOPP_IMPORTS</span>00121 <span class="preprocessor"></span>00122 <span class="comment">/* Tables defined in the Data Encryption Standard documents</span>00123 <span class="comment"> * Three of these tables, the initial permutation, the final</span>00124 <span class="comment"> * permutation and the expansion operator, are regular enough that</span>00125 <span class="comment"> * for speed, we hard-code them. They're here for reference only.</span>00126 <span class="comment"> * Also, the S and P boxes are used by a separate program, gensp.c,</span>00127 <span class="comment"> * to build the combined SP box, Spbox[]. They're also here just</span>00128 <span class="comment"> * for reference.</span>00129 <span class="comment"> */</span>00130 <span class="preprocessor">#ifdef notdef</span>00131 <span class="preprocessor"></span><span class="comment">/* initial permutation IP */</span>00132 <span class="keyword">static</span> byte ip[] = {00133 58, 50, 42, 34, 26, 18, 10, 2,00134 60, 52, 44, 36, 28, 20, 12, 4,00135 62, 54, 46, 38, 30, 22, 14, 6,00136 64, 56, 48, 40, 32, 24, 16, 8,00137 57, 49, 41, 33, 25, 17, 9, 1,00138 59, 51, 43, 35, 27, 19, 11, 3,00139 61, 53, 45, 37, 29, 21, 13, 5,00140 63, 55, 47, 39, 31, 23, 15, 700141 };00142 00143 <span class="comment">/* final permutation IP^-1 */</span>00144 <span class="keyword">static</span> byte fp[] = {00145 40, 8, 48, 16, 56, 24, 64, 32,00146 39, 7, 47, 15, 55, 23, 63, 31,00147 38, 6, 46, 14, 54, 22, 62, 30,00148 37, 5, 45, 13, 53, 21, 61, 29,00149 36, 4, 44, 12, 52, 20, 60, 28,00150 35, 3, 43, 11, 51, 19, 59, 27,00151 34, 2, 42, 10, 50, 18, 58, 26,00152 33, 1, 41, 9, 49, 17, 57, 2500153 };00154 <span class="comment">/* expansion operation matrix */</span>00155 <span class="keyword">static</span> byte ei[] = {00156 32, 1, 2, 3, 4, 5,00157 4, 5, 6, 7, 8, 9,00158 8, 9, 10, 11, 12, 13,00159 12, 13, 14, 15, 16, 17,00160 16, 17, 18, 19, 20, 21,00161 20, 21, 22, 23, 24, 25,00162 24, 25, 26, 27, 28, 29,00163 28, 29, 30, 31, 32, 100164 };00165 <span class="comment">/* The (in)famous S-boxes */</span>00166 <span class="keyword">static</span> byte sbox[8][64] = {00167 <span class="comment">/* S1 */</span>00168 14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,00169 0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,00170 4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,00171 15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,00172 00173 <span class="comment">/* S2 */</span>00174 15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,00175 3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,00176 0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,00177 13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,00178 00179 <span class="comment">/* S3 */</span>00180 10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,00181 13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,00182 13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,00183 1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,00184 00185 <span class="comment">/* S4 */</span>00186 7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,00187 13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,00188 10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,00189 3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,00190 00191 <span class="comment">/* S5 */</span>00192 2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,00193 14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,00194 4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,00195 11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,00196 00197 <span class="comment">/* S6 */</span>00198 12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,00199 10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,00200 9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,00201 4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,00202 00203 <span class="comment">/* S7 */</span>00204 4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,00205 13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,00206 1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,00207 6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,00208 00209 <span class="comment">/* S8 */</span>00210 13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,00211 1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,00212 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,00213 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 1100214 };00215 00216 <span class="comment">/* 32-bit permutation function P used on the output of the S-boxes */</span>00217 <span class="keyword">static</span> byte p32i[] = {00218 16, 7, 20, 21,00219 29, 12, 28, 17,00220 1, 15, 23, 26,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -