blob: 1f32170dd57f163d690f5b6b4477e552842dee92 [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ecc.c - TinyCrypt implementation of common ECC functions */
2
3/*
4 * Copyright (c) 2014, Kenneth MacKay
5 * All rights reserved.
6 *
Simon Butchercffedb52019-09-09 16:28:54 +01007 * SPDX-License-Identifier: BSD-3-Clause
8 *
Jarno Lamsa18987a42019-04-24 15:40:43 +03009 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions are met:
32 *
33 * - Redistributions of source code must retain the above copyright notice,
34 * this list of conditions and the following disclaimer.
35 *
36 * - Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 *
40 * - Neither the name of Intel Corporation nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
45 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
48 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
54 * POSSIBILITY OF SUCH DAMAGE.
55 */
56
Hanno Becker36ae7582019-07-23 15:52:35 +010057#if !defined(MBEDTLS_CONFIG_FILE)
58#include "mbedtls/config.h"
59#else
60#include MBEDTLS_CONFIG_FILE
61#endif
62
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +020063#if defined(MBEDTLS_USE_TINYCRYPT)
Jarno Lamsa18987a42019-04-24 15:40:43 +030064#include <tinycrypt/ecc.h>
Jarno Lamsa18987a42019-04-24 15:40:43 +030065#include <string.h>
66
67/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
68 * has access to enough entropy in order to feed the PRNG regularly. */
69#if default_RNG_defined
70static uECC_RNG_Function g_rng_function = &default_CSPRNG;
71#else
72static uECC_RNG_Function g_rng_function = 0;
73#endif
74
75void uECC_set_rng(uECC_RNG_Function rng_function)
76{
77 g_rng_function = rng_function;
78}
79
80uECC_RNG_Function uECC_get_rng(void)
81{
82 return g_rng_function;
83}
84
85int uECC_curve_private_key_size(uECC_Curve curve)
86{
87 return BITS_TO_BYTES(curve->num_n_bits);
88}
89
90int uECC_curve_public_key_size(uECC_Curve curve)
91{
92 return 2 * curve->num_bytes;
93}
94
95void uECC_vli_clear(uECC_word_t *vli, wordcount_t num_words)
96{
97 wordcount_t i;
98 for (i = 0; i < num_words; ++i) {
99 vli[i] = 0;
100 }
101}
102
103uECC_word_t uECC_vli_isZero(const uECC_word_t *vli, wordcount_t num_words)
104{
105 uECC_word_t bits = 0;
106 wordcount_t i;
107 for (i = 0; i < num_words; ++i) {
108 bits |= vli[i];
109 }
110 return (bits == 0);
111}
112
113uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit)
114{
115 return (vli[bit >> uECC_WORD_BITS_SHIFT] &
116 ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK)));
117}
118
119/* Counts the number of words in vli. */
120static wordcount_t vli_numDigits(const uECC_word_t *vli,
121 const wordcount_t max_words)
122{
123
124 wordcount_t i;
125 /* Search from the end until we find a non-zero digit. We do it in reverse
126 * because we expect that most digits will be nonzero. */
127 for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) {
128 }
129
130 return (i + 1);
131}
132
133bitcount_t uECC_vli_numBits(const uECC_word_t *vli,
134 const wordcount_t max_words)
135{
136
137 uECC_word_t i;
138 uECC_word_t digit;
139
140 wordcount_t num_digits = vli_numDigits(vli, max_words);
141 if (num_digits == 0) {
142 return 0;
143 }
144
145 digit = vli[num_digits - 1];
146 for (i = 0; digit; ++i) {
147 digit >>= 1;
148 }
149
150 return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i);
151}
152
153void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src,
154 wordcount_t num_words)
155{
156 wordcount_t i;
157
158 for (i = 0; i < num_words; ++i) {
159 dest[i] = src[i];
160 }
161}
162
163cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left,
164 const uECC_word_t *right,
165 wordcount_t num_words)
166{
167 wordcount_t i;
168
169 for (i = num_words - 1; i >= 0; --i) {
170 if (left[i] > right[i]) {
171 return 1;
172 } else if (left[i] < right[i]) {
173 return -1;
174 }
175 }
176 return 0;
177}
178
179uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right,
180 wordcount_t num_words)
181{
182
183 uECC_word_t diff = 0;
184 wordcount_t i;
185
186 for (i = num_words - 1; i >= 0; --i) {
187 diff |= (left[i] ^ right[i]);
188 }
189 return !(diff == 0);
190}
191
192uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond)
193{
194 return (p_true*(cond)) | (p_false*(!cond));
195}
196
197/* Computes result = left - right, returning borrow, in constant time.
198 * Can modify in place. */
199uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left,
200 const uECC_word_t *right, wordcount_t num_words)
201{
202 uECC_word_t borrow = 0;
203 wordcount_t i;
204 for (i = 0; i < num_words; ++i) {
205 uECC_word_t diff = left[i] - right[i] - borrow;
206 uECC_word_t val = (diff > left[i]);
207 borrow = cond_set(val, borrow, (diff != left[i]));
208
209 result[i] = diff;
210 }
211 return borrow;
212}
213
214/* Computes result = left + right, returning carry, in constant time.
215 * Can modify in place. */
216static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left,
217 const uECC_word_t *right, wordcount_t num_words)
218{
219 uECC_word_t carry = 0;
220 wordcount_t i;
221 for (i = 0; i < num_words; ++i) {
222 uECC_word_t sum = left[i] + right[i] + carry;
223 uECC_word_t val = (sum < left[i]);
224 carry = cond_set(val, carry, (sum != left[i]));
225 result[i] = sum;
226 }
227 return carry;
228}
229
230cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right,
231 wordcount_t num_words)
232{
233 uECC_word_t tmp[NUM_ECC_WORDS];
234 uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words);
235 uECC_word_t equal = uECC_vli_isZero(tmp, num_words);
236 return (!equal - 2 * neg);
237}
238
239/* Computes vli = vli >> 1. */
240static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words)
241{
242 uECC_word_t *end = vli;
243 uECC_word_t carry = 0;
244
245 vli += num_words;
246 while (vli-- > end) {
247 uECC_word_t temp = *vli;
248 *vli = (temp >> 1) | carry;
249 carry = temp << (uECC_WORD_BITS - 1);
250 }
251}
252
253static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0,
254 uECC_word_t *r1, uECC_word_t *r2)
255{
256
257 uECC_dword_t p = (uECC_dword_t)a * b;
258 uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0;
259 r01 += p;
260 *r2 += (r01 < p);
261 *r1 = r01 >> uECC_WORD_BITS;
262 *r0 = (uECC_word_t)r01;
263
264}
265
266/* Computes result = left * right. Result must be 2 * num_words long. */
267static void uECC_vli_mult(uECC_word_t *result, const uECC_word_t *left,
268 const uECC_word_t *right, wordcount_t num_words)
269{
270
271 uECC_word_t r0 = 0;
272 uECC_word_t r1 = 0;
273 uECC_word_t r2 = 0;
274 wordcount_t i, k;
275
276 /* Compute each digit of result in sequence, maintaining the carries. */
277 for (k = 0; k < num_words; ++k) {
278
279 for (i = 0; i <= k; ++i) {
280 muladd(left[i], right[k - i], &r0, &r1, &r2);
281 }
282
283 result[k] = r0;
284 r0 = r1;
285 r1 = r2;
286 r2 = 0;
287 }
288
289 for (k = num_words; k < num_words * 2 - 1; ++k) {
290
291 for (i = (k + 1) - num_words; i < num_words; ++i) {
292 muladd(left[i], right[k - i], &r0, &r1, &r2);
293 }
294 result[k] = r0;
295 r0 = r1;
296 r1 = r2;
297 r2 = 0;
298 }
299 result[num_words * 2 - 1] = r0;
300}
301
302void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left,
303 const uECC_word_t *right, const uECC_word_t *mod,
304 wordcount_t num_words)
305{
306 uECC_word_t carry = uECC_vli_add(result, left, right, num_words);
307 if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) {
308 /* result > mod (result = mod + remainder), so subtract mod to get
309 * remainder. */
310 uECC_vli_sub(result, result, mod, num_words);
311 }
312}
313
314void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left,
315 const uECC_word_t *right, const uECC_word_t *mod,
316 wordcount_t num_words)
317{
318 uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words);
319 if (l_borrow) {
320 /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x,
321 * we can get the correct result from result + mod (with overflow). */
322 uECC_vli_add(result, result, mod, num_words);
323 }
324}
325
326/* Computes result = product % mod, where product is 2N words long. */
327/* Currently only designed to work for curve_p or curve_n. */
328void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product,
329 const uECC_word_t *mod, wordcount_t num_words)
330{
331 uECC_word_t mod_multiple[2 * NUM_ECC_WORDS];
332 uECC_word_t tmp[2 * NUM_ECC_WORDS];
333 uECC_word_t *v[2] = {tmp, product};
334 uECC_word_t index;
335
336 /* Shift mod so its highest set bit is at the maximum position. */
337 bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) -
338 uECC_vli_numBits(mod, num_words);
339 wordcount_t word_shift = shift / uECC_WORD_BITS;
340 wordcount_t bit_shift = shift % uECC_WORD_BITS;
341 uECC_word_t carry = 0;
342 uECC_vli_clear(mod_multiple, word_shift);
343 if (bit_shift > 0) {
344 for(index = 0; index < (uECC_word_t)num_words; ++index) {
345 mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry;
346 carry = mod[index] >> (uECC_WORD_BITS - bit_shift);
347 }
348 } else {
349 uECC_vli_set(mod_multiple + word_shift, mod, num_words);
350 }
351
352 for (index = 1; shift >= 0; --shift) {
353 uECC_word_t borrow = 0;
354 wordcount_t i;
355 for (i = 0; i < num_words * 2; ++i) {
356 uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow;
357 if (diff != v[index][i]) {
358 borrow = (diff > v[index][i]);
359 }
360 v[1 - index][i] = diff;
361 }
362 /* Swap the index if there was no borrow */
363 index = !(index ^ borrow);
364 uECC_vli_rshift1(mod_multiple, num_words);
365 mod_multiple[num_words - 1] |= mod_multiple[num_words] <<
366 (uECC_WORD_BITS - 1);
367 uECC_vli_rshift1(mod_multiple + num_words, num_words);
368 }
369 uECC_vli_set(result, v[index], num_words);
370}
371
372void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left,
373 const uECC_word_t *right, const uECC_word_t *mod,
374 wordcount_t num_words)
375{
376 uECC_word_t product[2 * NUM_ECC_WORDS];
377 uECC_vli_mult(product, left, right, num_words);
378 uECC_vli_mmod(result, product, mod, num_words);
379}
380
381void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left,
382 const uECC_word_t *right, uECC_Curve curve)
383{
384 uECC_word_t product[2 * NUM_ECC_WORDS];
385 uECC_vli_mult(product, left, right, curve->num_words);
386
387 curve->mmod_fast(result, product);
388}
389
390static void uECC_vli_modSquare_fast(uECC_word_t *result,
391 const uECC_word_t *left,
392 uECC_Curve curve)
393{
394 uECC_vli_modMult_fast(result, left, left, curve);
395}
396
397
398#define EVEN(vli) (!(vli[0] & 1))
399
400static void vli_modInv_update(uECC_word_t *uv,
401 const uECC_word_t *mod,
402 wordcount_t num_words)
403{
404
405 uECC_word_t carry = 0;
406
407 if (!EVEN(uv)) {
408 carry = uECC_vli_add(uv, uv, mod, num_words);
409 }
410 uECC_vli_rshift1(uv, num_words);
411 if (carry) {
412 uv[num_words - 1] |= HIGH_BIT_SET;
413 }
414}
415
416void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input,
417 const uECC_word_t *mod, wordcount_t num_words)
418{
419 uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS];
420 uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS];
421 cmpresult_t cmpResult;
422
423 if (uECC_vli_isZero(input, num_words)) {
424 uECC_vli_clear(result, num_words);
425 return;
426 }
427
428 uECC_vli_set(a, input, num_words);
429 uECC_vli_set(b, mod, num_words);
430 uECC_vli_clear(u, num_words);
431 u[0] = 1;
432 uECC_vli_clear(v, num_words);
433 while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) {
434 if (EVEN(a)) {
435 uECC_vli_rshift1(a, num_words);
436 vli_modInv_update(u, mod, num_words);
437 } else if (EVEN(b)) {
438 uECC_vli_rshift1(b, num_words);
439 vli_modInv_update(v, mod, num_words);
440 } else if (cmpResult > 0) {
441 uECC_vli_sub(a, a, b, num_words);
442 uECC_vli_rshift1(a, num_words);
443 if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) {
444 uECC_vli_add(u, u, mod, num_words);
445 }
446 uECC_vli_sub(u, u, v, num_words);
447 vli_modInv_update(u, mod, num_words);
448 } else {
449 uECC_vli_sub(b, b, a, num_words);
450 uECC_vli_rshift1(b, num_words);
451 if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) {
452 uECC_vli_add(v, v, mod, num_words);
453 }
454 uECC_vli_sub(v, v, u, num_words);
455 vli_modInv_update(v, mod, num_words);
456 }
457 }
458 uECC_vli_set(result, u, num_words);
459}
460
461/* ------ Point operations ------ */
462
463void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1,
464 uECC_word_t * Z1, uECC_Curve curve)
465{
466 /* t1 = X, t2 = Y, t3 = Z */
467 uECC_word_t t4[NUM_ECC_WORDS];
468 uECC_word_t t5[NUM_ECC_WORDS];
469 wordcount_t num_words = curve->num_words;
470
471 if (uECC_vli_isZero(Z1, num_words)) {
472 return;
473 }
474
475 uECC_vli_modSquare_fast(t4, Y1, curve); /* t4 = y1^2 */
476 uECC_vli_modMult_fast(t5, X1, t4, curve); /* t5 = x1*y1^2 = A */
477 uECC_vli_modSquare_fast(t4, t4, curve); /* t4 = y1^4 */
478 uECC_vli_modMult_fast(Y1, Y1, Z1, curve); /* t2 = y1*z1 = z3 */
479 uECC_vli_modSquare_fast(Z1, Z1, curve); /* t3 = z1^2 */
480
481 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */
482 uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */
483 uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */
484 uECC_vli_modMult_fast(X1, X1, Z1, curve); /* t1 = x1^2 - z1^4 */
485
486 uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */
487 uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */
488 if (uECC_vli_testBit(X1, 0)) {
489 uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p, num_words);
490 uECC_vli_rshift1(X1, num_words);
491 X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1);
492 } else {
493 uECC_vli_rshift1(X1, num_words);
494 }
495
496 /* t1 = 3/2*(x1^2 - z1^4) = B */
497 uECC_vli_modSquare_fast(Z1, X1, curve); /* t3 = B^2 */
498 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */
499 uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */
500 uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */
501 uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = B * (A - x3) */
502 /* t4 = B * (A - x3) - y1^4 = y3: */
503 uECC_vli_modSub(t4, X1, t4, curve->p, num_words);
504
505 uECC_vli_set(X1, Z1, num_words);
506 uECC_vli_set(Z1, Y1, num_words);
507 uECC_vli_set(Y1, t4, num_words);
508}
509
510void x_side_default(uECC_word_t *result,
511 const uECC_word_t *x,
512 uECC_Curve curve)
513{
514 uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */
515 wordcount_t num_words = curve->num_words;
516
517 uECC_vli_modSquare_fast(result, x, curve); /* r = x^2 */
518 uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */
519 uECC_vli_modMult_fast(result, result, x, curve); /* r = x^3 - 3x */
520 /* r = x^3 - 3x + b: */
521 uECC_vli_modAdd(result, result, curve->b, curve->p, num_words);
522}
523
524uECC_Curve uECC_secp256r1(void)
525{
526 return &curve_secp256r1;
527}
528
529void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product)
530{
531 unsigned int tmp[NUM_ECC_WORDS];
532 int carry;
533
534 /* t */
535 uECC_vli_set(result, product, NUM_ECC_WORDS);
536
537 /* s1 */
538 tmp[0] = tmp[1] = tmp[2] = 0;
539 tmp[3] = product[11];
540 tmp[4] = product[12];
541 tmp[5] = product[13];
542 tmp[6] = product[14];
543 tmp[7] = product[15];
544 carry = uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
545 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
546
547 /* s2 */
548 tmp[3] = product[12];
549 tmp[4] = product[13];
550 tmp[5] = product[14];
551 tmp[6] = product[15];
552 tmp[7] = 0;
553 carry += uECC_vli_add(tmp, tmp, tmp, NUM_ECC_WORDS);
554 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
555
556 /* s3 */
557 tmp[0] = product[8];
558 tmp[1] = product[9];
559 tmp[2] = product[10];
560 tmp[3] = tmp[4] = tmp[5] = 0;
561 tmp[6] = product[14];
562 tmp[7] = product[15];
563 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
564
565 /* s4 */
566 tmp[0] = product[9];
567 tmp[1] = product[10];
568 tmp[2] = product[11];
569 tmp[3] = product[13];
570 tmp[4] = product[14];
571 tmp[5] = product[15];
572 tmp[6] = product[13];
573 tmp[7] = product[8];
574 carry += uECC_vli_add(result, result, tmp, NUM_ECC_WORDS);
575
576 /* d1 */
577 tmp[0] = product[11];
578 tmp[1] = product[12];
579 tmp[2] = product[13];
580 tmp[3] = tmp[4] = tmp[5] = 0;
581 tmp[6] = product[8];
582 tmp[7] = product[10];
583 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
584
585 /* d2 */
586 tmp[0] = product[12];
587 tmp[1] = product[13];
588 tmp[2] = product[14];
589 tmp[3] = product[15];
590 tmp[4] = tmp[5] = 0;
591 tmp[6] = product[9];
592 tmp[7] = product[11];
593 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
594
595 /* d3 */
596 tmp[0] = product[13];
597 tmp[1] = product[14];
598 tmp[2] = product[15];
599 tmp[3] = product[8];
600 tmp[4] = product[9];
601 tmp[5] = product[10];
602 tmp[6] = 0;
603 tmp[7] = product[12];
604 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
605
606 /* d4 */
607 tmp[0] = product[14];
608 tmp[1] = product[15];
609 tmp[2] = 0;
610 tmp[3] = product[9];
611 tmp[4] = product[10];
612 tmp[5] = product[11];
613 tmp[6] = 0;
614 tmp[7] = product[13];
615 carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS);
616
617 if (carry < 0) {
618 do {
619 carry += uECC_vli_add(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
620 }
621 while (carry < 0);
622 } else {
623 while (carry ||
624 uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) {
625 carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS);
626 }
627 }
628}
629
630uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve)
631{
632 return uECC_vli_isZero(point, curve->num_words * 2);
633}
634
635void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z,
636 uECC_Curve curve)
637{
638 uECC_word_t t1[NUM_ECC_WORDS];
639
640 uECC_vli_modSquare_fast(t1, Z, curve); /* z^2 */
641 uECC_vli_modMult_fast(X1, X1, t1, curve); /* x1 * z^2 */
642 uECC_vli_modMult_fast(t1, t1, Z, curve); /* z^3 */
643 uECC_vli_modMult_fast(Y1, Y1, t1, curve); /* y1 * z^3 */
644}
645
646/* P = (x1, y1) => 2P, (x2, y2) => P' */
647static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1,
648 uECC_word_t * X2, uECC_word_t * Y2,
649 const uECC_word_t * const initial_Z,
650 uECC_Curve curve)
651{
652 uECC_word_t z[NUM_ECC_WORDS];
653 wordcount_t num_words = curve->num_words;
654 if (initial_Z) {
655 uECC_vli_set(z, initial_Z, num_words);
656 } else {
657 uECC_vli_clear(z, num_words);
658 z[0] = 1;
659 }
660
661 uECC_vli_set(X2, X1, num_words);
662 uECC_vli_set(Y2, Y1, num_words);
663
664 apply_z(X1, Y1, z, curve);
665 curve->double_jacobian(X1, Y1, z, curve);
666 apply_z(X2, Y2, z, curve);
667}
668
669void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1,
670 uECC_word_t * X2, uECC_word_t * Y2,
671 uECC_Curve curve)
672{
673 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
674 uECC_word_t t5[NUM_ECC_WORDS];
675 wordcount_t num_words = curve->num_words;
676
677 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
678 uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
679 uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
680 uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
681 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
682 uECC_vli_modSquare_fast(t5, Y2, curve); /* t5 = (y2 - y1)^2 = D */
683
684 uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */
685 uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */
686 uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */
687 uECC_vli_modMult_fast(Y1, Y1, X2, curve); /* t2 = y1*(C - B) */
688 uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */
689 uECC_vli_modMult_fast(Y2, Y2, X2, curve); /* t4 = (y2 - y1)*(B - x3) */
690 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */
691
692 uECC_vli_set(X2, t5, num_words);
693}
694
695/* Input P = (x1, y1, Z), Q = (x2, y2, Z)
696 Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3)
697 or P => P - Q, Q => P + Q
698 */
699static void XYcZ_addC(uECC_word_t * X1, uECC_word_t * Y1,
700 uECC_word_t * X2, uECC_word_t * Y2,
701 uECC_Curve curve)
702{
703 /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */
704 uECC_word_t t5[NUM_ECC_WORDS];
705 uECC_word_t t6[NUM_ECC_WORDS];
706 uECC_word_t t7[NUM_ECC_WORDS];
707 wordcount_t num_words = curve->num_words;
708
709 uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */
710 uECC_vli_modSquare_fast(t5, t5, curve); /* t5 = (x2 - x1)^2 = A */
711 uECC_vli_modMult_fast(X1, X1, t5, curve); /* t1 = x1*A = B */
712 uECC_vli_modMult_fast(X2, X2, t5, curve); /* t3 = x2*A = C */
713 uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */
714 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */
715
716 uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */
717 uECC_vli_modMult_fast(Y1, Y1, t6, curve); /* t2 = y1 * (C - B) = E */
718 uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */
719 uECC_vli_modSquare_fast(X2, Y2, curve); /* t3 = (y2 - y1)^2 = D */
720 uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */
721
722 uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */
723 uECC_vli_modMult_fast(Y2, Y2, t7, curve); /* t4 = (y2 - y1)*(B - x3) */
724 /* t4 = (y2 - y1)*(B - x3) - E = y3: */
725 uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words);
726
727 uECC_vli_modSquare_fast(t7, t5, curve); /* t7 = (y2 + y1)^2 = F */
728 uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */
729 uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */
730 uECC_vli_modMult_fast(t6, t6, t5, curve); /* t6 = (y2+y1)*(x3' - B) */
731 /* t2 = (y2+y1)*(x3' - B) - E = y3': */
732 uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words);
733
734 uECC_vli_set(X1, t7, num_words);
735}
736
737void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point,
738 const uECC_word_t * scalar,
739 const uECC_word_t * initial_Z,
740 bitcount_t num_bits, uECC_Curve curve)
741{
742 /* R0 and R1 */
743 uECC_word_t Rx[2][NUM_ECC_WORDS];
744 uECC_word_t Ry[2][NUM_ECC_WORDS];
745 uECC_word_t z[NUM_ECC_WORDS];
746 bitcount_t i;
747 uECC_word_t nb;
748 wordcount_t num_words = curve->num_words;
749
750 uECC_vli_set(Rx[1], point, num_words);
751 uECC_vli_set(Ry[1], point + num_words, num_words);
752
753 XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve);
754
755 for (i = num_bits - 2; i > 0; --i) {
756 nb = !uECC_vli_testBit(scalar, i);
757 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
758 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
759 }
760
761 nb = !uECC_vli_testBit(scalar, 0);
762 XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve);
763
764 /* Find final 1/Z value. */
765 uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */
766 uECC_vli_modMult_fast(z, z, Ry[1 - nb], curve); /* Yb * (X1 - X0) */
767 uECC_vli_modMult_fast(z, z, point, curve); /* xP * Yb * (X1 - X0) */
768 uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/
769 /* yP / (xP * Yb * (X1 - X0)) */
770 uECC_vli_modMult_fast(z, z, point + num_words, curve);
771 /* Xb * yP / (xP * Yb * (X1 - X0)) */
772 uECC_vli_modMult_fast(z, z, Rx[1 - nb], curve);
773 /* End 1/Z calculation */
774
775 XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve);
776 apply_z(Rx[0], Ry[0], z, curve);
777
778 uECC_vli_set(result, Rx[0], num_words);
779 uECC_vli_set(result + num_words, Ry[0], num_words);
780}
781
782uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
783 uECC_word_t *k1, uECC_Curve curve)
784{
785
786 wordcount_t num_n_words = BITS_TO_WORDS(curve->num_n_bits);
787
788 bitcount_t num_n_bits = curve->num_n_bits;
789
790 uECC_word_t carry = uECC_vli_add(k0, k, curve->n, num_n_words) ||
791 (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
792 uECC_vli_testBit(k0, num_n_bits));
793
794 uECC_vli_add(k1, k0, curve->n, num_n_words);
795
796 return carry;
797}
798
799uECC_word_t EccPoint_compute_public_key(uECC_word_t *result,
800 uECC_word_t *private_key,
801 uECC_Curve curve)
802{
803
804 uECC_word_t tmp1[NUM_ECC_WORDS];
805 uECC_word_t tmp2[NUM_ECC_WORDS];
806 uECC_word_t *p2[2] = {tmp1, tmp2};
807 uECC_word_t carry;
808
809 /* Regularize the bitcount for the private key so that attackers cannot
810 * use a side channel attack to learn the number of leading zeros. */
811 carry = regularize_k(private_key, tmp1, tmp2, curve);
812
813 EccPoint_mult(result, curve->G, p2[!carry], 0, curve->num_n_bits + 1, curve);
814
815 if (EccPoint_isZero(result, curve)) {
816 return 0;
817 }
818 return 1;
819}
820
821/* Converts an integer in uECC native format to big-endian bytes. */
822void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes,
823 const unsigned int *native)
824{
825 wordcount_t i;
826 for (i = 0; i < num_bytes; ++i) {
827 unsigned b = num_bytes - 1 - i;
828 bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE));
829 }
830}
831
832/* Converts big-endian bytes to an integer in uECC native format. */
833void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes,
834 int num_bytes)
835{
836 wordcount_t i;
837 uECC_vli_clear(native, (num_bytes + (uECC_WORD_SIZE - 1)) / uECC_WORD_SIZE);
838 for (i = 0; i < num_bytes; ++i) {
839 unsigned b = num_bytes - 1 - i;
840 native[b / uECC_WORD_SIZE] |=
841 (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE));
842 }
843}
844
845int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
846 wordcount_t num_words)
847{
848 uECC_word_t mask = (uECC_word_t)-1;
849 uECC_word_t tries;
850 bitcount_t num_bits = uECC_vli_numBits(top, num_words);
851
852 if (!g_rng_function) {
853 return 0;
854 }
855
856 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
857 if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
858 return 0;
859 }
860 random[num_words - 1] &=
861 mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
862 if (!uECC_vli_isZero(random, num_words) &&
863 uECC_vli_cmp(top, random, num_words) == 1) {
864 return 1;
865 }
866 }
867 return 0;
868}
869
870
871int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve)
872{
873 uECC_word_t tmp1[NUM_ECC_WORDS];
874 uECC_word_t tmp2[NUM_ECC_WORDS];
875 wordcount_t num_words = curve->num_words;
876
877 /* The point at infinity is invalid. */
878 if (EccPoint_isZero(point, curve)) {
879 return -1;
880 }
881
882 /* x and y must be smaller than p. */
883 if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 ||
884 uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) {
885 return -2;
886 }
887
888 uECC_vli_modSquare_fast(tmp1, point + num_words, curve);
889 curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */
890
891 /* Make sure that y^2 == x^3 + ax + b */
892 if (uECC_vli_equal(tmp1, tmp2, num_words) != 0)
893 return -3;
894
895 return 0;
896}
897
898int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve)
899{
900
901 uECC_word_t _public[NUM_ECC_WORDS * 2];
902
903 uECC_vli_bytesToNative(_public, public_key, curve->num_bytes);
904 uECC_vli_bytesToNative(
905 _public + curve->num_words,
906 public_key + curve->num_bytes,
907 curve->num_bytes);
908
909 if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) {
910 return -4;
911 }
912
913 return uECC_valid_point(_public, curve);
914}
915
916int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
917 uECC_Curve curve)
918{
919
920 uECC_word_t _private[NUM_ECC_WORDS];
921 uECC_word_t _public[NUM_ECC_WORDS * 2];
922
923 uECC_vli_bytesToNative(
924 _private,
925 private_key,
926 BITS_TO_BYTES(curve->num_n_bits));
927
928 /* Make sure the private key is in the range [1, n-1]. */
929 if (uECC_vli_isZero(_private, BITS_TO_WORDS(curve->num_n_bits))) {
930 return 0;
931 }
932
933 if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) {
934 return 0;
935 }
936
937 /* Compute public key. */
938 if (!EccPoint_compute_public_key(_public, _private, curve)) {
939 return 0;
940 }
941
942 uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public);
943 uECC_vli_nativeToBytes(
944 public_key +
945 curve->num_bytes, curve->num_bytes, _public + curve->num_words);
946 return 1;
947}
Jarno Lamsa46132202019-04-29 14:29:52 +0300948#else
Manuel Pégourié-Gonnardafdc1b52019-05-09 11:24:11 +0200949typedef int mbedtls_dummy_tinycrypt_def;
950#endif /* MBEDTLS_USE_TINYCRYPT */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300951