fix truncation of integers on 32bit AIX
[openssl.git] / crypto / ec / curve448 / curve448.c
index eb8752db38522286e88bb44e841a9ab6a1b08abb..6236ad66383deeb619581b3ef4fa224ebf835788 100644 (file)
@@ -2,7 +2,7 @@
  * Copyright 2017-2018 The OpenSSL Project Authors. All Rights Reserved.
  * Copyright 2015-2016 Cryptography Research, Inc.
  *
- * Licensed under the OpenSSL license (the "License").  You may not use
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
  * in the file LICENSE in the source distribution or at
  * https://www.openssl.org/source/license.html
 
 #define COFACTOR 4
 
-/* Comb config: number of combs, n, t, s. */
-#define COMBS_N 5
-#define COMBS_T 5
-#define COMBS_S 18
 #define C448_WNAF_FIXED_TABLE_BITS 5
 #define C448_WNAF_VAR_TABLE_BITS 3
 
-static const int EDWARDS_D = -39081;
+#define EDWARDS_D       (-39081)
+
 static const curve448_scalar_t precomputed_scalarmul_adjustment = {
     {
         {
-            SC_LIMB(0xc873d6d54a7bb0cf), SC_LIMB(0xe933d8d723a70aad),
-            SC_LIMB(0xbb124b65129c96fd), SC_LIMB(0x00000008335dc163)
+            SC_LIMB(0xc873d6d54a7bb0cfULL), SC_LIMB(0xe933d8d723a70aadULL),
+            SC_LIMB(0xbb124b65129c96fdULL), SC_LIMB(0x00000008335dc163ULL)
         }
     }
 };
 
-#define TWISTED_D ((EDWARDS_D)-1)
+#define TWISTED_D (EDWARDS_D - 1)
 
 #define WBITS C448_WORD_BITS   /* NB this may be different from ARCH_WORD_BITS */
 
-/* Projective Niels coordinates */
-typedef struct {
-    gf a, b, c;
-} niels_s, niels_t[1];
-typedef struct {
-    niels_t n;
-    gf z;
-} VECTOR_ALIGNED pniels_t[1];
-
-/* Precomputed base */
-struct curve448_precomputed_s {
-    niels_t table[COMBS_N << (COMBS_T - 1)];
-};
-
-extern const gf curve448_precomputed_base_as_fe[];
-const curve448_precomputed_s *curve448_precomputed_base =
-    (const curve448_precomputed_s *)&curve448_precomputed_base_as_fe;
-
 /* Inverse. */
 static void gf_invert(gf y, const gf x, int assert_nonzero)
 {
     mask_t ret;
-
     gf t1, t2;
+
     gf_sqr(t1, x);              /* o^2 */
     ret = gf_isr(t2, t1);       /* +-1/sqrt(o^2) = +-1/o */
     (void)ret;
@@ -268,10 +247,9 @@ void curve448_precomputed_scalarmul(curve448_point_t out,
             for (k = 0; k < t; k++) {
                 unsigned int bit = (i - 1) + s * (k + j * t);
 
-                if (bit < C448_SCALAR_BITS) {
+                if (bit < C448_SCALAR_BITS)
                     tab |=
                         (scalar1x->limb[bit / WBITS] >> (bit % WBITS) & 1) << k;
-                }
             }
 
             invert = (tab >> (t - 1)) - 1;
@@ -282,11 +260,10 @@ void curve448_precomputed_scalarmul(curve448_point_t out,
                                        1 << (t - 1), tab);
 
             cond_neg_niels(ni, invert);
-            if ((i != s) || j != 0) {
+            if ((i != s) || j != 0)
                 add_niels_to_pt(out, ni, j == n - 1 && i != 1);
-            } else {
+            else
                 niels_to_pt(out, ni);
-            }
         }
     }
 
@@ -408,7 +385,7 @@ c448_error_t x448_int(uint8_t out[X_PUBLIC_BYTES],
     mask_t swap = 0;
     mask_t nz;
 
-    ignore_result(gf_deserialize(x1, base, 1, 0));
+    (void)gf_deserialize(x1, base, 1, 0);
     gf_copy(x2, ONE);
     gf_copy(z2, ZERO);
     gf_copy(x3, x1);
@@ -432,25 +409,30 @@ c448_error_t x448_int(uint8_t out[X_PUBLIC_BYTES],
         gf_cond_swap(z2, z3, swap);
         swap = k_t;
 
-        gf_add_nr(t1, x2, z2);  /* A = x2 + z2 *//* 2+e */
-        gf_sub_nr(t2, x2, z2);  /* B = x2 - z2 *//* 3+e */
-        gf_sub_nr(z2, x3, z3);  /* D = x3 - z3 *//* 3+e */
+        /*
+         * The "_nr" below skips coefficient reduction. In the following
+         * comments, "2+e" is saying that the coefficients are at most 2+epsilon
+         * times the reduction limit.
+         */
+        gf_add_nr(t1, x2, z2);  /* A = x2 + z2 */ /* 2+e */
+        gf_sub_nr(t2, x2, z2);  /* B = x2 - z2 */ /* 3+e */
+        gf_sub_nr(z2, x3, z3);  /* D = x3 - z3 */ /* 3+e */
         gf_mul(x2, t1, z2);     /* DA */
-        gf_add_nr(z2, z3, x3);  /* C = x3 + z3 *//* 2+e */
+        gf_add_nr(z2, z3, x3);  /* C = x3 + z3 */ /* 2+e */
         gf_mul(x3, t2, z2);     /* CB */
-        gf_sub_nr(z3, x2, x3);  /* DA-CB *//* 3+e */
+        gf_sub_nr(z3, x2, x3);  /* DA-CB */ /* 3+e */
         gf_sqr(z2, z3);         /* (DA-CB)^2 */
         gf_mul(z3, x1, z2);     /* z3 = x1(DA-CB)^2 */
-        gf_add_nr(z2, x2, x3);  /* (DA+CB) *//* 2+e */
+        gf_add_nr(z2, x2, x3);  /* (DA+CB) */ /* 2+e */
         gf_sqr(x3, z2);         /* x3 = (DA+CB)^2 */
 
         gf_sqr(z2, t1);         /* AA = A^2 */
         gf_sqr(t1, t2);         /* BB = B^2 */
         gf_mul(x2, z2, t1);     /* x2 = AA*BB */
-        gf_sub_nr(t2, z2, t1);  /* E = AA-BB *//* 3+e */
+        gf_sub_nr(t2, z2, t1);  /* E = AA-BB */ /* 3+e */
 
         gf_mulw(t1, t2, -EDWARDS_D); /* E*-d = a24*E */
-        gf_add_nr(t1, t1, z2);  /* AA + a24*E *//* 2+e */
+        gf_add_nr(t1, t1, z2);  /* AA + a24*E */ /* 2+e */
         gf_mul(z2, t2, t1);     /* z2 = E(AA+a24*E) */
     }
 
@@ -505,9 +487,9 @@ void x448_derive_public_key(uint8_t out[X_PUBLIC_BYTES],
     curve448_scalar_decode_long(the_scalar, scalar2, sizeof(scalar2));
 
     /* Compensate for the encoding ratio */
-    for (i = 1; i < X448_ENCODE_RATIO; i <<= 1) {
+    for (i = 1; i < X448_ENCODE_RATIO; i <<= 1)
         curve448_scalar_halve(the_scalar, the_scalar);
-    }
+
     curve448_precomputed_scalarmul(p, curve448_precomputed_base, the_scalar);
     curve448_point_mul_by_ratio_and_encode_like_x448(out, p);
     curve448_point_destroy(p);
@@ -518,7 +500,7 @@ struct smvt_control {
     int power, addend;
 };
 
-#if defined(__GNUC__) || defined(__clang__)
+#if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))
 # define NUMTRAILINGZEROS      __builtin_ctz
 #else
 # define NUMTRAILINGZEROS      numtrailingzeros
@@ -586,7 +568,7 @@ static int recode_wnaf(struct smvt_control *control,
         if (w < (C448_SCALAR_BITS - 1) / 16 + 1) {
             /* Refill the 16 high bits of current */
             current += (uint32_t)((scalar->limb[w / B_OVER_16]
-                       >> (16 * (w %  B_OVER_16))) << 16);
+                       >> (16 * (w % B_OVER_16))) << 16);
         }
 
         while (current & 0xFFFF) {
@@ -597,7 +579,7 @@ static int recode_wnaf(struct smvt_control *control,
             assert(position >= 0);
             if (odd & (1 << (table_bits + 1)))
                 delta -= (1 << (table_bits + 1));
-            current -= delta << pos;
+            current -= delta * (1 << pos);
             control[position].power = pos + 16 * (w - 1);
             control[position].addend = delta;
             position--;
@@ -642,10 +624,6 @@ static void prepare_wnaf_table(pniels_t * output,
     OPENSSL_cleanse(twop, sizeof(twop));
 }
 
-extern const gf curve448_precomputed_wnaf_as_fe[];
-static const niels_t *curve448_wnaf_base =
-    (const niels_t *)curve448_precomputed_wnaf_as_fe;
-
 void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
                                                const curve448_scalar_t scalar1,
                                                const curve448_point_t base2,
@@ -668,7 +646,8 @@ void curve448_base_double_scalarmul_non_secret(curve448_point_t combo,
     if (i < 0) {
         curve448_point_copy(combo, curve448_point_identity);
         return;
-    } else if (i > control_pre[0].power) {
+    }
+    if (i > control_pre[0].power) {
         pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
         contv++;
     } else if (i == control_pre[0].power && i >= 0) {