aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/ec/ecp_nistputil.c
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/ec/ecp_nistputil.c')
-rw-r--r--crypto/ec/ecp_nistputil.c191
1 files changed, 106 insertions, 85 deletions
diff --git a/crypto/ec/ecp_nistputil.c b/crypto/ec/ecp_nistputil.c
index c8140c807fb0..8ba2a25e9380 100644
--- a/crypto/ec/ecp_nistputil.c
+++ b/crypto/ec/ecp_nistputil.c
@@ -25,89 +25,108 @@
* Common utility functions for ecp_nistp224.c, ecp_nistp256.c, ecp_nistp521.c.
*/
-#include <stddef.h>
-#include "ec_lcl.h"
+# include <stddef.h>
+# include "ec_lcl.h"
-/* Convert an array of points into affine coordinates.
- * (If the point at infinity is found (Z = 0), it remains unchanged.)
- * This function is essentially an equivalent to EC_POINTs_make_affine(), but
- * works with the internal representation of points as used by ecp_nistp###.c
- * rather than with (BIGNUM-based) EC_POINT data structures.
- *
- * point_array is the input/output buffer ('num' points in projective form,
- * i.e. three coordinates each), based on an internal representation of
- * field elements of size 'felem_size'.
- *
- * tmp_felems needs to point to a temporary array of 'num'+1 field elements
- * for storage of intermediate values.
+/*
+ * Convert an array of points into affine coordinates. (If the point at
+ * infinity is found (Z = 0), it remains unchanged.) This function is
+ * essentially an equivalent to EC_POINTs_make_affine(), but works with the
+ * internal representation of points as used by ecp_nistp###.c rather than
+ * with (BIGNUM-based) EC_POINT data structures. point_array is the
+ * input/output buffer ('num' points in projective form, i.e. three
+ * coordinates each), based on an internal representation of field elements
+ * of size 'felem_size'. tmp_felems needs to point to a temporary array of
+ * 'num'+1 field elements for storage of intermediate values.
*/
void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
- size_t felem_size, void *tmp_felems,
- void (*felem_one)(void *out),
- int (*felem_is_zero)(const void *in),
- void (*felem_assign)(void *out, const void *in),
- void (*felem_square)(void *out, const void *in),
- void (*felem_mul)(void *out, const void *in1, const void *in2),
- void (*felem_inv)(void *out, const void *in),
- void (*felem_contract)(void *out, const void *in))
- {
- int i = 0;
+ size_t felem_size,
+ void *tmp_felems,
+ void (*felem_one) (void *out),
+ int (*felem_is_zero) (const void
+ *in),
+ void (*felem_assign) (void *out,
+ const void
+ *in),
+ void (*felem_square) (void *out,
+ const void
+ *in),
+ void (*felem_mul) (void *out,
+ const void
+ *in1,
+ const void
+ *in2),
+ void (*felem_inv) (void *out,
+ const void
+ *in),
+ void (*felem_contract) (void
+ *out,
+ const
+ void
+ *in))
+{
+ int i = 0;
-#define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
-#define X(I) (&((char *)point_array)[3*(I) * felem_size])
-#define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
-#define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
+# define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
+# define X(I) (&((char *)point_array)[3*(I) * felem_size])
+# define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
+# define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
- if (!felem_is_zero(Z(0)))
- felem_assign(tmp_felem(0), Z(0));
- else
- felem_one(tmp_felem(0));
- for (i = 1; i < (int)num; i++)
- {
- if (!felem_is_zero(Z(i)))
- felem_mul(tmp_felem(i), tmp_felem(i-1), Z(i));
- else
- felem_assign(tmp_felem(i), tmp_felem(i-1));
- }
- /* Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any zero-valued factors:
- * if Z(i) = 0, we essentially pretend that Z(i) = 1 */
+ if (!felem_is_zero(Z(0)))
+ felem_assign(tmp_felem(0), Z(0));
+ else
+ felem_one(tmp_felem(0));
+ for (i = 1; i < (int)num; i++) {
+ if (!felem_is_zero(Z(i)))
+ felem_mul(tmp_felem(i), tmp_felem(i - 1), Z(i));
+ else
+ felem_assign(tmp_felem(i), tmp_felem(i - 1));
+ }
+ /*
+ * Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any
+ * zero-valued factors: if Z(i) = 0, we essentially pretend that Z(i) = 1
+ */
- felem_inv(tmp_felem(num-1), tmp_felem(num-1));
- for (i = num - 1; i >= 0; i--)
- {
- if (i > 0)
- /* tmp_felem(i-1) is the product of Z(0) .. Z(i-1),
- * tmp_felem(i) is the inverse of the product of Z(0) .. Z(i)
- */
- felem_mul(tmp_felem(num), tmp_felem(i-1), tmp_felem(i)); /* 1/Z(i) */
- else
- felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
+ felem_inv(tmp_felem(num - 1), tmp_felem(num - 1));
+ for (i = num - 1; i >= 0; i--) {
+ if (i > 0)
+ /*
+ * tmp_felem(i-1) is the product of Z(0) .. Z(i-1), tmp_felem(i)
+ * is the inverse of the product of Z(0) .. Z(i)
+ */
+ /* 1/Z(i) */
+ felem_mul(tmp_felem(num), tmp_felem(i - 1), tmp_felem(i));
+ else
+ felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
- if (!felem_is_zero(Z(i)))
- {
- if (i > 0)
- /* For next iteration, replace tmp_felem(i-1) by its inverse */
- felem_mul(tmp_felem(i-1), tmp_felem(i), Z(i));
+ if (!felem_is_zero(Z(i))) {
+ if (i > 0)
+ /*
+ * For next iteration, replace tmp_felem(i-1) by its inverse
+ */
+ felem_mul(tmp_felem(i - 1), tmp_felem(i), Z(i));
- /* Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1) */
- felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
- felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
- felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
- felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
- felem_contract(X(i), X(i));
- felem_contract(Y(i), Y(i));
- felem_one(Z(i));
- }
- else
- {
- if (i > 0)
- /* For next iteration, replace tmp_felem(i-1) by its inverse */
- felem_assign(tmp_felem(i-1), tmp_felem(i));
- }
- }
- }
+ /*
+ * Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1)
+ */
+ felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
+ felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
+ felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
+ felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
+ felem_contract(X(i), X(i));
+ felem_contract(Y(i), Y(i));
+ felem_one(Z(i));
+ } else {
+ if (i > 0)
+ /*
+ * For next iteration, replace tmp_felem(i-1) by its inverse
+ */
+ felem_assign(tmp_felem(i - 1), tmp_felem(i));
+ }
+ }
+}
-/*
+/*-
* This function looks at 5+1 scalar bits (5 current, 1 adjacent less
* significant bit), and recodes them into a signed digit for use in fast point
* multiplication: the use of signed rather than unsigned digits means that
@@ -180,18 +199,20 @@ void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
* has to be b_4 b_3 b_2 b_1 b_0 0.
*
*/
-void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign, unsigned char *digit, unsigned char in)
- {
- unsigned char s, d;
+void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
+ unsigned char *digit, unsigned char in)
+{
+ unsigned char s, d;
- s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as 6-bit value */
- d = (1 << 6) - in - 1;
- d = (d & s) | (in & ~s);
- d = (d >> 1) + (d & 1);
+ s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
+ * 6-bit value */
+ d = (1 << 6) - in - 1;
+ d = (d & s) | (in & ~s);
+ d = (d >> 1) + (d & 1);
- *sign = s & 1;
- *digit = d;
- }
+ *sign = s & 1;
+ *digit = d;
+}
#else
-static void *dummy=&dummy;
+static void *dummy = &dummy;
#endif