📄 des_src.txt
字号:
00222 2, 8, 24, 14,00223 32, 27, 3, 9,00224 19, 13, 30, 6,00225 22, 11, 4, 2500226 };00227 #endif00228 00229 /* permuted choice table (key) */00230 static const 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 /* number left rotations of pc1 */00243 static const byte totrot[] = {00244 1,2,4,6,8,10,12,14,15,17,19,21,23,25,27,2800245 };00246 00247 /* permuted choice key (table) */00248 static const 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 /* End of DES-defined tables */00260 00261 /* bit 0 is left-most in byte */00262 static const int bytebit[] = {00263 0200,0100,040,020,010,04,02,0100264 };00265 00266 /* Set key (initialize key schedule array) */00267 void RawDES::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)00268 {00269 SecByteBlock buffer(56+56+8);00270 byte *const pc1m=buffer; /* place to modify pc1 into */00271 byte *const pcr=pc1m+56; /* place to rotate pc1 into */00272 byte *const ks=pcr+56;00273 register int i,j,l;00274 int m;00275 00276 for (j=0; j<56; j++) { /* convert pc1 to bits of key */00277 l=pc1[j]-1; /* integer bit location */00278 m = l & 07; /* find bit */00279 pc1m[j]=(key[l>>3] & /* find which key byte l is in */00280 bytebit[m]) /* and which bit of that byte */00281 ? 1 : 0; /* and store 1-bit result */00282 }00283 for (i=0; i<16; i++) { /* key chunk for each iteration */00284 memset(ks,0,8); /* Clear key schedule */00285 for (j=0; j<56; j++) /* rotate pc1 the right amount */00286 pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];00287 /* rotate left and right halves independently */00288 for (j=0; j<48; j++){ /* select bits individually */00289 /* check bit that goes to ks[j] */00290 if (pcr[pc2[j]-1]){00291 /* mask it in if it's there */00292 l= j % 6;00293 ks[j/6] |= bytebit[l] >> 2;00294 }00295 }00296 /* Now convert to odd/even interleaved form for use in F */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 if (dir==DECRYPTION) // reverse key schedule order00308 for (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 void RawDES::RawProcessBlock(word32 &l_, word32 &r_) const00316 {00317 word32 l = l_, r = r_;00318 const word32 *kptr=k;00319 00320 for (unsigned 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 void DES_EDE2::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int length)00349 {00350 AssertValidKeyLength(length);00351 00352 m_des1.UncheckedSetKey(dir, key);00353 m_des2.UncheckedSetKey(ReverseCipherDir(dir), key+8);00354 }00355 00356 void DES_EDE2::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const00357 {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 void DES_EDE3::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int 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 void DES_EDE3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const00378 {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 #endif // #ifndef CRYPTOPP_IMPORTS00390 00391 static inline bool CheckParity(byte b)00392 {00393 unsigned int a = b ^ (b >> 4);00394 return ((a ^ (a>>1) ^ (a>>2) ^ (a>>3)) & 1) == 1;00395 }00396 00397 bool DES::CheckKeyParityBits(const byte *key)00398 {00399 for (unsigned int i=0; i<8; i++)00400 if (!CheckParity(key[i]))00401 return false;00402 return true;00403 }00404 00405 void DES::CorrectKeyParityBits(byte *key)00406 {00407 for (unsigned int i=0; i<8; i++)00408 if (!CheckParity(key[i]))00409 key[i] ^= 1;00410 }00411 00412 // Encrypt or decrypt a block of data in ECB mode00413 void DES::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const00414 {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 void DES_XEX3::Base::UncheckedSetKey(CipherDir dir, const byte *key, unsigned int 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 void DES_XEX3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const00433 {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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -