1 // The MIT License (MIT)
2 //
3 // Copyright (c) 2015-2016 the fiat-crypto authors (see the AUTHORS file).
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22
23 // Some of this code is taken from the ref10 version of Ed25519 in SUPERCOP
24 // 20141124 (http://bench.cr.yp.to/supercop.html). That code is released as
25 // public domain but parts have been replaced with code generated by Fiat
26 // (https://github.com/mit-plv/fiat-crypto), which is MIT licensed.
27 //
28 // The field functions are shared by Ed25519 and X25519 where possible.
29
30 #include <string.h>
31 #include <stdint.h>
32
33 #include <bootutil/bootutil_public.h>
34
35 #if defined(MCUBOOT_USE_MBED_TLS)
36 #include <mbedtls/platform_util.h>
37 #include <mbedtls/sha512.h>
38 #include <mbedtls/version.h>
39 #if MBEDTLS_VERSION_NUMBER >= 0x03000000
40 #include <mbedtls/compat-2.x.h>
41 #endif
42 #else
43 #include <tinycrypt/constants.h>
44 #include <tinycrypt/utils.h>
45 #include <tinycrypt/sha512.h>
46 #endif
47
48 #include "curve25519.h"
49 // Various pre-computed constants.
50 #include "curve25519_tables.h"
51
52 #define SHA512_DIGEST_LENGTH 64
53
54 // Low-level intrinsic operations
55
load_3(const uint8_t * in)56 static uint64_t load_3(const uint8_t *in) {
57 uint64_t result;
58 result = (uint64_t)in[0];
59 result |= ((uint64_t)in[1]) << 8;
60 result |= ((uint64_t)in[2]) << 16;
61 return result;
62 }
63
load_4(const uint8_t * in)64 static uint64_t load_4(const uint8_t *in) {
65 uint64_t result;
66 result = (uint64_t)in[0];
67 result |= ((uint64_t)in[1]) << 8;
68 result |= ((uint64_t)in[2]) << 16;
69 result |= ((uint64_t)in[3]) << 24;
70 return result;
71 }
72
73
74 // Field operations.
75
76 typedef uint32_t fe_limb_t;
77 #define FE_NUM_LIMBS 10
78
79 // assert_fe asserts that |f| satisfies bounds:
80 //
81 // [[0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
82 // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
83 // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
84 // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333],
85 // [0x0 ~> 0x4666666], [0x0 ~> 0x2333333]]
86 //
87 // See comments in curve25519_32.h for which functions use these bounds for
88 // inputs or outputs.
89 #define assert_fe(f) \
90 do { \
91 for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
92 assert(f[_assert_fe_i] <= \
93 ((_assert_fe_i & 1) ? 0x2333333u : 0x4666666u)); \
94 } \
95 } while (0)
96
97 // assert_fe_loose asserts that |f| satisfies bounds:
98 //
99 // [[0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
100 // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
101 // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
102 // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999],
103 // [0x0 ~> 0xd333332], [0x0 ~> 0x6999999]]
104 //
105 // See comments in curve25519_32.h for which functions use these bounds for
106 // inputs or outputs.
107 #define assert_fe_loose(f) \
108 do { \
109 for (unsigned _assert_fe_i = 0; _assert_fe_i < 10; _assert_fe_i++) { \
110 assert(f[_assert_fe_i] <= \
111 ((_assert_fe_i & 1) ? 0x6999999u : 0xd333332u)); \
112 } \
113 } while (0)
114
115 //FIXME: use Zephyr macro
116 _Static_assert(sizeof(fe) == sizeof(fe_limb_t) * FE_NUM_LIMBS,
117 "fe_limb_t[FE_NUM_LIMBS] is inconsistent with fe");
118
fe_frombytes_strict(fe * h,const uint8_t s[32])119 static void fe_frombytes_strict(fe *h, const uint8_t s[32]) {
120 // |fiat_25519_from_bytes| requires the top-most bit be clear.
121 assert((s[31] & 0x80) == 0);
122 fiat_25519_from_bytes(h->v, s);
123 assert_fe(h->v);
124 }
125
fe_frombytes(fe * h,const uint8_t s[32])126 static void fe_frombytes(fe *h, const uint8_t s[32]) {
127 uint8_t s_copy[32];
128 memcpy(s_copy, s, 32);
129 s_copy[31] &= 0x7f;
130 fe_frombytes_strict(h, s_copy);
131 }
132
fe_tobytes(uint8_t s[32],const fe * f)133 static void fe_tobytes(uint8_t s[32], const fe *f) {
134 assert_fe(f->v);
135 fiat_25519_to_bytes(s, f->v);
136 }
137
138 // h = 0
fe_0(fe * h)139 static void fe_0(fe *h) {
140 #if defined(MCUBOOT_USE_MBED_TLS)
141 mbedtls_platform_zeroize(h, sizeof(fe));
142 #else
143 _set(h, 0, sizeof(fe));
144 #endif
145 }
146
147 // h = 1
fe_1(fe * h)148 static void fe_1(fe *h) {
149 #if defined(MCUBOOT_USE_MBED_TLS)
150 mbedtls_platform_zeroize(h, sizeof(fe));
151 #else
152 _set(h, 0, sizeof(fe));
153 #endif
154 h->v[0] = 1;
155 }
156
157 // h = f + g
158 // Can overlap h with f or g.
fe_add(fe_loose * h,const fe * f,const fe * g)159 static void fe_add(fe_loose *h, const fe *f, const fe *g) {
160 assert_fe(f->v);
161 assert_fe(g->v);
162 fiat_25519_add(h->v, f->v, g->v);
163 assert_fe_loose(h->v);
164 }
165
166 // h = f - g
167 // Can overlap h with f or g.
fe_sub(fe_loose * h,const fe * f,const fe * g)168 static void fe_sub(fe_loose *h, const fe *f, const fe *g) {
169 assert_fe(f->v);
170 assert_fe(g->v);
171 fiat_25519_sub(h->v, f->v, g->v);
172 assert_fe_loose(h->v);
173 }
174
fe_carry(fe * h,const fe_loose * f)175 static void fe_carry(fe *h, const fe_loose* f) {
176 assert_fe_loose(f->v);
177 fiat_25519_carry(h->v, f->v);
178 assert_fe(h->v);
179 }
180
fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS],const fe_limb_t in1[FE_NUM_LIMBS],const fe_limb_t in2[FE_NUM_LIMBS])181 static void fe_mul_impl(fe_limb_t out[FE_NUM_LIMBS],
182 const fe_limb_t in1[FE_NUM_LIMBS],
183 const fe_limb_t in2[FE_NUM_LIMBS]) {
184 assert_fe_loose(in1);
185 assert_fe_loose(in2);
186 fiat_25519_carry_mul(out, in1, in2);
187 assert_fe(out);
188 }
189
fe_mul_ltt(fe_loose * h,const fe * f,const fe * g)190 static void fe_mul_ltt(fe_loose *h, const fe *f, const fe *g) {
191 fe_mul_impl(h->v, f->v, g->v);
192 }
193
fe_mul_ttt(fe * h,const fe * f,const fe * g)194 static void fe_mul_ttt(fe *h, const fe *f, const fe *g) {
195 fe_mul_impl(h->v, f->v, g->v);
196 }
197
fe_mul_tlt(fe * h,const fe_loose * f,const fe * g)198 static void fe_mul_tlt(fe *h, const fe_loose *f, const fe *g) {
199 fe_mul_impl(h->v, f->v, g->v);
200 }
201
fe_mul_ttl(fe * h,const fe * f,const fe_loose * g)202 static void fe_mul_ttl(fe *h, const fe *f, const fe_loose *g) {
203 fe_mul_impl(h->v, f->v, g->v);
204 }
205
fe_mul_tll(fe * h,const fe_loose * f,const fe_loose * g)206 static void fe_mul_tll(fe *h, const fe_loose *f, const fe_loose *g) {
207 fe_mul_impl(h->v, f->v, g->v);
208 }
209
fe_sq_tl(fe * h,const fe_loose * f)210 static void fe_sq_tl(fe *h, const fe_loose *f) {
211 assert_fe_loose(f->v);
212 fiat_25519_carry_square(h->v, f->v);
213 assert_fe(h->v);
214 }
215
fe_sq_tt(fe * h,const fe * f)216 static void fe_sq_tt(fe *h, const fe *f) {
217 assert_fe_loose(f->v);
218 fiat_25519_carry_square(h->v, f->v);
219 assert_fe(h->v);
220 }
221
222 // h = -f
fe_neg(fe_loose * h,const fe * f)223 static void fe_neg(fe_loose *h, const fe *f) {
224 assert_fe(f->v);
225 fiat_25519_opp(h->v, f->v);
226 assert_fe_loose(h->v);
227 }
228
229 // h = f
fe_copy(fe * h,const fe * f)230 static void fe_copy(fe *h, const fe *f) {
231 memmove(h, f, sizeof(fe));
232 }
233
fe_copy_lt(fe_loose * h,const fe * f)234 static void fe_copy_lt(fe_loose *h, const fe *f) {
235 //FIXME: use Zephyr macro
236 _Static_assert(sizeof(fe_loose) == sizeof(fe), "fe and fe_loose mismatch");
237 memmove(h, f, sizeof(fe));
238 }
239
fe_loose_invert(fe * out,const fe_loose * z)240 static void fe_loose_invert(fe *out, const fe_loose *z) {
241 fe t0;
242 fe t1;
243 fe t2;
244 fe t3;
245 int i;
246
247 fe_sq_tl(&t0, z);
248 fe_sq_tt(&t1, &t0);
249 for (i = 1; i < 2; ++i) {
250 fe_sq_tt(&t1, &t1);
251 }
252 fe_mul_tlt(&t1, z, &t1);
253 fe_mul_ttt(&t0, &t0, &t1);
254 fe_sq_tt(&t2, &t0);
255 fe_mul_ttt(&t1, &t1, &t2);
256 fe_sq_tt(&t2, &t1);
257 for (i = 1; i < 5; ++i) {
258 fe_sq_tt(&t2, &t2);
259 }
260 fe_mul_ttt(&t1, &t2, &t1);
261 fe_sq_tt(&t2, &t1);
262 for (i = 1; i < 10; ++i) {
263 fe_sq_tt(&t2, &t2);
264 }
265 fe_mul_ttt(&t2, &t2, &t1);
266 fe_sq_tt(&t3, &t2);
267 for (i = 1; i < 20; ++i) {
268 fe_sq_tt(&t3, &t3);
269 }
270 fe_mul_ttt(&t2, &t3, &t2);
271 fe_sq_tt(&t2, &t2);
272 for (i = 1; i < 10; ++i) {
273 fe_sq_tt(&t2, &t2);
274 }
275 fe_mul_ttt(&t1, &t2, &t1);
276 fe_sq_tt(&t2, &t1);
277 for (i = 1; i < 50; ++i) {
278 fe_sq_tt(&t2, &t2);
279 }
280 fe_mul_ttt(&t2, &t2, &t1);
281 fe_sq_tt(&t3, &t2);
282 for (i = 1; i < 100; ++i) {
283 fe_sq_tt(&t3, &t3);
284 }
285 fe_mul_ttt(&t2, &t3, &t2);
286 fe_sq_tt(&t2, &t2);
287 for (i = 1; i < 50; ++i) {
288 fe_sq_tt(&t2, &t2);
289 }
290 fe_mul_ttt(&t1, &t2, &t1);
291 fe_sq_tt(&t1, &t1);
292 for (i = 1; i < 5; ++i) {
293 fe_sq_tt(&t1, &t1);
294 }
295 fe_mul_ttt(out, &t1, &t0);
296 }
297
fe_invert(fe * out,const fe * z)298 static void fe_invert(fe *out, const fe *z) {
299 fe_loose l;
300 fe_copy_lt(&l, z);
301 fe_loose_invert(out, &l);
302 }
303
CRYPTO_memcmp(const void * in_a,const void * in_b,size_t len)304 static int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
305 const uint8_t *a = in_a;
306 const uint8_t *b = in_b;
307 uint8_t x = 0;
308
309 for (size_t i = 0; i < len; i++) {
310 x |= a[i] ^ b[i];
311 }
312
313 return x;
314 }
315
316 // return 0 if f == 0
317 // return 1 if f != 0
fe_isnonzero(const fe_loose * f)318 static int fe_isnonzero(const fe_loose *f) {
319 fe tight;
320 fe_carry(&tight, f);
321 uint8_t s[32];
322 fe_tobytes(s, &tight);
323
324 static const uint8_t zero[32] = {0};
325 return CRYPTO_memcmp(s, zero, sizeof(zero)) != 0;
326 }
327
328 // return 1 if f is in {1,3,5,...,q-2}
329 // return 0 if f is in {0,2,4,...,q-1}
fe_isnegative(const fe * f)330 static int fe_isnegative(const fe *f) {
331 uint8_t s[32];
332 fe_tobytes(s, f);
333 return s[0] & 1;
334 }
335
fe_sq2_tt(fe * h,const fe * f)336 static void fe_sq2_tt(fe *h, const fe *f) {
337 // h = f^2
338 fe_sq_tt(h, f);
339
340 // h = h + h
341 fe_loose tmp;
342 fe_add(&tmp, h, h);
343 fe_carry(h, &tmp);
344 }
345
fe_pow22523(fe * out,const fe * z)346 static void fe_pow22523(fe *out, const fe *z) {
347 fe t0;
348 fe t1;
349 fe t2;
350 int i;
351
352 fe_sq_tt(&t0, z);
353 fe_sq_tt(&t1, &t0);
354 for (i = 1; i < 2; ++i) {
355 fe_sq_tt(&t1, &t1);
356 }
357 fe_mul_ttt(&t1, z, &t1);
358 fe_mul_ttt(&t0, &t0, &t1);
359 fe_sq_tt(&t0, &t0);
360 fe_mul_ttt(&t0, &t1, &t0);
361 fe_sq_tt(&t1, &t0);
362 for (i = 1; i < 5; ++i) {
363 fe_sq_tt(&t1, &t1);
364 }
365 fe_mul_ttt(&t0, &t1, &t0);
366 fe_sq_tt(&t1, &t0);
367 for (i = 1; i < 10; ++i) {
368 fe_sq_tt(&t1, &t1);
369 }
370 fe_mul_ttt(&t1, &t1, &t0);
371 fe_sq_tt(&t2, &t1);
372 for (i = 1; i < 20; ++i) {
373 fe_sq_tt(&t2, &t2);
374 }
375 fe_mul_ttt(&t1, &t2, &t1);
376 fe_sq_tt(&t1, &t1);
377 for (i = 1; i < 10; ++i) {
378 fe_sq_tt(&t1, &t1);
379 }
380 fe_mul_ttt(&t0, &t1, &t0);
381 fe_sq_tt(&t1, &t0);
382 for (i = 1; i < 50; ++i) {
383 fe_sq_tt(&t1, &t1);
384 }
385 fe_mul_ttt(&t1, &t1, &t0);
386 fe_sq_tt(&t2, &t1);
387 for (i = 1; i < 100; ++i) {
388 fe_sq_tt(&t2, &t2);
389 }
390 fe_mul_ttt(&t1, &t2, &t1);
391 fe_sq_tt(&t1, &t1);
392 for (i = 1; i < 50; ++i) {
393 fe_sq_tt(&t1, &t1);
394 }
395 fe_mul_ttt(&t0, &t1, &t0);
396 fe_sq_tt(&t0, &t0);
397 for (i = 1; i < 2; ++i) {
398 fe_sq_tt(&t0, &t0);
399 }
400 fe_mul_ttt(out, &t0, z);
401 }
402
403
404 // Group operations.
405
x25519_ge_tobytes(uint8_t s[32],const ge_p2 * h)406 void x25519_ge_tobytes(uint8_t s[32], const ge_p2 *h) {
407 fe recip;
408 fe x;
409 fe y;
410
411 fe_invert(&recip, &h->Z);
412 fe_mul_ttt(&x, &h->X, &recip);
413 fe_mul_ttt(&y, &h->Y, &recip);
414 fe_tobytes(s, &y);
415 s[31] ^= fe_isnegative(&x) << 7;
416 }
417
x25519_ge_frombytes_vartime(ge_p3 * h,const uint8_t s[32])418 int x25519_ge_frombytes_vartime(ge_p3 *h, const uint8_t s[32]) {
419 fe u;
420 fe_loose v;
421 fe v3;
422 fe vxx;
423 fe_loose check;
424
425 fe_frombytes(&h->Y, s);
426 fe_1(&h->Z);
427 fe_sq_tt(&v3, &h->Y);
428 fe_mul_ttt(&vxx, &v3, &d);
429 fe_sub(&v, &v3, &h->Z); // u = y^2-1
430 fe_carry(&u, &v);
431 fe_add(&v, &vxx, &h->Z); // v = dy^2+1
432
433 fe_sq_tl(&v3, &v);
434 fe_mul_ttl(&v3, &v3, &v); // v3 = v^3
435 fe_sq_tt(&h->X, &v3);
436 fe_mul_ttl(&h->X, &h->X, &v);
437 fe_mul_ttt(&h->X, &h->X, &u); // x = uv^7
438
439 fe_pow22523(&h->X, &h->X); // x = (uv^7)^((q-5)/8)
440 fe_mul_ttt(&h->X, &h->X, &v3);
441 fe_mul_ttt(&h->X, &h->X, &u); // x = uv^3(uv^7)^((q-5)/8)
442
443 fe_sq_tt(&vxx, &h->X);
444 fe_mul_ttl(&vxx, &vxx, &v);
445 fe_sub(&check, &vxx, &u);
446 if (fe_isnonzero(&check)) {
447 fe_add(&check, &vxx, &u);
448 if (fe_isnonzero(&check)) {
449 return 0;
450 }
451 fe_mul_ttt(&h->X, &h->X, &sqrtm1);
452 }
453
454 if (fe_isnegative(&h->X) != (s[31] >> 7)) {
455 fe_loose t;
456 fe_neg(&t, &h->X);
457 fe_carry(&h->X, &t);
458 }
459
460 fe_mul_ttt(&h->T, &h->X, &h->Y);
461 return 1;
462 }
463
ge_p2_0(ge_p2 * h)464 static void ge_p2_0(ge_p2 *h) {
465 fe_0(&h->X);
466 fe_1(&h->Y);
467 fe_1(&h->Z);
468 }
469
470 // r = p
ge_p3_to_p2(ge_p2 * r,const ge_p3 * p)471 static void ge_p3_to_p2(ge_p2 *r, const ge_p3 *p) {
472 fe_copy(&r->X, &p->X);
473 fe_copy(&r->Y, &p->Y);
474 fe_copy(&r->Z, &p->Z);
475 }
476
477 // r = p
x25519_ge_p3_to_cached(ge_cached * r,const ge_p3 * p)478 void x25519_ge_p3_to_cached(ge_cached *r, const ge_p3 *p) {
479 fe_add(&r->YplusX, &p->Y, &p->X);
480 fe_sub(&r->YminusX, &p->Y, &p->X);
481 fe_copy_lt(&r->Z, &p->Z);
482 fe_mul_ltt(&r->T2d, &p->T, &d2);
483 }
484
485 // r = p
x25519_ge_p1p1_to_p2(ge_p2 * r,const ge_p1p1 * p)486 void x25519_ge_p1p1_to_p2(ge_p2 *r, const ge_p1p1 *p) {
487 fe_mul_tll(&r->X, &p->X, &p->T);
488 fe_mul_tll(&r->Y, &p->Y, &p->Z);
489 fe_mul_tll(&r->Z, &p->Z, &p->T);
490 }
491
492 // r = p
x25519_ge_p1p1_to_p3(ge_p3 * r,const ge_p1p1 * p)493 void x25519_ge_p1p1_to_p3(ge_p3 *r, const ge_p1p1 *p) {
494 fe_mul_tll(&r->X, &p->X, &p->T);
495 fe_mul_tll(&r->Y, &p->Y, &p->Z);
496 fe_mul_tll(&r->Z, &p->Z, &p->T);
497 fe_mul_tll(&r->T, &p->X, &p->Y);
498 }
499
500 // r = 2 * p
ge_p2_dbl(ge_p1p1 * r,const ge_p2 * p)501 static void ge_p2_dbl(ge_p1p1 *r, const ge_p2 *p) {
502 fe trX, trZ, trT;
503 fe t0;
504
505 fe_sq_tt(&trX, &p->X);
506 fe_sq_tt(&trZ, &p->Y);
507 fe_sq2_tt(&trT, &p->Z);
508 fe_add(&r->Y, &p->X, &p->Y);
509 fe_sq_tl(&t0, &r->Y);
510
511 fe_add(&r->Y, &trZ, &trX);
512 fe_sub(&r->Z, &trZ, &trX);
513 fe_carry(&trZ, &r->Y);
514 fe_sub(&r->X, &t0, &trZ);
515 fe_carry(&trZ, &r->Z);
516 fe_sub(&r->T, &trT, &trZ);
517 }
518
519 // r = 2 * p
ge_p3_dbl(ge_p1p1 * r,const ge_p3 * p)520 static void ge_p3_dbl(ge_p1p1 *r, const ge_p3 *p) {
521 ge_p2 q;
522 ge_p3_to_p2(&q, p);
523 ge_p2_dbl(r, &q);
524 }
525
526 // r = p + q
ge_madd(ge_p1p1 * r,const ge_p3 * p,const ge_precomp * q)527 static void ge_madd(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
528 fe trY, trZ, trT;
529
530 fe_add(&r->X, &p->Y, &p->X);
531 fe_sub(&r->Y, &p->Y, &p->X);
532 fe_mul_tll(&trZ, &r->X, &q->yplusx);
533 fe_mul_tll(&trY, &r->Y, &q->yminusx);
534 fe_mul_tlt(&trT, &q->xy2d, &p->T);
535 fe_add(&r->T, &p->Z, &p->Z);
536 fe_sub(&r->X, &trZ, &trY);
537 fe_add(&r->Y, &trZ, &trY);
538 fe_carry(&trZ, &r->T);
539 fe_add(&r->Z, &trZ, &trT);
540 fe_sub(&r->T, &trZ, &trT);
541 }
542
543 // r = p - q
ge_msub(ge_p1p1 * r,const ge_p3 * p,const ge_precomp * q)544 static void ge_msub(ge_p1p1 *r, const ge_p3 *p, const ge_precomp *q) {
545 fe trY, trZ, trT;
546
547 fe_add(&r->X, &p->Y, &p->X);
548 fe_sub(&r->Y, &p->Y, &p->X);
549 fe_mul_tll(&trZ, &r->X, &q->yminusx);
550 fe_mul_tll(&trY, &r->Y, &q->yplusx);
551 fe_mul_tlt(&trT, &q->xy2d, &p->T);
552 fe_add(&r->T, &p->Z, &p->Z);
553 fe_sub(&r->X, &trZ, &trY);
554 fe_add(&r->Y, &trZ, &trY);
555 fe_carry(&trZ, &r->T);
556 fe_sub(&r->Z, &trZ, &trT);
557 fe_add(&r->T, &trZ, &trT);
558 }
559
560 // r = p + q
x25519_ge_add(ge_p1p1 * r,const ge_p3 * p,const ge_cached * q)561 void x25519_ge_add(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
562 fe trX, trY, trZ, trT;
563
564 fe_add(&r->X, &p->Y, &p->X);
565 fe_sub(&r->Y, &p->Y, &p->X);
566 fe_mul_tll(&trZ, &r->X, &q->YplusX);
567 fe_mul_tll(&trY, &r->Y, &q->YminusX);
568 fe_mul_tlt(&trT, &q->T2d, &p->T);
569 fe_mul_ttl(&trX, &p->Z, &q->Z);
570 fe_add(&r->T, &trX, &trX);
571 fe_sub(&r->X, &trZ, &trY);
572 fe_add(&r->Y, &trZ, &trY);
573 fe_carry(&trZ, &r->T);
574 fe_add(&r->Z, &trZ, &trT);
575 fe_sub(&r->T, &trZ, &trT);
576 }
577
578 // r = p - q
x25519_ge_sub(ge_p1p1 * r,const ge_p3 * p,const ge_cached * q)579 void x25519_ge_sub(ge_p1p1 *r, const ge_p3 *p, const ge_cached *q) {
580 fe trX, trY, trZ, trT;
581
582 fe_add(&r->X, &p->Y, &p->X);
583 fe_sub(&r->Y, &p->Y, &p->X);
584 fe_mul_tll(&trZ, &r->X, &q->YminusX);
585 fe_mul_tll(&trY, &r->Y, &q->YplusX);
586 fe_mul_tlt(&trT, &q->T2d, &p->T);
587 fe_mul_ttl(&trX, &p->Z, &q->Z);
588 fe_add(&r->T, &trX, &trX);
589 fe_sub(&r->X, &trZ, &trY);
590 fe_add(&r->Y, &trZ, &trY);
591 fe_carry(&trZ, &r->T);
592 fe_sub(&r->Z, &trZ, &trT);
593 fe_add(&r->T, &trZ, &trT);
594 }
595
slide(signed char * r,const uint8_t * a)596 static void slide(signed char *r, const uint8_t *a) {
597 int i;
598 int b;
599 int k;
600
601 for (i = 0; i < 256; ++i) {
602 r[i] = 1 & (a[i >> 3] >> (i & 7));
603 }
604
605 for (i = 0; i < 256; ++i) {
606 if (r[i]) {
607 for (b = 1; b <= 6 && i + b < 256; ++b) {
608 if (r[i + b]) {
609 if (r[i] + (r[i + b] << b) <= 15) {
610 r[i] += r[i + b] << b;
611 r[i + b] = 0;
612 } else if (r[i] - (r[i + b] << b) >= -15) {
613 r[i] -= r[i + b] << b;
614 for (k = i + b; k < 256; ++k) {
615 if (!r[k]) {
616 r[k] = 1;
617 break;
618 }
619 r[k] = 0;
620 }
621 } else {
622 break;
623 }
624 }
625 }
626 }
627 }
628 }
629
630 // r = a * A + b * B
631 // where a = a[0]+256*a[1]+...+256^31 a[31].
632 // and b = b[0]+256*b[1]+...+256^31 b[31].
633 // B is the Ed25519 base point (x,4/5) with x positive.
ge_double_scalarmult_vartime(ge_p2 * r,const uint8_t * a,const ge_p3 * A,const uint8_t * b)634 static void ge_double_scalarmult_vartime(ge_p2 *r, const uint8_t *a,
635 const ge_p3 *A, const uint8_t *b) {
636 signed char aslide[256];
637 signed char bslide[256];
638 ge_cached Ai[8]; // A,3A,5A,7A,9A,11A,13A,15A
639 ge_p1p1 t;
640 ge_p3 u;
641 ge_p3 A2;
642 int i;
643
644 slide(aslide, a);
645 slide(bslide, b);
646
647 x25519_ge_p3_to_cached(&Ai[0], A);
648 ge_p3_dbl(&t, A);
649 x25519_ge_p1p1_to_p3(&A2, &t);
650 x25519_ge_add(&t, &A2, &Ai[0]);
651 x25519_ge_p1p1_to_p3(&u, &t);
652 x25519_ge_p3_to_cached(&Ai[1], &u);
653 x25519_ge_add(&t, &A2, &Ai[1]);
654 x25519_ge_p1p1_to_p3(&u, &t);
655 x25519_ge_p3_to_cached(&Ai[2], &u);
656 x25519_ge_add(&t, &A2, &Ai[2]);
657 x25519_ge_p1p1_to_p3(&u, &t);
658 x25519_ge_p3_to_cached(&Ai[3], &u);
659 x25519_ge_add(&t, &A2, &Ai[3]);
660 x25519_ge_p1p1_to_p3(&u, &t);
661 x25519_ge_p3_to_cached(&Ai[4], &u);
662 x25519_ge_add(&t, &A2, &Ai[4]);
663 x25519_ge_p1p1_to_p3(&u, &t);
664 x25519_ge_p3_to_cached(&Ai[5], &u);
665 x25519_ge_add(&t, &A2, &Ai[5]);
666 x25519_ge_p1p1_to_p3(&u, &t);
667 x25519_ge_p3_to_cached(&Ai[6], &u);
668 x25519_ge_add(&t, &A2, &Ai[6]);
669 x25519_ge_p1p1_to_p3(&u, &t);
670 x25519_ge_p3_to_cached(&Ai[7], &u);
671
672 ge_p2_0(r);
673
674 for (i = 255; i >= 0; --i) {
675 if (aslide[i] || bslide[i]) {
676 break;
677 }
678 }
679
680 for (; i >= 0; --i) {
681 ge_p2_dbl(&t, r);
682
683 if (aslide[i] > 0) {
684 x25519_ge_p1p1_to_p3(&u, &t);
685 x25519_ge_add(&t, &u, &Ai[aslide[i] / 2]);
686 } else if (aslide[i] < 0) {
687 x25519_ge_p1p1_to_p3(&u, &t);
688 x25519_ge_sub(&t, &u, &Ai[(-aslide[i]) / 2]);
689 }
690
691 if (bslide[i] > 0) {
692 x25519_ge_p1p1_to_p3(&u, &t);
693 ge_madd(&t, &u, &Bi[bslide[i] / 2]);
694 } else if (bslide[i] < 0) {
695 x25519_ge_p1p1_to_p3(&u, &t);
696 ge_msub(&t, &u, &Bi[(-bslide[i]) / 2]);
697 }
698
699 x25519_ge_p1p1_to_p2(r, &t);
700 }
701 }
702
703 // int64_lshift21 returns |a << 21| but is defined when shifting bits into the
704 // sign bit. This works around a language flaw in C.
int64_lshift21(int64_t a)705 static inline int64_t int64_lshift21(int64_t a) {
706 return (int64_t)((uint64_t)a << 21);
707 }
708
709 // The set of scalars is \Z/l
710 // where l = 2^252 + 27742317777372353535851937790883648493.
711
712 // Input:
713 // s[0]+256*s[1]+...+256^63*s[63] = s
714 //
715 // Output:
716 // s[0]+256*s[1]+...+256^31*s[31] = s mod l
717 // where l = 2^252 + 27742317777372353535851937790883648493.
718 // Overwrites s in place.
x25519_sc_reduce(uint8_t s[64])719 void x25519_sc_reduce(uint8_t s[64]) {
720 int64_t s0 = 2097151 & load_3(s);
721 int64_t s1 = 2097151 & (load_4(s + 2) >> 5);
722 int64_t s2 = 2097151 & (load_3(s + 5) >> 2);
723 int64_t s3 = 2097151 & (load_4(s + 7) >> 7);
724 int64_t s4 = 2097151 & (load_4(s + 10) >> 4);
725 int64_t s5 = 2097151 & (load_3(s + 13) >> 1);
726 int64_t s6 = 2097151 & (load_4(s + 15) >> 6);
727 int64_t s7 = 2097151 & (load_3(s + 18) >> 3);
728 int64_t s8 = 2097151 & load_3(s + 21);
729 int64_t s9 = 2097151 & (load_4(s + 23) >> 5);
730 int64_t s10 = 2097151 & (load_3(s + 26) >> 2);
731 int64_t s11 = 2097151 & (load_4(s + 28) >> 7);
732 int64_t s12 = 2097151 & (load_4(s + 31) >> 4);
733 int64_t s13 = 2097151 & (load_3(s + 34) >> 1);
734 int64_t s14 = 2097151 & (load_4(s + 36) >> 6);
735 int64_t s15 = 2097151 & (load_3(s + 39) >> 3);
736 int64_t s16 = 2097151 & load_3(s + 42);
737 int64_t s17 = 2097151 & (load_4(s + 44) >> 5);
738 int64_t s18 = 2097151 & (load_3(s + 47) >> 2);
739 int64_t s19 = 2097151 & (load_4(s + 49) >> 7);
740 int64_t s20 = 2097151 & (load_4(s + 52) >> 4);
741 int64_t s21 = 2097151 & (load_3(s + 55) >> 1);
742 int64_t s22 = 2097151 & (load_4(s + 57) >> 6);
743 int64_t s23 = (load_4(s + 60) >> 3);
744 int64_t carry0;
745 int64_t carry1;
746 int64_t carry2;
747 int64_t carry3;
748 int64_t carry4;
749 int64_t carry5;
750 int64_t carry6;
751 int64_t carry7;
752 int64_t carry8;
753 int64_t carry9;
754 int64_t carry10;
755 int64_t carry11;
756 int64_t carry12;
757 int64_t carry13;
758 int64_t carry14;
759 int64_t carry15;
760 int64_t carry16;
761
762 s11 += s23 * 666643;
763 s12 += s23 * 470296;
764 s13 += s23 * 654183;
765 s14 -= s23 * 997805;
766 s15 += s23 * 136657;
767 s16 -= s23 * 683901;
768 s23 = 0;
769
770 s10 += s22 * 666643;
771 s11 += s22 * 470296;
772 s12 += s22 * 654183;
773 s13 -= s22 * 997805;
774 s14 += s22 * 136657;
775 s15 -= s22 * 683901;
776 s22 = 0;
777
778 s9 += s21 * 666643;
779 s10 += s21 * 470296;
780 s11 += s21 * 654183;
781 s12 -= s21 * 997805;
782 s13 += s21 * 136657;
783 s14 -= s21 * 683901;
784 s21 = 0;
785
786 s8 += s20 * 666643;
787 s9 += s20 * 470296;
788 s10 += s20 * 654183;
789 s11 -= s20 * 997805;
790 s12 += s20 * 136657;
791 s13 -= s20 * 683901;
792 s20 = 0;
793
794 s7 += s19 * 666643;
795 s8 += s19 * 470296;
796 s9 += s19 * 654183;
797 s10 -= s19 * 997805;
798 s11 += s19 * 136657;
799 s12 -= s19 * 683901;
800 s19 = 0;
801
802 s6 += s18 * 666643;
803 s7 += s18 * 470296;
804 s8 += s18 * 654183;
805 s9 -= s18 * 997805;
806 s10 += s18 * 136657;
807 s11 -= s18 * 683901;
808 s18 = 0;
809
810 carry6 = (s6 + (1 << 20)) >> 21;
811 s7 += carry6;
812 s6 -= int64_lshift21(carry6);
813 carry8 = (s8 + (1 << 20)) >> 21;
814 s9 += carry8;
815 s8 -= int64_lshift21(carry8);
816 carry10 = (s10 + (1 << 20)) >> 21;
817 s11 += carry10;
818 s10 -= int64_lshift21(carry10);
819 carry12 = (s12 + (1 << 20)) >> 21;
820 s13 += carry12;
821 s12 -= int64_lshift21(carry12);
822 carry14 = (s14 + (1 << 20)) >> 21;
823 s15 += carry14;
824 s14 -= int64_lshift21(carry14);
825 carry16 = (s16 + (1 << 20)) >> 21;
826 s17 += carry16;
827 s16 -= int64_lshift21(carry16);
828
829 carry7 = (s7 + (1 << 20)) >> 21;
830 s8 += carry7;
831 s7 -= int64_lshift21(carry7);
832 carry9 = (s9 + (1 << 20)) >> 21;
833 s10 += carry9;
834 s9 -= int64_lshift21(carry9);
835 carry11 = (s11 + (1 << 20)) >> 21;
836 s12 += carry11;
837 s11 -= int64_lshift21(carry11);
838 carry13 = (s13 + (1 << 20)) >> 21;
839 s14 += carry13;
840 s13 -= int64_lshift21(carry13);
841 carry15 = (s15 + (1 << 20)) >> 21;
842 s16 += carry15;
843 s15 -= int64_lshift21(carry15);
844
845 s5 += s17 * 666643;
846 s6 += s17 * 470296;
847 s7 += s17 * 654183;
848 s8 -= s17 * 997805;
849 s9 += s17 * 136657;
850 s10 -= s17 * 683901;
851 s17 = 0;
852
853 s4 += s16 * 666643;
854 s5 += s16 * 470296;
855 s6 += s16 * 654183;
856 s7 -= s16 * 997805;
857 s8 += s16 * 136657;
858 s9 -= s16 * 683901;
859 s16 = 0;
860
861 s3 += s15 * 666643;
862 s4 += s15 * 470296;
863 s5 += s15 * 654183;
864 s6 -= s15 * 997805;
865 s7 += s15 * 136657;
866 s8 -= s15 * 683901;
867 s15 = 0;
868
869 s2 += s14 * 666643;
870 s3 += s14 * 470296;
871 s4 += s14 * 654183;
872 s5 -= s14 * 997805;
873 s6 += s14 * 136657;
874 s7 -= s14 * 683901;
875 s14 = 0;
876
877 s1 += s13 * 666643;
878 s2 += s13 * 470296;
879 s3 += s13 * 654183;
880 s4 -= s13 * 997805;
881 s5 += s13 * 136657;
882 s6 -= s13 * 683901;
883 s13 = 0;
884
885 s0 += s12 * 666643;
886 s1 += s12 * 470296;
887 s2 += s12 * 654183;
888 s3 -= s12 * 997805;
889 s4 += s12 * 136657;
890 s5 -= s12 * 683901;
891 s12 = 0;
892
893 carry0 = (s0 + (1 << 20)) >> 21;
894 s1 += carry0;
895 s0 -= int64_lshift21(carry0);
896 carry2 = (s2 + (1 << 20)) >> 21;
897 s3 += carry2;
898 s2 -= int64_lshift21(carry2);
899 carry4 = (s4 + (1 << 20)) >> 21;
900 s5 += carry4;
901 s4 -= int64_lshift21(carry4);
902 carry6 = (s6 + (1 << 20)) >> 21;
903 s7 += carry6;
904 s6 -= int64_lshift21(carry6);
905 carry8 = (s8 + (1 << 20)) >> 21;
906 s9 += carry8;
907 s8 -= int64_lshift21(carry8);
908 carry10 = (s10 + (1 << 20)) >> 21;
909 s11 += carry10;
910 s10 -= int64_lshift21(carry10);
911
912 carry1 = (s1 + (1 << 20)) >> 21;
913 s2 += carry1;
914 s1 -= int64_lshift21(carry1);
915 carry3 = (s3 + (1 << 20)) >> 21;
916 s4 += carry3;
917 s3 -= int64_lshift21(carry3);
918 carry5 = (s5 + (1 << 20)) >> 21;
919 s6 += carry5;
920 s5 -= int64_lshift21(carry5);
921 carry7 = (s7 + (1 << 20)) >> 21;
922 s8 += carry7;
923 s7 -= int64_lshift21(carry7);
924 carry9 = (s9 + (1 << 20)) >> 21;
925 s10 += carry9;
926 s9 -= int64_lshift21(carry9);
927 carry11 = (s11 + (1 << 20)) >> 21;
928 s12 += carry11;
929 s11 -= int64_lshift21(carry11);
930
931 s0 += s12 * 666643;
932 s1 += s12 * 470296;
933 s2 += s12 * 654183;
934 s3 -= s12 * 997805;
935 s4 += s12 * 136657;
936 s5 -= s12 * 683901;
937 s12 = 0;
938
939 carry0 = s0 >> 21;
940 s1 += carry0;
941 s0 -= int64_lshift21(carry0);
942 carry1 = s1 >> 21;
943 s2 += carry1;
944 s1 -= int64_lshift21(carry1);
945 carry2 = s2 >> 21;
946 s3 += carry2;
947 s2 -= int64_lshift21(carry2);
948 carry3 = s3 >> 21;
949 s4 += carry3;
950 s3 -= int64_lshift21(carry3);
951 carry4 = s4 >> 21;
952 s5 += carry4;
953 s4 -= int64_lshift21(carry4);
954 carry5 = s5 >> 21;
955 s6 += carry5;
956 s5 -= int64_lshift21(carry5);
957 carry6 = s6 >> 21;
958 s7 += carry6;
959 s6 -= int64_lshift21(carry6);
960 carry7 = s7 >> 21;
961 s8 += carry7;
962 s7 -= int64_lshift21(carry7);
963 carry8 = s8 >> 21;
964 s9 += carry8;
965 s8 -= int64_lshift21(carry8);
966 carry9 = s9 >> 21;
967 s10 += carry9;
968 s9 -= int64_lshift21(carry9);
969 carry10 = s10 >> 21;
970 s11 += carry10;
971 s10 -= int64_lshift21(carry10);
972 carry11 = s11 >> 21;
973 s12 += carry11;
974 s11 -= int64_lshift21(carry11);
975
976 s0 += s12 * 666643;
977 s1 += s12 * 470296;
978 s2 += s12 * 654183;
979 s3 -= s12 * 997805;
980 s4 += s12 * 136657;
981 s5 -= s12 * 683901;
982 s12 = 0;
983
984 carry0 = s0 >> 21;
985 s1 += carry0;
986 s0 -= int64_lshift21(carry0);
987 carry1 = s1 >> 21;
988 s2 += carry1;
989 s1 -= int64_lshift21(carry1);
990 carry2 = s2 >> 21;
991 s3 += carry2;
992 s2 -= int64_lshift21(carry2);
993 carry3 = s3 >> 21;
994 s4 += carry3;
995 s3 -= int64_lshift21(carry3);
996 carry4 = s4 >> 21;
997 s5 += carry4;
998 s4 -= int64_lshift21(carry4);
999 carry5 = s5 >> 21;
1000 s6 += carry5;
1001 s5 -= int64_lshift21(carry5);
1002 carry6 = s6 >> 21;
1003 s7 += carry6;
1004 s6 -= int64_lshift21(carry6);
1005 carry7 = s7 >> 21;
1006 s8 += carry7;
1007 s7 -= int64_lshift21(carry7);
1008 carry8 = s8 >> 21;
1009 s9 += carry8;
1010 s8 -= int64_lshift21(carry8);
1011 carry9 = s9 >> 21;
1012 s10 += carry9;
1013 s9 -= int64_lshift21(carry9);
1014 carry10 = s10 >> 21;
1015 s11 += carry10;
1016 s10 -= int64_lshift21(carry10);
1017
1018 s[0] = s0 >> 0;
1019 s[1] = s0 >> 8;
1020 s[2] = (s0 >> 16) | (s1 << 5);
1021 s[3] = s1 >> 3;
1022 s[4] = s1 >> 11;
1023 s[5] = (s1 >> 19) | (s2 << 2);
1024 s[6] = s2 >> 6;
1025 s[7] = (s2 >> 14) | (s3 << 7);
1026 s[8] = s3 >> 1;
1027 s[9] = s3 >> 9;
1028 s[10] = (s3 >> 17) | (s4 << 4);
1029 s[11] = s4 >> 4;
1030 s[12] = s4 >> 12;
1031 s[13] = (s4 >> 20) | (s5 << 1);
1032 s[14] = s5 >> 7;
1033 s[15] = (s5 >> 15) | (s6 << 6);
1034 s[16] = s6 >> 2;
1035 s[17] = s6 >> 10;
1036 s[18] = (s6 >> 18) | (s7 << 3);
1037 s[19] = s7 >> 5;
1038 s[20] = s7 >> 13;
1039 s[21] = s8 >> 0;
1040 s[22] = s8 >> 8;
1041 s[23] = (s8 >> 16) | (s9 << 5);
1042 s[24] = s9 >> 3;
1043 s[25] = s9 >> 11;
1044 s[26] = (s9 >> 19) | (s10 << 2);
1045 s[27] = s10 >> 6;
1046 s[28] = (s10 >> 14) | (s11 << 7);
1047 s[29] = s11 >> 1;
1048 s[30] = s11 >> 9;
1049 s[31] = s11 >> 17;
1050 }
1051
ED25519_verify(const uint8_t * message,size_t message_len,const uint8_t signature[64],const uint8_t public_key[32])1052 int ED25519_verify(const uint8_t *message, size_t message_len,
1053 const uint8_t signature[64], const uint8_t public_key[32]) {
1054 ge_p3 A;
1055 if ((signature[63] & 224) != 0 ||
1056 !x25519_ge_frombytes_vartime(&A, public_key)) {
1057 return 0;
1058 }
1059
1060 fe_loose t;
1061 fe_neg(&t, &A.X);
1062 fe_carry(&A.X, &t);
1063 fe_neg(&t, &A.T);
1064 fe_carry(&A.T, &t);
1065
1066 uint8_t pkcopy[32];
1067 memcpy(pkcopy, public_key, 32);
1068 uint8_t rcopy[32];
1069 memcpy(rcopy, signature, 32);
1070 union {
1071 uint64_t u64[4];
1072 uint8_t u8[32];
1073 } scopy;
1074 memcpy(&scopy.u8[0], signature + 32, 32);
1075
1076 // https://tools.ietf.org/html/rfc8032#section-5.1.7 requires that s be in
1077 // the range [0, order) in order to prevent signature malleability.
1078
1079 // kOrder is the order of Curve25519 in little-endian form.
1080 static const uint64_t kOrder[4] = {
1081 UINT64_C(0x5812631a5cf5d3ed),
1082 UINT64_C(0x14def9dea2f79cd6),
1083 0,
1084 UINT64_C(0x1000000000000000),
1085 };
1086 for (size_t i = 3;; i--) {
1087 if (scopy.u64[i] > kOrder[i]) {
1088 return 0;
1089 } else if (scopy.u64[i] < kOrder[i]) {
1090 break;
1091 } else if (i == 0) {
1092 return 0;
1093 }
1094 }
1095
1096 #if defined(MCUBOOT_USE_MBED_TLS)
1097
1098 mbedtls_sha512_context ctx;
1099 int ret;
1100
1101 mbedtls_sha512_init(&ctx);
1102
1103 ret = mbedtls_sha512_starts_ret(&ctx, 0);
1104 assert(ret == 0);
1105
1106 ret = mbedtls_sha512_update_ret(&ctx, signature, 32);
1107 assert(ret == 0);
1108 ret = mbedtls_sha512_update_ret(&ctx, public_key, 32);
1109 assert(ret == 0);
1110 ret = mbedtls_sha512_update_ret(&ctx, message, message_len);
1111 assert(ret == 0);
1112
1113 uint8_t h[SHA512_DIGEST_LENGTH];
1114 ret = mbedtls_sha512_finish_ret(&ctx, h);
1115 assert(ret == 0);
1116 mbedtls_sha512_free(&ctx);
1117
1118 #else
1119
1120 struct tc_sha512_state_struct s;
1121 int rc;
1122
1123 rc = tc_sha512_init(&s);
1124 assert(rc == TC_CRYPTO_SUCCESS);
1125
1126 rc = tc_sha512_update(&s, signature, 32);
1127 assert(rc == TC_CRYPTO_SUCCESS);
1128 rc = tc_sha512_update(&s, public_key, 32);
1129 assert(rc == TC_CRYPTO_SUCCESS);
1130 rc = tc_sha512_update(&s, message, message_len);
1131 assert(rc == TC_CRYPTO_SUCCESS);
1132
1133 uint8_t h[TC_SHA512_DIGEST_SIZE];
1134 rc = tc_sha512_final(h, &s);
1135 assert(rc == TC_CRYPTO_SUCCESS);
1136
1137 #endif
1138
1139 x25519_sc_reduce(h);
1140
1141 ge_p2 R;
1142 ge_double_scalarmult_vartime(&R, h, &A, scopy.u8);
1143
1144 uint8_t rcheck[32];
1145 x25519_ge_tobytes(rcheck, &R);
1146
1147 return CRYPTO_memcmp(rcheck, rcopy, sizeof(rcheck)) == 0;
1148 }
1149
fe_cswap(fe * f,fe * g,fe_limb_t b)1150 static void fe_cswap(fe *f, fe *g, fe_limb_t b) {
1151 b = 0-b;
1152 for (unsigned i = 0; i < FE_NUM_LIMBS; i++) {
1153 fe_limb_t x = f->v[i] ^ g->v[i];
1154 x &= b;
1155 f->v[i] ^= x;
1156 g->v[i] ^= x;
1157 }
1158 }
1159
fiat_25519_carry_scmul_121666(uint32_t out1[10],const uint32_t arg1[10])1160 static void fiat_25519_carry_scmul_121666(uint32_t out1[10], const uint32_t arg1[10]) {
1161 uint64_t x1 = ((uint64_t)UINT32_C(0x1db42) * (arg1[9]));
1162 uint64_t x2 = ((uint64_t)UINT32_C(0x1db42) * (arg1[8]));
1163 uint64_t x3 = ((uint64_t)UINT32_C(0x1db42) * (arg1[7]));
1164 uint64_t x4 = ((uint64_t)UINT32_C(0x1db42) * (arg1[6]));
1165 uint64_t x5 = ((uint64_t)UINT32_C(0x1db42) * (arg1[5]));
1166 uint64_t x6 = ((uint64_t)UINT32_C(0x1db42) * (arg1[4]));
1167 uint64_t x7 = ((uint64_t)UINT32_C(0x1db42) * (arg1[3]));
1168 uint64_t x8 = ((uint64_t)UINT32_C(0x1db42) * (arg1[2]));
1169 uint64_t x9 = ((uint64_t)UINT32_C(0x1db42) * (arg1[1]));
1170 uint64_t x10 = ((uint64_t)UINT32_C(0x1db42) * (arg1[0]));
1171 uint32_t x11 = (uint32_t)(x10 >> 26);
1172 uint32_t x12 = (uint32_t)(x10 & UINT32_C(0x3ffffff));
1173 uint64_t x13 = (x11 + x9);
1174 uint32_t x14 = (uint32_t)(x13 >> 25);
1175 uint32_t x15 = (uint32_t)(x13 & UINT32_C(0x1ffffff));
1176 uint64_t x16 = (x14 + x8);
1177 uint32_t x17 = (uint32_t)(x16 >> 26);
1178 uint32_t x18 = (uint32_t)(x16 & UINT32_C(0x3ffffff));
1179 uint64_t x19 = (x17 + x7);
1180 uint32_t x20 = (uint32_t)(x19 >> 25);
1181 uint32_t x21 = (uint32_t)(x19 & UINT32_C(0x1ffffff));
1182 uint64_t x22 = (x20 + x6);
1183 uint32_t x23 = (uint32_t)(x22 >> 26);
1184 uint32_t x24 = (uint32_t)(x22 & UINT32_C(0x3ffffff));
1185 uint64_t x25 = (x23 + x5);
1186 uint32_t x26 = (uint32_t)(x25 >> 25);
1187 uint32_t x27 = (uint32_t)(x25 & UINT32_C(0x1ffffff));
1188 uint64_t x28 = (x26 + x4);
1189 uint32_t x29 = (uint32_t)(x28 >> 26);
1190 uint32_t x30 = (uint32_t)(x28 & UINT32_C(0x3ffffff));
1191 uint64_t x31 = (x29 + x3);
1192 uint32_t x32 = (uint32_t)(x31 >> 25);
1193 uint32_t x33 = (uint32_t)(x31 & UINT32_C(0x1ffffff));
1194 uint64_t x34 = (x32 + x2);
1195 uint32_t x35 = (uint32_t)(x34 >> 26);
1196 uint32_t x36 = (uint32_t)(x34 & UINT32_C(0x3ffffff));
1197 uint64_t x37 = (x35 + x1);
1198 uint32_t x38 = (uint32_t)(x37 >> 25);
1199 uint32_t x39 = (uint32_t)(x37 & UINT32_C(0x1ffffff));
1200 uint32_t x40 = (x38 * (uint32_t)UINT8_C(0x13));
1201 uint32_t x41 = (x12 + x40);
1202 uint32_t x42 = (x41 >> 26);
1203 uint32_t x43 = (x41 & UINT32_C(0x3ffffff));
1204 uint32_t x44 = (x42 + x15);
1205 uint32_t x45 = (x44 >> 25);
1206 uint32_t x46 = (x44 & UINT32_C(0x1ffffff));
1207 uint32_t x47 = (x45 + x18);
1208 out1[0] = x43;
1209 out1[1] = x46;
1210 out1[2] = x47;
1211 out1[3] = x21;
1212 out1[4] = x24;
1213 out1[5] = x27;
1214 out1[6] = x30;
1215 out1[7] = x33;
1216 out1[8] = x36;
1217 out1[9] = x39;
1218 }
1219
fe_mul121666(fe * h,const fe_loose * f)1220 static void fe_mul121666(fe *h, const fe_loose *f) {
1221 assert_fe_loose(f->v);
1222 fiat_25519_carry_scmul_121666(h->v, f->v);
1223 assert_fe(h->v);
1224 }
1225
x25519_scalar_mult_generic(uint8_t out[32],const uint8_t scalar[32],const uint8_t point[32])1226 static void x25519_scalar_mult_generic(uint8_t out[32],
1227 const uint8_t scalar[32],
1228 const uint8_t point[32]) {
1229 fe x1, x2, z2, x3, z3, tmp0, tmp1;
1230 fe_loose x2l, z2l, x3l, tmp0l, tmp1l;
1231
1232 uint8_t e[32];
1233 memcpy(e, scalar, 32);
1234 e[0] &= 248;
1235 e[31] &= 127;
1236 e[31] |= 64;
1237
1238 // The following implementation was transcribed to Coq and proven to
1239 // correspond to unary scalar multiplication in affine coordinates given that
1240 // x1 != 0 is the x coordinate of some point on the curve. It was also checked
1241 // in Coq that doing a ladderstep with x1 = x3 = 0 gives z2' = z3' = 0, and z2
1242 // = z3 = 0 gives z2' = z3' = 0. The statement was quantified over the
1243 // underlying field, so it applies to Curve25519 itself and the quadratic
1244 // twist of Curve25519. It was not proven in Coq that prime-field arithmetic
1245 // correctly simulates extension-field arithmetic on prime-field values.
1246 // The decoding of the byte array representation of e was not considered.
1247 // Specification of Montgomery curves in affine coordinates:
1248 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Spec/MontgomeryCurve.v#L27>
1249 // Proof that these form a group that is isomorphic to a Weierstrass curve:
1250 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/AffineProofs.v#L35>
1251 // Coq transcription and correctness proof of the loop (where scalarbits=255):
1252 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L118>
1253 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L278>
1254 // preconditions: 0 <= e < 2^255 (not necessarily e < order), fe_invert(0) = 0
1255 fe_frombytes(&x1, point);
1256 fe_1(&x2);
1257 fe_0(&z2);
1258 fe_copy(&x3, &x1);
1259 fe_1(&z3);
1260
1261 unsigned swap = 0;
1262 int pos;
1263 for (pos = 254; pos >= 0; --pos) {
1264 // loop invariant as of right before the test, for the case where x1 != 0:
1265 // pos >= -1; if z2 = 0 then x2 is nonzero; if z3 = 0 then x3 is nonzero
1266 // let r := e >> (pos+1) in the following equalities of projective points:
1267 // to_xz (r*P) === if swap then (x3, z3) else (x2, z2)
1268 // to_xz ((r+1)*P) === if swap then (x2, z2) else (x3, z3)
1269 // x1 is the nonzero x coordinate of the nonzero point (r*P-(r+1)*P)
1270 unsigned b = 1 & (e[pos / 8] >> (pos & 7));
1271 swap ^= b;
1272 fe_cswap(&x2, &x3, swap);
1273 fe_cswap(&z2, &z3, swap);
1274 swap = b;
1275 // Coq transcription of ladderstep formula (called from transcribed loop):
1276 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZ.v#L89>
1277 // <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L131>
1278 // x1 != 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L217>
1279 // x1 = 0 <https://github.com/mit-plv/fiat-crypto/blob/2456d821825521f7e03e65882cc3521795b0320f/src/Curves/Montgomery/XZProofs.v#L147>
1280 fe_sub(&tmp0l, &x3, &z3);
1281 fe_sub(&tmp1l, &x2, &z2);
1282 fe_add(&x2l, &x2, &z2);
1283 fe_add(&z2l, &x3, &z3);
1284 fe_mul_tll(&z3, &tmp0l, &x2l);
1285 fe_mul_tll(&z2, &z2l, &tmp1l);
1286 fe_sq_tl(&tmp0, &tmp1l);
1287 fe_sq_tl(&tmp1, &x2l);
1288 fe_add(&x3l, &z3, &z2);
1289 fe_sub(&z2l, &z3, &z2);
1290 fe_mul_ttt(&x2, &tmp1, &tmp0);
1291 fe_sub(&tmp1l, &tmp1, &tmp0);
1292 fe_sq_tl(&z2, &z2l);
1293 fe_mul121666(&z3, &tmp1l);
1294 fe_sq_tl(&x3, &x3l);
1295 fe_add(&tmp0l, &tmp0, &z3);
1296 fe_mul_ttt(&z3, &x1, &z2);
1297 fe_mul_tll(&z2, &tmp1l, &tmp0l);
1298 }
1299 // here pos=-1, so r=e, so to_xz (e*P) === if swap then (x3, z3) else (x2, z2)
1300 fe_cswap(&x2, &x3, swap);
1301 fe_cswap(&z2, &z3, swap);
1302
1303 fe_invert(&z2, &z2);
1304 fe_mul_ttt(&x2, &x2, &z2);
1305 fe_tobytes(out, &x2);
1306 }
1307
X25519(uint8_t out_shared_key[32],const uint8_t private_key[32],const uint8_t peer_public_value[32])1308 int X25519(uint8_t out_shared_key[32], const uint8_t private_key[32],
1309 const uint8_t peer_public_value[32]) {
1310 static const uint8_t kZeros[32] = {0};
1311 x25519_scalar_mult_generic(out_shared_key, private_key, peer_public_value);
1312 // The all-zero output results when the input is a point of small order.
1313 return CRYPTO_memcmp(kZeros, out_shared_key, 32) != 0;
1314 }
1315