Skip to content

Commit 81cda70

Browse files
author
Frederick Price
committed
Fix sybrenstuvel#165: CVE-2020-25658 - Bleichenbacher-style timing oracle Use as many constant-time comparisons as practical in the `rsa.pkcs1.decrypt` function. `cleartext.index(b'\x00', 2)` will still be non-constant-time. The alternative would be to iterate over all the data byte by byte in Python, which is several orders of magnitude slower. Given that a perfect constant-time implementation is very hard or even impossible to do in Python [1], I chose the more performant option here. [1]: https://securitypitfalls.wordpress.com/2018/08/03/constant-time-compare-in-python/
1 parent 4d3025f commit 81cda70

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

CHANGELOG.txt

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Version 4.3 is almost a re-tagged release of version 4.0. It is the last to
88
support Python 2.7. This is now made explicit in the `python_requires` argument
99
in `setup.py`. Python 3.4 is not supported by this release.
1010

11+
- Fix #165: CVE-2020-25658 - Bleichenbacher-style timing oracle in PKCS#1 v1.5
12+
decryption code
13+
14+
15+
## Version 4.4 & 4.6 - released 2020-06-12
16+
1117
Two security fixes have also been backported, so 4.3 = 4.0 + these two fixes.
1218

1319
- Choose blinding factor relatively prime to N. Thanks Christian Heimes for pointing this out.

rsa/pkcs1.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
import hashlib
3232
import os
33+
import sys
34+
import typing
35+
from hmac import compare_digest
3336

3437
from rsa._compat import range
3538
from rsa import common, transform, core
@@ -237,17 +240,20 @@ def decrypt(crypto, priv_key):
237240
# Detect leading zeroes in the crypto. These are not reflected in the
238241
# encrypted value (as leading zeroes do not influence the value of an
239242
# integer). This fixes CVE-2020-13757.
240-
if len(crypto) > blocksize:
241-
raise DecryptionError('Decryption failed')
243+
crypto_len_bad = len(crypto) > blocksize
242244

243245
# If we can't find the cleartext marker, decryption failed.
244-
if cleartext[0:2] != b'\x00\x02':
245-
raise DecryptionError('Decryption failed')
246+
cleartext_marker_bad = not compare_digest(cleartext[:2], b'\x00\x02')
246247

247248
# Find the 00 separator between the padding and the message
248249
try:
249250
sep_idx = cleartext.index(b'\x00', 2)
250251
except ValueError:
252+
sep_idx = -1
253+
sep_idx_bad = sep_idx < 0
254+
255+
anything_bad = crypto_len_bad | cleartext_marker_bad | sep_idx_bad
256+
if anything_bad:
251257
raise DecryptionError('Decryption failed')
252258

253259
return cleartext[sep_idx + 1:]

0 commit comments

Comments
 (0)