aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-23 11:26:37 -0600
committerPaul Kehrer <paul.l.kehrer@gmail.com>2014-02-23 11:26:37 -0600
commit4ccceaf4484dce24c5f0994b52079293a5fdb37c (patch)
tree7a8190e8f274ee210f1431014e932680b85df4db /tests
parent2d4f36a2246580466f3c1c6e027f03bb9b265960 (diff)
downloadcryptography-4ccceaf4484dce24c5f0994b52079293a5fdb37c.tar.gz
cryptography-4ccceaf4484dce24c5f0994b52079293a5fdb37c.tar.bz2
cryptography-4ccceaf4484dce24c5f0994b52079293a5fdb37c.zip
add RSA PKCS1 signing (and structure for PSS + verification)
Diffstat (limited to 'tests')
-rw-r--r--tests/hazmat/primitives/test_rsa.py60
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/hazmat/primitives/test_rsa.py b/tests/hazmat/primitives/test_rsa.py
index df3a70f5..b2ea9bad 100644
--- a/tests/hazmat/primitives/test_rsa.py
+++ b/tests/hazmat/primitives/test_rsa.py
@@ -14,16 +14,25 @@
from __future__ import absolute_import, division, print_function
+import binascii
import itertools
import os
import pytest
+from cryptography import exceptions, utils
+from cryptography.hazmat.primitives import hashes, interfaces
from cryptography.hazmat.primitives.asymmetric import rsa
+from cryptography.hazmat.primitives.asymmetric import padding
from ...utils import load_pkcs1_vectors, load_vectors_from_file
+@utils.register_interface(interfaces.AsymmetricPadding)
+class FakePadding(object):
+ name = "UNSUPPORTED-PADDING"
+
+
def _modinv(e, m):
"""
Modular Multiplicative Inverse. Returns x such that: (x*e) mod m == 1
@@ -55,6 +64,17 @@ def _check_rsa_private_key(skey):
assert skey.key_size == pkey.key_size
+def _flatten_pkcs1_examples(vectors):
+ flattened_vectors = []
+ for vector in vectors:
+ examples = vector[0].pop("examples")
+ for example in examples:
+ merged_vector = (vector[0], vector[1], example)
+ flattened_vectors.append(merged_vector)
+
+ return flattened_vectors
+
+
def test_modular_inverse():
p = int(
"d1f9f6c09fd3d38987f7970247b85a6da84907753d42ec52bc23b745093f4fff5cff3"
@@ -363,3 +383,43 @@ class TestRSA(object):
# Test a public_exponent that is not odd.
with pytest.raises(ValueError):
rsa.RSAPublicKey(public_exponent=6, modulus=15)
+
+
+@pytest.mark.rsa
+class TestRSASignature(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_signing(self, pkcs1_example, backend):
+ private, public, example = pkcs1_example
+ private_key = rsa.RSAPrivateKey(**private)
+ public_key = rsa.RSAPublicKey(**public)
+ signer = private_key.signer(padding.PKCS1(), hashes.SHA1(), backend)
+ signer.update(binascii.unhexlify(example["message"]))
+ signature = signer.finalize()
+ assert binascii.hexlify(signature) == example["signature"]
+
+ def test_use_after_finalize(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ signer = private_key.signer(padding.PKCS1(), hashes.SHA1(), backend)
+ signer.update(b"sign me")
+ signer.finalize()
+ with pytest.raises(exceptions.AlreadyFinalized):
+ signer.finalize()
+ with pytest.raises(exceptions.AlreadyFinalized):
+ signer.update(b"more data")
+
+ def test_unsupported_padding(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ with pytest.raises(exceptions.UnsupportedAsymmetricPadding):
+ private_key.signer(FakePadding(), hashes.SHA1(), backend)
+
+ def test_padding_incorrect_type(self, backend):
+ private_key = rsa.RSAPrivateKey.generate(65537, 512, backend)
+ with pytest.raises(TypeError):
+ private_key.signer("notpadding", hashes.SHA1(), backend)