From 1fe4659ec3e73307740dc9b407f30b70e2fec2e1 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 3 Dec 2026 15:11:50 -0800 Subject: [PATCH 1/2] F-19063 - Keep the non-blocking RSA verify buffer owned by the key in the signature wrapper --- doc/dox_comments/header_files/signature.h | 26 +- tests/api/test_signature.c | 275 +++++++++++++++++++++- tests/api/test_signature.h | 6 +- wolfcrypt/src/signature.c | 132 +++++++++-- wolfssl/wolfcrypt/signature.h | 11 +- 5 files changed, 405 insertions(+), 45 deletions(-) diff --git a/doc/dox_comments/header_files/signature.h b/doc/dox_comments/header_files/signature.h index 7c488e7f241..50e822bd855 100644 --- a/doc/dox_comments/header_files/signature.h +++ b/doc/dox_comments/header_files/signature.h @@ -4,9 +4,9 @@ \brief This function returns the maximum size of the resulting signature. \return Returns SIG_TYPE_E if sig_type is not supported. Returns - BAD_FUNC_ARG if sig_type was invalid or key_len does not exactly match - the size of the expected key structure. A positive return value indicates - the maximum size of a signature. + BAD_FUNC_ARG if key is NULL, sig_type was invalid or key_len does not + exactly match the size of the expected key structure. A positive return + value indicates the maximum size of a signature. \param sig_type A signature type enum value such as WC_SIGNATURE_TYPE_ECC or WC_SIGNATURE_TYPE_RSA. @@ -15,7 +15,7 @@ WC_SIGNATURE_TYPE_RSA / WC_SIGNATURE_TYPE_RSA_W_ENC. The caller is responsible for ensuring the pointer refers to the correct type; this function cannot verify the actual runtime type of the object. - \param key_len If key is non-NULL, key_len must be exactly sizeof(ecc_key) + \param key_len key_len must be exactly sizeof(ecc_key) or sizeof(RsaKey) matching the sig_type. Passing any other value causes the function to return BAD_FUNC_ARG without dereferencing key. Always pass the size of the concrete key type at the call site: if you @@ -51,7 +51,8 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type, \return 0 Success \return SIG_TYPE_E -231, signature type not enabled/ available - \return BAD_FUNC_ARG -173, bad function argument provided + \return BAD_FUNC_ARG -173, bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E -132, output buffer too small or input too large. \return HASH_TYPE_E -232, hash type not available or, for RSA_W_ENC, without a DigestInfo OID @@ -106,7 +107,8 @@ int wc_SignatureVerify( \return 0 Success \return SIG_TYPE_E -231, signature type not enabled/ available - \return BAD_FUNC_ARG -173, bad function argument provided + \return BAD_FUNC_ARG -173, bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E -132, output buffer too small or input too large. \return HASH_TYPE_E -232, hash type not available or, for RSA_W_ENC, without a DigestInfo OID @@ -177,7 +179,8 @@ int wc_SignatureGenerate( \return 0 Success \return SIG_TYPE_E Signature type not enabled/available - \return BAD_FUNC_ARG Bad function argument provided + \return BAD_FUNC_ARG Bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E Output buffer too small or input too large \return ASN_PARSE_E RSA_W_ENC hash data is not the DigestInfo for hash_type @@ -236,7 +239,8 @@ int wc_SignatureVerifyHash(enum wc_HashType hash_type, \return 0 Success \return SIG_TYPE_E Signature type not enabled/available - \return BAD_FUNC_ARG Bad function argument provided + \return BAD_FUNC_ARG Bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E Output buffer too small or input too large \return ASN_PARSE_E RSA_W_ENC hash data is not the DigestInfo for hash_type @@ -297,7 +301,8 @@ int wc_SignatureGenerateHash(enum wc_HashType hash_type, \return 0 Success \return SIG_TYPE_E Signature type not enabled/available - \return BAD_FUNC_ARG Bad function argument provided + \return BAD_FUNC_ARG Bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E Output buffer too small or input too large \return ASN_PARSE_E RSA_W_ENC hash data is not the DigestInfo for hash_type @@ -357,7 +362,8 @@ int wc_SignatureGenerateHash_ex(enum wc_HashType hash_type, \return 0 Success \return SIG_TYPE_E Signature type not enabled/available - \return BAD_FUNC_ARG Bad function argument provided + \return BAD_FUNC_ARG Bad function argument provided, or hash_type + weaker than WC_SIG_MIN_HASH_TYPE \return BUFFER_E Output buffer too small or input too large \return HASH_TYPE_E Hash type not available or, for RSA_W_ENC, without a DigestInfo OID diff --git a/tests/api/test_signature.c b/tests/api/test_signature.c index 035db314d65..770bab16e6d 100644 --- a/tests/api/test_signature.c +++ b/tests/api/test_signature.c @@ -42,6 +42,24 @@ #include #include +/* Nominal digest size the wrapper rates WC_SIG_MIN_HASH_TYPE at, whether or + * not that hash is compiled in. */ +#define TEST_SIG_FLOOR_SZ \ + ((WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_MD2 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_MD4 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_MD5) ? 16 : \ + (WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_MD5_SHA) ? 20 : \ + (WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA224 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA512_224 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA3_224) ? 28 : \ + (WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA384 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA3_384) ? 48 : \ + (WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA512 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHA3_512 || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_BLAKE2B || \ + WC_SIG_MIN_HASH_TYPE == WC_HASH_TYPE_SHAKE256) ? 64 : 32) + /* Testing wc_SignatureGetSize() for signature type ECC */ int test_wc_SignatureGetSize_ecc(void) { @@ -73,7 +91,8 @@ int test_wc_SignatureGetSize_ecc(void) ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); sig_type = WC_SIGNATURE_TYPE_ECC; - ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0); + ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); key_len = (word32)0; ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -353,8 +372,7 @@ int test_wc_SignatureDecisionCoverage(void) /* Hash weaker than WC_SIG_MIN_HASH_TYPE (default SHA-256) * rejected by wc_SignatureCheckHashStrength(). Only assert * when SHA-1 is below the effective floor of this build. */ - if (wc_HashGetDigestSize(WC_SIG_MIN_HASH_TYPE) > - wc_HashGetDigestSize(WC_HASH_TYPE_SHA)) { + if (TEST_SIG_FLOOR_SZ > WC_SHA_DIGEST_SIZE) { ExpectIntEQ(wc_SignatureVerify(WC_HASH_TYPE_SHA, sig_type, data, data_len, sig, sig_len, &ecc, key_len), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); @@ -541,8 +559,7 @@ int test_wc_SignatureDecisionCoverage(void) /* Weak hash rejected before any hashing/signing occurs. Only * assert when SHA-1 is below the effective floor of this * build. */ - if (wc_HashGetDigestSize(WC_SIG_MIN_HASH_TYPE) > - wc_HashGetDigestSize(WC_HASH_TYPE_SHA)) { + if (TEST_SIG_FLOOR_SZ > WC_SHA_DIGEST_SIZE) { word32 lenCopy = (word32)eccSigMax; ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA, sig_type, data, data_len, genSig, &lenCopy, &ecc, key_len, &rng), @@ -613,12 +630,15 @@ int test_wc_SignatureDecisionCoverage(void) #if !defined(NO_ASN) && !defined(NO_SHA) /* Well-formed DigestInfo (built for a SHA-256-sized digest) but - * verified as SHA-1 -> ASN_PARSE_E */ + * verified as SHA-1 -> ASN_PARSE_E, or BAD_FUNC_ARG when SHA-1 is + * below the hash-strength floor */ { byte digest[WC_SHA256_DIGEST_SIZE] = {0}; byte encoded[MAX_DER_DIGEST_SZ] = {0}; int oid = 0; word32 encLen = 0; + int shaErr = (TEST_SIG_FLOOR_SZ > WC_SHA_DIGEST_SIZE) ? + WC_NO_ERR_TRACE(BAD_FUNC_ARG) : WC_NO_ERR_TRACE(ASN_PARSE_E); XMEMSET(digest, 0x11, sizeof(digest)); ExpectIntGE(oid = wc_HashGetOID(WC_HASH_TYPE_SHA256), 0); @@ -627,8 +647,7 @@ int test_wc_SignatureDecisionCoverage(void) ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA, WC_SIGNATURE_TYPE_RSA_W_ENC, encoded, encLen, sigBuf, - (word32)sizeof(sigBuf), &rsa_key, key_len), - WC_NO_ERR_TRACE(ASN_PARSE_E)); + (word32)sizeof(sigBuf), &rsa_key, key_len), shaErr); } #endif /* !NO_ASN && !NO_SHA */ @@ -845,6 +864,9 @@ int test_wc_SignatureFeatureCoverage(void) word32 sigDataLen = (word32)sizeof(sigData); #if !defined(NO_MD5) && !defined(NO_SHA) byte md5Sha[WC_MD5_DIGEST_SIZE + WC_SHA_DIGEST_SIZE]; + /* MD5||SHA-1 is rated as SHA-1 by the hash-strength floor */ + int md5ShaErr = (TEST_SIG_FLOOR_SZ > WC_SHA_DIGEST_SIZE) ? + WC_NO_ERR_TRACE(BAD_FUNC_ARG) : WC_NO_ERR_TRACE(HASH_TYPE_E); #endif #ifdef WOLFSSL_SM3 byte encSm3[18 + WC_SM3_DIGEST_SIZE] = { @@ -931,14 +953,14 @@ int test_wc_SignatureFeatureCoverage(void) sig_len = (word32)sizeof(sig); ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_MD5_SHA, WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sig, &sig_len, - &rsa_key, key_len, &rng, 0), WC_NO_ERR_TRACE(HASH_TYPE_E)); + &rsa_key, key_len, &rng, 0), md5ShaErr); ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_MD5_SHA, WC_SIGNATURE_TYPE_RSA_W_ENC, enc, encLen, sigData, sigDataLen, - &rsa_key, key_len), WC_NO_ERR_TRACE(HASH_TYPE_E)); + &rsa_key, key_len), md5ShaErr); sig_len = (word32)sizeof(sig); ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_MD5_SHA, WC_SIGNATURE_TYPE_RSA_W_ENC, data, data_len, sig, &sig_len, - &rsa_key, key_len, &rng), WC_NO_ERR_TRACE(HASH_TYPE_E)); + &rsa_key, key_len, &rng), md5ShaErr); #endif #ifdef WOLFSSL_SM3 @@ -964,3 +986,234 @@ int test_wc_SignatureFeatureCoverage(void) #endif /* !NO_SIG_WRAPPER */ return EXPECT_RESULT(); } /* END test_wc_SignatureFeatureCoverage() */ + +/* Hash-strength floor and signature length checks on all entry points */ +int test_wc_SignatureHashStrength(void) +{ + EXPECT_DECLS; +#if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && \ + defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY) && \ + !defined(NO_ECC256) && !defined(NO_SHA256) && !defined(WC_NO_RNG) + ecc_key ecc; + WC_RNG rng; + word32 key_len = (word32)sizeof(ecc_key); + byte data[32]; + byte hash[WC_SHA256_DIGEST_SIZE]; + byte sig[ECC_MAX_SIG_SIZE]; + word32 sig_len = (word32)sizeof(sig); + int min_sz; +#if !defined(NO_SHA) + byte sha1[WC_SHA_DIGEST_SIZE]; + byte sig2[ECC_MAX_SIG_SIZE]; + word32 sig2_len; +#endif + const char* qx = + "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0"; + const char* qy = + "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09"; + const char* d = + "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25"; + + XMEMSET(&ecc, 0, sizeof(ecc)); + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(data, 0x3c, sizeof(data)); + + ExpectIntEQ(wc_ecc_init(&ecc), 0); + ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0); + ExpectIntEQ(wc_InitRng(&rng), 0); + ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, data, (word32)sizeof(data), hash, + (word32)sizeof(hash)), 0); + + /* The floor applies by nominal size even when its hash is not + * compiled in. */ + min_sz = TEST_SIG_FLOOR_SZ; + if (min_sz > WC_SHA256_DIGEST_SIZE) { + ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig, &sig_len, + &ecc, key_len, &rng), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + sig_len = (word32)sizeof(sig); + ExpectIntEQ(wc_SignatureGenerateHash(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, hash, (word32)sizeof(hash), sig, &sig_len, + &ecc, key_len, &rng), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + } + else { + ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig, &sig_len, + &ecc, key_len, &rng), 0); + + /* sig_len past INT_MAX must not pass the maximum size check */ + ExpectIntEQ(wc_SignatureVerify(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig, + (word32)0x80000000UL, &ecc, key_len), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, hash, (word32)sizeof(hash), sig, + (word32)0xFFFFFFFFUL, &ecc, key_len), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + + #if !defined(NO_SHA) + if (min_sz > WC_SHA_DIGEST_SIZE) { + /* Pre-computed hash APIs enforce the floor too */ + ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA, data, (word32)sizeof(data), + sha1, (word32)sizeof(sha1)), 0); + sig2_len = (word32)sizeof(sig2); + ExpectIntEQ(wc_SignatureGenerateHash(WC_HASH_TYPE_SHA, + WC_SIGNATURE_TYPE_ECC, sha1, (word32)sizeof(sha1), sig2, + &sig2_len, &ecc, key_len, &rng), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + sig2_len = (word32)sizeof(sig2); + ExpectIntEQ(wc_SignatureGenerateHash_ex(WC_HASH_TYPE_SHA, + WC_SIGNATURE_TYPE_ECC, sha1, (word32)sizeof(sha1), sig2, + &sig2_len, &ecc, key_len, &rng, 0), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SignatureVerifyHash(WC_HASH_TYPE_SHA, + WC_SIGNATURE_TYPE_ECC, sha1, (word32)sizeof(sha1), sig, + sig_len, &ecc, key_len), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + + #if !defined(NO_MD5) + /* MD5||SHA-1 is rated as SHA-1, not by its 36-byte size */ + sig2_len = (word32)sizeof(sig2); + ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_MD5_SHA, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig2, + &sig2_len, &ecc, key_len, &rng), + WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + ExpectIntEQ(wc_SignatureVerify(WC_HASH_TYPE_MD5_SHA, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig, + sig_len, &ecc, key_len), WC_NO_ERR_TRACE(BAD_FUNC_ARG)); + #endif + } + #endif + } + + DoExpectIntEQ(wc_FreeRng(&rng), 0); + DoExpectIntEQ(wc_ecc_free(&ecc), 0); +#endif + return EXPECT_RESULT(); +} + +/* Non-blocking RSA through the signature wrapper: the caller re-calls on + * MP_WOULDBLOCK and the operation must not use a buffer freed between calls. + */ +int test_wc_SignatureRsaNonBlock(void) +{ + EXPECT_DECLS; +#if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA) && \ + defined(WC_RSA_NONBLOCK) && !defined(WOLFSSL_RSA_PUBLIC_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(NO_SHA256) && \ + !defined(WC_NO_RNG) && !defined(USE_CERT_BUFFERS_1024) && \ + (defined(USE_CERT_BUFFERS_2048) || !defined(NO_FILESYSTEM)) + RsaKey key; + RsaNb nb; + WC_RNG rng; + byte* der = NULL; + word32 derSz = 0; + word32 idx = 0; + word32 keyLen = (word32)sizeof(RsaKey); + byte data[32]; + byte hash[WC_SHA256_DIGEST_SIZE]; + byte sig[512]; + word32 sigLen = (word32)sizeof(sig); + byte* other = NULL; + word32 i; + int ret; +#if !defined(USE_CERT_BUFFERS_2048) + XFILE file = XBADFILE; +#endif + + XMEMSET(&key, 0, sizeof(key)); + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(data, 0x5a, sizeof(data)); + + /* Init first so the final wc_FreeRsaKey() is safe on any early failure. + * INVALID_DEVID: drive the non-blocking state machine directly. */ + ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0); + +#ifdef USE_CERT_BUFFERS_2048 + der = (byte*)client_key_der_2048; + derSz = (word32)sizeof_client_key_der_2048; +#else + ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, + DYNAMIC_TYPE_TMP_BUFFER)); + ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE); + if (der != NULL && file != XBADFILE) { + derSz = (word32)XFREAD(der, 1, FOURK_BUF, file); + } + if (file != XBADFILE) { + XFCLOSE(file); + } + ExpectIntGT(derSz, 0); +#endif + + ExpectIntEQ(wc_RsaPrivateKeyDecode(der, &idx, &key, derSz), 0); + ExpectIntEQ(wc_InitRng(&rng), 0); + ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, data, (word32)sizeof(data), hash, + (word32)sizeof(hash)), 0); + + ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_RSA, data, (word32)sizeof(data), sig, &sigLen, &key, + keyLen, &rng), 0); + + ExpectIntEQ(wc_RsaSetNonBlock(&key, &nb), 0); + + /* Valid signature. Between calls allocate a block the size of the + * wrapper's temporary buffer and check the resumed operation did not + * write into it. */ + do { + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen); + if (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK) && other == NULL) { + other = (byte*)XMALLOC(sigLen, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + if (other != NULL) { + XMEMSET(other, 0xa5, sigLen); + } + } + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, 0); + if (other != NULL) { + for (i = 0; i < sigLen; i++) { + if (other[i] != 0xa5) { + break; + } + } + ExpectIntEQ(i, sigLen); + XFREE(other, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); + other = NULL; + } + + do { + ret = wc_SignatureVerifyHash(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_RSA, hash, (word32)sizeof(hash), sig, sigLen, + &key, keyLen); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, 0); + + /* Altered data */ + data[0] ^= 0x01; + do { + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, WC_NO_ERR_TRACE(SIG_VERIFY_E)); + data[0] ^= 0x01; + + /* Generate, including the self-check verify, with the non-blocking key */ + XMEMSET(sig, 0, sizeof(sig)); + sigLen = (word32)sizeof(sig); + do { + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, + data, (word32)sizeof(data), sig, &sigLen, &key, keyLen, &rng); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, 0); + + ExpectIntEQ(wc_RsaSetNonBlock(&key, NULL), 0); + ExpectIntEQ(wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_RSA, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen), 0); + + DoExpectIntEQ(wc_FreeRng(&rng), 0); + DoExpectIntEQ(wc_FreeRsaKey(&key), 0); +#ifndef USE_CERT_BUFFERS_2048 + XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER); +#endif +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_signature.h b/tests/api/test_signature.h index 0ff8364619b..515af421c57 100644 --- a/tests/api/test_signature.h +++ b/tests/api/test_signature.h @@ -29,12 +29,16 @@ int test_wc_SignatureGetSize_rsa(void); int test_wc_falcon_sign_verify(void); int test_wc_SignatureDecisionCoverage(void); int test_wc_SignatureFeatureCoverage(void); +int test_wc_SignatureHashStrength(void); +int test_wc_SignatureRsaNonBlock(void); #define TEST_SIGNATURE_DECLS \ TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_ecc), \ TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_rsa), \ TEST_DECL_GROUP("signature", test_wc_falcon_sign_verify), \ TEST_DECL_GROUP("signature", test_wc_SignatureDecisionCoverage), \ - TEST_DECL_GROUP("signature", test_wc_SignatureFeatureCoverage) + TEST_DECL_GROUP("signature", test_wc_SignatureFeatureCoverage), \ + TEST_DECL_GROUP("signature", test_wc_SignatureHashStrength), \ + TEST_DECL_GROUP("signature", test_wc_SignatureRsaNonBlock) #endif /* WOLFCRYPT_TEST_SIGNATURE_H */ diff --git a/wolfcrypt/src/signature.c b/wolfcrypt/src/signature.c index e6bcf10214b..766f16fbff4 100644 --- a/wolfcrypt/src/signature.c +++ b/wolfcrypt/src/signature.c @@ -31,6 +31,12 @@ #ifndef NO_RSA #include #endif +#ifdef NO_INLINE + #include +#else + #define WOLFSSL_MISC_INCLUDED + #include +#endif /* Signature wrapper disabled check (NO_SIG_WRAPPER set by signature.h) */ #ifndef NO_SIG_WRAPPER @@ -46,20 +52,59 @@ #endif #endif +/* Nominal digest size in bytes, used to rate hash strength whether or not + * the hash is compiled in. MD5||SHA-1 is no stronger than SHA-1. */ +static int wc_SignatureHashNominalSize(enum wc_HashType hash_type) +{ + switch (hash_type) { + case WC_HASH_TYPE_MD2: + case WC_HASH_TYPE_MD4: + case WC_HASH_TYPE_MD5: + return 16; + case WC_HASH_TYPE_SHA: + case WC_HASH_TYPE_MD5_SHA: + return 20; + case WC_HASH_TYPE_SHA224: + case WC_HASH_TYPE_SHA512_224: + case WC_HASH_TYPE_SHA3_224: + return 28; + case WC_HASH_TYPE_SHA256: + case WC_HASH_TYPE_SHA512_256: + case WC_HASH_TYPE_SHA3_256: + case WC_HASH_TYPE_SM3: + case WC_HASH_TYPE_BLAKE2S: + case WC_HASH_TYPE_SHAKE128: + return 32; + case WC_HASH_TYPE_SHA384: + case WC_HASH_TYPE_SHA3_384: + return 48; + case WC_HASH_TYPE_SHA512: + case WC_HASH_TYPE_SHA3_512: + case WC_HASH_TYPE_BLAKE2B: + case WC_HASH_TYPE_SHAKE256: + return 64; + case WC_HASH_TYPE_NONE: + default: + return 0; + } +} + static int wc_SignatureCheckHashStrength(enum wc_HashType hash_type) { - int min_sz, this_sz; + int ret; + int min_sz; - min_sz = wc_HashGetDigestSize(WC_SIG_MIN_HASH_TYPE); - if (min_sz < 0) { - /* configured floor not compiled in - skip enforcement */ - return 0; + /* The requested hash must be compiled in */ + ret = wc_HashGetDigestSize(hash_type); + if (ret < 0) { + return ret; } - this_sz = wc_HashGetDigestSize(hash_type); - if (this_sz < 0) { - return this_sz; + min_sz = wc_SignatureHashNominalSize(WC_SIG_MIN_HASH_TYPE); + if (min_sz == 0) { + WOLFSSL_MSG("wc_Signature*: invalid WC_SIG_MIN_HASH_TYPE"); + return BAD_FUNC_ARG; } - if (this_sz < min_sz) { + if (wc_SignatureHashNominalSize(hash_type) < min_sz) { WOLFSSL_MSG("wc_Signature*: hash weaker than WC_SIG_MIN_HASH_TYPE"); return BAD_FUNC_ARG; } @@ -131,6 +176,10 @@ int wc_SignatureGetSize(enum wc_SignatureType sig_type, (void)key; (void)key_len; + if (key == NULL) { + return BAD_FUNC_ARG; + } + switch(sig_type) { case WC_SIGNATURE_TYPE_ECC: #ifdef HAVE_ECC @@ -210,11 +259,18 @@ int wc_SignatureVerifyHash( } /* Validate signature len (1 to max is okay) */ - if ((int)sig_len > wc_SignatureGetSize(sig_type, key, key_len)) { + ret = wc_SignatureGetSize(sig_type, key, key_len); + if (ret < 0 || sig_len > (word32)ret) { WOLFSSL_MSG("wc_SignatureVerify: Invalid sig type/len"); return BAD_FUNC_ARG; } + /* Reject hashes weaker than WC_SIG_MIN_HASH_TYPE (default SHA-256) */ + ret = wc_SignatureCheckHashStrength(hash_type); + if (ret != 0) { + return ret; + } + /* Validate hash size */ ret = wc_HashGetDigestSize(hash_type); if (ret < 0) { @@ -295,9 +351,26 @@ int wc_SignatureVerifyHash( ret = wc_AsyncWait(ret, &((RsaKey*)key)->asyncDev, WC_ASYNC_FLAG_CALL_AGAIN); #endif - if (ret >= 0) + if (ret >= 0) { + #if defined(WC_RSA_NONBLOCK) && !defined(WOLFSSL_NO_MALLOC) && \ + !defined(WOLFSSL_RSA_VERIFY_ONLY) && \ + !defined(WOLFSSL_RSA_VERIFY_INLINE) + /* A non-blocking operation resumes across calls, so the + * key must own its working buffer rather than this + * function's temporary one. */ + if (((RsaKey*)key)->nb != NULL) { + ret = wc_RsaSSL_Verify(sig, sig_len, plain_data, + plain_len, (RsaKey*)key); + if (ret >= 0) + plain_ptr = plain_data; + } + else + #endif + { ret = wc_RsaSSL_VerifyInline(plain_data, sig_len, &plain_ptr, (RsaKey*)key); + } + } } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E)); if (ret >= 0 && plain_ptr) { if ((word32)ret == hash_len && @@ -315,7 +388,7 @@ int wc_SignatureVerifyHash( else { ret = MEMORY_E; } - if (ret != 0) { + if (ret != 0 && ret != WC_NO_ERR_TRACE(MP_WOULDBLOCK)) { WOLFSSL_MSG("RSA Signature Verify failed!"); } #else @@ -355,7 +428,8 @@ int wc_SignatureVerify( } /* Validate signature len (1 to max is okay) */ - if ((int)sig_len > wc_SignatureGetSize(sig_type, key, key_len)) { + ret = wc_SignatureGetSize(sig_type, key, key_len); + if (ret < 0 || sig_len > (word32)ret) { WOLFSSL_MSG("wc_SignatureVerify: Invalid sig type/len"); return BAD_FUNC_ARG; } @@ -446,11 +520,22 @@ int wc_SignatureGenerateHash_ex( } /* Validate signature len (needs to be at least max) */ - if ((int)*sig_len < wc_SignatureGetSize(sig_type, key, key_len)) { + ret = wc_SignatureGetSize(sig_type, key, key_len); + if (ret < 0) { + WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type"); + return ret; + } + if (*sig_len < (word32)ret) { WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type/len"); return BAD_FUNC_ARG; } + /* Reject hashes weaker than WC_SIG_MIN_HASH_TYPE (default SHA-256) */ + ret = wc_SignatureCheckHashStrength(hash_type); + if (ret != 0) { + return ret; + } + /* Validate hash size */ ret = wc_HashGetDigestSize(hash_type); if (ret < 0) { @@ -521,8 +606,16 @@ int wc_SignatureGenerateHash_ex( } if (ret == 0 && verify) { - ret = wc_SignatureVerifyHash(hash_type, sig_type, hash_data, hash_len, - sig, *sig_len, key, key_len); + /* Complete the check here: a pending return would make the next call + * start a new signature. */ + do { + ret = wc_SignatureVerifyHash(hash_type, sig_type, hash_data, + hash_len, sig, *sig_len, key, key_len); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + if (ret != 0) { + ForceZero(sig, *sig_len); + *sig_len = 0; + } } return ret; @@ -560,7 +653,12 @@ int wc_SignatureGenerate_ex( } /* Validate signature len (needs to be at least max) */ - if ((int)*sig_len < wc_SignatureGetSize(sig_type, key, key_len)) { + ret = wc_SignatureGetSize(sig_type, key, key_len); + if (ret < 0) { + WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type"); + return ret; + } + if (*sig_len < (word32)ret) { WOLFSSL_MSG("wc_SignatureGenerate: Invalid sig type/len"); return BAD_FUNC_ARG; } diff --git a/wolfssl/wolfcrypt/signature.h b/wolfssl/wolfcrypt/signature.h index 0befafd4b02..eb34ed62a74 100644 --- a/wolfssl/wolfcrypt/signature.h +++ b/wolfssl/wolfcrypt/signature.h @@ -42,12 +42,11 @@ #define NO_SIG_WRAPPER #endif -/* Minimum hash strength accepted by the wc_SignatureVerify/Generate - * convenience APIs. Default is SHA-256 to keep MD5 and SHA-1 (both with - * known collision attacks) out of new code. Define WC_SIG_MIN_HASH_TYPE - * to a weaker wc_HashType (e.g. WC_HASH_TYPE_SHA) to opt back into legacy - * behavior. The lower-level wc_SignatureVerifyHash/wc_SignatureGenerateHash - * APIs are unaffected. */ +/* Minimum hash strength accepted by the wc_Signature* APIs, rated by + * nominal digest size (MD5||SHA-1 counts as SHA-1). Default is SHA-256 to + * keep MD5 and SHA-1 (both with known collision attacks) out of new code. + * Define WC_SIG_MIN_HASH_TYPE to a weaker wc_HashType (e.g. + * WC_HASH_TYPE_SHA) to opt back into legacy behavior. */ #ifndef WC_SIG_MIN_HASH_TYPE #define WC_SIG_MIN_HASH_TYPE WC_HASH_TYPE_SHA256 #endif From 27c132c79e8596cd80cde45ad9b0b1c5161074f4 Mon Sep 17 00:00:00 2001 From: David Garske Date: Thu, 3 Dec 2026 15:11:50 -0800 Subject: [PATCH 2/2] F-19269 - Return MP_WOULDBLOCK from the signature wrapper for non-blocking ECC keys --- tests/api/test_signature.c | 74 ++++++++++++++++++++++++++++++++++++++ tests/api/test_signature.h | 4 ++- wolfcrypt/src/signature.c | 4 ++- 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/tests/api/test_signature.c b/tests/api/test_signature.c index 770bab16e6d..032fd5a9f36 100644 --- a/tests/api/test_signature.c +++ b/tests/api/test_signature.c @@ -1217,3 +1217,77 @@ int test_wc_SignatureRsaNonBlock(void) #endif return EXPECT_RESULT(); } + +/* Non-blocking ECC through the signature wrapper: MP_WOULDBLOCK is returned + * to the caller, which calls again until the verify completes. */ +int test_wc_SignatureEccNonBlock(void) +{ + EXPECT_DECLS; +#if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && \ + defined(HAVE_ECC_SIGN) && defined(HAVE_ECC_VERIFY) && \ + defined(WC_ECC_NONBLOCK) && defined(WOLFSSL_HAVE_SP_ECC) && \ + defined(WOLFSSL_SP_NONBLOCK) && !defined(NO_ECC256) && \ + !defined(NO_SHA256) && !defined(WC_NO_RNG) + ecc_key key; + ecc_nb_ctx_t nb; + WC_RNG rng; + word32 keyLen = (word32)sizeof(ecc_key); + byte data[32]; + byte sig[ECC_MAX_SIG_SIZE]; + word32 sigLen = (word32)sizeof(sig); + int count = 0; + int ret; + const char* qx = + "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0"; + const char* qy = + "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09"; + const char* d = + "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25"; + + XMEMSET(&key, 0, sizeof(key)); + XMEMSET(&rng, 0, sizeof(rng)); + XMEMSET(data, 0x5a, sizeof(data)); + + /* INVALID_DEVID: drive the non-blocking state machine directly. */ + ExpectIntEQ(wc_ecc_init_ex(&key, HEAP_HINT, INVALID_DEVID), 0); + ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, "SECP256R1"), 0); + ExpectIntEQ(wc_InitRng(&rng), 0); + + ExpectIntEQ(wc_SignatureGenerate(WC_HASH_TYPE_SHA256, + WC_SIGNATURE_TYPE_ECC, data, (word32)sizeof(data), sig, &sigLen, &key, + keyLen, &rng), 0); + + ExpectIntEQ(wc_ecc_set_nonblock(&key, &nb), 0); + + do { + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen); + count++; + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, 0); + ExpectIntGT(count, 1); + + data[0] ^= 0x01; + do { + ret = wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, WC_NO_ERR_TRACE(SIG_VERIFY_E)); + data[0] ^= 0x01; + + /* Generate, including the self-check verify, with the non-blocking key */ + sigLen = (word32)sizeof(sig); + do { + ret = wc_SignatureGenerate(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, + data, (word32)sizeof(data), sig, &sigLen, &key, keyLen, &rng); + } while (ret == WC_NO_ERR_TRACE(MP_WOULDBLOCK)); + ExpectIntEQ(ret, 0); + + ExpectIntEQ(wc_ecc_set_nonblock(&key, NULL), 0); + ExpectIntEQ(wc_SignatureVerify(WC_HASH_TYPE_SHA256, WC_SIGNATURE_TYPE_ECC, + data, (word32)sizeof(data), sig, sigLen, &key, keyLen), 0); + DoExpectIntEQ(wc_FreeRng(&rng), 0); + DoExpectIntEQ(wc_ecc_free(&key), 0); +#endif + return EXPECT_RESULT(); +} diff --git a/tests/api/test_signature.h b/tests/api/test_signature.h index 515af421c57..f09a0f966ba 100644 --- a/tests/api/test_signature.h +++ b/tests/api/test_signature.h @@ -31,6 +31,7 @@ int test_wc_SignatureDecisionCoverage(void); int test_wc_SignatureFeatureCoverage(void); int test_wc_SignatureHashStrength(void); int test_wc_SignatureRsaNonBlock(void); +int test_wc_SignatureEccNonBlock(void); #define TEST_SIGNATURE_DECLS \ TEST_DECL_GROUP("signature", test_wc_SignatureGetSize_ecc), \ @@ -39,6 +40,7 @@ int test_wc_SignatureRsaNonBlock(void); TEST_DECL_GROUP("signature", test_wc_SignatureDecisionCoverage), \ TEST_DECL_GROUP("signature", test_wc_SignatureFeatureCoverage), \ TEST_DECL_GROUP("signature", test_wc_SignatureHashStrength), \ - TEST_DECL_GROUP("signature", test_wc_SignatureRsaNonBlock) + TEST_DECL_GROUP("signature", test_wc_SignatureRsaNonBlock), \ + TEST_DECL_GROUP("signature", test_wc_SignatureEccNonBlock) #endif /* WOLFCRYPT_TEST_SIGNATURE_H */ diff --git a/wolfcrypt/src/signature.c b/wolfcrypt/src/signature.c index 766f16fbff4..b6fd2ba14d4 100644 --- a/wolfcrypt/src/signature.c +++ b/wolfcrypt/src/signature.c @@ -311,7 +311,9 @@ int wc_SignatureVerifyHash( ret = wc_ecc_verify_hash(sig, sig_len, hash_data, hash_len, &is_valid_sig, (ecc_key*)key); } while (ret == WC_NO_ERR_TRACE(WC_PENDING_E)); - if (ret != 0 || is_valid_sig != 1) { + /* MP_WOULDBLOCK: non-blocking key, the caller calls again */ + if (ret != WC_NO_ERR_TRACE(MP_WOULDBLOCK) && + (ret != 0 || is_valid_sig != 1)) { ret = SIG_VERIFY_E; } #else