Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1 | /* ecc.c - TinyCrypt implementation of common ECC functions */ |
| 2 | |
| 3 | /* |
Simon Butcher | 92c3d1f | 2019-09-09 17:25:08 +0100 | [diff] [blame] | 4 | * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved. |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
| 8 | /* |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 9 | * 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 Becker | 36ae758 | 2019-07-23 15:52:35 +0100 | [diff] [blame] | 60 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 61 | #include "mbedtls/config.h" |
| 62 | #else |
| 63 | #include MBEDTLS_CONFIG_FILE |
| 64 | #endif |
| 65 | |
Manuel Pégourié-Gonnard | afdc1b5 | 2019-05-09 11:24:11 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 67 | #include <tinycrypt/ecc.h> |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 68 | #include "mbedtls/platform_util.h" |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 69 | #include <string.h> |
| 70 | |
| 71 | /* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform |
| 72 | * has access to enough entropy in order to feed the PRNG regularly. */ |
| 73 | #if default_RNG_defined |
| 74 | static uECC_RNG_Function g_rng_function = &default_CSPRNG; |
| 75 | #else |
| 76 | static uECC_RNG_Function g_rng_function = 0; |
| 77 | #endif |
| 78 | |
| 79 | void uECC_set_rng(uECC_RNG_Function rng_function) |
| 80 | { |
| 81 | g_rng_function = rng_function; |
| 82 | } |
| 83 | |
| 84 | uECC_RNG_Function uECC_get_rng(void) |
| 85 | { |
| 86 | return g_rng_function; |
| 87 | } |
| 88 | |
| 89 | int uECC_curve_private_key_size(uECC_Curve curve) |
| 90 | { |
| 91 | return BITS_TO_BYTES(curve->num_n_bits); |
| 92 | } |
| 93 | |
| 94 | int uECC_curve_public_key_size(uECC_Curve curve) |
| 95 | { |
| 96 | return 2 * curve->num_bytes; |
| 97 | } |
| 98 | |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 99 | void uECC_vli_clear(uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 100 | { |
| 101 | wordcount_t i; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 102 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 103 | vli[i] = 0; |
| 104 | } |
| 105 | } |
| 106 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 107 | uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 108 | { |
| 109 | uECC_word_t bits = 0; |
| 110 | wordcount_t i; |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 111 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 112 | bits |= vli[i]; |
| 113 | } |
| 114 | return (bits == 0); |
| 115 | } |
| 116 | |
| 117 | uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit) |
| 118 | { |
| 119 | return (vli[bit >> uECC_WORD_BITS_SHIFT] & |
| 120 | ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK))); |
| 121 | } |
| 122 | |
| 123 | /* Counts the number of words in vli. */ |
| 124 | static wordcount_t vli_numDigits(const uECC_word_t *vli, |
| 125 | const wordcount_t max_words) |
| 126 | { |
| 127 | |
| 128 | wordcount_t i; |
| 129 | /* Search from the end until we find a non-zero digit. We do it in reverse |
| 130 | * because we expect that most digits will be nonzero. */ |
| 131 | for (i = max_words - 1; i >= 0 && vli[i] == 0; --i) { |
| 132 | } |
| 133 | |
| 134 | return (i + 1); |
| 135 | } |
| 136 | |
| 137 | bitcount_t uECC_vli_numBits(const uECC_word_t *vli, |
| 138 | const wordcount_t max_words) |
| 139 | { |
| 140 | |
| 141 | uECC_word_t i; |
| 142 | uECC_word_t digit; |
| 143 | |
| 144 | wordcount_t num_digits = vli_numDigits(vli, max_words); |
| 145 | if (num_digits == 0) { |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | digit = vli[num_digits - 1]; |
| 150 | for (i = 0; digit; ++i) { |
| 151 | digit >>= 1; |
| 152 | } |
| 153 | |
| 154 | return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); |
| 155 | } |
| 156 | |
| 157 | void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src, |
| 158 | wordcount_t num_words) |
| 159 | { |
| 160 | wordcount_t i; |
| 161 | |
| 162 | for (i = 0; i < num_words; ++i) { |
| 163 | dest[i] = src[i]; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, |
| 168 | const uECC_word_t *right, |
| 169 | wordcount_t num_words) |
| 170 | { |
| 171 | wordcount_t i; |
| 172 | |
| 173 | for (i = num_words - 1; i >= 0; --i) { |
| 174 | if (left[i] > right[i]) { |
| 175 | return 1; |
| 176 | } else if (left[i] < right[i]) { |
| 177 | return -1; |
| 178 | } |
| 179 | } |
| 180 | return 0; |
| 181 | } |
| 182 | |
| 183 | uECC_word_t uECC_vli_equal(const uECC_word_t *left, const uECC_word_t *right, |
| 184 | wordcount_t num_words) |
| 185 | { |
| 186 | |
| 187 | uECC_word_t diff = 0; |
| 188 | wordcount_t i; |
| 189 | |
| 190 | for (i = num_words - 1; i >= 0; --i) { |
| 191 | diff |= (left[i] ^ right[i]); |
| 192 | } |
| 193 | return !(diff == 0); |
| 194 | } |
| 195 | |
| 196 | uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond) |
| 197 | { |
| 198 | return (p_true*(cond)) | (p_false*(!cond)); |
| 199 | } |
| 200 | |
| 201 | /* Computes result = left - right, returning borrow, in constant time. |
| 202 | * Can modify in place. */ |
| 203 | uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, |
| 204 | const uECC_word_t *right, wordcount_t num_words) |
| 205 | { |
| 206 | uECC_word_t borrow = 0; |
| 207 | wordcount_t i; |
| 208 | for (i = 0; i < num_words; ++i) { |
| 209 | uECC_word_t diff = left[i] - right[i] - borrow; |
| 210 | uECC_word_t val = (diff > left[i]); |
| 211 | borrow = cond_set(val, borrow, (diff != left[i])); |
| 212 | |
| 213 | result[i] = diff; |
| 214 | } |
| 215 | return borrow; |
| 216 | } |
| 217 | |
| 218 | /* Computes result = left + right, returning carry, in constant time. |
| 219 | * Can modify in place. */ |
| 220 | static uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 221 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 222 | { |
| 223 | uECC_word_t carry = 0; |
| 224 | wordcount_t i; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 225 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 226 | uECC_word_t sum = left[i] + right[i] + carry; |
| 227 | uECC_word_t val = (sum < left[i]); |
| 228 | carry = cond_set(val, carry, (sum != left[i])); |
| 229 | result[i] = sum; |
| 230 | } |
| 231 | return carry; |
| 232 | } |
| 233 | |
| 234 | cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right, |
| 235 | wordcount_t num_words) |
| 236 | { |
| 237 | uECC_word_t tmp[NUM_ECC_WORDS]; |
| 238 | uECC_word_t neg = !!uECC_vli_sub(tmp, left, right, num_words); |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 239 | uECC_word_t equal = uECC_vli_isZero(tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 240 | return (!equal - 2 * neg); |
| 241 | } |
| 242 | |
| 243 | /* Computes vli = vli >> 1. */ |
| 244 | static void uECC_vli_rshift1(uECC_word_t *vli, wordcount_t num_words) |
| 245 | { |
| 246 | uECC_word_t *end = vli; |
| 247 | uECC_word_t carry = 0; |
| 248 | |
| 249 | vli += num_words; |
| 250 | while (vli-- > end) { |
| 251 | uECC_word_t temp = *vli; |
| 252 | *vli = (temp >> 1) | carry; |
| 253 | carry = temp << (uECC_WORD_BITS - 1); |
| 254 | } |
| 255 | } |
| 256 | |
Manuel Pégourié-Gonnard | 86c4f81 | 2019-10-31 13:02:03 +0100 | [diff] [blame] | 257 | /* Compute a * b + r, where r is a double-word with high-order word r1 and |
| 258 | * low-order word r0, and store the result in the same double-word (r1, r0), |
| 259 | * with the carry bit stored in r2. |
| 260 | * |
| 261 | * (r2, r1, r0) = a * b + (r1, r0): |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 262 | * [in] a, b: operands to be multiplied |
| 263 | * [in] r0, r1: low and high-order words of operand to add |
| 264 | * [out] r0, r1: low and high-order words of the result |
| 265 | * [out] r2: carry |
| 266 | */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 267 | static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, |
| 268 | uECC_word_t *r1, uECC_word_t *r2) |
| 269 | { |
| 270 | |
| 271 | uECC_dword_t p = (uECC_dword_t)a * b; |
| 272 | uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; |
| 273 | r01 += p; |
| 274 | *r2 += (r01 < p); |
| 275 | *r1 = r01 >> uECC_WORD_BITS; |
| 276 | *r0 = (uECC_word_t)r01; |
| 277 | |
| 278 | } |
| 279 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 280 | /* State for implementing random delays in uECC_vli_mult_rnd(). |
| 281 | * |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 282 | * The state is initialized by randomizing delays and setting i = 0. |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 283 | * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i. |
| 284 | * |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 285 | * Randomized vli multiplication is used only for point operations |
| 286 | * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication |
| 287 | * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to |
| 288 | * uECC_vli_mult_rnd() (6 in XYcZ_add_rnd() and 8 in XYcZ_addC_rnd(), |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 289 | * indirectly through uECC_vli_modMult_rnd(). |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 290 | * |
| 291 | * Considering this, in order to minimize the number of calls to the RNG |
| 292 | * (which impact performance) while keeping the size of the structure low, |
| 293 | * make room for 14 randomized vli mults, which corresponds to one step in the |
| 294 | * scalar multiplication routine. |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 295 | */ |
| 296 | typedef struct { |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 297 | uint8_t i; |
| 298 | uint8_t delays[14]; |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 299 | } ecc_wait_state_t; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 300 | |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 301 | /* |
| 302 | * Reset wait_state so that it's ready to be used. |
| 303 | */ |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 304 | void ecc_wait_state_reset(ecc_wait_state_t *ws) |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 305 | { |
| 306 | if (ws == NULL) |
| 307 | return; |
| 308 | |
| 309 | ws->i = 0; |
| 310 | g_rng_function(ws->delays, sizeof(ws->delays)); |
| 311 | } |
| 312 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 313 | /* Computes result = left * right. Result must be 2 * num_words long. |
| 314 | * |
| 315 | * As a counter-measure against horizontal attacks, add noise by performing |
| 316 | * a random number of extra computations performing random additional accesses |
| 317 | * to limbs of the input. |
| 318 | * |
| 319 | * Each of the two actual computation loops is surrounded by two |
| 320 | * similar-looking waiting loops, to make the beginning and end of the actual |
| 321 | * computation harder to spot. |
| 322 | * |
| 323 | * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That |
| 324 | * makes an average of 6 extra calls. Compared to the main computation which |
| 325 | * makes 64 such calls, this represents an average performance degradation of |
| 326 | * less than 10%. |
| 327 | * |
| 328 | * Compared to the original uECC_vli_mult(), loose the num_words argument as we |
| 329 | * know it's always 8. This saves a bit of code size and execution speed. |
| 330 | */ |
| 331 | static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 332 | const uECC_word_t *right, ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 333 | { |
| 334 | |
| 335 | uECC_word_t r0 = 0; |
| 336 | uECC_word_t r1 = 0; |
| 337 | uECC_word_t r2 = 0; |
| 338 | wordcount_t i, k; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 339 | const uint8_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 340 | |
| 341 | /* Fetch 8 bit worth of delay from the state; 0 if we have no state */ |
| 342 | uint8_t delays = s ? s->delays[s->i++] : 0; |
| 343 | uECC_word_t rr0 = 0, rr1 = 0; |
| 344 | volatile uECC_word_t r; |
| 345 | |
| 346 | /* Mimic start of next loop: k in [0, 3] */ |
| 347 | k = 0 + (delays & 0x03); |
| 348 | delays >>= 2; |
| 349 | /* k = 0 -> i in [1, 0] -> 0 extra muladd; |
| 350 | * k = 3 -> i in [1, 3] -> 3 extra muladd */ |
| 351 | for (i = 0; i <= k; ++i) { |
| 352 | muladd(left[i], right[k - i], &rr0, &rr1, &r2); |
| 353 | } |
| 354 | r = rr0; |
| 355 | rr0 = rr1; |
| 356 | rr1 = r2; |
| 357 | r2 = 0; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 358 | |
| 359 | /* Compute each digit of result in sequence, maintaining the carries. */ |
| 360 | for (k = 0; k < num_words; ++k) { |
| 361 | |
| 362 | for (i = 0; i <= k; ++i) { |
| 363 | muladd(left[i], right[k - i], &r0, &r1, &r2); |
| 364 | } |
| 365 | |
| 366 | result[k] = r0; |
| 367 | r0 = r1; |
| 368 | r1 = r2; |
| 369 | r2 = 0; |
| 370 | } |
| 371 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 372 | /* Mimic end of previous loop: k in [4, 7] */ |
| 373 | k = 4 + (delays & 0x03); |
| 374 | delays >>= 2; |
| 375 | /* k = 4 -> i in [5, 4] -> 0 extra muladd; |
| 376 | * k = 7 -> i in [5, 7] -> 3 extra muladd */ |
| 377 | for (i = 5; i <= k; ++i) { |
| 378 | muladd(left[i], right[k - i], &rr0, &rr1, &r2); |
| 379 | } |
| 380 | r = rr0; |
| 381 | rr0 = rr1; |
| 382 | rr1 = r2; |
| 383 | r2 = 0; |
| 384 | |
| 385 | /* Mimic start of next loop: k in [8, 11] */ |
| 386 | k = 11 - (delays & 0x03); |
| 387 | delays >>= 2; |
| 388 | /* k = 8 -> i in [5, 7] -> 3 extra muladd; |
| 389 | * k = 11 -> i in [8, 7] -> 0 extra muladd */ |
| 390 | for (i = (k + 5) - num_words; i < num_words; ++i) { |
| 391 | muladd(left[i], right[k - i], &rr0, &rr1, &r2); |
| 392 | } |
| 393 | r = rr0; |
| 394 | rr0 = rr1; |
| 395 | rr1 = r2; |
| 396 | r2 = 0; |
| 397 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 398 | for (k = num_words; k < num_words * 2 - 1; ++k) { |
| 399 | |
| 400 | for (i = (k + 1) - num_words; i < num_words; ++i) { |
| 401 | muladd(left[i], right[k - i], &r0, &r1, &r2); |
| 402 | } |
| 403 | result[k] = r0; |
| 404 | r0 = r1; |
| 405 | r1 = r2; |
| 406 | r2 = 0; |
| 407 | } |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 408 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 409 | result[num_words * 2 - 1] = r0; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 410 | |
| 411 | /* Mimic end of previous loop: k in [12, 15] */ |
| 412 | k = 15 - (delays & 0x03); |
| 413 | delays >>= 2; |
| 414 | /* k = 12 -> i in [5, 7] -> 3 extra muladd; |
| 415 | * k = 15 -> i in [8, 7] -> 0 extra muladd */ |
| 416 | for (i = (k + 1) - num_words; i < num_words; ++i) { |
| 417 | muladd(left[i], right[k - i], &rr0, &rr1, &r2); |
| 418 | } |
| 419 | r = rr0; |
| 420 | rr0 = rr1; |
| 421 | rr1 = r2; |
| 422 | r2 = 0; |
| 423 | |
| 424 | /* avoid warning that r is set but not used */ |
| 425 | (void) r; |
| 426 | } |
| 427 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 428 | void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left, |
| 429 | const uECC_word_t *right, const uECC_word_t *mod, |
| 430 | wordcount_t num_words) |
| 431 | { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 432 | uECC_word_t carry = uECC_vli_add(result, left, right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 433 | if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) { |
| 434 | /* result > mod (result = mod + remainder), so subtract mod to get |
| 435 | * remainder. */ |
| 436 | uECC_vli_sub(result, result, mod, num_words); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left, |
| 441 | const uECC_word_t *right, const uECC_word_t *mod, |
| 442 | wordcount_t num_words) |
| 443 | { |
| 444 | uECC_word_t l_borrow = uECC_vli_sub(result, left, right, num_words); |
| 445 | if (l_borrow) { |
| 446 | /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x, |
| 447 | * we can get the correct result from result + mod (with overflow). */ |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 448 | uECC_vli_add(result, result, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | /* Computes result = product % mod, where product is 2N words long. */ |
| 453 | /* Currently only designed to work for curve_p or curve_n. */ |
| 454 | void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product, |
| 455 | const uECC_word_t *mod, wordcount_t num_words) |
| 456 | { |
| 457 | uECC_word_t mod_multiple[2 * NUM_ECC_WORDS]; |
| 458 | uECC_word_t tmp[2 * NUM_ECC_WORDS]; |
| 459 | uECC_word_t *v[2] = {tmp, product}; |
| 460 | uECC_word_t index; |
| 461 | |
| 462 | /* Shift mod so its highest set bit is at the maximum position. */ |
| 463 | bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - |
| 464 | uECC_vli_numBits(mod, num_words); |
| 465 | wordcount_t word_shift = shift / uECC_WORD_BITS; |
| 466 | wordcount_t bit_shift = shift % uECC_WORD_BITS; |
| 467 | uECC_word_t carry = 0; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 468 | uECC_vli_clear(mod_multiple); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 469 | if (bit_shift > 0) { |
| 470 | for(index = 0; index < (uECC_word_t)num_words; ++index) { |
| 471 | mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry; |
| 472 | carry = mod[index] >> (uECC_WORD_BITS - bit_shift); |
| 473 | } |
| 474 | } else { |
| 475 | uECC_vli_set(mod_multiple + word_shift, mod, num_words); |
| 476 | } |
| 477 | |
| 478 | for (index = 1; shift >= 0; --shift) { |
| 479 | uECC_word_t borrow = 0; |
| 480 | wordcount_t i; |
| 481 | for (i = 0; i < num_words * 2; ++i) { |
| 482 | uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow; |
| 483 | if (diff != v[index][i]) { |
| 484 | borrow = (diff > v[index][i]); |
| 485 | } |
| 486 | v[1 - index][i] = diff; |
| 487 | } |
| 488 | /* Swap the index if there was no borrow */ |
| 489 | index = !(index ^ borrow); |
| 490 | uECC_vli_rshift1(mod_multiple, num_words); |
| 491 | mod_multiple[num_words - 1] |= mod_multiple[num_words] << |
| 492 | (uECC_WORD_BITS - 1); |
| 493 | uECC_vli_rshift1(mod_multiple + num_words, num_words); |
| 494 | } |
| 495 | uECC_vli_set(result, v[index], num_words); |
| 496 | } |
| 497 | |
| 498 | void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left, |
| 499 | const uECC_word_t *right, const uECC_word_t *mod, |
| 500 | wordcount_t num_words) |
| 501 | { |
| 502 | uECC_word_t product[2 * NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 503 | uECC_vli_mult_rnd(product, left, right, NULL); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 504 | uECC_vli_mmod(result, product, mod, num_words); |
| 505 | } |
| 506 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 507 | static void uECC_vli_modMult_rnd(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 508 | const uECC_word_t *right, ecc_wait_state_t *s) |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 509 | { |
| 510 | uECC_word_t product[2 * NUM_ECC_WORDS]; |
| 511 | uECC_vli_mult_rnd(product, left, right, s); |
| 512 | |
| 513 | vli_mmod_fast_secp256r1(result, product); |
| 514 | } |
| 515 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 516 | void uECC_vli_modMult_fast(uECC_word_t *result, const uECC_word_t *left, |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 517 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 518 | { |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 519 | uECC_vli_modMult_rnd(result, left, right, NULL); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 520 | } |
| 521 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 522 | #define EVEN(vli) (!(vli[0] & 1)) |
| 523 | |
| 524 | static void vli_modInv_update(uECC_word_t *uv, |
| 525 | const uECC_word_t *mod, |
| 526 | wordcount_t num_words) |
| 527 | { |
| 528 | |
| 529 | uECC_word_t carry = 0; |
| 530 | |
| 531 | if (!EVEN(uv)) { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 532 | carry = uECC_vli_add(uv, uv, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 533 | } |
| 534 | uECC_vli_rshift1(uv, num_words); |
| 535 | if (carry) { |
| 536 | uv[num_words - 1] |= HIGH_BIT_SET; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, |
| 541 | const uECC_word_t *mod, wordcount_t num_words) |
| 542 | { |
| 543 | uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS]; |
| 544 | uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS]; |
| 545 | cmpresult_t cmpResult; |
| 546 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 547 | if (uECC_vli_isZero(input)) { |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 548 | uECC_vli_clear(result); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 549 | return; |
| 550 | } |
| 551 | |
| 552 | uECC_vli_set(a, input, num_words); |
| 553 | uECC_vli_set(b, mod, num_words); |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 554 | uECC_vli_clear(u); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 555 | u[0] = 1; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 556 | uECC_vli_clear(v); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 557 | while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) { |
| 558 | if (EVEN(a)) { |
| 559 | uECC_vli_rshift1(a, num_words); |
| 560 | vli_modInv_update(u, mod, num_words); |
| 561 | } else if (EVEN(b)) { |
| 562 | uECC_vli_rshift1(b, num_words); |
| 563 | vli_modInv_update(v, mod, num_words); |
| 564 | } else if (cmpResult > 0) { |
| 565 | uECC_vli_sub(a, a, b, num_words); |
| 566 | uECC_vli_rshift1(a, num_words); |
| 567 | if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 568 | uECC_vli_add(u, u, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 569 | } |
| 570 | uECC_vli_sub(u, u, v, num_words); |
| 571 | vli_modInv_update(u, mod, num_words); |
| 572 | } else { |
| 573 | uECC_vli_sub(b, b, a, num_words); |
| 574 | uECC_vli_rshift1(b, num_words); |
| 575 | if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 576 | uECC_vli_add(v, v, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 577 | } |
| 578 | uECC_vli_sub(v, v, u, num_words); |
| 579 | vli_modInv_update(v, mod, num_words); |
| 580 | } |
| 581 | } |
| 582 | uECC_vli_set(result, u, num_words); |
| 583 | } |
| 584 | |
| 585 | /* ------ Point operations ------ */ |
| 586 | |
| 587 | void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1, |
| 588 | uECC_word_t * Z1, uECC_Curve curve) |
| 589 | { |
| 590 | /* t1 = X, t2 = Y, t3 = Z */ |
| 591 | uECC_word_t t4[NUM_ECC_WORDS]; |
| 592 | uECC_word_t t5[NUM_ECC_WORDS]; |
| 593 | wordcount_t num_words = curve->num_words; |
| 594 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 595 | if (uECC_vli_isZero(Z1)) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 596 | return; |
| 597 | } |
| 598 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 599 | uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */ |
| 600 | uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ |
| 601 | uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */ |
| 602 | uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ |
| 603 | uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 604 | |
| 605 | uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = x1 + z1^2 */ |
| 606 | uECC_vli_modAdd(Z1, Z1, Z1, curve->p, num_words); /* t3 = 2*z1^2 */ |
| 607 | uECC_vli_modSub(Z1, X1, Z1, curve->p, num_words); /* t3 = x1 - z1^2 */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 608 | uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 609 | |
| 610 | uECC_vli_modAdd(Z1, X1, X1, curve->p, num_words); /* t3 = 2*(x1^2 - z1^4) */ |
| 611 | uECC_vli_modAdd(X1, X1, Z1, curve->p, num_words); /* t1 = 3*(x1^2 - z1^4) */ |
| 612 | if (uECC_vli_testBit(X1, 0)) { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 613 | uECC_word_t l_carry = uECC_vli_add(X1, X1, curve->p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 614 | uECC_vli_rshift1(X1, num_words); |
| 615 | X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1); |
| 616 | } else { |
| 617 | uECC_vli_rshift1(X1, num_words); |
| 618 | } |
| 619 | |
| 620 | /* t1 = 3/2*(x1^2 - z1^4) = B */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 621 | uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 622 | uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - A */ |
| 623 | uECC_vli_modSub(Z1, Z1, t5, curve->p, num_words); /* t3 = B^2 - 2A = x3 */ |
| 624 | uECC_vli_modSub(t5, t5, Z1, curve->p, num_words); /* t5 = A - x3 */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 625 | uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 626 | /* t4 = B * (A - x3) - y1^4 = y3: */ |
| 627 | uECC_vli_modSub(t4, X1, t4, curve->p, num_words); |
| 628 | |
| 629 | uECC_vli_set(X1, Z1, num_words); |
| 630 | uECC_vli_set(Z1, Y1, num_words); |
| 631 | uECC_vli_set(Y1, t4, num_words); |
| 632 | } |
| 633 | |
| 634 | void x_side_default(uECC_word_t *result, |
| 635 | const uECC_word_t *x, |
| 636 | uECC_Curve curve) |
| 637 | { |
| 638 | uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */ |
| 639 | wordcount_t num_words = curve->num_words; |
| 640 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 641 | uECC_vli_modMult_fast(result, x, x); /* r = x^2 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 642 | uECC_vli_modSub(result, result, _3, curve->p, num_words); /* r = x^2 - 3 */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 643 | uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 644 | /* r = x^3 - 3x + b: */ |
| 645 | uECC_vli_modAdd(result, result, curve->b, curve->p, num_words); |
| 646 | } |
| 647 | |
| 648 | uECC_Curve uECC_secp256r1(void) |
| 649 | { |
| 650 | return &curve_secp256r1; |
| 651 | } |
| 652 | |
| 653 | void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product) |
| 654 | { |
| 655 | unsigned int tmp[NUM_ECC_WORDS]; |
| 656 | int carry; |
| 657 | |
| 658 | /* t */ |
| 659 | uECC_vli_set(result, product, NUM_ECC_WORDS); |
| 660 | |
| 661 | /* s1 */ |
| 662 | tmp[0] = tmp[1] = tmp[2] = 0; |
| 663 | tmp[3] = product[11]; |
| 664 | tmp[4] = product[12]; |
| 665 | tmp[5] = product[13]; |
| 666 | tmp[6] = product[14]; |
| 667 | tmp[7] = product[15]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 668 | carry = uECC_vli_add(tmp, tmp, tmp); |
| 669 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 670 | |
| 671 | /* s2 */ |
| 672 | tmp[3] = product[12]; |
| 673 | tmp[4] = product[13]; |
| 674 | tmp[5] = product[14]; |
| 675 | tmp[6] = product[15]; |
| 676 | tmp[7] = 0; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 677 | carry += uECC_vli_add(tmp, tmp, tmp); |
| 678 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 679 | |
| 680 | /* s3 */ |
| 681 | tmp[0] = product[8]; |
| 682 | tmp[1] = product[9]; |
| 683 | tmp[2] = product[10]; |
| 684 | tmp[3] = tmp[4] = tmp[5] = 0; |
| 685 | tmp[6] = product[14]; |
| 686 | tmp[7] = product[15]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 687 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 688 | |
| 689 | /* s4 */ |
| 690 | tmp[0] = product[9]; |
| 691 | tmp[1] = product[10]; |
| 692 | tmp[2] = product[11]; |
| 693 | tmp[3] = product[13]; |
| 694 | tmp[4] = product[14]; |
| 695 | tmp[5] = product[15]; |
| 696 | tmp[6] = product[13]; |
| 697 | tmp[7] = product[8]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 698 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 699 | |
| 700 | /* d1 */ |
| 701 | tmp[0] = product[11]; |
| 702 | tmp[1] = product[12]; |
| 703 | tmp[2] = product[13]; |
| 704 | tmp[3] = tmp[4] = tmp[5] = 0; |
| 705 | tmp[6] = product[8]; |
| 706 | tmp[7] = product[10]; |
| 707 | carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS); |
| 708 | |
| 709 | /* d2 */ |
| 710 | tmp[0] = product[12]; |
| 711 | tmp[1] = product[13]; |
| 712 | tmp[2] = product[14]; |
| 713 | tmp[3] = product[15]; |
| 714 | tmp[4] = tmp[5] = 0; |
| 715 | tmp[6] = product[9]; |
| 716 | tmp[7] = product[11]; |
| 717 | carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS); |
| 718 | |
| 719 | /* d3 */ |
| 720 | tmp[0] = product[13]; |
| 721 | tmp[1] = product[14]; |
| 722 | tmp[2] = product[15]; |
| 723 | tmp[3] = product[8]; |
| 724 | tmp[4] = product[9]; |
| 725 | tmp[5] = product[10]; |
| 726 | tmp[6] = 0; |
| 727 | tmp[7] = product[12]; |
| 728 | carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS); |
| 729 | |
| 730 | /* d4 */ |
| 731 | tmp[0] = product[14]; |
| 732 | tmp[1] = product[15]; |
| 733 | tmp[2] = 0; |
| 734 | tmp[3] = product[9]; |
| 735 | tmp[4] = product[10]; |
| 736 | tmp[5] = product[11]; |
| 737 | tmp[6] = 0; |
| 738 | tmp[7] = product[13]; |
| 739 | carry -= uECC_vli_sub(result, result, tmp, NUM_ECC_WORDS); |
| 740 | |
| 741 | if (carry < 0) { |
| 742 | do { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 743 | carry += uECC_vli_add(result, result, curve_secp256r1.p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 744 | } |
| 745 | while (carry < 0); |
| 746 | } else { |
| 747 | while (carry || |
| 748 | uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) { |
| 749 | carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS); |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | |
| 754 | uECC_word_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve) |
| 755 | { |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 756 | (void) curve; |
| 757 | return uECC_vli_isZero(point); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 758 | } |
| 759 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 760 | void apply_z(uECC_word_t * X1, uECC_word_t * Y1, const uECC_word_t * const Z) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 761 | { |
| 762 | uECC_word_t t1[NUM_ECC_WORDS]; |
| 763 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 764 | uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */ |
| 765 | uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ |
| 766 | uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */ |
| 767 | uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | /* P = (x1, y1) => 2P, (x2, y2) => P' */ |
| 771 | static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1, |
| 772 | uECC_word_t * X2, uECC_word_t * Y2, |
| 773 | const uECC_word_t * const initial_Z, |
| 774 | uECC_Curve curve) |
| 775 | { |
| 776 | uECC_word_t z[NUM_ECC_WORDS]; |
| 777 | wordcount_t num_words = curve->num_words; |
| 778 | if (initial_Z) { |
| 779 | uECC_vli_set(z, initial_Z, num_words); |
| 780 | } else { |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 781 | uECC_vli_clear(z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 782 | z[0] = 1; |
| 783 | } |
| 784 | |
| 785 | uECC_vli_set(X2, X1, num_words); |
| 786 | uECC_vli_set(Y2, Y1, num_words); |
| 787 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 788 | apply_z(X1, Y1, z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 789 | curve->double_jacobian(X1, Y1, z, curve); |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 790 | apply_z(X2, Y2, z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 791 | } |
| 792 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 793 | static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1, |
| 794 | uECC_word_t * X2, uECC_word_t * Y2, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 795 | ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 796 | { |
| 797 | /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ |
| 798 | uECC_word_t t5[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 799 | const uECC_Curve curve = &curve_secp256r1; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 800 | const wordcount_t num_words = NUM_ECC_WORDS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 801 | |
| 802 | uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */ |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 803 | uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 804 | uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */ |
| 805 | uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 806 | uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */ |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 807 | uECC_vli_modMult_rnd(t5, Y2, Y2, s); /* t5 = (y2 - y1)^2 = D */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 808 | |
| 809 | uECC_vli_modSub(t5, t5, X1, curve->p, num_words); /* t5 = D - B */ |
| 810 | uECC_vli_modSub(t5, t5, X2, curve->p, num_words); /* t5 = D - B - C = x3 */ |
| 811 | uECC_vli_modSub(X2, X2, X1, curve->p, num_words); /* t3 = C - B */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 812 | uECC_vli_modMult_rnd(Y1, Y1, X2, s); /* t2 = y1*(C - B) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 813 | uECC_vli_modSub(X2, X1, t5, curve->p, num_words); /* t3 = B - x3 */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 814 | uECC_vli_modMult_rnd(Y2, Y2, X2, s); /* t4 = (y2 - y1)*(B - x3) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 815 | uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y3 */ |
| 816 | |
| 817 | uECC_vli_set(X2, t5, num_words); |
| 818 | } |
| 819 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 820 | void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, |
| 821 | uECC_word_t * X2, uECC_word_t * Y2, |
| 822 | uECC_Curve curve) |
| 823 | { |
| 824 | (void) curve; |
| 825 | XYcZ_add_rnd(X1, Y1, X2, Y2, NULL); |
| 826 | } |
| 827 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 828 | /* Input P = (x1, y1, Z), Q = (x2, y2, Z) |
| 829 | Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) |
| 830 | or P => P - Q, Q => P + Q |
| 831 | */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 832 | static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1, |
| 833 | uECC_word_t * X2, uECC_word_t * Y2, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 834 | ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 835 | { |
| 836 | /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ |
| 837 | uECC_word_t t5[NUM_ECC_WORDS]; |
| 838 | uECC_word_t t6[NUM_ECC_WORDS]; |
| 839 | uECC_word_t t7[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 840 | const uECC_Curve curve = &curve_secp256r1; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 841 | const wordcount_t num_words = NUM_ECC_WORDS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 842 | |
| 843 | uECC_vli_modSub(t5, X2, X1, curve->p, num_words); /* t5 = x2 - x1 */ |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 844 | uECC_vli_modMult_rnd(t5, t5, t5, s); /* t5 = (x2 - x1)^2 = A */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 845 | uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */ |
| 846 | uECC_vli_modMult_rnd(X2, X2, t5, s); /* t3 = x2*A = C */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 847 | uECC_vli_modAdd(t5, Y2, Y1, curve->p, num_words); /* t5 = y2 + y1 */ |
| 848 | uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); /* t4 = y2 - y1 */ |
| 849 | |
| 850 | uECC_vli_modSub(t6, X2, X1, curve->p, num_words); /* t6 = C - B */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 851 | uECC_vli_modMult_rnd(Y1, Y1, t6, s); /* t2 = y1 * (C - B) = E */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 852 | uECC_vli_modAdd(t6, X1, X2, curve->p, num_words); /* t6 = B + C */ |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 853 | uECC_vli_modMult_rnd(X2, Y2, Y2, s); /* t3 = (y2 - y1)^2 = D */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 854 | uECC_vli_modSub(X2, X2, t6, curve->p, num_words); /* t3 = D - (B + C) = x3 */ |
| 855 | |
| 856 | uECC_vli_modSub(t7, X1, X2, curve->p, num_words); /* t7 = B - x3 */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 857 | uECC_vli_modMult_rnd(Y2, Y2, t7, s); /* t4 = (y2 - y1)*(B - x3) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 858 | /* t4 = (y2 - y1)*(B - x3) - E = y3: */ |
| 859 | uECC_vli_modSub(Y2, Y2, Y1, curve->p, num_words); |
| 860 | |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 861 | uECC_vli_modMult_rnd(t7, t5, t5, s); /* t7 = (y2 + y1)^2 = F */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 862 | uECC_vli_modSub(t7, t7, t6, curve->p, num_words); /* t7 = F - (B + C) = x3' */ |
| 863 | uECC_vli_modSub(t6, t7, X1, curve->p, num_words); /* t6 = x3' - B */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 864 | uECC_vli_modMult_rnd(t6, t6, t5, s); /* t6 = (y2+y1)*(x3' - B) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 865 | /* t2 = (y2+y1)*(x3' - B) - E = y3': */ |
| 866 | uECC_vli_modSub(Y1, t6, Y1, curve->p, num_words); |
| 867 | |
| 868 | uECC_vli_set(X1, t7, num_words); |
| 869 | } |
| 870 | |
Manuel Pégourié-Gonnard | 27926d6 | 2019-11-04 11:26:46 +0100 | [diff] [blame] | 871 | static void EccPoint_mult(uECC_word_t * result, const uECC_word_t * point, |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 872 | const uECC_word_t * scalar, |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 873 | const uECC_word_t * initial_Z) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 874 | { |
| 875 | /* R0 and R1 */ |
| 876 | uECC_word_t Rx[2][NUM_ECC_WORDS]; |
| 877 | uECC_word_t Ry[2][NUM_ECC_WORDS]; |
| 878 | uECC_word_t z[NUM_ECC_WORDS]; |
| 879 | bitcount_t i; |
| 880 | uECC_word_t nb; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 881 | const wordcount_t num_words = NUM_ECC_WORDS; |
| 882 | const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */ |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 883 | const uECC_Curve curve = uECC_secp256r1(); |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 884 | ecc_wait_state_t wait_state; |
| 885 | ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 886 | |
| 887 | uECC_vli_set(Rx[1], point, num_words); |
| 888 | uECC_vli_set(Ry[1], point + num_words, num_words); |
| 889 | |
| 890 | XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve); |
| 891 | |
| 892 | for (i = num_bits - 2; i > 0; --i) { |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 893 | ecc_wait_state_reset(ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 894 | nb = !uECC_vli_testBit(scalar, i); |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 895 | XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws); |
| 896 | XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 897 | } |
| 898 | |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 899 | ecc_wait_state_reset(ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 900 | nb = !uECC_vli_testBit(scalar, 0); |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 901 | XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 902 | |
| 903 | /* Find final 1/Z value. */ |
| 904 | uECC_vli_modSub(z, Rx[1], Rx[0], curve->p, num_words); /* X1 - X0 */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 905 | uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */ |
| 906 | uECC_vli_modMult_fast(z, z, point); /* xP * Yb * (X1 - X0) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 907 | uECC_vli_modInv(z, z, curve->p, num_words); /* 1 / (xP * Yb * (X1 - X0))*/ |
| 908 | /* yP / (xP * Yb * (X1 - X0)) */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 909 | uECC_vli_modMult_fast(z, z, point + num_words); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 910 | /* Xb * yP / (xP * Yb * (X1 - X0)) */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 911 | uECC_vli_modMult_fast(z, z, Rx[1 - nb]); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 912 | /* End 1/Z calculation */ |
| 913 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 914 | XYcZ_add_rnd(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], ws); |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 915 | apply_z(Rx[0], Ry[0], z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 916 | |
| 917 | uECC_vli_set(result, Rx[0], num_words); |
| 918 | uECC_vli_set(result + num_words, Ry[0], num_words); |
| 919 | } |
| 920 | |
Manuel Pégourié-Gonnard | 27926d6 | 2019-11-04 11:26:46 +0100 | [diff] [blame] | 921 | static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0, |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 922 | uECC_word_t *k1) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 923 | { |
| 924 | |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 925 | wordcount_t num_n_words = NUM_ECC_WORDS; |
| 926 | bitcount_t num_n_bits = NUM_ECC_BITS; |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 927 | const uECC_Curve curve = uECC_secp256r1(); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 928 | |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 929 | uECC_word_t carry = uECC_vli_add(k0, k, curve->n) || |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 930 | (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) && |
| 931 | uECC_vli_testBit(k0, num_n_bits)); |
| 932 | |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 933 | uECC_vli_add(k1, k0, curve->n); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 934 | |
| 935 | return carry; |
| 936 | } |
| 937 | |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 938 | int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point, |
| 939 | const uECC_word_t * scalar, uECC_Curve curve) |
| 940 | { |
| 941 | uECC_word_t tmp[NUM_ECC_WORDS]; |
| 942 | uECC_word_t s[NUM_ECC_WORDS]; |
| 943 | uECC_word_t *k2[2] = {tmp, s}; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 944 | wordcount_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 945 | uECC_word_t carry; |
| 946 | uECC_word_t *initial_Z = 0; |
| 947 | int r; |
| 948 | |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 949 | if (curve != uECC_secp256r1()) |
| 950 | return 0; |
| 951 | |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 952 | /* Regularize the bitcount for the private key so that attackers cannot use a |
| 953 | * side channel attack to learn the number of leading zeros. */ |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 954 | carry = regularize_k(scalar, tmp, s); |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 955 | |
| 956 | /* If an RNG function was specified, get a random initial Z value to |
| 957 | * protect against side-channel attacks such as Template SPA */ |
| 958 | if (g_rng_function) { |
| 959 | if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) { |
| 960 | r = 0; |
| 961 | goto clear_and_out; |
| 962 | } |
| 963 | initial_Z = k2[carry]; |
| 964 | } |
| 965 | |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 966 | EccPoint_mult(result, point, k2[!carry], initial_Z); |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 967 | r = 1; |
| 968 | |
| 969 | clear_and_out: |
| 970 | /* erasing temporary buffer used to store secret: */ |
| 971 | mbedtls_platform_zeroize(k2, sizeof(k2)); |
| 972 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 973 | mbedtls_platform_zeroize(s, sizeof(s)); |
| 974 | |
| 975 | return r; |
| 976 | } |
| 977 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 978 | uECC_word_t EccPoint_compute_public_key(uECC_word_t *result, |
| 979 | uECC_word_t *private_key, |
| 980 | uECC_Curve curve) |
| 981 | { |
| 982 | |
| 983 | uECC_word_t tmp1[NUM_ECC_WORDS]; |
| 984 | uECC_word_t tmp2[NUM_ECC_WORDS]; |
| 985 | uECC_word_t *p2[2] = {tmp1, tmp2}; |
| 986 | uECC_word_t carry; |
| 987 | |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 988 | if (curve != uECC_secp256r1()) |
| 989 | return 0; |
| 990 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 991 | /* Regularize the bitcount for the private key so that attackers cannot |
| 992 | * use a side channel attack to learn the number of leading zeros. */ |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 993 | carry = regularize_k(private_key, tmp1, tmp2); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 994 | |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 995 | EccPoint_mult(result, curve->G, p2[!carry], 0); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 996 | |
| 997 | if (EccPoint_isZero(result, curve)) { |
| 998 | return 0; |
| 999 | } |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
| 1003 | /* Converts an integer in uECC native format to big-endian bytes. */ |
| 1004 | void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes, |
| 1005 | const unsigned int *native) |
| 1006 | { |
| 1007 | wordcount_t i; |
| 1008 | for (i = 0; i < num_bytes; ++i) { |
| 1009 | unsigned b = num_bytes - 1 - i; |
| 1010 | bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE)); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | /* Converts big-endian bytes to an integer in uECC native format. */ |
| 1015 | void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes, |
| 1016 | int num_bytes) |
| 1017 | { |
| 1018 | wordcount_t i; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame^] | 1019 | uECC_vli_clear(native); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1020 | for (i = 0; i < num_bytes; ++i) { |
| 1021 | unsigned b = num_bytes - 1 - i; |
| 1022 | native[b / uECC_WORD_SIZE] |= |
| 1023 | (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE)); |
| 1024 | } |
| 1025 | } |
| 1026 | |
| 1027 | int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top, |
| 1028 | wordcount_t num_words) |
| 1029 | { |
| 1030 | uECC_word_t mask = (uECC_word_t)-1; |
| 1031 | uECC_word_t tries; |
| 1032 | bitcount_t num_bits = uECC_vli_numBits(top, num_words); |
| 1033 | |
| 1034 | if (!g_rng_function) { |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
| 1039 | if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { |
| 1040 | return 0; |
| 1041 | } |
| 1042 | random[num_words - 1] &= |
| 1043 | mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits)); |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 1044 | if (!uECC_vli_isZero(random) && |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1045 | uECC_vli_cmp(top, random, num_words) == 1) { |
| 1046 | return 1; |
| 1047 | } |
| 1048 | } |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve) |
| 1054 | { |
| 1055 | uECC_word_t tmp1[NUM_ECC_WORDS]; |
| 1056 | uECC_word_t tmp2[NUM_ECC_WORDS]; |
| 1057 | wordcount_t num_words = curve->num_words; |
| 1058 | |
| 1059 | /* The point at infinity is invalid. */ |
| 1060 | if (EccPoint_isZero(point, curve)) { |
| 1061 | return -1; |
| 1062 | } |
| 1063 | |
| 1064 | /* x and y must be smaller than p. */ |
| 1065 | if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 || |
| 1066 | uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) { |
| 1067 | return -2; |
| 1068 | } |
| 1069 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1070 | uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1071 | curve->x_side(tmp2, point, curve); /* tmp2 = x^3 + ax + b */ |
| 1072 | |
| 1073 | /* Make sure that y^2 == x^3 + ax + b */ |
| 1074 | if (uECC_vli_equal(tmp1, tmp2, num_words) != 0) |
| 1075 | return -3; |
| 1076 | |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve) |
| 1081 | { |
| 1082 | |
| 1083 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 1084 | |
| 1085 | uECC_vli_bytesToNative(_public, public_key, curve->num_bytes); |
| 1086 | uECC_vli_bytesToNative( |
| 1087 | _public + curve->num_words, |
| 1088 | public_key + curve->num_bytes, |
| 1089 | curve->num_bytes); |
| 1090 | |
| 1091 | if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) { |
| 1092 | return -4; |
| 1093 | } |
| 1094 | |
| 1095 | return uECC_valid_point(_public, curve); |
| 1096 | } |
| 1097 | |
| 1098 | int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, |
| 1099 | uECC_Curve curve) |
| 1100 | { |
| 1101 | |
| 1102 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 1103 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 1104 | |
| 1105 | uECC_vli_bytesToNative( |
| 1106 | _private, |
| 1107 | private_key, |
| 1108 | BITS_TO_BYTES(curve->num_n_bits)); |
| 1109 | |
| 1110 | /* Make sure the private key is in the range [1, n-1]. */ |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 1111 | if (uECC_vli_isZero(_private)) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | if (uECC_vli_cmp(curve->n, _private, BITS_TO_WORDS(curve->num_n_bits)) != 1) { |
| 1116 | return 0; |
| 1117 | } |
| 1118 | |
| 1119 | /* Compute public key. */ |
| 1120 | if (!EccPoint_compute_public_key(_public, _private, curve)) { |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | uECC_vli_nativeToBytes(public_key, curve->num_bytes, _public); |
| 1125 | uECC_vli_nativeToBytes( |
| 1126 | public_key + |
| 1127 | curve->num_bytes, curve->num_bytes, _public + curve->num_words); |
| 1128 | return 1; |
| 1129 | } |
Jarno Lamsa | 4613220 | 2019-04-29 14:29:52 +0300 | [diff] [blame] | 1130 | #else |
Manuel Pégourié-Gonnard | afdc1b5 | 2019-05-09 11:24:11 +0200 | [diff] [blame] | 1131 | typedef int mbedtls_dummy_tinycrypt_def; |
| 1132 | #endif /* MBEDTLS_USE_TINYCRYPT */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1133 | |