3 #ifndef CRYPTOPP_HASH_KEY_DERIVATION_FUNCTION_H 4 #define CRYPTOPP_HASH_KEY_DERIVATION_FUNCTION_H 18 virtual size_t MaxDerivedKeyLength()
const =0;
19 virtual bool Usesinfo()
const =0;
21 virtual unsigned int DeriveKey(byte *derived,
size_t derivedLen,
const byte *secret,
size_t secretLen,
const byte *salt,
size_t saltLen,
const byte* info=NULL,
size_t infoLen=0)
const =0;
23 virtual ~KeyDerivationFunction() {}
32 static const char* StaticAlgorithmName () {
33 static const std::string name(std::string(
"HKDF(") + std::string(T::StaticAlgorithmName()) + std::string(
")"));
37 bool Usesinfo()
const {
return true;}
38 unsigned int 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;
43 typedef byte NullVectorType[T::DIGESTSIZE];
44 static const NullVectorType& GetNullVector() {
45 static const NullVectorType s_NullVector = {0};
51 unsigned int 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 53 static const size_t DIGEST_SIZE =
static_cast<size_t>(T::DIGESTSIZE);
54 const unsigned int req =
static_cast<unsigned int>(derivedLen);
56 assert(secret && secretLen);
57 assert(derived && derivedLen);
61 throw InvalidArgument(
"HKDF: derivedLen must be less than or equal to MaxDerivedKeyLength");
67 const byte* key = (salt ? salt : GetNullVector());
68 const size_t klen = (salt ? saltLen : DIGEST_SIZE);
77 while (derivedLen > 0)
79 if (block++) {hmac.
Update(buffer, buffer.
size());}
80 if (info && infoLen) {hmac.
Update(info, infoLen);}
83 #if CRYPTOPP_MSC_VERSION 84 const size_t segmentLen =
STDMIN(derivedLen, DIGEST_SIZE);
85 memcpy_s(derived, segmentLen, buffer, segmentLen);
87 const size_t segmentLen =
STDMIN(derivedLen, DIGEST_SIZE);
88 std::memcpy(derived, buffer, segmentLen);
91 derived += segmentLen;
92 derivedLen -= segmentLen;
100 #endif // CRYPTOPP_HASH_KEY_DERIVATION_FUNCTION_H An invalid argument was detected.
virtual void SetKey(const byte *key, size_t length, const NameValuePairs ¶ms=g_nullNameValuePairs)
Sets or reset the key of this object.
General, multipurpose KDF from RFC 5869.
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()
size_type size() const
Provides the count of elements in the SecBlock.
Classes and functions for secure memory allocations.
Classes for HMAC message authentication codes.
A::pointer data()
Provides a pointer to the first element in the memory block.
size_t MaxDerivedKeyLength() const
maximum number of bytes which can be produced under a secuirty context
void Update(const byte *input, size_t length)
Updates a hash with additional input.
Fixed size stack-based SecBlock.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
Crypto++ library namespace.
unsigned int 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
derive a key from secret
abstract base class for key derivation function