From 5178da8bac39346f24655279edfd7f58c1646b41 Mon Sep 17 00:00:00 2001 From: tb <> Date: Wed, 30 Oct 2024 17:49:27 +0000 Subject: Rewrite EC_POINT_point2hex() Instead of doing everything by hand, this can use EC_POINT_point2bn() and chain it with BN_bn2hex(), slashing the number of lines in half. This removes one of the ten remaining "01234567890ABCDEF" strings from libcrypto. Unfortunately, none of the nine others is used in an API that could convert the octet string directly, so we use that ugly detour via a bignum. Still it's better than what was there. ok jsing --- src/lib/libcrypto/ec/ec_print.c | 44 ++++++++++------------------------------- 1 file changed, 10 insertions(+), 34 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/ec/ec_print.c b/src/lib/libcrypto/ec/ec_print.c index d8261a94f5..698d2be484 100644 --- a/src/lib/libcrypto/ec/ec_print.c +++ b/src/lib/libcrypto/ec/ec_print.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ec_print.c,v 1.16 2024/10/30 17:36:22 tb Exp $ */ +/* $OpenBSD: ec_print.c,v 1.17 2024/10/30 17:49:27 tb Exp $ */ /* ==================================================================== * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. * @@ -117,46 +117,22 @@ EC_POINT_bn2point(const EC_GROUP *group, } LCRYPTO_ALIAS(EC_POINT_bn2point); -static const char *HEX_DIGITS = "0123456789ABCDEF"; - -/* the return value must be freed (using free()) */ char * EC_POINT_point2hex(const EC_GROUP *group, const EC_POINT *point, point_conversion_form_t form, BN_CTX *ctx) { - char *ret, *p; - size_t buf_len = 0, i; - unsigned char *buf, *pbuf; - - buf_len = EC_POINT_point2oct(group, point, form, - NULL, 0, ctx); - if (buf_len == 0 || buf_len + 1 == 0) - return NULL; - - if ((buf = malloc(buf_len)) == NULL) - return NULL; + BIGNUM *bn; + char *hex = NULL; - if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { - free(buf); - return NULL; - } - ret = reallocarray(NULL, buf_len + 1, 2); - if (ret == NULL) { - free(buf); - return NULL; - } - p = ret; - pbuf = buf; - for (i = buf_len; i > 0; i--) { - int v = (int) *(pbuf++); - *(p++) = HEX_DIGITS[v >> 4]; - *(p++) = HEX_DIGITS[v & 0x0F]; - } - *p = '\0'; + if ((bn = EC_POINT_point2bn(group, point, form, NULL, ctx)) == NULL) + goto err; + if ((hex = BN_bn2hex(bn)) == NULL) + goto err; - free(buf); + err: + BN_free(bn); - return ret; + return hex; } LCRYPTO_ALIAS(EC_POINT_point2hex); -- cgit v1.2.3-55-g6feb