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