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