Debian Codegraph @

src/ec_ws.c

raw symbols SCIP
1 /* ===================================================================
2 *
3 * Copyright (c) 2018, Helder Eijs <[email protected]>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 * ===================================================================
30 */
31
32 #include <assert.h>
33
34 #include "common.h"
35 #include "endianess.h"
36 #include "multiply.h"
37 #include "mont.h"
38 #include "ec.h"
39 #include "p256_table.h"
40 #include "p384_table.h"
41 #include "p521_table.h"
42
43 #include "p256_table.h"
44 #include "p384_table.h"
45 #include "p521_table.h"
46
47 FAKE_INIT(ec_ws)
48
49 #ifdef MAIN
50 STATIC void print_x(const char *s, const uint64_t *number, const MontContext *ctx)
51 {
52 unsigned i;
53 size_t size;
54 uint8_t *encoded;
55 int res;
56
57 size = mont_bytes(ctx);
58 encoded = calloc(1, size);
59 res = mont_to_bytes(encoded, size, number, ctx);
60 assert(res == 0);
61
62 printf("%s: ", s);
63 for (i=0; i<size; i++)
64 printf("%02X", encoded[i]);
65 printf("\n");
66
67 free(encoded);
68 }
69 #endif
70
71 STATIC Workplace *new_workplace(const MontContext *ctx)
72 {
73 Workplace *wp;
74 int res;
75
76 wp = calloc(1, sizeof(Workplace));
77 if (NULL == wp)
78 return NULL;
79
80 res = mont_number(&wp->a, 1, ctx);
81 if (res) goto cleanup;
82 res = mont_number(&wp->b, 1, ctx);
83 if (res) goto cleanup;
84 res = mont_number(&wp->c, 1, ctx);
85 if (res) goto cleanup;
86 res = mont_number(&wp->d, 1, ctx);
87 if (res) goto cleanup;
88 res = mont_number(&wp->e, 1, ctx);
89 if (res) goto cleanup;
90 res = mont_number(&wp->f, 1, ctx);
91 if (res) goto cleanup;
92 res = mont_number(&wp->g, 1, ctx);
93 if (res) goto cleanup;
94 res = mont_number(&wp->h, 1, ctx);
95 if (res) goto cleanup;
96 res = mont_number(&wp->i, 1, ctx);
97 if (res) goto cleanup;
98 res = mont_number(&wp->j, 1, ctx);
99 if (res) goto cleanup;
100 res = mont_number(&wp->k, 1, ctx);
101 if (res) goto cleanup;
102 res = mont_number(&wp->scratch, SCRATCHPAD_NR, ctx);
103 if (res) goto cleanup;
104 return wp;
105
106 cleanup:
107 free(wp->a);
108 free(wp->b);
109 free(wp->c);
110 free(wp->d);
111 free(wp->e);
112 free(wp->f);
113 free(wp->g);
114 free(wp->h);
115 free(wp->i);
116 free(wp->j);
117 free(wp->k);
118 free(wp->scratch);
119 return NULL;
120 }
121
122 STATIC void free_workplace(Workplace *wp)
123 {
124 if (NULL == wp)
125 return;
126 free(wp->a);
127 free(wp->b);
128 free(wp->c);
129 free(wp->d);
130 free(wp->e);
131 free(wp->f);
132 free(wp->g);
133 free(wp->h);
134 free(wp->i);
135 free(wp->j);
136 free(wp->k);
137 free(wp->scratch);
138 free(wp);
139 }
140
141 /*
142 * Convert projective coordinates of an EC point to affine
143 */
144 STATIC void ec_projective_to_affine(uint64_t *x3, uint64_t *y3,
145 const uint64_t *x1, uint64_t *y1, uint64_t *z1,
146 Workplace *tmp,
147 const MontContext *ctx)
148 {
149 uint64_t *a = tmp->a;
150 uint64_t *s = tmp->scratch;
151
152 if (mont_is_zero(z1, ctx)) {
153 mont_set(x3, 0, ctx);
154 mont_set(y3, 0, ctx);
155 return;
156 }
157
158 mont_inv_prime(a, z1, ctx);
159 mont_mult(x3, x1, a, s, ctx); /* X/Z */
160 mont_mult(y3, y1, a, s, ctx); /* Y/Z */
161 }
162
163 /*
164 * Double an EC point on a short Weierstrass curve of equation y²=x³-3x+b.
165 *
166 * @param x3 Projective X coordinate of the output point, in Montgomery form
167 * @param y3 Projective Y coordinate of the output point, in Montgomery form
168 * @param z3 Projective Z coordinate of the output point, in Montgomery form
169 * @param x1 Projective X coordinate of the input point, in Montgomery form
170 * @param y1 Projective Y coordinate of the input point, in Montgomery form
171 * @param z1 Projective Z coordinate of the input point, in Montgomery form
172 * @param b Parameter b in the equation, in Montgomery form
173 * @param tmp Workplace for temporary variables
174 * @param ctx The Montgomery context
175 *
176 * Input and output points can match. The input can be the point-at-infinity.
177 */
178 STATIC void ec_full_double(uint64_t *x3, uint64_t *y3, uint64_t *z3,
179 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
180 const uint64_t *b,
181 Workplace *tmp, const MontContext *ctx)
182 {
183 uint64_t *t0 = tmp->a;
184 uint64_t *t1 = tmp->b;
185 uint64_t *t2 = tmp->c;
186 uint64_t *t3 = tmp->d;
187 uint64_t *x = tmp->e;
188 uint64_t *y = tmp->f;
189 uint64_t *z = tmp->g;
190 uint64_t *s = tmp->scratch;
191
192 /*
193 * Algorithm 6 in "Complete addition formulas for prime order elliptic curves", Renes et al.
194 */
195
196 memcpy(x, x1, ctx->bytes);
197 memcpy(y, y1, ctx->bytes);
198 memcpy(z, z1, ctx->bytes);
199
200 mont_mult(t0, x, x, s, ctx); /* 1 */
201 mont_mult(t1, y, y, s, ctx);
202 mont_mult(t2, z, z, s, ctx);
203
204 mont_mult(t3, x, y, s, ctx); /* 4 */
205 mont_add(t3, t3, t3, s, ctx);
206 mont_mult(z3, x, z, s, ctx);
207
208 mont_add(z3, z3, z3, s, ctx); /* 7 */
209 mont_mult(y3, b, t2, s, ctx);
210 mont_sub(y3, y3, z3, s, ctx);
211
212 mont_add(x3, y3, y3, s, ctx); /* 10 */
213 mont_add(y3, x3, y3, s, ctx);
214 mont_sub(x3, t1, y3, s, ctx);
215
216 mont_add(y3, t1, y3, s, ctx); /* 13 */
217 mont_mult(y3, x3, y3, s, ctx);
218 mont_mult(x3, x3, t3, s, ctx);
219
220 mont_add(t3, t2, t2, s, ctx); /* 16 */
221 mont_add(t2, t2, t3, s, ctx);
222 mont_mult(z3, b, z3, s, ctx);
223
224 mont_sub(z3, z3, t2, s, ctx); /* 19 */
225 mont_sub(z3, z3, t0, s, ctx);
226 mont_add(t3, z3, z3, s, ctx);
227
228 mont_add(z3, z3, t3, s, ctx); /* 22 */
229 mont_add(t3, t0, t0, s, ctx);
230 mont_add(t0, t3, t0, s, ctx);
231
232 mont_sub(t0, t0, t2, s, ctx); /* 25 */
233 mont_mult(t0, t0, z3, s, ctx);
234 mont_add(y3, y3, t0, s, ctx);
235
236 mont_mult(t0, y, z, s, ctx); /* 28 */
237 mont_add(t0, t0, t0, s, ctx);
238 mont_mult(z3, t0, z3, s, ctx);
239
240 mont_sub(x3, x3, z3, s, ctx); /* 31 */
241 mont_mult(z3, t0, t1, s, ctx);
242 mont_add(z3, z3, z3, s, ctx);
243
244 mont_add(z3, z3, z3, s, ctx); /* 34 */
245 }
246
247 /*
248 * Add two EC points on a short Weierstrass curve of equation y²=x³-3x+b.
249 *
250 * @param x3 Projective X coordinate of the output point, in Montgomery form
251 * @param y3 Projective Y coordinate of the output point, in Montgomery form
252 * @param z3 Projective Z coordinate of the output point, in Montgomery form
253 * @param x1 Projective X coordinate of the first input point, in Montgomery form
254 * @param y1 Projective Y coordinate of the first input point, in Montgomery form
255 * @param z1 Projective Z coordinate of the first input point, in Montgomery form
256 * @param x2 Affine X coordinate of the second input point, in Montgomery form
257 * @param y2 Affine Y coordinate of the second input point, in Montgomery form
258 * @param b Parameter b in the equation, in Montgomery form
259 * @param tmp Workplace for temporary variables
260 * @param ctx The Montgomery context
261 *
262 * Input and output points can match. The correct is produced if both or either
263 * input points are at infinity.
264 *
265 * @warning The function is regular (constant-time) only if the second point (affine)
266 * is NOT the point-at-infinity.
267 */
268 STATIC void ec_mix_add(uint64_t *x3, uint64_t *y3, uint64_t *z3,
269 const uint64_t *x13, const uint64_t *y13, const uint64_t *z13,
270 const uint64_t *x2, const uint64_t *y2,
271 const uint64_t *b,
272 Workplace *tmp,
273 const MontContext *ctx)
274 {
275 uint64_t *t0 = tmp->a;
276 uint64_t *t1 = tmp->b;
277 uint64_t *t2 = tmp->c;
278 uint64_t *t3 = tmp->d;
279 uint64_t *t4 = tmp->e;
280 uint64_t *x1 = tmp->f;
281 uint64_t *y1 = tmp->g;
282 uint64_t *z1 = tmp->h;
283 uint64_t *s = tmp->scratch;
284
285 /*
286 * Algorithm 5 in "Complete addition formulas for prime order elliptic curves", Renes et al.
287 */
288
289 if (mont_is_zero(x2, ctx) & mont_is_zero(y2, ctx)) {
290 mont_copy(x3, x13, ctx);
291 mont_copy(y3, y13, ctx);
292 mont_copy(z3, z13, ctx);
293 return;
294 }
295
296 memcpy(x1, x13, ctx->bytes);
297 memcpy(y1, y13, ctx->bytes);
298 memcpy(z1, z13, ctx->bytes);
299
300 mont_mult(t0, x1, x2, s, ctx); /* 1 */
301 mont_mult(t1, y1, y2, s, ctx);
302 mont_add(t3, x2, y2, s, ctx);
303
304 mont_add(t4, x1, y1, s, ctx); /* 4 */
305 mont_mult(t3, t3, t4, s, ctx);
306 mont_add(t4, t0, t1, s, ctx);
307
308 mont_sub(t3, t3, t4, s, ctx); /* 7 */
309 mont_mult(t4, y2, z1, s, ctx);
310 mont_add(t4, t4, y1, s, ctx);
311
312 mont_mult(y3, x2, z1, s, ctx); /* 10 */
313 mont_add(y3, y3, x1, s, ctx);
314 mont_mult(z3, b, z1, s, ctx);
315
316 mont_sub(x3, y3, z3, s, ctx); /* 13 */
317 mont_add(z3, x3, x3, s, ctx);
318 mont_add(x3, x3, z3, s, ctx);
319
320 mont_sub(z3, t1, x3, s, ctx); /* 16 */
321 mont_add(x3, t1, x3, s, ctx);
322 mont_mult(y3, b, y3, s, ctx);
323
324 mont_add(t1, z1, z1, s, ctx); /* 19 */
325 mont_add(t2, t1, z1, s, ctx);
326 mont_sub(y3, y3, t2, s, ctx);
327
328 mont_sub(y3, y3, t0, s, ctx); /* 22 */
329 mont_add(t1, y3, y3, s, ctx);
330 mont_add(y3, t1, y3, s, ctx);
331
332 mont_add(t1, t0, t0, s, ctx); /* 25 */
333 mont_add(t0, t1, t0, s, ctx);
334 mont_sub(t0, t0, t2, s, ctx);
335
336 mont_mult(t1, t4, y3, s, ctx); /* 28 */
337 mont_mult(t2, t0, y3, s, ctx);
338 mont_mult(y3, x3, z3, s, ctx);
339
340 mont_add(y3, y3, t2, s, ctx); /* 31 */
341 mont_mult(x3, t3, x3, s, ctx);
342 mont_sub(x3, x3, t1, s, ctx);
343
344 mont_mult(z3, t4, z3, s, ctx); /* 34 */
345 mont_mult(t1, t3, t0, s, ctx);
346 mont_add(z3, z3, t1, s, ctx);
347 }
348
349 /*
350 * Add two EC points on a short Weierstrass curve of equation y²=x³-3x+b.
351 *
352 * @param x3 Projective X coordinate of the output point, in Montgomery form
353 * @param y3 Projective Y coordinate of the output point, in Montgomery form
354 * @param z3 Projective Z coordinate of the output point, in Montgomery form
355 * @param x1 Projective X coordinate of the first input point, in Montgomery form
356 * @param y1 Projective Y coordinate of the first input point, in Montgomery form
357 * @param z1 Projective Z coordinate of the first input point, in Montgomery form
358 * @param x2 Projective X coordinate of the second input point, in Montgomery form
359 * @param y2 Projective Y coordinate of the second input point, in Montgomery form
360 * @param z2 Projective Z coordinate of the second input point, in Montgomery form
361 * @param b Parameter b in the equation, in Montgomery form
362 * @param tmp Workplace for temporary variables
363 * @param ctx The Montgomery context
364 */
365 STATIC void ec_full_add(uint64_t *x3, uint64_t *y3, uint64_t *z3,
366 const uint64_t *x13, const uint64_t *y13, const uint64_t *z13,
367 const uint64_t *x12, const uint64_t *y12, const uint64_t *z12,
368 const uint64_t *b,
369 Workplace *tmp,
370 const MontContext *ctx)
371 {
372 uint64_t *t0 = tmp->a;
373 uint64_t *t1 = tmp->b;
374 uint64_t *t2 = tmp->c;
375 uint64_t *t3 = tmp->d;
376 uint64_t *t4 = tmp->e;
377 uint64_t *x1 = tmp->f;
378 uint64_t *y1 = tmp->g;
379 uint64_t *z1 = tmp->h;
380 uint64_t *x2 = tmp->i;
381 uint64_t *y2 = tmp->j;
382 uint64_t *z2 = tmp->k;
383 uint64_t *s = tmp->scratch;
384
385 /*
386 * Algorithm 4 in "Complete addition formulas for prime order elliptic curves", Renes et al.
387 */
388
389 memcpy(x1, x13, ctx->bytes);
390 memcpy(y1, y13, ctx->bytes);
391 memcpy(z1, z13, ctx->bytes);
392
393 memcpy(x2, x12, ctx->bytes);
394 memcpy(y2, y12, ctx->bytes);
395 memcpy(z2, z12, ctx->bytes);
396
397 mont_mult(t0, x1, x2, s, ctx); /* 1 */
398 mont_mult(t1, y1, y2, s, ctx);
399 mont_mult(t2, z1, z2, s, ctx);
400
401 mont_add(t3, x1, y1, s, ctx); /* 4 */
402 mont_add(t4, x2, y2, s, ctx);
403 mont_mult(t3, t3, t4, s, ctx);
404
405 mont_add(t4, t0, t1, s, ctx); /* 7 */
406 mont_sub(t3, t3, t4, s, ctx);
407 mont_add(t4, y1, z1, s, ctx);
408
409 mont_add(x3, y2, z2, s, ctx); /* 10 */
410 mont_mult(t4, t4, x3, s, ctx);
411 mont_add(x3, t1, t2, s, ctx);
412
413 mont_sub(t4, t4, x3, s, ctx); /* 13 */
414 mont_add(x3, x1, z1, s, ctx);
415 mont_add(y3, x2, z2, s, ctx);
416
417 mont_mult(x3, x3, y3, s, ctx); /* 16 */
418 mont_add(y3, t0, t2, s, ctx);
419 mont_sub(y3, x3, y3, s, ctx);
420
421 mont_mult(z3, b, t2, s, ctx); /* 19 */
422 mont_sub(x3, y3, z3, s, ctx);
423 mont_add(z3, x3, x3, s, ctx);
424
425 mont_add(x3, x3, z3, s, ctx); /* 22 */
426 mont_sub(z3, t1, x3, s, ctx);
427 mont_add(x3, t1, x3, s, ctx);
428
429 mont_mult(y3, b, y3, s, ctx); /* 25 */
430 mont_add(t1, t2, t2, s, ctx);
431 mont_add(t2, t1, t2, s, ctx);
432
433 mont_sub(y3, y3, t2, s, ctx); /* 28 */
434 mont_sub(y3, y3, t0, s, ctx);
435 mont_add(t1, y3, y3, s, ctx);
436
437 mont_add(y3, t1, y3, s, ctx); /* 31 */
438 mont_add(t1, t0, t0, s, ctx);
439 mont_add(t0, t1, t0, s, ctx);
440
441 mont_sub(t0, t0, t2, s, ctx); /* 34 */
442 mont_mult(t1, t4, y3, s, ctx);
443 mont_mult(t2, t0, y3, s, ctx);
444
445 mont_mult(y3, x3, z3, s, ctx); /* 37 */
446 mont_add(y3, y3, t2, s, ctx);
447 mont_mult(x3, t3, x3, s, ctx);
448
449 mont_sub(x3, x3, t1, s, ctx); /* 40 */
450 mont_mult(z3, t4, z3, s, ctx);
451 mont_mult(t1, t3, t0, s, ctx);
452
453 mont_add(z3, z3, t1, s, ctx); /* 43 */
454 }
455
456 #define WINDOW_SIZE_BITS 4
457 #define WINDOW_SIZE_ITEMS (1<<WINDOW_SIZE_BITS)
458
459 /*
460 * Compute the scalar multiplication of an EC point.
461 * Projective coordinates as output and input.
462 */
463 STATIC int ec_scalar(uint64_t *x3, uint64_t *y3, uint64_t *z3,
464 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
465 const uint64_t *b,
466 const uint8_t *exp, size_t exp_size,
467 uint64_t seed,
468 Workplace *wp1,
469 Workplace *wp2,
470 const MontContext *ctx)
471 {
472 unsigned z1_is_one;
473 unsigned i;
474 int res;
475 uint64_t *window_x[WINDOW_SIZE_ITEMS],
476 *window_y[WINDOW_SIZE_ITEMS],
477 *window_z[WINDOW_SIZE_ITEMS];
478 uint64_t *xw=NULL, *yw=NULL, *zw=NULL;
479 ProtMemory *prot_x=NULL, *prot_y=NULL, *prot_z=NULL;
480
481 struct BitWindow_LR bw;
482
483 z1_is_one = (unsigned)mont_is_one(z1, ctx);
484 res = ERR_MEMORY;
485
486 #define alloc(n) n=calloc(ctx->words, 8); if (NULL == n) goto cleanup;
487
488 alloc(xw);
489 alloc(yw);
490 alloc(zw);
491
492 /** Create window O, P, P² .. P¹⁵ **/
493 memset(window_x, 0, sizeof window_x);
494 memset(window_y, 0, sizeof window_y);
495 memset(window_z, 0, sizeof window_z);
496
497 for (i=0; i<WINDOW_SIZE_ITEMS; i++) {
498 alloc(window_x[i]);
499 alloc(window_y[i]);
500 alloc(window_z[i]);
501 }
502
503 #undef alloc
504
505 mont_set(window_x[0], 0, ctx);
506 mont_set(window_y[0], 1, ctx);
507 mont_set(window_z[0], 0, ctx);
508
509 mont_copy(window_x[1], x1, ctx);
510 mont_copy(window_y[1], y1, ctx);
511 mont_copy(window_z[1], z1, ctx);
512
513 for (i=2; i<WINDOW_SIZE_ITEMS; i++) {
514 if (z1_is_one)
515 ec_mix_add(window_x[i], window_y[i], window_z[i],
516 window_x[i-1], window_y[i-1], window_z[i-1],
517 x1, y1,
518 b,
519 wp1, ctx);
520 else
521 ec_full_add(window_x[i], window_y[i], window_z[i],
522 window_x[i-1], window_y[i-1], window_z[i-1],
523 x1, y1, z1,
524 b,
525 wp1, ctx);
526 }
527
528 res = scatter(&prot_x, (const void**)window_x, WINDOW_SIZE_ITEMS, mont_bytes(ctx), seed);
529 if (res) goto cleanup;
530 res = scatter(&prot_y, (const void**)window_y, WINDOW_SIZE_ITEMS, mont_bytes(ctx), seed);
531 if (res) goto cleanup;
532 res = scatter(&prot_z, (const void**)window_z, WINDOW_SIZE_ITEMS, mont_bytes(ctx), seed);
533 if (res) goto cleanup;
534
535 /** Start from PAI **/
536 mont_set(x3, 0, ctx);
537 mont_set(y3, 1, ctx);
538 mont_set(z3, 0, ctx);
539
540 /** Find first non-zero byte in exponent **/
541 for (; exp_size && *exp==0; exp++, exp_size--);
542 bw = init_bit_window_lr(WINDOW_SIZE_BITS, exp, exp_size);
543
544 /** For every nibble, double 16 times and add window value **/
545 for (i=0; i < bw.nr_windows; i++) {
546 unsigned index;
547 int j;
548
549 index = get_next_digit_lr(&bw);
550 gather(xw, prot_x, index);
551 gather(yw, prot_y, index);
552 gather(zw, prot_z, index);
553 for (j=0; j<WINDOW_SIZE_BITS; j++)
554 ec_full_double(x3, y3, z3, x3, y3, z3, b, wp1, ctx);
555 ec_full_add(x3, y3, z3, x3, y3, z3, xw, yw, zw, b, wp1, ctx);
556 }
557
558 res = 0;
559
560 cleanup:
561 free(xw);
562 free(yw);
563 free(zw);
564 for (i=0; i<WINDOW_SIZE_ITEMS; i++) {
565 free(window_x[i]);
566 free(window_y[i]);
567 free(window_z[i]);
568 }
569 free_scattered(prot_x);
570 free_scattered(prot_y);
571 free_scattered(prot_z);
572
573 return res;
574 }
575
576 STATIC void free_g_p256(ProtMemory **prot_g)
577 {
578 unsigned i;
579
580 if (prot_g) {
581 for (i=0; i<p256_n_tables; i++)
582 free_scattered(prot_g[i]);
583 free(prot_g);
584 }
585 }
586
587 STATIC void free_g_p384(ProtMemory **prot_g)
588 {
589 unsigned i;
590
591 if (prot_g) {
592 for (i=0; i<p384_n_tables; i++)
593 free_scattered(prot_g[i]);
594 free(prot_g);
595 }
596 }
597
598 STATIC void free_g_p521(ProtMemory **prot_g)
599 {
600 unsigned i;
601
602 if (prot_g) {
603 for (i=0; i<p521_n_tables; i++)
604 free_scattered(prot_g[i]);
605 free(prot_g);
606 }
607 }
608
609 /*
610 * Fill the pre-computed table for the generator point in the P-256 EC context.
611 */
612 STATIC ProtMemory** ec_scramble_g_p256(const MontContext *ctx, uint64_t seed)
613 {
614 const void **tables_ptrs;
615 ProtMemory **prot_g;
616 unsigned i;
617 int res;
618
619 tables_ptrs = (const void**)calloc(p256_points_per_table, sizeof(void*));
620 if (NULL == tables_ptrs)
621 return NULL;
622
623 prot_g = (ProtMemory**)calloc(p256_n_tables, sizeof(ProtMemory*));
624 if (NULL == prot_g) {
625 free((void*)tables_ptrs);
626 return NULL;
627 }
628
629 res = 0;
630 for (i=0; res==0 && i<p256_n_tables; i++) {
631 unsigned j;
632
633 for (j=0; j<p256_points_per_table; j++) {
634 tables_ptrs[j] = &p256_tables[i][j];
635 }
636 res = scatter(&prot_g[i], tables_ptrs, (uint8_t)p256_points_per_table, 2*mont_bytes(ctx), seed);
637 }
638
639 if (res) {
640 free_g_p256(prot_g);
641 prot_g = NULL;
642 }
643
644 free((void*)tables_ptrs);
645 return prot_g;
646 }
647
648 /*
649 * Fill the pre-computed table for the generator point in the P-384 EC context.
650 */
651 STATIC ProtMemory** ec_scramble_g_p384(const MontContext *ctx, uint64_t seed)
652 {
653 const void **tables_ptrs;
654 ProtMemory **prot_g;
655 unsigned i;
656 int res;
657
658 tables_ptrs = (const void**)calloc(p384_points_per_table, sizeof(void*));
659 if (NULL == tables_ptrs)
660 return NULL;
661
662 prot_g = (ProtMemory**)calloc(p384_n_tables, sizeof(ProtMemory*));
663 if (NULL == prot_g) {
664 free((void*)tables_ptrs);
665 return NULL;
666 }
667
668 res = 0;
669 for (i=0; res==0 && i<p384_n_tables; i++) {
670 unsigned j;
671
672 for (j=0; j<p384_points_per_table; j++) {
673 tables_ptrs[j] = &p384_tables[i][j];
674 }
675 res = scatter(&prot_g[i], tables_ptrs, (uint8_t)p384_points_per_table, 2*mont_bytes(ctx), seed);
676 }
677
678 if (res) {
679 free_g_p384(prot_g);
680 prot_g = NULL;
681 }
682
683 free((void*)tables_ptrs);
684 return prot_g;
685 }
686
687 /*
688 * Fill the pre-computed table for the generator point in the P-521 EC context.
689 */
690 STATIC ProtMemory** ec_scramble_g_p521(const MontContext *ctx, uint64_t seed)
691 {
692 const void **tables_ptrs;
693 ProtMemory **prot_g;
694 unsigned i;
695 int res;
696
697 tables_ptrs = (const void**)calloc(p521_points_per_table, sizeof(void*));
698 if (NULL == tables_ptrs)
699 return NULL;
700
701 prot_g = (ProtMemory**)calloc(p521_n_tables, sizeof(ProtMemory*));
702 if (NULL == prot_g) {
703 free((void*)tables_ptrs);
704 return NULL;
705 }
706
707 res = 0;
708 for (i=0; res==0 && i<p521_n_tables; i++) {
709 unsigned j;
710
711 for (j=0; j<p521_points_per_table; j++) {
712 tables_ptrs[j] = &p521_tables[i][j];
713 }
714 res = scatter(&prot_g[i], tables_ptrs, (uint8_t)p521_points_per_table, 2*mont_bytes(ctx), seed);
715 }
716
717 if (res) {
718 free_g_p521(prot_g);
719 prot_g = NULL;
720 }
721
722 free((void*)tables_ptrs);
723 return prot_g;
724 }
725
726 STATIC int ec_scalar_g_p256(uint64_t *x3, uint64_t *y3, uint64_t *z3,
727 const uint64_t *b,
728 const uint8_t *exp, size_t exp_size,
729 uint64_t seed,
730 Workplace *wp1,
731 Workplace *wp2,
732 ProtMemory **prot_g,
733 const MontContext *ctx)
734 {
735 unsigned i;
736 struct BitWindow_RL bw;
737
738 /** Start from PAI **/
739 mont_set(x3, 0, ctx);
740 mont_set(y3, 1, ctx);
741 mont_set(z3, 0, ctx);
742
743 /** Find first non-zero byte in exponent **/
744 for (; exp_size && *exp==0; exp++, exp_size--);
745 bw = init_bit_window_rl(p256_window_size, exp, exp_size);
746
747 if (bw.nr_windows > p256_n_tables)
748 return ERR_VALUE;
749
750 for (i=0; i < bw.nr_windows; i++) {
751 unsigned index;
752 uint64_t buffer[4*2]; /* X and Y affine coordinates **/
753 uint64_t *xw, *yw;
754
755 index = get_next_digit_rl(&bw);
756 gather(buffer, prot_g[i], index);
757 xw = &buffer[0];
758 yw = &buffer[4];
759 ec_mix_add(x3, y3, z3,
760 x3, y3, z3,
761 xw, yw,
762 b,
763 wp1, ctx);
764 }
765
766 return 0;
767 }
768
769 STATIC int ec_scalar_g_p384(uint64_t *x3, uint64_t *y3, uint64_t *z3,
770 const uint64_t *b,
771 const uint8_t *exp, size_t exp_size,
772 uint64_t seed,
773 Workplace *wp1,
774 Workplace *wp2,
775 ProtMemory **prot_g,
776 const MontContext *ctx)
777 {
778 unsigned i;
779 struct BitWindow_RL bw;
780
781 /** Start from PAI **/
782 mont_set(x3, 0, ctx);
783 mont_set(y3, 1, ctx);
784 mont_set(z3, 0, ctx);
785
786 /** Find first non-zero byte in exponent **/
787 for (; exp_size && *exp==0; exp++, exp_size--);
788 bw = init_bit_window_rl(p384_window_size, exp, exp_size);
789
790 if (bw.nr_windows > p384_n_tables)
791 return ERR_VALUE;
792
793 for (i=0; i < bw.nr_windows; i++) {
794 unsigned index;
795 uint64_t buffer[6*2]; /* X and Y affine coordinates **/
796 uint64_t *xw, *yw;
797
798 index = get_next_digit_rl(&bw);
799 gather(buffer, prot_g[i], index);
800 xw = &buffer[0];
801 yw = &buffer[6];
802
803 ec_mix_add(x3, y3, z3,
804 x3, y3, z3,
805 xw, yw,
806 b,
807 wp1, ctx);
808 }
809
810 return 0;
811 }
812
813 STATIC int ec_scalar_g_p521(uint64_t *x3, uint64_t *y3, uint64_t *z3,
814 const uint64_t *b,
815 const uint8_t *exp, size_t exp_size,
816 uint64_t seed,
817 Workplace *wp1,
818 Workplace *wp2,
819 ProtMemory **prot_g,
820 const MontContext *ctx)
821 {
822 unsigned i;
823 struct BitWindow_RL bw;
824
825 /** Start from PAI **/
826 mont_set(x3, 0, ctx);
827 mont_set(y3, 1, ctx);
828 mont_set(z3, 0, ctx);
829
830 /** Find first non-zero byte in exponent **/
831 for (; exp_size && *exp==0; exp++, exp_size--);
832 bw = init_bit_window_rl(p521_window_size, exp, exp_size);
833
834 if (exp_size == 66) {
835 if (exp[0] >> 1) {
836 return ERR_VALUE;
837 }
838 switch (p521_window_size) {
839 case 1: bw.nr_windows -= 7; break;
840 case 2: bw.nr_windows -= 3; break;
841 case 3: bw.nr_windows -= 2; break;
842 case 4:
843 case 5:
844 case 6:
845 case 7: bw.nr_windows -= 1; break;
846 }
847 }
848
849 if (exp_size > 66)
850 return ERR_VALUE;
851
852 if (bw.nr_windows > p521_n_tables)
853 return ERR_VALUE;
854
855 for (i=0; i < bw.nr_windows; i++) {
856 unsigned index;
857 uint64_t buffer[9*2]; /* X and Y affine coordinates **/
858 uint64_t *xw, *yw;
859
860 index = get_next_digit_rl(&bw);
861 gather(buffer, prot_g[i], index);
862 xw = &buffer[0];
863 yw = &buffer[9];
864
865 ec_mix_add(x3, y3, z3,
866 x3, y3, z3,
867 xw, yw,
868 b,
869 wp1, ctx);
870 }
871
872 return 0;
873 }
874
875 /*
876 * Create an Elliptic Curve context for Weierstress curves y²=x³+ax+b with a=-3
877 *
878 * @param pec_ctx The memory area where the pointer to the newly allocated
879 * EC context will be stored.
880 * @param modulus The prime modulus for the curve, big-endian encoded
881 * @param b The constant b, big-endian encoded
882 * @param order The order of the EC curve
883 * @param len The length in bytes of modulus, b, and order
884 * @return 0 for success, the appopriate error code otherwise
885 */
886 EXPORT_SYM int ec_ws_new_context(EcContext **pec_ctx,
887 const uint8_t *modulus,
888 const uint8_t *b,
889 const uint8_t *order,
890 size_t len,
891 uint64_t seed)
892 {
893 EcContext *ec_ctx = NULL;
894 unsigned order_words;
895 int res;
896 MontContext *ctx;
897
898 if (NULL == pec_ctx || NULL == modulus || NULL == b)
899 return ERR_NULL;
900
901 *pec_ctx = NULL;
902
903 if (len == 0)
904 return ERR_NOT_ENOUGH_DATA;
905
906 *pec_ctx = ec_ctx = (EcContext*)calloc(1, sizeof(EcContext));
907 if (NULL == ec_ctx)
908 return ERR_MEMORY;
909
910 res = mont_context_init(&ec_ctx->mont_ctx, modulus, len);
911 if (res) goto cleanup;
912 ctx = ec_ctx->mont_ctx;
913
914 res = mont_from_bytes(&ec_ctx->b, b, len, ctx);
915 if (res) goto cleanup;
916
917 order_words = ((unsigned)len+7)/8;
918 ec_ctx->order = (uint64_t*)calloc(order_words, sizeof(uint64_t));
919 if (NULL == ec_ctx->order) {
920 res = ERR_MEMORY;
921 goto cleanup;
922 }
923 bytes_to_words(ec_ctx->order, order_words, order, len);
924
925 /* Scramble lookup table for special generators */
926 switch (ctx->modulus_type) {
927 case ModulusP256: {
928 ec_ctx->prot_g = ec_scramble_g_p256(ec_ctx->mont_ctx, seed);
929 if (NULL == ec_ctx->prot_g) {
930 res = ERR_MEMORY;
931 goto cleanup;
932 }
933 break;
934 }
935 case ModulusP384: {
936 ec_ctx->prot_g = ec_scramble_g_p384(ec_ctx->mont_ctx, seed);
937 if (NULL == ec_ctx->prot_g) {
938 res = ERR_MEMORY;
939 goto cleanup;
940 }
941 break;
942 }
943 case ModulusP521: {
944 ec_ctx->prot_g = ec_scramble_g_p521(ec_ctx->mont_ctx, seed);
945 if (NULL == ec_ctx->prot_g) {
946 res = ERR_MEMORY;
947 goto cleanup;
948 }
949 break;
950 }
951 case ModulusEd448:
952 case ModulusGeneric:
953 break;
954 }
955
956 return 0;
957
958 cleanup:
959 free(ec_ctx->b);
960 free(ec_ctx->order);
961 mont_context_free(ec_ctx->mont_ctx);
962 free(ec_ctx);
963 return res;
964 }
965
966 EXPORT_SYM void ec_free_context(EcContext *ec_ctx)
967 {
968 if (NULL == ec_ctx)
969 return;
970 switch (ec_ctx->mont_ctx->modulus_type) {
971 case ModulusP256:
972 free_g_p256(ec_ctx->prot_g);
973 break;
974 case ModulusP384:
975 free_g_p384(ec_ctx->prot_g);
976 break;
977 case ModulusP521:
978 free_g_p521(ec_ctx->prot_g);
979 break;
980 case ModulusEd448:
981 case ModulusGeneric:
982 break;
983 }
984 free(ec_ctx->b);
985 free(ec_ctx->order);
986 mont_context_free(ec_ctx->mont_ctx);
987 free(ec_ctx);
988 }
989
990 /*
991 * Create a new EC point on the given EC curve.
992 *
993 * @param pecp The memory area where the pointer to the newly allocated EC
994 * point will be stored. Use ec_ws_free_point() for deallocating it.
995 * @param x The X-coordinate (affine, big-endian)
996 * @param y The Y-coordinate (affine, big-endian)
997 * @param len The length of x and y in bytes
998 * @param ec_ctx The EC context
999 * @return 0 for success, the appopriate error code otherwise
1000 */
1001 EXPORT_SYM int ec_ws_new_point(EcPoint **pecp,
1002 const uint8_t *x,
1003 const uint8_t *y,
1004 size_t len,
1005 const EcContext *ec_ctx)
1006 {
1007 int res;
1008 Workplace *wp = NULL;
1009 EcPoint *ecp;
1010 MontContext *ctx;
1011
1012 if (NULL == pecp || NULL == x || NULL == y || NULL == ec_ctx)
1013 return ERR_NULL;
1014 ctx = ec_ctx->mont_ctx;
1015
1016 if (len == 0)
1017 return ERR_NOT_ENOUGH_DATA;
1018
1019 if (len > ctx->bytes)
1020 return ERR_VALUE;
1021
1022 *pecp = ecp = (EcPoint*)calloc(1, sizeof(EcPoint));
1023 if (NULL == ecp)
1024 return ERR_MEMORY;
1025
1026 ecp->ec_ctx = ec_ctx;
1027
1028 res = mont_from_bytes(&ecp->x, x, len, ctx);
1029 if (res) goto cleanup;
1030 res = mont_from_bytes(&ecp->y, y, len, ctx);
1031 if (res) goto cleanup;
1032 res = mont_number(&ecp->z, 1, ctx);
1033 if (res) goto cleanup;
1034 mont_set(ecp->z, 1, ctx);
1035
1036 /** Convert PAI: (0, 0) to (0, 1, 0) */
1037 /** Verify the point is on the curve, if not point-at-infinity */
1038 if (mont_is_zero(ecp->x, ctx) && mont_is_zero(ecp->y, ctx)) {
1039 mont_set(ecp->x, 0, ctx);
1040 mont_set(ecp->y, 1, ctx);
1041 mont_set(ecp->z, 0, ctx);
1042 } else {
1043 wp = new_workplace(ctx);
1044 mont_mult(wp->a, ecp->y, ecp->y, wp->scratch, ctx);
1045 mont_mult(wp->c, ecp->x, ecp->x, wp->scratch, ctx);
1046 mont_mult(wp->c, wp->c, ecp->x, wp->scratch, ctx);
1047 mont_sub(wp->c, wp->c, ecp->x, wp->scratch, ctx);
1048 mont_sub(wp->c, wp->c, ecp->x, wp->scratch, ctx);
1049 mont_sub(wp->c, wp->c, ecp->x, wp->scratch, ctx);
1050 mont_add(wp->c, wp->c, ec_ctx->b, wp->scratch, ctx);
1051 res = !mont_is_equal(wp->a, wp->c, ctx);
1052 free_workplace(wp);
1053
1054 if (res) {
1055 res = ERR_EC_POINT;
1056 goto cleanup;
1057 }
1058 }
1059 return 0;
1060
1061 cleanup:
1062 free(ecp->x);
1063 free(ecp->y);
1064 free(ecp->z);
1065 free(ecp);
1066 *pecp = NULL;
1067 return res;
1068 }
1069
1070 EXPORT_SYM void ec_ws_free_point(EcPoint *ecp)
1071 {
1072 if (NULL == ecp)
1073 return;
1074
1075 /* It is not up to us to deallocate the EC context */
1076 free(ecp->x);
1077 free(ecp->y);
1078 free(ecp->z);
1079 free(ecp);
1080 }
1081
1082 /*
1083 * Encode the affine coordinates of an EC point.
1084 *
1085 * @param x The location where the affine X-coordinate will be store in big-endian mode
1086 * @param y The location where the affine Y-coordinate will be store in big-endian mode
1087 * @param len The memory available for x and y in bytes.
1088 * It must be as long as the prime modulus of the curve field.
1089 * @param ecp The EC point to encode.
1090 */
1091 EXPORT_SYM int ec_ws_get_xy(uint8_t *x, uint8_t *y, size_t len, const EcPoint *ecp)
1092 {
1093 uint64_t *xw=NULL, *yw=NULL;
1094 Workplace *wp;
1095 MontContext *ctx;
1096 int res;
1097
1098 if (NULL == x || NULL == y || NULL == ecp)
1099 return ERR_NULL;
1100 ctx = ecp->ec_ctx->mont_ctx;
1101
1102 if (len < ctx->modulus_len)
1103 return ERR_NOT_ENOUGH_DATA;
1104
1105 wp = new_workplace(ctx);
1106 if (NULL == wp)
1107 return ERR_MEMORY;
1108
1109 res = mont_number(&xw, 1, ctx);
1110 if (res) goto cleanup;
1111 res = mont_number(&yw, 1, ctx);
1112 if (res) goto cleanup;
1113
1114 ec_projective_to_affine(xw, yw, ecp->x, ecp->y, ecp->z, wp, ctx);
1115
1116 res = mont_to_bytes(x, len, xw, ctx);
1117 if (res) goto cleanup;
1118 res = mont_to_bytes(y, len, yw, ctx);
1119 if (res) goto cleanup;
1120
1121 res = 0;
1122
1123 cleanup:
1124 free_workplace(wp);
1125 free(xw);
1126 free(yw);
1127 return res;
1128 }
1129
1130 /*
1131 * Double an EC point
1132 */
1133 EXPORT_SYM int ec_ws_double(EcPoint *p)
1134 {
1135 Workplace *wp;
1136 MontContext *ctx;
1137
1138 if (NULL == p)
1139 return ERR_NULL;
1140 ctx = p->ec_ctx->mont_ctx;
1141
1142 wp = new_workplace(ctx);
1143 if (NULL == wp)
1144 return ERR_MEMORY;
1145
1146 ec_full_double(p->x, p->y, p->z, p->x, p->y, p->z, p->ec_ctx->b, wp, ctx);
1147
1148 free_workplace(wp);
1149 return 0;
1150 }
1151
1152 /*
1153 * Add an EC point to another
1154 */
1155 EXPORT_SYM int ec_ws_add(EcPoint *ecpa, EcPoint *ecpb)
1156 {
1157 Workplace *wp;
1158 MontContext *ctx;
1159
1160 if (NULL == ecpa || NULL == ecpb)
1161 return ERR_NULL;
1162 if (ecpa->ec_ctx != ecpb->ec_ctx)
1163 return ERR_EC_CURVE;
1164 ctx = ecpa->ec_ctx->mont_ctx;
1165
1166 wp = new_workplace(ctx);
1167 if (NULL == wp)
1168 return ERR_MEMORY;
1169
1170 ec_full_add(ecpa->x, ecpa->y, ecpa->z,
1171 ecpa->x, ecpa->y, ecpa->z,
1172 ecpb->x, ecpb->y, ecpb->z,
1173 ecpa->ec_ctx->b,
1174 wp, ctx);
1175
1176 free_workplace(wp);
1177 return 0;
1178 }
1179
1180 /*
1181 * Blind the scalar factor to be used in an EC multiplication
1182 *
1183 * @param blind_scalar The area of memory where the pointer to a newly
1184 * allocated blind scalar is stored, in big endian mode.
1185 * The caller must deallocate this memory.
1186 * @param blind_scalar_len The area where the length of the blind scalar in bytes will be written to.
1187 * @param scalar The (secret) scalar to blind.
1188 * @param scalar_len The length of the secret scalar in bytes.
1189 * @param R_seed The 32-bit factor to use to blind the scalar.
1190 * @param order The order of the EC curve, big-endian mode, 64 bit words
1191 * @param order_words The number of words making up the order
1192 */
1193 static int blind_scalar_factor(uint8_t **blind_scalar,
1194 size_t *blind_scalar_len,
1195 const uint8_t *scalar,
1196 size_t scalar_len,
1197 uint32_t R_seed,
1198 uint64_t *order,
1199 size_t order_words)
1200 {
1201 size_t scalar_words;
1202 size_t blind_scalar_words;
1203 uint64_t *output_u64 = NULL;
1204 uint64_t *scratchpad = NULL;
1205 int res = ERR_MEMORY;
1206
1207 scalar_words = (scalar_len+7)/8;
1208 blind_scalar_words = MAX(order_words+2, scalar_words+2);
1209 *blind_scalar_len = blind_scalar_words*sizeof(uint64_t);
1210
1211 *blind_scalar = (uint8_t*)calloc(*blind_scalar_len, 1);
1212 if (NULL == *blind_scalar)
1213 goto cleanup;
1214
1215 output_u64 = (uint64_t*)calloc(blind_scalar_words, sizeof(uint64_t));
1216 if (NULL == output_u64)
1217 goto cleanup;
1218
1219 scratchpad = (uint64_t*)calloc(blind_scalar_words + order_words, sizeof(uint64_t));
1220 if (NULL == scratchpad)
1221 goto cleanup;
1222
1223 bytes_to_words(output_u64, blind_scalar_words, scalar, scalar_len);
1224 addmul128(output_u64, scratchpad, order, R_seed, 0, blind_scalar_words, order_words);
1225 words_to_bytes(*blind_scalar, *blind_scalar_len, output_u64, blind_scalar_words);
1226
1227 res = 0;
1228
1229 cleanup:
1230 free(output_u64);
1231 free(scratchpad);
1232 return res;
1233 }
1234
1235 /*
1236 * Multiply an EC point by a scalar
1237 *
1238 * @param ecp The EC point to multiply
1239 * @param k The scalar, encoded in big endian mode
1240 * @param len The length of the scalar, in bytes
1241 * @param seed The 64-bit to drive the randomizations against SCAs
1242 * @return 0 in case of success, the appropriate error code otherwise
1243 */
1244 EXPORT_SYM int ec_ws_scalar(EcPoint *ecp, const uint8_t *k, size_t len, uint64_t seed)
1245 {
1246 Workplace *wp1=NULL, *wp2=NULL;
1247 MontContext *ctx;
1248 int res;
1249
1250 if (NULL == ecp || NULL == k)
1251 return ERR_NULL;
1252 ctx = ecp->ec_ctx->mont_ctx;
1253
1254 if (len == 0) {
1255 return ERR_NOT_ENOUGH_DATA;
1256 }
1257
1258 wp1 = new_workplace(ctx);
1259 if (NULL == wp1) {
1260 res = ERR_MEMORY;
1261 goto cleanup;
1262 }
1263
1264 wp2 = new_workplace(ctx);
1265 if (NULL == wp2) {
1266 res = ERR_MEMORY;
1267 goto cleanup;
1268 }
1269
1270 switch (ctx->modulus_type) {
1271 case ModulusP256: {
1272 /** Coordinates in Montgomery form **/
1273 const uint64_t mont_Gx[4] = { 0x79E730D418A9143CULL, 0x75BA95FC5FEDB601ULL, 0x79FB732B77622510ULL, 0x18905F76A53755C6ULL };
1274 const uint64_t mont_Gy[4] = { 0xDDF25357CE95560AULL, 0x8B4AB8E4BA19E45CULL, 0xD2E88688DD21F325ULL, 0x8571FF1825885D85ULL };
1275 unsigned is_generator;
1276 unsigned i;
1277
1278 is_generator = 1;
1279 for (i=0; i<4; i++) {
1280 is_generator &= (mont_Gx[i] == ecp->x[i]);
1281 is_generator &= (mont_Gy[i] == ecp->y[i]);
1282 }
1283 is_generator &= (unsigned)mont_is_one(ecp->z, ctx);
1284
1285 if (is_generator) {
1286 res = ec_scalar_g_p256(ecp->x, ecp->y, ecp->z,
1287 ecp->ec_ctx->b,
1288 k, len,
1289 seed + 2,
1290 wp1, wp2,
1291 ecp->ec_ctx->prot_g,
1292 ctx);
1293 goto cleanup;
1294 }
1295 break;
1296 }
1297 case ModulusP384: {
1298 /** Coordinates in Montgomery form **/
1299 const uint64_t mont_Gx[6] = { 0x3DD0756649C0B528ULL, 0x20E378E2A0D6CE38ULL, 0x879C3AFC541B4D6EULL, 0x6454868459A30EFFULL, 0x812FF723614EDE2BULL, 0x4D3AADC2299E1513ULL };
1300 const uint64_t mont_Gy[6] = { 0x23043DAD4B03A4FEULL, 0xA1BFA8BF7BB4A9ACULL, 0x8BADE7562E83B050ULL, 0xC6C3521968F4FFD9ULL, 0xDD8002263969A840ULL, 0x2B78ABC25A15C5E9ULL };
1301 unsigned is_generator;
1302 unsigned i;
1303
1304 is_generator = 1;
1305 for (i=0; i<6; i++) {
1306 is_generator &= (mont_Gx[i] == ecp->x[i]);
1307 is_generator &= (mont_Gy[i] == ecp->y[i]);
1308 }
1309 is_generator &= (unsigned)mont_is_one(ecp->z, ctx);
1310
1311 if (is_generator) {
1312 res = ec_scalar_g_p384(ecp->x, ecp->y, ecp->z,
1313 ecp->ec_ctx->b,
1314 k, len,
1315 seed + 2,
1316 wp1, wp2,
1317 ecp->ec_ctx->prot_g,
1318 ctx);
1319 goto cleanup;
1320 }
1321 break;
1322 }
1323 case ModulusP521: {
1324 /** Coordinates in normal form **/
1325 const uint64_t mont_Gx[9] = { 0xF97E7E31C2E5BD66ULL, 0x3348B3C1856A429BULL, 0xFE1DC127A2FFA8DEULL, 0xA14B5E77EFE75928ULL, 0xF828AF606B4D3DBAULL, 0x9C648139053FB521ULL, 0x9E3ECB662395B442ULL, 0x858E06B70404E9CDULL, 0x00000000000000C6ULL };
1326 const uint64_t mont_Gy[9] = { 0x88BE94769FD16650ULL, 0x353C7086A272C240ULL, 0xC550B9013FAD0761ULL, 0x97EE72995EF42640ULL, 0x17AFBD17273E662CULL, 0x98F54449579B4468ULL, 0x5C8A5FB42C7D1BD9ULL, 0x39296A789A3BC004ULL, 0x0000000000000118ULL };
1327 unsigned is_generator;
1328 unsigned i;
1329
1330 is_generator = 1;
1331 for (i=0; i<9; i++) {
1332 is_generator &= (mont_Gx[i] == ecp->x[i]);
1333 is_generator &= (mont_Gy[i] == ecp->y[i]);
1334 }
1335 is_generator &= (unsigned)mont_is_one(ecp->z, ctx);
1336
1337 if (is_generator) {
1338 res = ec_scalar_g_p521(ecp->x, ecp->y, ecp->z,
1339 ecp->ec_ctx->b,
1340 k, len,
1341 seed + 2,
1342 wp1, wp2,
1343 ecp->ec_ctx->prot_g,
1344 ctx);
1345 goto cleanup;
1346 }
1347 break;
1348 }
1349 case ModulusEd448:
1350 case ModulusGeneric:
1351 break;
1352 }
1353
1354 if (seed != 0) {
1355 uint8_t *blind_scalar=NULL;
1356 size_t blind_scalar_len;
1357 uint64_t *factor=NULL;
1358
1359 /* Create the blinding factor for the base point */
1360 res = mont_random_number(&factor, 1, seed, ctx);
1361 if (res)
1362 goto cleanup;
1363
1364 /* Blind the base point */
1365 mont_mult(ecp->x, ecp->x, factor, wp1->scratch, ctx);
1366 mont_mult(ecp->y, ecp->y, factor, wp1->scratch, ctx);
1367 mont_mult(ecp->z, ecp->z, factor, wp1->scratch, ctx);
1368
1369 free(factor);
1370
1371 /* Blind the scalar, by adding R*order where R is at least 32 bits */
1372 res = blind_scalar_factor(&blind_scalar,
1373 &blind_scalar_len,
1374 k, len,
1375 (uint32_t)seed,
1376 ecp->ec_ctx->order,
1377 ctx->words);
1378 if (res) goto cleanup;
1379 res = ec_scalar(ecp->x, ecp->y, ecp->z,
1380 ecp->x, ecp->y, ecp->z,
1381 ecp->ec_ctx->b,
1382 blind_scalar, blind_scalar_len,
1383 seed + 1,
1384 wp1, wp2, ctx);
1385
1386 free(blind_scalar);
1387 if (res) goto cleanup;
1388 } else {
1389 /* No blinding */
1390 res = ec_scalar(ecp->x, ecp->y, ecp->z,
1391 ecp->x, ecp->y, ecp->z,
1392 ecp->ec_ctx->b,
1393 k, len,
1394 seed + 1,
1395 wp1, wp2, ctx);
1396 if (res) goto cleanup;
1397 }
1398
1399 res = 0;
1400
1401 cleanup:
1402 free_workplace(wp1);
1403 free_workplace(wp2);
1404 return res;
1405 }
1406
1407 EXPORT_SYM int ec_ws_clone(EcPoint **pecp2, const EcPoint *ecp)
1408 {
1409 int res;
1410 EcPoint *ecp2;
1411 MontContext *ctx;
1412
1413 if (NULL == pecp2 || NULL == ecp)
1414 return ERR_NULL;
1415 ctx = ecp->ec_ctx->mont_ctx;
1416
1417 *pecp2 = ecp2 = (EcPoint*)calloc(1, sizeof(EcPoint));
1418 if (NULL == ecp2)
1419 return ERR_MEMORY;
1420
1421 ecp2->ec_ctx = ecp->ec_ctx;
1422
1423 res = mont_number(&ecp2->x, 1, ctx);
1424 if (res) goto cleanup;
1425 mont_copy(ecp2->x, ecp->x, ctx);
1426
1427 res = mont_number(&ecp2->y, 1, ctx);
1428 if (res) goto cleanup;
1429 mont_copy(ecp2->y, ecp->y, ctx);
1430
1431 res = mont_number(&ecp2->z, 1, ctx);
1432 if (res) goto cleanup;
1433 mont_copy(ecp2->z, ecp->z, ctx);
1434
1435 return 0;
1436
1437 cleanup:
1438 free(ecp2->x);
1439 free(ecp2->y);
1440 free(ecp2->z);
1441 free(ecp2);
1442 *pecp2 = NULL;
1443 return res;
1444 }
1445
1446 /*
1447 * Compare two EC points and return 0 if they match
1448 */
1449 EXPORT_SYM int ec_ws_cmp(const EcPoint *ecp1, const EcPoint *ecp2)
1450 {
1451 Workplace *wp;
1452 MontContext *ctx;
1453 int p1_is_pai;
1454 int p2_is_pai;
1455 int result;
1456
1457 if (NULL == ecp1 || NULL == ecp2)
1458 return ERR_NULL;
1459
1460 if (ecp1->ec_ctx != ecp2->ec_ctx)
1461 return ERR_EC_CURVE;
1462 ctx = ecp1->ec_ctx->mont_ctx;
1463
1464 p1_is_pai = mont_is_zero(ecp1->z, ctx);
1465 p2_is_pai = mont_is_zero(ecp2->z, ctx);
1466
1467 /* Check for point-at-infinity */
1468 if (p1_is_pai | p2_is_pai) {
1469 return (p1_is_pai & p2_is_pai) ? 0 : ERR_VALUE;
1470 }
1471
1472 /** Normalize to have the same Z coordinate */
1473 wp = new_workplace(ctx);
1474 if (NULL == wp)
1475 return ERR_MEMORY;
1476
1477 mont_mult(wp->b, ecp1->x, ecp2->z, wp->scratch, ctx); /* B = X1*Z2 */
1478 mont_mult(wp->d, ecp2->x, ecp1->z, wp->scratch, ctx); /* D = X2*Z1 */
1479 mont_mult(wp->e, ecp1->y, ecp2->z, wp->scratch, ctx); /* E = Y1*Z2 */
1480 mont_mult(wp->f, ecp2->y, ecp1->z, wp->scratch, ctx); /* F = Y2*Z1 */
1481 result = (mont_is_equal(wp->b, wp->d, ctx) & mont_is_equal(wp->e, wp->f, ctx)) ? 0 : ERR_VALUE;
1482
1483 free_workplace(wp);
1484
1485 return result;
1486 }
1487
1488 EXPORT_SYM int ec_ws_neg(EcPoint *p)
1489 {
1490 MontContext *ctx;
1491 uint64_t *tmp;
1492 int res;
1493
1494 if (NULL == p)
1495 return ERR_NULL;
1496 ctx = p->ec_ctx->mont_ctx;
1497
1498 res = mont_number(&tmp, SCRATCHPAD_NR, ctx);
1499 if (res)
1500 return res;
1501
1502 mont_sub(p->y, ctx->modulus, p->y, tmp, ctx);
1503 free(tmp);
1504 return 0;
1505 }
1506
1507 EXPORT_SYM int ec_ws_copy(EcPoint *ecp1, const EcPoint *ecp2)
1508 {
1509 MontContext *ctx;
1510
1511 if (NULL == ecp1 || NULL == ecp2)
1512 return ERR_NULL;
1513 ctx = ecp2->ec_ctx->mont_ctx;
1514
1515 ecp1->ec_ctx = ecp2->ec_ctx;
1516 mont_copy(ecp1->x, ecp2->x, ctx);
1517 mont_copy(ecp1->y, ecp2->y, ctx);
1518 mont_copy(ecp1->z, ecp2->z, ctx);
1519
1520 return 0;
1521 }