Debian Codegraph @

mpi/ec.c

raw
1 /* ec.c - Elliptic Curve functions
2 * Copyright (C) 2007 Free Software Foundation, Inc.
3 * Copyright (C) 2013 g10 Code GmbH
4 *
5 * This file is part of Libgcrypt.
6 *
7 * Libgcrypt is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * Libgcrypt is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include "mpi-internal.h"
27 #include "longlong.h"
28 #include "g10lib.h"
29 #include "context.h"
30 #include "ec-context.h"
31 #include "ec-internal.h"
32 #include "cipher.h" /* for GCRYECC_FLAG_LEAST_LEAK */
33
34 extern void reverse_buffer (unsigned char *buffer, unsigned int length);
35
36 #define point_init(a, nbits) _gcry_mpi_point_init ((a), (nbits))
37 #define point_free(a) _gcry_mpi_point_free_parts ((a))
38
39
40 /* Print a point using the log functions. If CTX is not NULL affine
41 coordinates will be printed. */
42 void
43 _gcry_mpi_point_log (const char *name, mpi_point_t point, mpi_ec_t ctx)
44 {
45 gcry_mpi_t x, y;
46 char buf[100];
47
48 if (!point)
49 {
50 snprintf (buf, sizeof buf - 1, "%s.*", name);
51 log_mpidump (buf, NULL);
52 return;
53 }
54 snprintf (buf, sizeof buf - 1, "%s.X", name);
55
56 if (ctx)
57 {
58 x = mpi_new (ctx->nbits);
59 y = mpi_new (ctx->nbits);
60 }
61 if (!ctx || _gcry_mpi_ec_get_affine (x, y, point, ctx))
62 {
63 log_mpidump (buf, point->x);
64 buf[strlen(buf)-1] = 'Y';
65 log_mpidump (buf, point->y);
66 buf[strlen(buf)-1] = 'Z';
67 log_mpidump (buf, point->z);
68 }
69 else
70 {
71 buf[strlen(buf)-1] = 'x';
72 log_mpidump (buf, x);
73 buf[strlen(buf)-1] = 'y';
74 log_mpidump (buf, y);
75
76 }
77 if (ctx)
78 {
79 _gcry_mpi_release (x);
80 _gcry_mpi_release (y);
81 }
82 }
83
84
85 /* Create a new point option. NBITS gives the size in bits of one
86 coordinate; it is only used to pre-allocate some resources and
87 might also be passed as 0 to use a default value. */
88 mpi_point_t
89 _gcry_mpi_point_new (unsigned int nbits)
90 {
91 mpi_point_t p;
92
93 p = xmalloc (sizeof *p);
94 _gcry_mpi_point_init (p, nbits);
95 return p;
96 }
97
98
99 /* Release the point object P. P may be NULL. */
100 void
101 _gcry_mpi_point_release (mpi_point_t p)
102 {
103 if (p)
104 {
105 _gcry_mpi_point_free_parts (p);
106 xfree (p);
107 }
108 }
109
110
111 /* Initialize the fields of a point object. gcry_mpi_point_free_parts
112 may be used to release the fields. */
113 void
114 _gcry_mpi_point_init (mpi_point_t p, unsigned int nbits)
115 {
116 p->x = mpi_new (nbits);
117 p->y = mpi_new (nbits);
118 p->z = mpi_new (nbits);
119 }
120
121
122 /* Release the parts of a point object. */
123 void
124 _gcry_mpi_point_free_parts (mpi_point_t p)
125 {
126 mpi_free (p->x); p->x = NULL;
127 mpi_free (p->y); p->y = NULL;
128 mpi_free (p->z); p->z = NULL;
129 }
130
131
132 /* Set the value from S into D. */
133 static void
134 point_set (mpi_point_t d, mpi_point_t s)
135 {
136 mpi_set (d->x, s->x);
137 mpi_set (d->y, s->y);
138 mpi_set (d->z, s->z);
139 }
140
141
142 /* Return a copy of POINT. */
143 gcry_mpi_point_t
144 _gcry_mpi_point_copy (gcry_mpi_point_t point)
145 {
146 mpi_point_t newpoint;
147
148 newpoint = _gcry_mpi_point_new (0);
149 if (point)
150 point_set (newpoint, point);
151
152 return newpoint;
153 }
154
155
156 void
157 _gcry_mpi_ec_point_resize (mpi_point_t p, mpi_ec_t ctx)
158 {
159 size_t nlimbs = ctx->p->nlimbs;
160
161 mpi_resize (p->x, nlimbs);
162 p->x->nlimbs = nlimbs;
163 mpi_resize (p->z, nlimbs);
164 p->z->nlimbs = nlimbs;
165
166 if (ctx->model != MPI_EC_MONTGOMERY)
167 {
168 mpi_resize (p->y, nlimbs);
169 p->y->nlimbs = nlimbs;
170 }
171 }
172
173
174 static void
175 point_swap_cond (mpi_point_t d, mpi_point_t s, unsigned long swap,
176 mpi_ec_t ctx)
177 {
178 mpi_swap_cond (d->x, s->x, swap);
179 if (ctx->model != MPI_EC_MONTGOMERY)
180 mpi_swap_cond (d->y, s->y, swap);
181 mpi_swap_cond (d->z, s->z, swap);
182 }
183
184
185 /*
186 * Move the point value of A from B, when DIR is 1.
187 * Move the point value of A to B, when DIR is 0.
188 *
189 * For the use case when DIR is 0, it's actually dummy operations (the
190 * value copied into B is not used after the call). The intention
191 * here is to be constant-time and to reduce possible EM signal/noise
192 * ratio (by decreasing signal and increasing noise).
193 *
194 * The word "tfr" comes from the mnemonic of Motorola 6809
195 * instruction, which does "transfer" a register value to another
196 * register. "TFR A,B" means "Transfer A to B".
197 */
198 static void
199 point_tfr (mpi_point_t a, mpi_point_t b, unsigned long dir,
200 mpi_ec_t ctx)
201 {
202 mpi_tfr (a->x, b->x, dir);
203 if (ctx->model != MPI_EC_MONTGOMERY)
204 mpi_tfr (a->y, b->y, dir);
205 mpi_tfr (a->z, b->z, dir);
206 }
207
208
209 /* Set the projective coordinates from POINT into X, Y, and Z. If a
210 coordinate is not required, X, Y, or Z may be passed as NULL. */
211 void
212 _gcry_mpi_point_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z,
213 mpi_point_t point)
214 {
215 if (x)
216 mpi_set (x, point->x);
217 if (y)
218 mpi_set (y, point->y);
219 if (z)
220 mpi_set (z, point->z);
221 }
222
223
224 /* Set the projective coordinates from POINT into X, Y, and Z and
225 release POINT. If a coordinate is not required, X, Y, or Z may be
226 passed as NULL. */
227 void
228 _gcry_mpi_point_snatch_get (gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z,
229 mpi_point_t point)
230 {
231 mpi_snatch (x, point->x);
232 mpi_snatch (y, point->y);
233 mpi_snatch (z, point->z);
234 xfree (point);
235 }
236
237
238 /* Set the projective coordinates from X, Y, and Z into POINT. If a
239 coordinate is given as NULL, the value 0 is stored into point. If
240 POINT is given as NULL a new point object is allocated. Returns
241 POINT or the newly allocated point object. */
242 mpi_point_t
243 _gcry_mpi_point_set (mpi_point_t point,
244 gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z)
245 {
246 if (!point)
247 point = mpi_point_new (0);
248
249 if (x)
250 mpi_set (point->x, x);
251 else
252 mpi_clear (point->x);
253 if (y)
254 mpi_set (point->y, y);
255 else
256 mpi_clear (point->y);
257 if (z)
258 mpi_set (point->z, z);
259 else
260 mpi_clear (point->z);
261
262 return point;
263 }
264
265
266 /* Set the projective coordinates from X, Y, and Z into POINT. If a
267 coordinate is given as NULL, the value 0 is stored into point. If
268 POINT is given as NULL a new point object is allocated. The
269 coordinates X, Y, and Z are released. Returns POINT or the newly
270 allocated point object. */
271 mpi_point_t
272 _gcry_mpi_point_snatch_set (mpi_point_t point,
273 gcry_mpi_t x, gcry_mpi_t y, gcry_mpi_t z)
274 {
275 if (!point)
276 point = mpi_point_new (0);
277
278 if (x)
279 mpi_snatch (point->x, x);
280 else
281 mpi_clear (point->x);
282 if (y)
283 mpi_snatch (point->y, y);
284 else
285 mpi_clear (point->y);
286 if (z)
287 mpi_snatch (point->z, z);
288 else
289 mpi_clear (point->z);
290
291 return point;
292 }
293
294
295 /* W = W mod P. */
296 static void
297 ec_mod (gcry_mpi_t w, mpi_ec_t ec)
298 {
299 if (0 && ec->dialect == ECC_DIALECT_ED25519)
300 _gcry_mpi_ec_ed25519_mod (w);
301 else if (ec->t.p_barrett)
302 _gcry_mpi_mod_barrett (w, w, ec->t.p_barrett);
303 else
304 _gcry_mpi_mod (w, w, ec->p);
305
306 if ((ec->flags & GCRYECC_FLAG_LEAST_LEAK))
307 w->nlimbs = ec->p->nlimbs;
308 }
309
310 static void
311 ec_addm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
312 {
313 mpi_add (w, u, v);
314 ctx->mod (w, ctx);
315 }
316
317 static void
318 ec_subm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ec)
319 {
320 mpi_sub (w, u, v);
321 while (w->sign)
322 mpi_add (w, w, ec->p);
323 /*ctx->mod (w, ec);*/
324 }
325
326 static void
327 ec_mulm (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
328 {
329 mpi_mul (w, u, v);
330 ctx->mod (w, ctx);
331 }
332
333 static void
334 ec_addm_lli (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
335 {
336 mpi_limb_t cy1, cy2;
337
338 w->nlimbs = ctx->p->nlimbs;
339 cy1 = _gcry_mpih_add_lli (w->d, u->d, v->d, ctx->p->nlimbs);
340 cy2 = _gcry_mpih_sub_n (w->d, w->d, ctx->p->d, ctx->p->nlimbs);
341 _gcry_mpih_add_n_cond (w->d, w->d, ctx->p->d, ctx->p->nlimbs,
342 ~cy1 &
343 ((-cy2) >> (BITS_PER_MPI_LIMB - 1)));
344 }
345
346 static void
347 ec_subm_lli (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ec)
348 {
349 mpi_limb_t cy;
350
351 w->nlimbs = ec->p->nlimbs;
352 cy = _gcry_mpih_sub_n (w->d, u->d, v->d, ec->p->nlimbs);
353 _gcry_mpih_add_n_cond (w->d, w->d, ec->p->d, ec->p->nlimbs,
354 (-cy) >> (BITS_PER_MPI_LIMB - 1));
355 }
356
357 static void
358 ec_mulm_lli (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
359 {
360 mpi_ptr_t wp;
361 mpi_ptr_t tmp_limb = NULL;
362 unsigned int tmp_limb_nlimbs = 0;
363 mpi_limb_t cy;
364 mpi_size_t usize, vsize, wsize;
365
366 usize = u->nlimbs;
367 vsize = v->nlimbs;
368 wsize = usize + vsize;
369 mpi_resize (w, wsize);
370 w->nlimbs = wsize;
371 if (w->d == u->d || w->d == v->d)
372 {
373 tmp_limb_nlimbs = wsize;
374 wp = tmp_limb = mpi_alloc_limb_space (tmp_limb_nlimbs, 0);
375 }
376 else
377 wp = w->d;
378 cy = _gcry_mpih_mul_lli (wp, u->d, usize, v->d, vsize);
379 wp[w->nlimbs - 1] = cy;
380 if (tmp_limb)
381 {
382 MPN_COPY (w->d, tmp_limb, wsize);
383 _gcry_mpi_free_limb_space (tmp_limb, tmp_limb_nlimbs);
384 }
385 ctx->mod (w, ctx);
386 }
387
388 /* W = 2 * U mod P. */
389 static void
390 ec_mul2 (gcry_mpi_t w, gcry_mpi_t u, mpi_ec_t ctx)
391 {
392 ec_addm (w, u, u, ctx);
393 }
394
395 static void
396 ec_powm (gcry_mpi_t w, const gcry_mpi_t b, const gcry_mpi_t e,
397 mpi_ec_t ctx)
398 {
399 mpi_powm (w, b, e, ctx->p);
400 /* _gcry_mpi_abs (w); */
401 }
402
403
404 /* Shortcut for
405 ec_powm (B, B, mpi_const (MPI_C_TWO), ctx);
406 for easier optimization. */
407 static void
408 ec_pow2 (gcry_mpi_t w, const gcry_mpi_t b, mpi_ec_t ctx)
409 {
410 /* Using mpi_mul is slightly faster (at least on amd64). */
411 /* mpi_powm (w, b, mpi_const (MPI_C_TWO), ctx->p); */
412 ec_mulm (w, b, b, ctx);
413 }
414
415
416 static void
417 ec_invm (gcry_mpi_t x, gcry_mpi_t a, mpi_ec_t ctx)
418 {
419 if (!mpi_invm (x, a, ctx->p))
420 {
421 log_error ("ec_invm: inverse does not exist:\n");
422 log_mpidump (" a", a);
423 log_mpidump (" p", ctx->p);
424 }
425 }
426
427 /* Routines for 2^255 - 19. */
428
429 #define LIMB_SIZE_25519 ((256+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB)
430
431 static void
432 ec_addm_25519 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
433 {
434 mpi_ptr_t wp, up, vp;
435 mpi_size_t wsize = LIMB_SIZE_25519;
436 mpi_limb_t n[LIMB_SIZE_25519];
437 mpi_limb_t borrow;
438
439 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
440 log_bug ("addm_25519: different sizes\n");
441
442 up = u->d;
443 vp = v->d;
444 wp = w->d;
445
446 _gcry_mpih_add_n (wp, up, vp, wsize);
447 borrow = _gcry_mpih_sub_n (n, wp, ctx->p->d, wsize);
448 mpih_set_cond (wp, n, wsize, mpih_limb_is_zero (borrow));
449 wp[LIMB_SIZE_25519-1] &= ~((mpi_limb_t)1 << (255 % BITS_PER_MPI_LIMB));
450 }
451
452 static void
453 ec_subm_25519 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
454 {
455 mpi_ptr_t wp, up, vp;
456 mpi_size_t wsize = LIMB_SIZE_25519;
457 mpi_limb_t n[LIMB_SIZE_25519];
458 mpi_limb_t borrow;
459
460 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
461 log_bug ("subm_25519: different sizes\n");
462
463 up = u->d;
464 vp = v->d;
465 wp = w->d;
466
467 borrow = _gcry_mpih_sub_n (wp, up, vp, wsize);
468 _gcry_mpih_add_n (n, wp, ctx->p->d, wsize);
469 mpih_set_cond (wp, n, wsize, mpih_limb_is_not_zero (borrow));
470 wp[LIMB_SIZE_25519-1] &= ~((mpi_limb_t)1 << (255 % BITS_PER_MPI_LIMB));
471 }
472
473 static void
474 ec_mulm_25519 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
475 {
476 mpi_ptr_t wp, up, vp;
477 mpi_size_t wsize = LIMB_SIZE_25519;
478 mpi_limb_t n[LIMB_SIZE_25519*2];
479 mpi_limb_t cy;
480 int msb;
481
482 (void)ctx;
483 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
484 log_bug ("mulm_25519: different sizes\n");
485
486 up = u->d;
487 vp = v->d;
488 wp = w->d;
489
490 _gcry_mpih_mul_n (n, up, vp, wsize);
491 memcpy (wp, n, wsize * BYTES_PER_MPI_LIMB);
492 wp[LIMB_SIZE_25519-1] &= ~((mpi_limb_t)1 << (255 % BITS_PER_MPI_LIMB));
493
494 _gcry_mpih_rshift (n, n+LIMB_SIZE_25519-1, LIMB_SIZE_25519+1,
495 (255 % BITS_PER_MPI_LIMB));
496
497 cy = _gcry_mpih_addmul_1 (wp, n, wsize, 19);
498
499 memset (n, 0, wsize * BYTES_PER_MPI_LIMB);
500 msb = (wp[LIMB_SIZE_25519-1] >> (255 % BITS_PER_MPI_LIMB));
501 n[0] = (cy * 2 + msb) * 19;
502 wp[LIMB_SIZE_25519-1] &= ~((mpi_limb_t)1 << (255 % BITS_PER_MPI_LIMB));
503 _gcry_mpih_add_n (wp, wp, n, wsize);
504
505 cy = _gcry_mpih_sub_n (n, wp, ctx->p->d, wsize);
506 mpih_set_cond (wp, n, wsize, mpih_limb_is_zero (cy));
507 }
508
509 static void
510 ec_mul2_25519 (gcry_mpi_t w, gcry_mpi_t u, mpi_ec_t ctx)
511 {
512 ec_addm_25519 (w, u, u, ctx);
513 }
514
515 static void
516 ec_pow2_25519 (gcry_mpi_t w, const gcry_mpi_t b, mpi_ec_t ctx)
517 {
518 ec_mulm_25519 (w, b, b, ctx);
519 }
520
521 /* Routines for 2^448 - 2^224 - 1. */
522
523 #define LIMB_SIZE_448 ((448+BITS_PER_MPI_LIMB-1)/BITS_PER_MPI_LIMB)
524 #define LIMB_SIZE_HALF_448 ((LIMB_SIZE_448+1)/2)
525
526 static void
527 ec_addm_448 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
528 {
529 mpi_ptr_t wp, up, vp;
530 mpi_size_t wsize = LIMB_SIZE_448;
531 mpi_limb_t n[LIMB_SIZE_448];
532 mpi_limb_t cy;
533
534 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
535 log_bug ("addm_448: different sizes\n");
536
537 up = u->d;
538 vp = v->d;
539 wp = w->d;
540
541 cy = _gcry_mpih_add_n (wp, up, vp, wsize);
542 _gcry_mpih_sub_n (n, wp, ctx->p->d, wsize);
543 mpih_set_cond (wp, n, wsize, mpih_limb_is_not_zero (cy));
544 }
545
546 static void
547 ec_subm_448 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
548 {
549 mpi_ptr_t wp, up, vp;
550 mpi_size_t wsize = LIMB_SIZE_448;
551 mpi_limb_t n[LIMB_SIZE_448];
552 mpi_limb_t borrow;
553
554 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
555 log_bug ("subm_448: different sizes\n");
556
557 up = u->d;
558 vp = v->d;
559 wp = w->d;
560
561 borrow = _gcry_mpih_sub_n (wp, up, vp, wsize);
562 _gcry_mpih_add_n (n, wp, ctx->p->d, wsize);
563 mpih_set_cond (wp, n, wsize, mpih_limb_is_not_zero (borrow));
564 }
565
566 static void
567 ec_mulm_448 (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx)
568 {
569 mpi_ptr_t wp, up, vp;
570 mpi_size_t wsize = LIMB_SIZE_448;
571 mpi_limb_t n[LIMB_SIZE_448*2];
572 mpi_limb_t a2[LIMB_SIZE_HALF_448];
573 mpi_limb_t a3[LIMB_SIZE_HALF_448];
574 mpi_limb_t b0[LIMB_SIZE_HALF_448];
575 mpi_limb_t b1[LIMB_SIZE_HALF_448];
576 mpi_limb_t cy;
577
578 if (w->nlimbs != wsize || u->nlimbs != wsize || v->nlimbs != wsize)
579 log_bug ("mulm_448: different sizes\n");
580
581 up = u->d;
582 vp = v->d;
583 wp = w->d;
584
585 _gcry_mpih_mul_n (n, up, vp, wsize);
586
587 memcpy (b0, n, LIMB_SIZE_HALF_448 * BYTES_PER_MPI_LIMB);
588 memcpy (a2, n + wsize, LIMB_SIZE_HALF_448 * BYTES_PER_MPI_LIMB);
589
590 #if (LIMB_SIZE_HALF_448 > LIMB_SIZE_448/2)
591 b0[LIMB_SIZE_HALF_448-1] &= ((mpi_limb_t)1UL<<32)-1;
592 a2[LIMB_SIZE_HALF_448-1] &= ((mpi_limb_t)1UL<<32)-1;
593 _gcry_mpih_rshift (b1, n + wsize/2, LIMB_SIZE_HALF_448, 32);
594 _gcry_mpih_rshift (a3, n + wsize + wsize/2, LIMB_SIZE_HALF_448, 32);
595 #else
596 memcpy (b1, n + wsize/2, LIMB_SIZE_HALF_448 * BYTES_PER_MPI_LIMB);
597 memcpy (a3, n + wsize + wsize/2, LIMB_SIZE_HALF_448 * BYTES_PER_MPI_LIMB);
598 #endif
599
600 cy = _gcry_mpih_add_n (b0, b0, a2, LIMB_SIZE_HALF_448);
601 cy += _gcry_mpih_add_n (wp, b0, a3, LIMB_SIZE_HALF_448);
602 #if (LIMB_SIZE_HALF_448 > LIMB_SIZE_448/2)
603 cy = wp[LIMB_SIZE_HALF_448-1] >> 32;
604 wp[LIMB_SIZE_HALF_448-1] &= (((mpi_limb_t)1UL <<32)-1);
605 #endif
606 memset (b0, 0, LIMB_SIZE_HALF_448 * BYTES_PER_MPI_LIMB);
607 b0[0] = cy;
608
609 cy = _gcry_mpih_add_n (b1, b1, b0, LIMB_SIZE_HALF_448);
610 cy += _gcry_mpih_lshift (a3, a3, LIMB_SIZE_HALF_448, 1);
611 cy += _gcry_mpih_add_n (b1, b1, a2, LIMB_SIZE_HALF_448);
612 cy += _gcry_mpih_add_n (b1, b1, a3, LIMB_SIZE_HALF_448);
613 #if (LIMB_SIZE_HALF_448 > LIMB_SIZE_448/2)
614 cy = _gcry_mpih_rshift (b1, b1, LIMB_SIZE_HALF_448, 32);
615 wp[LIMB_SIZE_HALF_448-1] |= cy;
616 #endif
617 memcpy (wp + LIMB_SIZE_HALF_448, b1, (wsize / 2) * BYTES_PER_MPI_LIMB);
618
619 #if (LIMB_SIZE_HALF_448 > LIMB_SIZE_448/2)
620 cy = b1[LIMB_SIZE_HALF_448-1];
621 #endif
622
623 memset (n, 0, wsize * BYTES_PER_MPI_LIMB);
624
625 #if (LIMB_SIZE_HALF_448 > LIMB_SIZE_448/2)
626 n[LIMB_SIZE_HALF_448-1] = cy << 32;
627 #else
628 n[LIMB_SIZE_HALF_448] = cy;
629 #endif
630 n[0] = cy;
631 _gcry_mpih_add_n (wp, wp, n, wsize);
632
633 cy = _gcry_mpih_sub_n (n, wp, ctx->p->d, wsize);
634 mpih_set_cond (wp, n, wsize, mpih_limb_is_zero (cy));
635 }
636
637 static void
638 ec_mul2_448 (gcry_mpi_t w, gcry_mpi_t u, mpi_ec_t ctx)
639 {
640 ec_addm_448 (w, u, u, ctx);
641 }
642
643 static void
644 ec_pow2_448 (gcry_mpi_t w, const gcry_mpi_t b, mpi_ec_t ctx)
645 {
646 ec_mulm_448 (w, b, b, ctx);
647 }
648
649
650 /* Fast reduction for secp256k1 */
651 static void
652 ec_secp256k1_mod (gcry_mpi_t w, mpi_ec_t ctx)
653 {
654 mpi_limb_t s[(256 + BITS_PER_MPI_LIMB - 1) / BITS_PER_MPI_LIMB + 1];
655 mpi_limb_t n[DIM(s)];
656 const mpi_size_t wsize = DIM(s) - 1;
657 mpi_limb_t cy, borrow;
658 mpi_ptr_t wp;
659
660 RESIZE_AND_CLEAR_IF_NEEDED (w, wsize * 2);
661
662 wp = w->d;
663
664 /* mod P (2^256 - 2^32 - 977) */
665
666 /* first pass of reduction */
667 memcpy (n, wp + wsize, wsize * BYTES_PER_MPI_LIMB);
668 #if BITS_PER_MPI_LIMB == 64
669 s[wsize] = _gcry_mpih_lshift (s, wp + wsize, wsize, 32);
670 #else
671 s[0] = 0;
672 memcpy (s + 1, wp + wsize, wsize * BYTES_PER_MPI_LIMB);
673 #endif
674 wp[wsize] = _gcry_mpih_addmul_1 (wp, n, wsize, 977);
675 cy = _gcry_mpih_add_n (wp, wp, s, wsize + 1);
676
677 /* second pass of reduction */
678 #if BITS_PER_MPI_LIMB == 64
679 /* cy == 0 */
680 memset (n + 1, 0, (wsize - 1) * BYTES_PER_MPI_LIMB);
681 umul_ppmm(n[1], n[0], wp[wsize], ((mpi_limb_t)1 << 32) + 977);
682 #else
683 memset (n + 2, 0, (wsize - 2) * BYTES_PER_MPI_LIMB);
684 umul_ppmm(n[1], n[0], wp[wsize], 977);
685 add_ssaaaa(n[2], n[1], 0, n[1], 0, cy * 977);
686 add_ssaaaa(n[2], n[1], n[2], n[1], cy, wp[wsize]);
687 #endif
688 cy = _gcry_mpih_add_n (wp, wp, n, wsize);
689
690 borrow = _gcry_mpih_sub_n (s, wp, ctx->p->d, wsize);
691 mpih_set_cond (wp, s, wsize,
692 mpih_limb_is_not_zero (cy) | mpih_limb_is_zero (borrow));
693
694 w->nlimbs = wsize;
695 if (!(ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
696 MPN_NORMALIZE (wp, w->nlimbs);
697 }
698
699
700 struct field_table {
701 const char *p;
702
703 /* computation routines for the field. */
704 void (* addm) (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx);
705 void (* subm) (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx);
706 void (* mulm) (gcry_mpi_t w, gcry_mpi_t u, gcry_mpi_t v, mpi_ec_t ctx);
707 void (* mul2) (gcry_mpi_t w, gcry_mpi_t u, mpi_ec_t ctx);
708 void (* pow2) (gcry_mpi_t w, const gcry_mpi_t b, mpi_ec_t ctx);
709 void (* mod) (gcry_mpi_t w, mpi_ec_t ctx);
710 };
711
712 static const struct field_table field_table[] = {
713 {
714 "0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFED",
715 ec_addm_25519,
716 ec_subm_25519,
717 ec_mulm_25519,
718 ec_mul2_25519,
719 ec_pow2_25519,
720 NULL
721 },
722 {
723 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
724 "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
725 ec_addm_448,
726 ec_subm_448,
727 ec_mulm_448,
728 ec_mul2_448,
729 ec_pow2_448,
730 NULL
731 },
732 {
733 "0xfffffffffffffffffffffffffffffffeffffffffffffffff",
734 NULL,
735 NULL,
736 NULL,
737 NULL,
738 NULL,
739 _gcry_mpi_ec_nist192_mod
740 },
741 {
742 "0xffffffffffffffffffffffffffffffff000000000000000000000001",
743 NULL,
744 NULL,
745 NULL,
746 NULL,
747 NULL,
748 _gcry_mpi_ec_nist224_mod
749 },
750 {
751 "0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
752 NULL,
753 NULL,
754 NULL,
755 NULL,
756 NULL,
757 _gcry_mpi_ec_nist256_mod
758 },
759 {
760 "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
761 "ffffffff0000000000000000ffffffff",
762 NULL,
763 NULL,
764 NULL,
765 NULL,
766 NULL,
767 _gcry_mpi_ec_nist384_mod
768 },
769 {
770 "0x01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
771 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
772 NULL,
773 NULL,
774 NULL,
775 NULL,
776 NULL,
777 _gcry_mpi_ec_nist521_mod
778 },
779 {
780 "0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
781 NULL,
782 NULL,
783 NULL,
784 NULL,
785 NULL,
786 ec_secp256k1_mod
787 },
788 { NULL, NULL, NULL, NULL, NULL, NULL },
789 };
790
791 static gcry_mpi_t field_table_mpis[DIM(field_table)];
792
793
794 /* Force recomputation of all helper variables. */
795 void
796 _gcry_mpi_ec_get_reset (mpi_ec_t ec)
797 {
798 ec->t.valid.a_is_pminus3 = 0;
799 ec->t.valid.two_inv_p = 0;
800 }
801
802
803 /* Accessor for helper variable. */
804 static int
805 ec_get_a_is_pminus3 (mpi_ec_t ec)
806 {
807 gcry_mpi_t tmp;
808
809 if (!ec->t.valid.a_is_pminus3)
810 {
811 ec->t.valid.a_is_pminus3 = 1;
812 tmp = mpi_alloc_like (ec->p);
813 mpi_sub (tmp, ec->p, ec->a);
814 ec->t.a_is_pminus3 = !mpi_cmp_ui (tmp, 3);
815 mpi_free (tmp);
816 }
817
818 return ec->t.a_is_pminus3;
819 }
820
821
822 /* Accessor for helper variable. */
823 static gcry_mpi_t
824 ec_get_two_inv_p (mpi_ec_t ec)
825 {
826 if (!ec->t.valid.two_inv_p)
827 {
828 ec->t.valid.two_inv_p = 1;
829 if (!ec->t.two_inv_p)
830 ec->t.two_inv_p = mpi_new (ec->nbits);
831 ec_invm (ec->t.two_inv_p, mpi_const (MPI_C_TWO), ec);
832 }
833 return ec->t.two_inv_p;
834 }
835
836
837 static const char *const curve25519_bad_points[] = {
838 "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffed",
839 "0x0000000000000000000000000000000000000000000000000000000000000000",
840 "0x0000000000000000000000000000000000000000000000000000000000000001",
841 "0x00b8495f16056286fdb1329ceb8d09da6ac49ff1fae35616aeb8413b7c7aebe0",
842 "0x57119fd0dd4e22d8868e1c58c45c44045bef839c55b1d0b1248c50a3bc959c5f",
843 "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffec",
844 "0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffee",
845 NULL
846 };
847
848
849 static const char *const curve448_bad_points[] = {
850 "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
851 "ffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
852 "0x00000000000000000000000000000000000000000000000000000000"
853 "00000000000000000000000000000000000000000000000000000000",
854 "0x00000000000000000000000000000000000000000000000000000000"
855 "00000000000000000000000000000000000000000000000000000001",
856 "0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffe"
857 "fffffffffffffffffffffffffffffffffffffffffffffffffffffffe",
858 "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
859 "00000000000000000000000000000000000000000000000000000000",
860 NULL
861 };
862
863 static const char *const *bad_points_table[] = {
864 curve25519_bad_points,
865 curve448_bad_points,
866 };
867
868 static gcry_mpi_t
869 scanval (const char *string)
870 {
871 gpg_err_code_t rc;
872 gcry_mpi_t val;
873
874 rc = _gcry_mpi_scan (&val, GCRYMPI_FMT_HEX, string, 0, NULL);
875 if (rc)
876 log_fatal ("scanning ECC parameter failed: %s\n", gpg_strerror (rc));
877 return val;
878 }
879
880
881 /* This function initialized a context for elliptic curve based on the
882 field GF(p). P is the prime specifying this field, A is the first
883 coefficient. CTX is expected to be zeroized. */
884 static void
885 ec_p_init (mpi_ec_t ctx, enum gcry_mpi_ec_models model,
886 enum ecc_dialects dialect,
887 int flags,
888 gcry_mpi_t p, gcry_mpi_t a, gcry_mpi_t b)
889 {
890 int i;
891 static int use_barrett;
892
893 if (!use_barrett)
894 {
895 if (getenv ("GCRYPT_BARRETT"))
896 use_barrett = 1;
897 else
898 use_barrett = -1;
899 }
900
901 /* Fixme: Do we want to check some constraints? e.g. a < p */
902
903 ctx->model = model;
904 ctx->dialect = dialect;
905 ctx->flags = flags;
906 ctx->nbits = mpi_get_nbits (p);
907 ctx->p = mpi_copy (p);
908 ctx->a = mpi_copy (a);
909 ctx->b = mpi_copy (b);
910
911 ctx->t.p_barrett = use_barrett > 0? _gcry_mpi_barrett_init (ctx->p, 0):NULL;
912
913 _gcry_mpi_ec_get_reset (ctx);
914
915 if (model == MPI_EC_MONTGOMERY)
916 {
917 for (i=0; i< DIM(bad_points_table); i++)
918 {
919 gcry_mpi_t p_candidate = scanval (bad_points_table[i][0]);
920 int match_p = !mpi_cmp (ctx->p, p_candidate);
921 int j;
922
923 mpi_free (p_candidate);
924 if (!match_p)
925 continue;
926
927 for (j=0; j < DIM(ctx->t.scratch) && bad_points_table[i][j]; j++)
928 ctx->t.scratch[j] = scanval (bad_points_table[i][j]);
929 }
930 }
931 else
932 {
933 /* Allocate scratch variables. */
934 for (i=0; i< DIM(ctx->t.scratch); i++)
935 ctx->t.scratch[i] = mpi_alloc_like (ctx->p);
936 }
937
938 ctx->addm = ec_addm;
939 ctx->subm = ec_subm;
940 ctx->mulm = ec_mulm;
941 ctx->mul2 = ec_mul2;
942 ctx->pow2 = ec_pow2;
943 ctx->mod = ec_mod;
944
945 for (i=0; field_table[i].p; i++)
946 {
947 gcry_mpi_t f_p;
948 gpg_err_code_t rc;
949
950 if (field_table_mpis[i] == NULL)
951 {
952 rc = _gcry_mpi_scan (&f_p, GCRYMPI_FMT_HEX, field_table[i].p, 0,
953 NULL);
954 if (rc)
955 log_fatal ("scanning ECC parameter failed: %s\n",
956 gpg_strerror (rc));
957 field_table_mpis[i] = f_p; /* cache */
958 }
959 else
960 {
961 f_p = field_table_mpis[i];
962 }
963
964 if (!mpi_cmp (p, f_p))
965 {
966 ctx->addm = field_table[i].addm ? field_table[i].addm : ctx->addm;
967 ctx->subm = field_table[i].subm ? field_table[i].subm : ctx->subm;
968 ctx->mulm = field_table[i].mulm ? field_table[i].mulm : ctx->mulm;
969 ctx->mul2 = field_table[i].mul2 ? field_table[i].mul2 : ctx->mul2;
970 ctx->pow2 = field_table[i].pow2 ? field_table[i].pow2 : ctx->pow2;
971 ctx->mod = field_table[i].mod ? field_table[i].mod : ctx->mod;
972 break;
973 }
974 }
975
976 if (field_table[i].p || (flags & GCRYECC_FLAG_LEAST_LEAK))
977 {
978 if (ctx->a)
979 {
980 mpi_resize (ctx->a, ctx->p->nlimbs);
981 ctx->a->nlimbs = ctx->p->nlimbs;
982 }
983
984 if (ctx->b)
985 {
986 mpi_resize (ctx->b, ctx->p->nlimbs);
987 ctx->b->nlimbs = ctx->p->nlimbs;
988 }
989
990 for (i=0; i< DIM(ctx->t.scratch) && ctx->t.scratch[i]; i++)
991 ctx->t.scratch[i]->nlimbs = ctx->p->nlimbs;
992 }
993
994 /* Prepare for fast reduction. */
995 /* FIXME: need a test for NIST values. However it does not gain us
996 any real advantage, for 384 bits it is actually slower than using
997 mpi_mulm. */
998 /* ctx->nist_nbits = mpi_get_nbits (ctx->p); */
999 /* if (ctx->nist_nbits == 192) */
1000 /* { */
1001 /* for (i=0; i < 4; i++) */
1002 /* ctx->s[i] = mpi_new (192); */
1003 /* ctx->c = mpi_new (192*2); */
1004 /* } */
1005 /* else if (ctx->nist_nbits == 384) */
1006 /* { */
1007 /* for (i=0; i < 10; i++) */
1008 /* ctx->s[i] = mpi_new (384); */
1009 /* ctx->c = mpi_new (384*2); */
1010 /* } */
1011 }
1012
1013
1014 static void
1015 ec_deinit (void *opaque)
1016 {
1017 mpi_ec_t ctx = opaque;
1018 int i;
1019
1020 _gcry_mpi_barrett_free (ctx->t.p_barrett);
1021
1022 /* Domain parameter. */
1023 mpi_free (ctx->p);
1024 mpi_free (ctx->a);
1025 mpi_free (ctx->b);
1026 _gcry_mpi_point_release (ctx->G);
1027 mpi_free (ctx->n);
1028
1029 /* The key. */
1030 _gcry_mpi_point_release (ctx->Q);
1031 mpi_free (ctx->d);
1032
1033 /* Private data of ec.c. */
1034 mpi_free (ctx->t.two_inv_p);
1035
1036 for (i=0; i< DIM(ctx->t.scratch); i++)
1037 mpi_free (ctx->t.scratch[i]);
1038
1039 /* if (ctx->nist_nbits == 192) */
1040 /* { */
1041 /* for (i=0; i < 4; i++) */
1042 /* mpi_free (ctx->s[i]); */
1043 /* mpi_free (ctx->c); */
1044 /* } */
1045 /* else if (ctx->nist_nbits == 384) */
1046 /* { */
1047 /* for (i=0; i < 10; i++) */
1048 /* mpi_free (ctx->s[i]); */
1049 /* mpi_free (ctx->c); */
1050 /* } */
1051 }
1052
1053
1054 /* This function returns a new context for elliptic curve based on the
1055 field GF(p). P is the prime specifying this field, A is the first
1056 coefficient, B is the second coefficient, and MODEL is the model
1057 for the curve. This function is only used within Libgcrypt and not
1058 part of the public API.
1059
1060 This context needs to be released using _gcry_mpi_ec_free. */
1061 mpi_ec_t
1062 _gcry_mpi_ec_p_internal_new (enum gcry_mpi_ec_models model,
1063 enum ecc_dialects dialect,
1064 int flags,
1065 gcry_mpi_t p, gcry_mpi_t a, gcry_mpi_t b)
1066 {
1067 mpi_ec_t ctx;
1068
1069 ctx = xcalloc (1, sizeof *ctx);
1070 ec_p_init (ctx, model, dialect, flags, p, a, b);
1071
1072 return ctx;
1073 }
1074
1075
1076 /* This is a variant of _gcry_mpi_ec_p_internal_new which returns an
1077 public context and does some error checking on the supplied
1078 arguments. On success the new context is stored at R_CTX and 0 is
1079 returned; on error NULL is stored at R_CTX and an error code is
1080 returned.
1081
1082 The context needs to be released using gcry_ctx_release. */
1083 gpg_err_code_t
1084 _gcry_mpi_ec_p_new (gcry_ctx_t *r_ctx,
1085 enum gcry_mpi_ec_models model,
1086 enum ecc_dialects dialect,
1087 int flags,
1088 gcry_mpi_t p, gcry_mpi_t a, gcry_mpi_t b)
1089 {
1090 gcry_ctx_t ctx;
1091 mpi_ec_t ec;
1092
1093 *r_ctx = NULL;
1094 if (!p || !a)
1095 return GPG_ERR_EINVAL;
1096
1097 ctx = _gcry_ctx_alloc (CONTEXT_TYPE_EC, sizeof *ec, ec_deinit, NULL);
1098 if (!ctx)
1099 return gpg_err_code_from_syserror ();
1100 ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
1101 ec_p_init (ec, model, dialect, flags, p, a, b);
1102
1103 *r_ctx = ctx;
1104 return 0;
1105 }
1106
1107
1108 void
1109 _gcry_mpi_ec_free (mpi_ec_t ctx)
1110 {
1111 if (ctx)
1112 {
1113 ec_deinit (ctx);
1114 xfree (ctx);
1115 }
1116 }
1117
1118
1119 gcry_mpi_t
1120 _gcry_mpi_ec_get_mpi (const char *name, gcry_ctx_t ctx, int copy)
1121 {
1122 mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
1123
1124 return _gcry_ecc_get_mpi (name, ec, copy);
1125 }
1126
1127
1128 gcry_mpi_point_t
1129 _gcry_mpi_ec_get_point (const char *name, gcry_ctx_t ctx, int copy)
1130 {
1131 mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
1132
1133 (void)copy; /* Not used. */
1134
1135 return _gcry_ecc_get_point (name, ec);
1136 }
1137
1138
1139 gpg_err_code_t
1140 _gcry_mpi_ec_set_mpi (const char *name, gcry_mpi_t newvalue,
1141 gcry_ctx_t ctx)
1142 {
1143 mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
1144
1145 return _gcry_ecc_set_mpi (name, newvalue, ec);
1146 }
1147
1148
1149 gpg_err_code_t
1150 _gcry_mpi_ec_set_point (const char *name, gcry_mpi_point_t newvalue,
1151 gcry_ctx_t ctx)
1152 {
1153 mpi_ec_t ec = _gcry_ctx_get_pointer (ctx, CONTEXT_TYPE_EC);
1154
1155 return _gcry_ecc_set_point (name, newvalue, ec);
1156 }
1157
1158
1159 /* Given an encoded point in the MPI VALUE and a context EC, decode
1160 * the point according to the context and store it in RESULT. On
1161 * error an error code is return but RESULT might have been changed.
1162 * If no context is given the function tries to decode VALUE by
1163 * assuming a 0x04 prefixed uncompressed encoding. */
1164 gpg_err_code_t
1165 _gcry_mpi_ec_decode_point (mpi_point_t result, gcry_mpi_t value, mpi_ec_t ec)
1166 {
1167 gpg_err_code_t rc;
1168
1169 if (ec
1170 && (ec->dialect == ECC_DIALECT_ED25519
1171 || (ec->model == MPI_EC_EDWARDS
1172 && ec->dialect == ECC_DIALECT_SAFECURVE)))
1173 rc = _gcry_ecc_eddsa_decodepoint (value, ec, result, NULL, NULL);
1174 else if (ec && ec->model == MPI_EC_MONTGOMERY)
1175 rc = _gcry_ecc_mont_decodepoint (value, ec, result);
1176 else
1177 rc = _gcry_ecc_sec_decodepoint (value, ec, result);
1178
1179 return rc;
1180 }
1181
1182
1183 /* Compute the affine coordinates from the projective coordinates in
1184 POINT. Set them into X and Y. If one coordinate is not required,
1185 X or Y may be passed as NULL. CTX is the usual context. Returns: 0
1186 on success or !0 if POINT is at infinity. */
1187 int
1188 _gcry_mpi_ec_get_affine (gcry_mpi_t x, gcry_mpi_t y, mpi_point_t point,
1189 mpi_ec_t ctx)
1190 {
1191 if (!mpi_cmp_ui (point->z, 0))
1192 return -1;
1193
1194 switch (ctx->model)
1195 {
1196 case MPI_EC_WEIERSTRASS: /* Using Jacobian coordinates. */
1197 {
1198 gcry_mpi_t z1, z2, z3;
1199 int saved_flags;
1200
1201 if (!mpi_cmp_ui (point->z, 1))
1202 {
1203 if (x)
1204 mpi_set (x, point->x);
1205 if (y)
1206 mpi_set (y, point->y);
1207 return 0;
1208 }
1209
1210 /* Since we use LLI computations, the LL flag should be set. */
1211 saved_flags = ctx->flags;
1212 ctx->flags |= GCRYECC_FLAG_LEAST_LEAK;
1213
1214 z1 = mpi_new (ctx->nbits);
1215 z2 = mpi_new (ctx->nbits);
1216 ec_invm (z1, point->z, ctx); /* z1 = z^(-1) mod p */
1217 ec_mulm_lli (z2, z1, z1, ctx); /* z2 = z^(-2) mod p */
1218
1219 if (x)
1220 {
1221 mpi_set (x, point->x);
1222 mpi_resize (x, ctx->p->nlimbs);
1223 x->nlimbs = ctx->p->nlimbs;
1224 ec_mulm_lli (x, x, z2, ctx);
1225 }
1226
1227 if (y)
1228 {
1229 mpi_set (y, point->y);
1230 mpi_resize (y, ctx->p->nlimbs);
1231 y->nlimbs = ctx->p->nlimbs;
1232 z3 = mpi_new (ctx->nbits);
1233 ec_mulm_lli (z3, z2, z1, ctx); /* z3 = z^(-3) mod p */
1234 ec_mulm_lli (y, y, z3, ctx);
1235 mpi_free (z3);
1236 }
1237
1238 ctx->flags = saved_flags;
1239
1240 mpi_free (z2);
1241 mpi_free (z1);
1242 }
1243 return 0;
1244
1245 case MPI_EC_MONTGOMERY:
1246 {
1247 if (x)
1248 mpi_set (x, point->x);
1249
1250 if (y)
1251 {
1252 log_fatal ("%s: Getting Y-coordinate on %s is not supported\n",
1253 "_gcry_mpi_ec_get_affine", "Montgomery");
1254 return -1;
1255 }
1256 }
1257 return 0;
1258
1259 case MPI_EC_EDWARDS:
1260 {
1261 gcry_mpi_t z;
1262
1263 if (!mpi_cmp_ui (point->z, 1))
1264 {
1265 if (x)
1266 mpi_set (x, point->x);
1267 if (y)
1268 mpi_set (y, point->y);
1269 return 0;
1270 }
1271
1272 z = mpi_new (ctx->nbits);
1273 ec_invm (z, point->z, ctx);
1274
1275 mpi_resize (z, ctx->p->nlimbs);
1276 z->nlimbs = ctx->p->nlimbs;
1277
1278 if (x)
1279 {
1280 mpi_resize (x, ctx->p->nlimbs);
1281 x->nlimbs = ctx->p->nlimbs;
1282 ctx->mulm (x, point->x, z, ctx);
1283 }
1284 if (y)
1285 {
1286 mpi_resize (y, ctx->p->nlimbs);
1287 y->nlimbs = ctx->p->nlimbs;
1288 ctx->mulm (y, point->y, z, ctx);
1289 }
1290
1291 _gcry_mpi_release (z);
1292 }
1293 return 0;
1294
1295 default:
1296 return -1;
1297 }
1298 }
1299
1300
1301
1302 /* RESULT = 2 * POINT (Weierstrass version). */
1303 static void
1304 dup_point_weierstrass (mpi_point_t result, mpi_point_t point, mpi_ec_t ctx)
1305 {
1306 #define x3 (result->x)
1307 #define y3 (result->y)
1308 #define z3 (result->z)
1309 #define t1 (ctx->t.scratch[0])
1310 #define t2 (ctx->t.scratch[1])
1311 #define t3 (ctx->t.scratch[2])
1312 #define l1 (ctx->t.scratch[3])
1313 #define l2 (ctx->t.scratch[4])
1314 #define l3 (ctx->t.scratch[5])
1315
1316 if (!mpi_cmp_ui (point->y, 0) || !mpi_cmp_ui (point->z, 0))
1317 {
1318 /* P_y == 0 || P_z == 0 => [1:1:0] */
1319 mpi_set_ui (x3, 1);
1320 mpi_set_ui (y3, 1);
1321 mpi_set_ui (z3, 0);
1322 }
1323 else
1324 {
1325 if (ec_get_a_is_pminus3 (ctx)) /* Use the faster case. */
1326 {
1327 /* L1 = 3(X - Z^2)(X + Z^2) */
1328 /* T1: used for Z^2. */
1329 /* T2: used for the right term. */
1330 ec_pow2 (t1, point->z, ctx);
1331 ec_subm (l1, point->x, t1, ctx);
1332 ec_addm (t3, l1, l1, ctx);
1333 ec_addm (l1, l1, t3, ctx);
1334 ec_addm (t2, point->x, t1, ctx);
1335 ec_mulm (l1, l1, t2, ctx);
1336 }
1337 else /* Standard case. */
1338 {
1339 /* L1 = 3X^2 + aZ^4 */
1340 /* T1: used for aZ^4. */
1341 ec_pow2 (l1, point->x, ctx);
1342 ec_addm (t3, l1, l1, ctx);
1343 ec_addm (l1, l1, t3, ctx);
1344 ec_pow2 (t1, point->z, ctx);
1345 ec_pow2 (t1, t1, ctx);
1346 ec_mulm (t1, t1, ctx->a, ctx);
1347 ec_addm (l1, l1, t1, ctx);
1348 }
1349 /* Z3 = 2YZ */
1350 ec_mulm (z3, point->y, point->z, ctx);
1351 ec_mul2 (z3, z3, ctx);
1352
1353 /* L2 = 4XY^2 */
1354 /* T2: used for Y2; required later. */
1355 ec_pow2 (t2, point->y, ctx);
1356 ec_mulm (l2, t2, point->x, ctx);
1357 ec_addm (l2, l2, l2, ctx);
1358 ec_addm (l2, l2, l2, ctx);
1359
1360 /* X3 = L1^2 - 2L2 */
1361 /* T1: used for L2^2. */
1362 ec_pow2 (x3, l1, ctx);
1363 ec_mul2 (t1, l2, ctx);
1364 ec_subm (x3, x3, t1, ctx);
1365
1366 /* L3 = 8Y^4 */
1367 /* T2: taken from above. */
1368 ec_pow2 (t2, t2, ctx);
1369 ec_addm (l3, t2, t2, ctx);
1370 ec_addm (l3, l3, l3, ctx);
1371 ec_addm (l3, l3, l3, ctx);
1372
1373 /* Y3 = L1(L2 - X3) - L3 */
1374 ec_subm (y3, l2, x3, ctx);
1375 ec_mulm (y3, y3, l1, ctx);
1376 ec_subm (y3, y3, l3, ctx);
1377 }
1378
1379 #undef x3
1380 #undef y3
1381 #undef z3
1382 #undef t1
1383 #undef t2
1384 #undef t3
1385 #undef l1
1386 #undef l2
1387 #undef l3
1388 }
1389
1390
1391 /* RESULT = 2 * POINT (Weierstrass version). */
1392 static void
1393 dup_point_weierstrass_lli (mpi_point_t result, mpi_point_t point, mpi_ec_t ctx)
1394 {
1395 #define x3 (result->x)
1396 #define y3 (result->y)
1397 #define z3 (result->z)
1398 #define t1 (ctx->t.scratch[0])
1399 #define t2 (ctx->t.scratch[1])
1400 #define t3 (ctx->t.scratch[2])
1401 #define l1 (ctx->t.scratch[3])
1402 #define l2 (ctx->t.scratch[4])
1403 #define l3 (ctx->t.scratch[5])
1404
1405 if (ec_get_a_is_pminus3 (ctx)) /* Use the faster case. */
1406 {
1407 /* L1 = 3(X - Z^2)(X + Z^2) */
1408 /* T1: used for Z^2. */
1409 /* T2: used for the right term. */
1410 ec_mulm_lli (t1, point->z, point->z, ctx);
1411 ec_subm_lli (l1, point->x, t1, ctx);
1412 ec_addm_lli (t3, l1, l1, ctx);
1413 ec_addm_lli (l1, l1, t3, ctx);
1414 ec_addm_lli (t2, point->x, t1, ctx);
1415 ec_mulm_lli (l1, l1, t2, ctx);
1416 }
1417 else /* Standard case. */
1418 {
1419 /* L1 = 3X^2 + aZ^4 */
1420 /* T1: used for aZ^4. */
1421 ec_mulm_lli (l1, point->x, point->x, ctx);
1422 ec_addm_lli (t3, l1, l1, ctx);
1423 ec_addm_lli (l1, l1, t3, ctx);
1424 ec_mulm_lli (t1, point->z, point->z, ctx);
1425 ec_mulm_lli (t1, t1, t1, ctx);
1426 ec_mulm_lli (t1, t1, ctx->a, ctx);
1427 ec_addm_lli (l1, l1, t1, ctx);
1428 }
1429 /* Z3 = 2YZ */
1430 ec_mulm_lli (z3, point->y, point->z, ctx);
1431 ec_addm_lli (z3, z3, z3, ctx);
1432
1433 /* L2 = 4XY^2 */
1434 /* T2: used for Y2; required later. */
1435 ec_mulm_lli (t2, point->y, point->y, ctx);
1436 ec_mulm_lli (l2, t2, point->x, ctx);
1437 ec_addm_lli (l2, l2, l2, ctx);
1438 ec_addm_lli (l2, l2, l2, ctx);
1439
1440 /* X3 = L1^2 - 2L2 */
1441 /* T1: used for L2^2. */
1442 ec_mulm_lli (x3, l1, l1, ctx);
1443 ec_addm_lli (t1, l2, l2, ctx);
1444 ec_subm_lli (x3, x3, t1, ctx);
1445
1446 /* L3 = 8Y^4 */
1447 /* T2: taken from above. */
1448 ec_mulm_lli (t2, t2, t2, ctx);
1449 ec_addm_lli (l3, t2, t2, ctx);
1450 ec_addm_lli (l3, l3, l3, ctx);
1451 ec_addm_lli (l3, l3, l3, ctx);
1452
1453 /* Y3 = L1(L2 - X3) - L3 */
1454 ec_subm_lli (y3, l2, x3, ctx);
1455 ec_mulm_lli (y3, y3, l1, ctx);
1456 ec_subm_lli (y3, y3, l3, ctx);
1457
1458 #undef x3
1459 #undef y3
1460 #undef z3
1461 #undef t1
1462 #undef t2
1463 #undef t3
1464 #undef l1
1465 #undef l2
1466 #undef l3
1467 }
1468
1469
1470 /* RESULT = 2 * POINT (Montgomery version). */
1471 static void
1472 dup_point_montgomery (mpi_point_t result, mpi_point_t point, mpi_ec_t ctx)
1473 {
1474 (void)result;
1475 (void)point;
1476 (void)ctx;
1477 log_fatal ("%s: %s not yet supported\n",
1478 "_gcry_mpi_ec_dup_point", "Montgomery");
1479 }
1480
1481
1482 /* RESULT = 2 * POINT (Twisted Edwards version). */
1483 static void
1484 dup_point_edwards (mpi_point_t result, mpi_point_t point, mpi_ec_t ctx)
1485 {
1486 #define X1 (point->x)
1487 #define Y1 (point->y)
1488 #define Z1 (point->z)
1489 #define X3 (result->x)
1490 #define Y3 (result->y)
1491 #define Z3 (result->z)
1492 #define B (ctx->t.scratch[0])
1493 #define C (ctx->t.scratch[1])
1494 #define D (ctx->t.scratch[2])
1495 #define E (ctx->t.scratch[3])
1496 #define F (ctx->t.scratch[4])
1497 #define H (ctx->t.scratch[5])
1498 #define J (ctx->t.scratch[6])
1499
1500 /* Compute: (X_3 : Y_3 : Z_3) = 2( X_1 : Y_1 : Z_1 ) */
1501
1502 /* B = (X_1 + Y_1)^2 */
1503 ctx->addm (B, X1, Y1, ctx);
1504 ctx->pow2 (B, B, ctx);
1505
1506 /* C = X_1^2 */
1507 /* D = Y_1^2 */
1508 ctx->pow2 (C, X1, ctx);
1509 ctx->pow2 (D, Y1, ctx);
1510
1511 /* E = aC */
1512 if (ctx->dialect == ECC_DIALECT_ED25519)
1513 ctx->subm (E, ctx->p, C, ctx);
1514 else
1515 ctx->mulm (E, ctx->a, C, ctx);
1516
1517 /* F = E + D */
1518 ctx->addm (F, E, D, ctx);
1519
1520 /* H = Z_1^2 */
1521 ctx->pow2 (H, Z1, ctx);
1522
1523 /* J = F - 2H */
1524 ctx->mul2 (J, H, ctx);
1525 ctx->subm (J, F, J, ctx);
1526
1527 /* X_3 = (B - C - D) · J */
1528 ctx->subm (X3, B, C, ctx);
1529 ctx->subm (X3, X3, D, ctx);
1530 ctx->mulm (X3, X3, J, ctx);
1531
1532 /* Y_3 = F · (E - D) */
1533 ctx->subm (Y3, E, D, ctx);
1534 ctx->mulm (Y3, Y3, F, ctx);
1535
1536 /* Z_3 = F · J */
1537 ctx->mulm (Z3, F, J, ctx);
1538
1539 #undef X1
1540 #undef Y1
1541 #undef Z1
1542 #undef X3
1543 #undef Y3
1544 #undef Z3
1545 #undef B
1546 #undef C
1547 #undef D
1548 #undef E
1549 #undef F
1550 #undef H
1551 #undef J
1552 }
1553
1554
1555 /* RESULT = 2 * POINT */
1556 void
1557 _gcry_mpi_ec_dup_point (mpi_point_t result, mpi_point_t point, mpi_ec_t ctx)
1558 {
1559 switch (ctx->model)
1560 {
1561 case MPI_EC_WEIERSTRASS:
1562 dup_point_weierstrass (result, point, ctx);
1563 break;
1564 case MPI_EC_MONTGOMERY:
1565 dup_point_montgomery (result, point, ctx);
1566 break;
1567 case MPI_EC_EDWARDS:
1568 dup_point_edwards (result, point, ctx);
1569 break;
1570 }
1571 }
1572
1573
1574 /* RESULT = P1 + P2 (Weierstrass version).
1575 * P2 is represented by affine coordinate (X2,Y2).
1576 * P1 is never same to P2.
1577 * P1 may be at infinity, in this case, it results invalid RESULT.
1578 * It's caller's responsibility not to use the invalid RESULT. */
1579 static void
1580 add_points_weierstrass_a (mpi_point_t result,
1581 mpi_point_t p1,
1582 gcry_mpi_t x2, gcry_mpi_t y2,
1583 mpi_ec_t ctx)
1584 {
1585 #define x1 (p1->x )
1586 #define y1 (p1->y )
1587 #define z1 (p1->z )
1588 #define x3 (result->x)
1589 #define y3 (result->y)
1590 #define z3 (result->z)
1591 #define l1 (ctx->t.scratch[0])
1592 #define l2 (ctx->t.scratch[1])
1593 #define l3 (ctx->t.scratch[2])
1594 #define l4 (ctx->t.scratch[3])
1595 #define l5 (ctx->t.scratch[4])
1596 #define l6 (ctx->t.scratch[5])
1597 #define l7 (ctx->t.scratch[6])
1598 #define l8 (ctx->t.scratch[7])
1599 #define l9 (ctx->t.scratch[8])
1600
1601 /* l2 = x2 z1^2 */
1602 ec_mulm_lli (l5, z1, z1, ctx);
1603 ec_mulm_lli (l2, l5, x2, ctx);
1604 /* l3 = x1 - l2 */
1605 ec_subm_lli (l3, x1, l2, ctx);
1606 /* l5 = y2 z1^3 */
1607 ec_mulm_lli (l5, z1, l5, ctx);
1608 ec_mulm_lli (l5, l5, y2, ctx);
1609 /* l6 = y1 - l5 */
1610 ec_subm_lli (l6, y1, l5, ctx);
1611
1612 /* l7 = x1 + l2 */
1613 ec_addm_lli (l7, x1, l2, ctx);
1614 /* l8 = y1 + l5 */
1615 ec_addm_lli (l8, y1, l5, ctx);
1616 /* z3 = z1 l3 */
1617 ec_mulm_lli (z3, z1, l3, ctx);
1618 /* x3 = l6^2 - l7 l3^2 */
1619 ec_mulm_lli (l1, l6, l6, ctx);
1620 ec_mulm_lli (l2, l3, l3, ctx);
1621 ec_mulm_lli (l4, l2, l7, ctx);
1622 ec_subm_lli (x3, l1, l4, ctx);
1623 /* l9 = l7 l3^2 - 2 x3 */
1624 ec_addm_lli (l1, x3, x3, ctx);
1625 ec_subm_lli (l9, l4, l1, ctx);
1626 /* y3 = (l9 l6 - l8 l3^3)/2 */
1627 ec_mulm_lli (l9, l9, l6, ctx);
1628 ec_mulm_lli (l1, l3, l2, ctx);
1629 ec_mulm_lli (l1, l1, l8, ctx);
1630 ec_subm_lli (y3, l9, l1, ctx);
1631 ec_mulm_lli (y3, y3, ec_get_two_inv_p (ctx), ctx);
1632
1633 #undef x1
1634 #undef y1
1635 #undef z1
1636 #undef x3
1637 #undef y3
1638 #undef z3
1639 #undef l1
1640 #undef l2
1641 #undef l3
1642 #undef l4
1643 #undef l5
1644 #undef l6
1645 #undef l7
1646 #undef l8
1647 #undef l9
1648 }
1649
1650
1651 /* RESULT = P1 + P2 (Weierstrass version).*/
1652 static void
1653 add_points_weierstrass (mpi_point_t result,
1654 mpi_point_t p1, mpi_point_t p2,
1655 mpi_ec_t ctx)
1656 {
1657 #define x1 (p1->x )
1658 #define y1 (p1->y )
1659 #define z1 (p1->z )
1660 #define x2 (p2->x )
1661 #define y2 (p2->y )
1662 #define z2 (p2->z )
1663 #define x3 (result->x)
1664 #define y3 (result->y)
1665 #define z3 (result->z)
1666 #define l1 (ctx->t.scratch[0])
1667 #define l2 (ctx->t.scratch[1])
1668 #define l3 (ctx->t.scratch[2])
1669 #define l4 (ctx->t.scratch[3])
1670 #define l5 (ctx->t.scratch[4])
1671 #define l6 (ctx->t.scratch[5])
1672 #define l7 (ctx->t.scratch[6])
1673 #define l8 (ctx->t.scratch[7])
1674 #define l9 (ctx->t.scratch[8])
1675
1676 if ( (!mpi_cmp (x1, x2)) && (!mpi_cmp (y1, y2)) && (!mpi_cmp (z1, z2)) )
1677 {
1678 /* Same point; need to call the duplicate function. */
1679 _gcry_mpi_ec_dup_point (result, p1, ctx);
1680 }
1681 else if (!mpi_cmp_ui (z1, 0))
1682 {
1683 /* P1 is at infinity. */
1684 mpi_set (x3, p2->x);
1685 mpi_set (y3, p2->y);
1686 mpi_set (z3, p2->z);
1687 }
1688 else if (!mpi_cmp_ui (z2, 0))
1689 {
1690 /* P2 is at infinity. */
1691 mpi_set (x3, p1->x);
1692 mpi_set (y3, p1->y);
1693 mpi_set (z3, p1->z);
1694 }
1695 else
1696 {
1697 /* l1 = x1 z2^2 */
1698 /* l2 = x2 z1^2 */
1699 ec_pow2 (l4, z2, ctx);
1700 ec_mulm (l1, l4, x1, ctx);
1701 ec_pow2 (l5, z1, ctx);
1702 ec_mulm (l2, l5, x2, ctx);
1703 /* l3 = l1 - l2 */
1704 ec_subm (l3, l1, l2, ctx);
1705 /* l4 = y1 z2^3 */
1706 ec_mulm (l4, z2, l4, ctx);
1707 ec_mulm (l4, l4, y1, ctx);
1708 /* l5 = y2 z1^3 */
1709 ec_mulm (l5, z1, l5, ctx);
1710 ec_mulm (l5, l5, y2, ctx);
1711 /* l6 = l4 - l5 */
1712 ec_subm (l6, l4, l5, ctx);
1713
1714 if (!mpi_cmp_ui (l3, 0))
1715 {
1716 if (!mpi_cmp_ui (l6, 0))
1717 {
1718 /* P1 and P2 are the same - use duplicate function. */
1719 _gcry_mpi_ec_dup_point (result, p1, ctx);
1720 }
1721 else
1722 {
1723 /* P1 is the inverse of P2. */
1724 mpi_set_ui (x3, 1);
1725 mpi_set_ui (y3, 1);
1726 mpi_set_ui (z3, 0);
1727 }
1728 }
1729 else
1730 {
1731 /* l7 = l1 + l2 */
1732 ec_addm (l7, l1, l2, ctx);
1733 /* l8 = l4 + l5 */
1734 ec_addm (l8, l4, l5, ctx);
1735 /* z3 = z1 z2 l3 */
1736 ec_mulm (z3, z1, z2, ctx);
1737 ec_mulm (z3, z3, l3, ctx);
1738 /* x3 = l6^2 - l7 l3^2 */
1739 ec_pow2 (l1, l6, ctx);
1740 ec_pow2 (l2, l3, ctx);
1741 ec_mulm (l4, l2, l7, ctx);
1742 ec_subm (x3, l1, l4, ctx);
1743 /* l9 = l7 l3^2 - 2 x3 */
1744 ec_mul2 (l1, x3, ctx);
1745 ec_subm (l9, l4, l1, ctx);
1746 /* y3 = (l9 l6 - l8 l3^3)/2 */
1747 ec_mulm (l9, l9, l6, ctx);
1748 ec_mulm (l1, l3, l2, ctx);
1749 ec_mulm (l1, l1, l8, ctx);
1750 ec_subm (y3, l9, l1, ctx);
1751 ec_mulm (y3, y3, ec_get_two_inv_p (ctx), ctx);
1752 }
1753 }
1754
1755 #undef x1
1756 #undef y1
1757 #undef z1
1758 #undef x2
1759 #undef y2
1760 #undef z2
1761 #undef x3
1762 #undef y3
1763 #undef z3
1764 #undef l1
1765 #undef l2
1766 #undef l3
1767 #undef l4
1768 #undef l5
1769 #undef l6
1770 #undef l7
1771 #undef l8
1772 #undef l9
1773 }
1774
1775
1776 /* RESULT = P1 + P2 (Montgomery version).*/
1777 static void
1778 add_points_montgomery (mpi_point_t result,
1779 mpi_point_t p1, mpi_point_t p2,
1780 mpi_ec_t ctx)
1781 {
1782 (void)result;
1783 (void)p1;
1784 (void)p2;
1785 (void)ctx;
1786 log_fatal ("%s: %s not yet supported\n",
1787 "_gcry_mpi_ec_add_points", "Montgomery");
1788 }
1789
1790
1791 /* RESULT = P1 + P2 (Twisted Edwards version).*/
1792 static void
1793 add_points_edwards (mpi_point_t result,
1794 mpi_point_t p1, mpi_point_t p2,
1795 mpi_ec_t ctx)
1796 {
1797 #define X1 (p1->x)
1798 #define Y1 (p1->y)
1799 #define Z1 (p1->z)
1800 #define X2 (p2->x)
1801 #define Y2 (p2->y)
1802 #define Z2 (p2->z)
1803 #define X3 (result->x)
1804 #define Y3 (result->y)
1805 #define Z3 (result->z)
1806 #define A (ctx->t.scratch[0])
1807 #define B (ctx->t.scratch[1])
1808 #define C (ctx->t.scratch[2])
1809 #define D (ctx->t.scratch[3])
1810 #define E (ctx->t.scratch[4])
1811 #define F (ctx->t.scratch[5])
1812 #define G (ctx->t.scratch[6])
1813 #define tmp (ctx->t.scratch[7])
1814
1815 mpi_point_resize (result, ctx);
1816
1817 /* Compute: (X_3 : Y_3 : Z_3) = (X_1 : Y_1 : Z_1) + (X_2 : Y_2 : Z_2) */
1818
1819 /* A = Z1 · Z2 */
1820 ctx->mulm (A, Z1, Z2, ctx);
1821
1822 /* B = A^2 */
1823 ctx->pow2 (B, A, ctx);
1824
1825 /* C = X1 · X2 */
1826 ctx->mulm (C, X1, X2, ctx);
1827
1828 /* D = Y1 · Y2 */
1829 ctx->mulm (D, Y1, Y2, ctx);
1830
1831 /* E = d · C · D */
1832 ctx->mulm (E, ctx->b, C, ctx);
1833 ctx->mulm (E, E, D, ctx);
1834
1835 /* F = B - E */
1836 ctx->subm (F, B, E, ctx);
1837
1838 /* G = B + E */
1839 ctx->addm (G, B, E, ctx);
1840
1841 /* X_3 = A · F · ((X_1 + Y_1) · (X_2 + Y_2) - C - D) */
1842 ctx->addm (tmp, X1, Y1, ctx);
1843 ctx->addm (X3, X2, Y2, ctx);
1844 ctx->mulm (X3, X3, tmp, ctx);
1845 ctx->subm (X3, X3, C, ctx);
1846 ctx->subm (X3, X3, D, ctx);
1847 ctx->mulm (X3, X3, F, ctx);
1848 ctx->mulm (X3, X3, A, ctx);
1849
1850 /* Y_3 = A · G · (D - aC) */
1851 if (ctx->dialect == ECC_DIALECT_ED25519)
1852 {
1853 ctx->addm (Y3, D, C, ctx);
1854 }
1855 else
1856 {
1857 ctx->mulm (Y3, ctx->a, C, ctx);
1858 ctx->subm (Y3, D, Y3, ctx);
1859 }
1860 ctx->mulm (Y3, Y3, G, ctx);
1861 ctx->mulm (Y3, Y3, A, ctx);
1862
1863 /* Z_3 = F · G */
1864 ctx->mulm (Z3, F, G, ctx);
1865
1866
1867 #undef X1
1868 #undef Y1
1869 #undef Z1
1870 #undef X2
1871 #undef Y2
1872 #undef Z2
1873 #undef X3
1874 #undef Y3
1875 #undef Z3
1876 #undef A
1877 #undef B
1878 #undef C
1879 #undef D
1880 #undef E
1881 #undef F
1882 #undef G
1883 #undef tmp
1884 }
1885
1886
1887 /* RESULT = P1 + P2 (Twisted Edwards version).
1888 * P2 is represented by affine coordinate (X2,Y2).
1889 * P1 is never same to P1. */
1890 static void
1891 add_points_edwards_a (mpi_point_t result,
1892 mpi_point_t p1,
1893 gcry_mpi_t x2, gcry_mpi_t y2,
1894 mpi_ec_t ctx)
1895 {
1896 #define X1 (p1->x)
1897 #define Y1 (p1->y)
1898 #define Z1 (p1->z)
1899 #define X2 x2
1900 #define Y2 y2
1901 #define X3 (result->x)
1902 #define Y3 (result->y)
1903 #define Z3 (result->z)
1904 #define B (ctx->t.scratch[1])
1905 #define C (ctx->t.scratch[2])
1906 #define D (ctx->t.scratch[3])
1907 #define E (ctx->t.scratch[4])
1908 #define F (ctx->t.scratch[5])
1909 #define G (ctx->t.scratch[6])
1910 #define tmp (ctx->t.scratch[7])
1911
1912 mpi_point_resize (result, ctx);
1913
1914 /* Compute: (X_3 : Y_3 : Z_3) = (X_1 : Y_1 : Z_1) + (X_2 : Y_2 : 1) */
1915
1916 /* B = Z1^2 */
1917 ctx->pow2 (B, Z1, ctx);
1918
1919 /* C = X1 · X2 */
1920 ctx->mulm (C, X1, X2, ctx);
1921
1922 /* D = Y1 · Y2 */
1923 ctx->mulm (D, Y1, Y2, ctx);
1924
1925 /* E = d · C · D */
1926 ctx->mulm (E, ctx->b, C, ctx);
1927 ctx->mulm (E, E, D, ctx);
1928
1929 /* F = B - E */
1930 ctx->subm (F, B, E, ctx);
1931
1932 /* G = B + E */
1933 ctx->addm (G, B, E, ctx);
1934
1935 /* X_3 = Z1 · F · ((X_1 + Y_1) · (X_2 + Y_2) - C - D) */
1936 ctx->addm (tmp, X1, Y1, ctx);
1937 ctx->addm (X3, X2, Y2, ctx);
1938 ctx->mulm (X3, X3, tmp, ctx);
1939 ctx->subm (X3, X3, C, ctx);
1940 ctx->subm (X3, X3, D, ctx);
1941 ctx->mulm (X3, X3, F, ctx);
1942 ctx->mulm (X3, X3, Z1, ctx);
1943
1944 /* Y_3 = Z1 · G · (D - aC) */
1945 if (ctx->dialect == ECC_DIALECT_ED25519)
1946 {
1947 ctx->addm (Y3, D, C, ctx);
1948 }
1949 else
1950 {
1951 ctx->mulm (Y3, ctx->a, C, ctx);
1952 ctx->subm (Y3, D, Y3, ctx);
1953 }
1954 ctx->mulm (Y3, Y3, G, ctx);
1955 ctx->mulm (Y3, Y3, Z1, ctx);
1956
1957 /* Z_3 = F · G */
1958 ctx->mulm (Z3, F, G, ctx);
1959
1960
1961 #undef X1
1962 #undef Y1
1963 #undef Z1
1964 #undef X2
1965 #undef Y2
1966 #undef X3
1967 #undef Y3
1968 #undef Z3
1969 #undef A
1970 #undef B
1971 #undef C
1972 #undef D
1973 #undef E
1974 #undef F
1975 #undef G
1976 #undef tmp
1977 }
1978
1979
1980 /* Compute a step of Montgomery Ladder (only use X and Z in the point).
1981 Inputs: P1, P2, and x-coordinate of DIF = P1 - P1.
1982 Outputs: PRD = 2 * P1 and SUM = P1 + P2. */
1983 static void
1984 montgomery_ladder (mpi_point_t prd, mpi_point_t sum,
1985 mpi_point_t p1, mpi_point_t p2, gcry_mpi_t dif_x,
1986 mpi_ec_t ctx)
1987 {
1988 ctx->addm (sum->x, p2->x, p2->z, ctx);
1989 ctx->subm (p2->z, p2->x, p2->z, ctx);
1990 ctx->addm (prd->x, p1->x, p1->z, ctx);
1991 ctx->subm (p1->z, p1->x, p1->z, ctx);
1992 ctx->mulm (p2->x, p1->z, sum->x, ctx);
1993 ctx->mulm (p2->z, prd->x, p2->z, ctx);
1994 ctx->pow2 (p1->x, prd->x, ctx);
1995 ctx->pow2 (p1->z, p1->z, ctx);
1996 ctx->addm (sum->x, p2->x, p2->z, ctx);
1997 ctx->subm (p2->z, p2->x, p2->z, ctx);
1998 ctx->mulm (prd->x, p1->x, p1->z, ctx);
1999 ctx->subm (p1->z, p1->x, p1->z, ctx);
2000 ctx->pow2 (sum->x, sum->x, ctx);
2001 ctx->pow2 (sum->z, p2->z, ctx);
2002 ctx->mulm (prd->z, p1->z, ctx->a, ctx); /* CTX->A: (a-2)/4 */
2003 ctx->mulm (sum->z, sum->z, dif_x, ctx);
2004 ctx->addm (prd->z, p1->x, prd->z, ctx);
2005 ctx->mulm (prd->z, prd->z, p1->z, ctx);
2006 }
2007
2008
2009 /* RESULT = P1 + P2 */
2010 void
2011 _gcry_mpi_ec_add_points (mpi_point_t result,
2012 mpi_point_t p1, mpi_point_t p2,
2013 mpi_ec_t ctx)
2014 {
2015 switch (ctx->model)
2016 {
2017 case MPI_EC_WEIERSTRASS:
2018 add_points_weierstrass (result, p1, p2, ctx);
2019 break;
2020 case MPI_EC_MONTGOMERY:
2021 add_points_montgomery (result, p1, p2, ctx);
2022 break;
2023 case MPI_EC_EDWARDS:
2024 add_points_edwards (result, p1, p2, ctx);
2025 break;
2026 }
2027 }
2028
2029
2030 /* RESULT = P1 - P2 (Weierstrass version).*/
2031 static void
2032 sub_points_weierstrass (mpi_point_t result,
2033 mpi_point_t p1, mpi_point_t p2,
2034 mpi_ec_t ctx)
2035 {
2036 (void)result;
2037 (void)p1;
2038 (void)p2;
2039 (void)ctx;
2040 log_fatal ("%s: %s not yet supported\n",
2041 "_gcry_mpi_ec_sub_points", "Weierstrass");
2042 }
2043
2044
2045 /* RESULT = P1 - P2 (Montgomery version).*/
2046 static void
2047 sub_points_montgomery (mpi_point_t result,
2048 mpi_point_t p1, mpi_point_t p2,
2049 mpi_ec_t ctx)
2050 {
2051 (void)result;
2052 (void)p1;
2053 (void)p2;
2054 (void)ctx;
2055 log_fatal ("%s: %s not yet supported\n",
2056 "_gcry_mpi_ec_sub_points", "Montgomery");
2057 }
2058
2059
2060 /* RESULT = P1 - P2 (Twisted Edwards version).*/
2061 static void
2062 sub_points_edwards (mpi_point_t result,
2063 mpi_point_t p1, mpi_point_t p2,
2064 mpi_ec_t ctx)
2065 {
2066 mpi_point_t p2i = _gcry_mpi_point_new (0);
2067 point_set (p2i, p2);
2068 ctx->subm (p2i->x, ctx->p, p2i->x, ctx);
2069 add_points_edwards (result, p1, p2i, ctx);
2070 _gcry_mpi_point_release (p2i);
2071 }
2072
2073
2074 /* RESULT = P1 - P2 */
2075 void
2076 _gcry_mpi_ec_sub_points (mpi_point_t result,
2077 mpi_point_t p1, mpi_point_t p2,
2078 mpi_ec_t ctx)
2079 {
2080 switch (ctx->model)
2081 {
2082 case MPI_EC_WEIERSTRASS:
2083 sub_points_weierstrass (result, p1, p2, ctx);
2084 break;
2085 case MPI_EC_MONTGOMERY:
2086 sub_points_montgomery (result, p1, p2, ctx);
2087 break;
2088 case MPI_EC_EDWARDS:
2089 sub_points_edwards (result, p1, p2, ctx);
2090 break;
2091 }
2092 }
2093
2094
2095 /* Compute scalar point multiplication with Montgomery Ladder.
2096 Note that we don't use Y-coordinate in the points at all.
2097 RESULT->Y will be filled by zero. */
2098 static void
2099 montgomery_mul_point (mpi_point_t result,
2100 gcry_mpi_t scalar, mpi_point_t point,
2101 mpi_ec_t ctx)
2102 {
2103 unsigned int nbits;
2104 int j;
2105 gcry_mpi_t z1;
2106 mpi_point_struct p1, p2;
2107 mpi_point_struct p1_, p2_;
2108 mpi_point_t q1, q2, prd, sum;
2109 unsigned long sw;
2110 mpi_size_t rsize;
2111 int scalar_copied = 0;
2112
2113 nbits = mpi_get_nbits (scalar);
2114 point_init (&p1, ctx->nbits);
2115 point_init (&p2, ctx->nbits);
2116 point_init (&p1_, ctx->nbits);
2117 point_init (&p2_, ctx->nbits);
2118 mpi_set_ui (p1.x, 1);
2119 mpi_free (p2.x);
2120 p2.x = mpi_copy (point->x);
2121 mpi_set_ui (p2.z, 1);
2122
2123 if (mpi_is_opaque (scalar))
2124 {
2125 const unsigned int pbits = ctx->nbits;
2126 gcry_mpi_t a;
2127 unsigned int n;
2128 unsigned char *raw;
2129
2130 scalar_copied = 1;
2131
2132 raw = _gcry_mpi_get_opaque_copy (scalar, &n);
2133 if ((n+7)/8 != (pbits+7)/8)
2134 log_fatal ("scalar size (%d) != prime size (%d)\n",
2135 (n+7)/8, (pbits+7)/8);
2136
2137 reverse_buffer (raw, (n+7)/8);
2138 if ((pbits % 8))
2139 raw[0] &= (1 << (pbits % 8)) - 1;
2140 raw[0] |= (1 << ((pbits + 7) % 8));
2141 raw[(pbits+7)/8 - 1] &= (256 - ctx->h);
2142 a = mpi_is_secure (scalar) ? mpi_snew (pbits): mpi_new (pbits);
2143 _gcry_mpi_set_buffer (a, raw, (n+7)/8, 0);
2144 xfree (raw);
2145
2146 scalar = a;
2147 }
2148
2149 mpi_point_resize (&p1, ctx);
2150 mpi_point_resize (&p2, ctx);
2151 mpi_point_resize (&p1_, ctx);
2152 mpi_point_resize (&p2_, ctx);
2153
2154 mpi_resize (point->x, ctx->p->nlimbs);
2155 point->x->nlimbs = ctx->p->nlimbs;
2156
2157 q1 = &p1;
2158 q2 = &p2;
2159 prd = &p1_;
2160 sum = &p2_;
2161
2162 for (j=nbits-1; j >= 0; j--)
2163 {
2164 mpi_point_t t;
2165
2166 sw = mpi_test_bit (scalar, j);
2167 point_swap_cond (q1, q2, sw, ctx);
2168 montgomery_ladder (prd, sum, q1, q2, point->x, ctx);
2169 point_swap_cond (prd, sum, sw, ctx);
2170 t = q1; q1 = prd; prd = t;
2171 t = q2; q2 = sum; sum = t;
2172 }
2173
2174 mpi_clear (result->y);
2175 sw = (nbits & 1);
2176 point_swap_cond (&p1, &p1_, sw, ctx);
2177
2178 rsize = p1.z->nlimbs;
2179 MPN_NORMALIZE (p1.z->d, rsize);
2180 if (rsize == 0)
2181 {
2182 mpi_set_ui (result->x, 1);
2183 mpi_set_ui (result->z, 0);
2184 }
2185 else
2186 {
2187 z1 = mpi_new (ctx->nbits);
2188 ec_invm (z1, p1.z, ctx);
2189 ec_mulm (result->x, p1.x, z1, ctx);
2190 mpi_set_ui (result->z, 1);
2191 mpi_free (z1);
2192 }
2193
2194 point_free (&p1);
2195 point_free (&p2);
2196 point_free (&p1_);
2197 point_free (&p2_);
2198 if (scalar_copied)
2199 _gcry_mpi_release (scalar);
2200 }
2201
2202
2203 /* Compute scalar point multiplication, Least Leak Intended. */
2204 static void
2205 mpi_ec_mul_point_lli (mpi_point_t result,
2206 gcry_mpi_t scalar, mpi_point_t point1,
2207 mpi_ec_t ctx)
2208 {
2209 unsigned int nbits;
2210 int j;
2211 mpi_point_struct tmppnt;
2212 mpi_point_struct point_;
2213 mpi_point_t point = &point_;
2214
2215 mpi_point_resize (point1, ctx);
2216
2217 /* Convert POINT1 into affine coordinate, so that we can use
2218 add_points_*_a routine with affine coordinate. */
2219 point_init (point, ctx->nbits);
2220 if (_gcry_mpih_cmp_ui (point1->z->d, ctx->p->nlimbs, 1))
2221 {
2222 gcry_mpi_t x, y;
2223
2224 x = mpi_new (ctx->nbits);
2225 y = mpi_new (ctx->nbits);
2226
2227 if (_gcry_mpi_ec_get_affine (x, y, point1, ctx))
2228 {
2229 mpi_free (x);
2230 mpi_free (y);
2231 point_free (point);
2232 point_set (result, point1);
2233 return;
2234 }
2235
2236 mpi_set (point->x, x);
2237 mpi_set (point->y, y);
2238 mpi_set_ui (point->z, 1);
2239 mpi_free (x);
2240 mpi_free (y);
2241 }
2242 else
2243 point_set (point, point1);
2244
2245 mpi_point_resize (point, ctx);
2246
2247 nbits = mpi_get_nbits (scalar);
2248 if (nbits < ctx->nbits)
2249 nbits = ctx->nbits;
2250
2251 if (ctx->model == MPI_EC_WEIERSTRASS)
2252 {
2253 mpi_set_ui (result->x, 1);
2254 mpi_set_ui (result->y, 1);
2255 mpi_set_ui (result->z, 0);
2256 }
2257 else
2258 {
2259 mpi_set_ui (result->x, 0);
2260 mpi_set_ui (result->y, 1);
2261 mpi_set_ui (result->z, 1);
2262 }
2263
2264 point_init (&tmppnt, ctx->nbits);
2265 mpi_point_resize (result, ctx);
2266 mpi_point_resize (&tmppnt, ctx);
2267 if (ctx->model == MPI_EC_WEIERSTRASS)
2268 {
2269 for (j=nbits-1; j >= 0; j--)
2270 {
2271 unsigned long is_z_zero;
2272
2273 dup_point_weierstrass_lli (result, result, ctx);
2274 is_z_zero = _gcry_mpih_cmp_ui (result->z->d, ctx->p->nlimbs, 0) == 0;
2275 add_points_weierstrass_a (&tmppnt, result, point->x, point->y, ctx);
2276 /* When P1 is O (at infinity), computation of
2277 add_points_weierstrass_a is invalid, and RESULT is P2. */
2278 mpih_set_cond (tmppnt.x->d, point->x->d, ctx->p->nlimbs, is_z_zero);
2279 mpih_set_cond (tmppnt.y->d, point->y->d, ctx->p->nlimbs, is_z_zero);
2280 mpih_set_cond (tmppnt.z->d, point->z->d, ctx->p->nlimbs, is_z_zero);
2281 point_tfr (result, &tmppnt, mpi_test_bit (scalar, j), ctx);
2282 }
2283 }
2284 else /* MPI_EC_EDWARDS */
2285 {
2286 for (j=nbits-1; j >= 0; j--)
2287 {
2288 dup_point_edwards (result, result, ctx);
2289 add_points_edwards_a (&tmppnt, result, point->x, point->y, ctx);
2290 point_tfr (result, &tmppnt, mpi_test_bit (scalar, j), ctx);
2291 }
2292 }
2293 point_free (&tmppnt);
2294 point_free (point);
2295 }
2296
2297
2298 /* Scalar point multiplication - the main function for ECC. It takes
2299 an integer SCALAR and a POINT as well as the usual context CTX.
2300 RESULT will be set to the resulting point. */
2301 void
2302 _gcry_mpi_ec_mul_point (mpi_point_t result,
2303 gcry_mpi_t scalar, mpi_point_t point,
2304 mpi_ec_t ctx)
2305 {
2306 gcry_mpi_t x1, y1, z1, k, h, yy;
2307 unsigned int i, loops;
2308 mpi_point_struct p1, p2, p1inv;
2309
2310 /* First try HW accelerated scalar multiplications. Error
2311 is returned if acceleration is not supported or if HW
2312 does not support acceleration of given input. */
2313 if (mpi_ec_hw_mul_point (result, scalar, point, ctx) >= 0)
2314 {
2315 return;
2316 }
2317
2318 if (ctx->model == MPI_EC_MONTGOMERY)
2319 {
2320 montgomery_mul_point (result, scalar, point, ctx);
2321 return;
2322 }
2323 else if ((ctx->flags & GCRYECC_FLAG_LEAST_LEAK))
2324 {
2325 mpi_ec_mul_point_lli (result, scalar, point, ctx);
2326 return;
2327 }
2328 else if (mpi_is_secure (scalar))
2329 {
2330 ctx->flags |= GCRYECC_FLAG_LEAST_LEAK;
2331 mpi_ec_mul_point_lli (result, scalar, point, ctx);
2332 ctx->flags &= ~GCRYECC_FLAG_LEAST_LEAK;
2333 return;
2334 }
2335 else if (ctx->model == MPI_EC_EDWARDS)
2336 {
2337 int j;
2338 unsigned int nbits = mpi_get_nbits (scalar);
2339
2340 mpi_set_ui (result->x, 0);
2341 mpi_set_ui (result->y, 1);
2342 mpi_set_ui (result->z, 1);
2343
2344 mpi_point_resize (result, ctx);
2345 mpi_point_resize (point, ctx);
2346
2347 /* Simple left to right binary method. Algorithm 3.27 from
2348 * {author={Hankerson, Darrel and Menezes, Alfred J. and Vanstone, Scott},
2349 * title = {Guide to Elliptic Curve Cryptography},
2350 * year = {2003}, isbn = {038795273X},
2351 * url = {http://www.cacr.math.uwaterloo.ca/ecc/},
2352 * publisher = {Springer-Verlag New York, Inc.}} */
2353 for (j=nbits-1; j >= 0; j--)
2354 {
2355 _gcry_mpi_ec_dup_point (result, result, ctx);
2356 if (mpi_test_bit (scalar, j))
2357 _gcry_mpi_ec_add_points (result, result, point, ctx);
2358 }
2359 return;
2360 }
2361
2362 /* The case of Weierstrass curve. */
2363
2364 x1 = mpi_alloc_like (ctx->p);
2365 y1 = mpi_alloc_like (ctx->p);
2366 h = mpi_alloc_like (ctx->p);
2367 k = mpi_copy (scalar);
2368 yy = mpi_copy (point->y);
2369
2370 if ( mpi_has_sign (k) )
2371 {
2372 k->sign = 0;
2373 ec_invm (yy, yy, ctx);
2374 }
2375
2376 if (!mpi_cmp_ui (point->z, 1))
2377 {
2378 mpi_set (x1, point->x);
2379 mpi_set (y1, yy);
2380 }
2381 else
2382 {
2383 gcry_mpi_t z2, z3;
2384
2385 z2 = mpi_alloc_like (ctx->p);
2386 z3 = mpi_alloc_like (ctx->p);
2387 ec_mulm (z2, point->z, point->z, ctx);
2388 ec_mulm (z3, point->z, z2, ctx);
2389 ec_invm (z2, z2, ctx);
2390 ec_mulm (x1, point->x, z2, ctx);
2391 ec_invm (z3, z3, ctx);
2392 ec_mulm (y1, yy, z3, ctx);
2393 mpi_free (z2);
2394 mpi_free (z3);
2395 }
2396 z1 = mpi_set_ui (NULL, 1);
2397
2398 mpi_mul_ui (h, k, 3); /* h = 3k */
2399 loops = mpi_get_nbits (h);
2400 if (loops < 2)
2401 {
2402 /* If SCALAR is zero, the above mpi_mul sets H to zero and thus
2403 LOOPs will be zero. To avoid an underflow of I in the main
2404 loop we set LOOP to 2 and the result to (0,0,0). */
2405 loops = 2;
2406 mpi_clear (result->x);
2407 mpi_clear (result->y);
2408 mpi_clear (result->z);
2409 }
2410 else
2411 {
2412 mpi_set (result->x, point->x);
2413 mpi_set (result->y, yy);
2414 mpi_set (result->z, point->z);
2415 }
2416 mpi_free (yy); yy = NULL;
2417
2418 p1.x = x1; x1 = NULL;
2419 p1.y = y1; y1 = NULL;
2420 p1.z = z1; z1 = NULL;
2421 point_init (&p2, ctx->nbits);
2422 point_init (&p1inv, ctx->nbits);
2423
2424 /* Invert point: y = p - y mod p */
2425 point_set (&p1inv, &p1);
2426 ec_subm (p1inv.y, ctx->p, p1inv.y, ctx);
2427
2428 for (i=loops-2; i > 0; i--)
2429 {
2430 _gcry_mpi_ec_dup_point (result, result, ctx);
2431 if (mpi_test_bit (h, i) == 1 && mpi_test_bit (k, i) == 0)
2432 {
2433 point_set (&p2, result);
2434 _gcry_mpi_ec_add_points (result, &p2, &p1, ctx);
2435 }
2436 if (mpi_test_bit (h, i) == 0 && mpi_test_bit (k, i) == 1)
2437 {
2438 point_set (&p2, result);
2439 _gcry_mpi_ec_add_points (result, &p2, &p1inv, ctx);
2440 }
2441 }
2442
2443 point_free (&p1);
2444 point_free (&p2);
2445 point_free (&p1inv);
2446 mpi_free (h);
2447 mpi_free (k);
2448 }
2449
2450
2451 /* Return true if POINT is on the curve described by CTX. */
2452 int
2453 _gcry_mpi_ec_curve_point (gcry_mpi_point_t point, mpi_ec_t ctx)
2454 {
2455 int res = 0;
2456 gcry_mpi_t x, y, w;
2457
2458 x = mpi_new (ctx->nbits);
2459 y = mpi_new (ctx->nbits);
2460 w = mpi_new (ctx->nbits);
2461
2462 /* Check that the point is in range. This needs to be done here and
2463 * not after conversion to affine coordinates. */
2464 if (mpi_cmpabs (point->x, ctx->p) >= 0)
2465 goto leave;
2466 if (mpi_cmpabs (point->y, ctx->p) >= 0)
2467 goto leave;
2468 if (mpi_cmpabs (point->z, ctx->p) >= 0)
2469 goto leave;
2470
2471 switch (ctx->model)
2472 {
2473 case MPI_EC_WEIERSTRASS:
2474 {
2475 gcry_mpi_t xxx;
2476
2477 if (_gcry_mpi_ec_get_affine (x, y, point, ctx))
2478 goto leave;
2479
2480 xxx = mpi_new (ctx->nbits);
2481
2482 /* y^2 == x^3 + a·x + b */
2483 ec_pow2 (y, y, ctx);
2484
2485 ec_pow2 (xxx, x, ctx);
2486 ec_mulm (xxx, xxx, x, ctx);
2487 ec_mulm (w, ctx->a, x, ctx);
2488 ec_addm (w, w, ctx->b, ctx);
2489 ec_addm (w, w, xxx, ctx);
2490
2491 if (!mpi_cmp (y, w))
2492 res = 1;
2493
2494 _gcry_mpi_release (xxx);
2495 }
2496 break;
2497 case MPI_EC_MONTGOMERY:
2498 {
2499 #define xx y
2500 /* With Montgomery curve, only X-coordinate is valid. */
2501 if (_gcry_mpi_ec_get_affine (x, NULL, point, ctx))
2502 goto leave;
2503
2504 /* The equation is: b * y^2 == x^3 + a · x^2 + x */
2505 /* We check if right hand is quadratic residue or not by
2506 Euler's criterion. */
2507 /* CTX->A has (a-2)/4 and CTX->B has b^-1 */
2508 mpi_mul_ui (w, ctx->a, 4);
2509 mpi_add_ui (w, w, 2);
2510 ec_mulm (w, w, x, ctx);
2511 ec_pow2 (xx, x, ctx);
2512 ec_addm (w, w, xx, ctx);
2513 ec_mulm (w, w, x, ctx);
2514 ec_addm (w, w, x, ctx);
2515 ec_mulm (w, w, ctx->b, ctx);
2516 #undef xx
2517 /* Compute Euler's criterion: w^(p-1)/2 */
2518 #define p_minus1_half y
2519 mpi_rshift (p_minus1_half, ctx->p, 1); /* p is odd */
2520 ec_powm (w, w, p_minus1_half, ctx);
2521
2522 res = !mpi_cmp_ui (w, 1);
2523 #undef p_minus1
2524 }
2525 break;
2526 case MPI_EC_EDWARDS:
2527 {
2528 if (_gcry_mpi_ec_get_affine (x, y, point, ctx))
2529 goto leave;
2530
2531 mpi_resize (w, ctx->p->nlimbs);
2532 w->nlimbs = ctx->p->nlimbs;
2533
2534 /* a · x^2 + y^2 - 1 - b · x^2 · y^2 == 0 */
2535 ctx->pow2 (x, x, ctx);
2536 ctx->pow2 (y, y, ctx);
2537 if (ctx->dialect == ECC_DIALECT_ED25519)
2538 ctx->subm (w, ctx->p, x, ctx);
2539 else
2540 ctx->mulm (w, ctx->a, x, ctx);
2541 ctx->addm (w, w, y, ctx);
2542 ctx->mulm (x, x, y, ctx);
2543 ctx->mulm (x, x, ctx->b, ctx);
2544 ctx->subm (w, w, x, ctx);
2545 if (!mpi_cmp_ui (w, 1))
2546 res = 1;
2547 }
2548 break;
2549 }
2550
2551 leave:
2552 _gcry_mpi_release (w);
2553 _gcry_mpi_release (x);
2554 _gcry_mpi_release (y);
2555
2556 return res;
2557 }
2558
2559
2560 int
2561 _gcry_mpi_ec_bad_point (gcry_mpi_point_t point, mpi_ec_t ctx)
2562 {
2563 int i;
2564 gcry_mpi_t x_bad;
2565
2566 for (i = 0; (x_bad = ctx->t.scratch[i]); i++)
2567 if (!mpi_cmp (point->x, x_bad))
2568 return 1;
2569
2570 return 0;
2571 }