ecp_nistputil.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /* crypto/ec/ecp_nistputil.c */
  2. /*
  3. * Written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /* Copyright 2011 Google Inc.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. *
  9. * you may not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #include <openssl/opensslconf.h>
  21. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  22. /*
  23. * Common utility functions for ecp_nistp224.c, ecp_nistp256.c, ecp_nistp521.c.
  24. */
  25. # include <stddef.h>
  26. # include "ec_lcl.h"
  27. /*
  28. * Convert an array of points into affine coordinates. (If the point at
  29. * infinity is found (Z = 0), it remains unchanged.) This function is
  30. * essentially an equivalent to EC_POINTs_make_affine(), but works with the
  31. * internal representation of points as used by ecp_nistp###.c rather than
  32. * with (BIGNUM-based) EC_POINT data structures. point_array is the
  33. * input/output buffer ('num' points in projective form, i.e. three
  34. * coordinates each), based on an internal representation of field elements
  35. * of size 'felem_size'. tmp_felems needs to point to a temporary array of
  36. * 'num'+1 field elements for storage of intermediate values.
  37. */
  38. void ec_GFp_nistp_points_make_affine_internal(size_t num, void *point_array,
  39. size_t felem_size,
  40. void *tmp_felems,
  41. void (*felem_one) (void *out),
  42. int (*felem_is_zero) (const void
  43. *in),
  44. void (*felem_assign) (void *out,
  45. const void
  46. *in),
  47. void (*felem_square) (void *out,
  48. const void
  49. *in),
  50. void (*felem_mul) (void *out,
  51. const void
  52. *in1,
  53. const void
  54. *in2),
  55. void (*felem_inv) (void *out,
  56. const void
  57. *in),
  58. void (*felem_contract) (void
  59. *out,
  60. const
  61. void
  62. *in))
  63. {
  64. int i = 0;
  65. # define tmp_felem(I) (&((char *)tmp_felems)[(I) * felem_size])
  66. # define X(I) (&((char *)point_array)[3*(I) * felem_size])
  67. # define Y(I) (&((char *)point_array)[(3*(I) + 1) * felem_size])
  68. # define Z(I) (&((char *)point_array)[(3*(I) + 2) * felem_size])
  69. if (!felem_is_zero(Z(0)))
  70. felem_assign(tmp_felem(0), Z(0));
  71. else
  72. felem_one(tmp_felem(0));
  73. for (i = 1; i < (int)num; i++) {
  74. if (!felem_is_zero(Z(i)))
  75. felem_mul(tmp_felem(i), tmp_felem(i - 1), Z(i));
  76. else
  77. felem_assign(tmp_felem(i), tmp_felem(i - 1));
  78. }
  79. /*
  80. * Now each tmp_felem(i) is the product of Z(0) .. Z(i), skipping any
  81. * zero-valued factors: if Z(i) = 0, we essentially pretend that Z(i) = 1
  82. */
  83. felem_inv(tmp_felem(num - 1), tmp_felem(num - 1));
  84. for (i = num - 1; i >= 0; i--) {
  85. if (i > 0)
  86. /*
  87. * tmp_felem(i-1) is the product of Z(0) .. Z(i-1), tmp_felem(i)
  88. * is the inverse of the product of Z(0) .. Z(i)
  89. */
  90. /* 1/Z(i) */
  91. felem_mul(tmp_felem(num), tmp_felem(i - 1), tmp_felem(i));
  92. else
  93. felem_assign(tmp_felem(num), tmp_felem(0)); /* 1/Z(0) */
  94. if (!felem_is_zero(Z(i))) {
  95. if (i > 0)
  96. /*
  97. * For next iteration, replace tmp_felem(i-1) by its inverse
  98. */
  99. felem_mul(tmp_felem(i - 1), tmp_felem(i), Z(i));
  100. /*
  101. * Convert point (X, Y, Z) into affine form (X/(Z^2), Y/(Z^3), 1)
  102. */
  103. felem_square(Z(i), tmp_felem(num)); /* 1/(Z^2) */
  104. felem_mul(X(i), X(i), Z(i)); /* X/(Z^2) */
  105. felem_mul(Z(i), Z(i), tmp_felem(num)); /* 1/(Z^3) */
  106. felem_mul(Y(i), Y(i), Z(i)); /* Y/(Z^3) */
  107. felem_contract(X(i), X(i));
  108. felem_contract(Y(i), Y(i));
  109. felem_one(Z(i));
  110. } else {
  111. if (i > 0)
  112. /*
  113. * For next iteration, replace tmp_felem(i-1) by its inverse
  114. */
  115. felem_assign(tmp_felem(i - 1), tmp_felem(i));
  116. }
  117. }
  118. }
  119. /*-
  120. * This function looks at 5+1 scalar bits (5 current, 1 adjacent less
  121. * significant bit), and recodes them into a signed digit for use in fast point
  122. * multiplication: the use of signed rather than unsigned digits means that
  123. * fewer points need to be precomputed, given that point inversion is easy
  124. * (a precomputed point dP makes -dP available as well).
  125. *
  126. * BACKGROUND:
  127. *
  128. * Signed digits for multiplication were introduced by Booth ("A signed binary
  129. * multiplication technique", Quart. Journ. Mech. and Applied Math., vol. IV,
  130. * pt. 2 (1951), pp. 236-240), in that case for multiplication of integers.
  131. * Booth's original encoding did not generally improve the density of nonzero
  132. * digits over the binary representation, and was merely meant to simplify the
  133. * handling of signed factors given in two's complement; but it has since been
  134. * shown to be the basis of various signed-digit representations that do have
  135. * further advantages, including the wNAF, using the following general approach:
  136. *
  137. * (1) Given a binary representation
  138. *
  139. * b_k ... b_2 b_1 b_0,
  140. *
  141. * of a nonnegative integer (b_k in {0, 1}), rewrite it in digits 0, 1, -1
  142. * by using bit-wise subtraction as follows:
  143. *
  144. * b_k b_(k-1) ... b_2 b_1 b_0
  145. * - b_k ... b_3 b_2 b_1 b_0
  146. * -------------------------------------
  147. * s_k b_(k-1) ... s_3 s_2 s_1 s_0
  148. *
  149. * A left-shift followed by subtraction of the original value yields a new
  150. * representation of the same value, using signed bits s_i = b_(i+1) - b_i.
  151. * This representation from Booth's paper has since appeared in the
  152. * literature under a variety of different names including "reversed binary
  153. * form", "alternating greedy expansion", "mutual opposite form", and
  154. * "sign-alternating {+-1}-representation".
  155. *
  156. * An interesting property is that among the nonzero bits, values 1 and -1
  157. * strictly alternate.
  158. *
  159. * (2) Various window schemes can be applied to the Booth representation of
  160. * integers: for example, right-to-left sliding windows yield the wNAF
  161. * (a signed-digit encoding independently discovered by various researchers
  162. * in the 1990s), and left-to-right sliding windows yield a left-to-right
  163. * equivalent of the wNAF (independently discovered by various researchers
  164. * around 2004).
  165. *
  166. * To prevent leaking information through side channels in point multiplication,
  167. * we need to recode the given integer into a regular pattern: sliding windows
  168. * as in wNAFs won't do, we need their fixed-window equivalent -- which is a few
  169. * decades older: we'll be using the so-called "modified Booth encoding" due to
  170. * MacSorley ("High-speed arithmetic in binary computers", Proc. IRE, vol. 49
  171. * (1961), pp. 67-91), in a radix-2^5 setting. That is, we always combine five
  172. * signed bits into a signed digit:
  173. *
  174. * s_(4j + 4) s_(4j + 3) s_(4j + 2) s_(4j + 1) s_(4j)
  175. *
  176. * The sign-alternating property implies that the resulting digit values are
  177. * integers from -16 to 16.
  178. *
  179. * Of course, we don't actually need to compute the signed digits s_i as an
  180. * intermediate step (that's just a nice way to see how this scheme relates
  181. * to the wNAF): a direct computation obtains the recoded digit from the
  182. * six bits b_(4j + 4) ... b_(4j - 1).
  183. *
  184. * This function takes those five bits as an integer (0 .. 63), writing the
  185. * recoded digit to *sign (0 for positive, 1 for negative) and *digit (absolute
  186. * value, in the range 0 .. 8). Note that this integer essentially provides the
  187. * input bits "shifted to the left" by one position: for example, the input to
  188. * compute the least significant recoded digit, given that there's no bit b_-1,
  189. * has to be b_4 b_3 b_2 b_1 b_0 0.
  190. *
  191. */
  192. void ec_GFp_nistp_recode_scalar_bits(unsigned char *sign,
  193. unsigned char *digit, unsigned char in)
  194. {
  195. unsigned char s, d;
  196. s = ~((in >> 5) - 1); /* sets all bits to MSB(in), 'in' seen as
  197. * 6-bit value */
  198. d = (1 << 6) - in - 1;
  199. d = (d & s) | (in & ~s);
  200. d = (d >> 1) + (d & 1);
  201. *sign = s & 1;
  202. *digit = d;
  203. }
  204. #else
  205. static void *dummy = &dummy;
  206. #endif