Skip to content

Commit e818efc

Browse files
authored
Reject d, e values <= 1 (#12272)
* Reject d, e values <= 1 This avoids a potential infinite loop (e.g. with d=e=1 or d=e=-1). * Add tests for possible loop/DoS in rsa_recover_prime_factors()
1 parent d7596d0 commit e818efc

File tree

2 files changed

+6
-0
lines changed
  • src/cryptography/hazmat/primitives/asymmetric
  • tests/hazmat/primitives

2 files changed

+6
-0
lines changed

src/cryptography/hazmat/primitives/asymmetric/rsa.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,8 @@ def rsa_recover_prime_factors(n: int, e: int, d: int) -> tuple[int, int]:
235235
no more than two factors. This function is adapted from code in PyCrypto.
236236
"""
237237
# reject invalid values early
238+
if d <= 1 or e <= 1:
239+
raise ValueError("d, e can't be <= 1")
238240
if 17 != pow(17, e * d, n):
239241
raise ValueError("n, d, e don't match")
240242
# See 8.2.2(i) in Handbook of Applied Cryptography.

tests/hazmat/primitives/test_rsa.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,6 +2400,10 @@ def test_invalid_recover_prime_factors(self):
24002400
rsa.rsa_recover_prime_factors(34, 3, 7)
24012401
with pytest.raises(ValueError):
24022402
rsa.rsa_recover_prime_factors(629, 17, 20)
2403+
with pytest.raises(ValueError):
2404+
rsa.rsa_recover_prime_factors(21, 1, 1)
2405+
with pytest.raises(ValueError):
2406+
rsa.rsa_recover_prime_factors(21, -1, -1)
24032407

24042408

24052409
class TestRSAPrivateKeySerialization:

0 commit comments

Comments
 (0)