6 #ifndef CRYPTOPP_PWDBASED_H 7 #define CRYPTOPP_PWDBASED_H 20 #ifndef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY_562 21 virtual ~PasswordBasedKeyDerivationFunction() {}
26 virtual size_t MaxDerivedKeyLength()
const =0;
30 virtual bool UsesPurposeByte()
const =0;
46 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;
58 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;
69 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;
83 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 85 CRYPTOPP_UNUSED(purpose);
87 assert(iterations > 0 || timeInSeconds > 0);
93 hash.Update(password, passwordLen);
94 hash.Update(salt, saltLen);
105 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
106 hash.CalculateDigest(buffer, buffer, buffer.size());
108 memcpy(derived, buffer, derivedLen);
113 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 115 CRYPTOPP_UNUSED(purpose);
117 assert(iterations > 0 || timeInSeconds > 0);
122 HMAC<T> hmac(password, passwordLen);
127 while (derivedLen > 0)
129 hmac.
Update(salt, saltLen);
133 byte b = byte(i >> ((3-j)*8));
138 #if CRYPTOPP_MSC_VERSION 139 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
140 memcpy_s(derived, segmentLen, buffer, segmentLen);
142 const size_t segmentLen =
STDMIN(derivedLen, buffer.size());
143 memcpy(derived, buffer, segmentLen);
148 timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
152 for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
155 xorbuf(derived, buffer, segmentLen);
164 derived += segmentLen;
165 derivedLen -= segmentLen;
180 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;
184 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 187 assert(iterations > 0 || timeInSeconds > 0);
192 const size_t v = T::BLOCKSIZE;
196 byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
198 memset(D, purpose, DLen);
200 for (i=0; i<SLen; i++)
201 S[i] = salt[i % saltLen];
202 for (i=0; i<PLen; i++)
203 P[i] = password[i % passwordLen];
210 while (derivedLen > 0)
212 hash.CalculateDigest(Ai, buffer, buffer.
size());
216 timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
220 for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
221 hash.CalculateDigest(Ai, Ai, Ai.size());
225 iterations = (
unsigned int)i;
229 for (i=0; i<B.
size(); i++)
230 B[i] = Ai[i % Ai.size()];
234 for (i=0; i<ILen; i+=v)
235 (
Integer(I+i, v) + B1).Encode(I+i, v);
237 #if CRYPTOPP_MSC_VERSION 238 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
239 memcpy_s(derived, segmentLen, Ai, segmentLen);
241 const size_t segmentLen =
STDMIN(derivedLen, Ai.size());
242 std::memcpy(derived, Ai, segmentLen);
245 derived += segmentLen;
246 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.
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.