8 #ifndef CRYPTOPP_PWDBASED_H 9 #define CRYPTOPP_PWDBASED_H 29 static std::string StaticAlgorithmName () {
30 const std::string name(std::string(
"PBKDF1(") +
31 std::string(T::StaticAlgorithmName()) + std::string(
")"));
37 return StaticAlgorithmName();
41 size_t MaxDerivedKeyLength()
const {
42 return static_cast<size_t>(T::DIGESTSIZE);
49 virtual size_t DeriveKey(
byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
72 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;
84 if (keylength > MaxDerivedLength())
85 return MaxDerivedLength();
91 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 100 double timeInSeconds = 0.0f;
101 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
106 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
110 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 116 CRYPTOPP_UNUSED(purpose);
118 ThrowIfInvalidDerivedLength(derivedLen);
121 if (!iterations) { iterations = 1; }
124 hash.Update(secret, secretLen);
125 hash.Update(salt, saltLen);
136 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
137 hash.CalculateDigest(buffer, buffer, buffer.size());
139 memcpy(derived, buffer, derivedLen);
153 static std::string StaticAlgorithmName () {
154 const std::string name(std::string(
"PBKDF2_HMAC(") +
155 std::string(T::StaticAlgorithmName()) + std::string(
")"));
161 return StaticAlgorithmName();
166 size_t MaxDerivedKeyLength()
const {
174 size_t DeriveKey(
byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
195 size_t DeriveKey(
byte *derived,
size_t derivedLen,
byte purpose,
const byte *secret,
size_t secretLen,
196 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
208 if (keylength > MaxDerivedLength())
209 return MaxDerivedLength();
215 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 224 double timeInSeconds = 0.0f;
225 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
230 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
234 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 240 CRYPTOPP_UNUSED(purpose);
242 ThrowIfInvalidDerivedLength(derivedLen);
245 if (!iterations) { iterations = 1; }
247 HMAC<T> hmac(secret, secretLen);
252 while (derivedLen > 0)
254 hmac.
Update(salt, saltLen);
258 byte b = byte(i >> ((3-j)*8));
263 #if CRYPTOPP_MSC_VERSION 264 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
265 memcpy_s(derived, segmentLen, buffer, segmentLen);
267 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
268 memcpy(derived, buffer, segmentLen);
273 timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
277 for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
280 xorbuf(derived, buffer, segmentLen);
289 derived += segmentLen;
290 derivedLen -= segmentLen;
307 static std::string StaticAlgorithmName () {
308 const std::string name(std::string(
"PBKDF_PKCS12(") +
309 std::string(T::StaticAlgorithmName()) + std::string(
")"));
315 return StaticAlgorithmName();
319 size_t MaxDerivedKeyLength()
const {
320 return static_cast<size_t>(-1);
327 size_t DeriveKey(
byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
348 size_t DeriveKey(
byte *derived,
size_t derivedLen,
byte purpose,
const byte *secret,
size_t secretLen,
349 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const;
361 if (keylength > MaxDerivedLength())
362 return MaxDerivedLength();
368 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 377 double timeInSeconds = 0.0f;
378 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
384 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
388 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 395 ThrowIfInvalidDerivedLength(derivedLen);
398 if (!iterations) { iterations = 1; }
400 const size_t v = T::BLOCKSIZE;
404 byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
406 memset(D, purpose, DLen);
408 for (i=0; i<SLen; i++)
409 S[i] = salt[i % saltLen];
410 for (i=0; i<PLen; i++)
411 P[i] = secret[i % secretLen];
417 while (derivedLen > 0)
419 hash.CalculateDigest(Ai, buffer, buffer.
size());
423 timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
427 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
428 hash.CalculateDigest(Ai, Ai, Ai.size());
432 iterations = (
unsigned int)i;
436 for (i=0; i<B.
size(); i++)
437 B[i] = Ai[i % Ai.size()];
441 for (i=0; i<ILen; i+=v)
442 (
Integer(I+i, v) + B1).Encode(I+i, v);
444 #if CRYPTOPP_MSC_VERSION 445 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
446 memcpy_s(derived, segmentLen, Ai, segmentLen);
448 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
449 std::memcpy(derived, Ai, segmentLen);
452 derived += segmentLen;
453 derivedLen -= segmentLen;
Used to pass byte array input as part of a NameValuePairs object.
int GetIntValueWithDefault(const char *name, int defaultValue) const
Get a named value with type int, with default.
Standard names for retrieving values by name when working with NameValuePairs.
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 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.
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.
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.
const NameValuePairs g_nullNameValuePairs
An empty set of name-value pairs.
std::string AlgorithmName() const
Provides the name of this algorithm.
void xorbuf(byte *buf, const byte *mask, size_t count)
Performs an XOR of a buffer with a mask.
Interface for password based key derivation functions.
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 (if supported by OS)
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 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.