Additional comment changes for reformat of 1.0.2
[openssl.git] / crypto / ec / ec_mult.c
index 2ebb2af7203341c44eed11b288acca6084457ab0..518525d85ac95339d9a64772538f974039e3098a 100644 (file)
@@ -3,7 +3,7 @@
  * Originally written by Bodo Moeller and Nils Larsch for the OpenSSL project.
  */
 /* ====================================================================
- * Copyright (c) 1998-2003 The OpenSSL Project.  All rights reserved.
+ * Copyright (c) 1998-2007 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
@@ -61,6 +61,8 @@
  * and contributed to the OpenSSL project.
  */
 
+#include <string.h>
+
 #include <openssl/err.h>
 
 #include "ec_lcl.h"
@@ -85,6 +87,7 @@ typedef struct ec_pre_comp_st {
        EC_POINT **points;     /* array with pre-calculated multiples of generator:
                                * 'num' pointers to EC_POINT objects followed by a NULL */
        size_t num;            /* numblocks * 2^(w-1) */
+       int references;
 } EC_PRE_COMP;
  
 /* functions to manage EC_PRE_COMP within the EC_GROUP extra_data framework */
@@ -101,75 +104,49 @@ static EC_PRE_COMP *ec_pre_comp_new(const EC_GROUP *group)
 
        ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
        if (!ret)
+               {
+               ECerr(EC_F_EC_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
                return ret;
+               }
        ret->group = group;
        ret->blocksize = 8; /* default */
        ret->numblocks = 0;
        ret->w = 4; /* default */
        ret->points = NULL;
        ret->num = 0;
+       ret->references = 1;
        return ret;
        }
 
 static void *ec_pre_comp_dup(void *src_)
        {
-       const EC_PRE_COMP *src = src_;
-       EC_PRE_COMP *ret = NULL;
+       EC_PRE_COMP *src = src_;
 
-       ret = ec_pre_comp_new(src->group);
-       if (!ret)
-               return ret;
-       ret->blocksize = src->blocksize;
-       ret->numblocks = src->numblocks;
-       ret->w = src->w;
-       ret->num = 0;
-
-       if (src->points)
-               {
-               EC_POINT **src_var, **dest_var;
-
-               ret->points = (EC_POINT **)OPENSSL_malloc((src->num + 1) * sizeof(EC_POINT *));
-               if (!ret->points)
-                       {
-                       ec_pre_comp_free(ret);
-                       return NULL;
-                       }
-
-               for (dest_var = ret->points, src_var = src->points; *src_var != NULL; src_var++, dest_var++)
-                       {
-                       *dest_var = EC_POINT_dup(*src_var, src->group);
-                       if (*dest_var == NULL)
-                               {
-                               ec_pre_comp_free(ret);
-                               return NULL;
-                               }
-                       ret->num++;
-                       }
+       /* no need to actually copy, these objects never change! */
 
-               ret->points[ret->num] = NULL;
-               if (ret->num != src->num)
-                       {
-                       ec_pre_comp_free(ret);
-                       ECerr(EC_F_EC_PRE_COMP_DUP, ERR_R_INTERNAL_ERROR);
-                       return NULL;
-                       }
-               }
+       CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
 
-       return ret;
+       return src_;
        }
 
 static void ec_pre_comp_free(void *pre_)
        {
+       int i;
        EC_PRE_COMP *pre = pre_;
 
        if (!pre)
                return;
+
+       i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
+       if (i > 0)
+               return;
+
        if (pre->points)
                {
-               EC_POINT **var;
+               EC_POINT **p;
 
-               for (var = pre->points; *var != NULL; var++)
-                       EC_POINT_free(*var);
+               for (p = pre->points; *p != NULL; p++)
+                       EC_POINT_free(*p);
                OPENSSL_free(pre->points);
                }
        OPENSSL_free(pre);
@@ -177,27 +154,36 @@ static void ec_pre_comp_free(void *pre_)
 
 static void ec_pre_comp_clear_free(void *pre_)
        {
+       int i;
        EC_PRE_COMP *pre = pre_;
 
        if (!pre)
                return;
+
+       i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
+       if (i > 0)
+               return;
+
        if (pre->points)
                {
                EC_POINT **p;
 
                for (p = pre->points; *p != NULL; p++)
+                       {
                        EC_POINT_clear_free(*p);
-               OPENSSL_cleanse(pre->points, sizeof pre->points);
+                       OPENSSL_cleanse(p, sizeof *p);
+                       }
                OPENSSL_free(pre->points);
                }
-       OPENSSL_cleanse(pre, sizeof pre);
+       OPENSSL_cleanse(pre, sizeof *pre);
        OPENSSL_free(pre);
        }
 
 
 
 
-/* Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
+/*-
+ * Determine the modified width-(w+1) Non-Adjacent Form (wNAF) of 'scalar'.
  * This is an array  r[]  of values that are either zero or odd with an
  * absolute value less than  2^w  satisfying
  *     scalar = \sum_j r[j]*2^j
@@ -214,6 +200,19 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
        int bit, next_bit, mask;
        size_t len = 0, j;
        
+       if (BN_is_zero(scalar))
+               {
+               r = OPENSSL_malloc(1);
+               if (!r)
+                       {
+                       ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
+                       goto err;
+                       }
+               r[0] = 0;
+               *ret_len = 1;
+               return r;
+               }
+               
        if (w <= 0 || w > 7) /* 'signed char' can represent integers with absolute values less than 2^7 */
                {
                ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
@@ -223,20 +222,24 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
        next_bit = bit << 1; /* at most 256 */
        mask = next_bit - 1; /* at most 255 */
 
-       if (BN_get_sign(scalar))
+       if (BN_is_negative(scalar))
                {
                sign = -1;
                }
 
+       if (scalar->d == NULL || scalar->top == 0)
+               {
+               ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
+               goto err;
+               }
+
        len = BN_num_bits(scalar);
        r = OPENSSL_malloc(len + 1); /* modified wNAF may be one digit longer than binary representation
                                      * (*ret_len will be set to the actual length, i.e. at most
                                      * BN_num_bits(scalar) + 1) */
-       if (r == NULL) goto err;
-
-       if (scalar->d == NULL || scalar->top == 0)
+       if (r == NULL)
                {
-               ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
+               ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
                goto err;
                }
        window_val = scalar->d[0] & mask;
@@ -327,14 +330,16 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
  *       (thus the boundaries should be increased)
  */
 #define EC_window_bits_for_scalar_size(b) \
-               ((b) >= 2000 ? 6 : \
-                (b) >=  800 ? 5 : \
-                (b) >=  300 ? 4 : \
-                (b) >=   70 ? 3 : \
-                (b) >=   20 ? 2 : \
-                 1)
-
-/* Compute
+               ((size_t) \
+                ((b) >= 2000 ? 6 : \
+                 (b) >=  800 ? 5 : \
+                 (b) >=  300 ? 4 : \
+                 (b) >=   70 ? 3 : \
+                 (b) >=   20 ? 2 : \
+                 1))
+
+/*-
+ * Compute
  *      \sum scalars[i]*points[i],
  * also including
  *      scalar*generator
@@ -344,7 +349,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        size_t num, const EC_POINT *points[], const BIGNUM *scalars[], BN_CTX *ctx)
        {
        BN_CTX *new_ctx = NULL;
-       EC_POINT *generator = NULL;
+       const EC_POINT *generator = NULL;
        EC_POINT *tmp = NULL;
        size_t totalnum;
        size_t blocksize = 0, numblocks = 0; /* for wNAF splitting */
@@ -361,7 +366,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        EC_POINT **val = NULL; /* precomputation */
        EC_POINT **v;
        EC_POINT ***val_sub = NULL; /* pointers to sub-arrays of 'val' or 'pre_comp->points' */
-       EC_PRE_COMP *pre_comp = NULL;
+       const EC_PRE_COMP *pre_comp = NULL;
        int num_scalar = 0; /* flag: will be set to 1 if 'scalar' must be treated like other scalars,
                             * i.e. precomputation is not available */
        int ret = 0;
@@ -404,7 +409,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                
                /* look if we can use precomputed multiples of generator */
 
-               pre_comp = EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
+               pre_comp = EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
 
                if (pre_comp && pre_comp->numblocks && (EC_POINT_cmp(group, generator, pre_comp->points[0], ctx) == 0))
                        {
@@ -418,7 +423,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                        if (numblocks > pre_comp->numblocks)
                                numblocks = pre_comp->numblocks;
 
-                       pre_points_per_block = 1u << (pre_comp->w - 1);
+                       pre_points_per_block = (size_t)1 << (pre_comp->w - 1);
 
                        /* check that pre_comp looks sane */
                        if (pre_comp->num != (pre_comp->numblocks * pre_points_per_block))
@@ -442,11 +447,15 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        wNAF_len = OPENSSL_malloc(totalnum * sizeof wNAF_len[0]);
        wNAF     = OPENSSL_malloc((totalnum + 1) * sizeof wNAF[0]); /* includes space for pivot */
        val_sub  = OPENSSL_malloc(totalnum * sizeof val_sub[0]);
-                
+
+       /* Ensure wNAF is initialised in case we end up going to err */
+       if (wNAF) wNAF[0] = NULL;       /* preliminary pivot */
+
        if (!wsize || !wNAF_len || !wNAF || !val_sub)
+               {
+               ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
                goto err;
-
-       wNAF[0] = NULL; /* preliminary pivot */
+               }
 
        /* num_val will be the total number of temporarily precomputed points */
        num_val = 0;
@@ -457,7 +466,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
 
                bits = i < num ? BN_num_bits(scalars[i]) : BN_num_bits(scalar);
                wsize[i] = EC_window_bits_for_scalar_size(bits);
-               num_val += 1u << (wsize[i] - 1);
+               num_val += (size_t)1 << (wsize[i] - 1);
                wNAF[i + 1] = NULL; /* make sure we always have a pivot */
                wNAF[i] = compute_wNAF((i < num ? scalars[i] : scalar), wsize[i], &wNAF_len[i]);
                if (wNAF[i] == NULL)
@@ -557,6 +566,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                                        wNAF[i] = OPENSSL_malloc(wNAF_len[i]);
                                        if (wNAF[i] == NULL)
                                                {
+                                               ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
                                                OPENSSL_free(tmp_wNAF);
                                                goto err;
                                                }
@@ -583,7 +593,11 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
         * 'val_sub[i]' is a pointer to the subarray for the i-th point,
         * or to a subarray of 'pre_comp->points' if we already have precomputation. */
        val = OPENSSL_malloc((num_val + 1) * sizeof val[0]);
-       if (val == NULL) goto err;
+       if (val == NULL)
+               {
+               ECerr(EC_F_EC_WNAF_MUL, ERR_R_MALLOC_FAILURE);
+               goto err;
+               }
        val[num_val] = NULL; /* pivot element */
 
        /* allocate points for precomputation */
@@ -591,7 +605,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        for (i = 0; i < num + num_scalar; i++)
                {
                val_sub[i] = v;
-               for (j = 0; j < (1u << (wsize[i] - 1)); j++)
+               for (j = 0; j < ((size_t)1 << (wsize[i] - 1)); j++)
                        {
                        *v = EC_POINT_new(group);
                        if (*v == NULL) goto err;
@@ -607,7 +621,8 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        if (!(tmp = EC_POINT_new(group)))
                goto err;
 
-       /* prepare precomputed values:
+       /*-
+        * prepare precomputed values:
         *    val_sub[i][0] :=     points[i]
         *    val_sub[i][1] := 3 * points[i]
         *    val_sub[i][2] := 5 * points[i]
@@ -627,7 +642,7 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
                if (wsize[i] > 1)
                        {
                        if (!EC_POINT_dbl(group, tmp, val_sub[i][0], ctx)) goto err;
-                       for (j = 1; j < (1u << (wsize[i] - 1)); j++)
+                       for (j = 1; j < ((size_t)1 << (wsize[i] - 1)); j++)
                                {
                                if (!EC_POINT_add(group, val_sub[i][j], val_sub[i][j - 1], tmp, ctx)) goto err;
                                }
@@ -732,7 +747,8 @@ int ec_wNAF_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *scalar,
        }
 
 
-/* ec_wNAF_precompute_mult()
+/*-
+ * ec_wNAF_precompute_mult()
  * creates an EC_PRE_COMP object with preprecomputed multiples of the generator
  * for use with wNAF splitting as implemented in ec_wNAF_mul().
  * 
@@ -759,13 +775,14 @@ int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
        BIGNUM *order;
        size_t i, bits, w, pre_points_per_block, blocksize, numblocks, num;
        EC_POINT **points = NULL;
-       EC_PRE_COMP *pre_comp, *new_pre_comp = NULL;
+       EC_PRE_COMP *pre_comp;
        int ret = 0;
 
-       pre_comp = EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
-       if (pre_comp == NULL)
-               if ((pre_comp = new_pre_comp = ec_pre_comp_new(group)) == NULL)
-                       return 0;
+       /* if there is an old EC_PRE_COMP object, throw it away */
+       EC_EX_DATA_free_data(&group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free);
+
+       if ((pre_comp = ec_pre_comp_new(group)) == NULL)
+               return 0;
 
        generator = EC_GROUP_get0_generator(group);
        if (generator == NULL)
@@ -810,7 +827,7 @@ int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
 
        numblocks = (bits + blocksize - 1) / blocksize; /* max. number of blocks to use for wNAF splitting */
        
-       pre_points_per_block = 1u << (w - 1);
+       pre_points_per_block = (size_t)1 << (w - 1);
        num = pre_points_per_block * numblocks; /* number of points to compute and store */
 
        points = OPENSSL_malloc(sizeof (EC_POINT*)*(num + 1));
@@ -886,32 +903,23 @@ int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
        pre_comp->blocksize = blocksize;
        pre_comp->numblocks = numblocks;
        pre_comp->w = w;
-       if (pre_comp->points)
-               {
-               EC_POINT **p;
-
-               for (p = pre_comp->points; *p != NULL; p++)
-                       EC_POINT_free(*p);
-               OPENSSL_free(pre_comp->points);
-               }
        pre_comp->points = points;
        points = NULL;
        pre_comp->num = num;
 
-       if (new_pre_comp)
-               {
-               if (!EC_GROUP_set_extra_data(group, new_pre_comp, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
-                       goto err;
-               new_pre_comp = NULL;
-               }
+       if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
+               ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free))
+               goto err;
+       pre_comp = NULL;
 
        ret = 1;
  err:
-       BN_CTX_end(ctx);
+       if (ctx != NULL)
+               BN_CTX_end(ctx);
        if (new_ctx != NULL)
                BN_CTX_free(new_ctx);
-       if (new_pre_comp)
-               ec_pre_comp_free(new_pre_comp);
+       if (pre_comp)
+               ec_pre_comp_free(pre_comp);
        if (points)
                {
                EC_POINT **p;
@@ -930,7 +938,7 @@ int ec_wNAF_precompute_mult(EC_GROUP *group, BN_CTX *ctx)
 
 int ec_wNAF_have_precompute_mult(const EC_GROUP *group)
        {
-       if (EC_GROUP_get_extra_data(group, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free) != NULL)
+       if (EC_EX_DATA_get_data(group->extra_data, ec_pre_comp_dup, ec_pre_comp_free, ec_pre_comp_clear_free) != NULL)
                return 1;
        else
                return 0;