]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - crypto/ecc.c
crypto: ecc - make ecc into separate module
[mirror_ubuntu-jammy-kernel.git] / crypto / ecc.c
CommitLineData
3c4b2390
SB
1/*
2 * Copyright (c) 2013, Kenneth MacKay
3 * All rights reserved.
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
4a2289da 27#include <linux/module.h>
3c4b2390
SB
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>
6755fd26 33#include <crypto/rng.h>
3c4b2390
SB
34
35#include "ecc.h"
36#include "ecc_curve_defs.h"
37
38typedef struct {
39 u64 m_low;
40 u64 m_high;
41} uint128_t;
42
43static inline const struct ecc_curve *ecc_get_curve(unsigned int curve_id)
44{
45 switch (curve_id) {
46 /* In FIPS mode only allow P256 and higher */
47 case ECC_CURVE_NIST_P192:
48 return fips_enabled ? NULL : &nist_p192;
49 case ECC_CURVE_NIST_P256:
50 return &nist_p256;
51 default:
52 return NULL;
53 }
54}
55
56static u64 *ecc_alloc_digits_space(unsigned int ndigits)
57{
58 size_t len = ndigits * sizeof(u64);
59
60 if (!len)
61 return NULL;
62
63 return kmalloc(len, GFP_KERNEL);
64}
65
66static void ecc_free_digits_space(u64 *space)
67{
68 kzfree(space);
69}
70
71static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
72{
73 struct ecc_point *p = kmalloc(sizeof(*p), GFP_KERNEL);
74
75 if (!p)
76 return NULL;
77
78 p->x = ecc_alloc_digits_space(ndigits);
79 if (!p->x)
80 goto err_alloc_x;
81
82 p->y = ecc_alloc_digits_space(ndigits);
83 if (!p->y)
84 goto err_alloc_y;
85
86 p->ndigits = ndigits;
87
88 return p;
89
90err_alloc_y:
91 ecc_free_digits_space(p->x);
92err_alloc_x:
93 kfree(p);
94 return NULL;
95}
96
97static void ecc_free_point(struct ecc_point *p)
98{
99 if (!p)
100 return;
101
102 kzfree(p->x);
103 kzfree(p->y);
104 kzfree(p);
105}
106
107static void vli_clear(u64 *vli, unsigned int ndigits)
108{
109 int i;
110
111 for (i = 0; i < ndigits; i++)
112 vli[i] = 0;
113}
114
115/* Returns true if vli == 0, false otherwise. */
4a2289da 116bool vli_is_zero(const u64 *vli, unsigned int ndigits)
3c4b2390
SB
117{
118 int i;
119
120 for (i = 0; i < ndigits; i++) {
121 if (vli[i])
122 return false;
123 }
124
125 return true;
126}
4a2289da 127EXPORT_SYMBOL(vli_is_zero);
3c4b2390
SB
128
129/* Returns nonzero if bit bit of vli is set. */
130static u64 vli_test_bit(const u64 *vli, unsigned int bit)
131{
132 return (vli[bit / 64] & ((u64)1 << (bit % 64)));
133}
134
135/* Counts the number of 64-bit "digits" in vli. */
136static unsigned int vli_num_digits(const u64 *vli, unsigned int ndigits)
137{
138 int i;
139
140 /* Search from the end until we find a non-zero digit.
141 * We do it in reverse because we expect that most digits will
142 * be nonzero.
143 */
144 for (i = ndigits - 1; i >= 0 && vli[i] == 0; i--);
145
146 return (i + 1);
147}
148
149/* Counts the number of bits required for vli. */
150static unsigned int vli_num_bits(const u64 *vli, unsigned int ndigits)
151{
152 unsigned int i, num_digits;
153 u64 digit;
154
155 num_digits = vli_num_digits(vli, ndigits);
156 if (num_digits == 0)
157 return 0;
158
159 digit = vli[num_digits - 1];
160 for (i = 0; digit; i++)
161 digit >>= 1;
162
163 return ((num_digits - 1) * 64 + i);
164}
165
166/* Sets dest = src. */
167static void vli_set(u64 *dest, const u64 *src, unsigned int ndigits)
168{
169 int i;
170
171 for (i = 0; i < ndigits; i++)
172 dest[i] = src[i];
173}
174
175/* Returns sign of left - right. */
4a2289da 176int vli_cmp(const u64 *left, const u64 *right, unsigned int ndigits)
3c4b2390
SB
177{
178 int i;
179
180 for (i = ndigits - 1; i >= 0; i--) {
181 if (left[i] > right[i])
182 return 1;
183 else if (left[i] < right[i])
184 return -1;
185 }
186
187 return 0;
188}
4a2289da 189EXPORT_SYMBOL(vli_cmp);
3c4b2390
SB
190
191/* Computes result = in << c, returning carry. Can modify in place
192 * (if result == in). 0 < shift < 64.
193 */
194static u64 vli_lshift(u64 *result, const u64 *in, unsigned int shift,
195 unsigned int ndigits)
196{
197 u64 carry = 0;
198 int i;
199
200 for (i = 0; i < ndigits; i++) {
201 u64 temp = in[i];
202
203 result[i] = (temp << shift) | carry;
204 carry = temp >> (64 - shift);
205 }
206
207 return carry;
208}
209
210/* Computes vli = vli >> 1. */
211static void vli_rshift1(u64 *vli, unsigned int ndigits)
212{
213 u64 *end = vli;
214 u64 carry = 0;
215
216 vli += ndigits;
217
218 while (vli-- > end) {
219 u64 temp = *vli;
220 *vli = (temp >> 1) | carry;
221 carry = temp << 63;
222 }
223}
224
225/* Computes result = left + right, returning carry. Can modify in place. */
226static u64 vli_add(u64 *result, const u64 *left, const u64 *right,
227 unsigned int ndigits)
228{
229 u64 carry = 0;
230 int i;
231
232 for (i = 0; i < ndigits; i++) {
233 u64 sum;
234
235 sum = left[i] + right[i] + carry;
236 if (sum != left[i])
237 carry = (sum < left[i]);
238
239 result[i] = sum;
240 }
241
242 return carry;
243}
244
245/* Computes result = left - right, returning borrow. Can modify in place. */
4a2289da 246u64 vli_sub(u64 *result, const u64 *left, const u64 *right,
3c4b2390
SB
247 unsigned int ndigits)
248{
249 u64 borrow = 0;
250 int i;
251
252 for (i = 0; i < ndigits; i++) {
253 u64 diff;
254
255 diff = left[i] - right[i] - borrow;
256 if (diff != left[i])
257 borrow = (diff > left[i]);
258
259 result[i] = diff;
260 }
261
262 return borrow;
263}
4a2289da 264EXPORT_SYMBOL(vli_sub);
3c4b2390
SB
265
266static uint128_t mul_64_64(u64 left, u64 right)
267{
268 u64 a0 = left & 0xffffffffull;
269 u64 a1 = left >> 32;
270 u64 b0 = right & 0xffffffffull;
271 u64 b1 = right >> 32;
272 u64 m0 = a0 * b0;
273 u64 m1 = a0 * b1;
274 u64 m2 = a1 * b0;
275 u64 m3 = a1 * b1;
276 uint128_t result;
277
278 m2 += (m0 >> 32);
279 m2 += m1;
280
281 /* Overflow */
282 if (m2 < m1)
283 m3 += 0x100000000ull;
284
285 result.m_low = (m0 & 0xffffffffull) | (m2 << 32);
286 result.m_high = m3 + (m2 >> 32);
287
288 return result;
289}
290
291static uint128_t add_128_128(uint128_t a, uint128_t b)
292{
293 uint128_t result;
294
295 result.m_low = a.m_low + b.m_low;
296 result.m_high = a.m_high + b.m_high + (result.m_low < a.m_low);
297
298 return result;
299}
300
301static void vli_mult(u64 *result, const u64 *left, const u64 *right,
302 unsigned int ndigits)
303{
304 uint128_t r01 = { 0, 0 };
305 u64 r2 = 0;
306 unsigned int i, k;
307
308 /* Compute each digit of result in sequence, maintaining the
309 * carries.
310 */
311 for (k = 0; k < ndigits * 2 - 1; k++) {
312 unsigned int min;
313
314 if (k < ndigits)
315 min = 0;
316 else
317 min = (k + 1) - ndigits;
318
319 for (i = min; i <= k && i < ndigits; i++) {
320 uint128_t product;
321
322 product = mul_64_64(left[i], right[k - i]);
323
324 r01 = add_128_128(r01, product);
325 r2 += (r01.m_high < product.m_high);
326 }
327
328 result[k] = r01.m_low;
329 r01.m_low = r01.m_high;
330 r01.m_high = r2;
331 r2 = 0;
332 }
333
334 result[ndigits * 2 - 1] = r01.m_low;
335}
336
337static void vli_square(u64 *result, const u64 *left, unsigned int ndigits)
338{
339 uint128_t r01 = { 0, 0 };
340 u64 r2 = 0;
341 int i, k;
342
343 for (k = 0; k < ndigits * 2 - 1; k++) {
344 unsigned int min;
345
346 if (k < ndigits)
347 min = 0;
348 else
349 min = (k + 1) - ndigits;
350
351 for (i = min; i <= k && i <= k - i; i++) {
352 uint128_t product;
353
354 product = mul_64_64(left[i], left[k - i]);
355
356 if (i < k - i) {
357 r2 += product.m_high >> 63;
358 product.m_high = (product.m_high << 1) |
359 (product.m_low >> 63);
360 product.m_low <<= 1;
361 }
362
363 r01 = add_128_128(r01, product);
364 r2 += (r01.m_high < product.m_high);
365 }
366
367 result[k] = r01.m_low;
368 r01.m_low = r01.m_high;
369 r01.m_high = r2;
370 r2 = 0;
371 }
372
373 result[ndigits * 2 - 1] = r01.m_low;
374}
375
376/* Computes result = (left + right) % mod.
377 * Assumes that left < mod and right < mod, result != mod.
378 */
379static void vli_mod_add(u64 *result, const u64 *left, const u64 *right,
380 const u64 *mod, unsigned int ndigits)
381{
382 u64 carry;
383
384 carry = vli_add(result, left, right, ndigits);
385
386 /* result > mod (result = mod + remainder), so subtract mod to
387 * get remainder.
388 */
389 if (carry || vli_cmp(result, mod, ndigits) >= 0)
390 vli_sub(result, result, mod, ndigits);
391}
392
393/* Computes result = (left - right) % mod.
394 * Assumes that left < mod and right < mod, result != mod.
395 */
396static void vli_mod_sub(u64 *result, const u64 *left, const u64 *right,
397 const u64 *mod, unsigned int ndigits)
398{
399 u64 borrow = vli_sub(result, left, right, ndigits);
400
401 /* In this case, p_result == -diff == (max int) - diff.
402 * Since -x % d == d - x, we can get the correct result from
403 * result + mod (with overflow).
404 */
405 if (borrow)
406 vli_add(result, result, mod, ndigits);
407}
408
409/* Computes p_result = p_product % curve_p.
410 * See algorithm 5 and 6 from
411 * http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf
412 */
413static void vli_mmod_fast_192(u64 *result, const u64 *product,
414 const u64 *curve_prime, u64 *tmp)
415{
416 const unsigned int ndigits = 3;
417 int carry;
418
419 vli_set(result, product, ndigits);
420
421 vli_set(tmp, &product[3], ndigits);
422 carry = vli_add(result, result, tmp, ndigits);
423
424 tmp[0] = 0;
425 tmp[1] = product[3];
426 tmp[2] = product[4];
427 carry += vli_add(result, result, tmp, ndigits);
428
429 tmp[0] = tmp[1] = product[5];
430 tmp[2] = 0;
431 carry += vli_add(result, result, tmp, ndigits);
432
433 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
434 carry -= vli_sub(result, result, curve_prime, ndigits);
435}
436
437/* Computes result = product % curve_prime
438 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
439 */
440static void vli_mmod_fast_256(u64 *result, const u64 *product,
441 const u64 *curve_prime, u64 *tmp)
442{
443 int carry;
444 const unsigned int ndigits = 4;
445
446 /* t */
447 vli_set(result, product, ndigits);
448
449 /* s1 */
450 tmp[0] = 0;
451 tmp[1] = product[5] & 0xffffffff00000000ull;
452 tmp[2] = product[6];
453 tmp[3] = product[7];
454 carry = vli_lshift(tmp, tmp, 1, ndigits);
455 carry += vli_add(result, result, tmp, ndigits);
456
457 /* s2 */
458 tmp[1] = product[6] << 32;
459 tmp[2] = (product[6] >> 32) | (product[7] << 32);
460 tmp[3] = product[7] >> 32;
461 carry += vli_lshift(tmp, tmp, 1, ndigits);
462 carry += vli_add(result, result, tmp, ndigits);
463
464 /* s3 */
465 tmp[0] = product[4];
466 tmp[1] = product[5] & 0xffffffff;
467 tmp[2] = 0;
468 tmp[3] = product[7];
469 carry += vli_add(result, result, tmp, ndigits);
470
471 /* s4 */
472 tmp[0] = (product[4] >> 32) | (product[5] << 32);
473 tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull);
474 tmp[2] = product[7];
475 tmp[3] = (product[6] >> 32) | (product[4] << 32);
476 carry += vli_add(result, result, tmp, ndigits);
477
478 /* d1 */
479 tmp[0] = (product[5] >> 32) | (product[6] << 32);
480 tmp[1] = (product[6] >> 32);
481 tmp[2] = 0;
482 tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32);
483 carry -= vli_sub(result, result, tmp, ndigits);
484
485 /* d2 */
486 tmp[0] = product[6];
487 tmp[1] = product[7];
488 tmp[2] = 0;
489 tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull);
490 carry -= vli_sub(result, result, tmp, ndigits);
491
492 /* d3 */
493 tmp[0] = (product[6] >> 32) | (product[7] << 32);
494 tmp[1] = (product[7] >> 32) | (product[4] << 32);
495 tmp[2] = (product[4] >> 32) | (product[5] << 32);
496 tmp[3] = (product[6] << 32);
497 carry -= vli_sub(result, result, tmp, ndigits);
498
499 /* d4 */
500 tmp[0] = product[7];
501 tmp[1] = product[4] & 0xffffffff00000000ull;
502 tmp[2] = product[5];
503 tmp[3] = product[6] & 0xffffffff00000000ull;
504 carry -= vli_sub(result, result, tmp, ndigits);
505
506 if (carry < 0) {
507 do {
508 carry += vli_add(result, result, curve_prime, ndigits);
509 } while (carry < 0);
510 } else {
511 while (carry || vli_cmp(curve_prime, result, ndigits) != 1)
512 carry -= vli_sub(result, result, curve_prime, ndigits);
513 }
514}
515
516/* Computes result = product % curve_prime
517 * from http://www.nsa.gov/ia/_files/nist-routines.pdf
518*/
519static bool vli_mmod_fast(u64 *result, u64 *product,
520 const u64 *curve_prime, unsigned int ndigits)
521{
d5c3b178 522 u64 tmp[2 * ECC_MAX_DIGITS];
3c4b2390
SB
523
524 switch (ndigits) {
525 case 3:
526 vli_mmod_fast_192(result, product, curve_prime, tmp);
527 break;
528 case 4:
529 vli_mmod_fast_256(result, product, curve_prime, tmp);
530 break;
531 default:
532 pr_err("unsupports digits size!\n");
533 return false;
534 }
535
536 return true;
537}
538
539/* Computes result = (left * right) % curve_prime. */
540static void vli_mod_mult_fast(u64 *result, const u64 *left, const u64 *right,
541 const u64 *curve_prime, unsigned int ndigits)
542{
d5c3b178 543 u64 product[2 * ECC_MAX_DIGITS];
3c4b2390
SB
544
545 vli_mult(product, left, right, ndigits);
546 vli_mmod_fast(result, product, curve_prime, ndigits);
547}
548
549/* Computes result = left^2 % curve_prime. */
550static void vli_mod_square_fast(u64 *result, const u64 *left,
551 const u64 *curve_prime, unsigned int ndigits)
552{
d5c3b178 553 u64 product[2 * ECC_MAX_DIGITS];
3c4b2390
SB
554
555 vli_square(product, left, ndigits);
556 vli_mmod_fast(result, product, curve_prime, ndigits);
557}
558
559#define EVEN(vli) (!(vli[0] & 1))
560/* Computes result = (1 / p_input) % mod. All VLIs are the same size.
561 * See "From Euclid's GCD to Montgomery Multiplication to the Great Divide"
562 * https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf
563 */
4a2289da 564void vli_mod_inv(u64 *result, const u64 *input, const u64 *mod,
3c4b2390
SB
565 unsigned int ndigits)
566{
d5c3b178
KC
567 u64 a[ECC_MAX_DIGITS], b[ECC_MAX_DIGITS];
568 u64 u[ECC_MAX_DIGITS], v[ECC_MAX_DIGITS];
3c4b2390
SB
569 u64 carry;
570 int cmp_result;
571
572 if (vli_is_zero(input, ndigits)) {
573 vli_clear(result, ndigits);
574 return;
575 }
576
577 vli_set(a, input, ndigits);
578 vli_set(b, mod, ndigits);
579 vli_clear(u, ndigits);
580 u[0] = 1;
581 vli_clear(v, ndigits);
582
583 while ((cmp_result = vli_cmp(a, b, ndigits)) != 0) {
584 carry = 0;
585
586 if (EVEN(a)) {
587 vli_rshift1(a, ndigits);
588
589 if (!EVEN(u))
590 carry = vli_add(u, u, mod, ndigits);
591
592 vli_rshift1(u, ndigits);
593 if (carry)
594 u[ndigits - 1] |= 0x8000000000000000ull;
595 } else if (EVEN(b)) {
596 vli_rshift1(b, ndigits);
597
598 if (!EVEN(v))
599 carry = vli_add(v, v, mod, ndigits);
600
601 vli_rshift1(v, ndigits);
602 if (carry)
603 v[ndigits - 1] |= 0x8000000000000000ull;
604 } else if (cmp_result > 0) {
605 vli_sub(a, a, b, ndigits);
606 vli_rshift1(a, ndigits);
607
608 if (vli_cmp(u, v, ndigits) < 0)
609 vli_add(u, u, mod, ndigits);
610
611 vli_sub(u, u, v, ndigits);
612 if (!EVEN(u))
613 carry = vli_add(u, u, mod, ndigits);
614
615 vli_rshift1(u, ndigits);
616 if (carry)
617 u[ndigits - 1] |= 0x8000000000000000ull;
618 } else {
619 vli_sub(b, b, a, ndigits);
620 vli_rshift1(b, ndigits);
621
622 if (vli_cmp(v, u, ndigits) < 0)
623 vli_add(v, v, mod, ndigits);
624
625 vli_sub(v, v, u, ndigits);
626 if (!EVEN(v))
627 carry = vli_add(v, v, mod, ndigits);
628
629 vli_rshift1(v, ndigits);
630 if (carry)
631 v[ndigits - 1] |= 0x8000000000000000ull;
632 }
633 }
634
635 vli_set(result, u, ndigits);
636}
4a2289da 637EXPORT_SYMBOL(vli_mod_inv);
3c4b2390
SB
638
639/* ------ Point operations ------ */
640
641/* Returns true if p_point is the point at infinity, false otherwise. */
642static bool ecc_point_is_zero(const struct ecc_point *point)
643{
644 return (vli_is_zero(point->x, point->ndigits) &&
645 vli_is_zero(point->y, point->ndigits));
646}
647
648/* Point multiplication algorithm using Montgomery's ladder with co-Z
649 * coordinates. From http://eprint.iacr.org/2011/338.pdf
650 */
651
652/* Double in place */
653static void ecc_point_double_jacobian(u64 *x1, u64 *y1, u64 *z1,
654 u64 *curve_prime, unsigned int ndigits)
655{
656 /* t1 = x, t2 = y, t3 = z */
d5c3b178
KC
657 u64 t4[ECC_MAX_DIGITS];
658 u64 t5[ECC_MAX_DIGITS];
3c4b2390
SB
659
660 if (vli_is_zero(z1, ndigits))
661 return;
662
663 /* t4 = y1^2 */
664 vli_mod_square_fast(t4, y1, curve_prime, ndigits);
665 /* t5 = x1*y1^2 = A */
666 vli_mod_mult_fast(t5, x1, t4, curve_prime, ndigits);
667 /* t4 = y1^4 */
668 vli_mod_square_fast(t4, t4, curve_prime, ndigits);
669 /* t2 = y1*z1 = z3 */
670 vli_mod_mult_fast(y1, y1, z1, curve_prime, ndigits);
671 /* t3 = z1^2 */
672 vli_mod_square_fast(z1, z1, curve_prime, ndigits);
673
674 /* t1 = x1 + z1^2 */
675 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
676 /* t3 = 2*z1^2 */
677 vli_mod_add(z1, z1, z1, curve_prime, ndigits);
678 /* t3 = x1 - z1^2 */
679 vli_mod_sub(z1, x1, z1, curve_prime, ndigits);
680 /* t1 = x1^2 - z1^4 */
681 vli_mod_mult_fast(x1, x1, z1, curve_prime, ndigits);
682
683 /* t3 = 2*(x1^2 - z1^4) */
684 vli_mod_add(z1, x1, x1, curve_prime, ndigits);
685 /* t1 = 3*(x1^2 - z1^4) */
686 vli_mod_add(x1, x1, z1, curve_prime, ndigits);
687 if (vli_test_bit(x1, 0)) {
688 u64 carry = vli_add(x1, x1, curve_prime, ndigits);
689
690 vli_rshift1(x1, ndigits);
691 x1[ndigits - 1] |= carry << 63;
692 } else {
693 vli_rshift1(x1, ndigits);
694 }
695 /* t1 = 3/2*(x1^2 - z1^4) = B */
696
697 /* t3 = B^2 */
698 vli_mod_square_fast(z1, x1, curve_prime, ndigits);
699 /* t3 = B^2 - A */
700 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
701 /* t3 = B^2 - 2A = x3 */
702 vli_mod_sub(z1, z1, t5, curve_prime, ndigits);
703 /* t5 = A - x3 */
704 vli_mod_sub(t5, t5, z1, curve_prime, ndigits);
705 /* t1 = B * (A - x3) */
706 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
707 /* t4 = B * (A - x3) - y1^4 = y3 */
708 vli_mod_sub(t4, x1, t4, curve_prime, ndigits);
709
710 vli_set(x1, z1, ndigits);
711 vli_set(z1, y1, ndigits);
712 vli_set(y1, t4, ndigits);
713}
714
715/* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */
716static void apply_z(u64 *x1, u64 *y1, u64 *z, u64 *curve_prime,
717 unsigned int ndigits)
718{
d5c3b178 719 u64 t1[ECC_MAX_DIGITS];
3c4b2390
SB
720
721 vli_mod_square_fast(t1, z, curve_prime, ndigits); /* z^2 */
722 vli_mod_mult_fast(x1, x1, t1, curve_prime, ndigits); /* x1 * z^2 */
723 vli_mod_mult_fast(t1, t1, z, curve_prime, ndigits); /* z^3 */
724 vli_mod_mult_fast(y1, y1, t1, curve_prime, ndigits); /* y1 * z^3 */
725}
726
727/* P = (x1, y1) => 2P, (x2, y2) => P' */
728static void xycz_initial_double(u64 *x1, u64 *y1, u64 *x2, u64 *y2,
729 u64 *p_initial_z, u64 *curve_prime,
730 unsigned int ndigits)
731{
d5c3b178 732 u64 z[ECC_MAX_DIGITS];
3c4b2390
SB
733
734 vli_set(x2, x1, ndigits);
735 vli_set(y2, y1, ndigits);
736
737 vli_clear(z, ndigits);
738 z[0] = 1;
739
740 if (p_initial_z)
741 vli_set(z, p_initial_z, ndigits);
742
743 apply_z(x1, y1, z, curve_prime, ndigits);
744
745 ecc_point_double_jacobian(x1, y1, z, curve_prime, ndigits);
746
747 apply_z(x2, y2, z, curve_prime, ndigits);
748}
749
750/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
751 * Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3)
752 * or P => P', Q => P + Q
753 */
754static void xycz_add(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
755 unsigned int ndigits)
756{
757 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
d5c3b178 758 u64 t5[ECC_MAX_DIGITS];
3c4b2390
SB
759
760 /* t5 = x2 - x1 */
761 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
762 /* t5 = (x2 - x1)^2 = A */
763 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
764 /* t1 = x1*A = B */
765 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
766 /* t3 = x2*A = C */
767 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
768 /* t4 = y2 - y1 */
769 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
770 /* t5 = (y2 - y1)^2 = D */
771 vli_mod_square_fast(t5, y2, curve_prime, ndigits);
772
773 /* t5 = D - B */
774 vli_mod_sub(t5, t5, x1, curve_prime, ndigits);
775 /* t5 = D - B - C = x3 */
776 vli_mod_sub(t5, t5, x2, curve_prime, ndigits);
777 /* t3 = C - B */
778 vli_mod_sub(x2, x2, x1, curve_prime, ndigits);
779 /* t2 = y1*(C - B) */
780 vli_mod_mult_fast(y1, y1, x2, curve_prime, ndigits);
781 /* t3 = B - x3 */
782 vli_mod_sub(x2, x1, t5, curve_prime, ndigits);
783 /* t4 = (y2 - y1)*(B - x3) */
784 vli_mod_mult_fast(y2, y2, x2, curve_prime, ndigits);
785 /* t4 = y3 */
786 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
787
788 vli_set(x2, t5, ndigits);
789}
790
791/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
792 * Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
793 * or P => P - Q, Q => P + Q
794 */
795static void xycz_add_c(u64 *x1, u64 *y1, u64 *x2, u64 *y2, u64 *curve_prime,
796 unsigned int ndigits)
797{
798 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
d5c3b178
KC
799 u64 t5[ECC_MAX_DIGITS];
800 u64 t6[ECC_MAX_DIGITS];
801 u64 t7[ECC_MAX_DIGITS];
3c4b2390
SB
802
803 /* t5 = x2 - x1 */
804 vli_mod_sub(t5, x2, x1, curve_prime, ndigits);
805 /* t5 = (x2 - x1)^2 = A */
806 vli_mod_square_fast(t5, t5, curve_prime, ndigits);
807 /* t1 = x1*A = B */
808 vli_mod_mult_fast(x1, x1, t5, curve_prime, ndigits);
809 /* t3 = x2*A = C */
810 vli_mod_mult_fast(x2, x2, t5, curve_prime, ndigits);
811 /* t4 = y2 + y1 */
812 vli_mod_add(t5, y2, y1, curve_prime, ndigits);
813 /* t4 = y2 - y1 */
814 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
815
816 /* t6 = C - B */
817 vli_mod_sub(t6, x2, x1, curve_prime, ndigits);
818 /* t2 = y1 * (C - B) */
819 vli_mod_mult_fast(y1, y1, t6, curve_prime, ndigits);
820 /* t6 = B + C */
821 vli_mod_add(t6, x1, x2, curve_prime, ndigits);
822 /* t3 = (y2 - y1)^2 */
823 vli_mod_square_fast(x2, y2, curve_prime, ndigits);
824 /* t3 = x3 */
825 vli_mod_sub(x2, x2, t6, curve_prime, ndigits);
826
827 /* t7 = B - x3 */
828 vli_mod_sub(t7, x1, x2, curve_prime, ndigits);
829 /* t4 = (y2 - y1)*(B - x3) */
830 vli_mod_mult_fast(y2, y2, t7, curve_prime, ndigits);
831 /* t4 = y3 */
832 vli_mod_sub(y2, y2, y1, curve_prime, ndigits);
833
834 /* t7 = (y2 + y1)^2 = F */
835 vli_mod_square_fast(t7, t5, curve_prime, ndigits);
836 /* t7 = x3' */
837 vli_mod_sub(t7, t7, t6, curve_prime, ndigits);
838 /* t6 = x3' - B */
839 vli_mod_sub(t6, t7, x1, curve_prime, ndigits);
840 /* t6 = (y2 + y1)*(x3' - B) */
841 vli_mod_mult_fast(t6, t6, t5, curve_prime, ndigits);
842 /* t2 = y3' */
843 vli_mod_sub(y1, t6, y1, curve_prime, ndigits);
844
845 vli_set(x1, t7, ndigits);
846}
847
848static void ecc_point_mult(struct ecc_point *result,
849 const struct ecc_point *point, const u64 *scalar,
3da2c1df 850 u64 *initial_z, const struct ecc_curve *curve,
3c4b2390
SB
851 unsigned int ndigits)
852{
853 /* R0 and R1 */
d5c3b178
KC
854 u64 rx[2][ECC_MAX_DIGITS];
855 u64 ry[2][ECC_MAX_DIGITS];
856 u64 z[ECC_MAX_DIGITS];
3da2c1df
VC
857 u64 sk[2][ECC_MAX_DIGITS];
858 u64 *curve_prime = curve->p;
3c4b2390 859 int i, nb;
3da2c1df
VC
860 int num_bits;
861 int carry;
862
863 carry = vli_add(sk[0], scalar, curve->n, ndigits);
864 vli_add(sk[1], sk[0], curve->n, ndigits);
865 scalar = sk[!carry];
866 num_bits = sizeof(u64) * ndigits * 8 + 1;
3c4b2390
SB
867
868 vli_set(rx[1], point->x, ndigits);
869 vli_set(ry[1], point->y, ndigits);
870
871 xycz_initial_double(rx[1], ry[1], rx[0], ry[0], initial_z, curve_prime,
872 ndigits);
873
874 for (i = num_bits - 2; i > 0; i--) {
875 nb = !vli_test_bit(scalar, i);
876 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
877 ndigits);
878 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime,
879 ndigits);
880 }
881
882 nb = !vli_test_bit(scalar, 0);
883 xycz_add_c(rx[1 - nb], ry[1 - nb], rx[nb], ry[nb], curve_prime,
884 ndigits);
885
886 /* Find final 1/Z value. */
887 /* X1 - X0 */
888 vli_mod_sub(z, rx[1], rx[0], curve_prime, ndigits);
889 /* Yb * (X1 - X0) */
890 vli_mod_mult_fast(z, z, ry[1 - nb], curve_prime, ndigits);
891 /* xP * Yb * (X1 - X0) */
892 vli_mod_mult_fast(z, z, point->x, curve_prime, ndigits);
893
894 /* 1 / (xP * Yb * (X1 - X0)) */
895 vli_mod_inv(z, z, curve_prime, point->ndigits);
896
897 /* yP / (xP * Yb * (X1 - X0)) */
898 vli_mod_mult_fast(z, z, point->y, curve_prime, ndigits);
899 /* Xb * yP / (xP * Yb * (X1 - X0)) */
900 vli_mod_mult_fast(z, z, rx[1 - nb], curve_prime, ndigits);
901 /* End 1/Z calculation */
902
903 xycz_add(rx[nb], ry[nb], rx[1 - nb], ry[1 - nb], curve_prime, ndigits);
904
905 apply_z(rx[0], ry[0], z, curve_prime, ndigits);
906
907 vli_set(result->x, rx[0], ndigits);
908 vli_set(result->y, ry[0], ndigits);
909}
910
911static inline void ecc_swap_digits(const u64 *in, u64 *out,
912 unsigned int ndigits)
913{
914 int i;
915
916 for (i = 0; i < ndigits; i++)
917 out[i] = __swab64(in[ndigits - 1 - i]);
918}
919
2eb4942b
VC
920static int __ecc_is_key_valid(const struct ecc_curve *curve,
921 const u64 *private_key, unsigned int ndigits)
3c4b2390 922{
2eb4942b
VC
923 u64 one[ECC_MAX_DIGITS] = { 1, };
924 u64 res[ECC_MAX_DIGITS];
3c4b2390
SB
925
926 if (!private_key)
927 return -EINVAL;
928
2eb4942b 929 if (curve->g.ndigits != ndigits)
3c4b2390
SB
930 return -EINVAL;
931
2eb4942b
VC
932 /* Make sure the private key is in the range [2, n-3]. */
933 if (vli_cmp(one, private_key, ndigits) != -1)
3c4b2390 934 return -EINVAL;
2eb4942b
VC
935 vli_sub(res, curve->n, one, ndigits);
936 vli_sub(res, res, one, ndigits);
937 if (vli_cmp(res, private_key, ndigits) != 1)
3c4b2390
SB
938 return -EINVAL;
939
940 return 0;
941}
942
2eb4942b
VC
943int ecc_is_key_valid(unsigned int curve_id, unsigned int ndigits,
944 const u64 *private_key, unsigned int private_key_len)
945{
946 int nbytes;
947 const struct ecc_curve *curve = ecc_get_curve(curve_id);
948
949 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
950
951 if (private_key_len != nbytes)
952 return -EINVAL;
953
954 return __ecc_is_key_valid(curve, private_key, ndigits);
955}
4a2289da 956EXPORT_SYMBOL(ecc_is_key_valid);
2eb4942b 957
6755fd26
TDA
958/*
959 * ECC private keys are generated using the method of extra random bits,
960 * equivalent to that described in FIPS 186-4, Appendix B.4.1.
961 *
962 * d = (c mod(n–1)) + 1 where c is a string of random bits, 64 bits longer
963 * than requested
964 * 0 <= c mod(n-1) <= n-2 and implies that
965 * 1 <= d <= n-1
966 *
967 * This method generates a private key uniformly distributed in the range
968 * [1, n-1].
969 */
970int ecc_gen_privkey(unsigned int curve_id, unsigned int ndigits, u64 *privkey)
971{
972 const struct ecc_curve *curve = ecc_get_curve(curve_id);
d5c3b178 973 u64 priv[ECC_MAX_DIGITS];
6755fd26
TDA
974 unsigned int nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
975 unsigned int nbits = vli_num_bits(curve->n, ndigits);
976 int err;
977
978 /* Check that N is included in Table 1 of FIPS 186-4, section 6.1.1 */
d5c3b178 979 if (nbits < 160 || ndigits > ARRAY_SIZE(priv))
6755fd26
TDA
980 return -EINVAL;
981
982 /*
983 * FIPS 186-4 recommends that the private key should be obtained from a
984 * RBG with a security strength equal to or greater than the security
985 * strength associated with N.
986 *
987 * The maximum security strength identified by NIST SP800-57pt1r4 for
988 * ECC is 256 (N >= 512).
989 *
990 * This condition is met by the default RNG because it selects a favored
991 * DRBG with a security strength of 256.
992 */
993 if (crypto_get_default_rng())
4c0e22c9 994 return -EFAULT;
6755fd26
TDA
995
996 err = crypto_rng_get_bytes(crypto_default_rng, (u8 *)priv, nbytes);
997 crypto_put_default_rng();
998 if (err)
999 return err;
1000
2eb4942b
VC
1001 /* Make sure the private key is in the valid range. */
1002 if (__ecc_is_key_valid(curve, priv, ndigits))
6755fd26
TDA
1003 return -EINVAL;
1004
1005 ecc_swap_digits(priv, privkey, ndigits);
1006
1007 return 0;
1008}
4a2289da 1009EXPORT_SYMBOL(ecc_gen_privkey);
6755fd26 1010
7380c56d
TDA
1011int ecc_make_pub_key(unsigned int curve_id, unsigned int ndigits,
1012 const u64 *private_key, u64 *public_key)
3c4b2390
SB
1013{
1014 int ret = 0;
1015 struct ecc_point *pk;
d5c3b178 1016 u64 priv[ECC_MAX_DIGITS];
3c4b2390
SB
1017 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1018
d5c3b178 1019 if (!private_key || !curve || ndigits > ARRAY_SIZE(priv)) {
3c4b2390
SB
1020 ret = -EINVAL;
1021 goto out;
1022 }
1023
ad269597 1024 ecc_swap_digits(private_key, priv, ndigits);
3c4b2390
SB
1025
1026 pk = ecc_alloc_point(ndigits);
1027 if (!pk) {
1028 ret = -ENOMEM;
1029 goto out;
1030 }
1031
3da2c1df 1032 ecc_point_mult(pk, &curve->g, priv, NULL, curve, ndigits);
3c4b2390
SB
1033 if (ecc_point_is_zero(pk)) {
1034 ret = -EAGAIN;
1035 goto err_free_point;
1036 }
1037
ad269597
TDA
1038 ecc_swap_digits(pk->x, public_key, ndigits);
1039 ecc_swap_digits(pk->y, &public_key[ndigits], ndigits);
3c4b2390
SB
1040
1041err_free_point:
1042 ecc_free_point(pk);
1043out:
1044 return ret;
1045}
4a2289da 1046EXPORT_SYMBOL(ecc_make_pub_key);
3c4b2390 1047
ea169a30 1048/* SP800-56A section 5.6.2.3.4 partial verification: ephemeral keys only */
4a2289da
VC
1049int ecc_is_pubkey_valid_partial(const struct ecc_curve *curve,
1050 struct ecc_point *pk)
ea169a30
SM
1051{
1052 u64 yy[ECC_MAX_DIGITS], xxx[ECC_MAX_DIGITS], w[ECC_MAX_DIGITS];
1053
1054 /* Check 1: Verify key is not the zero point. */
1055 if (ecc_point_is_zero(pk))
1056 return -EINVAL;
1057
1058 /* Check 2: Verify key is in the range [1, p-1]. */
1059 if (vli_cmp(curve->p, pk->x, pk->ndigits) != 1)
1060 return -EINVAL;
1061 if (vli_cmp(curve->p, pk->y, pk->ndigits) != 1)
1062 return -EINVAL;
1063
1064 /* Check 3: Verify that y^2 == (x^3 + a·x + b) mod p */
1065 vli_mod_square_fast(yy, pk->y, curve->p, pk->ndigits); /* y^2 */
1066 vli_mod_square_fast(xxx, pk->x, curve->p, pk->ndigits); /* x^2 */
1067 vli_mod_mult_fast(xxx, xxx, pk->x, curve->p, pk->ndigits); /* x^3 */
1068 vli_mod_mult_fast(w, curve->a, pk->x, curve->p, pk->ndigits); /* a·x */
1069 vli_mod_add(w, w, curve->b, curve->p, pk->ndigits); /* a·x + b */
1070 vli_mod_add(w, w, xxx, curve->p, pk->ndigits); /* x^3 + a·x + b */
1071 if (vli_cmp(yy, w, pk->ndigits) != 0) /* Equation */
1072 return -EINVAL;
1073
1074 return 0;
ea169a30 1075}
4a2289da 1076EXPORT_SYMBOL(ecc_is_pubkey_valid_partial);
ea169a30 1077
8f44df15 1078int crypto_ecdh_shared_secret(unsigned int curve_id, unsigned int ndigits,
ad269597
TDA
1079 const u64 *private_key, const u64 *public_key,
1080 u64 *secret)
3c4b2390
SB
1081{
1082 int ret = 0;
1083 struct ecc_point *product, *pk;
d5c3b178
KC
1084 u64 priv[ECC_MAX_DIGITS];
1085 u64 rand_z[ECC_MAX_DIGITS];
1086 unsigned int nbytes;
3c4b2390
SB
1087 const struct ecc_curve *curve = ecc_get_curve(curve_id);
1088
d5c3b178
KC
1089 if (!private_key || !public_key || !curve ||
1090 ndigits > ARRAY_SIZE(priv) || ndigits > ARRAY_SIZE(rand_z)) {
3c4b2390
SB
1091 ret = -EINVAL;
1092 goto out;
1093 }
1094
d5c3b178 1095 nbytes = ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
3c4b2390 1096
d5c3b178 1097 get_random_bytes(rand_z, nbytes);
3c4b2390
SB
1098
1099 pk = ecc_alloc_point(ndigits);
1100 if (!pk) {
1101 ret = -ENOMEM;
d5c3b178 1102 goto out;
3c4b2390
SB
1103 }
1104
ea169a30
SM
1105 ecc_swap_digits(public_key, pk->x, ndigits);
1106 ecc_swap_digits(&public_key[ndigits], pk->y, ndigits);
1107 ret = ecc_is_pubkey_valid_partial(curve, pk);
1108 if (ret)
1109 goto err_alloc_product;
1110
1111 ecc_swap_digits(private_key, priv, ndigits);
1112
3c4b2390
SB
1113 product = ecc_alloc_point(ndigits);
1114 if (!product) {
1115 ret = -ENOMEM;
1116 goto err_alloc_product;
1117 }
1118
3da2c1df 1119 ecc_point_mult(product, pk, priv, rand_z, curve, ndigits);
3c4b2390 1120
ad269597 1121 ecc_swap_digits(product->x, secret, ndigits);
3c4b2390
SB
1122
1123 if (ecc_point_is_zero(product))
1124 ret = -EFAULT;
1125
1126 ecc_free_point(product);
1127err_alloc_product:
1128 ecc_free_point(pk);
1129out:
1130 return ret;
1131}
4a2289da
VC
1132EXPORT_SYMBOL(crypto_ecdh_shared_secret);
1133
1134MODULE_LICENSE("Dual BSD/GPL");