aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-23 19:13:19 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-25 22:59:03 -0600
commit42b3713eede3f5b417b0ce123fdcc9c4c24009d3 (patch)
treec610439bb695d7e1da8390a0c1133daef612727f /tests
parentd8c8f7cde6b43d08f39cd11cd2e2dd3ed7feb5a5 (diff)
downloadcryptography-42b3713eede3f5b417b0ce123fdcc9c4c24009d3.tar.gz
cryptography-42b3713eede3f5b417b0ce123fdcc9c4c24009d3.tar.bz2
cryptography-42b3713eede3f5b417b0ce123fdcc9c4c24009d3.zip
add RSA verification support
Diffstat (limited to 'tests')
-rw-r--r--tests/hazmat/backends/test_multibackend.py11
-rw-r--r--tests/hazmat/primitives/test_rsa.py88
2 files changed, 99 insertions, 0 deletions
diff --git a/tests/hazmat/backends/test_multibackend.py b/tests/hazmat/backends/test_multibackend.py
index be1e76e2..5a8f9934 100644
--- a/tests/hazmat/backends/test_multibackend.py
+++ b/tests/hazmat/backends/test_multibackend.py
@@ -89,6 +89,10 @@ class DummyRSABackend(object):
def create_rsa_signature_ctx(self, private_key, padding, algorithm):
pass
+ def create_rsa_verification_ctx(self, public_key, signature, padding,
+ algorithm):
+ pass
+
class TestMultiBackend(object):
def test_ciphers(self):
@@ -165,6 +169,9 @@ class TestMultiBackend(object):
backend.create_rsa_signature_ctx("private_key", padding.PKCS1v15(),
hashes.MD5())
+ backend.create_rsa_verification_ctx("public_key", "sig",
+ padding.PKCS1(), hashes.MD5())
+
backend = MultiBackend([])
with pytest.raises(UnsupportedAlgorithm):
backend.generate_rsa_private_key(key_size=1024, public_exponent=3)
@@ -172,3 +179,7 @@ class TestMultiBackend(object):
with pytest.raises(UnsupportedAlgorithm):
backend.create_rsa_signature_ctx("private_key", padding.PKCS1v15(),
hashes.MD5())
+
+ with pytest.raises(UnsupportedAlgorithm):
+ backend.create_rsa_verification_ctx("public_key", "sig",
+ padding.PKCS1(), hashes.MD5())
diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py
index 647c51b4..552bfc5f 100644
--- a/tests/hazmat/primitives/test_rsa.py
+++ b/tests/hazmat/primitives/test_rsa.py
@@ -443,3 +443,91 @@ 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)
+ verifier = public_key.verifier(
+ binascii.unhexlify(example["signature"]),
+ padding.PKCS1(),
+ 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(65537, 512, backend)
+ public_key = private_key.public_key()
+ signer = private_key.signer(padding.PKCS1(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(b"incorrect data")
+ with pytest.raises(exceptions.InvalidAsymmetricSignature):
+ verifier.verify()
+
+ def test_invalid_pkcs1v15_signature_wrong_key(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ public_key = private_key.public_key()
+ public_key._modulus += 2
+ signer = private_key.signer(padding.PKCS1(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1(),
+ hashes.SHA1(),
+ backend
+ )
+ verifier.update(b"sign me")
+ with pytest.raises(exceptions.InvalidAsymmetricSignature):
+ verifier.verify()
+
+ def test_use_after_finalize(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ public_key = private_key.public_key()
+ signer = private_key.signer(padding.PKCS1(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signature = signer.finalize()
+
+ verifier = public_key.verifier(
+ signature,
+ padding.PKCS1(),
+ 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(65537, 512, backend)
+ public_key = private_key.public_key()
+ with pytest.raises(exceptions.UnsupportedAsymmetricPadding):
+ public_key.verifier(b"sig", DummyPadding(), hashes.SHA1(), backend)
+
+ def test_padding_incorrect_type(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ public_key = private_key.public_key()
+ with pytest.raises(TypeError):
+ public_key.verifier(b"sig", "notpadding", hashes.SHA1(), backend)