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