Skip to content

Commit e22700f

Browse files
committed
Fix use of MaxDerivedKeyLength (GH #874)
1 parent c0a5a06 commit e22700f

7 files changed

Lines changed: 27 additions & 27 deletions

File tree

‎cryptlib.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ size_t KeyDerivationFunction::MaxDerivedKeyLength() const
344344
return static_cast<size_t>(-1);
345345
}
346346

347-
void KeyDerivationFunction::ThrowIfInvalidDerivedLength(size_t length) const
347+
void KeyDerivationFunction::ThrowIfInvalidDerivedKeyLength(size_t length) const
348348
{
349349
if (!IsValidDerivedLength(length))
350-
throw InvalidDerivedLength(GetAlgorithm().AlgorithmName(), length);
350+
throw InvalidDerivedKeyLength(GetAlgorithm().AlgorithmName(), length);
351351
}
352352

353353
void KeyDerivationFunction::SetParameters(const NameValuePairs& params) {

‎cryptlib.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE KeyDerivationFunction : public Algorithm
15031503
/// \param secretLen the size of the secret buffer, in bytes
15041504
/// \param params additional initialization parameters to configure this object
15051505
/// \returns the number of iterations performed
1506-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
1506+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
15071507
/// \details DeriveKey() provides a standard interface to derive a key from
15081508
/// a secret seed and other parameters. Each class that derives from KeyDerivationFunction
15091509
/// provides an overload that accepts most parameters used by the derivation function.
@@ -1525,7 +1525,7 @@ class CRYPTOPP_DLL CRYPTOPP_NO_VTABLE KeyDerivationFunction : public Algorithm
15251525
/// \brief Validates the derived key length
15261526
/// \param length the size of the derived key material, in bytes
15271527
/// \throws InvalidKeyLength if the key length is invalid
1528-
void ThrowIfInvalidDerivedLength(size_t length) const;
1528+
void ThrowIfInvalidDerivedKeyLength(size_t length) const;
15291529
};
15301530

15311531
/// \brief Interface for password based key derivation functions

‎hkdf.h‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class HKDF : public KeyDerivationFunction
3939
}
4040

4141
// KeyDerivationFunction interface
42-
size_t MaxDerivedLength() const {
42+
size_t MaxDerivedKeyLength() const {
4343
return static_cast<size_t>(T::DIGESTSIZE) * 255;
4444
}
4545

@@ -60,7 +60,7 @@ class HKDF : public KeyDerivationFunction
6060
/// \param info the additional input buffer
6161
/// \param infoLen the size of the info buffer, in bytes
6262
/// \returns the number of iterations performed
63-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
63+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
6464
/// \details DeriveKey() provides a standard interface to derive a key from
6565
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
6666
/// provides an overload that accepts most parameters used by the derivation function.
@@ -92,8 +92,8 @@ class HKDF : public KeyDerivationFunction
9292
template <class T>
9393
size_t HKDF<T>::GetValidDerivedLength(size_t keylength) const
9494
{
95-
if (keylength > MaxDerivedLength())
96-
return MaxDerivedLength();
95+
if (keylength > MaxDerivedKeyLength())
96+
return MaxDerivedKeyLength();
9797
return keylength;
9898
}
9999

@@ -103,7 +103,7 @@ size_t HKDF<T>::DeriveKey(byte *derived, size_t derivedLen,
103103
{
104104
CRYPTOPP_ASSERT(secret && secretLen);
105105
CRYPTOPP_ASSERT(derived && derivedLen);
106-
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
106+
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
107107

108108
ConstByteArrayParameter p;
109109
SecByteBlock salt, info;
@@ -127,9 +127,9 @@ size_t HKDF<T>::DeriveKey(byte *derived, size_t derivedLen, const byte *secret,
127127
{
128128
CRYPTOPP_ASSERT(secret && secretLen);
129129
CRYPTOPP_ASSERT(derived && derivedLen);
130-
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
130+
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
131131

132-
ThrowIfInvalidDerivedLength(derivedLen);
132+
ThrowIfInvalidDerivedKeyLength(derivedLen);
133133

134134
// HKDF business logic. NULL is different than empty.
135135
if (salt == NULLPTR)

‎pwdbased.h‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class PKCS5_PBKDF1 : public PasswordBasedKeyDerivationFunction
6161
/// \param iterations the number of iterations
6262
/// \param timeInSeconds the in seconds
6363
/// \returns the number of iterations performed
64-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
64+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
6565
/// \details DeriveKey() provides a standard interface to derive a key from
6666
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
6767
/// provides an overload that accepts most parameters used by the derivation function.
@@ -116,7 +116,7 @@ size_t PKCS5_PBKDF1<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose
116116
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);
117117
CRYPTOPP_UNUSED(purpose);
118118

119-
ThrowIfInvalidDerivedLength(derivedLen);
119+
ThrowIfInvalidDerivedKeyLength(derivedLen);
120120

121121
// Business logic
122122
if (!iterations) { iterations = 1; }
@@ -187,7 +187,7 @@ class PKCS5_PBKDF2_HMAC : public PasswordBasedKeyDerivationFunction
187187
/// \param iterations the number of iterations
188188
/// \param timeInSeconds the in seconds
189189
/// \returns the number of iterations performed
190-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
190+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
191191
/// \details DeriveKey() provides a standard interface to derive a key from
192192
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
193193
/// provides an overload that accepts most parameters used by the derivation function.
@@ -241,7 +241,7 @@ size_t PKCS5_PBKDF2_HMAC<T>::DeriveKey(byte *derived, size_t derivedLen, byte pu
241241
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);
242242
CRYPTOPP_UNUSED(purpose);
243243

244-
ThrowIfInvalidDerivedLength(derivedLen);
244+
ThrowIfInvalidDerivedKeyLength(derivedLen);
245245

246246
// Business logic
247247
if (!iterations) { iterations = 1; }
@@ -344,7 +344,7 @@ class PKCS12_PBKDF : public PasswordBasedKeyDerivationFunction
344344
/// \param iterations the number of iterations
345345
/// \param timeInSeconds the in seconds
346346
/// \returns the number of iterations performed
347-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
347+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
348348
/// \details DeriveKey() provides a standard interface to derive a key from
349349
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
350350
/// provides an overload that accepts most parameters used by the derivation function.
@@ -398,7 +398,7 @@ size_t PKCS12_PBKDF<T>::DeriveKey(byte *derived, size_t derivedLen, byte purpose
398398
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
399399
CRYPTOPP_ASSERT(iterations > 0 || timeInSeconds > 0);
400400

401-
ThrowIfInvalidDerivedLength(derivedLen);
401+
ThrowIfInvalidDerivedKeyLength(derivedLen);
402402

403403
// Business logic
404404
if (!iterations) { iterations = 1; }

‎scrypt.cpp‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ NAMESPACE_BEGIN(CryptoPP)
177177

178178
size_t Scrypt::GetValidDerivedLength(size_t keylength) const
179179
{
180-
if (keylength > MaxDerivedLength())
181-
return MaxDerivedLength();
180+
if (keylength > MaxDerivedKeyLength())
181+
return MaxDerivedKeyLength();
182182
return keylength;
183183
}
184184

@@ -261,7 +261,7 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen,
261261
{
262262
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
263263
CRYPTOPP_ASSERT(derived && derivedLen);
264-
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
264+
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
265265

266266
word64 cost=0, blockSize=0, parallelization=0;
267267
if(params.GetValue("Cost", cost) == false)
@@ -284,9 +284,9 @@ size_t Scrypt::DeriveKey(byte*derived, size_t derivedLen, const byte*secret, siz
284284
{
285285
CRYPTOPP_ASSERT(secret /*&& secretLen*/);
286286
CRYPTOPP_ASSERT(derived && derivedLen);
287-
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
287+
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedKeyLength());
288288

289-
ThrowIfInvalidDerivedLength(derivedLen);
289+
ThrowIfInvalidDerivedKeyLength(derivedLen);
290290
ValidateParameters(derivedLen, cost, blockSize, parallel);
291291

292292
AlignedSecByteBlock B(static_cast<size_t>(blockSize * parallel * 128U));

‎scrypt.h‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ class Scrypt : public KeyDerivationFunction
4545
}
4646

4747
// KeyDerivationFunction interface
48-
size_t MaxDerivedLength() const {
49-
return static_cast<size_t>(-1);
48+
size_t MaxDerivedKeyLength() const {
49+
return static_cast<size_t>(0)-1;
5050
}
5151

5252
// KeyDerivationFunction interface
@@ -67,7 +67,7 @@ class Scrypt : public KeyDerivationFunction
6767
/// \param blockSize the block size
6868
/// \param parallelization the parallelization factor
6969
/// \returns the number of iterations performed
70-
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
70+
/// \throws InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
7171
/// \details DeriveKey() provides a standard interface to derive a key from
7272
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
7373
/// provides an overload that accepts most parameters used by the derivation function.

‎simple.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ class CRYPTOPP_DLL InvalidBlockSize : public InvalidArgument
7070
};
7171

7272
/// \brief Exception thrown when an invalid derived key length is encountered
73-
class CRYPTOPP_DLL InvalidDerivedLength : public InvalidArgument
73+
class CRYPTOPP_DLL InvalidDerivedKeyLength : public InvalidArgument
7474
{
7575
public:
76-
explicit InvalidDerivedLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid derived key length") {}
76+
explicit InvalidDerivedKeyLength(const std::string &algorithm, size_t length) : InvalidArgument(algorithm + ": " + IntToString(length) + " is not a valid derived key length") {}
7777
};
7878

7979
/// \brief Exception thrown when an invalid personalization string length is encountered

0 commit comments

Comments
 (0)