Allow rsa pkcs#1 decryption of ciphertexts less-than or equal to key-size #11411
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.
This PR allows RSA PKCS#1 decryption of ciphertexts less-than or equal to key-size for compatibility with other implementations, such as Tom Wu's JSBN/RSA library which seems to occasionally produce ciphertext octet-strings shorter than key-size depending on if it contains leading zero octets.
Example
Private key (2048-bits):
Public key:
Plaintext value:
Ciphertext produced by Tom Wu's JSBN/RSA library (255-bytes, one less than the key-size):
Prior to this PR decrypting the ciphertext would produce an error
Ciphertext length must be equal to key size.
. After this PR decrypting the ciphertext produces the plaintext value.Are JSBN/RSA ciphertext-lengths shorter than key-size valid?
RSA PKCS#1 described in rfc3447 section 7.2.1 ultimately produces a ciphertext by an I2OSP (Integer-to-Octet-String primitive) step described in rfc3447 section 4.1.
This I2OSP step suggests the resulting octet string may be of a specified length:
And it acknowledges that there may be leading zeros if the integer
x
is too small:Given this I am still unsure whether or not the ciphertexts produced by Tom Wu's JSBN/RSA library are technically a violation of the specification. However, at one time this library was a de facto standard for client-side RSA and was fairly pervasive. Therefore it might be prudent to handle its ciphertexts if there are no consequences.
Other implementations
Apple's SecKeyDecrypt API decrypts the above example successfully.
The Bouncy Castle package decrypts the above example successfully.
The python-rsa package decrypts the above example successfully.
The pycryptodome package does NOT decrypt the above example successfully.
The micro-rsa-dsa-dh package does NOT decrypt the above example successfully.