ext-cryptopp/pwdbased.h

215 lines
6.3 KiB
C
Raw Normal View History

2002-10-04 17:31:41 +00:00
// pwdbased.h - written and placed in the public domain by Wei Dai
#ifndef CRYPTOPP_PWDBASED_H
#define CRYPTOPP_PWDBASED_H
#include "cryptlib.h"
#include "hmac.h"
2004-04-08 01:57:33 +00:00
#include "hrtimer.h"
#include "integer.h"
2002-10-04 17:31:41 +00:00
NAMESPACE_BEGIN(CryptoPP)
2004-04-08 01:57:33 +00:00
//! abstract base class for password based key derivation function
2002-10-04 17:31:41 +00:00
class PasswordBasedKeyDerivationFunction
{
public:
2005-07-12 04:23:32 +00:00
virtual size_t MaxDerivedKeyLength() const =0;
2004-04-08 01:57:33 +00:00
virtual bool UsesPurposeByte() const =0;
//! derive key from password
/*! If timeInSeconds != 0, will iterate until time elapsed, as measured by ThreadUserTimer
Returns actual iteration count, which is equal to iterations if timeInSeconds == 0, and not less than iterations otherwise. */
2005-07-12 04:23:32 +00:00
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;
2002-10-04 17:31:41 +00:00
};
//! PBKDF1 from PKCS #5, T should be a HashTransformation class
template <class T>
class PKCS5_PBKDF1 : public PasswordBasedKeyDerivationFunction
{
public:
2005-07-12 04:23:32 +00:00
size_t MaxDerivedKeyLength() const {return T::DIGESTSIZE;}
2004-04-08 01:57:33 +00:00
bool UsesPurposeByte() const {return false;}
2002-10-04 17:31:41 +00:00
// PKCS #5 says PBKDF1 should only take 8-byte salts. This implementation allows salts of any length.
2005-07-12 04:23:32 +00:00
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;
2002-10-04 17:31:41 +00:00
};
//! PBKDF2 from PKCS #5, T should be a HashTransformation class
template <class T>
class PKCS5_PBKDF2_HMAC : public PasswordBasedKeyDerivationFunction
{
public:
2005-07-12 04:23:32 +00:00
size_t MaxDerivedKeyLength() const {return 0xffffffffU;} // should multiply by T::DIGESTSIZE, but gets overflow that way
2004-04-08 01:57:33 +00:00
bool UsesPurposeByte() const {return false;}
2005-07-12 04:23:32 +00:00
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;
2002-10-04 17:31:41 +00:00
};
/*
class PBKDF2Params
{
public:
SecByteBlock m_salt;
unsigned int m_interationCount;
ASNOptional<ASNUnsignedWrapper<word32> > m_keyLength;
};
*/
template <class T>
2005-07-12 04:23:32 +00:00
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
2002-10-04 17:31:41 +00:00
{
2003-06-19 19:05:50 +00:00
assert(derivedLen <= MaxDerivedKeyLength());
2004-04-08 01:57:33 +00:00
assert(iterations > 0 || timeInSeconds > 0);
if (!iterations)
iterations = 1;
2002-10-04 17:31:41 +00:00
T hash;
hash.Update(password, passwordLen);
hash.Update(salt, saltLen);
SecByteBlock buffer(hash.DigestSize());
hash.Final(buffer);
2004-04-08 01:57:33 +00:00
unsigned int i;
ThreadUserTimer timer;
if (timeInSeconds)
timer.StartTimer();
2004-04-21 08:40:17 +00:00
for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
2002-10-04 17:31:41 +00:00
hash.CalculateDigest(buffer, buffer, buffer.size());
memcpy(derived, buffer, derivedLen);
2004-04-08 01:57:33 +00:00
return i;
2002-10-04 17:31:41 +00:00
}
template <class T>
2005-07-12 04:23:32 +00:00
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
2002-10-04 17:31:41 +00:00
{
assert(derivedLen <= MaxDerivedKeyLength());
2004-04-08 01:57:33 +00:00
assert(iterations > 0 || timeInSeconds > 0);
if (!iterations)
iterations = 1;
2002-10-04 17:31:41 +00:00
HMAC<T> hmac(password, passwordLen);
SecByteBlock buffer(hmac.DigestSize());
2004-04-08 01:57:33 +00:00
ThreadUserTimer timer;
2002-10-04 17:31:41 +00:00
unsigned int i=1;
while (derivedLen > 0)
{
hmac.Update(salt, saltLen);
unsigned int j;
for (j=0; j<4; j++)
{
2006-06-09 06:31:46 +00:00
byte b = byte(i >> ((3-j)*8));
2002-10-04 17:31:41 +00:00
hmac.Update(&b, 1);
}
hmac.Final(buffer);
2005-07-12 04:23:32 +00:00
size_t segmentLen = STDMIN(derivedLen, buffer.size());
2002-10-04 17:31:41 +00:00
memcpy(derived, buffer, segmentLen);
2004-04-08 01:57:33 +00:00
if (timeInSeconds)
{
timeInSeconds = timeInSeconds / ((derivedLen + buffer.size() - 1) / buffer.size());
timer.StartTimer();
}
for (j=1; j<iterations || (timeInSeconds && (j%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); j++)
2002-10-04 17:31:41 +00:00
{
hmac.CalculateDigest(buffer, buffer, buffer.size());
xorbuf(derived, buffer, segmentLen);
}
2004-04-08 01:57:33 +00:00
if (timeInSeconds)
{
iterations = j;
timeInSeconds = 0;
}
2002-10-04 17:31:41 +00:00
derived += segmentLen;
derivedLen -= segmentLen;
i++;
}
2004-04-08 01:57:33 +00:00
return iterations;
2002-10-04 17:31:41 +00:00
}
//! PBKDF from PKCS #12, appendix B, T should be a HashTransformation class
template <class T>
class PKCS12_PBKDF : public PasswordBasedKeyDerivationFunction
{
public:
2005-07-12 04:23:32 +00:00
size_t MaxDerivedKeyLength() const {return size_t(0)-1;}
2004-04-08 01:57:33 +00:00
bool UsesPurposeByte() const {return true;}
2005-07-12 04:23:32 +00:00
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;
2002-10-04 17:31:41 +00:00
};
template <class T>
2005-07-12 04:23:32 +00:00
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
2002-10-04 17:31:41 +00:00
{
assert(derivedLen <= MaxDerivedKeyLength());
2004-04-08 01:57:33 +00:00
assert(iterations > 0 || timeInSeconds > 0);
if (!iterations)
iterations = 1;
2002-10-04 17:31:41 +00:00
2005-07-12 04:23:32 +00:00
const size_t v = T::BLOCKSIZE; // v is in bytes rather than bits as in PKCS #12
const size_t DLen = v, SLen = RoundUpToMultipleOf(saltLen, v);
const size_t PLen = RoundUpToMultipleOf(passwordLen, v), ILen = SLen + PLen;
2002-10-04 17:31:41 +00:00
SecByteBlock buffer(DLen + SLen + PLen);
byte *D = buffer, *S = buffer+DLen, *P = buffer+DLen+SLen, *I = S;
2004-04-08 01:57:33 +00:00
memset(D, purpose, DLen);
2005-07-12 04:23:32 +00:00
size_t i;
2002-10-04 17:31:41 +00:00
for (i=0; i<SLen; i++)
S[i] = salt[i % saltLen];
for (i=0; i<PLen; i++)
P[i] = password[i % passwordLen];
T hash;
SecByteBlock Ai(T::DIGESTSIZE), B(v);
2004-04-08 01:57:33 +00:00
ThreadUserTimer timer;
2002-10-04 17:31:41 +00:00
while (derivedLen > 0)
{
hash.CalculateDigest(Ai, buffer, buffer.size());
2004-04-08 01:57:33 +00:00
if (timeInSeconds)
{
timeInSeconds = timeInSeconds / ((derivedLen + Ai.size() - 1) / Ai.size());
timer.StartTimer();
}
for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
2002-10-04 17:31:41 +00:00
hash.CalculateDigest(Ai, Ai, Ai.size());
2004-04-08 01:57:33 +00:00
if (timeInSeconds)
{
2005-07-12 04:23:32 +00:00
iterations = (unsigned int)i;
2004-04-08 01:57:33 +00:00
timeInSeconds = 0;
}
2002-10-04 17:31:41 +00:00
for (i=0; i<B.size(); i++)
B[i] = Ai[i % Ai.size()];
Integer B1(B, B.size());
++B1;
for (i=0; i<ILen; i+=v)
(Integer(I+i, v) + B1).Encode(I+i, v);
2005-07-12 04:23:32 +00:00
size_t segmentLen = STDMIN(derivedLen, Ai.size());
2002-10-04 17:31:41 +00:00
memcpy(derived, Ai, segmentLen);
derived += segmentLen;
derivedLen -= segmentLen;
}
2004-04-08 01:57:33 +00:00
return iterations;
2002-10-04 17:31:41 +00:00
}
NAMESPACE_END
#endif