From 97e0de7f2cfb7bc5dc7a47959f6ec4c7c28eb8ad Mon Sep 17 00:00:00 2001 From: tb Date: Tue, 22 Oct 2024 12:06:08 +0000 Subject: [PATCH] Provide and use ec_group_get_field_type() All internal uses of EC_METHOD_get_field_type() and EC_GROUP_method_of() are chained together. Implement this as a single API call that takes a group and use it throughout. Gets rid of another eyesore in this part of the tree. Not that there will be a shortage of eyesores anytime soon... ok jsing --- lib/libcrypto/ec/ec_asn1.c | 4 ++-- lib/libcrypto/ec/ec_lib.c | 14 +++++++++++--- lib/libcrypto/ec/ec_local.h | 3 ++- lib/libcrypto/ec/eck_prn.c | 4 ++-- 4 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/libcrypto/ec/ec_asn1.c b/lib/libcrypto/ec/ec_asn1.c index 6e97d43c5c2..60225f48f9f 100644 --- a/lib/libcrypto/ec/ec_asn1.c +++ b/lib/libcrypto/ec/ec_asn1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_asn1.c,v 1.76 2024/10/20 10:48:29 tb Exp $ */ +/* $OpenBSD: ec_asn1.c,v 1.77 2024/10/22 12:06:08 tb Exp $ */ /* * Written by Nils Larsch for the OpenSSL project. */ @@ -570,7 +570,7 @@ ec_asn1_group2fieldid(const EC_GROUP *group, X9_62_FIELDID *field) if (group == NULL || field == NULL) goto err; - nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); + nid = ec_group_get_field_type(group); if (nid == NID_X9_62_characteristic_two_field) { ECerror(EC_R_GF2M_NOT_SUPPORTED); goto err; diff --git a/lib/libcrypto/ec/ec_lib.c b/lib/libcrypto/ec/ec_lib.c index 1918d0ba527..6da20266622 100644 --- a/lib/libcrypto/ec/ec_lib.c +++ b/lib/libcrypto/ec/ec_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_lib.c,v 1.72 2024/10/19 08:29:40 tb Exp $ */ +/* $OpenBSD: ec_lib.c,v 1.73 2024/10/22 12:06:08 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -219,6 +219,15 @@ EC_METHOD_get_field_type(const EC_METHOD *meth) } LCRYPTO_ALIAS(EC_METHOD_get_field_type); +int +ec_group_get_field_type(const EC_GROUP *group) +{ + if (group == NULL || group->meth == NULL) + return NID_undef; + + return group->meth->field_type; +} + /* * If there is a user-provided cofactor, sanity check and use it. Otherwise * try computing the cofactor from generator order n and field cardinality q. @@ -663,8 +672,7 @@ EC_GROUP_cmp(const EC_GROUP *a, const EC_GROUP *b, BN_CTX *ctx) BN_CTX *ctx_new = NULL; /* compare the field types */ - if (EC_METHOD_get_field_type(EC_GROUP_method_of(a)) != - EC_METHOD_get_field_type(EC_GROUP_method_of(b))) + if (ec_group_get_field_type(a) != ec_group_get_field_type(b)) return 1; /* compare the curve name (if present in both) */ if (EC_GROUP_get_curve_name(a) && EC_GROUP_get_curve_name(b) && diff --git a/lib/libcrypto/ec/ec_local.h b/lib/libcrypto/ec/ec_local.h index b837e291f7c..4786f8520b8 100644 --- a/lib/libcrypto/ec/ec_local.h +++ b/lib/libcrypto/ec/ec_local.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_local.h,v 1.30 2024/10/18 17:27:07 tb Exp $ */ +/* $OpenBSD: ec_local.h,v 1.31 2024/10/22 12:06:08 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -356,6 +356,7 @@ int EC_POINT_get_Jprojective_coordinates(const EC_GROUP *group, const EC_POINT *p, BIGNUM *x, BIGNUM *y, BIGNUM *z, BN_CTX *ctx); int ec_group_is_builtin_curve(const EC_GROUP *group); +int ec_group_get_field_type(const EC_GROUP *group); /* Public API in OpenSSL */ const BIGNUM *EC_GROUP_get0_cofactor(const EC_GROUP *group); diff --git a/lib/libcrypto/ec/eck_prn.c b/lib/libcrypto/ec/eck_prn.c index 2798d53d0c6..847dc0a159a 100644 --- a/lib/libcrypto/ec/eck_prn.c +++ b/lib/libcrypto/ec/eck_prn.c @@ -1,4 +1,4 @@ -/* $OpenBSD: eck_prn.c,v 1.30 2023/11/21 22:05:33 tb Exp $ */ +/* $OpenBSD: eck_prn.c,v 1.31 2024/10/22 12:06:08 tb Exp $ */ /* * Written by Nils Larsch for the OpenSSL project. */ @@ -258,7 +258,7 @@ ecpk_print_explicit_parameters(BIO *bp, const EC_GROUP *group, int off) if (!BIO_indent(bp, off, 128)) goto err; - nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); + nid = ec_group_get_field_type(group); if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(nid)) <= 0) goto err; -- 2.20.1