2 * Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
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
10 #include <string.h> /* strlen */
11 #include <openssl/crypto.h>
14 static const char *HEX_DIGITS = "0123456789ABCDEF";
16 /* the return value must be freed (using OPENSSL_free()) */
17 char *EC_POINT_point2hex(const EC_GROUP *group,
18 const EC_POINT *point,
19 point_conversion_form_t form, BN_CTX *ctx)
22 size_t buf_len = 0, i;
23 unsigned char *buf = NULL, *pbuf;
25 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
30 ret = OPENSSL_malloc(buf_len * 2 + 2);
37 for (i = buf_len; i > 0; i--) {
38 int v = (int)*(pbuf++);
39 *(p++) = HEX_DIGITS[v >> 4];
40 *(p++) = HEX_DIGITS[v & 0x0F];
49 EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
50 const char *hex, EC_POINT *point, BN_CTX *ctx)
53 unsigned char *oct_buf = NULL;
54 size_t len, oct_buf_len = 0;
57 if (group == NULL || hex == NULL)
61 pt = EC_POINT_new(group);
68 len = strlen(hex) / 2;
69 oct_buf = OPENSSL_malloc(len);
73 if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0')
74 || !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx))
78 OPENSSL_clear_free(oct_buf, oct_buf_len);
81 EC_POINT_clear_free(pt);