]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ec_print.c
Do not silently truncate files on perlasm errors
[thirdparty/openssl.git] / crypto / ec / ec_print.c
CommitLineData
4f22f405 1/*
28428130 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
6cbe6382 3 *
4f22f405
RS
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
6cbe6382
BM
8 */
9
10#include <openssl/crypto.h>
cdb10bae 11#include <openssl/err.h>
b5acbf91 12#include "ec_local.h"
6cbe6382 13
0f113f3e
MC
14BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
15 const EC_POINT *point,
6cbe6382 16 point_conversion_form_t form,
0f113f3e
MC
17 BIGNUM *ret, BN_CTX *ctx)
18{
19 size_t buf_len = 0;
20 unsigned char *buf;
6cbe6382 21
981bd8a2 22 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
6cbe6382 23
981bd8a2 24 if (buf_len == 0)
0f113f3e 25 return NULL;
6cbe6382 26
0f113f3e 27 ret = BN_bin2bn(buf, buf_len, ret);
6cbe6382 28
0f113f3e 29 OPENSSL_free(buf);
6cbe6382 30
0f113f3e 31 return ret;
6cbe6382
BM
32}
33
34EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
0f113f3e
MC
35 const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
36{
37 size_t buf_len = 0;
38 unsigned char *buf;
39 EC_POINT *ret;
40
41 if ((buf_len = BN_num_bytes(bn)) == 0)
6f6adf1d 42 buf_len = 1;
cdb10bae
RS
43 if ((buf = OPENSSL_malloc(buf_len)) == NULL) {
44 ECerr(EC_F_EC_POINT_BN2POINT, ERR_R_MALLOC_FAILURE);
0f113f3e 45 return NULL;
cdb10bae 46 }
0f113f3e 47
6f6adf1d 48 if (!BN_bn2binpad(bn, buf, buf_len)) {
0f113f3e
MC
49 OPENSSL_free(buf);
50 return NULL;
51 }
52
53 if (point == NULL) {
54 if ((ret = EC_POINT_new(group)) == NULL) {
55 OPENSSL_free(buf);
56 return NULL;
57 }
58 } else
59 ret = point;
60
61 if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
8fdc3734 62 if (ret != point)
0f113f3e
MC
63 EC_POINT_clear_free(ret);
64 OPENSSL_free(buf);
65 return NULL;
66 }
67
68 OPENSSL_free(buf);
69 return ret;
70}
6cbe6382
BM
71
72static const char *HEX_DIGITS = "0123456789ABCDEF";
73
74/* the return value must be freed (using OPENSSL_free()) */
75char *EC_POINT_point2hex(const EC_GROUP *group,
76 const EC_POINT *point,
0f113f3e
MC
77 point_conversion_form_t form, BN_CTX *ctx)
78{
79 char *ret, *p;
80 size_t buf_len = 0, i;
981bd8a2 81 unsigned char *buf = NULL, *pbuf;
0f113f3e 82
981bd8a2 83 buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
0f113f3e 84
981bd8a2 85 if (buf_len == 0)
0f113f3e 86 return NULL;
0f113f3e 87
b196e7d9 88 ret = OPENSSL_malloc(buf_len * 2 + 2);
0f113f3e
MC
89 if (ret == NULL) {
90 OPENSSL_free(buf);
91 return NULL;
92 }
93 p = ret;
94 pbuf = buf;
95 for (i = buf_len; i > 0; i--) {
96 int v = (int)*(pbuf++);
97 *(p++) = HEX_DIGITS[v >> 4];
98 *(p++) = HEX_DIGITS[v & 0x0F];
99 }
100 *p = '\0';
101
102 OPENSSL_free(buf);
103
104 return ret;
105}
6cbe6382
BM
106
107EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
0f113f3e
MC
108 const char *buf, EC_POINT *point, BN_CTX *ctx)
109{
110 EC_POINT *ret = NULL;
111 BIGNUM *tmp_bn = NULL;
6cbe6382 112
0f113f3e
MC
113 if (!BN_hex2bn(&tmp_bn, buf))
114 return NULL;
6cbe6382 115
0f113f3e 116 ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
6cbe6382 117
0f113f3e 118 BN_clear_free(tmp_bn);
6cbe6382 119
0f113f3e
MC
120 return ret;
121}