1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#include <linux/module.h>
28#include <linux/random.h>
29#include <linux/slab.h>
30#include <linux/swab.h>
31#include <linux/fips.h>
32#include <crypto/ecdh.h>
33#include <crypto/rng.h>
34#include <asm/unaligned.h>
35#include <linux/ratelimit.h>
36
37#include "ecc.h"
38#include "ecc_curve_defs.h"
39
40typedef struct {
41 u64 m_low;
42 u64 m_high;
43} uint128_t;
44
45static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
46{
47 switch (curve_id) {
48
49 case ECC_CURVE_NIST_P192:
50 return fips_enabled ? NULL : &nist_p192;
51 case ECC_CURVE_NIST_P256:
52 return &nist_p256;
53 default:
54 return NULL;
55 }
56}
57
58static u64 *ecc_alloc_digits_space(unsigned int ndigits)
59{
60 size_t len = ndigits * sizeof(u64);
61
62 if (!len)
63 return NULL;
64
65 return kmalloc(len, GFP_KERNEL);
66}
67
68static void ecc_free_digits_space(u64 *space)
69{
70 kfree_sensitive(space);
71}
72
73static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
74{
75 struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
76
77 if (!p)
78 return NULL;
79
80 p->x = ecc_alloc_digits_space(ndigits);
81 if (!p->x)
82 goto err_alloc_x;
83
84 p->y = ecc_alloc_digits_space(ndigits);
85 if (!p->y)
86 goto err_alloc_y;
87
88 p->ndigits = ndigits;
89
90 return p;
91
92err_alloc_y:
93 ecc_free_digits_space(p->x);
94err_alloc_x:
95 kfree(p);
96 return NULL;
97}
98
99static void ecc_free_point(struct ecc_point *p)
100{
101 if (!p)
102 return;
103
104 kfree_sensitive(p->x);
105 kfree_sensitive(p->y);
106 kfree_sensitive(p);
107}
108
109static void vli_clear(u64 *vli, unsigned int ndigits)
110{
111 int i;
112
113 for (i = 0; i < ndigits; i++)
114 vli[i] = 0;
115}
116
117
118bool vli_is_zero(const u64 *vli, unsigned int ndigits)
119{
120 int i;
121
122 for (i = 0; i < ndigits; i++) {
123 if (vli[i])
124 return false;
125 }
126
127 return true;
128}
129EXPORT_SYMBOL(vli_is_zero);
130
131
132static u64 vli_test_bit(const u64 *vli, unsigned int bit)
133{
134 return (vli[bit / 64] & ((u64)1 << (bit % 64)));
135}
136
137static bool vli_is_negative(const u64 *vli, unsigned int ndigits)
138{
139 return vli_test_bit(vli, ndigits * 64 - 1);
140}
141
142
143static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
144{
145 int i;
146
147
148
149
150
151 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
152
153 return (i + 1);
154}
155
156
157static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
158{
159 unsigned int i, num_digits;
160 u64 digit;
161
162 num_digits = vli_num_digits(vli, ndigits);
163 if (num_digits == 0)
164 return 0;
165
166 digit = vli[num_digits - 1];
167 for (i = 0; digit; i++)
168 digit >>= 1;
169
170 return ((num_digits - 1) * 64 + i);
171}
172
173
174void vli_from_be64(u64 *dest, const void *src, unsigned int ndigits)
175{
176 int i;
177 const u64 *from = src;
178
179 for (i = 0; i < ndigits; i++)
180 dest[i] = get_unaligned_be64(&from[ndigits - 1 - i]);
181}
182EXPORT_SYMBOL(vli_from_be64);
183
184void vli_from_le64(u64 *dest, const void *src, unsigned int ndigits)
185{
186 int i;
187 const u64 *from = src;
188
189 for (i = 0; i < ndigits; i++)
190 dest[i] = get_unaligned_le64(&from[i]);
191}
192EXPORT_SYMBOL(vli_from_le64);
193
194
195static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
196{
197 int i;
198
199 for (i = 0; i < ndigits; i++)
200 dest[i] = src[i];
201}
202
203
204int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
205{
206 int i;
207
208 for (i = ndigits - 1; i >= 0; i--) {
209 if (left[i] > right[i])
210 return 1;
211 else if (left[i] < right[i])
212 return -1;
213 }
214
215 return 0;
216}
217EXPORT_SYMBOL(vli_cmp);
218
219
220
221
222static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
223 unsigned int ndigits)
224{
225 u64 carry = 0;
226 int i;
227
228 for (i = 0; i < ndigits; i++) {
229 u64 temp = in[i];
230
231 result[i] = (temp << shift) | carry;
232 carry = temp >> (64 - shift);
233 }
234
235 return carry;
236}
237
238
239static void vli_rshift1(u64 *vli, unsigned int ndigits)
240{
241 u64 *end = vli;
242 u64 carry = 0;
243
244 vli += ndigits;
245
246 while (vli-- > end) {
247 u64 temp = *vli;
248 *vli = (temp >> 1) | carry;
249 carry = temp << 63;
250 }
251}
252
253
254static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
255 unsigned int ndigits)
256{
257 u64 carry = 0;
258 int i;
259
260 for (i = 0; i < ndigits; i++) {
261 u64 sum;
262
263 sum = left[i] + right[i] + carry;
264 if (sum != left[i])
265 carry = (sum < left[i]);
266
267 result[i] = sum;
268 }
269
270 return carry;
271}
272
273
274static u64 vli_uadd(u64 *result, const u64 *left, u64 right,
275 unsigned int ndigits)
276{
277 u64 carry = right;
278 int i;
279
280 for (i = 0; i < ndigits; i++) {
281 u64 sum;
282
283 sum = left[i] + carry;
284 if (sum != left[i])
285 carry = (sum < left[i]);
286 else
287 carry = !!carry;
288
289 result[i] = sum;
290 }
291
292 return carry;
293}
294
295
296u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
297 unsigned int ndigits)
298{
299 u64 borrow = 0;
300 int i;
301
302 for (i = 0; i < ndigits; i++) {
303 u64 diff;
304
305 diff = left[i] - right[i] - borrow;
306 if (diff != left[i])
307 borrow = (diff > left[i]);
308
309 result[i] = diff;
310 }
311
312 return borrow;
313}
314EXPORT_SYMBOL(vli_sub);
315
316
317static u64 vli_usub(u64 *result, const u64 *left, u64 right,
318 unsigned int ndigits)
319{
320 u64 borrow = right;
321 int i;
322
323 for (i = 0; i < ndigits; i++) {
324 u64 diff;
325
326 diff = left[i] - borrow;
327 if (diff != left[i])
328 borrow = (diff > left[i]);
329
330 result[i] = diff;
331 }
332
333 return borrow;
334}
335
336static uint128_t mul_64_64(u64 left, u64 right)
337{
338 uint128_t result;
339#if defined(CONFIG_ARCH_SUPPORTS_INT128)
340 unsigned __int128 m = (unsigned __int128)left * right;
341
342 result.m_low = m;
343 result.m_high = m >> 64;
344#else
345 u64 a0 = left & 0xffffffffull;
346 u64 a1 = left >> 32;
347 u64 b0 = right & 0xffffffffull;
348 u64 b1 = right >> 32;
349 u64 m0 = a0 * b0;
350 u64 m1 = a0 * b1;
351 u64 m2 = a1 * b0;
352 u64 m3 = a1 * b1;
353
354 m2 += (m0 >> 32);
355 m2 += m1;
356
357
358 if (m2 < m1)
359 m3 += 0x100000000ull;
360
361 result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
362 result.m_high = m3 + (m2 >> 32);
363#endif
364 return result;
365}
366
367static uint128_t add_128_128(uint128_t a, uint128_t b)
368{
369 uint128_t result;
370
371 result.m_low = a.m_low + b.m_low;
372 result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
373
374 return result;
375}
376
377static void vli_mult(u64 *result, const u64 *left, const u64 *right,
378 unsigned int ndigits)
379{
380 uint128_t r01 = { 0, 0 };
381 u64 r2 = 0;
382 unsigned int i, k;
383
384
385
386
387 for (k = 0; k < ndigits * 2 - 1; k++) {
388 unsigned int min;
389
390 if (k < ndigits)
391 min = 0;
392 else
393 min = (k + 1) - ndigits;
394
395 for (i = min; i <= k && i < ndigits; i++) {
396 uint128_t product;
397
398 product = mul_64_64(left[i], right[k - i]);
399
400 r01 = add_128_128(r01, product);
401 r2 += (r01.m_high < product.m_high);
402 }
403
404 result[k] = r01.m_low;
405 r01.m_low = r01.m_high;
406 r01.m_high = r2;
407 r2 = 0;
408 }
409
410 result[ndigits * 2 - 1] = r01.m_low;
411}
412
413
414static void vli_umult(u64 *result, const u64 *left, u32 right,
415 unsigned int ndigits)
416{
417 uint128_t r01 = { 0 };
418 unsigned int k;
419
420 for (k = 0; k < ndigits; k++) {
421 uint128_t product;
422
423 product = mul_64_64(left[k], right);
424 r01 = add_128_128(r01, product);
425
426 result[k] = r01.m_low;
427 r01.m_low = r01.m_high;
428 r01.m_high = 0;
429 }
430 result[k] = r01.m_low;
431 for (++k; k < ndigits * 2; k++)
432 result[k] = 0;
433}
434
435static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
436{
437 uint128_t r01 = { 0, 0 };
438 u64 r2 = 0;
439 int i, k;
440
441 for (k = 0; k < ndigits * 2 - 1; k++) {
442 unsigned int min;
443
444 if (k < ndigits)
445 min = 0;
446 else
447 min = (k + 1) - ndigits;
448
449 for (i = min; i <= k && i <= k - i; i++) {
450 uint128_t product;
451
452 product = mul_64_64(left[i], left[k - i]);
453
454 if (i < k - i) {
455 r2 += product.m_high >> 63;
456 product.m_high = (product.m_high << 1) |
457 (product.m_low >> 63);
458 product.m_low <<= 1;
459 }
460
461 r01 = add_128_128(r01, product);
462 r2 += (r01.m_high < product.m_high);
463 }
464
465 result[k] = r01.m_low;
466 r01.m_low = r01.m_high;
467 r01.m_high = r2;
468 r2 = 0;
469 }
470
471 result[ndigits * 2 - 1] = r01.m_low;
472}
473
474
475
476
477static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
478 const u64 *mod, unsigned int ndigits)
479{
480 u64 carry;
481
482 carry = vli_add(result, left, right, ndigits);
483
484
485
486
487 if (carry || vli_cmp(result, mod, ndigits) >= 0)
488 vli_sub(result, result, mod, ndigits);
489}
490
491
492
493
494static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
495 const u64 *mod, unsigned int ndigits)
496{
497 u64 borrow = vli_sub(result, left, right, ndigits);
498
499
500
501
502
503 if (borrow)
504 vli_add(result, result, mod, ndigits);
505}
506
507
508
509
510
511
512
513
514
515
516static void vli_mmod_special(u64 *result, const u64 *product,
517 const u64 *mod, unsigned int ndigits)
518{
519 u64 c = -mod[0];
520 u64 t[ECC_MAX_DIGITS * 2];
521 u64 r[ECC_MAX_DIGITS * 2];
522
523 vli_set(r, product, ndigits * 2);
524 while (!vli_is_zero(r + ndigits, ndigits)) {
525 vli_umult(t, r + ndigits, c, ndigits);
526 vli_clear(r + ndigits, ndigits);
527 vli_add(r, r, t, ndigits * 2);
528 }
529 vli_set(t, mod, ndigits);
530 vli_clear(t + ndigits, ndigits);
531 while (vli_cmp(r, t, ndigits * 2) >= 0)
532 vli_sub(r, r, t, ndigits * 2);
533 vli_set(result, r, ndigits);
534}
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550static void vli_mmod_special2(u64 *result, const u64 *product,
551 const u64 *mod, unsigned int ndigits)
552{
553 u64 c2 = mod[0] * 2;
554 u64 q[ECC_MAX_DIGITS];
555 u64 r[ECC_MAX_DIGITS * 2];
556 u64 m[ECC_MAX_DIGITS * 2];
557 int carry;
558 int i;
559
560 vli_set(m, mod, ndigits);
561 vli_clear(m + ndigits, ndigits);
562
563 vli_set(r, product, ndigits);
564
565 vli_set(q, product + ndigits, ndigits);
566 vli_clear(r + ndigits, ndigits);
567 carry = vli_is_negative(r, ndigits);
568 if (carry)
569 r[ndigits - 1] &= (1ull << 63) - 1;
570 for (i = 1; carry || !vli_is_zero(q, ndigits); i++) {
571 u64 qc[ECC_MAX_DIGITS * 2];
572
573 vli_umult(qc, q, c2, ndigits);
574 if (carry)
575 vli_uadd(qc, qc, mod[0], ndigits * 2);
576 vli_set(q, qc + ndigits, ndigits);
577 vli_clear(qc + ndigits, ndigits);
578 carry = vli_is_negative(qc, ndigits);
579 if (carry)
580 qc[ndigits - 1] &= (1ull << 63) - 1;
581 if (i & 1)
582 vli_sub(r, r, qc, ndigits * 2);
583 else
584 vli_add(r, r, qc, ndigits * 2);
585 }
586 while (vli_is_negative(r, ndigits * 2))
587 vli_add(r, r, m, ndigits * 2);
588 while (vli_cmp(r, m, ndigits * 2) >= 0)
589 vli_sub(r, r, m, ndigits * 2);
590
591 vli_set(result, r, ndigits);
592}
593
594
595
596
597
598
599static void vli_mmod_slow(u64 *result, u64 *product, const u64 *mod,
600 unsigned int ndigits)
601{
602 u64 mod_m[2 * ECC_MAX_DIGITS];
603 u64 tmp[2 * ECC_MAX_DIGITS];
604 u64 *v[2] = { tmp, product };
605 u64 carry = 0;
606 unsigned int i;
607
608 int shift = (ndigits * 2 * 64) - vli_num_bits(mod, ndigits);
609 int word_shift = shift / 64;
610 int bit_shift = shift % 64;
611
612 vli_clear(mod_m, word_shift);
613 if (bit_shift > 0) {
614 for (i = 0; i < ndigits; ++i) {
615 mod_m[word_shift + i] = (mod[i] << bit_shift) | carry;
616 carry = mod[i] >> (64 - bit_shift);
617 }
618 } else
619 vli_set(mod_m + word_shift, mod, ndigits);
620
621 for (i = 1; shift >= 0; --shift) {
622 u64 borrow = 0;
623 unsigned int j;
624
625 for (j = 0; j < ndigits * 2; ++j) {
626 u64 diff = v[i][j] - mod_m[j] - borrow;
627
628 if (diff != v[i][j])
629 borrow = (diff > v[i][j]);
630 v[1 - i][j] = diff;
631 }
632 i = !(i ^ borrow);
633 vli_rshift1(mod_m, ndigits);
634 mod_m[ndigits - 1] |= mod_m[ndigits] << (64 - 1);
635 vli_rshift1(mod_m + ndigits, ndigits);
636 }
637 vli_set(result, v[i], ndigits);
638}
639
640
641
642
643
644
645
646
647
648
649static void vli_mmod_barrett(u64 *result, u64 *product, const u64 *mod,
650 unsigned int ndigits)
651{
652 u64 q[ECC_MAX_DIGITS * 2];
653 u64 r[ECC_MAX_DIGITS * 2];
654 const u64 *mu = mod + ndigits;
655
656 vli_mult(q, product + ndigits, mu, ndigits);
657 if (mu[ndigits])
658 vli_add(q + ndigits, q + ndigits, product + ndigits, ndigits);
659 vli_mult(r, mod, q + ndigits, ndigits);
660 vli_sub(r, product, r, ndigits * 2);
661 while (!vli_is_zero(r + ndigits, ndigits) ||
662 vli_cmp(r, mod, ndigits) != -1) {
663 u64 carry;
664
665 carry = vli_sub(r, r, mod, ndigits);
666 vli_usub(r + ndigits, r + ndigits, carry, ndigits);
667 }
668 vli_set(result, r, ndigits);
669}
670
671
672
673
674
675static void vli_mmod_fast_192(u64 *result, const u64 *product,
676 const u64 *curve_prime, u64 *tmp)
677{
678 const unsigned int ndigits = 3;
679 int carry;
680
681 vli_set(result, product, ndigits);
682
683 vli_set(tmp, &product[3], ndigits);
684 carry = vli_add(result, result, tmp, ndigits);
685
686 tmp[0] = 0;
687 tmp[1] = product[3];
688 tmp[2] = product[4];
689 carry += vli_add(result, result, tmp, ndigits);
690
691 tmp[0] = tmp[1] = product[5];
692 tmp[2] = 0;
693 carry += vli_add(result, result, tmp, ndigits);
694
695 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
696 carry -= vli_sub(result, result, curve_prime, ndigits);
697}
698
699
700
701
702static void vli_mmod_fast_256(u64 *result, const u64 *product,
703 const u64 *curve_prime, u64 *tmp)
704{
705 int carry;
706 const unsigned int ndigits = 4;
707
708
709 vli_set(result, product, ndigits);
710
711
712 tmp[0] = 0;
713 tmp[1] = product[5] & 0xffffffff00000000ull;
714 tmp[2] = product[6];
715 tmp[3] = product[7];
716 carry = vli_lshift(tmp, tmp, 1, ndigits);
717 carry += vli_add(result, result, tmp, ndigits);
718
719
720 tmp[1] = product[6] << 32;
721 tmp[2] = (product[6] >> 32) | (product[7] << 32);
722 tmp[3] = product[7] >> 32;
723 carry += vli_lshift(tmp, tmp, 1, ndigits);
724 carry += vli_add(result, result, tmp, ndigits);
725
726
727 tmp[0] = product[4];
728 tmp[1] = product[5] & 0xffffffff;
729 tmp[2] = 0;
730 tmp[3] = product[7];
731 carry += vli_add(result, result, tmp, ndigits);
732
733
734 tmp[0] = (product[4] >> 32) | (product[5] << 32);
735 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
736 tmp[2] = product[7];
737 tmp[3] = (product[6] >> 32) | (product[4] << 32);
738 carry += vli_add(result, result, tmp, ndigits);
739
740
741 tmp[0] = (product[5] >> 32) | (product[6] << 32);
742 tmp[1] = (product[6] >> 32);
743 tmp[2] = 0;
744 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
745 carry -= vli_sub(result, result, tmp, ndigits);
746
747
748 tmp[0] = product[6];
749 tmp[1] = product[7];
750 tmp[2] = 0;
751 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
752 carry -= vli_sub(result, result, tmp, ndigits);
753
754
755 tmp[0] = (product[6] >> 32) | (product[7] << 32);
756 tmp[1] = (product[7] >> 32) | (product[4] << 32);
757 tmp[2] = (product[4] >> 32) | (product[5] << 32);
758 tmp[3] = (product[6] << 32);
759 carry -= vli_sub(result, result, tmp, ndigits);
760
761
762 tmp[0] = product[7];
763 tmp[1] = product[4] & 0xffffffff00000000ull;
764 tmp[2] = product[5];
765 tmp[3] = product[6] & 0xffffffff00000000ull;
766 carry -= vli_sub(result, result, tmp, ndigits);
767
768 if (carry < 0) {
769 do {
770 carry += vli_add(result, result, curve_prime, ndigits);
771 } while (carry < 0);
772 } else {
773 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
774 carry -= vli_sub(result, result, curve_prime, ndigits);
775 }
776}
777
778
779
780
781
782
783static bool vli_mmod_fast(u64 *result, u64 *product,
784 const u64 *curve_prime, unsigned int ndigits)
785{
786 u64 tmp[2 * ECC_MAX_DIGITS];
787
788
789 if (curve_prime[0] != -1ull) {
790
791 if (curve_prime[ndigits - 1] == -1ull) {
792 vli_mmod_special(result, product, curve_prime,
793 ndigits);
794 return true;
795 } else if (curve_prime[ndigits - 1] == 1ull << 63 &&
796 curve_prime[ndigits - 2] == 0) {
797 vli_mmod_special2(result, product, curve_prime,
798 ndigits);
799 return true;
800 }
801 vli_mmod_barrett(result, product, curve_prime, ndigits);
802 return true;
803 }
804
805 switch (ndigits) {
806 case 3:
807 vli_mmod_fast_192(result, product, curve_prime, tmp);
808 break;
809 case 4:
810 vli_mmod_fast_256(result, product, curve_prime, tmp);
811 break;
812 default:
813 pr_err_ratelimited("ecc: unsupported digits size!\n");
814 return false;
815 }
816
817 return true;
818}
819
820
821
822
823void vli_mod_mult_slow(u64 *result, const u64 *left, const u64 *right,
824 const u64 *mod, unsigned int ndigits)
825{
826 u64 product[ECC_MAX_DIGITS * 2];
827
828 vli_mult(product, left, right, ndigits);
829 vli_mmod_slow(result, product, mod, ndigits);
830}
831EXPORT_SYMBOL(vli_mod_mult_slow);
832
833
834static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
835 const u64 *curve_prime, unsigned int ndigits)
836{
837 u64 product[2 * ECC_MAX_DIGITS];
838
839 vli_mult(product, left, right, ndigits);
840 vli_mmod_fast(result, product, curve_prime, ndigits);
841}
842
843
844static void vli_mod_square_fast(u64 *result, const u64 *left,
845 const u64 *curve_prime, unsigned int ndigits)
846{
847 u64 product[2 * ECC_MAX_DIGITS];
848
849 vli_square(product, left, ndigits);
850 vli_mmod_fast(result, product, curve_prime, ndigits);
851}
852
853#define EVEN(vli) (!(vli[0] & 1))
854
855
856
857
858void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
859 unsigned int ndigits)
860{
861 u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
862 u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
863 u64 carry;
864 int cmp_result;
865
866 if (vli_is_zero(input, ndigits)) {
867 vli_clear(result, ndigits);
868 return;
869 }
870
871 vli_set(a, input, ndigits);
872 vli_set(b, mod, ndigits);
873 vli_clear(u, ndigits);
874 u[0] = 1;
875 vli_clear(v, ndigits);
876
877 while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
878 carry = 0;
879
880 if (EVEN(a)) {
881 vli_rshift1(a, ndigits);
882
883 if (!EVEN(u))
884 carry = vli_add(u, u, mod, ndigits);
885
886 vli_rshift1(u, ndigits);
887 if (carry)
888 u[ndigits - 1] |= 0x8000000000000000ull;
889 } else if (EVEN(b)) {
890 vli_rshift1(b, ndigits);
891
892 if (!EVEN(v))
893 carry = vli_add(v, v, mod, ndigits);
894
895 vli_rshift1(v, ndigits);
896 if (carry)
897 v[ndigits - 1] |= 0x8000000000000000ull;
898 } else if (cmp_result > 0) {
899 vli_sub(a, a, b, ndigits);
900 vli_rshift1(a, ndigits);
901
902 if (vli_cmp(u, v, ndigits) < 0)
903 vli_add(u, u, mod, ndigits);
904
905 vli_sub(u, u, v, ndigits);
906 if (!EVEN(u))
907 carry = vli_add(u, u, mod, ndigits);
908
909 vli_rshift1(u, ndigits);
910 if (carry)
911 u[ndigits - 1] |= 0x8000000000000000ull;
912 } else {
913 vli_sub(b, b, a, ndigits);
914 vli_rshift1(b, ndigits);
915
916 if (vli_cmp(v, u, ndigits) < 0)
917 vli_add(v, v, mod, ndigits);
918
919 vli_sub(v, v, u, ndigits);
920 if (!EVEN(v))
921 carry = vli_add(v, v, mod, ndigits);
922
923 vli_rshift1(v, ndigits);
924 if (carry)
925 v[ndigits - 1] |= 0x8000000000000000ull;
926 }
927 }
928
929 vli_set(result, u, ndigits);
930}
931EXPORT_SYMBOL(vli_mod_inv);
932
933
934
935
936static bool ecc_point_is_zero(const struct ecc_point *point)
937{
938 return (vli_is_zero(point->x, point->ndigits) &&
939 vli_is_zero(point->y, point->ndigits));
940}
941
942
943
944
945
946
947static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
948 u64 *curve_prime, unsigned int ndigits)
949{
950
951 u64 t4[ECC_MAX_DIGITS];
952 u64 t5[ECC_MAX_DIGITS];
953
954 if (vli_is_zero(z1, ndigits))
955 return;
956
957
958 vli_mod_square_fast(t4, y1, curve_prime, ndigits);
959
960 vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
961
962 vli_mod_square_fast(t4, t4, curve_prime, ndigits);
963
964 vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
965
966 vli_mod_square_fast(z1, z1, curve_prime, ndigits);
967
968
969 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
970
971 vli_mod_add(z1, z1, z1, curve_prime, ndigits);
972
973 vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
974
975 vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
976
977
978 vli_mod_add(z1, x1, x1, curve_prime, ndigits);
979
980 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
981 if (vli_test_bit(x1, 0)) {
982 u64 carry = vli_add(x1, x1, curve_prime, ndigits);
983
984 vli_rshift1(x1, ndigits);
985 x1[ndigits - 1] |= carry << 63;
986 } else {
987 vli_rshift1(x1, ndigits);
988 }
989
990
991
992 vli_mod_square_fast(z1, x1, curve_prime, ndigits);
993
994 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
995
996 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
997
998 vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
999
1000 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1001
1002 vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
1003
1004 vli_set(x1, z1, ndigits);
1005 vli_set(z1, y1, ndigits);
1006 vli_set(y1, t4, ndigits);
1007}
1008
1009
1010static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
1011 unsigned int ndigits)
1012{
1013 u64 t1[ECC_MAX_DIGITS];
1014
1015 vli_mod_square_fast(t1, z, curve_prime, ndigits);
1016 vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits);
1017 vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits);
1018 vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits);
1019}
1020
1021
1022static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
1023 u64 *p_initial_z, u64 *curve_prime,
1024 unsigned int ndigits)
1025{
1026 u64 z[ECC_MAX_DIGITS];
1027
1028 vli_set(x2, x1, ndigits);
1029 vli_set(y2, y1, ndigits);
1030
1031 vli_clear(z, ndigits);
1032 z[0] = 1;
1033
1034 if (p_initial_z)
1035 vli_set(z, p_initial_z, ndigits);
1036
1037 apply_z(x1, y1, z, curve_prime, ndigits);
1038
1039 ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
1040
1041 apply_z(x2, y2, z, curve_prime, ndigits);
1042}
1043
1044
1045
1046
1047
1048static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1049 unsigned int ndigits)
1050{
1051
1052 u64 t5[ECC_MAX_DIGITS];
1053
1054
1055 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1056
1057 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1058
1059 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1060
1061 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1062
1063 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1064
1065 vli_mod_square_fast(t5, y2, curve_prime, ndigits);
1066
1067
1068 vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
1069
1070 vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
1071
1072 vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
1073
1074 vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
1075
1076 vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
1077
1078 vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
1079
1080 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1081
1082 vli_set(x2, t5, ndigits);
1083}
1084
1085
1086
1087
1088
1089static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
1090 unsigned int ndigits)
1091{
1092
1093 u64 t5[ECC_MAX_DIGITS];
1094 u64 t6[ECC_MAX_DIGITS];
1095 u64 t7[ECC_MAX_DIGITS];
1096
1097
1098 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
1099
1100 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
1101
1102 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
1103
1104 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
1105
1106 vli_mod_add(t5, y2, y1, curve_prime, ndigits);
1107
1108 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1109
1110
1111 vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
1112
1113 vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
1114
1115 vli_mod_add(t6, x1, x2, curve_prime, ndigits);
1116
1117 vli_mod_square_fast(x2, y2, curve_prime, ndigits);
1118
1119 vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
1120
1121
1122 vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
1123
1124 vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
1125
1126 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
1127
1128
1129 vli_mod_square_fast(t7, t5, curve_prime, ndigits);
1130
1131 vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
1132
1133 vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
1134
1135 vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
1136
1137 vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
1138
1139 vli_set(x1, t7, ndigits);
1140}
1141
1142static void ecc_point_mult(struct ecc_point *result,
1143 const struct ecc_point *point, const u64 *scalar,
1144 u64 *initial_z, const struct ecc_curve *curve,
1145 unsigned int ndigits)
1146{
1147
1148 u64 rx[2][ECC_MAX_DIGITS];
1149 u64 ry[2][ECC_MAX_DIGITS];
1150 u64 z[ECC_MAX_DIGITS];
1151 u64 sk[2][ECC_MAX_DIGITS];
1152 u64 *curve_prime = curve->p;
1153 int i, nb;
1154 int num_bits;
1155 int carry;
1156
1157 carry = vli_add(sk[0], scalar, curve->n, ndigits);
1158 vli_add(sk[1], sk[0], curve->n, ndigits);
1159 scalar = sk[!carry];
1160 num_bits = sizeof(u64) * ndigits * 8 + 1;
1161
1162 vli_set(rx[1], point->x, ndigits);
1163 vli_set(ry[1], point->y, ndigits);
1164
1165 xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
1166 ndigits);
1167
1168 for (i = num_bits - 2; i > 0; i--) {
1169 nb = !vli_test_bit(scalar, i);
1170 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1171 ndigits);
1172 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
1173 ndigits);
1174 }
1175
1176 nb = !vli_test_bit(scalar, 0);
1177 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
1178 ndigits);
1179
1180
1181
1182 vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
1183
1184 vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
1185
1186 vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
1187
1188
1189 vli_mod_inv(z, z, curve_prime, point->ndigits);
1190
1191
1192 vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
1193
1194 vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
1195
1196
1197 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
1198
1199 apply_z(rx[0], ry[0], z, curve_prime, ndigits);
1200
1201 vli_set(result->x, rx[0], ndigits);
1202 vli_set(result->y, ry[0], ndigits);
1203}
1204
1205
1206static void ecc_point_add(const struct ecc_point *result,
1207 const struct ecc_point *p, const struct ecc_point *q,
1208 const struct ecc_curve *curve)
1209{
1210 u64 z[ECC_MAX_DIGITS];
1211 u64 px[ECC_MAX_DIGITS];
1212 u64 py[ECC_MAX_DIGITS];
1213 unsigned int ndigits = curve->g.ndigits;
1214
1215 vli_set(result->x, q->x, ndigits);
1216 vli_set(result->y, q->y, ndigits);
1217 vli_mod_sub(z, result->x, p->x, curve->p, ndigits);
1218 vli_set(px, p->x, ndigits);
1219 vli_set(py, p->y, ndigits);
1220 xycz_add(px, py, result->x, result->y, curve->p, ndigits);
1221 vli_mod_inv(z, z, curve->p, ndigits);
1222 apply_z(result->x, result->y, z, curve->p, ndigits);
1223}
1224
1225
1226
1227
1228void ecc_point_mult_shamir(const struct ecc_point *result,
1229 const u64 *u1, const struct ecc_point *p,
1230 const u64 *u2, const struct ecc_point *q,
1231 const struct ecc_curve *curve)
1232{
1233 u64 z[ECC_MAX_DIGITS];
1234 u64 sump[2][ECC_MAX_DIGITS];
1235 u64 *rx = result->x;
1236 u64 *ry = result->y;
1237 unsigned int ndigits = curve->g.ndigits;
1238 unsigned int num_bits;
1239 struct ecc_point sum = ECC_POINT_INIT(sump[0], sump[1], ndigits);
1240 const struct ecc_point *points[4];
1241 const struct ecc_point *point;
1242 unsigned int idx;
1243 int i;
1244
1245 ecc_point_add(&sum, p, q, curve);
1246 points[0] = NULL;
1247 points[1] = p;
1248 points[2] = q;
1249 points[3] = ∑
1250
1251 num_bits = max(vli_num_bits(u1, ndigits),
1252 vli_num_bits(u2, ndigits));
1253 i = num_bits - 1;
1254 idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1255 point = points[idx];
1256
1257 vli_set(rx, point->x, ndigits);
1258 vli_set(ry, point->y, ndigits);
1259 vli_clear(z + 1, ndigits - 1);
1260 z[0] = 1;
1261
1262 for (--i; i >= 0; i--) {
1263 ecc_point_double_jacobian(rx, ry, z, curve->p, ndigits);
1264 idx = (!!vli_test_bit(u1, i)) | ((!!vli_test_bit(u2, i)) << 1);
1265 point = points[idx];
1266 if (point) {
1267 u64 tx[ECC_MAX_DIGITS];
1268 u64 ty[ECC_MAX_DIGITS];
1269 u64 tz[ECC_MAX_DIGITS];
1270
1271 vli_set(tx, point->x, ndigits);
1272 vli_set(ty, point->y, ndigits);
1273 apply_z(tx, ty, z, curve->p, ndigits);
1274 vli_mod_sub(tz, rx, tx, curve->p, ndigits);
1275 xycz_add(tx, ty, rx, ry, curve->p, ndigits);
1276 vli_mod_mult_fast(z, z, tz, curve->p, ndigits);
1277 }
1278 }
1279 vli_mod_inv(z, z, curve->p, ndigits);
1280 apply_z(rx, ry, z, curve->p, ndigits);
1281}
1282EXPORT_SYMBOL(ecc_point_mult_shamir);
1283
1284static inline void ecc_swap_digits(const u64 *in, u64 *out,
1285 unsigned int ndigits)
1286{
1287 const __be64 *src = (__force __be64 *)in;
1288 int i;
1289
1290 for (i = 0; i < ndigits; i++)
1291 out[i] = be64_to_cpu(src[ndigits - 1 - i]);
1292}
1293
1294static int __ecc_is_key_valid(const struct ecc_curve *curve,
1295 const u64 *private_key, unsigned int ndigits)
1296{
1297 u64 one[ECC_MAX_DIGITS] = { 1, };
1298 u64 res[ECC_MAX_DIGITS];
1299
1300 if (!private_key)
1301 return -EINVAL;
1302
1303 if (curve->g.ndigits != ndigits)
1304 return -EINVAL;
1305
1306
1307 if (vli_cmp(one, private_key, ndigits) != -1)
1308 return -EINVAL;
1309 vli_sub(res, curve->n, one, ndigits);
1310 vli_sub(res, res, one, ndigits);
1311 if (vli_cmp(res, private_key, ndigits) != 1)
1312 return -EINVAL;
1313
1314 return 0;
1315}
1316
1317int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
1318 const u64 *private_key, unsigned int private_key_len)
1319{
1320 int nbytes;
1321 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1322
1323 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1324
1325 if (private_key_len != nbytes)
1326 return -EINVAL;
1327
1328 return __ecc_is_key_valid(curve, private_key, ndigits);
1329}
1330EXPORT_SYMBOL(ecc_is_key_valid);
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
1345{
1346 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1347 u64 priv[ECC_MAX_DIGITS];
1348 unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1349 unsigned int nbits = vli_num_bits(curve->n, ndigits);
1350 int err;
1351
1352
1353 if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
1354 return -EINVAL;
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367 if (crypto_get_default_rng())
1368 return -EFAULT;
1369
1370 err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
1371 crypto_put_default_rng();
1372 if (err)
1373 return err;
1374
1375
1376 if (__ecc_is_key_valid(curve, priv, ndigits))
1377 return -EINVAL;
1378
1379 ecc_swap_digits(priv, privkey, ndigits);
1380
1381 return 0;
1382}
1383EXPORT_SYMBOL(ecc_gen_privkey);
1384
1385int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1386 const u64 *private_key, u64 *public_key)
1387{
1388 int ret = 0;
1389 struct ecc_point *pk;
1390 u64 priv[ECC_MAX_DIGITS];
1391 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1392
1393 if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
1394 ret = -EINVAL;
1395 goto out;
1396 }
1397
1398 ecc_swap_digits(private_key, priv, ndigits);
1399
1400 pk = ecc_alloc_point(ndigits);
1401 if (!pk) {
1402 ret = -ENOMEM;
1403 goto out;
1404 }
1405
1406 ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
1407
1408
1409 if (ecc_is_pubkey_valid_full(curve, pk)) {
1410 ret = -EAGAIN;
1411 goto err_free_point;
1412 }
1413
1414 ecc_swap_digits(pk->x, public_key, ndigits);
1415 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
1416
1417err_free_point:
1418 ecc_free_point(pk);
1419out:
1420 return ret;
1421}
1422EXPORT_SYMBOL(ecc_make_pub_key);
1423
1424
1425int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1426 struct ecc_point *pk)
1427{
1428 u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1429
1430 if (WARN_ON(pk->ndigits != curve->g.ndigits))
1431 return -EINVAL;
1432
1433
1434 if (ecc_point_is_zero(pk))
1435 return -EINVAL;
1436
1437
1438 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1439 return -EINVAL;
1440 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1441 return -EINVAL;
1442
1443
1444 vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits);
1445 vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits);
1446 vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits);
1447 vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits);
1448 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits);
1449 vli_mod_add(w, w, xxx, curve->p, pk->ndigits);
1450 if (vli_cmp(yy, w, pk->ndigits) != 0)
1451 return -EINVAL;
1452
1453 return 0;
1454}
1455EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
1456
1457
1458int ecc_is_pubkey_valid_full(const struct ecc_curve *curve,
1459 struct ecc_point *pk)
1460{
1461 struct ecc_point *nQ;
1462
1463
1464 int ret = ecc_is_pubkey_valid_partial(curve, pk);
1465
1466 if (ret)
1467 return ret;
1468
1469
1470 nQ = ecc_alloc_point(pk->ndigits);
1471 if (!nQ)
1472 return -ENOMEM;
1473
1474 ecc_point_mult(nQ, pk, curve->n, NULL, curve, pk->ndigits);
1475 if (!ecc_point_is_zero(nQ))
1476 ret = -EINVAL;
1477
1478 ecc_free_point(nQ);
1479
1480 return ret;
1481}
1482EXPORT_SYMBOL(ecc_is_pubkey_valid_full);
1483
1484int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
1485 const u64 *private_key, const u64 *public_key,
1486 u64 *secret)
1487{
1488 int ret = 0;
1489 struct ecc_point *product, *pk;
1490 u64 priv[ECC_MAX_DIGITS];
1491 u64 rand_z[ECC_MAX_DIGITS];
1492 unsigned int nbytes;
1493 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1494
1495 if (!private_key || !public_key || !curve ||
1496 ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
1497 ret = -EINVAL;
1498 goto out;
1499 }
1500
1501 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
1502
1503 get_random_bytes(rand_z, nbytes);
1504
1505 pk = ecc_alloc_point(ndigits);
1506 if (!pk) {
1507 ret = -ENOMEM;
1508 goto out;
1509 }
1510
1511 ecc_swap_digits(public_key, pk->x, ndigits);
1512 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1513 ret = ecc_is_pubkey_valid_partial(curve, pk);
1514 if (ret)
1515 goto err_alloc_product;
1516
1517 ecc_swap_digits(private_key, priv, ndigits);
1518
1519 product = ecc_alloc_point(ndigits);
1520 if (!product) {
1521 ret = -ENOMEM;
1522 goto err_alloc_product;
1523 }
1524
1525 ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
1526
1527 if (ecc_point_is_zero(product)) {
1528 ret = -EFAULT;
1529 goto err_validity;
1530 }
1531
1532 ecc_swap_digits(product->x, secret, ndigits);
1533
1534err_validity:
1535 memzero_explicit(priv, sizeof(priv));
1536 memzero_explicit(rand_z, sizeof(rand_z));
1537 ecc_free_point(product);
1538err_alloc_product:
1539 ecc_free_point(pk);
1540out:
1541 return ret;
1542}
1543EXPORT_SYMBOL(crypto_ecdh_shared_secret);
1544
1545MODULE_LICENSE("Dual BSD/GPL");
1546