Crypto++  5.6.3
Free C++ class library of cryptographic schemes
default.h
Go to the documentation of this file.
1 // default.h - written and placed in the public domain by Wei Dai
2 
3 //! \file default.h
4 //! \brief Classes for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
5 
6 #ifndef CRYPTOPP_DEFAULT_H
7 #define CRYPTOPP_DEFAULT_H
8 
9 #include "sha.h"
10 #include "hmac.h"
11 #include "des.h"
12 #include "modes.h"
13 #include "filters.h"
14 #include "smartptr.h"
15 
16 NAMESPACE_BEGIN(CryptoPP)
17 
18 //! \brief Default block cipher for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
20 //! \brief Default hash for use with DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
22 //! \brief Default HMAC for use withDefaultEncryptorWithMAC and DefaultDecryptorWithMAC
23 typedef HMAC<DefaultHashModule> DefaultMAC;
24 
25 //! \class DefaultEncryptor
26 //! \brief Password-Based Encryptor using TripleDES
27 //! \details The class uses 2-key TripleDES (DES_EDE2) for encryption, which only
28 //! provides about 80-bits of security.
30 {
31 public:
32  //! \brief Construct a DefaultEncryptor
33  //! \param passphrase a C-String password
34  //! \param attachment a BufferedTransformation to attach to this object
35  DefaultEncryptor(const char *passphrase, BufferedTransformation *attachment = NULL);
36 
37  //! \brief Construct a DefaultEncryptor
38  //! \param passphrase a byte string password
39  //! \param passphraseLength the length of the byte string password
40  //! \param attachment a BufferedTransformation to attach to this object
41  DefaultEncryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL);
42 
43 protected:
44  void FirstPut(const byte *);
45  void LastPut(const byte *inString, size_t length);
46 
47 private:
48  SecByteBlock m_passphrase;
50 
51 #if (CRYPTOPP_GCC_VERSION >= 40500) || (CRYPTOPP_CLANG_VERSION >= 20800)
52 } __attribute__((deprecated ("DefaultEncryptor will be changing in the near future because the algorithms are no longer secure")));
53 #elif (CRYPTOPP_GCC_VERSION)
54 } __attribute__((deprecated));
55 #else
56 };
57 #endif
58 
59 //! \class DefaultDecryptor
60 //! \brief Password-Based Decryptor using TripleDES
61 //! \details The class uses 2-key TripleDES (DES_EDE2) for encryption, which only
62 //! provides about 80-bits of security.
64 {
65 public:
66  //! \brief Constructs a DefaultDecryptor
67  //! \param passphrase a C-String password
68  //! \param attachment a BufferedTransformation to attach to this object
69  //! \param throwException a flag specifiying whether an Exception should be thrown on error
70  DefaultDecryptor(const char *passphrase, BufferedTransformation *attachment = NULL, bool throwException=true);
71 
72  //! \brief Constructs a DefaultDecryptor
73  //! \param passphrase a byte string password
74  //! \param passphraseLength the length of the byte string password
75  //! \param attachment a BufferedTransformation to attach to this object
76  //! \param throwException a flag specifiying whether an Exception should be thrown on error
77  DefaultDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL, bool throwException=true);
78 
79  class Err : public Exception
80  {
81  public:
82  Err(const std::string &s)
84  };
85  class KeyBadErr : public Err {public: KeyBadErr() : Err("DefaultDecryptor: cannot decrypt message with this passphrase") {}};
86 
87  enum State {WAITING_FOR_KEYCHECK, KEY_GOOD, KEY_BAD};
88  State CurrentState() const {return m_state;}
89 
90 protected:
91  void FirstPut(const byte *inString);
92  void LastPut(const byte *inString, size_t length);
93 
94  State m_state;
95 
96 private:
97  void CheckKey(const byte *salt, const byte *keyCheck);
98 
99  SecByteBlock m_passphrase;
102  bool m_throwException;
103 
104 #if (CRYPTOPP_GCC_VERSION >= 40500) || (CRYPTOPP_CLANG_VERSION >= 20800)
105 } __attribute__((deprecated ("DefaultDecryptor will be changing in the near future because the algorithms are no longer secure")));
106 #elif (CRYPTOPP_GCC_VERSION)
107 } __attribute__((deprecated));
108 #else
109 };
110 #endif
111 
112 //! \class DefaultEncryptorWithMAC
113 //! \brief Password-Based encryptor using TripleDES and HMAC/SHA-1
114 //! \details DefaultEncryptorWithMAC uses a non-standard mashup function called Mash() to derive key
115 //! bits from the password. The class also uses 2-key TripleDES (DES_EDE2) for encryption, which only
116 //! provides about 80-bits of security.
117 //! \details The purpose of the function Mash() is to take an arbitrary length input string and
118 //! *deterministicly* produce an arbitrary length output string such that (1) it looks random,
119 //! (2) no information about the input is deducible from it, and (3) it contains as much entropy
120 //! as it can hold, or the amount of entropy in the input string, whichever is smaller.
122 {
123 public:
124  //! \brief Constructs a DefaultEncryptorWithMAC
125  //! \param passphrase a C-String password
126  //! \param attachment a BufferedTransformation to attach to this object
127  DefaultEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULL);
128 
129  //! \brief Constructs a DefaultEncryptorWithMAC
130  //! \param passphrase a byte string password
131  //! \param passphraseLength the length of the byte string password
132  //! \param attachment a BufferedTransformation to attach to this object
133  DefaultEncryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL);
134 
135 protected:
136  void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
137  void LastPut(const byte *inString, size_t length);
138 
139 private:
141 
142 #if (CRYPTOPP_GCC_VERSION >= 40500) || (CRYPTOPP_CLANG_VERSION >= 20800)
143 } __attribute__((deprecated ("DefaultEncryptorWithMAC will be changing in the near future because the algorithms are no longer secure")));
144 #elif (CRYPTOPP_GCC_VERSION)
145 } __attribute__((deprecated));
146 #else
147 };
148 #endif
149 
150 //! \class DefaultDecryptorWithMAC
151 //! \brief Password-Based decryptor using TripleDES and HMAC/SHA-1
152 //! \details DefaultDecryptorWithMAC uses a non-standard mashup function called Mash() to derive key
153 //! bits from the password. The class also uses 2-key TripleDES (DES_EDE2) for encryption, which only
154 //! provides about 80-bits of security.
155 //! \details The purpose of the function Mash() is to take an arbitrary length input string and
156 //! *deterministicly* produce an arbitrary length output string such that (1) it looks random,
157 //! (2) no information about the input is deducible from it, and (3) it contains as much entropy
158 //! as it can hold, or the amount of entropy in the input string, whichever is smaller.
160 {
161 public:
162  //! \class MACBadErr
163  //! \brief Excpetion thrown when an incorrect MAC is encountered
164  class MACBadErr : public DefaultDecryptor::Err {public: MACBadErr() : DefaultDecryptor::Err("DefaultDecryptorWithMAC: MAC check failed") {}};
165 
166  //! \brief Constructs a DefaultDecryptor
167  //! \param passphrase a C-String password
168  //! \param attachment a BufferedTransformation to attach to this object
169  //! \param throwException a flag specifiying whether an Exception should be thrown on error
170  DefaultDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULL, bool throwException=true);
171 
172  //! \brief Constructs a DefaultDecryptor
173  //! \param passphrase a byte string password
174  //! \param passphraseLength the length of the byte string password
175  //! \param attachment a BufferedTransformation to attach to this object
176  //! \param throwException a flag specifiying whether an Exception should be thrown on error
177  DefaultDecryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL, bool throwException=true);
178 
179  DefaultDecryptor::State CurrentState() const;
180  bool CheckLastMAC() const;
181 
182 protected:
183  void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
184  void LastPut(const byte *inString, size_t length);
185 
186 private:
188  HashVerifier *m_hashVerifier;
189  bool m_throwException;
190 
191 #if (CRYPTOPP_GCC_VERSION >= 40500) || (CRYPTOPP_CLANG_VERSION >= 20800)
192 } __attribute__((deprecated ("DefaultDecryptorWithMAC will be changing in the near future because the algorithms are no longer secure")));
193 #elif (CRYPTOPP_GCC_VERSION)
194 } __attribute__((deprecated));
195 #else
196 };
197 #endif
198 
199 NAMESPACE_END
200 
201 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:139
Base class for Filter classes that are proxies for a chain of other filters.
Definition: filters.h:869
Password-Based Encryptor using TripleDES.
Definition: default.h:29
Class file for modes of operation.
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
Definition: cryptlib.h:162
Classes for automatic resource management.
SecBlock typedef.
Definition: secblock.h:734
Interface for buffered transformations.
Definition: cryptlib.h:1352
Excpetion thrown when an incorrect MAC is encountered.
Definition: default.h:164
Data integerity check, such as CRC or MAC, failed.
Definition: cryptlib.h:152
Password-Based Decryptor using TripleDES.
Definition: default.h:63
DefaultDecryptor(const char *passphrase, BufferedTransformation *attachment=NULL, bool throwException=true)
Constructs a DefaultDecryptor.
Definition: default.cpp:136
Classes for HMAC message authentication codes.
Filter wrapper for HashTransformation.
Definition: filters.h:535
2-key TripleDES block cipher
Definition: des.h:71
SHA-1
Definition: sha.h:21
Password-Based encryptor using TripleDES and HMAC/SHA-1.
Definition: default.h:121
Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX.
Classes for SHA-1 and SHA-2 family of message digests.
Implementation of BufferedTransformation's attachment interface.
HMAC.
Definition: hmac.h:50
Password-Based decryptor using TripleDES and HMAC/SHA-1.
Definition: default.h:159
Crypto++ library namespace.
DefaultDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULL, bool throwException=true)
Constructs a DefaultDecryptor.
Definition: default.cpp:238
DefaultEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULL)
Constructs a DefaultEncryptorWithMAC.
Definition: default.cpp:216