1 /******************************************************************************
3 * Copyright 2014 Intel Corporation *
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 *
9 * http://www.apache.org/licenses/LICENSE-2.0 *
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. *
17 ******************************************************************************
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 *
24 * S.Gueron and V.Krasnov, "Fast Prime Field Elliptic Curve Cryptography with *
27 ******************************************************************************/
31 #include <openssl/bn.h>
32 #include <openssl/err.h>
33 #include <openssl/ec.h>
39 # define TOBN(hi,lo) lo,hi
41 # define TOBN(hi,lo) ((BN_ULONG)hi<<32|lo)
45 # define ALIGN32 __attribute((aligned(32)))
46 #elif defined(_MSC_VER)
47 # define ALIGN32 __declspec(align(32))
52 #define ALIGNPTR(p,N) ((unsigned char *)p+N-(size_t)p%N)
53 #define P256_LIMBS (256/BN_BITS2)
55 typedef unsigned short u16;
58 BN_ULONG X[P256_LIMBS];
59 BN_ULONG Y[P256_LIMBS];
60 BN_ULONG Z[P256_LIMBS];
64 BN_ULONG X[P256_LIMBS];
65 BN_ULONG Y[P256_LIMBS];
68 typedef P256_POINT_AFFINE PRECOMP256_ROW[64];
70 /* structure for precomputed multiples of the generator */
71 typedef struct ec_pre_comp_st {
72 const EC_GROUP *group; /* Parent EC_GROUP object */
73 size_t w; /* Window size */
75 * Constant time access to the X and Y coordinates of the pre-computed,
76 * generator multiplies, in the Montgomery domain. Pre-calculated
77 * multiplies are stored in affine form.
79 PRECOMP256_ROW *precomp;
80 void *precomp_storage;
84 /* Functions implemented in assembly */
86 * Most of below mentioned functions *preserve* the property of inputs
87 * being fully reduced, i.e. being in [0, modulus) range. Simply put if
88 * inputs are fully reduced, then output is too. Note that reverse is
89 * not true, in sense that given partially reduced inputs output can be
90 * either, not unlikely reduced. And "most" in first sentence refers to
91 * the fact that given the calculations flow one can tolerate that
92 * addition, 1st function below, produces partially reduced result *if*
93 * multiplications by 2 and 3, which customarily use addition, fully
94 * reduce it. This effectively gives two options: a) addition produces
95 * fully reduced result [as long as inputs are, just like remaining
96 * functions]; b) addition is allowed to produce partially reduced
97 * result, but multiplications by 2 and 3 perform additional reduction
98 * step. Choice between the two can be platform-specific, but it was a)
99 * in all cases so far...
101 /* Modular add: res = a+b mod P */
102 void ecp_nistz256_add(BN_ULONG res[P256_LIMBS],
103 const BN_ULONG a[P256_LIMBS],
104 const BN_ULONG b[P256_LIMBS]);
105 /* Modular mul by 2: res = 2*a mod P */
106 void ecp_nistz256_mul_by_2(BN_ULONG res[P256_LIMBS],
107 const BN_ULONG a[P256_LIMBS]);
108 /* Modular mul by 3: res = 3*a mod P */
109 void ecp_nistz256_mul_by_3(BN_ULONG res[P256_LIMBS],
110 const BN_ULONG a[P256_LIMBS]);
112 /* Modular div by 2: res = a/2 mod P */
113 void ecp_nistz256_div_by_2(BN_ULONG res[P256_LIMBS],
114 const BN_ULONG a[P256_LIMBS]);
115 /* Modular sub: res = a-b mod P */
116 void ecp_nistz256_sub(BN_ULONG res[P256_LIMBS],
117 const BN_ULONG a[P256_LIMBS],
118 const BN_ULONG b[P256_LIMBS]);
119 /* Modular neg: res = -a mod P */
120 void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
121 /* Montgomery mul: res = a*b*2^-256 mod P */
122 void ecp_nistz256_mul_mont(BN_ULONG res[P256_LIMBS],
123 const BN_ULONG a[P256_LIMBS],
124 const BN_ULONG b[P256_LIMBS]);
125 /* Montgomery sqr: res = a*a*2^-256 mod P */
126 void ecp_nistz256_sqr_mont(BN_ULONG res[P256_LIMBS],
127 const BN_ULONG a[P256_LIMBS]);
128 /* Convert a number from Montgomery domain, by multiplying with 1 */
129 void ecp_nistz256_from_mont(BN_ULONG res[P256_LIMBS],
130 const BN_ULONG in[P256_LIMBS]);
131 /* Convert a number to Montgomery domain, by multiplying with 2^512 mod P*/
132 void ecp_nistz256_to_mont(BN_ULONG res[P256_LIMBS],
133 const BN_ULONG in[P256_LIMBS]);
134 /* Functions that perform constant time access to the precomputed tables */
135 void ecp_nistz256_select_w5(P256_POINT * val,
136 const P256_POINT * in_t, int index);
137 void ecp_nistz256_select_w7(P256_POINT_AFFINE * val,
138 const P256_POINT_AFFINE * in_t, int index);
140 /* One converted into the Montgomery domain */
141 static const BN_ULONG ONE[P256_LIMBS] = {
142 TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
143 TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
146 static void *ecp_nistz256_pre_comp_dup(void *);
147 static void ecp_nistz256_pre_comp_free(void *);
148 static void ecp_nistz256_pre_comp_clear_free(void *);
149 static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group);
151 /* Precomputed tables for the default generator */
152 #include "ecp_nistz256_table.c"
154 /* Recode window to a signed digit, see ecp_nistputil.c for details */
155 static unsigned int _booth_recode_w5(unsigned int in)
159 s = ~((in >> 5) - 1);
160 d = (1 << 6) - in - 1;
161 d = (d & s) | (in & ~s);
162 d = (d >> 1) + (d & 1);
164 return (d << 1) + (s & 1);
167 static unsigned int _booth_recode_w7(unsigned int in)
171 s = ~((in >> 7) - 1);
172 d = (1 << 8) - in - 1;
173 d = (d & s) | (in & ~s);
174 d = (d >> 1) + (d & 1);
176 return (d << 1) + (s & 1);
179 static void copy_conditional(BN_ULONG dst[P256_LIMBS],
180 const BN_ULONG src[P256_LIMBS], BN_ULONG move)
182 BN_ULONG mask1 = -move;
183 BN_ULONG mask2 = ~mask1;
185 dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
186 dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
187 dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
188 dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
189 if (P256_LIMBS == 8) {
190 dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
191 dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
192 dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
193 dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
197 static BN_ULONG is_zero(BN_ULONG in)
206 static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
207 const BN_ULONG b[P256_LIMBS])
215 if (P256_LIMBS == 8) {
225 static BN_ULONG is_one(const BIGNUM *z)
230 if (z->top == (P256_LIMBS - P256_LIMBS / 8)) {
232 res |= a[1] ^ ONE[1];
233 res |= a[2] ^ ONE[2];
234 res |= a[3] ^ ONE[3];
235 if (P256_LIMBS == 8) {
236 res |= a[4] ^ ONE[4];
237 res |= a[5] ^ ONE[5];
238 res |= a[6] ^ ONE[6];
240 * no check for a[7] (being zero) on 32-bit platforms,
241 * because value of "one" takes only 7 limbs.
250 static int ecp_nistz256_set_words(BIGNUM *a, BN_ULONG words[P256_LIMBS])
252 if (bn_wexpand(a, P256_LIMBS) == NULL) {
253 ECerr(EC_F_ECP_NISTZ256_SET_WORDS, ERR_R_MALLOC_FAILURE);
256 memcpy(a->d, words, sizeof(BN_ULONG) * P256_LIMBS);
262 #ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
263 void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a);
264 void ecp_nistz256_point_add(P256_POINT *r,
265 const P256_POINT *a, const P256_POINT *b);
266 void ecp_nistz256_point_add_affine(P256_POINT *r,
268 const P256_POINT_AFFINE *b);
270 /* Point double: r = 2*a */
271 static void ecp_nistz256_point_double(P256_POINT *r, const P256_POINT *a)
273 BN_ULONG S[P256_LIMBS];
274 BN_ULONG M[P256_LIMBS];
275 BN_ULONG Zsqr[P256_LIMBS];
276 BN_ULONG tmp0[P256_LIMBS];
278 const BN_ULONG *in_x = a->X;
279 const BN_ULONG *in_y = a->Y;
280 const BN_ULONG *in_z = a->Z;
282 BN_ULONG *res_x = r->X;
283 BN_ULONG *res_y = r->Y;
284 BN_ULONG *res_z = r->Z;
286 ecp_nistz256_mul_by_2(S, in_y);
288 ecp_nistz256_sqr_mont(Zsqr, in_z);
290 ecp_nistz256_sqr_mont(S, S);
292 ecp_nistz256_mul_mont(res_z, in_z, in_y);
293 ecp_nistz256_mul_by_2(res_z, res_z);
295 ecp_nistz256_add(M, in_x, Zsqr);
296 ecp_nistz256_sub(Zsqr, in_x, Zsqr);
298 ecp_nistz256_sqr_mont(res_y, S);
299 ecp_nistz256_div_by_2(res_y, res_y);
301 ecp_nistz256_mul_mont(M, M, Zsqr);
302 ecp_nistz256_mul_by_3(M, M);
304 ecp_nistz256_mul_mont(S, S, in_x);
305 ecp_nistz256_mul_by_2(tmp0, S);
307 ecp_nistz256_sqr_mont(res_x, M);
309 ecp_nistz256_sub(res_x, res_x, tmp0);
310 ecp_nistz256_sub(S, S, res_x);
312 ecp_nistz256_mul_mont(S, S, M);
313 ecp_nistz256_sub(res_y, S, res_y);
316 /* Point addition: r = a+b */
317 static void ecp_nistz256_point_add(P256_POINT *r,
318 const P256_POINT *a, const P256_POINT *b)
320 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
321 BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
322 BN_ULONG Z1sqr[P256_LIMBS];
323 BN_ULONG Z2sqr[P256_LIMBS];
324 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
325 BN_ULONG Hsqr[P256_LIMBS];
326 BN_ULONG Rsqr[P256_LIMBS];
327 BN_ULONG Hcub[P256_LIMBS];
329 BN_ULONG res_x[P256_LIMBS];
330 BN_ULONG res_y[P256_LIMBS];
331 BN_ULONG res_z[P256_LIMBS];
333 BN_ULONG in1infty, in2infty;
335 const BN_ULONG *in1_x = a->X;
336 const BN_ULONG *in1_y = a->Y;
337 const BN_ULONG *in1_z = a->Z;
339 const BN_ULONG *in2_x = b->X;
340 const BN_ULONG *in2_y = b->Y;
341 const BN_ULONG *in2_z = b->Z;
344 * Infinity in encoded as (,,0)
346 in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
348 in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
350 in2infty = (in2_z[0] | in2_z[1] | in2_z[2] | in2_z[3]);
352 in2infty |= (in2_z[4] | in2_z[5] | in2_z[6] | in2_z[7]);
354 in1infty = is_zero(in1infty);
355 in2infty = is_zero(in2infty);
357 ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
358 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
360 ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
361 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
363 ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
364 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
365 ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
367 ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
368 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
369 ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
372 * This should not happen during sign/ecdh, so no constant time violation
374 if (is_equal(U1, U2) && !in1infty && !in2infty) {
375 if (is_equal(S1, S2)) {
376 ecp_nistz256_point_double(r, a);
379 memset(r, 0, sizeof(*r));
384 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
385 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
386 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
387 ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
388 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
390 ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
391 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
393 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
394 ecp_nistz256_sub(res_x, res_x, Hcub);
396 ecp_nistz256_sub(res_y, U2, res_x);
398 ecp_nistz256_mul_mont(S2, S1, Hcub);
399 ecp_nistz256_mul_mont(res_y, R, res_y);
400 ecp_nistz256_sub(res_y, res_y, S2);
402 copy_conditional(res_x, in2_x, in1infty);
403 copy_conditional(res_y, in2_y, in1infty);
404 copy_conditional(res_z, in2_z, in1infty);
406 copy_conditional(res_x, in1_x, in2infty);
407 copy_conditional(res_y, in1_y, in2infty);
408 copy_conditional(res_z, in1_z, in2infty);
410 memcpy(r->X, res_x, sizeof(res_x));
411 memcpy(r->Y, res_y, sizeof(res_y));
412 memcpy(r->Z, res_z, sizeof(res_z));
415 /* Point addition when b is known to be affine: r = a+b */
416 static void ecp_nistz256_point_add_affine(P256_POINT *r,
418 const P256_POINT_AFFINE *b)
420 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
421 BN_ULONG Z1sqr[P256_LIMBS];
422 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
423 BN_ULONG Hsqr[P256_LIMBS];
424 BN_ULONG Rsqr[P256_LIMBS];
425 BN_ULONG Hcub[P256_LIMBS];
427 BN_ULONG res_x[P256_LIMBS];
428 BN_ULONG res_y[P256_LIMBS];
429 BN_ULONG res_z[P256_LIMBS];
431 BN_ULONG in1infty, in2infty;
433 const BN_ULONG *in1_x = a->X;
434 const BN_ULONG *in1_y = a->Y;
435 const BN_ULONG *in1_z = a->Z;
437 const BN_ULONG *in2_x = b->X;
438 const BN_ULONG *in2_y = b->Y;
441 * Infinity in encoded as (,,0)
443 in1infty = (in1_z[0] | in1_z[1] | in1_z[2] | in1_z[3]);
445 in1infty |= (in1_z[4] | in1_z[5] | in1_z[6] | in1_z[7]);
448 * In affine representation we encode infinity as (0,0), which is
449 * not on the curve, so it is OK
451 in2infty = (in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
452 in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3]);
454 in2infty |= (in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
455 in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7]);
457 in1infty = is_zero(in1infty);
458 in2infty = is_zero(in2infty);
460 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
462 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
463 ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
465 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
467 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
469 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
470 ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
472 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
473 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
474 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
476 ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
477 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
479 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
480 ecp_nistz256_sub(res_x, res_x, Hcub);
481 ecp_nistz256_sub(H, U2, res_x);
483 ecp_nistz256_mul_mont(S2, in1_y, Hcub);
484 ecp_nistz256_mul_mont(H, H, R);
485 ecp_nistz256_sub(res_y, H, S2);
487 copy_conditional(res_x, in2_x, in1infty);
488 copy_conditional(res_x, in1_x, in2infty);
490 copy_conditional(res_y, in2_y, in1infty);
491 copy_conditional(res_y, in1_y, in2infty);
493 copy_conditional(res_z, ONE, in1infty);
494 copy_conditional(res_z, in1_z, in2infty);
496 memcpy(r->X, res_x, sizeof(res_x));
497 memcpy(r->Y, res_y, sizeof(res_y));
498 memcpy(r->Z, res_z, sizeof(res_z));
502 /* r = in^-1 mod p */
503 static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
504 const BN_ULONG in[P256_LIMBS])
507 * The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff
508 * ffffffff ffffffff We use FLT and used poly-2 as exponent
510 BN_ULONG p2[P256_LIMBS];
511 BN_ULONG p4[P256_LIMBS];
512 BN_ULONG p8[P256_LIMBS];
513 BN_ULONG p16[P256_LIMBS];
514 BN_ULONG p32[P256_LIMBS];
515 BN_ULONG res[P256_LIMBS];
518 ecp_nistz256_sqr_mont(res, in);
519 ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
521 ecp_nistz256_sqr_mont(res, p2);
522 ecp_nistz256_sqr_mont(res, res);
523 ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
525 ecp_nistz256_sqr_mont(res, p4);
526 ecp_nistz256_sqr_mont(res, res);
527 ecp_nistz256_sqr_mont(res, res);
528 ecp_nistz256_sqr_mont(res, res);
529 ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
531 ecp_nistz256_sqr_mont(res, p8);
532 for (i = 0; i < 7; i++)
533 ecp_nistz256_sqr_mont(res, res);
534 ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
536 ecp_nistz256_sqr_mont(res, p16);
537 for (i = 0; i < 15; i++)
538 ecp_nistz256_sqr_mont(res, res);
539 ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
541 ecp_nistz256_sqr_mont(res, p32);
542 for (i = 0; i < 31; i++)
543 ecp_nistz256_sqr_mont(res, res);
544 ecp_nistz256_mul_mont(res, res, in);
546 for (i = 0; i < 32 * 4; i++)
547 ecp_nistz256_sqr_mont(res, res);
548 ecp_nistz256_mul_mont(res, res, p32);
550 for (i = 0; i < 32; i++)
551 ecp_nistz256_sqr_mont(res, res);
552 ecp_nistz256_mul_mont(res, res, p32);
554 for (i = 0; i < 16; i++)
555 ecp_nistz256_sqr_mont(res, res);
556 ecp_nistz256_mul_mont(res, res, p16);
558 for (i = 0; i < 8; i++)
559 ecp_nistz256_sqr_mont(res, res);
560 ecp_nistz256_mul_mont(res, res, p8);
562 ecp_nistz256_sqr_mont(res, res);
563 ecp_nistz256_sqr_mont(res, res);
564 ecp_nistz256_sqr_mont(res, res);
565 ecp_nistz256_sqr_mont(res, res);
566 ecp_nistz256_mul_mont(res, res, p4);
568 ecp_nistz256_sqr_mont(res, res);
569 ecp_nistz256_sqr_mont(res, res);
570 ecp_nistz256_mul_mont(res, res, p2);
572 ecp_nistz256_sqr_mont(res, res);
573 ecp_nistz256_sqr_mont(res, res);
574 ecp_nistz256_mul_mont(res, res, in);
576 memcpy(r, res, sizeof(res));
580 * ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
581 * returns one if it fits. Otherwise it returns zero.
583 static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
586 if (in->top > P256_LIMBS)
589 memset(out, 0, sizeof(BN_ULONG) * P256_LIMBS);
590 memcpy(out, in->d, sizeof(BN_ULONG) * in->top);
594 /* r = sum(scalar[i]*point[i]) */
595 static int ecp_nistz256_windowed_mul(const EC_GROUP *group,
597 const BIGNUM **scalar,
598 const EC_POINT **point,
599 int num, BN_CTX *ctx)
604 unsigned char (*p_str)[33] = NULL;
605 const unsigned int window_size = 5;
606 const unsigned int mask = (1 << (window_size + 1)) - 1;
608 BN_ULONG tmp[P256_LIMBS];
609 ALIGN32 P256_POINT h;
610 const BIGNUM **scalars = NULL;
611 P256_POINT (*table)[16] = NULL;
612 void *table_storage = NULL;
615 OPENSSL_malloc(num * 16 * sizeof(P256_POINT) + 64)) == NULL
617 OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
618 || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
619 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
622 table = (void *)ALIGNPTR(table_storage, 64);
625 for (i = 0; i < num; i++) {
626 P256_POINT *row = table[i];
628 /* This is an unusual input, we don't guarantee constant-timeness. */
629 if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
632 if ((mod = BN_CTX_get(ctx)) == NULL)
634 if (!BN_nnmod(mod, scalar[i], &group->order, ctx)) {
635 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
640 scalars[i] = scalar[i];
642 for (j = 0; j < scalars[i]->top * BN_BYTES; j += BN_BYTES) {
643 BN_ULONG d = scalars[i]->d[j / BN_BYTES];
645 p_str[i][j + 0] = d & 0xff;
646 p_str[i][j + 1] = (d >> 8) & 0xff;
647 p_str[i][j + 2] = (d >> 16) & 0xff;
648 p_str[i][j + 3] = (d >>= 24) & 0xff;
651 p_str[i][j + 4] = d & 0xff;
652 p_str[i][j + 5] = (d >> 8) & 0xff;
653 p_str[i][j + 6] = (d >> 16) & 0xff;
654 p_str[i][j + 7] = (d >> 24) & 0xff;
660 /* table[0] is implicitly (0,0,0) (the point at infinity),
661 * therefore it is not stored. All other values are actually
662 * stored with an offset of -1 in table.
665 if (!ecp_nistz256_bignum_to_field_elem(row[1 - 1].X, &point[i]->X)
666 || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Y, &point[i]->Y)
667 || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Z, &point[i]->Z)) {
668 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, EC_R_COORDINATES_OUT_OF_RANGE);
672 ecp_nistz256_point_double(&row[ 2 - 1], &row[ 1 - 1]);
673 ecp_nistz256_point_add (&row[ 3 - 1], &row[ 2 - 1], &row[1 - 1]);
674 ecp_nistz256_point_double(&row[ 4 - 1], &row[ 2 - 1]);
675 ecp_nistz256_point_double(&row[ 6 - 1], &row[ 3 - 1]);
676 ecp_nistz256_point_double(&row[ 8 - 1], &row[ 4 - 1]);
677 ecp_nistz256_point_double(&row[12 - 1], &row[ 6 - 1]);
678 ecp_nistz256_point_add (&row[ 5 - 1], &row[ 4 - 1], &row[1 - 1]);
679 ecp_nistz256_point_add (&row[ 7 - 1], &row[ 6 - 1], &row[1 - 1]);
680 ecp_nistz256_point_add (&row[ 9 - 1], &row[ 8 - 1], &row[1 - 1]);
681 ecp_nistz256_point_add (&row[13 - 1], &row[12 - 1], &row[1 - 1]);
682 ecp_nistz256_point_double(&row[14 - 1], &row[ 7 - 1]);
683 ecp_nistz256_point_double(&row[10 - 1], &row[ 5 - 1]);
684 ecp_nistz256_point_add (&row[15 - 1], &row[14 - 1], &row[1 - 1]);
685 ecp_nistz256_point_add (&row[11 - 1], &row[10 - 1], &row[1 - 1]);
686 ecp_nistz256_point_add (&row[16 - 1], &row[15 - 1], &row[1 - 1]);
691 wvalue = p_str[0][(index - 1) / 8];
692 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
694 ecp_nistz256_select_w5(r, table[0], _booth_recode_w5(wvalue) >> 1);
697 for (i = (index == 255 ? 1 : 0); i < num; i++) {
698 unsigned int off = (index - 1) / 8;
700 wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
701 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
703 wvalue = _booth_recode_w5(wvalue);
705 ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
707 ecp_nistz256_neg(tmp, h.Y);
708 copy_conditional(h.Y, tmp, (wvalue & 1));
710 ecp_nistz256_point_add(r, r, &h);
713 index -= window_size;
715 ecp_nistz256_point_double(r, r);
716 ecp_nistz256_point_double(r, r);
717 ecp_nistz256_point_double(r, r);
718 ecp_nistz256_point_double(r, r);
719 ecp_nistz256_point_double(r, r);
723 for (i = 0; i < num; i++) {
724 wvalue = p_str[i][0];
725 wvalue = (wvalue << 1) & mask;
727 wvalue = _booth_recode_w5(wvalue);
729 ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
731 ecp_nistz256_neg(tmp, h.Y);
732 copy_conditional(h.Y, tmp, wvalue & 1);
734 ecp_nistz256_point_add(r, r, &h);
740 OPENSSL_free(table_storage);
744 OPENSSL_free(scalars);
748 /* Coordinates of G, for which we have precomputed tables */
749 const static BN_ULONG def_xG[P256_LIMBS] = {
750 TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
751 TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
754 const static BN_ULONG def_yG[P256_LIMBS] = {
755 TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
756 TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
760 * ecp_nistz256_is_affine_G returns one if |generator| is the standard, P-256
763 static int ecp_nistz256_is_affine_G(const EC_POINT *generator)
765 return (generator->X.top == P256_LIMBS) &&
766 (generator->Y.top == P256_LIMBS) &&
767 is_equal(generator->X.d, def_xG) &&
768 is_equal(generator->Y.d, def_yG) && is_one(&generator->Z);
771 static int ecp_nistz256_mult_precompute(EC_GROUP *group, BN_CTX *ctx)
774 * We precompute a table for a Booth encoded exponent (wNAF) based
775 * computation. Each table holds 64 values for safe access, with an
776 * implicit value of infinity at index zero. We use window of size 7, and
777 * therefore require ceil(256/7) = 37 tables.
780 EC_POINT *P = NULL, *T = NULL;
781 const EC_POINT *generator;
782 EC_PRE_COMP *pre_comp;
783 BN_CTX *new_ctx = NULL;
784 int i, j, k, ret = 0;
787 PRECOMP256_ROW *preComputedTable = NULL;
788 unsigned char *precomp_storage = NULL;
790 /* if there is an old EC_PRE_COMP object, throw it away */
791 EC_EX_DATA_free_data(&group->extra_data, ecp_nistz256_pre_comp_dup,
792 ecp_nistz256_pre_comp_free,
793 ecp_nistz256_pre_comp_clear_free);
795 generator = EC_GROUP_get0_generator(group);
796 if (generator == NULL) {
797 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
801 if (ecp_nistz256_is_affine_G(generator)) {
803 * No need to calculate tables for the standard generator because we
804 * have them statically.
809 if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
813 ctx = new_ctx = BN_CTX_new();
819 order = BN_CTX_get(ctx);
824 if (!EC_GROUP_get_order(group, order, ctx))
827 if (BN_is_zero(order)) {
828 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
834 if ((precomp_storage =
835 OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
836 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
839 preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
842 P = EC_POINT_new(group);
843 T = EC_POINT_new(group);
844 if (P == NULL || T == NULL)
848 * The zero entry is implicitly infinity, and we skip it, storing other
849 * values with -1 offset.
851 if (!EC_POINT_copy(T, generator))
854 for (k = 0; k < 64; k++) {
855 if (!EC_POINT_copy(P, T))
857 for (j = 0; j < 37; j++) {
859 * It would be faster to use EC_POINTs_make_affine and
860 * make multiple points affine at the same time.
862 if (!EC_POINT_make_affine(group, P, ctx))
864 if (!ecp_nistz256_bignum_to_field_elem(preComputedTable[j][k].X,
866 !ecp_nistz256_bignum_to_field_elem(preComputedTable[j][k].Y,
868 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE,
869 EC_R_COORDINATES_OUT_OF_RANGE);
872 for (i = 0; i < 7; i++) {
873 if (!EC_POINT_dbl(group, P, P, ctx))
877 if (!EC_POINT_add(group, T, T, generator, ctx))
881 pre_comp->group = group;
883 pre_comp->precomp = preComputedTable;
884 pre_comp->precomp_storage = precomp_storage;
886 precomp_storage = NULL;
888 if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
889 ecp_nistz256_pre_comp_dup,
890 ecp_nistz256_pre_comp_free,
891 ecp_nistz256_pre_comp_clear_free)) {
902 BN_CTX_free(new_ctx);
905 ecp_nistz256_pre_comp_free(pre_comp);
907 OPENSSL_free(precomp_storage);
916 * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
917 * code processing 4 points in parallel, corresponding serial operation
918 * is several times slower, because it uses 29x29=58-bit multiplication
919 * as opposite to 64x64=128-bit in integer-only scalar case. As result
920 * it doesn't provide *significant* performance improvement. Note that
921 * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
922 * you'd need to compile even asm/ecp_nistz256-avx.pl module.
924 #if defined(ECP_NISTZ256_AVX2)
925 # if !(defined(__x86_64) || defined(__x86_64__)) || \
926 defined(_M_AMD64) || defined(_MX64)) || \
927 !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
928 # undef ECP_NISTZ256_AVX2
930 /* Constant time access, loading four values, from four consecutive tables */
931 void ecp_nistz256_avx2_select_w7(P256_POINT_AFFINE * val,
932 const P256_POINT_AFFINE * in_t, int index);
933 void ecp_nistz256_avx2_multi_select_w7(void *result, const void *in, int index0,
934 int index1, int index2, int index3);
935 void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
936 void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
937 void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
939 void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
941 void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
942 void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
943 void ecp_nistz256_avx2_set1(void *RESULTx4);
944 int ecp_nistz_avx2_eligible(void);
946 static void booth_recode_w7(unsigned char *sign,
947 unsigned char *digit, unsigned char in)
951 s = ~((in >> 7) - 1);
952 d = (1 << 8) - in - 1;
953 d = (d & s) | (in & ~s);
954 d = (d >> 1) + (d & 1);
961 * ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
962 * precomputed table. It does 4 affine point additions in parallel,
963 * significantly speeding up point multiplication for a fixed value.
965 static void ecp_nistz256_avx2_mul_g(P256_POINT *r,
966 unsigned char p_str[33],
967 const P256_POINT_AFFINE(*preComputedTable)[64])
969 const unsigned int window_size = 7;
970 const unsigned int mask = (1 << (window_size + 1)) - 1;
972 /* Using 4 windows at a time */
973 unsigned char sign0, digit0;
974 unsigned char sign1, digit1;
975 unsigned char sign2, digit2;
976 unsigned char sign3, digit3;
977 unsigned int index = 0;
978 BN_ULONG tmp[P256_LIMBS];
981 ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
982 ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
983 ALIGN32 P256_POINT_AFFINE point_arr[P256_LIMBS];
984 ALIGN32 P256_POINT res_point_arr[P256_LIMBS];
986 /* Initial four windows */
987 wvalue = *((u16 *) & p_str[0]);
988 wvalue = (wvalue << 1) & mask;
989 index += window_size;
990 booth_recode_w7(&sign0, &digit0, wvalue);
991 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
992 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
993 index += window_size;
994 booth_recode_w7(&sign1, &digit1, wvalue);
995 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
996 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
997 index += window_size;
998 booth_recode_w7(&sign2, &digit2, wvalue);
999 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1000 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1001 index += window_size;
1002 booth_recode_w7(&sign3, &digit3, wvalue);
1004 ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[0],
1005 digit0, digit1, digit2, digit3);
1007 ecp_nistz256_neg(tmp, point_arr[0].Y);
1008 copy_conditional(point_arr[0].Y, tmp, sign0);
1009 ecp_nistz256_neg(tmp, point_arr[1].Y);
1010 copy_conditional(point_arr[1].Y, tmp, sign1);
1011 ecp_nistz256_neg(tmp, point_arr[2].Y);
1012 copy_conditional(point_arr[2].Y, tmp, sign2);
1013 ecp_nistz256_neg(tmp, point_arr[3].Y);
1014 copy_conditional(point_arr[3].Y, tmp, sign3);
1016 ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
1017 ecp_nistz256_avx2_to_mont(aX4, aX4);
1018 ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
1019 ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
1021 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1022 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1023 index += window_size;
1024 booth_recode_w7(&sign0, &digit0, wvalue);
1025 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1026 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1027 index += window_size;
1028 booth_recode_w7(&sign1, &digit1, wvalue);
1029 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1030 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1031 index += window_size;
1032 booth_recode_w7(&sign2, &digit2, wvalue);
1033 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1034 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1035 index += window_size;
1036 booth_recode_w7(&sign3, &digit3, wvalue);
1038 ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[4 * 1],
1039 digit0, digit1, digit2, digit3);
1041 ecp_nistz256_neg(tmp, point_arr[0].Y);
1042 copy_conditional(point_arr[0].Y, tmp, sign0);
1043 ecp_nistz256_neg(tmp, point_arr[1].Y);
1044 copy_conditional(point_arr[1].Y, tmp, sign1);
1045 ecp_nistz256_neg(tmp, point_arr[2].Y);
1046 copy_conditional(point_arr[2].Y, tmp, sign2);
1047 ecp_nistz256_neg(tmp, point_arr[3].Y);
1048 copy_conditional(point_arr[3].Y, tmp, sign3);
1050 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
1051 ecp_nistz256_avx2_to_mont(bX4, bX4);
1052 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
1053 /* Optimized when both inputs are affine */
1054 ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
1056 for (i = 2; i < 9; i++) {
1057 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1058 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1059 index += window_size;
1060 booth_recode_w7(&sign0, &digit0, wvalue);
1061 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1062 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1063 index += window_size;
1064 booth_recode_w7(&sign1, &digit1, wvalue);
1065 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1066 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1067 index += window_size;
1068 booth_recode_w7(&sign2, &digit2, wvalue);
1069 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1070 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1071 index += window_size;
1072 booth_recode_w7(&sign3, &digit3, wvalue);
1074 ecp_nistz256_avx2_multi_select_w7(point_arr,
1075 preComputedTable[4 * i],
1076 digit0, digit1, digit2, digit3);
1078 ecp_nistz256_neg(tmp, point_arr[0].Y);
1079 copy_conditional(point_arr[0].Y, tmp, sign0);
1080 ecp_nistz256_neg(tmp, point_arr[1].Y);
1081 copy_conditional(point_arr[1].Y, tmp, sign1);
1082 ecp_nistz256_neg(tmp, point_arr[2].Y);
1083 copy_conditional(point_arr[2].Y, tmp, sign2);
1084 ecp_nistz256_neg(tmp, point_arr[3].Y);
1085 copy_conditional(point_arr[3].Y, tmp, sign3);
1087 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
1088 ecp_nistz256_avx2_to_mont(bX4, bX4);
1089 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
1091 ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
1094 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
1095 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
1096 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
1098 ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
1099 /* Last window is performed serially */
1100 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1101 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1102 booth_recode_w7(&sign0, &digit0, wvalue);
1103 ecp_nistz256_avx2_select_w7((P256_POINT_AFFINE *) r,
1104 preComputedTable[36], digit0);
1105 ecp_nistz256_neg(tmp, r->Y);
1106 copy_conditional(r->Y, tmp, sign0);
1107 memcpy(r->Z, ONE, sizeof(ONE));
1108 /* Sum the four windows */
1109 ecp_nistz256_point_add(r, r, &res_point_arr[0]);
1110 ecp_nistz256_point_add(r, r, &res_point_arr[1]);
1111 ecp_nistz256_point_add(r, r, &res_point_arr[2]);
1112 ecp_nistz256_point_add(r, r, &res_point_arr[3]);
1117 static int ecp_nistz256_set_from_affine(EC_POINT *out, const EC_GROUP *group,
1118 const P256_POINT_AFFINE *in,
1122 BN_ULONG d_x[P256_LIMBS], d_y[P256_LIMBS];
1125 memcpy(d_x, in->X, sizeof(d_x));
1127 x.dmax = x.top = P256_LIMBS;
1129 x.flags = BN_FLG_STATIC_DATA;
1131 memcpy(d_y, in->Y, sizeof(d_y));
1133 y.dmax = y.top = P256_LIMBS;
1135 y.flags = BN_FLG_STATIC_DATA;
1137 ret = EC_POINT_set_affine_coordinates_GFp(group, out, &x, &y, ctx);
1142 /* r = scalar*G + sum(scalars[i]*points[i]) */
1143 static int ecp_nistz256_points_mul(const EC_GROUP *group,
1145 const BIGNUM *scalar,
1147 const EC_POINT *points[],
1148 const BIGNUM *scalars[], BN_CTX *ctx)
1150 int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
1152 unsigned char p_str[33] = { 0 };
1153 const PRECOMP256_ROW *preComputedTable = NULL;
1154 const EC_PRE_COMP *pre_comp = NULL;
1155 const EC_POINT *generator = NULL;
1156 unsigned int index = 0;
1157 BN_CTX *new_ctx = NULL;
1158 const BIGNUM **new_scalars = NULL;
1159 const EC_POINT **new_points = NULL;
1160 const unsigned int window_size = 7;
1161 const unsigned int mask = (1 << (window_size + 1)) - 1;
1162 unsigned int wvalue;
1165 P256_POINT_AFFINE a;
1169 if (group->meth != r->meth) {
1170 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1174 if ((scalar == NULL) && (num == 0))
1175 return EC_POINT_set_to_infinity(group, r);
1177 for (j = 0; j < num; j++) {
1178 if (group->meth != points[j]->meth) {
1179 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1185 ctx = new_ctx = BN_CTX_new();
1193 generator = EC_GROUP_get0_generator(group);
1194 if (generator == NULL) {
1195 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
1199 /* look if we can use precomputed multiples of generator */
1201 EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1202 ecp_nistz256_pre_comp_free,
1203 ecp_nistz256_pre_comp_clear_free);
1207 * If there is a precomputed table for the generator, check that
1208 * it was generated with the same generator.
1210 EC_POINT *pre_comp_generator = EC_POINT_new(group);
1211 if (pre_comp_generator == NULL)
1214 if (!ecp_nistz256_set_from_affine
1215 (pre_comp_generator, group, pre_comp->precomp[0], ctx)) {
1216 EC_POINT_free(pre_comp_generator);
1220 if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
1221 preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
1223 EC_POINT_free(pre_comp_generator);
1226 if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
1228 * If there is no precomputed data, but the generator
1229 * is the default, a hardcoded table of precomputed
1230 * data is used. This is because applications, such as
1231 * Apache, do not use EC_KEY_precompute_mult.
1233 preComputedTable = (const PRECOMP256_ROW *)ecp_nistz256_precomputed;
1236 if (preComputedTable) {
1237 if ((BN_num_bits(scalar) > 256)
1238 || BN_is_negative(scalar)) {
1239 if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
1242 if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
1243 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
1246 scalar = tmp_scalar;
1249 for (i = 0; i < scalar->top * BN_BYTES; i += BN_BYTES) {
1250 BN_ULONG d = scalar->d[i / BN_BYTES];
1252 p_str[i + 0] = d & 0xff;
1253 p_str[i + 1] = (d >> 8) & 0xff;
1254 p_str[i + 2] = (d >> 16) & 0xff;
1255 p_str[i + 3] = (d >>= 24) & 0xff;
1256 if (BN_BYTES == 8) {
1258 p_str[i + 4] = d & 0xff;
1259 p_str[i + 5] = (d >> 8) & 0xff;
1260 p_str[i + 6] = (d >> 16) & 0xff;
1261 p_str[i + 7] = (d >> 24) & 0xff;
1268 #if defined(ECP_NISTZ256_AVX2)
1269 if (ecp_nistz_avx2_eligible()) {
1270 ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
1277 wvalue = (p_str[0] << 1) & mask;
1278 index += window_size;
1280 wvalue = _booth_recode_w7(wvalue);
1282 ecp_nistz256_select_w7(&p.a, preComputedTable[0], wvalue >> 1);
1284 ecp_nistz256_neg(p.p.Z, p.p.Y);
1285 copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
1288 * Since affine infinity is encoded as (0,0) and
1289 * Jacobian ias (,,0), we need to harmonize them
1290 * by assigning "one" or zero to Z.
1292 infty = (p.p.X[0] | p.p.X[1] | p.p.X[2] | p.p.X[3] |
1293 p.p.Y[0] | p.p.Y[1] | p.p.Y[2] | p.p.Y[3]);
1294 if (P256_LIMBS == 8)
1295 infty |= (p.p.X[4] | p.p.X[5] | p.p.X[6] | p.p.X[7] |
1296 p.p.Y[4] | p.p.Y[5] | p.p.Y[6] | p.p.Y[7]);
1298 infty = 0 - is_zero(infty);
1301 p.p.Z[0] = ONE[0] & infty;
1302 p.p.Z[1] = ONE[1] & infty;
1303 p.p.Z[2] = ONE[2] & infty;
1304 p.p.Z[3] = ONE[3] & infty;
1305 if (P256_LIMBS == 8) {
1306 p.p.Z[4] = ONE[4] & infty;
1307 p.p.Z[5] = ONE[5] & infty;
1308 p.p.Z[6] = ONE[6] & infty;
1309 p.p.Z[7] = ONE[7] & infty;
1312 for (i = 1; i < 37; i++) {
1313 unsigned int off = (index - 1) / 8;
1314 wvalue = p_str[off] | p_str[off + 1] << 8;
1315 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1316 index += window_size;
1318 wvalue = _booth_recode_w7(wvalue);
1320 ecp_nistz256_select_w7(&t.a,
1321 preComputedTable[i], wvalue >> 1);
1323 ecp_nistz256_neg(t.p.Z, t.a.Y);
1324 copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
1326 ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
1331 no_precomp_for_generator = 1;
1336 if (no_precomp_for_generator) {
1338 * Without a precomputed table for the generator, it has to be
1339 * handled like a normal point.
1341 new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
1343 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1347 new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
1349 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1353 memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
1354 new_scalars[num] = scalar;
1355 memcpy(new_points, points, num * sizeof(EC_POINT *));
1356 new_points[num] = generator;
1358 scalars = new_scalars;
1359 points = new_points;
1364 P256_POINT *out = &t.p;
1368 if (!ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx))
1372 ecp_nistz256_point_add(&p.p, &p.p, out);
1375 /* Not constant-time, but we're only operating on the public output. */
1376 if (!ecp_nistz256_set_words(&r->X, p.p.X) ||
1377 !ecp_nistz256_set_words(&r->Y, p.p.Y) ||
1378 !ecp_nistz256_set_words(&r->Z, p.p.Z)) {
1381 r->Z_is_one = is_one(&r->Z) & 1;
1388 BN_CTX_free(new_ctx);
1390 OPENSSL_free(new_points);
1392 OPENSSL_free(new_scalars);
1396 static int ecp_nistz256_get_affine(const EC_GROUP *group,
1397 const EC_POINT *point,
1398 BIGNUM *x, BIGNUM *y, BN_CTX *ctx)
1400 BN_ULONG z_inv2[P256_LIMBS];
1401 BN_ULONG z_inv3[P256_LIMBS];
1402 BN_ULONG x_aff[P256_LIMBS];
1403 BN_ULONG y_aff[P256_LIMBS];
1404 BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
1405 BN_ULONG x_ret[P256_LIMBS], y_ret[P256_LIMBS];
1407 if (EC_POINT_is_at_infinity(group, point)) {
1408 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
1412 if (!ecp_nistz256_bignum_to_field_elem(point_x, &point->X) ||
1413 !ecp_nistz256_bignum_to_field_elem(point_y, &point->Y) ||
1414 !ecp_nistz256_bignum_to_field_elem(point_z, &point->Z)) {
1415 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
1419 ecp_nistz256_mod_inverse(z_inv3, point_z);
1420 ecp_nistz256_sqr_mont(z_inv2, z_inv3);
1421 ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
1424 ecp_nistz256_from_mont(x_ret, x_aff);
1425 if (!ecp_nistz256_set_words(x, x_ret))
1430 ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
1431 ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
1432 ecp_nistz256_from_mont(y_ret, y_aff);
1433 if (!ecp_nistz256_set_words(y, y_ret))
1440 static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP *group)
1442 EC_PRE_COMP *ret = NULL;
1447 ret = (EC_PRE_COMP *)OPENSSL_malloc(sizeof(EC_PRE_COMP));
1450 ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
1455 ret->w = 6; /* default */
1456 ret->precomp = NULL;
1457 ret->precomp_storage = NULL;
1458 ret->references = 1;
1462 static void *ecp_nistz256_pre_comp_dup(void *src_)
1464 EC_PRE_COMP *src = src_;
1466 /* no need to actually copy, these objects never change! */
1467 CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
1472 static void ecp_nistz256_pre_comp_free(void *pre_)
1475 EC_PRE_COMP *pre = pre_;
1480 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1484 if (pre->precomp_storage)
1485 OPENSSL_free(pre->precomp_storage);
1490 static void ecp_nistz256_pre_comp_clear_free(void *pre_)
1493 EC_PRE_COMP *pre = pre_;
1498 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1502 if (pre->precomp_storage) {
1503 OPENSSL_cleanse(pre->precomp,
1504 32 * sizeof(unsigned char) * (1 << pre->w) * 2 * 37);
1505 OPENSSL_free(pre->precomp_storage);
1507 OPENSSL_cleanse(pre, sizeof *pre);
1511 static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP *group)
1513 /* There is a hard-coded table for the default generator. */
1514 const EC_POINT *generator = EC_GROUP_get0_generator(group);
1515 if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
1516 /* There is a hard-coded table for the default generator. */
1520 return EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1521 ecp_nistz256_pre_comp_free,
1522 ecp_nistz256_pre_comp_clear_free) != NULL;
1525 const EC_METHOD *EC_GFp_nistz256_method(void)
1527 static const EC_METHOD ret = {
1528 EC_FLAGS_DEFAULT_OCT,
1529 NID_X9_62_prime_field,
1530 ec_GFp_mont_group_init,
1531 ec_GFp_mont_group_finish,
1532 ec_GFp_mont_group_clear_finish,
1533 ec_GFp_mont_group_copy,
1534 ec_GFp_mont_group_set_curve,
1535 ec_GFp_simple_group_get_curve,
1536 ec_GFp_simple_group_get_degree,
1537 ec_GFp_simple_group_check_discriminant,
1538 ec_GFp_simple_point_init,
1539 ec_GFp_simple_point_finish,
1540 ec_GFp_simple_point_clear_finish,
1541 ec_GFp_simple_point_copy,
1542 ec_GFp_simple_point_set_to_infinity,
1543 ec_GFp_simple_set_Jprojective_coordinates_GFp,
1544 ec_GFp_simple_get_Jprojective_coordinates_GFp,
1545 ec_GFp_simple_point_set_affine_coordinates,
1546 ecp_nistz256_get_affine,
1550 ec_GFp_simple_invert,
1551 ec_GFp_simple_is_at_infinity,
1552 ec_GFp_simple_is_on_curve,
1554 ec_GFp_simple_make_affine,
1555 ec_GFp_simple_points_make_affine,
1556 ecp_nistz256_points_mul, /* mul */
1557 ecp_nistz256_mult_precompute, /* precompute_mult */
1558 ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
1559 ec_GFp_mont_field_mul,
1560 ec_GFp_mont_field_sqr,
1562 ec_GFp_mont_field_encode,
1563 ec_GFp_mont_field_decode,
1564 ec_GFp_mont_field_set_to_one