@@ -296,7 +296,14 @@ ECC Curve Sizes:
296296 #undef HAVE_ECC_VERIFY_HELPER
297297 #define HAVE_ECC_VERIFY_HELPER
298298#endif
299- #if defined(WOLFSSL_SE050_NO_ECDSA_VERIFY) && defined(HAVE_ECC_VERIFY)
299+ /* Compile in the software verify helper whenever SE050 hardware ECDSA verify is
300+ * bypassed:
301+ * - WOLFSSL_SE050_NO_ECDSA_VERIFY disables SE050 ECDSA verify outright.
302+ * - WOLFSSL_SE050_ONLY_KEY_ID verifies software keys (keyIdSet == 0) in
303+ * wolfCrypt; the SE050 is used at runtime only for keys resident in HW. */
304+ #if (defined(WOLFSSL_SE050_NO_ECDSA_VERIFY) || \
305+ (defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_ONLY_KEY_ID))) && \
306+ defined(HAVE_ECC_VERIFY)
300307 #define HAVE_ECC_VERIFY_HELPER
301308#endif
302309
@@ -4728,13 +4735,15 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
47284735 word32* outlen)
47294736{
47304737 int err = 0;
4738+ int privateKeyOk = 0;
47314739
47324740#if defined(WOLFSSL_CRYPTOCELL) && !defined(WOLFSSL_ATECC508A) && \
47334741 !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_MICROCHIP_TA100)
47344742 CRYS_ECDH_TempData_t tempBuff;
47354743#endif
47364744
47374745 (void)err;
4746+ (void)privateKeyOk;
47384747
47394748 if (private_key == NULL || public_key == NULL || out == NULL ||
47404749 outlen == NULL) {
@@ -4757,8 +4766,16 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
47574766 return NO_VALID_DEVID;
47584767#else /* !WOLF_CRYPTO_CB_ONLY_ECC */
47594768 /* type valid? */
4760- if (private_key->type != ECC_PRIVATEKEY &&
4761- private_key->type != ECC_PRIVATEKEY_ONLY) {
4769+ privateKeyOk = private_key->type == ECC_PRIVATEKEY ||
4770+ private_key->type == ECC_PRIVATEKEY_ONLY;
4771+ #if defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDHE) && \
4772+ defined(WOLFSSL_SE050_ONLY_KEY_ID)
4773+ /* A SE050 key-id handle can represent a private key even though
4774+ * wc_ecc_use_key_id() only loads the public point into the ecc_key. */
4775+ if (private_key->keyIdSet)
4776+ privateKeyOk = 1;
4777+ #endif
4778+ if (!privateKeyOk) {
47624779 return ECC_BAD_ARG_E;
47634780 }
47644781
@@ -4801,6 +4818,15 @@ int wc_ecc_shared_secret(ecc_key* private_key, ecc_key* public_key, byte* out,
48014818 err = silabs_ecc_shared_secret(private_key, public_key, out, outlen);
48024819#elif defined(WOLFSSL_KCAPI_ECC)
48034820 err = KcapiEcc_SharedSecret(private_key, public_key, out, outlen);
4821+ #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDHE) && \
4822+ defined(WOLFSSL_SE050_ONLY_KEY_ID)
4823+ /* SE050-resident private key uses hardware ECDH; a software private key
4824+ * (keyIdSet == 0) uses the wolfCrypt software implementation. */
4825+ if (private_key->keyIdSet)
4826+ err = se050_ecc_shared_secret(private_key, public_key, out, outlen);
4827+ else
4828+ err = wc_ecc_shared_secret_ex(private_key, &public_key->pubkey, out,
4829+ outlen);
48044830#elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDHE)
48054831 err = se050_ecc_shared_secret(private_key, public_key, out, outlen);
48064832#else
@@ -5881,7 +5907,8 @@ static int _ecc_make_key_ex(WC_RNG* rng, int keysize, ecc_key* key,
58815907 else {
58825908 err = NOT_COMPILED_IN;
58835909 }
5884- #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDHE)
5910+ #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDHE) && \
5911+ !defined(WOLFSSL_SE050_ONLY_KEY_ID)
58855912 err = se050_ecc_create_key(key, key->dp->id, key->dp->size);
58865913 key->type = ECC_PRIVATEKEY;
58875914#elif defined(WOLFSSL_CRYPTOCELL)
@@ -6981,7 +7008,14 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
69817008 }
69827009
69837010/* hardware crypto */
6984- #if defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
7011+ #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_ONLY_KEY_ID)
7012+ /* Route by key location: SE050-resident keys sign in hardware, software
7013+ * keys (keyIdSet == 0) sign with the wolfCrypt software implementation. */
7014+ if (key->keyIdSet)
7015+ err = wc_ecc_sign_hash_hw(in, inlen, r, s, out, outlen, rng, key);
7016+ else
7017+ err = wc_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
7018+ #elif defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
69857019 defined(WOLFSSL_MICROCHIP_TA100) || \
69867020 defined(PLUTON_CRYPTO_ECC) || defined(WOLFSSL_CRYPTOCELL) || \
69877021 defined(WOLFSSL_SILABS_SE_ACCEL) || defined(WOLFSSL_KCAPI_ECC) || \
@@ -9753,8 +9787,11 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
97539787#elif defined(WOLFSSL_XILINX_CRYPT_VERSAL)
97549788 byte sigRS[ECC_MAX_CRYPTO_HW_SIZE * 2];
97559789 byte hashcopy[ECC_MAX_CRYPTO_HW_SIZE] = {0};
9756- #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDSA_VERIFY)
9790+ #elif defined(WOLFSSL_SE050) && !defined(WOLFSSL_SE050_NO_ECDSA_VERIFY) && \
9791+ !defined(WOLFSSL_SE050_ONLY_KEY_ID)
97579792#else
9793+ /* Software verify helper (also used for SE050 software keys under
9794+ * WOLFSSL_SE050_ONLY_KEY_ID) needs the curve specs. */
97589795 int curveLoaded = 0;
97599796 DECLARE_CURVE_SPECS(ECC_CURVE_FIELD_COUNT);
97609797#endif
@@ -9789,6 +9826,15 @@ int wc_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
97899826 return err;
97909827 }
97919828
9829+ #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_ONLY_KEY_ID) && \
9830+ !defined(WOLFSSL_SE050_NO_ECDSA_VERIFY)
9831+ /* Key resident in the SE050: verify in hardware. Software keys fall through
9832+ * to the wolfCrypt verify helper below. */
9833+ if (key->keyIdSet) {
9834+ return se050_ecc_verify_hash_ex(hash, hashlen, r, s, key, res);
9835+ }
9836+ #endif
9837+
97929838 keySz = (word32)key->dp->size;
97939839
97949840#if defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_ECC) && \
0 commit comments