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 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 36 | * - Redistributions of source code must retain the above copyright notice, |
| 37 | * this list of conditions and the following disclaimer. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 38 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 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. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 42 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 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. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 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 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 66 | #include <tinycrypt/ecc.h> |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 67 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 68 | #include "mbedtls/sha256.h" |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 69 | #include <string.h> |
Shelly Liberman | 05beb9a | 2020-09-13 15:23:56 +0300 | [diff] [blame] | 70 | #include "mbedtls/platform_util.h" |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 71 | |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 72 | #ifdef __CC_ARM |
| 73 | #pragma diag_suppress 667 // strict diagnostic: "asm" function is nonstandard |
| 74 | #endif |
| 75 | |
| 76 | #if defined MBEDTLS_HAVE_ASM |
| 77 | #ifndef asm |
| 78 | #define asm __asm |
| 79 | #endif |
| 80 | #endif |
| 81 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 82 | /* Parameters for curve NIST P-256 aka secp256r1 */ |
| 83 | const uECC_word_t curve_p[NUM_ECC_WORDS] = { |
| 84 | BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), |
| 85 | BYTES_TO_WORDS_8(FF, FF, FF, FF, 00, 00, 00, 00), |
| 86 | BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00), |
| 87 | BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF) |
| 88 | }; |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 89 | const uECC_word_t curve_n[NUM_ECC_WORDS] = { |
| 90 | BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3), |
| 91 | BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC), |
| 92 | BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), |
| 93 | BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF) |
| 94 | }; |
Manuel Pégourié-Gonnard | a611508 | 2019-11-21 10:29:14 +0100 | [diff] [blame] | 95 | const uECC_word_t curve_G[2 * NUM_ECC_WORDS] = { |
| 96 | BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4), |
| 97 | BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77), |
| 98 | BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8), |
| 99 | BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B), |
| 100 | BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB), |
| 101 | BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B), |
| 102 | BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E), |
| 103 | BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F) |
| 104 | }; |
Manuel Pégourié-Gonnard | ffd1399 | 2019-11-21 10:39:06 +0100 | [diff] [blame] | 105 | const uECC_word_t curve_b[NUM_ECC_WORDS] = { |
| 106 | BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B), |
| 107 | BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65), |
| 108 | BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3), |
| 109 | BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A) |
| 110 | }; |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 111 | |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 112 | static int uECC_update_param_sha256(mbedtls_sha256_context *ctx, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 113 | const uECC_word_t val[NUM_ECC_WORDS]) |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 114 | { |
| 115 | uint8_t bytes[NUM_ECC_BYTES]; |
| 116 | |
| 117 | uECC_vli_nativeToBytes(bytes, NUM_ECC_BYTES, val); |
| 118 | return mbedtls_sha256_update_ret(ctx, bytes, NUM_ECC_BYTES); |
| 119 | } |
| 120 | |
| 121 | static int uECC_compute_param_sha256(unsigned char output[32]) |
| 122 | { |
| 123 | int ret = UECC_FAILURE; |
| 124 | mbedtls_sha256_context ctx; |
| 125 | |
| 126 | mbedtls_sha256_init( &ctx ); |
| 127 | |
| 128 | if (mbedtls_sha256_starts_ret(&ctx, 0) != 0) { |
| 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | if (uECC_update_param_sha256(&ctx, curve_p) != 0 || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 133 | uECC_update_param_sha256(&ctx, curve_n) != 0 || |
| 134 | uECC_update_param_sha256(&ctx, curve_G) != 0 || |
| 135 | uECC_update_param_sha256(&ctx, curve_G + NUM_ECC_WORDS) != 0 || |
| 136 | uECC_update_param_sha256(&ctx, curve_b) != 0) |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 137 | { |
| 138 | goto exit; |
| 139 | } |
| 140 | |
| 141 | if (mbedtls_sha256_finish_ret(&ctx, output) != 0) { |
| 142 | goto exit; |
| 143 | } |
| 144 | |
| 145 | ret = UECC_SUCCESS; |
| 146 | |
| 147 | exit: |
| 148 | mbedtls_sha256_free( &ctx ); |
| 149 | |
| 150 | return ret; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * Check integrity of curve parameters. |
| 155 | * Return 0 if everything's OK, non-zero otherwise. |
| 156 | */ |
| 157 | static int uECC_check_curve_integrity(void) |
| 158 | { |
| 159 | unsigned char computed[32]; |
Manuel Pégourié-Gonnard | e1cb884 | 2019-11-28 12:21:34 +0100 | [diff] [blame] | 160 | static const unsigned char reference[32] = { |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 161 | 0x2d, 0xa1, 0xa4, 0x64, 0x45, 0x28, 0x0d, 0xe1, |
| 162 | 0x93, 0xf9, 0x29, 0x2f, 0xac, 0x3e, 0xe2, 0x92, |
| 163 | 0x76, 0x0a, 0xe2, 0xbc, 0xce, 0x2a, 0xa2, 0xc6, |
| 164 | 0x38, 0xf2, 0x19, 0x1d, 0x76, 0x72, 0x93, 0x49, |
| 165 | }; |
Manuel Pégourié-Gonnard | 645896e | 2019-12-05 15:30:09 +0100 | [diff] [blame] | 166 | unsigned char diff = 0; |
| 167 | unsigned char tmp1, tmp2; |
Manuel Pégourié-Gonnard | e1cb884 | 2019-11-28 12:21:34 +0100 | [diff] [blame] | 168 | volatile unsigned i; |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 169 | |
| 170 | if (uECC_compute_param_sha256(computed) != UECC_SUCCESS) { |
| 171 | return UECC_FAILURE; |
| 172 | } |
| 173 | |
Manuel Pégourié-Gonnard | 645896e | 2019-12-05 15:30:09 +0100 | [diff] [blame] | 174 | for (i = 0; i < 32; i++) { |
| 175 | /* make sure the order of volatile accesses is well-defined */ |
| 176 | tmp1 = computed[i]; |
| 177 | tmp2 = reference[i]; |
| 178 | diff |= tmp1 ^ tmp2; |
| 179 | } |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 180 | |
Manuel Pégourié-Gonnard | e1cb884 | 2019-11-28 12:21:34 +0100 | [diff] [blame] | 181 | /* i should be 32 */ |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 182 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | e1cb884 | 2019-11-28 12:21:34 +0100 | [diff] [blame] | 183 | diff |= (unsigned char) i ^ 32; |
| 184 | |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 185 | return diff; |
| 186 | } |
| 187 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 188 | /* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform |
| 189 | * has access to enough entropy in order to feed the PRNG regularly. */ |
| 190 | #if default_RNG_defined |
| 191 | static uECC_RNG_Function g_rng_function = &default_CSPRNG; |
| 192 | #else |
| 193 | static uECC_RNG_Function g_rng_function = 0; |
| 194 | #endif |
| 195 | |
| 196 | void uECC_set_rng(uECC_RNG_Function rng_function) |
| 197 | { |
| 198 | g_rng_function = rng_function; |
| 199 | } |
| 200 | |
| 201 | uECC_RNG_Function uECC_get_rng(void) |
| 202 | { |
| 203 | return g_rng_function; |
| 204 | } |
| 205 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 206 | int uECC_curve_private_key_size(void) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 207 | { |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 208 | return BITS_TO_BYTES(NUM_ECC_BITS); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 209 | } |
| 210 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 211 | int uECC_curve_public_key_size(void) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 212 | { |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 213 | return 2 * NUM_ECC_BYTES; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 214 | } |
| 215 | |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 216 | void uECC_vli_clear(uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 217 | { |
| 218 | wordcount_t i; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 219 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 220 | vli[i] = 0; |
| 221 | } |
| 222 | } |
| 223 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 224 | uECC_word_t uECC_vli_isZero(const uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 225 | { |
| 226 | uECC_word_t bits = 0; |
| 227 | wordcount_t i; |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 228 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 229 | bits |= vli[i]; |
| 230 | } |
| 231 | return (bits == 0); |
| 232 | } |
| 233 | |
| 234 | uECC_word_t uECC_vli_testBit(const uECC_word_t *vli, bitcount_t bit) |
| 235 | { |
| 236 | return (vli[bit >> uECC_WORD_BITS_SHIFT] & |
| 237 | ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK))); |
| 238 | } |
| 239 | |
| 240 | /* Counts the number of words in vli. */ |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 241 | static wordcount_t vli_numDigits(const uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 242 | { |
| 243 | |
| 244 | wordcount_t i; |
| 245 | /* Search from the end until we find a non-zero digit. We do it in reverse |
| 246 | * because we expect that most digits will be nonzero. */ |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 247 | for (i = NUM_ECC_WORDS - 1; i >= 0 && vli[i] == 0; --i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | return (i + 1); |
| 251 | } |
| 252 | |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 253 | bitcount_t uECC_vli_numBits(const uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 254 | { |
| 255 | |
| 256 | uECC_word_t i; |
| 257 | uECC_word_t digit; |
| 258 | |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 259 | wordcount_t num_digits = vli_numDigits(vli); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 260 | if (num_digits == 0) { |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | digit = vli[num_digits - 1]; |
| 265 | for (i = 0; digit; ++i) { |
| 266 | digit >>= 1; |
| 267 | } |
| 268 | |
| 269 | return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); |
| 270 | } |
| 271 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 272 | 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] | 273 | { |
| 274 | wordcount_t i; |
| 275 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 276 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 277 | dest[i] = src[i]; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | 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] | 282 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 283 | { |
| 284 | wordcount_t i; |
| 285 | |
Manuel Pégourié-Gonnard | a752191 | 2019-11-04 14:31:35 +0100 | [diff] [blame] | 286 | for (i = NUM_ECC_WORDS - 1; i >= 0; --i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 287 | if (left[i] > right[i]) { |
| 288 | return 1; |
| 289 | } else if (left[i] < right[i]) { |
| 290 | return -1; |
| 291 | } |
| 292 | } |
| 293 | return 0; |
| 294 | } |
| 295 | |
Manuel Pégourié-Gonnard | 2eca3d3 | 2019-11-04 14:33:09 +0100 | [diff] [blame] | 296 | 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] | 297 | { |
| 298 | |
| 299 | uECC_word_t diff = 0; |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 300 | uECC_word_t flow_monitor = 0; |
Manuel Pégourié-Gonnard | 645896e | 2019-12-05 15:30:09 +0100 | [diff] [blame] | 301 | uECC_word_t tmp1, tmp2; |
Manuel Pégourié-Gonnard | 98e1fe0 | 2019-11-27 11:57:49 +0100 | [diff] [blame] | 302 | volatile int i; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 303 | |
Piotr Nowicki | f0ab6d6 | 2020-05-25 12:48:30 +0200 | [diff] [blame] | 304 | /* Start from a random location and check the correct number of iterations */ |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 305 | int start_offset = mbedtls_platform_random_in_range(NUM_ECC_WORDS); |
| 306 | |
| 307 | for (i = start_offset; i < NUM_ECC_WORDS; ++i) { |
Manuel Pégourié-Gonnard | 645896e | 2019-12-05 15:30:09 +0100 | [diff] [blame] | 308 | tmp1 = left[i]; |
| 309 | tmp2 = right[i]; |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 310 | flow_monitor++; |
Manuel Pégourié-Gonnard | 645896e | 2019-12-05 15:30:09 +0100 | [diff] [blame] | 311 | diff |= (tmp1 ^ tmp2); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 312 | } |
Manuel Pégourié-Gonnard | 98e1fe0 | 2019-11-27 11:57:49 +0100 | [diff] [blame] | 313 | |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 314 | for (i = 0; i < start_offset; ++i) { |
| 315 | tmp1 = left[i]; |
| 316 | tmp2 = right[i]; |
| 317 | flow_monitor++; |
| 318 | diff |= (tmp1 ^ tmp2); |
| 319 | } |
Manuel Pégourié-Gonnard | 98e1fe0 | 2019-11-27 11:57:49 +0100 | [diff] [blame] | 320 | |
Piotr Nowicki | f0ab6d6 | 2020-05-25 12:48:30 +0200 | [diff] [blame] | 321 | /* Random delay to increase security */ |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 322 | mbedtls_platform_random_delay(); |
| 323 | |
| 324 | /* Return 0 only when diff is 0 and flow_counter is equal to NUM_ECC_WORDS */ |
| 325 | return (diff | (flow_monitor ^ NUM_ECC_WORDS)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | uECC_word_t cond_set(uECC_word_t p_true, uECC_word_t p_false, unsigned int cond) |
| 329 | { |
| 330 | return (p_true*(cond)) | (p_false*(!cond)); |
| 331 | } |
| 332 | |
| 333 | /* Computes result = left - right, returning borrow, in constant time. |
| 334 | * Can modify in place. */ |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 335 | #if defined MBEDTLS_HAVE_ASM && defined __CC_ARM |
| 336 | __asm uECC_word_t uECC_vli_sub(uECC_word_t *result, const uECC_word_t *left, |
| 337 | const uECC_word_t *right) |
| 338 | { |
| 339 | #if NUM_ECC_WORDS != 8 |
| 340 | #error adjust ARM assembly to handle NUM_ECC_WORDS != 8 |
| 341 | #endif |
| 342 | #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 |
| 343 | PUSH {r4-r6,lr} |
| 344 | FRAME PUSH {r4-r6,lr} |
| 345 | LDMIA r1!,{r3,r4} |
| 346 | LDMIA r2!,{r5,r6} |
| 347 | SUBS r3,r5 |
| 348 | SBCS r4,r6 |
| 349 | STMIA r0!,{r3,r4} |
| 350 | LDMIA r1!,{r3,r4} |
| 351 | LDMIA r2!,{r5,r6} |
| 352 | SBCS r3,r5 |
| 353 | SBCS r4,r6 |
| 354 | STMIA r0!,{r3,r4} |
| 355 | LDMIA r1!,{r3,r4} |
| 356 | LDMIA r2!,{r5,r6} |
| 357 | SBCS r3,r5 |
| 358 | SBCS r4,r6 |
| 359 | STMIA r0!,{r3,r4} |
| 360 | LDMIA r1!,{r3,r4} |
| 361 | LDMIA r2!,{r5,r6} |
| 362 | SBCS r3,r5 |
| 363 | SBCS r4,r6 |
| 364 | STMIA r0!,{r3,r4} |
| 365 | SBCS r0,r0 // r0 := r0 - r0 - borrow = -borrow |
| 366 | RSBS r0,r0,#0 // r0 := borrow |
| 367 | POP {r4-r6,pc} |
| 368 | #else |
| 369 | PUSH {r4-r8,lr} |
| 370 | FRAME PUSH {r4-r8,lr} |
| 371 | LDMIA r1!,{r3-r6} |
| 372 | LDMIA r2!,{r7,r8,r12,lr} |
| 373 | SUBS r3,r7 |
| 374 | SBCS r4,r8 |
| 375 | SBCS r5,r12 |
| 376 | SBCS r6,lr |
| 377 | STMIA r0!,{r3-r6} |
| 378 | LDMIA r1!,{r3-r6} |
| 379 | LDMIA r2!,{r7,r8,r12,lr} |
| 380 | SBCS r3,r7 |
| 381 | SBCS r4,r8 |
| 382 | SBCS r5,r12 |
| 383 | SBCS r6,lr |
| 384 | STMIA r0!,{r3-r6} |
| 385 | SBCS r0,r0 // r0 := r0 - r0 - borrow = -borrow |
| 386 | RSBS r0,r0,#0 // r0 := borrow |
| 387 | POP {r4-r8,pc} |
| 388 | #endif |
| 389 | } |
| 390 | #else |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 391 | 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] | 392 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 393 | { |
| 394 | uECC_word_t borrow = 0; |
| 395 | wordcount_t i; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 396 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 397 | uECC_word_t diff = left[i] - right[i] - borrow; |
| 398 | uECC_word_t val = (diff > left[i]); |
| 399 | borrow = cond_set(val, borrow, (diff != left[i])); |
| 400 | |
| 401 | result[i] = diff; |
| 402 | } |
| 403 | return borrow; |
| 404 | } |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 405 | #endif |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 406 | |
| 407 | /* Computes result = left + right, returning carry, in constant time. |
| 408 | * Can modify in place. */ |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 409 | #if defined MBEDTLS_HAVE_ASM && defined __CC_ARM |
| 410 | static __asm uECC_word_t uECC_vli_add(uECC_word_t *result, const uECC_word_t *left, |
| 411 | const uECC_word_t *right) |
| 412 | { |
| 413 | #if NUM_ECC_WORDS != 8 |
| 414 | #error adjust ARM assembly to handle NUM_ECC_WORDS != 8 |
| 415 | #endif |
| 416 | #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 |
| 417 | PUSH {r4-r6,lr} |
| 418 | FRAME PUSH {r4-r6,lr} |
| 419 | LDMIA r1!,{r3,r4} |
| 420 | LDMIA r2!,{r5,r6} |
| 421 | ADDS r3,r5 |
| 422 | ADCS r4,r6 |
| 423 | STMIA r0!,{r3,r4} |
| 424 | LDMIA r1!,{r3,r4} |
| 425 | LDMIA r2!,{r5,r6} |
| 426 | ADCS r3,r5 |
| 427 | ADCS r4,r6 |
| 428 | STMIA r0!,{r3,r4} |
| 429 | LDMIA r1!,{r3,r4} |
| 430 | LDMIA r2!,{r5,r6} |
| 431 | ADCS r3,r5 |
| 432 | ADCS r4,r6 |
| 433 | STMIA r0!,{r3,r4} |
| 434 | LDMIA r1!,{r3,r4} |
| 435 | LDMIA r2!,{r5,r6} |
| 436 | ADCS r3,r5 |
| 437 | ADCS r4,r6 |
| 438 | STMIA r0!,{r3,r4} |
| 439 | MOVS r0,#0 // does not affect C flag |
| 440 | ADCS r0,r0 // r0 := 0 + 0 + C = carry |
| 441 | POP {r4-r6,pc} |
| 442 | #else |
| 443 | PUSH {r4-r8,lr} |
| 444 | FRAME PUSH {r4-r8,lr} |
| 445 | LDMIA r1!,{r3-r6} |
| 446 | LDMIA r2!,{r7,r8,r12,lr} |
| 447 | ADDS r3,r7 |
| 448 | ADCS r4,r8 |
| 449 | ADCS r5,r12 |
| 450 | ADCS r6,lr |
| 451 | STMIA r0!,{r3-r6} |
| 452 | LDMIA r1!,{r3-r6} |
| 453 | LDMIA r2!,{r7,r8,r12,lr} |
| 454 | ADCS r3,r7 |
| 455 | ADCS r4,r8 |
| 456 | ADCS r5,r12 |
| 457 | ADCS r6,lr |
| 458 | STMIA r0!,{r3-r6} |
| 459 | MOVS r0,#0 // does not affect C flag |
| 460 | ADCS r0,r0 // r0 := 0 + 0 + C = carry |
| 461 | POP {r4-r8,pc} |
| 462 | #endif |
| 463 | } |
| 464 | #else |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 465 | 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] | 466 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 467 | { |
| 468 | uECC_word_t carry = 0; |
| 469 | wordcount_t i; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 470 | for (i = 0; i < NUM_ECC_WORDS; ++i) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 471 | uECC_word_t sum = left[i] + right[i] + carry; |
| 472 | uECC_word_t val = (sum < left[i]); |
| 473 | carry = cond_set(val, carry, (sum != left[i])); |
| 474 | result[i] = sum; |
| 475 | } |
| 476 | return carry; |
| 477 | } |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 478 | #endif |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 479 | |
Manuel Pégourié-Gonnard | 2cb3eea | 2019-11-04 14:43:35 +0100 | [diff] [blame] | 480 | 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] | 481 | { |
| 482 | uECC_word_t tmp[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 483 | uECC_word_t neg = !!uECC_vli_sub(tmp, left, right); |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 484 | uECC_word_t equal = uECC_vli_isZero(tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 485 | return (!equal - 2 * neg); |
| 486 | } |
| 487 | |
| 488 | /* Computes vli = vli >> 1. */ |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 489 | #if defined MBEDTLS_HAVE_ASM && defined __CC_ARM |
| 490 | static __asm void uECC_vli_rshift1(uECC_word_t *vli) |
| 491 | { |
| 492 | #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 |
| 493 | // RRX instruction is not available, so although we |
| 494 | // can use C flag, it's not that effective. Does at |
| 495 | // least save one working register, meaning we don't need stack |
| 496 | MOVS r3,#0 // initial carry = 0 |
| 497 | MOVS r2,#__cpp(4 * (NUM_ECC_WORDS - 1)) |
| 498 | 01 LDR r1,[r0,r2] |
| 499 | LSRS r1,r1,#1 // r2 = word >> 1 |
| 500 | ORRS r1,r3 // merge in the previous carry |
| 501 | STR r1,[r0,r2] |
| 502 | ADCS r3,r3 // put C into bottom bit of r3 |
| 503 | LSLS r3,r3,#31 // shift it up to the top ready for next word |
| 504 | SUBS r2,r2,#4 |
| 505 | BPL %B01 |
| 506 | BX lr |
| 507 | #else |
| 508 | #if NUM_ECC_WORDS != 8 |
| 509 | #error adjust ARM assembly to handle NUM_ECC_WORDS != 8 |
| 510 | #endif |
| 511 | // Smooth multiword operation, lots of 32-bit instructions |
| 512 | ADDS r0,#32 |
| 513 | LDMDB r0,{r1-r3,ip} |
| 514 | LSRS ip,ip,#1 |
| 515 | RRXS r3,r3 |
| 516 | RRXS r2,r2 |
| 517 | RRXS r1,r1 |
| 518 | STMDB r0!,{r1-r3,ip} |
| 519 | LDMDB r0,{r1-r3,ip} |
| 520 | RRXS ip,ip |
| 521 | RRXS r3,r3 |
| 522 | RRXS r2,r2 |
| 523 | RRX r1,r1 |
| 524 | STMDB r0!,{r1-r3,ip} |
| 525 | BX lr |
| 526 | #endif |
| 527 | } |
| 528 | #else |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 529 | static void uECC_vli_rshift1(uECC_word_t *vli) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 530 | { |
| 531 | uECC_word_t *end = vli; |
| 532 | uECC_word_t carry = 0; |
| 533 | |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 534 | vli += NUM_ECC_WORDS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 535 | while (vli-- > end) { |
| 536 | uECC_word_t temp = *vli; |
| 537 | *vli = (temp >> 1) | carry; |
| 538 | carry = temp << (uECC_WORD_BITS - 1); |
| 539 | } |
| 540 | } |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 541 | #endif |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 542 | |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 543 | /* Compute a * b + r, where r is a triple-word with high-order word r[2] and |
| 544 | * low-order word r[0], and store the result in the same triple-word. |
Manuel Pégourié-Gonnard | 86c4f81 | 2019-10-31 13:02:03 +0100 | [diff] [blame] | 545 | * |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 546 | * r[2..0] = a * b + r[2..0]: |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 547 | * [in] a, b: operands to be multiplied |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 548 | * [in] r: 3 words of operand to add |
| 549 | * [out] r: 3 words of result |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 550 | */ |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 551 | #if defined MBEDTLS_HAVE_ASM && defined __CC_ARM |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 552 | static __asm void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 553 | { |
| 554 | #if defined __thumb__ && __TARGET_ARCH_THUMB < 4 |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 555 | PUSH {r4-r5} |
| 556 | FRAME PUSH {r4-r5} |
| 557 | // __ARM_common_mul_uu replacement - inline, faster, don't touch R2 |
| 558 | // Separate operands into halfwords |
| 559 | UXTH r3,r0 // r3 := a.lo |
| 560 | LSRS r4,r0,#16 // r4 := a.hi |
| 561 | UXTH r5,r1 // r5 := b.lo |
| 562 | LSRS r1,r1,#16 // r1 := b.hi |
| 563 | // Multiply halfword pairs |
| 564 | MOVS r0,r3 |
| 565 | MULS r0,r5,r0 // r0 := a.lo * b.lo |
| 566 | MULS r3,r1,r3 // r3 := a.lo * b.hi |
| 567 | MULS r5,r4,r5 // r5 := a.hi * b.lo |
| 568 | MULS r1,r4,r1 // r1 := a.hi * b.hi |
| 569 | // Split, shift and add a.lo * b.hi |
| 570 | LSRS r4,r3,#16 // r4 := (a.lo * b.hi).hi |
| 571 | LSLS r3,r3,#16 // r3 := (a.lo * b.hi).lo |
| 572 | ADDS r0,r0,r3 // r0 := a.lo * b.lo + (a.lo * b.hi).lo |
| 573 | ADCS r1,r4 // r1 := a.hi * b.hi + (a.lo * b.hi).hi + carry |
| 574 | // Split, shift and add a.hi * b.lo |
| 575 | LSRS r4,r5,#16 // r4 := (a.hi * b.lo).hi |
| 576 | LSLS r5,r5,#16 // r5 := (a.hi * b.lo).lo |
| 577 | ADDS r0,r0,r5 // r0 := a.lo * b.lo + (a.lo * b.hi).lo + (a.hi * b.lo).lo |
| 578 | ADCS r1,r4 // r1 := a.hi * b.hi + (a.lo * b.hi).hi + (a.hi * b.lo).hi + carries |
| 579 | // Finally add r[] |
| 580 | LDMIA r2!,{r3,r4,r5} |
| 581 | ADDS r3,r3,r0 |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 582 | ADCS r4,r1 |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 583 | MOVS r0,#0 |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 584 | ADCS r5,r0 |
| 585 | SUBS r2,#12 |
| 586 | STMIA r2!,{r3,r4,r5} |
| 587 | POP {r4-r5} |
| 588 | FRAME POP {r4-r5} |
| 589 | BX lr |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 590 | #else |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 591 | UMULL r3,ip,r0,r1 // pre-ARMv6 requires Rd[Lo|Hi] != Rn |
| 592 | LDMIA r2,{r0,r1} |
| 593 | ADDS r0,r0,r3 |
| 594 | LDR r3,[r2,#8] |
| 595 | ADCS r1,r1,ip |
| 596 | ADC r3,r3,#0 |
| 597 | STMIA r2!,{r0,r1,r3} |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 598 | BX lr |
| 599 | #endif |
Kevin Bracey | e0f88d5 | 2020-09-30 12:52:15 +0300 | [diff] [blame^] | 600 | } |
| 601 | #elif defined MBEDTLS_HAVE_ASM && (defined __GNUC__ || defined __clang__) && defined __arm__ |
| 602 | static __attribute__((__naked__)) |
| 603 | void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) |
| 604 | { |
| 605 | asm( |
| 606 | #if defined __thumb__ && !defined(__thumb2__) |
| 607 | ".syntax unified \n\t" |
| 608 | "PUSH {r4-r5} \n\t" |
| 609 | ".cfi_remember_state \n\t" |
| 610 | ".cfi_adjust_cfa_offset 8 \n\t" |
| 611 | ".cfi_rel_offset r4,0 \n\t" |
| 612 | ".cfi_rel_offset r5,4 \n\t" |
| 613 | // __ARM_common_mul_uu replacement - inline, faster, don't touch R2 |
| 614 | // Separate operands into halfwords |
| 615 | "UXTH r3,r0 \n\t" // r3 := a.lo |
| 616 | "LSRS r4,r0,#16 \n\t" // r4 := a.hi |
| 617 | "UXTH r5,r1 \n\t" // r5 := b.lo |
| 618 | "LSRS r1,r1,#16 \n\t" // r1 := b.hi |
| 619 | // Multiply halfword pairs |
| 620 | "MOVS r0,r3 \n\t" |
| 621 | "MULS r0,r5,r0 \n\t" // r0 := a.lo * b.lo |
| 622 | "MULS r3,r1,r3 \n\t" // r3 := a.lo * b.hi |
| 623 | "MULS r5,r4,r5 \n\t" // r5 := a.hi * b.lo |
| 624 | "MULS r1,r4,r1 \n\t" // r1 := a.hi * b.hi |
| 625 | // Split, shift and add a.lo * b.hi |
| 626 | "LSRS r4,r3,#16 \n\t" // r4 := (a.lo * b.hi).hi |
| 627 | "LSLS r3,r3,#16 \n\t" // r3 := (a.lo * b.hi).lo |
| 628 | "ADDS r0,r0,r3 \n\t" // r0 := a.lo * b.lo + (a.lo * b.hi).lo |
| 629 | "ADCS r1,r4 \n\t" // r1 := a.hi * b.hi + (a.lo * b.hi).hi + carry |
| 630 | // Split, shift and add a.hi * b.lo |
| 631 | "LSRS r4,r5,#16 \n\t" // r4 := (a.hi * b.lo).hi |
| 632 | "LSLS r5,r5,#16 \n\t" // r5 := (a.hi * b.lo).lo |
| 633 | "ADDS r0,r0,r5 \n\t" // r0 := a.lo * b.lo + (a.lo * b.hi).lo + (a.hi * b.lo).lo |
| 634 | "ADCS r1,r4 \n\t" // r1 := a.hi * b.hi + (a.lo * b.hi).hi + (a.hi * b.lo).hi + carries |
| 635 | // Finally add r[] |
| 636 | "LDMIA r2!,{r3,r4,r5} \n\t" |
| 637 | "ADDS r3,r3,r0 \n\t" |
| 638 | "ADCS r4,r1 \n\t" |
| 639 | "MOVS r0,#0 \n\t" |
| 640 | "ADCS r5,r0 \n\t" |
| 641 | "SUBS r2,#12 \n\t" |
| 642 | "STMIA r2!,{r3,r4,r5} \n\t" |
| 643 | "POP {r4-r5} \n\t" |
| 644 | ".cfi_restore_state \n\t" |
| 645 | "BX lr \n\t" |
| 646 | #else |
| 647 | "UMULL r3,ip,r0,r1 \n\t" // pre-ARMv6 requires Rd[Lo|Hi] != Rn |
| 648 | "LDMIA r2,{r0,r1} \n\t" |
| 649 | "ADDS r0,r0,r3 \n\t" |
| 650 | "LDR r3,[r2,#8] \n\t" |
| 651 | "ADCS r1,r1,ip \n\t" |
| 652 | "ADC r3,r3,#0 \n\t" |
| 653 | "STMIA r2!,{r0,r1,r3} \n\t" |
| 654 | "BX lr \n\t" |
| 655 | #endif |
| 656 | ); |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 657 | } |
| 658 | #else |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 659 | static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t r[3]) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 660 | { |
| 661 | |
| 662 | uECC_dword_t p = (uECC_dword_t)a * b; |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 663 | uECC_dword_t r01 = ((uECC_dword_t)(r[1]) << uECC_WORD_BITS) | r[0]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 664 | r01 += p; |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 665 | r[2] += (r01 < p); |
| 666 | r[1] = r01 >> uECC_WORD_BITS; |
| 667 | r[0] = (uECC_word_t)r01; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 668 | } |
Kevin Bracey | 1959c18 | 2020-07-16 21:03:19 +0300 | [diff] [blame] | 669 | #endif |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 670 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 671 | /* State for implementing random delays in uECC_vli_mult_rnd(). |
| 672 | * |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 673 | * 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] | 674 | * Each call to uECC_vli_mult_rnd() uses one byte of delays and increments i. |
| 675 | * |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 676 | * Randomized vli multiplication is used only for point operations |
| 677 | * (XYcZ_add_rnd() * and XYcZ_addC_rnd()) in scalar multiplication |
| 678 | * (ECCPoint_mult()). Those go in pair, and each pair does 14 calls to |
| 679 | * 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] | 680 | * indirectly through uECC_vli_modMult_rnd(). |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 681 | * |
| 682 | * Considering this, in order to minimize the number of calls to the RNG |
| 683 | * (which impact performance) while keeping the size of the structure low, |
| 684 | * make room for 14 randomized vli mults, which corresponds to one step in the |
| 685 | * scalar multiplication routine. |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 686 | */ |
| 687 | typedef struct { |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 688 | uint8_t i; |
| 689 | uint8_t delays[14]; |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 690 | } ecc_wait_state_t; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 691 | |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 692 | /* |
| 693 | * Reset wait_state so that it's ready to be used. |
| 694 | */ |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 695 | void ecc_wait_state_reset(ecc_wait_state_t *ws) |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 696 | { |
| 697 | if (ws == NULL) |
| 698 | return; |
| 699 | |
| 700 | ws->i = 0; |
Shelly Liberman | 05beb9a | 2020-09-13 15:23:56 +0300 | [diff] [blame] | 701 | mbedtls_platform_random_buf(ws->delays, sizeof(ws->delays)); |
Manuel Pégourié-Gonnard | d467116 | 2019-10-31 11:26:26 +0100 | [diff] [blame] | 702 | } |
| 703 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 704 | /* Computes result = left * right. Result must be 2 * num_words long. |
| 705 | * |
| 706 | * As a counter-measure against horizontal attacks, add noise by performing |
| 707 | * a random number of extra computations performing random additional accesses |
| 708 | * to limbs of the input. |
| 709 | * |
| 710 | * Each of the two actual computation loops is surrounded by two |
| 711 | * similar-looking waiting loops, to make the beginning and end of the actual |
| 712 | * computation harder to spot. |
| 713 | * |
| 714 | * We add 4 waiting loops of between 0 and 3 calls to muladd() each. That |
| 715 | * makes an average of 6 extra calls. Compared to the main computation which |
| 716 | * makes 64 such calls, this represents an average performance degradation of |
| 717 | * less than 10%. |
| 718 | * |
| 719 | * Compared to the original uECC_vli_mult(), loose the num_words argument as we |
| 720 | * know it's always 8. This saves a bit of code size and execution speed. |
| 721 | */ |
| 722 | static void uECC_vli_mult_rnd(uECC_word_t *result, const uECC_word_t *left, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 723 | const uECC_word_t *right, ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 724 | { |
| 725 | |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 726 | uECC_word_t r[3] = { 0, 0, 0 }; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 727 | wordcount_t i, k; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 728 | const uint8_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 729 | |
| 730 | /* Fetch 8 bit worth of delay from the state; 0 if we have no state */ |
| 731 | uint8_t delays = s ? s->delays[s->i++] : 0; |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 732 | uECC_word_t rr[3] = { 0, 0, 0 }; |
| 733 | volatile uECC_word_t rdummy; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 734 | |
| 735 | /* Mimic start of next loop: k in [0, 3] */ |
| 736 | k = 0 + (delays & 0x03); |
| 737 | delays >>= 2; |
| 738 | /* k = 0 -> i in [1, 0] -> 0 extra muladd; |
| 739 | * k = 3 -> i in [1, 3] -> 3 extra muladd */ |
Manuel Pégourié-Gonnard | c881486 | 2019-11-05 10:32:37 +0100 | [diff] [blame] | 740 | for (i = 1; i <= k; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 741 | muladd(left[i], right[k - i], rr); |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 742 | } |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 743 | rdummy = rr[0]; |
| 744 | rr[0] = rr[1]; |
| 745 | rr[1] = rr[2]; |
| 746 | rr[2] = 0; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 747 | |
| 748 | /* Compute each digit of result in sequence, maintaining the carries. */ |
| 749 | for (k = 0; k < num_words; ++k) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 750 | for (i = 0; i <= k; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 751 | muladd(left[i], right[k - i], r); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 752 | } |
| 753 | |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 754 | result[k] = r[0]; |
| 755 | r[0] = r[1]; |
| 756 | r[1] = r[2]; |
| 757 | r[2] = 0; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 758 | } |
| 759 | |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 760 | /* Mimic end of previous loop: k in [4, 7] */ |
| 761 | k = 4 + (delays & 0x03); |
| 762 | delays >>= 2; |
| 763 | /* k = 4 -> i in [5, 4] -> 0 extra muladd; |
| 764 | * k = 7 -> i in [5, 7] -> 3 extra muladd */ |
| 765 | for (i = 5; i <= k; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 766 | muladd(left[i], right[k - i], rr); |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 767 | } |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 768 | rdummy = rr[0]; |
| 769 | rr[0] = rr[1]; |
| 770 | rr[1] = rr[2]; |
| 771 | rr[2] = 0; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 772 | |
| 773 | /* Mimic start of next loop: k in [8, 11] */ |
| 774 | k = 11 - (delays & 0x03); |
| 775 | delays >>= 2; |
| 776 | /* k = 8 -> i in [5, 7] -> 3 extra muladd; |
| 777 | * k = 11 -> i in [8, 7] -> 0 extra muladd */ |
| 778 | for (i = (k + 5) - num_words; i < num_words; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 779 | muladd(left[i], right[k - i], rr); |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 780 | } |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 781 | rdummy = rr[0]; |
| 782 | rr[0] = rr[1]; |
| 783 | rr[1] = rr[2]; |
| 784 | rr[2] = 0; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 785 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 786 | for (k = num_words; k < num_words * 2 - 1; ++k) { |
| 787 | |
| 788 | for (i = (k + 1) - num_words; i < num_words; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 789 | muladd(left[i], right[k - i], r); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 790 | } |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 791 | result[k] = r[0]; |
| 792 | r[0] = r[1]; |
| 793 | r[1] = r[2]; |
| 794 | r[2] = 0; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 795 | } |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 796 | |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 797 | result[num_words * 2 - 1] = r[0]; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 798 | |
| 799 | /* Mimic end of previous loop: k in [12, 15] */ |
| 800 | k = 15 - (delays & 0x03); |
| 801 | delays >>= 2; |
| 802 | /* k = 12 -> i in [5, 7] -> 3 extra muladd; |
| 803 | * k = 15 -> i in [8, 7] -> 0 extra muladd */ |
| 804 | for (i = (k + 1) - num_words; i < num_words; ++i) { |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 805 | muladd(left[i], right[k - i], rr); |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 806 | } |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 807 | rdummy = rr[0]; |
| 808 | rr[0] = rr[1]; |
| 809 | rr[1] = rr[2]; |
| 810 | rr[2] = 0; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 811 | |
Kevin Bracey | 84f31d3 | 2020-09-29 17:51:04 +0300 | [diff] [blame] | 812 | /* avoid warning that rdummy is set but not used */ |
| 813 | (void) rdummy; |
Manuel Pégourié-Gonnard | 14ab9c2 | 2019-10-22 09:49:53 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 816 | void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 817 | const uECC_word_t *right, const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 818 | { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 819 | uECC_word_t carry = uECC_vli_add(result, left, right); |
Manuel Pégourié-Gonnard | a752191 | 2019-11-04 14:31:35 +0100 | [diff] [blame] | 820 | if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 821 | /* result > mod (result = mod + remainder), so subtract mod to get |
| 822 | * remainder. */ |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 823 | uECC_vli_sub(result, result, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | |
| 827 | void uECC_vli_modSub(uECC_word_t *result, const uECC_word_t *left, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 828 | const uECC_word_t *right, const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 829 | { |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 830 | uECC_word_t l_borrow = uECC_vli_sub(result, left, right); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 831 | if (l_borrow) { |
| 832 | /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x, |
| 833 | * 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] | 834 | uECC_vli_add(result, result, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | /* Computes result = product % mod, where product is 2N words long. */ |
| 839 | /* Currently only designed to work for curve_p or curve_n. */ |
| 840 | void uECC_vli_mmod(uECC_word_t *result, uECC_word_t *product, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 841 | const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 842 | { |
| 843 | uECC_word_t mod_multiple[2 * NUM_ECC_WORDS]; |
| 844 | uECC_word_t tmp[2 * NUM_ECC_WORDS]; |
| 845 | uECC_word_t *v[2] = {tmp, product}; |
| 846 | uECC_word_t index; |
Manuel Pégourié-Gonnard | 10349e4 | 2019-11-04 14:57:53 +0100 | [diff] [blame] | 847 | const wordcount_t num_words = NUM_ECC_WORDS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 848 | |
| 849 | /* Shift mod so its highest set bit is at the maximum position. */ |
| 850 | bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 851 | uECC_vli_numBits(mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 852 | wordcount_t word_shift = shift / uECC_WORD_BITS; |
| 853 | wordcount_t bit_shift = shift % uECC_WORD_BITS; |
| 854 | uECC_word_t carry = 0; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 855 | uECC_vli_clear(mod_multiple); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 856 | if (bit_shift > 0) { |
| 857 | for(index = 0; index < (uECC_word_t)num_words; ++index) { |
| 858 | mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry; |
| 859 | carry = mod[index] >> (uECC_WORD_BITS - bit_shift); |
| 860 | } |
| 861 | } else { |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 862 | uECC_vli_set(mod_multiple + word_shift, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | for (index = 1; shift >= 0; --shift) { |
| 866 | uECC_word_t borrow = 0; |
| 867 | wordcount_t i; |
| 868 | for (i = 0; i < num_words * 2; ++i) { |
| 869 | uECC_word_t diff = v[index][i] - mod_multiple[i] - borrow; |
| 870 | if (diff != v[index][i]) { |
| 871 | borrow = (diff > v[index][i]); |
| 872 | } |
| 873 | v[1 - index][i] = diff; |
| 874 | } |
| 875 | /* Swap the index if there was no borrow */ |
| 876 | index = !(index ^ borrow); |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 877 | uECC_vli_rshift1(mod_multiple); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 878 | mod_multiple[num_words - 1] |= mod_multiple[num_words] << |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 879 | (uECC_WORD_BITS - 1); |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 880 | uECC_vli_rshift1(mod_multiple + num_words); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 881 | } |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 882 | uECC_vli_set(result, v[index]); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 883 | } |
| 884 | |
| 885 | void uECC_vli_modMult(uECC_word_t *result, const uECC_word_t *left, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 886 | const uECC_word_t *right, const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 887 | { |
| 888 | uECC_word_t product[2 * NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 889 | uECC_vli_mult_rnd(product, left, right, NULL); |
Manuel Pégourié-Gonnard | 10349e4 | 2019-11-04 14:57:53 +0100 | [diff] [blame] | 890 | uECC_vli_mmod(result, product, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 891 | } |
| 892 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 893 | 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] | 894 | const uECC_word_t *right, ecc_wait_state_t *s) |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 895 | { |
| 896 | uECC_word_t product[2 * NUM_ECC_WORDS]; |
| 897 | uECC_vli_mult_rnd(product, left, right, s); |
| 898 | |
| 899 | vli_mmod_fast_secp256r1(result, product); |
| 900 | } |
| 901 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 902 | 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] | 903 | const uECC_word_t *right) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 904 | { |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 905 | uECC_vli_modMult_rnd(result, left, right, NULL); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 906 | } |
| 907 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 908 | #define EVEN(vli) (!(vli[0] & 1)) |
| 909 | |
| 910 | static void vli_modInv_update(uECC_word_t *uv, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 911 | const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 912 | { |
| 913 | |
| 914 | uECC_word_t carry = 0; |
| 915 | |
| 916 | if (!EVEN(uv)) { |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 917 | carry = uECC_vli_add(uv, uv, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 918 | } |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 919 | uECC_vli_rshift1(uv); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 920 | if (carry) { |
Manuel Pégourié-Gonnard | 9135348 | 2019-11-04 15:04:20 +0100 | [diff] [blame] | 921 | uv[NUM_ECC_WORDS - 1] |= HIGH_BIT_SET; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 922 | } |
| 923 | } |
| 924 | |
| 925 | void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 926 | const uECC_word_t *mod) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 927 | { |
| 928 | uECC_word_t a[NUM_ECC_WORDS], b[NUM_ECC_WORDS]; |
| 929 | uECC_word_t u[NUM_ECC_WORDS], v[NUM_ECC_WORDS]; |
| 930 | cmpresult_t cmpResult; |
| 931 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 932 | if (uECC_vli_isZero(input)) { |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 933 | uECC_vli_clear(result); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 934 | return; |
| 935 | } |
| 936 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 937 | uECC_vli_set(a, input); |
| 938 | uECC_vli_set(b, mod); |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 939 | uECC_vli_clear(u); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 940 | u[0] = 1; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 941 | uECC_vli_clear(v); |
Manuel Pégourié-Gonnard | a752191 | 2019-11-04 14:31:35 +0100 | [diff] [blame] | 942 | while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 943 | if (EVEN(a)) { |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 944 | uECC_vli_rshift1(a); |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 945 | vli_modInv_update(u, mod); |
| 946 | } else if (EVEN(b)) { |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 947 | uECC_vli_rshift1(b); |
Manuel Pégourié-Gonnard | 9135348 | 2019-11-04 15:04:20 +0100 | [diff] [blame] | 948 | vli_modInv_update(v, mod); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 949 | } else if (cmpResult > 0) { |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 950 | uECC_vli_sub(a, a, b); |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 951 | uECC_vli_rshift1(a); |
Manuel Pégourié-Gonnard | a752191 | 2019-11-04 14:31:35 +0100 | [diff] [blame] | 952 | if (uECC_vli_cmp_unsafe(u, v) < 0) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 953 | uECC_vli_add(u, u, mod); |
| 954 | } |
| 955 | uECC_vli_sub(u, u, v); |
| 956 | vli_modInv_update(u, mod); |
| 957 | } else { |
| 958 | uECC_vli_sub(b, b, a); |
| 959 | uECC_vli_rshift1(b); |
| 960 | if (uECC_vli_cmp_unsafe(v, u) < 0) { |
| 961 | uECC_vli_add(v, v, mod); |
| 962 | } |
| 963 | uECC_vli_sub(v, v, u); |
| 964 | vli_modInv_update(v, mod); |
| 965 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 966 | } |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 967 | uECC_vli_set(result, u); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* ------ Point operations ------ */ |
| 971 | |
| 972 | void double_jacobian_default(uECC_word_t * X1, uECC_word_t * Y1, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 973 | uECC_word_t * Z1) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 974 | { |
| 975 | /* t1 = X, t2 = Y, t3 = Z */ |
| 976 | uECC_word_t t4[NUM_ECC_WORDS]; |
| 977 | uECC_word_t t5[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 1765933 | 2019-11-21 09:27:38 +0100 | [diff] [blame] | 978 | wordcount_t num_words = NUM_ECC_WORDS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 979 | |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 980 | if (uECC_vli_isZero(Z1)) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 981 | return; |
| 982 | } |
| 983 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 984 | uECC_vli_modMult_fast(t4, Y1, Y1); /* t4 = y1^2 */ |
| 985 | uECC_vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ |
| 986 | uECC_vli_modMult_fast(t4, t4, t4); /* t4 = y1^4 */ |
| 987 | uECC_vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ |
| 988 | uECC_vli_modMult_fast(Z1, Z1, Z1); /* t3 = z1^2 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 989 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 990 | uECC_vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */ |
| 991 | uECC_vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */ |
| 992 | 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] | 993 | uECC_vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 994 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 995 | uECC_vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */ |
| 996 | 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] | 997 | if (uECC_vli_testBit(X1, 0)) { |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 998 | 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] | 999 | uECC_vli_rshift1(X1); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1000 | X1[num_words - 1] |= l_carry << (uECC_WORD_BITS - 1); |
| 1001 | } else { |
Manuel Pégourié-Gonnard | 5e3baf2 | 2019-11-04 14:46:10 +0100 | [diff] [blame] | 1002 | uECC_vli_rshift1(X1); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* t1 = 3/2*(x1^2 - z1^4) = B */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1006 | uECC_vli_modMult_fast(Z1, X1, X1); /* t3 = B^2 */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1007 | uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - A */ |
| 1008 | uECC_vli_modSub(Z1, Z1, t5, curve_p); /* t3 = B^2 - 2A = x3 */ |
| 1009 | 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] | 1010 | uECC_vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1011 | /* t4 = B * (A - x3) - y1^4 = y3: */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1012 | uECC_vli_modSub(t4, X1, t4, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1013 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1014 | uECC_vli_set(X1, Z1); |
| 1015 | uECC_vli_set(Z1, Y1); |
| 1016 | uECC_vli_set(Y1, t4); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1017 | } |
| 1018 | |
Manuel Pégourié-Gonnard | 1c6f7ea | 2019-11-21 09:18:29 +0100 | [diff] [blame] | 1019 | /* |
| 1020 | * @brief Computes x^3 + ax + b. result must not overlap x. |
| 1021 | * @param result OUT -- x^3 + ax + b |
| 1022 | * @param x IN -- value of x |
| 1023 | * @param curve IN -- elliptic curve |
| 1024 | */ |
| 1025 | static void x_side_default(uECC_word_t *result, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1026 | const uECC_word_t *x) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1027 | { |
| 1028 | uECC_word_t _3[NUM_ECC_WORDS] = {3}; /* -a = 3 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1029 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1030 | uECC_vli_modMult_fast(result, x, x); /* r = x^2 */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1031 | 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] | 1032 | uECC_vli_modMult_fast(result, result, x); /* r = x^3 - 3x */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1033 | /* r = x^3 - 3x + b: */ |
Manuel Pégourié-Gonnard | ffd1399 | 2019-11-21 10:39:06 +0100 | [diff] [blame] | 1034 | uECC_vli_modAdd(result, result, curve_b, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1035 | } |
| 1036 | |
| 1037 | void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product) |
| 1038 | { |
| 1039 | unsigned int tmp[NUM_ECC_WORDS]; |
| 1040 | int carry; |
| 1041 | |
| 1042 | /* t */ |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1043 | uECC_vli_set(result, product); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1044 | |
| 1045 | /* s1 */ |
| 1046 | tmp[0] = tmp[1] = tmp[2] = 0; |
| 1047 | tmp[3] = product[11]; |
| 1048 | tmp[4] = product[12]; |
| 1049 | tmp[5] = product[13]; |
| 1050 | tmp[6] = product[14]; |
| 1051 | tmp[7] = product[15]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 1052 | carry = uECC_vli_add(tmp, tmp, tmp); |
| 1053 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1054 | |
| 1055 | /* s2 */ |
| 1056 | tmp[3] = product[12]; |
| 1057 | tmp[4] = product[13]; |
| 1058 | tmp[5] = product[14]; |
| 1059 | tmp[6] = product[15]; |
| 1060 | tmp[7] = 0; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 1061 | carry += uECC_vli_add(tmp, tmp, tmp); |
| 1062 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1063 | |
| 1064 | /* s3 */ |
| 1065 | tmp[0] = product[8]; |
| 1066 | tmp[1] = product[9]; |
| 1067 | tmp[2] = product[10]; |
| 1068 | tmp[3] = tmp[4] = tmp[5] = 0; |
| 1069 | tmp[6] = product[14]; |
| 1070 | tmp[7] = product[15]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 1071 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1072 | |
| 1073 | /* s4 */ |
| 1074 | tmp[0] = product[9]; |
| 1075 | tmp[1] = product[10]; |
| 1076 | tmp[2] = product[11]; |
| 1077 | tmp[3] = product[13]; |
| 1078 | tmp[4] = product[14]; |
| 1079 | tmp[5] = product[15]; |
| 1080 | tmp[6] = product[13]; |
| 1081 | tmp[7] = product[8]; |
Manuel Pégourié-Gonnard | 02d9d21 | 2019-11-04 12:37:08 +0100 | [diff] [blame] | 1082 | carry += uECC_vli_add(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1083 | |
| 1084 | /* d1 */ |
| 1085 | tmp[0] = product[11]; |
| 1086 | tmp[1] = product[12]; |
| 1087 | tmp[2] = product[13]; |
| 1088 | tmp[3] = tmp[4] = tmp[5] = 0; |
| 1089 | tmp[6] = product[8]; |
| 1090 | tmp[7] = product[10]; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 1091 | carry -= uECC_vli_sub(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1092 | |
| 1093 | /* d2 */ |
| 1094 | tmp[0] = product[12]; |
| 1095 | tmp[1] = product[13]; |
| 1096 | tmp[2] = product[14]; |
| 1097 | tmp[3] = product[15]; |
| 1098 | tmp[4] = tmp[5] = 0; |
| 1099 | tmp[6] = product[9]; |
| 1100 | tmp[7] = product[11]; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 1101 | carry -= uECC_vli_sub(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1102 | |
| 1103 | /* d3 */ |
| 1104 | tmp[0] = product[13]; |
| 1105 | tmp[1] = product[14]; |
| 1106 | tmp[2] = product[15]; |
| 1107 | tmp[3] = product[8]; |
| 1108 | tmp[4] = product[9]; |
| 1109 | tmp[5] = product[10]; |
| 1110 | tmp[6] = 0; |
| 1111 | tmp[7] = product[12]; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 1112 | carry -= uECC_vli_sub(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1113 | |
| 1114 | /* d4 */ |
| 1115 | tmp[0] = product[14]; |
| 1116 | tmp[1] = product[15]; |
| 1117 | tmp[2] = 0; |
| 1118 | tmp[3] = product[9]; |
| 1119 | tmp[4] = product[10]; |
| 1120 | tmp[5] = product[11]; |
| 1121 | tmp[6] = 0; |
| 1122 | tmp[7] = product[13]; |
Manuel Pégourié-Gonnard | 129b42e | 2019-11-04 14:41:45 +0100 | [diff] [blame] | 1123 | carry -= uECC_vli_sub(result, result, tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1124 | |
| 1125 | if (carry < 0) { |
| 1126 | do { |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1127 | carry += uECC_vli_add(result, result, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1128 | } |
| 1129 | while (carry < 0); |
| 1130 | } else { |
Piotr Nowicki | 1a9d33e | 2020-05-20 22:10:14 +0200 | [diff] [blame] | 1131 | while (carry || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1132 | uECC_vli_cmp_unsafe(curve_p, result) != 1) { |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1133 | carry -= uECC_vli_sub(result, result, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1134 | } |
| 1135 | } |
| 1136 | } |
| 1137 | |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1138 | uECC_word_t EccPoint_isZero(const uECC_word_t *point) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1139 | { |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 1140 | return uECC_vli_isZero(point); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1141 | } |
| 1142 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1143 | 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] | 1144 | { |
| 1145 | uECC_word_t t1[NUM_ECC_WORDS]; |
| 1146 | |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1147 | uECC_vli_modMult_fast(t1, Z, Z); /* z^2 */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1148 | uECC_vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ |
| 1149 | uECC_vli_modMult_fast(t1, t1, Z); /* z^3 */ |
| 1150 | uECC_vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | /* P = (x1, y1) => 2P, (x2, y2) => P' */ |
| 1154 | static void XYcZ_initial_double(uECC_word_t * X1, uECC_word_t * Y1, |
| 1155 | uECC_word_t * X2, uECC_word_t * Y2, |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1156 | const uECC_word_t * const initial_Z) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1157 | { |
| 1158 | uECC_word_t z[NUM_ECC_WORDS]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1159 | if (initial_Z) { |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1160 | uECC_vli_set(z, initial_Z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1161 | } else { |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 1162 | uECC_vli_clear(z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1163 | z[0] = 1; |
| 1164 | } |
| 1165 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1166 | uECC_vli_set(X2, X1); |
| 1167 | uECC_vli_set(Y2, Y1); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1168 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1169 | apply_z(X1, Y1, z); |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1170 | double_jacobian_default(X1, Y1, z); |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1171 | apply_z(X2, Y2, z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1172 | } |
| 1173 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1174 | static void XYcZ_add_rnd(uECC_word_t * X1, uECC_word_t * Y1, |
| 1175 | uECC_word_t * X2, uECC_word_t * Y2, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 1176 | ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1177 | { |
| 1178 | /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ |
| 1179 | uECC_word_t t5[NUM_ECC_WORDS]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1180 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1181 | 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] | 1182 | 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] | 1183 | uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */ |
| 1184 | 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] | 1185 | 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] | 1186 | 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] | 1187 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1188 | uECC_vli_modSub(t5, t5, X1, curve_p); /* t5 = D - B */ |
| 1189 | uECC_vli_modSub(t5, t5, X2, curve_p); /* t5 = D - B - C = x3 */ |
| 1190 | 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] | 1191 | 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] | 1192 | 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] | 1193 | 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] | 1194 | uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y3 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1195 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1196 | uECC_vli_set(X2, t5); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1197 | } |
| 1198 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1199 | void XYcZ_add(uECC_word_t * X1, uECC_word_t * Y1, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1200 | uECC_word_t * X2, uECC_word_t * Y2) |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1201 | { |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1202 | XYcZ_add_rnd(X1, Y1, X2, Y2, NULL); |
| 1203 | } |
| 1204 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1205 | /* Input P = (x1, y1, Z), Q = (x2, y2, Z) |
| 1206 | Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) |
| 1207 | or P => P - Q, Q => P + Q |
| 1208 | */ |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1209 | static void XYcZ_addC_rnd(uECC_word_t * X1, uECC_word_t * Y1, |
| 1210 | uECC_word_t * X2, uECC_word_t * Y2, |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 1211 | ecc_wait_state_t *s) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1212 | { |
| 1213 | /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ |
| 1214 | uECC_word_t t5[NUM_ECC_WORDS]; |
| 1215 | uECC_word_t t6[NUM_ECC_WORDS]; |
| 1216 | uECC_word_t t7[NUM_ECC_WORDS]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1217 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1218 | 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] | 1219 | 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] | 1220 | uECC_vli_modMult_rnd(X1, X1, t5, s); /* t1 = x1*A = B */ |
| 1221 | 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] | 1222 | uECC_vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */ |
| 1223 | uECC_vli_modSub(Y2, Y2, Y1, curve_p); /* t4 = y2 - y1 */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1224 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1225 | 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] | 1226 | 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] | 1227 | 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] | 1228 | 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] | 1229 | 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] | 1230 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1231 | 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] | 1232 | 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] | 1233 | /* t4 = (y2 - y1)*(B - x3) - E = y3: */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1234 | uECC_vli_modSub(Y2, Y2, Y1, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1235 | |
Manuel Pégourié-Gonnard | c78d86b | 2019-11-04 10:18:42 +0100 | [diff] [blame] | 1236 | 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] | 1237 | uECC_vli_modSub(t7, t7, t6, curve_p); /* t7 = F - (B + C) = x3' */ |
| 1238 | 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] | 1239 | 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] | 1240 | /* t2 = (y2+y1)*(x3' - B) - E = y3': */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1241 | uECC_vli_modSub(Y1, t6, Y1, curve_p); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1242 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1243 | uECC_vli_set(X1, t7); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1244 | } |
| 1245 | |
Manuel Pégourié-Gonnard | 27926d6 | 2019-11-04 11:26:46 +0100 | [diff] [blame] | 1246 | 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] | 1247 | const uECC_word_t * scalar, |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 1248 | const uECC_word_t * initial_Z) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1249 | { |
| 1250 | /* R0 and R1 */ |
| 1251 | uECC_word_t Rx[2][NUM_ECC_WORDS]; |
| 1252 | uECC_word_t Ry[2][NUM_ECC_WORDS]; |
| 1253 | uECC_word_t z[NUM_ECC_WORDS]; |
| 1254 | bitcount_t i; |
| 1255 | uECC_word_t nb; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 1256 | const wordcount_t num_words = NUM_ECC_WORDS; |
| 1257 | const bitcount_t num_bits = NUM_ECC_BITS + 1; /* from regularize_k */ |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 1258 | ecc_wait_state_t wait_state; |
| 1259 | ecc_wait_state_t * const ws = g_rng_function ? &wait_state : NULL; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1260 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1261 | uECC_vli_set(Rx[1], point); |
| 1262 | uECC_vli_set(Ry[1], point + num_words); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1263 | |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1264 | XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1265 | |
| 1266 | for (i = num_bits - 2; i > 0; --i) { |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 1267 | ecc_wait_state_reset(ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1268 | nb = !uECC_vli_testBit(scalar, i); |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1269 | XYcZ_addC_rnd(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], ws); |
| 1270 | 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] | 1271 | } |
| 1272 | |
Manuel Pégourié-Gonnard | d5e503e | 2019-10-31 12:53:44 +0100 | [diff] [blame] | 1273 | ecc_wait_state_reset(ws); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1274 | nb = !uECC_vli_testBit(scalar, 0); |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1275 | 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] | 1276 | |
| 1277 | /* Find final 1/Z value. */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1278 | 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] | 1279 | uECC_vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */ |
| 1280 | 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] | 1281 | uECC_vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0))*/ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1282 | /* yP / (xP * Yb * (X1 - X0)) */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1283 | uECC_vli_modMult_fast(z, z, point + num_words); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1284 | /* Xb * yP / (xP * Yb * (X1 - X0)) */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1285 | uECC_vli_modMult_fast(z, z, Rx[1 - nb]); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1286 | /* End 1/Z calculation */ |
| 1287 | |
Manuel Pégourié-Gonnard | 938f53f | 2019-10-29 11:23:43 +0100 | [diff] [blame] | 1288 | 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] | 1289 | apply_z(Rx[0], Ry[0], z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1290 | |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 1291 | uECC_vli_set(result, Rx[0]); |
| 1292 | uECC_vli_set(result + num_words, Ry[0]); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1293 | } |
| 1294 | |
Manuel Pégourié-Gonnard | 27926d6 | 2019-11-04 11:26:46 +0100 | [diff] [blame] | 1295 | 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] | 1296 | uECC_word_t *k1) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1297 | { |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 1298 | bitcount_t num_n_bits = NUM_ECC_BITS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1299 | |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 1300 | uECC_word_t carry = uECC_vli_add(k0, k, curve_n) || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1301 | uECC_vli_testBit(k0, num_n_bits); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1302 | |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 1303 | uECC_vli_add(k1, k0, curve_n); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1304 | |
| 1305 | return carry; |
| 1306 | } |
| 1307 | |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1308 | int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 1309 | const uECC_word_t * scalar) |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1310 | { |
| 1311 | uECC_word_t tmp[NUM_ECC_WORDS]; |
| 1312 | uECC_word_t s[NUM_ECC_WORDS]; |
| 1313 | uECC_word_t *k2[2] = {tmp, s}; |
Manuel Pégourié-Gonnard | 78a7e35 | 2019-11-04 12:31:06 +0100 | [diff] [blame] | 1314 | wordcount_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1315 | uECC_word_t carry; |
| 1316 | uECC_word_t *initial_Z = 0; |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1317 | int r = UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1318 | volatile int problem; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1319 | |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 1320 | /* Protect against faults modifying curve paremeters in flash */ |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1321 | problem = -1; |
| 1322 | problem = uECC_check_curve_integrity(); |
| 1323 | if (problem != 0) { |
| 1324 | return UECC_FAULT_DETECTED; |
| 1325 | } |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1326 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1327 | if (problem != 0) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1328 | return UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 1329 | } |
| 1330 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1331 | /* Protects against invalid curve attacks */ |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1332 | problem = -1; |
| 1333 | problem = uECC_valid_point(point); |
| 1334 | if (problem != 0) { |
| 1335 | /* invalid input, can happen without fault */ |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1336 | return UECC_FAILURE; |
Manuel Pégourié-Gonnard | e714332 | 2019-11-15 10:47:45 +0100 | [diff] [blame] | 1337 | } |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1338 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1339 | if (problem != 0) { |
| 1340 | /* failure on second check means fault, though */ |
| 1341 | return UECC_FAULT_DETECTED; |
| 1342 | } |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 1343 | |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1344 | /* Regularize the bitcount for the private key so that attackers cannot use a |
| 1345 | * side channel attack to learn the number of leading zeros. */ |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 1346 | carry = regularize_k(scalar, tmp, s); |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1347 | |
| 1348 | /* If an RNG function was specified, get a random initial Z value to |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1349 | * protect against side-channel attacks such as Template SPA */ |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1350 | if (g_rng_function) { |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 1351 | if (uECC_generate_random_int(k2[carry], curve_p, num_words) != UECC_SUCCESS) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1352 | r = UECC_FAILURE; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1353 | goto clear_and_out; |
| 1354 | } |
| 1355 | initial_Z = k2[carry]; |
| 1356 | } |
| 1357 | |
Manuel Pégourié-Gonnard | 3645ac9 | 2019-11-04 11:39:18 +0100 | [diff] [blame] | 1358 | EccPoint_mult(result, point, k2[!carry], initial_Z); |
Manuel Pégourié-Gonnard | 41ab8cb | 2019-11-14 11:59:09 +0100 | [diff] [blame] | 1359 | |
Manuel Pégourié-Gonnard | e714332 | 2019-11-15 10:47:45 +0100 | [diff] [blame] | 1360 | /* Protect against fault injections that would make the resulting |
| 1361 | * point not lie on the intended curve */ |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1362 | problem = -1; |
| 1363 | problem = uECC_valid_point(result); |
| 1364 | if (problem != 0) { |
| 1365 | r = UECC_FAULT_DETECTED; |
| 1366 | goto clear_and_out; |
| 1367 | } |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1368 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1369 | if (problem != 0) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1370 | r = UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 41ab8cb | 2019-11-14 11:59:09 +0100 | [diff] [blame] | 1371 | goto clear_and_out; |
| 1372 | } |
| 1373 | |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 1374 | /* Protect against faults modifying curve paremeters in flash */ |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1375 | problem = -1; |
| 1376 | problem = uECC_check_curve_integrity(); |
| 1377 | if (problem != 0) { |
| 1378 | r = UECC_FAULT_DETECTED; |
| 1379 | goto clear_and_out; |
| 1380 | } |
Arto Kinnunen | ac6d226 | 2020-01-09 10:11:20 +0200 | [diff] [blame] | 1381 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1382 | if (problem != 0) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1383 | r = UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | 2b90961 | 2019-11-21 13:37:00 +0100 | [diff] [blame] | 1384 | goto clear_and_out; |
| 1385 | } |
| 1386 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1387 | r = UECC_SUCCESS; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 1388 | |
| 1389 | clear_and_out: |
| 1390 | /* erasing temporary buffer used to store secret: */ |
| 1391 | mbedtls_platform_zeroize(k2, sizeof(k2)); |
| 1392 | mbedtls_platform_zeroize(tmp, sizeof(tmp)); |
| 1393 | mbedtls_platform_zeroize(s, sizeof(s)); |
| 1394 | |
| 1395 | return r; |
| 1396 | } |
| 1397 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1398 | uECC_word_t EccPoint_compute_public_key(uECC_word_t *result, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 1399 | uECC_word_t *private_key) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1400 | { |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 1401 | return EccPoint_mult_safer(result, curve_G, private_key); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | /* Converts an integer in uECC native format to big-endian bytes. */ |
| 1405 | void uECC_vli_nativeToBytes(uint8_t *bytes, int num_bytes, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1406 | const unsigned int *native) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1407 | { |
| 1408 | wordcount_t i; |
| 1409 | for (i = 0; i < num_bytes; ++i) { |
| 1410 | unsigned b = num_bytes - 1 - i; |
| 1411 | bytes[i] = native[b / uECC_WORD_SIZE] >> (8 * (b % uECC_WORD_SIZE)); |
| 1412 | } |
| 1413 | } |
| 1414 | |
| 1415 | /* Converts big-endian bytes to an integer in uECC native format. */ |
| 1416 | void uECC_vli_bytesToNative(unsigned int *native, const uint8_t *bytes, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1417 | int num_bytes) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1418 | { |
| 1419 | wordcount_t i; |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 1420 | uECC_vli_clear(native); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1421 | for (i = 0; i < num_bytes; ++i) { |
| 1422 | unsigned b = num_bytes - 1 - i; |
| 1423 | native[b / uECC_WORD_SIZE] |= |
| 1424 | (uECC_word_t)bytes[i] << (8 * (b % uECC_WORD_SIZE)); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1429 | wordcount_t num_words) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1430 | { |
| 1431 | uECC_word_t mask = (uECC_word_t)-1; |
| 1432 | uECC_word_t tries; |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 1433 | bitcount_t num_bits = uECC_vli_numBits(top); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1434 | |
| 1435 | if (!g_rng_function) { |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 1436 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
Andrzej Kurek | 090365f | 2020-06-08 11:00:51 -0400 | [diff] [blame] | 1440 | if (g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE) != num_words * uECC_WORD_SIZE) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1441 | return UECC_FAILURE; |
| 1442 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1443 | random[num_words - 1] &= |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1444 | 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] | 1445 | if (!uECC_vli_isZero(random) && |
Manuel Pégourié-Gonnard | 2cb3eea | 2019-11-04 14:43:35 +0100 | [diff] [blame] | 1446 | uECC_vli_cmp(top, random) == 1) { |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 1447 | return UECC_SUCCESS; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1448 | } |
| 1449 | } |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 1450 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1454 | int uECC_valid_point(const uECC_word_t *point) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1455 | { |
| 1456 | uECC_word_t tmp1[NUM_ECC_WORDS]; |
| 1457 | uECC_word_t tmp2[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 1765933 | 2019-11-21 09:27:38 +0100 | [diff] [blame] | 1458 | wordcount_t num_words = NUM_ECC_WORDS; |
Jarno Lamsa | 83d7881 | 2019-12-04 14:40:57 +0200 | [diff] [blame] | 1459 | volatile uECC_word_t diff = 0xffffffff; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1460 | |
| 1461 | /* The point at infinity is invalid. */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1462 | if (EccPoint_isZero(point)) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1463 | return -1; |
| 1464 | } |
| 1465 | |
| 1466 | /* x and y must be smaller than p. */ |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 1467 | if (uECC_vli_cmp_unsafe(curve_p, point) != 1 || |
| 1468 | uECC_vli_cmp_unsafe(curve_p, point + num_words) != 1) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1469 | return -2; |
| 1470 | } |
| 1471 | |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 1472 | uECC_vli_modMult_fast(tmp1, point + num_words, point + num_words); |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1473 | x_side_default(tmp2, point); /* tmp2 = x^3 + ax + b */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1474 | |
| 1475 | /* Make sure that y^2 == x^3 + ax + b */ |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1476 | diff = uECC_vli_equal(tmp1, tmp2); |
| 1477 | if (diff == 0) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1478 | mbedtls_platform_random_delay(); |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1479 | if (diff == 0) { |
| 1480 | return 0; |
| 1481 | } |
| 1482 | } |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1483 | |
Manuel Pégourié-Gonnard | 5c3066a | 2019-11-27 12:27:48 +0100 | [diff] [blame] | 1484 | return -3; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1485 | } |
| 1486 | |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1487 | int uECC_valid_public_key(const uint8_t *public_key) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1488 | { |
| 1489 | |
| 1490 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 1491 | |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 1492 | uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1493 | uECC_vli_bytesToNative( |
Manuel Pégourié-Gonnard | 1765933 | 2019-11-21 09:27:38 +0100 | [diff] [blame] | 1494 | _public + NUM_ECC_WORDS, |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 1495 | public_key + NUM_ECC_BYTES, |
| 1496 | NUM_ECC_BYTES); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1497 | |
Manuel Pégourié-Gonnard | a611508 | 2019-11-21 10:29:14 +0100 | [diff] [blame] | 1498 | if (memcmp(_public, curve_G, NUM_ECC_WORDS * 2) == 0) { |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1499 | return -4; |
| 1500 | } |
| 1501 | |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 1502 | return uECC_valid_point(_public); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1503 | } |
| 1504 | |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 1505 | int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1506 | { |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 1507 | int ret = UECC_FAULT_DETECTED; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1508 | uECC_word_t _private[NUM_ECC_WORDS]; |
| 1509 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 1510 | |
| 1511 | uECC_vli_bytesToNative( |
| 1512 | _private, |
| 1513 | private_key, |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 1514 | BITS_TO_BYTES(NUM_ECC_BITS)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1515 | |
| 1516 | /* 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] | 1517 | if (uECC_vli_isZero(_private)) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1518 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1519 | } |
| 1520 | |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 1521 | if (uECC_vli_cmp(curve_n, _private) != 1) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1522 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1523 | } |
| 1524 | |
| 1525 | /* Compute public key. */ |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 1526 | ret = EccPoint_compute_public_key(_public, _private); |
| 1527 | if (ret != UECC_SUCCESS) { |
| 1528 | return ret; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1529 | } |
| 1530 | |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 1531 | uECC_vli_nativeToBytes(public_key, NUM_ECC_BYTES, _public); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1532 | uECC_vli_nativeToBytes( |
| 1533 | public_key + |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 1534 | NUM_ECC_BYTES, NUM_ECC_BYTES, _public + NUM_ECC_WORDS); |
Andrzej Kurek | cf3e35c | 2020-07-15 22:32:08 -0400 | [diff] [blame] | 1535 | |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 1536 | return ret; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1537 | } |