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

Expose EC group order #5

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
7 changes: 7 additions & 0 deletions src/_cffi_src/openssl/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
int nid;
const char *comment;
} EC_builtin_curve;
typedef ... EC_GROUP;
"""

FUNCTIONS = """
Expand All @@ -23,6 +24,12 @@
void EC_KEY_free(EC_KEY *);

EC_KEY *EC_KEY_new_by_curve_name(int);

EC_GROUP *EC_GROUP_new_by_curve_name(int);

int EC_GROUP_get_order(const EC_GROUP *, BIGNUM *, BN_CTX *);

void EC_GROUP_free(EC_GROUP *);
"""

CUSTOMIZATIONS = """
Expand Down
25 changes: 25 additions & 0 deletions src/cryptography/hazmat/backends/openssl/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,31 @@ def elliptic_curve_exchange_algorithm_supported(
algorithm, ec.ECDH
)

def elliptic_curve_group_order(self, curve: ec.EllipticCurve) -> int:
group = self._lib.EC_GROUP_new_by_curve_name(
self._lib.OBJ_txt2nid(curve.name.encode())
)

if not group:
raise Exception("EC_GROUP_new_by_curve_name failed")

bn = self._lib.BN_new()

if not bn:
self._lib.EC_GROUP_free(group)
raise Exception("BN_new failed")

if self._lib.EC_GROUP_get_order(group, bn, self._ffi.NULL) != 1:
self._lib.BN_free(bn)
self._lib.EC_GROUP_free(group)
raise Exception("EC_GROUP_get_order failed")

group_order = int(backend._ffi.string(self._lib.BN_bn2hex(bn)), 16)

self._lib.BN_free(bn)
self._lib.EC_GROUP_free(group)
return group_order

def dh_supported(self) -> bool:
return not rust_openssl.CRYPTOGRAPHY_IS_BORINGSSL

Expand Down
9 changes: 9 additions & 0 deletions src/cryptography/hazmat/primitives/asymmetric/ec.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ def key_size(self) -> int:
Bit size of a secret scalar for the curve.
"""

@property
def group_order(self) -> int:
"""
The order of the curve's group
"""
from cryptography.hazmat.backends.openssl.backend import backend

return backend.elliptic_curve_group_order(self)


class EllipticCurveSignatureAlgorithm(metaclass=abc.ABCMeta):
@property
Expand Down