6#if CRYPTOPP_MSC_VERSION
7# pragma warning(disable: 4127 4189)
19NAMESPACE_BEGIN(CryptoPP)
28static void Mash(
const byte *in,
size_t inLen,
byte *out,
size_t outLen,
int iterations)
40 for(i=0; i<outLen; i+=H::DIGESTSIZE)
42 b[0] = (
byte) (i >> 8);
45 hash.Update(in, inLen);
49 while (iterations-- > 1)
51 memcpy(buf, outBuf, bufSize);
52 for (i=0; i<bufSize; i+=H::DIGESTSIZE)
54 b[0] = (
byte) (i >> 8);
57 hash.Update(buf, bufSize);
62 memcpy(out, outBuf, outLen);
65template <
class BC,
class H,
class Info>
66static void GenerateKeyIV(
const byte *passphrase,
size_t passphraseLength,
const byte *salt,
size_t saltLength,
unsigned int iterations,
byte *key,
byte *IV)
70 if (passphrase != NULLPTR)
71 memcpy(temp, passphrase, passphraseLength);
73 memcpy(temp+passphraseLength, salt, saltLength);
77 Mash<H>(temp, passphraseLength + saltLength, keyIV,
EnumToInt(Info::KEYLENGTH)+
EnumToInt(+Info::BLOCKSIZE), iterations);
78 memcpy(key, keyIV, Info::KEYLENGTH);
79 memcpy(IV, keyIV+Info::KEYLENGTH, Info::BLOCKSIZE);
84template <
class BC,
class H,
class Info>
86 :
ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase((const
byte *)passphrase, strlen(passphrase))
88 CRYPTOPP_COMPILE_ASSERT((
int)SALTLENGTH <= DIGESTSIZE);
89 CRYPTOPP_COMPILE_ASSERT((
int)BLOCKSIZE <= (
int)DIGESTSIZE);
92template <
class BC,
class H,
class Info>
94 :
ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase(passphrase, passphraseLength)
96 CRYPTOPP_COMPILE_ASSERT((
int)SALTLENGTH <= (
int)DIGESTSIZE);
97 CRYPTOPP_COMPILE_ASSERT((
int)BLOCKSIZE <= (
int)DIGESTSIZE);
100template <
class BC,
class H,
class Info>
107 hash.Update(m_passphrase, m_passphrase.size());
108 time_t t=time(NULLPTR);
109 hash.Update((
byte *)&t,
sizeof(t));
111 hash.Update((
byte *)&c,
sizeof(c));
115 hash.Update(m_passphrase, m_passphrase.size());
116 hash.Update(salt, SALTLENGTH);
117 hash.Final(keyCheck);
119 AttachedTransformation()->Put(salt, SALTLENGTH);
124 GenerateKeyIV<BC,H,Info>(m_passphrase, m_passphrase.size(), salt, SALTLENGTH, ITERATIONS, key, IV);
126 m_cipher.SetKeyWithIV(key, key.size(), IV);
129 m_filter->Put(keyCheck, BLOCKSIZE);
132template <
class BC,
class H,
class Info>
135 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
136 m_filter->MessageEnd();
141template <
class BC,
class H,
class Info>
144 , m_state(WAITING_FOR_KEYCHECK)
145 , m_passphrase((const
byte *)p, strlen(p))
146 , m_throwException(throwException)
148 CRYPTOPP_COMPILE_ASSERT((
int)SALTLENGTH <= (
int)DIGESTSIZE);
149 CRYPTOPP_COMPILE_ASSERT((
int)BLOCKSIZE <= (
int)DIGESTSIZE);
152template <
class BC,
class H,
class Info>
155 , m_state(WAITING_FOR_KEYCHECK)
156 , m_passphrase(passphrase, passphraseLength)
157 , m_throwException(throwException)
159 CRYPTOPP_COMPILE_ASSERT((
int)SALTLENGTH <= (
int)DIGESTSIZE);
160 CRYPTOPP_COMPILE_ASSERT((
int)BLOCKSIZE <= (
int)DIGESTSIZE);
163template <
class BC,
class H,
class Info>
166 CheckKey(inString, inString+SALTLENGTH);
169template <
class BC,
class H,
class Info>
172 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
173 if (m_filter.get() == NULLPTR)
176 if (m_throwException)
181 m_filter->MessageEnd();
182 m_state = WAITING_FOR_KEYCHECK;
186template <
class BC,
class H,
class Info>
192 hash.Update(m_passphrase, m_passphrase.size());
193 hash.Update(salt, SALTLENGTH);
198 GenerateKeyIV<BC,H,Info>(m_passphrase, m_passphrase.size(), salt, SALTLENGTH, ITERATIONS, key, IV);
200 m_cipher.SetKeyWithIV(key, key.size(), IV);
203 decryptor->Put(keyCheck, BLOCKSIZE);
204 decryptor->ForceNextPut();
205 decryptor->Get(check+
EnumToInt(BLOCKSIZE), BLOCKSIZE);
207 SetFilter(decryptor.release());
209 if (!VerifyBufsEqual(check, check+
EnumToInt(BLOCKSIZE), BLOCKSIZE))
212 if (m_throwException)
221template <
class H,
class MAC>
222static MAC* NewDataEncryptorMAC(
const byte *passphrase,
size_t passphraseLength)
224 size_t macKeyLength = MAC::StaticGetValidKeyLength(16);
227 Mash<H>(passphrase, passphraseLength, macKey, macKeyLength, 1);
228 return new MAC(macKey, macKeyLength);
231template <
class BC,
class H,
class MAC,
class Info>
234 , m_mac(NewDataEncryptorMAC<H,MAC>((const
byte *)passphrase, strlen(passphrase)))
239template <
class BC,
class H,
class MAC,
class Info>
242 , m_mac(NewDataEncryptorMAC<H,MAC>(passphrase, passphraseLength))
247template <
class BC,
class H,
class MAC,
class Info>
250 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
251 m_filter->MessageEnd();
256template <
class BC,
class H,
class MAC,
class Info>
259 , m_mac(NewDataEncryptorMAC<H,MAC>((const
byte *)passphrase, strlen(passphrase)))
260 , m_throwException(throwException)
265template <
class BC,
class H,
class MAC,
class Info>
268 , m_mac(NewDataEncryptorMAC<H,MAC>(passphrase, passphraseLength))
269 , m_throwException(throwException)
274template <
class BC,
class H,
class MAC,
class Info>
280template <
class BC,
class H,
class MAC,
class Info>
283 return m_hashVerifier->GetLastResult();
286template <
class BC,
class H,
class MAC,
class Info>
289 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
290 m_filter->MessageEnd();
291 if (m_throwException && !CheckLastMAC())
Password-based Decryptor.
DataDecryptor(const char *passphrase, BufferedTransformation *attachment=NULLPTR, bool throwException=true)
Constructs a DataDecryptor.
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Password-based decryptor with MAC.
DataDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULLPTR, bool throwException=true)
Constructs a DataDecryptor.
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Password-based Encryptor.
DataEncryptor(const char *passphrase, BufferedTransformation *attachment=NULLPTR)
Construct a DataEncryptor.
void LastPut(const byte *inString, size_t length)
Input the last block of data.
Password-based encryptor with MAC.
void LastPut(const byte *inString, size_t length)
Input the last block of data.
DataEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULLPTR)
Constructs a DataEncryptorWithMAC.
Filter wrapper for HashTransformation.
Filter wrapper for HashTransformation.
@ PUT_MESSAGE
The message should be passed to an attached transformation.
An invalid argument was detected.
Exception thrown when a bad key is encountered in DefaultDecryptorWithMAC and LegacyDecryptorWithMAC.
Exception thrown when an incorrect MAC is encountered in DefaultDecryptorWithMAC and LegacyDecryptorW...
Base class for Filter classes that are proxies for a chain of other filters.
void SetFilter(Filter *filter)
Sets the OutputProxy filter.
Pointer that overloads operator ->
Library configuration file.
unsigned char byte
8-bit unsigned datatype
Abstract base classes that provide a uniform interface to this library.
Classes for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC.
Implementation of BufferedTransformation's attachment interface.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
#define EnumToInt(v)
Integer value.
Classes for an unlimited queue to store bytes.
Classes for automatic resource management.
Algorithm information for password-based encryptors and decryptors.