diff options
Diffstat (limited to 'tests/hazmat')
| -rw-r--r-- | tests/hazmat/backends/test_openssl.py | 55 | ||||
| -rw-r--r-- | tests/hazmat/primitives/test_rsa.py | 70 | 
2 files changed, 112 insertions, 13 deletions
| diff --git a/tests/hazmat/backends/test_openssl.py b/tests/hazmat/backends/test_openssl.py index 43d28c33..46feae46 100644 --- a/tests/hazmat/backends/test_openssl.py +++ b/tests/hazmat/backends/test_openssl.py @@ -143,8 +143,8 @@ class TestOpenSSL(object):          with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH):              backend.derive_pbkdf2_hmac(hashes.SHA256(), 10, b"", 1000, b"") -    # This test is not in the next class because to check if it's really -    # default we don't want to run the setup_method before it +    # This test is not in the TestOpenSSLRandomEngine class because to check +    # if it's really default we don't want to run the setup_method before it      def test_osrandom_engine_is_default(self):          e = backend._lib.ENGINE_get_default_RAND()          name = backend._lib.ENGINE_get_name(e) @@ -291,3 +291,54 @@ class TestOpenSSLRSA(object):      def test_unsupported_mgf1_hash_algorithm(self):          assert backend.mgf1_hash_supported(DummyHash()) is False + +    def test_unsupported_mgf1_hash_algorithm_decrypt(self): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): +            private_key.decrypt( +                b"ciphertext", +                padding.OAEP( +                    mgf=padding.MGF1(algorithm=hashes.SHA256()), +                    algorithm=hashes.SHA1(), +                    label=None +                ), +                backend +            ) + +    def test_unsupported_oaep_hash_algorithm_decrypt(self): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_HASH): +            private_key.decrypt( +                b"ciphertext", +                padding.OAEP( +                    mgf=padding.MGF1(algorithm=hashes.SHA1()), +                    algorithm=hashes.SHA256(), +                    label=None +                ), +                backend +            ) + +    def test_unsupported_oaep_label_decrypt(self): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with pytest.raises(ValueError): +            private_key.decrypt( +                b"ciphertext", +                padding.OAEP( +                    mgf=padding.MGF1(algorithm=hashes.SHA1()), +                    algorithm=hashes.SHA1(), +                    label=b"label" +                ), +                backend +            ) diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py index 70ae20dc..7b658b69 100644 --- a/tests/hazmat/primitives/test_rsa.py +++ b/tests/hazmat/primitives/test_rsa.py @@ -1227,6 +1227,17 @@ class TestMGF1(object):          assert mgf._salt_length == padding.MGF1.MAX_LENGTH +class TestOAEP(object): +    def test_invalid_algorithm(self): +        mgf = padding.MGF1(hashes.SHA1()) +        with pytest.raises(TypeError): +            padding.OAEP( +                mgf=mgf, +                algorithm=b"", +                label=None +            ) + +  @pytest.mark.rsa  class TestRSADecryption(object):      @pytest.mark.parametrize( @@ -1249,16 +1260,14 @@ class TestRSADecryption(object):              public_exponent=private["public_exponent"],              modulus=private["modulus"]          ) -        message = backend.rsa_decrypt( -            skey, +        message = skey.decrypt(              binascii.unhexlify(example["encryption"]), -            # TODO: handle MGF1 here              padding.OAEP( -                padding.MGF1( -                    algorithm=hashes.SHA1(), -                    salt_length=padding.MGF1.MAX_LENGTH -                ) -            ) +                mgf=padding.MGF1(algorithm=hashes.SHA1()), +                algorithm=hashes.SHA1(), +                label=None +            ), +            backend          )          assert message == binascii.unhexlify(example["message"]) @@ -1282,9 +1291,48 @@ class TestRSADecryption(object):              public_exponent=private["public_exponent"],              modulus=private["modulus"]          ) -        message = backend.rsa_decrypt( -            skey, +        message = skey.decrypt(              binascii.unhexlify(example["encryption"]), -            padding.PKCS1v15() +            padding.PKCS1v15(), +            backend          )          assert message == binascii.unhexlify(example["message"]) + +    def test_unsupported_padding(self, backend): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_PADDING): +            private_key.decrypt(b"somedata", DummyPadding(), backend) + +    def test_unsupported_oaep_mgf(self, backend): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_MGF): +            private_key.decrypt( +                b"ciphertext", +                padding.OAEP( +                    mgf=DummyMGF(), +                    algorithm=hashes.SHA1(), +                    label=None +                ), +                backend +            ) + +    def test_decrypt_invalid_decrypt(self, backend): +        private_key = rsa.RSAPrivateKey.generate( +            public_exponent=65537, +            key_size=512, +            backend=backend +        ) +        with pytest.raises(exceptions.InternalError): +            private_key.decrypt( +                b"\x00" * 64, +                padding.PKCS1v15(), +                backend +            ) | 
