1*2139Sjp161948 /* crypto/ec/ec_print.c */
2*2139Sjp161948 /* ====================================================================
3*2139Sjp161948 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
4*2139Sjp161948 *
5*2139Sjp161948 * Redistribution and use in source and binary forms, with or without
6*2139Sjp161948 * modification, are permitted provided that the following conditions
7*2139Sjp161948 * are met:
8*2139Sjp161948 *
9*2139Sjp161948 * 1. Redistributions of source code must retain the above copyright
10*2139Sjp161948 * notice, this list of conditions and the following disclaimer.
11*2139Sjp161948 *
12*2139Sjp161948 * 2. Redistributions in binary form must reproduce the above copyright
13*2139Sjp161948 * notice, this list of conditions and the following disclaimer in
14*2139Sjp161948 * the documentation and/or other materials provided with the
15*2139Sjp161948 * distribution.
16*2139Sjp161948 *
17*2139Sjp161948 * 3. All advertising materials mentioning features or use of this
18*2139Sjp161948 * software must display the following acknowledgment:
19*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
20*2139Sjp161948 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21*2139Sjp161948 *
22*2139Sjp161948 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23*2139Sjp161948 * endorse or promote products derived from this software without
24*2139Sjp161948 * prior written permission. For written permission, please contact
25*2139Sjp161948 * openssl-core@openssl.org.
26*2139Sjp161948 *
27*2139Sjp161948 * 5. Products derived from this software may not be called "OpenSSL"
28*2139Sjp161948 * nor may "OpenSSL" appear in their names without prior written
29*2139Sjp161948 * permission of the OpenSSL Project.
30*2139Sjp161948 *
31*2139Sjp161948 * 6. Redistributions of any form whatsoever must retain the following
32*2139Sjp161948 * acknowledgment:
33*2139Sjp161948 * "This product includes software developed by the OpenSSL Project
34*2139Sjp161948 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35*2139Sjp161948 *
36*2139Sjp161948 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37*2139Sjp161948 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38*2139Sjp161948 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39*2139Sjp161948 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40*2139Sjp161948 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41*2139Sjp161948 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42*2139Sjp161948 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43*2139Sjp161948 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44*2139Sjp161948 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45*2139Sjp161948 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46*2139Sjp161948 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47*2139Sjp161948 * OF THE POSSIBILITY OF SUCH DAMAGE.
48*2139Sjp161948 * ====================================================================
49*2139Sjp161948 *
50*2139Sjp161948 * This product includes cryptographic software written by Eric Young
51*2139Sjp161948 * (eay@cryptsoft.com). This product includes software written by Tim
52*2139Sjp161948 * Hudson (tjh@cryptsoft.com).
53*2139Sjp161948 *
54*2139Sjp161948 */
55*2139Sjp161948
56*2139Sjp161948 #include <openssl/crypto.h>
57*2139Sjp161948 #include "ec_lcl.h"
58*2139Sjp161948
EC_POINT_point2bn(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,BIGNUM * ret,BN_CTX * ctx)59*2139Sjp161948 BIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
60*2139Sjp161948 const EC_POINT *point,
61*2139Sjp161948 point_conversion_form_t form,
62*2139Sjp161948 BIGNUM *ret,
63*2139Sjp161948 BN_CTX *ctx)
64*2139Sjp161948 {
65*2139Sjp161948 size_t buf_len=0;
66*2139Sjp161948 unsigned char *buf;
67*2139Sjp161948
68*2139Sjp161948 buf_len = EC_POINT_point2oct(group, point, form,
69*2139Sjp161948 NULL, 0, ctx);
70*2139Sjp161948 if (buf_len == 0)
71*2139Sjp161948 return NULL;
72*2139Sjp161948
73*2139Sjp161948 if ((buf = OPENSSL_malloc(buf_len)) == NULL)
74*2139Sjp161948 return NULL;
75*2139Sjp161948
76*2139Sjp161948 if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx))
77*2139Sjp161948 {
78*2139Sjp161948 OPENSSL_free(buf);
79*2139Sjp161948 return NULL;
80*2139Sjp161948 }
81*2139Sjp161948
82*2139Sjp161948 ret = BN_bin2bn(buf, buf_len, ret);
83*2139Sjp161948
84*2139Sjp161948 OPENSSL_free(buf);
85*2139Sjp161948
86*2139Sjp161948 return ret;
87*2139Sjp161948 }
88*2139Sjp161948
EC_POINT_bn2point(const EC_GROUP * group,const BIGNUM * bn,EC_POINT * point,BN_CTX * ctx)89*2139Sjp161948 EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
90*2139Sjp161948 const BIGNUM *bn,
91*2139Sjp161948 EC_POINT *point,
92*2139Sjp161948 BN_CTX *ctx)
93*2139Sjp161948 {
94*2139Sjp161948 size_t buf_len=0;
95*2139Sjp161948 unsigned char *buf;
96*2139Sjp161948 EC_POINT *ret;
97*2139Sjp161948
98*2139Sjp161948 if ((buf_len = BN_num_bytes(bn)) == 0) return NULL;
99*2139Sjp161948 buf = OPENSSL_malloc(buf_len);
100*2139Sjp161948 if (buf == NULL)
101*2139Sjp161948 return NULL;
102*2139Sjp161948
103*2139Sjp161948 if (!BN_bn2bin(bn, buf))
104*2139Sjp161948 {
105*2139Sjp161948 OPENSSL_free(buf);
106*2139Sjp161948 return NULL;
107*2139Sjp161948 }
108*2139Sjp161948
109*2139Sjp161948 if (point == NULL)
110*2139Sjp161948 {
111*2139Sjp161948 if ((ret = EC_POINT_new(group)) == NULL)
112*2139Sjp161948 {
113*2139Sjp161948 OPENSSL_free(buf);
114*2139Sjp161948 return NULL;
115*2139Sjp161948 }
116*2139Sjp161948 }
117*2139Sjp161948 else
118*2139Sjp161948 ret = point;
119*2139Sjp161948
120*2139Sjp161948 if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx))
121*2139Sjp161948 {
122*2139Sjp161948 if (point == NULL)
123*2139Sjp161948 EC_POINT_clear_free(ret);
124*2139Sjp161948 OPENSSL_free(buf);
125*2139Sjp161948 return NULL;
126*2139Sjp161948 }
127*2139Sjp161948
128*2139Sjp161948 OPENSSL_free(buf);
129*2139Sjp161948 return ret;
130*2139Sjp161948 }
131*2139Sjp161948
132*2139Sjp161948 static const char *HEX_DIGITS = "0123456789ABCDEF";
133*2139Sjp161948
134*2139Sjp161948 /* the return value must be freed (using OPENSSL_free()) */
EC_POINT_point2hex(const EC_GROUP * group,const EC_POINT * point,point_conversion_form_t form,BN_CTX * ctx)135*2139Sjp161948 char *EC_POINT_point2hex(const EC_GROUP *group,
136*2139Sjp161948 const EC_POINT *point,
137*2139Sjp161948 point_conversion_form_t form,
138*2139Sjp161948 BN_CTX *ctx)
139*2139Sjp161948 {
140*2139Sjp161948 char *ret, *p;
141*2139Sjp161948 size_t buf_len=0,i;
142*2139Sjp161948 unsigned char *buf, *pbuf;
143*2139Sjp161948
144*2139Sjp161948 buf_len = EC_POINT_point2oct(group, point, form,
145*2139Sjp161948 NULL, 0, ctx);
146*2139Sjp161948 if (buf_len == 0)
147*2139Sjp161948 return NULL;
148*2139Sjp161948
149*2139Sjp161948 if ((buf = OPENSSL_malloc(buf_len)) == NULL)
150*2139Sjp161948 return NULL;
151*2139Sjp161948
152*2139Sjp161948 if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx))
153*2139Sjp161948 {
154*2139Sjp161948 OPENSSL_free(buf);
155*2139Sjp161948 return NULL;
156*2139Sjp161948 }
157*2139Sjp161948
158*2139Sjp161948 ret = (char *)OPENSSL_malloc(buf_len*2+2);
159*2139Sjp161948 if (ret == NULL)
160*2139Sjp161948 {
161*2139Sjp161948 OPENSSL_free(buf);
162*2139Sjp161948 return NULL;
163*2139Sjp161948 }
164*2139Sjp161948 p = ret;
165*2139Sjp161948 pbuf = buf;
166*2139Sjp161948 for (i=buf_len; i > 0; i--)
167*2139Sjp161948 {
168*2139Sjp161948 int v = (int) *(pbuf++);
169*2139Sjp161948 *(p++)=HEX_DIGITS[v>>4];
170*2139Sjp161948 *(p++)=HEX_DIGITS[v&0x0F];
171*2139Sjp161948 }
172*2139Sjp161948 *p='\0';
173*2139Sjp161948
174*2139Sjp161948 OPENSSL_free(buf);
175*2139Sjp161948
176*2139Sjp161948 return ret;
177*2139Sjp161948 }
178*2139Sjp161948
EC_POINT_hex2point(const EC_GROUP * group,const char * buf,EC_POINT * point,BN_CTX * ctx)179*2139Sjp161948 EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
180*2139Sjp161948 const char *buf,
181*2139Sjp161948 EC_POINT *point,
182*2139Sjp161948 BN_CTX *ctx)
183*2139Sjp161948 {
184*2139Sjp161948 EC_POINT *ret=NULL;
185*2139Sjp161948 BIGNUM *tmp_bn=NULL;
186*2139Sjp161948
187*2139Sjp161948 if (!BN_hex2bn(&tmp_bn, buf))
188*2139Sjp161948 return NULL;
189*2139Sjp161948
190*2139Sjp161948 ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
191*2139Sjp161948
192*2139Sjp161948 BN_clear_free(tmp_bn);
193*2139Sjp161948
194*2139Sjp161948 return ret;
195*2139Sjp161948 }
196