Skip to content

Commit 9a09f96

Browse files
davidbenreaperhulk
authored andcommitted
Fix some backend feature checks in tests (pyca#4931)
* Remove irrelevant DHBackend test conditions DHBackend provides functions for plain finite-field Diffie-Hellman. X25519 and X448 are their own algorithms, and Ed25519 and Ed448 aren't even Diffie-Hellman primitives. * Add missing backend support checks. Some new AES and EC tests did not check for whether the corresponding mode or curve was supported by the backend. * Add a DummyMode for coverage
1 parent 1e8c5a6 commit 9a09f96

8 files changed

Lines changed: 10 additions & 20 deletions

File tree

tests/hazmat/primitives/test_aes.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from cryptography.hazmat.primitives.ciphers import algorithms, base, modes
1414

1515
from .utils import _load_all_params, generate_aead_test, generate_encrypt_test
16+
from ...doubles import DummyMode
1617
from ...utils import load_nist_vectors
1718

1819

@@ -484,14 +485,17 @@ def test_buffer_protocol(self, backend):
484485
modes.CFB(bytearray(b"\x00" * 16)),
485486
modes.CFB8(bytearray(b"\x00" * 16)),
486487
modes.XTS(bytearray(b"\x00" * 16)),
488+
# Add a dummy mode for coverage of the cipher_supported check.
489+
DummyMode(),
487490
]
488491
)
489492
@pytest.mark.requires_backend_interface(interface=CipherBackend)
490493
def test_buffer_protocol_alternate_modes(mode, backend):
491494
data = bytearray(b"sixteen_byte_msg")
492-
cipher = base.Cipher(
493-
algorithms.AES(bytearray(os.urandom(32))), mode, backend
494-
)
495+
key = algorithms.AES(bytearray(os.urandom(32)))
496+
if not backend.cipher_supported(key, mode):
497+
pytest.skip("AES in {} mode not supported".format(mode.name))
498+
cipher = base.Cipher(key, mode, backend)
495499
enc = cipher.encryptor()
496500
ct = enc.update(data) + enc.finalize()
497501
dec = cipher.decryptor()

tests/hazmat/primitives/test_ec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,11 +1070,12 @@ def test_public_bytes_pkcs1_unsupported(self, backend):
10701070
load_nist_vectors
10711071
)
10721072
)
1073-
def test_from_encoded_point_compressed(self, vector):
1073+
def test_from_encoded_point_compressed(self, vector, backend):
10741074
curve = {
10751075
b"SECP256R1": ec.SECP256R1(),
10761076
b"SECP256K1": ec.SECP256K1(),
10771077
}[vector["curve"]]
1078+
_skip_curve_unsupported(backend, curve)
10781079
point = binascii.unhexlify(vector["point"])
10791080
pn = ec.EllipticCurvePublicKey.from_encoded_point(curve, point)
10801081
public_num = pn.public_numbers()
@@ -1155,6 +1156,7 @@ def test_serialize_point(self, vector, backend):
11551156
b"SECP256R1": ec.SECP256R1(),
11561157
b"SECP256K1": ec.SECP256K1(),
11571158
}[vector["curve"]]
1159+
_skip_curve_unsupported(backend, curve)
11581160
point = binascii.unhexlify(vector["point"])
11591161
key = ec.EllipticCurvePublicKey.from_encoded_point(curve, point)
11601162
key2 = ec.EllipticCurvePublicKey.from_encoded_point(

tests/hazmat/primitives/test_ed25519.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
from cryptography.exceptions import InvalidSignature, _Reasons
13-
from cryptography.hazmat.backends.interfaces import DHBackend
1413
from cryptography.hazmat.primitives import serialization
1514
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
1615
Ed25519PrivateKey, Ed25519PublicKey
@@ -47,7 +46,6 @@ def load_ed25519_vectors(vector_data):
4746
only_if=lambda backend: not backend.ed25519_supported(),
4847
skip_message="Requires OpenSSL without Ed25519 support"
4948
)
50-
@pytest.mark.requires_backend_interface(interface=DHBackend)
5149
def test_ed25519_unsupported(backend):
5250
with raises_unsupported_algorithm(
5351
_Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM
@@ -69,7 +67,6 @@ def test_ed25519_unsupported(backend):
6967
only_if=lambda backend: backend.ed25519_supported(),
7068
skip_message="Requires OpenSSL with Ed25519 support"
7169
)
72-
@pytest.mark.requires_backend_interface(interface=DHBackend)
7370
class TestEd25519Signing(object):
7471
@pytest.mark.parametrize(
7572
"vector",

tests/hazmat/primitives/test_ed448.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
from cryptography.exceptions import InvalidSignature, _Reasons
13-
from cryptography.hazmat.backends.interfaces import DHBackend
1413
from cryptography.hazmat.primitives import serialization
1514
from cryptography.hazmat.primitives.asymmetric.ed448 import (
1615
Ed448PrivateKey, Ed448PublicKey
@@ -25,7 +24,6 @@
2524
only_if=lambda backend: not backend.ed448_supported(),
2625
skip_message="Requires OpenSSL without Ed448 support"
2726
)
28-
@pytest.mark.requires_backend_interface(interface=DHBackend)
2927
def test_ed448_unsupported(backend):
3028
with raises_unsupported_algorithm(
3129
_Reasons.UNSUPPORTED_PUBLIC_KEY_ALGORITHM
@@ -47,7 +45,6 @@ def test_ed448_unsupported(backend):
4745
only_if=lambda backend: backend.ed448_supported(),
4846
skip_message="Requires OpenSSL with Ed448 support"
4947
)
50-
@pytest.mark.requires_backend_interface(interface=DHBackend)
5148
class TestEd448Signing(object):
5249
@pytest.mark.parametrize(
5350
"vector",

tests/hazmat/primitives/test_x25519.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
from cryptography import utils
1313
from cryptography.exceptions import _Reasons
14-
from cryptography.hazmat.backends.interfaces import DHBackend
1514
from cryptography.hazmat.primitives import serialization
1615
from cryptography.hazmat.primitives.asymmetric.x25519 import (
1716
X25519PrivateKey, X25519PublicKey
@@ -26,7 +25,6 @@
2625
only_if=lambda backend: not backend.x25519_supported(),
2726
skip_message="Requires OpenSSL without X25519 support"
2827
)
29-
@pytest.mark.requires_backend_interface(interface=DHBackend)
3028
def test_x25519_unsupported(backend):
3129
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM):
3230
X25519PublicKey.from_public_bytes(b"0" * 32)
@@ -42,7 +40,6 @@ def test_x25519_unsupported(backend):
4240
only_if=lambda backend: backend.x25519_supported(),
4341
skip_message="Requires OpenSSL with X25519 support"
4442
)
45-
@pytest.mark.requires_backend_interface(interface=DHBackend)
4643
class TestX25519Exchange(object):
4744
@pytest.mark.parametrize(
4845
"vector",

tests/hazmat/primitives/test_x448.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import pytest
1111

1212
from cryptography.exceptions import _Reasons
13-
from cryptography.hazmat.backends.interfaces import DHBackend
1413
from cryptography.hazmat.primitives import serialization
1514
from cryptography.hazmat.primitives.asymmetric.x448 import (
1615
X448PrivateKey, X448PublicKey
@@ -25,7 +24,6 @@
2524
only_if=lambda backend: not backend.x448_supported(),
2625
skip_message="Requires OpenSSL without X448 support"
2726
)
28-
@pytest.mark.requires_backend_interface(interface=DHBackend)
2927
def test_x448_unsupported(backend):
3028
with raises_unsupported_algorithm(_Reasons.UNSUPPORTED_EXCHANGE_ALGORITHM):
3129
X448PublicKey.from_public_bytes(b"0" * 56)
@@ -41,7 +39,6 @@ def test_x448_unsupported(backend):
4139
only_if=lambda backend: backend.x448_supported(),
4240
skip_message="Requires OpenSSL with X448 support"
4341
)
44-
@pytest.mark.requires_backend_interface(interface=DHBackend)
4542
class TestX448Exchange(object):
4643
@pytest.mark.parametrize(
4744
"vector",

tests/wycheproof/test_eddsa.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pytest
1010

1111
from cryptography.exceptions import InvalidSignature
12-
from cryptography.hazmat.backends.interfaces import DHBackend
1312
from cryptography.hazmat.primitives.asymmetric.ed25519 import (
1413
Ed25519PublicKey
1514
)
@@ -19,7 +18,6 @@
1918
only_if=lambda backend: backend.ed25519_supported(),
2019
skip_message="Requires OpenSSL with Ed25519 support"
2120
)
22-
@pytest.mark.requires_backend_interface(interface=DHBackend)
2321
@pytest.mark.wycheproof_tests(
2422
"eddsa_test.json",
2523
)

tests/wycheproof/test_x25519.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
import pytest
1010

11-
from cryptography.hazmat.backends.interfaces import DHBackend
1211
from cryptography.hazmat.primitives.asymmetric.x25519 import (
1312
X25519PrivateKey, X25519PublicKey
1413
)
@@ -18,7 +17,6 @@
1817
only_if=lambda backend: backend.x25519_supported(),
1918
skip_message="Requires OpenSSL with X25519 support"
2019
)
21-
@pytest.mark.requires_backend_interface(interface=DHBackend)
2220
@pytest.mark.wycheproof_tests("x25519_test.json")
2321
def test_x25519(backend, wycheproof):
2422
assert list(wycheproof.testgroup.items()) == [("curve", "curve25519")]

0 commit comments

Comments
 (0)