X-Git-Url: https://git.openssl.org/?a=blobdiff_plain;f=crypto%2Fec%2Fecp_nistputil.c;h=82cca66b0dad4238a94c01d7270911c9262b785f;hb=fb82cbfe3da846d61e1d4c6d14bf7f4111cccbb2;hp=c65bb2d911ab41108d5b4e7ef2500a33c83b93cc;hpb=68d39f3ce6ff4f65170d94f7310b3f485f33328d;p=openssl.git diff --git a/crypto/ec/ecp_nistputil.c b/crypto/ec/ecp_nistputil.c index c65bb2d911..82cca66b0d 100644 --- a/crypto/ec/ecp_nistputil.c +++ b/crypto/ec/ecp_nistputil.c @@ -1,7 +1,12 @@ -/* crypto/ec/ecp_nistputil.c */ /* - * Written by Bodo Moeller for the OpenSSL project. + * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved. + * + * 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 */ + /* Copyright 2011 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,94 +24,114 @@ */ #include -#ifndef OPENSSL_NO_EC_NISTP_64_GCC_128 +#ifdef OPENSSL_NO_EC_NISTP_64_GCC_128 +NON_EMPTY_TRANSLATION_UNIT +#else /* * Common utility functions for ecp_nistp224.c, ecp_nistp256.c, ecp_nistp521.c. */ -#include -#include "ec_lcl.h" +# include +# 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) - */ - /* 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) */ + 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 @@ -181,18 +206,18 @@ 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; - } -#else -static void *dummy=&dummy; + *sign = s & 1; + *digit = d; +} #endif