Path: blob/a-new-beginning/SharedDependencies/Sources/cryptopp/des.cpp
2 views
// des.cpp - modified by Wei Dai from Phil Karn's des.c1// The original code and all modifications are in the public domain.23/*4* This is a major rewrite of my old public domain DES code written5* circa 1987, which in turn borrowed heavily from Jim Gillogly's 19776* public domain code. I pretty much kept my key scheduling code, but7* the actual encrypt/decrypt routines are taken from from Richard8* Outerbridge's DES code as printed in Schneier's "Applied Cryptography."9*10* This code is in the public domain. I would appreciate bug reports and11* enhancements.12*13* Phil Karn KA9Q, [email protected], August 1994.14*/1516#include "pch.h"17#include "misc.h"18#include "des.h"1920NAMESPACE_BEGIN(CryptoPP)2122typedef BlockGetAndPut<word32, BigEndian> Block;2324// Richard Outerbridge's initial permutation algorithm25/*26inline void IPERM(word32 &left, word32 &right)27{28word32 work;2930work = ((left >> 4) ^ right) & 0x0f0f0f0f;31right ^= work;32left ^= work << 4;33work = ((left >> 16) ^ right) & 0xffff;34right ^= work;35left ^= work << 16;36work = ((right >> 2) ^ left) & 0x33333333;37left ^= work;38right ^= (work << 2);39work = ((right >> 8) ^ left) & 0xff00ff;40left ^= work;41right ^= (work << 8);42right = rotl(right, 1);43work = (left ^ right) & 0xaaaaaaaa;44left ^= work;45right ^= work;46left = rotl(left, 1);47}48inline void FPERM(word32 &left, word32 &right)49{50word32 work;5152right = rotr(right, 1);53work = (left ^ right) & 0xaaaaaaaa;54left ^= work;55right ^= work;56left = rotr(left, 1);57work = ((left >> 8) ^ right) & 0xff00ff;58right ^= work;59left ^= work << 8;60work = ((left >> 2) ^ right) & 0x33333333;61right ^= work;62left ^= work << 2;63work = ((right >> 16) ^ left) & 0xffff;64left ^= work;65right ^= work << 16;66work = ((right >> 4) ^ left) & 0x0f0f0f0f;67left ^= work;68right ^= work << 4;69}70*/7172// Wei Dai's modification to Richard Outerbridge's initial permutation73// algorithm, this one is faster if you have access to rotate instructions74// (like in MSVC)75static inline void IPERM(word32 &left, word32 &right)76{77word32 work;7879right = rotlConstant<4>(right);80work = (left ^ right) & 0xf0f0f0f0;81left ^= work;82right = rotrConstant<20>(right^work);83work = (left ^ right) & 0xffff0000;84left ^= work;85right = rotrConstant<18>(right^work);86work = (left ^ right) & 0x33333333;87left ^= work;88right = rotrConstant<6>(right^work);89work = (left ^ right) & 0x00ff00ff;90left ^= work;91right = rotlConstant<9>(right^work);92work = (left ^ right) & 0xaaaaaaaa;93left = rotlConstant<1>(left^work);94right ^= work;95}9697static inline void FPERM(word32 &left, word32 &right)98{99word32 work;100101right = rotrConstant<1>(right);102work = (left ^ right) & 0xaaaaaaaa;103right ^= work;104left = rotrConstant<9>(left^work);105work = (left ^ right) & 0x00ff00ff;106right ^= work;107left = rotlConstant<6>(left^work);108work = (left ^ right) & 0x33333333;109right ^= work;110left = rotlConstant<18>(left^work);111work = (left ^ right) & 0xffff0000;112right ^= work;113left = rotlConstant<20>(left^work);114work = (left ^ right) & 0xf0f0f0f0;115right ^= work;116left = rotrConstant<4>(left^work);117}118119void DES::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)120{121AssertValidKeyLength(length);122123RawSetKey(GetCipherDirection(), userKey);124}125126#ifndef CRYPTOPP_IMPORTS127128/* Tables defined in the Data Encryption Standard documents129* Three of these tables, the initial permutation, the final130* permutation and the expansion operator, are regular enough that131* for speed, we hard-code them. They're here for reference only.132* Also, the S and P boxes are used by a separate program, gensp.c,133* to build the combined SP box, Spbox[]. They're also here just134* for reference.135*/136#ifdef notdef137/* initial permutation IP */138static byte ip[] = {13958, 50, 42, 34, 26, 18, 10, 2,14060, 52, 44, 36, 28, 20, 12, 4,14162, 54, 46, 38, 30, 22, 14, 6,14264, 56, 48, 40, 32, 24, 16, 8,14357, 49, 41, 33, 25, 17, 9, 1,14459, 51, 43, 35, 27, 19, 11, 3,14561, 53, 45, 37, 29, 21, 13, 5,14663, 55, 47, 39, 31, 23, 15, 7147};148149/* final permutation IP^-1 */150static byte fp[] = {15140, 8, 48, 16, 56, 24, 64, 32,15239, 7, 47, 15, 55, 23, 63, 31,15338, 6, 46, 14, 54, 22, 62, 30,15437, 5, 45, 13, 53, 21, 61, 29,15536, 4, 44, 12, 52, 20, 60, 28,15635, 3, 43, 11, 51, 19, 59, 27,15734, 2, 42, 10, 50, 18, 58, 26,15833, 1, 41, 9, 49, 17, 57, 25159};160/* expansion operation matrix */161static byte ei[] = {16232, 1, 2, 3, 4, 5,1634, 5, 6, 7, 8, 9,1648, 9, 10, 11, 12, 13,16512, 13, 14, 15, 16, 17,16616, 17, 18, 19, 20, 21,16720, 21, 22, 23, 24, 25,16824, 25, 26, 27, 28, 29,16928, 29, 30, 31, 32, 1170};171/* The (in)famous S-boxes */172static byte sbox[8][64] = {173/* S1 */17414, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7,1750, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8,1764, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0,17715, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13,178179/* S2 */18015, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10,1813, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5,1820, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15,18313, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9,184185/* S3 */18610, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8,18713, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1,18813, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7,1891, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12,190191/* S4 */1927, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15,19313, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9,19410, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4,1953, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14,196197/* S5 */1982, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9,19914, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6,2004, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14,20111, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3,202203/* S6 */20412, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11,20510, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8,2069, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6,2074, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13,208209/* S7 */2104, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1,21113, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6,2121, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2,2136, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12,214215/* S8 */21613, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7,2171, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2,2187, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8,2192, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11220};221222/* 32-bit permutation function P used on the output of the S-boxes */223namespace {224const byte p32i[] = {22516, 7, 20, 21,22629, 12, 28, 17,2271, 15, 23, 26,2285, 18, 31, 10,2292, 8, 24, 14,23032, 27, 3, 9,23119, 13, 30, 6,23222, 11, 4, 25233};234}235#endif236237/* permuted choice table (key) */238namespace {239const byte pc1[] = {24057, 49, 41, 33, 25, 17, 9,2411, 58, 50, 42, 34, 26, 18,24210, 2, 59, 51, 43, 35, 27,24319, 11, 3, 60, 52, 44, 36,24424563, 55, 47, 39, 31, 23, 15,2467, 62, 54, 46, 38, 30, 22,24714, 6, 61, 53, 45, 37, 29,24821, 13, 5, 28, 20, 12, 4249};250}251252/* number left rotations of pc1 */253namespace {254const byte totrot[] = {2551,2,4,6,8,10,12,14,15,17,19,21,23,25,27,28256};257}258259/* permuted choice key (table) */260namespace {261const byte pc2[] = {26214, 17, 11, 24, 1, 5,2633, 28, 15, 6, 21, 10,26423, 19, 12, 4, 26, 8,26516, 7, 27, 20, 13, 2,26641, 52, 31, 37, 47, 55,26730, 40, 51, 45, 33, 48,26844, 49, 39, 56, 34, 53,26946, 42, 50, 36, 29, 32270};271}272273/* End of DES-defined tables */274275/* bit 0 is left-most in byte */276namespace {277const int bytebit[] = {2780200,0100,040,020,010,04,02,01279};280}281282/* Set key (initialize key schedule array) */283void RawDES::RawSetKey(CipherDir dir, const byte *key)284{285#if (CRYPTOPP_MSC_VERSION >= 1600) || (__cplusplus >= 201103L)286# define REGISTER /* Define to nothing for C++11 and above */287#else288# define REGISTER register289#endif290291SecByteBlock buffer(56+56+8);292byte *const pc1m=buffer; /* place to modify pc1 into */293byte *const pcr=pc1m+56; /* place to rotate pc1 into */294byte *const ks=pcr+56;295REGISTER int i,j,l;296int m;297298for (j=0; j<56; j++) { /* convert pc1 to bits of key */299l=pc1[j]-1; /* integer bit location */300m = l & 07; /* find bit */301pc1m[j]=(key[l>>3] & /* find which key byte l is in */302bytebit[m]) /* and which bit of that byte */303? 1 : 0; /* and store 1-bit result */304}305for (i=0; i<16; i++) { /* key chunk for each iteration */306std::memset(ks,0,8); /* Clear key schedule */307for (j=0; j<56; j++) /* rotate pc1 the right amount */308pcr[j] = pc1m[(l=j+totrot[i])<(j<28? 28 : 56) ? l: l-28];309/* rotate left and right halves independently */310for (j=0; j<48; j++){ /* select bits individually */311/* check bit that goes to ks[j] */312if (pcr[pc2[j]-1]){313/* mask it in if it's there */314l= j % 6;315ks[j/6] |= bytebit[l] >> 2;316}317}318/* Now convert to odd/even interleaved form for use in F */319k[2*i] = ((word32)ks[0] << 24)320| ((word32)ks[2] << 16)321| ((word32)ks[4] << 8)322| ((word32)ks[6]);323k[2*i+1] = ((word32)ks[1] << 24)324| ((word32)ks[3] << 16)325| ((word32)ks[5] << 8)326| ((word32)ks[7]);327}328329if (dir==DECRYPTION) // reverse key schedule order330for (i=0; i<16; i+=2)331{332std::swap(k[i], k[32-2-i]);333std::swap(k[i+1], k[32-1-i]);334}335}336337void RawDES::RawProcessBlock(word32 &l_, word32 &r_) const338{339word32 l = l_, r = r_;340const word32 *kptr=k;341342for (unsigned i=0; i<8; i++)343{344word32 work = rotrConstant<4>(r) ^ kptr[4 * i + 0];345l ^= Spbox[6][(work) & 0x3f]346^ Spbox[4][(work >> 8) & 0x3f]347^ Spbox[2][(work >> 16) & 0x3f]348^ Spbox[0][(work >> 24) & 0x3f];349work = r ^ kptr[4*i+1];350l ^= Spbox[7][(work) & 0x3f]351^ Spbox[5][(work >> 8) & 0x3f]352^ Spbox[3][(work >> 16) & 0x3f]353^ Spbox[1][(work >> 24) & 0x3f];354355work = rotrConstant<4>(l) ^ kptr[4 * i + 2];356r ^= Spbox[6][(work) & 0x3f]357^ Spbox[4][(work >> 8) & 0x3f]358^ Spbox[2][(work >> 16) & 0x3f]359^ Spbox[0][(work >> 24) & 0x3f];360work = l ^ kptr[4*i+3];361r ^= Spbox[7][(work) & 0x3f]362^ Spbox[5][(work >> 8) & 0x3f]363^ Spbox[3][(work >> 16) & 0x3f]364^ Spbox[1][(work >> 24) & 0x3f];365}366367l_ = l; r_ = r;368}369370void DES_EDE2::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)371{372AssertValidKeyLength(length);373374m_des1.RawSetKey(GetCipherDirection(), userKey);375m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey+8);376}377378void DES_EDE2::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const379{380word32 l,r;381Block::Get(inBlock)(l)(r);382IPERM(l,r);383m_des1.RawProcessBlock(l, r);384m_des2.RawProcessBlock(r, l);385m_des1.RawProcessBlock(l, r);386FPERM(l,r);387Block::Put(xorBlock, outBlock)(r)(l);388}389390void DES_EDE3::Base::UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &)391{392AssertValidKeyLength(length);393394m_des1.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 0 : 16));395m_des2.RawSetKey(ReverseCipherDir(GetCipherDirection()), userKey + 8);396m_des3.RawSetKey(GetCipherDirection(), userKey + (IsForwardTransformation() ? 16 : 0));397}398399void DES_EDE3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const400{401word32 l,r;402Block::Get(inBlock)(l)(r);403IPERM(l,r);404m_des1.RawProcessBlock(l, r);405m_des2.RawProcessBlock(r, l);406m_des3.RawProcessBlock(l, r);407FPERM(l,r);408Block::Put(xorBlock, outBlock)(r)(l);409}410411#endif // #ifndef CRYPTOPP_IMPORTS412413static inline bool CheckParity(byte b)414{415unsigned int a = b ^ (b >> 4);416return ((a ^ (a>>1) ^ (a>>2) ^ (a>>3)) & 1) == 1;417}418419bool DES::CheckKeyParityBits(const byte *key)420{421for (unsigned int i=0; i<8; i++)422if (!CheckParity(key[i]))423return false;424return true;425}426427void DES::CorrectKeyParityBits(byte *key)428{429for (unsigned int i=0; i<8; i++)430if (!CheckParity(key[i]))431key[i] ^= 1;432}433434// Encrypt or decrypt a block of data in ECB mode435void DES::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const436{437word32 l,r;438Block::Get(inBlock)(l)(r);439IPERM(l,r);440RawProcessBlock(l, r);441FPERM(l,r);442Block::Put(xorBlock, outBlock)(r)(l);443}444445void DES_XEX3::Base::UncheckedSetKey(const byte *key, unsigned int length, const NameValuePairs &)446{447AssertValidKeyLength(length);448449if (!m_des.get())450m_des.reset(new DES::Encryption);451452std::memcpy(m_x1, key + (IsForwardTransformation() ? 0 : 16), BLOCKSIZE);453m_des->RawSetKey(GetCipherDirection(), key + 8);454std::memcpy(m_x3, key + (IsForwardTransformation() ? 16 : 0), BLOCKSIZE);455}456457void DES_XEX3::Base::ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const458{459xorbuf(outBlock, inBlock, m_x1, BLOCKSIZE);460m_des->ProcessAndXorBlock(outBlock, xorBlock, outBlock);461xorbuf(outBlock, m_x3, BLOCKSIZE);462}463464NAMESPACE_END465466467