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