New functions EC_POINT_point2bn(), EC_POINT_bn2point(), EC_POINT_point2hex(), EC_POIN...
authorBodo Möller <bodo@openssl.org>
Thu, 30 May 2002 13:16:03 +0000 (13:16 +0000)
committerBodo Möller <bodo@openssl.org>
Thu, 30 May 2002 13:16:03 +0000 (13:16 +0000)
Submitted by: Nils Larsch

CHANGES
crypto/asn1/t_pkey.c
crypto/ec/Makefile.ssl
crypto/ec/ec.h
crypto/ec/ec_print.c [new file with mode: 0644]

diff --git a/CHANGES b/CHANGES
index c329d130889d49624f9ae3cc4bb40f82f2106a1b..b18262b72e24ecaa3445a47be4d4b855565da77d 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -4,6 +4,15 @@
 
  Changes between 0.9.7 and 0.9.8  [xx XXX 2002]
 
 
  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 <nla@trustcenter.de>]
+
   *) Change internals of the EC library so that the functions
           EC_GROUP_set_generator()
           EC_GROUP_get_generator()
   *) 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.
      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 <nla@trustcenter.de> with input by Bodo Moeller]
 
   *) Implement compute_wNAF (crypto/ec/ec_mult.c) without BIGNUM
      arithmetic, and such that modified wNAFs are generated
 
   *) Implement compute_wNAF (crypto/ec/ec_mult.c) without BIGNUM
      arithmetic, and such that modified wNAFs are generated
index 1b29fdc4a8911e2d66875fdd415c015fa52c2668..48414449e5ea865f2e3f7c86754b7adfcae39ac2 100644 (file)
@@ -266,58 +266,36 @@ int ECDSA_print(BIO *bp, const ECDSA *x, int off)
                reason = ERR_R_EC_LIB;
                goto err;
                }
                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;
                }
                {
                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;
                }
                {
                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;
                }
                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;
                {
                reason = ERR_R_MALLOC_FAILURE;
-               buf_len = i;
                goto err;
                }
        if (off)
                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;     
        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;
                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;
        {
                reason=ERR_R_MALLOC_FAILURE;
                goto err;
index 3b5ae1951227426761628b89b8abb0662f690f6c..49b5300708863f1efbb5419f37cc12dc1f939fef 100644 (file)
@@ -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 \
 
 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 \
 
 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)
 
 
 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_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
 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
index 6baf39cfa658c567b7dce196d39fe7b71ea110c9..2dbe3efadf94da474a8666f3bb0d7d9a73967a8d 100644 (file)
@@ -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 *);
 
 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 *);
 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 (file)
index 0000000..4b9b882
--- /dev/null
@@ -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 <openssl/crypto.h>
+#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;
+       }