| 1 |
/* |
| 2 |
* Copyright 2002-2024 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 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 |
| 8 |
*/ |
| 9 |
|
| 10 |
#include <string.h> /* strlen */ |
| 11 |
#include <openssl/crypto.h> |
| 12 |
#include "internal/cryptlib.h" |
| 13 |
#include "ec_local.h" |
| 14 |
|
| 15 |
/* the return value must be freed (using OPENSSL_free()) */ |
| 16 |
char *EC_POINT_point2hex(const EC_GROUP *group, |
| 17 |
const EC_POINT *point, |
| 18 |
point_conversion_form_t form, BN_CTX *ctx) |
| 19 |
{ |
| 20 |
char *ret, *p; |
| 21 |
size_t buf_len, i; |
| 22 |
unsigned char *buf = NULL; |
| 23 |
|
| 24 |
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx); |
| 25 |
|
| 26 |
if (buf_len == 0) |
| 27 |
return NULL; |
| 28 |
|
| 29 |
ret = OPENSSL_malloc(buf_len * 2 + 2); |
| 30 |
if (ret == NULL) |
| 31 |
goto err; |
| 32 |
|
| 33 |
p = ret; |
| 34 |
for (i = 0; i < buf_len; ++i) |
| 35 |
p += ossl_to_hex(p, buf[i]); |
| 36 |
*p = '\0'; |
| 37 |
|
| 38 |
err: |
| 39 |
OPENSSL_free(buf); |
| 40 |
return ret; |
| 41 |
} |
| 42 |
|
| 43 |
EC_POINT *EC_POINT_hex2point(const EC_GROUP *group, |
| 44 |
const char *hex, EC_POINT *point, BN_CTX *ctx) |
| 45 |
{ |
| 46 |
int ok = 0; |
| 47 |
unsigned char *oct_buf = NULL; |
| 48 |
size_t len, oct_buf_len = 0; |
| 49 |
EC_POINT *pt = NULL; |
| 50 |
|
| 51 |
if (group == NULL || hex == NULL) |
| 52 |
return NULL; |
| 53 |
|
| 54 |
if (point == NULL) { |
| 55 |
pt = EC_POINT_new(group); |
| 56 |
if (pt == NULL) |
| 57 |
goto err; |
| 58 |
} else { |
| 59 |
pt = point; |
| 60 |
} |
| 61 |
|
| 62 |
len = strlen(hex) / 2; |
| 63 |
oct_buf = OPENSSL_malloc(len); |
| 64 |
if (oct_buf == NULL) |
| 65 |
goto err; |
| 66 |
|
| 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; |
| 71 |
err: |
| 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; |
| 79 |
} |