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 */
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;
82 /* Functions implemented in assembly */
83 /* Modular mul by 2: res = 2*a mod P */
84 void 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 */
87 void 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 */
90 void 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 */
93 void 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 */
97 void 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 */
101 void ecp_nistz256_neg(BN_ULONG res[P256_LIMBS], const BN_ULONG a[P256_LIMBS]);
102 /* Montgomery mul: res = a*b*2^-256 mod P */
103 void 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 */
107 void 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 */
110 void 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*/
113 void 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 */
116 void ecp_nistz256_select_w5(P256_POINT * val,
117 const P256_POINT * in_t, int index);
118 void ecp_nistz256_select_w7(P256_POINT_AFFINE * val,
119 const P256_POINT_AFFINE * in_t, int index);
121 /* One converted into the Montgomery domain */
122 static const BN_ULONG ONE[P256_LIMBS] = {
123 TOBN(0x00000000, 0x00000001), TOBN(0xffffffff, 0x00000000),
124 TOBN(0xffffffff, 0xffffffff), TOBN(0x00000000, 0xfffffffe)
127 static void *ecp_nistz256_pre_comp_dup(void *);
128 static void ecp_nistz256_pre_comp_free(void *);
129 static void ecp_nistz256_pre_comp_clear_free(void *);
130 static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP * group);
132 /* Precomputed tables for the default generator */
133 #include "ecp_nistz256_table.c"
135 /* Recode window to a signed digit, see ecp_nistputil.c for details */
136 static unsigned int _booth_recode_w5(unsigned int in)
140 s = ~((in >> 5) - 1);
141 d = (1 << 6) - in - 1;
142 d = (d & s) | (in & ~s);
143 d = (d >> 1) + (d & 1);
145 return (d << 1) + (s & 1);
148 static unsigned int _booth_recode_w7(unsigned int in)
152 s = ~((in >> 7) - 1);
153 d = (1 << 8) - in - 1;
154 d = (d & s) | (in & ~s);
155 d = (d >> 1) + (d & 1);
157 return (d << 1) + (s & 1);
160 static void copy_conditional(BN_ULONG dst[P256_LIMBS],
161 const BN_ULONG src[P256_LIMBS], BN_ULONG move)
163 BN_ULONG mask1 = -move;
164 BN_ULONG mask2 = ~mask1;
166 dst[0] = (src[0] & mask1) ^ (dst[0] & mask2);
167 dst[1] = (src[1] & mask1) ^ (dst[1] & mask2);
168 dst[2] = (src[2] & mask1) ^ (dst[2] & mask2);
169 dst[3] = (src[3] & mask1) ^ (dst[3] & mask2);
170 if (P256_LIMBS == 8) {
171 dst[4] = (src[4] & mask1) ^ (dst[4] & mask2);
172 dst[5] = (src[5] & mask1) ^ (dst[5] & mask2);
173 dst[6] = (src[6] & mask1) ^ (dst[6] & mask2);
174 dst[7] = (src[7] & mask1) ^ (dst[7] & mask2);
178 static BN_ULONG is_zero(BN_ULONG in)
187 static BN_ULONG is_equal(const BN_ULONG a[P256_LIMBS],
188 const BN_ULONG b[P256_LIMBS])
196 if (P256_LIMBS == 8) {
206 static BN_ULONG is_one(const BN_ULONG a[P256_LIMBS])
211 res |= a[1] ^ ONE[1];
212 res |= a[2] ^ ONE[2];
213 res |= a[3] ^ ONE[3];
214 if (P256_LIMBS == 8) {
215 res |= a[4] ^ ONE[4];
216 res |= a[5] ^ ONE[5];
217 res |= a[6] ^ ONE[6];
223 #ifndef ECP_NISTZ256_REFERENCE_IMPLEMENTATION
224 void ecp_nistz256_point_double(P256_POINT * r, const P256_POINT * a);
225 void ecp_nistz256_point_add(P256_POINT * r,
226 const P256_POINT * a, const P256_POINT * b);
227 void ecp_nistz256_point_add_affine(P256_POINT * r,
228 const P256_POINT * a,
229 const P256_POINT_AFFINE * b);
231 /* Point double: r = 2*a */
232 static void ecp_nistz256_point_double(P256_POINT * r, const P256_POINT * a)
234 BN_ULONG S[P256_LIMBS];
235 BN_ULONG M[P256_LIMBS];
236 BN_ULONG Zsqr[P256_LIMBS];
237 BN_ULONG tmp0[P256_LIMBS];
239 const BN_ULONG *in_x = a->X;
240 const BN_ULONG *in_y = a->Y;
241 const BN_ULONG *in_z = a->Z;
243 BN_ULONG *res_x = r->X;
244 BN_ULONG *res_y = r->Y;
245 BN_ULONG *res_z = r->Z;
247 ecp_nistz256_mul_by_2(S, in_y);
249 ecp_nistz256_sqr_mont(Zsqr, in_z);
251 ecp_nistz256_sqr_mont(S, S);
253 ecp_nistz256_mul_mont(res_z, in_z, in_y);
254 ecp_nistz256_mul_by_2(res_z, res_z);
256 ecp_nistz256_add(M, in_x, Zsqr);
257 ecp_nistz256_sub(Zsqr, in_x, Zsqr);
259 ecp_nistz256_sqr_mont(res_y, S);
260 ecp_nistz256_div_by_2(res_y, res_y);
262 ecp_nistz256_mul_mont(M, M, Zsqr);
263 ecp_nistz256_mul_by_3(M, M);
265 ecp_nistz256_mul_mont(S, S, in_x);
266 ecp_nistz256_mul_by_2(tmp0, S);
268 ecp_nistz256_sqr_mont(res_x, M);
270 ecp_nistz256_sub(res_x, res_x, tmp0);
271 ecp_nistz256_sub(S, S, res_x);
273 ecp_nistz256_mul_mont(S, S, M);
274 ecp_nistz256_sub(res_y, S, res_y);
277 /* Point addition: r = a+b */
278 static void ecp_nistz256_point_add(P256_POINT * r,
279 const P256_POINT * a, const P256_POINT * b)
281 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
282 BN_ULONG U1[P256_LIMBS], S1[P256_LIMBS];
283 BN_ULONG Z1sqr[P256_LIMBS];
284 BN_ULONG Z2sqr[P256_LIMBS];
285 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
286 BN_ULONG Hsqr[P256_LIMBS];
287 BN_ULONG Rsqr[P256_LIMBS];
288 BN_ULONG Hcub[P256_LIMBS];
290 BN_ULONG res_x[P256_LIMBS];
291 BN_ULONG res_y[P256_LIMBS];
292 BN_ULONG res_z[P256_LIMBS];
294 BN_ULONG in1infty, in2infty;
296 const BN_ULONG *in1_x = a->X;
297 const BN_ULONG *in1_y = a->Y;
298 const BN_ULONG *in1_z = a->Z;
300 const BN_ULONG *in2_x = b->X;
301 const BN_ULONG *in2_y = b->Y;
302 const BN_ULONG *in2_z = b->Z;
304 /* We encode infinity as (0,0), which is not on the curve,
306 in1infty = in1_x[0] | in1_x[1] | in1_x[2] | in1_x[3] |
307 in1_y[0] | in1_y[1] | in1_y[2] | in1_y[3];
309 in1infty |= in1_x[4] | in1_x[5] | in1_x[6] | in1_x[7] |
310 in1_y[4] | in1_y[5] | in1_y[6] | in1_y[7];
312 in2infty = in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
313 in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3];
315 in2infty |= in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
316 in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7];
318 in1infty = is_zero(in1infty);
319 in2infty = is_zero(in2infty);
321 ecp_nistz256_sqr_mont(Z2sqr, in2_z); /* Z2^2 */
322 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
324 ecp_nistz256_mul_mont(S1, Z2sqr, in2_z); /* S1 = Z2^3 */
325 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
327 ecp_nistz256_mul_mont(S1, S1, in1_y); /* S1 = Y1*Z2^3 */
328 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
329 ecp_nistz256_sub(R, S2, S1); /* R = S2 - S1 */
331 ecp_nistz256_mul_mont(U1, in1_x, Z2sqr); /* U1 = X1*Z2^2 */
332 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
333 ecp_nistz256_sub(H, U2, U1); /* H = U2 - U1 */
335 /* This should not happen during sign/ecdh,
336 * so no constant time violation */
337 if (is_equal(U1, U2) && !in1infty && !in2infty) {
338 if (is_equal(S1, S2)) {
339 ecp_nistz256_point_double(r, a);
342 memset(r, 0, sizeof(*r));
347 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
348 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
349 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
350 ecp_nistz256_mul_mont(res_z, res_z, in2_z); /* Z3 = H*Z1*Z2 */
351 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
353 ecp_nistz256_mul_mont(U2, U1, Hsqr); /* U1*H^2 */
354 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
356 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
357 ecp_nistz256_sub(res_x, res_x, Hcub);
359 ecp_nistz256_sub(res_y, U2, res_x);
361 ecp_nistz256_mul_mont(S2, S1, Hcub);
362 ecp_nistz256_mul_mont(res_y, R, res_y);
363 ecp_nistz256_sub(res_y, res_y, S2);
365 copy_conditional(res_x, in2_x, in1infty);
366 copy_conditional(res_y, in2_y, in1infty);
367 copy_conditional(res_z, in2_z, in1infty);
369 copy_conditional(res_x, in1_x, in2infty);
370 copy_conditional(res_y, in1_y, in2infty);
371 copy_conditional(res_z, in1_z, in2infty);
373 memcpy(r->X, res_x, sizeof(res_x));
374 memcpy(r->Y, res_y, sizeof(res_y));
375 memcpy(r->Z, res_z, sizeof(res_z));
378 /* Point addition when b is known to be affine: r = a+b */
379 static void ecp_nistz256_point_add_affine(P256_POINT * r,
380 const P256_POINT * a,
381 const P256_POINT_AFFINE * b)
383 BN_ULONG U2[P256_LIMBS], S2[P256_LIMBS];
384 BN_ULONG Z1sqr[P256_LIMBS];
385 BN_ULONG H[P256_LIMBS], R[P256_LIMBS];
386 BN_ULONG Hsqr[P256_LIMBS];
387 BN_ULONG Rsqr[P256_LIMBS];
388 BN_ULONG Hcub[P256_LIMBS];
390 BN_ULONG res_x[P256_LIMBS];
391 BN_ULONG res_y[P256_LIMBS];
392 BN_ULONG res_z[P256_LIMBS];
394 BN_ULONG in1infty, in2infty;
396 const BN_ULONG *in1_x = a->X;
397 const BN_ULONG *in1_y = a->Y;
398 const BN_ULONG *in1_z = a->Z;
400 const BN_ULONG *in2_x = b->X;
401 const BN_ULONG *in2_y = b->Y;
403 /* In affine representation we encode infty as (0,0),
404 * which is not on the curve, so it is OK */
405 in1infty = in1_x[0] | in1_x[1] | in1_x[2] | in1_x[3] |
406 in1_y[0] | in1_y[1] | in1_y[2] | in1_y[3];
408 in1infty |= in1_x[4] | in1_x[5] | in1_x[6] | in1_x[7] |
409 in1_y[4] | in1_y[5] | in1_y[6] | in1_y[7];
411 in2infty = in2_x[0] | in2_x[1] | in2_x[2] | in2_x[3] |
412 in2_y[0] | in2_y[1] | in2_y[2] | in2_y[3];
414 in2infty |= in2_x[4] | in2_x[5] | in2_x[6] | in2_x[7] |
415 in2_y[4] | in2_y[5] | in2_y[6] | in2_y[7];
417 in1infty = is_zero(in1infty);
418 in2infty = is_zero(in2infty);
420 ecp_nistz256_sqr_mont(Z1sqr, in1_z); /* Z1^2 */
422 ecp_nistz256_mul_mont(U2, in2_x, Z1sqr); /* U2 = X2*Z1^2 */
423 ecp_nistz256_sub(H, U2, in1_x); /* H = U2 - U1 */
425 ecp_nistz256_mul_mont(S2, Z1sqr, in1_z); /* S2 = Z1^3 */
427 ecp_nistz256_mul_mont(res_z, H, in1_z); /* Z3 = H*Z1*Z2 */
429 ecp_nistz256_mul_mont(S2, S2, in2_y); /* S2 = Y2*Z1^3 */
430 ecp_nistz256_sub(R, S2, in1_y); /* R = S2 - S1 */
432 ecp_nistz256_sqr_mont(Hsqr, H); /* H^2 */
433 ecp_nistz256_sqr_mont(Rsqr, R); /* R^2 */
434 ecp_nistz256_mul_mont(Hcub, Hsqr, H); /* H^3 */
436 ecp_nistz256_mul_mont(U2, in1_x, Hsqr); /* U1*H^2 */
437 ecp_nistz256_mul_by_2(Hsqr, U2); /* 2*U1*H^2 */
439 ecp_nistz256_sub(res_x, Rsqr, Hsqr);
440 ecp_nistz256_sub(res_x, res_x, Hcub);
441 ecp_nistz256_sub(H, U2, res_x);
443 ecp_nistz256_mul_mont(S2, in1_y, Hcub);
444 ecp_nistz256_mul_mont(H, H, R);
445 ecp_nistz256_sub(res_y, H, S2);
447 copy_conditional(res_x, in2_x, in1infty);
448 copy_conditional(res_x, in1_x, in2infty);
450 copy_conditional(res_y, in2_y, in1infty);
451 copy_conditional(res_y, in1_y, in2infty);
453 copy_conditional(res_z, ONE, in1infty);
454 copy_conditional(res_z, in1_z, in2infty);
456 memcpy(r->X, res_x, sizeof(res_x));
457 memcpy(r->Y, res_y, sizeof(res_y));
458 memcpy(r->Z, res_z, sizeof(res_z));
462 /* r = in^-1 mod p */
463 static void ecp_nistz256_mod_inverse(BN_ULONG r[P256_LIMBS],
464 const BN_ULONG in[P256_LIMBS])
466 /* The poly is ffffffff 00000001 00000000 00000000 00000000 ffffffff ffffffff ffffffff
467 We use FLT and used poly-2 as exponent */
468 BN_ULONG p2[P256_LIMBS];
469 BN_ULONG p4[P256_LIMBS];
470 BN_ULONG p8[P256_LIMBS];
471 BN_ULONG p16[P256_LIMBS];
472 BN_ULONG p32[P256_LIMBS];
473 BN_ULONG res[P256_LIMBS];
476 ecp_nistz256_sqr_mont(res, in);
477 ecp_nistz256_mul_mont(p2, res, in); /* 3*p */
479 ecp_nistz256_sqr_mont(res, p2);
480 ecp_nistz256_sqr_mont(res, res);
481 ecp_nistz256_mul_mont(p4, res, p2); /* f*p */
483 ecp_nistz256_sqr_mont(res, p4);
484 ecp_nistz256_sqr_mont(res, res);
485 ecp_nistz256_sqr_mont(res, res);
486 ecp_nistz256_sqr_mont(res, res);
487 ecp_nistz256_mul_mont(p8, res, p4); /* ff*p */
489 ecp_nistz256_sqr_mont(res, p8);
490 for (i = 0; i < 7; i++)
491 ecp_nistz256_sqr_mont(res, res);
492 ecp_nistz256_mul_mont(p16, res, p8); /* ffff*p */
494 ecp_nistz256_sqr_mont(res, p16);
495 for (i = 0; i < 15; i++)
496 ecp_nistz256_sqr_mont(res, res);
497 ecp_nistz256_mul_mont(p32, res, p16); /* ffffffff*p */
499 ecp_nistz256_sqr_mont(res, p32);
500 for (i = 0; i < 31; i++)
501 ecp_nistz256_sqr_mont(res, res);
502 ecp_nistz256_mul_mont(res, res, in);
504 for (i = 0; i < 32 * 4; i++)
505 ecp_nistz256_sqr_mont(res, res);
506 ecp_nistz256_mul_mont(res, res, p32);
508 for (i = 0; i < 32; i++)
509 ecp_nistz256_sqr_mont(res, res);
510 ecp_nistz256_mul_mont(res, res, p32);
512 for (i = 0; i < 16; i++)
513 ecp_nistz256_sqr_mont(res, res);
514 ecp_nistz256_mul_mont(res, res, p16);
516 for (i = 0; i < 8; i++)
517 ecp_nistz256_sqr_mont(res, res);
518 ecp_nistz256_mul_mont(res, res, p8);
520 ecp_nistz256_sqr_mont(res, res);
521 ecp_nistz256_sqr_mont(res, res);
522 ecp_nistz256_sqr_mont(res, res);
523 ecp_nistz256_sqr_mont(res, res);
524 ecp_nistz256_mul_mont(res, res, p4);
526 ecp_nistz256_sqr_mont(res, res);
527 ecp_nistz256_sqr_mont(res, res);
528 ecp_nistz256_mul_mont(res, res, p2);
530 ecp_nistz256_sqr_mont(res, res);
531 ecp_nistz256_sqr_mont(res, res);
532 ecp_nistz256_mul_mont(res, res, in);
534 memcpy(r, res, sizeof(res));
537 /* ecp_nistz256_bignum_to_field_elem copies the contents of |in| to |out| and
538 * returns one if it fits. Otherwise it returns zero. */
539 static int ecp_nistz256_bignum_to_field_elem(BN_ULONG out[P256_LIMBS],
542 if (in->top > P256_LIMBS)
545 memset(out, 0, sizeof(BN_ULONG) * P256_LIMBS);
546 memcpy(out, in->d, sizeof(BN_ULONG) * in->top);
550 /* r = sum(scalar[i]*point[i]) */
551 static void ecp_nistz256_windowed_mul(const EC_GROUP * group,
553 const BIGNUM ** scalar,
554 const EC_POINT ** point,
555 int num, BN_CTX * ctx)
559 unsigned char (*p_str)[33] = NULL;
560 const unsigned int window_size = 5;
561 const unsigned int mask = (1 << (window_size + 1)) - 1;
563 BN_ULONG tmp[P256_LIMBS];
564 ALIGN32 P256_POINT h;
565 const BIGNUM **scalars = NULL;
566 P256_POINT(*table)[16] = NULL;
567 void *table_storage = NULL;
570 OPENSSL_malloc(num * 16 * sizeof(P256_POINT) + 64)) == NULL
572 OPENSSL_malloc(num * 33 * sizeof(unsigned char))) == NULL
573 || (scalars = OPENSSL_malloc(num * sizeof(BIGNUM *))) == NULL) {
574 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_MALLOC_FAILURE);
577 table = (void *)ALIGNPTR(table_storage, 64);
580 for (i = 0; i < num; i++) {
581 P256_POINT *row = table[i];
583 if ((BN_num_bits(scalar[i]) > 256) || BN_is_negative(scalar[i])) {
586 if ((mod = BN_CTX_get(ctx)) == NULL)
588 if (!BN_nnmod(mod, scalar[i], &group->order, ctx)) {
589 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, ERR_R_BN_LIB);
594 scalars[i] = scalar[i];
596 for (j = 0; j < scalars[i]->top * BN_BYTES; j += BN_BYTES) {
597 BN_ULONG d = scalars[i]->d[j / BN_BYTES];
599 p_str[i][j + 0] = d & 0xff;
600 p_str[i][j + 1] = (d >> 8) & 0xff;
601 p_str[i][j + 2] = (d >> 16) & 0xff;
602 p_str[i][j + 3] = (d >>= 24) & 0xff;
605 p_str[i][j + 4] = d & 0xff;
606 p_str[i][j + 5] = (d >> 8) & 0xff;
607 p_str[i][j + 6] = (d >> 16) & 0xff;
608 p_str[i][j + 7] = (d >> 24) & 0xff;
614 /* table[0] is implicitly (0,0,0) (the point at infinity),
615 * therefore it is not stored. All other values are actually
616 * stored with an offset of -1 in table.
619 if (!ecp_nistz256_bignum_to_field_elem(row[1 - 1].X, &point[i]->X)
620 || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Y, &point[i]->Y)
621 || !ecp_nistz256_bignum_to_field_elem(row[1 - 1].Z, &point[i]->Z)) {
622 ECerr(EC_F_ECP_NISTZ256_WINDOWED_MUL, EC_R_COORDINATES_OUT_OF_RANGE);
626 ecp_nistz256_point_double(&row[ 2 - 1], &row[ 1 - 1]);
627 ecp_nistz256_point_add (&row[ 3 - 1], &row[ 2 - 1], &row[1 - 1]);
628 ecp_nistz256_point_double(&row[ 4 - 1], &row[ 2 - 1]);
629 ecp_nistz256_point_double(&row[ 6 - 1], &row[ 3 - 1]);
630 ecp_nistz256_point_double(&row[ 8 - 1], &row[ 4 - 1]);
631 ecp_nistz256_point_double(&row[12 - 1], &row[ 6 - 1]);
632 ecp_nistz256_point_add (&row[ 5 - 1], &row[ 4 - 1], &row[1 - 1]);
633 ecp_nistz256_point_add (&row[ 7 - 1], &row[ 6 - 1], &row[1 - 1]);
634 ecp_nistz256_point_add (&row[ 9 - 1], &row[ 8 - 1], &row[1 - 1]);
635 ecp_nistz256_point_add (&row[13 - 1], &row[12 - 1], &row[1 - 1]);
636 ecp_nistz256_point_double(&row[14 - 1], &row[ 7 - 1]);
637 ecp_nistz256_point_double(&row[10 - 1], &row[ 5 - 1]);
638 ecp_nistz256_point_add (&row[15 - 1], &row[14 - 1], &row[1 - 1]);
639 ecp_nistz256_point_add (&row[11 - 1], &row[10 - 1], &row[1 - 1]);
640 ecp_nistz256_point_add (&row[16 - 1], &row[15 - 1], &row[1 - 1]);
645 wvalue = p_str[0][(index - 1) / 8];
646 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
648 ecp_nistz256_select_w5(r, table[0], _booth_recode_w5(wvalue) >> 1);
651 for (i = (index == 255 ? 1 : 0); i < num; i++) {
652 unsigned int off = (index - 1) / 8;
654 wvalue = p_str[i][off] | p_str[i][off + 1] << 8;
655 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
657 wvalue = _booth_recode_w5(wvalue);
659 ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
661 ecp_nistz256_neg(tmp, h.Y);
662 copy_conditional(h.Y, tmp, (wvalue & 1));
664 ecp_nistz256_point_add(r, r, &h);
667 index -= window_size;
669 ecp_nistz256_point_double(r, r);
670 ecp_nistz256_point_double(r, r);
671 ecp_nistz256_point_double(r, r);
672 ecp_nistz256_point_double(r, r);
673 ecp_nistz256_point_double(r, r);
677 for (i = 0; i < num; i++) {
678 wvalue = p_str[i][0];
679 wvalue = (wvalue << 1) & mask;
681 wvalue = _booth_recode_w5(wvalue);
683 ecp_nistz256_select_w5(&h, table[i], wvalue >> 1);
685 ecp_nistz256_neg(tmp, h.Y);
686 copy_conditional(h.Y, tmp, wvalue & 1);
688 ecp_nistz256_point_add(r, r, &h);
693 OPENSSL_free(table_storage);
697 OPENSSL_free(scalars);
700 /* Coordinates of G, for which we have precomputed tables */
701 const static BN_ULONG def_xG[P256_LIMBS] = {
702 TOBN(0x79e730d4, 0x18a9143c), TOBN(0x75ba95fc, 0x5fedb601),
703 TOBN(0x79fb732b, 0x77622510), TOBN(0x18905f76, 0xa53755c6)
706 const static BN_ULONG def_yG[P256_LIMBS] = {
707 TOBN(0xddf25357, 0xce95560a), TOBN(0x8b4ab8e4, 0xba19e45c),
708 TOBN(0xd2e88688, 0xdd21f325), TOBN(0x8571ff18, 0x25885d85)
711 /* ecp_nistz256_is_affine_G returns one if |generator| is the standard,
712 * P-256 generator. */
713 static int ecp_nistz256_is_affine_G(const EC_POINT * generator)
715 return (generator->X.top == P256_LIMBS) &&
716 (generator->Y.top == P256_LIMBS) &&
717 (generator->Z.top == (P256_LIMBS - P256_LIMBS / 8)) &&
718 is_equal(generator->X.d, def_xG) &&
719 is_equal(generator->Y.d, def_yG) && is_one(generator->Z.d);
722 static int ecp_nistz256_mult_precompute(EC_GROUP * group, BN_CTX * ctx)
724 /* We precompute a table for a Booth encoded exponent (wNAF) based
725 * computation. Each table holds 64 values for safe access, with an
726 * implicit value of infinity at index zero. We use window of size 7,
727 * and therefore require ceil(256/7) = 37 tables. */
729 EC_POINT *P = NULL, *T = NULL;
730 const EC_POINT *generator;
731 EC_PRE_COMP *pre_comp;
732 int i, j, k, ret = 0;
735 PRECOMP256_ROW *preComputedTable = NULL;
736 unsigned char *precomp_storage = NULL;
738 /* if there is an old EC_PRE_COMP object, throw it away */
739 EC_EX_DATA_free_data(&group->extra_data, ecp_nistz256_pre_comp_dup,
740 ecp_nistz256_pre_comp_free,
741 ecp_nistz256_pre_comp_clear_free);
743 generator = EC_GROUP_get0_generator(group);
744 if (generator == NULL) {
745 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNDEFINED_GENERATOR);
749 if (ecp_nistz256_is_affine_G(generator)) {
750 /* No need to calculate tables for the standard generator
751 * because we have them statically. */
755 if ((pre_comp = ecp_nistz256_pre_comp_new(group)) == NULL)
765 order = BN_CTX_get(ctx);
770 if (!EC_GROUP_get_order(group, order, ctx))
773 if (BN_is_zero(order)) {
774 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, EC_R_UNKNOWN_ORDER);
780 if ((precomp_storage =
781 OPENSSL_malloc(37 * 64 * sizeof(P256_POINT_AFFINE) + 64)) == NULL) {
782 ECerr(EC_F_ECP_NISTZ256_MULT_PRECOMPUTE, ERR_R_MALLOC_FAILURE);
785 preComputedTable = (void *)ALIGNPTR(precomp_storage, 64);
788 P = EC_POINT_new(group);
789 T = EC_POINT_new(group);
791 /* The zero entry is implicitly infinity, and we skip it,
792 * storing other values with -1 offset. */
793 EC_POINT_copy(T, generator);
795 for (k = 0; k < 64; k++) {
797 for (j = 0; j < 37; j++) {
798 /* It would be faster to use
799 * ec_GFp_simple_points_make_affine and make multiple
800 * points affine at the same time. */
801 ec_GFp_simple_make_affine(group, P, ctx);
802 ecp_nistz256_bignum_to_field_elem(preComputedTable[j]
804 ecp_nistz256_bignum_to_field_elem(preComputedTable[j]
806 for (i = 0; i < 7; i++)
807 ec_GFp_simple_dbl(group, P, P, ctx);
809 ec_GFp_simple_add(group, T, T, generator, ctx);
812 pre_comp->group = group;
814 pre_comp->precomp = preComputedTable;
815 pre_comp->precomp_storage = precomp_storage;
817 precomp_storage = NULL;
819 if (!EC_EX_DATA_set_data(&group->extra_data, pre_comp,
820 ecp_nistz256_pre_comp_dup,
821 ecp_nistz256_pre_comp_free,
822 ecp_nistz256_pre_comp_clear_free)) {
834 ecp_nistz256_pre_comp_free(pre_comp);
836 OPENSSL_free(precomp_storage);
845 * Note that by default ECP_NISTZ256_AVX2 is undefined. While it's great
846 * code processing 4 points in parallel, corresponding serial operation
847 * is several times slower, because it uses 29x29=58-bit multiplication
848 * as opposite to 64x64=128-bit in integer-only scalar case. As result
849 * it doesn't provide *significant* performance improvement. Note that
850 * just defining ECP_NISTZ256_AVX2 is not sufficient to make it work,
851 * you'd need to compile even asm/ecp_nistz256-avx.pl module.
853 #if defined(ECP_NISTZ256_AVX2)
854 # if !(defined(__x86_64) || defined(__x86_64__)) || \
855 defined(_M_AMD64) || defined(_MX64)) || \
856 !(defined(__GNUC__) || defined(_MSC_VER)) /* this is for ALIGN32 */
857 # undef ECP_NISTZ256_AVX2
859 /* Constant time access, loading four values, from four consecutive tables */
860 void ecp_nistz256_avx2_select_w7(P256_POINT_AFFINE * val,
861 const P256_POINT_AFFINE * in_t, int index);
862 void ecp_nistz256_avx2_multi_select_w7(void *result, const void *in, int index0,
863 int index1, int index2, int index3);
864 void ecp_nistz256_avx2_transpose_convert(void *RESULTx4, const void *in);
865 void ecp_nistz256_avx2_convert_transpose_back(void *result, const void *Ax4);
866 void ecp_nistz256_avx2_point_add_affine_x4(void *RESULTx4, const void *Ax4,
868 void ecp_nistz256_avx2_point_add_affines_x4(void *RESULTx4, const void *Ax4,
870 void ecp_nistz256_avx2_to_mont(void *RESULTx4, const void *Ax4);
871 void ecp_nistz256_avx2_from_mont(void *RESULTx4, const void *Ax4);
872 void ecp_nistz256_avx2_set1(void *RESULTx4);
873 int ecp_nistz_avx2_eligible(void);
875 static void booth_recode_w7(unsigned char *sign,
876 unsigned char *digit, unsigned char in)
880 s = ~((in >> 7) - 1);
881 d = (1 << 8) - in - 1;
882 d = (d & s) | (in & ~s);
883 d = (d >> 1) + (d & 1);
889 /* ecp_nistz256_avx2_mul_g performs multiplication by G, using only the
890 * precomputed table. It does 4 affine point additions in parallel,
891 * significantly speeding up point multiplication for a fixed value. */
892 static void ecp_nistz256_avx2_mul_g(P256_POINT * r,
893 unsigned char p_str[33],
895 P256_POINT_AFFINE(*preComputedTable)[64])
897 const unsigned int window_size = 7;
898 const unsigned int mask = (1 << (window_size + 1)) - 1;
900 /* Using 4 windows at a time */
901 unsigned char sign0, digit0;
902 unsigned char sign1, digit1;
903 unsigned char sign2, digit2;
904 unsigned char sign3, digit3;
905 unsigned int index = 0;
906 BN_ULONG tmp[P256_LIMBS];
909 ALIGN32 BN_ULONG aX4[4 * 9 * 3] = { 0 };
910 ALIGN32 BN_ULONG bX4[4 * 9 * 2] = { 0 };
911 ALIGN32 P256_POINT_AFFINE point_arr[P256_LIMBS];
912 ALIGN32 P256_POINT res_point_arr[P256_LIMBS];
914 /* Initial four windows */
915 wvalue = *((u16 *) & p_str[0]);
916 wvalue = (wvalue << 1) & mask;
917 index += window_size;
918 booth_recode_w7(&sign0, &digit0, wvalue);
919 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
920 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
921 index += window_size;
922 booth_recode_w7(&sign1, &digit1, wvalue);
923 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
924 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
925 index += window_size;
926 booth_recode_w7(&sign2, &digit2, wvalue);
927 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
928 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
929 index += window_size;
930 booth_recode_w7(&sign3, &digit3, wvalue);
932 ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[0],
933 digit0, digit1, digit2, digit3);
935 ecp_nistz256_neg(tmp, point_arr[0].Y);
936 copy_conditional(point_arr[0].Y, tmp, sign0);
937 ecp_nistz256_neg(tmp, point_arr[1].Y);
938 copy_conditional(point_arr[1].Y, tmp, sign1);
939 ecp_nistz256_neg(tmp, point_arr[2].Y);
940 copy_conditional(point_arr[2].Y, tmp, sign2);
941 ecp_nistz256_neg(tmp, point_arr[3].Y);
942 copy_conditional(point_arr[3].Y, tmp, sign3);
944 ecp_nistz256_avx2_transpose_convert(aX4, point_arr);
945 ecp_nistz256_avx2_to_mont(aX4, aX4);
946 ecp_nistz256_avx2_to_mont(&aX4[4 * 9], &aX4[4 * 9]);
947 ecp_nistz256_avx2_set1(&aX4[4 * 9 * 2]);
949 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
950 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
951 index += window_size;
952 booth_recode_w7(&sign0, &digit0, wvalue);
953 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
954 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
955 index += window_size;
956 booth_recode_w7(&sign1, &digit1, wvalue);
957 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
958 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
959 index += window_size;
960 booth_recode_w7(&sign2, &digit2, wvalue);
961 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
962 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
963 index += window_size;
964 booth_recode_w7(&sign3, &digit3, wvalue);
966 ecp_nistz256_avx2_multi_select_w7(point_arr, preComputedTable[4 * 1],
967 digit0, digit1, digit2, digit3);
969 ecp_nistz256_neg(tmp, point_arr[0].Y);
970 copy_conditional(point_arr[0].Y, tmp, sign0);
971 ecp_nistz256_neg(tmp, point_arr[1].Y);
972 copy_conditional(point_arr[1].Y, tmp, sign1);
973 ecp_nistz256_neg(tmp, point_arr[2].Y);
974 copy_conditional(point_arr[2].Y, tmp, sign2);
975 ecp_nistz256_neg(tmp, point_arr[3].Y);
976 copy_conditional(point_arr[3].Y, tmp, sign3);
978 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
979 ecp_nistz256_avx2_to_mont(bX4, bX4);
980 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
981 /* Optimized when both inputs are affine */
982 ecp_nistz256_avx2_point_add_affines_x4(aX4, aX4, bX4);
984 for (i = 2; i < 9; i++) {
985 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
986 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
987 index += window_size;
988 booth_recode_w7(&sign0, &digit0, wvalue);
989 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
990 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
991 index += window_size;
992 booth_recode_w7(&sign1, &digit1, wvalue);
993 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
994 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
995 index += window_size;
996 booth_recode_w7(&sign2, &digit2, wvalue);
997 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
998 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
999 index += window_size;
1000 booth_recode_w7(&sign3, &digit3, wvalue);
1002 ecp_nistz256_avx2_multi_select_w7(point_arr,
1003 preComputedTable[4 * i],
1004 digit0, digit1, digit2, digit3);
1006 ecp_nistz256_neg(tmp, point_arr[0].Y);
1007 copy_conditional(point_arr[0].Y, tmp, sign0);
1008 ecp_nistz256_neg(tmp, point_arr[1].Y);
1009 copy_conditional(point_arr[1].Y, tmp, sign1);
1010 ecp_nistz256_neg(tmp, point_arr[2].Y);
1011 copy_conditional(point_arr[2].Y, tmp, sign2);
1012 ecp_nistz256_neg(tmp, point_arr[3].Y);
1013 copy_conditional(point_arr[3].Y, tmp, sign3);
1015 ecp_nistz256_avx2_transpose_convert(bX4, point_arr);
1016 ecp_nistz256_avx2_to_mont(bX4, bX4);
1017 ecp_nistz256_avx2_to_mont(&bX4[4 * 9], &bX4[4 * 9]);
1019 ecp_nistz256_avx2_point_add_affine_x4(aX4, aX4, bX4);
1022 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 0], &aX4[4 * 9 * 0]);
1023 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 1], &aX4[4 * 9 * 1]);
1024 ecp_nistz256_avx2_from_mont(&aX4[4 * 9 * 2], &aX4[4 * 9 * 2]);
1026 ecp_nistz256_avx2_convert_transpose_back(res_point_arr, aX4);
1027 /* Last window is performed serially */
1028 wvalue = *((u16 *) & p_str[(index - 1) / 8]);
1029 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1030 booth_recode_w7(&sign0, &digit0, wvalue);
1031 ecp_nistz256_avx2_select_w7((P256_POINT_AFFINE *) r,
1032 preComputedTable[36], digit0);
1033 ecp_nistz256_neg(tmp, r->Y);
1034 copy_conditional(r->Y, tmp, sign0);
1035 memcpy(r->Z, ONE, sizeof(ONE));
1036 /* Sum the four windows */
1037 ecp_nistz256_point_add(r, r, &res_point_arr[0]);
1038 ecp_nistz256_point_add(r, r, &res_point_arr[1]);
1039 ecp_nistz256_point_add(r, r, &res_point_arr[2]);
1040 ecp_nistz256_point_add(r, r, &res_point_arr[3]);
1045 static int ecp_nistz256_set_from_affine(EC_POINT * out, const EC_GROUP * group,
1046 const P256_POINT_AFFINE * in,
1050 BN_ULONG d_x[P256_LIMBS], d_y[P256_LIMBS];
1053 memcpy(d_x, in->X, sizeof(d_x));
1055 x.dmax = x.top = P256_LIMBS;
1057 x.flags = BN_FLG_STATIC_DATA;
1059 memcpy(d_y, in->Y, sizeof(d_y));
1061 y.dmax = y.top = P256_LIMBS;
1063 y.flags = BN_FLG_STATIC_DATA;
1065 ret = EC_POINT_set_affine_coordinates_GFp(group, out, &x, &y, ctx);
1070 /* r = scalar*G + sum(scalars[i]*points[i]) */
1071 static int ecp_nistz256_points_mul(const EC_GROUP * group,
1073 const BIGNUM * scalar,
1075 const EC_POINT * points[],
1076 const BIGNUM * scalars[], BN_CTX * ctx)
1078 int i = 0, ret = 0, no_precomp_for_generator = 0, p_is_infinity = 0;
1080 unsigned char p_str[33] = { 0 };
1081 const PRECOMP256_ROW *preComputedTable = NULL;
1082 const EC_PRE_COMP *pre_comp = NULL;
1083 const EC_POINT *generator = NULL;
1084 unsigned int index = 0;
1085 const unsigned int window_size = 7;
1086 const unsigned int mask = (1 << (window_size + 1)) - 1;
1087 unsigned int wvalue;
1090 P256_POINT_AFFINE a;
1094 if (group->meth != r->meth) {
1095 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1098 if ((scalar == NULL) && (num == 0))
1099 return EC_POINT_set_to_infinity(group, r);
1101 for (j = 0; j < num; j++) {
1102 if (group->meth != points[j]->meth) {
1103 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_INCOMPATIBLE_OBJECTS);
1108 /* Need 256 bits for space for all coordinates. */
1109 bn_wexpand(&r->X, P256_LIMBS);
1110 bn_wexpand(&r->Y, P256_LIMBS);
1111 bn_wexpand(&r->Z, P256_LIMBS);
1112 r->X.top = P256_LIMBS;
1113 r->Y.top = P256_LIMBS;
1114 r->Z.top = P256_LIMBS;
1117 generator = EC_GROUP_get0_generator(group);
1118 if (generator == NULL) {
1119 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, EC_R_UNDEFINED_GENERATOR);
1123 /* look if we can use precomputed multiples of generator */
1125 EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1126 ecp_nistz256_pre_comp_free,
1127 ecp_nistz256_pre_comp_clear_free);
1130 /* If there is a precomputed table for the generator,
1131 * check that it was generated with the same
1133 EC_POINT *pre_comp_generator = EC_POINT_new(group);
1134 if (pre_comp_generator == NULL)
1137 if (!ecp_nistz256_set_from_affine
1138 (pre_comp_generator, group, pre_comp->precomp[0], ctx))
1141 if (0 == EC_POINT_cmp(group, generator, pre_comp_generator, ctx))
1142 preComputedTable = (const PRECOMP256_ROW *)pre_comp->precomp;
1144 EC_POINT_free(pre_comp_generator);
1147 if (preComputedTable == NULL && ecp_nistz256_is_affine_G(generator)) {
1148 /* If there is no precomputed data, but the generator
1149 * is the default, a hardcoded table of precomputed
1150 * data is used. This is because applications, such as
1151 * Apache, do not use EC_KEY_precompute_mult. */
1152 preComputedTable = (const PRECOMP256_ROW *)ecp_nistz256_precomputed;
1155 if (preComputedTable) {
1156 if ((BN_num_bits(scalar) > 256)
1157 || BN_is_negative(scalar)) {
1158 if ((tmp_scalar = BN_CTX_get(ctx)) == NULL)
1161 if (!BN_nnmod(tmp_scalar, scalar, &group->order, ctx)) {
1162 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_BN_LIB);
1165 scalar = tmp_scalar;
1168 for (i = 0; i < scalar->top * BN_BYTES; i += BN_BYTES) {
1169 BN_ULONG d = scalar->d[i / BN_BYTES];
1171 p_str[i + 0] = d & 0xff;
1172 p_str[i + 1] = (d >> 8) & 0xff;
1173 p_str[i + 2] = (d >> 16) & 0xff;
1174 p_str[i + 3] = (d >>= 24) & 0xff;
1175 if (BN_BYTES == 8) {
1177 p_str[i + 4] = d & 0xff;
1178 p_str[i + 5] = (d >> 8) & 0xff;
1179 p_str[i + 6] = (d >> 16) & 0xff;
1180 p_str[i + 7] = (d >> 24) & 0xff;
1187 #if defined(ECP_NISTZ256_AVX2)
1188 if (ecp_nistz_avx2_eligible()) {
1189 ecp_nistz256_avx2_mul_g(&p.p, p_str, preComputedTable);
1194 wvalue = (p_str[0] << 1) & mask;
1195 index += window_size;
1197 wvalue = _booth_recode_w7(wvalue);
1199 ecp_nistz256_select_w7(&p.a, preComputedTable[0], wvalue >> 1);
1201 ecp_nistz256_neg(p.p.Z, p.p.Y);
1202 copy_conditional(p.p.Y, p.p.Z, wvalue & 1);
1204 memcpy(p.p.Z, ONE, sizeof(ONE));
1206 for (i = 1; i < 37; i++) {
1207 unsigned int off = (index - 1) / 8;
1208 wvalue = p_str[off] | p_str[off + 1] << 8;
1209 wvalue = (wvalue >> ((index - 1) % 8)) & mask;
1210 index += window_size;
1212 wvalue = _booth_recode_w7(wvalue);
1214 ecp_nistz256_select_w7(&t.a,
1215 preComputedTable[i], wvalue >> 1);
1217 ecp_nistz256_neg(t.p.Z, t.a.Y);
1218 copy_conditional(t.a.Y, t.p.Z, wvalue & 1);
1220 ecp_nistz256_point_add_affine(&p.p, &p.p, &t.a);
1225 no_precomp_for_generator = 1;
1230 if (no_precomp_for_generator) {
1231 /* Without a precomputed table for the generator, it has to be
1232 * handled like a normal point. */
1233 const BIGNUM **new_scalars;
1234 const EC_POINT **new_points;
1236 new_scalars = OPENSSL_malloc((num + 1) * sizeof(BIGNUM *));
1238 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1242 new_points = OPENSSL_malloc((num + 1) * sizeof(EC_POINT *));
1244 OPENSSL_free(new_scalars);
1245 ECerr(EC_F_ECP_NISTZ256_POINTS_MUL, ERR_R_MALLOC_FAILURE);
1249 memcpy(new_scalars, scalars, num * sizeof(BIGNUM *));
1250 new_scalars[num] = scalar;
1251 memcpy(new_points, points, num * sizeof(EC_POINT *));
1252 new_points[num] = generator;
1254 scalars = new_scalars;
1255 points = new_points;
1260 P256_POINT *out = &t.p;
1264 ecp_nistz256_windowed_mul(group, out, scalars, points, num, ctx);
1267 ecp_nistz256_point_add(&p.p, &p.p, out);
1270 if (no_precomp_for_generator) {
1271 OPENSSL_free(points);
1272 OPENSSL_free(scalars);
1275 memcpy(r->X.d, p.p.X, sizeof(p.p.X));
1276 memcpy(r->Y.d, p.p.Y, sizeof(p.p.Y));
1277 memcpy(r->Z.d, p.p.Z, sizeof(p.p.Z));
1278 bn_correct_top(&r->X);
1279 bn_correct_top(&r->Y);
1280 bn_correct_top(&r->Z);
1288 static int ecp_nistz256_get_affine(const EC_GROUP * group,
1289 const EC_POINT * point,
1290 BIGNUM * x, BIGNUM * y, BN_CTX * ctx)
1292 BN_ULONG z_inv2[P256_LIMBS];
1293 BN_ULONG z_inv3[P256_LIMBS];
1294 BN_ULONG x_aff[P256_LIMBS];
1295 BN_ULONG y_aff[P256_LIMBS];
1296 BN_ULONG point_x[P256_LIMBS], point_y[P256_LIMBS], point_z[P256_LIMBS];
1298 if (EC_POINT_is_at_infinity(group, point)) {
1299 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_POINT_AT_INFINITY);
1303 if (!ecp_nistz256_bignum_to_field_elem(point_x, &point->X) ||
1304 !ecp_nistz256_bignum_to_field_elem(point_y, &point->Y) ||
1305 !ecp_nistz256_bignum_to_field_elem(point_z, &point->Z)) {
1306 ECerr(EC_F_ECP_NISTZ256_GET_AFFINE, EC_R_COORDINATES_OUT_OF_RANGE);
1310 ecp_nistz256_mod_inverse(z_inv3, point_z);
1311 ecp_nistz256_sqr_mont(z_inv2, z_inv3);
1312 ecp_nistz256_mul_mont(x_aff, z_inv2, point_x);
1315 bn_wexpand(x, P256_LIMBS);
1316 x->top = P256_LIMBS;
1317 ecp_nistz256_from_mont(x->d, x_aff);
1322 ecp_nistz256_mul_mont(z_inv3, z_inv3, z_inv2);
1323 ecp_nistz256_mul_mont(y_aff, z_inv3, point_y);
1324 bn_wexpand(y, P256_LIMBS);
1325 y->top = P256_LIMBS;
1326 ecp_nistz256_from_mont(y->d, y_aff);
1333 static EC_PRE_COMP *ecp_nistz256_pre_comp_new(const EC_GROUP * group)
1335 EC_PRE_COMP *ret = NULL;
1340 ret = (EC_PRE_COMP *) OPENSSL_malloc(sizeof(EC_PRE_COMP));
1343 ECerr(EC_F_ECP_NISTZ256_PRE_COMP_NEW, ERR_R_MALLOC_FAILURE);
1348 ret->w = 6; /* default */
1349 ret->precomp = NULL;
1350 ret->precomp_storage = NULL;
1351 ret->references = 1;
1355 static void *ecp_nistz256_pre_comp_dup(void *src_)
1357 EC_PRE_COMP *src = src_;
1359 /* no need to actually copy, these objects never change! */
1360 CRYPTO_add(&src->references, 1, CRYPTO_LOCK_EC_PRE_COMP);
1365 static void ecp_nistz256_pre_comp_free(void *pre_)
1368 EC_PRE_COMP *pre = pre_;
1373 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1377 if (pre->precomp_storage)
1378 OPENSSL_free(pre->precomp_storage);
1383 static void ecp_nistz256_pre_comp_clear_free(void *pre_)
1386 EC_PRE_COMP *pre = pre_;
1391 i = CRYPTO_add(&pre->references, -1, CRYPTO_LOCK_EC_PRE_COMP);
1395 if (pre->precomp_storage) {
1396 OPENSSL_cleanse(pre->precomp,
1397 32 * sizeof(unsigned char) * (1 << pre->w) * 2 * 37);
1398 OPENSSL_free(pre->precomp_storage);
1400 OPENSSL_cleanse(pre, sizeof *pre);
1404 static int ecp_nistz256_window_have_precompute_mult(const EC_GROUP * group)
1406 /* There is a hard-coded table for the default generator. */
1407 const EC_POINT *generator = EC_GROUP_get0_generator(group);
1408 if (generator != NULL && ecp_nistz256_is_affine_G(generator)) {
1409 /* There is a hard-coded table for the default generator. */
1413 return EC_EX_DATA_get_data(group->extra_data, ecp_nistz256_pre_comp_dup,
1414 ecp_nistz256_pre_comp_free,
1415 ecp_nistz256_pre_comp_clear_free) != NULL;
1418 const EC_METHOD *EC_GFp_nistz256_method(void)
1420 static const EC_METHOD ret = {
1421 EC_FLAGS_DEFAULT_OCT,
1422 NID_X9_62_prime_field,
1423 ec_GFp_mont_group_init,
1424 ec_GFp_mont_group_finish,
1425 ec_GFp_mont_group_clear_finish,
1426 ec_GFp_mont_group_copy,
1427 ec_GFp_mont_group_set_curve,
1428 ec_GFp_simple_group_get_curve,
1429 ec_GFp_simple_group_get_degree,
1430 ec_GFp_simple_group_check_discriminant,
1431 ec_GFp_simple_point_init,
1432 ec_GFp_simple_point_finish,
1433 ec_GFp_simple_point_clear_finish,
1434 ec_GFp_simple_point_copy,
1435 ec_GFp_simple_point_set_to_infinity,
1436 ec_GFp_simple_set_Jprojective_coordinates_GFp,
1437 ec_GFp_simple_get_Jprojective_coordinates_GFp,
1438 ec_GFp_simple_point_set_affine_coordinates,
1439 ecp_nistz256_get_affine,
1443 ec_GFp_simple_invert,
1444 ec_GFp_simple_is_at_infinity,
1445 ec_GFp_simple_is_on_curve,
1447 ec_GFp_simple_make_affine,
1448 ec_GFp_simple_points_make_affine,
1449 ecp_nistz256_points_mul, /* mul */
1450 ecp_nistz256_mult_precompute, /* precompute_mult */
1451 ecp_nistz256_window_have_precompute_mult, /* have_precompute_mult */
1452 ec_GFp_mont_field_mul,
1453 ec_GFp_mont_field_sqr,
1455 ec_GFp_mont_field_encode,
1456 ec_GFp_mont_field_decode,
1457 ec_GFp_mont_field_set_to_one