From 069691a27b98bbca13faa843a1408d631b691bea Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 26 Apr 2020 11:44:49 -0400 Subject: Added wycheproof RSA PKCSv1 encryption tests (#5234) --- tests/wycheproof/test_rsa.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'tests') diff --git a/tests/wycheproof/test_rsa.py b/tests/wycheproof/test_rsa.py index 8a971d98..f17eff69 100644 --- a/tests/wycheproof/test_rsa.py +++ b/tests/wycheproof/test_rsa.py @@ -78,7 +78,9 @@ def test_rsa_pkcs1v15_signature(backend, wycheproof): digest = _DIGESTS[wycheproof.testgroup["sha"]] if digest is None or not backend.hash_supported(digest): - pytest.skip("Hash {} not supported".format(digest)) + pytest.skip( + "Hash {} not supported".format(wycheproof.testgroup["sha"]) + ) if should_verify(backend, wycheproof): key.verify( @@ -184,7 +186,11 @@ def test_rsa_oaep_encryption(backend, wycheproof): ) if not backend.rsa_padding_supported(padding_algo): - pytest.skip("Padding {} not supported".format(padding_algo)) + pytest.skip( + "OAEP with digest={} and MGF digest={} not supported".format( + wycheproof.testgroup["sha"], wycheproof.testgroup["mgfSha"], + ) + ) if wycheproof.valid or wycheproof.acceptable: pt = key.decrypt( @@ -198,3 +204,29 @@ def test_rsa_oaep_encryption(backend, wycheproof): binascii.unhexlify(wycheproof.testcase["ct"]), padding_algo ) + + +@pytest.mark.wycheproof_tests( + "rsa_pkcs1_2048_test.json", + "rsa_pkcs1_3072_test.json", + "rsa_pkcs1_4096_test.json", +) +def test_rsa_pkcs1_encryption(backend, wycheproof): + key = serialization.load_pem_private_key( + wycheproof.testgroup["privateKeyPem"].encode("ascii"), + password=None, + backend=backend, + ) + + if wycheproof.valid: + pt = key.decrypt( + binascii.unhexlify(wycheproof.testcase["ct"]), + padding.PKCS1v15() + ) + assert pt == binascii.unhexlify(wycheproof.testcase["msg"]) + else: + with pytest.raises(ValueError): + key.decrypt( + binascii.unhexlify(wycheproof.testcase["ct"]), + padding.PKCS1v15() + ) -- cgit v1.2.3