Skip to content

Commit ed2df3d

Browse files
committed
Add STM32 DHUK crypto-callback ECDSA verify handler for callback-only ECC
1 parent 938761d commit ed2df3d

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

wolfcrypt/src/port/st/stm32.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,6 +3881,39 @@ static int Stm32Dhuk_PkSign(struct wc_CryptoInfo* info)
38813881
info->pk.eccsign.out, info->pk.eccsign.outlen,
38823882
info->pk.eccsign.rng);
38833883
}
3884+
3885+
#if defined(HAVE_ECC_VERIFY) && !defined(WC_STM32_PKA_SIGN_ONLY)
3886+
/* Route an ECDSA verify to the HW PKA. Verify uses the public key only, so it
3887+
* is not device-bound -- but under WOLF_CRYPTO_CB_ONLY_ECC the software verify
3888+
* in ecc.c is compiled out and verify is dispatched to this callback instead.
3889+
* The signature arrives DER-encoded; decode it to (r,s) for the HW call. */
3890+
static int Stm32Dhuk_PkVerify(struct wc_CryptoInfo* info)
3891+
{
3892+
ecc_key* key = info->pk.eccverify.key;
3893+
mp_int r;
3894+
mp_int s;
3895+
int ret;
3896+
3897+
if (key == NULL || info->pk.eccverify.sig == NULL ||
3898+
info->pk.eccverify.res == NULL) {
3899+
return CRYPTOCB_UNAVAILABLE;
3900+
}
3901+
XMEMSET(&r, 0, sizeof(r));
3902+
XMEMSET(&s, 0, sizeof(s));
3903+
3904+
/* DecodeECC_DSA_Sig() mp_init's r and s (mirrors the ecc.c verify path). */
3905+
ret = DecodeECC_DSA_Sig(info->pk.eccverify.sig,
3906+
info->pk.eccverify.siglen, &r, &s);
3907+
if (ret == 0) {
3908+
ret = stm32_ecc_verify_hash_ex(&r, &s, info->pk.eccverify.hash,
3909+
info->pk.eccverify.hashlen,
3910+
info->pk.eccverify.res, key);
3911+
}
3912+
mp_free(&r);
3913+
mp_free(&s);
3914+
return ret;
3915+
}
3916+
#endif /* HAVE_ECC_VERIFY && !WC_STM32_PKA_SIGN_ONLY */
38843917
#endif /* HAVE_ECC && HAVE_ECC_SIGN && WOLFSSL_STM32_PKA */
38853918

38863919
/* The crypto-callback device entry point (registered by wc_Stm32_DhukRegister).
@@ -3904,6 +3937,11 @@ static int Stm32_CryptoDevCb(int devId, struct wc_CryptoInfo* info, void* ctx)
39043937
if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
39053938
return Stm32Dhuk_PkSign(info);
39063939
}
3940+
#if defined(HAVE_ECC_VERIFY) && !defined(WC_STM32_PKA_SIGN_ONLY)
3941+
if (info->pk.type == WC_PK_TYPE_ECDSA_VERIFY) {
3942+
return Stm32Dhuk_PkVerify(info);
3943+
}
3944+
#endif
39073945
#ifdef WOLFSSL_STM32_CCB
39083946
/* Transparent provisioning: wc_ecc_make_key() on a WC_DHUK_DEVID
39093947
* key binds a fresh CCB-protected blob to it (no CCB-specific API). */

0 commit comments

Comments
 (0)