default.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // default.h - originally written and placed in the public domain by Wei Dai
  2. /// \file default.h
  3. /// \brief Classes for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
  4. #ifndef CRYPTOPP_DEFAULT_H
  5. #define CRYPTOPP_DEFAULT_H
  6. #include "sha.h"
  7. #include "hmac.h"
  8. #include "aes.h"
  9. #include "des.h"
  10. #include "modes.h"
  11. #include "filters.h"
  12. #include "smartptr.h"
  13. NAMESPACE_BEGIN(CryptoPP)
  14. /// \brief Legacy block cipher for LegacyEncryptor, LegacyDecryptor, LegacyEncryptorWithMAC and LegacyDecryptorWithMAC
  15. typedef DES_EDE2 LegacyBlockCipher;
  16. /// \brief Legacy hash for use with LegacyEncryptorWithMAC and LegacyDecryptorWithMAC
  17. typedef SHA1 LegacyHashModule;
  18. /// \brief Legacy HMAC for use withLegacyEncryptorWithMAC and LegacyDecryptorWithMAC
  19. typedef HMAC<LegacyHashModule> LegacyMAC;
  20. /// \brief Default block cipher for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
  21. typedef AES DefaultBlockCipher;
  22. /// \brief Default hash for use with DefaultEncryptorWithMAC and DefaultDecryptorWithMAC
  23. typedef SHA256 DefaultHashModule;
  24. /// \brief Default HMAC for use withDefaultEncryptorWithMAC and DefaultDecryptorWithMAC
  25. typedef HMAC<DefaultHashModule> DefaultMAC;
  26. /// \brief Exception thrown when LegacyDecryptorWithMAC or DefaultDecryptorWithMAC decryption error is encountered
  27. class DataDecryptorErr : public Exception
  28. {
  29. public:
  30. DataDecryptorErr(const std::string &s)
  31. : Exception(DATA_INTEGRITY_CHECK_FAILED, s) {}
  32. };
  33. /// \brief Exception thrown when a bad key is encountered in DefaultDecryptorWithMAC and LegacyDecryptorWithMAC
  34. class KeyBadErr : public DataDecryptorErr
  35. {
  36. public: KeyBadErr()
  37. : DataDecryptorErr("DataDecryptor: cannot decrypt message with this passphrase") {}
  38. };
  39. /// \brief Exception thrown when an incorrect MAC is encountered in DefaultDecryptorWithMAC and LegacyDecryptorWithMAC
  40. class MACBadErr : public DataDecryptorErr
  41. {
  42. public: MACBadErr()
  43. : DataDecryptorErr("DataDecryptorWithMAC: MAC check failed") {}
  44. };
  45. /// \brief Algorithm information for password-based encryptors and decryptors
  46. template <unsigned int BlockSize, unsigned int KeyLength, unsigned int DigestSize, unsigned int SaltSize, unsigned int Iterations>
  47. struct DataParametersInfo
  48. {
  49. CRYPTOPP_CONSTANT(BLOCKSIZE = BlockSize);
  50. CRYPTOPP_CONSTANT(KEYLENGTH = KeyLength);
  51. CRYPTOPP_CONSTANT(SALTLENGTH = SaltSize);
  52. CRYPTOPP_CONSTANT(DIGESTSIZE = DigestSize);
  53. CRYPTOPP_CONSTANT(ITERATIONS = Iterations);
  54. };
  55. typedef DataParametersInfo<LegacyBlockCipher::BLOCKSIZE, LegacyBlockCipher::DEFAULT_KEYLENGTH, LegacyHashModule::DIGESTSIZE, 8, 200> LegacyParametersInfo;
  56. typedef DataParametersInfo<DefaultBlockCipher::BLOCKSIZE, DefaultBlockCipher::DEFAULT_KEYLENGTH, DefaultHashModule::DIGESTSIZE, 8, 2500> DefaultParametersInfo;
  57. /// \brief Password-based Encryptor
  58. /// \tparam BC BlockCipher based class used for encryption
  59. /// \tparam H HashTransformation based class used for mashing
  60. /// \tparam Info Constants used by the algorithms
  61. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  62. /// Crypto++ 5.7 switched to AES and SHA256.
  63. /// \sa DefaultEncryptor, DefaultDecryptor, LegacyEncryptor, LegacyDecryptor
  64. /// \since Crypto++ 2.0
  65. template <class BC, class H, class Info>
  66. class DataEncryptor : public ProxyFilter, public Info
  67. {
  68. public:
  69. CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
  70. CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
  71. CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
  72. CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
  73. CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
  74. /// \brief Construct a DataEncryptor
  75. /// \param passphrase a C-String password
  76. /// \param attachment a BufferedTransformation to attach to this object
  77. DataEncryptor(const char *passphrase, BufferedTransformation *attachment = NULLPTR);
  78. /// \brief Construct a DataEncryptor
  79. /// \param passphrase a byte string password
  80. /// \param passphraseLength the length of the byte string password
  81. /// \param attachment a BufferedTransformation to attach to this object
  82. DataEncryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR);
  83. protected:
  84. void FirstPut(const byte *);
  85. void LastPut(const byte *inString, size_t length);
  86. private:
  87. SecByteBlock m_passphrase;
  88. typename CBC_Mode<BC>::Encryption m_cipher;
  89. };
  90. /// \brief Password-based Decryptor
  91. /// \tparam BC BlockCipher based class used for encryption
  92. /// \tparam H HashTransformation based class used for mashing
  93. /// \tparam Info Constants used by the algorithms
  94. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  95. /// Crypto++ 5.7 switched to AES and SHA256.
  96. /// \sa DefaultEncryptor, DefaultDecryptor, LegacyEncryptor, LegacyDecryptor
  97. /// \since Crypto++ 2.0
  98. template <class BC, class H, class Info>
  99. class DataDecryptor : public ProxyFilter, public Info
  100. {
  101. public:
  102. CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
  103. CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
  104. CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
  105. CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
  106. CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
  107. /// \brief Constructs a DataDecryptor
  108. /// \param passphrase a C-String password
  109. /// \param attachment a BufferedTransformation to attach to this object
  110. /// \param throwException a flag specifying whether an Exception should be thrown on error
  111. DataDecryptor(const char *passphrase, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
  112. /// \brief Constructs a DataDecryptor
  113. /// \param passphrase a byte string password
  114. /// \param passphraseLength the length of the byte string password
  115. /// \param attachment a BufferedTransformation to attach to this object
  116. /// \param throwException a flag specifying whether an Exception should be thrown on error
  117. DataDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
  118. enum State {WAITING_FOR_KEYCHECK, KEY_GOOD, KEY_BAD};
  119. State CurrentState() const {return m_state;}
  120. protected:
  121. void FirstPut(const byte *inString);
  122. void LastPut(const byte *inString, size_t length);
  123. State m_state;
  124. private:
  125. void CheckKey(const byte *salt, const byte *keyCheck);
  126. SecByteBlock m_passphrase;
  127. typename CBC_Mode<BC>::Decryption m_cipher;
  128. member_ptr<FilterWithBufferedInput> m_decryptor;
  129. bool m_throwException;
  130. };
  131. /// \brief Password-based encryptor with MAC
  132. /// \tparam BC BlockCipher based class used for encryption
  133. /// \tparam H HashTransformation based class used for mashing
  134. /// \tparam MAC HashTransformation based class used for authentication
  135. /// \tparam Info Constants used by the algorithms
  136. /// \details DataEncryptorWithMAC uses a non-standard mashup function called Mash() to derive key
  137. /// bits from the password.
  138. /// \details The purpose of the function Mash() is to take an arbitrary length input string and
  139. /// *deterministically* produce an arbitrary length output string such that (1) it looks random,
  140. /// (2) no information about the input is deducible from it, and (3) it contains as much entropy
  141. /// as it can hold, or the amount of entropy in the input string, whichever is smaller.
  142. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  143. /// Crypto++ 5.7 switched to AES and SHA256.
  144. /// \sa DefaultEncryptorWithMAC, DefaultDecryptorWithMAC, LegacyDecryptorWithMAC, LegacyEncryptorWithMAC
  145. /// \since Crypto++ 2.0
  146. template <class BC, class H, class MAC, class Info>
  147. class DataEncryptorWithMAC : public ProxyFilter
  148. {
  149. public:
  150. CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
  151. CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
  152. CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
  153. CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
  154. CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
  155. /// \brief Constructs a DataEncryptorWithMAC
  156. /// \param passphrase a C-String password
  157. /// \param attachment a BufferedTransformation to attach to this object
  158. DataEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULLPTR);
  159. /// \brief Constructs a DataEncryptorWithMAC
  160. /// \param passphrase a byte string password
  161. /// \param passphraseLength the length of the byte string password
  162. /// \param attachment a BufferedTransformation to attach to this object
  163. DataEncryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR);
  164. protected:
  165. void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
  166. void LastPut(const byte *inString, size_t length);
  167. private:
  168. member_ptr<MAC> m_mac;
  169. };
  170. /// \brief Password-based decryptor with MAC
  171. /// \tparam BC BlockCipher based class used for encryption
  172. /// \tparam H HashTransformation based class used for mashing
  173. /// \tparam MAC HashTransformation based class used for authentication
  174. /// \tparam Info Constants used by the algorithms
  175. /// \details DataDecryptorWithMAC uses a non-standard mashup function called Mash() to derive key
  176. /// bits from the password.
  177. /// \details The purpose of the function Mash() is to take an arbitrary length input string and
  178. /// *deterministically* produce an arbitrary length output string such that (1) it looks random,
  179. /// (2) no information about the input is deducible from it, and (3) it contains as much entropy
  180. /// as it can hold, or the amount of entropy in the input string, whichever is smaller.
  181. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  182. /// Crypto++ 5.7 switched to AES and SHA256.
  183. /// \sa DefaultEncryptorWithMAC, DefaultDecryptorWithMAC, LegacyDecryptorWithMAC, LegacyEncryptorWithMAC
  184. /// \since Crypto++ 2.0
  185. template <class BC, class H, class MAC, class Info>
  186. class DataDecryptorWithMAC : public ProxyFilter
  187. {
  188. public:
  189. CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
  190. CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
  191. CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
  192. CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
  193. CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
  194. /// \brief Constructs a DataDecryptor
  195. /// \param passphrase a C-String password
  196. /// \param attachment a BufferedTransformation to attach to this object
  197. /// \param throwException a flag specifying whether an Exception should be thrown on error
  198. DataDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
  199. /// \brief Constructs a DataDecryptor
  200. /// \param passphrase a byte string password
  201. /// \param passphraseLength the length of the byte string password
  202. /// \param attachment a BufferedTransformation to attach to this object
  203. /// \param throwException a flag specifying whether an Exception should be thrown on error
  204. DataDecryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
  205. typename DataDecryptor<BC,H,Info>::State CurrentState() const;
  206. bool CheckLastMAC() const;
  207. protected:
  208. void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
  209. void LastPut(const byte *inString, size_t length);
  210. private:
  211. member_ptr<MAC> m_mac;
  212. HashVerificationFilter *m_hashVerifier;
  213. bool m_throwException;
  214. };
  215. #if defined(CRYPTOPP_DOXYGEN_PROCESSING)
  216. /// \brief Password-based encryptor (deprecated)
  217. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  218. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  219. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  220. struct LegacyEncryptor : public DataEncryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> {};
  221. /// \brief Password-based decryptor (deprecated)
  222. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  223. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  224. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  225. struct LegacyDecryptor : public DataDecryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> {};
  226. /// \brief Password-based encryptor
  227. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  228. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  229. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  230. struct DefaultEncryptor : public DataEncryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> {};
  231. /// \brief Password-based decryptor
  232. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  233. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  234. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  235. struct DefaultDecryptor : public DataDecryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> {};
  236. /// \brief Password-based encryptor with MAC (deprecated)
  237. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  238. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  239. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  240. struct LegacyEncryptorWithMAC : public DataEncryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> {};
  241. /// \brief Password-based decryptor with MAC (deprecated)
  242. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  243. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  244. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  245. struct LegacyDecryptorWithMAC : public DataDecryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> {};
  246. /// \brief Password-based encryptor with MAC
  247. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  248. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  249. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  250. struct DefaultEncryptorWithMAC : public DataEncryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> {};
  251. /// \brief Password-based decryptor with MAC
  252. /// \details Crypto++ 5.6.5 and earlier used the legacy algorithms, including DES_EDE2 and SHA1.
  253. /// Crypto++ 5.7 switched to AES and SHA256. The updated algorithms are available with the
  254. /// <tt>Default*</tt> classes, and the old algorithms are available with the <tt>Legacy*</tt> classes.
  255. struct DefaultDecryptorWithMAC : public DataDecryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> {};
  256. #else
  257. typedef DataEncryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> LegacyEncryptor;
  258. typedef DataDecryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> LegacyDecryptor;
  259. typedef DataEncryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> DefaultEncryptor;
  260. typedef DataDecryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> DefaultDecryptor;
  261. typedef DataEncryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> LegacyEncryptorWithMAC;
  262. typedef DataDecryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> LegacyDecryptorWithMAC;
  263. typedef DataEncryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> DefaultEncryptorWithMAC;
  264. typedef DataDecryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> DefaultDecryptorWithMAC;
  265. #endif
  266. NAMESPACE_END
  267. #endif