From d5c28a21f36a8e340666157c55c2a534c013b5ae Mon Sep 17 00:00:00 2001 From: tb <> Date: Mon, 4 Nov 2024 13:19:08 +0000 Subject: Rewrite EC_POINT_new() and EC_POINT_dup() Like most of the code in this file that hasn't been overhauled, these are just terrible. As jsing points out, we will need to ensure that finish() works on a not fully initialized point. That's currently safe. ok jsing --- src/lib/libcrypto/ec/ec_lib.c | 61 ++++++++++++++++++++++++------------------- 1 file changed, 34 insertions(+), 27 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/ec/ec_lib.c b/src/lib/libcrypto/ec/ec_lib.c index 327cda900b..0d28df59ba 100644 --- a/src/lib/libcrypto/ec/ec_lib.c +++ b/src/lib/libcrypto/ec/ec_lib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_lib.c,v 1.77 2024/11/03 13:53:14 tb Exp $ */ +/* $OpenBSD: ec_lib.c,v 1.78 2024/11/04 13:19:08 tb Exp $ */ /* * Originally written by Bodo Moeller for the OpenSSL project. */ @@ -762,28 +762,33 @@ ec_point_blind_coordinates(const EC_GROUP *group, EC_POINT *p, BN_CTX *ctx) EC_POINT * EC_POINT_new(const EC_GROUP *group) { - EC_POINT *ret; + EC_POINT *point = NULL; if (group == NULL) { ECerror(ERR_R_PASSED_NULL_PARAMETER); - return NULL; + goto err; } if (group->meth->point_init == NULL) { ECerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); - return NULL; + goto err; } - ret = malloc(sizeof *ret); - if (ret == NULL) { + + if ((point = calloc(1, sizeof(*point))) == NULL) { ECerror(ERR_R_MALLOC_FAILURE); - return NULL; + goto err; } - ret->meth = group->meth; - if (!ret->meth->point_init(ret)) { - free(ret); - return NULL; - } - return ret; + point->meth = group->meth; + + if (!point->meth->point_init(point)) + goto err; + + return point; + + err: + EC_POINT_free(point); + + return NULL; } LCRYPTO_ALIAS(EC_POINT_new); @@ -825,23 +830,25 @@ EC_POINT_copy(EC_POINT *dest, const EC_POINT *src) LCRYPTO_ALIAS(EC_POINT_copy); EC_POINT * -EC_POINT_dup(const EC_POINT *a, const EC_GROUP *group) +EC_POINT_dup(const EC_POINT *in_point, const EC_GROUP *group) { - EC_POINT *t; - int r; + EC_POINT *point = NULL; - if (a == NULL) - return NULL; + if (in_point == NULL) + goto err; - t = EC_POINT_new(group); - if (t == NULL) - return (NULL); - r = EC_POINT_copy(t, a); - if (!r) { - EC_POINT_free(t); - return NULL; - } else - return t; + if ((point = EC_POINT_new(group)) == NULL) + goto err; + + if (!EC_POINT_copy(point, in_point)) + goto err; + + return point; + + err: + EC_POINT_free(point); + + return NULL; } LCRYPTO_ALIAS(EC_POINT_dup); -- cgit v1.2.3-55-g6feb