8 #ifndef CRYPTOPP_PWDBASED_H 9 #define CRYPTOPP_PWDBASED_H 30 static std::string StaticAlgorithmName () {
31 const std::string name(std::string(
"PBKDF1(") +
32 std::string(T::StaticAlgorithmName()) + std::string(
")"));
38 return StaticAlgorithmName();
42 size_t MaxDerivedKeyLength()
const {
43 return static_cast<size_t>(T::DIGESTSIZE);
50 virtual size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
73 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;
85 if (keylength > MaxDerivedLength())
86 return MaxDerivedLength();
92 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 101 double timeInSeconds = 0.0f;
102 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
107 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
111 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 117 CRYPTOPP_UNUSED(purpose);
119 ThrowIfInvalidDerivedLength(derivedLen);
122 if (!iterations) { iterations = 1; }
125 hash.Update(secret, secretLen);
126 hash.Update(salt, saltLen);
137 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
138 hash.CalculateDigest(buffer, buffer, buffer.size());
140 memcpy(derived, buffer, derivedLen);
154 static std::string StaticAlgorithmName () {
155 const std::string name(std::string(
"PBKDF2_HMAC(") +
156 std::string(T::StaticAlgorithmName()) + std::string(
")"));
162 return StaticAlgorithmName();
167 size_t MaxDerivedKeyLength()
const {
175 size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
196 size_t DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
197 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds=0)
const;
209 if (keylength > MaxDerivedLength())
210 return MaxDerivedLength();
216 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 225 double timeInSeconds = 0.0f;
226 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
231 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
235 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 241 CRYPTOPP_UNUSED(purpose);
243 ThrowIfInvalidDerivedLength(derivedLen);
246 if (!iterations) { iterations = 1; }
248 HMAC<T> hmac(secret, secretLen);
253 while (derivedLen > 0)
255 hmac.
Update(salt, saltLen);
259 byte b = byte(i >> ((3-j)*8));
264 #if CRYPTOPP_MSC_VERSION 265 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
266 memcpy_s(derived, segmentLen, buffer, segmentLen);
268 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
269 memcpy(derived, buffer, segmentLen);
274 timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
278 for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
281 xorbuf(derived, buffer, segmentLen);
290 derived += segmentLen;
291 derivedLen -= segmentLen;
308 static std::string StaticAlgorithmName () {
309 const std::string name(std::string(
"PBKDF_PKCS12(") +
310 std::string(T::StaticAlgorithmName()) + std::string(
")"));
316 return StaticAlgorithmName();
320 size_t MaxDerivedKeyLength()
const {
321 return static_cast<size_t>(-1);
328 size_t DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
349 size_t DeriveKey(byte *derived,
size_t derivedLen, byte purpose,
const byte *secret,
size_t secretLen,
350 const byte *salt,
size_t saltLen,
unsigned int iterations,
double timeInSeconds)
const;
362 if (keylength > MaxDerivedLength())
363 return MaxDerivedLength();
369 const byte *secret,
size_t secretLen,
const NameValuePairs& params)
const 378 double timeInSeconds = 0.0f;
379 (void)params.
GetValue(
"TimeInSeconds", timeInSeconds);
385 return DeriveKey(derived, derivedLen, purpose, secret, secretLen, salt.
begin(), salt.
size(), iterations, timeInSeconds);
389 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 396 ThrowIfInvalidDerivedLength(derivedLen);
399 if (!iterations) { iterations = 1; }
401 const size_t v = T::BLOCKSIZE;
405 byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
407 memset(D, purpose, DLen);
409 for (i=0; i<SLen; i++)
410 S[i] = salt[i % saltLen];
411 for (i=0; i<PLen; i++)
412 P[i] = secret[i % secretLen];
418 while (derivedLen > 0)
420 hash.CalculateDigest(Ai, buffer, buffer.
size());
424 timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
428 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
429 hash.CalculateDigest(Ai, Ai, Ai.size());
433 iterations = (
unsigned int)i;
437 for (i=0; i<B.
size(); i++)
438 B[i] = Ai[i % Ai.size()];
442 for (i=0; i<ILen; i+=v)
443 (
Integer(I+i, v) + B1).Encode(I+i, v);
445 #if CRYPTOPP_MSC_VERSION 446 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
447 memcpy_s(derived, segmentLen, Ai, segmentLen);
449 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
450 std::memcpy(derived, Ai, segmentLen);
453 derived += segmentLen;
454 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.
Classes for 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.
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.
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.