Fix RSA-OAEP to allow zero-length plaintext per RFC 8017#10012
Open
MarkAtwood wants to merge 1 commit intowolfSSL:masterfrom
Open
Fix RSA-OAEP to allow zero-length plaintext per RFC 8017#10012MarkAtwood wants to merge 1 commit intowolfSSL:masterfrom
MarkAtwood wants to merge 1 commit intowolfSSL:masterfrom
Conversation
RsaPublicEncryptEx() rejected inLen==0 unconditionally with BAD_FUNC_ARG. RFC 8017 Section 7.1.1 (RSAES-OAEP-ENCRYPT) permits zero-length messages: the only length constraint is mLen <= k - 2*hLen - 2, which mLen=0 always satisfies. RsaPrivateDecryptEx() converted a zero-length decryption result to RSA_BUFFER_E (unless WOLFSSL_RSA_DECRYPT_TO_0_LEN was defined). RFC 8017 Section 7.1.2 (RSAES-OAEP-DECRYPT) produces the original message M which may be empty. The fix uses constant-time masking to allow ret==0 when pad_type is WC_RSA_OAEP_PAD, preserving the existing timing-safe behavior for other padding types. Both OpenSSL and BoringSSL accept empty OAEP plaintexts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates RSA-OAEP handling to permit zero-length plaintexts on both encryption and decryption, aligning behavior with RFC 8017 and other TLS/crypto libraries.
Changes:
- Relaxed
RsaPublicEncryptEx()argument validation to allowinLen == 0for OAEP padding. - Adjusted
RsaPrivateDecryptEx()constant-time error handling to permitret == 0results for OAEP padding. - Added RFC 8017 references in inline comments to document the behavioral change.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+3328
to
+3329
| * a zero-length input is invalid. */ | ||
| if (in == NULL || (inLen == 0 && pad_type != WC_RSA_OAEP_PAD)) { |
Comment on lines
+3326
to
+3328
| * permitted: the spec requires mLen <= k - 2*hLen - 2, and mLen = 0 | ||
| * satisfies this for all supported key sizes. For other padding types, | ||
| * a zero-length input is invalid. */ |
Comment on lines
+3720
to
+3721
| int zeroOk = ctMaskEq(pad_type, WC_RSA_OAEP_PAD); | ||
| ret = ctMaskSelInt(ctMaskNotEq(ret, 0) | zeroOk, ret, |
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.
Summary
RsaPublicEncryptEx()rejectsinLen == 0withBAD_FUNC_ARG, but RFC 8017 Section 7.1.1 (RSAES-OAEP-ENCRYPT) permits zero-length messages. The only length constraint ismLen <= k - 2*hLen - 2, whichmLen = 0always satisfies.RsaPrivateDecryptEx()converts a zero-length decryption result toRSA_BUFFER_E(unlessWOLFSSL_RSA_DECRYPT_TO_0_LENis defined). RFC 8017 Section 7.1.2 (RSAES-OAEP-DECRYPT) produces the original messageMwhich may be empty.Both OpenSSL and BoringSSL accept empty OAEP plaintexts.
Changes
Encrypt side (
RsaPublicEncryptEx, line ~3321):inLen == 0rejection to only apply for non-OAEP padding typesDecrypt side (
RsaPrivateDecryptEx, line ~3716):WOLFSSL_RSA_DECRYPT_TO_0_LENis not defined, theret == 0rejection now uses constant-time masking (ctMaskEq) to allow zero-length results forWC_RSA_OAEP_PADwhile preserving the existing behavior for other padding typesSpec references
mLen <= k - 2*hLen - 2;mLen = 0is validMwhich may be the empty stringTesting
Verified with wolfcrypt-ring RSA-OAEP round-trip tests using SHA-1, SHA-256, SHA-384, and SHA-512 with zero-length plaintext.
🤖 Generated with Claude Code