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 10280304Sjkim * 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 59280304SjkimBIGNUM *EC_POINT_point2bn(const EC_GROUP *group, 60280304Sjkim const EC_POINT *point, 61160814Ssimon point_conversion_form_t form, 62280304Sjkim BIGNUM *ret, BN_CTX *ctx) 63280304Sjkim{ 64280304Sjkim size_t buf_len = 0; 65280304Sjkim unsigned char *buf; 66160814Ssimon 67280304Sjkim buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); 68280304Sjkim if (buf_len == 0) 69280304Sjkim return NULL; 70160814Ssimon 71280304Sjkim if ((buf = OPENSSL_malloc(buf_len)) == NULL) 72280304Sjkim return NULL; 73160814Ssimon 74280304Sjkim if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { 75280304Sjkim OPENSSL_free(buf); 76280304Sjkim return NULL; 77280304Sjkim } 78160814Ssimon 79280304Sjkim ret = BN_bin2bn(buf, buf_len, ret); 80160814Ssimon 81280304Sjkim OPENSSL_free(buf); 82160814Ssimon 83280304Sjkim return ret; 84160814Ssimon} 85160814Ssimon 86160814SsimonEC_POINT *EC_POINT_bn2point(const EC_GROUP *group, 87280304Sjkim const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx) 88280304Sjkim{ 89280304Sjkim size_t buf_len = 0; 90280304Sjkim unsigned char *buf; 91280304Sjkim EC_POINT *ret; 92160814Ssimon 93280304Sjkim if ((buf_len = BN_num_bytes(bn)) == 0) 94280304Sjkim return NULL; 95280304Sjkim buf = OPENSSL_malloc(buf_len); 96280304Sjkim if (buf == NULL) 97280304Sjkim return NULL; 98160814Ssimon 99280304Sjkim if (!BN_bn2bin(bn, buf)) { 100280304Sjkim OPENSSL_free(buf); 101280304Sjkim return NULL; 102280304Sjkim } 103160814Ssimon 104280304Sjkim if (point == NULL) { 105280304Sjkim if ((ret = EC_POINT_new(group)) == NULL) { 106280304Sjkim OPENSSL_free(buf); 107280304Sjkim return NULL; 108280304Sjkim } 109280304Sjkim } else 110280304Sjkim ret = point; 111160814Ssimon 112280304Sjkim if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) { 113280304Sjkim if (point == NULL) 114280304Sjkim EC_POINT_clear_free(ret); 115280304Sjkim OPENSSL_free(buf); 116280304Sjkim return NULL; 117280304Sjkim } 118160814Ssimon 119280304Sjkim OPENSSL_free(buf); 120280304Sjkim return ret; 121280304Sjkim} 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, 128280304Sjkim point_conversion_form_t form, BN_CTX *ctx) 129280304Sjkim{ 130280304Sjkim char *ret, *p; 131280304Sjkim size_t buf_len = 0, i; 132280304Sjkim unsigned char *buf, *pbuf; 133160814Ssimon 134280304Sjkim buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); 135280304Sjkim if (buf_len == 0) 136280304Sjkim return NULL; 137160814Ssimon 138280304Sjkim if ((buf = OPENSSL_malloc(buf_len)) == NULL) 139280304Sjkim return NULL; 140160814Ssimon 141280304Sjkim if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { 142280304Sjkim OPENSSL_free(buf); 143280304Sjkim return NULL; 144280304Sjkim } 145160814Ssimon 146280304Sjkim ret = (char *)OPENSSL_malloc(buf_len * 2 + 2); 147280304Sjkim if (ret == NULL) { 148280304Sjkim OPENSSL_free(buf); 149280304Sjkim return NULL; 150280304Sjkim } 151280304Sjkim p = ret; 152280304Sjkim pbuf = buf; 153280304Sjkim for (i = buf_len; i > 0; i--) { 154280304Sjkim int v = (int)*(pbuf++); 155280304Sjkim *(p++) = HEX_DIGITS[v >> 4]; 156280304Sjkim *(p++) = HEX_DIGITS[v & 0x0F]; 157280304Sjkim } 158280304Sjkim *p = '\0'; 159160814Ssimon 160280304Sjkim OPENSSL_free(buf); 161160814Ssimon 162280304Sjkim return ret; 163280304Sjkim} 164160814Ssimon 165160814SsimonEC_POINT *EC_POINT_hex2point(const EC_GROUP *group, 166280304Sjkim const char *buf, EC_POINT *point, BN_CTX *ctx) 167280304Sjkim{ 168280304Sjkim EC_POINT *ret = NULL; 169280304Sjkim BIGNUM *tmp_bn = NULL; 170160814Ssimon 171280304Sjkim if (!BN_hex2bn(&tmp_bn, buf)) 172280304Sjkim return NULL; 173160814Ssimon 174280304Sjkim ret = EC_POINT_bn2point(group, tmp_bn, point, ctx); 175160814Ssimon 176280304Sjkim BN_clear_free(tmp_bn); 177160814Ssimon 178280304Sjkim return ret; 179280304Sjkim} 180