@@ -6535,6 +6535,27 @@ int wc_ecc_make_key_ex2(WC_RNG* rng, int keysize, ecc_key* key, int curve_id,
65356535{
65366536 int err;
65376537
6538+ #if FIPS_VERSION3_GE(7,0,0)
6539+ /* SP 800-131A Rev. 2 Table 2 disallows ECDSA key pair generation
6540+ * providing fewer than 112 bits of security strength (len(n) < 224), and
6541+ * SP 800-186 Section 3.2.1.1 retains P-192 for legacy use only, so
6542+ * refuse to generate on any curve smaller than 224 bits. The bound
6543+ * mirrors wc_ecc_set_curve(): an explicit curve_id selects the curve
6544+ * directly, otherwise keysize selects the smallest compiled curve that
6545+ * fits, so keysize 0 would land on the smallest compiled curve and is
6546+ * rejected too. Signature verification with the small curves stays
6547+ * available (legacy use per SP 800-131A Rev. 2 Table 2). */
6548+ if (curve_id > ECC_CURVE_DEF) {
6549+ if (wc_ecc_get_curve_size_from_id(curve_id) < WC_ECC_FIPS_GEN_MIN) {
6550+ return ECC_CURVE_OID_E;
6551+ }
6552+ }
6553+ else if (keysize < WC_ECC_FIPS_GEN_MIN) {
6554+ /* Same code the fips.c keygen wrappers return for this case. */
6555+ return BAD_FUNC_ARG;
6556+ }
6557+ #endif
6558+
65386559 err = _ecc_make_key_ex(rng, keysize, key, curve_id, flags);
65396560
65406561#if (FIPS_VERSION_GE(5,0) || defined(WOLFSSL_VALIDATE_ECC_KEYGEN)) && \
@@ -7232,6 +7253,25 @@ static int wc_ecc_sign_hash_async(const byte* in, word32 inlen, byte* out,
72327253}
72337254#endif /* WOLFSSL_ASYNC_CRYPT && WC_ASYNC_ENABLE_ECC */
72347255
7256+ #if FIPS_VERSION3_GE(7,0,0)
7257+ /* SP 800-131A Rev. 2 Table 2: ECDSA digital signature generation with
7258+ * len(n) < 224 is disallowed, while signature verification with those
7259+ * curves remains legacy use, so only the signing paths are gated. Every
7260+ * signing entry point calls this ahead of any crypto-callback or hardware
7261+ * dispatch, so no backend signs on a too-small curve. The curve is resolved
7262+ * from key->dp, never from ecc_sets[key->idx], so a custom-curve key
7263+ * (idx == ECC_CUSTOM_IDX) cannot index out of range. A key with no curve
7264+ * set is left to the caller's own argument checks; wc_ecc_sign_hash()
7265+ * checks again after a crypto callback has had the chance to resolve it. */
7266+ static int ecc_fips_sign_curve_allowed(const ecc_key* key)
7267+ {
7268+ if ((key->dp != NULL) && (key->dp->size < WC_ECC_FIPS_GEN_MIN)) {
7269+ return SIG_TYPE_E;
7270+ }
7271+ return 0;
7272+ }
7273+ #endif /* FIPS_VERSION3_GE(7,0,0) */
7274+
72357275/**
72367276 Sign a message digest
72377277 in The message digest to sign
@@ -7267,14 +7307,33 @@ int wc_ecc_sign_hash(const byte* in, word32 inlen, byte* out, word32 *outlen,
72677307 return BAD_LENGTH_E;
72687308 }
72697309
7310+ #if FIPS_VERSION3_GE(7,0,0)
7311+ err = ecc_fips_sign_curve_allowed(key);
7312+ if (err != 0) {
7313+ return err;
7314+ }
7315+ #endif
7316+
72707317#ifdef WOLF_CRYPTO_CB
72717318 #ifndef WOLF_CRYPTO_CB_FIND
72727319 if (key->devId != INVALID_DEVID)
72737320 #endif
72747321 {
72757322 err = wc_CryptoCb_EccSign(in, inlen, out, outlen, rng, key);
7276- if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE))
7323+ if (err != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) {
7324+ #if FIPS_VERSION3_GE(7,0,0)
7325+ /* A key referenced by id or label has no curve until the
7326+ * callback resolves it, so it passed the check above. Check
7327+ * again and do not release a signature made on a too-small
7328+ * curve. */
7329+ if ((err == 0) && (ecc_fips_sign_curve_allowed(key) != 0)) {
7330+ ForceZero(out, *outlen);
7331+ *outlen = 0;
7332+ err = SIG_TYPE_E;
7333+ }
7334+ #endif
72777335 return err;
7336+ }
72787337 /* fall-through when unavailable */
72797338 }
72807339#endif
@@ -7498,6 +7557,12 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
74987557 return ECC_BAD_ARG_E;
74997558#endif
75007559
7560+ #if FIPS_VERSION3_GE(7,0,0)
7561+ if (ecc_fips_sign_curve_allowed(key) != 0) {
7562+ return SIG_TYPE_E;
7563+ }
7564+ #endif
7565+
75017566 return stm32_ecc_sign_hash_ex(in, inlen, rng, key, r, s);
75027567}
75037568
@@ -7896,6 +7961,14 @@ int wc_ecc_sign_hash_ex(const byte* in, word32 inlen, WC_RNG* rng,
78967961 return ECC_BAD_ARG_E;
78977962 }
78987963
7964+ #if FIPS_VERSION3_GE(7,0,0)
7965+ /* wc_ecc_sign_hash_ex() is public, so it is gated as well as
7966+ * wc_ecc_sign_hash(). */
7967+ if (ecc_fips_sign_curve_allowed(key) != 0) {
7968+ return SIG_TYPE_E;
7969+ }
7970+ #endif
7971+
78997972#if defined(WOLFSSL_SP_MATH)
79007973 if (key->idx == ECC_CUSTOM_IDX || (1
79017974 #ifndef WOLFSSL_SP_NO_256
0 commit comments