2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 <openssl/crypto.h>
13 BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
14 const EC_POINT *point,
15 point_conversion_form_t form,
16 BIGNUM *ret, BN_CTX *ctx)
21 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
26 ret = BN_bin2bn(buf, buf_len, ret);
33 EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
34 const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
40 if ((buf_len = BN_num_bytes(bn)) == 0)
42 buf = OPENSSL_malloc(buf_len);
46 if (!BN_bn2bin(bn, buf)) {
52 if ((ret = EC_POINT_new(group)) == NULL) {
59 if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
61 EC_POINT_clear_free(ret);
70 static const char *HEX_DIGITS = "0123456789ABCDEF";
72 /* the return value must be freed (using OPENSSL_free()) */
73 char *EC_POINT_point2hex(const EC_GROUP *group,
74 const EC_POINT *point,
75 point_conversion_form_t form, BN_CTX *ctx)
78 size_t buf_len = 0, i;
79 unsigned char *buf = NULL, *pbuf;
81 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
86 ret = OPENSSL_malloc(buf_len * 2 + 2);
93 for (i = buf_len; i > 0; i--) {
94 int v = (int)*(pbuf++);
95 *(p++) = HEX_DIGITS[v >> 4];
96 *(p++) = HEX_DIGITS[v & 0x0F];
105 EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
106 const char *buf, EC_POINT *point, BN_CTX *ctx)
108 EC_POINT *ret = NULL;
109 BIGNUM *tmp_bn = NULL;
111 if (!BN_hex2bn(&tmp_bn, buf))
114 ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
116 BN_clear_free(tmp_bn);