ec_print.c revision 280304
155682Smarkm/* crypto/ec/ec_print.c */ 2233294Sstas/* ==================================================================== 355682Smarkm * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. 455682Smarkm * 5233294Sstas * Redistribution and use in source and binary forms, with or without 655682Smarkm * modification, are permitted provided that the following conditions 755682Smarkm * are met: 855682Smarkm * 9233294Sstas * 1. Redistributions of source code must retain the above copyright 1055682Smarkm * notice, this list of conditions and the following disclaimer. 1155682Smarkm * 12233294Sstas * 2. Redistributions in binary form must reproduce the above copyright 1355682Smarkm * notice, this list of conditions and the following disclaimer in 1455682Smarkm * the documentation and/or other materials provided with the 1555682Smarkm * distribution. 16233294Sstas * 1755682Smarkm * 3. All advertising materials mentioning features or use of this 1855682Smarkm * software must display the following acknowledgment: 1955682Smarkm * "This product includes software developed by the OpenSSL Project 20233294Sstas * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 2155682Smarkm * 2255682Smarkm * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2355682Smarkm * endorse or promote products derived from this software without 2455682Smarkm * prior written permission. For written permission, please contact 2555682Smarkm * openssl-core@openssl.org. 2655682Smarkm * 2755682Smarkm * 5. Products derived from this software may not be called "OpenSSL" 2855682Smarkm * nor may "OpenSSL" appear in their names without prior written 2955682Smarkm * permission of the OpenSSL Project. 3055682Smarkm * 3155682Smarkm * 6. Redistributions of any form whatsoever must retain the following 3255682Smarkm * acknowledgment: 3355682Smarkm * "This product includes software developed by the OpenSSL Project 3455682Smarkm * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 3555682Smarkm * 36178825Sdfr * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 3755682Smarkm * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3855682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 3955682Smarkm * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4055682Smarkm * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4155682Smarkm * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4255682Smarkm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43233294Sstas * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4455682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4555682Smarkm * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 4655682Smarkm * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 4755682Smarkm * OF THE POSSIBILITY OF SUCH DAMAGE. 48178825Sdfr * ==================================================================== 4955682Smarkm * 50178825Sdfr * This product includes cryptographic software written by Eric Young 51178825Sdfr * (eay@cryptsoft.com). This product includes software written by Tim 52178825Sdfr * Hudson (tjh@cryptsoft.com). 53178825Sdfr * 5455682Smarkm */ 5555682Smarkm 5655682Smarkm#include <openssl/crypto.h> 5755682Smarkm#include "ec_lcl.h" 5855682Smarkm 5955682SmarkmBIGNUM *EC_POINT_point2bn(const EC_GROUP *group, 6055682Smarkm const EC_POINT *point, 6155682Smarkm point_conversion_form_t form, 6255682Smarkm BIGNUM *ret, BN_CTX *ctx) 6355682Smarkm{ 6455682Smarkm size_t buf_len = 0; 6555682Smarkm unsigned char *buf; 6655682Smarkm 6755682Smarkm buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); 6855682Smarkm if (buf_len == 0) 6955682Smarkm return NULL; 7055682Smarkm 7155682Smarkm if ((buf = OPENSSL_malloc(buf_len)) == NULL) 7255682Smarkm return NULL; 7355682Smarkm 7455682Smarkm if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { 7555682Smarkm OPENSSL_free(buf); 7655682Smarkm return NULL; 77233294Sstas } 7855682Smarkm 7955682Smarkm ret = BN_bin2bn(buf, buf_len, ret); 8055682Smarkm 8155682Smarkm OPENSSL_free(buf); 82178825Sdfr 8355682Smarkm return ret; 84178825Sdfr} 85178825Sdfr 86178825SdfrEC_POINT *EC_POINT_bn2point(const EC_GROUP *group, 87178825Sdfr const BIGNUM *bn, EC_POINT *point, BN_CTX *ctx) 8855682Smarkm{ 8955682Smarkm size_t buf_len = 0; 9055682Smarkm unsigned char *buf; 9155682Smarkm EC_POINT *ret; 9255682Smarkm 9355682Smarkm if ((buf_len = BN_num_bytes(bn)) == 0) 9455682Smarkm return NULL; 9555682Smarkm buf = OPENSSL_malloc(buf_len); 9655682Smarkm if (buf == NULL) 9755682Smarkm return NULL; 9855682Smarkm 9955682Smarkm if (!BN_bn2bin(bn, buf)) { 10055682Smarkm OPENSSL_free(buf); 10155682Smarkm return NULL; 10255682Smarkm } 10355682Smarkm 10455682Smarkm if (point == NULL) { 10555682Smarkm if ((ret = EC_POINT_new(group)) == NULL) { 10655682Smarkm OPENSSL_free(buf); 10755682Smarkm return NULL; 10855682Smarkm } 10955682Smarkm } else 11055682Smarkm ret = point; 111233294Sstas 11255682Smarkm if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) { 11355682Smarkm if (point == NULL) 11455682Smarkm EC_POINT_clear_free(ret); 11555682Smarkm OPENSSL_free(buf); 11655682Smarkm return NULL; 11755682Smarkm } 11855682Smarkm 11955682Smarkm OPENSSL_free(buf); 12055682Smarkm return ret; 12155682Smarkm} 122233294Sstas 12355682Smarkmstatic const char *HEX_DIGITS = "0123456789ABCDEF"; 12455682Smarkm 12555682Smarkm/* the return value must be freed (using OPENSSL_free()) */ 12655682Smarkmchar *EC_POINT_point2hex(const EC_GROUP *group, 12755682Smarkm const EC_POINT *point, 12855682Smarkm point_conversion_form_t form, BN_CTX *ctx) 12955682Smarkm{ 130233294Sstas char *ret, *p; 13155682Smarkm size_t buf_len = 0, i; 13255682Smarkm unsigned char *buf, *pbuf; 13355682Smarkm 13455682Smarkm buf_len = EC_POINT_point2oct(group, point, form, NULL, 0, ctx); 13555682Smarkm if (buf_len == 0) 13655682Smarkm return NULL; 13755682Smarkm 13855682Smarkm if ((buf = OPENSSL_malloc(buf_len)) == NULL) 13955682Smarkm return NULL; 140233294Sstas 141233294Sstas if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) { 14255682Smarkm OPENSSL_free(buf); 14355682Smarkm return NULL; 14455682Smarkm } 14555682Smarkm 14655682Smarkm ret = (char *)OPENSSL_malloc(buf_len * 2 + 2); 14755682Smarkm if (ret == NULL) { 14855682Smarkm OPENSSL_free(buf); 149233294Sstas return NULL; 150233294Sstas } 15155682Smarkm p = ret; 15255682Smarkm pbuf = buf; 15355682Smarkm for (i = buf_len; i > 0; i--) { 154233294Sstas int v = (int)*(pbuf++); 155233294Sstas *(p++) = HEX_DIGITS[v >> 4]; 15655682Smarkm *(p++) = HEX_DIGITS[v & 0x0F]; 15755682Smarkm } 15855682Smarkm *p = '\0'; 159233294Sstas 160233294Sstas OPENSSL_free(buf); 16155682Smarkm 16255682Smarkm return ret; 163233294Sstas} 164233294Sstas 16555682SmarkmEC_POINT *EC_POINT_hex2point(const EC_GROUP *group, 16655682Smarkm const char *buf, EC_POINT *point, BN_CTX *ctx) 16755682Smarkm{ 16855682Smarkm EC_POINT *ret = NULL; 16955682Smarkm BIGNUM *tmp_bn = NULL; 17055682Smarkm 17155682Smarkm if (!BN_hex2bn(&tmp_bn, buf)) 172233294Sstas return NULL; 17355682Smarkm 17455682Smarkm ret = EC_POINT_bn2point(group, tmp_bn, point, ctx); 17555682Smarkm 17655682Smarkm BN_clear_free(tmp_bn); 177178825Sdfr 178178825Sdfr return ret; 17955682Smarkm} 18055682Smarkm