Additional comment changes for reformat of 1.0.0
[openssl.git] / crypto / ec / ec_mult.c
index 2ba173ef36422ad1c715fe3ebd5b0dc9b17cb03d..518525d85ac95339d9a64772538f974039e3098a 100644 (file)
@@ -169,18 +169,21 @@ static void ec_pre_comp_clear_free(void *pre_)
                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
@@ -224,6 +227,12 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
                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
@@ -233,12 +242,6 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
                ECerr(EC_F_COMPUTE_WNAF, ERR_R_MALLOC_FAILURE);
                goto err;
                }
-
-       if (scalar->d == NULL || scalar->top == 0)
-               {
-               ECerr(EC_F_COMPUTE_WNAF, ERR_R_INTERNAL_ERROR);
-               goto err;
-               }
        window_val = scalar->d[0] & mask;
        j = 0;
        while ((window_val != 0) || (j + w + 1 < len)) /* if j+w+1 >= len, window_val will not increase */
@@ -335,7 +338,8 @@ static signed char *compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len)
                  (b) >=   20 ? 2 : \
                  1))
 
-/* Compute
+/*-
+ * Compute
  *      \sum scalars[i]*points[i],
  * also including
  *      scalar*generator
@@ -419,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))
@@ -443,15 +447,16 @@ 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;
 
@@ -461,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)
@@ -600,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;
@@ -616,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]
@@ -636,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;
                                }
@@ -741,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().
  * 
@@ -820,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));