blob: 60b565e3cf268de3bc492c310da74f32f0cbf80c [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
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100155void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300156{
157 wordcount_t i;
158
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100159 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300160 dest[i] = src[i];
161 }
162}
163
164cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
165 const uECC_word_t *right,
166 wordcount_t num_words)
167{
168 wordcount_t i;
169
170 for (i = num_words - 1; i >= 0; --i) {
171 if (left[i] > right[i]) {
172 return 1;
173 } else if (left[i] < right[i]) {
174 return -1;
175 }
176 }
177 return 0;
178}
179
180uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
181 wordcount_t num_words)
182{
183
184 uECC_word_t diff = 0;
185 wordcount_t i;
186
187 for (i = num_words - 1; i >= 0; --i) {
188 diff |= (left[i] ^ right[i]);
189 }
190 return !(diff == 0);
191}
192
193uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
194{
195 return (p_true*(cond)) | (p_false*(!cond));
196}
197
198/* Computes result = left - right, returning borrow, in constant time.
199 * Can modify in place. */
200uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
201 const uECC_word_t *right, wordcount_t num_words)
202{
203 uECC_word_t borrow = 0;
204 wordcount_t i;
205 for (i = 0; i < num_words; ++i) {
206 uECC_word_t diff = left[i] - right[i] - borrow;
207 uECC_word_t val = (diff > left[i]);
208 borrow = cond_set(val, borrow, (diff != left[i]));
209
210 result[i] = diff;
211 }
212 return borrow;
213}
214
215/* Computes result = left + right, returning carry, in constant time.
216 * Can modify in place. */
217static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100218 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300219{
220 uECC_word_t carry = 0;
221 wordcount_t i;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100222 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300223 uECC_word_t sum = left[i] + right[i] + carry;
224 uECC_word_t val = (sum < left[i]);
225 carry = cond_set(val, carry, (sum != left[i]));
226 result[i] = sum;
227 }
228 return carry;
229}
230
231cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
232 wordcount_t num_words)
233{
234 uECC_word_t tmp[NUM_ECC_WORDS];
235 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100236 uECC_word_t equal = uECC_vli_isZero(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300237 return (!equal - 2 * neg);
238}
239
240/* Computes vli = vli >> 1. */
241static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
242{
243 uECC_word_t *end = vli;
244 uECC_word_t carry = 0;
245
246 vli += num_words;
247 while (vli-- > end) {
248 uECC_word_t temp = *vli;
249 *vli = (temp >> 1) | carry;
250 carry = temp << (uECC_WORD_BITS - 1);
251 }
252}
253
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100254/* Compute a * b + r, where r is a double-word with high-order word r1 and
255 * low-order word r0, and store the result in the same double-word (r1, r0),
256 * with the carry bit stored in r2.
257 *
258 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200259 * [in] a, b: operands to be multiplied
260 * [in] r0, r1: low and high-order words of operand to add
261 * [out] r0, r1: low and high-order words of the result
262 * [out] r2: carry
263 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300264static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
265 uECC_word_t *r1, uECC_word_t *r2)
266{
267
268 uECC_dword_t p = (uECC_dword_t)a * b;
269 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
270 r01 += p;
271 *r2 += (r01 < p);
272 *r1 = r01 >> uECC_WORD_BITS;
273 *r0 = (uECC_word_t)r01;
274
275}
276
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200277/* State for implementing random delays in uECC_vli_mult_rnd().
278 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100279 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200280 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
281 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100282 * Randomized vli multiplication is used only for point operations
283 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
284 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
285 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100286 * indirectly through uECC_vli_modMult_rnd().
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100287 *
288 * Considering this, in order to minimize the number of calls to the RNG
289 * (which impact performance) while keeping the size of the structure low,
290 * make room for 14 randomized vli mults, which corresponds to one step in the
291 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200292 */
293typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100294 uint8_t i;
295 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100296} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200297
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100298/*
299 * Reset wait_state so that it's ready to be used.
300 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100301void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100302{
303 if (ws == NULL)
304 return;
305
306 ws->i = 0;
307 g_rng_function(ws->delays, sizeof(ws->delays));
308}
309
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200310/* Computes result = left * right. Result must be 2 * num_words long.
311 *
312 * As a counter-measure against horizontal attacks, add noise by performing
313 * a random number of extra computations performing random additional accesses
314 * to limbs of the input.
315 *
316 * Each of the two actual computation loops is surrounded by two
317 * similar-looking waiting loops, to make the beginning and end of the actual
318 * computation harder to spot.
319 *
320 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
321 * makes an average of 6 extra calls. Compared to the main computation which
322 * makes 64 such calls, this represents an average performance degradation of
323 * less than 10%.
324 *
325 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
326 * know it's always 8. This saves a bit of code size and execution speed.
327 */
328static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100329 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300330{
331
332 uECC_word_t r0 = 0;
333 uECC_word_t r1 = 0;
334 uECC_word_t r2 = 0;
335 wordcount_t i, k;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100336 const uint8_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200337
338 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
339 uint8_t delays = s ? s->delays[s->i++] : 0;
340 uECC_word_t rr0 = 0, rr1 = 0;
341 volatile uECC_word_t r;
342
343 /* Mimic start of next loop: k in [0, 3] */
344 k = 0 + (delays & 0x03);
345 delays >>= 2;
346 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
347 * k = 3 -> i in [1, 3] -> 3 extra muladd */
348 for (i = 0; i <= k; ++i) {
349 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
350 }
351 r = rr0;
352 rr0 = rr1;
353 rr1 = r2;
354 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300355
356 /* Compute each digit of result in sequence, maintaining the carries. */
357 for (k = 0; k < num_words; ++k) {
358
359 for (i = 0; i <= k; ++i) {
360 muladd(left[i], right[k - i], &r0, &r1, &r2);
361 }
362
363 result[k] = r0;
364 r0 = r1;
365 r1 = r2;
366 r2 = 0;
367 }
368
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200369 /* Mimic end of previous loop: k in [4, 7] */
370 k = 4 + (delays & 0x03);
371 delays >>= 2;
372 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
373 * k = 7 -> i in [5, 7] -> 3 extra muladd */
374 for (i = 5; i <= k; ++i) {
375 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
376 }
377 r = rr0;
378 rr0 = rr1;
379 rr1 = r2;
380 r2 = 0;
381
382 /* Mimic start of next loop: k in [8, 11] */
383 k = 11 - (delays & 0x03);
384 delays >>= 2;
385 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
386 * k = 11 -> i in [8, 7] -> 0 extra muladd */
387 for (i = (k + 5) - num_words; i < num_words; ++i) {
388 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
389 }
390 r = rr0;
391 rr0 = rr1;
392 rr1 = r2;
393 r2 = 0;
394
Jarno Lamsa18987a42019-04-24 15:40:43 +0300395 for (k = num_words; k < num_words * 2 - 1; ++k) {
396
397 for (i = (k + 1) - num_words; i < num_words; ++i) {
398 muladd(left[i], right[k - i], &r0, &r1, &r2);
399 }
400 result[k] = r0;
401 r0 = r1;
402 r1 = r2;
403 r2 = 0;
404 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200405
Jarno Lamsa18987a42019-04-24 15:40:43 +0300406 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200407
408 /* Mimic end of previous loop: k in [12, 15] */
409 k = 15 - (delays & 0x03);
410 delays >>= 2;
411 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
412 * k = 15 -> i in [8, 7] -> 0 extra muladd */
413 for (i = (k + 1) - num_words; i < num_words; ++i) {
414 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
415 }
416 r = rr0;
417 rr0 = rr1;
418 rr1 = r2;
419 r2 = 0;
420
421 /* avoid warning that r is set but not used */
422 (void) r;
423}
424
Jarno Lamsa18987a42019-04-24 15:40:43 +0300425void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
426 const uECC_word_t *right, const uECC_word_t *mod,
427 wordcount_t num_words)
428{
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100429 uECC_word_t carry = uECC_vli_add(result, left, right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300430 if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
431 /* result > mod (result = mod + remainder), so subtract mod to get
432 * remainder. */
433 uECC_vli_sub(result, result, mod, num_words);
434 }
435}
436
437void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
438 const uECC_word_t *right, const uECC_word_t *mod,
439 wordcount_t num_words)
440{
441 uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
442 if (l_borrow) {
443 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
444 * we can get the correct result from result + mod (with overflow). */
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100445 uECC_vli_add(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300446 }
447}
448
449/* Computes result = product % mod, where product is 2N words long. */
450/* Currently only designed to work for curve_p or curve_n. */
451void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
452 const uECC_word_t *mod, wordcount_t num_words)
453{
454 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
455 uECC_word_t tmp[2 * NUM_ECC_WORDS];
456 uECC_word_t *v[2] = {tmp, product};
457 uECC_word_t index;
458
459 /* Shift mod so its highest set bit is at the maximum position. */
460 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100461 uECC_vli_numBits(mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300462 wordcount_t word_shift = shift / uECC_WORD_BITS;
463 wordcount_t bit_shift = shift % uECC_WORD_BITS;
464 uECC_word_t carry = 0;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100465 uECC_vli_clear(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300466 if (bit_shift > 0) {
467 for(index = 0; index < (uECC_word_t)num_words; ++index) {
468 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
469 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
470 }
471 } else {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100472 uECC_vli_set(mod_multiple + word_shift, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300473 }
474
475 for (index = 1; shift >= 0; --shift) {
476 uECC_word_t borrow = 0;
477 wordcount_t i;
478 for (i = 0; i < num_words * 2; ++i) {
479 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
480 if (diff != v[index][i]) {
481 borrow = (diff > v[index][i]);
482 }
483 v[1 - index][i] = diff;
484 }
485 /* Swap the index if there was no borrow */
486 index = !(index ^ borrow);
487 uECC_vli_rshift1(mod_multiple, num_words);
488 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
489 (uECC_WORD_BITS - 1);
490 uECC_vli_rshift1(mod_multiple + num_words, num_words);
491 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100492 uECC_vli_set(result, v[index]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300493}
494
495void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
496 const uECC_word_t *right, const uECC_word_t *mod,
497 wordcount_t num_words)
498{
499 uECC_word_t product[2 * NUM_ECC_WORDS];
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100500 uECC_vli_mult_rnd(product, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300501 uECC_vli_mmod(result, product, mod, num_words);
502}
503
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100504static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100505 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100506{
507 uECC_word_t product[2 * NUM_ECC_WORDS];
508 uECC_vli_mult_rnd(product, left, right, s);
509
510 vli_mmod_fast_secp256r1(result, product);
511}
512
Jarno Lamsa18987a42019-04-24 15:40:43 +0300513void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100514 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300515{
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100516 uECC_vli_modMult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300517}
518
Jarno Lamsa18987a42019-04-24 15:40:43 +0300519#define EVEN(vli) (!(vli[0] & 1))
520
521static void vli_modInv_update(uECC_word_t *uv,
522 const uECC_word_t *mod,
523 wordcount_t num_words)
524{
525
526 uECC_word_t carry = 0;
527
528 if (!EVEN(uv)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100529 carry = uECC_vli_add(uv, uv, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300530 }
531 uECC_vli_rshift1(uv, num_words);
532 if (carry) {
533 uv[num_words - 1] |= HIGH_BIT_SET;
534 }
535}
536
537void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
538 const uECC_word_t *mod, wordcount_t num_words)
539{
540 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
541 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
542 cmpresult_t cmpResult;
543
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100544 if (uECC_vli_isZero(input)) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100545 uECC_vli_clear(result);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300546 return;
547 }
548
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100549 uECC_vli_set(a, input);
550 uECC_vli_set(b, mod);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100551 uECC_vli_clear(u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300552 u[0] = 1;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100553 uECC_vli_clear(v);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300554 while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
555 if (EVEN(a)) {
556 uECC_vli_rshift1(a, num_words);
557 vli_modInv_update(u, mod, num_words);
558 } else if (EVEN(b)) {
559 uECC_vli_rshift1(b, num_words);
560 vli_modInv_update(v, mod, num_words);
561 } else if (cmpResult > 0) {
562 uECC_vli_sub(a, a, b, num_words);
563 uECC_vli_rshift1(a, num_words);
564 if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100565 uECC_vli_add(u, u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300566 }
567 uECC_vli_sub(u, u, v, num_words);
568 vli_modInv_update(u, mod, num_words);
569 } else {
570 uECC_vli_sub(b, b, a, num_words);
571 uECC_vli_rshift1(b, num_words);
572 if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100573 uECC_vli_add(v, v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300574 }
575 uECC_vli_sub(v, v, u, num_words);
576 vli_modInv_update(v, mod, num_words);
577 }
578 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100579 uECC_vli_set(result, u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300580}
581
582/* ------ Point operations ------ */
583
584void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
585 uECC_word_t * Z1, uECC_Curve curve)
586{
587 /* t1 = X, t2 = Y, t3 = Z */
588 uECC_word_t t4[NUM_ECC_WORDS];
589 uECC_word_t t5[NUM_ECC_WORDS];
590 wordcount_t num_words = curve->num_words;
591
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100592 if (uECC_vli_isZero(Z1)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300593 return;
594 }
595
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100596 uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */
597 uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
598 uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */
599 uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
600 uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300601
602 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
603 uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
604 uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100605 uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300606
607 uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
608 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
609 if (uECC_vli_testBit(X1, 0)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100610 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300611 uECC_vli_rshift1(X1, num_words);
612 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
613 } else {
614 uECC_vli_rshift1(X1, num_words);
615 }
616
617 /* t1 = 3/2*(x1^2 - z1^4) = B */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100618 uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300619 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
620 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
621 uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100622 uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300623 /* t4 = B * (A - x3) - y1^4 = y3: */
624 uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
625
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100626 uECC_vli_set(X1, Z1);
627 uECC_vli_set(Z1, Y1);
628 uECC_vli_set(Y1, t4);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300629}
630
631void x_side_default(uECC_word_t *result,
632 const uECC_word_t *x,
633 uECC_Curve curve)
634{
635 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
636 wordcount_t num_words = curve->num_words;
637
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100638 uECC_vli_modMult_fast(result, x, x); /* r = x^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300639 uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100640 uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300641 /* r = x^3 - 3x + b: */
642 uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
643}
644
645uECC_Curve uECC_secp256r1(void)
646{
647 return &curve_secp256r1;
648}
649
650void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
651{
652 unsigned int tmp[NUM_ECC_WORDS];
653 int carry;
654
655 /* t */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100656 uECC_vli_set(result, product);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300657
658 /* s1 */
659 tmp[0] = tmp[1] = tmp[2] = 0;
660 tmp[3] = product[11];
661 tmp[4] = product[12];
662 tmp[5] = product[13];
663 tmp[6] = product[14];
664 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100665 carry = uECC_vli_add(tmp, tmp, tmp);
666 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300667
668 /* s2 */
669 tmp[3] = product[12];
670 tmp[4] = product[13];
671 tmp[5] = product[14];
672 tmp[6] = product[15];
673 tmp[7] = 0;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100674 carry += uECC_vli_add(tmp, tmp, tmp);
675 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300676
677 /* s3 */
678 tmp[0] = product[8];
679 tmp[1] = product[9];
680 tmp[2] = product[10];
681 tmp[3] = tmp[4] = tmp[5] = 0;
682 tmp[6] = product[14];
683 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100684 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300685
686 /* s4 */
687 tmp[0] = product[9];
688 tmp[1] = product[10];
689 tmp[2] = product[11];
690 tmp[3] = product[13];
691 tmp[4] = product[14];
692 tmp[5] = product[15];
693 tmp[6] = product[13];
694 tmp[7] = product[8];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100695 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300696
697 /* d1 */
698 tmp[0] = product[11];
699 tmp[1] = product[12];
700 tmp[2] = product[13];
701 tmp[3] = tmp[4] = tmp[5] = 0;
702 tmp[6] = product[8];
703 tmp[7] = product[10];
704 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
705
706 /* d2 */
707 tmp[0] = product[12];
708 tmp[1] = product[13];
709 tmp[2] = product[14];
710 tmp[3] = product[15];
711 tmp[4] = tmp[5] = 0;
712 tmp[6] = product[9];
713 tmp[7] = product[11];
714 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
715
716 /* d3 */
717 tmp[0] = product[13];
718 tmp[1] = product[14];
719 tmp[2] = product[15];
720 tmp[3] = product[8];
721 tmp[4] = product[9];
722 tmp[5] = product[10];
723 tmp[6] = 0;
724 tmp[7] = product[12];
725 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
726
727 /* d4 */
728 tmp[0] = product[14];
729 tmp[1] = product[15];
730 tmp[2] = 0;
731 tmp[3] = product[9];
732 tmp[4] = product[10];
733 tmp[5] = product[11];
734 tmp[6] = 0;
735 tmp[7] = product[13];
736 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
737
738 if (carry < 0) {
739 do {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100740 carry += uECC_vli_add(result, result, curve_secp256r1.p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300741 }
742 while (carry < 0);
743 } else {
744 while (carry ||
745 uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
746 carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
747 }
748 }
749}
750
751uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
752{
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100753 (void) curve;
754 return uECC_vli_isZero(point);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300755}
756
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100757void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300758{
759 uECC_word_t t1[NUM_ECC_WORDS];
760
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100761 uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
762 uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
763 uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
764 uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300765}
766
767/* P = (x1, y1) => 2P, (x2, y2) => P' */
768static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
769 uECC_word_t * X2, uECC_word_t * Y2,
770 const uECC_word_t * const initial_Z,
771 uECC_Curve curve)
772{
773 uECC_word_t z[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300774 if (initial_Z) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100775 uECC_vli_set(z, initial_Z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300776 } else {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100777 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300778 z[0] = 1;
779 }
780
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100781 uECC_vli_set(X2, X1);
782 uECC_vli_set(Y2, Y1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300783
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100784 apply_z(X1, Y1, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300785 curve->double_jacobian(X1, Y1, z, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100786 apply_z(X2, Y2, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300787}
788
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100789static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
790 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100791 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300792{
793 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
794 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100795 const uECC_Curve curve = &curve_secp256r1;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100796 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300797
798 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100799 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100800 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
801 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300802 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100803 uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300804
805 uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
806 uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
807 uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100808 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300809 uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100810 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300811 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
812
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100813 uECC_vli_set(X2, t5);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300814}
815
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100816void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
817 uECC_word_t * X2, uECC_word_t * Y2,
818 uECC_Curve curve)
819{
820 (void) curve;
821 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
822}
823
Jarno Lamsa18987a42019-04-24 15:40:43 +0300824/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
825 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
826 or P => P - Q, Q => P + Q
827 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100828static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
829 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100830 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300831{
832 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
833 uECC_word_t t5[NUM_ECC_WORDS];
834 uECC_word_t t6[NUM_ECC_WORDS];
835 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100836 const uECC_Curve curve = &curve_secp256r1;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100837 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300838
839 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100840 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100841 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
842 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300843 uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
844 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
845
846 uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100847 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300848 uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100849 uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300850 uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
851
852 uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100853 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300854 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
855 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
856
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100857 uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300858 uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
859 uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100860 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300861 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
862 uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
863
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100864 uECC_vli_set(X1, t7);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300865}
866
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100867static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300868 const uECC_word_t * scalar,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100869 const uECC_word_t * initial_Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300870{
871 /* R0 and R1 */
872 uECC_word_t Rx[2][NUM_ECC_WORDS];
873 uECC_word_t Ry[2][NUM_ECC_WORDS];
874 uECC_word_t z[NUM_ECC_WORDS];
875 bitcount_t i;
876 uECC_word_t nb;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100877 const wordcount_t num_words = NUM_ECC_WORDS;
878 const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100879 const uECC_Curve curve = uECC_secp256r1();
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100880 ecc_wait_state_t wait_state;
881 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300882
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100883 uECC_vli_set(Rx[1], point);
884 uECC_vli_set(Ry[1], point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300885
886 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
887
888 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100889 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300890 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100891 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
892 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300893 }
894
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100895 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300896 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100897 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300898
899 /* Find final 1/Z value. */
900 uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100901 uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
902 uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300903 uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
904 /* yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100905 uECC_vli_modMult_fast(z, z, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300906 /* Xb * yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100907 uECC_vli_modMult_fast(z, z, Rx[1 - nb]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300908 /* End 1/Z calculation */
909
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100910 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100911 apply_z(Rx[0], Ry[0], z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300912
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100913 uECC_vli_set(result, Rx[0]);
914 uECC_vli_set(result + num_words, Ry[0]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300915}
916
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100917static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100918 uECC_word_t *k1)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300919{
920
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100921 wordcount_t num_n_words = NUM_ECC_WORDS;
922 bitcount_t num_n_bits = NUM_ECC_BITS;
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100923 const uECC_Curve curve = uECC_secp256r1();
Jarno Lamsa18987a42019-04-24 15:40:43 +0300924
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100925 uECC_word_t carry = uECC_vli_add(k0, k, curve->n) ||
Jarno Lamsa18987a42019-04-24 15:40:43 +0300926 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
927 uECC_vli_testBit(k0, num_n_bits));
928
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100929 uECC_vli_add(k1, k0, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300930
931 return carry;
932}
933
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100934int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
935 const uECC_word_t * scalar, uECC_Curve curve)
936{
937 uECC_word_t tmp[NUM_ECC_WORDS];
938 uECC_word_t s[NUM_ECC_WORDS];
939 uECC_word_t *k2[2] = {tmp, s};
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100940 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100941 uECC_word_t carry;
942 uECC_word_t *initial_Z = 0;
943 int r;
944
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100945 if (curve != uECC_secp256r1())
946 return 0;
947
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100948 /* Regularize the bitcount for the private key so that attackers cannot use a
949 * side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100950 carry = regularize_k(scalar, tmp, s);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100951
952 /* If an RNG function was specified, get a random initial Z value to
953 * protect against side-channel attacks such as Template SPA */
954 if (g_rng_function) {
955 if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) {
956 r = 0;
957 goto clear_and_out;
958 }
959 initial_Z = k2[carry];
960 }
961
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100962 EccPoint_mult(result, point, k2[!carry], initial_Z);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100963 r = 1;
964
965clear_and_out:
966 /* erasing temporary buffer used to store secret: */
967 mbedtls_platform_zeroize(k2, sizeof(k2));
968 mbedtls_platform_zeroize(tmp, sizeof(tmp));
969 mbedtls_platform_zeroize(s, sizeof(s));
970
971 return r;
972}
973
Jarno Lamsa18987a42019-04-24 15:40:43 +0300974uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
975 uECC_word_t *private_key,
976 uECC_Curve curve)
977{
978
979 uECC_word_t tmp1[NUM_ECC_WORDS];
980 uECC_word_t tmp2[NUM_ECC_WORDS];
981 uECC_word_t *p2[2] = {tmp1, tmp2};
982 uECC_word_t carry;
983
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100984 if (curve != uECC_secp256r1())
985 return 0;
986
Jarno Lamsa18987a42019-04-24 15:40:43 +0300987 /* Regularize the bitcount for the private key so that attackers cannot
988 * use a side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100989 carry = regularize_k(private_key, tmp1, tmp2);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300990
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100991 EccPoint_mult(result, curve->G, p2[!carry], 0);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300992
993 if (EccPoint_isZero(result, curve)) {
994 return 0;
995 }
996 return 1;
997}
998
999/* Converts an integer in uECC native format to big-endian bytes. */
1000void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
1001 const unsigned int *native)
1002{
1003 wordcount_t i;
1004 for (i = 0; i < num_bytes; ++i) {
1005 unsigned b = num_bytes - 1 - i;
1006 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
1007 }
1008}
1009
1010/* Converts big-endian bytes to an integer in uECC native format. */
1011void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
1012 int num_bytes)
1013{
1014 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +01001015 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001016 for (i = 0; i < num_bytes; ++i) {
1017 unsigned b = num_bytes - 1 - i;
1018 native[b / uECC_WORD_SIZE] |=
1019 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1020 }
1021}
1022
1023int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1024 wordcount_t num_words)
1025{
1026 uECC_word_t mask = (uECC_word_t)-1;
1027 uECC_word_t tries;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +01001028 bitcount_t num_bits = uECC_vli_numBits(top);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001029
1030 if (!g_rng_function) {
1031 return 0;
1032 }
1033
1034 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1035 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1036 return 0;
1037 }
1038 random[num_words - 1] &=
1039 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001040 if (!uECC_vli_isZero(random) &&
Jarno Lamsa18987a42019-04-24 15:40:43 +03001041 uECC_vli_cmp(top, random, num_words) == 1) {
1042 return 1;
1043 }
1044 }
1045 return 0;
1046}
1047
1048
1049int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1050{
1051 uECC_word_t tmp1[NUM_ECC_WORDS];
1052 uECC_word_t tmp2[NUM_ECC_WORDS];
1053 wordcount_t num_words = curve->num_words;
1054
1055 /* The point at infinity is invalid. */
1056 if (EccPoint_isZero(point, curve)) {
1057 return -1;
1058 }
1059
1060 /* x and y must be smaller than p. */
1061 if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
1062 uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
1063 return -2;
1064 }
1065
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +01001066 uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001067 curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
1068
1069 /* Make sure that y^2 == x^3 + ax + b */
1070 if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
1071 return -3;
1072
1073 return 0;
1074}
1075
1076int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1077{
1078
1079 uECC_word_t _public[NUM_ECC_WORDS * 2];
1080
1081 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1082 uECC_vli_bytesToNative(
1083 _public + curve->num_words,
1084 public_key + curve->num_bytes,
1085 curve->num_bytes);
1086
1087 if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
1088 return -4;
1089 }
1090
1091 return uECC_valid_point(_public, curve);
1092}
1093
1094int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1095 uECC_Curve curve)
1096{
1097
1098 uECC_word_t _private[NUM_ECC_WORDS];
1099 uECC_word_t _public[NUM_ECC_WORDS * 2];
1100
1101 uECC_vli_bytesToNative(
1102 _private,
1103 private_key,
1104 BITS_TO_BYTES(curve->num_n_bits));
1105
1106 /* Make sure the private key is in the range [1, n-1]. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001107 if (uECC_vli_isZero(_private)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001108 return 0;
1109 }
1110
1111 if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
1112 return 0;
1113 }
1114
1115 /* Compute public key. */
1116 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1117 return 0;
1118 }
1119
1120 uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1121 uECC_vli_nativeToBytes(
1122 public_key +
1123 curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1124 return 1;
1125}
Jarno Lamsa46132202019-04-29 14:29:52 +03001126#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +02001127typedef int mbedtls_dummy_tinycrypt_def;
1128#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001129