ec_print.c revision 160814
1268896Sbapt/* crypto/ec/ec_print.c */ 2268896Sbapt/* ==================================================================== 3268896Sbapt * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 4268896Sbapt * 5268896Sbapt * Redistribution and use in source and binary forms, with or without 6268896Sbapt * modification, are permitted provided that the following conditions 7268896Sbapt * are met: 8268896Sbapt * 9268896Sbapt * 1. Redistributions of source code must retain the above copyright 10268896Sbapt * notice, this list of conditions and the following disclaimer. 11268896Sbapt * 12268896Sbapt * 2. Redistributions in binary form must reproduce the above copyright 13268896Sbapt * notice, this list of conditions and the following disclaimer in 14268896Sbapt * the documentation and/or other materials provided with the 15268896Sbapt * distribution. 16268896Sbapt * 17268896Sbapt * 3. All advertising materials mentioning features or use of this 18268896Sbapt * software must display the following acknowledgment: 19268896Sbapt * "This product includes software developed by the OpenSSL Project 20268896Sbapt * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 21268896Sbapt * 22268896Sbapt * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23268896Sbapt * endorse or promote products derived from this software without 24262395Sbapt * prior written permission. For written permission, please contact 25262395Sbapt * openssl-core@openssl.org. 26262395Sbapt * 27262395Sbapt * 5. Products derived from this software may not be called "OpenSSL" 28262395Sbapt * nor may "OpenSSL" appear in their names without prior written 29262395Sbapt * permission of the OpenSSL Project. 30262395Sbapt * 31262395Sbapt * 6. Redistributions of any form whatsoever must retain the following 32262395Sbapt * acknowledgment: 33262395Sbapt * "This product includes software developed by the OpenSSL Project 34262395Sbapt * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 35262395Sbapt * 36262395Sbapt * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37262395Sbapt * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38262395Sbapt * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39262395Sbapt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40262395Sbapt * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41262395Sbapt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42262395Sbapt * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43262395Sbapt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44262395Sbapt * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45262395Sbapt * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46262395Sbapt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47262395Sbapt * OF THE POSSIBILITY OF SUCH DAMAGE. 48262395Sbapt * ==================================================================== 49262395Sbapt * 50262395Sbapt * This product includes cryptographic software written by Eric Young 51262395Sbapt * (eay@cryptsoft.com). This product includes software written by Tim 52262395Sbapt * Hudson (tjh@cryptsoft.com). 53262395Sbapt * 54262395Sbapt */ 55262395Sbapt 56262395Sbapt#include <openssl/crypto.h> 57262395Sbapt#include "ec_lcl.h" 58262395Sbapt 59262395SbaptBIGNUM *EC_POINT_point2bn(const EC_GROUP *group, 60262395Sbapt const EC_POINT *point, 61262395Sbapt point_conversion_form_t form, 62262395Sbapt BIGNUM *ret, 63262395Sbapt BN_CTX *ctx) 64262395Sbapt { 65262395Sbapt size_t buf_len=0; 66262395Sbapt unsigned char *buf; 67262395Sbapt 68262395Sbapt buf_len = EC_POINT_point2oct(group, point, form, 69262395Sbapt NULL, 0, ctx); 70262395Sbapt if (buf_len == 0) 71262395Sbapt return NULL; 72262395Sbapt 73262395Sbapt if ((buf = OPENSSL_malloc(buf_len)) == NULL) 74262395Sbapt return NULL; 75262395Sbapt 76262395Sbapt if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) 77262395Sbapt { 78262395Sbapt OPENSSL_free(buf); 79262395Sbapt return NULL; 80262395Sbapt } 81262395Sbapt 82262395Sbapt ret = BN_bin2bn(buf, buf_len, ret); 83262395Sbapt 84262395Sbapt OPENSSL_free(buf); 85262395Sbapt 86262395Sbapt return ret; 87262395Sbapt} 88262395Sbapt 89262395SbaptEC_POINT *EC_POINT_bn2point(const EC_GROUP *group, 90262395Sbapt const BIGNUM *bn, 91262395Sbapt EC_POINT *point, 92262395Sbapt BN_CTX *ctx) 93262395Sbapt { 94262395Sbapt size_t buf_len=0; 95262395Sbapt unsigned char *buf; 96262395Sbapt EC_POINT *ret; 97262395Sbapt 98262395Sbapt if ((buf_len = BN_num_bytes(bn)) == 0) return NULL; 99262395Sbapt buf = OPENSSL_malloc(buf_len); 100262395Sbapt if (buf == NULL) 101262395Sbapt return NULL; 102262395Sbapt 103262395Sbapt if (!BN_bn2bin(bn, buf)) 104262395Sbapt { 105262395Sbapt OPENSSL_free(buf); 106262395Sbapt return NULL; 107262395Sbapt } 108262395Sbapt 109262395Sbapt if (point == NULL) 110262395Sbapt { 111262395Sbapt if ((ret = EC_POINT_new(group)) == NULL) 112262395Sbapt { 113262395Sbapt OPENSSL_free(buf); 114262395Sbapt return NULL; 115262395Sbapt } 116262395Sbapt } 117262395Sbapt else 118262395Sbapt ret = point; 119262395Sbapt 120262395Sbapt if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) 121262395Sbapt { 122262395Sbapt if (point == NULL) 123262395Sbapt EC_POINT_clear_free(ret); 124262395Sbapt OPENSSL_free(buf); 125262395Sbapt return NULL; 126262395Sbapt } 127262395Sbapt 128262395Sbapt OPENSSL_free(buf); 129262395Sbapt return ret; 130262395Sbapt } 131262395Sbapt 132262395Sbaptstatic const char *HEX_DIGITS = "0123456789ABCDEF"; 133262395Sbapt 134262395Sbapt/* the return value must be freed (using OPENSSL_free()) */ 135262395Sbaptchar *EC_POINT_point2hex(const EC_GROUP *group, 136262395Sbapt const EC_POINT *point, 137262395Sbapt point_conversion_form_t form, 138262395Sbapt BN_CTX *ctx) 139262395Sbapt { 140262395Sbapt char *ret, *p; 141262395Sbapt size_t buf_len=0,i; 142262395Sbapt unsigned char *buf, *pbuf; 143262395Sbapt 144262395Sbapt buf_len = EC_POINT_point2oct(group, point, form, 145262395Sbapt NULL, 0, ctx); 146262395Sbapt if (buf_len == 0) 147262395Sbapt return NULL; 148262395Sbapt 149262395Sbapt if ((buf = OPENSSL_malloc(buf_len)) == NULL) 150262395Sbapt return NULL; 151262395Sbapt 152262395Sbapt if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) 153262395Sbapt { 154262395Sbapt OPENSSL_free(buf); 155262395Sbapt return NULL; 156262395Sbapt } 157262395Sbapt 158262395Sbapt ret = (char *)OPENSSL_malloc(buf_len*2+2); 159262395Sbapt if (ret == NULL) 160262395Sbapt { 161262395Sbapt OPENSSL_free(buf); 162262395Sbapt return NULL; 163262395Sbapt } 164262395Sbapt p = ret; 165262395Sbapt pbuf = buf; 166262395Sbapt for (i=buf_len; i > 0; i--) 167262395Sbapt { 168262395Sbapt int v = (int) *(pbuf++); 169262395Sbapt *(p++)=HEX_DIGITS[v>>4]; 170262395Sbapt *(p++)=HEX_DIGITS[v&0x0F]; 171262395Sbapt } 172262395Sbapt *p='\0'; 173262395Sbapt 174262395Sbapt OPENSSL_free(buf); 175262395Sbapt 176262395Sbapt return ret; 177262395Sbapt } 178262395Sbapt 179262395SbaptEC_POINT *EC_POINT_hex2point(const EC_GROUP *group, 180262395Sbapt const char *buf, 181262395Sbapt EC_POINT *point, 182262395Sbapt BN_CTX *ctx) 183262395Sbapt { 184262395Sbapt EC_POINT *ret=NULL; 185262395Sbapt BIGNUM *tmp_bn=NULL; 186262395Sbapt 187262395Sbapt if (!BN_hex2bn(&tmp_bn, buf)) 188262395Sbapt return NULL; 189262395Sbapt 190262395Sbapt ret = EC_POINT_bn2point(group, tmp_bn, point, ctx); 191262395Sbapt 192262395Sbapt BN_clear_free(tmp_bn); 193262395Sbapt 194262395Sbapt return ret; 195262395Sbapt } 196262395Sbapt