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