blob: b8f2e19d093c4a55ccccace22190a797659d6b84 [file] [log] [blame]
Alex Gaynor2e85a922018-07-16 11:18:33 -04001# This file is dual licensed under the terms of the Apache License, Version
2# 2.0, and the BSD License. See the LICENSE file in the root of this repository
3# for complete details.
4
5from __future__ import absolute_import, division, print_function
6
7import binascii
8
9import pytest
10
11from cryptography.exceptions import InvalidSignature
12from cryptography.hazmat.backends.interfaces import RSABackend
13from cryptography.hazmat.primitives import hashes, serialization
14from cryptography.hazmat.primitives.asymmetric import padding
15
16
17_DIGESTS = {
18 "SHA-1": hashes.SHA1(),
19 "SHA-224": hashes.SHA224(),
20 "SHA-256": hashes.SHA256(),
21 "SHA-384": hashes.SHA384(),
22 "SHA-512": hashes.SHA512(),
23}
24
25
26def should_verify(backend, wycheproof):
27 if wycheproof.valid:
28 return True
29
30 if wycheproof.acceptable:
31 if (
32 backend._lib.CRYPTOGRAPHY_OPENSSL_110_OR_GREATER and
33 wycheproof.has_flag("MissingNull")
34 ):
35 return False
36 return True
37
38 return False
39
40
41@pytest.mark.requires_backend_interface(interface=RSABackend)
42@pytest.mark.supported(
43 only_if=lambda backend: (
44 # TODO: this also skips on LibreSSL, which is ok for now, since these
45 # don't pass on Libre, but we'll need to fix this after they resolve
46 # it.
47 not backend._lib.CRYPTOGRAPHY_OPENSSL_LESS_THAN_102
48 ),
49 skip_message=(
50 "Many of these tests fail on OpenSSL < 1.0.2 and since upstream isn't"
51 " maintaining it, they'll never be fixed."
52 ),
53)
54@pytest.mark.wycheproof_tests(
55 "rsa_signature_test.json",
56 "rsa_signature_2048_sha224_test.json",
57 "rsa_signature_2048_sha256_test.json",
58 "rsa_signature_2048_sha512_test.json",
59 "rsa_signature_3072_sha256_test.json",
60 "rsa_signature_3072_sha384_test.json",
61 "rsa_signature_3072_sha512_test.json",
62 "rsa_signature_4096_sha384_test.json",
63 "rsa_signature_4096_sha512_test.json",
64)
65def test_rsa_signature(backend, wycheproof):
66 key = serialization.load_der_public_key(
67 binascii.unhexlify(wycheproof.testgroup["keyDer"]), backend
68 )
69 digest = _DIGESTS[wycheproof.testgroup["sha"]]
70
71 if should_verify(backend, wycheproof):
72 key.verify(
73 binascii.unhexlify(wycheproof.testcase["sig"]),
74 binascii.unhexlify(wycheproof.testcase["msg"]),
75 padding.PKCS1v15(),
76 digest,
77 )
78 else:
79 with pytest.raises(InvalidSignature):
80 key.verify(
81 binascii.unhexlify(wycheproof.testcase["sig"]),
82 binascii.unhexlify(wycheproof.testcase["msg"]),
83 padding.PKCS1v15(),
84 digest,
85 )