1 // des.h - written and placed in the public domain by Wei Dai
2 
3 //! \file des.h
4 //! \brief Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX
5 
6 #ifndef CRYPTOPP_DES_H
7 #define CRYPTOPP_DES_H
8 
9 #include "seckey.h"
10 #include "secblock.h"
11 
NAMESPACE_BEGIN(CryptoPP)12 NAMESPACE_BEGIN(CryptoPP)
13 
14 //! \class RawDES
15 //! \brief DES block cipher base class
16 class CRYPTOPP_DLL RawDES
17 {
18 public:
19 	void RawSetKey(CipherDir direction, const byte *userKey);
20 	void RawProcessBlock(word32 &l, word32 &r) const;
21 
22 protected:
23 	static const word32 Spbox[8][64];
24 
25 	FixedSizeSecBlock<word32, 32> k;
26 };
27 
28 //! \class DES_Info
29 //! \brief DES block cipher information
30 struct DES_Info : public FixedBlockSize<8>, public FixedKeyLength<8>
31 {
32 	// disable DES in DLL version by not exporting this function
StaticAlgorithmNameDES_Info33 	CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "DES";}
34 };
35 
36 //! \class DES
37 //! \brief DES block cipher
38 //! \details The DES implementation in Crypto++ ignores the parity bits
39 //!   (the least significant bits of each byte) in the key. However you can use CheckKeyParityBits()
40 //!   and CorrectKeyParityBits() to	check or correct the parity bits if you wish.
41 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DES">DES</a>
42 //! \since Crypto++ 1.0
43 class DES : public DES_Info, public BlockCipherDocumentation
44 {
45 	//! \class Base
46 	//! \brief DES block cipher default operation
47 	class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
48 	{
49 	public:
50 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
51 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
52 	};
53 
54 public:
55 	//! check DES key parity bits
56 	static bool CheckKeyParityBits(const byte *key);
57 	//! correct DES key parity bits
58 	static void CorrectKeyParityBits(byte *key);
59 
60 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
61 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
62 };
63 
64 //! \class DES_EDE2_Info
65 //! \brief 2-key TripleDES block cipher information
66 struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
67 {
StaticAlgorithmNameDES_EDE2_Info68 	CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";}
69 };
70 
71 //! \class DES_EDE2
72 //! \brief 2-key TripleDES block cipher
73 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE2</a>
74 //! \since Crypto++ 1.0
75 class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation
76 {
77 	//! \class Base
78 	//! \brief DES_EDE2 block cipher default operation
79 	class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
80 	{
81 	public:
82 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
83 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
84 
85 	protected:
86 		RawDES m_des1, m_des2;
87 	};
88 
89 public:
90 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
91 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
92 };
93 
94 //! \class DES_EDE3_Info
95 //! \brief 3-key TripleDES block cipher information
96 struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
97 {
StaticAlgorithmNameDES_EDE3_Info98 	CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";}
99 };
100 
101 //! \class DES_EDE3
102 //! \brief 3-key TripleDES block cipher
103 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE3</a>
104 //! \since Crypto++ 1.0
105 class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation
106 {
107 	//! \class Base
108 	//! \brief DES_EDE3 block cipher default operation
109 	class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
110 	{
111 	public:
112 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
113 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
114 
115 	protected:
116 		RawDES m_des1, m_des2, m_des3;
117 	};
118 
119 public:
120 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
121 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
122 };
123 
124 //! \class DES_XEX3_Info
125 //! \brief DESX block cipher information
126 struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
127 {
StaticAlgorithmNameDES_XEX3_Info128 	CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "DES-XEX3";}
129 };
130 
131 //! \class DES_XEX3
132 //! \brief DESX block cipher
133 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESX">DES-XEX3</a>, AKA DESX
134 //! \since Crypto++ 1.0
135 class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation
136 {
137 	//! \class Base
138 	//! \brief DES_XEX3 block cipher default operation
139 	class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
140 	{
141 	public:
142 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
143 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
144 
145 	protected:
146 		FixedSizeSecBlock<byte, BLOCKSIZE> m_x1, m_x3;
147 		// VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock
148 		// if we use DES::Encryption here directly without value_ptr.
149 		value_ptr<DES::Encryption> m_des;
150 	};
151 
152 public:
153 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
154 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
155 };
156 
157 typedef DES::Encryption DESEncryption;
158 typedef DES::Decryption DESDecryption;
159 
160 typedef DES_EDE2::Encryption DES_EDE2_Encryption;
161 typedef DES_EDE2::Decryption DES_EDE2_Decryption;
162 
163 typedef DES_EDE3::Encryption DES_EDE3_Encryption;
164 typedef DES_EDE3::Decryption DES_EDE3_Decryption;
165 
166 typedef DES_XEX3::Encryption DES_XEX3_Encryption;
167 typedef DES_XEX3::Decryption DES_XEX3_Decryption;
168 
169 NAMESPACE_END
170 
171 #endif
172