Skip to content

Fail EVP_DigestSignFinal() for ECDSA when buffer is too small - #11924

Open
julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:fix-evp-digestsignfinal-ecc-siglen
Open

julek-wolfssl wants to merge 1 commit into
wolfSSL:masterfrom
julek-wolfssl:fix-evp-digestsignfinal-ecc-siglen

Conversation

@julek-wolfssl

Copy link
Copy Markdown
Member

The EC branch of EVP_DigestSignFinal() returned success without writing the signature when the DER encoding did not fit in *siglen, and it set *siglen past the end of the caller's buffer. It now fails in that case and leaves sig and *siglen unchanged.

The sig == NULL size query now uses wc_ecc_sig_size(), matching EVP_PKEY_size() and EVP_PKEY_sign(). The previous size * 2 + 8 calculation had two problems:

  • It was too small for brainpoolP512r1 (136 instead of 137).
  • It read ecc_sets[-1] for keys with explicit curve parameters.

The reported size is now the DER maximum. For P-521 it is 139 (was 140), and for prime239v1-3 it is 66 (was 68).

Fixes #11904

The EC branch returned success without writing the signature when the
DER encoding did not fit in *siglen, and set *siglen past the end of
the caller's buffer. Fail instead and leave sig and *siglen unchanged.

Get the sig == NULL size from wc_ecc_sig_size(), as EVP_PKEY_size() and
EVP_PKEY_sign() do. size * 2 + 8 was too small for brainpoolP512r1 (136
vs 137) and read ecc_sets[-1] for keys with explicit curve parameters.
The size is now the DER maximum: P-521 139 (was 140) and prime239v1-3
66 (was 68).

Fixes wolfSSL#11904

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The test guard enables Brainpool key generation in FIPS 6+ development configurations, where it is prohibited, causing the key-generation assertion to fail before signature sizing is exercised.

1 open issue
What changed in this PR

Fixes #11904 in wolfSSL’s OpenSSL compatibility layer by correcting ECDSA signature buffer handling and size queries.

Changes:

  • Rejects undersized signature buffers without changing the buffer or its reported length.
  • Uses wc_ecc_sig_size() for size queries, fixing Brainpool sizing and explicit-curve handling.
  • Adds regression checks for undersized buffers and the Brainpool P512 maximum signature size.
File Description
wolfcrypt/​src/​evp.c Corrects ECDSA buffer validation and signature sizing.
tests/​api/​test_evp_pkey.h Declares and registers the new size-query test.
tests/​api/​test_evp_pkey.c Adds buffer-failure checks and Brainpool sizing coverage.

Review effort: Balanced

Comment thread tests/api/test_evp_pkey.c
#if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_SHA256) && \
defined(HAVE_ECC_BRAINPOOL) && \
(defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)) && \
ECC_MIN_KEY_SZ <= 512
@github-actions

github-actions Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

@Marti2203

Copy link
Copy Markdown

Thanks Juliusz! I tested 0a71909 with both reproducers from #11904 (--enable-opensslextra): with a 16-byte buffer EVP_DigestSignFinal() now returns 0 and leaves *siglen and the buffer unchanged, and the size query still returns 72 for P-256. The evp_pkey group passes (18 passed, 41 skipped); test_wolfSSL_EVP_DigestSignFinal_ecc_size is skipped in that configuration since it has no brainpool curves, so I did not exercise the new size query for brainpoolP512r1. Using wc_ecc_sig_size() for the size query, so it matches EVP_PKEY_size(), makes sense to me.

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.

[Bug]: EVP_DigestSignFinal() with an EC key returns success but writes no signature when the buffer is too small

3 participants