diff options
author | tb <> | 2024-10-30 17:49:27 +0000 |
---|---|---|
committer | tb <> | 2024-10-30 17:49:27 +0000 |
commit | 5178da8bac39346f24655279edfd7f58c1646b41 (patch) | |
tree | b5f13b12e48905d5444aa9a407301866f9cff247 /src/lib | |
parent | f174b640b45fb43e39517a8614a0e8215bad9c13 (diff) | |
download | openbsd-5178da8bac39346f24655279edfd7f58c1646b41.tar.gz openbsd-5178da8bac39346f24655279edfd7f58c1646b41.tar.bz2 openbsd-5178da8bac39346f24655279edfd7f58c1646b41.zip |
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
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/ec/ec_print.c | 44 |
1 files changed, 10 insertions, 34 deletions
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 @@ | |||
1 | /* $OpenBSD: ec_print.c,v 1.16 2024/10/30 17:36:22 tb Exp $ */ | 1 | /* $OpenBSD: ec_print.c,v 1.17 2024/10/30 17:49:27 tb Exp $ */ |
2 | /* ==================================================================== | 2 | /* ==================================================================== |
3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. | 3 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
4 | * | 4 | * |
@@ -117,46 +117,22 @@ EC_POINT_bn2point(const EC_GROUP *group, | |||
117 | } | 117 | } |
118 | LCRYPTO_ALIAS(EC_POINT_bn2point); | 118 | LCRYPTO_ALIAS(EC_POINT_bn2point); |
119 | 119 | ||
120 | static const char *HEX_DIGITS = "0123456789ABCDEF"; | ||
121 | |||
122 | /* the return value must be freed (using free()) */ | ||
123 | char * | 120 | char * |
124 | EC_POINT_point2hex(const EC_GROUP *group, const EC_POINT *point, | 121 | EC_POINT_point2hex(const EC_GROUP *group, const EC_POINT *point, |
125 | point_conversion_form_t form, BN_CTX *ctx) | 122 | point_conversion_form_t form, BN_CTX *ctx) |
126 | { | 123 | { |
127 | char *ret, *p; | 124 | BIGNUM *bn; |
128 | size_t buf_len = 0, i; | 125 | char *hex = NULL; |
129 | unsigned char *buf, *pbuf; | ||
130 | |||
131 | buf_len = EC_POINT_point2oct(group, point, form, | ||
132 | NULL, 0, ctx); | ||
133 | if (buf_len == 0 || buf_len + 1 == 0) | ||
134 | return NULL; | ||
135 | |||
136 | if ((buf = malloc(buf_len)) == NULL) | ||
137 | return NULL; | ||
138 | 126 | ||
139 | if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { | 127 | if ((bn = EC_POINT_point2bn(group, point, form, NULL, ctx)) == NULL) |
140 | free(buf); | 128 | goto err; |
141 | return NULL; | 129 | if ((hex = BN_bn2hex(bn)) == NULL) |
142 | } | 130 | goto err; |
143 | ret = reallocarray(NULL, buf_len + 1, 2); | ||
144 | if (ret == NULL) { | ||
145 | free(buf); | ||
146 | return NULL; | ||
147 | } | ||
148 | p = ret; | ||
149 | pbuf = buf; | ||
150 | for (i = buf_len; i > 0; i--) { | ||
151 | int v = (int) *(pbuf++); | ||
152 | *(p++) = HEX_DIGITS[v >> 4]; | ||
153 | *(p++) = HEX_DIGITS[v & 0x0F]; | ||
154 | } | ||
155 | *p = '\0'; | ||
156 | 131 | ||
157 | free(buf); | 132 | err: |
133 | BN_free(bn); | ||
158 | 134 | ||
159 | return ret; | 135 | return hex; |
160 | } | 136 | } |
161 | LCRYPTO_ALIAS(EC_POINT_point2hex); | 137 | LCRYPTO_ALIAS(EC_POINT_point2hex); |
162 | 138 | ||