Skip to content

Commit 070ae5a

Browse files
authored
Reject invalid values in various functions for partial RSA key recovery (#13032)
* Reject invalid values in various functions for partial RSA key recovery This avoids raising unexpected exceptions due to invalid inputs, and always raises a ValueError for invalid inputs. Bug: #13031 * Add positive and negative tests for partial RSA recovery functions
1 parent fd100fd commit 070ae5a

File tree

2 files changed

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

2 files changed

+30
-0
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ def rsa_crt_iqmp(p: int, q: int) -> int:
184184
"""
185185
Compute the CRT (q ** -1) % p value from RSA primes p and q.
186186
"""
187+
if p <= 1 or q <= 1:
188+
raise ValueError("Values can't be <= 1")
187189
return _modinv(q, p)
188190

189191

@@ -192,6 +194,8 @@ def rsa_crt_dmp1(private_exponent: int, p: int) -> int:
192194
Compute the CRT private_exponent % (p - 1) value from the RSA
193195
private_exponent (d) and p.
194196
"""
197+
if private_exponent <= 1 or p <= 1:
198+
raise ValueError("Values can't be <= 1")
195199
return private_exponent % (p - 1)
196200

197201

@@ -200,6 +204,8 @@ def rsa_crt_dmq1(private_exponent: int, q: int) -> int:
200204
Compute the CRT private_exponent % (q - 1) value from the RSA
201205
private_exponent (d) and q.
202206
"""
207+
if private_exponent <= 1 or q <= 1:
208+
raise ValueError("Values can't be <= 1")
203209
return private_exponent % (q - 1)
204210

205211

@@ -220,6 +226,8 @@ def rsa_recover_private_exponent(e: int, p: int, q: int) -> int:
220226
#
221227
# TODO: Replace with lcm(p - 1, q - 1) once the minimum
222228
# supported Python version is >= 3.9.
229+
if e <= 1 or p <= 1 or q <= 1:
230+
raise ValueError("Values can't be <= 1")
223231
lambda_n = (p - 1) * (q - 1) // gcd(p - 1, q - 1)
224232
return _modinv(e, lambda_n)
225233

tests/hazmat/primitives/test_rsa.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,6 +2388,28 @@ def test_invalid_recover_prime_factors(self):
23882388
rsa.rsa_recover_prime_factors(21, -1, -1)
23892389

23902390

2391+
class TestRSAPartial:
2392+
def test_rsa_partial(self):
2393+
# Toy RSA key values
2394+
p = 521
2395+
q = 491
2396+
e = 3
2397+
d = 16987
2398+
assert rsa.rsa_crt_iqmp(p, q) == 191
2399+
assert rsa.rsa_crt_dmp1(d, p) == 347
2400+
assert rsa.rsa_crt_dmq1(d, q) == 327
2401+
assert rsa.rsa_recover_private_exponent(e, p, q) == d
2402+
2403+
with pytest.raises(ValueError):
2404+
rsa.rsa_crt_iqmp(0, 0)
2405+
with pytest.raises(ValueError):
2406+
rsa.rsa_crt_dmp1(1, 1)
2407+
with pytest.raises(ValueError):
2408+
rsa.rsa_crt_dmq1(1, 1)
2409+
with pytest.raises(ValueError):
2410+
rsa.rsa_recover_private_exponent(0, 1, 0)
2411+
2412+
23912413
class TestRSAPrivateKeySerialization:
23922414
@pytest.mark.parametrize(
23932415
("fmt", "password"),

0 commit comments

Comments
 (0)