blob: 14fa582cc7e88a40591601dc732b52ef48f95cf3 [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
Jarno Lamsa18987a42019-04-24 15:40:43 +030066#include <tinycrypt/ecc.h>
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +010067#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030068#include <string.h>
69
70/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
71 * has access to enough entropy in order to feed the PRNG regularly. */
72#if default_RNG_defined
73static uECC_RNG_Function g_rng_function = &default_CSPRNG;
74#else
75static uECC_RNG_Function g_rng_function = 0;
76#endif
77
78void uECC_set_rng(uECC_RNG_Function rng_function)
79{
80 g_rng_function = rng_function;
81}
82
83uECC_RNG_Function uECC_get_rng(void)
84{
85 return g_rng_function;
86}
87
88int uECC_curve_private_key_size(uECC_Curve curve)
89{
90 return BITS_TO_BYTES(curve->num_n_bits);
91}
92
93int uECC_curve_public_key_size(uECC_Curve curve)
94{
95 return 2 * curve->num_bytes;
96}
97
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +010098void uECC_vli_clear(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +030099{
100 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100101 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300102 vli[i] = 0;
103 }
104}
105
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100106uECC_word_t uECC_vli_isZero(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300107{
108 uECC_word_t bits = 0;
109 wordcount_t i;
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100110 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300111 bits |= vli[i];
112 }
113 return (bits == 0);
114}
115
116uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
117{
118 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
119 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
120}
121
122/* Counts the number of words in vli. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100123static wordcount_t vli_numDigits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300124{
125
126 wordcount_t i;
127 /* Search from the end until we find a non-zero digit. We do it in reverse
128 * because we expect that most digits will be nonzero. */
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100129 for (i = NUM_ECC_WORDS - 1; i >= 0 && vli[i] == 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300130 }
131
132 return (i + 1);
133}
134
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100135bitcount_t uECC_vli_numBits(const uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300136{
137
138 uECC_word_t i;
139 uECC_word_t digit;
140
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100141 wordcount_t num_digits = vli_numDigits(vli);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300142 if (num_digits == 0) {
143 return 0;
144 }
145
146 digit = vli[num_digits - 1];
147 for (i = 0; digit; ++i) {
148 digit >>= 1;
149 }
150
151 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
152}
153
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100154void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300155{
156 wordcount_t i;
157
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100158 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300159 dest[i] = src[i];
160 }
161}
162
163cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100164 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300165{
166 wordcount_t i;
167
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100168 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300169 if (left[i] > right[i]) {
170 return 1;
171 } else if (left[i] < right[i]) {
172 return -1;
173 }
174 }
175 return 0;
176}
177
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100178uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300179{
180
181 uECC_word_t diff = 0;
182 wordcount_t i;
183
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +0100184 for (i = NUM_ECC_WORDS - 1; i >= 0; --i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300185 diff |= (left[i] ^ right[i]);
186 }
187 return !(diff == 0);
188}
189
190uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
191{
192 return (p_true*(cond)) | (p_false*(!cond));
193}
194
195/* Computes result = left - right, returning borrow, in constant time.
196 * Can modify in place. */
197uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100198 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300199{
200 uECC_word_t borrow = 0;
201 wordcount_t i;
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100202 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300203 uECC_word_t diff = left[i] - right[i] - borrow;
204 uECC_word_t val = (diff > left[i]);
205 borrow = cond_set(val, borrow, (diff != left[i]));
206
207 result[i] = diff;
208 }
209 return borrow;
210}
211
212/* Computes result = left + right, returning carry, in constant time.
213 * Can modify in place. */
214static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100215 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300216{
217 uECC_word_t carry = 0;
218 wordcount_t i;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100219 for (i = 0; i < NUM_ECC_WORDS; ++i) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300220 uECC_word_t sum = left[i] + right[i] + carry;
221 uECC_word_t val = (sum < left[i]);
222 carry = cond_set(val, carry, (sum != left[i]));
223 result[i] = sum;
224 }
225 return carry;
226}
227
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +0100228cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300229{
230 uECC_word_t tmp[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100231 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right);
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100232 uECC_word_t equal = uECC_vli_isZero(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300233 return (!equal - 2 * neg);
234}
235
236/* Computes vli = vli >> 1. */
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100237static void uECC_vli_rshift1(uECC_word_t *vli)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300238{
239 uECC_word_t *end = vli;
240 uECC_word_t carry = 0;
241
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100242 vli += NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300243 while (vli-- > end) {
244 uECC_word_t temp = *vli;
245 *vli = (temp >> 1) | carry;
246 carry = temp << (uECC_WORD_BITS - 1);
247 }
248}
249
Manuel Pégourié-Gonnard86c4f812019-10-31 13:02:03 +0100250/* Compute a * b + r, where r is a double-word with high-order word r1 and
251 * low-order word r0, and store the result in the same double-word (r1, r0),
252 * with the carry bit stored in r2.
253 *
254 * (r2, r1, r0) = a * b + (r1, r0):
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200255 * [in] a, b: operands to be multiplied
256 * [in] r0, r1: low and high-order words of operand to add
257 * [out] r0, r1: low and high-order words of the result
258 * [out] r2: carry
259 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300260static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
261 uECC_word_t *r1, uECC_word_t *r2)
262{
263
264 uECC_dword_t p = (uECC_dword_t)a * b;
265 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
266 r01 += p;
267 *r2 += (r01 < p);
268 *r1 = r01 >> uECC_WORD_BITS;
269 *r0 = (uECC_word_t)r01;
270
271}
272
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200273/* State for implementing random delays in uECC_vli_mult_rnd().
274 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100275 * The state is initialized by randomizing delays and setting i = 0.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200276 * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i.
277 *
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100278 * Randomized vli multiplication is used only for point operations
279 * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication
280 * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to
281 * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(),
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100282 * indirectly through uECC_vli_modMult_rnd().
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100283 *
284 * Considering this, in order to minimize the number of calls to the RNG
285 * (which impact performance) while keeping the size of the structure low,
286 * make room for 14 randomized vli mults, which corresponds to one step in the
287 * scalar multiplication routine.
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200288 */
289typedef struct {
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100290 uint8_t i;
291 uint8_t delays[14];
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100292} ecc_wait_state_t;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200293
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100294/*
295 * Reset wait_state so that it's ready to be used.
296 */
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100297void ecc_wait_state_reset(ecc_wait_state_t *ws)
Manuel Pégourié-Gonnardd4671162019-10-31 11:26:26 +0100298{
299 if (ws == NULL)
300 return;
301
302 ws->i = 0;
303 g_rng_function(ws->delays, sizeof(ws->delays));
304}
305
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200306/* Computes result = left * right. Result must be 2 * num_words long.
307 *
308 * As a counter-measure against horizontal attacks, add noise by performing
309 * a random number of extra computations performing random additional accesses
310 * to limbs of the input.
311 *
312 * Each of the two actual computation loops is surrounded by two
313 * similar-looking waiting loops, to make the beginning and end of the actual
314 * computation harder to spot.
315 *
316 * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That
317 * makes an average of 6 extra calls. Compared to the main computation which
318 * makes 64 such calls, this represents an average performance degradation of
319 * less than 10%.
320 *
321 * Compared to the original uECC_vli_mult(), loose the num_words argument as we
322 * know it's always 8. This saves a bit of code size and execution speed.
323 */
324static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100325 const uECC_word_t *right, ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300326{
327
328 uECC_word_t r0 = 0;
329 uECC_word_t r1 = 0;
330 uECC_word_t r2 = 0;
331 wordcount_t i, k;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100332 const uint8_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200333
334 /* Fetch 8 bit worth of delay from the state; 0 if we have no state */
335 uint8_t delays = s ? s->delays[s->i++] : 0;
336 uECC_word_t rr0 = 0, rr1 = 0;
337 volatile uECC_word_t r;
338
339 /* Mimic start of next loop: k in [0, 3] */
340 k = 0 + (delays & 0x03);
341 delays >>= 2;
342 /* k = 0 -> i in [1, 0] -> 0 extra muladd;
343 * k = 3 -> i in [1, 3] -> 3 extra muladd */
Manuel Pégourié-Gonnardc8814862019-11-05 10:32:37 +0100344 for (i = 1; i <= k; ++i) {
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200345 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
346 }
347 r = rr0;
348 rr0 = rr1;
349 rr1 = r2;
350 r2 = 0;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300351
352 /* Compute each digit of result in sequence, maintaining the carries. */
353 for (k = 0; k < num_words; ++k) {
354
355 for (i = 0; i <= k; ++i) {
356 muladd(left[i], right[k - i], &r0, &r1, &r2);
357 }
358
359 result[k] = r0;
360 r0 = r1;
361 r1 = r2;
362 r2 = 0;
363 }
364
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200365 /* Mimic end of previous loop: k in [4, 7] */
366 k = 4 + (delays & 0x03);
367 delays >>= 2;
368 /* k = 4 -> i in [5, 4] -> 0 extra muladd;
369 * k = 7 -> i in [5, 7] -> 3 extra muladd */
370 for (i = 5; i <= k; ++i) {
371 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
372 }
373 r = rr0;
374 rr0 = rr1;
375 rr1 = r2;
376 r2 = 0;
377
378 /* Mimic start of next loop: k in [8, 11] */
379 k = 11 - (delays & 0x03);
380 delays >>= 2;
381 /* k = 8 -> i in [5, 7] -> 3 extra muladd;
382 * k = 11 -> i in [8, 7] -> 0 extra muladd */
383 for (i = (k + 5) - num_words; i < num_words; ++i) {
384 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
385 }
386 r = rr0;
387 rr0 = rr1;
388 rr1 = r2;
389 r2 = 0;
390
Jarno Lamsa18987a42019-04-24 15:40:43 +0300391 for (k = num_words; k < num_words * 2 - 1; ++k) {
392
393 for (i = (k + 1) - num_words; i < num_words; ++i) {
394 muladd(left[i], right[k - i], &r0, &r1, &r2);
395 }
396 result[k] = r0;
397 r0 = r1;
398 r1 = r2;
399 r2 = 0;
400 }
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200401
Jarno Lamsa18987a42019-04-24 15:40:43 +0300402 result[num_words * 2 - 1] = r0;
Manuel Pégourié-Gonnard14ab9c22019-10-22 09:49:53 +0200403
404 /* Mimic end of previous loop: k in [12, 15] */
405 k = 15 - (delays & 0x03);
406 delays >>= 2;
407 /* k = 12 -> i in [5, 7] -> 3 extra muladd;
408 * k = 15 -> i in [8, 7] -> 0 extra muladd */
409 for (i = (k + 1) - num_words; i < num_words; ++i) {
410 muladd(left[i], right[k - i], &rr0, &rr1, &r2);
411 }
412 r = rr0;
413 rr0 = rr1;
414 rr1 = r2;
415 r2 = 0;
416
417 /* avoid warning that r is set but not used */
418 (void) r;
419}
420
Jarno Lamsa18987a42019-04-24 15:40:43 +0300421void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100422 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300423{
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100424 uECC_word_t carry = uECC_vli_add(result, left, right);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100425 if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300426 /* result > mod (result = mod + remainder), so subtract mod to get
427 * remainder. */
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100428 uECC_vli_sub(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300429 }
430}
431
432void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100433 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300434{
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100435 uECC_word_t l_borrow = uECC_vli_sub(result, left, right);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300436 if (l_borrow) {
437 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
438 * we can get the correct result from result + mod (with overflow). */
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100439 uECC_vli_add(result, result, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300440 }
441}
442
443/* Computes result = product % mod, where product is 2N words long. */
444/* Currently only designed to work for curve_p or curve_n. */
445void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100446 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300447{
448 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
449 uECC_word_t tmp[2 * NUM_ECC_WORDS];
450 uECC_word_t *v[2] = {tmp, product};
451 uECC_word_t index;
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100452 const wordcount_t num_words = NUM_ECC_WORDS;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300453
454 /* Shift mod so its highest set bit is at the maximum position. */
455 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100456 uECC_vli_numBits(mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300457 wordcount_t word_shift = shift / uECC_WORD_BITS;
458 wordcount_t bit_shift = shift % uECC_WORD_BITS;
459 uECC_word_t carry = 0;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100460 uECC_vli_clear(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300461 if (bit_shift > 0) {
462 for(index = 0; index < (uECC_word_t)num_words; ++index) {
463 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
464 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
465 }
466 } else {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100467 uECC_vli_set(mod_multiple + word_shift, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300468 }
469
470 for (index = 1; shift >= 0; --shift) {
471 uECC_word_t borrow = 0;
472 wordcount_t i;
473 for (i = 0; i < num_words * 2; ++i) {
474 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
475 if (diff != v[index][i]) {
476 borrow = (diff > v[index][i]);
477 }
478 v[1 - index][i] = diff;
479 }
480 /* Swap the index if there was no borrow */
481 index = !(index ^ borrow);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100482 uECC_vli_rshift1(mod_multiple);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300483 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
484 (uECC_WORD_BITS - 1);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100485 uECC_vli_rshift1(mod_multiple + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300486 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100487 uECC_vli_set(result, v[index]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300488}
489
490void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnard3e20adf2019-11-04 15:00:43 +0100491 const uECC_word_t *right, const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300492{
493 uECC_word_t product[2 * NUM_ECC_WORDS];
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100494 uECC_vli_mult_rnd(product, left, right, NULL);
Manuel Pégourié-Gonnard10349e42019-11-04 14:57:53 +0100495 uECC_vli_mmod(result, product, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300496}
497
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100498static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100499 const uECC_word_t *right, ecc_wait_state_t *s)
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100500{
501 uECC_word_t product[2 * NUM_ECC_WORDS];
502 uECC_vli_mult_rnd(product, left, right, s);
503
504 vli_mmod_fast_secp256r1(result, product);
505}
506
Jarno Lamsa18987a42019-04-24 15:40:43 +0300507void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100508 const uECC_word_t *right)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300509{
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100510 uECC_vli_modMult_rnd(result, left, right, NULL);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300511}
512
Jarno Lamsa18987a42019-04-24 15:40:43 +0300513#define EVEN(vli) (!(vli[0] & 1))
514
515static void vli_modInv_update(uECC_word_t *uv,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100516 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300517{
518
519 uECC_word_t carry = 0;
520
521 if (!EVEN(uv)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100522 carry = uECC_vli_add(uv, uv, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300523 }
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100524 uECC_vli_rshift1(uv);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300525 if (carry) {
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100526 uv[NUM_ECC_WORDS - 1] |= HIGH_BIT_SET;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300527 }
528}
529
530void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100531 const uECC_word_t *mod)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300532{
533 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
534 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
535 cmpresult_t cmpResult;
536
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100537 if (uECC_vli_isZero(input)) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100538 uECC_vli_clear(result);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300539 return;
540 }
541
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100542 uECC_vli_set(a, input);
543 uECC_vli_set(b, mod);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100544 uECC_vli_clear(u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300545 u[0] = 1;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100546 uECC_vli_clear(v);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100547 while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300548 if (EVEN(a)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100549 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100550 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300551 } else if (EVEN(b)) {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100552 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100553 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300554 } else if (cmpResult > 0) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100555 uECC_vli_sub(a, a, b);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100556 uECC_vli_rshift1(a);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100557 if (uECC_vli_cmp_unsafe(u, v) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100558 uECC_vli_add(u, u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300559 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100560 uECC_vli_sub(u, u, v);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100561 vli_modInv_update(u, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300562 } else {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100563 uECC_vli_sub(b, b, a);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100564 uECC_vli_rshift1(b);
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100565 if (uECC_vli_cmp_unsafe(v, u) < 0) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100566 uECC_vli_add(v, v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300567 }
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100568 uECC_vli_sub(v, v, u);
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100569 vli_modInv_update(v, mod);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300570 }
571 }
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100572 uECC_vli_set(result, u);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300573}
574
575/* ------ Point operations ------ */
576
577void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
578 uECC_word_t * Z1, uECC_Curve curve)
579{
580 /* t1 = X, t2 = Y, t3 = Z */
581 uECC_word_t t4[NUM_ECC_WORDS];
582 uECC_word_t t5[NUM_ECC_WORDS];
583 wordcount_t num_words = curve->num_words;
584
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100585 if (uECC_vli_isZero(Z1)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +0300586 return;
587 }
588
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100589 uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */
590 uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */
591 uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */
592 uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */
593 uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300594
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100595 uECC_vli_modAdd(X1, X1, Z1, curve->p); /* t1 = x1 + z1^2 */
596 uECC_vli_modAdd(Z1, Z1, Z1, curve->p); /* t3 = 2*z1^2 */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100597 uECC_vli_modSub(Z1, X1, Z1, curve->p); /* t3 = x1 - z1^2 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100598 uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300599
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100600 uECC_vli_modAdd(Z1, X1, X1, curve->p); /* t3 = 2*(x1^2 - z1^4) */
601 uECC_vli_modAdd(X1, X1, Z1, curve->p); /* t1 = 3*(x1^2 - z1^4) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300602 if (uECC_vli_testBit(X1, 0)) {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100603 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p);
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100604 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300605 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
606 } else {
Manuel Pégourié-Gonnard5e3baf22019-11-04 14:46:10 +0100607 uECC_vli_rshift1(X1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300608 }
609
610 /* t1 = 3/2*(x1^2 - z1^4) = B */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100611 uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100612 uECC_vli_modSub(Z1, Z1, t5, curve->p); /* t3 = B^2 - A */
613 uECC_vli_modSub(Z1, Z1, t5, curve->p); /* t3 = B^2 - 2A = x3 */
614 uECC_vli_modSub(t5, t5, Z1, curve->p); /* t5 = A - x3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100615 uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300616 /* t4 = B * (A - x3) - y1^4 = y3: */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100617 uECC_vli_modSub(t4, X1, t4, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300618
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100619 uECC_vli_set(X1, Z1);
620 uECC_vli_set(Z1, Y1);
621 uECC_vli_set(Y1, t4);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300622}
623
624void x_side_default(uECC_word_t *result,
625 const uECC_word_t *x,
626 uECC_Curve curve)
627{
628 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300629
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100630 uECC_vli_modMult_fast(result, x, x); /* r = x^2 */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100631 uECC_vli_modSub(result, result, _3, curve->p); /* r = x^2 - 3 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100632 uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300633 /* r = x^3 - 3x + b: */
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100634 uECC_vli_modAdd(result, result, curve->b, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300635}
636
637uECC_Curve uECC_secp256r1(void)
638{
639 return &curve_secp256r1;
640}
641
642void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
643{
644 unsigned int tmp[NUM_ECC_WORDS];
645 int carry;
646
647 /* t */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100648 uECC_vli_set(result, product);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300649
650 /* s1 */
651 tmp[0] = tmp[1] = tmp[2] = 0;
652 tmp[3] = product[11];
653 tmp[4] = product[12];
654 tmp[5] = product[13];
655 tmp[6] = product[14];
656 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100657 carry = uECC_vli_add(tmp, tmp, tmp);
658 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300659
660 /* s2 */
661 tmp[3] = product[12];
662 tmp[4] = product[13];
663 tmp[5] = product[14];
664 tmp[6] = product[15];
665 tmp[7] = 0;
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100666 carry += uECC_vli_add(tmp, tmp, tmp);
667 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300668
669 /* s3 */
670 tmp[0] = product[8];
671 tmp[1] = product[9];
672 tmp[2] = product[10];
673 tmp[3] = tmp[4] = tmp[5] = 0;
674 tmp[6] = product[14];
675 tmp[7] = product[15];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100676 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300677
678 /* s4 */
679 tmp[0] = product[9];
680 tmp[1] = product[10];
681 tmp[2] = product[11];
682 tmp[3] = product[13];
683 tmp[4] = product[14];
684 tmp[5] = product[15];
685 tmp[6] = product[13];
686 tmp[7] = product[8];
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100687 carry += uECC_vli_add(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300688
689 /* d1 */
690 tmp[0] = product[11];
691 tmp[1] = product[12];
692 tmp[2] = product[13];
693 tmp[3] = tmp[4] = tmp[5] = 0;
694 tmp[6] = product[8];
695 tmp[7] = product[10];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100696 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300697
698 /* d2 */
699 tmp[0] = product[12];
700 tmp[1] = product[13];
701 tmp[2] = product[14];
702 tmp[3] = product[15];
703 tmp[4] = tmp[5] = 0;
704 tmp[6] = product[9];
705 tmp[7] = product[11];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100706 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300707
708 /* d3 */
709 tmp[0] = product[13];
710 tmp[1] = product[14];
711 tmp[2] = product[15];
712 tmp[3] = product[8];
713 tmp[4] = product[9];
714 tmp[5] = product[10];
715 tmp[6] = 0;
716 tmp[7] = product[12];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100717 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300718
719 /* d4 */
720 tmp[0] = product[14];
721 tmp[1] = product[15];
722 tmp[2] = 0;
723 tmp[3] = product[9];
724 tmp[4] = product[10];
725 tmp[5] = product[11];
726 tmp[6] = 0;
727 tmp[7] = product[13];
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100728 carry -= uECC_vli_sub(result, result, tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300729
730 if (carry < 0) {
731 do {
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100732 carry += uECC_vli_add(result, result, curve_secp256r1.p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300733 }
734 while (carry < 0);
735 } else {
736 while (carry ||
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +0100737 uECC_vli_cmp_unsafe(curve_secp256r1.p, result) != 1) {
Manuel Pégourié-Gonnard129b42e2019-11-04 14:41:45 +0100738 carry -= uECC_vli_sub(result, result, curve_secp256r1.p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300739 }
740 }
741}
742
743uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
744{
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100745 (void) curve;
746 return uECC_vli_isZero(point);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300747}
748
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100749void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300750{
751 uECC_word_t t1[NUM_ECC_WORDS];
752
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100753 uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */
754 uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */
755 uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */
756 uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300757}
758
759/* P = (x1, y1) => 2P, (x2, y2) => P' */
760static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
761 uECC_word_t * X2, uECC_word_t * Y2,
762 const uECC_word_t * const initial_Z,
763 uECC_Curve curve)
764{
765 uECC_word_t z[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +0300766 if (initial_Z) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100767 uECC_vli_set(z, initial_Z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300768 } else {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100769 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300770 z[0] = 1;
771 }
772
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100773 uECC_vli_set(X2, X1);
774 uECC_vli_set(Y2, Y1);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300775
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100776 apply_z(X1, Y1, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300777 curve->double_jacobian(X1, Y1, z, curve);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100778 apply_z(X2, Y2, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300779}
780
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100781static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1,
782 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100783 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300784{
785 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
786 uECC_word_t t5[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100787 const uECC_Curve curve = &curve_secp256r1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300788
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100789 uECC_vli_modSub(t5, X2, X1, curve->p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100790 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100791 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
792 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100793 uECC_vli_modSub(Y2, Y2, Y1, curve->p); /* t4 = y2 - y1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100794 uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300795
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100796 uECC_vli_modSub(t5, t5, X1, curve->p); /* t5 = D - B */
797 uECC_vli_modSub(t5, t5, X2, curve->p); /* t5 = D - B - C = x3 */
798 uECC_vli_modSub(X2, X2, X1, curve->p); /* t3 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100799 uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100800 uECC_vli_modSub(X2, X1, t5, curve->p); /* t3 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100801 uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100802 uECC_vli_modSub(Y2, Y2, Y1, curve->p); /* t4 = y3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300803
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100804 uECC_vli_set(X2, t5);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300805}
806
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100807void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
808 uECC_word_t * X2, uECC_word_t * Y2,
809 uECC_Curve curve)
810{
811 (void) curve;
812 XYcZ_add_rnd(X1, Y1, X2, Y2, NULL);
813}
814
Jarno Lamsa18987a42019-04-24 15:40:43 +0300815/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
816 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
817 or P => P - Q, Q => P + Q
818 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100819static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1,
820 uECC_word_t * X2, uECC_word_t * Y2,
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100821 ecc_wait_state_t *s)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300822{
823 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
824 uECC_word_t t5[NUM_ECC_WORDS];
825 uECC_word_t t6[NUM_ECC_WORDS];
826 uECC_word_t t7[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100827 const uECC_Curve curve = &curve_secp256r1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300828
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100829 uECC_vli_modSub(t5, X2, X1, curve->p); /* t5 = x2 - x1 */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100830 uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100831 uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */
832 uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100833 uECC_vli_modAdd(t5, Y2, Y1, curve->p); /* t5 = y2 + y1 */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100834 uECC_vli_modSub(Y2, Y2, Y1, curve->p); /* t4 = y2 - y1 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300835
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100836 uECC_vli_modSub(t6, X2, X1, curve->p); /* t6 = C - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100837 uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */
Manuel Pégourié-Gonnard0779be72019-11-04 14:48:22 +0100838 uECC_vli_modAdd(t6, X1, X2, curve->p); /* t6 = B + C */
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100839 uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100840 uECC_vli_modSub(X2, X2, t6, curve->p); /* t3 = D - (B + C) = x3 */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300841
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100842 uECC_vli_modSub(t7, X1, X2, curve->p); /* t7 = B - x3 */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100843 uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300844 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100845 uECC_vli_modSub(Y2, Y2, Y1, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300846
Manuel Pégourié-Gonnardc78d86b2019-11-04 10:18:42 +0100847 uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100848 uECC_vli_modSub(t7, t7, t6, curve->p); /* t7 = F - (B + C) = x3' */
849 uECC_vli_modSub(t6, t7, X1, curve->p); /* t6 = x3' - B */
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100850 uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300851 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100852 uECC_vli_modSub(Y1, t6, Y1, curve->p);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300853
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100854 uECC_vli_set(X1, t7);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300855}
856
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100857static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
Jarno Lamsa18987a42019-04-24 15:40:43 +0300858 const uECC_word_t * scalar,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100859 const uECC_word_t * initial_Z)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300860{
861 /* R0 and R1 */
862 uECC_word_t Rx[2][NUM_ECC_WORDS];
863 uECC_word_t Ry[2][NUM_ECC_WORDS];
864 uECC_word_t z[NUM_ECC_WORDS];
865 bitcount_t i;
866 uECC_word_t nb;
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100867 const wordcount_t num_words = NUM_ECC_WORDS;
868 const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100869 const uECC_Curve curve = uECC_secp256r1();
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100870 ecc_wait_state_t wait_state;
871 ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300872
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100873 uECC_vli_set(Rx[1], point);
874 uECC_vli_set(Ry[1], point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300875
876 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
877
878 for (i = num_bits - 2; i > 0; --i) {
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100879 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300880 nb = !uECC_vli_testBit(scalar, i);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100881 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
882 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300883 }
884
Manuel Pégourié-Gonnardd5e503e2019-10-31 12:53:44 +0100885 ecc_wait_state_reset(ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300886 nb = !uECC_vli_testBit(scalar, 0);
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100887 XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300888
889 /* Find final 1/Z value. */
Manuel Pégourié-Gonnard1b0875d2019-11-04 14:50:54 +0100890 uECC_vli_modSub(z, Rx[1], Rx[0], curve->p); /* X1 - X0 */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100891 uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */
892 uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */
Manuel Pégourié-Gonnard91353482019-11-04 15:04:20 +0100893 uECC_vli_modInv(z, z, curve->p); /* 1 / (xP * Yb * (X1 - X0))*/
Jarno Lamsa18987a42019-04-24 15:40:43 +0300894 /* yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100895 uECC_vli_modMult_fast(z, z, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300896 /* Xb * yP / (xP * Yb * (X1 - X0)) */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100897 uECC_vli_modMult_fast(z, z, Rx[1 - nb]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300898 /* End 1/Z calculation */
899
Manuel Pégourié-Gonnard938f53f2019-10-29 11:23:43 +0100900 XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100901 apply_z(Rx[0], Ry[0], z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300902
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100903 uECC_vli_set(result, Rx[0]);
904 uECC_vli_set(result + num_words, Ry[0]);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300905}
906
Manuel Pégourié-Gonnard27926d62019-11-04 11:26:46 +0100907static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100908 uECC_word_t *k1)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300909{
910
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100911 wordcount_t num_n_words = NUM_ECC_WORDS;
912 bitcount_t num_n_bits = NUM_ECC_BITS;
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100913 const uECC_Curve curve = uECC_secp256r1();
Jarno Lamsa18987a42019-04-24 15:40:43 +0300914
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100915 uECC_word_t carry = uECC_vli_add(k0, k, curve->n) ||
Jarno Lamsa18987a42019-04-24 15:40:43 +0300916 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
917 uECC_vli_testBit(k0, num_n_bits));
918
Manuel Pégourié-Gonnard02d9d212019-11-04 12:37:08 +0100919 uECC_vli_add(k1, k0, curve->n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300920
921 return carry;
922}
923
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100924int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
925 const uECC_word_t * scalar, uECC_Curve curve)
926{
927 uECC_word_t tmp[NUM_ECC_WORDS];
928 uECC_word_t s[NUM_ECC_WORDS];
929 uECC_word_t *k2[2] = {tmp, s};
Manuel Pégourié-Gonnard78a7e352019-11-04 12:31:06 +0100930 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100931 uECC_word_t carry;
932 uECC_word_t *initial_Z = 0;
933 int r;
934
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100935 if (curve != uECC_secp256r1())
936 return 0;
937
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100938 /* Regularize the bitcount for the private key so that attackers cannot use a
939 * side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100940 carry = regularize_k(scalar, tmp, s);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100941
942 /* If an RNG function was specified, get a random initial Z value to
943 * protect against side-channel attacks such as Template SPA */
944 if (g_rng_function) {
945 if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) {
946 r = 0;
947 goto clear_and_out;
948 }
949 initial_Z = k2[carry];
950 }
951
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100952 EccPoint_mult(result, point, k2[!carry], initial_Z);
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +0100953 r = 1;
954
955clear_and_out:
956 /* erasing temporary buffer used to store secret: */
957 mbedtls_platform_zeroize(k2, sizeof(k2));
958 mbedtls_platform_zeroize(tmp, sizeof(tmp));
959 mbedtls_platform_zeroize(s, sizeof(s));
960
961 return r;
962}
963
Jarno Lamsa18987a42019-04-24 15:40:43 +0300964uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
965 uECC_word_t *private_key,
966 uECC_Curve curve)
967{
968
969 uECC_word_t tmp1[NUM_ECC_WORDS];
970 uECC_word_t tmp2[NUM_ECC_WORDS];
971 uECC_word_t *p2[2] = {tmp1, tmp2};
972 uECC_word_t carry;
973
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100974 if (curve != uECC_secp256r1())
975 return 0;
976
Jarno Lamsa18987a42019-04-24 15:40:43 +0300977 /* Regularize the bitcount for the private key so that attackers cannot
978 * use a side channel attack to learn the number of leading zeros. */
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100979 carry = regularize_k(private_key, tmp1, tmp2);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300980
Manuel Pégourié-Gonnard3645ac92019-11-04 11:39:18 +0100981 EccPoint_mult(result, curve->G, p2[!carry], 0);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300982
983 if (EccPoint_isZero(result, curve)) {
984 return 0;
985 }
986 return 1;
987}
988
989/* Converts an integer in uECC native format to big-endian bytes. */
990void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
991 const unsigned int *native)
992{
993 wordcount_t i;
994 for (i = 0; i < num_bytes; ++i) {
995 unsigned b = num_bytes - 1 - i;
996 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
997 }
998}
999
1000/* Converts big-endian bytes to an integer in uECC native format. */
1001void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
1002 int num_bytes)
1003{
1004 wordcount_t i;
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +01001005 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001006 for (i = 0; i < num_bytes; ++i) {
1007 unsigned b = num_bytes - 1 - i;
1008 native[b / uECC_WORD_SIZE] |=
1009 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
1010 }
1011}
1012
1013int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
1014 wordcount_t num_words)
1015{
1016 uECC_word_t mask = (uECC_word_t)-1;
1017 uECC_word_t tries;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +01001018 bitcount_t num_bits = uECC_vli_numBits(top);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001019
1020 if (!g_rng_function) {
1021 return 0;
1022 }
1023
1024 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
1025 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
1026 return 0;
1027 }
1028 random[num_words - 1] &=
1029 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001030 if (!uECC_vli_isZero(random) &&
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +01001031 uECC_vli_cmp(top, random) == 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001032 return 1;
1033 }
1034 }
1035 return 0;
1036}
1037
1038
1039int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
1040{
1041 uECC_word_t tmp1[NUM_ECC_WORDS];
1042 uECC_word_t tmp2[NUM_ECC_WORDS];
1043 wordcount_t num_words = curve->num_words;
1044
1045 /* The point at infinity is invalid. */
1046 if (EccPoint_isZero(point, curve)) {
1047 return -1;
1048 }
1049
1050 /* x and y must be smaller than p. */
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +01001051 if (uECC_vli_cmp_unsafe(curve->p, point) != 1 ||
1052 uECC_vli_cmp_unsafe(curve->p, point + num_words) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001053 return -2;
1054 }
1055
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +01001056 uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words);
Jarno Lamsa18987a42019-04-24 15:40:43 +03001057 curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
1058
1059 /* Make sure that y^2 == x^3 + ax + b */
Manuel Pégourié-Gonnard2eca3d32019-11-04 14:33:09 +01001060 if (uECC_vli_equal(tmp1, tmp2) != 0)
Jarno Lamsa18987a42019-04-24 15:40:43 +03001061 return -3;
1062
1063 return 0;
1064}
1065
1066int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
1067{
1068
1069 uECC_word_t _public[NUM_ECC_WORDS * 2];
1070
1071 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
1072 uECC_vli_bytesToNative(
1073 _public + curve->num_words,
1074 public_key + curve->num_bytes,
1075 curve->num_bytes);
1076
Manuel Pégourié-Gonnarda7521912019-11-04 14:31:35 +01001077 if (memcmp(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001078 return -4;
1079 }
1080
1081 return uECC_valid_point(_public, curve);
1082}
1083
1084int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
1085 uECC_Curve curve)
1086{
1087
1088 uECC_word_t _private[NUM_ECC_WORDS];
1089 uECC_word_t _public[NUM_ECC_WORDS * 2];
1090
1091 uECC_vli_bytesToNative(
1092 _private,
1093 private_key,
1094 BITS_TO_BYTES(curve->num_n_bits));
1095
1096 /* Make sure the private key is in the range [1, n-1]. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +01001097 if (uECC_vli_isZero(_private)) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001098 return 0;
1099 }
1100
Manuel Pégourié-Gonnard2cb3eea2019-11-04 14:43:35 +01001101 if (uECC_vli_cmp(curve->n, _private) != 1) {
Jarno Lamsa18987a42019-04-24 15:40:43 +03001102 return 0;
1103 }
1104
1105 /* Compute public key. */
1106 if (!EccPoint_compute_public_key(_public, _private, curve)) {
1107 return 0;
1108 }
1109
1110 uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
1111 uECC_vli_nativeToBytes(
1112 public_key +
1113 curve->num_bytes, curve->num_bytes, _public + curve->num_words);
1114 return 1;
1115}
Jarno Lamsa18987a42019-04-24 15:40:43 +03001116