Skip to content

Incorrect maximum encryption size for RSA OAEP#423

Merged
Lukasa merged 1 commit intoapple:mainfrom
mschrag:schrag/rsa-pkcs1_oaep-max-size
Oct 29, 2025
Merged

Incorrect maximum encryption size for RSA OAEP#423
Lukasa merged 1 commit intoapple:mainfrom
mschrag:schrag/rsa-pkcs1_oaep-max-size

Conversation

@mschrag
Copy link
Contributor

@mschrag mschrag commented Oct 28, 2025

Incorrect maximum encryption size for RSA OAEP

Checklist

  • I've run tests to see all new and existing tests pass
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

If you've made changes to gyb files

  • [n/a] I've run ./scripts/generate_boilerplate_files_with_gyb.sh and included updated generated files in a commit of this pull request

Motivation:

The maximumEncryptSize function is hardcoded to use 42 as the hash offset, but the RFC actually says it's "2hLen-2" so 42 is only valid for SHA1. SHA256 should be 62 (232-2). This adds a hash length onto the Digest enum, which can then be used in the length calculation.

In writing the tests for this, I also stumbled on the unsafe PEM representation for RSA Public Keys don't allow 1024-bit keys as documented, so this also fixes that.

Modifications:

  • Added a hashBitLength to the RSA Digest which is then used in the maximumEncryptSize to properly compute the maximum length
  • Corrected the minimum key size for RSA unsafe PEM public keys to 1024 from 2048

Result:

  • The maximumEncryptSize function will return the expected value for RSA OAEP SHA256 keys. I don't believe anything calls this internally, so this would be for external consumers.
  • Unsafe construction of RSA public keys now allow 1024 bit keys. It was documented as supporting them, but the check was still 2048 (probably copy-paste error from the safe variant). This only expands the potential uses, so it shouldn't introduce any new failures.

The `maximumEncryptSize` function is hardcoded to use 42 as the hash
offset, but the RFC actually says it's "2*hLen-2" so 42 is only valid
for SHA1. SHA256 should be 62 (2*32-2). This adds a hash length onto the
Digest enum, which can then be used in the length calculation.

In writing the tests for this, I also stumbled on the unsafe PEM
representation for RSA Public Keys don't allow 1024-bit keys as
documented, so this also fixes that.
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

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

Nice one, thanks!

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Oct 29, 2025
@Lukasa Lukasa enabled auto-merge (squash) October 29, 2025 10:53
@Lukasa Lukasa merged commit e8ed886 into apple:main Oct 29, 2025
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants