blob: 6f96bd939a84ada2e5219568da1e7023d6a14cb1 [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
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +010071/* Parameters for curve NIST P-256 aka secp256r1 */
72const uECC_word_t curve_p[NUM_ECC_WORDS] = {
73 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
74 BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00),
75 BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
76 BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
77};
78
Jarno Lamsa18987a42019-04-24 15:40:43 +030079/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
80 * has access to enough entropy in order to feed the PRNG regularly. */
81#if default_RNG_defined
82static uECC_RNG_Function g_rng_function = &default_CSPRNG;
83#else
84static uECC_RNG_Function g_rng_function = 0;
85#endif
86
87void uECC_set_rng(uECC_RNG_Function rng_function)
88{
89 g_rng_function = rng_function;
90}
91
92uECC_RNG_Function uECC_get_rng(void)
93{
94 return g_rng_function;
95}
96
97int uECC_curve_private_key_size(uECC_Curve curve)
98{
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010099 (void) curve;
100 return BITS_TO_BYTES(NUM_ECC_BITS);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300101}
102
103int uECC_curve_public_key_size(uECC_Curve curve)
104{
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100105 (void) curve;
106 return 2 * NUM_ECC_BYTES;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300107}
108
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100109void uECC_vli_clear(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300110{
111 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100112 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300113 vli[i] = 0;
114 }
115}
116
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100117uECC_word_t uECC_vli_isZero(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300118{
119 uECC_word_t bits = 0;
120 wordcount_t i;
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100121 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300122 bits |= vli[i];
123 }
124 return (bits == 0);
125}
126
127uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
128{
129 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
130 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
131}
132
133/* Counts the number of words in vli. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100134static wordcount_t vli_numDigits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300135{
136
137 wordcount_t i;
138 /* Search from the end until we find a non-zero digit. We do it in reverse
139 * because we expect that most digits will be nonzero. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100140 for (i = NUM_ECC_WORDS - 1; i >= 0 && vli[i] == 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300141 }
142
143 return (i + 1);
144}
145
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100146bitcount_t uECC_vli_numBits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300147{
148
149 uECC_word_t i;
150 uECC_word_t digit;
151
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100152 wordcount_t num_digits = vli_numDigits(vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300153 if (num_digits == 0) {
154 return 0;
155 }
156
157 digit = vli[num_digits - 1];
158 for (i = 0; digit; ++i) {
159 digit >>= 1;
160 }
161
162 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
163}
164
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100165void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300166{
167 wordcount_t i;
168
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100169 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300170 dest[i] = src[i];
171 }
172}
173
174cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100175 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300176{
177 wordcount_t i;
178
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100179 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300180 if (left[i] > right[i]) {
181 return 1;
182 } else if (left[i] < right[i]) {
183 return -1;
184 }
185 }
186 return 0;
187}
188
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100189uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300190{
191
192 uECC_word_t diff = 0;
193 wordcount_t i;
194
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100195 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300196 diff |= (left[i] ^ right[i]);
197 }
Manuel Pégourié-Gonnard2b6312b2019-11-06 10:42:02 +0100198 return diff;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300199}
200
201uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
202{
203 return (p_true*(cond)) | (p_false*(!cond));
204}
205
206/* Computes result = left - right, returning borrow, in constant time.
207 * Can modify in place. */
208uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100209 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300210{
211 uECC_word_t borrow = 0;
212 wordcount_t i;
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100213 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300214 uECC_word_t diff = left[i] - right[i] - borrow;
215 uECC_word_t val = (diff > left[i]);
216 borrow = cond_set(val, borrow, (diff != left[i]));
217
218 result[i] = diff;
219 }
220 return borrow;
221}
222
223/* Computes result = left + right, returning carry, in constant time.
224 * Can modify in place. */
225static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100226 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300227{
228 uECC_word_t carry = 0;
229 wordcount_t i;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100230 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300231 uECC_word_t sum = left[i] + right[i] + carry;
232 uECC_word_t val = (sum < left[i]);
233 carry = cond_set(val, carry, (sum != left[i]));
234 result[i] = sum;
235 }
236 return carry;
237}
238
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +0100239cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300240{
241 uECC_word_t tmp[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100242 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100243 uECC_word_t equal = uECC_vli_isZero(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300244 return (!equal - 2 * neg);
245}
246
247/* Computes vli = vli >> 1. */
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100248static void uECC_vli_rshift1(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300249{
250 uECC_word_t *end = vli;
251 uECC_word_t carry = 0;
252
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100253 vli += NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300254 while (vli-- > end) {
255 uECC_word_t temp = *vli;
256 *vli = (temp >> 1) | carry;
257 carry = temp << (uECC_WORD_BITS - 1);
258 }
259}
260
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100261/* Compute a * b + r, where r is a double-word with high-order word r1 and
262 * low-order word r0, and store the result in the same double-word (r1, r0),
263 * with the carry bit stored in r2.
264 *
265 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200266 * [in] a, b: operands to be multiplied
267 * [in] r0, r1: low and high-order words of operand to add
268 * [out] r0, r1: low and high-order words of the result
269 * [out] r2: carry
270 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300271static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
272 uECC_word_t *r1, uECC_word_t *r2)
273{
274
275 uECC_dword_t p = (uECC_dword_t)a * b;
276 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
277 r01 += p;
278 *r2 += (r01 < p);
279 *r1 = r01 >> uECC_WORD_BITS;
280 *r0 = (uECC_word_t)r01;
281
282}
283
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200284/* State for implementing random delays in uECC_vli_mult_rnd().
285 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100286 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200287 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
288 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100289 * Randomized vli multiplication is used only for point operations
290 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
291 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
292 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100293 * indirectly through uECC_vli_modMult_rnd().
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100294 *
295 * Considering this, in order to minimize the number of calls to the RNG
296 * (which impact performance) while keeping the size of the structure low,
297 * make room for 14 randomized vli mults, which corresponds to one step in the
298 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200299 */
300typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100301 uint8_t i;
302 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100303} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200304
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100305/*
306 * Reset wait_state so that it's ready to be used.
307 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100308void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100309{
310 if (ws == NULL)
311 return;
312
313 ws->i = 0;
314 g_rng_function(ws->delays, sizeof(ws->delays));
315}
316
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200317/* Computes result = left * right. Result must be 2 * num_words long.
318 *
319 * As a counter-measure against horizontal attacks, add noise by performing
320 * a random number of extra computations performing random additional accesses
321 * to limbs of the input.
322 *
323 * Each of the two actual computation loops is surrounded by two
324 * similar-looking waiting loops, to make the beginning and end of the actual
325 * computation harder to spot.
326 *
327 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
328 * makes an average of 6 extra calls. Compared to the main computation which
329 * makes 64 such calls, this represents an average performance degradation of
330 * less than 10%.
331 *
332 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
333 * know it's always 8. This saves a bit of code size and execution speed.
334 */
335static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100336 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300337{
338
339 uECC_word_t r0 = 0;
340 uECC_word_t r1 = 0;
341 uECC_word_t r2 = 0;
342 wordcount_t i, k;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100343 const uint8_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200344
345 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
346 uint8_t delays = s ? s->delays[s->i++] : 0;
347 uECC_word_t rr0 = 0, rr1 = 0;
348 volatile uECC_word_t r;
349
350 /* Mimic start of next loop: k in [0, 3] */
351 k = 0 + (delays & 0x03);
352 delays >>= 2;
353 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
354 * k = 3 -> i in [1, 3] -> 3 extra muladd */
Manuel Pégourié-Gonnardc8814862019-11-05 10:32:37 +0100355 for (i = 1; i <= k; ++i) {
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200356 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
357 }
358 r = rr0;
359 rr0 = rr1;
360 rr1 = r2;
361 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300362
363 /* Compute each digit of result in sequence, maintaining the carries. */
364 for (k = 0; k < num_words; ++k) {
365
366 for (i = 0; i <= k; ++i) {
367 muladd(left[i], right[k - i], &r0, &r1, &r2);
368 }
369
370 result[k] = r0;
371 r0 = r1;
372 r1 = r2;
373 r2 = 0;
374 }
375
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200376 /* Mimic end of previous loop: k in [4, 7] */
377 k = 4 + (delays & 0x03);
378 delays >>= 2;
379 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
380 * k = 7 -> i in [5, 7] -> 3 extra muladd */
381 for (i = 5; i <= k; ++i) {
382 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
383 }
384 r = rr0;
385 rr0 = rr1;
386 rr1 = r2;
387 r2 = 0;
388
389 /* Mimic start of next loop: k in [8, 11] */
390 k = 11 - (delays & 0x03);
391 delays >>= 2;
392 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
393 * k = 11 -> i in [8, 7] -> 0 extra muladd */
394 for (i = (k + 5) - num_words; i < num_words; ++i) {
395 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
396 }
397 r = rr0;
398 rr0 = rr1;
399 rr1 = r2;
400 r2 = 0;
401
Jarno Lamsa18987a42019-04-24 15:40:43 +0300402 for (k = num_words; k < num_words * 2 - 1; ++k) {
403
404 for (i = (k + 1) - num_words; i < num_words; ++i) {
405 muladd(left[i], right[k - i], &r0, &r1, &r2);
406 }
407 result[k] = r0;
408 r0 = r1;
409 r1 = r2;
410 r2 = 0;
411 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200412
Jarno Lamsa18987a42019-04-24 15:40:43 +0300413 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200414
415 /* Mimic end of previous loop: k in [12, 15] */
416 k = 15 - (delays & 0x03);
417 delays >>= 2;
418 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
419 * k = 15 -> i in [8, 7] -> 0 extra muladd */
420 for (i = (k + 1) - num_words; i < num_words; ++i) {
421 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
422 }
423 r = rr0;
424 rr0 = rr1;
425 rr1 = r2;
426 r2 = 0;
427
428 /* avoid warning that r is set but not used */
429 (void) r;
430}
431
Jarno Lamsa18987a42019-04-24 15:40:43 +0300432void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100433 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300434{
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100435 uECC_word_t carry = uECC_vli_add(result, left, right);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100436 if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300437 /* result > mod (result = mod + remainder), so subtract mod to get
438 * remainder. */
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100439 uECC_vli_sub(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300440 }
441}
442
443void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100444 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300445{
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100446 uECC_word_t l_borrow = uECC_vli_sub(result, left, right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300447 if (l_borrow) {
448 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
449 * we can get the correct result from result + mod (with overflow). */
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100450 uECC_vli_add(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300451 }
452}
453
454/* Computes result = product % mod, where product is 2N words long. */
455/* Currently only designed to work for curve_p or curve_n. */
456void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100457 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300458{
459 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
460 uECC_word_t tmp[2 * NUM_ECC_WORDS];
461 uECC_word_t *v[2] = {tmp, product};
462 uECC_word_t index;
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100463 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300464
465 /* Shift mod so its highest set bit is at the maximum position. */
466 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100467 uECC_vli_numBits(mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300468 wordcount_t word_shift = shift / uECC_WORD_BITS;
469 wordcount_t bit_shift = shift % uECC_WORD_BITS;
470 uECC_word_t carry = 0;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100471 uECC_vli_clear(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300472 if (bit_shift > 0) {
473 for(index = 0; index < (uECC_word_t)num_words; ++index) {
474 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
475 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
476 }
477 } else {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100478 uECC_vli_set(mod_multiple + word_shift, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300479 }
480
481 for (index = 1; shift >= 0; --shift) {
482 uECC_word_t borrow = 0;
483 wordcount_t i;
484 for (i = 0; i < num_words * 2; ++i) {
485 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
486 if (diff != v[index][i]) {
487 borrow = (diff > v[index][i]);
488 }
489 v[1 - index][i] = diff;
490 }
491 /* Swap the index if there was no borrow */
492 index = !(index ^ borrow);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100493 uECC_vli_rshift1(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300494 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
495 (uECC_WORD_BITS - 1);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100496 uECC_vli_rshift1(mod_multiple + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300497 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100498 uECC_vli_set(result, v[index]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300499}
500
501void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100502 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300503{
504 uECC_word_t product[2 * NUM_ECC_WORDS];
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100505 uECC_vli_mult_rnd(product, left, right, NULL);
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100506 uECC_vli_mmod(result, product, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300507}
508
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100509static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100510 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100511{
512 uECC_word_t product[2 * NUM_ECC_WORDS];
513 uECC_vli_mult_rnd(product, left, right, s);
514
515 vli_mmod_fast_secp256r1(result, product);
516}
517
Jarno Lamsa18987a42019-04-24 15:40:43 +0300518void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100519 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300520{
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100521 uECC_vli_modMult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300522}
523
Jarno Lamsa18987a42019-04-24 15:40:43 +0300524#define EVEN(vli) (!(vli[0] & 1))
525
526static void vli_modInv_update(uECC_word_t *uv,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100527 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300528{
529
530 uECC_word_t carry = 0;
531
532 if (!EVEN(uv)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100533 carry = uECC_vli_add(uv, uv, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300534 }
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100535 uECC_vli_rshift1(uv);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300536 if (carry) {
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100537 uv[NUM_ECC_WORDS - 1] |= HIGH_BIT_SET;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300538 }
539}
540
541void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100542 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300543{
544 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
545 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
546 cmpresult_t cmpResult;
547
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100548 if (uECC_vli_isZero(input)) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100549 uECC_vli_clear(result);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300550 return;
551 }
552
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100553 uECC_vli_set(a, input);
554 uECC_vli_set(b, mod);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100555 uECC_vli_clear(u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300556 u[0] = 1;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100557 uECC_vli_clear(v);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100558 while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300559 if (EVEN(a)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100560 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100561 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300562 } else if (EVEN(b)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100563 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100564 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300565 } else if (cmpResult > 0) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100566 uECC_vli_sub(a, a, b);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100567 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100568 if (uECC_vli_cmp_unsafe(u, v) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100569 uECC_vli_add(u, u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300570 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100571 uECC_vli_sub(u, u, v);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100572 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300573 } else {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100574 uECC_vli_sub(b, b, a);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100575 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100576 if (uECC_vli_cmp_unsafe(v, u) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100577 uECC_vli_add(v, v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300578 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100579 uECC_vli_sub(v, v, u);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100580 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300581 }
582 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100583 uECC_vli_set(result, u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300584}
585
586/* ------ Point operations ------ */
587
588void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
589 uECC_word_t * Z1, uECC_Curve curve)
590{
591 /* t1 = X, t2 = Y, t3 = Z */
592 uECC_word_t t4[NUM_ECC_WORDS];
593 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +0100594 wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300595
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100596 (void) curve;
597
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100598 if (uECC_vli_isZero(Z1)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300599 return;
600 }
601
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100602 uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */
603 uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
604 uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */
605 uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
606 uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300607
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100608 uECC_vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
609 uECC_vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
610 uECC_vli_modSub(Z1, X1, Z1, curve_p); /* t3 = x1 - z1^2 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100611 uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300612
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100613 uECC_vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
614 uECC_vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300615 if (uECC_vli_testBit(X1, 0)) {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100616 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve_p);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100617 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300618 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
619 } else {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100620 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300621 }
622
623 /* t1 = 3/2*(x1^2 - z1^4) = B */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100624 uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100625 uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */
626 uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */
627 uECC_vli_modSub(t5, t5, Z1, curve_p); /* t5 = A - x3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100628 uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300629 /* t4 = B * (A - x3) - y1^4 = y3: */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100630 uECC_vli_modSub(t4, X1, t4, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300631
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100632 uECC_vli_set(X1, Z1);
633 uECC_vli_set(Z1, Y1);
634 uECC_vli_set(Y1, t4);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300635}
636
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +0100637/*
638 * @brief Computes x^3 + ax + b. result must not overlap x.
639 * @param result OUT -- x^3 + ax + b
640 * @param x IN -- value of x
641 * @param curve IN -- elliptic curve
642 */
643static void x_side_default(uECC_word_t *result,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300644 const uECC_word_t *x,
645 uECC_Curve curve)
646{
647 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300648
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100649 uECC_vli_modMult_fast(result, x, x); /* r = x^2 */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100650 uECC_vli_modSub(result, result, _3, curve_p); /* r = x^2 - 3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100651 uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300652 /* r = x^3 - 3x + b: */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100653 uECC_vli_modAdd(result, result, curve->b, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300654}
655
656uECC_Curve uECC_secp256r1(void)
657{
658 return &curve_secp256r1;
659}
660
661void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
662{
663 unsigned int tmp[NUM_ECC_WORDS];
664 int carry;
665
666 /* t */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100667 uECC_vli_set(result, product);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300668
669 /* s1 */
670 tmp[0] = tmp[1] = tmp[2] = 0;
671 tmp[3] = product[11];
672 tmp[4] = product[12];
673 tmp[5] = product[13];
674 tmp[6] = product[14];
675 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100676 carry = uECC_vli_add(tmp, tmp, tmp);
677 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300678
679 /* s2 */
680 tmp[3] = product[12];
681 tmp[4] = product[13];
682 tmp[5] = product[14];
683 tmp[6] = product[15];
684 tmp[7] = 0;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100685 carry += uECC_vli_add(tmp, tmp, tmp);
686 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300687
688 /* s3 */
689 tmp[0] = product[8];
690 tmp[1] = product[9];
691 tmp[2] = product[10];
692 tmp[3] = tmp[4] = tmp[5] = 0;
693 tmp[6] = product[14];
694 tmp[7] = product[15];
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 /* s4 */
698 tmp[0] = product[9];
699 tmp[1] = product[10];
700 tmp[2] = product[11];
701 tmp[3] = product[13];
702 tmp[4] = product[14];
703 tmp[5] = product[15];
704 tmp[6] = product[13];
705 tmp[7] = product[8];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100706 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300707
708 /* d1 */
709 tmp[0] = product[11];
710 tmp[1] = product[12];
711 tmp[2] = product[13];
712 tmp[3] = tmp[4] = tmp[5] = 0;
713 tmp[6] = product[8];
714 tmp[7] = product[10];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100715 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300716
717 /* d2 */
718 tmp[0] = product[12];
719 tmp[1] = product[13];
720 tmp[2] = product[14];
721 tmp[3] = product[15];
722 tmp[4] = tmp[5] = 0;
723 tmp[6] = product[9];
724 tmp[7] = product[11];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100725 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300726
727 /* d3 */
728 tmp[0] = product[13];
729 tmp[1] = product[14];
730 tmp[2] = product[15];
731 tmp[3] = product[8];
732 tmp[4] = product[9];
733 tmp[5] = product[10];
734 tmp[6] = 0;
735 tmp[7] = product[12];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100736 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300737
738 /* d4 */
739 tmp[0] = product[14];
740 tmp[1] = product[15];
741 tmp[2] = 0;
742 tmp[3] = product[9];
743 tmp[4] = product[10];
744 tmp[5] = product[11];
745 tmp[6] = 0;
746 tmp[7] = product[13];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100747 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300748
749 if (carry < 0) {
750 do {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100751 carry += uECC_vli_add(result, result, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300752 }
753 while (carry < 0);
754 } else {
755 while (carry ||
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100756 uECC_vli_cmp_unsafe(curve_p, result) != 1) {
757 carry -= uECC_vli_sub(result, result, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300758 }
759 }
760}
761
762uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
763{
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100764 (void) curve;
765 return uECC_vli_isZero(point);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300766}
767
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100768void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300769{
770 uECC_word_t t1[NUM_ECC_WORDS];
771
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100772 uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
773 uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
774 uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
775 uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300776}
777
778/* P = (x1, y1) => 2P, (x2, y2) => P' */
779static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
780 uECC_word_t * X2, uECC_word_t * Y2,
781 const uECC_word_t * const initial_Z,
782 uECC_Curve curve)
783{
784 uECC_word_t z[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300785 if (initial_Z) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100786 uECC_vli_set(z, initial_Z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300787 } else {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100788 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300789 z[0] = 1;
790 }
791
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100792 uECC_vli_set(X2, X1);
793 uECC_vli_set(Y2, Y1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300794
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100795 apply_z(X1, Y1, z);
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +0100796 double_jacobian_default(X1, Y1, z, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100797 apply_z(X2, Y2, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300798}
799
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100800static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
801 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100802 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300803{
804 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
805 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100806 const uECC_Curve curve = &curve_secp256r1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300807
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100808 (void) curve;
809
810 uECC_vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100811 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100812 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
813 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100814 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100815 uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300816
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100817 uECC_vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */
818 uECC_vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */
819 uECC_vli_modSub(X2, X2, X1, curve_p); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100820 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100821 uECC_vli_modSub(X2, X1, t5, curve_p); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100822 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100823 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300824
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100825 uECC_vli_set(X2, t5);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300826}
827
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100828void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
829 uECC_word_t * X2, uECC_word_t * Y2,
830 uECC_Curve curve)
831{
832 (void) curve;
833 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
834}
835
Jarno Lamsa18987a42019-04-24 15:40:43 +0300836/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
837 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
838 or P => P - Q, Q => P + Q
839 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100840static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
841 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100842 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300843{
844 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
845 uECC_word_t t5[NUM_ECC_WORDS];
846 uECC_word_t t6[NUM_ECC_WORDS];
847 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100848 const uECC_Curve curve = &curve_secp256r1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300849
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100850 (void) curve;
851
852 uECC_vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100853 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100854 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
855 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100856 uECC_vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
857 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300858
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100859 uECC_vli_modSub(t6, X2, X1, curve_p); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100860 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100861 uECC_vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100862 uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100863 uECC_vli_modSub(X2, X2, t6, curve_p); /* t3 = D - (B + C) = x3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300864
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100865 uECC_vli_modSub(t7, X1, X2, curve_p); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100866 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300867 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100868 uECC_vli_modSub(Y2, Y2, Y1, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300869
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100870 uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100871 uECC_vli_modSub(t7, t7, t6, curve_p); /* t7 = F - (B + C) = x3' */
872 uECC_vli_modSub(t6, t7, X1, curve_p); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100873 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300874 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100875 uECC_vli_modSub(Y1, t6, Y1, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300876
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100877 uECC_vli_set(X1, t7);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300878}
879
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100880static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300881 const uECC_word_t * scalar,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100882 const uECC_word_t * initial_Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300883{
884 /* R0 and R1 */
885 uECC_word_t Rx[2][NUM_ECC_WORDS];
886 uECC_word_t Ry[2][NUM_ECC_WORDS];
887 uECC_word_t z[NUM_ECC_WORDS];
888 bitcount_t i;
889 uECC_word_t nb;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100890 const wordcount_t num_words = NUM_ECC_WORDS;
891 const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100892 const uECC_Curve curve = uECC_secp256r1();
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100893 ecc_wait_state_t wait_state;
894 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300895
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100896 uECC_vli_set(Rx[1], point);
897 uECC_vli_set(Ry[1], point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300898
899 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
900
901 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100902 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300903 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100904 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
905 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300906 }
907
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100908 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300909 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100910 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300911
912 /* Find final 1/Z value. */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100913 uECC_vli_modSub(z, Rx[1], Rx[0], curve_p); /* X1 - X0 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100914 uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
915 uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100916 uECC_vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0))*/
Jarno Lamsa18987a42019-04-24 15:40:43 +0300917 /* yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100918 uECC_vli_modMult_fast(z, z, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300919 /* Xb * yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100920 uECC_vli_modMult_fast(z, z, Rx[1 - nb]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300921 /* End 1/Z calculation */
922
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100923 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100924 apply_z(Rx[0], Ry[0], z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300925
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100926 uECC_vli_set(result, Rx[0]);
927 uECC_vli_set(result + num_words, Ry[0]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300928}
929
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100930static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100931 uECC_word_t *k1)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300932{
933
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100934 wordcount_t num_n_words = NUM_ECC_WORDS;
935 bitcount_t num_n_bits = NUM_ECC_BITS;
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100936 const uECC_Curve curve = uECC_secp256r1();
Jarno Lamsa18987a42019-04-24 15:40:43 +0300937
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100938 uECC_word_t carry = uECC_vli_add(k0, k, curve->n) ||
Jarno Lamsa18987a42019-04-24 15:40:43 +0300939 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
940 uECC_vli_testBit(k0, num_n_bits));
941
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100942 uECC_vli_add(k1, k0, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300943
944 return carry;
945}
946
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100947int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
948 const uECC_word_t * scalar, uECC_Curve curve)
949{
950 uECC_word_t tmp[NUM_ECC_WORDS];
951 uECC_word_t s[NUM_ECC_WORDS];
952 uECC_word_t *k2[2] = {tmp, s};
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100953 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100954 uECC_word_t carry;
955 uECC_word_t *initial_Z = 0;
956 int r;
957
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100958 if (curve != uECC_secp256r1())
959 return 0;
960
Manuel Pégourié-Gonnarde7143322019-11-15 10:47:45 +0100961 /* Protects against invalid curves attacks */
962 if (uECC_valid_point(point, curve) != 0 ) {
963 return 0;
964 }
965
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100966 /* Regularize the bitcount for the private key so that attackers cannot use a
967 * side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100968 carry = regularize_k(scalar, tmp, s);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100969
970 /* If an RNG function was specified, get a random initial Z value to
971 * protect against side-channel attacks such as Template SPA */
972 if (g_rng_function) {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100973 if (!uECC_generate_random_int(k2[carry], curve_p, num_words)) {
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100974 r = 0;
975 goto clear_and_out;
976 }
977 initial_Z = k2[carry];
978 }
979
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100980 EccPoint_mult(result, point, k2[!carry], initial_Z);
Manuel Pégourié-Gonnard41ab8cb2019-11-14 11:59:09 +0100981
Manuel Pégourié-Gonnarde7143322019-11-15 10:47:45 +0100982 /* Protect against fault injections that would make the resulting
983 * point not lie on the intended curve */
984 if (uECC_valid_point(result, curve) != 0 ) {
Manuel Pégourié-Gonnard41ab8cb2019-11-14 11:59:09 +0100985 r = 0;
986 goto clear_and_out;
987 }
988
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100989 r = 1;
990
991clear_and_out:
992 /* erasing temporary buffer used to store secret: */
993 mbedtls_platform_zeroize(k2, sizeof(k2));
994 mbedtls_platform_zeroize(tmp, sizeof(tmp));
995 mbedtls_platform_zeroize(s, sizeof(s));
996
997 return r;
998}
999
Jarno Lamsa18987a42019-04-24 15:40:43 +03001000uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
1001 uECC_word_t *private_key,
1002 uECC_Curve curve)
1003{
Manuel Pégourié-Gonnard41ab8cb2019-11-14 11:59:09 +01001004 return EccPoint_mult_safer(result, curve->G, private_key, curve);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001005}
1006
1007/* Converts an integer in uECC native format to big-endian bytes. */
1008void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
1009 const unsigned int *native)
1010{
1011 wordcount_t i;
1012 for (i = 0; i < num_bytes; ++i) {
1013 unsigned b = num_bytes - 1 - i;
1014 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
1015 }
1016}
1017
1018/* Converts big-endian bytes to an integer in uECC native format. */
1019void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
1020 int num_bytes)
1021{
1022 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +01001023 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001024 for (i = 0; i < num_bytes; ++i) {
1025 unsigned b = num_bytes - 1 - i;
1026 native[b / uECC_WORD_SIZE] |=
1027 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1028 }
1029}
1030
1031int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1032 wordcount_t num_words)
1033{
1034 uECC_word_t mask = (uECC_word_t)-1;
1035 uECC_word_t tries;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +01001036 bitcount_t num_bits = uECC_vli_numBits(top);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001037
1038 if (!g_rng_function) {
1039 return 0;
1040 }
1041
1042 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1043 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1044 return 0;
1045 }
1046 random[num_words - 1] &=
1047 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001048 if (!uECC_vli_isZero(random) &&
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +01001049 uECC_vli_cmp(top, random) == 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001050 return 1;
1051 }
1052 }
1053 return 0;
1054}
1055
1056
1057int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1058{
1059 uECC_word_t tmp1[NUM_ECC_WORDS];
1060 uECC_word_t tmp2[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +01001061 wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +03001062
1063 /* The point at infinity is invalid. */
1064 if (EccPoint_isZero(point, curve)) {
1065 return -1;
1066 }
1067
1068 /* x and y must be smaller than p. */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +01001069 if (uECC_vli_cmp_unsafe(curve_p, point) != 1 ||
1070 uECC_vli_cmp_unsafe(curve_p, point + num_words) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001071 return -2;
1072 }
1073
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +01001074 uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words);
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +01001075 x_side_default(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001076
1077 /* Make sure that y^2 == x^3 + ax + b */
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +01001078 if (uECC_vli_equal(tmp1, tmp2) != 0)
Jarno Lamsa18987a42019-04-24 15:40:43 +03001079 return -3;
1080
1081 return 0;
1082}
1083
1084int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1085{
1086
1087 uECC_word_t _public[NUM_ECC_WORDS * 2];
1088
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001089 uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001090 uECC_vli_bytesToNative(
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +01001091 _public + NUM_ECC_WORDS,
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001092 public_key + NUM_ECC_BYTES,
1093 NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001094
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +01001095 if (memcmp(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001096 return -4;
1097 }
1098
1099 return uECC_valid_point(_public, curve);
1100}
1101
1102int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1103 uECC_Curve curve)
1104{
1105
1106 uECC_word_t _private[NUM_ECC_WORDS];
1107 uECC_word_t _public[NUM_ECC_WORDS * 2];
1108
1109 uECC_vli_bytesToNative(
1110 _private,
1111 private_key,
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +01001112 BITS_TO_BYTES(NUM_ECC_BITS));
Jarno Lamsa18987a42019-04-24 15:40:43 +03001113
1114 /* Make sure the private key is in the range [1, n-1]. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001115 if (uECC_vli_isZero(_private)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001116 return 0;
1117 }
1118
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +01001119 if (uECC_vli_cmp(curve->n, _private) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001120 return 0;
1121 }
1122
1123 /* Compute public key. */
1124 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1125 return 0;
1126 }
1127
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001128 uECC_vli_nativeToBytes(public_key, NUM_ECC_BYTES, _public);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001129 uECC_vli_nativeToBytes(
1130 public_key +
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001131 NUM_ECC_BYTES, NUM_ECC_BYTES, _public + NUM_ECC_WORDS);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001132 return 1;
1133}
Jarno Lamsa46132202019-04-29 14:29:52 +03001134#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +02001135typedef int mbedtls_dummy_tinycrypt_def;
1136#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001137