6 #ifndef CRYPTOPP_PWDBASED_H 7 #define CRYPTOPP_PWDBASED_H 24 virtual size_t MaxDerivedKeyLength()
const =0;
28 virtual bool UsesPurposeByte()
const =0;
44 virtual unsigned int DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const =0;
56 unsigned int DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
67 unsigned int DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
81 unsigned int PKCS5_PBKDF1<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 83 CRYPTOPP_UNUSED(purpose);
91 hash.Update(password, passwordLen);
92 hash.Update(salt, saltLen);
103 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
104 hash.CalculateDigest(buffer, buffer, buffer.size());
106 memcpy(derived, buffer, derivedLen);
111 unsigned int PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 113 CRYPTOPP_UNUSED(purpose);
120 HMAC<T> hmac(password, passwordLen);
125 while (derivedLen > 0)
127 hmac.
Update(salt, saltLen);
131 byte b = byte(i >> ((3-j)*8));
136 #if CRYPTOPP_MSC_VERSION 137 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
138 memcpy_s(derived, segmentLen, buffer, segmentLen);
140 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
141 memcpy(derived, buffer, segmentLen);
146 timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
150 for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
153 xorbuf(derived, buffer, segmentLen);
162 derived += segmentLen;
163 derivedLen -= segmentLen;
178 unsigned int DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const;
182 unsigned int PKCS12_PBKDF<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *password,
size_t passwordLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 190 const size_t v = T::BLOCKSIZE;
194 byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
196 memset(D, purpose, DLen);
198 for (i=0; i<SLen; i++)
199 S[i] = salt[i % saltLen];
200 for (i=0; i<PLen; i++)
201 P[i] = password[i % passwordLen];
208 while (derivedLen > 0)
210 hash.CalculateDigest(Ai, buffer, buffer.
size());
214 timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
218 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
219 hash.CalculateDigest(Ai, Ai, Ai.size());
223 iterations = (
unsigned int)i;
227 for (i=0; i<B.
size(); i++)
228 B[i] = Ai[i % Ai.size()];
232 for (i=0; i<ILen; i+=v)
233 (
Integer(I+i, v) + B1).Encode(I+i, v);
235 #if CRYPTOPP_MSC_VERSION 236 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
237 memcpy_s(derived, segmentLen, Ai, segmentLen);
239 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
240 std::memcpy(derived, Ai, segmentLen);
243 derived += segmentLen;
244 derivedLen -= segmentLen;
unsigned int DeriveKey(byte *derived, size_t derivedLen, byte purpose, const byte *password, size_t passwordLen, const byte *salt, size_t saltLen, unsigned int iterations, double timeInSeconds=0) const
Derive key from the password.
Abstract base class for password based key derivation function.
unsigned int DigestSize() const
Provides the digest size of the hash.
Abstract base classes that provide a uniform interface to this library.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
bool UsesPurposeByte() const
Determines if the derivation function uses the purpose byte.
size_t MaxDerivedKeyLength() const
Provides the maximum derived key length.
bool UsesPurposeByte() const
Determines if the derivation function uses the purpose byte.
Classes for HMAC message authentication codes.
PBKDF from PKCS #12, appendix B.
size_t MaxDerivedKeyLength() const
Provides the maximum derived key length.
Multiple precision integer with arithmetic operations.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
bool UsesPurposeByte() const
Determines if the derivation function uses the purpose byte.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
unsigned int DeriveKey(byte *derived, size_t derivedLen, byte purpose, const byte *password, size_t passwordLen, const byte *salt, size_t saltLen, unsigned int iterations, double timeInSeconds=0) const
Derive key from the password.
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Multiple precision integer with arithmetic operations.
T1 RoundUpToMultipleOf(const T1 &n, const T2 &m)
Rounds a value up to a multiple of a second value.
Crypto++ library namespace.
unsigned int DeriveKey(byte *derived, size_t derivedLen, byte purpose, const byte *password, size_t passwordLen, const byte *salt, size_t saltLen, unsigned int iterations, double timeInSeconds) const
Derive key from the password.
Measure CPU time spent executing instructions of this thread (if supported by OS) ...
size_t MaxDerivedKeyLength() const
Provides the maximum derived key length.
size_type size() const
Provides the count of elements in the SecBlock.