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