diff options
Diffstat (limited to 'tests/hazmat/primitives/test_rsa.py')
-rw-r--r-- | tests/hazmat/primitives/test_rsa.py | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 79323265..0e88bb7f 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -21,9 +21,9 @@ import os import pytest from cryptography import exceptions, utils +from cryptography.exceptions import UnsupportedInterface from cryptography.hazmat.primitives import hashes, interfaces -from cryptography.hazmat.primitives.asymmetric import rsa -from cryptography.hazmat.primitives.asymmetric import padding +from cryptography.hazmat.primitives.asymmetric import rsa, padding from ...utils import load_pkcs1_vectors, load_vectors_from_file @@ -385,6 +385,13 @@ class TestRSA(object): rsa.RSAPublicKey(public_exponent=6, modulus=15) +def test_rsa_generate_invalid_backend(): + pretend_backend = object() + + with pytest.raises(UnsupportedInterface): + rsa.RSAPrivateKey.generate(65537, 2048, pretend_backend) + + @pytest.mark.rsa class TestRSASignature(object): @pytest.mark.parametrize( @@ -444,6 +451,14 @@ class TestRSASignature(object): with pytest.raises(TypeError): private_key.signer("notpadding", hashes.SHA1(), backend) + def test_rsa_signer_invalid_backend(self, backend): + pretend_backend = object() + private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) + + with pytest.raises(UnsupportedInterface): + private_key.signer( + padding.PKCS1v15(), hashes.SHA256, pretend_backend) + @pytest.mark.rsa class TestRSAVerification(object): @@ -558,3 +573,39 @@ class TestRSAVerification(object): public_key = private_key.public_key() with pytest.raises(TypeError): public_key.verifier(b"sig", "notpadding", hashes.SHA1(), backend) + + def test_rsa_verifier_invalid_backend(self, backend): + pretend_backend = object() + private_key = rsa.RSAPrivateKey.generate(65537, 2048, backend) + public_key = private_key.public_key() + + with pytest.raises(UnsupportedInterface): + public_key.verifier( + b"foo", padding.PKCS1v15(), hashes.SHA256(), pretend_backend) + + +class TestMGF1(object): + def test_invalid_hash_algorithm(self): + with pytest.raises(TypeError): + padding.MGF1(b"not_a_hash", 0) + + def test_invalid_salt_length_not_integer(self): + with pytest.raises(TypeError): + padding.MGF1(hashes.SHA1(), b"not_a_length") + + def test_invalid_salt_length_negative_integer(self): + with pytest.raises(ValueError): + padding.MGF1(hashes.SHA1(), -1) + + def test_valid_mgf1_parameters(self): + algorithm = hashes.SHA1() + salt_length = algorithm.digest_size + mgf = padding.MGF1(algorithm, salt_length) + assert mgf._algorithm == algorithm + assert mgf._salt_length == salt_length + + def test_valid_mgf1_parameters_maximum(self): + algorithm = hashes.SHA1() + mgf = padding.MGF1(algorithm, padding.MGF1.MAX_LENGTH) + assert mgf._algorithm == algorithm + assert mgf._salt_length == padding.MGF1.MAX_LENGTH |