1 // des.h - originally 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 /// \brief DES block cipher base class
15 /// \since Crypto++ 1.0
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 /// \brief DES block cipher information
29 /// \since Crypto++ 1.0
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 /// \brief DES block cipher
37 /// \details The DES implementation in Crypto++ ignores the parity bits
38 ///   (the least significant bits of each byte) in the key. However you can use CheckKeyParityBits()
39 ///   and CorrectKeyParityBits() to	check or correct the parity bits if you wish.
40 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES</a>
41 /// \since Crypto++ 1.0
42 class DES : public DES_Info, public BlockCipherDocumentation
43 {
44 	/// \brief DES block cipher default operation
45 	class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
46 	{
47 	public:
48 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
49 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
50 	};
51 
52 public:
53 	/// check DES key parity bits
54 	static bool CheckKeyParityBits(const byte *key);
55 	/// correct DES key parity bits
56 	static void CorrectKeyParityBits(byte *key);
57 
58 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
59 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
60 };
61 
62 /// \brief 2-key TripleDES block cipher information
63 /// \since Crypto++ 1.0
64 struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
65 {
StaticAlgorithmNameDES_EDE2_Info66 	CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";}
67 };
68 
69 /// \brief 2-key TripleDES block cipher
70 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-EDE2</a>
71 /// \since Crypto++ 1.0
72 class DES_EDE2 : public DES_EDE2_Info, public BlockCipherDocumentation
73 {
74 	/// \brief DES_EDE2 block cipher default operation
75 	class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
76 	{
77 	public:
78 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
79 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
80 
81 	protected:
82 		RawDES m_des1, m_des2;
83 	};
84 
85 public:
86 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
87 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
88 };
89 
90 /// \brief 3-key TripleDES block cipher information
91 /// \since Crypto++ 1.0
92 struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
93 {
StaticAlgorithmNameDES_EDE3_Info94 	CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";}
95 };
96 
97 /// \brief 3-key TripleDES block cipher
98 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-EDE3</a>
99 /// \since Crypto++ 1.0
100 class DES_EDE3 : public DES_EDE3_Info, public BlockCipherDocumentation
101 {
102 	/// \brief DES_EDE3 block cipher default operation
103 	class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
104 	{
105 	public:
106 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
107 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
108 
109 	protected:
110 		RawDES m_des1, m_des2, m_des3;
111 	};
112 
113 public:
114 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
115 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
116 };
117 
118 /// \brief DESX block cipher information
119 /// \since Crypto++ 3.2
120 struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
121 {
StaticAlgorithmNameDES_XEX3_Info122 	CRYPTOPP_STATIC_CONSTEXPR const char* StaticAlgorithmName() {return "DES-XEX3";}
123 };
124 
125 /// \brief DESX block cipher
126 /// \sa <a href="http://www.cryptopp.com/wiki/TripleDES">DES-XEX3</a>, AKA DESX
127 /// \since Crypto++ 3.2
128 class DES_XEX3 : public DES_XEX3_Info, public BlockCipherDocumentation
129 {
130 	/// \brief DES_XEX3 block cipher default operation
131 	class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
132 	{
133 	public:
134 		void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
135 		void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
136 
137 	protected:
138 		FixedSizeSecBlock<byte, BLOCKSIZE> m_x1, m_x3;
139 		// VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock
140 		// if we use DES::Encryption here directly without value_ptr.
141 		value_ptr<DES::Encryption> m_des;
142 	};
143 
144 public:
145 	typedef BlockCipherFinal<ENCRYPTION, Base> Encryption;
146 	typedef BlockCipherFinal<DECRYPTION, Base> Decryption;
147 };
148 
149 typedef DES::Encryption DESEncryption;
150 typedef DES::Decryption DESDecryption;
151 
152 typedef DES_EDE2::Encryption DES_EDE2_Encryption;
153 typedef DES_EDE2::Decryption DES_EDE2_Decryption;
154 
155 typedef DES_EDE3::Encryption DES_EDE3_Encryption;
156 typedef DES_EDE3::Decryption DES_EDE3_Decryption;
157 
158 typedef DES_XEX3::Encryption DES_XEX3_Encryption;
159 typedef DES_XEX3::Decryption DES_XEX3_Decryption;
160 
161 NAMESPACE_END
162 
163 #endif
164