Skip to content

Create individual crypto callbacks for hkdf extract and hkdf expand#10598

Open
twcook86 wants to merge 1 commit into
wolfSSL:masterfrom
twcook86:hkdf_cryptocb_split
Open

Create individual crypto callbacks for hkdf extract and hkdf expand#10598
twcook86 wants to merge 1 commit into
wolfSSL:masterfrom
twcook86:hkdf_cryptocb_split

Conversation

@twcook86
Copy link
Copy Markdown
Contributor

@twcook86 twcook86 commented Jun 4, 2026

Description

Fixes zd#21919

Testing

built-in test.c expanded

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refines the crypto-callback (WOLF_CRYPTO_CB) KDF interface by splitting HKDF into distinct callback operations for HKDF-Extract and HKDF-Expand, and updates library internals and tests accordingly.

Changes:

  • Introduces new KDF callback types and wc_CryptoInfo payloads for HKDF extract/expand.
  • Adds new internal crypto-callback dispatch helpers (wc_CryptoCb_Hkdf_Extract / wc_CryptoCb_Hkdf_Expand) and wires them into HKDF code paths.
  • Expands HKDF tests to exercise extract+expand flows in addition to one-shot HKDF.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
wolfssl/wolfcrypt/types.h Adds new wc_KdfType values for HKDF extract/expand.
wolfssl/wolfcrypt/cryptocb.h Extends wc_CryptoInfo KDF union with HKDF extract/expand structs and declares new callback helpers.
wolfcrypt/src/hmac.c Routes HKDF Extract/Expand through crypto callbacks when available.
wolfcrypt/src/cryptocb.c Implements new crypto-callback helper functions and updates debug strings for KDF type.
wolfcrypt/test/test.c Adds HKDF extract/expand test coverage and updates test crypto-device callback handling.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread wolfssl/wolfcrypt/types.h
Comment on lines 1441 to +1446
WC_KDF_TYPE_NONE = 0,
WC_KDF_TYPE_HKDF = 1,
WC_KDF_TYPE_TWOSTEP_CMAC = 2 /* NIST SP 800-56C two-step cmac kdf. */
/* Future: WC_KDF_TYPE_PBKDF2 = 3, WC_KDF_TYPE_SCRYPT = 4, etc. */
WC_KDF_TYPE_HKDF_EXTRACT = 2,
WC_KDF_TYPE_HKDF_EXPAND = 3,
WC_KDF_TYPE_TWOSTEP_CMAC = 4 /* NIST SP 800-56C two-step cmac kdf. */
/* Future: WC_KDF_TYPE_PBKDF2 = 5, WC_KDF_TYPE_SCRYPT = 6, etc. */
Comment thread wolfcrypt/src/hmac.c
Comment on lines +1835 to +1839
#ifdef WOLF_CRYPTO_CB
/* Try crypto callback first for complete operation */
if (devId != INVALID_DEVID) {
ret = wc_CryptoCb_Hkdf_Expand(type, inKey, inKeySz, info, infoSz,
out, outSz, devId);
Copy link
Copy Markdown
Contributor

@bigbrett bigbrett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer you append-only to public structs, but its fine as is since it is only used by CryptoCbs and those aren't ABI restricted

Comment thread wolfssl/wolfcrypt/types.h
Comment on lines +1443 to +1444
WC_KDF_TYPE_HKDF_EXTRACT = 2,
WC_KDF_TYPE_HKDF_EXPAND = 3,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I would try to not modify existing public enum values once they are declared, just in case they are used by customers.

In this case I think it is fine as I believe it is ONLY used internally. But something to keep in mind going forward

Copy link
Copy Markdown

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fenrir Automated Review — PR #10598

Scan targets checked: wolfcrypt-bugs, wolfcrypt-port-bugs, wolfcrypt-rs-bugs, wolfcrypt-src, wolfssl-bugs, wolfssl-src

Findings: 3
3 finding(s) posted as inline comments (see file-level comments below)

This review was generated automatically by Fenrir. Findings are non-blocking.

Comment thread wolfssl/wolfcrypt/types.h
/* Future: WC_KDF_TYPE_PBKDF2 = 3, WC_KDF_TYPE_SCRYPT = 4, etc. */
WC_KDF_TYPE_HKDF_EXTRACT = 2,
WC_KDF_TYPE_HKDF_EXPAND = 3,
WC_KDF_TYPE_TWOSTEP_CMAC = 4 /* NIST SP 800-56C two-step cmac kdf. */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 [Medium] Existing KDF enum value is renumbered · API contract violations

WC_KDF_TYPE_TWOSTEP_CMAC changes from value 2 to 4, so existing crypto callback implementations that dispatch on the public enum value stop recognizing two-step CMAC KDF requests.

Fix: Keep WC_KDF_TYPE_TWOSTEP_CMAC at 2 and assign the new HKDF split values after existing enum constants.

Comment thread wolfcrypt/src/hmac.c
#ifdef WOLF_CRYPTO_CB
/* Try crypto callback first for complete operation */
if (devId != INVALID_DEVID) {
ret = wc_CryptoCb_Hkdf_Expand(type, inKey, inKeySz, info, infoSz,
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟠 [Medium] HKDF expand callback bypasses argument validation · Incorrect error handling

wc_HKDF_Expand_ex() dispatches to the new expand callback before the existing out == NULL and 255-block length checks, so invalid calls can bypass BAD_FUNC_ARG or reach a callback with invalid buffers.

Fix: Move the callback dispatch after wc_HmacSizeByType() and the out/length validation.

Comment thread wolfssl/wolfcrypt/types.h
/* Future: WC_KDF_TYPE_PBKDF2 = 3, WC_KDF_TYPE_SCRYPT = 4, etc. */
WC_KDF_TYPE_HKDF_EXTRACT = 2,
WC_KDF_TYPE_HKDF_EXPAND = 3,
WC_KDF_TYPE_TWOSTEP_CMAC = 4 /* NIST SP 800-56C two-step cmac kdf. */
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 [Low] Public CMAC KDF callback type value changed · API contract violations

WC_KDF_TYPE_TWOSTEP_CMAC changed from value 2 to 4, so existing crypto callbacks built against the public KDF enum no longer recognize CMAC KDF requests sent by wc_CryptoCb_Kdf_TwostepCmac.

Fix: Keep WC_KDF_TYPE_TWOSTEP_CMAC = 2 and assign the new HKDF split values after it.

Copy link
Copy Markdown
Contributor

@billphipps billphipps left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move the check for cryptocb into the hdkf functions, not in the side that is using it. Check on unit testing the interface as well. Looks like a great start!

Comment thread wolfcrypt/src/hmac.c
Comment on lines +1761 to +1770
#ifdef WOLF_CRYPTO_CB
/* Try crypto callback first */
if (devId != INVALID_DEVID) {
ret = wc_CryptoCb_Hkdf_Extract(type, salt, saltSz, inKey, inKeySz,
out, devId);
if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
return ret;
}
#endif

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to the wc_Hkdf_Extract public function so ANY potential invocation will trigger this check/offload. This means that if someone invokes the one-shot, it will try to offload the one-shot. If the one-shot is not offloaded, then the each of the extract/expand will be subsequently attempted to be offloaded since the SW version of the one-shot invokes the Expand/Extract directly. This allows a customer to invoke the one-shot separately since it may optimize better.

Comment thread wolfcrypt/src/cryptocb.c
Comment on lines +2535 to +2536
int wc_CryptoCb_Hkdf_Extract(int hashType, const byte* salt, word32 saltSz,
const byte* inKey, word32 inKeySz, byte* out, int devId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are all of these parameters necessary? Please confirm. Fill in NULL/0 as necessary in the shared cryptoInfo structure below.

Comment thread wolfcrypt/src/cryptocb.c
Comment on lines +2563 to +2565
int wc_CryptoCb_Hkdf_Expand(int hashType, const byte* inKey, word32 inKeySz,
const byte* info, word32 infoSz, byte* out, word32 outSz,
int devId)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Samme comment about if all of these are necessary

Comment thread wolfcrypt/test/test.c
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider looking at the test/swdev series of tests to support testing of the cryptocb plumbing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants