6 #if CRYPTOPP_MSC_VERSION
7 # pragma warning(disable: 4127 4189)
28 static 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);
65 template <
class BC,
class H,
class Info>
66 static 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);
84 template <
class BC,
class H,
class Info>
86 :
ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase((const
byte *)passphrase, strlen(passphrase))
92 template <
class BC,
class H,
class Info>
94 :
ProxyFilter(NULLPTR, 0, 0, attachment), m_passphrase(passphrase, passphraseLength)
100 template <
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);
132 template <
class BC,
class H,
class Info>
135 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
136 m_filter->MessageEnd();
141 template <
class BC,
class H,
class Info>
144 , m_state(WAITING_FOR_KEYCHECK)
145 , m_passphrase((const
byte *)p, strlen(p))
146 , m_throwException(throwException)
152 template <
class BC,
class H,
class Info>
155 , m_state(WAITING_FOR_KEYCHECK)
156 , m_passphrase(passphrase, passphraseLength)
157 , m_throwException(throwException)
163 template <
class BC,
class H,
class Info>
166 CheckKey(inString, inString+SALTLENGTH);
169 template <
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;
186 template <
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());
212 if (m_throwException)
221 template <
class H,
class MAC>
222 static 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);
231 template <
class BC,
class H,
class MAC,
class Info>
234 , m_mac(NewDataEncryptorMAC<H,MAC>((const
byte *)passphrase, strlen(passphrase)))
239 template <
class BC,
class H,
class MAC,
class Info>
242 , m_mac(NewDataEncryptorMAC<H,MAC>(passphrase, passphraseLength))
247 template <
class BC,
class H,
class MAC,
class Info>
250 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
251 m_filter->MessageEnd();
256 template <
class BC,
class H,
class MAC,
class Info>
259 , m_mac(NewDataEncryptorMAC<H,MAC>((const
byte *)passphrase, strlen(passphrase)))
260 , m_throwException(throwException)
265 template <
class BC,
class H,
class MAC,
class Info>
268 , m_mac(NewDataEncryptorMAC<H,MAC>(passphrase, passphraseLength))
269 , m_throwException(throwException)
274 template <
class BC,
class H,
class MAC,
class Info>
280 template <
class BC,
class H,
class MAC,
class Info>
283 return m_hashVerifier->GetLastResult();
286 template <
class BC,
class H,
class MAC,
class Info>
289 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
290 m_filter->MessageEnd();
291 if (m_throwException && !CheckLastMAC())