crypto: hisilicon/hpre - add 'ECDH' algorithm
[linux-block.git] / crypto / ecc.c
CommitLineData
3c4b2390 1/*
0d7a7864
VC
2 * Copyright (c) 2013, 2014 Kenneth MacKay. All rights reserved.
3 * Copyright (c) 2019 Vitaly Chikunov <vt@altlinux.org>
3c4b2390
SB
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
16 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
18 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
20 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
14bb7676 27#include <crypto/ecc_curve.h>
4a2289da 28#include <linux/module.h>
3c4b2390
SB
29#include <linux/random.h>
30#include <linux/slab.h>
31#include <linux/swab.h>
32#include <linux/fips.h>
33#include <crypto/ecdh.h>
6755fd26 34#include <crypto/rng.h>
0d7a7864
VC
35#include <asm/unaligned.h>
36#include <linux/ratelimit.h>
3c4b2390
SB
37
38#include "ecc.h"
39#include "ecc_curve_defs.h"
40
41typedef struct {
42 u64 m_low;
43 u64 m_high;
44} uint128_t;
45
14bb7676
MY
46
47const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
3c4b2390
SB
48{
49 switch (curve_id) {
50 /* In FIPS mode only allow P256 and higher */
51 case ECC_CURVE_NIST_P192:
52 return fips_enabled ? NULL : &nist_p192;
53 case ECC_CURVE_NIST_P256:
54 return &nist_p256;
55 default:
56 return NULL;
57 }
58}
14bb7676 59EXPORT_SYMBOL(ecc_get_curve);
3c4b2390
SB
60
61static u64 *ecc_alloc_digits_space(unsigned int ndigits)
62{
63 size_t len = ndigits * sizeof(u64);
64
65 if (!len)
66 return NULL;
67
68 return kmalloc(len, GFP_KERNEL);
69}
70
71static void ecc_free_digits_space(u64 *space)
72{
453431a5 73 kfree_sensitive(space);
3c4b2390
SB
74}
75
76static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
77{
78 struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
79
80 if (!p)
81 return NULL;
82
83 p->x = ecc_alloc_digits_space(ndigits);
84 if (!p->x)
85 goto err_alloc_x;
86
87 p->y = ecc_alloc_digits_space(ndigits);
88 if (!p->y)
89 goto err_alloc_y;
90
91 p->ndigits = ndigits;
92
93 return p;
94
95err_alloc_y:
96 ecc_free_digits_space(p->x);
97err_alloc_x:
98 kfree(p);
99 return NULL;
100}
101
102static void ecc_free_point(struct ecc_point *p)
103{
104 if (!p)
105 return;
106
453431a5
WL
107 kfree_sensitive(p->x);
108 kfree_sensitive(p->y);
109 kfree_sensitive(p);
3c4b2390
SB
110}
111
112static void vli_clear(u64 *vli, unsigned int ndigits)
113{
114 int i;
115
116 for (i = 0; i < ndigits; i++)
117 vli[i] = 0;
118}
119
120/* Returns true if vli == 0, false otherwise. */
4a2289da 121bool vli_is_zero(const u64 *vli, unsigned int ndigits)
3c4b2390
SB
122{
123 int i;
124
125 for (i = 0; i < ndigits; i++) {
126 if (vli[i])
127 return false;
128 }
129
130 return true;
131}
4a2289da 132EXPORT_SYMBOL(vli_is_zero);
3c4b2390
SB
133
134/* Returns nonzero if bit bit of vli is set. */
135static u64 vli_test_bit(const u64 *vli, unsigned int bit)
136{
137 return (vli[bit / 64] & ((u64)1 << (bit % 64)));
138}
139
0d7a7864
VC
140static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
141{
142 return vli_test_bit(vli, ndigits * 64 - 1);
143}
144
3c4b2390
SB
145/* Counts the number of 64-bit "digits" in vli. */
146static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
147{
148 int i;
149
150 /* Search from the end until we find a non-zero digit.
151 * We do it in reverse because we expect that most digits will
152 * be nonzero.
153 */
154 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
155
156 return (i + 1);
157}
158
159/* Counts the number of bits required for vli. */
160static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
161{
162 unsigned int i, num_digits;
163 u64 digit;
164
165 num_digits = vli_num_digits(vli, ndigits);
166 if (num_digits == 0)
167 return 0;
168
169 digit = vli[num_digits - 1];
170 for (i = 0; digit; i++)
171 digit >>= 1;
172
173 return ((num_digits - 1) * 64 + i);
174}
175
0d7a7864
VC
176/* Set dest from unaligned bit string src. */
177void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
178{
179 int i;
180 const u64 *from = src;
181
182 for (i = 0; i < ndigits; i++)
183 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
184}
185EXPORT_SYMBOL(vli_from_be64);
186
187void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
188{
189 int i;
190 const u64 *from = src;
191
192 for (i = 0; i < ndigits; i++)
193 dest[i] = get_unaligned_le64(&from[i]);
194}
195EXPORT_SYMBOL(vli_from_le64);
196
3c4b2390
SB
197/* Sets dest = src. */
198static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
199{
200 int i;
201
202 for (i = 0; i < ndigits; i++)
203 dest[i] = src[i];
204}
205
206/* Returns sign of left - right. */
4a2289da 207int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
3c4b2390
SB
208{
209 int i;
210
211 for (i = ndigits - 1; i >= 0; i--) {
212 if (left[i] > right[i])
213 return 1;
214 else if (left[i] < right[i])
215 return -1;
216 }
217
218 return 0;
219}
4a2289da 220EXPORT_SYMBOL(vli_cmp);
3c4b2390
SB
221
222/* Computes result = in << c, returning carry. Can modify in place
223 * (if result == in). 0 < shift < 64.
224 */
225static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
226 unsigned int ndigits)
227{
228 u64 carry = 0;
229 int i;
230
231 for (i = 0; i < ndigits; i++) {
232 u64 temp = in[i];
233
234 result[i] = (temp << shift) | carry;
235 carry = temp >> (64 - shift);
236 }
237
238 return carry;
239}
240
241/* Computes vli = vli >> 1. */
242static void vli_rshift1(u64 *vli, unsigned int ndigits)
243{
244 u64 *end = vli;
245 u64 carry = 0;
246
247 vli += ndigits;
248
249 while (vli-- > end) {
250 u64 temp = *vli;
251 *vli = (temp >> 1) | carry;
252 carry = temp << 63;
253 }
254}
255
256/* Computes result = left + right, returning carry. Can modify in place. */
257static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
258 unsigned int ndigits)
259{
260 u64 carry = 0;
261 int i;
262
263 for (i = 0; i < ndigits; i++) {
264 u64 sum;
265
266 sum = left[i] + right[i] + carry;
267 if (sum != left[i])
268 carry = (sum < left[i]);
269
270 result[i] = sum;
271 }
272
273 return carry;
274}
275
0d7a7864
VC
276/* Computes result = left + right, returning carry. Can modify in place. */
277static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
278 unsigned int ndigits)
279{
280 u64 carry = right;
281 int i;
282
283 for (i = 0; i < ndigits; i++) {
284 u64 sum;
285
286 sum = left[i] + carry;
287 if (sum != left[i])
288 carry = (sum < left[i]);
289 else
290 carry = !!carry;
291
292 result[i] = sum;
293 }
294
295 return carry;
296}
297
3c4b2390 298/* Computes result = left - right, returning borrow. Can modify in place. */
4a2289da 299u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
3c4b2390
SB
300 unsigned int ndigits)
301{
302 u64 borrow = 0;
303 int i;
304
305 for (i = 0; i < ndigits; i++) {
306 u64 diff;
307
308 diff = left[i] - right[i] - borrow;
309 if (diff != left[i])
310 borrow = (diff > left[i]);
311
312 result[i] = diff;
313 }
314
315 return borrow;
316}
4a2289da 317EXPORT_SYMBOL(vli_sub);
3c4b2390 318
0d7a7864
VC
319/* Computes result = left - right, returning borrow. Can modify in place. */
320static u64 vli_usub(u64 *result, const u64 *left, u64 right,
321 unsigned int ndigits)
322{
323 u64 borrow = right;
324 int i;
325
326 for (i = 0; i < ndigits; i++) {
327 u64 diff;
328
329 diff = left[i] - borrow;
330 if (diff != left[i])
331 borrow = (diff > left[i]);
332
333 result[i] = diff;
334 }
335
336 return borrow;
337}
338
3c4b2390
SB
339static uint128_t mul_64_64(u64 left, u64 right)
340{
0d7a7864 341 uint128_t result;
c12d3362 342#if defined(CONFIG_ARCH_SUPPORTS_INT128)
0d7a7864
VC
343 unsigned __int128 m = (unsigned __int128)left * right;
344
345 result.m_low = m;
346 result.m_high = m >> 64;
347#else
3c4b2390
SB
348 u64 a0 = left & 0xffffffffull;
349 u64 a1 = left >> 32;
350 u64 b0 = right & 0xffffffffull;
351 u64 b1 = right >> 32;
352 u64 m0 = a0 * b0;
353 u64 m1 = a0 * b1;
354 u64 m2 = a1 * b0;
355 u64 m3 = a1 * b1;
3c4b2390
SB
356
357 m2 += (m0 >> 32);
358 m2 += m1;
359
360 /* Overflow */
361 if (m2 < m1)
362 m3 += 0x100000000ull;
363
364 result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
365 result.m_high = m3 + (m2 >> 32);
0d7a7864 366#endif
3c4b2390
SB
367 return result;
368}
369
370static uint128_t add_128_128(uint128_t a, uint128_t b)
371{
372 uint128_t result;
373
374 result.m_low = a.m_low + b.m_low;
375 result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
376
377 return result;
378}
379
380static void vli_mult(u64 *result, const u64 *left, const u64 *right,
381 unsigned int ndigits)
382{
383 uint128_t r01 = { 0, 0 };
384 u64 r2 = 0;
385 unsigned int i, k;
386
387 /* Compute each digit of result in sequence, maintaining the
388 * carries.
389 */
390 for (k = 0; k < ndigits * 2 - 1; k++) {
391 unsigned int min;
392
393 if (k < ndigits)
394 min = 0;
395 else
396 min = (k + 1) - ndigits;
397
398 for (i = min; i <= k && i < ndigits; i++) {
399 uint128_t product;
400
401 product = mul_64_64(left[i], right[k - i]);
402
403 r01 = add_128_128(r01, product);
404 r2 += (r01.m_high < product.m_high);
405 }
406
407 result[k] = r01.m_low;
408 r01.m_low = r01.m_high;
409 r01.m_high = r2;
410 r2 = 0;
411 }
412
413 result[ndigits * 2 - 1] = r01.m_low;
414}
415
0d7a7864
VC
416/* Compute product = left * right, for a small right value. */
417static void vli_umult(u64 *result, const u64 *left, u32 right,
418 unsigned int ndigits)
419{
420 uint128_t r01 = { 0 };
421 unsigned int k;
422
423 for (k = 0; k < ndigits; k++) {
424 uint128_t product;
425
426 product = mul_64_64(left[k], right);
427 r01 = add_128_128(r01, product);
428 /* no carry */
429 result[k] = r01.m_low;
430 r01.m_low = r01.m_high;
431 r01.m_high = 0;
432 }
433 result[k] = r01.m_low;
434 for (++k; k < ndigits * 2; k++)
435 result[k] = 0;
436}
437
3c4b2390
SB
438static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
439{
440 uint128_t r01 = { 0, 0 };
441 u64 r2 = 0;
442 int i, k;
443
444 for (k = 0; k < ndigits * 2 - 1; k++) {
445 unsigned int min;
446
447 if (k < ndigits)
448 min = 0;
449 else
450 min = (k + 1) - ndigits;
451
452 for (i = min; i <= k && i <= k - i; i++) {
453 uint128_t product;
454
455 product = mul_64_64(left[i], left[k - i]);
456
457 if (i < k - i) {
458 r2 += product.m_high >> 63;
459 product.m_high = (product.m_high << 1) |
460 (product.m_low >> 63);
461 product.m_low <<= 1;
462 }
463
464 r01 = add_128_128(r01, product);
465 r2 += (r01.m_high < product.m_high);
466 }
467
468 result[k] = r01.m_low;
469 r01.m_low = r01.m_high;
470 r01.m_high = r2;
471 r2 = 0;
472 }
473
474 result[ndigits * 2 - 1] = r01.m_low;
475}
476
477/* Computes result = (left + right) % mod.
478 * Assumes that left < mod and right < mod, result != mod.
479 */
480static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
481 const u64 *mod, unsigned int ndigits)
482{
483 u64 carry;
484
485 carry = vli_add(result, left, right, ndigits);
486
487 /* result > mod (result = mod + remainder), so subtract mod to
488 * get remainder.
489 */
490 if (carry || vli_cmp(result, mod, ndigits) >= 0)
491 vli_sub(result, result, mod, ndigits);
492}
493
494/* Computes result = (left - right) % mod.
495 * Assumes that left < mod and right < mod, result != mod.
496 */
497static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
498 const u64 *mod, unsigned int ndigits)
499{
500 u64 borrow = vli_sub(result, left, right, ndigits);
501
502 /* In this case, p_result == -diff == (max int) - diff.
503 * Since -x % d == d - x, we can get the correct result from
504 * result + mod (with overflow).
505 */
506 if (borrow)
507 vli_add(result, result, mod, ndigits);
508}
509
0d7a7864
VC
510/*
511 * Computes result = product % mod
512 * for special form moduli: p = 2^k-c, for small c (note the minus sign)
513 *
514 * References:
515 * R. Crandall, C. Pomerance. Prime Numbers: A Computational Perspective.
516 * 9 Fast Algorithms for Large-Integer Arithmetic. 9.2.3 Moduli of special form
517 * Algorithm 9.2.13 (Fast mod operation for special-form moduli).
518 */
519static void vli_mmod_special(u64 *result, const u64 *product,
520 const u64 *mod, unsigned int ndigits)
521{
522 u64 c = -mod[0];
523 u64 t[ECC_MAX_DIGITS * 2];
524 u64 r[ECC_MAX_DIGITS * 2];
525
526 vli_set(r, product, ndigits * 2);
527 while (!vli_is_zero(r + ndigits, ndigits)) {
528 vli_umult(t, r + ndigits, c, ndigits);
529 vli_clear(r + ndigits, ndigits);
530 vli_add(r, r, t, ndigits * 2);
531 }
532 vli_set(t, mod, ndigits);
533 vli_clear(t + ndigits, ndigits);
534 while (vli_cmp(r, t, ndigits * 2) >= 0)
535 vli_sub(r, r, t, ndigits * 2);
536 vli_set(result, r, ndigits);
537}
538
539/*
540 * Computes result = product % mod
541 * for special form moduli: p = 2^{k-1}+c, for small c (note the plus sign)
542 * where k-1 does not fit into qword boundary by -1 bit (such as 255).
543
544 * References (loosely based on):
545 * A. Menezes, P. van Oorschot, S. Vanstone. Handbook of Applied Cryptography.
546 * 14.3.4 Reduction methods for moduli of special form. Algorithm 14.47.
547 * URL: http://cacr.uwaterloo.ca/hac/about/chap14.pdf
548 *
549 * H. Cohen, G. Frey, R. Avanzi, C. Doche, T. Lange, K. Nguyen, F. Vercauteren.
550 * Handbook of Elliptic and Hyperelliptic Curve Cryptography.
551 * Algorithm 10.25 Fast reduction for special form moduli
552 */
553static void vli_mmod_special2(u64 *result, const u64 *product,
554 const u64 *mod, unsigned int ndigits)
555{
556 u64 c2 = mod[0] * 2;
557 u64 q[ECC_MAX_DIGITS];
558 u64 r[ECC_MAX_DIGITS * 2];
559 u64 m[ECC_MAX_DIGITS * 2]; /* expanded mod */
560 int carry; /* last bit that doesn't fit into q */
561 int i;
562
563 vli_set(m, mod, ndigits);
564 vli_clear(m + ndigits, ndigits);
565
566 vli_set(r, product, ndigits);
567 /* q and carry are top bits */
568 vli_set(q, product + ndigits, ndigits);
569 vli_clear(r + ndigits, ndigits);
570 carry = vli_is_negative(r, ndigits);
571 if (carry)
572 r[ndigits - 1] &= (1ull << 63) - 1;
573 for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
574 u64 qc[ECC_MAX_DIGITS * 2];
575
576 vli_umult(qc, q, c2, ndigits);
577 if (carry)
578 vli_uadd(qc, qc, mod[0], ndigits * 2);
579 vli_set(q, qc + ndigits, ndigits);
580 vli_clear(qc + ndigits, ndigits);
581 carry = vli_is_negative(qc, ndigits);
582 if (carry)
583 qc[ndigits - 1] &= (1ull << 63) - 1;
584 if (i & 1)
585 vli_sub(r, r, qc, ndigits * 2);
586 else
587 vli_add(r, r, qc, ndigits * 2);
588 }
589 while (vli_is_negative(r, ndigits * 2))
590 vli_add(r, r, m, ndigits * 2);
591 while (vli_cmp(r, m, ndigits * 2) >= 0)
592 vli_sub(r, r, m, ndigits * 2);
593
594 vli_set(result, r, ndigits);
595}
596
597/*
598 * Computes result = product % mod, where product is 2N words long.
599 * Reference: Ken MacKay's micro-ecc.
600 * Currently only designed to work for curve_p or curve_n.
601 */
602static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
603 unsigned int ndigits)
604{
605 u64 mod_m[2 * ECC_MAX_DIGITS];
606 u64 tmp[2 * ECC_MAX_DIGITS];
607 u64 *v[2] = { tmp, product };
608 u64 carry = 0;
609 unsigned int i;
610 /* Shift mod so its highest set bit is at the maximum position. */
611 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
612 int word_shift = shift / 64;
613 int bit_shift = shift % 64;
614
615 vli_clear(mod_m, word_shift);
616 if (bit_shift > 0) {
617 for (i = 0; i < ndigits; ++i) {
618 mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
619 carry = mod[i] >> (64 - bit_shift);
620 }
621 } else
622 vli_set(mod_m + word_shift, mod, ndigits);
623
624 for (i = 1; shift >= 0; --shift) {
625 u64 borrow = 0;
626 unsigned int j;
627
628 for (j = 0; j < ndigits * 2; ++j) {
629 u64 diff = v[i][j] - mod_m[j] - borrow;
630
631 if (diff != v[i][j])
632 borrow = (diff > v[i][j]);
633 v[1 - i][j] = diff;
634 }
635 i = !(i ^ borrow); /* Swap the index if there was no borrow */
636 vli_rshift1(mod_m, ndigits);
637 mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
638 vli_rshift1(mod_m + ndigits, ndigits);
639 }
640 vli_set(result, v[i], ndigits);
641}
642
643/* Computes result = product % mod using Barrett's reduction with precomputed
644 * value mu appended to the mod after ndigits, mu = (2^{2w} / mod) and have
645 * length ndigits + 1, where mu * (2^w - 1) should not overflow ndigits
646 * boundary.
647 *
648 * Reference:
649 * R. Brent, P. Zimmermann. Modern Computer Arithmetic. 2010.
650 * 2.4.1 Barrett's algorithm. Algorithm 2.5.
651 */
652static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
653 unsigned int ndigits)
654{
655 u64 q[ECC_MAX_DIGITS * 2];
656 u64 r[ECC_MAX_DIGITS * 2];
657 const u64 *mu = mod + ndigits;
658
659 vli_mult(q, product + ndigits, mu, ndigits);
660 if (mu[ndigits])
661 vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
662 vli_mult(r, mod, q + ndigits, ndigits);
663 vli_sub(r, product, r, ndigits * 2);
664 while (!vli_is_zero(r + ndigits, ndigits) ||
665 vli_cmp(r, mod, ndigits) != -1) {
666 u64 carry;
667
668 carry = vli_sub(r, r, mod, ndigits);
669 vli_usub(r + ndigits, r + ndigits, carry, ndigits);
670 }
671 vli_set(result, r, ndigits);
672}
673
3c4b2390
SB
674/* Computes p_result = p_product % curve_p.
675 * See algorithm 5 and 6 from
676 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
677 */
678static void vli_mmod_fast_192(u64 *result, const u64 *product,
679 const u64 *curve_prime, u64 *tmp)
680{
681 const unsigned int ndigits = 3;
682 int carry;
683
684 vli_set(result, product, ndigits);
685
686 vli_set(tmp, &product[3], ndigits);
687 carry = vli_add(result, result, tmp, ndigits);
688
689 tmp[0] = 0;
690 tmp[1] = product[3];
691 tmp[2] = product[4];
692 carry += vli_add(result, result, tmp, ndigits);
693
694 tmp[0] = tmp[1] = product[5];
695 tmp[2] = 0;
696 carry += vli_add(result, result, tmp, ndigits);
697
698 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
699 carry -= vli_sub(result, result, curve_prime, ndigits);
700}
701
702/* Computes result = product % curve_prime
703 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
704 */
705static void vli_mmod_fast_256(u64 *result, const u64 *product,
706 const u64 *curve_prime, u64 *tmp)
707{
708 int carry;
709 const unsigned int ndigits = 4;
710
711 /* t */
712 vli_set(result, product, ndigits);
713
714 /* s1 */
715 tmp[0] = 0;
716 tmp[1] = product[5] & 0xffffffff00000000ull;
717 tmp[2] = product[6];
718 tmp[3] = product[7];
719 carry = vli_lshift(tmp, tmp, 1, ndigits);
720 carry += vli_add(result, result, tmp, ndigits);
721
722 /* s2 */
723 tmp[1] = product[6] << 32;
724 tmp[2] = (product[6] >> 32) | (product[7] << 32);
725 tmp[3] = product[7] >> 32;
726 carry += vli_lshift(tmp, tmp, 1, ndigits);
727 carry += vli_add(result, result, tmp, ndigits);
728
729 /* s3 */
730 tmp[0] = product[4];
731 tmp[1] = product[5] & 0xffffffff;
732 tmp[2] = 0;
733 tmp[3] = product[7];
734 carry += vli_add(result, result, tmp, ndigits);
735
736 /* s4 */
737 tmp[0] = (product[4] >> 32) | (product[5] << 32);
738 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
739 tmp[2] = product[7];
740 tmp[3] = (product[6] >> 32) | (product[4] << 32);
741 carry += vli_add(result, result, tmp, ndigits);
742
743 /* d1 */
744 tmp[0] = (product[5] >> 32) | (product[6] << 32);
745 tmp[1] = (product[6] >> 32);
746 tmp[2] = 0;
747 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
748 carry -= vli_sub(result, result, tmp, ndigits);
749
750 /* d2 */
751 tmp[0] = product[6];
752 tmp[1] = product[7];
753 tmp[2] = 0;
754 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
755 carry -= vli_sub(result, result, tmp, ndigits);
756
757 /* d3 */
758 tmp[0] = (product[6] >> 32) | (product[7] << 32);
759 tmp[1] = (product[7] >> 32) | (product[4] << 32);
760 tmp[2] = (product[4] >> 32) | (product[5] << 32);
761 tmp[3] = (product[6] << 32);
762 carry -= vli_sub(result, result, tmp, ndigits);
763
764 /* d4 */
765 tmp[0] = product[7];
766 tmp[1] = product[4] & 0xffffffff00000000ull;
767 tmp[2] = product[5];
768 tmp[3] = product[6] & 0xffffffff00000000ull;
769 carry -= vli_sub(result, result, tmp, ndigits);
770
771 if (carry < 0) {
772 do {
773 carry += vli_add(result, result, curve_prime, ndigits);
774 } while (carry < 0);
775 } else {
776 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
777 carry -= vli_sub(result, result, curve_prime, ndigits);
778 }
779}
780
0d7a7864
VC
781/* Computes result = product % curve_prime for different curve_primes.
782 *
783 * Note that curve_primes are distinguished just by heuristic check and
784 * not by complete conformance check.
785 */
3c4b2390
SB
786static bool vli_mmod_fast(u64 *result, u64 *product,
787 const u64 *curve_prime, unsigned int ndigits)
788{
d5c3b178 789 u64 tmp[2 * ECC_MAX_DIGITS];
3c4b2390 790
0d7a7864
VC
791 /* Currently, both NIST primes have -1 in lowest qword. */
792 if (curve_prime[0] != -1ull) {
793 /* Try to handle Pseudo-Marsenne primes. */
794 if (curve_prime[ndigits - 1] == -1ull) {
795 vli_mmod_special(result, product, curve_prime,
796 ndigits);
797 return true;
798 } else if (curve_prime[ndigits - 1] == 1ull << 63 &&
799 curve_prime[ndigits - 2] == 0) {
800 vli_mmod_special2(result, product, curve_prime,
801 ndigits);
802 return true;
803 }
804 vli_mmod_barrett(result, product, curve_prime, ndigits);
805 return true;
806 }
807
3c4b2390
SB
808 switch (ndigits) {
809 case 3:
810 vli_mmod_fast_192(result, product, curve_prime, tmp);
811 break;
812 case 4:
813 vli_mmod_fast_256(result, product, curve_prime, tmp);
814 break;
815 default:
0d7a7864 816 pr_err_ratelimited("ecc: unsupported digits size!\n");
3c4b2390
SB
817 return false;
818 }
819
820 return true;
821}
822
0d7a7864
VC
823/* Computes result = (left * right) % mod.
824 * Assumes that mod is big enough curve order.
825 */
826void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
827 const u64 *mod, unsigned int ndigits)
828{
829 u64 product[ECC_MAX_DIGITS * 2];
830
831 vli_mult(product, left, right, ndigits);
832 vli_mmod_slow(result, product, mod, ndigits);
833}
834EXPORT_SYMBOL(vli_mod_mult_slow);
835
3c4b2390
SB
836/* Computes result = (left * right) % curve_prime. */
837static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
838 const u64 *curve_prime, unsigned int ndigits)
839{
d5c3b178 840 u64 product[2 * ECC_MAX_DIGITS];
3c4b2390
SB
841
842 vli_mult(product, left, right, ndigits);
843 vli_mmod_fast(result, product, curve_prime, ndigits);
844}
845
846/* Computes result = left^2 % curve_prime. */
847static void vli_mod_square_fast(u64 *result, const u64 *left,
848 const u64 *curve_prime, unsigned int ndigits)
849{
d5c3b178 850 u64 product[2 * ECC_MAX_DIGITS];
3c4b2390
SB
851
852 vli_square(product, left, ndigits);
853 vli_mmod_fast(result, product, curve_prime, ndigits);
854}
855
856#define EVEN(vli) (!(vli[0] & 1))
857/* Computes result = (1 / p_input) % mod. All VLIs are the same size.
858 * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
859 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
860 */
4a2289da 861void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
3c4b2390
SB
862 unsigned int ndigits)
863{
d5c3b178
KC
864 u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
865 u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
3c4b2390
SB
866 u64 carry;
867 int cmp_result;
868
869 if (vli_is_zero(input, ndigits)) {
870 vli_clear(result, ndigits);
871 return;
872 }
873
874 vli_set(a, input, ndigits);
875 vli_set(b, mod, ndigits);
876 vli_clear(u, ndigits);
877 u[0] = 1;
878 vli_clear(v, ndigits);
879
880 while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
881 carry = 0;
882
883 if (EVEN(a)) {
884 vli_rshift1(a, ndigits);
885
886 if (!EVEN(u))
887 carry = vli_add(u, u, mod, ndigits);
888
889 vli_rshift1(u, ndigits);
890 if (carry)
891 u[ndigits - 1] |= 0x8000000000000000ull;
892 } else if (EVEN(b)) {
893 vli_rshift1(b, ndigits);
894
895 if (!EVEN(v))
896 carry = vli_add(v, v, mod, ndigits);
897
898 vli_rshift1(v, ndigits);
899 if (carry)
900 v[ndigits - 1] |= 0x8000000000000000ull;
901 } else if (cmp_result > 0) {
902 vli_sub(a, a, b, ndigits);
903 vli_rshift1(a, ndigits);
904
905 if (vli_cmp(u, v, ndigits) < 0)
906 vli_add(u, u, mod, ndigits);
907
908 vli_sub(u, u, v, ndigits);
909 if (!EVEN(u))
910 carry = vli_add(u, u, mod, ndigits);
911
912 vli_rshift1(u, ndigits);
913 if (carry)
914 u[ndigits - 1] |= 0x8000000000000000ull;
915 } else {
916 vli_sub(b, b, a, ndigits);
917 vli_rshift1(b, ndigits);
918
919 if (vli_cmp(v, u, ndigits) < 0)
920 vli_add(v, v, mod, ndigits);
921
922 vli_sub(v, v, u, ndigits);
923 if (!EVEN(v))
924 carry = vli_add(v, v, mod, ndigits);
925
926 vli_rshift1(v, ndigits);
927 if (carry)
928 v[ndigits - 1] |= 0x8000000000000000ull;
929 }
930 }
931
932 vli_set(result, u, ndigits);
933}
4a2289da 934EXPORT_SYMBOL(vli_mod_inv);
3c4b2390
SB
935
936/* ------ Point operations ------ */
937
938/* Returns true if p_point is the point at infinity, false otherwise. */
939static bool ecc_point_is_zero(const struct ecc_point *point)
940{
941 return (vli_is_zero(point->x, point->ndigits) &&
942 vli_is_zero(point->y, point->ndigits));
943}
944
945/* Point multiplication algorithm using Montgomery's ladder with co-Z
9332a9e7 946 * coordinates. From https://eprint.iacr.org/2011/338.pdf
3c4b2390
SB
947 */
948
949/* Double in place */
950static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
951 u64 *curve_prime, unsigned int ndigits)
952{
953 /* t1 = x, t2 = y, t3 = z */
d5c3b178
KC
954 u64 t4[ECC_MAX_DIGITS];
955 u64 t5[ECC_MAX_DIGITS];
3c4b2390
SB
956
957 if (vli_is_zero(z1, ndigits))
958 return;
959
960 /* t4 = y1^2 */
961 vli_mod_square_fast(t4, y1, curve_prime, ndigits);
962 /* t5 = x1*y1^2 = A */
963 vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
964 /* t4 = y1^4 */
965 vli_mod_square_fast(t4, t4, curve_prime, ndigits);
966 /* t2 = y1*z1 = z3 */
967 vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
968 /* t3 = z1^2 */
969 vli_mod_square_fast(z1, z1, curve_prime, ndigits);
970
971 /* t1 = x1 + z1^2 */
972 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
973 /* t3 = 2*z1^2 */
974 vli_mod_add(z1, z1, z1, curve_prime, ndigits);
975 /* t3 = x1 - z1^2 */
976 vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
977 /* t1 = x1^2 - z1^4 */
978 vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
979
980 /* t3 = 2*(x1^2 - z1^4) */
981 vli_mod_add(z1, x1, x1, curve_prime, ndigits);
982 /* t1 = 3*(x1^2 - z1^4) */
983 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
984 if (vli_test_bit(x1, 0)) {
985 u64 carry = vli_add(x1, x1, curve_prime, ndigits);
986
987 vli_rshift1(x1, ndigits);
988 x1[ndigits - 1] |= carry << 63;
989 } else {
990 vli_rshift1(x1, ndigits);
991 }
992 /* t1 = 3/2*(x1^2 - z1^4) = B */
993
994 /* t3 = B^2 */
995 vli_mod_square_fast(z1, x1, curve_prime, ndigits);
996 /* t3 = B^2 - A */
997 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
998 /* t3 = B^2 - 2A = x3 */
999 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
1000 /* t5 = A - x3 */
1001 vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
1002 /* t1 = B * (A - x3) */
1003 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1004 /* t4 = B * (A - x3) - y1^4 = y3 */
1005 vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
1006
1007 vli_set(x1, z1, ndigits);
1008 vli_set(z1, y1, ndigits);
1009 vli_set(y1, t4, ndigits);
1010}
1011
1012/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
1013static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
1014 unsigned int ndigits)
1015{
d5c3b178 1016 u64 t1[ECC_MAX_DIGITS];
3c4b2390
SB
1017
1018 vli_mod_square_fast(t1, z, curve_prime, ndigits); /* z^2 */
1019 vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
1020 vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits); /* z^3 */
1021 vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
1022}
1023
1024/* P = (x1, y1) => 2P, (x2, y2) => P' */
1025static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1026 u64 *p_initial_z, u64 *curve_prime,
1027 unsigned int ndigits)
1028{
d5c3b178 1029 u64 z[ECC_MAX_DIGITS];
3c4b2390
SB
1030
1031 vli_set(x2, x1, ndigits);
1032 vli_set(y2, y1, ndigits);
1033
1034 vli_clear(z, ndigits);
1035 z[0] = 1;
1036
1037 if (p_initial_z)
1038 vli_set(z, p_initial_z, ndigits);
1039
1040 apply_z(x1, y1, z, curve_prime, ndigits);
1041
1042 ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
1043
1044 apply_z(x2, y2, z, curve_prime, ndigits);
1045}
1046
1047/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1048 * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
1049 * or P => P', Q => P + Q
1050 */
1051static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1052 unsigned int ndigits)
1053{
1054 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
d5c3b178 1055 u64 t5[ECC_MAX_DIGITS];
3c4b2390
SB
1056
1057 /* t5 = x2 - x1 */
1058 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1059 /* t5 = (x2 - x1)^2 = A */
1060 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1061 /* t1 = x1*A = B */
1062 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1063 /* t3 = x2*A = C */
1064 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1065 /* t4 = y2 - y1 */
1066 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1067 /* t5 = (y2 - y1)^2 = D */
1068 vli_mod_square_fast(t5, y2, curve_prime, ndigits);
1069
1070 /* t5 = D - B */
1071 vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
1072 /* t5 = D - B - C = x3 */
1073 vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
1074 /* t3 = C - B */
1075 vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
1076 /* t2 = y1*(C - B) */
1077 vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
1078 /* t3 = B - x3 */
1079 vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
1080 /* t4 = (y2 - y1)*(B - x3) */
1081 vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
1082 /* t4 = y3 */
1083 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1084
1085 vli_set(x2, t5, ndigits);
1086}
1087
1088/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
1089 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
1090 * or P => P - Q, Q => P + Q
1091 */
1092static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1093 unsigned int ndigits)
1094{
1095 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
d5c3b178
KC
1096 u64 t5[ECC_MAX_DIGITS];
1097 u64 t6[ECC_MAX_DIGITS];
1098 u64 t7[ECC_MAX_DIGITS];
3c4b2390
SB
1099
1100 /* t5 = x2 - x1 */
1101 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1102 /* t5 = (x2 - x1)^2 = A */
1103 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1104 /* t1 = x1*A = B */
1105 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1106 /* t3 = x2*A = C */
1107 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1108 /* t4 = y2 + y1 */
1109 vli_mod_add(t5, y2, y1, curve_prime, ndigits);
1110 /* t4 = y2 - y1 */
1111 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1112
1113 /* t6 = C - B */
1114 vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
1115 /* t2 = y1 * (C - B) */
1116 vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
1117 /* t6 = B + C */
1118 vli_mod_add(t6, x1, x2, curve_prime, ndigits);
1119 /* t3 = (y2 - y1)^2 */
1120 vli_mod_square_fast(x2, y2, curve_prime, ndigits);
1121 /* t3 = x3 */
1122 vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
1123
1124 /* t7 = B - x3 */
1125 vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
1126 /* t4 = (y2 - y1)*(B - x3) */
1127 vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
1128 /* t4 = y3 */
1129 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1130
1131 /* t7 = (y2 + y1)^2 = F */
1132 vli_mod_square_fast(t7, t5, curve_prime, ndigits);
1133 /* t7 = x3' */
1134 vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
1135 /* t6 = x3' - B */
1136 vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
1137 /* t6 = (y2 + y1)*(x3' - B) */
1138 vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
1139 /* t2 = y3' */
1140 vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
1141
1142 vli_set(x1, t7, ndigits);
1143}
1144
1145static void ecc_point_mult(struct ecc_point *result,
1146 const struct ecc_point *point, const u64 *scalar,
3da2c1df 1147 u64 *initial_z, const struct ecc_curve *curve,
3c4b2390
SB
1148 unsigned int ndigits)
1149{
1150 /* R0 and R1 */
d5c3b178
KC
1151 u64 rx[2][ECC_MAX_DIGITS];
1152 u64 ry[2][ECC_MAX_DIGITS];
1153 u64 z[ECC_MAX_DIGITS];
3da2c1df
VC
1154 u64 sk[2][ECC_MAX_DIGITS];
1155 u64 *curve_prime = curve->p;
3c4b2390 1156 int i, nb;
3da2c1df
VC
1157 int num_bits;
1158 int carry;
1159
1160 carry = vli_add(sk[0], scalar, curve->n, ndigits);
1161 vli_add(sk[1], sk[0], curve->n, ndigits);
1162 scalar = sk[!carry];
1163 num_bits = sizeof(u64) * ndigits * 8 + 1;
3c4b2390
SB
1164
1165 vli_set(rx[1], point->x, ndigits);
1166 vli_set(ry[1], point->y, ndigits);
1167
1168 xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
1169 ndigits);
1170
1171 for (i = num_bits - 2; i > 0; i--) {
1172 nb = !vli_test_bit(scalar, i);
1173 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1174 ndigits);
1175 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
1176 ndigits);
1177 }
1178
1179 nb = !vli_test_bit(scalar, 0);
1180 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1181 ndigits);
1182
1183 /* Find final 1/Z value. */
1184 /* X1 - X0 */
1185 vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
1186 /* Yb * (X1 - X0) */
1187 vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
1188 /* xP * Yb * (X1 - X0) */
1189 vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
1190
1191 /* 1 / (xP * Yb * (X1 - X0)) */
1192 vli_mod_inv(z, z, curve_prime, point->ndigits);
1193
1194 /* yP / (xP * Yb * (X1 - X0)) */
1195 vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
1196 /* Xb * yP / (xP * Yb * (X1 - X0)) */
1197 vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
1198 /* End 1/Z calculation */
1199
1200 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
1201
1202 apply_z(rx[0], ry[0], z, curve_prime, ndigits);
1203
1204 vli_set(result->x, rx[0], ndigits);
1205 vli_set(result->y, ry[0], ndigits);
1206}
1207
0d7a7864
VC
1208/* Computes R = P + Q mod p */
1209static void ecc_point_add(const struct ecc_point *result,
1210 const struct ecc_point *p, const struct ecc_point *q,
1211 const struct ecc_curve *curve)
1212{
1213 u64 z[ECC_MAX_DIGITS];
1214 u64 px[ECC_MAX_DIGITS];
1215 u64 py[ECC_MAX_DIGITS];
1216 unsigned int ndigits = curve->g.ndigits;
1217
1218 vli_set(result->x, q->x, ndigits);
1219 vli_set(result->y, q->y, ndigits);
1220 vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
1221 vli_set(px, p->x, ndigits);
1222 vli_set(py, p->y, ndigits);
1223 xycz_add(px, py, result->x, result->y, curve->p, ndigits);
1224 vli_mod_inv(z, z, curve->p, ndigits);
1225 apply_z(result->x, result->y, z, curve->p, ndigits);
1226}
1227
1228/* Computes R = u1P + u2Q mod p using Shamir's trick.
1229 * Based on: Kenneth MacKay's micro-ecc (2014).
1230 */
1231void ecc_point_mult_shamir(const struct ecc_point *result,
1232 const u64 *u1, const struct ecc_point *p,
1233 const u64 *u2, const struct ecc_point *q,
1234 const struct ecc_curve *curve)
1235{
1236 u64 z[ECC_MAX_DIGITS];
1237 u64 sump[2][ECC_MAX_DIGITS];
1238 u64 *rx = result->x;
1239 u64 *ry = result->y;
1240 unsigned int ndigits = curve->g.ndigits;
1241 unsigned int num_bits;
1242 struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
1243 const struct ecc_point *points[4];
1244 const struct ecc_point *point;
1245 unsigned int idx;
1246 int i;
1247
1248 ecc_point_add(&sum, p, q, curve);
1249 points[0] = NULL;
1250 points[1] = p;
1251 points[2] = q;
1252 points[3] = &sum;
1253
1254 num_bits = max(vli_num_bits(u1, ndigits),
1255 vli_num_bits(u2, ndigits));
1256 i = num_bits - 1;
1257 idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1258 point = points[idx];
1259
1260 vli_set(rx, point->x, ndigits);
1261 vli_set(ry, point->y, ndigits);
1262 vli_clear(z + 1, ndigits - 1);
1263 z[0] = 1;
1264
1265 for (--i; i >= 0; i--) {
1266 ecc_point_double_jacobian(rx, ry, z, curve->p, ndigits);
1267 idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1268 point = points[idx];
1269 if (point) {
1270 u64 tx[ECC_MAX_DIGITS];
1271 u64 ty[ECC_MAX_DIGITS];
1272 u64 tz[ECC_MAX_DIGITS];
1273
1274 vli_set(tx, point->x, ndigits);
1275 vli_set(ty, point->y, ndigits);
1276 apply_z(tx, ty, z, curve->p, ndigits);
1277 vli_mod_sub(tz, rx, tx, curve->p, ndigits);
1278 xycz_add(tx, ty, rx, ry, curve->p, ndigits);
1279 vli_mod_mult_fast(z, z, tz, curve->p, ndigits);
1280 }
1281 }
1282 vli_mod_inv(z, z, curve->p, ndigits);
1283 apply_z(rx, ry, z, curve->p, ndigits);
1284}
1285EXPORT_SYMBOL(ecc_point_mult_shamir);
1286
3c4b2390
SB
1287static inline void ecc_swap_digits(const u64 *in, u64 *out,
1288 unsigned int ndigits)
1289{
f398243e 1290 const __be64 *src = (__force __be64 *)in;
3c4b2390
SB
1291 int i;
1292
1293 for (i = 0; i < ndigits; i++)
f398243e 1294 out[i] = be64_to_cpu(src[ndigits - 1 - i]);
3c4b2390
SB
1295}
1296
2eb4942b
VC
1297static int __ecc_is_key_valid(const struct ecc_curve *curve,
1298 const u64 *private_key, unsigned int ndigits)
3c4b2390 1299{
2eb4942b
VC
1300 u64 one[ECC_MAX_DIGITS] = { 1, };
1301 u64 res[ECC_MAX_DIGITS];
3c4b2390
SB
1302
1303 if (!private_key)
1304 return -EINVAL;
1305
2eb4942b 1306 if (curve->g.ndigits != ndigits)
3c4b2390
SB
1307 return -EINVAL;
1308
2eb4942b
VC
1309 /* Make sure the private key is in the range [2, n-3]. */
1310 if (vli_cmp(one, private_key, ndigits) != -1)
3c4b2390 1311 return -EINVAL;
2eb4942b
VC
1312 vli_sub(res, curve->n, one, ndigits);
1313 vli_sub(res, res, one, ndigits);
1314 if (vli_cmp(res, private_key, ndigits) != 1)
3c4b2390
SB
1315 return -EINVAL;
1316
1317 return 0;
1318}
1319
2eb4942b
VC
1320int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
1321 const u64 *private_key, unsigned int private_key_len)
1322{
1323 int nbytes;
1324 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1325
1326 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1327
1328 if (private_key_len != nbytes)
1329 return -EINVAL;
1330
1331 return __ecc_is_key_valid(curve, private_key, ndigits);
1332}
4a2289da 1333EXPORT_SYMBOL(ecc_is_key_valid);
2eb4942b 1334
6755fd26
TDA
1335/*
1336 * ECC private keys are generated using the method of extra random bits,
1337 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
1338 *
1339 * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
1340 * than requested
1341 * 0 <= c mod(n-1) <= n-2 and implies that
1342 * 1 <= d <= n-1
1343 *
1344 * This method generates a private key uniformly distributed in the range
1345 * [1, n-1].
1346 */
1347int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
1348{
1349 const struct ecc_curve *curve = ecc_get_curve(curve_id);
d5c3b178 1350 u64 priv[ECC_MAX_DIGITS];
6755fd26
TDA
1351 unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1352 unsigned int nbits = vli_num_bits(curve->n, ndigits);
1353 int err;
1354
1355 /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
d5c3b178 1356 if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
6755fd26
TDA
1357 return -EINVAL;
1358
1359 /*
1360 * FIPS 186-4 recommends that the private key should be obtained from a
1361 * RBG with a security strength equal to or greater than the security
1362 * strength associated with N.
1363 *
1364 * The maximum security strength identified by NIST SP800-57pt1r4 for
1365 * ECC is 256 (N >= 512).
1366 *
1367 * This condition is met by the default RNG because it selects a favored
1368 * DRBG with a security strength of 256.
1369 */
1370 if (crypto_get_default_rng())
4c0e22c9 1371 return -EFAULT;
6755fd26
TDA
1372
1373 err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
1374 crypto_put_default_rng();
1375 if (err)
1376 return err;
1377
2eb4942b
VC
1378 /* Make sure the private key is in the valid range. */
1379 if (__ecc_is_key_valid(curve, priv, ndigits))
6755fd26
TDA
1380 return -EINVAL;
1381
1382 ecc_swap_digits(priv, privkey, ndigits);
1383
1384 return 0;
1385}
4a2289da 1386EXPORT_SYMBOL(ecc_gen_privkey);
6755fd26 1387
7380c56d
TDA
1388int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1389 const u64 *private_key, u64 *public_key)
3c4b2390
SB
1390{
1391 int ret = 0;
1392 struct ecc_point *pk;
d5c3b178 1393 u64 priv[ECC_MAX_DIGITS];
3c4b2390
SB
1394 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1395
d5c3b178 1396 if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
3c4b2390
SB
1397 ret = -EINVAL;
1398 goto out;
1399 }
1400
ad269597 1401 ecc_swap_digits(private_key, priv, ndigits);
3c4b2390
SB
1402
1403 pk = ecc_alloc_point(ndigits);
1404 if (!pk) {
1405 ret = -ENOMEM;
1406 goto out;
1407 }
1408
3da2c1df 1409 ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
6914dd53
SM
1410
1411 /* SP800-56A rev 3 5.6.2.1.3 key check */
1412 if (ecc_is_pubkey_valid_full(curve, pk)) {
3c4b2390
SB
1413 ret = -EAGAIN;
1414 goto err_free_point;
1415 }
1416
ad269597
TDA
1417 ecc_swap_digits(pk->x, public_key, ndigits);
1418 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
3c4b2390
SB
1419
1420err_free_point:
1421 ecc_free_point(pk);
1422out:
1423 return ret;
1424}
4a2289da 1425EXPORT_SYMBOL(ecc_make_pub_key);
3c4b2390 1426
ea169a30 1427/* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
4a2289da
VC
1428int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1429 struct ecc_point *pk)
ea169a30
SM
1430{
1431 u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1432
0d7a7864
VC
1433 if (WARN_ON(pk->ndigits != curve->g.ndigits))
1434 return -EINVAL;
1435
ea169a30
SM
1436 /* Check 1: Verify key is not the zero point. */
1437 if (ecc_point_is_zero(pk))
1438 return -EINVAL;
1439
1440 /* Check 2: Verify key is in the range [1, p-1]. */
1441 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1442 return -EINVAL;
1443 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1444 return -EINVAL;
1445
1446 /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1447 vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
1448 vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
1449 vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
1450 vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
1451 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1452 vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1453 if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1454 return -EINVAL;
1455
1456 return 0;
ea169a30 1457}
4a2289da 1458EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
ea169a30 1459
6914dd53
SM
1460/* SP800-56A section 5.6.2.3.3 full verification */
1461int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
1462 struct ecc_point *pk)
1463{
1464 struct ecc_point *nQ;
1465
1466 /* Checks 1 through 3 */
1467 int ret = ecc_is_pubkey_valid_partial(curve, pk);
1468
1469 if (ret)
1470 return ret;
1471
1472 /* Check 4: Verify that nQ is the zero point. */
1473 nQ = ecc_alloc_point(pk->ndigits);
1474 if (!nQ)
1475 return -ENOMEM;
1476
1477 ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits);
1478 if (!ecc_point_is_zero(nQ))
1479 ret = -EINVAL;
1480
1481 ecc_free_point(nQ);
1482
1483 return ret;
1484}
1485EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
1486
8f44df15 1487int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
ad269597
TDA
1488 const u64 *private_key, const u64 *public_key,
1489 u64 *secret)
3c4b2390
SB
1490{
1491 int ret = 0;
1492 struct ecc_point *product, *pk;
d5c3b178
KC
1493 u64 priv[ECC_MAX_DIGITS];
1494 u64 rand_z[ECC_MAX_DIGITS];
1495 unsigned int nbytes;
3c4b2390
SB
1496 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1497
d5c3b178
KC
1498 if (!private_key || !public_key || !curve ||
1499 ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
3c4b2390
SB
1500 ret = -EINVAL;
1501 goto out;
1502 }
1503
d5c3b178 1504 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
3c4b2390 1505
d5c3b178 1506 get_random_bytes(rand_z, nbytes);
3c4b2390
SB
1507
1508 pk = ecc_alloc_point(ndigits);
1509 if (!pk) {
1510 ret = -ENOMEM;
d5c3b178 1511 goto out;
3c4b2390
SB
1512 }
1513
ea169a30
SM
1514 ecc_swap_digits(public_key, pk->x, ndigits);
1515 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1516 ret = ecc_is_pubkey_valid_partial(curve, pk);
1517 if (ret)
1518 goto err_alloc_product;
1519
1520 ecc_swap_digits(private_key, priv, ndigits);
1521
3c4b2390
SB
1522 product = ecc_alloc_point(ndigits);
1523 if (!product) {
1524 ret = -ENOMEM;
1525 goto err_alloc_product;
1526 }
1527
3da2c1df 1528 ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
3c4b2390 1529
e7d2b41e 1530 if (ecc_point_is_zero(product)) {
3c4b2390 1531 ret = -EFAULT;
e7d2b41e
SM
1532 goto err_validity;
1533 }
1534
1535 ecc_swap_digits(product->x, secret, ndigits);
3c4b2390 1536
e7d2b41e
SM
1537err_validity:
1538 memzero_explicit(priv, sizeof(priv));
1539 memzero_explicit(rand_z, sizeof(rand_z));
3c4b2390
SB
1540 ecc_free_point(product);
1541err_alloc_product:
1542 ecc_free_point(pk);
1543out:
1544 return ret;
1545}
4a2289da
VC
1546EXPORT_SYMBOL(crypto_ecdh_shared_secret);
1547
1548MODULE_LICENSE("Dual BSD/GPL");