blob: 8eaa1e74985ed28192097e96d5d79308700dba66 [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc.c - TinyCrypt implementation of common ECC functions */
2
3/*
Simon Butcher92c3d1f2019-09-09 17:25:08 +01004 * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8/*
Jarno Lamsa18987a42019-04-24 15:40:43 +03009 * Copyright (c) 2014, Kenneth MacKay
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 * * Redistributions of source code must retain the above copyright notice,
15 * this list of conditions and the following disclaimer.
16 * * Redistributions in binary form must reproduce the above copyright notice,
17 * this list of conditions and the following disclaimer in the documentation
18 * and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions are met:
35 *
36 * - Redistributions of source code must retain the above copyright notice,
37 * this list of conditions and the following disclaimer.
38 *
39 * - Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 *
43 * - Neither the name of Intel Corporation nor the names of its contributors
44 * may be used to endorse or promote products derived from this software
45 * without specific prior written permission.
46 *
47 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
48 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
49 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
50 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
51 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
52 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
53 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
54 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
55 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
56 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57 * POSSIBILITY OF SUCH DAMAGE.
58 */
59
Hanno Becker36ae7582019-07-23 15:52:35 +010060#if !defined(MBEDTLS_CONFIG_FILE)
61#include "mbedtls/config.h"
62#else
63#include MBEDTLS_CONFIG_FILE
64#endif
65
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020066#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030067#include <tinycrypt/ecc.h>
Jarno Lamsa18987a42019-04-24 15:40:43 +030068#include <string.h>
69
70/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
71 * has access to enough entropy in order to feed the PRNG regularly. */
72#if default_RNG_defined
73static uECC_RNG_Function g_rng_function = &default_CSPRNG;
74#else
75static uECC_RNG_Function g_rng_function = 0;
76#endif
77
78void uECC_set_rng(uECC_RNG_Function rng_function)
79{
80 g_rng_function = rng_function;
81}
82
83uECC_RNG_Function uECC_get_rng(void)
84{
85 return g_rng_function;
86}
87
88int uECC_curve_private_key_size(uECC_Curve curve)
89{
90 return BITS_TO_BYTES(curve->num_n_bits);
91}
92
93int uECC_curve_public_key_size(uECC_Curve curve)
94{
95 return 2 * curve->num_bytes;
96}
97
98void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words)
99{
100 wordcount_t i;
101 for (i = 0; i < num_words; ++i) {
102 vli[i] = 0;
103 }
104}
105
106uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words)
107{
108 uECC_word_t bits = 0;
109 wordcount_t i;
110 for (i = 0; i < num_words; ++i) {
111 bits |= vli[i];
112 }
113 return (bits == 0);
114}
115
116uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
117{
118 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
119 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
120}
121
122/* Counts the number of words in vli. */
123static wordcount_t vli_numDigits(const uECC_word_t *vli,
124 const wordcount_t max_words)
125{
126
127 wordcount_t i;
128 /* Search from the end until we find a non-zero digit. We do it in reverse
129 * because we expect that most digits will be nonzero. */
130 for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
131 }
132
133 return (i + 1);
134}
135
136bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
137 const wordcount_t max_words)
138{
139
140 uECC_word_t i;
141 uECC_word_t digit;
142
143 wordcount_t num_digits = vli_numDigits(vli, max_words);
144 if (num_digits == 0) {
145 return 0;
146 }
147
148 digit = vli[num_digits - 1];
149 for (i = 0; digit; ++i) {
150 digit >>= 1;
151 }
152
153 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
154}
155
156void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
157 wordcount_t num_words)
158{
159 wordcount_t i;
160
161 for (i = 0; i < num_words; ++i) {
162 dest[i] = src[i];
163 }
164}
165
166cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
167 const uECC_word_t *right,
168 wordcount_t num_words)
169{
170 wordcount_t i;
171
172 for (i = num_words - 1; i >= 0; --i) {
173 if (left[i] > right[i]) {
174 return 1;
175 } else if (left[i] < right[i]) {
176 return -1;
177 }
178 }
179 return 0;
180}
181
182uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
183 wordcount_t num_words)
184{
185
186 uECC_word_t diff = 0;
187 wordcount_t i;
188
189 for (i = num_words - 1; i >= 0; --i) {
190 diff |= (left[i] ^ right[i]);
191 }
192 return !(diff == 0);
193}
194
195uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
196{
197 return (p_true*(cond)) | (p_false*(!cond));
198}
199
200/* Computes result = left - right, returning borrow, in constant time.
201 * Can modify in place. */
202uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
203 const uECC_word_t *right, wordcount_t num_words)
204{
205 uECC_word_t borrow = 0;
206 wordcount_t i;
207 for (i = 0; i < num_words; ++i) {
208 uECC_word_t diff = left[i] - right[i] - borrow;
209 uECC_word_t val = (diff > left[i]);
210 borrow = cond_set(val, borrow, (diff != left[i]));
211
212 result[i] = diff;
213 }
214 return borrow;
215}
216
217/* Computes result = left + right, returning carry, in constant time.
218 * Can modify in place. */
219static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
220 const uECC_word_t *right, wordcount_t num_words)
221{
222 uECC_word_t carry = 0;
223 wordcount_t i;
224 for (i = 0; i < num_words; ++i) {
225 uECC_word_t sum = left[i] + right[i] + carry;
226 uECC_word_t val = (sum < left[i]);
227 carry = cond_set(val, carry, (sum != left[i]));
228 result[i] = sum;
229 }
230 return carry;
231}
232
233cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
234 wordcount_t num_words)
235{
236 uECC_word_t tmp[NUM_ECC_WORDS];
237 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
238 uECC_word_t equal = uECC_vli_isZero(tmp, num_words);
239 return (!equal - 2 * neg);
240}
241
242/* Computes vli = vli >> 1. */
243static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
244{
245 uECC_word_t *end = vli;
246 uECC_word_t carry = 0;
247
248 vli += num_words;
249 while (vli-- > end) {
250 uECC_word_t temp = *vli;
251 *vli = (temp >> 1) | carry;
252 carry = temp << (uECC_WORD_BITS - 1);
253 }
254}
255
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100256/* Compute a * b + r, where r is a double-word with high-order word r1 and
257 * low-order word r0, and store the result in the same double-word (r1, r0),
258 * with the carry bit stored in r2.
259 *
260 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200261 * [in] a, b: operands to be multiplied
262 * [in] r0, r1: low and high-order words of operand to add
263 * [out] r0, r1: low and high-order words of the result
264 * [out] r2: carry
265 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300266static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
267 uECC_word_t *r1, uECC_word_t *r2)
268{
269
270 uECC_dword_t p = (uECC_dword_t)a * b;
271 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
272 r01 += p;
273 *r2 += (r01 < p);
274 *r1 = r01 >> uECC_WORD_BITS;
275 *r0 = (uECC_word_t)r01;
276
277}
278
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200279/* State for implementing random delays in uECC_vli_mult_rnd().
280 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100281 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200282 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
283 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100284 * Randomized vli multiplication is used only for point operations
285 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
286 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
287 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
288 * indirectly through uECC_vli_modMult_rnd() or uECC_vli_modSquare_rnd()).
289 *
290 * Considering this, in order to minimize the number of calls to the RNG
291 * (which impact performance) while keeping the size of the structure low,
292 * make room for 14 randomized vli mults, which corresponds to one step in the
293 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200294 */
295typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100296 uint8_t i;
297 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100298} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200299
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100300/*
301 * Reset wait_state so that it's ready to be used.
302 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100303void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100304{
305 if (ws == NULL)
306 return;
307
308 ws->i = 0;
309 g_rng_function(ws->delays, sizeof(ws->delays));
310}
311
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200312/* Computes result = left * right. Result must be 2 * num_words long.
313 *
314 * As a counter-measure against horizontal attacks, add noise by performing
315 * a random number of extra computations performing random additional accesses
316 * to limbs of the input.
317 *
318 * Each of the two actual computation loops is surrounded by two
319 * similar-looking waiting loops, to make the beginning and end of the actual
320 * computation harder to spot.
321 *
322 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
323 * makes an average of 6 extra calls. Compared to the main computation which
324 * makes 64 such calls, this represents an average performance degradation of
325 * less than 10%.
326 *
327 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
328 * know it's always 8. This saves a bit of code size and execution speed.
329 */
330static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100331 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300332{
333
334 uECC_word_t r0 = 0;
335 uECC_word_t r1 = 0;
336 uECC_word_t r2 = 0;
337 wordcount_t i, k;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200338 const uint8_t num_words = 8;
339
340 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
341 uint8_t delays = s ? s->delays[s->i++] : 0;
342 uECC_word_t rr0 = 0, rr1 = 0;
343 volatile uECC_word_t r;
344
345 /* Mimic start of next loop: k in [0, 3] */
346 k = 0 + (delays & 0x03);
347 delays >>= 2;
348 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
349 * k = 3 -> i in [1, 3] -> 3 extra muladd */
350 for (i = 0; i <= k; ++i) {
351 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
352 }
353 r = rr0;
354 rr0 = rr1;
355 rr1 = r2;
356 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300357
358 /* Compute each digit of result in sequence, maintaining the carries. */
359 for (k = 0; k < num_words; ++k) {
360
361 for (i = 0; i <= k; ++i) {
362 muladd(left[i], right[k - i], &r0, &r1, &r2);
363 }
364
365 result[k] = r0;
366 r0 = r1;
367 r1 = r2;
368 r2 = 0;
369 }
370
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200371 /* Mimic end of previous loop: k in [4, 7] */
372 k = 4 + (delays & 0x03);
373 delays >>= 2;
374 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
375 * k = 7 -> i in [5, 7] -> 3 extra muladd */
376 for (i = 5; i <= k; ++i) {
377 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
378 }
379 r = rr0;
380 rr0 = rr1;
381 rr1 = r2;
382 r2 = 0;
383
384 /* Mimic start of next loop: k in [8, 11] */
385 k = 11 - (delays & 0x03);
386 delays >>= 2;
387 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
388 * k = 11 -> i in [8, 7] -> 0 extra muladd */
389 for (i = (k + 5) - num_words; i < num_words; ++i) {
390 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
391 }
392 r = rr0;
393 rr0 = rr1;
394 rr1 = r2;
395 r2 = 0;
396
Jarno Lamsa18987a42019-04-24 15:40:43 +0300397 for (k = num_words; k < num_words * 2 - 1; ++k) {
398
399 for (i = (k + 1) - num_words; i < num_words; ++i) {
400 muladd(left[i], right[k - i], &r0, &r1, &r2);
401 }
402 result[k] = r0;
403 r0 = r1;
404 r1 = r2;
405 r2 = 0;
406 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200407
Jarno Lamsa18987a42019-04-24 15:40:43 +0300408 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200409
410 /* Mimic end of previous loop: k in [12, 15] */
411 k = 15 - (delays & 0x03);
412 delays >>= 2;
413 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
414 * k = 15 -> i in [8, 7] -> 0 extra muladd */
415 for (i = (k + 1) - num_words; i < num_words; ++i) {
416 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
417 }
418 r = rr0;
419 rr0 = rr1;
420 rr1 = r2;
421 r2 = 0;
422
423 /* avoid warning that r is set but not used */
424 (void) r;
425}
426
427/* Computes result = left * right. Result must be 2 * num_words long. */
428static void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left,
429 const uECC_word_t *right, wordcount_t num_words)
430{
431 (void) num_words;
432 uECC_vli_mult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300433}
434
435void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
436 const uECC_word_t *right, const uECC_word_t *mod,
437 wordcount_t num_words)
438{
439 uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
440 if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
441 /* result > mod (result = mod + remainder), so subtract mod to get
442 * remainder. */
443 uECC_vli_sub(result, result, mod, num_words);
444 }
445}
446
447void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
448 const uECC_word_t *right, const uECC_word_t *mod,
449 wordcount_t num_words)
450{
451 uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
452 if (l_borrow) {
453 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
454 * we can get the correct result from result + mod (with overflow). */
455 uECC_vli_add(result, result, mod, num_words);
456 }
457}
458
459/* Computes result = product % mod, where product is 2N words long. */
460/* Currently only designed to work for curve_p or curve_n. */
461void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
462 const uECC_word_t *mod, wordcount_t num_words)
463{
464 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
465 uECC_word_t tmp[2 * NUM_ECC_WORDS];
466 uECC_word_t *v[2] = {tmp, product};
467 uECC_word_t index;
468
469 /* Shift mod so its highest set bit is at the maximum position. */
470 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
471 uECC_vli_numBits(mod, num_words);
472 wordcount_t word_shift = shift / uECC_WORD_BITS;
473 wordcount_t bit_shift = shift % uECC_WORD_BITS;
474 uECC_word_t carry = 0;
475 uECC_vli_clear(mod_multiple, word_shift);
476 if (bit_shift > 0) {
477 for(index = 0; index < (uECC_word_t)num_words; ++index) {
478 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
479 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
480 }
481 } else {
482 uECC_vli_set(mod_multiple + word_shift, mod, num_words);
483 }
484
485 for (index = 1; shift >= 0; --shift) {
486 uECC_word_t borrow = 0;
487 wordcount_t i;
488 for (i = 0; i < num_words * 2; ++i) {
489 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
490 if (diff != v[index][i]) {
491 borrow = (diff > v[index][i]);
492 }
493 v[1 - index][i] = diff;
494 }
495 /* Swap the index if there was no borrow */
496 index = !(index ^ borrow);
497 uECC_vli_rshift1(mod_multiple, num_words);
498 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
499 (uECC_WORD_BITS - 1);
500 uECC_vli_rshift1(mod_multiple + num_words, num_words);
501 }
502 uECC_vli_set(result, v[index], num_words);
503}
504
505void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
506 const uECC_word_t *right, const uECC_word_t *mod,
507 wordcount_t num_words)
508{
509 uECC_word_t product[2 * NUM_ECC_WORDS];
510 uECC_vli_mult(product, left, right, num_words);
511 uECC_vli_mmod(result, product, mod, num_words);
512}
513
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100514static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100515 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100516{
517 uECC_word_t product[2 * NUM_ECC_WORDS];
518 uECC_vli_mult_rnd(product, left, right, s);
519
520 vli_mmod_fast_secp256r1(result, product);
521}
522
Jarno Lamsa18987a42019-04-24 15:40:43 +0300523void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
524 const uECC_word_t *right, uECC_Curve curve)
525{
526 uECC_word_t product[2 * NUM_ECC_WORDS];
527 uECC_vli_mult(product, left, right, curve->num_words);
528
529 curve->mmod_fast(result, product);
530}
531
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100532static void uECC_vli_modSquare_rnd(uECC_word_t *result,
533 const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100534 ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100535{
536 uECC_vli_modMult_rnd(result, left, left, s);
537}
538
Jarno Lamsa18987a42019-04-24 15:40:43 +0300539static void uECC_vli_modSquare_fast(uECC_word_t *result,
540 const uECC_word_t *left,
541 uECC_Curve curve)
542{
543 uECC_vli_modMult_fast(result, left, left, curve);
544}
545
546
547#define EVEN(vli) (!(vli[0] & 1))
548
549static void vli_modInv_update(uECC_word_t *uv,
550 const uECC_word_t *mod,
551 wordcount_t num_words)
552{
553
554 uECC_word_t carry = 0;
555
556 if (!EVEN(uv)) {
557 carry = uECC_vli_add(uv, uv, mod, num_words);
558 }
559 uECC_vli_rshift1(uv, num_words);
560 if (carry) {
561 uv[num_words - 1] |= HIGH_BIT_SET;
562 }
563}
564
565void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
566 const uECC_word_t *mod, wordcount_t num_words)
567{
568 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
569 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
570 cmpresult_t cmpResult;
571
572 if (uECC_vli_isZero(input, num_words)) {
573 uECC_vli_clear(result, num_words);
574 return;
575 }
576
577 uECC_vli_set(a, input, num_words);
578 uECC_vli_set(b, mod, num_words);
579 uECC_vli_clear(u, num_words);
580 u[0] = 1;
581 uECC_vli_clear(v, num_words);
582 while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
583 if (EVEN(a)) {
584 uECC_vli_rshift1(a, num_words);
585 vli_modInv_update(u, mod, num_words);
586 } else if (EVEN(b)) {
587 uECC_vli_rshift1(b, num_words);
588 vli_modInv_update(v, mod, num_words);
589 } else if (cmpResult > 0) {
590 uECC_vli_sub(a, a, b, num_words);
591 uECC_vli_rshift1(a, num_words);
592 if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
593 uECC_vli_add(u, u, mod, num_words);
594 }
595 uECC_vli_sub(u, u, v, num_words);
596 vli_modInv_update(u, mod, num_words);
597 } else {
598 uECC_vli_sub(b, b, a, num_words);
599 uECC_vli_rshift1(b, num_words);
600 if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
601 uECC_vli_add(v, v, mod, num_words);
602 }
603 uECC_vli_sub(v, v, u, num_words);
604 vli_modInv_update(v, mod, num_words);
605 }
606 }
607 uECC_vli_set(result, u, num_words);
608}
609
610/* ------ Point operations ------ */
611
612void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
613 uECC_word_t * Z1, uECC_Curve curve)
614{
615 /* t1 = X, t2 = Y, t3 = Z */
616 uECC_word_t t4[NUM_ECC_WORDS];
617 uECC_word_t t5[NUM_ECC_WORDS];
618 wordcount_t num_words = curve->num_words;
619
620 if (uECC_vli_isZero(Z1, num_words)) {
621 return;
622 }
623
624 uECC_vli_modSquare_fast(t4, Y1, curve); /* t4 = y1^2 */
625 uECC_vli_modMult_fast(t5, X1, t4, curve); /* t5 = x1*y1^2 = A */
626 uECC_vli_modSquare_fast(t4, t4, curve); /* t4 = y1^4 */
627 uECC_vli_modMult_fast(Y1, Y1, Z1, curve); /* t2 = y1*z1 = z3 */
628 uECC_vli_modSquare_fast(Z1, Z1, curve); /* t3 = z1^2 */
629
630 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
631 uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
632 uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
633 uECC_vli_modMult_fast(X1, X1, Z1, curve); /* t1 = x1^2 - z1^4 */
634
635 uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
636 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
637 if (uECC_vli_testBit(X1, 0)) {
638 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p, num_words);
639 uECC_vli_rshift1(X1, num_words);
640 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
641 } else {
642 uECC_vli_rshift1(X1, num_words);
643 }
644
645 /* t1 = 3/2*(x1^2 - z1^4) = B */
646 uECC_vli_modSquare_fast(Z1, X1, curve); /* t3 = B^2 */
647 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
648 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
649 uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
650 uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = B * (A - x3) */
651 /* t4 = B * (A - x3) - y1^4 = y3: */
652 uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
653
654 uECC_vli_set(X1, Z1, num_words);
655 uECC_vli_set(Z1, Y1, num_words);
656 uECC_vli_set(Y1, t4, num_words);
657}
658
659void x_side_default(uECC_word_t *result,
660 const uECC_word_t *x,
661 uECC_Curve curve)
662{
663 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
664 wordcount_t num_words = curve->num_words;
665
666 uECC_vli_modSquare_fast(result, x, curve); /* r = x^2 */
667 uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
668 uECC_vli_modMult_fast(result, result, x, curve); /* r = x^3 - 3x */
669 /* r = x^3 - 3x + b: */
670 uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
671}
672
673uECC_Curve uECC_secp256r1(void)
674{
675 return &curve_secp256r1;
676}
677
678void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
679{
680 unsigned int tmp[NUM_ECC_WORDS];
681 int carry;
682
683 /* t */
684 uECC_vli_set(result, product, NUM_ECC_WORDS);
685
686 /* s1 */
687 tmp[0] = tmp[1] = tmp[2] = 0;
688 tmp[3] = product[11];
689 tmp[4] = product[12];
690 tmp[5] = product[13];
691 tmp[6] = product[14];
692 tmp[7] = product[15];
693 carry = uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
694 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
695
696 /* s2 */
697 tmp[3] = product[12];
698 tmp[4] = product[13];
699 tmp[5] = product[14];
700 tmp[6] = product[15];
701 tmp[7] = 0;
702 carry += uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
703 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
704
705 /* s3 */
706 tmp[0] = product[8];
707 tmp[1] = product[9];
708 tmp[2] = product[10];
709 tmp[3] = tmp[4] = tmp[5] = 0;
710 tmp[6] = product[14];
711 tmp[7] = product[15];
712 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
713
714 /* s4 */
715 tmp[0] = product[9];
716 tmp[1] = product[10];
717 tmp[2] = product[11];
718 tmp[3] = product[13];
719 tmp[4] = product[14];
720 tmp[5] = product[15];
721 tmp[6] = product[13];
722 tmp[7] = product[8];
723 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
724
725 /* d1 */
726 tmp[0] = product[11];
727 tmp[1] = product[12];
728 tmp[2] = product[13];
729 tmp[3] = tmp[4] = tmp[5] = 0;
730 tmp[6] = product[8];
731 tmp[7] = product[10];
732 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
733
734 /* d2 */
735 tmp[0] = product[12];
736 tmp[1] = product[13];
737 tmp[2] = product[14];
738 tmp[3] = product[15];
739 tmp[4] = tmp[5] = 0;
740 tmp[6] = product[9];
741 tmp[7] = product[11];
742 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
743
744 /* d3 */
745 tmp[0] = product[13];
746 tmp[1] = product[14];
747 tmp[2] = product[15];
748 tmp[3] = product[8];
749 tmp[4] = product[9];
750 tmp[5] = product[10];
751 tmp[6] = 0;
752 tmp[7] = product[12];
753 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
754
755 /* d4 */
756 tmp[0] = product[14];
757 tmp[1] = product[15];
758 tmp[2] = 0;
759 tmp[3] = product[9];
760 tmp[4] = product[10];
761 tmp[5] = product[11];
762 tmp[6] = 0;
763 tmp[7] = product[13];
764 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
765
766 if (carry < 0) {
767 do {
768 carry += uECC_vli_add(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
769 }
770 while (carry < 0);
771 } else {
772 while (carry ||
773 uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
774 carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
775 }
776 }
777}
778
779uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
780{
781 return uECC_vli_isZero(point, curve->num_words * 2);
782}
783
784void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
785 uECC_Curve curve)
786{
787 uECC_word_t t1[NUM_ECC_WORDS];
788
789 uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */
790 uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
791 uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */
792 uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
793}
794
795/* P = (x1, y1) => 2P, (x2, y2) => P' */
796static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
797 uECC_word_t * X2, uECC_word_t * Y2,
798 const uECC_word_t * const initial_Z,
799 uECC_Curve curve)
800{
801 uECC_word_t z[NUM_ECC_WORDS];
802 wordcount_t num_words = curve->num_words;
803 if (initial_Z) {
804 uECC_vli_set(z, initial_Z, num_words);
805 } else {
806 uECC_vli_clear(z, num_words);
807 z[0] = 1;
808 }
809
810 uECC_vli_set(X2, X1, num_words);
811 uECC_vli_set(Y2, Y1, num_words);
812
813 apply_z(X1, Y1, z, curve);
814 curve->double_jacobian(X1, Y1, z, curve);
815 apply_z(X2, Y2, z, curve);
816}
817
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100818static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
819 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100820 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300821{
822 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
823 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100824 const uECC_Curve curve = &curve_secp256r1;
825 const wordcount_t num_words = 8;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300826
827 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100828 uECC_vli_modSquare_rnd(t5, t5, s); /* t5 = (x2 - x1)^2 = A */
829 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
830 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300831 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100832 uECC_vli_modSquare_rnd(t5, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300833
834 uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
835 uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
836 uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100837 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300838 uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100839 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300840 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
841
842 uECC_vli_set(X2, t5, num_words);
843}
844
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100845void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
846 uECC_word_t * X2, uECC_word_t * Y2,
847 uECC_Curve curve)
848{
849 (void) curve;
850 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
851}
852
Jarno Lamsa18987a42019-04-24 15:40:43 +0300853/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
854 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
855 or P => P - Q, Q => P + Q
856 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100857static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
858 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100859 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300860{
861 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
862 uECC_word_t t5[NUM_ECC_WORDS];
863 uECC_word_t t6[NUM_ECC_WORDS];
864 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100865 const uECC_Curve curve = &curve_secp256r1;
866 const wordcount_t num_words = 8;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300867
868 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100869 uECC_vli_modSquare_rnd(t5, t5, s); /* t5 = (x2 - x1)^2 = A */
870 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
871 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300872 uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
873 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
874
875 uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100876 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300877 uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100878 uECC_vli_modSquare_rnd(X2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300879 uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
880
881 uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100882 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300883 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
884 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
885
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100886 uECC_vli_modSquare_rnd(t7, t5, s); /* t7 = (y2 + y1)^2 = F */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300887 uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
888 uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100889 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300890 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
891 uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
892
893 uECC_vli_set(X1, t7, num_words);
894}
895
896void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
897 const uECC_word_t * scalar,
898 const uECC_word_t * initial_Z,
899 bitcount_t num_bits, uECC_Curve curve)
900{
901 /* R0 and R1 */
902 uECC_word_t Rx[2][NUM_ECC_WORDS];
903 uECC_word_t Ry[2][NUM_ECC_WORDS];
904 uECC_word_t z[NUM_ECC_WORDS];
905 bitcount_t i;
906 uECC_word_t nb;
907 wordcount_t num_words = curve->num_words;
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100908 ecc_wait_state_t wait_state;
909 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300910
911 uECC_vli_set(Rx[1], point, num_words);
912 uECC_vli_set(Ry[1], point + num_words, num_words);
913
914 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
915
916 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100917 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300918 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100919 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
920 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300921 }
922
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100923 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300924 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100925 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300926
927 /* Find final 1/Z value. */
928 uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
929 uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */
930 uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */
931 uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
932 /* yP / (xP * Yb * (X1 - X0)) */
933 uECC_vli_modMult_fast(z, z, point + num_words, curve);
934 /* Xb * yP / (xP * Yb * (X1 - X0)) */
935 uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve);
936 /* End 1/Z calculation */
937
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100938 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300939 apply_z(Rx[0], Ry[0], z, curve);
940
941 uECC_vli_set(result, Rx[0], num_words);
942 uECC_vli_set(result + num_words, Ry[0], num_words);
943}
944
945uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
946 uECC_word_t *k1, uECC_Curve curve)
947{
948
949 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
950
951 bitcount_t num_n_bits = curve->num_n_bits;
952
953 uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
954 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
955 uECC_vli_testBit(k0, num_n_bits));
956
957 uECC_vli_add(k1, k0, curve->n, num_n_words);
958
959 return carry;
960}
961
962uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
963 uECC_word_t *private_key,
964 uECC_Curve curve)
965{
966
967 uECC_word_t tmp1[NUM_ECC_WORDS];
968 uECC_word_t tmp2[NUM_ECC_WORDS];
969 uECC_word_t *p2[2] = {tmp1, tmp2};
970 uECC_word_t carry;
971
972 /* Regularize the bitcount for the private key so that attackers cannot
973 * use a side channel attack to learn the number of leading zeros. */
974 carry = regularize_k(private_key, tmp1, tmp2, curve);
975
976 EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
977
978 if (EccPoint_isZero(result, curve)) {
979 return 0;
980 }
981 return 1;
982}
983
984/* Converts an integer in uECC native format to big-endian bytes. */
985void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
986 const unsigned int *native)
987{
988 wordcount_t i;
989 for (i = 0; i < num_bytes; ++i) {
990 unsigned b = num_bytes - 1 - i;
991 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
992 }
993}
994
995/* Converts big-endian bytes to an integer in uECC native format. */
996void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
997 int num_bytes)
998{
999 wordcount_t i;
1000 uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
1001 for (i = 0; i < num_bytes; ++i) {
1002 unsigned b = num_bytes - 1 - i;
1003 native[b / uECC_WORD_SIZE] |=
1004 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1005 }
1006}
1007
1008int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1009 wordcount_t num_words)
1010{
1011 uECC_word_t mask = (uECC_word_t)-1;
1012 uECC_word_t tries;
1013 bitcount_t num_bits = uECC_vli_numBits(top, num_words);
1014
1015 if (!g_rng_function) {
1016 return 0;
1017 }
1018
1019 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1020 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1021 return 0;
1022 }
1023 random[num_words - 1] &=
1024 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
1025 if (!uECC_vli_isZero(random, num_words) &&
1026 uECC_vli_cmp(top, random, num_words) == 1) {
1027 return 1;
1028 }
1029 }
1030 return 0;
1031}
1032
1033
1034int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1035{
1036 uECC_word_t tmp1[NUM_ECC_WORDS];
1037 uECC_word_t tmp2[NUM_ECC_WORDS];
1038 wordcount_t num_words = curve->num_words;
1039
1040 /* The point at infinity is invalid. */
1041 if (EccPoint_isZero(point, curve)) {
1042 return -1;
1043 }
1044
1045 /* x and y must be smaller than p. */
1046 if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
1047 uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
1048 return -2;
1049 }
1050
1051 uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
1052 curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
1053
1054 /* Make sure that y^2 == x^3 + ax + b */
1055 if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
1056 return -3;
1057
1058 return 0;
1059}
1060
1061int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1062{
1063
1064 uECC_word_t _public[NUM_ECC_WORDS * 2];
1065
1066 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1067 uECC_vli_bytesToNative(
1068 _public + curve->num_words,
1069 public_key + curve->num_bytes,
1070 curve->num_bytes);
1071
1072 if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
1073 return -4;
1074 }
1075
1076 return uECC_valid_point(_public, curve);
1077}
1078
1079int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1080 uECC_Curve curve)
1081{
1082
1083 uECC_word_t _private[NUM_ECC_WORDS];
1084 uECC_word_t _public[NUM_ECC_WORDS * 2];
1085
1086 uECC_vli_bytesToNative(
1087 _private,
1088 private_key,
1089 BITS_TO_BYTES(curve->num_n_bits));
1090
1091 /* Make sure the private key is in the range [1, n-1]. */
1092 if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
1093 return 0;
1094 }
1095
1096 if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
1097 return 0;
1098 }
1099
1100 /* Compute public key. */
1101 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1102 return 0;
1103 }
1104
1105 uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1106 uECC_vli_nativeToBytes(
1107 public_key +
1108 curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1109 return 1;
1110}
Jarno Lamsa46132202019-04-29 14:29:52 +03001111#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +02001112typedef int mbedtls_dummy_tinycrypt_def;
1113#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001114