blob: cbc024acd2af1406014df4df63083f8d9c4e0247 [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>
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +010068#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030069#include <string.h>
70
71/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
72 * has access to enough entropy in order to feed the PRNG regularly. */
73#if default_RNG_defined
74static uECC_RNG_Function g_rng_function = &default_CSPRNG;
75#else
76static uECC_RNG_Function g_rng_function = 0;
77#endif
78
79void uECC_set_rng(uECC_RNG_Function rng_function)
80{
81 g_rng_function = rng_function;
82}
83
84uECC_RNG_Function uECC_get_rng(void)
85{
86 return g_rng_function;
87}
88
89int uECC_curve_private_key_size(uECC_Curve curve)
90{
91 return BITS_TO_BYTES(curve->num_n_bits);
92}
93
94int uECC_curve_public_key_size(uECC_Curve curve)
95{
96 return 2 * curve->num_bytes;
97}
98
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +010099void uECC_vli_clear(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300100{
101 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100102 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300103 vli[i] = 0;
104 }
105}
106
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100107uECC_word_t uECC_vli_isZero(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300108{
109 uECC_word_t bits = 0;
110 wordcount_t i;
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100111 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300112 bits |= vli[i];
113 }
114 return (bits == 0);
115}
116
117uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
118{
119 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
120 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
121}
122
123/* Counts the number of words in vli. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100124static wordcount_t vli_numDigits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300125{
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. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100130 for (i = NUM_ECC_WORDS - 1; i >= 0 && vli[i] == 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300131 }
132
133 return (i + 1);
134}
135
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100136bitcount_t uECC_vli_numBits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300137{
138
139 uECC_word_t i;
140 uECC_word_t digit;
141
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100142 wordcount_t num_digits = vli_numDigits(vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300143 if (num_digits == 0) {
144 return 0;
145 }
146
147 digit = vli[num_digits - 1];
148 for (i = 0; digit; ++i) {
149 digit >>= 1;
150 }
151
152 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
153}
154
155void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
156 wordcount_t num_words)
157{
158 wordcount_t i;
159
160 for (i = 0; i < num_words; ++i) {
161 dest[i] = src[i];
162 }
163}
164
165cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
166 const uECC_word_t *right,
167 wordcount_t num_words)
168{
169 wordcount_t i;
170
171 for (i = num_words - 1; i >= 0; --i) {
172 if (left[i] > right[i]) {
173 return 1;
174 } else if (left[i] < right[i]) {
175 return -1;
176 }
177 }
178 return 0;
179}
180
181uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
182 wordcount_t num_words)
183{
184
185 uECC_word_t diff = 0;
186 wordcount_t i;
187
188 for (i = num_words - 1; i >= 0; --i) {
189 diff |= (left[i] ^ right[i]);
190 }
191 return !(diff == 0);
192}
193
194uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
195{
196 return (p_true*(cond)) | (p_false*(!cond));
197}
198
199/* Computes result = left - right, returning borrow, in constant time.
200 * Can modify in place. */
201uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
202 const uECC_word_t *right, wordcount_t num_words)
203{
204 uECC_word_t borrow = 0;
205 wordcount_t i;
206 for (i = 0; i < num_words; ++i) {
207 uECC_word_t diff = left[i] - right[i] - borrow;
208 uECC_word_t val = (diff > left[i]);
209 borrow = cond_set(val, borrow, (diff != left[i]));
210
211 result[i] = diff;
212 }
213 return borrow;
214}
215
216/* Computes result = left + right, returning carry, in constant time.
217 * Can modify in place. */
218static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100219 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300220{
221 uECC_word_t carry = 0;
222 wordcount_t i;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100223 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300224 uECC_word_t sum = left[i] + right[i] + carry;
225 uECC_word_t val = (sum < left[i]);
226 carry = cond_set(val, carry, (sum != left[i]));
227 result[i] = sum;
228 }
229 return carry;
230}
231
232cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
233 wordcount_t num_words)
234{
235 uECC_word_t tmp[NUM_ECC_WORDS];
236 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100237 uECC_word_t equal = uECC_vli_isZero(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300238 return (!equal - 2 * neg);
239}
240
241/* Computes vli = vli >> 1. */
242static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
243{
244 uECC_word_t *end = vli;
245 uECC_word_t carry = 0;
246
247 vli += num_words;
248 while (vli-- > end) {
249 uECC_word_t temp = *vli;
250 *vli = (temp >> 1) | carry;
251 carry = temp << (uECC_WORD_BITS - 1);
252 }
253}
254
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100255/* Compute a * b + r, where r is a double-word with high-order word r1 and
256 * low-order word r0, and store the result in the same double-word (r1, r0),
257 * with the carry bit stored in r2.
258 *
259 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200260 * [in] a, b: operands to be multiplied
261 * [in] r0, r1: low and high-order words of operand to add
262 * [out] r0, r1: low and high-order words of the result
263 * [out] r2: carry
264 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300265static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
266 uECC_word_t *r1, uECC_word_t *r2)
267{
268
269 uECC_dword_t p = (uECC_dword_t)a * b;
270 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
271 r01 += p;
272 *r2 += (r01 < p);
273 *r1 = r01 >> uECC_WORD_BITS;
274 *r0 = (uECC_word_t)r01;
275
276}
277
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200278/* State for implementing random delays in uECC_vli_mult_rnd().
279 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100280 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200281 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
282 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100283 * Randomized vli multiplication is used only for point operations
284 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
285 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
286 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100287 * indirectly through uECC_vli_modMult_rnd().
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100288 *
289 * Considering this, in order to minimize the number of calls to the RNG
290 * (which impact performance) while keeping the size of the structure low,
291 * make room for 14 randomized vli mults, which corresponds to one step in the
292 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200293 */
294typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100295 uint8_t i;
296 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100297} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200298
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100299/*
300 * Reset wait_state so that it's ready to be used.
301 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100302void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100303{
304 if (ws == NULL)
305 return;
306
307 ws->i = 0;
308 g_rng_function(ws->delays, sizeof(ws->delays));
309}
310
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200311/* Computes result = left * right. Result must be 2 * num_words long.
312 *
313 * As a counter-measure against horizontal attacks, add noise by performing
314 * a random number of extra computations performing random additional accesses
315 * to limbs of the input.
316 *
317 * Each of the two actual computation loops is surrounded by two
318 * similar-looking waiting loops, to make the beginning and end of the actual
319 * computation harder to spot.
320 *
321 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
322 * makes an average of 6 extra calls. Compared to the main computation which
323 * makes 64 such calls, this represents an average performance degradation of
324 * less than 10%.
325 *
326 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
327 * know it's always 8. This saves a bit of code size and execution speed.
328 */
329static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100330 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300331{
332
333 uECC_word_t r0 = 0;
334 uECC_word_t r1 = 0;
335 uECC_word_t r2 = 0;
336 wordcount_t i, k;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100337 const uint8_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200338
339 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
340 uint8_t delays = s ? s->delays[s->i++] : 0;
341 uECC_word_t rr0 = 0, rr1 = 0;
342 volatile uECC_word_t r;
343
344 /* Mimic start of next loop: k in [0, 3] */
345 k = 0 + (delays & 0x03);
346 delays >>= 2;
347 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
348 * k = 3 -> i in [1, 3] -> 3 extra muladd */
349 for (i = 0; i <= k; ++i) {
350 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
351 }
352 r = rr0;
353 rr0 = rr1;
354 rr1 = r2;
355 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300356
357 /* Compute each digit of result in sequence, maintaining the carries. */
358 for (k = 0; k < num_words; ++k) {
359
360 for (i = 0; i <= k; ++i) {
361 muladd(left[i], right[k - i], &r0, &r1, &r2);
362 }
363
364 result[k] = r0;
365 r0 = r1;
366 r1 = r2;
367 r2 = 0;
368 }
369
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200370 /* Mimic end of previous loop: k in [4, 7] */
371 k = 4 + (delays & 0x03);
372 delays >>= 2;
373 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
374 * k = 7 -> i in [5, 7] -> 3 extra muladd */
375 for (i = 5; i <= k; ++i) {
376 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
377 }
378 r = rr0;
379 rr0 = rr1;
380 rr1 = r2;
381 r2 = 0;
382
383 /* Mimic start of next loop: k in [8, 11] */
384 k = 11 - (delays & 0x03);
385 delays >>= 2;
386 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
387 * k = 11 -> i in [8, 7] -> 0 extra muladd */
388 for (i = (k + 5) - num_words; i < num_words; ++i) {
389 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
390 }
391 r = rr0;
392 rr0 = rr1;
393 rr1 = r2;
394 r2 = 0;
395
Jarno Lamsa18987a42019-04-24 15:40:43 +0300396 for (k = num_words; k < num_words * 2 - 1; ++k) {
397
398 for (i = (k + 1) - num_words; i < num_words; ++i) {
399 muladd(left[i], right[k - i], &r0, &r1, &r2);
400 }
401 result[k] = r0;
402 r0 = r1;
403 r1 = r2;
404 r2 = 0;
405 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200406
Jarno Lamsa18987a42019-04-24 15:40:43 +0300407 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200408
409 /* Mimic end of previous loop: k in [12, 15] */
410 k = 15 - (delays & 0x03);
411 delays >>= 2;
412 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
413 * k = 15 -> i in [8, 7] -> 0 extra muladd */
414 for (i = (k + 1) - num_words; i < num_words; ++i) {
415 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
416 }
417 r = rr0;
418 rr0 = rr1;
419 rr1 = r2;
420 r2 = 0;
421
422 /* avoid warning that r is set but not used */
423 (void) r;
424}
425
Jarno Lamsa18987a42019-04-24 15:40:43 +0300426void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
427 const uECC_word_t *right, const uECC_word_t *mod,
428 wordcount_t num_words)
429{
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100430 uECC_word_t carry = uECC_vli_add(result, left, right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300431 if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
432 /* result > mod (result = mod + remainder), so subtract mod to get
433 * remainder. */
434 uECC_vli_sub(result, result, mod, num_words);
435 }
436}
437
438void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
439 const uECC_word_t *right, const uECC_word_t *mod,
440 wordcount_t num_words)
441{
442 uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
443 if (l_borrow) {
444 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
445 * we can get the correct result from result + mod (with overflow). */
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100446 uECC_vli_add(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300447 }
448}
449
450/* Computes result = product % mod, where product is 2N words long. */
451/* Currently only designed to work for curve_p or curve_n. */
452void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
453 const uECC_word_t *mod, wordcount_t num_words)
454{
455 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
456 uECC_word_t tmp[2 * NUM_ECC_WORDS];
457 uECC_word_t *v[2] = {tmp, product};
458 uECC_word_t index;
459
460 /* Shift mod so its highest set bit is at the maximum position. */
461 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100462 uECC_vli_numBits(mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300463 wordcount_t word_shift = shift / uECC_WORD_BITS;
464 wordcount_t bit_shift = shift % uECC_WORD_BITS;
465 uECC_word_t carry = 0;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100466 uECC_vli_clear(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300467 if (bit_shift > 0) {
468 for(index = 0; index < (uECC_word_t)num_words; ++index) {
469 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
470 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
471 }
472 } else {
473 uECC_vli_set(mod_multiple + word_shift, mod, num_words);
474 }
475
476 for (index = 1; shift >= 0; --shift) {
477 uECC_word_t borrow = 0;
478 wordcount_t i;
479 for (i = 0; i < num_words * 2; ++i) {
480 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
481 if (diff != v[index][i]) {
482 borrow = (diff > v[index][i]);
483 }
484 v[1 - index][i] = diff;
485 }
486 /* Swap the index if there was no borrow */
487 index = !(index ^ borrow);
488 uECC_vli_rshift1(mod_multiple, num_words);
489 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
490 (uECC_WORD_BITS - 1);
491 uECC_vli_rshift1(mod_multiple + num_words, num_words);
492 }
493 uECC_vli_set(result, v[index], num_words);
494}
495
496void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
497 const uECC_word_t *right, const uECC_word_t *mod,
498 wordcount_t num_words)
499{
500 uECC_word_t product[2 * NUM_ECC_WORDS];
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100501 uECC_vli_mult_rnd(product, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300502 uECC_vli_mmod(result, product, mod, num_words);
503}
504
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100505static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100506 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100507{
508 uECC_word_t product[2 * NUM_ECC_WORDS];
509 uECC_vli_mult_rnd(product, left, right, s);
510
511 vli_mmod_fast_secp256r1(result, product);
512}
513
Jarno Lamsa18987a42019-04-24 15:40:43 +0300514void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100515 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300516{
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100517 uECC_vli_modMult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300518}
519
Jarno Lamsa18987a42019-04-24 15:40:43 +0300520#define EVEN(vli) (!(vli[0] & 1))
521
522static void vli_modInv_update(uECC_word_t *uv,
523 const uECC_word_t *mod,
524 wordcount_t num_words)
525{
526
527 uECC_word_t carry = 0;
528
529 if (!EVEN(uv)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100530 carry = uECC_vli_add(uv, uv, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300531 }
532 uECC_vli_rshift1(uv, num_words);
533 if (carry) {
534 uv[num_words - 1] |= HIGH_BIT_SET;
535 }
536}
537
538void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
539 const uECC_word_t *mod, wordcount_t num_words)
540{
541 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
542 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
543 cmpresult_t cmpResult;
544
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100545 if (uECC_vli_isZero(input)) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100546 uECC_vli_clear(result);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300547 return;
548 }
549
550 uECC_vli_set(a, input, num_words);
551 uECC_vli_set(b, mod, num_words);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100552 uECC_vli_clear(u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300553 u[0] = 1;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100554 uECC_vli_clear(v);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300555 while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
556 if (EVEN(a)) {
557 uECC_vli_rshift1(a, num_words);
558 vli_modInv_update(u, mod, num_words);
559 } else if (EVEN(b)) {
560 uECC_vli_rshift1(b, num_words);
561 vli_modInv_update(v, mod, num_words);
562 } else if (cmpResult > 0) {
563 uECC_vli_sub(a, a, b, num_words);
564 uECC_vli_rshift1(a, num_words);
565 if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100566 uECC_vli_add(u, u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300567 }
568 uECC_vli_sub(u, u, v, num_words);
569 vli_modInv_update(u, mod, num_words);
570 } else {
571 uECC_vli_sub(b, b, a, num_words);
572 uECC_vli_rshift1(b, num_words);
573 if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100574 uECC_vli_add(v, v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300575 }
576 uECC_vli_sub(v, v, u, num_words);
577 vli_modInv_update(v, mod, num_words);
578 }
579 }
580 uECC_vli_set(result, u, num_words);
581}
582
583/* ------ Point operations ------ */
584
585void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
586 uECC_word_t * Z1, uECC_Curve curve)
587{
588 /* t1 = X, t2 = Y, t3 = Z */
589 uECC_word_t t4[NUM_ECC_WORDS];
590 uECC_word_t t5[NUM_ECC_WORDS];
591 wordcount_t num_words = curve->num_words;
592
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100593 if (uECC_vli_isZero(Z1)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300594 return;
595 }
596
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100597 uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */
598 uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
599 uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */
600 uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
601 uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300602
603 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
604 uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
605 uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100606 uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300607
608 uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
609 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
610 if (uECC_vli_testBit(X1, 0)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100611 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300612 uECC_vli_rshift1(X1, num_words);
613 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
614 } else {
615 uECC_vli_rshift1(X1, num_words);
616 }
617
618 /* t1 = 3/2*(x1^2 - z1^4) = B */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100619 uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300620 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
621 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
622 uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100623 uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300624 /* t4 = B * (A - x3) - y1^4 = y3: */
625 uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
626
627 uECC_vli_set(X1, Z1, num_words);
628 uECC_vli_set(Z1, Y1, num_words);
629 uECC_vli_set(Y1, t4, num_words);
630}
631
632void x_side_default(uECC_word_t *result,
633 const uECC_word_t *x,
634 uECC_Curve curve)
635{
636 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
637 wordcount_t num_words = curve->num_words;
638
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100639 uECC_vli_modMult_fast(result, x, x); /* r = x^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300640 uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100641 uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300642 /* r = x^3 - 3x + b: */
643 uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
644}
645
646uECC_Curve uECC_secp256r1(void)
647{
648 return &curve_secp256r1;
649}
650
651void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
652{
653 unsigned int tmp[NUM_ECC_WORDS];
654 int carry;
655
656 /* t */
657 uECC_vli_set(result, product, NUM_ECC_WORDS);
658
659 /* s1 */
660 tmp[0] = tmp[1] = tmp[2] = 0;
661 tmp[3] = product[11];
662 tmp[4] = product[12];
663 tmp[5] = product[13];
664 tmp[6] = product[14];
665 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100666 carry = uECC_vli_add(tmp, tmp, tmp);
667 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300668
669 /* s2 */
670 tmp[3] = product[12];
671 tmp[4] = product[13];
672 tmp[5] = product[14];
673 tmp[6] = product[15];
674 tmp[7] = 0;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100675 carry += uECC_vli_add(tmp, tmp, tmp);
676 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300677
678 /* s3 */
679 tmp[0] = product[8];
680 tmp[1] = product[9];
681 tmp[2] = product[10];
682 tmp[3] = tmp[4] = tmp[5] = 0;
683 tmp[6] = product[14];
684 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100685 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300686
687 /* s4 */
688 tmp[0] = product[9];
689 tmp[1] = product[10];
690 tmp[2] = product[11];
691 tmp[3] = product[13];
692 tmp[4] = product[14];
693 tmp[5] = product[15];
694 tmp[6] = product[13];
695 tmp[7] = product[8];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100696 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300697
698 /* d1 */
699 tmp[0] = product[11];
700 tmp[1] = product[12];
701 tmp[2] = product[13];
702 tmp[3] = tmp[4] = tmp[5] = 0;
703 tmp[6] = product[8];
704 tmp[7] = product[10];
705 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
706
707 /* d2 */
708 tmp[0] = product[12];
709 tmp[1] = product[13];
710 tmp[2] = product[14];
711 tmp[3] = product[15];
712 tmp[4] = tmp[5] = 0;
713 tmp[6] = product[9];
714 tmp[7] = product[11];
715 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
716
717 /* d3 */
718 tmp[0] = product[13];
719 tmp[1] = product[14];
720 tmp[2] = product[15];
721 tmp[3] = product[8];
722 tmp[4] = product[9];
723 tmp[5] = product[10];
724 tmp[6] = 0;
725 tmp[7] = product[12];
726 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
727
728 /* d4 */
729 tmp[0] = product[14];
730 tmp[1] = product[15];
731 tmp[2] = 0;
732 tmp[3] = product[9];
733 tmp[4] = product[10];
734 tmp[5] = product[11];
735 tmp[6] = 0;
736 tmp[7] = product[13];
737 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
738
739 if (carry < 0) {
740 do {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100741 carry += uECC_vli_add(result, result, curve_secp256r1.p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300742 }
743 while (carry < 0);
744 } else {
745 while (carry ||
746 uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
747 carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
748 }
749 }
750}
751
752uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
753{
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100754 (void) curve;
755 return uECC_vli_isZero(point);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300756}
757
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100758void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300759{
760 uECC_word_t t1[NUM_ECC_WORDS];
761
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100762 uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
763 uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
764 uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
765 uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300766}
767
768/* P = (x1, y1) => 2P, (x2, y2) => P' */
769static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
770 uECC_word_t * X2, uECC_word_t * Y2,
771 const uECC_word_t * const initial_Z,
772 uECC_Curve curve)
773{
774 uECC_word_t z[NUM_ECC_WORDS];
775 wordcount_t num_words = curve->num_words;
776 if (initial_Z) {
777 uECC_vli_set(z, initial_Z, num_words);
778 } else {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100779 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300780 z[0] = 1;
781 }
782
783 uECC_vli_set(X2, X1, num_words);
784 uECC_vli_set(Y2, Y1, num_words);
785
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100786 apply_z(X1, Y1, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300787 curve->double_jacobian(X1, Y1, z, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100788 apply_z(X2, Y2, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300789}
790
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100791static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
792 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100793 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300794{
795 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
796 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100797 const uECC_Curve curve = &curve_secp256r1;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100798 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300799
800 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100801 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100802 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
803 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300804 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100805 uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300806
807 uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
808 uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
809 uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100810 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300811 uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100812 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300813 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
814
815 uECC_vli_set(X2, t5, num_words);
816}
817
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100818void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
819 uECC_word_t * X2, uECC_word_t * Y2,
820 uECC_Curve curve)
821{
822 (void) curve;
823 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
824}
825
Jarno Lamsa18987a42019-04-24 15:40:43 +0300826/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
827 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
828 or P => P - Q, Q => P + Q
829 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100830static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
831 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100832 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300833{
834 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
835 uECC_word_t t5[NUM_ECC_WORDS];
836 uECC_word_t t6[NUM_ECC_WORDS];
837 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100838 const uECC_Curve curve = &curve_secp256r1;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100839 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300840
841 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100842 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100843 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
844 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300845 uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
846 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
847
848 uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100849 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300850 uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100851 uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300852 uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
853
854 uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100855 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300856 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
857 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
858
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100859 uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300860 uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
861 uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100862 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300863 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
864 uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
865
866 uECC_vli_set(X1, t7, num_words);
867}
868
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100869static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300870 const uECC_word_t * scalar,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100871 const uECC_word_t * initial_Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300872{
873 /* R0 and R1 */
874 uECC_word_t Rx[2][NUM_ECC_WORDS];
875 uECC_word_t Ry[2][NUM_ECC_WORDS];
876 uECC_word_t z[NUM_ECC_WORDS];
877 bitcount_t i;
878 uECC_word_t nb;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100879 const wordcount_t num_words = NUM_ECC_WORDS;
880 const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100881 const uECC_Curve curve = uECC_secp256r1();
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100882 ecc_wait_state_t wait_state;
883 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300884
885 uECC_vli_set(Rx[1], point, num_words);
886 uECC_vli_set(Ry[1], point + num_words, num_words);
887
888 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
889
890 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100891 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300892 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100893 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
894 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300895 }
896
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100897 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300898 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100899 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300900
901 /* Find final 1/Z value. */
902 uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100903 uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
904 uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300905 uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
906 /* yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100907 uECC_vli_modMult_fast(z, z, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300908 /* Xb * yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100909 uECC_vli_modMult_fast(z, z, Rx[1 - nb]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300910 /* End 1/Z calculation */
911
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100912 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100913 apply_z(Rx[0], Ry[0], z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300914
915 uECC_vli_set(result, Rx[0], num_words);
916 uECC_vli_set(result + num_words, Ry[0], num_words);
917}
918
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100919static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100920 uECC_word_t *k1)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300921{
922
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100923 wordcount_t num_n_words = NUM_ECC_WORDS;
924 bitcount_t num_n_bits = NUM_ECC_BITS;
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100925 const uECC_Curve curve = uECC_secp256r1();
Jarno Lamsa18987a42019-04-24 15:40:43 +0300926
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100927 uECC_word_t carry = uECC_vli_add(k0, k, curve->n) ||
Jarno Lamsa18987a42019-04-24 15:40:43 +0300928 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
929 uECC_vli_testBit(k0, num_n_bits));
930
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100931 uECC_vli_add(k1, k0, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300932
933 return carry;
934}
935
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100936int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
937 const uECC_word_t * scalar, uECC_Curve curve)
938{
939 uECC_word_t tmp[NUM_ECC_WORDS];
940 uECC_word_t s[NUM_ECC_WORDS];
941 uECC_word_t *k2[2] = {tmp, s};
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100942 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100943 uECC_word_t carry;
944 uECC_word_t *initial_Z = 0;
945 int r;
946
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100947 if (curve != uECC_secp256r1())
948 return 0;
949
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100950 /* Regularize the bitcount for the private key so that attackers cannot use a
951 * side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100952 carry = regularize_k(scalar, tmp, s);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100953
954 /* If an RNG function was specified, get a random initial Z value to
955 * protect against side-channel attacks such as Template SPA */
956 if (g_rng_function) {
957 if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) {
958 r = 0;
959 goto clear_and_out;
960 }
961 initial_Z = k2[carry];
962 }
963
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100964 EccPoint_mult(result, point, k2[!carry], initial_Z);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100965 r = 1;
966
967clear_and_out:
968 /* erasing temporary buffer used to store secret: */
969 mbedtls_platform_zeroize(k2, sizeof(k2));
970 mbedtls_platform_zeroize(tmp, sizeof(tmp));
971 mbedtls_platform_zeroize(s, sizeof(s));
972
973 return r;
974}
975
Jarno Lamsa18987a42019-04-24 15:40:43 +0300976uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
977 uECC_word_t *private_key,
978 uECC_Curve curve)
979{
980
981 uECC_word_t tmp1[NUM_ECC_WORDS];
982 uECC_word_t tmp2[NUM_ECC_WORDS];
983 uECC_word_t *p2[2] = {tmp1, tmp2};
984 uECC_word_t carry;
985
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100986 if (curve != uECC_secp256r1())
987 return 0;
988
Jarno Lamsa18987a42019-04-24 15:40:43 +0300989 /* Regularize the bitcount for the private key so that attackers cannot
990 * use a side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100991 carry = regularize_k(private_key, tmp1, tmp2);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300992
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100993 EccPoint_mult(result, curve->G, p2[!carry], 0);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300994
995 if (EccPoint_isZero(result, curve)) {
996 return 0;
997 }
998 return 1;
999}
1000
1001/* Converts an integer in uECC native format to big-endian bytes. */
1002void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
1003 const unsigned int *native)
1004{
1005 wordcount_t i;
1006 for (i = 0; i < num_bytes; ++i) {
1007 unsigned b = num_bytes - 1 - i;
1008 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
1009 }
1010}
1011
1012/* Converts big-endian bytes to an integer in uECC native format. */
1013void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
1014 int num_bytes)
1015{
1016 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +01001017 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001018 for (i = 0; i < num_bytes; ++i) {
1019 unsigned b = num_bytes - 1 - i;
1020 native[b / uECC_WORD_SIZE] |=
1021 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1022 }
1023}
1024
1025int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1026 wordcount_t num_words)
1027{
1028 uECC_word_t mask = (uECC_word_t)-1;
1029 uECC_word_t tries;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +01001030 bitcount_t num_bits = uECC_vli_numBits(top);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001031
1032 if (!g_rng_function) {
1033 return 0;
1034 }
1035
1036 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1037 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1038 return 0;
1039 }
1040 random[num_words - 1] &=
1041 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001042 if (!uECC_vli_isZero(random) &&
Jarno Lamsa18987a42019-04-24 15:40:43 +03001043 uECC_vli_cmp(top, random, num_words) == 1) {
1044 return 1;
1045 }
1046 }
1047 return 0;
1048}
1049
1050
1051int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1052{
1053 uECC_word_t tmp1[NUM_ECC_WORDS];
1054 uECC_word_t tmp2[NUM_ECC_WORDS];
1055 wordcount_t num_words = curve->num_words;
1056
1057 /* The point at infinity is invalid. */
1058 if (EccPoint_isZero(point, curve)) {
1059 return -1;
1060 }
1061
1062 /* x and y must be smaller than p. */
1063 if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
1064 uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
1065 return -2;
1066 }
1067
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +01001068 uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001069 curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
1070
1071 /* Make sure that y^2 == x^3 + ax + b */
1072 if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
1073 return -3;
1074
1075 return 0;
1076}
1077
1078int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1079{
1080
1081 uECC_word_t _public[NUM_ECC_WORDS * 2];
1082
1083 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1084 uECC_vli_bytesToNative(
1085 _public + curve->num_words,
1086 public_key + curve->num_bytes,
1087 curve->num_bytes);
1088
1089 if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
1090 return -4;
1091 }
1092
1093 return uECC_valid_point(_public, curve);
1094}
1095
1096int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1097 uECC_Curve curve)
1098{
1099
1100 uECC_word_t _private[NUM_ECC_WORDS];
1101 uECC_word_t _public[NUM_ECC_WORDS * 2];
1102
1103 uECC_vli_bytesToNative(
1104 _private,
1105 private_key,
1106 BITS_TO_BYTES(curve->num_n_bits));
1107
1108 /* Make sure the private key is in the range [1, n-1]. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001109 if (uECC_vli_isZero(_private)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001110 return 0;
1111 }
1112
1113 if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
1114 return 0;
1115 }
1116
1117 /* Compute public key. */
1118 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1119 return 0;
1120 }
1121
1122 uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1123 uECC_vli_nativeToBytes(
1124 public_key +
1125 curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1126 return 1;
1127}
Jarno Lamsa46132202019-04-29 14:29:52 +03001128#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +02001129typedef int mbedtls_dummy_tinycrypt_def;
1130#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001131