Incorrect maximum encryption size for RSA OAEP#423
Merged
Lukasa merged 1 commit intoapple:mainfrom Oct 29, 2025
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Incorrect maximum encryption size for RSA OAEP
Checklist
If you've made changes to
gybfiles./scripts/generate_boilerplate_files_with_gyb.shand included updated generated files in a commit of this pull requestMotivation:
The
maximumEncryptSizefunction 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:
hashBitLengthto the RSA Digest which is then used in themaximumEncryptSizeto properly compute the maximum lengthResult:
maximumEncryptSizefunction 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.