|
Packit Service |
084de1 |
/*
|
|
Packit Service |
084de1 |
* Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
Packit Service |
084de1 |
*
|
|
Packit Service |
084de1 |
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
Packit Service |
084de1 |
* this file except in compliance with the License. You can obtain a copy
|
|
Packit Service |
084de1 |
* in the file LICENSE in the source distribution or at
|
|
Packit Service |
084de1 |
* https://www.openssl.org/source/license.html
|
|
Packit Service |
084de1 |
*/
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
#include <openssl/crypto.h>
|
|
Packit Service |
084de1 |
#include <openssl/err.h>
|
|
Packit Service |
084de1 |
#include "ec_local.h"
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
|
|
Packit Service |
084de1 |
const EC_POINT *point,
|
|
Packit Service |
084de1 |
point_conversion_form_t form,
|
|
Packit Service |
084de1 |
BIGNUM *ret, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
size_t buf_len = 0;
|
|
Packit Service |
084de1 |
unsigned char *buf;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (buf_len == 0)
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = BN_bin2bn(buf, buf_len, ret);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
|
|
Packit Service |
084de1 |
const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
size_t buf_len = 0;
|
|
Packit Service |
084de1 |
unsigned char *buf;
|
|
Packit Service |
084de1 |
EC_POINT *ret;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if ((buf_len = BN_num_bytes(bn)) == 0)
|
|
Packit Service |
084de1 |
buf_len = 1;
|
|
Packit Service |
084de1 |
if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
|
|
Packit Service |
084de1 |
ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_bn2binpad(bn, buf, buf_len)) {
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (point == NULL) {
|
|
Packit Service |
084de1 |
if ((ret = EC_POINT_new(group)) == NULL) {
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
} else
|
|
Packit Service |
084de1 |
ret = point;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
|
|
Packit Service |
084de1 |
if (ret != point)
|
|
Packit Service |
084de1 |
EC_POINT_clear_free(ret);
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
static const char *HEX_DIGITS = "0123456789ABCDEF";
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
/* the return value must be freed (using OPENSSL_free()) */
|
|
Packit Service |
084de1 |
char *EC_POINT_point2hex(const EC_GROUP *group,
|
|
Packit Service |
084de1 |
const EC_POINT *point,
|
|
Packit Service |
084de1 |
point_conversion_form_t form, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
char *ret, *p;
|
|
Packit Service |
084de1 |
size_t buf_len = 0, i;
|
|
Packit Service |
084de1 |
unsigned char *buf = NULL, *pbuf;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (buf_len == 0)
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = OPENSSL_malloc(buf_len * 2 + 2);
|
|
Packit Service |
084de1 |
if (ret == NULL) {
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
p = ret;
|
|
Packit Service |
084de1 |
pbuf = buf;
|
|
Packit Service |
084de1 |
for (i = buf_len; i > 0; i--) {
|
|
Packit Service |
084de1 |
int v = (int)*(pbuf++);
|
|
Packit Service |
084de1 |
*(p++) = HEX_DIGITS[v >> 4];
|
|
Packit Service |
084de1 |
*(p++) = HEX_DIGITS[v & 0x0F];
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
*p = '\0';
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
OPENSSL_free(buf);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
|
|
Packit Service |
084de1 |
const char *buf, EC_POINT *point, BN_CTX *ctx)
|
|
Packit Service |
084de1 |
{
|
|
Packit Service |
084de1 |
EC_POINT *ret = NULL;
|
|
Packit Service |
084de1 |
BIGNUM *tmp_bn = NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
if (!BN_hex2bn(&tmp_bn, buf))
|
|
Packit Service |
084de1 |
return NULL;
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
BN_clear_free(tmp_bn);
|
|
Packit Service |
084de1 |
|
|
Packit Service |
084de1 |
return ret;
|
|
Packit Service |
084de1 |
}
|