Security Scol plugin
default.h
Go to the documentation of this file.
1// default.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_DEFAULT_H
7#define CRYPTOPP_DEFAULT_H
8
9#include "sha.h"
10#include "hmac.h"
11#include "aes.h"
12#include "des.h"
13#include "modes.h"
14#include "filters.h"
15#include "smartptr.h"
16
17NAMESPACE_BEGIN(CryptoPP)
18
19
25
32
35{
36public:
37 DataDecryptorErr(const std::string &s)
38 : Exception(DATA_INTEGRITY_CHECK_FAILED, s) {}
39};
40
43{
44 public: KeyBadErr()
45 : DataDecryptorErr("DataDecryptor: cannot decrypt message with this passphrase") {}
46};
47
50{
51 public: MACBadErr()
52 : DataDecryptorErr("DataDecryptorWithMAC: MAC check failed") {}
53};
54
56template <unsigned int BlockSize, unsigned int KeyLength, unsigned int DigestSize, unsigned int SaltSize, unsigned int Iterations>
58{
59 CRYPTOPP_CONSTANT(BLOCKSIZE = BlockSize);
60 CRYPTOPP_CONSTANT(KEYLENGTH = KeyLength);
61 CRYPTOPP_CONSTANT(SALTLENGTH = SaltSize);
62 CRYPTOPP_CONSTANT(DIGESTSIZE = DigestSize);
63 CRYPTOPP_CONSTANT(ITERATIONS = Iterations);
64};
65
68
77template <class BC, class H, class Info>
78class DataEncryptor : public ProxyFilter, public Info
79{
80public:
81 CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
82 CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
83 CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
84 CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
85 CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
86
90 DataEncryptor(const char *passphrase, BufferedTransformation *attachment = NULLPTR);
91
96 DataEncryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR);
97
98protected:
99 void FirstPut(const byte *);
100 void LastPut(const byte *inString, size_t length);
101
102private:
103 SecByteBlock m_passphrase;
104 typename CBC_Mode<BC>::Encryption m_cipher;
105};
106
115template <class BC, class H, class Info>
116class DataDecryptor : public ProxyFilter, public Info
117{
118public:
119 CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
120 CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
121 CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
122 CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
123 CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
124
129 DataDecryptor(const char *passphrase, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
130
136 DataDecryptor(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
137
138 enum State {WAITING_FOR_KEYCHECK, KEY_GOOD, KEY_BAD};
139 State CurrentState() const {return m_state;}
140
141protected:
142 void FirstPut(const byte *inString);
143 void LastPut(const byte *inString, size_t length);
144
145 State m_state;
146
147private:
148 void CheckKey(const byte *salt, const byte *keyCheck);
149
150 SecByteBlock m_passphrase;
151 typename CBC_Mode<BC>::Decryption m_cipher;
153 bool m_throwException;
154
155};
156
172template <class BC, class H, class MAC, class Info>
174{
175public:
176 CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
177 CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
178 CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
179 CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
180 CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
181
185 DataEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULLPTR);
186
191 DataEncryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR);
192
193protected:
194 void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
195 void LastPut(const byte *inString, size_t length);
196
197private:
198 member_ptr<MAC> m_mac;
199
200};
201
217template <class BC, class H, class MAC, class Info>
219{
220public:
221 CRYPTOPP_CONSTANT(BLOCKSIZE = Info::BLOCKSIZE);
222 CRYPTOPP_CONSTANT(KEYLENGTH = Info::KEYLENGTH);
223 CRYPTOPP_CONSTANT(SALTLENGTH = Info::SALTLENGTH);
224 CRYPTOPP_CONSTANT(DIGESTSIZE = Info::DIGESTSIZE);
225 CRYPTOPP_CONSTANT(ITERATIONS = Info::ITERATIONS);
226
231 DataDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
232
238 DataDecryptorWithMAC(const byte *passphrase, size_t passphraseLength, BufferedTransformation *attachment = NULLPTR, bool throwException=true);
239
240 typename DataDecryptor<BC,H,Info>::State CurrentState() const;
241 bool CheckLastMAC() const;
242
243protected:
244 void FirstPut(const byte *inString) {CRYPTOPP_UNUSED(inString);}
245 void LastPut(const byte *inString, size_t length);
246
247private:
248 member_ptr<MAC> m_mac;
249 HashVerificationFilter *m_hashVerifier;
250 bool m_throwException;
251};
252
253#if defined(CRYPTOPP_DOXYGEN_PROCESSING)
258struct LegacyEncryptor : public DataEncryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> {};
263struct LegacyDecryptor : public DataDecryptor<LegacyBlockCipher,LegacyHashModule,LegacyParametersInfo> {};
268struct DefaultEncryptor : public DataEncryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> {};
273struct DefaultDecryptor : public DataDecryptor<DefaultBlockCipher,DefaultHashModule,DefaultParametersInfo> {};
278struct LegacyEncryptorWithMAC : public DataEncryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> {};
283struct LegacyDecryptorWithMAC : public DataDecryptorWithMAC<LegacyBlockCipher,LegacyHashModule,LegacyMAC,LegacyParametersInfo> {};
288struct DefaultEncryptorWithMAC : public DataEncryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> {};
293struct DefaultDecryptorWithMAC : public DataDecryptorWithMAC<DefaultBlockCipher,DefaultHashModule,DefaultMAC,DefaultParametersInfo> {};
294#else
297
300
303
306#endif
307
308NAMESPACE_END
309
310#endif
Class file for the AES cipher (Rijndael)
Interface for buffered transformations.
Definition cryptlib.h:1652
Block cipher mode of operation aggregate.
Definition modes.h:347
2-key TripleDES block cipher
Definition des.h:73
Exception thrown when LegacyDecryptorWithMAC or DefaultDecryptorWithMAC decryption error is encounter...
Definition default.h:35
Password-based Decryptor.
Definition default.h:117
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition default.cpp:170
Password-based decryptor with MAC.
Definition default.h:219
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition default.cpp:287
Password-based Encryptor.
Definition default.h:79
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition default.cpp:133
Password-based encryptor with MAC.
Definition default.h:174
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Definition default.cpp:248
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
HMAC.
Definition hmac.h:53
Filter wrapper for HashTransformation.
Definition filters.h:611
Exception thrown when a bad key is encountered in DefaultDecryptorWithMAC and LegacyDecryptorWithMAC.
Definition default.h:43
Exception thrown when an incorrect MAC is encountered in DefaultDecryptorWithMAC and LegacyDecryptorW...
Definition default.h:50
Base class for Filter classes that are proxies for a chain of other filters.
Definition filters.h:1039
SHA-1 message digest.
Definition sha.h:27
SHA-256 message digest.
Definition sha.h:65
Pointer that overloads operator ->
Definition smartptr.h:38
AES DefaultBlockCipher
Default block cipher for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecry...
Definition default.h:27
Classes for DES, 2-key Triple-DES, 3-key Triple-DES and DESX.
Implementation of BufferedTransformation's attachment interface.
Classes for HMAC message authentication codes.
Classes for block cipher modes of operation.
Classes for SHA-1 and SHA-2 family of message digests.
Classes for automatic resource management.
Algorithm information for password-based encryptors and decryptors.
Definition default.h:58