Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow p % 24 == 23 when generator == 2 in DH_check #3768

Merged
merged 4 commits into from
Jul 10, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/_cffi_src/openssl/dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

TYPES = """
typedef ... DH;

const long DH_NOT_SUITABLE_GENERATOR;
"""

FUNCTIONS = """
Expand Down
13 changes: 11 additions & 2 deletions src/cryptography/hazmat/backends/openssl/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,8 +1776,17 @@ def load_dh_private_numbers(self, numbers):
res = self._lib.Cryptography_DH_check(dh_cdata, codes)
self.openssl_assert(res == 1)

if codes[0] != 0:
raise ValueError("DH private numbers did not pass safety checks.")
# DH_check will return DH_NOT_SUITABLE_GENERATOR if p % 24 does not
# equal 11 when the generator is 2. We want to ignore that error
# because p % 24 == 23 is also fine. See:
# https://crypto.stackexchange.com/questions/12961
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be marginally happier if this had the language "quadratic residue" and "quadratic nonresidue", but looking at the SO answer it's actually... really good?

if codes[0] != 0 and not (
parameter_numbers.g == 2 and
codes[0] ^ self._lib.DH_NOT_SUITABLE_GENERATOR == 0
):
raise ValueError(
"DH private numbers did not pass safety checks."
)

evp_pkey = self._dh_cdata_to_evp_pkey(dh_cdata)

Expand Down
21 changes: 20 additions & 1 deletion tests/hazmat/primitives/test_dh.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from __future__ import absolute_import, division, print_function

import binascii
import os

import pytest
Expand Down Expand Up @@ -158,6 +159,24 @@ def test_dh_parameters_supported(self, backend):
assert backend.dh_parameters_supported(23, 5)
assert not backend.dh_parameters_supported(23, 18)

@pytest.mark.parametrize(
"vector",
load_vectors_from_file(
os.path.join("asymmetric", "DH", "rfc3526.txt"),
load_nist_vectors
)
)
def test_dh_parameters_allows_rfc3526_groups(self, backend, vector):
p = int_from_bytes(binascii.unhexlify(vector["p"]), 'big')
params = dh.DHParameterNumbers(p, int(vector["g"]))
param = params.parameters(backend)
key = param.generate_private_key()
# This confirms that a key generated with this group
# will pass DH_check when we serialize and de-serialize it via
# the Numbers path.
roundtripped_key = key.private_numbers().private_key(backend)
assert key.private_numbers() == roundtripped_key.private_numbers()

@pytest.mark.parametrize(
"vector",
load_vectors_from_file(
Expand Down Expand Up @@ -202,7 +221,7 @@ def test_convert_to_numbers(self, backend, with_q):
dh.DHPrivateKeyWithSerialization)

def test_numbers_unsupported_parameters(self, backend):
params = dh.DHParameterNumbers(23, 2)
params = dh.DHParameterNumbers(21, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be happier if this had a comment, because someone git-blaming this (heck, me git blaming this) would probably not stop to think about why this is different/better and why 23 suddenly works now.

public = dh.DHPublicNumbers(1, params)
private = dh.DHPrivateNumbers(2, public)

Expand Down