Skip to content

Cryptocb hooks rsapss ed448 cmac - #10886

Open
night1rider wants to merge 7 commits into
wolfSSL:masterfrom
night1rider:cryptocb-hooks-rsapss-ed448-cmac
Open

Cryptocb hooks rsapss ed448 cmac#10886
night1rider wants to merge 7 commits into
wolfSSL:masterfrom
night1rider:cryptocb-hooks-rsapss-ed448-cmac

Conversation

@night1rider

@night1rider night1rider commented Jul 12, 2026

Copy link
Copy Markdown
Contributor

Adds WOLF_CRYPTO_CB crypto callback hooks so a device can service three more operations:
Ed448 sign and verify, mirroring the existing Ed25519 hooks.
CMAC context free on wc_CmacFree (WOLF_CRYPTO_CB_FREE), letting a device release offload state.
RSA-PSS verify with the digest (WOLF_CRYPTO_CB_RSA_PAD) so the device performs the full signature and padding check.
Includes testwolfcrypt and API unit test coverage for each hook.

@night1rider night1rider self-assigned this Jul 12, 2026
@night1rider
night1rider marked this pull request as ready for review July 12, 2026 03:51
@github-actions

Copy link
Copy Markdown

retest this please

@github-actions

github-actions Bot commented Jul 12, 2026

Copy link
Copy Markdown

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10886

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

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

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

@night1rider
night1rider force-pushed the cryptocb-hooks-rsapss-ed448-cmac branch from 0108bd2 to 10370ac Compare July 12, 2026 04:45

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10886

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.

@night1rider
night1rider force-pushed the cryptocb-hooks-rsapss-ed448-cmac branch from 10370ac to 513619f Compare July 12, 2026 06:08

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10886

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

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

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

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

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #10886

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

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

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

Comment thread wolfcrypt/src/rsa.c Outdated
@night1rider
night1rider force-pushed the cryptocb-hooks-rsapss-ed448-cmac branch 2 times, most recently from b20a6df to dc84530 Compare July 20, 2026 22:42
@night1rider

Copy link
Copy Markdown
Contributor Author

jenkins retest this please

Comment thread wolfcrypt/src/ed448.c Outdated
Comment thread wolfcrypt/src/ed448.c Outdated
Comment thread wolfcrypt/src/rsa.c Outdated
Comment thread wolfcrypt/test/test.c Outdated
Comment thread wolfssl/wolfcrypt/rsa.h Outdated

@dgarske dgarske left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please resolve merge conflicts. Thanks

Comment thread wolfcrypt/src/cmac.c
Comment thread wolfcrypt/src/ed448.c Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommend we do this check for crypto callback too right? Maybe move it up higher? Also bring in the brace to same line if under 80 chars.

Comment thread wolfcrypt/src/ed448.c
if (key->devId != INVALID_DEVID)
#endif
{
ret = wc_CryptoCb_Ed448Verify(sig, sigLen, msg, msgLen, res, key, type,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Set *res = 0; before, just in case.

Comment thread wolfcrypt/src/ed448.c
#endif

/* sanity check on arguments */
if ((in == NULL) || (out == NULL) || (outLen == NULL) || (key == NULL) ||

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommendation: Relocate the WOLFSSL_CHECK_MEM_ZERO registration block to after the crypto-callback hook (mirroring wc_ed25519_sign_msg_ex), or alternatively goto a common exit label instead of returning directly. Add a --enable-cryptocb + WOLFSSL_CHECK_MEM_ZERO build to the validation matrix for this PR.

Comment thread wolfcrypt/src/rsa.c Outdated
* so report no inline output rather than a misleading pointer. */
if (out != NULL)
*out = NULL;
ret = (res != 0) ? (int)inLen : SIG_VERIFY_E;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wc_RsaPSS_VerifyCheck device path returns a different length and leaves out untouched, with no way for the caller to tell.

The software path returns the value of wc_RsaPSS_Verify_ex(), which ultimately comes from RsaUnPad_PSS (rsa.c:2024): return saltLen + hLen; — 64 for SHA-256 with a 32-byte salt — and it fills the caller's out buffer with that recovered PSS block. The new crypto-callback path returns (int)inLen — 256 for a 2048-bit key — and never writes out at all.

Recommendation: Do not return inLen as if it were the recovered-PSS-block length. Either have the device report the actual output length and fill out, or define an unambiguous "device verified, no recovered data" signal. Whichever is chosen, document it in doc/dox_comments/header_files/rsa.h for wc_RsaPSS_VerifyCheck as well as wc_RsaPSS_VerifyCheckInline, and add a test that asserts the device return value equals the software return value (or the documented sentinel).

Comment thread tests/api/test_rsa.c Outdated
ExpectIntEQ(wc_RsaSetRNG(key, &rng), 0);

/* PSS sign runs in software (device declines). */
ExpectIntGT(sigLen = (word32)wc_RsaPSS_Sign(digest,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Buffer overflow in new RSA-PSS test when wc_RsaPSS_Sign fails: word32 sigLen holds a cast negative error code

Recommendation: Capture the signature length in a signed int and only widen to word32 after confirming it is positive, and additionally gate the plain-if inline sub-test on EXPECT_SUCCESS() so it does not run after a failure.

Comment thread wolfcrypt/src/rsa.c
*out = NULL;
ret = (res != 0) ? (int)inLen : SIG_VERIFY_E;
}
return ret;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Recommendation: Explicitly map any positive device return to SIG_VERIFY_E (fail closed), and document in cryptocb.h that a WC_PK_TYPE_RSA_PSS_VERIFY handler must return exactly 0 (with *res set) or a negative error.

Comment thread wolfcrypt/src/cmac.c
if (cmac->devId != INVALID_DEVID)
#endif
{
(void)wc_CryptoCb_Free(cmac->devId, WC_ALGO_TYPE_CMAC, (int)cmac->type,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Reset cmac->devId = INVALID_DEVID after the ForceZero so a repeat wc_CmacFree() is inert

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Writing devId back after the ForceZero breaks WOLFSSL_CHECK_MEM_ZERO, which requires the freed struct to be all zero. On the repeat-free concern: with nothing registered at devId 0 the dispatch finds no device and is a no-op, and if something is registered there it receives a fully zeroed context with a NULL devCtx and nothing to release. wc_AesFree and wc_Sha256Free behave the same way, so I left CMAC consistent with them.

Comment thread wolfcrypt/test/test.c Outdated
WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
#endif

#if defined(WC_RSA_PSS) && defined(WOLF_CRYPTO_CB_RSA_PAD)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Add the same WOLF_CRYPTO_CB_ONLY_RSA escape as the sibling branches, or add && !defined(WOLF_CRYPTO_CB_ONLY_RSA) to the block guard.

Add WOLF_CRYPTO_CB dispatch hooks so a device can service:
* Ed448 sign and verify, mirroring the existing Ed25519 hooks.
* CMAC context free on wc_CmacFree (WOLF_CRYPTO_CB_FREE), letting a device
  release offload state.
* RSA-PSS verify with the digest (WOLF_CRYPTO_CB_RSA_PAD) so the device does
  the full signature and padding check. On that path *out is set to NULL with a
  positive return, documented in rsa.h.

Includes testwolfcrypt and API unit test coverage for each hook.
The PSS hook can hand back the recovered block through out/outSz/outLen.
A device that reports only a verdict leaves outLen at 0; wolfSSL then
zeroes the buffer and returns saltLen + hLen, and rejects a buffer
smaller than that with RSA_BUFFER_E. A reported length is clamped to the
buffer size, and any positive handler return maps to SIG_VERIFY_E.

Move the Ed448 sign WOLFSSL_CHECK_MEM_ZERO registration below the crypto
callback hook so the device path no longer returns past it, and guard the
RSA-PSS test callback against WOLF_CRYPTO_CB_ONLY_RSA.

Adds tests for the recovered-data, over-claimed-length and undersized
buffer paths, and an os-check config that builds the hooks under
WOLFSSL_CHECK_MEM_ZERO.
@night1rider
night1rider force-pushed the cryptocb-hooks-rsapss-ed448-cmac branch from da24c28 to 375febc Compare August 8, 2026 21:18
@night1rider

Copy link
Copy Markdown
Contributor Author

Jenkins retest this please

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.

5 participants