Skip to content

JCE: validate RSA keygen size and public exponent at KeyPairGenerator.initialize() - #295

Merged
rlm2002 merged 1 commit into
wolfSSL:masterfrom
cconlon:rsaKeyGenSpecChecks
Oct 19, 2026
Merged

rlm2002 merged 1 commit into
wolfSSL:masterfrom
cconlon:rsaKeyGenSpecChecks

Conversation

@cconlon

@cconlon cconlon commented Oct 8, 2026

Copy link
Copy Markdown
Member

This PR adjusts KeyPairGenerator.initialize(RSAKeyGenParameterSpec) to throw InvalidAlgorithmParameterException for RSA key sizes or public exponents that native key generation would reject. Before, these failed later in generateKeyPair() with an unchecked WolfCryptException (BAD_FUNC_ARG).

  • Rejects key sizes below Rsa.RSA_MIN_SIZE, matching the existing initialize(int) check.
  • Adds Rsa.RSA_MIN_EXPONENT: WC_RSA_EXPONENT (65537) on FIPS builds, since FIPS 186-4/186-5 key generation requires e > 2^16, and 3 otherwise. This matches the wc_MakeRsaKey() check. Exponents below it, or even exponents, are rejected.
  • Adds a unit test for invalid exponents, which also generates a key at RSA_MIN_EXPONENT, and makes the existing bad key size test fail when no exception is thrown.

Copilot AI balanced review requested due to automatic review settings October 8, 2026 22:56
@cconlon cconlon self-assigned this Oct 8, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Native maximum and platform-specific constraints can still cause late failures or exponent truncation.

2 open findings
What changed in this PR

Adds early RSA key-generation validation to avoid deferred native errors.

Changes:

  • Adds native minimum-exponent detection.
  • Validates minimum key size and exponent parity/range.
  • Adds invalid-parameter tests.
File Description
jni/​jni_rsa.c Exposes the native minimum exponent.
jni/​include/​com_wolfssl_wolfcrypt_Rsa.h Declares the new JNI method.
src/​main/​java/​com/​wolfssl/​wolfcrypt/​Rsa.java Publishes RSA_MIN_EXPONENT.
src/​main/​java/​com/​wolfssl/​provider/​jce/​WolfCryptKeyPairGenerator.java Adds early RSA parameter validation.
src/​test/​java/​com/​wolfssl/​provider/​jce/​test/​WolfCryptKeyPairGeneratorTest.java Tests rejected sizes and exponents.
Files not reviewed (1)
  • jni/include/com_wolfssl_wolfcrypt_Rsa.h: Generated file

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/main/java/com/wolfssl/provider/jce/WolfCryptKeyPairGenerator.java Outdated

@wolfSSL-Fenrir-bot wolfSSL-Fenrir-bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fenrir Automated Review — PR #295

Scan targets checked: wolfcrypt-jni-src, wolfcrypt-jni-bugs
Coverage: 2 of 5 in-scope changed file(s) opened by the reviewer; not opened: jni/include/com_wolfssl_wolfcrypt_Rsa.h, src/main/java/com/wolfssl/wolfcrypt/Rsa.java, src/test/java/com/wolfssl/provider/jce/test/WolfCryptKeyPairGeneratorTest.java

Fenrir result: Approved ✅

No new issues found in the changed files.

Advisory only — this automated result does not count as a GitHub approval.

Review tier: Lite

…ative minimum at initialize(), add Rsa.RSA_MIN_EXPONENT
@cconlon
cconlon force-pushed the rsaKeyGenSpecChecks branch from e3761d9 to 8d1f6fa Compare October 9, 2026 15:45
@cconlon cconlon assigned rlm2002 and unassigned cconlon Oct 9, 2026
@rlm2002
rlm2002 merged commit 2bda113 into wolfSSL:master Oct 19, 2026
189 checks passed
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.

4 participants