From 836b830b155c1b04fbad40ab76f0de4339d8628c Mon Sep 17 00:00:00 2001 From: Paul Kehrer Date: Sun, 18 Jan 2015 09:42:58 -0600 Subject: recover (p, q) given (n, e, d). fixes #975 --- tests/hazmat/primitives/test_rsa.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'tests/hazmat/primitives/test_rsa.py') diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 095ed037..3de228bb 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1698,3 +1698,36 @@ class TestRSANumbersEquality(object): 1, 2, 3, 4, 5, 6, RSAPublicNumbers(1, 3) ) assert num != object() + + +class TestRSAPrimeFactorRecovery(object): + @pytest.mark.parametrize( + "vector", + _flatten_pkcs1_examples(load_vectors_from_file( + os.path.join( + "asymmetric", "RSA", "pkcs1v15crypt-vectors.txt"), + load_pkcs1_vectors + )) + ) + def test_recover_prime_factors(self, vector): + private, public, example = vector + p, q = rsa.rsa_recover_prime_factors( + private["modulus"], + private["public_exponent"], + private["private_exponent"] + ) + # Unfortunately there is no convention on which prime should be p + # and which one q. The function we use always makes p < q, but the + # NIST vectors are not so consistent. Accordingly we verify we've + # recovered the proper (p, q) by being willing to match against either + # one and then altering the asserts accordingly. + if p == private["p"]: + assert p == private["p"] + assert q == private["q"] + else: + assert p == private["q"] + assert q == private["p"] + + def test_invalid_recover_prime_factors(self): + with pytest.raises(ValueError): + rsa.rsa_recover_prime_factors(34, 3, 7) -- cgit v1.2.3