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
10280297Sjkim *    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
59280297SjkimBIGNUM *EC_POINT_point2bn(const EC_GROUP *group,
60280297Sjkim                          const EC_POINT *point,
61160814Ssimon                          point_conversion_form_t form,
62280297Sjkim                          BIGNUM *ret, BN_CTX *ctx)
63280297Sjkim{
64280297Sjkim    size_t buf_len = 0;
65280297Sjkim    unsigned char *buf;
66160814Ssimon
67280297Sjkim    buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
68280297Sjkim    if (buf_len == 0)
69280297Sjkim        return NULL;
70160814Ssimon
71280297Sjkim    if ((buf = OPENSSL_malloc(buf_len)) == NULL)
72280297Sjkim        return NULL;
73160814Ssimon
74280297Sjkim    if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) {
75280297Sjkim        OPENSSL_free(buf);
76280297Sjkim        return NULL;
77280297Sjkim    }
78160814Ssimon
79280297Sjkim    ret = BN_bin2bn(buf, buf_len, ret);
80160814Ssimon
81280297Sjkim    OPENSSL_free(buf);
82160814Ssimon
83280297Sjkim    return ret;
84160814Ssimon}
85160814Ssimon
86160814SsimonEC_POINT *EC_POINT_bn2point(const EC_GROUP *group,
87280297Sjkim                            const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx)
88280297Sjkim{
89280297Sjkim    size_t buf_len = 0;
90280297Sjkim    unsigned char *buf;
91280297Sjkim    EC_POINT *ret;
92160814Ssimon
93280297Sjkim    if ((buf_len = BN_num_bytes(bn)) == 0)
94280297Sjkim        return NULL;
95280297Sjkim    buf = OPENSSL_malloc(buf_len);
96280297Sjkim    if (buf == NULL)
97280297Sjkim        return NULL;
98160814Ssimon
99280297Sjkim    if (!BN_bn2bin(bn, buf)) {
100280297Sjkim        OPENSSL_free(buf);
101280297Sjkim        return NULL;
102280297Sjkim    }
103160814Ssimon
104280297Sjkim    if (point == NULL) {
105280297Sjkim        if ((ret = EC_POINT_new(group)) == NULL) {
106280297Sjkim            OPENSSL_free(buf);
107280297Sjkim            return NULL;
108280297Sjkim        }
109280297Sjkim    } else
110280297Sjkim        ret = point;
111160814Ssimon
112280297Sjkim    if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) {
113280297Sjkim        if (point == NULL)
114280297Sjkim            EC_POINT_clear_free(ret);
115280297Sjkim        OPENSSL_free(buf);
116280297Sjkim        return NULL;
117280297Sjkim    }
118160814Ssimon
119280297Sjkim    OPENSSL_free(buf);
120280297Sjkim    return ret;
121280297Sjkim}
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,
128280297Sjkim                         point_conversion_form_t form, BN_CTX *ctx)
129280297Sjkim{
130280297Sjkim    char *ret, *p;
131280297Sjkim    size_t buf_len = 0, i;
132280297Sjkim    unsigned char *buf, *pbuf;
133160814Ssimon
134280297Sjkim    buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx);
135280297Sjkim    if (buf_len == 0)
136280297Sjkim        return NULL;
137160814Ssimon
138280297Sjkim    if ((buf = OPENSSL_malloc(buf_len)) == NULL)
139280297Sjkim        return NULL;
140160814Ssimon
141280297Sjkim    if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) {
142280297Sjkim        OPENSSL_free(buf);
143280297Sjkim        return NULL;
144280297Sjkim    }
145160814Ssimon
146280297Sjkim    ret = (char *)OPENSSL_malloc(buf_len * 2 + 2);
147280297Sjkim    if (ret == NULL) {
148280297Sjkim        OPENSSL_free(buf);
149280297Sjkim        return NULL;
150280297Sjkim    }
151280297Sjkim    p = ret;
152280297Sjkim    pbuf = buf;
153280297Sjkim    for (i = buf_len; i > 0; i--) {
154280297Sjkim        int v = (int)*(pbuf++);
155280297Sjkim        *(p++) = HEX_DIGITS[v >> 4];
156280297Sjkim        *(p++) = HEX_DIGITS[v & 0x0F];
157280297Sjkim    }
158280297Sjkim    *p = '\0';
159160814Ssimon
160280297Sjkim    OPENSSL_free(buf);
161160814Ssimon
162280297Sjkim    return ret;
163280297Sjkim}
164160814Ssimon
165160814SsimonEC_POINT *EC_POINT_hex2point(const EC_GROUP *group,
166280297Sjkim                             const char *buf, EC_POINT *point, BN_CTX *ctx)
167280297Sjkim{
168280297Sjkim    EC_POINT *ret = NULL;
169280297Sjkim    BIGNUM *tmp_bn = NULL;
170160814Ssimon
171280297Sjkim    if (!BN_hex2bn(&tmp_bn, buf))
172280297Sjkim        return NULL;
173160814Ssimon
174280297Sjkim    ret = EC_POINT_bn2point(group, tmp_bn, point, ctx);
175160814Ssimon
176280297Sjkim    BN_clear_free(tmp_bn);
177160814Ssimon
178280297Sjkim    return ret;
179280297Sjkim}
180