Premaster secret handling fixes
[openssl.git] / crypto / ec / ec_lib.c
index f7b2025ea6f2a9fd6626b3471559159bb98adc1d..8fb8b089ef263bf70a900bc9723e3117989affff 100644 (file)
@@ -61,7 +61,7 @@
  * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  */
 
-#define OPENSSL_FIPSAPI
+
 
 #include <string.h>
 
@@ -70,7 +70,6 @@
 
 #include "ec_lcl.h"
 
-__fips_constseg
 static const char EC_version[] = "EC" OPENSSL_VERSION_PTEXT;
 
 
@@ -101,10 +100,14 @@ EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
        ret->meth = meth;
 
        ret->extra_data = NULL;
+       ret->mont_data = NULL;
 
        ret->generator = NULL;
-       BN_init(&ret->order);
-       BN_init(&ret->cofactor);
+       ret->order = BN_new();
+       ret->cofactor = NULL;
+       if(!ret->order) goto err;
+       ret->cofactor = BN_new();
+       if(!ret->cofactor) goto err;
 
        ret->curve_name = 0;    
        ret->asn1_flag  = 0;
@@ -113,13 +116,14 @@ EC_GROUP *EC_GROUP_new(const EC_METHOD *meth)
        ret->seed = NULL;
        ret->seed_len = 0;
 
-       if (!meth->group_init(ret))
-               {
-               OPENSSL_free(ret);
-               return NULL;
-               }
+       if (!meth->group_init(ret)) goto err;
        
        return ret;
+err:
+       if(ret->order) BN_free(ret->order);
+       if(ret->cofactor) BN_free(ret->cofactor);
+       OPENSSL_free(ret);
+       return NULL;
        }
 
 
@@ -132,10 +136,13 @@ void EC_GROUP_free(EC_GROUP *group)
 
        EC_EX_DATA_free_all_data(&group->extra_data);
 
+       if (group->mont_data)
+               BN_MONT_CTX_free(group->mont_data);
+
        if (group->generator != NULL)
                EC_POINT_free(group->generator);
-       BN_free(&group->order);
-       BN_free(&group->cofactor);
+       BN_free(group->order);
+       BN_free(group->cofactor);
 
        if (group->seed)
                OPENSSL_free(group->seed);
@@ -155,10 +162,13 @@ void EC_GROUP_clear_free(EC_GROUP *group)
 
        EC_EX_DATA_clear_free_all_data(&group->extra_data);
 
+       if (group->mont_data)
+               BN_MONT_CTX_free(group->mont_data);
+
        if (group->generator != NULL)
                EC_POINT_clear_free(group->generator);
-       BN_clear_free(&group->order);
-       BN_clear_free(&group->cofactor);
+       BN_clear_free(group->order);
+       BN_clear_free(group->cofactor);
 
        if (group->seed)
                {
@@ -200,6 +210,25 @@ int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
                        return 0;
                }
 
+       if (src->mont_data != NULL)
+               {
+               if (dest->mont_data == NULL)
+                       {
+                       dest->mont_data = BN_MONT_CTX_new();
+                       if (dest->mont_data == NULL) return 0;
+                       }
+               if (!BN_MONT_CTX_copy(dest->mont_data, src->mont_data)) return 0;
+               }
+       else
+               {
+               /* src->generator == NULL */
+               if (dest->mont_data != NULL)
+                       {
+                       BN_MONT_CTX_free(dest->mont_data);
+                       dest->mont_data = NULL;
+                       }
+               }
+
        if (src->generator != NULL)
                {
                if (dest->generator == NULL)
@@ -219,8 +248,8 @@ int EC_GROUP_copy(EC_GROUP *dest, const EC_GROUP *src)
                        }
                }
 
-       if (!BN_copy(&dest->order, &src->order)) return 0;
-       if (!BN_copy(&dest->cofactor, &src->cofactor)) return 0;
+       if (!BN_copy(dest->order, src->order)) return 0;
+       if (!BN_copy(dest->cofactor, src->cofactor)) return 0;
 
        dest->curve_name = src->curve_name;
        dest->asn1_flag  = src->asn1_flag;
@@ -300,14 +329,19 @@ int EC_GROUP_set_generator(EC_GROUP *group, const EC_POINT *generator, const BIG
        if (!EC_POINT_copy(group->generator, generator)) return 0;
 
        if (order != NULL)
-               { if (!BN_copy(&group->order, order)) return 0; }       
+               { if (!BN_copy(group->order, order)) return 0; }
        else
-               BN_zero(&group->order);
+               BN_zero(group->order);
 
        if (cofactor != NULL)
-               { if (!BN_copy(&group->cofactor, cofactor)) return 0; } 
+               { if (!BN_copy(group->cofactor, cofactor)) return 0; }
        else
-               BN_zero(&group->cofactor);
+               BN_zero(group->cofactor);
+
+       /* We ignore the return value because some groups have an order with
+        * factors of two, which makes the Montgomery setup fail.
+        * |group->mont_data| will be NULL in this case. */
+       ec_precompute_mont_data(group);
 
        return 1;
        }
@@ -318,10 +352,14 @@ const EC_POINT *EC_GROUP_get0_generator(const EC_GROUP *group)
        return group->generator;
        }
 
+BN_MONT_CTX *EC_GROUP_get_mont_data(const EC_GROUP *group)
+       {
+       return group->mont_data;
+       }
 
 int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
        {
-       if (!BN_copy(order, &group->order))
+       if (!BN_copy(order, group->order))
                return 0;
 
        return !BN_is_zero(order);
@@ -330,10 +368,10 @@ int EC_GROUP_get_order(const EC_GROUP *group, BIGNUM *order, BN_CTX *ctx)
 
 int EC_GROUP_get_cofactor(const EC_GROUP *group, BIGNUM *cofactor, BN_CTX *ctx)
        {
-       if (!BN_copy(cofactor, &group->cofactor))
+       if (!BN_copy(cofactor, group->cofactor))
                return 0;
 
-       return !BN_is_zero(&group->cofactor);
+       return !BN_is_zero(group->cofactor);
        }
 
 
@@ -945,7 +983,7 @@ int EC_POINT_dbl(const EC_GROUP *group, EC_POINT *r, const EC_POINT *a, BN_CTX *
 
 int EC_POINT_invert(const EC_GROUP *group, EC_POINT *a, BN_CTX *ctx)
        {
-       if (group->meth->dbl == 0)
+       if (group->meth->invert == 0)
                {
                ECerr(EC_F_EC_POINT_INVERT, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
                return 0;
@@ -1097,3 +1135,39 @@ int EC_GROUP_have_precompute_mult(const EC_GROUP *group)
        else
                return 0; /* cannot tell whether precomputation has been performed */
        }
+
+/* ec_precompute_mont_data sets |group->mont_data| from |group->order| and
+ * returns one on success. On error it returns zero. */
+int ec_precompute_mont_data(EC_GROUP *group)
+       {
+       BN_CTX *ctx = BN_CTX_new();
+       int ret = 0;
+
+       if (group->mont_data)
+               {
+               BN_MONT_CTX_free(group->mont_data);
+               group->mont_data = NULL;
+               }
+
+       if (ctx == NULL)
+               goto err;
+
+       group->mont_data = BN_MONT_CTX_new();
+       if (!group->mont_data)
+               goto err;
+
+       if (!BN_MONT_CTX_set(group->mont_data, group->order, ctx))
+               {
+               BN_MONT_CTX_free(group->mont_data);
+               group->mont_data = NULL;
+               goto err;
+               }
+
+       ret = 1;
+
+err:
+
+       if (ctx)
+               BN_CTX_free(ctx);
+       return ret;
+       }