Crypto++
|
00001 #ifndef CRYPTOPP_DES_H 00002 #define CRYPTOPP_DES_H 00003 00004 /** \file 00005 */ 00006 00007 #include "seckey.h" 00008 #include "secblock.h" 00009 00010 NAMESPACE_BEGIN(CryptoPP) 00011 00012 class CRYPTOPP_DLL RawDES 00013 { 00014 public: 00015 void RawSetKey(CipherDir direction, const byte *userKey); 00016 void RawProcessBlock(word32 &l, word32 &r) const; 00017 00018 protected: 00019 static const word32 Spbox[8][64]; 00020 00021 FixedSizeSecBlock<word32, 32> k; 00022 }; 00023 00024 //! _ 00025 struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8> 00026 { 00027 // disable DES in DLL version by not exporting this function 00028 static const char * StaticAlgorithmName() {return "DES";} 00029 }; 00030 00031 /// <a href="http://www.weidai.com/scan-mirror/cs.html#DES">DES</a> 00032 /*! The DES implementation in Crypto++ ignores the parity bits 00033 (the least significant bits of each byte) in the key. However 00034 you can use CheckKeyParityBits() and CorrectKeyParityBits() to 00035 check or correct the parity bits if you wish. */ 00036 class DES : public DES_Info, public BlockCipherDocumentation 00037 { 00038 class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES 00039 { 00040 public: 00041 void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs ¶ms); 00042 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; 00043 }; 00044 00045 public: 00046 //! check DES key parity bits 00047 static bool CheckKeyParityBits(const byte *key); 00048 //! correct DES key parity bits 00049 static void CorrectKeyParityBits(byte *key); 00050 00051 typedef BlockCipherFinal<ENCRYPTION, Base> Encryption; 00052 typedef BlockCipherFinal<DECRYPTION, Base> Decryption; 00053 }; 00054 00055 //! _ 00056 struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16> 00057 { 00058 CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";} 00059 }; 00060 00061 /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE2</a> 00062 class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation 00063 { 00064 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info> 00065 { 00066 public: 00067 void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs ¶ms); 00068 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; 00069 00070 protected: 00071 RawDES m_des1, m_des2; 00072 }; 00073 00074 public: 00075 typedef BlockCipherFinal<ENCRYPTION, Base> Encryption; 00076 typedef BlockCipherFinal<DECRYPTION, Base> Decryption; 00077 }; 00078 00079 //! _ 00080 struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> 00081 { 00082 CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";} 00083 }; 00084 00085 /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE3</a> 00086 class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation 00087 { 00088 class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info> 00089 { 00090 public: 00091 void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs ¶ms); 00092 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; 00093 00094 protected: 00095 RawDES m_des1, m_des2, m_des3; 00096 }; 00097 00098 public: 00099 typedef BlockCipherFinal<ENCRYPTION, Base> Encryption; 00100 typedef BlockCipherFinal<DECRYPTION, Base> Decryption; 00101 }; 00102 00103 //! _ 00104 struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24> 00105 { 00106 static const char *StaticAlgorithmName() {return "DES-XEX3";} 00107 }; 00108 00109 /// <a href="http://www.weidai.com/scan-mirror/cs.html#DESX">DES-XEX3</a>, AKA DESX 00110 class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation 00111 { 00112 class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info> 00113 { 00114 public: 00115 void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs ¶ms); 00116 void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const; 00117 00118 protected: 00119 FixedSizeSecBlock<byte, BLOCKSIZE> m_x1, m_x3; 00120 // VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock 00121 // if we use DES::Encryption here directly without value_ptr. 00122 value_ptr<DES::Encryption> m_des; 00123 }; 00124 00125 public: 00126 typedef BlockCipherFinal<ENCRYPTION, Base> Encryption; 00127 typedef BlockCipherFinal<DECRYPTION, Base> Decryption; 00128 }; 00129 00130 typedef DES::Encryption DESEncryption; 00131 typedef DES::Decryption DESDecryption; 00132 00133 typedef DES_EDE2::Encryption DES_EDE2_Encryption; 00134 typedef DES_EDE2::Decryption DES_EDE2_Decryption; 00135 00136 typedef DES_EDE3::Encryption DES_EDE3_Encryption; 00137 typedef DES_EDE3::Decryption DES_EDE3_Decryption; 00138 00139 typedef DES_XEX3::Encryption DES_XEX3_Encryption; 00140 typedef DES_XEX3::Decryption DES_XEX3_Decryption; 00141 00142 NAMESPACE_END 00143 00144 #endif