ec_print.c revision 160814
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 10160814Ssimon * 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 59160814SsimonBIGNUM *EC_POINT_point2bn(const EC_GROUP *group, 60160814Ssimon const EC_POINT *point, 61160814Ssimon point_conversion_form_t form, 62160814Ssimon BIGNUM *ret, 63160814Ssimon BN_CTX *ctx) 64160814Ssimon { 65160814Ssimon size_t buf_len=0; 66160814Ssimon unsigned char *buf; 67160814Ssimon 68160814Ssimon buf_len = EC_POINT_point2oct(group, point, form, 69160814Ssimon NULL, 0, ctx); 70160814Ssimon if (buf_len == 0) 71160814Ssimon return NULL; 72160814Ssimon 73160814Ssimon if ((buf = OPENSSL_malloc(buf_len)) == NULL) 74160814Ssimon return NULL; 75160814Ssimon 76160814Ssimon if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) 77160814Ssimon { 78160814Ssimon OPENSSL_free(buf); 79160814Ssimon return NULL; 80160814Ssimon } 81160814Ssimon 82160814Ssimon ret = BN_bin2bn(buf, buf_len, ret); 83160814Ssimon 84160814Ssimon OPENSSL_free(buf); 85160814Ssimon 86160814Ssimon return ret; 87160814Ssimon} 88160814Ssimon 89160814SsimonEC_POINT *EC_POINT_bn2point(const EC_GROUP *group, 90160814Ssimon const BIGNUM *bn, 91160814Ssimon EC_POINT *point, 92160814Ssimon BN_CTX *ctx) 93160814Ssimon { 94160814Ssimon size_t buf_len=0; 95160814Ssimon unsigned char *buf; 96160814Ssimon EC_POINT *ret; 97160814Ssimon 98160814Ssimon if ((buf_len = BN_num_bytes(bn)) == 0) return NULL; 99160814Ssimon buf = OPENSSL_malloc(buf_len); 100160814Ssimon if (buf == NULL) 101160814Ssimon return NULL; 102160814Ssimon 103160814Ssimon if (!BN_bn2bin(bn, buf)) 104160814Ssimon { 105160814Ssimon OPENSSL_free(buf); 106160814Ssimon return NULL; 107160814Ssimon } 108160814Ssimon 109160814Ssimon if (point == NULL) 110160814Ssimon { 111160814Ssimon if ((ret = EC_POINT_new(group)) == NULL) 112160814Ssimon { 113160814Ssimon OPENSSL_free(buf); 114160814Ssimon return NULL; 115160814Ssimon } 116160814Ssimon } 117160814Ssimon else 118160814Ssimon ret = point; 119160814Ssimon 120160814Ssimon if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) 121160814Ssimon { 122160814Ssimon if (point == NULL) 123160814Ssimon EC_POINT_clear_free(ret); 124160814Ssimon OPENSSL_free(buf); 125160814Ssimon return NULL; 126160814Ssimon } 127160814Ssimon 128160814Ssimon OPENSSL_free(buf); 129160814Ssimon return ret; 130160814Ssimon } 131160814Ssimon 132160814Ssimonstatic const char *HEX_DIGITS = "0123456789ABCDEF"; 133160814Ssimon 134160814Ssimon/* the return value must be freed (using OPENSSL_free()) */ 135160814Ssimonchar *EC_POINT_point2hex(const EC_GROUP *group, 136160814Ssimon const EC_POINT *point, 137160814Ssimon point_conversion_form_t form, 138160814Ssimon BN_CTX *ctx) 139160814Ssimon { 140160814Ssimon char *ret, *p; 141160814Ssimon size_t buf_len=0,i; 142160814Ssimon unsigned char *buf, *pbuf; 143160814Ssimon 144160814Ssimon buf_len = EC_POINT_point2oct(group, point, form, 145160814Ssimon NULL, 0, ctx); 146160814Ssimon if (buf_len == 0) 147160814Ssimon return NULL; 148160814Ssimon 149160814Ssimon if ((buf = OPENSSL_malloc(buf_len)) == NULL) 150160814Ssimon return NULL; 151160814Ssimon 152160814Ssimon if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) 153160814Ssimon { 154160814Ssimon OPENSSL_free(buf); 155160814Ssimon return NULL; 156160814Ssimon } 157160814Ssimon 158160814Ssimon ret = (char *)OPENSSL_malloc(buf_len*2+2); 159160814Ssimon if (ret == NULL) 160160814Ssimon { 161160814Ssimon OPENSSL_free(buf); 162160814Ssimon return NULL; 163160814Ssimon } 164160814Ssimon p = ret; 165160814Ssimon pbuf = buf; 166160814Ssimon for (i=buf_len; i > 0; i--) 167160814Ssimon { 168160814Ssimon int v = (int) *(pbuf++); 169160814Ssimon *(p++)=HEX_DIGITS[v>>4]; 170160814Ssimon *(p++)=HEX_DIGITS[v&0x0F]; 171160814Ssimon } 172160814Ssimon *p='\0'; 173160814Ssimon 174160814Ssimon OPENSSL_free(buf); 175160814Ssimon 176160814Ssimon return ret; 177160814Ssimon } 178160814Ssimon 179160814SsimonEC_POINT *EC_POINT_hex2point(const EC_GROUP *group, 180160814Ssimon const char *buf, 181160814Ssimon EC_POINT *point, 182160814Ssimon BN_CTX *ctx) 183160814Ssimon { 184160814Ssimon EC_POINT *ret=NULL; 185160814Ssimon BIGNUM *tmp_bn=NULL; 186160814Ssimon 187160814Ssimon if (!BN_hex2bn(&tmp_bn, buf)) 188160814Ssimon return NULL; 189160814Ssimon 190160814Ssimon ret = EC_POINT_bn2point(group, tmp_bn, point, ctx); 191160814Ssimon 192160814Ssimon BN_clear_free(tmp_bn); 193160814Ssimon 194160814Ssimon return ret; 195160814Ssimon } 196