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;
89 if (keylength > MaxDerivedKeyLength())
90 return MaxDerivedKeyLength();
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);
115size_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;
218 if (keylength > MaxDerivedKeyLength())
219 return MaxDerivedKeyLength();
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);
244size_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;
379 if (keylength > MaxDerivedKeyLength())
380 return MaxDerivedKeyLength();
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);
406size_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)
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;
Classes for working with NameValuePairs.
Standard names for retrieving values by name when working with NameValuePairs.
Interface for all crypto algorithms.
Used to pass byte array input as part of a NameValuePairs object.
const byte * begin() const
Pointer to the first byte in the memory block.
size_t size() const
Length of the memory block.
unsigned int DigestSize() const
Provides the digest size of the hash.
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Multiple precision integer with arithmetic operations.
void Encode(byte *output, size_t outputLen, Signedness sign=UNSIGNED) const
Encode in big-endian format.
An invalid argument was detected.
Interface for retrieving values given their names.
bool GetValue(const char *name, T &value) const
Get a named value.
CRYPTOPP_DLL int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
PBKDF from PKCS #12, appendix B.
size_t MaxDerivedKeyLength() const
Determine maximum number of bytes.
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.
std::string AlgorithmName() const
Provides the name of this algorithm.
size_t GetValidDerivedLength(size_t keylength) const
Returns a valid key length for the derivation function.
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.
std::string AlgorithmName() const
Provides the name of this algorithm.
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.
std::string AlgorithmName() const
Provides the name of this algorithm.
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.
size_type size() const
Provides the count of elements in the SecBlock.
Measure CPU time spent executing instructions of this thread.
double ElapsedTimeAsDouble()
Retrieve the elapsed time.
void StartTimer()
Start the timer.
unsigned char byte
8-bit unsigned datatype
Abstract base classes that provide a uniform interface to this library.
const NameValuePairs & g_nullNameValuePairs
An empty set of name-value pairs.
Classes for HMAC message authentication codes.
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.
void memcpy_s(void *dest, size_t sizeInBytes, const void *src, size_t count)
Bounds checking replacement for memcpy()
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
CRYPTOPP_DLL void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Crypto++ library namespace.
const char * Salt()
ConstByteArrayParameter.
Interface for password based key derivation functions.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.