]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_print.c
crypto/slh_dsa/slh_hash.c: Add check for EVP_MD_get_size()
[thirdparty/openssl.git] / crypto / ec / ec_print.c
CommitLineData
4f22f405 1/*
7ed6de99 2 * Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved.
6cbe6382 3 *
a7f182b7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
4f22f405
RS
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
6cbe6382
BM
8 */
9
c1131e6a 10#include <string.h> /* strlen */
6cbe6382 11#include <openssl/crypto.h>
ca3c6f38 12#include "internal/cryptlib.h"
706457b7 13#include "ec_local.h"
6cbe6382 14
6cbe6382
BM
15/* the return value must be freed (using OPENSSL_free()) */
16char *EC_POINT_point2hex(const EC_GROUP *group,
17 const EC_POINT *point,
0f113f3e
MC
18 point_conversion_form_t form, BN_CTX *ctx)
19{
20 char *ret, *p;
ca3c6f38
F
21 size_t buf_len, i;
22 unsigned char *buf = NULL;
0f113f3e 23
981bd8a2 24 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
0f113f3e 25
981bd8a2 26 if (buf_len == 0)
0f113f3e 27 return NULL;
0f113f3e 28
b196e7d9 29 ret = OPENSSL_malloc(buf_len * 2 + 2);
ca3c6f38
F
30 if (ret == NULL)
31 goto err;
32
0f113f3e 33 p = ret;
ca3c6f38
F
34 for (i = 0; i < buf_len; ++i)
35 p += ossl_to_hex(p, buf[i]);
0f113f3e
MC
36 *p = '\0';
37
ca3c6f38 38 err:
0f113f3e 39 OPENSSL_free(buf);
0f113f3e
MC
40 return ret;
41}
6cbe6382
BM
42
43EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
c1131e6a 44 const char *hex, EC_POINT *point, BN_CTX *ctx)
0f113f3e 45{
c1131e6a
SL
46 int ok = 0;
47 unsigned char *oct_buf = NULL;
48 size_t len, oct_buf_len = 0;
49 EC_POINT *pt = NULL;
6cbe6382 50
c1131e6a 51 if (group == NULL || hex == NULL)
0f113f3e 52 return NULL;
6cbe6382 53
c1131e6a
SL
54 if (point == NULL) {
55 pt = EC_POINT_new(group);
56 if (pt == NULL)
57 goto err;
58 } else {
59 pt = point;
60 }
6cbe6382 61
c1131e6a
SL
62 len = strlen(hex) / 2;
63 oct_buf = OPENSSL_malloc(len);
64 if (oct_buf == NULL)
dd2fcc1f 65 goto err;
6cbe6382 66
c1131e6a
SL
67 if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0')
68 || !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx))
69 goto err;
70 ok = 1;
71err:
72 OPENSSL_clear_free(oct_buf, oct_buf_len);
73 if (!ok) {
74 if (pt != point)
75 EC_POINT_clear_free(pt);
76 pt = NULL;
77 }
78 return pt;
0f113f3e 79}