1 | /*
|
---|
2 | * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
|
---|
3 | *
|
---|
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
|
---|
8 | */
|
---|
9 |
|
---|
10 | #include <openssl/crypto.h>
|
---|
11 | #include "ec_lcl.h"
|
---|
12 |
|
---|
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)
|
---|
17 | {
|
---|
18 | size_t buf_len = 0;
|
---|
19 | unsigned char *buf;
|
---|
20 |
|
---|
21 | buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
|
---|
22 |
|
---|
23 | if (buf_len == 0)
|
---|
24 | return NULL;
|
---|
25 |
|
---|
26 | ret = BN_bin2bn(buf, buf_len, ret);
|
---|
27 |
|
---|
28 | OPENSSL_free(buf);
|
---|
29 |
|
---|
30 | return ret;
|
---|
31 | }
|
---|
32 |
|
---|
33 | EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
|
---|
34 | const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
|
---|
35 | {
|
---|
36 | size_t buf_len = 0;
|
---|
37 | unsigned char *buf;
|
---|
38 | EC_POINT *ret;
|
---|
39 |
|
---|
40 | if ((buf_len = BN_num_bytes(bn)) == 0)
|
---|
41 | return NULL;
|
---|
42 | buf = OPENSSL_malloc(buf_len);
|
---|
43 | if (buf == NULL)
|
---|
44 | return NULL;
|
---|
45 |
|
---|
46 | if (!BN_bn2bin(bn, buf)) {
|
---|
47 | OPENSSL_free(buf);
|
---|
48 | return NULL;
|
---|
49 | }
|
---|
50 |
|
---|
51 | if (point == NULL) {
|
---|
52 | if ((ret = EC_POINT_new(group)) == NULL) {
|
---|
53 | OPENSSL_free(buf);
|
---|
54 | return NULL;
|
---|
55 | }
|
---|
56 | } else
|
---|
57 | ret = point;
|
---|
58 |
|
---|
59 | if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
|
---|
60 | if (ret != point)
|
---|
61 | EC_POINT_clear_free(ret);
|
---|
62 | OPENSSL_free(buf);
|
---|
63 | return NULL;
|
---|
64 | }
|
---|
65 |
|
---|
66 | OPENSSL_free(buf);
|
---|
67 | return ret;
|
---|
68 | }
|
---|
69 |
|
---|
70 | static const char *HEX_DIGITS = "0123456789ABCDEF";
|
---|
71 |
|
---|
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)
|
---|
76 | {
|
---|
77 | char *ret, *p;
|
---|
78 | size_t buf_len = 0, i;
|
---|
79 | unsigned char *buf = NULL, *pbuf;
|
---|
80 |
|
---|
81 | buf_len = EC_POINT_point2buf(group, point, form, &buf, ctx);
|
---|
82 |
|
---|
83 | if (buf_len == 0)
|
---|
84 | return NULL;
|
---|
85 |
|
---|
86 | ret = OPENSSL_malloc(buf_len * 2 + 2);
|
---|
87 | if (ret == NULL) {
|
---|
88 | OPENSSL_free(buf);
|
---|
89 | return NULL;
|
---|
90 | }
|
---|
91 | p = ret;
|
---|
92 | pbuf = buf;
|
---|
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];
|
---|
97 | }
|
---|
98 | *p = '\0';
|
---|
99 |
|
---|
100 | OPENSSL_free(buf);
|
---|
101 |
|
---|
102 | return ret;
|
---|
103 | }
|
---|
104 |
|
---|
105 | EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
|
---|
106 | const char *buf, EC_POINT *point, BN_CTX *ctx)
|
---|
107 | {
|
---|
108 | EC_POINT *ret = NULL;
|
---|
109 | BIGNUM *tmp_bn = NULL;
|
---|
110 |
|
---|
111 | if (!BN_hex2bn(&tmp_bn, buf))
|
---|
112 | return NULL;
|
---|
113 |
|
---|
114 | ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
|
---|
115 |
|
---|
116 | BN_clear_free(tmp_bn);
|
---|
117 |
|
---|
118 | return ret;
|
---|
119 | }
|
---|