00001 #ifndef CRYPTOPP_DES_H
00002 #define CRYPTOPP_DES_H
00003
00007 #include "cryptlib.h"
00008 #include "misc.h"
00009
00010 NAMESPACE_BEGIN(CryptoPP)
00011
00012
00013
00014
00015
00016 bool DES_CheckKeyParityBits(const byte *key);
00017 void DES_CorrectKeyParityBits(byte *key);
00018
00020 class DES : public FixedBlockSize<8>, public FixedKeyLength<8>
00021 {
00022 public:
00023 DES(const byte *userKey, CipherDir);
00024
00025 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00026 void ProcessBlock(byte * inoutBlock) const
00027 {DES::ProcessBlock(inoutBlock, inoutBlock);}
00028
00029
00030 void RawProcessBlock(word32 &l, word32 &r) const;
00031
00032 protected:
00033 static const word32 Spbox[8][64];
00034
00035 SecBlock<word32> k;
00036 };
00037
00039 class DESEncryption : public DES
00040 {
00041 public:
00042 DESEncryption(const byte * userKey, unsigned int = 0)
00043 : DES (userKey, ENCRYPTION) {}
00044 };
00045
00047 class DESDecryption : public DES
00048 {
00049 public:
00050 DESDecryption(const byte * userKey, unsigned int = 0)
00051 : DES (userKey, DECRYPTION) {}
00052 };
00053
00055 class DES_EDE2_Encryption : public FixedBlockSize<8>, public FixedKeyLength<16>
00056 {
00057 public:
00058 DES_EDE2_Encryption(const byte * userKey, unsigned int = 0)
00059 : e(userKey, ENCRYPTION), d(userKey + DES::KEYLENGTH, DECRYPTION) {}
00060
00061 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00062 void ProcessBlock(byte * inoutBlock) const
00063 {DES_EDE2_Encryption::ProcessBlock(inoutBlock, inoutBlock);}
00064
00065 private:
00066 DES e, d;
00067 };
00068
00070 class DES_EDE2_Decryption : public FixedBlockSize<8>, public FixedKeyLength<16>
00071 {
00072 public:
00073 DES_EDE2_Decryption(const byte * userKey, unsigned int = 0)
00074 : d(userKey, DECRYPTION), e(userKey + DES::KEYLENGTH, ENCRYPTION) {}
00075
00076 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00077 void ProcessBlock(byte * inoutBlock) const
00078 {DES_EDE2_Decryption::ProcessBlock(inoutBlock, inoutBlock);}
00079
00080 private:
00081 DES d, e;
00082 };
00083
00085 class DES_EDE3_Encryption : public FixedBlockSize<8>, public FixedKeyLength<24>
00086 {
00087 public:
00088 DES_EDE3_Encryption(const byte * userKey, unsigned int = 0)
00089 : e1(userKey, ENCRYPTION), d2(userKey + DES::KEYLENGTH, DECRYPTION),
00090 e3(userKey + 2*DES::KEYLENGTH, ENCRYPTION) {}
00091
00092 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00093 void ProcessBlock(byte * inoutBlock) const
00094 {DES_EDE3_Encryption::ProcessBlock(inoutBlock, inoutBlock);}
00095
00096 private:
00097 DES e1, d2, e3;
00098 };
00099
00101 class DES_EDE3_Decryption : public FixedBlockSize<8>, public FixedKeyLength<24>
00102 {
00103 public:
00104 DES_EDE3_Decryption(const byte * userKey, unsigned int = 0)
00105 : d1(userKey, DECRYPTION), e2(userKey + DES::KEYLENGTH, ENCRYPTION),
00106 d3(userKey + 2*DES::KEYLENGTH, DECRYPTION) {}
00107
00108 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00109 void ProcessBlock(byte * inoutBlock) const
00110 {DES_EDE3_Decryption::ProcessBlock(inoutBlock, inoutBlock);}
00111
00112 private:
00113 DES d1, e2, d3;
00114 };
00115
00117 class DES_XEX3_Encryption : public FixedBlockSize<8>, public FixedKeyLength<24>
00118 {
00119 public:
00120 DES_XEX3_Encryption(const byte * userKey, unsigned int = 0)
00121 : x1(userKey, 8), e2(userKey + 8, ENCRYPTION), x3(userKey + 16, 8) {}
00122
00123 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00124 void ProcessBlock(byte * inoutBlock) const
00125 {DES_XEX3_Encryption::ProcessBlock(inoutBlock, inoutBlock);}
00126
00127 private:
00128 SecByteBlock x1;
00129 DES e2;
00130 SecByteBlock x3;
00131 };
00132
00134 class DES_XEX3_Decryption : public FixedBlockSize<8>, public FixedKeyLength<24>
00135 {
00136 public:
00137 DES_XEX3_Decryption(const byte * userKey, unsigned int = 0)
00138 : x1(userKey, 8), d2(userKey + 8, DECRYPTION), x3(userKey + 16, 8) {}
00139
00140 void ProcessBlock(const byte *inBlock, byte * outBlock) const;
00141 void ProcessBlock(byte * inoutBlock) const
00142 {DES_XEX3_Decryption::ProcessBlock(inoutBlock, inoutBlock);}
00143
00144 private:
00145 SecByteBlock x1;
00146 DES d2;
00147 SecByteBlock x3;
00148 };
00149
00150 NAMESPACE_END
00151
00152 #endif