From eeb5fbc368d05de41121893a7c536fcc59a5a6bd Mon Sep 17 00:00:00 2001 From: David Reid Date: Wed, 7 May 2014 19:57:07 -0700 Subject: Implement load_rsa_numbers on the openssl backend. --- cryptography/hazmat/backends/openssl/backend.py | 18 ++ tests/hazmat/primitives/test_rsa.py | 248 ++++++++++++++++++++++++ 2 files changed, 266 insertions(+) diff --git a/cryptography/hazmat/backends/openssl/backend.py b/cryptography/hazmat/backends/openssl/backend.py index 4112f0e5..e5870f3e 100644 --- a/cryptography/hazmat/backends/openssl/backend.py +++ b/cryptography/hazmat/backends/openssl/backend.py @@ -377,6 +377,24 @@ class Backend(object): return (public_exponent >= 3 and public_exponent & 1 != 0 and key_size >= 512) + def load_rsa_numbers(self, numbers): + if isinstance(numbers, rsa.RSAPublicNumbers): + return rsa.RSAPublicKey( + public_exponent=numbers.e, + modulus=numbers.n + ) + elif isinstance(numbers, rsa.RSAPrivateNumbers): + return rsa.RSAPrivateKey( + p=numbers.p, + q=numbers.q, + private_exponent=numbers.d, + dmp1=numbers.dmp1, + dmq1=numbers.dmq1, + iqmp=numbers.iqmp, + public_exponent=numbers.public_numbers.e, + modulus=numbers.public_numbers.n + ) + def _new_evp_pkey(self): evp_pkey = self._lib.EVP_PKEY_new() assert evp_pkey != self._ffi.NULL diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index ba668bff..a1652594 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1666,6 +1666,7 @@ class TestRSAEncryption(object): ) +@pytest.mark.rsa class TestRSANumbers(object): def test_rsa_public_numbers(self): public_numbers = rsa.RSAPublicNumbers(e=1, n=15) @@ -1778,3 +1779,250 @@ class TestRSANumbers(object): iqmp=2, public_numbers=None ) + + def test_invalid_public_numbers_argument_values(self, backend): + # Start with public_exponent=7, modulus=15. Then change one value at a + # time to test the bounds. + + # Test a modulus < 3. + + with pytest.raises(ValueError): + backend.load_rsa_numbers(rsa.RSAPublicNumbers(e=7, n=2)) + + # Test a public_exponent < 3 + with pytest.raises(ValueError): + backend.load_rsa_numbers(rsa.RSAPublicNumbers(e=1, n=15)) + + # Test a public_exponent > modulus + with pytest.raises(ValueError): + backend.load_rsa_numbers(rsa.RSAPublicNumbers(e=17, n=15)) + + # Test a public_exponent that is not odd. + with pytest.raises(ValueError): + backend.load_rsa_numbers(rsa.RSAPublicNumbers(e=16, n=15)) + + def test_invalid_private_numbers_argument_values(self, backend): + # Start with p=3, q=11, private_exponent=3, public_exponent=7, + # modulus=33, dmp1=1, dmq1=3, iqmp=2. Then change one value at + # a time to test the bounds. + + # Test a modulus < 3. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=2 + ) + ) + ) + + # Test a modulus != p * q. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=35 + ) + ) + ) + + # Test a p > modulus. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=37, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a q > modulus. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=37, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a dmp1 > modulus. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=35, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a dmq1 > modulus. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=35, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test an iqmp > modulus. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=35, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a private_exponent > modulus + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=37, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a public_exponent < 3 + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=1, + n=33 + ) + ) + ) + + # Test a public_exponent > modulus + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=35, + public_numbers=rsa.RSAPublicNumbers( + e=65537, + n=33 + ) + ) + ) + + # Test a public_exponent that is not odd. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=6, + n=33 + ) + ) + ) + + # Test a dmp1 that is not odd. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=2, + dmq1=3, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) + + # Test a dmq1 that is not odd. + with pytest.raises(ValueError): + backend.load_rsa_numbers( + rsa.RSAPrivateNumbers( + p=3, + q=11, + d=3, + dmp1=1, + dmq1=4, + iqmp=2, + public_numbers=rsa.RSAPublicNumbers( + e=7, + n=33 + ) + ) + ) -- cgit v1.2.3