8 #ifndef CRYPTOPP_PWDBASED_H 9 #define CRYPTOPP_PWDBASED_H 34 static std::string StaticAlgorithmName () {
35 const std::string name(std::string(
"PBKDF1(") +
36 std::string(T::StaticAlgorithmName()) + std::string(
")"));
42 return StaticAlgorithmName();
47 return static_cast<size_t>(T::DIGESTSIZE);
54 virtual size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
77 size_t DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
96 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 105 double timeInSeconds = 0.0f;
106 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
111 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
115 size_t PKCS5_PBKDF1<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 121 CRYPTOPP_UNUSED(purpose);
123 ThrowIfInvalidDerivedKeyLength(derivedLen);
126 if (!iterations) { iterations = 1; }
129 hash.Update(secret, secretLen);
130 hash.Update(salt, saltLen);
141 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.
ElapsedTimeAsDouble() < timeInSeconds)); i++)
142 hash.CalculateDigest(buffer, buffer, buffer.size());
145 std::memcpy(derived, buffer, derivedLen);
163 static std::string StaticAlgorithmName () {
164 const std::string name(std::string(
"PBKDF2_HMAC(") +
165 std::string(T::StaticAlgorithmName()) + std::string(
")"));
171 return StaticAlgorithmName();
184 size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
205 size_t DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
206 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
225 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 234 double timeInSeconds = 0.0f;
235 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
240 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
244 size_t PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 250 CRYPTOPP_UNUSED(purpose);
252 ThrowIfInvalidDerivedKeyLength(derivedLen);
255 if (!iterations) { iterations = 1; }
258 HMAC<T> hmac(secret, secretLen);
266 while (derivedLen > 0)
268 hmac.
Update(salt, saltLen);
272 byte b = byte(i >> ((3-j)*8));
277 #if CRYPTOPP_MSC_VERSION 278 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
279 memcpy_s(derived, segmentLen, buffer, segmentLen);
281 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
282 std::memcpy(derived, buffer, segmentLen);
287 timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
291 for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
294 xorbuf(derived, buffer, segmentLen);
303 derived += segmentLen;
304 derivedLen -= segmentLen;
325 static std::string StaticAlgorithmName () {
326 const std::string name(std::string(
"PBKDF_PKCS12(") +
327 std::string(T::StaticAlgorithmName()) + std::string(
")"));
333 return StaticAlgorithmName();
338 return static_cast<size_t>(-1);
345 size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
366 size_t DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
367 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const;
386 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 395 double timeInSeconds = 0.0f;
396 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
402 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
406 size_t PKCS12_PBKDF<T>::DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const 413 ThrowIfInvalidDerivedKeyLength(derivedLen);
416 if (!iterations) { iterations = 1; }
418 const size_t v = T::BLOCKSIZE;
422 byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
425 std::memset(D, purpose, DLen);
428 for (i=0; i<SLen; i++)
429 S[i] = salt[i % saltLen];
430 for (i=0; i<PLen; i++)
431 P[i] = secret[i % secretLen];
437 while (derivedLen > 0)
439 hash.CalculateDigest(Ai, buffer, buffer.
size());
443 timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
447 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.
ElapsedTimeAsDouble() < timeInSeconds)); i++)
448 hash.CalculateDigest(Ai, Ai, Ai.size());
452 iterations = (
unsigned int)i;
456 for (i=0; i<B.
size(); i++)
457 B[i] = Ai[i % Ai.size()];
461 for (i=0; i<ILen; i+=v)
462 (
Integer(I+i, v) + B1).Encode(I+i, v);
464 #if CRYPTOPP_MSC_VERSION 465 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
466 memcpy_s(derived, segmentLen, Ai, segmentLen);
468 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
469 std::memcpy(derived, Ai, segmentLen);
472 derived += segmentLen;
473 derivedLen -= segmentLen;
Used to pass byte array input as part of a NameValuePairs object.
Standard names for retrieving values by name when working with NameValuePairs.
An invalid argument was detected.
Classes for working with NameValuePairs.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
size_t size() const
Length of the memory block.
std::string AlgorithmName() const
Provides the name of this algorithm.
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()
size_t GetValidDerivedLength(size_t keylength) const
Returns a valid key length for the derivation function.
size_t MaxDerivedKeyLength() const
Determine maximum number of bytes.
void StartTimer()
Start the timer.
size_t GetValidDerivedLength(size_t keylength) const
Returns a valid key length for the derivation function.
size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen, const NameValuePairs ¶ms=g_nullNameValuePairs) const
Derive a key from a seed.
Classes for HMAC message authentication codes.
PBKDF from PKCS #12, appendix B.
const byte * begin() const
Pointer to the first byte in the memory block.
const char * Salt()
ConstByteArrayParameter.
size_t MaxDerivedKeyLength() const
Determine maximum number of bytes.
Multiple precision integer with arithmetic operations.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen, const NameValuePairs ¶ms=g_nullNameValuePairs) const
Derive a key from a seed.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
std::string AlgorithmName() const
Provides the name of this algorithm.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
Interface for all crypto algorithms.
std::string AlgorithmName() const
Provides the name of this algorithm.
double ElapsedTimeAsDouble()
Retrieve the elapsed time.
Interface for password based key derivation functions.
CRYPTOPP_DLL void CRYPTOPP_API 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.
bool GetValue(const char *name, T &value) const
Get a named value.
Measure CPU time spent executing instructions of this thread.
virtual size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen, const NameValuePairs ¶ms=g_nullNameValuePairs) const
Derive a key from a seed.
size_t MaxDerivedKeyLength() const
Determine maximum number of bytes.
size_t GetValidDerivedLength(size_t keylength) const
Returns a valid key length for the derivation function.
size_type size() const
Provides the count of elements in the SecBlock.
Interface for retrieving values given their names.