]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecp_nistz256.c
Add ecp_nistz256.c to list of files skipped by openssl-format-source
[thirdparty/openssl.git] / crypto / ec / ecp_nistz256.c
CommitLineData
4d3fa06f
AP
1/******************************************************************************
2 * *
3 * Copyright 2014 Intel Corporation *
4 * *
5 * Licensed under the Apache License, Version 2.0 (the "License"); *
6 * you may not use this file except in compliance with the License. *
7 * You may obtain a copy of the License at *
8 * *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
10 * *
11 * Unless required by applicable law or agreed to in writing, software *
12 * distributed under the License is distributed on an "AS IS" BASIS, *
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14 * See the License for the specific language governing permissions and *
15 * limitations under the License. *
16 * *
17 ******************************************************************************
18 * *
19 * Developers and authors: *
20 * Shay Gueron (1, 2), and Vlad Krasnov (1) *
21 * (1) Intel Corporation, Israel Development Center *
22 * (2) University of Haifa *
23 * Reference: *
24 * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with *
25 * 256 Bit Primes" *
26 * *
27 ******************************************************************************/
28
29#include <string.h>
30
5784a521 31#include "internal/bn_int.h"
4d3fa06f
AP
32#include <openssl/err.h>
33#include <openssl/ec.h>
34#include "cryptlib.h"
35
36#include "ec_lcl.h"
37
38#if BN_BITS2 != 64
58d47cf0 39# define TOBN(hi,lo) lo,hi
4d3fa06f 40#else
58d47cf0 41# define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo)
4d3fa06f
AP
42#endif
43
44#if defined(__GNUC__)
58d47cf0 45# define ALIGN32 __attribute((aligned(32)))
4d3fa06f 46#elif defined(_MSC_VER)
58d47cf0 47# define ALIGN32 __declspec(align(32))
4d3fa06f
AP
48#else
49# define ALIGN32
50#endif
51
58d47cf0
AP
52#define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N)
53#define P256_LIMBS (256/BN_BITS2)
4d3fa06f
AP
54
55typedef unsigned short u16;
56
57typedef struct {
58 BN_ULONG X[P256_LIMBS];
59 BN_ULONG Y[P256_LIMBS];
60 BN_ULONG Z[P256_LIMBS];
61} P256_POINT;
62
63typedef struct {
64 BN_ULONG X[P256_LIMBS];
65 BN_ULONG Y[P256_LIMBS];
66} P256_POINT_AFFINE;
67
68typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
69
70/* structure for precomputed multiples of the generator */
71typedef struct ec_pre_comp_st {
72 const EC_GROUP *group; /* Parent EC_GROUP object */
73 size_t w; /* Window size */
74 /* Constant time access to the X and Y coordinates of the pre-computed,
75 * generator multiplies, in the Montgomery domain. Pre-calculated
76 * multiplies are stored in affine form. */
77 PRECOMP256_ROW *precomp;
78 void *precomp_storage;
79 int references;
80} EC_PRE_COMP;
81
82/* Functions implemented in assembly */
83/* Modular mul by 2: res = 2*a mod P */
84void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
85 const BN_ULONG a[P256_LIMBS]);
86/* Modular div by 2: res = a/2 mod P */
87void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
88 const BN_ULONG a[P256_LIMBS]);
89/* Modular mul by 3: res = 3*a mod P */
90void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
91 const BN_ULONG a[P256_LIMBS]);
92/* Modular add: res = a+b mod P */
93void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
94 const BN_ULONG a[P256_LIMBS],
95 const BN_ULONG b[P256_LIMBS]);
96/* Modular sub: res = a-b mod P */
97void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
98 const BN_ULONG a[P256_LIMBS],
99 const BN_ULONG b[P256_LIMBS]);
100/* Modular neg: res = -a mod P */
101void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
102/* Montgomery mul: res = a*b*2^-256 mod P */
103void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
104 const BN_ULONG a[P256_LIMBS],
105 const BN_ULONG b[P256_LIMBS]);
106/* Montgomery sqr: res = a*a*2^-256 mod P */
107void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
108 const BN_ULONG a[P256_LIMBS]);
109/* Convert a number from Montgomery domain, by multiplying with 1 */
110void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
111 const BN_ULONG in[P256_LIMBS]);
112/* Convert a number to Montgomery domain, by multiplying with 2^512 mod P*/
113void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
114 const BN_ULONG in[P256_LIMBS]);
115/* Functions that perform constant time access to the precomputed tables */
58d47cf0
AP
116void ecp_nistz256_scatter_w5(P256_POINT *val,
117 const P256_POINT *in_t, int index);
3ff08e1d 118void ecp_nistz256_gather_w5(P256_POINT * val,
58d47cf0
AP
119 const P256_POINT *in_t, int index);
120void ecp_nistz256_scatter_w7(P256_POINT_AFFINE *val,
121 const P256_POINT_AFFINE *in_t, int index);
122void ecp_nistz256_gather_w7(P256_POINT_AFFINE *val,
123 const P256_POINT_AFFINE *in_t, int index);
4d3fa06f
AP
124
125/* One converted into the Montgomery domain */
126static const BN_ULONG ONE[P256_LIMBS] = {
127 TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
128 TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
129};
130
be07ae9b
AP
131static void *ecp_nistz256_pre_comp_dup(void *);
132static void ecp_nistz256_pre_comp_free(void *);
133static void ecp_nistz256_pre_comp_clear_free(void *);
58d47cf0 134static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group);
4d3fa06f
AP
135
136/* Precomputed tables for the default generator */
3ff08e1d 137extern const PRECOMP256_ROW ecp_nistz256_precomputed[37];
4d3fa06f
AP
138
139/* Recode window to a signed digit, see ecp_nistputil.c for details */
140static unsigned int _booth_recode_w5(unsigned int in)
141{
142 unsigned int s, d;
143
144 s = ~((in >> 5) - 1);
145 d = (1 << 6) - in - 1;
146 d = (d & s) | (in & ~s);
147 d = (d >> 1) + (d & 1);
148
149 return (d << 1) + (s & 1);
150}
151
152static unsigned int _booth_recode_w7(unsigned int in)
153{
154 unsigned int s, d;
155
156 s = ~((in >> 7) - 1);
157 d = (1 << 8) - in - 1;
158 d = (d & s) | (in & ~s);
159 d = (d >> 1) + (d & 1);
160
161 return (d << 1) + (s & 1);
162}
163
164static void copy_conditional(BN_ULONG dst[P256_LIMBS],
165 const BN_ULONG src[P256_LIMBS], BN_ULONG move)
166{
167 BN_ULONG mask1 = -move;
168 BN_ULONG mask2 = ~mask1;
169
170 dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
171 dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
172 dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
173 dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
174 if (P256_LIMBS == 8) {
175 dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
176 dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
177 dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
178 dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
179 }
180}
181
182static BN_ULONG is_zero(BN_ULONG in)
183{
184 in |= (0 - in);
185 in = ~in;
186 in &= BN_MASK2;
187 in >>= BN_BITS2 - 1;
188 return in;
189}
190
191static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
192 const BN_ULONG b[P256_LIMBS])
193{
194 BN_ULONG res;
195
196 res = a[0] ^ b[0];
197 res |= a[1] ^ b[1];
198 res |= a[2] ^ b[2];
199 res |= a[3] ^ b[3];
200 if (P256_LIMBS == 8) {
201 res |= a[4] ^ b[4];
202 res |= a[5] ^ b[5];
203 res |= a[6] ^ b[6];
204 res |= a[7] ^ b[7];
205 }
206
207 return is_zero(res);
208}
209
210static BN_ULONG is_one(const BN_ULONG a[P256_LIMBS])
211{
212 BN_ULONG res;
213
214 res = a[0] ^ ONE[0];
215 res |= a[1] ^ ONE[1];
216 res |= a[2] ^ ONE[2];
217 res |= a[3] ^ ONE[3];
218 if (P256_LIMBS == 8) {
219 res |= a[4] ^ ONE[4];
220 res |= a[5] ^ ONE[5];
221 res |= a[6] ^ ONE[6];
222 }
223
224 return is_zero(res);
225}
226
227#ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
58d47cf0
AP
228void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
229void ecp_nistz256_point_add(P256_POINT *r,
230 const P256_POINT *a, const P256_POINT *b);
231void ecp_nistz256_point_add_affine(P256_POINT *r,
232 const P256_POINT *a,
233 const P256_POINT_AFFINE *b);
4d3fa06f
AP
234#else
235/* Point double: r = 2*a */
58d47cf0 236static void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a)
4d3fa06f
AP
237{
238 BN_ULONG S[P256_LIMBS];
239 BN_ULONG M[P256_LIMBS];
240 BN_ULONG Zsqr[P256_LIMBS];
241 BN_ULONG tmp0[P256_LIMBS];
242
243 const BN_ULONG *in_x = a->X;
244 const BN_ULONG *in_y = a->Y;
245 const BN_ULONG *in_z = a->Z;
246
247 BN_ULONG *res_x = r->X;
248 BN_ULONG *res_y = r->Y;
249 BN_ULONG *res_z = r->Z;
250
251 ecp_nistz256_mul_by_2(S, in_y);
252
253 ecp_nistz256_sqr_mont(Zsqr, in_z);
254
255 ecp_nistz256_sqr_mont(S, S);
256
257 ecp_nistz256_mul_mont(res_z, in_z, in_y);
258 ecp_nistz256_mul_by_2(res_z, res_z);
259
260 ecp_nistz256_add(M, in_x, Zsqr);
261 ecp_nistz256_sub(Zsqr, in_x, Zsqr);
262
263 ecp_nistz256_sqr_mont(res_y, S);
264 ecp_nistz256_div_by_2(res_y, res_y);
265
266 ecp_nistz256_mul_mont(M, M, Zsqr);
267 ecp_nistz256_mul_by_3(M, M);
268
269 ecp_nistz256_mul_mont(S, S, in_x);
270 ecp_nistz256_mul_by_2(tmp0, S);
271
272 ecp_nistz256_sqr_mont(res_x, M);
273
274 ecp_nistz256_sub(res_x, res_x, tmp0);
275 ecp_nistz256_sub(S, S, res_x);
276
277 ecp_nistz256_mul_mont(S, S, M);
278 ecp_nistz256_sub(res_y, S, res_y);
279}
280
281/* Point addition: r = a+b */
282static void ecp_nistz256_point_add(P256_POINT * r,
283 const P256_POINT * a, const P256_POINT * b)
284{
285 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
286 BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
287 BN_ULONG Z1sqr[P256_LIMBS];
288 BN_ULONG Z2sqr[P256_LIMBS];
289 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
290 BN_ULONG Hsqr[P256_LIMBS];
291 BN_ULONG Rsqr[P256_LIMBS];
292 BN_ULONG Hcub[P256_LIMBS];
293
294 BN_ULONG res_x[P256_LIMBS];
295 BN_ULONG res_y[P256_LIMBS];
296 BN_ULONG res_z[P256_LIMBS];
297
298 BN_ULONG in1infty, in2infty;
299
300 const BN_ULONG *in1_x = a->X;
301 const BN_ULONG *in1_y = a->Y;
302 const BN_ULONG *in1_z = a->Z;
303
304 const BN_ULONG *in2_x = b->X;
305 const BN_ULONG *in2_y = b->Y;
306 const BN_ULONG *in2_z = b->Z;
307
308 /* We encode infinity as (0,0), which is not on the curve,
309 * so it is OK. */
58d47cf0
AP
310 in1infty = (in1_x[0] | in1_x[1] | in1_x[2] | in1_x[3] |
311 in1_y[0] | in1_y[1] | in1_y[2] | in1_y[3]);
4d3fa06f 312 if (P256_LIMBS == 8)
58d47cf0
AP
313 in1infty |= (in1_x[4] | in1_x[5] | in1_x[6] | in1_x[7] |
314 in1_y[4] | in1_y[5] | in1_y[6] | in1_y[7]);
4d3fa06f 315
58d47cf0
AP
316 in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
317 in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
4d3fa06f 318 if (P256_LIMBS == 8)
58d47cf0
AP
319 in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
320 in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
4d3fa06f
AP
321
322 in1infty = is_zero(in1infty);
323 in2infty = is_zero(in2infty);
324
325 ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
326 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
327
328 ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
329 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
330
331 ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
332 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
333 ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
334
335 ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
336 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
337 ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
338
339 /* This should not happen during sign/ecdh,
340 * so no constant time violation */
341 if (is_equal(U1, U2) && !in1infty && !in2infty) {
342 if (is_equal(S1, S2)) {
343 ecp_nistz256_point_double(r, a);
344 return;
345 } else {
346 memset(r, 0, sizeof(*r));
347 return;
348 }
349 }
350
351 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
352 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
353 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
354 ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
355 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
356
357 ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
358 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
359
360 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
361 ecp_nistz256_sub(res_x, res_x, Hcub);
362
363 ecp_nistz256_sub(res_y, U2, res_x);
364
365 ecp_nistz256_mul_mont(S2, S1, Hcub);
366 ecp_nistz256_mul_mont(res_y, R, res_y);
367 ecp_nistz256_sub(res_y, res_y, S2);
368
369 copy_conditional(res_x, in2_x, in1infty);
370 copy_conditional(res_y, in2_y, in1infty);
371 copy_conditional(res_z, in2_z, in1infty);
372
373 copy_conditional(res_x, in1_x, in2infty);
374 copy_conditional(res_y, in1_y, in2infty);
375 copy_conditional(res_z, in1_z, in2infty);
376
377 memcpy(r->X, res_x, sizeof(res_x));
378 memcpy(r->Y, res_y, sizeof(res_y));
379 memcpy(r->Z, res_z, sizeof(res_z));
380}
381
382/* Point addition when b is known to be affine: r = a+b */
58d47cf0
AP
383static void ecp_nistz256_point_add_affine(P256_POINT *r,
384 const P256_POINT *a,
385 const P256_POINT_AFFINE *b)
4d3fa06f
AP
386{
387 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
388 BN_ULONG Z1sqr[P256_LIMBS];
389 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
390 BN_ULONG Hsqr[P256_LIMBS];
391 BN_ULONG Rsqr[P256_LIMBS];
392 BN_ULONG Hcub[P256_LIMBS];
393
394 BN_ULONG res_x[P256_LIMBS];
395 BN_ULONG res_y[P256_LIMBS];
396 BN_ULONG res_z[P256_LIMBS];
397
398 BN_ULONG in1infty, in2infty;
399
400 const BN_ULONG *in1_x = a->X;
401 const BN_ULONG *in1_y = a->Y;
402 const BN_ULONG *in1_z = a->Z;
403
404 const BN_ULONG *in2_x = b->X;
405 const BN_ULONG *in2_y = b->Y;
406
407 /* In affine representation we encode infty as (0,0),
408 * which is not on the curve, so it is OK */
58d47cf0
AP
409 in1infty = (in1_x[0] | in1_x[1] | in1_x[2] | in1_x[3] |
410 in1_y[0] | in1_y[1] | in1_y[2] | in1_y[3]);
4d3fa06f 411 if (P256_LIMBS == 8)
58d47cf0
AP
412 in1infty |= (in1_x[4] | in1_x[5] | in1_x[6] | in1_x[7] |
413 in1_y[4] | in1_y[5] | in1_y[6] | in1_y[7]);
4d3fa06f 414
58d47cf0
AP
415 in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
416 in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
4d3fa06f 417 if (P256_LIMBS == 8)
58d47cf0
AP
418 in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
419 in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
4d3fa06f
AP
420
421 in1infty = is_zero(in1infty);
422 in2infty = is_zero(in2infty);
423
424 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
425
426 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
427 ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
428
429 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
430
431 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
432
433 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
434 ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
435
436 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
437 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
438 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
439
440 ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
441 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
442
443 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
444 ecp_nistz256_sub(res_x, res_x, Hcub);
445 ecp_nistz256_sub(H, U2, res_x);
446
447 ecp_nistz256_mul_mont(S2, in1_y, Hcub);
448 ecp_nistz256_mul_mont(H, H, R);
449 ecp_nistz256_sub(res_y, H, S2);
450
451 copy_conditional(res_x, in2_x, in1infty);
452 copy_conditional(res_x, in1_x, in2infty);
453
454 copy_conditional(res_y, in2_y, in1infty);
455 copy_conditional(res_y, in1_y, in2infty);
456
457 copy_conditional(res_z, ONE, in1infty);
458 copy_conditional(res_z, in1_z, in2infty);
459
460 memcpy(r->X, res_x, sizeof(res_x));
461 memcpy(r->Y, res_y, sizeof(res_y));
462 memcpy(r->Z, res_z, sizeof(res_z));
463}
464#endif
465
466/* r = in^-1 mod p */
467static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
468 const BN_ULONG in[P256_LIMBS])
469{
470 /* The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
471 We use FLT and used poly-2 as exponent */
472 BN_ULONG p2[P256_LIMBS];
473 BN_ULONG p4[P256_LIMBS];
474 BN_ULONG p8[P256_LIMBS];
475 BN_ULONG p16[P256_LIMBS];
476 BN_ULONG p32[P256_LIMBS];
477 BN_ULONG res[P256_LIMBS];
478 int i;
479
480 ecp_nistz256_sqr_mont(res, in);
481 ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
482
483 ecp_nistz256_sqr_mont(res, p2);
484 ecp_nistz256_sqr_mont(res, res);
485 ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
486
487 ecp_nistz256_sqr_mont(res, p4);
488 ecp_nistz256_sqr_mont(res, res);
489 ecp_nistz256_sqr_mont(res, res);
490 ecp_nistz256_sqr_mont(res, res);
491 ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
492
493 ecp_nistz256_sqr_mont(res, p8);
494 for (i = 0; i < 7; i++)
495 ecp_nistz256_sqr_mont(res, res);
496 ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
497
498 ecp_nistz256_sqr_mont(res, p16);
499 for (i = 0; i < 15; i++)
500 ecp_nistz256_sqr_mont(res, res);
501 ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
502
503 ecp_nistz256_sqr_mont(res, p32);
504 for (i = 0; i < 31; i++)
505 ecp_nistz256_sqr_mont(res, res);
506 ecp_nistz256_mul_mont(res, res, in);
507
508 for (i = 0; i < 32 * 4; i++)
509 ecp_nistz256_sqr_mont(res, res);
510 ecp_nistz256_mul_mont(res, res, p32);
511
512 for (i = 0; i < 32; i++)
513 ecp_nistz256_sqr_mont(res, res);
514 ecp_nistz256_mul_mont(res, res, p32);
515
516 for (i = 0; i < 16; i++)
517 ecp_nistz256_sqr_mont(res, res);
518 ecp_nistz256_mul_mont(res, res, p16);
519
520 for (i = 0; i < 8; i++)
521 ecp_nistz256_sqr_mont(res, res);
522 ecp_nistz256_mul_mont(res, res, p8);
523
524 ecp_nistz256_sqr_mont(res, res);
525 ecp_nistz256_sqr_mont(res, res);
526 ecp_nistz256_sqr_mont(res, res);
527 ecp_nistz256_sqr_mont(res, res);
528 ecp_nistz256_mul_mont(res, res, p4);
529
530 ecp_nistz256_sqr_mont(res, res);
531 ecp_nistz256_sqr_mont(res, res);
532 ecp_nistz256_mul_mont(res, res, p2);
533
534 ecp_nistz256_sqr_mont(res, res);
535 ecp_nistz256_sqr_mont(res, res);
536 ecp_nistz256_mul_mont(res, res, in);
537
538 memcpy(r, res, sizeof(res));
539}
540
541/* ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
542 * returns one if it fits. Otherwise it returns zero. */
543static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
544 const BIGNUM * in)
545{
5784a521 546 return bn_copy_words(out, in, P256_LIMBS);
4d3fa06f
AP
547}
548
549/* r = sum(scalar[i]*point[i]) */
58d47cf0
AP
550static void ecp_nistz256_windowed_mul(const EC_GROUP *group,
551 P256_POINT *r,
552 const BIGNUM **scalar,
553 const EC_POINT **point,
554 int num, BN_CTX *ctx)
4d3fa06f
AP
555{
556 int i, j;
557 unsigned int index;
558 unsigned char (*p_str)[33] = NULL;
559 const unsigned int window_size = 5;
560 const unsigned int mask = (1 << (window_size + 1)) - 1;
561 unsigned int wvalue;
3ff08e1d 562 P256_POINT *temp; /* place for 5 temporary points */
4d3fa06f
AP
563 const BIGNUM **scalars = NULL;
564 P256_POINT(*table)[16] = NULL;
565 void *table_storage = NULL;
566
567 if ((table_storage =
3ff08e1d 568 OPENSSL_malloc((num * 16 + 5) * sizeof(P256_POINT) + 64)) == NULL
4d3fa06f
AP
569 || (p_str =
570 OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
571 || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
be07ae9b 572 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
4d3fa06f 573 goto err;
4d3fa06f
AP
574 }
575
3ff08e1d
AP
576 table = (void *)ALIGNPTR(table_storage, 64);
577 temp = (P256_POINT *)(table + num);
578
4d3fa06f
AP
579 for (i = 0; i < num; i++) {
580 P256_POINT *row = table[i];
581
582 if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
583 BIGNUM *mod;
584
585 if ((mod = BN_CTX_get(ctx)) == NULL)
586 goto err;
5784a521 587 if (!BN_nnmod(mod, scalar[i], group->order, ctx)) {
be07ae9b 588 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
4d3fa06f
AP
589 goto err;
590 }
591 scalars[i] = mod;
592 } else
593 scalars[i] = scalar[i];
594
5784a521
MC
595 for (j = 0; j < bn_get_top(scalars[i]) * BN_BYTES; j += BN_BYTES) {
596 BN_ULONG d = bn_get_words(scalars[i])[j / BN_BYTES];
4d3fa06f
AP
597
598 p_str[i][j + 0] = d & 0xff;
599 p_str[i][j + 1] = (d >> 8) & 0xff;
600 p_str[i][j + 2] = (d >> 16) & 0xff;
601 p_str[i][j + 3] = (d >>= 24) & 0xff;
602 if (BN_BYTES == 8) {
603 d >>= 8;
604 p_str[i][j + 4] = d & 0xff;
605 p_str[i][j + 5] = (d >> 8) & 0xff;
606 p_str[i][j + 6] = (d >> 16) & 0xff;
607 p_str[i][j + 7] = (d >> 24) & 0xff;
608 }
609 }
610 for (; j < 33; j++)
611 p_str[i][j] = 0;
612
5784a521
MC
613 if (!ecp_nistz256_bignum_to_field_elem(temp[0].X, point[i]->X)
614 || !ecp_nistz256_bignum_to_field_elem(temp[0].Y, point[i]->Y)
615 || !ecp_nistz256_bignum_to_field_elem(temp[0].Z, point[i]->Z)) {
58d47cf0
AP
616 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL,
617 EC_R_COORDINATES_OUT_OF_RANGE);
4d3fa06f
AP
618 goto err;
619 }
620
3ff08e1d
AP
621 /* row[0] is implicitly (0,0,0) (the point at infinity),
622 * therefore it is not stored. All other values are actually
623 * stored with an offset of -1 in table.
624 */
625
626 ecp_nistz256_scatter_w5 (row, &temp[0], 1);
627 ecp_nistz256_point_double(&temp[1], &temp[0]); /*1+1=2 */
628 ecp_nistz256_scatter_w5 (row, &temp[1], 2);
629 ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*2+1=3 */
630 ecp_nistz256_scatter_w5 (row, &temp[2], 3);
631 ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*2=4 */
632 ecp_nistz256_scatter_w5 (row, &temp[1], 4);
633 ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*3=6 */
634 ecp_nistz256_scatter_w5 (row, &temp[2], 6);
635 ecp_nistz256_point_add (&temp[3], &temp[1], &temp[0]); /*4+1=5 */
636 ecp_nistz256_scatter_w5 (row, &temp[3], 5);
637 ecp_nistz256_point_add (&temp[4], &temp[2], &temp[0]); /*6+1=7 */
638 ecp_nistz256_scatter_w5 (row, &temp[4], 7);
639 ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*4=8 */
640 ecp_nistz256_scatter_w5 (row, &temp[1], 8);
641 ecp_nistz256_point_double(&temp[2], &temp[2]); /*2*6=12 */
642 ecp_nistz256_scatter_w5 (row, &temp[2], 12);
643 ecp_nistz256_point_double(&temp[3], &temp[3]); /*2*5=10 */
644 ecp_nistz256_scatter_w5 (row, &temp[3], 10);
645 ecp_nistz256_point_double(&temp[4], &temp[4]); /*2*7=14 */
646 ecp_nistz256_scatter_w5 (row, &temp[4], 14);
647 ecp_nistz256_point_add (&temp[2], &temp[2], &temp[0]); /*12+1=13*/
648 ecp_nistz256_scatter_w5 (row, &temp[2], 13);
649 ecp_nistz256_point_add (&temp[3], &temp[3], &temp[0]); /*10+1=11*/
650 ecp_nistz256_scatter_w5 (row, &temp[3], 11);
651 ecp_nistz256_point_add (&temp[4], &temp[4], &temp[0]); /*14+1=15*/
652 ecp_nistz256_scatter_w5 (row, &temp[4], 15);
653 ecp_nistz256_point_add (&temp[2], &temp[1], &temp[0]); /*8+1=9 */
654 ecp_nistz256_scatter_w5 (row, &temp[2], 9);
655 ecp_nistz256_point_double(&temp[1], &temp[1]); /*2*8=16 */
656 ecp_nistz256_scatter_w5 (row, &temp[1], 16);
4d3fa06f
AP
657 }
658
659 index = 255;
660
661 wvalue = p_str[0][(index - 1) / 8];
662 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
663
3ff08e1d
AP
664 /*
665 * We gather to temp[0], because we know it's position relative
666 * to table
667 */
668 ecp_nistz256_gather_w5(&temp[0], table[0], _booth_recode_w5(wvalue) >> 1);
669 memcpy(r, &temp[0], sizeof(temp[0]));
4d3fa06f
AP
670
671 while (index >= 5) {
672 for (i = (index == 255 ? 1 : 0); i < num; i++) {
673 unsigned int off = (index - 1) / 8;
674
675 wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
676 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
677
678 wvalue = _booth_recode_w5(wvalue);
679
3ff08e1d 680 ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
4d3fa06f 681
3ff08e1d
AP
682 ecp_nistz256_neg(temp[1].Y, temp[0].Y);
683 copy_conditional(temp[0].Y, temp[1].Y, (wvalue & 1));
4d3fa06f 684
3ff08e1d 685 ecp_nistz256_point_add(r, r, &temp[0]);
4d3fa06f
AP
686 }
687
688 index -= window_size;
689
690 ecp_nistz256_point_double(r, r);
691 ecp_nistz256_point_double(r, r);
692 ecp_nistz256_point_double(r, r);
693 ecp_nistz256_point_double(r, r);
694 ecp_nistz256_point_double(r, r);
695 }
696
697 /* Final window */
698 for (i = 0; i < num; i++) {
699 wvalue = p_str[i][0];
700 wvalue = (wvalue << 1) & mask;
701
702 wvalue = _booth_recode_w5(wvalue);
703
3ff08e1d 704 ecp_nistz256_gather_w5(&temp[0], table[i], wvalue >> 1);
4d3fa06f 705
3ff08e1d
AP
706 ecp_nistz256_neg(temp[1].Y, temp[0].Y);
707 copy_conditional(temp[0].Y, temp[1].Y, wvalue & 1);
4d3fa06f 708
3ff08e1d 709 ecp_nistz256_point_add(r, r, &temp[0]);
4d3fa06f
AP
710 }
711
58d47cf0 712 err:
4d3fa06f
AP
713 if (table_storage)
714 OPENSSL_free(table_storage);
715 if (p_str)
716 OPENSSL_free(p_str);
717 if (scalars)
718 OPENSSL_free(scalars);
719}
720
721/* Coordinates of G, for which we have precomputed tables */
722const static BN_ULONG def_xG[P256_LIMBS] = {
723 TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
724 TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
725};
726
727const static BN_ULONG def_yG[P256_LIMBS] = {
728 TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
729 TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
730};
731
732/* ecp_nistz256_is_affine_G returns one if |generator| is the standard,
733 * P-256 generator. */
58d47cf0 734static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
4d3fa06f 735{
5784a521
MC
736 return (bn_get_top(generator->X) == P256_LIMBS) &&
737 (bn_get_top(generator->Y) == P256_LIMBS) &&
738 (bn_get_top(generator->Z) == (P256_LIMBS - P256_LIMBS / 8)) &&
739 is_equal(bn_get_words(generator->X), def_xG) &&
740 is_equal(bn_get_words(generator->Y), def_yG) &&
741 is_one(bn_get_words(generator->Z));
4d3fa06f
AP
742}
743
58d47cf0 744static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
4d3fa06f
AP
745{
746 /* We precompute a table for a Booth encoded exponent (wNAF) based
747 * computation. Each table holds 64 values for safe access, with an
748 * implicit value of infinity at index zero. We use window of size 7,
749 * and therefore require ceil(256/7) = 37 tables. */
750 BIGNUM *order;
751 EC_POINT *P = NULL, *T = NULL;
752 const EC_POINT *generator;
753 EC_PRE_COMP *pre_comp;
754 int i, j, k, ret = 0;
755 size_t w;
756
757 PRECOMP256_ROW *preComputedTable = NULL;
758 unsigned char *precomp_storage = NULL;
759
760 /* if there is an old EC_PRE_COMP object, throw it away */
be07ae9b
AP
761 EC_EX_DATA_free_data(&group->extra_data, ecp_nistz256_pre_comp_dup,
762 ecp_nistz256_pre_comp_free,
763 ecp_nistz256_pre_comp_clear_free);
4d3fa06f
AP
764
765 generator = EC_GROUP_get0_generator(group);
766 if (generator == NULL) {
be07ae9b 767 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
4d3fa06f
AP
768 return 0;
769 }
770
771 if (ecp_nistz256_is_affine_G(generator)) {
772 /* No need to calculate tables for the standard generator
773 * because we have them statically. */
774 return 1;
775 }
776
be07ae9b 777 if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
4d3fa06f
AP
778 return 0;
779
780 if (ctx == NULL) {
781 ctx = BN_CTX_new();
782 if (ctx == NULL)
783 goto err;
784 }
785
786 BN_CTX_start(ctx);
787 order = BN_CTX_get(ctx);
788
789 if (order == NULL)
790 goto err;
791
792 if (!EC_GROUP_get_order(group, order, ctx))
793 goto err;
794
795 if (BN_is_zero(order)) {
be07ae9b 796 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
4d3fa06f
AP
797 goto err;
798 }
799
800 w = 7;
801
802 if ((precomp_storage =
803 OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
be07ae9b 804 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
4d3fa06f 805 goto err;
4d3fa06f
AP
806 }
807
3ff08e1d
AP
808 preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
809
4d3fa06f
AP
810 P = EC_POINT_new(group);
811 T = EC_POINT_new(group);
812
813 /* The zero entry is implicitly infinity, and we skip it,
814 * storing other values with -1 offset. */
815 EC_POINT_copy(T, generator);
816
817 for (k = 0; k < 64; k++) {
818 EC_POINT_copy(P, T);
819 for (j = 0; j < 37; j++) {
3ff08e1d 820 P256_POINT_AFFINE temp;
4d3fa06f
AP
821 /* It would be faster to use
822 * ec_GFp_simple_points_make_affine and make multiple
823 * points affine at the same time. */
824 ec_GFp_simple_make_affine(group, P, ctx);
5784a521
MC
825 ecp_nistz256_bignum_to_field_elem(temp.X, P->X);
826 ecp_nistz256_bignum_to_field_elem(temp.Y, P->Y);
3ff08e1d 827 ecp_nistz256_scatter_w7(preComputedTable[j], &temp, k);
4d3fa06f
AP
828 for (i = 0; i < 7; i++)
829 ec_GFp_simple_dbl(group, P, P, ctx);
830 }
831 ec_GFp_simple_add(group, T, T, generator, ctx);
832 }
833
834 pre_comp->group = group;
835 pre_comp->w = w;
836 pre_comp->precomp = preComputedTable;
837 pre_comp->precomp_storage = precomp_storage;
838
839 precomp_storage = NULL;
840
841 if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
be07ae9b
AP
842 ecp_nistz256_pre_comp_dup,
843 ecp_nistz256_pre_comp_free,
844 ecp_nistz256_pre_comp_clear_free)) {
4d3fa06f
AP
845 goto err;
846 }
847
848 pre_comp = NULL;
849
850 ret = 1;
851
58d47cf0 852 err:
4d3fa06f
AP
853 if (ctx != NULL)
854 BN_CTX_end(ctx);
855 if (pre_comp)
be07ae9b 856 ecp_nistz256_pre_comp_free(pre_comp);
4d3fa06f
AP
857 if (precomp_storage)
858 OPENSSL_free(precomp_storage);
859 if (P)
860 EC_POINT_free(P);
861 if (T)
862 EC_POINT_free(T);
863 return ret;
864}
865
866/*
867 * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
868 * code processing 4 points in parallel, corresponding serial operation
869 * is several times slower, because it uses 29x29=58-bit multiplication
870 * as opposite to 64x64=128-bit in integer-only scalar case. As result
871 * it doesn't provide *significant* performance improvement. Note that
872 * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
873 * you'd need to compile even asm/ecp_nistz256-avx.pl module.
874 */
875#if defined(ECP_NISTZ256_AVX2)
3ff08e1d 876# if !(defined(__x86_64) || defined(__x86_64__) || \
4d3fa06f
AP
877 defined(_M_AMD64) || defined(_MX64)) || \
878 !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
879# undef ECP_NISTZ256_AVX2
880# else
881/* Constant time access, loading four values, from four consecutive tables */
58d47cf0
AP
882void ecp_nistz256_avx2_multi_gather_w7(void *result, const void *in,
883 int index0, int index1, int index2,
884 int index3);
4d3fa06f
AP
885void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
886void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
887void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
888 const void *Bx4);
889void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
890 const void *Bx4);
891void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
892void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
893void ecp_nistz256_avx2_set1(void *RESULTx4);
894int ecp_nistz_avx2_eligible(void);
895
896static void booth_recode_w7(unsigned char *sign,
897 unsigned char *digit, unsigned char in)
898{
899 unsigned char s, d;
900
901 s = ~((in >> 7) - 1);
902 d = (1 << 8) - in - 1;
903 d = (d & s) | (in & ~s);
904 d = (d >> 1) + (d & 1);
905
906 *sign = s & 1;
907 *digit = d;
908}
909
910/* ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
911 * precomputed table. It does 4 affine point additions in parallel,
912 * significantly speeding up point multiplication for a fixed value. */
58d47cf0 913static void ecp_nistz256_avx2_mul_g(P256_POINT *r,
4d3fa06f 914 unsigned char p_str[33],
58d47cf0 915 const P256_POINT_AFFINE(*preComputedTable)[64])
4d3fa06f
AP
916{
917 const unsigned int window_size = 7;
918 const unsigned int mask = (1 << (window_size + 1)) - 1;
919 unsigned int wvalue;
920 /* Using 4 windows at a time */
921 unsigned char sign0, digit0;
922 unsigned char sign1, digit1;
923 unsigned char sign2, digit2;
924 unsigned char sign3, digit3;
925 unsigned int index = 0;
926 BN_ULONG tmp[P256_LIMBS];
927 int i;
928
929 ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
930 ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
3ff08e1d
AP
931 ALIGN32 P256_POINT_AFFINE point_arr[4];
932 ALIGN32 P256_POINT res_point_arr[4];
4d3fa06f
AP
933
934 /* Initial four windows */
935 wvalue = *((u16 *) & p_str[0]);
936 wvalue = (wvalue << 1) & mask;
937 index += window_size;
938 booth_recode_w7(&sign0, &digit0, wvalue);
939 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
940 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
941 index += window_size;
942 booth_recode_w7(&sign1, &digit1, wvalue);
943 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
944 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
945 index += window_size;
946 booth_recode_w7(&sign2, &digit2, wvalue);
947 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
948 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
949 index += window_size;
950 booth_recode_w7(&sign3, &digit3, wvalue);
951
3ff08e1d 952 ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[0],
4d3fa06f
AP
953 digit0, digit1, digit2, digit3);
954
955 ecp_nistz256_neg(tmp, point_arr[0].Y);
956 copy_conditional(point_arr[0].Y, tmp, sign0);
957 ecp_nistz256_neg(tmp, point_arr[1].Y);
958 copy_conditional(point_arr[1].Y, tmp, sign1);
959 ecp_nistz256_neg(tmp, point_arr[2].Y);
960 copy_conditional(point_arr[2].Y, tmp, sign2);
961 ecp_nistz256_neg(tmp, point_arr[3].Y);
962 copy_conditional(point_arr[3].Y, tmp, sign3);
963
964 ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
965 ecp_nistz256_avx2_to_mont(aX4, aX4);
966 ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
967 ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
968
969 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
970 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
971 index += window_size;
972 booth_recode_w7(&sign0, &digit0, wvalue);
973 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
974 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
975 index += window_size;
976 booth_recode_w7(&sign1, &digit1, wvalue);
977 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
978 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
979 index += window_size;
980 booth_recode_w7(&sign2, &digit2, wvalue);
981 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
982 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
983 index += window_size;
984 booth_recode_w7(&sign3, &digit3, wvalue);
985
3ff08e1d 986 ecp_nistz256_avx2_multi_gather_w7(point_arr, preComputedTable[4 * 1],
4d3fa06f
AP
987 digit0, digit1, digit2, digit3);
988
989 ecp_nistz256_neg(tmp, point_arr[0].Y);
990 copy_conditional(point_arr[0].Y, tmp, sign0);
991 ecp_nistz256_neg(tmp, point_arr[1].Y);
992 copy_conditional(point_arr[1].Y, tmp, sign1);
993 ecp_nistz256_neg(tmp, point_arr[2].Y);
994 copy_conditional(point_arr[2].Y, tmp, sign2);
995 ecp_nistz256_neg(tmp, point_arr[3].Y);
996 copy_conditional(point_arr[3].Y, tmp, sign3);
997
998 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
999 ecp_nistz256_avx2_to_mont(bX4, bX4);
1000 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
1001 /* Optimized when both inputs are affine */
1002 ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
1003
1004 for (i = 2; i < 9; i++) {
1005 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1006 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1007 index += window_size;
1008 booth_recode_w7(&sign0, &digit0, wvalue);
1009 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1010 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1011 index += window_size;
1012 booth_recode_w7(&sign1, &digit1, wvalue);
1013 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1014 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1015 index += window_size;
1016 booth_recode_w7(&sign2, &digit2, wvalue);
1017 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1018 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1019 index += window_size;
1020 booth_recode_w7(&sign3, &digit3, wvalue);
1021
3ff08e1d 1022 ecp_nistz256_avx2_multi_gather_w7(point_arr,
4d3fa06f
AP
1023 preComputedTable[4 * i],
1024 digit0, digit1, digit2, digit3);
1025
1026 ecp_nistz256_neg(tmp, point_arr[0].Y);
1027 copy_conditional(point_arr[0].Y, tmp, sign0);
1028 ecp_nistz256_neg(tmp, point_arr[1].Y);
1029 copy_conditional(point_arr[1].Y, tmp, sign1);
1030 ecp_nistz256_neg(tmp, point_arr[2].Y);
1031 copy_conditional(point_arr[2].Y, tmp, sign2);
1032 ecp_nistz256_neg(tmp, point_arr[3].Y);
1033 copy_conditional(point_arr[3].Y, tmp, sign3);
1034
1035 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
1036 ecp_nistz256_avx2_to_mont(bX4, bX4);
1037 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
1038
1039 ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
1040 }
1041
1042 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
1043 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
1044 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
1045
1046 ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
1047 /* Last window is performed serially */
1048 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1049 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1050 booth_recode_w7(&sign0, &digit0, wvalue);
58d47cf0
AP
1051 ecp_nistz256_gather_w7((P256_POINT_AFFINE *)r,
1052 preComputedTable[36], digit0);
4d3fa06f
AP
1053 ecp_nistz256_neg(tmp, r->Y);
1054 copy_conditional(r->Y, tmp, sign0);
1055 memcpy(r->Z, ONE, sizeof(ONE));
1056 /* Sum the four windows */
1057 ecp_nistz256_point_add(r, r, &res_point_arr[0]);
1058 ecp_nistz256_point_add(r, r, &res_point_arr[1]);
1059 ecp_nistz256_point_add(r, r, &res_point_arr[2]);
1060 ecp_nistz256_point_add(r, r, &res_point_arr[3]);
1061}
1062# endif
1063#endif
1064
58d47cf0
AP
1065static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
1066 const P256_POINT_AFFINE *in,
1067 BN_CTX *ctx)
4d3fa06f 1068{
5784a521 1069 BIGNUM *x, *y;
4d3fa06f
AP
1070 BN_ULONG d_x[P256_LIMBS], d_y[P256_LIMBS];
1071 int ret = 0;
1072
5784a521 1073 x = BN_new();
58d47cf0 1074 if (!x)
5784a521
MC
1075 return 0;
1076 y = BN_new();
58d47cf0 1077 if (!y) {
5784a521
MC
1078 BN_free(x);
1079 return 0;
1080 }
4d3fa06f 1081 memcpy(d_x, in->X, sizeof(d_x));
5784a521 1082 bn_set_static_words(x, d_x, P256_LIMBS);
4d3fa06f
AP
1083
1084 memcpy(d_y, in->Y, sizeof(d_y));
5784a521
MC
1085 bn_set_static_words(y, d_y, P256_LIMBS);
1086
1087 ret = EC_POINT_set_affine_coordinates_GFp(group, out, x, y, ctx);
4d3fa06f 1088
58d47cf0
AP
1089 if (x)
1090 BN_free(x);
1091 if (y)
1092 BN_free(y);
4d3fa06f
AP
1093
1094 return ret;
1095}
1096
1097/* r = scalar*G + sum(scalars[i]*points[i]) */
58d47cf0
AP
1098static int ecp_nistz256_points_mul(const EC_GROUP *group,
1099 EC_POINT *r,
1100 const BIGNUM *scalar,
4d3fa06f 1101 size_t num,
58d47cf0
AP
1102 const EC_POINT *points[],
1103 const BIGNUM *scalars[], BN_CTX *ctx)
4d3fa06f
AP
1104{
1105 int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
16e5b45f 1106 size_t j;
4d3fa06f
AP
1107 unsigned char p_str[33] = { 0 };
1108 const PRECOMP256_ROW *preComputedTable = NULL;
1109 const EC_PRE_COMP *pre_comp = NULL;
1110 const EC_POINT *generator = NULL;
1111 unsigned int index = 0;
1112 const unsigned int window_size = 7;
1113 const unsigned int mask = (1 << (window_size + 1)) - 1;
1114 unsigned int wvalue;
1115 ALIGN32 union {
1116 P256_POINT p;
1117 P256_POINT_AFFINE a;
1118 } t, p;
1119 BIGNUM *tmp_scalar;
1120
58d47cf0 1121 if ((num + 1) == 0 || (num + 1) > OPENSSL_MALLOC_MAX_NELEMS(void *)) {
3ff08e1d
AP
1122 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1123 return 0;
1124 }
1125
4d3fa06f 1126 if (group->meth != r->meth) {
be07ae9b 1127 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
4d3fa06f
AP
1128 return 0;
1129 }
1130 if ((scalar == NULL) && (num == 0))
1131 return EC_POINT_set_to_infinity(group, r);
1132
16e5b45f
DSH
1133 for (j = 0; j < num; j++) {
1134 if (group->meth != points[j]->meth) {
be07ae9b 1135 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
4d3fa06f
AP
1136 return 0;
1137 }
1138 }
1139
1140 /* Need 256 bits for space for all coordinates. */
5784a521
MC
1141 bn_wexpand(r->X, P256_LIMBS);
1142 bn_wexpand(r->Y, P256_LIMBS);
1143 bn_wexpand(r->Z, P256_LIMBS);
1144 bn_set_top(r->X, P256_LIMBS);
1145 bn_set_top(r->Y, P256_LIMBS);
1146 bn_set_top(r->Z, P256_LIMBS);
4d3fa06f
AP
1147
1148 if (scalar) {
1149 generator = EC_GROUP_get0_generator(group);
1150 if (generator == NULL) {
be07ae9b 1151 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
4d3fa06f
AP
1152 goto err;
1153 }
1154
1155 /* look if we can use precomputed multiples of generator */
1156 pre_comp =
be07ae9b
AP
1157 EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1158 ecp_nistz256_pre_comp_free,
1159 ecp_nistz256_pre_comp_clear_free);
4d3fa06f
AP
1160
1161 if (pre_comp) {
1162 /* If there is a precomputed table for the generator,
1163 * check that it was generated with the same
1164 * generator. */
1165 EC_POINT *pre_comp_generator = EC_POINT_new(group);
1166 if (pre_comp_generator == NULL)
1167 goto err;
1168
3ff08e1d
AP
1169 if (!ecp_nistz256_set_from_affine(pre_comp_generator,
1170 group, pre_comp->precomp[0],
1171 ctx))
4d3fa06f
AP
1172 goto err;
1173
1174 if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
1175 preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
1176
1177 EC_POINT_free(pre_comp_generator);
1178 }
1179
1180 if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
1181 /* If there is no precomputed data, but the generator
1182 * is the default, a hardcoded table of precomputed
1183 * data is used. This is because applications, such as
1184 * Apache, do not use EC_KEY_precompute_mult. */
3ff08e1d 1185 preComputedTable = ecp_nistz256_precomputed;
4d3fa06f
AP
1186 }
1187
1188 if (preComputedTable) {
1189 if ((BN_num_bits(scalar) > 256)
1190 || BN_is_negative(scalar)) {
1191 if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
1192 goto err;
1193
5784a521 1194 if (!BN_nnmod(tmp_scalar, scalar, group->order, ctx)) {
be07ae9b 1195 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
4d3fa06f
AP
1196 goto err;
1197 }
1198 scalar = tmp_scalar;
1199 }
1200
5784a521
MC
1201 for (i = 0; i < bn_get_top(scalar) * BN_BYTES; i += BN_BYTES) {
1202 BN_ULONG d = bn_get_words(scalar)[i / BN_BYTES];
4d3fa06f
AP
1203
1204 p_str[i + 0] = d & 0xff;
1205 p_str[i + 1] = (d >> 8) & 0xff;
1206 p_str[i + 2] = (d >> 16) & 0xff;
1207 p_str[i + 3] = (d >>= 24) & 0xff;
1208 if (BN_BYTES == 8) {
1209 d >>= 8;
1210 p_str[i + 4] = d & 0xff;
1211 p_str[i + 5] = (d >> 8) & 0xff;
1212 p_str[i + 6] = (d >> 16) & 0xff;
1213 p_str[i + 7] = (d >> 24) & 0xff;
1214 }
1215 }
1216
1217 for (; i < 33; i++)
1218 p_str[i] = 0;
1219
1220#if defined(ECP_NISTZ256_AVX2)
1221 if (ecp_nistz_avx2_eligible()) {
1222 ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
1223 } else
1224#endif
1225 {
1226 /* First window */
1227 wvalue = (p_str[0] << 1) & mask;
1228 index += window_size;
1229
1230 wvalue = _booth_recode_w7(wvalue);
1231
58d47cf0
AP
1232 ecp_nistz256_gather_w7(&p.a, preComputedTable[0],
1233 wvalue >> 1);
4d3fa06f
AP
1234
1235 ecp_nistz256_neg(p.p.Z, p.p.Y);
1236 copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
1237
1238 memcpy(p.p.Z, ONE, sizeof(ONE));
1239
1240 for (i = 1; i < 37; i++) {
1241 unsigned int off = (index - 1) / 8;
1242 wvalue = p_str[off] | p_str[off + 1] << 8;
1243 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1244 index += window_size;
1245
1246 wvalue = _booth_recode_w7(wvalue);
1247
3ff08e1d 1248 ecp_nistz256_gather_w7(&t.a,
4d3fa06f
AP
1249 preComputedTable[i], wvalue >> 1);
1250
1251 ecp_nistz256_neg(t.p.Z, t.a.Y);
1252 copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
1253
1254 ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
1255 }
1256 }
1257 } else {
1258 p_is_infinity = 1;
1259 no_precomp_for_generator = 1;
1260 }
1261 } else
1262 p_is_infinity = 1;
1263
1264 if (no_precomp_for_generator) {
1265 /* Without a precomputed table for the generator, it has to be
1266 * handled like a normal point. */
1267 const BIGNUM **new_scalars;
1268 const EC_POINT **new_points;
1269
1270 new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
1271 if (!new_scalars) {
be07ae9b 1272 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
4d3fa06f
AP
1273 return 0;
1274 }
1275
1276 new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
1277 if (!new_points) {
1278 OPENSSL_free(new_scalars);
be07ae9b 1279 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
4d3fa06f
AP
1280 return 0;
1281 }
1282
1283 memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
1284 new_scalars[num] = scalar;
1285 memcpy(new_points, points, num * sizeof(EC_POINT *));
1286 new_points[num] = generator;
1287
1288 scalars = new_scalars;
1289 points = new_points;
1290 num++;
1291 }
1292
1293 if (num) {
1294 P256_POINT *out = &t.p;
1295 if (p_is_infinity)
1296 out = &p.p;
1297
1298 ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx);
1299
1300 if (!p_is_infinity)
1301 ecp_nistz256_point_add(&p.p, &p.p, out);
1302 }
1303
1304 if (no_precomp_for_generator) {
1305 OPENSSL_free(points);
1306 OPENSSL_free(scalars);
1307 }
1308
5784a521
MC
1309 bn_set_data(r->X, p.p.X, sizeof(p.p.X));
1310 bn_set_data(r->Y, p.p.Y, sizeof(p.p.Y));
1311 bn_set_data(r->Z, p.p.Z, sizeof(p.p.Z));
1312 bn_correct_top(r->X);
1313 bn_correct_top(r->Y);
1314 bn_correct_top(r->Z);
4d3fa06f
AP
1315
1316 ret = 1;
1317
58d47cf0 1318 err:
4d3fa06f
AP
1319 return ret;
1320}
1321
58d47cf0
AP
1322static int ecp_nistz256_get_affine(const EC_GROUP *group,
1323 const EC_POINT *point,
1324 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
4d3fa06f
AP
1325{
1326 BN_ULONG z_inv2[P256_LIMBS];
1327 BN_ULONG z_inv3[P256_LIMBS];
1328 BN_ULONG x_aff[P256_LIMBS];
1329 BN_ULONG y_aff[P256_LIMBS];
1330 BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
1331
1332 if (EC_POINT_is_at_infinity(group, point)) {
be07ae9b 1333 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
4d3fa06f
AP
1334 return 0;
1335 }
1336
5784a521
MC
1337 if (!ecp_nistz256_bignum_to_field_elem(point_x, point->X) ||
1338 !ecp_nistz256_bignum_to_field_elem(point_y, point->Y) ||
1339 !ecp_nistz256_bignum_to_field_elem(point_z, point->Z)) {
be07ae9b 1340 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
4d3fa06f
AP
1341 return 0;
1342 }
1343
1344 ecp_nistz256_mod_inverse(z_inv3, point_z);
1345 ecp_nistz256_sqr_mont(z_inv2, z_inv3);
1346 ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
1347
1348 if (x != NULL) {
1349 bn_wexpand(x, P256_LIMBS);
5784a521
MC
1350 bn_set_top(x, P256_LIMBS);
1351 ecp_nistz256_from_mont(bn_get_words(x), x_aff);
4d3fa06f
AP
1352 bn_correct_top(x);
1353 }
1354
1355 if (y != NULL) {
1356 ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
1357 ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
1358 bn_wexpand(y, P256_LIMBS);
5784a521
MC
1359 bn_set_top(y, P256_LIMBS);
1360 ecp_nistz256_from_mont(bn_get_words(y), y_aff);
4d3fa06f
AP
1361 bn_correct_top(y);
1362 }
1363
1364 return 1;
1365}
1366
58d47cf0 1367static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
4d3fa06f
AP
1368{
1369 EC_PRE_COMP *ret = NULL;
1370
1371 if (!group)
1372 return NULL;
1373
58d47cf0 1374 ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
4d3fa06f
AP
1375
1376 if (!ret) {
be07ae9b 1377 ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
4d3fa06f
AP
1378 return ret;
1379 }
1380
1381 ret->group = group;
1382 ret->w = 6; /* default */
1383 ret->precomp = NULL;
1384 ret->precomp_storage = NULL;
1385 ret->references = 1;
1386 return ret;
1387}
1388
be07ae9b 1389static void *ecp_nistz256_pre_comp_dup(void *src_)
4d3fa06f
AP
1390{
1391 EC_PRE_COMP *src = src_;
1392
1393 /* no need to actually copy, these objects never change! */
1394 CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
1395
1396 return src_;
1397}
1398
be07ae9b 1399static void ecp_nistz256_pre_comp_free(void *pre_)
4d3fa06f
AP
1400{
1401 int i;
1402 EC_PRE_COMP *pre = pre_;
1403
1404 if (!pre)
1405 return;
1406
1407 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1408 if (i > 0)
1409 return;
1410
1411 if (pre->precomp_storage)
1412 OPENSSL_free(pre->precomp_storage);
1413
1414 OPENSSL_free(pre);
1415}
1416
be07ae9b 1417static void ecp_nistz256_pre_comp_clear_free(void *pre_)
4d3fa06f
AP
1418{
1419 int i;
1420 EC_PRE_COMP *pre = pre_;
1421
1422 if (!pre)
1423 return;
1424
1425 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1426 if (i > 0)
1427 return;
1428
1429 if (pre->precomp_storage) {
1430 OPENSSL_cleanse(pre->precomp,
1431 32 * sizeof(unsigned char) * (1 << pre->w) * 2 * 37);
1432 OPENSSL_free(pre->precomp_storage);
1433 }
1434 OPENSSL_cleanse(pre, sizeof *pre);
1435 OPENSSL_free(pre);
1436}
1437
58d47cf0 1438static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
4d3fa06f
AP
1439{
1440 /* There is a hard-coded table for the default generator. */
1441 const EC_POINT *generator = EC_GROUP_get0_generator(group);
1442 if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
1443 /* There is a hard-coded table for the default generator. */
1444 return 1;
1445 }
1446
be07ae9b
AP
1447 return EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1448 ecp_nistz256_pre_comp_free,
1449 ecp_nistz256_pre_comp_clear_free) != NULL;
4d3fa06f
AP
1450}
1451
1452const EC_METHOD *EC_GFp_nistz256_method(void)
1453{
1454 static const EC_METHOD ret = {
1455 EC_FLAGS_DEFAULT_OCT,
1456 NID_X9_62_prime_field,
1457 ec_GFp_mont_group_init,
1458 ec_GFp_mont_group_finish,
1459 ec_GFp_mont_group_clear_finish,
1460 ec_GFp_mont_group_copy,
1461 ec_GFp_mont_group_set_curve,
1462 ec_GFp_simple_group_get_curve,
1463 ec_GFp_simple_group_get_degree,
1464 ec_GFp_simple_group_check_discriminant,
1465 ec_GFp_simple_point_init,
1466 ec_GFp_simple_point_finish,
1467 ec_GFp_simple_point_clear_finish,
1468 ec_GFp_simple_point_copy,
1469 ec_GFp_simple_point_set_to_infinity,
1470 ec_GFp_simple_set_Jprojective_coordinates_GFp,
1471 ec_GFp_simple_get_Jprojective_coordinates_GFp,
1472 ec_GFp_simple_point_set_affine_coordinates,
1473 ecp_nistz256_get_affine,
1474 0, 0, 0,
1475 ec_GFp_simple_add,
1476 ec_GFp_simple_dbl,
1477 ec_GFp_simple_invert,
1478 ec_GFp_simple_is_at_infinity,
1479 ec_GFp_simple_is_on_curve,
1480 ec_GFp_simple_cmp,
1481 ec_GFp_simple_make_affine,
1482 ec_GFp_simple_points_make_affine,
1483 ecp_nistz256_points_mul, /* mul */
1484 ecp_nistz256_mult_precompute, /* precompute_mult */
1485 ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
1486 ec_GFp_mont_field_mul,
1487 ec_GFp_mont_field_sqr,
1488 0, /* field_div */
1489 ec_GFp_mont_field_encode,
1490 ec_GFp_mont_field_decode,
1491 ec_GFp_mont_field_set_to_one
1492 };
1493
1494 return &ret;
1495}