2017-02-21 07:54:09 +00:00
|
|
|
// hkdf.h - written and placed in public domain by Jeffrey Walton.
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file hkdf.h
|
|
|
|
/// \brief Classes for HKDF from RFC 5869
|
|
|
|
/// \since Crypto++ 5.6.3
|
2016-09-08 18:41:42 +00:00
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
#ifndef CRYPTOPP_HKDF_H
|
|
|
|
#define CRYPTOPP_HKDF_H
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "secblock.h"
|
2018-07-27 22:16:25 +00:00
|
|
|
#include "algparam.h"
|
2015-11-05 06:59:46 +00:00
|
|
|
#include "hmac.h"
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \brief Extract-and-Expand Key Derivation Function (HKDF)
|
|
|
|
/// \tparam T HashTransformation class
|
2018-04-08 07:20:14 +00:00
|
|
|
/// \sa <A HREF="http://eprint.iacr.org/2010/264">Cryptographic Extraction and Key
|
|
|
|
/// Derivation: The HKDF Scheme</A> and
|
|
|
|
/// <A HREF="http://tools.ietf.org/html/rfc5869">HMAC-based Extract-and-Expand Key
|
|
|
|
/// Derivation Function (HKDF)</A>
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \since Crypto++ 5.6.3
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T>
|
|
|
|
class HKDF : public KeyDerivationFunction
|
|
|
|
{
|
|
|
|
public:
|
2018-03-30 00:18:27 +00:00
|
|
|
virtual ~HKDF() {}
|
|
|
|
|
|
|
|
static std::string StaticAlgorithmName () {
|
|
|
|
const std::string name(std::string("HKDF(") +
|
|
|
|
std::string(T::StaticAlgorithmName()) + std::string(")"));
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2018-03-30 20:40:15 +00:00
|
|
|
// KeyDerivationFunction interface
|
2018-03-30 00:18:27 +00:00
|
|
|
std::string AlgorithmName() const {
|
|
|
|
return StaticAlgorithmName();
|
|
|
|
}
|
|
|
|
|
2018-03-30 20:40:15 +00:00
|
|
|
// KeyDerivationFunction interface
|
2018-03-30 00:18:27 +00:00
|
|
|
size_t MaxDerivedLength() const {
|
|
|
|
return static_cast<size_t>(T::DIGESTSIZE) * 255;
|
|
|
|
}
|
|
|
|
|
2018-03-30 20:40:15 +00:00
|
|
|
// KeyDerivationFunction interface
|
2018-03-30 00:18:27 +00:00
|
|
|
size_t GetValidDerivedLength(size_t keylength) const;
|
|
|
|
|
2018-03-30 20:40:15 +00:00
|
|
|
// KeyDerivationFunction interface
|
2018-03-30 00:18:27 +00:00
|
|
|
size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen,
|
|
|
|
const NameValuePairs& params) const;
|
|
|
|
|
|
|
|
/// \brief Derive a key from a seed
|
|
|
|
/// \param derived the derived output buffer
|
|
|
|
/// \param derivedLen the size of the derived buffer, in bytes
|
|
|
|
/// \param secret the seed input buffer
|
|
|
|
/// \param secretLen the size of the secret buffer, in bytes
|
|
|
|
/// \param salt the salt input buffer
|
|
|
|
/// \param saltLen the size of the salt buffer, in bytes
|
|
|
|
/// \param info the additional input buffer
|
|
|
|
/// \param infoLen the size of the info buffer, in bytes
|
2018-03-30 03:13:56 +00:00
|
|
|
/// \returns the number of iterations performed
|
2018-03-30 00:18:27 +00:00
|
|
|
/// \throws InvalidDerivedLength if <tt>derivedLen</tt> is invalid for the scheme
|
|
|
|
/// \details DeriveKey() provides a standard interface to derive a key from
|
|
|
|
/// a seed and other parameters. Each class that derives from KeyDerivationFunction
|
|
|
|
/// provides an overload that accepts most parameters used by the derivation function.
|
|
|
|
/// \details <tt>salt</tt> and <tt>info</tt> can be <tt>nullptr</tt> with 0 length.
|
2018-03-30 03:13:56 +00:00
|
|
|
/// HKDF is unusual in that a non-NULL salt with length 0 is different than a
|
|
|
|
/// NULL <tt>salt</tt>. A NULL <tt>salt</tt> causes HKDF to use a string of 0's
|
2018-03-30 00:18:27 +00:00
|
|
|
/// of length <tt>T::DIGESTSIZE</tt> for the <tt>salt</tt>.
|
2018-03-30 03:13:56 +00:00
|
|
|
/// \details HKDF always returns 1 because it only performs 1 iteration. Other
|
|
|
|
/// derivation functions, like PBKDF's, will return more interesting values.
|
2018-03-30 00:18:27 +00:00
|
|
|
size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen,
|
|
|
|
const byte *salt, size_t saltLen, const byte* info, size_t infoLen) const;
|
2016-09-08 18:41:42 +00:00
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
protected:
|
2018-03-30 03:13:56 +00:00
|
|
|
// KeyDerivationFunction interface
|
|
|
|
const Algorithm & GetAlgorithm() const {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
// If salt is absent (NULL), then use the NULL vector. Missing is different than
|
|
|
|
// EMPTY (Non-NULL, 0 length). The length of s_NullVector used depends on the Hash
|
|
|
|
// function. SHA-256 will use 32 bytes of s_NullVector.
|
|
|
|
typedef byte NullVectorType[T::DIGESTSIZE];
|
2015-11-05 06:59:46 +00:00
|
|
|
static const NullVectorType& GetNullVector() {
|
|
|
|
static const NullVectorType s_NullVector = {0};
|
|
|
|
return s_NullVector;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
2018-03-30 00:18:27 +00:00
|
|
|
size_t HKDF<T>::GetValidDerivedLength(size_t keylength) const
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2018-03-30 00:18:27 +00:00
|
|
|
if (keylength > MaxDerivedLength())
|
|
|
|
return MaxDerivedLength();
|
|
|
|
return keylength;
|
|
|
|
}
|
2016-09-08 18:41:42 +00:00
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
template <class T>
|
|
|
|
size_t HKDF<T>::DeriveKey(byte *derived, size_t derivedLen,
|
|
|
|
const byte *secret, size_t secretLen, const NameValuePairs& params) const
|
|
|
|
{
|
2016-09-16 15:27:15 +00:00
|
|
|
CRYPTOPP_ASSERT(secret && secretLen);
|
|
|
|
CRYPTOPP_ASSERT(derived && derivedLen);
|
2018-03-30 00:18:27 +00:00
|
|
|
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
|
|
|
|
|
|
|
|
ConstByteArrayParameter p;
|
2018-03-30 04:34:12 +00:00
|
|
|
SecByteBlock salt, info;
|
|
|
|
|
|
|
|
if (params.GetValue("Salt", p))
|
|
|
|
salt.Assign(p.begin(), p.size());
|
|
|
|
else
|
|
|
|
salt.Assign(GetNullVector(), T::DIGESTSIZE);
|
|
|
|
|
|
|
|
if (params.GetValue("Info", p))
|
|
|
|
info.Assign(p.begin(), p.size());
|
|
|
|
else
|
|
|
|
info.Assign(GetNullVector(), 0);
|
2018-03-30 00:18:27 +00:00
|
|
|
|
2018-03-30 03:13:56 +00:00
|
|
|
return DeriveKey(derived, derivedLen, secret, secretLen, salt.begin(), salt.size(), info.begin(), info.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
size_t HKDF<T>::DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen,
|
|
|
|
const byte *salt, size_t saltLen, const byte* info, size_t infoLen) const
|
|
|
|
{
|
|
|
|
CRYPTOPP_ASSERT(secret && secretLen);
|
|
|
|
CRYPTOPP_ASSERT(derived && derivedLen);
|
|
|
|
CRYPTOPP_ASSERT(derivedLen <= MaxDerivedLength());
|
|
|
|
|
|
|
|
ThrowIfInvalidDerivedLength(derivedLen);
|
|
|
|
|
2018-04-08 07:20:14 +00:00
|
|
|
// HKDF business logic. NULL is different than empty.
|
|
|
|
if (salt == NULLPTR)
|
|
|
|
{
|
|
|
|
salt = GetNullVector();
|
|
|
|
saltLen = T::DIGESTSIZE;
|
|
|
|
}
|
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
// key is PRK from the RFC, salt is IKM from the RFC
|
2015-11-05 06:59:46 +00:00
|
|
|
HMAC<T> hmac;
|
2018-03-30 00:18:27 +00:00
|
|
|
SecByteBlock key(T::DIGESTSIZE), buffer(T::DIGESTSIZE);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
|
|
|
// Extract
|
2018-03-30 03:13:56 +00:00
|
|
|
hmac.SetKey(salt, saltLen);
|
2018-03-30 00:18:27 +00:00
|
|
|
hmac.CalculateDigest(key, secret, secretLen);
|
2015-11-05 06:59:46 +00:00
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
// Key
|
|
|
|
hmac.SetKey(key.begin(), key.size());
|
2015-11-05 06:59:46 +00:00
|
|
|
byte block = 0;
|
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
// Expand
|
2018-03-30 03:13:56 +00:00
|
|
|
while (derivedLen > 0)
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
|
|
|
if (block++) {hmac.Update(buffer, buffer.size());}
|
2018-03-30 03:13:56 +00:00
|
|
|
if (infoLen) {hmac.Update(info, infoLen);}
|
2015-11-05 06:59:46 +00:00
|
|
|
hmac.CalculateDigest(buffer, &block, 1);
|
|
|
|
|
|
|
|
#if CRYPTOPP_MSC_VERSION
|
2018-03-30 03:13:56 +00:00
|
|
|
const size_t digestSize = static_cast<size_t>(T::DIGESTSIZE);
|
|
|
|
const size_t segmentLen = STDMIN(derivedLen, digestSize);
|
2015-11-05 06:59:46 +00:00
|
|
|
memcpy_s(derived, segmentLen, buffer, segmentLen);
|
|
|
|
#else
|
2018-03-30 03:13:56 +00:00
|
|
|
const size_t digestSize = static_cast<size_t>(T::DIGESTSIZE);
|
|
|
|
const size_t segmentLen = STDMIN(derivedLen, digestSize);
|
2015-11-05 06:59:46 +00:00
|
|
|
std::memcpy(derived, buffer, segmentLen);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
derived += segmentLen;
|
2018-03-30 03:13:56 +00:00
|
|
|
derivedLen -= segmentLen;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 03:13:56 +00:00
|
|
|
return 1;
|
2015-11-05 06:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
2018-03-30 00:18:27 +00:00
|
|
|
#endif // CRYPTOPP_HKDF_H
|