Skip to content

Commit

Permalink
Fix use of MaxDerivedKeyLength (GH #874)
Browse files Browse the repository at this point in the history
Also fix memcpy with NULL buffer
  • Loading branch information
noloader committed Aug 16, 2019
1 parent 2ba9d3d commit c0a5a06
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cryptlib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ void RandomNumberGenerator::GenerateIntoBufferedTransformation(BufferedTransform
}
}

size_t KeyDerivationFunction::MinDerivedLength() const
size_t KeyDerivationFunction::MinDerivedKeyLength() const
{
return 0;
}

size_t KeyDerivationFunction::MaxDerivedLength() const
size_t KeyDerivationFunction::MaxDerivedKeyLength() const
{
return static_cast<size_t>(-1);
}
Expand Down
4 changes: 2 additions & 2 deletions cryptlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -1477,11 +1477,11 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE KeyDerivationFunction : public Algorithm

/// \brief Determine minimum number of bytes
/// \returns Minimum number of bytes which can be derived
virtual size_t MinDerivedLength() const;
virtual size_t MinDerivedKeyLength() const;

/// \brief Determine maximum number of bytes
/// \returns Maximum number of bytes which can be derived
virtual size_t MaxDerivedLength() const;
virtual size_t MaxDerivedKeyLength() const;

/// \brief Returns a valid key length for the derivation function
/// \param keylength the size of the derived key, in bytes
Expand Down
27 changes: 14 additions & 13 deletions pwdbased.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ class PKCS5_PBKDF1 : public PasswordBasedKeyDerivationFunction
template <class T>
size_t PKCS5_PBKDF1<T>::GetValidDerivedLength(size_t keylength) const
{
if (keylength > MaxDerivedLength())
return MaxDerivedLength();
if (keylength > MaxDerivedKeyLength())
return MaxDerivedKeyLength();
return keylength;
}

Expand All @@ -93,7 +93,7 @@ size_t PKCS5_PBKDF1<T>::DeriveKey(byte *derived, size_t derivedLen,
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());

byte purpose = (byte)params.GetIntValueWithDefault("Purpose", 0);
unsigned int iterations = (unsigned int)params.GetIntValueWithDefault("Iterations", 1);
Expand All @@ -112,7 +112,7 @@ size_t PKCS5_PBKDF1<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);
CRYPTOPP_UNUSED(purpose);

Expand All @@ -137,7 +137,8 @@ size_t PKCS5_PBKDF1<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose
for (i=1; i<iterations || (timeInSeconds && (i%128!=0 || timer.ElapsedTimeAsDouble() < timeInSeconds)); i++)
hash.CalculateDigest(buffer, buffer, buffer.size());

memcpy(derived, buffer, derivedLen);
if (derived)
memcpy(derived, buffer, derivedLen);
return i;
}

Expand Down Expand Up @@ -206,8 +207,8 @@ class PKCS5_PBKDF2_HMAC : public PasswordBasedKeyDerivationFunction
template <class T>
size_t PKCS5_PBKDF2_HMAC<T>::GetValidDerivedLength(size_t keylength) const
{
if (keylength > MaxDerivedLength())
return MaxDerivedLength();
if (keylength > MaxDerivedKeyLength())
return MaxDerivedKeyLength();
return keylength;
}

Expand All @@ -217,7 +218,7 @@ size_t PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived, size_t derivedLen,
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());

byte purpose = (byte)params.GetIntValueWithDefault("Purpose", 0);
unsigned int iterations = (unsigned int)params.GetIntValueWithDefault("Iterations", 1);
Expand All @@ -236,7 +237,7 @@ size_t PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived, size_t derivedLen, byte pu
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);
CRYPTOPP_UNUSED(purpose);

Expand Down Expand Up @@ -363,8 +364,8 @@ class PKCS12_PBKDF : public PasswordBasedKeyDerivationFunction
template <class T>
size_t PKCS12_PBKDF<T>::GetValidDerivedLength(size_t keylength) const
{
if (keylength > MaxDerivedLength())
return MaxDerivedLength();
if (keylength > MaxDerivedKeyLength())
return MaxDerivedKeyLength();
return keylength;
}

Expand All @@ -374,7 +375,7 @@ size_t PKCS12_PBKDF<T>::DeriveKey(byte *derived, size_t derivedLen,
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());

byte purpose = (byte)params.GetIntValueWithDefault("Purpose", 0);
unsigned int iterations = (unsigned int)params.GetIntValueWithDefault("Iterations", 1);
Expand All @@ -394,7 +395,7 @@ size_t PKCS12_PBKDF<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose
{
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
CRYPTOPP_ASSERT(derived && derivedLen);
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);

ThrowIfInvalidDerivedLength(derivedLen);
Expand Down

0 comments on commit c0a5a06

Please sign in to comment.