Debian Codegraph @

external/boringssl/crypto/ec/p256-64.c

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