Skip to content

Add configurable RSA-OAEP support (selectable OAEP & MGF1 digests)#244

Open
nquangit wants to merge 1 commit into
usdAG:masterfrom
nquangit:feature/rsa-oaep-support
Open

Add configurable RSA-OAEP support (selectable OAEP & MGF1 digests)#244
nquangit wants to merge 1 commit into
usdAG:masterfrom
nquangit:feature/rsa-oaep-support

Conversation

@nquangit
Copy link
Copy Markdown

@nquangit nquangit commented Jun 5, 2026

Summary

The RSA Encryption / Decryption operations could not reproduce RSA-OAEP where the OAEP digest and the MGF1 digest are configured independently — a common pattern in client-side encryption. For example, the widely used [node-forge] snippet below uses RSA-OAEP with SHA-256 for both the OAEP digest and MGF1:

pkey.encrypt(forge.util.encodeUtf8(plainPassword), 'RSA-OAEP', {
  md:   forge.md.sha256.create(),
  mgf1: { md: forge.md.sha256.create() }
});

Previously:

  • PEM mode hardcoded Cipher.getInstance("RSA") (= RSA/ECB/PKCS1Padding) — no OAEP at all.
  • KeyStore mode built the cipher from named transformations like OAEPWITHSHA-256ANDMGF1PADDING. Under SunJCE these silently leave MGF1 at SHA-1 regardless of the name, so the result didn't match forge and the MGF1 digest couldn't be controlled.

Changes

  • New RsaCipherBuilder helper — single place that turns a padding (and, for OAEP, two digests) into an initialised Cipher. For OAEP it uses an explicit OAEPParameterSpec, so both the OAEP hash and the MGF1 hash are honoured. Padding and digest option lists live here, so adding a digest in
    future is a one-line change.
  • RsaEncryption / RsaDecryption now expose Padding, OAEP Hash and MGF1 Hash dropdowns in both PEM and KeyStore modes. The two hash fields are shown only while an OAEP padding is selected.
  • Backward compatible: padding defaults to PKCS1Padding, so existing recipes behave exactly as before; older saved recipes load unchanged.
  • Resilient KeyStore padding list: sourced from the security provider but always guaranteed to include an OAEP option, because some JVMs (including Burp's bundled JRE) advertise a reduced RSA padding list that omits OAEP.

How to reproduce the forge config

RSA Encryption → Input Type PEM → paste the public key → Padding OAEPPadding, OAEP Hash SHA-256, MGF1 Hash SHA-256. (UTF-8 encoding and Base64 wrapping are left to the existing operations, e.g. To Base64.)

Testing

  • mvn test — 50 tests pass.
  • New RsaCipherBuilderTest covers: OAEP/SHA-256 round-trip (the forge case), OAEP randomisation, mixed OAEP/MGF1 digests, PKCS1 backward-compat, and the KeyStore padding-fallback behaviour.

The RSA Encryption/Decryption operations previously only did RSA/ECB/
  PKCS1Padding in PEM mode, and in KeyStore mode relied on named OAEP
  transformations (e.g. OAEPWITHSHA-256ANDMGF1PADDING) which silently keep
  MGF1 at SHA-1 under SunJCE. Neither could reproduce a common client-side
  config such as forge's RSA-OAEP with SHA-256 for both the OAEP and MGF1
  digests.

  Introduce RsaCipherBuilder, a small helper that builds an RSA Cipher from
  a padding plus, for OAEP, an explicit OAEPParameterSpec with independently
  selectable OAEP and MGF1 message digests. Both operations now expose
  'Padding', 'OAEP Hash' and 'MGF1 Hash' dropdowns in PEM and KeyStore mode;
  the hash fields are shown only while OAEP is selected. Defaults stay on
  PKCS1Padding so existing recipes are unaffected.

  KeyStore padding choices are sourced from the provider but always include
  an OAEP option, since some JVMs (incl. Burp's bundled JRE) advertise a
  reduced RSA padding list that omits OAEP.

  Add RsaCipherBuilderTest covering OAEP round-trips, mixed digests, PKCS1
  backward-compat and the KeyStore padding fallbacks.
@fhaag95
Copy link
Copy Markdown
Collaborator

fhaag95 commented Jun 5, 2026

Hi! Thanks for your contribution to the CSTC. We'll look into your proposed changes and will update you soon.

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.

2 participants