ec_print.c revision 160814
152419Sjulian/* crypto/ec/ec_print.c */
252419Sjulian/* ====================================================================
352419Sjulian * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
452419Sjulian *
552419Sjulian * Redistribution and use in source and binary forms, with or without
652419Sjulian * modification, are permitted provided that the following conditions
752419Sjulian * are met:
852419Sjulian *
952419Sjulian * 1. Redistributions of source code must retain the above copyright
1052419Sjulian *    notice, this list of conditions and the following disclaimer.
1152419Sjulian *
1252419Sjulian * 2. Redistributions in binary form must reproduce the above copyright
1352419Sjulian *    notice, this list of conditions and the following disclaimer in
1452419Sjulian *    the documentation and/or other materials provided with the
1552419Sjulian *    distribution.
1652419Sjulian *
1752419Sjulian * 3. All advertising materials mentioning features or use of this
1852419Sjulian *    software must display the following acknowledgment:
1952419Sjulian *    "This product includes software developed by the OpenSSL Project
2052419Sjulian *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
2152419Sjulian *
2252419Sjulian * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2352419Sjulian *    endorse or promote products derived from this software without
2452419Sjulian *    prior written permission. For written permission, please contact
2552419Sjulian *    openssl-core@openssl.org.
2652419Sjulian *
2752419Sjulian * 5. Products derived from this software may not be called "OpenSSL"
2852419Sjulian *    nor may "OpenSSL" appear in their names without prior written
2952419Sjulian *    permission of the OpenSSL Project.
3052419Sjulian *
3152419Sjulian * 6. Redistributions of any form whatsoever must retain the following
3252419Sjulian *    acknowledgment:
3352419Sjulian *    "This product includes software developed by the OpenSSL Project
3452419Sjulian *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
3552419Sjulian *
3652419Sjulian * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
3752419Sjulian * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3852419Sjulian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
3952419Sjulian * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40158882Sglebius * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41158882Sglebius * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42158882Sglebius * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43158882Sglebius * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4452419Sjulian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4552419Sjulian * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4652419Sjulian * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
4752419Sjulian * OF THE POSSIBILITY OF SUCH DAMAGE.
4852419Sjulian * ====================================================================
4952419Sjulian *
5052419Sjulian * This product includes cryptographic software written by Eric Young
5152419Sjulian * (eay@cryptsoft.com).  This product includes software written by Tim
5253913Sarchie * Hudson (tjh@cryptsoft.com).
5353913Sarchie *
5452419Sjulian */
5552419Sjulian
5652419Sjulian#include <openssl/crypto.h>
5752419Sjulian#include "ec_lcl.h"
5852419Sjulian
5952419SjulianBIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
6052419Sjulian                          const EC_POINT *point,
6152419Sjulian                          point_conversion_form_t form,
6252419Sjulian                          BIGNUM *ret,
6352419Sjulian                          BN_CTX *ctx)
6452419Sjulian	{
6552419Sjulian	size_t        buf_len=0;
6652419Sjulian	unsigned char *buf;
67160002Sglebius
6852419Sjulian	buf_len = EC_POINT_point2oct(group, point, form,
6952419Sjulian                                     NULL, 0, ctx);
7052419Sjulian	if (buf_len == 0)
7152419Sjulian		return NULL;
7252419Sjulian
7352419Sjulian	if ((buf = OPENSSL_malloc(buf_len)) == NULL)
74160002Sglebius		return NULL;
7552419Sjulian
76160002Sglebius	if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx))
7752419Sjulian		{
7852419Sjulian		OPENSSL_free(buf);
7952419Sjulian		return NULL;
80		}
81
82	ret = BN_bin2bn(buf, buf_len, ret);
83
84	OPENSSL_free(buf);
85
86	return ret;
87}
88
89EC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
90                            const BIGNUM *bn,
91                            EC_POINT *point,
92                            BN_CTX *ctx)
93	{
94	size_t        buf_len=0;
95	unsigned char *buf;
96	EC_POINT      *ret;
97
98	if ((buf_len = BN_num_bytes(bn)) == 0) return NULL;
99	buf = OPENSSL_malloc(buf_len);
100	if (buf == NULL)
101		return NULL;
102
103	if (!BN_bn2bin(bn, buf))
104		{
105		OPENSSL_free(buf);
106		return NULL;
107		}
108
109	if (point == NULL)
110		{
111		if ((ret = EC_POINT_new(group)) == NULL)
112			{
113			OPENSSL_free(buf);
114			return NULL;
115			}
116		}
117	else
118		ret = point;
119
120	if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx))
121		{
122		if (point == NULL)
123			EC_POINT_clear_free(ret);
124		OPENSSL_free(buf);
125		return NULL;
126		}
127
128	OPENSSL_free(buf);
129	return ret;
130	}
131
132static const char *HEX_DIGITS = "0123456789ABCDEF";
133
134/* the return value must be freed (using OPENSSL_free()) */
135char *EC_POINT_point2hex(const EC_GROUP *group,
136                         const EC_POINT *point,
137                         point_conversion_form_t form,
138                         BN_CTX *ctx)
139	{
140	char          *ret, *p;
141	size_t        buf_len=0,i;
142	unsigned char *buf, *pbuf;
143
144	buf_len = EC_POINT_point2oct(group, point, form,
145                                     NULL, 0, ctx);
146	if (buf_len == 0)
147		return NULL;
148
149	if ((buf = OPENSSL_malloc(buf_len)) == NULL)
150		return NULL;
151
152	if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx))
153		{
154		OPENSSL_free(buf);
155		return NULL;
156		}
157
158	ret = (char *)OPENSSL_malloc(buf_len*2+2);
159	if (ret == NULL)
160		{
161		OPENSSL_free(buf);
162		return NULL;
163		}
164	p = ret;
165	pbuf = buf;
166	for (i=buf_len; i > 0; i--)
167		{
168			int v = (int) *(pbuf++);
169			*(p++)=HEX_DIGITS[v>>4];
170			*(p++)=HEX_DIGITS[v&0x0F];
171		}
172	*p='\0';
173
174	OPENSSL_free(buf);
175
176	return ret;
177	}
178
179EC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
180                             const char *buf,
181                             EC_POINT *point,
182                             BN_CTX *ctx)
183	{
184	EC_POINT *ret=NULL;
185	BIGNUM   *tmp_bn=NULL;
186
187	if (!BN_hex2bn(&tmp_bn, buf))
188		return NULL;
189
190	ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
191
192	BN_clear_free(tmp_bn);
193
194	return ret;
195	}
196