6 #if CRYPTOPP_MSC_VERSION
7 # pragma warning(disable: 4127 4189)
21 static const unsigned int MASH_ITERATIONS = 200;
22 static const unsigned int SALTLENGTH = 8;
23 static const unsigned int BLOCKSIZE = DefaultBlockCipher::Encryption::BLOCKSIZE;
24 static const unsigned int KEYLENGTH = DefaultBlockCipher::Encryption::DEFAULT_KEYLENGTH;
32 static void Mash(
const byte *in,
size_t inLen,
byte *out,
size_t outLen,
int iterations)
44 for(i=0; i<outLen; i+=DefaultHashModule::DIGESTSIZE)
46 b[0] = (byte) (i >> 8);
49 hash.Update(in, inLen);
53 while (iterations-- > 1)
55 memcpy(buf, outBuf, bufSize);
56 for (i=0; i<bufSize; i+=DefaultHashModule::DIGESTSIZE)
58 b[0] = (byte) (i >> 8);
61 hash.Update(buf, bufSize);
66 memcpy(out, outBuf, outLen);
69 static void GenerateKeyIV(
const byte *passphrase,
size_t passphraseLength,
const byte *salt,
size_t saltLength,
byte *key,
byte *
IV)
72 memcpy(temp, passphrase, passphraseLength);
73 memcpy(temp+passphraseLength, salt, saltLength);
75 Mash(temp, passphraseLength + saltLength, keyIV, KEYLENGTH+BLOCKSIZE, MASH_ITERATIONS);
76 memcpy(key, keyIV, KEYLENGTH);
77 memcpy(
IV, keyIV+KEYLENGTH, BLOCKSIZE);
83 :
ProxyFilter(NULL, 0, 0, attachment), m_passphrase((const byte *)passphrase, strlen(passphrase))
88 :
ProxyFilter(NULL, 0, 0, attachment), m_passphrase(passphrase, passphraseLength)
93 void DefaultEncryptor::FirstPut(
const byte *)
96 CRYPTOPP_COMPILE_ASSERT_INSTANCE(SALTLENGTH <= DefaultHashModule::DIGESTSIZE, 1);
97 CRYPTOPP_COMPILE_ASSERT_INSTANCE(BLOCKSIZE <= DefaultHashModule::DIGESTSIZE, 2);
99 SecByteBlock salt(DefaultHashModule::DIGESTSIZE), keyCheck(DefaultHashModule::DIGESTSIZE);
103 hash.Update(m_passphrase, m_passphrase.
size());
105 hash.Update((
byte *)&t,
sizeof(t));
107 hash.Update((
byte *)&c,
sizeof(c));
111 hash.Update(m_passphrase, m_passphrase.
size());
112 hash.Update(salt, SALTLENGTH);
113 hash.
Final(keyCheck);
120 GenerateKeyIV(m_passphrase, m_passphrase.
size(), salt, SALTLENGTH, key,
IV);
122 m_cipher.SetKeyWithIV(key, key.size(),
IV);
125 m_filter->Put(keyCheck, BLOCKSIZE);
128 void DefaultEncryptor::LastPut(
const byte *inString,
size_t length)
130 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
131 m_filter->MessageEnd();
137 :
ProxyFilter(NULL, SALTLENGTH+BLOCKSIZE, 0, attachment)
138 , m_state(WAITING_FOR_KEYCHECK)
139 , m_passphrase((const byte *)p, strlen(p))
140 , m_throwException(throwException)
145 :
ProxyFilter(NULL, SALTLENGTH+BLOCKSIZE, 0, attachment)
146 , m_state(WAITING_FOR_KEYCHECK)
147 , m_passphrase(passphrase, passphraseLength)
148 , m_throwException(throwException)
152 void DefaultDecryptor::FirstPut(
const byte *inString)
154 CheckKey(inString, inString+SALTLENGTH);
157 void DefaultDecryptor::LastPut(
const byte *inString,
size_t length)
159 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
160 if (m_filter.get() == NULL)
163 if (m_throwException)
168 m_filter->MessageEnd();
169 m_state = WAITING_FOR_KEYCHECK;
173 void DefaultDecryptor::CheckKey(
const byte *salt,
const byte *keyCheck)
175 SecByteBlock check(
STDMAX((
unsigned int)2*BLOCKSIZE, (
unsigned int)DefaultHashModule::DIGESTSIZE));
178 hash.Update(m_passphrase, m_passphrase.
size());
179 hash.Update(salt, SALTLENGTH);
184 GenerateKeyIV(m_passphrase, m_passphrase.
size(), salt, SALTLENGTH, key,
IV);
186 m_cipher.SetKeyWithIV(key, key.size(),
IV);
189 decryptor->Put(keyCheck, BLOCKSIZE);
190 decryptor->ForceNextPut();
191 decryptor->Get(check+BLOCKSIZE, BLOCKSIZE);
198 if (m_throwException)
207 static DefaultMAC * NewDefaultEncryptorMAC(
const byte *passphrase,
size_t passphraseLength)
212 Mash(passphrase, passphraseLength, macKey, macKeyLength, 1);
218 , m_mac(NewDefaultEncryptorMAC((const byte *)passphrase, strlen(passphrase)))
225 , m_mac(NewDefaultEncryptorMAC(passphrase, passphraseLength))
230 void DefaultEncryptorWithMAC::LastPut(
const byte *inString,
size_t length)
232 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
233 m_filter->MessageEnd();
240 , m_mac(NewDefaultEncryptorMAC((const byte *)passphrase, strlen(passphrase)))
241 , m_throwException(throwException)
248 , m_mac(NewDefaultEncryptorMAC(passphrase, passphraseLength))
249 , m_throwException(throwException)
254 DefaultDecryptor::State DefaultDecryptorWithMAC::CurrentState()
const
256 return static_cast<const DefaultDecryptor *
>(m_filter.get())->CurrentState();
259 bool DefaultDecryptorWithMAC::CheckLastMAC()
const
261 return m_hashVerifier->GetLastResult();
264 void DefaultDecryptorWithMAC::LastPut(
const byte *inString,
size_t length)
266 CRYPTOPP_UNUSED(inString); CRYPTOPP_UNUSED(length);
267 m_filter->MessageEnd();
268 if (m_throwException && !CheckLastMAC())