2016-05-18 02:51:04 +08:00
|
|
|
/*
|
2020-04-23 20:55:52 +08:00
|
|
|
* Copyright 2002-2020 The OpenSSL Project Authors. All Rights Reserved.
|
2002-05-30 21:16:03 +08:00
|
|
|
*
|
2018-12-06 20:38:06 +08:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-18 02:51:04 +08:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2002-05-30 21:16:03 +08:00
|
|
|
*/
|
|
|
|
|
2020-11-02 10:46:38 +08:00
|
|
|
#include <string.h> /* strlen */
|
2002-05-30 21:16:03 +08:00
|
|
|
#include <openssl/crypto.h>
|
2019-09-28 06:45:40 +08:00
|
|
|
#include "ec_local.h"
|
2002-05-30 21:16:03 +08:00
|
|
|
|
|
|
|
static const char *HEX_DIGITS = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
/* the return value must be freed (using OPENSSL_free()) */
|
|
|
|
char *EC_POINT_point2hex(const EC_GROUP *group,
|
|
|
|
const EC_POINT *point,
|
2015-01-22 11:40:55 +08:00
|
|
|
point_conversion_form_t form, BN_CTX *ctx)
|
|
|
|
{
|
|
|
|
char *ret, *p;
|
|
|
|
size_t buf_len = 0, i;
|
2015-12-12 09:04:25 +08:00
|
|
|
unsigned char *buf = NULL, *pbuf;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-12-12 09:04:25 +08:00
|
|
|
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2015-12-12 09:04:25 +08:00
|
|
|
if (buf_len == 0)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
|
|
|
|
2015-04-29 03:28:14 +08:00
|
|
|
ret = OPENSSL_malloc(buf_len * 2 + 2);
|
2015-01-22 11:40:55 +08:00
|
|
|
if (ret == NULL) {
|
|
|
|
OPENSSL_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';
|
|
|
|
|
|
|
|
OPENSSL_free(buf);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2002-05-30 21:16:03 +08:00
|
|
|
|
|
|
|
EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
|
2020-11-02 10:46:38 +08:00
|
|
|
const char *hex, EC_POINT *point, BN_CTX *ctx)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2020-11-02 10:46:38 +08:00
|
|
|
int ok = 0;
|
|
|
|
unsigned char *oct_buf = NULL;
|
|
|
|
size_t len, oct_buf_len = 0;
|
|
|
|
EC_POINT *pt = NULL;
|
2002-05-30 21:16:03 +08:00
|
|
|
|
2020-11-02 10:46:38 +08:00
|
|
|
if (group == NULL || hex == NULL)
|
2015-01-22 11:40:55 +08:00
|
|
|
return NULL;
|
2002-05-30 21:16:03 +08:00
|
|
|
|
2020-11-02 10:46:38 +08:00
|
|
|
if (point == NULL) {
|
|
|
|
pt = EC_POINT_new(group);
|
|
|
|
if (pt == NULL)
|
|
|
|
goto err;
|
|
|
|
} else {
|
|
|
|
pt = point;
|
|
|
|
}
|
2002-05-30 21:16:03 +08:00
|
|
|
|
2020-11-02 10:46:38 +08:00
|
|
|
len = strlen(hex) / 2;
|
|
|
|
oct_buf = OPENSSL_malloc(len);
|
|
|
|
if (oct_buf == NULL)
|
2021-12-15 14:53:53 +08:00
|
|
|
goto err;
|
2002-05-30 21:16:03 +08:00
|
|
|
|
2020-11-02 10:46:38 +08:00
|
|
|
if (!OPENSSL_hexstr2buf_ex(oct_buf, len, &oct_buf_len, hex, '\0')
|
|
|
|
|| !EC_POINT_oct2point(group, pt, oct_buf, oct_buf_len, ctx))
|
|
|
|
goto err;
|
|
|
|
ok = 1;
|
|
|
|
err:
|
|
|
|
OPENSSL_clear_free(oct_buf, oct_buf_len);
|
|
|
|
if (!ok) {
|
|
|
|
if (pt != point)
|
|
|
|
EC_POINT_clear_free(pt);
|
|
|
|
pt = NULL;
|
|
|
|
}
|
|
|
|
return pt;
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|