From Crypto++ Wiki
Jump to navigation Jump to search
#include <cryptopp/hkdf.h>

HKDF is HMAC-based Extract-and-Expand key derivation function by Krawczyk and Eronen. The Crypto++ implementation is from Cryptographic Extraction and Key Derivation: The HKDF Scheme and HMAC-based Extract-and-Expand Key Derivation Function (HKDF).

HKDF provides the KeyDerivationFunction interface rather than the MessageAuthenticationCode interface. The KDF interface consists of a default constructor and a method DeriveKey which derives a user key form the parameters.


HKDF provides a default constructor.


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

derived is the buffer to receive the derived key. derivedLen is the size of the buffer, in bytes.

secret is private information to use during derivation. secretLen is the size of the buffer, in bytes.

salt is possibly public information to use during derivation. saltLen is the size of the buffer, in bytes.

info is additional, possibly public information to use during derivation. infoLen is the size of the buffer, in bytes.

DeriveKey returns the number of bytes returned in the derived buffer.

salt and info are used to help distinguish one instance or run of the algorithm from another. The parameters can be NULL.

Sample Program

The sample program below demonstrates a HKDF with SHA1.

int main(int argc, char* argv[])
    byte password[] ="password";
    size_t plen = strlen((const char*)password);

    byte salt[] = "salt";
    size_t slen = strlen((const char*)salt);

    byte info[] = "HKDF key derivation";
    size_t ilen = strlen((const char*)ilen);

    byte derived[SHA1::DIGESTSIZE];

    HKDF<SHA1> hkdf;
    hkdf.DeriveKey(derived, sizeof(derived), password, plen, salt, slen, info, ilen);

    string result;
    HexEncoder encoder(new StringSink(result));

    encoder.Put(derived, sizeof(derived));

    cout << "Derived: " << result << endl;

    return 0;

Running the program results in the following.

$ ./test.exe
Derived: 9912F20853DFF1AFA944E9B88CA63C410CBB1938