1160814Ssimon/* crypto/ec/ec_print.c */
2160814Ssimon/* ====================================================================
3160814Ssimon * Copyright (c) 1998-2002 The OpenSSL Project.  All rights reserved.
4160814Ssimon *
5160814Ssimon * Redistribution and use in source and binary forms, with or without
6160814Ssimon * modification, are permitted provided that the following conditions
7160814Ssimon * are met:
8160814Ssimon *
9160814Ssimon * 1. Redistributions of source code must retain the above copyright
10296341Sdelphij *    notice, this list of conditions and the following disclaimer.
11160814Ssimon *
12160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
13160814Ssimon *    notice, this list of conditions and the following disclaimer in
14160814Ssimon *    the documentation and/or other materials provided with the
15160814Ssimon *    distribution.
16160814Ssimon *
17160814Ssimon * 3. All advertising materials mentioning features or use of this
18160814Ssimon *    software must display the following acknowledgment:
19160814Ssimon *    "This product includes software developed by the OpenSSL Project
20160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21160814Ssimon *
22160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23160814Ssimon *    endorse or promote products derived from this software without
24160814Ssimon *    prior written permission. For written permission, please contact
25160814Ssimon *    openssl-core@openssl.org.
26160814Ssimon *
27160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
28160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
29160814Ssimon *    permission of the OpenSSL Project.
30160814Ssimon *
31160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
32160814Ssimon *    acknowledgment:
33160814Ssimon *    "This product includes software developed by the OpenSSL Project
34160814Ssimon *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35160814Ssimon *
36160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
48160814Ssimon * ====================================================================
49160814Ssimon *
50160814Ssimon * This product includes cryptographic software written by Eric Young
51160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
52160814Ssimon * Hudson (tjh@cryptsoft.com).
53160814Ssimon *
54160814Ssimon */
55160814Ssimon
56160814Ssimon#include <openssl/crypto.h>
57160814Ssimon#include "ec_lcl.h"
58160814Ssimon
59296341SdelphijBIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
60296341Sdelphij                          const EC_POINT *point,
61160814Ssimon                          point_conversion_form_t form,
62296341Sdelphij                          BIGNUM *ret, BN_CTX *ctx)
63296341Sdelphij{
64296341Sdelphij    size_t buf_len = 0;
65296341Sdelphij    unsigned char *buf;
66160814Ssimon
67296341Sdelphij    buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
68296341Sdelphij    if (buf_len == 0)
69296341Sdelphij        return NULL;
70160814Ssimon
71296341Sdelphij    if ((buf = OPENSSL_malloc(buf_len)) == NULL)
72296341Sdelphij        return NULL;
73160814Ssimon
74296341Sdelphij    if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) {
75296341Sdelphij        OPENSSL_free(buf);
76296341Sdelphij        return NULL;
77296341Sdelphij    }
78160814Ssimon
79296341Sdelphij    ret = BN_bin2bn(buf, buf_len, ret);
80160814Ssimon
81296341Sdelphij    OPENSSL_free(buf);
82160814Ssimon
83296341Sdelphij    return ret;
84160814Ssimon}
85160814Ssimon
86160814SsimonEC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
87296341Sdelphij                            const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
88296341Sdelphij{
89296341Sdelphij    size_t buf_len = 0;
90296341Sdelphij    unsigned char *buf;
91296341Sdelphij    EC_POINT *ret;
92160814Ssimon
93296341Sdelphij    if ((buf_len = BN_num_bytes(bn)) == 0)
94296341Sdelphij        return NULL;
95296341Sdelphij    buf = OPENSSL_malloc(buf_len);
96296341Sdelphij    if (buf == NULL)
97296341Sdelphij        return NULL;
98160814Ssimon
99296341Sdelphij    if (!BN_bn2bin(bn, buf)) {
100296341Sdelphij        OPENSSL_free(buf);
101296341Sdelphij        return NULL;
102296341Sdelphij    }
103160814Ssimon
104296341Sdelphij    if (point == NULL) {
105296341Sdelphij        if ((ret = EC_POINT_new(group)) == NULL) {
106296341Sdelphij            OPENSSL_free(buf);
107296341Sdelphij            return NULL;
108296341Sdelphij        }
109296341Sdelphij    } else
110296341Sdelphij        ret = point;
111160814Ssimon
112296341Sdelphij    if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
113296341Sdelphij        if (point == NULL)
114296341Sdelphij            EC_POINT_clear_free(ret);
115296341Sdelphij        OPENSSL_free(buf);
116296341Sdelphij        return NULL;
117296341Sdelphij    }
118160814Ssimon
119296341Sdelphij    OPENSSL_free(buf);
120296341Sdelphij    return ret;
121296341Sdelphij}
122160814Ssimon
123160814Ssimonstatic const char *HEX_DIGITS = "0123456789ABCDEF";
124160814Ssimon
125160814Ssimon/* the return value must be freed (using OPENSSL_free()) */
126160814Ssimonchar *EC_POINT_point2hex(const EC_GROUP *group,
127160814Ssimon                         const EC_POINT *point,
128296341Sdelphij                         point_conversion_form_t form, BN_CTX *ctx)
129296341Sdelphij{
130296341Sdelphij    char *ret, *p;
131296341Sdelphij    size_t buf_len = 0, i;
132296341Sdelphij    unsigned char *buf, *pbuf;
133160814Ssimon
134296341Sdelphij    buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
135296341Sdelphij    if (buf_len == 0)
136296341Sdelphij        return NULL;
137160814Ssimon
138296341Sdelphij    if ((buf = OPENSSL_malloc(buf_len)) == NULL)
139296341Sdelphij        return NULL;
140160814Ssimon
141296341Sdelphij    if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) {
142296341Sdelphij        OPENSSL_free(buf);
143296341Sdelphij        return NULL;
144296341Sdelphij    }
145160814Ssimon
146296341Sdelphij    ret = (char *)OPENSSL_malloc(buf_len * 2 + 2);
147296341Sdelphij    if (ret == NULL) {
148296341Sdelphij        OPENSSL_free(buf);
149296341Sdelphij        return NULL;
150296341Sdelphij    }
151296341Sdelphij    p = ret;
152296341Sdelphij    pbuf = buf;
153296341Sdelphij    for (i = buf_len; i > 0; i--) {
154296341Sdelphij        int v = (int)*(pbuf++);
155296341Sdelphij        *(p++) = HEX_DIGITS[v >> 4];
156296341Sdelphij        *(p++) = HEX_DIGITS[v & 0x0F];
157296341Sdelphij    }
158296341Sdelphij    *p = '\0';
159160814Ssimon
160296341Sdelphij    OPENSSL_free(buf);
161160814Ssimon
162296341Sdelphij    return ret;
163296341Sdelphij}
164160814Ssimon
165160814SsimonEC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
166296341Sdelphij                             const char *buf, EC_POINT *point, BN_CTX *ctx)
167296341Sdelphij{
168296341Sdelphij    EC_POINT *ret = NULL;
169296341Sdelphij    BIGNUM *tmp_bn = NULL;
170160814Ssimon
171296341Sdelphij    if (!BN_hex2bn(&tmp_bn, buf))
172296341Sdelphij        return NULL;
173160814Ssimon
174296341Sdelphij    ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
175160814Ssimon
176296341Sdelphij    BN_clear_free(tmp_bn);
177160814Ssimon
178296341Sdelphij    return ret;
179296341Sdelphij}
180