1 /* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 /* A 64-bit implementation of the NIST P-256 elliptic curve point
16 * multiplication
17 *
18 * OpenSSL integration was taken from Emilia Kasper's work in ecp_nistp224.c.
19 * Otherwise based on Emilia's P224 work, which was inspired by my curve25519
20 * work which got its smarts from Daniel J. Bernstein's work on the same. */
21
22 #include <openssl/base.h>
23
24 #if defined(OPENSSL_64_BIT) && !defined(OPENSSL_WINDOWS)
25
26 #include <openssl/bn.h>
27 #include <openssl/ec.h>
28 #include <openssl/err.h>
29 #include <openssl/mem.h>
30
31 #include <string.h>
32
33 #include "../delocate.h"
34 #include "../../internal.h"
35 #include "internal.h"
36
37
38 /* The underlying field. P256 operates over GF(2^256-2^224+2^192+2^96-1). We
39 * can serialise an element of this field into 32 bytes. We call this an
40 * felem_bytearray. */
41 typedef uint8_t felem_bytearray[32];
42
43 /* The representation of field elements.
44 * ------------------------------------
45 *
46 * We represent field elements with either four 128-bit values, eight 128-bit
47 * values, or four 64-bit values. The field element represented is:
48 * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + v[3]*2^192 (mod p)
49 * or:
50 * v[0]*2^0 + v[1]*2^64 + v[2]*2^128 + ... + v[8]*2^512 (mod p)
51 *
52 * 128-bit values are called 'limbs'. Since the limbs are spaced only 64 bits
53 * apart, but are 128-bits wide, the most significant bits of each limb overlap
54 * with the least significant bits of the next.
55 *
56 * A field element with four limbs is an 'felem'. One with eight limbs is a
57 * 'longfelem'
58 *
59 * A field element with four, 64-bit values is called a 'smallfelem'. Small
60 * values are used as intermediate values before multiplication. */
61
62 #define NLIMBS 4
63
64 typedef uint128_t limb;
65 typedef limb felem[NLIMBS];
66 typedef limb longfelem[NLIMBS * 2];
67 typedef uint64_t smallfelem[NLIMBS];
68
69 /* This is the value of the prime as four 64-bit words, little-endian. */
70 static const uint64_t kPrime[4] = {0xfffffffffffffffful, 0xffffffff, 0,
71 0xffffffff00000001ul};
72 static const uint64_t bottom63bits = 0x7ffffffffffffffful;
73
load_u64(const uint8_t in[8])74 static uint64_t load_u64(const uint8_t in[8]) {
75 uint64_t ret;
76 OPENSSL_memcpy(&ret, in, sizeof(ret));
77 return ret;
78 }
79
store_u64(uint8_t out[8],uint64_t in)80 static void store_u64(uint8_t out[8], uint64_t in) {
81 OPENSSL_memcpy(out, &in, sizeof(in));
82 }
83
84 /* bin32_to_felem takes a little-endian byte array and converts it into felem
85 * form. This assumes that the CPU is little-endian. */
bin32_to_felem(felem out,const uint8_t in[32])86 static void bin32_to_felem(felem out, const uint8_t in[32]) {
87 out[0] = load_u64(&in[0]);
88 out[1] = load_u64(&in[8]);
89 out[2] = load_u64(&in[16]);
90 out[3] = load_u64(&in[24]);
91 }
92
93 /* smallfelem_to_bin32 takes a smallfelem and serialises into a little endian,
94 * 32 byte array. This assumes that the CPU is little-endian. */
smallfelem_to_bin32(uint8_t out[32],const smallfelem in)95 static void smallfelem_to_bin32(uint8_t out[32], const smallfelem in) {
96 store_u64(&out[0], in[0]);
97 store_u64(&out[8], in[1]);
98 store_u64(&out[16], in[2]);
99 store_u64(&out[24], in[3]);
100 }
101
102 /* To preserve endianness when using BN_bn2bin and BN_bin2bn. */
flip_endian(uint8_t * out,const uint8_t * in,size_t len)103 static void flip_endian(uint8_t *out, const uint8_t *in, size_t len) {
104 for (size_t i = 0; i < len; ++i) {
105 out[i] = in[len - 1 - i];
106 }
107 }
108
109 /* BN_to_felem converts an OpenSSL BIGNUM into an felem. */
BN_to_felem(felem out,const BIGNUM * bn)110 static int BN_to_felem(felem out, const BIGNUM *bn) {
111 if (BN_is_negative(bn)) {
112 OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
113 return 0;
114 }
115
116 felem_bytearray b_out;
117 /* BN_bn2bin eats leading zeroes */
118 OPENSSL_memset(b_out, 0, sizeof(b_out));
119 size_t num_bytes = BN_num_bytes(bn);
120 if (num_bytes > sizeof(b_out)) {
121 OPENSSL_PUT_ERROR(EC, EC_R_BIGNUM_OUT_OF_RANGE);
122 return 0;
123 }
124
125 felem_bytearray b_in;
126 num_bytes = BN_bn2bin(bn, b_in);
127 flip_endian(b_out, b_in, num_bytes);
128 bin32_to_felem(out, b_out);
129 return 1;
130 }
131
132 /* felem_to_BN converts an felem into an OpenSSL BIGNUM. */
smallfelem_to_BN(BIGNUM * out,const smallfelem in)133 static BIGNUM *smallfelem_to_BN(BIGNUM *out, const smallfelem in) {
134 felem_bytearray b_in, b_out;
135 smallfelem_to_bin32(b_in, in);
136 flip_endian(b_out, b_in, sizeof(b_out));
137 return BN_bin2bn(b_out, sizeof(b_out), out);
138 }
139
140 /* Field operations. */
141
felem_assign(felem out,const felem in)142 static void felem_assign(felem out, const felem in) {
143 out[0] = in[0];
144 out[1] = in[1];
145 out[2] = in[2];
146 out[3] = in[3];
147 }
148
149 /* felem_sum sets out = out + in. */
felem_sum(felem out,const felem in)150 static void felem_sum(felem out, const felem in) {
151 out[0] += in[0];
152 out[1] += in[1];
153 out[2] += in[2];
154 out[3] += in[3];
155 }
156
157 /* felem_small_sum sets out = out + in. */
felem_small_sum(felem out,const smallfelem in)158 static void felem_small_sum(felem out, const smallfelem in) {
159 out[0] += in[0];
160 out[1] += in[1];
161 out[2] += in[2];
162 out[3] += in[3];
163 }
164
165 /* felem_scalar sets out = out * scalar */
felem_scalar(felem out,const uint64_t scalar)166 static void felem_scalar(felem out, const uint64_t scalar) {
167 out[0] *= scalar;
168 out[1] *= scalar;
169 out[2] *= scalar;
170 out[3] *= scalar;
171 }
172
173 /* longfelem_scalar sets out = out * scalar */
longfelem_scalar(longfelem out,const uint64_t scalar)174 static void longfelem_scalar(longfelem out, const uint64_t scalar) {
175 out[0] *= scalar;
176 out[1] *= scalar;
177 out[2] *= scalar;
178 out[3] *= scalar;
179 out[4] *= scalar;
180 out[5] *= scalar;
181 out[6] *= scalar;
182 out[7] *= scalar;
183 }
184
185 #define two105m41m9 ((((limb)1) << 105) - (((limb)1) << 41) - (((limb)1) << 9))
186 #define two105 (((limb)1) << 105)
187 #define two105m41p9 ((((limb)1) << 105) - (((limb)1) << 41) + (((limb)1) << 9))
188
189 /* zero105 is 0 mod p */
190 static const felem zero105 = {two105m41m9, two105, two105m41p9, two105m41p9};
191
192 /* smallfelem_neg sets |out| to |-small|
193 * On exit:
194 * out[i] < out[i] + 2^105 */
smallfelem_neg(felem out,const smallfelem small)195 static void smallfelem_neg(felem out, const smallfelem small) {
196 /* In order to prevent underflow, we subtract from 0 mod p. */
197 out[0] = zero105[0] - small[0];
198 out[1] = zero105[1] - small[1];
199 out[2] = zero105[2] - small[2];
200 out[3] = zero105[3] - small[3];
201 }
202
203 /* felem_diff subtracts |in| from |out|
204 * On entry:
205 * in[i] < 2^104
206 * On exit:
207 * out[i] < out[i] + 2^105. */
felem_diff(felem out,const felem in)208 static void felem_diff(felem out, const felem in) {
209 /* In order to prevent underflow, we add 0 mod p before subtracting. */
210 out[0] += zero105[0];
211 out[1] += zero105[1];
212 out[2] += zero105[2];
213 out[3] += zero105[3];
214
215 out[0] -= in[0];
216 out[1] -= in[1];
217 out[2] -= in[2];
218 out[3] -= in[3];
219 }
220
221 #define two107m43m11 \
222 ((((limb)1) << 107) - (((limb)1) << 43) - (((limb)1) << 11))
223 #define two107 (((limb)1) << 107)
224 #define two107m43p11 \
225 ((((limb)1) << 107) - (((limb)1) << 43) + (((limb)1) << 11))
226
227 /* zero107 is 0 mod p */
228 static const felem zero107 = {two107m43m11, two107, two107m43p11, two107m43p11};
229
230 /* An alternative felem_diff for larger inputs |in|
231 * felem_diff_zero107 subtracts |in| from |out|
232 * On entry:
233 * in[i] < 2^106
234 * On exit:
235 * out[i] < out[i] + 2^107. */
felem_diff_zero107(felem out,const felem in)236 static void felem_diff_zero107(felem out, const felem in) {
237 /* In order to prevent underflow, we add 0 mod p before subtracting. */
238 out[0] += zero107[0];
239 out[1] += zero107[1];
240 out[2] += zero107[2];
241 out[3] += zero107[3];
242
243 out[0] -= in[0];
244 out[1] -= in[1];
245 out[2] -= in[2];
246 out[3] -= in[3];
247 }
248
249 /* longfelem_diff subtracts |in| from |out|
250 * On entry:
251 * in[i] < 7*2^67
252 * On exit:
253 * out[i] < out[i] + 2^70 + 2^40. */
longfelem_diff(longfelem out,const longfelem in)254 static void longfelem_diff(longfelem out, const longfelem in) {
255 static const limb two70m8p6 =
256 (((limb)1) << 70) - (((limb)1) << 8) + (((limb)1) << 6);
257 static const limb two70p40 = (((limb)1) << 70) + (((limb)1) << 40);
258 static const limb two70 = (((limb)1) << 70);
259 static const limb two70m40m38p6 = (((limb)1) << 70) - (((limb)1) << 40) -
260 (((limb)1) << 38) + (((limb)1) << 6);
261 static const limb two70m6 = (((limb)1) << 70) - (((limb)1) << 6);
262
263 /* add 0 mod p to avoid underflow */
264 out[0] += two70m8p6;
265 out[1] += two70p40;
266 out[2] += two70;
267 out[3] += two70m40m38p6;
268 out[4] += two70m6;
269 out[5] += two70m6;
270 out[6] += two70m6;
271 out[7] += two70m6;
272
273 /* in[i] < 7*2^67 < 2^70 - 2^40 - 2^38 + 2^6 */
274 out[0] -= in[0];
275 out[1] -= in[1];
276 out[2] -= in[2];
277 out[3] -= in[3];
278 out[4] -= in[4];
279 out[5] -= in[5];
280 out[6] -= in[6];
281 out[7] -= in[7];
282 }
283
284 #define two64m0 ((((limb)1) << 64) - 1)
285 #define two110p32m0 ((((limb)1) << 110) + (((limb)1) << 32) - 1)
286 #define two64m46 ((((limb)1) << 64) - (((limb)1) << 46))
287 #define two64m32 ((((limb)1) << 64) - (((limb)1) << 32))
288
289 /* zero110 is 0 mod p. */
290 static const felem zero110 = {two64m0, two110p32m0, two64m46, two64m32};
291
292 /* felem_shrink converts an felem into a smallfelem. The result isn't quite
293 * minimal as the value may be greater than p.
294 *
295 * On entry:
296 * in[i] < 2^109
297 * On exit:
298 * out[i] < 2^64. */
felem_shrink(smallfelem out,const felem in)299 static void felem_shrink(smallfelem out, const felem in) {
300 felem tmp;
301 uint64_t a, b, mask;
302 int64_t high, low;
303 static const uint64_t kPrime3Test =
304 0x7fffffff00000001ul; /* 2^63 - 2^32 + 1 */
305
306 /* Carry 2->3 */
307 tmp[3] = zero110[3] + in[3] + ((uint64_t)(in[2] >> 64));
308 /* tmp[3] < 2^110 */
309
310 tmp[2] = zero110[2] + (uint64_t)in[2];
311 tmp[0] = zero110[0] + in[0];
312 tmp[1] = zero110[1] + in[1];
313 /* tmp[0] < 2**110, tmp[1] < 2^111, tmp[2] < 2**65 */
314
315 /* We perform two partial reductions where we eliminate the high-word of
316 * tmp[3]. We don't update the other words till the end. */
317 a = tmp[3] >> 64; /* a < 2^46 */
318 tmp[3] = (uint64_t)tmp[3];
319 tmp[3] -= a;
320 tmp[3] += ((limb)a) << 32;
321 /* tmp[3] < 2^79 */
322
323 b = a;
324 a = tmp[3] >> 64; /* a < 2^15 */
325 b += a; /* b < 2^46 + 2^15 < 2^47 */
326 tmp[3] = (uint64_t)tmp[3];
327 tmp[3] -= a;
328 tmp[3] += ((limb)a) << 32;
329 /* tmp[3] < 2^64 + 2^47 */
330
331 /* This adjusts the other two words to complete the two partial
332 * reductions. */
333 tmp[0] += b;
334 tmp[1] -= (((limb)b) << 32);
335
336 /* In order to make space in tmp[3] for the carry from 2 -> 3, we
337 * conditionally subtract kPrime if tmp[3] is large enough. */
338 high = tmp[3] >> 64;
339 /* As tmp[3] < 2^65, high is either 1 or 0 */
340 high = ~(high - 1);
341 /* high is:
342 * all ones if the high word of tmp[3] is 1
343 * all zeros if the high word of tmp[3] if 0 */
344 low = tmp[3];
345 mask = low >> 63;
346 /* mask is:
347 * all ones if the MSB of low is 1
348 * all zeros if the MSB of low if 0 */
349 low &= bottom63bits;
350 low -= kPrime3Test;
351 /* if low was greater than kPrime3Test then the MSB is zero */
352 low = ~low;
353 low >>= 63;
354 /* low is:
355 * all ones if low was > kPrime3Test
356 * all zeros if low was <= kPrime3Test */
357 mask = (mask & low) | high;
358 tmp[0] -= mask & kPrime[0];
359 tmp[1] -= mask & kPrime[1];
360 /* kPrime[2] is zero, so omitted */
361 tmp[3] -= mask & kPrime[3];
362 /* tmp[3] < 2**64 - 2**32 + 1 */
363
364 tmp[1] += ((uint64_t)(tmp[0] >> 64));
365 tmp[0] = (uint64_t)tmp[0];
366 tmp[2] += ((uint64_t)(tmp[1] >> 64));
367 tmp[1] = (uint64_t)tmp[1];
368 tmp[3] += ((uint64_t)(tmp[2] >> 64));
369 tmp[2] = (uint64_t)tmp[2];
370 /* tmp[i] < 2^64 */
371
372 out[0] = tmp[0];
373 out[1] = tmp[1];
374 out[2] = tmp[2];
375 out[3] = tmp[3];
376 }
377
378 /* smallfelem_expand converts a smallfelem to an felem */
smallfelem_expand(felem out,const smallfelem in)379 static void smallfelem_expand(felem out, const smallfelem in) {
380 out[0] = in[0];
381 out[1] = in[1];
382 out[2] = in[2];
383 out[3] = in[3];
384 }
385
386 /* smallfelem_square sets |out| = |small|^2
387 * On entry:
388 * small[i] < 2^64
389 * On exit:
390 * out[i] < 7 * 2^64 < 2^67 */
smallfelem_square(longfelem out,const smallfelem small)391 static void smallfelem_square(longfelem out, const smallfelem small) {
392 limb a;
393 uint64_t high, low;
394
395 a = ((uint128_t)small[0]) * small[0];
396 low = a;
397 high = a >> 64;
398 out[0] = low;
399 out[1] = high;
400
401 a = ((uint128_t)small[0]) * small[1];
402 low = a;
403 high = a >> 64;
404 out[1] += low;
405 out[1] += low;
406 out[2] = high;
407
408 a = ((uint128_t)small[0]) * small[2];
409 low = a;
410 high = a >> 64;
411 out[2] += low;
412 out[2] *= 2;
413 out[3] = high;
414
415 a = ((uint128_t)small[0]) * small[3];
416 low = a;
417 high = a >> 64;
418 out[3] += low;
419 out[4] = high;
420
421 a = ((uint128_t)small[1]) * small[2];
422 low = a;
423 high = a >> 64;
424 out[3] += low;
425 out[3] *= 2;
426 out[4] += high;
427
428 a = ((uint128_t)small[1]) * small[1];
429 low = a;
430 high = a >> 64;
431 out[2] += low;
432 out[3] += high;
433
434 a = ((uint128_t)small[1]) * small[3];
435 low = a;
436 high = a >> 64;
437 out[4] += low;
438 out[4] *= 2;
439 out[5] = high;
440
441 a = ((uint128_t)small[2]) * small[3];
442 low = a;
443 high = a >> 64;
444 out[5] += low;
445 out[5] *= 2;
446 out[6] = high;
447 out[6] += high;
448
449 a = ((uint128_t)small[2]) * small[2];
450 low = a;
451 high = a >> 64;
452 out[4] += low;
453 out[5] += high;
454
455 a = ((uint128_t)small[3]) * small[3];
456 low = a;
457 high = a >> 64;
458 out[6] += low;
459 out[7] = high;
460 }
461
462 /*felem_square sets |out| = |in|^2
463 * On entry:
464 * in[i] < 2^109
465 * On exit:
466 * out[i] < 7 * 2^64 < 2^67. */
felem_square(longfelem out,const felem in)467 static void felem_square(longfelem out, const felem in) {
468 uint64_t small[4];
469 felem_shrink(small, in);
470 smallfelem_square(out, small);
471 }
472
473 /* smallfelem_mul sets |out| = |small1| * |small2|
474 * On entry:
475 * small1[i] < 2^64
476 * small2[i] < 2^64
477 * On exit:
478 * out[i] < 7 * 2^64 < 2^67. */
smallfelem_mul(longfelem out,const smallfelem small1,const smallfelem small2)479 static void smallfelem_mul(longfelem out, const smallfelem small1,
480 const smallfelem small2) {
481 limb a;
482 uint64_t high, low;
483
484 a = ((uint128_t)small1[0]) * small2[0];
485 low = a;
486 high = a >> 64;
487 out[0] = low;
488 out[1] = high;
489
490 a = ((uint128_t)small1[0]) * small2[1];
491 low = a;
492 high = a >> 64;
493 out[1] += low;
494 out[2] = high;
495
496 a = ((uint128_t)small1[1]) * small2[0];
497 low = a;
498 high = a >> 64;
499 out[1] += low;
500 out[2] += high;
501
502 a = ((uint128_t)small1[0]) * small2[2];
503 low = a;
504 high = a >> 64;
505 out[2] += low;
506 out[3] = high;
507
508 a = ((uint128_t)small1[1]) * small2[1];
509 low = a;
510 high = a >> 64;
511 out[2] += low;
512 out[3] += high;
513
514 a = ((uint128_t)small1[2]) * small2[0];
515 low = a;
516 high = a >> 64;
517 out[2] += low;
518 out[3] += high;
519
520 a = ((uint128_t)small1[0]) * small2[3];
521 low = a;
522 high = a >> 64;
523 out[3] += low;
524 out[4] = high;
525
526 a = ((uint128_t)small1[1]) * small2[2];
527 low = a;
528 high = a >> 64;
529 out[3] += low;
530 out[4] += high;
531
532 a = ((uint128_t)small1[2]) * small2[1];
533 low = a;
534 high = a >> 64;
535 out[3] += low;
536 out[4] += high;
537
538 a = ((uint128_t)small1[3]) * small2[0];
539 low = a;
540 high = a >> 64;
541 out[3] += low;
542 out[4] += high;
543
544 a = ((uint128_t)small1[1]) * small2[3];
545 low = a;
546 high = a >> 64;
547 out[4] += low;
548 out[5] = high;
549
550 a = ((uint128_t)small1[2]) * small2[2];
551 low = a;
552 high = a >> 64;
553 out[4] += low;
554 out[5] += high;
555
556 a = ((uint128_t)small1[3]) * small2[1];
557 low = a;
558 high = a >> 64;
559 out[4] += low;
560 out[5] += high;
561
562 a = ((uint128_t)small1[2]) * small2[3];
563 low = a;
564 high = a >> 64;
565 out[5] += low;
566 out[6] = high;
567
568 a = ((uint128_t)small1[3]) * small2[2];
569 low = a;
570 high = a >> 64;
571 out[5] += low;
572 out[6] += high;
573
574 a = ((uint128_t)small1[3]) * small2[3];
575 low = a;
576 high = a >> 64;
577 out[6] += low;
578 out[7] = high;
579 }
580
581 /* felem_mul sets |out| = |in1| * |in2|
582 * On entry:
583 * in1[i] < 2^109
584 * in2[i] < 2^109
585 * On exit:
586 * out[i] < 7 * 2^64 < 2^67 */
felem_mul(longfelem out,const felem in1,const felem in2)587 static void felem_mul(longfelem out, const felem in1, const felem in2) {
588 smallfelem small1, small2;
589 felem_shrink(small1, in1);
590 felem_shrink(small2, in2);
591 smallfelem_mul(out, small1, small2);
592 }
593
594 /* felem_small_mul sets |out| = |small1| * |in2|
595 * On entry:
596 * small1[i] < 2^64
597 * in2[i] < 2^109
598 * On exit:
599 * out[i] < 7 * 2^64 < 2^67 */
felem_small_mul(longfelem out,const smallfelem small1,const felem in2)600 static void felem_small_mul(longfelem out, const smallfelem small1,
601 const felem in2) {
602 smallfelem small2;
603 felem_shrink(small2, in2);
604 smallfelem_mul(out, small1, small2);
605 }
606
607 #define two100m36m4 ((((limb)1) << 100) - (((limb)1) << 36) - (((limb)1) << 4))
608 #define two100 (((limb)1) << 100)
609 #define two100m36p4 ((((limb)1) << 100) - (((limb)1) << 36) + (((limb)1) << 4))
610
611 /* zero100 is 0 mod p */
612 static const felem zero100 = {two100m36m4, two100, two100m36p4, two100m36p4};
613
614 /* Internal function for the different flavours of felem_reduce.
615 * felem_reduce_ reduces the higher coefficients in[4]-in[7].
616 * On entry:
617 * out[0] >= in[6] + 2^32*in[6] + in[7] + 2^32*in[7]
618 * out[1] >= in[7] + 2^32*in[4]
619 * out[2] >= in[5] + 2^32*in[5]
620 * out[3] >= in[4] + 2^32*in[5] + 2^32*in[6]
621 * On exit:
622 * out[0] <= out[0] + in[4] + 2^32*in[5]
623 * out[1] <= out[1] + in[5] + 2^33*in[6]
624 * out[2] <= out[2] + in[7] + 2*in[6] + 2^33*in[7]
625 * out[3] <= out[3] + 2^32*in[4] + 3*in[7] */
felem_reduce_(felem out,const longfelem in)626 static void felem_reduce_(felem out, const longfelem in) {
627 int128_t c;
628 /* combine common terms from below */
629 c = in[4] + (in[5] << 32);
630 out[0] += c;
631 out[3] -= c;
632
633 c = in[5] - in[7];
634 out[1] += c;
635 out[2] -= c;
636
637 /* the remaining terms */
638 /* 256: [(0,1),(96,-1),(192,-1),(224,1)] */
639 out[1] -= (in[4] << 32);
640 out[3] += (in[4] << 32);
641
642 /* 320: [(32,1),(64,1),(128,-1),(160,-1),(224,-1)] */
643 out[2] -= (in[5] << 32);
644
645 /* 384: [(0,-1),(32,-1),(96,2),(128,2),(224,-1)] */
646 out[0] -= in[6];
647 out[0] -= (in[6] << 32);
648 out[1] += (in[6] << 33);
649 out[2] += (in[6] * 2);
650 out[3] -= (in[6] << 32);
651
652 /* 448: [(0,-1),(32,-1),(64,-1),(128,1),(160,2),(192,3)] */
653 out[0] -= in[7];
654 out[0] -= (in[7] << 32);
655 out[2] += (in[7] << 33);
656 out[3] += (in[7] * 3);
657 }
658
659 /* felem_reduce converts a longfelem into an felem.
660 * To be called directly after felem_square or felem_mul.
661 * On entry:
662 * in[0] < 2^64, in[1] < 3*2^64, in[2] < 5*2^64, in[3] < 7*2^64
663 * in[4] < 7*2^64, in[5] < 5*2^64, in[6] < 3*2^64, in[7] < 2*64
664 * On exit:
665 * out[i] < 2^101 */
felem_reduce(felem out,const longfelem in)666 static void felem_reduce(felem out, const longfelem in) {
667 out[0] = zero100[0] + in[0];
668 out[1] = zero100[1] + in[1];
669 out[2] = zero100[2] + in[2];
670 out[3] = zero100[3] + in[3];
671
672 felem_reduce_(out, in);
673
674 /* out[0] > 2^100 - 2^36 - 2^4 - 3*2^64 - 3*2^96 - 2^64 - 2^96 > 0
675 * out[1] > 2^100 - 2^64 - 7*2^96 > 0
676 * out[2] > 2^100 - 2^36 + 2^4 - 5*2^64 - 5*2^96 > 0
677 * out[3] > 2^100 - 2^36 + 2^4 - 7*2^64 - 5*2^96 - 3*2^96 > 0
678 *
679 * out[0] < 2^100 + 2^64 + 7*2^64 + 5*2^96 < 2^101
680 * out[1] < 2^100 + 3*2^64 + 5*2^64 + 3*2^97 < 2^101
681 * out[2] < 2^100 + 5*2^64 + 2^64 + 3*2^65 + 2^97 < 2^101
682 * out[3] < 2^100 + 7*2^64 + 7*2^96 + 3*2^64 < 2^101 */
683 }
684
685 /* felem_reduce_zero105 converts a larger longfelem into an felem.
686 * On entry:
687 * in[0] < 2^71
688 * On exit:
689 * out[i] < 2^106 */
felem_reduce_zero105(felem out,const longfelem in)690 static void felem_reduce_zero105(felem out, const longfelem in) {
691 out[0] = zero105[0] + in[0];
692 out[1] = zero105[1] + in[1];
693 out[2] = zero105[2] + in[2];
694 out[3] = zero105[3] + in[3];
695
696 felem_reduce_(out, in);
697
698 /* out[0] > 2^105 - 2^41 - 2^9 - 2^71 - 2^103 - 2^71 - 2^103 > 0
699 * out[1] > 2^105 - 2^71 - 2^103 > 0
700 * out[2] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 > 0
701 * out[3] > 2^105 - 2^41 + 2^9 - 2^71 - 2^103 - 2^103 > 0
702 *
703 * out[0] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
704 * out[1] < 2^105 + 2^71 + 2^71 + 2^103 < 2^106
705 * out[2] < 2^105 + 2^71 + 2^71 + 2^71 + 2^103 < 2^106
706 * out[3] < 2^105 + 2^71 + 2^103 + 2^71 < 2^106 */
707 }
708
709 /* subtract_u64 sets *result = *result - v and *carry to one if the
710 * subtraction underflowed. */
subtract_u64(uint64_t * result,uint64_t * carry,uint64_t v)711 static void subtract_u64(uint64_t *result, uint64_t *carry, uint64_t v) {
712 uint128_t r = *result;
713 r -= v;
714 *carry = (r >> 64) & 1;
715 *result = (uint64_t)r;
716 }
717
718 /* felem_contract converts |in| to its unique, minimal representation. On
719 * entry: in[i] < 2^109. */
felem_contract(smallfelem out,const felem in)720 static void felem_contract(smallfelem out, const felem in) {
721 uint64_t all_equal_so_far = 0, result = 0;
722
723 felem_shrink(out, in);
724 /* small is minimal except that the value might be > p */
725
726 all_equal_so_far--;
727 /* We are doing a constant time test if out >= kPrime. We need to compare
728 * each uint64_t, from most-significant to least significant. For each one, if
729 * all words so far have been equal (m is all ones) then a non-equal
730 * result is the answer. Otherwise we continue. */
731 for (size_t i = 3; i < 4; i--) {
732 uint64_t equal;
733 uint128_t a = ((uint128_t)kPrime[i]) - out[i];
734 /* if out[i] > kPrime[i] then a will underflow and the high 64-bits
735 * will all be set. */
736 result |= all_equal_so_far & ((uint64_t)(a >> 64));
737
738 /* if kPrime[i] == out[i] then |equal| will be all zeros and the
739 * decrement will make it all ones. */
740 equal = kPrime[i] ^ out[i];
741 equal--;
742 equal &= equal << 32;
743 equal &= equal << 16;
744 equal &= equal << 8;
745 equal &= equal << 4;
746 equal &= equal << 2;
747 equal &= equal << 1;
748 equal = ((int64_t)equal) >> 63;
749
750 all_equal_so_far &= equal;
751 }
752
753 /* if all_equal_so_far is still all ones then the two values are equal
754 * and so out >= kPrime is true. */
755 result |= all_equal_so_far;
756
757 /* if out >= kPrime then we subtract kPrime. */
758 uint64_t carry;
759 subtract_u64(&out[0], &carry, result & kPrime[0]);
760 subtract_u64(&out[1], &carry, carry);
761 subtract_u64(&out[2], &carry, carry);
762 subtract_u64(&out[3], &carry, carry);
763
764 subtract_u64(&out[1], &carry, result & kPrime[1]);
765 subtract_u64(&out[2], &carry, carry);
766 subtract_u64(&out[3], &carry, carry);
767
768 subtract_u64(&out[2], &carry, result & kPrime[2]);
769 subtract_u64(&out[3], &carry, carry);
770
771 subtract_u64(&out[3], &carry, result & kPrime[3]);
772 }
773
774 /* felem_is_zero returns a limb with all bits set if |in| == 0 (mod p) and 0
775 * otherwise.
776 * On entry:
777 * small[i] < 2^64 */
smallfelem_is_zero(const smallfelem small)778 static limb smallfelem_is_zero(const smallfelem small) {
779 limb result;
780 uint64_t is_p;
781
782 uint64_t is_zero = small[0] | small[1] | small[2] | small[3];
783 is_zero--;
784 is_zero &= is_zero << 32;
785 is_zero &= is_zero << 16;
786 is_zero &= is_zero << 8;
787 is_zero &= is_zero << 4;
788 is_zero &= is_zero << 2;
789 is_zero &= is_zero << 1;
790 is_zero = ((int64_t)is_zero) >> 63;
791
792 is_p = (small[0] ^ kPrime[0]) | (small[1] ^ kPrime[1]) |
793 (small[2] ^ kPrime[2]) | (small[3] ^ kPrime[3]);
794 is_p--;
795 is_p &= is_p << 32;
796 is_p &= is_p << 16;
797 is_p &= is_p << 8;
798 is_p &= is_p << 4;
799 is_p &= is_p << 2;
800 is_p &= is_p << 1;
801 is_p = ((int64_t)is_p) >> 63;
802
803 is_zero |= is_p;
804
805 result = is_zero;
806 result |= ((limb)is_zero) << 64;
807 return result;
808 }
809
810 /* felem_inv calculates |out| = |in|^{-1}
811 *
812 * Based on Fermat's Little Theorem:
813 * a^p = a (mod p)
814 * a^{p-1} = 1 (mod p)
815 * a^{p-2} = a^{-1} (mod p) */
felem_inv(felem out,const felem in)816 static void felem_inv(felem out, const felem in) {
817 felem ftmp, ftmp2;
818 /* each e_I will hold |in|^{2^I - 1} */
819 felem e2, e4, e8, e16, e32, e64;
820 longfelem tmp;
821
822 felem_square(tmp, in);
823 felem_reduce(ftmp, tmp); /* 2^1 */
824 felem_mul(tmp, in, ftmp);
825 felem_reduce(ftmp, tmp); /* 2^2 - 2^0 */
826 felem_assign(e2, ftmp);
827 felem_square(tmp, ftmp);
828 felem_reduce(ftmp, tmp); /* 2^3 - 2^1 */
829 felem_square(tmp, ftmp);
830 felem_reduce(ftmp, tmp); /* 2^4 - 2^2 */
831 felem_mul(tmp, ftmp, e2);
832 felem_reduce(ftmp, tmp); /* 2^4 - 2^0 */
833 felem_assign(e4, ftmp);
834 felem_square(tmp, ftmp);
835 felem_reduce(ftmp, tmp); /* 2^5 - 2^1 */
836 felem_square(tmp, ftmp);
837 felem_reduce(ftmp, tmp); /* 2^6 - 2^2 */
838 felem_square(tmp, ftmp);
839 felem_reduce(ftmp, tmp); /* 2^7 - 2^3 */
840 felem_square(tmp, ftmp);
841 felem_reduce(ftmp, tmp); /* 2^8 - 2^4 */
842 felem_mul(tmp, ftmp, e4);
843 felem_reduce(ftmp, tmp); /* 2^8 - 2^0 */
844 felem_assign(e8, ftmp);
845 for (size_t i = 0; i < 8; i++) {
846 felem_square(tmp, ftmp);
847 felem_reduce(ftmp, tmp);
848 } /* 2^16 - 2^8 */
849 felem_mul(tmp, ftmp, e8);
850 felem_reduce(ftmp, tmp); /* 2^16 - 2^0 */
851 felem_assign(e16, ftmp);
852 for (size_t i = 0; i < 16; i++) {
853 felem_square(tmp, ftmp);
854 felem_reduce(ftmp, tmp);
855 } /* 2^32 - 2^16 */
856 felem_mul(tmp, ftmp, e16);
857 felem_reduce(ftmp, tmp); /* 2^32 - 2^0 */
858 felem_assign(e32, ftmp);
859 for (size_t i = 0; i < 32; i++) {
860 felem_square(tmp, ftmp);
861 felem_reduce(ftmp, tmp);
862 } /* 2^64 - 2^32 */
863 felem_assign(e64, ftmp);
864 felem_mul(tmp, ftmp, in);
865 felem_reduce(ftmp, tmp); /* 2^64 - 2^32 + 2^0 */
866 for (size_t i = 0; i < 192; i++) {
867 felem_square(tmp, ftmp);
868 felem_reduce(ftmp, tmp);
869 } /* 2^256 - 2^224 + 2^192 */
870
871 felem_mul(tmp, e64, e32);
872 felem_reduce(ftmp2, tmp); /* 2^64 - 2^0 */
873 for (size_t i = 0; i < 16; i++) {
874 felem_square(tmp, ftmp2);
875 felem_reduce(ftmp2, tmp);
876 } /* 2^80 - 2^16 */
877 felem_mul(tmp, ftmp2, e16);
878 felem_reduce(ftmp2, tmp); /* 2^80 - 2^0 */
879 for (size_t i = 0; i < 8; i++) {
880 felem_square(tmp, ftmp2);
881 felem_reduce(ftmp2, tmp);
882 } /* 2^88 - 2^8 */
883 felem_mul(tmp, ftmp2, e8);
884 felem_reduce(ftmp2, tmp); /* 2^88 - 2^0 */
885 for (size_t i = 0; i < 4; i++) {
886 felem_square(tmp, ftmp2);
887 felem_reduce(ftmp2, tmp);
888 } /* 2^92 - 2^4 */
889 felem_mul(tmp, ftmp2, e4);
890 felem_reduce(ftmp2, tmp); /* 2^92 - 2^0 */
891 felem_square(tmp, ftmp2);
892 felem_reduce(ftmp2, tmp); /* 2^93 - 2^1 */
893 felem_square(tmp, ftmp2);
894 felem_reduce(ftmp2, tmp); /* 2^94 - 2^2 */
895 felem_mul(tmp, ftmp2, e2);
896 felem_reduce(ftmp2, tmp); /* 2^94 - 2^0 */
897 felem_square(tmp, ftmp2);
898 felem_reduce(ftmp2, tmp); /* 2^95 - 2^1 */
899 felem_square(tmp, ftmp2);
900 felem_reduce(ftmp2, tmp); /* 2^96 - 2^2 */
901 felem_mul(tmp, ftmp2, in);
902 felem_reduce(ftmp2, tmp); /* 2^96 - 3 */
903
904 felem_mul(tmp, ftmp2, ftmp);
905 felem_reduce(out, tmp); /* 2^256 - 2^224 + 2^192 + 2^96 - 3 */
906 }
907
908 /* Group operations
909 * ----------------
910 *
911 * Building on top of the field operations we have the operations on the
912 * elliptic curve group itself. Points on the curve are represented in Jacobian
913 * coordinates. */
914
915 /* point_double calculates 2*(x_in, y_in, z_in)
916 *
917 * The method is taken from:
918 * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#doubling-dbl-2001-b
919 *
920 * Outputs can equal corresponding inputs, i.e., x_out == x_in is allowed.
921 * while x_out == y_in is not (maybe this works, but it's not tested). */
point_double(felem x_out,felem y_out,felem z_out,const felem x_in,const felem y_in,const felem z_in)922 static void point_double(felem x_out, felem y_out, felem z_out,
923 const felem x_in, const felem y_in, const felem z_in) {
924 longfelem tmp, tmp2;
925 felem delta, gamma, beta, alpha, ftmp, ftmp2;
926 smallfelem small1, small2;
927
928 felem_assign(ftmp, x_in);
929 /* ftmp[i] < 2^106 */
930 felem_assign(ftmp2, x_in);
931 /* ftmp2[i] < 2^106 */
932
933 /* delta = z^2 */
934 felem_square(tmp, z_in);
935 felem_reduce(delta, tmp);
936 /* delta[i] < 2^101 */
937
938 /* gamma = y^2 */
939 felem_square(tmp, y_in);
940 felem_reduce(gamma, tmp);
941 /* gamma[i] < 2^101 */
942 felem_shrink(small1, gamma);
943
944 /* beta = x*gamma */
945 felem_small_mul(tmp, small1, x_in);
946 felem_reduce(beta, tmp);
947 /* beta[i] < 2^101 */
948
949 /* alpha = 3*(x-delta)*(x+delta) */
950 felem_diff(ftmp, delta);
951 /* ftmp[i] < 2^105 + 2^106 < 2^107 */
952 felem_sum(ftmp2, delta);
953 /* ftmp2[i] < 2^105 + 2^106 < 2^107 */
954 felem_scalar(ftmp2, 3);
955 /* ftmp2[i] < 3 * 2^107 < 2^109 */
956 felem_mul(tmp, ftmp, ftmp2);
957 felem_reduce(alpha, tmp);
958 /* alpha[i] < 2^101 */
959 felem_shrink(small2, alpha);
960
961 /* x' = alpha^2 - 8*beta */
962 smallfelem_square(tmp, small2);
963 felem_reduce(x_out, tmp);
964 felem_assign(ftmp, beta);
965 felem_scalar(ftmp, 8);
966 /* ftmp[i] < 8 * 2^101 = 2^104 */
967 felem_diff(x_out, ftmp);
968 /* x_out[i] < 2^105 + 2^101 < 2^106 */
969
970 /* z' = (y + z)^2 - gamma - delta */
971 felem_sum(delta, gamma);
972 /* delta[i] < 2^101 + 2^101 = 2^102 */
973 felem_assign(ftmp, y_in);
974 felem_sum(ftmp, z_in);
975 /* ftmp[i] < 2^106 + 2^106 = 2^107 */
976 felem_square(tmp, ftmp);
977 felem_reduce(z_out, tmp);
978 felem_diff(z_out, delta);
979 /* z_out[i] < 2^105 + 2^101 < 2^106 */
980
981 /* y' = alpha*(4*beta - x') - 8*gamma^2 */
982 felem_scalar(beta, 4);
983 /* beta[i] < 4 * 2^101 = 2^103 */
984 felem_diff_zero107(beta, x_out);
985 /* beta[i] < 2^107 + 2^103 < 2^108 */
986 felem_small_mul(tmp, small2, beta);
987 /* tmp[i] < 7 * 2^64 < 2^67 */
988 smallfelem_square(tmp2, small1);
989 /* tmp2[i] < 7 * 2^64 */
990 longfelem_scalar(tmp2, 8);
991 /* tmp2[i] < 8 * 7 * 2^64 = 7 * 2^67 */
992 longfelem_diff(tmp, tmp2);
993 /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
994 felem_reduce_zero105(y_out, tmp);
995 /* y_out[i] < 2^106 */
996 }
997
998 /* point_double_small is the same as point_double, except that it operates on
999 * smallfelems. */
point_double_small(smallfelem x_out,smallfelem y_out,smallfelem z_out,const smallfelem x_in,const smallfelem y_in,const smallfelem z_in)1000 static void point_double_small(smallfelem x_out, smallfelem y_out,
1001 smallfelem z_out, const smallfelem x_in,
1002 const smallfelem y_in, const smallfelem z_in) {
1003 felem felem_x_out, felem_y_out, felem_z_out;
1004 felem felem_x_in, felem_y_in, felem_z_in;
1005
1006 smallfelem_expand(felem_x_in, x_in);
1007 smallfelem_expand(felem_y_in, y_in);
1008 smallfelem_expand(felem_z_in, z_in);
1009 point_double(felem_x_out, felem_y_out, felem_z_out, felem_x_in, felem_y_in,
1010 felem_z_in);
1011 felem_shrink(x_out, felem_x_out);
1012 felem_shrink(y_out, felem_y_out);
1013 felem_shrink(z_out, felem_z_out);
1014 }
1015
1016 /* p256_copy_conditional copies in to out iff mask is all ones. */
p256_copy_conditional(felem out,const felem in,limb mask)1017 static void p256_copy_conditional(felem out, const felem in, limb mask) {
1018 for (size_t i = 0; i < NLIMBS; ++i) {
1019 const limb tmp = mask & (in[i] ^ out[i]);
1020 out[i] ^= tmp;
1021 }
1022 }
1023
1024 /* copy_small_conditional copies in to out iff mask is all ones. */
copy_small_conditional(felem out,const smallfelem in,limb mask)1025 static void copy_small_conditional(felem out, const smallfelem in, limb mask) {
1026 const uint64_t mask64 = mask;
1027 for (size_t i = 0; i < NLIMBS; ++i) {
1028 out[i] = ((limb)(in[i] & mask64)) | (out[i] & ~mask);
1029 }
1030 }
1031
1032 /* point_add calcuates (x1, y1, z1) + (x2, y2, z2)
1033 *
1034 * The method is taken from:
1035 * http://hyperelliptic.org/EFD/g1p/auto-shortw-jacobian-3.html#addition-add-2007-bl,
1036 * adapted for mixed addition (z2 = 1, or z2 = 0 for the point at infinity).
1037 *
1038 * This function includes a branch for checking whether the two input points
1039 * are equal, (while not equal to the point at infinity). This case never
1040 * happens during single point multiplication, so there is no timing leak for
1041 * ECDH or ECDSA signing. */
point_add(felem x3,felem y3,felem z3,const felem x1,const felem y1,const felem z1,const int mixed,const smallfelem x2,const smallfelem y2,const smallfelem z2)1042 static void point_add(felem x3, felem y3, felem z3, const felem x1,
1043 const felem y1, const felem z1, const int mixed,
1044 const smallfelem x2, const smallfelem y2,
1045 const smallfelem z2) {
1046 felem ftmp, ftmp2, ftmp3, ftmp4, ftmp5, ftmp6, x_out, y_out, z_out;
1047 longfelem tmp, tmp2;
1048 smallfelem small1, small2, small3, small4, small5;
1049 limb x_equal, y_equal, z1_is_zero, z2_is_zero;
1050
1051 felem_shrink(small3, z1);
1052
1053 z1_is_zero = smallfelem_is_zero(small3);
1054 z2_is_zero = smallfelem_is_zero(z2);
1055
1056 /* ftmp = z1z1 = z1**2 */
1057 smallfelem_square(tmp, small3);
1058 felem_reduce(ftmp, tmp);
1059 /* ftmp[i] < 2^101 */
1060 felem_shrink(small1, ftmp);
1061
1062 if (!mixed) {
1063 /* ftmp2 = z2z2 = z2**2 */
1064 smallfelem_square(tmp, z2);
1065 felem_reduce(ftmp2, tmp);
1066 /* ftmp2[i] < 2^101 */
1067 felem_shrink(small2, ftmp2);
1068
1069 felem_shrink(small5, x1);
1070
1071 /* u1 = ftmp3 = x1*z2z2 */
1072 smallfelem_mul(tmp, small5, small2);
1073 felem_reduce(ftmp3, tmp);
1074 /* ftmp3[i] < 2^101 */
1075
1076 /* ftmp5 = z1 + z2 */
1077 felem_assign(ftmp5, z1);
1078 felem_small_sum(ftmp5, z2);
1079 /* ftmp5[i] < 2^107 */
1080
1081 /* ftmp5 = (z1 + z2)**2 - (z1z1 + z2z2) = 2z1z2 */
1082 felem_square(tmp, ftmp5);
1083 felem_reduce(ftmp5, tmp);
1084 /* ftmp2 = z2z2 + z1z1 */
1085 felem_sum(ftmp2, ftmp);
1086 /* ftmp2[i] < 2^101 + 2^101 = 2^102 */
1087 felem_diff(ftmp5, ftmp2);
1088 /* ftmp5[i] < 2^105 + 2^101 < 2^106 */
1089
1090 /* ftmp2 = z2 * z2z2 */
1091 smallfelem_mul(tmp, small2, z2);
1092 felem_reduce(ftmp2, tmp);
1093
1094 /* s1 = ftmp2 = y1 * z2**3 */
1095 felem_mul(tmp, y1, ftmp2);
1096 felem_reduce(ftmp6, tmp);
1097 /* ftmp6[i] < 2^101 */
1098 } else {
1099 /* We'll assume z2 = 1 (special case z2 = 0 is handled later). */
1100
1101 /* u1 = ftmp3 = x1*z2z2 */
1102 felem_assign(ftmp3, x1);
1103 /* ftmp3[i] < 2^106 */
1104
1105 /* ftmp5 = 2z1z2 */
1106 felem_assign(ftmp5, z1);
1107 felem_scalar(ftmp5, 2);
1108 /* ftmp5[i] < 2*2^106 = 2^107 */
1109
1110 /* s1 = ftmp2 = y1 * z2**3 */
1111 felem_assign(ftmp6, y1);
1112 /* ftmp6[i] < 2^106 */
1113 }
1114
1115 /* u2 = x2*z1z1 */
1116 smallfelem_mul(tmp, x2, small1);
1117 felem_reduce(ftmp4, tmp);
1118
1119 /* h = ftmp4 = u2 - u1 */
1120 felem_diff_zero107(ftmp4, ftmp3);
1121 /* ftmp4[i] < 2^107 + 2^101 < 2^108 */
1122 felem_shrink(small4, ftmp4);
1123
1124 x_equal = smallfelem_is_zero(small4);
1125
1126 /* z_out = ftmp5 * h */
1127 felem_small_mul(tmp, small4, ftmp5);
1128 felem_reduce(z_out, tmp);
1129 /* z_out[i] < 2^101 */
1130
1131 /* ftmp = z1 * z1z1 */
1132 smallfelem_mul(tmp, small1, small3);
1133 felem_reduce(ftmp, tmp);
1134
1135 /* s2 = tmp = y2 * z1**3 */
1136 felem_small_mul(tmp, y2, ftmp);
1137 felem_reduce(ftmp5, tmp);
1138
1139 /* r = ftmp5 = (s2 - s1)*2 */
1140 felem_diff_zero107(ftmp5, ftmp6);
1141 /* ftmp5[i] < 2^107 + 2^107 = 2^108 */
1142 felem_scalar(ftmp5, 2);
1143 /* ftmp5[i] < 2^109 */
1144 felem_shrink(small1, ftmp5);
1145 y_equal = smallfelem_is_zero(small1);
1146
1147 if (x_equal && y_equal && !z1_is_zero && !z2_is_zero) {
1148 point_double(x3, y3, z3, x1, y1, z1);
1149 return;
1150 }
1151
1152 /* I = ftmp = (2h)**2 */
1153 felem_assign(ftmp, ftmp4);
1154 felem_scalar(ftmp, 2);
1155 /* ftmp[i] < 2*2^108 = 2^109 */
1156 felem_square(tmp, ftmp);
1157 felem_reduce(ftmp, tmp);
1158
1159 /* J = ftmp2 = h * I */
1160 felem_mul(tmp, ftmp4, ftmp);
1161 felem_reduce(ftmp2, tmp);
1162
1163 /* V = ftmp4 = U1 * I */
1164 felem_mul(tmp, ftmp3, ftmp);
1165 felem_reduce(ftmp4, tmp);
1166
1167 /* x_out = r**2 - J - 2V */
1168 smallfelem_square(tmp, small1);
1169 felem_reduce(x_out, tmp);
1170 felem_assign(ftmp3, ftmp4);
1171 felem_scalar(ftmp4, 2);
1172 felem_sum(ftmp4, ftmp2);
1173 /* ftmp4[i] < 2*2^101 + 2^101 < 2^103 */
1174 felem_diff(x_out, ftmp4);
1175 /* x_out[i] < 2^105 + 2^101 */
1176
1177 /* y_out = r(V-x_out) - 2 * s1 * J */
1178 felem_diff_zero107(ftmp3, x_out);
1179 /* ftmp3[i] < 2^107 + 2^101 < 2^108 */
1180 felem_small_mul(tmp, small1, ftmp3);
1181 felem_mul(tmp2, ftmp6, ftmp2);
1182 longfelem_scalar(tmp2, 2);
1183 /* tmp2[i] < 2*2^67 = 2^68 */
1184 longfelem_diff(tmp, tmp2);
1185 /* tmp[i] < 2^67 + 2^70 + 2^40 < 2^71 */
1186 felem_reduce_zero105(y_out, tmp);
1187 /* y_out[i] < 2^106 */
1188
1189 copy_small_conditional(x_out, x2, z1_is_zero);
1190 p256_copy_conditional(x_out, x1, z2_is_zero);
1191 copy_small_conditional(y_out, y2, z1_is_zero);
1192 p256_copy_conditional(y_out, y1, z2_is_zero);
1193 copy_small_conditional(z_out, z2, z1_is_zero);
1194 p256_copy_conditional(z_out, z1, z2_is_zero);
1195 felem_assign(x3, x_out);
1196 felem_assign(y3, y_out);
1197 felem_assign(z3, z_out);
1198 }
1199
1200 /* point_add_small is the same as point_add, except that it operates on
1201 * smallfelems. */
point_add_small(smallfelem x3,smallfelem y3,smallfelem z3,smallfelem x1,smallfelem y1,smallfelem z1,smallfelem x2,smallfelem y2,smallfelem z2)1202 static void point_add_small(smallfelem x3, smallfelem y3, smallfelem z3,
1203 smallfelem x1, smallfelem y1, smallfelem z1,
1204 smallfelem x2, smallfelem y2, smallfelem z2) {
1205 felem felem_x3, felem_y3, felem_z3;
1206 felem felem_x1, felem_y1, felem_z1;
1207 smallfelem_expand(felem_x1, x1);
1208 smallfelem_expand(felem_y1, y1);
1209 smallfelem_expand(felem_z1, z1);
1210 point_add(felem_x3, felem_y3, felem_z3, felem_x1, felem_y1, felem_z1, 0, x2,
1211 y2, z2);
1212 felem_shrink(x3, felem_x3);
1213 felem_shrink(y3, felem_y3);
1214 felem_shrink(z3, felem_z3);
1215 }
1216
1217 /* Base point pre computation
1218 * --------------------------
1219 *
1220 * Two different sorts of precomputed tables are used in the following code.
1221 * Each contain various points on the curve, where each point is three field
1222 * elements (x, y, z).
1223 *
1224 * For the base point table, z is usually 1 (0 for the point at infinity).
1225 * This table has 2 * 16 elements, starting with the following:
1226 * index | bits | point
1227 * ------+---------+------------------------------
1228 * 0 | 0 0 0 0 | 0G
1229 * 1 | 0 0 0 1 | 1G
1230 * 2 | 0 0 1 0 | 2^64G
1231 * 3 | 0 0 1 1 | (2^64 + 1)G
1232 * 4 | 0 1 0 0 | 2^128G
1233 * 5 | 0 1 0 1 | (2^128 + 1)G
1234 * 6 | 0 1 1 0 | (2^128 + 2^64)G
1235 * 7 | 0 1 1 1 | (2^128 + 2^64 + 1)G
1236 * 8 | 1 0 0 0 | 2^192G
1237 * 9 | 1 0 0 1 | (2^192 + 1)G
1238 * 10 | 1 0 1 0 | (2^192 + 2^64)G
1239 * 11 | 1 0 1 1 | (2^192 + 2^64 + 1)G
1240 * 12 | 1 1 0 0 | (2^192 + 2^128)G
1241 * 13 | 1 1 0 1 | (2^192 + 2^128 + 1)G
1242 * 14 | 1 1 1 0 | (2^192 + 2^128 + 2^64)G
1243 * 15 | 1 1 1 1 | (2^192 + 2^128 + 2^64 + 1)G
1244 * followed by a copy of this with each element multiplied by 2^32.
1245 *
1246 * The reason for this is so that we can clock bits into four different
1247 * locations when doing simple scalar multiplies against the base point,
1248 * and then another four locations using the second 16 elements.
1249 *
1250 * Tables for other points have table[i] = iG for i in 0 .. 16. */
1251
1252 /* g_pre_comp is the table of precomputed base points */
1253 static const smallfelem g_pre_comp[2][16][3] = {
1254 {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1255 {{0xf4a13945d898c296, 0x77037d812deb33a0, 0xf8bce6e563a440f2,
1256 0x6b17d1f2e12c4247},
1257 {0xcbb6406837bf51f5, 0x2bce33576b315ece, 0x8ee7eb4a7c0f9e16,
1258 0x4fe342e2fe1a7f9b},
1259 {1, 0, 0, 0}},
1260 {{0x90e75cb48e14db63, 0x29493baaad651f7e, 0x8492592e326e25de,
1261 0x0fa822bc2811aaa5},
1262 {0xe41124545f462ee7, 0x34b1a65050fe82f5, 0x6f4ad4bcb3df188b,
1263 0xbff44ae8f5dba80d},
1264 {1, 0, 0, 0}},
1265 {{0x93391ce2097992af, 0xe96c98fd0d35f1fa, 0xb257c0de95e02789,
1266 0x300a4bbc89d6726f},
1267 {0xaa54a291c08127a0, 0x5bb1eeada9d806a5, 0x7f1ddb25ff1e3c6f,
1268 0x72aac7e0d09b4644},
1269 {1, 0, 0, 0}},
1270 {{0x57c84fc9d789bd85, 0xfc35ff7dc297eac3, 0xfb982fd588c6766e,
1271 0x447d739beedb5e67},
1272 {0x0c7e33c972e25b32, 0x3d349b95a7fae500, 0xe12e9d953a4aaff7,
1273 0x2d4825ab834131ee},
1274 {1, 0, 0, 0}},
1275 {{0x13949c932a1d367f, 0xef7fbd2b1a0a11b7, 0xddc6068bb91dfc60,
1276 0xef9519328a9c72ff},
1277 {0x196035a77376d8a8, 0x23183b0895ca1740, 0xc1ee9807022c219c,
1278 0x611e9fc37dbb2c9b},
1279 {1, 0, 0, 0}},
1280 {{0xcae2b1920b57f4bc, 0x2936df5ec6c9bc36, 0x7dea6482e11238bf,
1281 0x550663797b51f5d8},
1282 {0x44ffe216348a964c, 0x9fb3d576dbdefbe1, 0x0afa40018d9d50e5,
1283 0x157164848aecb851},
1284 {1, 0, 0, 0}},
1285 {{0xe48ecafffc5cde01, 0x7ccd84e70d715f26, 0xa2e8f483f43e4391,
1286 0xeb5d7745b21141ea},
1287 {0xcac917e2731a3479, 0x85f22cfe2844b645, 0x0990e6a158006cee,
1288 0xeafd72ebdbecc17b},
1289 {1, 0, 0, 0}},
1290 {{0x6cf20ffb313728be, 0x96439591a3c6b94a, 0x2736ff8344315fc5,
1291 0xa6d39677a7849276},
1292 {0xf2bab833c357f5f4, 0x824a920c2284059b, 0x66b8babd2d27ecdf,
1293 0x674f84749b0b8816},
1294 {1, 0, 0, 0}},
1295 {{0x2df48c04677c8a3e, 0x74e02f080203a56b, 0x31855f7db8c7fedb,
1296 0x4e769e7672c9ddad},
1297 {0xa4c36165b824bbb0, 0xfb9ae16f3b9122a5, 0x1ec0057206947281,
1298 0x42b99082de830663},
1299 {1, 0, 0, 0}},
1300 {{0x6ef95150dda868b9, 0xd1f89e799c0ce131, 0x7fdc1ca008a1c478,
1301 0x78878ef61c6ce04d},
1302 {0x9c62b9121fe0d976, 0x6ace570ebde08d4f, 0xde53142c12309def,
1303 0xb6cb3f5d7b72c321},
1304 {1, 0, 0, 0}},
1305 {{0x7f991ed2c31a3573, 0x5b82dd5bd54fb496, 0x595c5220812ffcae,
1306 0x0c88bc4d716b1287},
1307 {0x3a57bf635f48aca8, 0x7c8181f4df2564f3, 0x18d1b5b39c04e6aa,
1308 0xdd5ddea3f3901dc6},
1309 {1, 0, 0, 0}},
1310 {{0xe96a79fb3e72ad0c, 0x43a0a28c42ba792f, 0xefe0a423083e49f3,
1311 0x68f344af6b317466},
1312 {0xcdfe17db3fb24d4a, 0x668bfc2271f5c626, 0x604ed93c24d67ff3,
1313 0x31b9c405f8540a20},
1314 {1, 0, 0, 0}},
1315 {{0xd36b4789a2582e7f, 0x0d1a10144ec39c28, 0x663c62c3edbad7a0,
1316 0x4052bf4b6f461db9},
1317 {0x235a27c3188d25eb, 0xe724f33999bfcc5b, 0x862be6bd71d70cc8,
1318 0xfecf4d5190b0fc61},
1319 {1, 0, 0, 0}},
1320 {{0x74346c10a1d4cfac, 0xafdf5cc08526a7a4, 0x123202a8f62bff7a,
1321 0x1eddbae2c802e41a},
1322 {0x8fa0af2dd603f844, 0x36e06b7e4c701917, 0x0c45f45273db33a0,
1323 0x43104d86560ebcfc},
1324 {1, 0, 0, 0}},
1325 {{0x9615b5110d1d78e5, 0x66b0de3225c4744b, 0x0a4a46fb6aaf363a,
1326 0xb48e26b484f7a21c},
1327 {0x06ebb0f621a01b2d, 0xc004e4048b7b0f98, 0x64131bcdfed6f668,
1328 0xfac015404d4d3dab},
1329 {1, 0, 0, 0}}},
1330 {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},
1331 {{0x3a5a9e22185a5943, 0x1ab919365c65dfb6, 0x21656b32262c71da,
1332 0x7fe36b40af22af89},
1333 {0xd50d152c699ca101, 0x74b3d5867b8af212, 0x9f09f40407dca6f1,
1334 0xe697d45825b63624},
1335 {1, 0, 0, 0}},
1336 {{0xa84aa9397512218e, 0xe9a521b074ca0141, 0x57880b3a18a2e902,
1337 0x4a5b506612a677a6},
1338 {0x0beada7a4c4f3840, 0x626db15419e26d9d, 0xc42604fbe1627d40,
1339 0xeb13461ceac089f1},
1340 {1, 0, 0, 0}},
1341 {{0xf9faed0927a43281, 0x5e52c4144103ecbc, 0xc342967aa815c857,
1342 0x0781b8291c6a220a},
1343 {0x5a8343ceeac55f80, 0x88f80eeee54a05e3, 0x97b2a14f12916434,
1344 0x690cde8df0151593},
1345 {1, 0, 0, 0}},
1346 {{0xaee9c75df7f82f2a, 0x9e4c35874afdf43a, 0xf5622df437371326,
1347 0x8a535f566ec73617},
1348 {0xc5f9a0ac223094b7, 0xcde533864c8c7669, 0x37e02819085a92bf,
1349 0x0455c08468b08bd7},
1350 {1, 0, 0, 0}},
1351 {{0x0c0a6e2c9477b5d9, 0xf9a4bf62876dc444, 0x5050a949b6cdc279,
1352 0x06bada7ab77f8276},
1353 {0xc8b4aed1ea48dac9, 0xdebd8a4b7ea1070f, 0x427d49101366eb70,
1354 0x5b476dfd0e6cb18a},
1355 {1, 0, 0, 0}},
1356 {{0x7c5c3e44278c340a, 0x4d54606812d66f3b, 0x29a751b1ae23c5d8,
1357 0x3e29864e8a2ec908},
1358 {0x142d2a6626dbb850, 0xad1744c4765bd780, 0x1f150e68e322d1ed,
1359 0x239b90ea3dc31e7e},
1360 {1, 0, 0, 0}},
1361 {{0x78c416527a53322a, 0x305dde6709776f8e, 0xdbcab759f8862ed4,
1362 0x820f4dd949f72ff7},
1363 {0x6cc544a62b5debd4, 0x75be5d937b4e8cc4, 0x1b481b1b215c14d3,
1364 0x140406ec783a05ec},
1365 {1, 0, 0, 0}},
1366 {{0x6a703f10e895df07, 0xfd75f3fa01876bd8, 0xeb5b06e70ce08ffe,
1367 0x68f6b8542783dfee},
1368 {0x90c76f8a78712655, 0xcf5293d2f310bf7f, 0xfbc8044dfda45028,
1369 0xcbe1feba92e40ce6},
1370 {1, 0, 0, 0}},
1371 {{0xe998ceea4396e4c1, 0xfc82ef0b6acea274, 0x230f729f2250e927,
1372 0xd0b2f94d2f420109},
1373 {0x4305adddb38d4966, 0x10b838f8624c3b45, 0x7db2636658954e7a,
1374 0x971459828b0719e5},
1375 {1, 0, 0, 0}},
1376 {{0x4bd6b72623369fc9, 0x57f2929e53d0b876, 0xc2d5cba4f2340687,
1377 0x961610004a866aba},
1378 {0x49997bcd2e407a5e, 0x69ab197d92ddcb24, 0x2cf1f2438fe5131c,
1379 0x7acb9fadcee75e44},
1380 {1, 0, 0, 0}},
1381 {{0x254e839423d2d4c0, 0xf57f0c917aea685b, 0xa60d880f6f75aaea,
1382 0x24eb9acca333bf5b},
1383 {0xe3de4ccb1cda5dea, 0xfeef9341c51a6b4f, 0x743125f88bac4c4d,
1384 0x69f891c5acd079cc},
1385 {1, 0, 0, 0}},
1386 {{0xeee44b35702476b5, 0x7ed031a0e45c2258, 0xb422d1e7bd6f8514,
1387 0xe51f547c5972a107},
1388 {0xa25bcd6fc9cf343d, 0x8ca922ee097c184e, 0xa62f98b3a9fe9a06,
1389 0x1c309a2b25bb1387},
1390 {1, 0, 0, 0}},
1391 {{0x9295dbeb1967c459, 0xb00148833472c98e, 0xc504977708011828,
1392 0x20b87b8aa2c4e503},
1393 {0x3063175de057c277, 0x1bd539338fe582dd, 0x0d11adef5f69a044,
1394 0xf5c6fa49919776be},
1395 {1, 0, 0, 0}},
1396 {{0x8c944e760fd59e11, 0x3876cba1102fad5f, 0xa454c3fad83faa56,
1397 0x1ed7d1b9332010b9},
1398 {0xa1011a270024b889, 0x05e4d0dcac0cd344, 0x52b520f0eb6a2a24,
1399 0x3a2b03f03217257a},
1400 {1, 0, 0, 0}},
1401 {{0xf20fc2afdf1d043d, 0xf330240db58d5a62, 0xfc7d229ca0058c3b,
1402 0x15fee545c78dd9f6},
1403 {0x501e82885bc98cda, 0x41ef80e5d046ac04, 0x557d9f49461210fb,
1404 0x4ab5b6b2b8753f81},
1405 {1, 0, 0, 0}}}};
1406
1407 /* select_point selects the |idx|th point from a precomputation table and
1408 * copies it to out. */
select_point(const uint64_t idx,size_t size,const smallfelem pre_comp[][3],smallfelem out[3])1409 static void select_point(const uint64_t idx, size_t size,
1410 const smallfelem pre_comp[/*size*/][3],
1411 smallfelem out[3]) {
1412 uint64_t *outlimbs = &out[0][0];
1413 OPENSSL_memset(outlimbs, 0, 3 * sizeof(smallfelem));
1414
1415 for (size_t i = 0; i < size; i++) {
1416 const uint64_t *inlimbs = (const uint64_t *)&pre_comp[i][0][0];
1417 uint64_t mask = i ^ idx;
1418 mask |= mask >> 4;
1419 mask |= mask >> 2;
1420 mask |= mask >> 1;
1421 mask &= 1;
1422 mask--;
1423 for (size_t j = 0; j < NLIMBS * 3; j++) {
1424 outlimbs[j] |= inlimbs[j] & mask;
1425 }
1426 }
1427 }
1428
1429 /* get_bit returns the |i|th bit in |in| */
get_bit(const felem_bytearray in,int i)1430 static char get_bit(const felem_bytearray in, int i) {
1431 if (i < 0 || i >= 256) {
1432 return 0;
1433 }
1434 return (in[i >> 3] >> (i & 7)) & 1;
1435 }
1436
1437 /* Interleaved point multiplication using precomputed point multiples: The
1438 * small point multiples 0*P, 1*P, ..., 17*P are in p_pre_comp, the scalar
1439 * in p_scalar, if non-NULL. If g_scalar is non-NULL, we also add this multiple
1440 * of the generator, using certain (large) precomputed multiples in g_pre_comp.
1441 * Output point (X, Y, Z) is stored in x_out, y_out, z_out. */
batch_mul(felem x_out,felem y_out,felem z_out,const uint8_t * p_scalar,const uint8_t * g_scalar,const smallfelem p_pre_comp[17][3])1442 static void batch_mul(felem x_out, felem y_out, felem z_out,
1443 const uint8_t *p_scalar, const uint8_t *g_scalar,
1444 const smallfelem p_pre_comp[17][3]) {
1445 felem nq[3], ftmp;
1446 smallfelem tmp[3];
1447 uint64_t bits;
1448 uint8_t sign, digit;
1449
1450 /* set nq to the point at infinity */
1451 OPENSSL_memset(nq, 0, 3 * sizeof(felem));
1452
1453 /* Loop over both scalars msb-to-lsb, interleaving additions of multiples
1454 * of the generator (two in each of the last 32 rounds) and additions of p
1455 * (every 5th round). */
1456
1457 int skip = 1; /* save two point operations in the first round */
1458 size_t i = p_scalar != NULL ? 255 : 31;
1459 for (;;) {
1460 /* double */
1461 if (!skip) {
1462 point_double(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2]);
1463 }
1464
1465 /* add multiples of the generator */
1466 if (g_scalar != NULL && i <= 31) {
1467 /* first, look 32 bits upwards */
1468 bits = get_bit(g_scalar, i + 224) << 3;
1469 bits |= get_bit(g_scalar, i + 160) << 2;
1470 bits |= get_bit(g_scalar, i + 96) << 1;
1471 bits |= get_bit(g_scalar, i + 32);
1472 /* select the point to add, in constant time */
1473 select_point(bits, 16, g_pre_comp[1], tmp);
1474
1475 if (!skip) {
1476 point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */,
1477 tmp[0], tmp[1], tmp[2]);
1478 } else {
1479 smallfelem_expand(nq[0], tmp[0]);
1480 smallfelem_expand(nq[1], tmp[1]);
1481 smallfelem_expand(nq[2], tmp[2]);
1482 skip = 0;
1483 }
1484
1485 /* second, look at the current position */
1486 bits = get_bit(g_scalar, i + 192) << 3;
1487 bits |= get_bit(g_scalar, i + 128) << 2;
1488 bits |= get_bit(g_scalar, i + 64) << 1;
1489 bits |= get_bit(g_scalar, i);
1490 /* select the point to add, in constant time */
1491 select_point(bits, 16, g_pre_comp[0], tmp);
1492 point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 1 /* mixed */, tmp[0],
1493 tmp[1], tmp[2]);
1494 }
1495
1496 /* do other additions every 5 doublings */
1497 if (p_scalar != NULL && i % 5 == 0) {
1498 bits = get_bit(p_scalar, i + 4) << 5;
1499 bits |= get_bit(p_scalar, i + 3) << 4;
1500 bits |= get_bit(p_scalar, i + 2) << 3;
1501 bits |= get_bit(p_scalar, i + 1) << 2;
1502 bits |= get_bit(p_scalar, i) << 1;
1503 bits |= get_bit(p_scalar, i - 1);
1504 ec_GFp_nistp_recode_scalar_bits(&sign, &digit, bits);
1505
1506 /* select the point to add or subtract, in constant time. */
1507 select_point(digit, 17, p_pre_comp, tmp);
1508 smallfelem_neg(ftmp, tmp[1]); /* (X, -Y, Z) is the negative
1509 * point */
1510 copy_small_conditional(ftmp, tmp[1], (((limb)sign) - 1));
1511 felem_contract(tmp[1], ftmp);
1512
1513 if (!skip) {
1514 point_add(nq[0], nq[1], nq[2], nq[0], nq[1], nq[2], 0 /* mixed */,
1515 tmp[0], tmp[1], tmp[2]);
1516 } else {
1517 smallfelem_expand(nq[0], tmp[0]);
1518 smallfelem_expand(nq[1], tmp[1]);
1519 smallfelem_expand(nq[2], tmp[2]);
1520 skip = 0;
1521 }
1522 }
1523
1524 if (i == 0) {
1525 break;
1526 }
1527 --i;
1528 }
1529 felem_assign(x_out, nq[0]);
1530 felem_assign(y_out, nq[1]);
1531 felem_assign(z_out, nq[2]);
1532 }
1533
1534 /******************************************************************************/
1535 /*
1536 * OPENSSL EC_METHOD FUNCTIONS
1537 */
1538
1539 /* Takes the Jacobian coordinates (X, Y, Z) of a point and returns (X', Y') =
1540 * (X/Z^2, Y/Z^3). */
ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP * group,const EC_POINT * point,BIGNUM * x,BIGNUM * y,BN_CTX * ctx)1541 static int ec_GFp_nistp256_point_get_affine_coordinates(const EC_GROUP *group,
1542 const EC_POINT *point,
1543 BIGNUM *x, BIGNUM *y,
1544 BN_CTX *ctx) {
1545 felem z1, z2, x_in, y_in;
1546 smallfelem x_out, y_out;
1547 longfelem tmp;
1548
1549 if (EC_POINT_is_at_infinity(group, point)) {
1550 OPENSSL_PUT_ERROR(EC, EC_R_POINT_AT_INFINITY);
1551 return 0;
1552 }
1553 if (!BN_to_felem(x_in, &point->X) ||
1554 !BN_to_felem(y_in, &point->Y) ||
1555 !BN_to_felem(z1, &point->Z)) {
1556 return 0;
1557 }
1558 felem_inv(z2, z1);
1559 felem_square(tmp, z2);
1560 felem_reduce(z1, tmp);
1561
1562 if (x != NULL) {
1563 felem_mul(tmp, x_in, z1);
1564 felem_reduce(x_in, tmp);
1565 felem_contract(x_out, x_in);
1566 if (!smallfelem_to_BN(x, x_out)) {
1567 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1568 return 0;
1569 }
1570 }
1571
1572 if (y != NULL) {
1573 felem_mul(tmp, z1, z2);
1574 felem_reduce(z1, tmp);
1575 felem_mul(tmp, y_in, z1);
1576 felem_reduce(y_in, tmp);
1577 felem_contract(y_out, y_in);
1578 if (!smallfelem_to_BN(y, y_out)) {
1579 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1580 return 0;
1581 }
1582 }
1583
1584 return 1;
1585 }
1586
ec_GFp_nistp256_points_mul(const EC_GROUP * group,EC_POINT * r,const BIGNUM * g_scalar,const EC_POINT * p,const BIGNUM * p_scalar,BN_CTX * ctx)1587 static int ec_GFp_nistp256_points_mul(const EC_GROUP *group, EC_POINT *r,
1588 const BIGNUM *g_scalar, const EC_POINT *p,
1589 const BIGNUM *p_scalar, BN_CTX *ctx) {
1590 int ret = 0;
1591 BN_CTX *new_ctx = NULL;
1592 BIGNUM *x, *y, *z, *tmp_scalar;
1593 felem_bytearray g_secret, p_secret;
1594 smallfelem p_pre_comp[17][3];
1595 felem_bytearray tmp;
1596 smallfelem x_in, y_in, z_in;
1597 felem x_out, y_out, z_out;
1598
1599 if (ctx == NULL) {
1600 ctx = new_ctx = BN_CTX_new();
1601 if (ctx == NULL) {
1602 return 0;
1603 }
1604 }
1605
1606 BN_CTX_start(ctx);
1607 if ((x = BN_CTX_get(ctx)) == NULL ||
1608 (y = BN_CTX_get(ctx)) == NULL ||
1609 (z = BN_CTX_get(ctx)) == NULL ||
1610 (tmp_scalar = BN_CTX_get(ctx)) == NULL) {
1611 goto err;
1612 }
1613
1614 if (p != NULL && p_scalar != NULL) {
1615 /* We treat NULL scalars as 0, and NULL points as points at infinity, i.e.,
1616 * they contribute nothing to the linear combination. */
1617 OPENSSL_memset(&p_secret, 0, sizeof(p_secret));
1618 OPENSSL_memset(&p_pre_comp, 0, sizeof(p_pre_comp));
1619 size_t num_bytes;
1620 /* Reduce g_scalar to 0 <= g_scalar < 2^256. */
1621 if (BN_num_bits(p_scalar) > 256 || BN_is_negative(p_scalar)) {
1622 /* This is an unusual input, and we don't guarantee constant-timeness. */
1623 if (!BN_nnmod(tmp_scalar, p_scalar, &group->order, ctx)) {
1624 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1625 goto err;
1626 }
1627 num_bytes = BN_bn2bin(tmp_scalar, tmp);
1628 } else {
1629 num_bytes = BN_bn2bin(p_scalar, tmp);
1630 }
1631 flip_endian(p_secret, tmp, num_bytes);
1632 /* Precompute multiples. */
1633 if (!BN_to_felem(x_out, &p->X) ||
1634 !BN_to_felem(y_out, &p->Y) ||
1635 !BN_to_felem(z_out, &p->Z)) {
1636 goto err;
1637 }
1638 felem_shrink(p_pre_comp[1][0], x_out);
1639 felem_shrink(p_pre_comp[1][1], y_out);
1640 felem_shrink(p_pre_comp[1][2], z_out);
1641 for (size_t j = 2; j <= 16; ++j) {
1642 if (j & 1) {
1643 point_add_small(p_pre_comp[j][0], p_pre_comp[j][1],
1644 p_pre_comp[j][2], p_pre_comp[1][0],
1645 p_pre_comp[1][1], p_pre_comp[1][2],
1646 p_pre_comp[j - 1][0], p_pre_comp[j - 1][1],
1647 p_pre_comp[j - 1][2]);
1648 } else {
1649 point_double_small(p_pre_comp[j][0], p_pre_comp[j][1],
1650 p_pre_comp[j][2], p_pre_comp[j / 2][0],
1651 p_pre_comp[j / 2][1], p_pre_comp[j / 2][2]);
1652 }
1653 }
1654 }
1655
1656 if (g_scalar != NULL) {
1657 size_t num_bytes;
1658
1659 OPENSSL_memset(g_secret, 0, sizeof(g_secret));
1660 /* reduce g_scalar to 0 <= g_scalar < 2^256 */
1661 if (BN_num_bits(g_scalar) > 256 || BN_is_negative(g_scalar)) {
1662 /* this is an unusual input, and we don't guarantee
1663 * constant-timeness. */
1664 if (!BN_nnmod(tmp_scalar, g_scalar, &group->order, ctx)) {
1665 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1666 goto err;
1667 }
1668 num_bytes = BN_bn2bin(tmp_scalar, tmp);
1669 } else {
1670 num_bytes = BN_bn2bin(g_scalar, tmp);
1671 }
1672 flip_endian(g_secret, tmp, num_bytes);
1673 }
1674 batch_mul(x_out, y_out, z_out,
1675 (p != NULL && p_scalar != NULL) ? p_secret : NULL,
1676 g_scalar != NULL ? g_secret : NULL,
1677 (const smallfelem(*)[3]) &p_pre_comp);
1678
1679 /* reduce the output to its unique minimal representation */
1680 felem_contract(x_in, x_out);
1681 felem_contract(y_in, y_out);
1682 felem_contract(z_in, z_out);
1683 if (!smallfelem_to_BN(x, x_in) ||
1684 !smallfelem_to_BN(y, y_in) ||
1685 !smallfelem_to_BN(z, z_in)) {
1686 OPENSSL_PUT_ERROR(EC, ERR_R_BN_LIB);
1687 goto err;
1688 }
1689 ret = ec_point_set_Jprojective_coordinates_GFp(group, r, x, y, z, ctx);
1690
1691 err:
1692 BN_CTX_end(ctx);
1693 BN_CTX_free(new_ctx);
1694 return ret;
1695 }
1696
DEFINE_METHOD_FUNCTION(EC_METHOD,EC_GFp_nistp256_method)1697 DEFINE_METHOD_FUNCTION(EC_METHOD, EC_GFp_nistp256_method) {
1698 out->group_init = ec_GFp_simple_group_init;
1699 out->group_finish = ec_GFp_simple_group_finish;
1700 out->group_copy = ec_GFp_simple_group_copy;
1701 out->group_set_curve = ec_GFp_simple_group_set_curve;
1702 out->point_get_affine_coordinates =
1703 ec_GFp_nistp256_point_get_affine_coordinates;
1704 out->mul = ec_GFp_nistp256_points_mul;
1705 out->field_mul = ec_GFp_simple_field_mul;
1706 out->field_sqr = ec_GFp_simple_field_sqr;
1707 out->field_encode = NULL;
1708 out->field_decode = NULL;
1709 };
1710
1711 #endif /* 64_BIT && !WINDOWS */
1712