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;
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);
191 decryptor->
Get(check+BLOCKSIZE, BLOCKSIZE);
193 SetFilter(decryptor.release());
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)
251 SetFilter(
new DefaultDecryptor(passphrase, passphraseLength, m_hashVerifier=
new HashVerifier(*m_mac, NULL, HashVerifier::PUT_MESSAGE), 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())
An invalid argument was detected.
Base class for Filter classes that are proxies for a chain of other filters.
Password-Based Encryptor using TripleDES.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
Abstract base classes that provide a uniform interface to this library.
size_type size() const
Provides the count of elements in the SecBlock.
Classes for automatic resource management.
Classes for DefaultEncryptor, DefaultDecryptor, DefaultEncryptorWithMAC and DefaultDecryptorWithMAC.
Library configuration file.
SecByteBlock is a SecBlock<byte> typedef.
static size_t StaticGetValidKeyLength(size_t keylength)
Provides a valid key length for the cipher provided by a static function.
Excpetion thrown when an incorrect MAC is encountered.
Password-Based Decryptor using TripleDES.
Pointer that overloads operator→
DefaultDecryptor(const char *passphrase, BufferedTransformation *attachment=NULL, bool throwException=true)
Constructs a DefaultDecryptor.
HMAC< DefaultHashModule > DefaultMAC
Default HMAC for use withDefaultEncryptorWithMAC and DefaultDecryptorWithMAC.
Classes for an unlimited queue to store bytes.
Filter Wrapper for HashTransformation.
Filter Wrapper for HashTransformation.
BufferedTransformation * AttachedTransformation()
Retrieve attached transformation.
Implementation of BufferedTransformation's attachment interface in cryptlib.h.
const char * IV()
ConstByteArrayParameter, also accepts const byte * for backwards compatibility.
DefaultEncryptor(const char *passphrase, BufferedTransformation *attachment=NULL)
Construct a DefaultEncryptor.
bool VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count)
Performs a near constant-time comparison of two equally sized buffers.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Crypto++ library namespace.
DefaultDecryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULL, bool throwException=true)
Constructs a DefaultDecryptor.
DefaultEncryptorWithMAC(const char *passphrase, BufferedTransformation *attachment=NULL)
Constructs a DefaultEncryptorWithMAC.