Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:56

0001 // des.h - originally written and placed in the public domain by Wei Dai

0002 
0003 /// \file des.h

0004 /// \brief Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX

0005 
0006 #ifndef CRYPTOPP_DES_H
0007 #define CRYPTOPP_DES_H
0008 
0009 #include "seckey.h"
0010 #include "secblock.h"
0011 
0012 NAMESPACE_BEGIN(CryptoPP)
0013 
0014 /// \brief DES block cipher base class

0015 /// \since Crypto++ 1.0

0016 class CRYPTOPP_DLL RawDES
0017 {
0018 public:
0019     void RawSetKey(CipherDir direction, const byte *userKey);
0020     void RawProcessBlock(word32 &l, word32 &r) const;
0021 
0022 protected:
0023     static const word32 Spbox[8][64];
0024 
0025     FixedSizeSecBlock<word32, 32> k;
0026 };
0027 
0028 /// \brief DES block cipher information

0029 /// \since Crypto++ 1.0

0030 struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8>
0031 {
0032     // disable DES in DLL version by not exporting this function

0033     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "DES";}
0034 };
0035 
0036 /// \brief DES block cipher

0037 /// \details The DES implementation in Crypto++ ignores the parity bits

0038 ///   (the least significant bits of each byte) in the key. However you can use CheckKeyParityBits()

0039 ///   and CorrectKeyParityBits() to check or correct the parity bits if you wish.

0040 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES</a>

0041 /// \since Crypto++ 1.0

0042 class DES : public DES_Info, public BlockCipherDocumentation
0043 {
0044     /// \brief DES block cipher default operation

0045     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
0046     {
0047     public:
0048         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0049         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0050     };
0051 
0052 public:
0053     /// check DES key parity bits

0054     static bool CheckKeyParityBits(const byte *key);
0055     /// correct DES key parity bits

0056     static void CorrectKeyParityBits(byte *key);
0057 
0058     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0059     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0060 };
0061 
0062 /// \brief 2-key TripleDES block cipher information

0063 /// \since Crypto++ 1.0

0064 struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
0065 {
0066     CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";}
0067 };
0068 
0069 /// \brief 2-key TripleDES block cipher

0070 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-EDE2</a>

0071 /// \since Crypto++ 1.0

0072 class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation
0073 {
0074     /// \brief DES_EDE2 block cipher default operation

0075     class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
0076     {
0077     public:
0078         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0079         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0080 
0081     protected:
0082         RawDES m_des1, m_des2;
0083     };
0084 
0085 public:
0086     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0087     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0088 };
0089 
0090 /// \brief 3-key TripleDES block cipher information

0091 /// \since Crypto++ 1.0

0092 struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
0093 {
0094     CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";}
0095 };
0096 
0097 /// \brief 3-key TripleDES block cipher

0098 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-EDE3</a>

0099 /// \since Crypto++ 1.0

0100 class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation
0101 {
0102     /// \brief DES_EDE3 block cipher default operation

0103     class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
0104     {
0105     public:
0106         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0107         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0108 
0109     protected:
0110         RawDES m_des1, m_des2, m_des3;
0111     };
0112 
0113 public:
0114     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0115     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0116 };
0117 
0118 /// \brief DESX block cipher information

0119 /// \since Crypto++ 3.2

0120 struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
0121 {
0122     CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "DES-XEX3";}
0123 };
0124 
0125 /// \brief DESX block cipher

0126 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-XEX3</a>, AKA DESX

0127 /// \since Crypto++ 3.2

0128 class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation
0129 {
0130     /// \brief DES_XEX3 block cipher default operation

0131     class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
0132     {
0133     public:
0134         void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
0135         void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
0136 
0137     protected:
0138         FixedSizeSecBlock<byte, BLOCKSIZE> m_x1, m_x3;
0139         // VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock

0140         // if we use DES::Encryption here directly without value_ptr.

0141         value_ptr<DES::Encryption> m_des;
0142     };
0143 
0144 public:
0145     typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
0146     typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
0147 };
0148 
0149 typedef DES::Encryption DESEncryption;
0150 typedef DES::Decryption DESDecryption;
0151 
0152 typedef DES_EDE2::Encryption DES_EDE2_Encryption;
0153 typedef DES_EDE2::Decryption DES_EDE2_Decryption;
0154 
0155 typedef DES_EDE3::Encryption DES_EDE3_Encryption;
0156 typedef DES_EDE3::Decryption DES_EDE3_Decryption;
0157 
0158 typedef DES_XEX3::Encryption DES_XEX3_Encryption;
0159 typedef DES_XEX3::Decryption DES_XEX3_Decryption;
0160 
0161 NAMESPACE_END
0162 
0163 #endif