blob: 03645c0b841c4449bd3eca0f9b80068880bc522c [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};
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +010078const uECC_word_t curve_n[NUM_ECC_WORDS] = {
79 BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
80 BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
81 BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
82 BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
83};
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +010084const uECC_word_t curve_G[2 * NUM_ECC_WORDS] = {
85 BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
86 BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
87 BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),
88 BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B),
89 BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB),
90 BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B),
91 BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E),
92 BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F)
93};
Manuel Pégourié-Gonnardffd13992019-11-21 10:39:06 +010094const uECC_word_t curve_b[NUM_ECC_WORDS] = {
95 BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B),
96 BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65),
97 BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3),
98 BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A)
99};
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100100
Jarno Lamsa18987a42019-04-24 15:40:43 +0300101/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
102 * has access to enough entropy in order to feed the PRNG regularly. */
103#if default_RNG_defined
104static uECC_RNG_Function g_rng_function = &default_CSPRNG;
105#else
106static uECC_RNG_Function g_rng_function = 0;
107#endif
108
109void uECC_set_rng(uECC_RNG_Function rng_function)
110{
111 g_rng_function = rng_function;
112}
113
114uECC_RNG_Function uECC_get_rng(void)
115{
116 return g_rng_function;
117}
118
119int uECC_curve_private_key_size(uECC_Curve curve)
120{
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100121 (void) curve;
122 return BITS_TO_BYTES(NUM_ECC_BITS);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300123}
124
125int uECC_curve_public_key_size(uECC_Curve curve)
126{
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100127 (void) curve;
128 return 2 * NUM_ECC_BYTES;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300129}
130
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100131void uECC_vli_clear(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300132{
133 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100134 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300135 vli[i] = 0;
136 }
137}
138
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100139uECC_word_t uECC_vli_isZero(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300140{
141 uECC_word_t bits = 0;
142 wordcount_t i;
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100143 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300144 bits |= vli[i];
145 }
146 return (bits == 0);
147}
148
149uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
150{
151 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
152 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
153}
154
155/* Counts the number of words in vli. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100156static wordcount_t vli_numDigits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300157{
158
159 wordcount_t i;
160 /* Search from the end until we find a non-zero digit. We do it in reverse
161 * because we expect that most digits will be nonzero. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100162 for (i = NUM_ECC_WORDS - 1; i >= 0 && vli[i] == 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300163 }
164
165 return (i + 1);
166}
167
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100168bitcount_t uECC_vli_numBits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300169{
170
171 uECC_word_t i;
172 uECC_word_t digit;
173
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100174 wordcount_t num_digits = vli_numDigits(vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300175 if (num_digits == 0) {
176 return 0;
177 }
178
179 digit = vli[num_digits - 1];
180 for (i = 0; digit; ++i) {
181 digit >>= 1;
182 }
183
184 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
185}
186
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100187void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300188{
189 wordcount_t i;
190
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100191 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300192 dest[i] = src[i];
193 }
194}
195
196cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100197 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300198{
199 wordcount_t i;
200
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100201 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300202 if (left[i] > right[i]) {
203 return 1;
204 } else if (left[i] < right[i]) {
205 return -1;
206 }
207 }
208 return 0;
209}
210
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100211uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300212{
213
214 uECC_word_t diff = 0;
215 wordcount_t i;
216
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100217 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300218 diff |= (left[i] ^ right[i]);
219 }
Manuel Pégourié-Gonnard2b6312b2019-11-06 10:42:02 +0100220 return diff;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300221}
222
223uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
224{
225 return (p_true*(cond)) | (p_false*(!cond));
226}
227
228/* Computes result = left - right, returning borrow, in constant time.
229 * Can modify in place. */
230uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100231 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300232{
233 uECC_word_t borrow = 0;
234 wordcount_t i;
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100235 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300236 uECC_word_t diff = left[i] - right[i] - borrow;
237 uECC_word_t val = (diff > left[i]);
238 borrow = cond_set(val, borrow, (diff != left[i]));
239
240 result[i] = diff;
241 }
242 return borrow;
243}
244
245/* Computes result = left + right, returning carry, in constant time.
246 * Can modify in place. */
247static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100248 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300249{
250 uECC_word_t carry = 0;
251 wordcount_t i;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100252 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300253 uECC_word_t sum = left[i] + right[i] + carry;
254 uECC_word_t val = (sum < left[i]);
255 carry = cond_set(val, carry, (sum != left[i]));
256 result[i] = sum;
257 }
258 return carry;
259}
260
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +0100261cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300262{
263 uECC_word_t tmp[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100264 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100265 uECC_word_t equal = uECC_vli_isZero(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300266 return (!equal - 2 * neg);
267}
268
269/* Computes vli = vli >> 1. */
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100270static void uECC_vli_rshift1(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300271{
272 uECC_word_t *end = vli;
273 uECC_word_t carry = 0;
274
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100275 vli += NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300276 while (vli-- > end) {
277 uECC_word_t temp = *vli;
278 *vli = (temp >> 1) | carry;
279 carry = temp << (uECC_WORD_BITS - 1);
280 }
281}
282
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100283/* Compute a * b + r, where r is a double-word with high-order word r1 and
284 * low-order word r0, and store the result in the same double-word (r1, r0),
285 * with the carry bit stored in r2.
286 *
287 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200288 * [in] a, b: operands to be multiplied
289 * [in] r0, r1: low and high-order words of operand to add
290 * [out] r0, r1: low and high-order words of the result
291 * [out] r2: carry
292 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300293static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
294 uECC_word_t *r1, uECC_word_t *r2)
295{
296
297 uECC_dword_t p = (uECC_dword_t)a * b;
298 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
299 r01 += p;
300 *r2 += (r01 < p);
301 *r1 = r01 >> uECC_WORD_BITS;
302 *r0 = (uECC_word_t)r01;
303
304}
305
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200306/* State for implementing random delays in uECC_vli_mult_rnd().
307 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100308 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200309 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
310 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100311 * Randomized vli multiplication is used only for point operations
312 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
313 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
314 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100315 * indirectly through uECC_vli_modMult_rnd().
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100316 *
317 * Considering this, in order to minimize the number of calls to the RNG
318 * (which impact performance) while keeping the size of the structure low,
319 * make room for 14 randomized vli mults, which corresponds to one step in the
320 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200321 */
322typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100323 uint8_t i;
324 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100325} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200326
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100327/*
328 * Reset wait_state so that it's ready to be used.
329 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100330void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100331{
332 if (ws == NULL)
333 return;
334
335 ws->i = 0;
336 g_rng_function(ws->delays, sizeof(ws->delays));
337}
338
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200339/* Computes result = left * right. Result must be 2 * num_words long.
340 *
341 * As a counter-measure against horizontal attacks, add noise by performing
342 * a random number of extra computations performing random additional accesses
343 * to limbs of the input.
344 *
345 * Each of the two actual computation loops is surrounded by two
346 * similar-looking waiting loops, to make the beginning and end of the actual
347 * computation harder to spot.
348 *
349 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
350 * makes an average of 6 extra calls. Compared to the main computation which
351 * makes 64 such calls, this represents an average performance degradation of
352 * less than 10%.
353 *
354 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
355 * know it's always 8. This saves a bit of code size and execution speed.
356 */
357static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100358 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300359{
360
361 uECC_word_t r0 = 0;
362 uECC_word_t r1 = 0;
363 uECC_word_t r2 = 0;
364 wordcount_t i, k;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100365 const uint8_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200366
367 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
368 uint8_t delays = s ? s->delays[s->i++] : 0;
369 uECC_word_t rr0 = 0, rr1 = 0;
370 volatile uECC_word_t r;
371
372 /* Mimic start of next loop: k in [0, 3] */
373 k = 0 + (delays & 0x03);
374 delays >>= 2;
375 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
376 * k = 3 -> i in [1, 3] -> 3 extra muladd */
Manuel Pégourié-Gonnardc8814862019-11-05 10:32:37 +0100377 for (i = 1; i <= k; ++i) {
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200378 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
379 }
380 r = rr0;
381 rr0 = rr1;
382 rr1 = r2;
383 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300384
385 /* Compute each digit of result in sequence, maintaining the carries. */
386 for (k = 0; k < num_words; ++k) {
387
388 for (i = 0; i <= k; ++i) {
389 muladd(left[i], right[k - i], &r0, &r1, &r2);
390 }
391
392 result[k] = r0;
393 r0 = r1;
394 r1 = r2;
395 r2 = 0;
396 }
397
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200398 /* Mimic end of previous loop: k in [4, 7] */
399 k = 4 + (delays & 0x03);
400 delays >>= 2;
401 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
402 * k = 7 -> i in [5, 7] -> 3 extra muladd */
403 for (i = 5; i <= k; ++i) {
404 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
405 }
406 r = rr0;
407 rr0 = rr1;
408 rr1 = r2;
409 r2 = 0;
410
411 /* Mimic start of next loop: k in [8, 11] */
412 k = 11 - (delays & 0x03);
413 delays >>= 2;
414 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
415 * k = 11 -> i in [8, 7] -> 0 extra muladd */
416 for (i = (k + 5) - num_words; i < num_words; ++i) {
417 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
418 }
419 r = rr0;
420 rr0 = rr1;
421 rr1 = r2;
422 r2 = 0;
423
Jarno Lamsa18987a42019-04-24 15:40:43 +0300424 for (k = num_words; k < num_words * 2 - 1; ++k) {
425
426 for (i = (k + 1) - num_words; i < num_words; ++i) {
427 muladd(left[i], right[k - i], &r0, &r1, &r2);
428 }
429 result[k] = r0;
430 r0 = r1;
431 r1 = r2;
432 r2 = 0;
433 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200434
Jarno Lamsa18987a42019-04-24 15:40:43 +0300435 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200436
437 /* Mimic end of previous loop: k in [12, 15] */
438 k = 15 - (delays & 0x03);
439 delays >>= 2;
440 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
441 * k = 15 -> i in [8, 7] -> 0 extra muladd */
442 for (i = (k + 1) - num_words; i < num_words; ++i) {
443 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
444 }
445 r = rr0;
446 rr0 = rr1;
447 rr1 = r2;
448 r2 = 0;
449
450 /* avoid warning that r is set but not used */
451 (void) r;
452}
453
Jarno Lamsa18987a42019-04-24 15:40:43 +0300454void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100455 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300456{
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100457 uECC_word_t carry = uECC_vli_add(result, left, right);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100458 if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300459 /* result > mod (result = mod + remainder), so subtract mod to get
460 * remainder. */
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100461 uECC_vli_sub(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300462 }
463}
464
465void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100466 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300467{
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100468 uECC_word_t l_borrow = uECC_vli_sub(result, left, right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300469 if (l_borrow) {
470 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
471 * we can get the correct result from result + mod (with overflow). */
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100472 uECC_vli_add(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300473 }
474}
475
476/* Computes result = product % mod, where product is 2N words long. */
477/* Currently only designed to work for curve_p or curve_n. */
478void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100479 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300480{
481 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
482 uECC_word_t tmp[2 * NUM_ECC_WORDS];
483 uECC_word_t *v[2] = {tmp, product};
484 uECC_word_t index;
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100485 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300486
487 /* Shift mod so its highest set bit is at the maximum position. */
488 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100489 uECC_vli_numBits(mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300490 wordcount_t word_shift = shift / uECC_WORD_BITS;
491 wordcount_t bit_shift = shift % uECC_WORD_BITS;
492 uECC_word_t carry = 0;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100493 uECC_vli_clear(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300494 if (bit_shift > 0) {
495 for(index = 0; index < (uECC_word_t)num_words; ++index) {
496 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
497 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
498 }
499 } else {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100500 uECC_vli_set(mod_multiple + word_shift, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300501 }
502
503 for (index = 1; shift >= 0; --shift) {
504 uECC_word_t borrow = 0;
505 wordcount_t i;
506 for (i = 0; i < num_words * 2; ++i) {
507 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
508 if (diff != v[index][i]) {
509 borrow = (diff > v[index][i]);
510 }
511 v[1 - index][i] = diff;
512 }
513 /* Swap the index if there was no borrow */
514 index = !(index ^ borrow);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100515 uECC_vli_rshift1(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300516 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
517 (uECC_WORD_BITS - 1);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100518 uECC_vli_rshift1(mod_multiple + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300519 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100520 uECC_vli_set(result, v[index]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300521}
522
523void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100524 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300525{
526 uECC_word_t product[2 * NUM_ECC_WORDS];
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100527 uECC_vli_mult_rnd(product, left, right, NULL);
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100528 uECC_vli_mmod(result, product, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300529}
530
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100531static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100532 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100533{
534 uECC_word_t product[2 * NUM_ECC_WORDS];
535 uECC_vli_mult_rnd(product, left, right, s);
536
537 vli_mmod_fast_secp256r1(result, product);
538}
539
Jarno Lamsa18987a42019-04-24 15:40:43 +0300540void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100541 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300542{
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100543 uECC_vli_modMult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300544}
545
Jarno Lamsa18987a42019-04-24 15:40:43 +0300546#define EVEN(vli) (!(vli[0] & 1))
547
548static void vli_modInv_update(uECC_word_t *uv,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100549 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300550{
551
552 uECC_word_t carry = 0;
553
554 if (!EVEN(uv)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100555 carry = uECC_vli_add(uv, uv, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300556 }
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100557 uECC_vli_rshift1(uv);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300558 if (carry) {
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100559 uv[NUM_ECC_WORDS - 1] |= HIGH_BIT_SET;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300560 }
561}
562
563void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100564 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300565{
566 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
567 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
568 cmpresult_t cmpResult;
569
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100570 if (uECC_vli_isZero(input)) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100571 uECC_vli_clear(result);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300572 return;
573 }
574
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100575 uECC_vli_set(a, input);
576 uECC_vli_set(b, mod);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100577 uECC_vli_clear(u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300578 u[0] = 1;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100579 uECC_vli_clear(v);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100580 while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300581 if (EVEN(a)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100582 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100583 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300584 } else if (EVEN(b)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100585 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100586 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300587 } else if (cmpResult > 0) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100588 uECC_vli_sub(a, a, b);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100589 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100590 if (uECC_vli_cmp_unsafe(u, v) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100591 uECC_vli_add(u, u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300592 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100593 uECC_vli_sub(u, u, v);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100594 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300595 } else {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100596 uECC_vli_sub(b, b, a);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100597 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100598 if (uECC_vli_cmp_unsafe(v, u) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100599 uECC_vli_add(v, v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300600 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100601 uECC_vli_sub(v, v, u);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100602 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300603 }
604 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100605 uECC_vli_set(result, u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300606}
607
608/* ------ Point operations ------ */
609
610void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
611 uECC_word_t * Z1, uECC_Curve curve)
612{
613 /* t1 = X, t2 = Y, t3 = Z */
614 uECC_word_t t4[NUM_ECC_WORDS];
615 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +0100616 wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300617
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100618 (void) curve;
619
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100620 if (uECC_vli_isZero(Z1)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300621 return;
622 }
623
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100624 uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */
625 uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
626 uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */
627 uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
628 uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300629
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100630 uECC_vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */
631 uECC_vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */
632 uECC_vli_modSub(Z1, X1, Z1, curve_p); /* t3 = x1 - z1^2 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100633 uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300634
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100635 uECC_vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */
636 uECC_vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300637 if (uECC_vli_testBit(X1, 0)) {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100638 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve_p);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100639 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300640 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
641 } else {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100642 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300643 }
644
645 /* t1 = 3/2*(x1^2 - z1^4) = B */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100646 uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100647 uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */
648 uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */
649 uECC_vli_modSub(t5, t5, Z1, curve_p); /* t5 = A - x3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100650 uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300651 /* t4 = B * (A - x3) - y1^4 = y3: */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100652 uECC_vli_modSub(t4, X1, t4, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300653
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100654 uECC_vli_set(X1, Z1);
655 uECC_vli_set(Z1, Y1);
656 uECC_vli_set(Y1, t4);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300657}
658
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +0100659/*
660 * @brief Computes x^3 + ax + b. result must not overlap x.
661 * @param result OUT -- x^3 + ax + b
662 * @param x IN -- value of x
663 * @param curve IN -- elliptic curve
664 */
665static void x_side_default(uECC_word_t *result,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300666 const uECC_word_t *x,
667 uECC_Curve curve)
668{
669 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300670
Manuel Pégourié-Gonnardffd13992019-11-21 10:39:06 +0100671 (void) curve;
672
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100673 uECC_vli_modMult_fast(result, x, x); /* r = x^2 */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100674 uECC_vli_modSub(result, result, _3, curve_p); /* r = x^2 - 3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100675 uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300676 /* r = x^3 - 3x + b: */
Manuel Pégourié-Gonnardffd13992019-11-21 10:39:06 +0100677 uECC_vli_modAdd(result, result, curve_b, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300678}
679
680uECC_Curve uECC_secp256r1(void)
681{
Manuel Pégourié-Gonnardbc3f4902019-11-21 11:34:43 +0100682 return curve_secp256r1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300683}
684
685void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
686{
687 unsigned int tmp[NUM_ECC_WORDS];
688 int carry;
689
690 /* t */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100691 uECC_vli_set(result, product);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300692
693 /* s1 */
694 tmp[0] = tmp[1] = tmp[2] = 0;
695 tmp[3] = product[11];
696 tmp[4] = product[12];
697 tmp[5] = product[13];
698 tmp[6] = product[14];
699 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100700 carry = uECC_vli_add(tmp, tmp, tmp);
701 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300702
703 /* s2 */
704 tmp[3] = product[12];
705 tmp[4] = product[13];
706 tmp[5] = product[14];
707 tmp[6] = product[15];
708 tmp[7] = 0;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100709 carry += uECC_vli_add(tmp, tmp, tmp);
710 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300711
712 /* s3 */
713 tmp[0] = product[8];
714 tmp[1] = product[9];
715 tmp[2] = product[10];
716 tmp[3] = tmp[4] = tmp[5] = 0;
717 tmp[6] = product[14];
718 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100719 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300720
721 /* s4 */
722 tmp[0] = product[9];
723 tmp[1] = product[10];
724 tmp[2] = product[11];
725 tmp[3] = product[13];
726 tmp[4] = product[14];
727 tmp[5] = product[15];
728 tmp[6] = product[13];
729 tmp[7] = product[8];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100730 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300731
732 /* d1 */
733 tmp[0] = product[11];
734 tmp[1] = product[12];
735 tmp[2] = product[13];
736 tmp[3] = tmp[4] = tmp[5] = 0;
737 tmp[6] = product[8];
738 tmp[7] = product[10];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100739 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300740
741 /* d2 */
742 tmp[0] = product[12];
743 tmp[1] = product[13];
744 tmp[2] = product[14];
745 tmp[3] = product[15];
746 tmp[4] = tmp[5] = 0;
747 tmp[6] = product[9];
748 tmp[7] = product[11];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100749 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300750
751 /* d3 */
752 tmp[0] = product[13];
753 tmp[1] = product[14];
754 tmp[2] = product[15];
755 tmp[3] = product[8];
756 tmp[4] = product[9];
757 tmp[5] = product[10];
758 tmp[6] = 0;
759 tmp[7] = product[12];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100760 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300761
762 /* d4 */
763 tmp[0] = product[14];
764 tmp[1] = product[15];
765 tmp[2] = 0;
766 tmp[3] = product[9];
767 tmp[4] = product[10];
768 tmp[5] = product[11];
769 tmp[6] = 0;
770 tmp[7] = product[13];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100771 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300772
773 if (carry < 0) {
774 do {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100775 carry += uECC_vli_add(result, result, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300776 }
777 while (carry < 0);
778 } else {
779 while (carry ||
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100780 uECC_vli_cmp_unsafe(curve_p, result) != 1) {
781 carry -= uECC_vli_sub(result, result, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300782 }
783 }
784}
785
786uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
787{
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100788 (void) curve;
789 return uECC_vli_isZero(point);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300790}
791
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100792void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300793{
794 uECC_word_t t1[NUM_ECC_WORDS];
795
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100796 uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
797 uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
798 uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
799 uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300800}
801
802/* P = (x1, y1) => 2P, (x2, y2) => P' */
803static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
804 uECC_word_t * X2, uECC_word_t * Y2,
805 const uECC_word_t * const initial_Z,
806 uECC_Curve curve)
807{
808 uECC_word_t z[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300809 if (initial_Z) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100810 uECC_vli_set(z, initial_Z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300811 } else {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100812 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300813 z[0] = 1;
814 }
815
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100816 uECC_vli_set(X2, X1);
817 uECC_vli_set(Y2, Y1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300818
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100819 apply_z(X1, Y1, z);
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +0100820 double_jacobian_default(X1, Y1, z, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100821 apply_z(X2, Y2, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300822}
823
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100824static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
825 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100826 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300827{
828 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
829 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100830
831 uECC_vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100832 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100833 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
834 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100835 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100836 uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300837
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100838 uECC_vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */
839 uECC_vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */
840 uECC_vli_modSub(X2, X2, X1, curve_p); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100841 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100842 uECC_vli_modSub(X2, X1, t5, curve_p); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100843 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100844 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300845
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100846 uECC_vli_set(X2, t5);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300847}
848
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100849void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
850 uECC_word_t * X2, uECC_word_t * Y2,
851 uECC_Curve curve)
852{
853 (void) curve;
854 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
855}
856
Jarno Lamsa18987a42019-04-24 15:40:43 +0300857/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
858 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
859 or P => P - Q, Q => P + Q
860 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100861static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
862 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100863 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300864{
865 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
866 uECC_word_t t5[NUM_ECC_WORDS];
867 uECC_word_t t6[NUM_ECC_WORDS];
868 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100869
870 uECC_vli_modSub(t5, X2, X1, curve_p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100871 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100872 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
873 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100874 uECC_vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */
875 uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300876
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100877 uECC_vli_modSub(t6, X2, X1, curve_p); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100878 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100879 uECC_vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100880 uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100881 uECC_vli_modSub(X2, X2, t6, curve_p); /* t3 = D - (B + C) = x3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300882
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100883 uECC_vli_modSub(t7, X1, X2, curve_p); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100884 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300885 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100886 uECC_vli_modSub(Y2, Y2, Y1, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300887
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100888 uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100889 uECC_vli_modSub(t7, t7, t6, curve_p); /* t7 = F - (B + C) = x3' */
890 uECC_vli_modSub(t6, t7, X1, curve_p); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100891 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300892 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100893 uECC_vli_modSub(Y1, t6, Y1, curve_p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300894
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100895 uECC_vli_set(X1, t7);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300896}
897
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100898static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300899 const uECC_word_t * scalar,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100900 const uECC_word_t * initial_Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300901{
902 /* R0 and R1 */
903 uECC_word_t Rx[2][NUM_ECC_WORDS];
904 uECC_word_t Ry[2][NUM_ECC_WORDS];
905 uECC_word_t z[NUM_ECC_WORDS];
906 bitcount_t i;
907 uECC_word_t nb;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100908 const wordcount_t num_words = NUM_ECC_WORDS;
909 const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100910 const uECC_Curve curve = uECC_secp256r1();
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100911 ecc_wait_state_t wait_state;
912 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300913
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100914 uECC_vli_set(Rx[1], point);
915 uECC_vli_set(Ry[1], point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300916
917 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
918
919 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100920 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300921 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100922 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
923 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300924 }
925
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100926 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300927 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100928 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300929
930 /* Find final 1/Z value. */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100931 uECC_vli_modSub(z, Rx[1], Rx[0], curve_p); /* X1 - X0 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100932 uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
933 uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100934 uECC_vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0))*/
Jarno Lamsa18987a42019-04-24 15:40:43 +0300935 /* yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100936 uECC_vli_modMult_fast(z, z, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300937 /* Xb * yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100938 uECC_vli_modMult_fast(z, z, Rx[1 - nb]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300939 /* End 1/Z calculation */
940
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100941 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100942 apply_z(Rx[0], Ry[0], z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300943
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100944 uECC_vli_set(result, Rx[0]);
945 uECC_vli_set(result + num_words, Ry[0]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300946}
947
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100948static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100949 uECC_word_t *k1)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300950{
951
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100952 wordcount_t num_n_words = NUM_ECC_WORDS;
953 bitcount_t num_n_bits = NUM_ECC_BITS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300954
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100955 uECC_word_t carry = uECC_vli_add(k0, k, curve_n) ||
Jarno Lamsa18987a42019-04-24 15:40:43 +0300956 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
957 uECC_vli_testBit(k0, num_n_bits));
958
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100959 uECC_vli_add(k1, k0, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300960
961 return carry;
962}
963
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100964int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
965 const uECC_word_t * scalar, uECC_Curve curve)
966{
967 uECC_word_t tmp[NUM_ECC_WORDS];
968 uECC_word_t s[NUM_ECC_WORDS];
969 uECC_word_t *k2[2] = {tmp, s};
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100970 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100971 uECC_word_t carry;
972 uECC_word_t *initial_Z = 0;
973 int r;
974
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100975 if (curve != uECC_secp256r1())
976 return 0;
977
Manuel Pégourié-Gonnarde7143322019-11-15 10:47:45 +0100978 /* Protects against invalid curves attacks */
979 if (uECC_valid_point(point, curve) != 0 ) {
980 return 0;
981 }
982
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100983 /* Regularize the bitcount for the private key so that attackers cannot use a
984 * side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100985 carry = regularize_k(scalar, tmp, s);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100986
987 /* If an RNG function was specified, get a random initial Z value to
988 * protect against side-channel attacks such as Template SPA */
989 if (g_rng_function) {
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100990 if (!uECC_generate_random_int(k2[carry], curve_p, num_words)) {
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100991 r = 0;
992 goto clear_and_out;
993 }
994 initial_Z = k2[carry];
995 }
996
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100997 EccPoint_mult(result, point, k2[!carry], initial_Z);
Manuel Pégourié-Gonnard41ab8cb2019-11-14 11:59:09 +0100998
Manuel Pégourié-Gonnarde7143322019-11-15 10:47:45 +0100999 /* Protect against fault injections that would make the resulting
1000 * point not lie on the intended curve */
1001 if (uECC_valid_point(result, curve) != 0 ) {
Manuel Pégourié-Gonnard41ab8cb2019-11-14 11:59:09 +01001002 r = 0;
1003 goto clear_and_out;
1004 }
1005
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +01001006 r = 1;
1007
1008clear_and_out:
1009 /* erasing temporary buffer used to store secret: */
1010 mbedtls_platform_zeroize(k2, sizeof(k2));
1011 mbedtls_platform_zeroize(tmp, sizeof(tmp));
1012 mbedtls_platform_zeroize(s, sizeof(s));
1013
1014 return r;
1015}
1016
Jarno Lamsa18987a42019-04-24 15:40:43 +03001017uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
1018 uECC_word_t *private_key,
1019 uECC_Curve curve)
1020{
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +01001021 return EccPoint_mult_safer(result, curve_G, private_key, curve);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001022}
1023
1024/* Converts an integer in uECC native format to big-endian bytes. */
1025void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
1026 const unsigned int *native)
1027{
1028 wordcount_t i;
1029 for (i = 0; i < num_bytes; ++i) {
1030 unsigned b = num_bytes - 1 - i;
1031 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
1032 }
1033}
1034
1035/* Converts big-endian bytes to an integer in uECC native format. */
1036void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
1037 int num_bytes)
1038{
1039 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +01001040 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001041 for (i = 0; i < num_bytes; ++i) {
1042 unsigned b = num_bytes - 1 - i;
1043 native[b / uECC_WORD_SIZE] |=
1044 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1045 }
1046}
1047
1048int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1049 wordcount_t num_words)
1050{
1051 uECC_word_t mask = (uECC_word_t)-1;
1052 uECC_word_t tries;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +01001053 bitcount_t num_bits = uECC_vli_numBits(top);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001054
1055 if (!g_rng_function) {
1056 return 0;
1057 }
1058
1059 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1060 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1061 return 0;
1062 }
1063 random[num_words - 1] &=
1064 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001065 if (!uECC_vli_isZero(random) &&
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +01001066 uECC_vli_cmp(top, random) == 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001067 return 1;
1068 }
1069 }
1070 return 0;
1071}
1072
1073
1074int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1075{
1076 uECC_word_t tmp1[NUM_ECC_WORDS];
1077 uECC_word_t tmp2[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +01001078 wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +03001079
1080 /* The point at infinity is invalid. */
1081 if (EccPoint_isZero(point, curve)) {
1082 return -1;
1083 }
1084
1085 /* x and y must be smaller than p. */
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +01001086 if (uECC_vli_cmp_unsafe(curve_p, point) != 1 ||
1087 uECC_vli_cmp_unsafe(curve_p, point + num_words) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001088 return -2;
1089 }
1090
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +01001091 uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words);
Manuel Pégourié-Gonnard1c6f7ea2019-11-21 09:18:29 +01001092 x_side_default(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001093
1094 /* Make sure that y^2 == x^3 + ax + b */
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +01001095 if (uECC_vli_equal(tmp1, tmp2) != 0)
Jarno Lamsa18987a42019-04-24 15:40:43 +03001096 return -3;
1097
1098 return 0;
1099}
1100
1101int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1102{
1103
1104 uECC_word_t _public[NUM_ECC_WORDS * 2];
1105
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001106 uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001107 uECC_vli_bytesToNative(
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +01001108 _public + NUM_ECC_WORDS,
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001109 public_key + NUM_ECC_BYTES,
1110 NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001111
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +01001112 if (memcmp(_public, curve_G, NUM_ECC_WORDS * 2) == 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001113 return -4;
1114 }
1115
1116 return uECC_valid_point(_public, curve);
1117}
1118
1119int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1120 uECC_Curve curve)
1121{
1122
1123 uECC_word_t _private[NUM_ECC_WORDS];
1124 uECC_word_t _public[NUM_ECC_WORDS * 2];
1125
1126 uECC_vli_bytesToNative(
1127 _private,
1128 private_key,
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +01001129 BITS_TO_BYTES(NUM_ECC_BITS));
Jarno Lamsa18987a42019-04-24 15:40:43 +03001130
1131 /* Make sure the private key is in the range [1, n-1]. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001132 if (uECC_vli_isZero(_private)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001133 return 0;
1134 }
1135
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +01001136 if (uECC_vli_cmp(curve_n, _private) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001137 return 0;
1138 }
1139
1140 /* Compute public key. */
1141 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1142 return 0;
1143 }
1144
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001145 uECC_vli_nativeToBytes(public_key, NUM_ECC_BYTES, _public);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001146 uECC_vli_nativeToBytes(
1147 public_key +
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +01001148 NUM_ECC_BYTES, NUM_ECC_BYTES, _public + NUM_ECC_WORDS);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001149 return 1;
1150}
Jarno Lamsa46132202019-04-29 14:29:52 +03001151#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +02001152typedef int mbedtls_dummy_tinycrypt_def;
1153#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +03001154