Skip to content

Commit 341e5c4

Browse files
committed
Directly raise DecryptionError when crypto length is bad
Crypto length and blocksize are public info, so don't need side-channel free comparison.
1 parent f254895 commit 341e5c4

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

rsa/pkcs1.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ def decrypt(crypto: bytes, priv_key: key.PrivateKey) -> bytes:
252252
# Detect leading zeroes in the crypto. These are not reflected in the
253253
# encrypted value (as leading zeroes do not influence the value of an
254254
# integer). This fixes CVE-2020-13757.
255-
crypto_len_bad = len(crypto) > blocksize
255+
if len(crypto) > blocksize:
256+
# This is operating on public information, so doesn't need to be constant-time.
257+
raise DecryptionError('Decryption failed')
256258

257259
# If we can't find the cleartext marker, decryption failed.
258260
cleartext_marker_bad = not compare_digest(cleartext[:2], b'\x00\x02')
@@ -267,7 +269,7 @@ def decrypt(crypto: bytes, priv_key: key.PrivateKey) -> bytes:
267269
# `\x00\x02` marker that preceeds it).
268270
sep_idx_bad = sep_idx < 10
269271

270-
anything_bad = crypto_len_bad | cleartext_marker_bad | sep_idx_bad
272+
anything_bad = cleartext_marker_bad | sep_idx_bad
271273
if anything_bad:
272274
raise DecryptionError('Decryption failed')
273275

0 commit comments

Comments
 (0)