1#ifndef CRYPTOPP_DEFAULT_H
2#define CRYPTOPP_DEFAULT_H
3
4#include "sha.h"
5#include "hmac.h"
6#include "des.h"
7#include "filters.h"
8#include "modes.h"
9
10NAMESPACE_BEGIN(CryptoPP)
11
12typedef DES_EDE2 Default_BlockCipher;
13typedef SHA DefaultHashModule;
14typedef HMAC<DefaultHashModule> DefaultMAC;
15
16//! Password-Based Encryptor using DES-EDE2
17class DefaultEncryptor : public ProxyFilter
18{
19public:
20	DefaultEncryptor(const char *passphrase, BufferedTransformation *attachment = NULL);
21	DefaultEncryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL);
22
23protected:
24	void FirstPut(const byte *);
25	void LastPut(const byte *inString, size_t length);
26
27private:
28	SecByteBlock m_passphrase;
29	CBC_Mode<Default_BlockCipher>::Encryption m_cipher;
30};
31
32//! Password-Based Decryptor using DES-EDE2
33class DefaultDecryptor : public ProxyFilter
34{
35public:
36	DefaultDecryptor(const char *passphrase, BufferedTransformation *attachment = NULL, bool throwException=true);
37	DefaultDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL, bool throwException=true);
38
39	class Err : public Exception
40	{
41	public:
42		Err(const std::string &s)
43			: Exception(DATA_INTEGRITY_CHECK_FAILED, s) {}
44	};
45	class KeyBadErr : public Err {public: KeyBadErr() : Err("DefaultDecryptor: cannot decrypt message with this passphrase") {}};
46
47	enum State {WAITING_FOR_KEYCHECK, KEY_GOOD, KEY_BAD};
48	State CurrentState() const {return m_state;}
49
50protected:
51	void FirstPut(const byte *inString);
52	void LastPut(const byte *inString, size_t length);
53
54	State m_state;
55
56private:
57	void CheckKey(const byte *salt, const byte *keyCheck);
58
59	SecByteBlock m_passphrase;
60	CBC_Mode<Default_BlockCipher>::Decryption m_cipher;
61	member_ptr<FilterWithBufferedInput> m_decryptor;
62	bool m_throwException;
63};
64
65//! Password-Based Encryptor using DES-EDE2 and HMAC/SHA-1
66class DefaultEncryptorWithMAC : public ProxyFilter
67{
68public:
69	DefaultEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULL);
70	DefaultEncryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL);
71
72protected:
73	void FirstPut(const byte *inString) {}
74	void LastPut(const byte *inString, size_t length);
75
76private:
77	member_ptr<DefaultMAC> m_mac;
78};
79
80//! Password-Based Decryptor using DES-EDE2 and HMAC/SHA-1
81class DefaultDecryptorWithMAC : public ProxyFilter
82{
83public:
84	class MACBadErr : public DefaultDecryptor::Err {public: MACBadErr() : DefaultDecryptor::Err("DefaultDecryptorWithMAC: MAC check failed") {}};
85
86	DefaultDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULL, bool throwException=true);
87	DefaultDecryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULL, bool throwException=true);
88
89	DefaultDecryptor::State CurrentState() const;
90	bool CheckLastMAC() const;
91
92protected:
93	void FirstPut(const byte *inString) {}
94	void LastPut(const byte *inString, size_t length);
95
96private:
97	member_ptr<DefaultMAC> m_mac;
98	HashVerifier *m_hashVerifier;
99	bool m_throwException;
100};
101
102NAMESPACE_END
103
104#endif
105