Skip to content

Commit f70de66

Browse files
committed
Fixed DH tests for latest CentOS FIPS OpenSSL
1 parent 6693d55 commit f70de66

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

.github/workflows/ci.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ jobs:
8888
IMAGE:
8989
- {IMAGE: "pyca/cryptography-runner-centos8", TOXENV: "py27"}
9090
- {IMAGE: "pyca/cryptography-runner-centos8", TOXENV: "py36"}
91-
- {IMAGE: "pyca/cryptography-runner-centos8-fips", TOXENV: "py36", ENV: "OPENSSL_FORCE_FIPS_MODE=1"}
91+
- {IMAGE: "pyca/cryptography-runner-centos8-fips", TOXENV: "py36", ENV: "OPENSSL_FORCE_FIPS_MODE=1\nCFLAGS=-DUSE_OSRANDOM_RNG_FOR_TESTING"}
9292
- {IMAGE: "pyca/cryptography-runner-stretch", TOXENV: "py27"}
9393
- {IMAGE: "pyca/cryptography-runner-buster", TOXENV: "py37"}
9494
- {IMAGE: "pyca/cryptography-runner-bullseye", TOXENV: "py38"}
@@ -104,7 +104,7 @@ jobs:
104104
steps:
105105
- uses: actions/checkout@v2
106106
- run: 'git clone --depth=1 https://github.com/google/wycheproof "$HOME/wycheproof"'
107-
- run: 'echo "$ENV_VAR" >> $GITHUB_ENV'
107+
- run: 'echo -e "$ENV_VAR" >> $GITHUB_ENV'
108108
if: matrix.IMAGE.ENV
109109
env:
110110
ENV_VAR: ${{ matrix.IMAGE.ENV }}

tests/hazmat/primitives/test_dh.py

+12
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def test_unsupported_generator_generate_dh(self, backend):
151151
with pytest.raises(ValueError):
152152
dh.generate_parameters(7, 512, backend)
153153

154+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
154155
def test_dh_parameters_supported(self, backend):
155156
valid_p = int(
156157
b"907c7211ae61aaaba1825ff53b6cb71ac6df9f1a424c033f4a0a41ac42fad3a9"
@@ -171,6 +172,12 @@ def test_dh_parameters_supported(self, backend):
171172
)
172173
def test_dh_parameters_allows_rfc3526_groups(self, backend, vector):
173174
p = int_from_bytes(binascii.unhexlify(vector["p"]), "big")
175+
if (
176+
backend._fips_enabled
177+
and p.bit_length() < backend._fips_dh_min_modulus
178+
):
179+
pytest.skip("modulus too small for FIPS mode")
180+
174181
params = dh.DHParameterNumbers(p, int(vector["g"]))
175182
param = params.parameters(backend)
176183
key = param.generate_private_key()
@@ -180,6 +187,7 @@ def test_dh_parameters_allows_rfc3526_groups(self, backend, vector):
180187
roundtripped_key = key.private_numbers().private_key(backend)
181188
assert key.private_numbers() == roundtripped_key.private_numbers()
182189

190+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
183191
@pytest.mark.parametrize(
184192
"vector",
185193
load_vectors_from_file(
@@ -227,6 +235,7 @@ def test_convert_to_numbers(self, backend, with_q):
227235
deserialized_private, dh.DHPrivateKeyWithSerialization
228236
)
229237

238+
@pytest.mark.skip_fips(reason="FIPS requires specific parameters")
230239
def test_numbers_unsupported_parameters(self, backend):
231240
# p is set to P_1536 + 1 because when calling private_key we want it to
232241
# fail the DH_check call OpenSSL does, but we specifically want it to
@@ -415,6 +424,7 @@ def test_dh_vectors(self, backend, vector):
415424

416425
assert int_from_bytes(symkey, "big") == int(vector["k"], 16)
417426

427+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
418428
@pytest.mark.parametrize(
419429
"vector",
420430
load_vectors_from_file(
@@ -477,6 +487,7 @@ def test_private_bytes_rejects_invalid(self, encoding, fmt, backend):
477487
with pytest.raises(ValueError):
478488
key.private_bytes(encoding, fmt, serialization.NoEncryption())
479489

490+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
480491
@pytest.mark.parametrize(
481492
("key_path", "loader_func", "encoding", "is_dhx"),
482493
[
@@ -521,6 +532,7 @@ def test_private_bytes_match(
521532
)
522533
assert serialized == key_bytes
523534

535+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
524536
@pytest.mark.parametrize(
525537
("key_path", "loader_func", "vec_path", "is_dhx"),
526538
[

tests/hazmat/primitives/test_serialization.py

+2
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,7 @@ def test_openssh_serialization_unsupported(self, backend):
17571757
class TestDHSerialization(object):
17581758
"""Test all options with least-supported key type."""
17591759

1760+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
17601761
def test_dh_public_key(self, backend):
17611762
data = load_vectors_from_file(
17621763
os.path.join("asymmetric", "DH", "dhkey.pem"),
@@ -1788,6 +1789,7 @@ def test_dh_public_key(self, backend):
17881789
with pytest.raises(ValueError):
17891790
public_key.public_bytes(enc, fmt)
17901791

1792+
@pytest.mark.skip_fips(reason="non-FIPS parameters")
17911793
def test_dh_private_key(self, backend):
17921794
data = load_vectors_from_file(
17931795
os.path.join("asymmetric", "DH", "dhkey.pem"),

tests/x509/test_x509.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
)
4242
from cryptography.hazmat.primitives import hashes, serialization
4343
from cryptography.hazmat.primitives.asymmetric import (
44+
dh,
4445
dsa,
4546
ec,
4647
ed25519,
@@ -51,6 +52,7 @@
5152
from cryptography.hazmat.primitives.asymmetric.utils import (
5253
decode_dss_signature,
5354
)
55+
from cryptography.utils import int_from_bytes
5456
from cryptography.x509.name import _ASN1Type
5557
from cryptography.x509.oid import (
5658
AuthorityInformationAccessOID,
@@ -65,7 +67,7 @@
6567
from ..hazmat.primitives.fixtures_ec import EC_KEY_SECP256R1
6668
from ..hazmat.primitives.fixtures_rsa import RSA_KEY_2048, RSA_KEY_512
6769
from ..hazmat.primitives.test_ec import _skip_curve_unsupported
68-
from ..utils import load_vectors_from_file
70+
from ..utils import load_nist_vectors, load_vectors_from_file
6971

7072

7173
@utils.register_interface(x509.ExtensionType)
@@ -5237,12 +5239,14 @@ class TestSignatureRejection(object):
52375239
"""Test if signing rejects DH keys properly."""
52385240

52395241
def load_key(self, backend):
5240-
data = load_vectors_from_file(
5241-
os.path.join("asymmetric", "DH", "dhkey.pem"),
5242-
lambda pemfile: pemfile.read(),
5243-
mode="rb",
5244-
)
5245-
return serialization.load_pem_private_key(data, None, backend)
5242+
vector = load_vectors_from_file(
5243+
os.path.join("asymmetric", "DH", "rfc3526.txt"),
5244+
load_nist_vectors,
5245+
)[1]
5246+
p = int_from_bytes(binascii.unhexlify(vector["p"]), "big")
5247+
params = dh.DHParameterNumbers(p, int(vector["g"]))
5248+
param = params.parameters(backend)
5249+
return param.generate_private_key()
52465250

52475251
def test_crt_signing_check(self, backend):
52485252
issuer_private_key = self.load_key(backend)

0 commit comments

Comments
 (0)