ecp_nistputil.c 9.8 KB

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