hmac.h
Functions
| Name | |
|---|---|
| int | wc_HmacInit(Hmac * hmac, void * heap, int devId) This function initializes an Hmac object, setting the heap hint and the device ID used with crypto callbacks or async hardware. It is up to the user to call wc_HmacFree on the Hmac object when done. |
| int | wc_HmacSetKey(Hmac * hmac, int type, const byte * key, word32 keySz) This function initializes an Hmac object, setting its encryption type, key and HMAC length. |
| int | wc_HmacUpdate(Hmac * hmac, const byte * in, word32 sz) This function updates the message to authenticate using HMAC. It should be called after the Hmac object has been initialized with wc_HmacSetKey. This function may be called multiple times to update the message to hash. After calling wc_HmacUpdate as desired, one should call wc_HmacFinal to obtain the final authenticated message tag. |
| int | wc_HmacFinal(Hmac * hmac, byte * out) This function computes the final hash of an Hmac object's message. |
| int | wolfSSL_GetHmacMaxSize(void ) This function returns the largest HMAC digest size available based on the configured cipher suites. |
| int | wc_HmacGenerate(int type, byte * out, word32 * outSz, const byte * in, word32 inSz, const byte * key, word32 keySz) Single shot function for generating an HMAC. Initializes, keys, hashes the message and finalizes in one call. The full digest size tag of the hash type is always written. |
| int | wc_HmacGenerate_ex(Hmac * hmac, int type, byte * out, word32 * outSz, const byte * in, word32 inSz, const byte * key, word32 keySz, void * heap, int devId) Single shot HMAC for use with a crypto callback device. Use this function only with crypto callbacks; otherwise use wc_HmacGenerate. It works like wc_HmacGenerate, but uses caller provided storage for the HMAC context and takes a heap hint and device ID. Unless key is NULL, the context is initialized with wc_HmacInit and released with wc_HmacFree inside this call, so it must not hold a live HMAC operation on entry. |
| int | wc_HmacVerify(int type, const byte * check, word32 checkSz, const byte * in, word32 inSz, const byte * key, word32 keySz) Single shot function for validating an HMAC. Computes the tag over the message and compares it against check in constant time. Only full length tags are accepted. |
| int | wc_HmacVerify_ex(Hmac * hmac, int type, const byte * check, word32 checkSz, const byte * in, word32 inSz, const byte * key, word32 keySz, void * heap, int devId) Single shot HMAC verify for use with a crypto callback device. Use this function only with crypto callbacks; otherwise use wc_HmacVerify. It works like wc_HmacVerify, but uses caller provided storage for the HMAC context and takes a heap hint and device ID. The context, the crypto callback device and a key stored on the device are used as in wc_HmacGenerate_ex. |
| int | wc_HKDF(int type, const byte * inKey, word32 inKeySz, const byte * salt, word32 saltSz, const byte * info, word32 infoSz, byte * out, word32 outSz) This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt and optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. |
| int | wc_HKDF_Extract(int type, const byte * salt, word32 saltSz, const byte * inKey, word32 inKeySz, byte * out) This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. |
| int | wc_HKDF_Extract_ex(int type, const byte * salt, word32 saltSz, const byte * inKey, word32 inKeySz, byte * out, void * heap, int devId) This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. This is the _ex version adding heap hint and device identifier. |
| int | wc_HKDF_Expand(int type, const byte * inKey, word32 inKeySz, const byte * info, word32 infoSz, byte * out, word32 outSz) This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. |
| int | wc_HKDF_Expand_ex(int type, const byte * inKey, word32 inKeySz, const byte * info, word32 infoSz, byte * out, word32 outSz, void * heap, int devId) This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. This is the _ex version adding heap hint and device identifier. |
| int | wc_Tls13_HKDF_Extract(byte * prk, const byte * salt, word32 saltLen, byte * ikm, word32 ikmLen, int digest) This function provides access to RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for TLS v1.3 key derivation. |
| int | wc_Tls13_HKDF_Extract_ex(byte * prk, const byte * salt, word32 saltLen, byte * ikm, word32 ikmLen, int digest, void * heap, int devId) This function provides access to RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for TLS v1.3 key derivation. This is the _ex version adding heap hint and device identifier. |
| int | wc_Tls13_HKDF_Expand_Label_ex(byte * okm, word32 okmLen, const byte * prk, word32 prkLen, const byte * protocol, word32 protocolLen, const byte * label, word32 labelLen, const byte * info, word32 infoLen, int digest, void * heap, int devId) Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation. This is the _ex version adding heap hint and device identifier. |
| int | wc_Tls13_HKDF_Expand_Label(byte * okm, word32 okmLen, const byte * prk, word32 prkLen, const byte * protocol, word32 protocolLen, const byte * label, word32 labelLen, const byte * info, word32 infoLen, int digest) Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation. |
| int | wc_Tls13_HKDF_Expand_Label_Alloc(byte * okm, word32 okmLen, const byte * prk, word32 prkLen, const byte * protocol, word32 protocolLen, const byte * label, word32 labelLen, const byte * info, word32 infoLen, int digest, void * heap) This functions is very similar to wc_Tls13_HKDF_Expand_Label(), but it allocates memory if the stack space usually used isn't enough. Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation. This is the _ex version adding heap hint and device identifier. |
Functions Documentation
function wc_HmacInit
int wc_HmacInit(
Hmac * hmac,
void * heap,
int devId
)
This function initializes an Hmac object, setting the heap hint and the device ID used with crypto callbacks or async hardware. It is up to the user to call wc_HmacFree on the Hmac object when done.
Parameters:
- hmac pointer to the Hmac object to initialize
- heap heap hint to use for memory allocation, or NULL
- devId ID to use with crypto callbacks or async hardware. Set to INVALID_DEVID (-2) if not used
See: wc_HmacSetKey
Return:
- 0 Returned on successfully initializing the Hmac object
- BAD_FUNC_ARG Returned if hmac is NULL
Note: On 64-bit CPUs, Hmac has 16-byte alignment when WOLFSSL_USE_ALIGN is defined (default in configure and CMake, forced for AES-NI and asm).
Example
Hmac hmac;
byte key[] = { // initialize with key to use for encryption };
if (wc_HmacInit(&hmac, NULL, INVALID_DEVID) != 0) {
// error initializing Hmac object
}
else if (wc_HmacSetKey(&hmac, WC_SHA256, key, sizeof(key)) != 0) {
// error setting the key
}
wc_HmacFree(&hmac);
function wc_HmacSetKey
int wc_HmacSetKey(
Hmac * hmac,
int type,
const byte * key,
word32 keySz
)
This function initializes an Hmac object, setting its encryption type, key and HMAC length.
Parameters:
- hmac pointer to the Hmac object to initialize
- type type specifying which encryption method the Hmac object should use. Valid options are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- key pointer to a buffer containing the key with which to initialize the Hmac object
- length length of the key
See:
Return:
- 0 Returned on successfully initializing the Hmac object
- BAD_FUNC_ARG Returned if the input type is invalid (see type param)
- MEMORY_E Returned if there is an error allocating memory for the structure to use for hashing
- HMAC_MIN_KEYLEN_E Returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard of 14 bytes
Example
Hmac hmac;
byte key[] = { // initialize with key to use for encryption };
if (wc_HmacSetKey(&hmac, WC_MD5, key, sizeof(key)) != 0) {
// error initializing Hmac object
}
function wc_HmacUpdate
int wc_HmacUpdate(
Hmac * hmac,
const byte * in,
word32 sz
)
This function updates the message to authenticate using HMAC. It should be called after the Hmac object has been initialized with wc_HmacSetKey. This function may be called multiple times to update the message to hash. After calling wc_HmacUpdate as desired, one should call wc_HmacFinal to obtain the final authenticated message tag.
Parameters:
- hmac pointer to the Hmac object for which to update the message
- msg pointer to the buffer containing the message to append
- length length of the message to append
See:
Return:
- 0 Returned on successfully updating the message to authenticate
- MEMORY_E Returned if there is an error allocating memory for use with a hashing algorithm
- MISSING_KEY Returned for an Hmac named by id or label with no key bytes set when no device performs the operation
Example
Hmac hmac;
byte msg[] = { // initialize with message to authenticate };
byte msg2[] = { // initialize with second half of message };
// initialize hmac
if( wc_HmacUpdate(&hmac, msg, sizeof(msg)) != 0) {
// error updating message
}
if( wc_HmacUpdate(&hmac, msg2, sizeof(msg)) != 0) {
// error updating with second message
}
function wc_HmacFinal
int wc_HmacFinal(
Hmac * hmac,
byte * out
)
This function computes the final hash of an Hmac object's message.
Parameters:
- hmac pointer to the Hmac object for which to calculate the final hash
- hash pointer to the buffer in which to store the final hash. Should have room available as required by the hashing algorithm chosen
See:
Return:
- 0 Returned on successfully computing the final hash
- MEMORY_E Returned if there is an error allocating memory for use with a hashing algorithm
- MISSING_KEY Returned for an Hmac named by id or label with no key bytes set when no device performs the operation
Example
Hmac hmac;
byte hash[MD5_DIGEST_SIZE];
// initialize hmac with MD5 as type
// wc_HmacUpdate() with messages
if (wc_HmacFinal(&hmac, hash) != 0) {
// error computing hash
}
function wolfSSL_GetHmacMaxSize
int wolfSSL_GetHmacMaxSize(
void
)
This function returns the largest HMAC digest size available based on the configured cipher suites.
Parameters:
- none No parameters.
See: none
Return: Success Returns the largest HMAC digest size available based on the configured cipher suites
Example
int maxDigestSz = wolfSSL_GetHmacMaxSize();
function wc_HmacGenerate
int wc_HmacGenerate(
int type,
byte * out,
word32 * outSz,
const byte * in,
word32 inSz,
const byte * key,
word32 keySz
)
Single shot function for generating an HMAC. Initializes, keys, hashes the message and finalizes in one call. The full digest size tag of the hash type is always written.
Parameters:
- type hash type, such as WC_SHA256 or WC_SHA3_384
- out buffer to hold the tag
- outSz on input the size of out, at least the digest size of type (WC_MAX_DIGEST_SIZE always fits); on output the digest size
- in message to authenticate
- inSz length of the message
- key HMAC key; may be NULL when keySz is 0
- keySz length of the key
See:
Return:
- 0 on success
- BAD_FUNC_ARG if out or outSz is NULL, in is NULL with a non-zero inSz, key is NULL with a non-zero keySz, or type is invalid
- BUFFER_E if *outSz is smaller than the digest size of type
- MEMORY_E if allocating the HMAC context fails
- HMAC_MIN_KEYLEN_E may be returned in FIPS builds when the key is shorter than the FIPS minimum
Example
byte tag[WC_SHA256_DIGEST_SIZE];
word32 tagSz = sizeof(tag);
ret = wc_HmacGenerate(WC_SHA256, tag, &tagSz, msg, msgSz, key, keySz);
function wc_HmacGenerate_ex
int wc_HmacGenerate_ex(
Hmac * hmac,
int type,
byte * out,
word32 * outSz,
const byte * in,
word32 inSz,
const byte * key,
word32 keySz,
void * heap,
int devId
)
Single shot HMAC for use with a crypto callback device. Use this function only with crypto callbacks; otherwise use wc_HmacGenerate. It works like wc_HmacGenerate, but uses caller provided storage for the HMAC context and takes a heap hint and device ID. Unless key is NULL, the context is initialized with wc_HmacInit and released with wc_HmacFree inside this call, so it must not hold a live HMAC operation on entry.
Parameters:
- hmac storage for the HMAC context, or an initialized context that names a key on the device
- type hash type, such as WC_SHA256 or WC_SHA3_384
- out buffer to hold the tag
- outSz on input the size of out; on output the digest size
- in message to authenticate
- inSz length of the message
- key HMAC key, or NULL to use the key on the device that hmac names. With a NULL key, hmac must be initialized by the caller
- keySz length of the key
- heap heap hint for dynamic memory, or NULL
- devId device ID for crypto callbacks or async, or INVALID_DEVID
See:
Return:
- see wc_HmacGenerate; also BAD_FUNC_ARG if hmac is NULL
- MISSING_KEY if key is NULL and no device computed the tag
- WC_PENDING_E if a crypto callback device returned pending; no tag was produced. The caller may call again, which starts a new operation.
If a devId is specified, the crypto callback is immediately dispatched to, with all arguments passed in and no context initialization. If the one-shot operation is declined, the HMAC context is then initialized with the supplied key (if present) and offload to the device will again be attempted, but through the streaming interface (update/final).
To use a key that is stored on the device, initialize hmac with wc_HmacInit_Id or wc_HmacInit_Label and pass a NULL key. The context is then not initialized or freed by this call, and the caller is expected to free it manually.
With a NULL key, hmac must always be initialized by the caller. A NULL key is only for a key that is stored on the crypto callback device. If no device computes the tag, the call fails with MISSING_KEY. A NULL key is never used as a key of zero length. For a key of zero length, pass a non-NULL key with a keySz of 0, or use wc_HmacGenerate.
Example
Hmac hmac;
byte tag[WC_SHA256_DIGEST_SIZE];
word32 tagSz = sizeof(tag);
ret = wc_HmacGenerate_ex(&hmac, WC_SHA256, tag, &tagSz, msg, msgSz,
key, keySz, NULL, devId);
// key stored on the device
ret = wc_HmacInit_Id(&hmac, id, idLen, NULL, devId);
if (ret == 0) {
tagSz = sizeof(tag);
ret = wc_HmacGenerate_ex(&hmac, WC_SHA256, tag, &tagSz, msg, msgSz,
NULL, 0, NULL, devId);
wc_HmacFree(&hmac);
}
function wc_HmacVerify
int wc_HmacVerify(
int type,
const byte * check,
word32 checkSz,
const byte * in,
word32 inSz,
const byte * key,
word32 keySz
)
Single shot function for validating an HMAC. Computes the tag over the message and compares it against check in constant time. Only full length tags are accepted.
Parameters:
- type hash type, such as WC_SHA256 or WC_SHA3_384
- check expected tag
- checkSz length of the expected tag; must equal the digest size of type
- in message that was authenticated
- inSz length of the message
- key HMAC key; may be NULL when keySz is 0
- keySz length of the key
See:
Return:
- 0 if the tag matches
- MAC_CMP_FAILED_E if the tag does not match
- BAD_FUNC_ARG if check is NULL, checkSz is not the digest size of type, or any wc_HmacGenerate argument check fails
- MEMORY_E if allocating the HMAC context fails
- HMAC_MIN_KEYLEN_E may be returned in FIPS builds when the key is shorter than the FIPS minimum
Example
ret = wc_HmacVerify(WC_SHA256, tag, tagSz, msg, msgSz, key, keySz);
if (ret == MAC_CMP_FAILED_E) {
// reject the message
}
function wc_HmacVerify_ex
int wc_HmacVerify_ex(
Hmac * hmac,
int type,
const byte * check,
word32 checkSz,
const byte * in,
word32 inSz,
const byte * key,
word32 keySz,
void * heap,
int devId
)
Single shot HMAC verify for use with a crypto callback device. Use this function only with crypto callbacks; otherwise use wc_HmacVerify. It works like wc_HmacVerify, but uses caller provided storage for the HMAC context and takes a heap hint and device ID. The context, the crypto callback device and a key stored on the device are used as in wc_HmacGenerate_ex.
Parameters:
- hmac storage for the HMAC context, or an initialized context that names a key on the device
- type hash type, such as WC_SHA256 or WC_SHA3_384
- check expected tag
- checkSz length of the expected tag; must equal the digest size of type
- in message that was authenticated
- inSz length of the message
- key HMAC key, or NULL to use the key on the device that hmac names. With a NULL key, hmac must be initialized by the caller
- keySz length of the key
- heap heap hint for dynamic memory, or NULL
- devId device ID for crypto callbacks or async, or INVALID_DEVID
See:
Return:
- see wc_HmacVerify; also BAD_FUNC_ARG if hmac is NULL
- MISSING_KEY if key is NULL and no device computed the tag
- WC_PENDING_E if a crypto callback device returned pending; the tag was not checked. The caller may call again, which starts a new operation.
With a NULL key, hmac must always be initialized by the caller. A NULL key is only for a key that is stored on the crypto callback device. If no device computes the tag, the call fails with MISSING_KEY. A NULL key is never used as a key of zero length.
Example
Hmac hmac;
ret = wc_HmacVerify_ex(&hmac, WC_SHA256, tag, tagSz, msg, msgSz,
key, keySz, NULL, devId);
function wc_HKDF
int wc_HKDF(
int type,
const byte * inKey,
word32 inKeySz,
const byte * salt,
word32 saltSz,
const byte * info,
word32 infoSz,
byte * out,
word32 outSz
)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt and optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given.
Parameters:
- type hash type to use for the HKDF. Valid types are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- inKey pointer to the buffer containing the key to use for KDF
- inKeySz length of the input key
- salt pointer to a buffer containing an optional salt. Use NULL instead if not using a salt
- saltSz length of the salt. Use 0 if not using a salt
- info pointer to a buffer containing optional additional info. Use NULL if not appending extra info
- infoSz length of additional info. Use 0 if not using additional info
- out pointer to the buffer in which to store the derived key
- outSz space available in the output buffer to store the generated key
See: wc_HmacSetKey
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
The HMAC configure option is –enable-hmac (on by default) or if building sources directly HAVE_HKDF
Example
byte key[] = { // initialize with key };
byte salt[] = { // initialize with salt };
byte derivedKey[MAX_DIGEST_SIZE];
int ret = wc_HKDF(WC_SHA512, key, sizeof(key), salt, sizeof(salt),
NULL, 0, derivedKey, sizeof(derivedKey));
if ( ret != 0 ) {
// error generating derived key
}
function wc_HKDF_Extract
int wc_HKDF_Extract(
int type,
const byte * salt,
word32 saltSz,
const byte * inKey,
word32 inKeySz,
byte * out
)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given.
Parameters:
- type hash type to use for the HKDF. Valid types are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- salt pointer to a buffer containing an optional salt. Use NULL instead if not using a salt
- saltSz length of the salt. Use 0 if not using a salt
- inKey pointer to the buffer containing the key to use for KDF
- inKeySz length of the input key
- out pointer to the buffer in which to store the derived key
See:
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
The HMAC configure option is –enable-hmac (on by default) or if building sources directly HAVE_HKDF
Example
byte key[] = { // initialize with key };
byte salt[] = { // initialize with salt };
byte derivedKey[MAX_DIGEST_SIZE];
int ret = wc_HKDF_Extract(WC_SHA512, salt, sizeof(salt), key, sizeof(key),
derivedKey);
if ( ret != 0 ) {
// error generating derived key
}
function wc_HKDF_Extract_ex
int wc_HKDF_Extract_ex(
int type,
const byte * salt,
word32 saltSz,
const byte * inKey,
word32 inKeySz,
byte * out,
void * heap,
int devId
)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with an optional salt into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. This is the _ex version adding heap hint and device identifier.
Parameters:
- type hash type to use for the HKDF. Valid types are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- salt pointer to a buffer containing an optional salt. Use NULL instead if not using a salt
- saltSz length of the salt. Use 0 if not using a salt
- inKey pointer to the buffer containing the key to use for KDF
- inKeySz length of the input key
- out pointer to the buffer in which to store the derived key. Must be digest length per hash 'type'. See wc_HmacSizeByType()
- heap heap hint to use for memory. Can be NULL
- devId ID to use with crypto callbacks or async hardware. Set to INVALID_DEVID (-2) if not used
See:
- wc_HKDF
- wc_HKDF_Extract
- wc_HKDF_Expand
- wc_HKDF_Expand_ex
- wc_HmacSizeByType
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
- WC_PENDING_E May be returned in a WOLF_CRYPTO_CB build when the registered crypto callback device has taken the request but not yet finished it. The caller must re-invoke with identical arguments until the result is no longer WC_PENDING_E; HKDF has no WC_ASYNC_DEV, so this is a poll and not a wc_AsyncWait(). In a WOLFSSL_ASYNC_CRYPT build the TLS 1.3 key schedule resumes a pending HKDF request by re-invoking the callback with identical arguments; without WOLFSSL_ASYNC_CRYPT a device also used for TLS 1.3 must complete HKDF requests synchronously. wc_HKDF_ex() follows the same contract and re-issues its extract step on every retry, so a device that pends must serve a repeated identical request from its completed result. There is no request handle: a device should key its completion tracking on the output pointer plus the argument tuple, and one that cannot correlate a retry that way must not pend.
The HMAC configure option is –enable-hmac (on by default) or if building sources directly HAVE_HKDF
Example
byte key[] = { // initialize with key };
byte salt[] = { // initialize with salt };
byte derivedKey[MAX_DIGEST_SIZE];
int ret = wc_HKDF_Extract_ex(WC_SHA512, salt, sizeof(salt), key, sizeof(key),
derivedKey, NULL, INVALID_DEVID);
if ( ret != 0 ) {
// error generating derived key
}
function wc_HKDF_Expand
int wc_HKDF_Expand(
int type,
const byte * inKey,
word32 inKeySz,
const byte * info,
word32 infoSz,
byte * out,
word32 outSz
)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given.
Parameters:
- type hash type to use for the HKDF. Valid types are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- inKey pointer to the buffer containing the key to use for KDF
- inKeySz length of the input key
- info pointer to a buffer containing optional additional info. Use NULL if not appending extra info
- infoSz length of additional info. Use 0 if not using additional info
- out pointer to the buffer in which to store the derived key
- outSz space available in the output buffer to store the generated key
See:
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
The HMAC configure option is –enable-hmac (on by default) or if building sources directly HAVE_HKDF
Example
byte key[] = { // initialize with key };
byte salt[] = { // initialize with salt };
byte derivedKey[MAX_DIGEST_SIZE];
int ret = wc_HKDF_Expand(WC_SHA512, key, sizeof(key), NULL, 0,
derivedKey, sizeof(derivedKey));
if ( ret != 0 ) {
// error generating derived key
}
function wc_HKDF_Expand_ex
int wc_HKDF_Expand_ex(
int type,
const byte * inKey,
word32 inKeySz,
const byte * info,
word32 infoSz,
byte * out,
word32 outSz,
void * heap,
int devId
)
This function provides access to a HMAC Key Derivation Function (HKDF). It utilizes HMAC to convert inKey, with optional info into a derived key, which it stores in out. The hash type defaults to MD5 if 0 or NULL is given. This is the _ex version adding heap hint and device identifier.
Parameters:
- type hash type to use for the HKDF. Valid types are: WC_MD5, WC_SHA, WC_SHA256, WC_SHA384, WC_SHA512, WC_SHA3_224, WC_SHA3_256, WC_SHA3_384 or WC_SHA3_512
- inKey pointer to the buffer containing the key to use for KDF
- inKeySz length of the input key
- info pointer to a buffer containing optional additional info. Use NULL if not appending extra info
- infoSz length of additional info. Use 0 if not using additional info
- out pointer to the buffer in which to store the derived key
- outSz space available in the output buffer to store the generated key
- heap heap hint to use for memory. Can be NULL
- devId ID to use with crypto callbacks or async hardware. Set to INVALID_DEVID (-2) if not used
See:
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
- WC_PENDING_E May be returned in a WOLF_CRYPTO_CB build when the registered crypto callback device has taken the request but not yet finished it. The caller must re-invoke with identical arguments until the result is no longer WC_PENDING_E; HKDF has no WC_ASYNC_DEV, so this is a poll and not a wc_AsyncWait(). In a WOLFSSL_ASYNC_CRYPT build the TLS 1.3 key schedule resumes a pending HKDF request by re-invoking the callback with identical arguments; without WOLFSSL_ASYNC_CRYPT a device also used for TLS 1.3 must complete HKDF requests synchronously. wc_HKDF_ex() follows the same contract and re-issues its extract step on every retry, so a device that pends must serve a repeated identical request from its completed result. There is no request handle: a device should key its completion tracking on the output pointer plus the argument tuple, and one that cannot correlate a retry that way must not pend.
The HMAC configure option is –enable-hmac (on by default) or if building sources directly HAVE_HKDF
Example
byte key[] = { // initialize with key };
byte salt[] = { // initialize with salt };
byte derivedKey[MAX_DIGEST_SIZE];
int ret = wc_HKDF_Expand_ex(WC_SHA512, key, sizeof(key), NULL, 0,
derivedKey, sizeof(derivedKey), NULL, INVALID_DEVID);
if ( ret != 0 ) {
// error generating derived key
}
function wc_Tls13_HKDF_Extract
int wc_Tls13_HKDF_Extract(
byte * prk,
const byte * salt,
word32 saltLen,
byte * ikm,
word32 ikmLen,
int digest
)
This function provides access to RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for TLS v1.3 key derivation.
Parameters:
- prk Generated pseudorandom key
- salt salt. May be NULL, in which case saltLen is ignored
- saltLen length of the salt
- ikm input keying material. May be NULL when ikmLen is 0
- ikmLen length of the input keying material. 0 substitutes a digest length zeroed IKM per RFC 8446, not an empty IKM
- digest hash type to use for the HKDF. Valid types are: WC_SHA256, WC_SHA384, WC_SHA512 or WC_SM3
See:
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see digest param), if prk is NULL, or if ikm is NULL and ikmLen is not 0
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
Example
byte ikm[] = { // initialize with input keying material };
byte salt[] = { // initialize with optional salt };
byte prk[MAX_DIGEST_SIZE];
int ret = wc_Tls13_HKDF_Extract(prk, salt, sizeof(salt), ikm, sizeof(ikm),
WC_SHA512);
if ( ret != 0 ) {
// error generating derived key
}
function wc_Tls13_HKDF_Extract_ex
int wc_Tls13_HKDF_Extract_ex(
byte * prk,
const byte * salt,
word32 saltLen,
byte * ikm,
word32 ikmLen,
int digest,
void * heap,
int devId
)
This function provides access to RFC 5869 HMAC-based Extract-and-Expand Key Derivation Function (HKDF) for TLS v1.3 key derivation. This is the _ex version adding heap hint and device identifier.
Parameters:
- prk Generated pseudorandom key
- salt Salt. May be NULL; saltLen is then ignored unless a crypto callback handles the request
- saltLen Length of the salt
- ikm Input keying material. May be NULL when ikmLen is 0
- ikmLen Length of the input keying material. 0 substitutes a digest length zeroed IKM per RFC 8446, not an empty IKM
- digest Hash type to use for the HKDF. Valid types are: WC_SHA256, WC_SHA384, WC_SHA512 or WC_SM3
- heap Heap hint to use for memory. Can be NULL
- devId ID to use with crypto callbacks or async hardware. Set to INVALID_DEVID (-2) if not used
See:
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see digest param), if prk is NULL, or if ikm is NULL and ikmLen is not 0
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
- WC_PENDING_E May be returned in a WOLF_CRYPTO_CB build when the registered crypto callback device has taken the request but not yet finished it; the caller re-invokes with identical arguments until the result is no longer WC_PENDING_E. The TLS 1.3 key schedule does this in WOLFSSL_ASYNC_CRYPT builds.
Example
byte ikm[] = { // initialize with input keying material };
byte salt[] = { // initialize with optional salt };
byte prk[MAX_DIGEST_SIZE];
int ret = wc_Tls13_HKDF_Extract_ex(prk, salt, sizeof(salt), ikm,
sizeof(ikm), WC_SHA512, NULL, INVALID_DEVID);
if ( ret != 0 ) {
// error generating derived key
}
function wc_Tls13_HKDF_Expand_Label_ex
int wc_Tls13_HKDF_Expand_Label_ex(
byte * okm,
word32 okmLen,
const byte * prk,
word32 prkLen,
const byte * protocol,
word32 protocolLen,
const byte * label,
word32 labelLen,
const byte * info,
word32 infoLen,
int digest,
void * heap,
int devId
)
Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation. This is the _ex version adding heap hint and device identifier.
Parameters:
- okm Generated pseudorandom key - output key material.
- okmLen Length of generated pseudorandom key - output key material.
- prk Salt - pseudo-random key.
- prkLen Length of the salt - pseudo-random key.
- protocol TLS protocol label.
- protocolLen Length of the TLS protocol label.
- info Information to expand.
- infoLen Length of the information.
- digest Hash type to use for the HKDF. Valid types are: WC_SHA256, WC_SHA384 or WC_SHA512
- heap Heap hint to use for memory. Can be NULL
- devId ID to use with crypto callbacks or async hardware. Set to INVALID_DEVID (-2) if not used
See:
- wc_HKDF
- wc_HKDF_Extract
- wc_HKDF_Extract_ex
- wc_HKDF_Expand
- wc_Tls13_HKDF_Expand_Label
- wc_Tls13_HKDF_Expand_Label_Alloc
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
- WC_PENDING_E May be returned in a WOLF_CRYPTO_CB build when the registered crypto callback device has taken the request but not yet finished it; the caller re-invokes with identical arguments until the result is no longer WC_PENDING_E. The TLS 1.3 key schedule does this in WOLFSSL_ASYNC_CRYPT builds.
function wc_Tls13_HKDF_Expand_Label
int wc_Tls13_HKDF_Expand_Label(
byte * okm,
word32 okmLen,
const byte * prk,
word32 prkLen,
const byte * protocol,
word32 protocolLen,
const byte * label,
word32 labelLen,
const byte * info,
word32 infoLen,
int digest
)
Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation.
Parameters:
- okm Generated pseudorandom key - output key material.
- okmLen Length of generated pseudorandom key - output key material.
- prk Salt - pseudo-random key.
- prkLen Length of the salt - pseudo-random key.
- protocol TLS protocol label.
- protocolLen Length of the TLS protocol label.
- info Information to expand.
- infoLen Length of the information.
- digest Hash type to use for the HKDF. Valid types are: WC_SHA256, WC_SHA384 or WC_SHA512
See:
- wc_HKDF
- wc_HKDF_Extract
- wc_HKDF_Extract_ex
- wc_HKDF_Expand
- wc_Tls13_HKDF_Expand_Label_ex
- wc_Tls13_HKDF_Expand_Label_Alloc
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
function wc_Tls13_HKDF_Expand_Label_Alloc
int wc_Tls13_HKDF_Expand_Label_Alloc(
byte * okm,
word32 okmLen,
const byte * prk,
word32 prkLen,
const byte * protocol,
word32 protocolLen,
const byte * label,
word32 labelLen,
const byte * info,
word32 infoLen,
int digest,
void * heap
)
This functions is very similar to wc_Tls13_HKDF_Expand_Label(), but it allocates memory if the stack space usually used isn't enough. Expand data using HMAC, salt and label and info. TLS v1.3 defines this function for key derivation. This is the _ex version adding heap hint and device identifier.
Parameters:
- okm Generated pseudorandom key - output key material.
- okmLen Length of generated pseudorandom key - output key material.
- prk Salt - pseudo-random key.
- prkLen Length of the salt - pseudo-random key.
- protocol TLS protocol label.
- protocolLen Length of the TLS protocol label.
- info Information to expand.
- infoLen Length of the information.
- digest Hash type to use for the HKDF. Valid types are: WC_SHA256, WC_SHA384 or WC_SHA512
- heap Heap hint to use for memory. Can be NULL
See:
- wc_HKDF
- wc_HKDF_Extract
- wc_HKDF_Extract_ex
- wc_HKDF_Expand
- wc_Tls13_HKDF_Expand_Label
- wc_Tls13_HKDF_Expand_Label_ex
Return:
- 0 Returned upon successfully generating a key with the given inputs
- BAD_FUNC_ARG Returned if an invalid hash type is given (see type param)
- MEMORY_E Returned if there is an error allocating memory
- HMAC_MIN_KEYLEN_E May be returned when using a FIPS implementation and the key length specified is shorter than the minimum acceptable FIPS standard
- WC_PENDING_E May be returned in a WOLF_CRYPTO_CB build when the registered crypto callback device has taken the request but not yet finished it; the caller re-invokes with identical arguments until the result is no longer WC_PENDING_E. The TLS 1.3 key schedule does this in WOLFSSL_ASYNC_CRYPT builds.
Source code
int wc_HmacInit(Hmac* hmac, void* heap, int devId);
int wc_HmacSetKey(Hmac* hmac, int type, const byte* key, word32 keySz);
int wc_HmacUpdate(Hmac* hmac, const byte* in, word32 sz);
int wc_HmacFinal(Hmac* hmac, byte* out);
int wolfSSL_GetHmacMaxSize(void);
int wc_HmacGenerate(int type, byte* out, word32* outSz,
const byte* in, word32 inSz,
const byte* key, word32 keySz);
int wc_HmacGenerate_ex(Hmac* hmac, int type, byte* out, word32* outSz,
const byte* in, word32 inSz,
const byte* key, word32 keySz,
void* heap, int devId);
int wc_HmacVerify(int type, const byte* check, word32 checkSz,
const byte* in, word32 inSz,
const byte* key, word32 keySz);
int wc_HmacVerify_ex(Hmac* hmac, int type, const byte* check, word32 checkSz,
const byte* in, word32 inSz,
const byte* key, word32 keySz,
void* heap, int devId);
int wc_HKDF(int type, const byte* inKey, word32 inKeySz,
const byte* salt, word32 saltSz,
const byte* info, word32 infoSz,
byte* out, word32 outSz);
int wc_HKDF_Extract(
int type,
const byte* salt, word32 saltSz,
const byte* inKey, word32 inKeySz,
byte* out);
int wc_HKDF_Extract_ex(
int type,
const byte* salt, word32 saltSz,
const byte* inKey, word32 inKeySz,
byte* out,
void* heap, int devId);
int wc_HKDF_Expand(
int type,
const byte* inKey, word32 inKeySz,
const byte* info, word32 infoSz,
byte* out, word32 outSz);
int wc_HKDF_Expand_ex(
int type,
const byte* inKey, word32 inKeySz,
const byte* info, word32 infoSz,
byte* out, word32 outSz,
void* heap, int devId);
int wc_Tls13_HKDF_Extract(
byte* prk,
const byte* salt, word32 saltLen,
byte* ikm, word32 ikmLen, int digest);
int wc_Tls13_HKDF_Extract_ex(
byte* prk,
const byte* salt, word32 saltLen,
byte* ikm, word32 ikmLen, int digest,
void* heap, int devId);
int wc_Tls13_HKDF_Expand_Label_ex(
byte* okm, word32 okmLen,
const byte* prk, word32 prkLen,
const byte* protocol, word32 protocolLen,
const byte* label, word32 labelLen,
const byte* info, word32 infoLen,
int digest,
void* heap, int devId);
int wc_Tls13_HKDF_Expand_Label(
byte* okm, word32 okmLen,
const byte* prk, word32 prkLen,
const byte* protocol, word32 protocolLen,
const byte* label, word32 labelLen,
const byte* info, word32 infoLen,
int digest);
int wc_Tls13_HKDF_Expand_Label_Alloc(
byte* okm, word32 okmLen,
const byte* prk, word32 prkLen,
const byte* protocol, word32 protocolLen,
const byte* label, word32 labelLen,
const byte* info, word32 infoLen,
int digest, void* heap);
Updated on 2026-12-01 at 01:13:09 +0000