📄 des_8cpp-source.html
字号:
00221 5, 18, 31, 10,00222 2, 8, 24, 14,00223 32, 27, 3, 9,00224 19, 13, 30, 6,00225 22, 11, 4, 2500226 };00227 <span class="preprocessor">#endif</span>00228 <span class="preprocessor"></span>00229 <span class="comment">/* permuted choice table (key) */</span>00230 <span class="keyword">static</span> <span class="keyword">const</span> byte pc1[] = {00231 57, 49, 41, 33, 25, 17, 9,00232 1, 58, 50, 42, 34, 26, 18,00233 10, 2, 59, 51, 43, 35, 27,00234 19, 11, 3, 60, 52, 44, 36,00235 00236 63, 55, 47, 39, 31, 23, 15,00237 7, 62, 54, 46, 38, 30, 22,00238 14, 6, 61, 53, 45, 37, 29,00239 21, 13, 5, 28, 20, 12, 400240 };00241 00242 <span class="comment">/* number left rotations of pc1 */</span>00243 <span class="keyword">static</span> <span class="keyword">const</span> byte totrot[] = {00244 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,2800245 };00246 00247 <span class="comment">/* permuted choice key (table) */</span>00248 <span class="keyword">static</span> <span class="keyword">const</span> byte pc2[] = {00249 14, 17, 11, 24, 1, 5,00250 3, 28, 15, 6, 21, 10,00251 23, 19, 12, 4, 26, 8,00252 16, 7, 27, 20, 13, 2,00253 41, 52, 31, 37, 47, 55,00254 30, 40, 51, 45, 33, 48,00255 44, 49, 39, 56, 34, 53,00256 46, 42, 50, 36, 29, 3200257 };00258 00259 <span class="comment">/* End of DES-defined tables */</span>00260 00261 <span class="comment">/* bit 0 is left-most in byte */</span>00262 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">int</span> bytebit[] = {00263 0200,0100,040,020,010,04,02,0100264 };00265 00266 <span class="comment">/* Set key (initialize key schedule array) */</span>00267 <span class="keywordtype">void</span> RawDES::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00268 {00269 <a class="code" href="class_sec_block.html">SecByteBlock</a> buffer(56+56+8);00270 byte *<span class="keyword">const</span> pc1m=buffer; <span class="comment">/* place to modify pc1 into */</span>00271 byte *<span class="keyword">const</span> pcr=pc1m+56; <span class="comment">/* place to rotate pc1 into */</span>00272 byte *<span class="keyword">const</span> ks=pcr+56;00273 <span class="keyword">register</span> <span class="keywordtype">int</span> i,j,l;00274 <span class="keywordtype">int</span> m;00275 00276 <span class="keywordflow">for</span> (j=0; j<56; j++) { <span class="comment">/* convert pc1 to bits of key */</span>00277 l=pc1[j]-1; <span class="comment">/* integer bit location */</span>00278 m = l & 07; <span class="comment">/* find bit */</span>00279 pc1m[j]=(key[l>>3] & <span class="comment">/* find which key byte l is in */</span>00280 bytebit[m]) <span class="comment">/* and which bit of that byte */</span>00281 ? 1 : 0; <span class="comment">/* and store 1-bit result */</span>00282 }00283 <span class="keywordflow">for</span> (i=0; i<16; i++) { <span class="comment">/* key chunk for each iteration */</span>00284 memset(ks,0,8); <span class="comment">/* Clear key schedule */</span>00285 <span class="keywordflow">for</span> (j=0; j<56; j++) <span class="comment">/* rotate pc1 the right amount */</span>00286 pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];00287 <span class="comment">/* rotate left and right halves independently */</span>00288 <span class="keywordflow">for</span> (j=0; j<48; j++){ <span class="comment">/* select bits individually */</span>00289 <span class="comment">/* check bit that goes to ks[j] */</span>00290 <span class="keywordflow">if</span> (pcr[pc2[j]-1]){00291 <span class="comment">/* mask it in if it's there */</span>00292 l= j % 6;00293 ks[j/6] |= bytebit[l] >> 2;00294 }00295 }00296 <span class="comment">/* Now convert to odd/even interleaved form for use in F */</span>00297 k[2*i] = ((word32)ks[0] << 24)00298 | ((word32)ks[2] << 16)00299 | ((word32)ks[4] << 8)00300 | ((word32)ks[6]);00301 k[2*i+1] = ((word32)ks[1] << 24)00302 | ((word32)ks[3] << 16)00303 | ((word32)ks[5] << 8)00304 | ((word32)ks[7]);00305 }00306 00307 <span class="keywordflow">if</span> (dir==DECRYPTION) <span class="comment">// reverse key schedule order</span>00308 <span class="keywordflow">for</span> (i=0; i<16; i+=2)00309 {00310 std::swap(k[i], k[32-2-i]);00311 std::swap(k[i+1], k[32-1-i]);00312 }00313 }00314 00315 <span class="keywordtype">void</span> RawDES::RawProcessBlock(word32 &l_, word32 &r_)<span class="keyword"> const</span>00316 <span class="keyword"></span>{00317 word32 l = l_, r = r_;00318 <span class="keyword">const</span> word32 *kptr=k;00319 00320 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> i=0; i<8; i++)00321 {00322 word32 work = rotrFixed(r, 4U) ^ kptr[4*i+0];00323 l ^= Spbox[6][(work) & 0x3f]00324 ^ Spbox[4][(work >> 8) & 0x3f]00325 ^ Spbox[2][(work >> 16) & 0x3f]00326 ^ Spbox[0][(work >> 24) & 0x3f];00327 work = r ^ kptr[4*i+1];00328 l ^= Spbox[7][(work) & 0x3f]00329 ^ Spbox[5][(work >> 8) & 0x3f]00330 ^ Spbox[3][(work >> 16) & 0x3f]00331 ^ Spbox[1][(work >> 24) & 0x3f];00332 00333 work = rotrFixed(l, 4U) ^ kptr[4*i+2];00334 r ^= Spbox[6][(work) & 0x3f]00335 ^ Spbox[4][(work >> 8) & 0x3f]00336 ^ Spbox[2][(work >> 16) & 0x3f]00337 ^ Spbox[0][(work >> 24) & 0x3f];00338 work = l ^ kptr[4*i+3];00339 r ^= Spbox[7][(work) & 0x3f]00340 ^ Spbox[5][(work >> 8) & 0x3f]00341 ^ Spbox[3][(work >> 16) & 0x3f]00342 ^ Spbox[1][(work >> 24) & 0x3f];00343 }00344 00345 l_ = l; r_ = r;00346 }00347 00348 <span class="keywordtype">void</span> DES_EDE2::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00349 {00350 AssertValidKeyLength(length);00351 00352 m_des1.UncheckedSetKey(dir, key);00353 m_des2.UncheckedSetKey(ReverseCipherDir(dir), key+8);00354 }00355 00356 <span class="keywordtype">void</span> DES_EDE2::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00357 <span class="keyword"></span>{00358 word32 l,r;00359 Block::Get(inBlock)(l)(r);00360 IPERM(l,r);00361 m_des1.RawProcessBlock(l, r);00362 m_des2.RawProcessBlock(r, l);00363 m_des1.RawProcessBlock(l, r);00364 FPERM(l,r);00365 Block::Put(xorBlock, outBlock)(r)(l);00366 }00367 00368 <span class="keywordtype">void</span> DES_EDE3::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00369 {00370 AssertValidKeyLength(length);00371 00372 m_des1.UncheckedSetKey(dir, key+(dir==ENCRYPTION?0:2*8));00373 m_des2.UncheckedSetKey(ReverseCipherDir(dir), key+8);00374 m_des3.UncheckedSetKey(dir, key+(dir==DECRYPTION?0:2*8));00375 }00376 00377 <span class="keywordtype">void</span> DES_EDE3::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00378 <span class="keyword"></span>{00379 word32 l,r;00380 Block::Get(inBlock)(l)(r);00381 IPERM(l,r);00382 m_des1.RawProcessBlock(l, r);00383 m_des2.RawProcessBlock(r, l);00384 m_des3.RawProcessBlock(l, r);00385 FPERM(l,r);00386 Block::Put(xorBlock, outBlock)(r)(l);00387 }00388 00389 <span class="preprocessor">#endif // #ifndef CRYPTOPP_IMPORTS</span>00390 <span class="preprocessor"></span>00391 <span class="keyword">static</span> <span class="keyword">inline</span> <span class="keywordtype">bool</span> CheckParity(byte b)00392 {00393 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> a = b ^ (b >> 4);00394 <span class="keywordflow">return</span> ((a ^ (a>>1) ^ (a>>2) ^ (a>>3)) & 1) == 1;00395 }00396 <a name="l00397"></a><a class="code" href="class_d_e_s.html#_d_e_se0">00397</a> <span class="keywordtype">bool</span> <a class="code" href="class_d_e_s.html#_d_e_se0">DES::CheckKeyParityBits</a>(<span class="keyword">const</span> byte *key)00398 {00399 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<8; i++)00400 <span class="keywordflow">if</span> (!CheckParity(key[i]))00401 <span class="keywordflow">return</span> <span class="keyword">false</span>;00402 <span class="keywordflow">return</span> <span class="keyword">true</span>;00403 }00404 <a name="l00405"></a><a class="code" href="class_d_e_s.html#_d_e_se1">00405</a> <span class="keywordtype">void</span> <a class="code" href="class_d_e_s.html#_d_e_se1">DES::CorrectKeyParityBits</a>(byte *key)00406 {00407 <span class="keywordflow">for</span> (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<8; i++)00408 <span class="keywordflow">if</span> (!CheckParity(key[i]))00409 key[i] ^= 1;00410 }00411 00412 <span class="comment">// Encrypt or decrypt a block of data in ECB mode</span>00413 <span class="keywordtype">void</span> DES::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00414 <span class="keyword"></span>{00415 word32 l,r;00416 Block::Get(inBlock)(l)(r);00417 IPERM(l,r);00418 RawProcessBlock(l, r);00419 FPERM(l,r);00420 Block::Put(xorBlock, outBlock)(r)(l);00421 }00422 00423 <span class="keywordtype">void</span> DES_XEX3::Base::UncheckedSetKey(CipherDir dir, <span class="keyword">const</span> byte *key, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> length)00424 {00425 AssertValidKeyLength(length);00426 00427 memcpy(m_x1, key+(dir==ENCRYPTION?0:2*8), BLOCKSIZE);00428 m_des.UncheckedSetKey(dir, key+8);00429 memcpy(m_x3, key+(dir==DECRYPTION?0:2*8), BLOCKSIZE);00430 }00431 00432 <span class="keywordtype">void</span> DES_XEX3::Base::ProcessAndXorBlock(<span class="keyword">const</span> byte *inBlock, <span class="keyword">const</span> byte *xorBlock, byte *outBlock)<span class="keyword"> const</span>00433 <span class="keyword"></span>{00434 xorbuf(outBlock, inBlock, m_x1, BLOCKSIZE);00435 m_des.ProcessAndXorBlock(outBlock, xorBlock, outBlock);00436 xorbuf(outBlock, m_x3, BLOCKSIZE);00437 }00438 00439 NAMESPACE_END</pre></div><hr size="1"><address style="align: right;"><small>Generated on Tue Jul 8 23:34:11 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 + -