diff options
Diffstat (limited to 'tests/hazmat')
| -rw-r--r-- | tests/hazmat/backends/test_multibackend.py | 20 | ||||
| -rw-r--r-- | tests/hazmat/primitives/test_rsa.py | 248 | 
2 files changed, 268 insertions, 0 deletions
| diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py index 3fa364e2..93d58483 100644 --- a/tests/hazmat/backends/test_multibackend.py +++ b/tests/hazmat/backends/test_multibackend.py @@ -113,6 +113,12 @@ class DummyRSABackend(object):      def encrypt_rsa(self, public_key, plaintext, padding):          pass +    def load_rsa_private_numbers(self, numbers): +        pass + +    def load_rsa_public_numbers(self, numbers): +        pass +  @utils.register_interface(DSABackend)  class DummyDSABackend(object): @@ -236,6 +242,10 @@ class TestMultiBackend(object):          backend.decrypt_rsa("private_key", "encrypted", padding.PKCS1v15()) +        backend.load_rsa_private_numbers("private_numbers") + +        backend.load_rsa_public_numbers("public_numbers") +          backend = MultiBackend([])          with raises_unsupported_algorithm(              _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM @@ -279,6 +289,16 @@ class TestMultiBackend(object):          ):              backend.decrypt_rsa("private_key", "encrypted", padding.PKCS1v15()) +        with raises_unsupported_algorithm( +            _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM +        ): +            backend.load_rsa_private_numbers("private_numbers") + +        with raises_unsupported_algorithm( +            _Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM +        ): +            backend.load_rsa_public_numbers("public_numbers") +      def test_dsa(self):          backend = MultiBackend([              DummyDSABackend() diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index ba668bff..a76c0ec2 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_public_numbers(rsa.RSAPublicNumbers(e=7, n=2)) + +        # Test a public_exponent < 3 +        with pytest.raises(ValueError): +            backend.load_rsa_public_numbers(rsa.RSAPublicNumbers(e=1, n=15)) + +        # Test a public_exponent > modulus +        with pytest.raises(ValueError): +            backend.load_rsa_public_numbers(rsa.RSAPublicNumbers(e=17, n=15)) + +        # Test a public_exponent that is not odd. +        with pytest.raises(ValueError): +            backend.load_rsa_public_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_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_private_numbers( +                rsa.RSAPrivateNumbers( +                    p=3, +                    q=11, +                    d=3, +                    dmp1=1, +                    dmq1=4, +                    iqmp=2, +                    public_numbers=rsa.RSAPublicNumbers( +                        e=7, +                        n=33 +                    ) +                ) +            ) | 
