aboutsummaryrefslogtreecommitdiffstats
path: root/tests/hazmat/primitives/test_rsa.py
diff options
context:
space:
mode:
authorAlex Gaynor <alex.gaynor@gmail.com>2014-03-06 14:10:59 -0800
committerAlex Gaynor <alex.gaynor@gmail.com>2014-03-06 14:10:59 -0800
commit8b347932fb3612e622d07643af62ed939976b9cb (patch)
tree72837619ab4e9064ff50649f245ed3ee316ce66e /tests/hazmat/primitives/test_rsa.py
parent25d24de1417844f95b247365a3fb24f17bf120ff (diff)
parent1e8aa9b09351bf0fb47bed24defc4d9f37560e31 (diff)
downloadcryptography-8b347932fb3612e622d07643af62ed939976b9cb.tar.gz
cryptography-8b347932fb3612e622d07643af62ed939976b9cb.tar.bz2
cryptography-8b347932fb3612e622d07643af62ed939976b9cb.zip
Merge branch 'master' into exception-heirarchy-refactor
Conflicts: cryptography/hazmat/backends/openssl/backend.py
Diffstat (limited to 'tests/hazmat/primitives/test_rsa.py')
-rw-r--r--tests/hazmat/primitives/test_rsa.py115
1 files changed, 115 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py
index 647c51b4..79323265 100644
--- a/tests/hazmat/primitives/test_rsa.py
+++ b/tests/hazmat/primitives/test_rsa.py
@@ -443,3 +443,118 @@ class TestRSASignature(object):
)
with pytest.raises(TypeError):
private_key.signer("notpadding", hashes.SHA1(), backend)
+
+
+@pytest.mark.rsa
+class TestRSAVerification(object):
+ @pytest.mark.parametrize(
+ "pkcs1_example",
+ _flatten_pkcs1_examples(load_vectors_from_file(
+ os.path.join(
+ "asymmetric", "RSA", "pkcs1v15sign-vectors.txt"),
+ load_pkcs1_vectors
+ ))
+ )
+ def test_pkcs1v15_verification(self, pkcs1_example, backend):
+ private, public, example = pkcs1_example
+ public_key = rsa.RSAPublicKey(
+ public_exponent=public["public_exponent"],
+ modulus=public["modulus"]
+ )
+ verifier = public_key.verifier(
+ binascii.unhexlify(example["signature"]),
+ padding.PKCS1v15(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(binascii.unhexlify(example["message"]))
+ verifier.verify()
+
+ def test_invalid_pkcs1v15_signature_wrong_data(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ public_key = private_key.public_key()
+ signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1v15(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(b"incorrect data")
+ with pytest.raises(exceptions.InvalidSignature):
+ verifier.verify()
+
+ def test_invalid_pkcs1v15_signature_wrong_key(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ private_key2 = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ public_key = private_key2.public_key()
+ signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1v15(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(b"sign me")
+ with pytest.raises(exceptions.InvalidSignature):
+ verifier.verify()
+
+ def test_use_after_finalize(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ public_key = private_key.public_key()
+ signer = private_key.signer(padding.PKCS1v15(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1v15(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(b"sign me")
+ verifier.verify()
+ with pytest.raises(exceptions.AlreadyFinalized):
+ verifier.verify()
+ with pytest.raises(exceptions.AlreadyFinalized):
+ verifier.update(b"more data")
+
+ def test_unsupported_padding(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ public_key = private_key.public_key()
+ with pytest.raises(exceptions.UnsupportedPadding):
+ public_key.verifier(b"sig", DummyPadding(), hashes.SHA1(), backend)
+
+ def test_padding_incorrect_type(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(
+ public_exponent=65537,
+ key_size=512,
+ backend=backend
+ )
+ public_key = private_key.public_key()
+ with pytest.raises(TypeError):
+ public_key.verifier(b"sig", "notpadding", hashes.SHA1(), backend)