Crypto++  5.6.3
Free C++ class library of cryptographic schemes
des.h
Go to the documentation of this file.
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 
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  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 CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_Info>, public RawDES
45  {
46  public:
47  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
48  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
49  };
50 
51 public:
52  //! check DES key parity bits
53  static bool CheckKeyParityBits(const byte *key);
54  //! correct DES key parity bits
55  static void CorrectKeyParityBits(byte *key);
56 
59 };
60 
61 //! \class DES_EDE2_Info
62 //! \brief 2-key TripleDES block cipher information
63 struct DES_EDE2_Info : public FixedBlockSize<8>, public FixedKeyLength<16>
64 {
65  CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE2";}
66 };
67 
68 //! \class DES_EDE2
69 //! \brief 2-key TripleDES block cipher
70 /// \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE2</a>
72 {
73  class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE2_Info>
74  {
75  public:
76  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
77  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
78 
79  protected:
80  RawDES m_des1, m_des2;
81  };
82 
83 public:
86 };
87 
88 //! \class DES_EDE3_Info
89 //! \brief 3-key TripleDES block cipher information
90 struct DES_EDE3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
91 {
92  CRYPTOPP_DLL static const char * CRYPTOPP_API StaticAlgorithmName() {return "DES-EDE3";}
93 };
94 
95 //! \class DES_EDE3
96 //! \brief 3-key TripleDES block cipher
97 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESede">DES-EDE3</a>
99 {
100  class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_EDE3_Info>
101  {
102  public:
103  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
104  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
105 
106  protected:
107  RawDES m_des1, m_des2, m_des3;
108  };
109 
110 public:
113 };
114 
115 //! \class DES_XEX3_Info
116 //! \brief DESX block cipher information
117 struct DES_XEX3_Info : public FixedBlockSize<8>, public FixedKeyLength<24>
118 {
119  static const char *StaticAlgorithmName() {return "DES-XEX3";}
120 };
121 
122 //! \class DES_XEX3
123 //! \brief DESX block cipher
124 //! \sa <a href="http://www.weidai.com/scan-mirror/cs.html#DESX">DES-XEX3</a>, AKA DESX
126 {
127  class CRYPTOPP_NO_VTABLE Base : public BlockCipherImpl<DES_XEX3_Info>
128  {
129  public:
130  void UncheckedSetKey(const byte *userKey, unsigned int length, const NameValuePairs &params);
131  void ProcessAndXorBlock(const byte *inBlock, const byte *xorBlock, byte *outBlock) const;
132 
133  protected:
135  // VS2005 workaround: calling modules compiled with /clr gets unresolved external symbol DES::Base::ProcessAndXorBlock
136  // if we use DES::Encryption here directly without value_ptr.
138  };
139 
140 public:
143 };
144 
147 
150 
153 
156 
157 NAMESPACE_END
158 
159 #endif
Inherited by keyed algorithms with fixed key length.
Definition: seckey.h:117
Provides Encryption and Decryption typedefs used by derived classes to implement a block cipher...
Definition: seckey.h:396
static void CorrectKeyParityBits(byte *key)
correct DES key parity bits
Definition: des.cpp:416
CipherDir
Specifies a direction for a cipher to operate.
Definition: cryptlib.h:103
DES block cipher base class.
Definition: des.h:16
DES block cipher.
Definition: des.h:42
Classes and functions for secure memory allocations.
Inherited by algorithms with fixed block size.
Definition: seckey.h:34
DESX block cipher.
Definition: des.h:125
3-key TripleDES block cipher
Definition: des.h:98
Classes and functions for implementing secret key algorithms.
2-key TripleDES block cipher information
Definition: des.h:63
Provides class member functions to key a block cipher.
Definition: seckey.h:316
3-key TripleDES block cipher information
Definition: des.h:90
2-key TripleDES block cipher
Definition: des.h:71
DES block cipher information.
Definition: des.h:30
DESX block cipher information.
Definition: des.h:117
static bool CheckKeyParityBits(const byte *key)
check DES key parity bits
Definition: des.cpp:408
Value pointer.
Definition: smartptr.h:79
Provides a base implementation of Algorithm and SimpleKeyingInterface for block ciphers.
Definition: seckey.h:303
Crypto++ library namespace.
Interface for retrieving values given their names.
Definition: cryptlib.h:277