From 6cbe6382944ff47881593935cc5b029f745beeea Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bodo=20M=C3=B6ller?= Date: Thu, 30 May 2002 13:16:03 +0000 Subject: [PATCH] New functions EC_POINT_point2bn(), EC_POINT_bn2point(), EC_POINT_point2hex(), EC_POINT_hex2point() Submitted by: Nils Larsch --- CHANGES | 11 ++- crypto/asn1/t_pkey.c | 87 +++++++------------ crypto/ec/Makefile.ssl | 10 ++- crypto/ec/ec.h | 10 +++ crypto/ec/ec_print.c | 192 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 251 insertions(+), 59 deletions(-) create mode 100644 crypto/ec/ec_print.c diff --git a/CHANGES b/CHANGES index c329d13088..b18262b72e 100644 --- a/CHANGES +++ b/CHANGES @@ -4,6 +4,15 @@ Changes between 0.9.7 and 0.9.8 [xx XXX 2002] + *) Add functions + EC_POINT_point2bn() + EC_POINT_bn2point() + EC_POINT_point2hex() + EC_POINT_hex2point() + providing useful interfaces to EC_POINT_point2oct() and + EC_POINT_oct2point(). + [Nils Larsch ] + *) Change internals of the EC library so that the functions EC_GROUP_set_generator() EC_GROUP_get_generator() @@ -12,7 +21,7 @@ are implemented directly in crypto/ec/ec_lib.c and not dispatched to methods, which would lead to unnecessary code duplication when adding different types of curves. - [Nils Larsch with input by Bodo Moeller] + [Nils Larsch with input by Bodo Moeller] *) Implement compute_wNAF (crypto/ec/ec_mult.c) without BIGNUM arithmetic, and such that modified wNAFs are generated diff --git a/crypto/asn1/t_pkey.c b/crypto/asn1/t_pkey.c index 1b29fdc4a8..48414449e5 100644 --- a/crypto/asn1/t_pkey.c +++ b/crypto/asn1/t_pkey.c @@ -266,58 +266,36 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off) reason = ERR_R_EC_LIB; goto err; } - if (!EC_GROUP_get_order(x->group, tmp_6, NULL) || !EC_GROUP_get_cofactor(x->group, tmp_7, NULL)) + if (!EC_GROUP_get_order(x->group, tmp_6, NULL) || + !EC_GROUP_get_cofactor(x->group, tmp_7, NULL)) { reason = ERR_R_EC_LIB; goto err; } - if ((buf_len = EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), NULL, 0, ctx)) == 0) - { - reason = ECDSA_R_UNEXPECTED_PARAMETER_LENGTH; - goto err; - } - if ((buffer = OPENSSL_malloc(buf_len)) == NULL) - { - reason = ERR_R_MALLOC_FAILURE; - goto err; - } - if (!EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), - buffer, buf_len, ctx)) goto err; - if ((tmp_4 = BN_bin2bn(buffer, buf_len, NULL)) == NULL) - { - reason = ERR_R_BN_LIB; - goto err; - } - if ((i = EC_POINT_point2oct(x->group, x->pub_key, ECDSA_get_conversion_form(x), NULL, 0, ctx)) == 0) - { - reason = ECDSA_R_UNEXPECTED_PARAMETER_LENGTH; - goto err; - } - if (i > buf_len && (buffer = OPENSSL_realloc(buffer, i)) == NULL) - { - reason = ERR_R_MALLOC_FAILURE; - buf_len = i; - goto err; - } - if (!EC_POINT_point2oct(x->group, x->pub_key, ECDSA_get_conversion_form(x), - buffer, buf_len, ctx)) + if ((tmp_4 = EC_POINT_point2bn(x->group, point, + ECDSA_get_conversion_form(x), tmp_4, ctx)) == NULL) { reason = ERR_R_EC_LIB; goto err; } - if ((tmp_5 = BN_bin2bn(buffer, buf_len, NULL)) == NULL) + if ((tmp_5 = EC_POINT_point2bn(x->group, x->pub_key, + ECDSA_get_conversion_form(x), tmp_5, ctx)) == NULL) { - reason = ERR_R_BN_LIB; + reason = ERR_R_EC_LIB; goto err; } - if (tmp_1 != NULL) - i = BN_num_bytes(tmp_1)*2; - else - i=256; - if ((i + 10) > buf_len && (buffer = OPENSSL_realloc(buffer, i+10)) == NULL) + + buf_len = BN_num_bytes(tmp_1); + if (buf_len < (i = BN_num_bytes(tmp_2))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_3))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_4))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_5))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_6))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_7))) buf_len = i; + buf_len += 10; + if ((buffer = OPENSSL_malloc(buf_len)) == NULL) { reason = ERR_R_MALLOC_FAILURE; - buf_len = i; goto err; } if (off) @@ -552,25 +530,22 @@ int ECDSAParameters_print(BIO *bp, const ECDSA *x) if ((point = EC_GROUP_get0_generator(x->group)) == NULL) goto err; if (!EC_GROUP_get_order(x->group, tmp_5, ctx)) goto err; if (!EC_GROUP_get_cofactor(x->group, tmp_6, ctx)) goto err; - buf_len = EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), NULL, 0, ctx); - if (!buf_len || (buffer = OPENSSL_malloc(buf_len)) == NULL) - { - reason = ERR_R_MALLOC_FAILURE; - goto err; - } - if (!EC_POINT_point2oct(x->group, point, ECDSA_get_conversion_form(x), buffer, buf_len, ctx)) - { + + if ((tmp_4 = EC_POINT_point2bn(x->group, point, + ECDSA_get_conversion_form(x), NULL, ctx)) == NULL) + { reason = ERR_R_EC_LIB; goto err; - } - if ((tmp_4 = BN_bin2bn(buffer, buf_len, NULL)) == NULL) - { - reason = ERR_R_BN_LIB; - goto err; - } - - i = BN_num_bits(tmp_1) + 10; - if (i > buf_len && (buffer = OPENSSL_realloc(buffer, i)) == NULL) + } + + buf_len = BN_num_bytes(tmp_1); + if (buf_len < (i = BN_num_bytes(tmp_2))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_3))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_4))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_5))) buf_len = i; + if (buf_len < (i = BN_num_bytes(tmp_6))) buf_len = i; + buf_len += 10; + if ((buffer = OPENSSL_malloc(buf_len)) == NULL) { reason=ERR_R_MALLOC_FAILURE; goto err; diff --git a/crypto/ec/Makefile.ssl b/crypto/ec/Makefile.ssl index 3b5ae19512..49b5300708 100644 --- a/crypto/ec/Makefile.ssl +++ b/crypto/ec/Makefile.ssl @@ -24,10 +24,10 @@ APPS= LIB=$(TOP)/libcrypto.a LIBSRC= ec_lib.c ecp_smpl.c ecp_mont.c ecp_recp.c ecp_nist.c ec_cvt.c ec_mult.c \ - ec_err.c ec_curve.c ec_check.c + ec_err.c ec_curve.c ec_check.c ec_print.c LIBOBJ= ec_lib.o ecp_smpl.o ecp_mont.o ecp_recp.o ecp_nist.o ec_cvt.o ec_mult.o \ - ec_err.o ec_curve.o ec_check.o + ec_err.o ec_curve.o ec_check.o ec_print.o SRC= $(LIBSRC) @@ -122,6 +122,12 @@ ec_mult.o: ../../include/openssl/lhash.h ../../include/openssl/opensslconf.h ec_mult.o: ../../include/openssl/opensslv.h ../../include/openssl/safestack.h ec_mult.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h ec_mult.o: ec_lcl.h ec_mult.c +ec_print.o: ../../include/openssl/bn.h ../../include/openssl/crypto.h +ec_print.o: ../../include/openssl/e_os2.h ../../include/openssl/ec.h +ec_print.o: ../../include/openssl/opensslconf.h +ec_print.o: ../../include/openssl/opensslv.h ../../include/openssl/safestack.h +ec_print.o: ../../include/openssl/stack.h ../../include/openssl/symhacks.h +ec_print.o: ec_lcl.h ec_print.c ecp_mont.o: ../../include/openssl/bio.h ../../include/openssl/bn.h ecp_mont.o: ../../include/openssl/crypto.h ../../include/openssl/e_os2.h ecp_mont.o: ../../include/openssl/ec.h ../../include/openssl/err.h diff --git a/crypto/ec/ec.h b/crypto/ec/ec.h index 6baf39cfa6..2dbe3efadf 100644 --- a/crypto/ec/ec.h +++ b/crypto/ec/ec.h @@ -198,6 +198,16 @@ size_t EC_POINT_point2oct(const EC_GROUP *, const EC_POINT *, point_conversion_f int EC_POINT_oct2point(const EC_GROUP *, EC_POINT *, const unsigned char *buf, size_t len, BN_CTX *); +/* other interfaces to point2oct/oct2point: */ +BIGNUM *EC_POINT_point2bn(const EC_GROUP *, const EC_POINT *, + point_conversion_form_t form, BIGNUM *, BN_CTX *); +EC_POINT *EC_POINT_bn2point(const EC_GROUP *, const BIGNUM *, + EC_POINT *, BN_CTX *); +char *EC_POINT_point2hex(const EC_GROUP *, const EC_POINT *, + point_conversion_form_t form, BN_CTX *); +EC_POINT *EC_POINT_hex2point(const EC_GROUP *, const char *, + EC_POINT *, BN_CTX *); + int EC_POINT_add(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, const EC_POINT *b, BN_CTX *); int EC_POINT_dbl(const EC_GROUP *, EC_POINT *r, const EC_POINT *a, BN_CTX *); int EC_POINT_invert(const EC_GROUP *, EC_POINT *, BN_CTX *); diff --git a/crypto/ec/ec_print.c b/crypto/ec/ec_print.c new file mode 100644 index 0000000000..4b9b882668 --- /dev/null +++ b/crypto/ec/ec_print.c @@ -0,0 +1,192 @@ +/* crypto/ec/ec_print.c */ +/* ==================================================================== + * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" + * + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * openssl-core@openssl.org. + * + * 5. Products derived from this software may not be called "OpenSSL" + * nor may "OpenSSL" appear in their names without prior written + * permission of the OpenSSL Project. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the OpenSSL Project + * for use in the OpenSSL Toolkit (http://www.openssl.org/)" + * + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This product includes cryptographic software written by Eric Young + * (eay@cryptsoft.com). This product includes software written by Tim + * Hudson (tjh@cryptsoft.com). + * + */ + +#include +#include "ec_lcl.h" + +BIGNUM *EC_POINT_point2bn(const EC_GROUP *group, + const EC_POINT *point, + point_conversion_form_t form, + BIGNUM *ret, + BN_CTX *ctx) + { + size_t buf_len=0; + unsigned char *buf; + + buf_len = EC_POINT_point2oct(group, point, form, + NULL, 0, ctx); + if (buf_len == 0) + return NULL; + + if ((buf = OPENSSL_malloc(buf_len)) == NULL) + return NULL; + + if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) + { + OPENSSL_free(buf); + return NULL; + } + + ret = BN_bin2bn(buf, buf_len, ret); + + OPENSSL_free(buf); + + return ret; +} + +EC_POINT *EC_POINT_bn2point(const EC_GROUP *group, + const BIGNUM *bn, + EC_POINT *point, + BN_CTX *ctx) + { + size_t buf_len=0; + unsigned char *buf; + EC_POINT *ret; + + if ((buf_len = BN_num_bytes(bn)) == 0) return NULL; + buf = OPENSSL_malloc(buf_len); + if (buf == NULL) + return NULL; + + if (!BN_bn2bin(bn, buf)) + { + OPENSSL_free(buf); + return NULL; + } + + if (point == NULL) + { + if ((ret = EC_POINT_new(group)) == NULL) + { + OPENSSL_free(buf); + return NULL; + } + } + else + ret = point; + + if (!EC_POINT_oct2point(group, ret, buf, buf_len, ctx)) + { + if (point == NULL) + EC_POINT_clear_free(ret); + OPENSSL_free(buf); + return NULL; + } + + OPENSSL_free(buf); + return ret; + } + +static const char *HEX_DIGITS = "0123456789ABCDEF"; + +/* the return value must be freed (using OPENSSL_free()) */ +char *EC_POINT_point2hex(const EC_GROUP *group, + const EC_POINT *point, + point_conversion_form_t form, + BN_CTX *ctx) + { + char *ret, *p; + size_t buf_len=0,i; + unsigned char *buf; + + buf_len = EC_POINT_point2oct(group, point, form, + NULL, 0, ctx); + if (buf_len == 0) + return NULL; + + if ((buf = OPENSSL_malloc(buf_len)) == NULL) + return NULL; + + if (!EC_POINT_point2oct(group, point, form, buf, buf_len, ctx)) + { + OPENSSL_free(buf); + return NULL; + } + + ret = (char *)OPENSSL_malloc(buf_len*2+2); + if (ret == NULL) + { + OPENSSL_free(buf); + return NULL; + } + p = ret; + for (i=buf_len; i > 0; i--) + { + int v = (int) *(buf++); + *(p++)=HEX_DIGITS[v>>4]; + *(p++)=HEX_DIGITS[v&0x0F]; + } + *p='\0'; + + return ret; + } + +EC_POINT *EC_POINT_hex2point(const EC_GROUP *group, + const char *buf, + EC_POINT *point, + BN_CTX *ctx) + { + EC_POINT *ret=NULL; + BIGNUM *tmp_bn=NULL; + + if (!BN_hex2bn(&tmp_bn, buf)) + return NULL; + + ret = EC_POINT_bn2point(group, tmp_bn, point, ctx); + + BN_clear_free(tmp_bn); + + return ret; + } -- 2.25.1