Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 1 | /* ec_dsa.c - TinyCrypt implementation of EC-DSA */ |
| 2 | |
Simon Butcher | 92c3d1f | 2019-09-09 17:25:08 +0100 | [diff] [blame] | 3 | /* |
| 4 | * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved. |
| 5 | * SPDX-License-Identifier: BSD-3-Clause |
| 6 | */ |
| 7 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 8 | /* Copyright (c) 2014, Kenneth MacKay |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * Redistribution and use in source and binary forms, with or without |
| 12 | * modification, are permitted provided that the following conditions are met: |
| 13 | * * Redistributions of source code must retain the above copyright notice, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 14 | * this list of conditions and the following disclaimer. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 15 | * * Redistributions in binary form must reproduce the above copyright notice, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 16 | * this list of conditions and the following disclaimer in the documentation |
| 17 | * and/or other materials provided with the distribution. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE.*/ |
| 30 | |
| 31 | /* |
| 32 | * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. |
| 33 | * |
| 34 | * Redistribution and use in source and binary forms, with or without |
| 35 | * modification, are permitted provided that the following conditions are met: |
| 36 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 37 | * - Redistributions of source code must retain the above copyright notice, |
| 38 | * this list of conditions and the following disclaimer. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 39 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 40 | * - Redistributions in binary form must reproduce the above copyright |
| 41 | * notice, this list of conditions and the following disclaimer in the |
| 42 | * documentation and/or other materials provided with the distribution. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 43 | * |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 44 | * - Neither the name of Intel Corporation nor the names of its contributors |
| 45 | * may be used to endorse or promote products derived from this software |
| 46 | * without specific prior written permission. |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 47 | * |
| 48 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 49 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 52 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 53 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 54 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 55 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 56 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 57 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 58 | * POSSIBILITY OF SUCH DAMAGE. |
| 59 | */ |
| 60 | |
Hanno Becker | 36ae758 | 2019-07-23 15:52:35 +0100 | [diff] [blame] | 61 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 62 | #include "mbedtls/config.h" |
| 63 | #else |
| 64 | #include MBEDTLS_CONFIG_FILE |
| 65 | #endif |
| 66 | |
Andrzej Kurek | 7e62c31 | 2020-10-14 12:02:40 +0200 | [diff] [blame] | 67 | #if defined(MBEDTLS_USE_TINYCRYPT) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 68 | #include <tinycrypt/ecc.h> |
| 69 | #include <tinycrypt/ecc_dsa.h> |
Manuel Pégourié-Gonnard | 72a8c9e | 2019-11-08 10:21:00 +0100 | [diff] [blame] | 70 | #include "mbedtls/platform_util.h" |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 71 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 72 | static void bits2int(uECC_word_t *native, const uint8_t *bits, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 73 | unsigned bits_size) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 74 | { |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 75 | unsigned num_n_bytes = BITS_TO_BYTES(NUM_ECC_BITS); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 76 | |
| 77 | if (bits_size > num_n_bytes) { |
| 78 | bits_size = num_n_bytes; |
| 79 | } |
| 80 | |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 81 | uECC_vli_clear(native); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 82 | uECC_vli_bytesToNative(native, bits, bits_size); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 86 | unsigned hash_size, uECC_word_t *k, uint8_t *signature) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 87 | { |
| 88 | |
| 89 | uECC_word_t tmp[NUM_ECC_WORDS]; |
| 90 | uECC_word_t s[NUM_ECC_WORDS]; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 91 | uECC_word_t p[NUM_ECC_WORDS * 2]; |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 92 | wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS); |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 93 | int r = UECC_FAILURE; |
Manuel Pégourié-Gonnard | ef23828 | 2019-11-04 11:19:30 +0100 | [diff] [blame] | 94 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 95 | |
| 96 | /* Make sure 0 < k < curve_n */ |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 97 | if (uECC_vli_isZero(k) || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 98 | uECC_vli_cmp(curve_n, k) != 1) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 99 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 100 | } |
| 101 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 102 | r = EccPoint_mult_safer(p, curve_G, k); |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 103 | if (r != UECC_SUCCESS) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 104 | return r; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | /* If an RNG function was specified, get a random number |
| 108 | to prevent side channel analysis of k. */ |
Manuel Pégourié-Gonnard | a4b4218 | 2019-12-18 10:29:58 +0100 | [diff] [blame] | 109 | if (!uECC_get_rng()) { |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 110 | uECC_vli_clear(tmp); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 111 | tmp[0] = 1; |
| 112 | } |
Andrzej Kurek | 3a0df03 | 2020-06-12 06:32:13 -0400 | [diff] [blame] | 113 | else if (uECC_generate_random_int(tmp, curve_n, num_n_words) != UECC_SUCCESS) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 114 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* Prevent side channel analysis of uECC_vli_modInv() to determine |
| 118 | bits of k / the private key by premultiplying by a random number */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 119 | uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */ |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 120 | uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 121 | uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 122 | |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 123 | uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 124 | |
| 125 | /* tmp = d: */ |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 126 | uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(NUM_ECC_BITS)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 127 | |
| 128 | s[num_n_words - 1] = 0; |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 129 | uECC_vli_set(s, p); |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 130 | uECC_vli_modMult(s, tmp, s, curve_n); /* s = r*d */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 131 | |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 132 | bits2int(tmp, message_hash, hash_size); |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 133 | uECC_vli_modAdd(s, tmp, s, curve_n); /* s = e + r*d */ |
| 134 | uECC_vli_modMult(s, s, k, curve_n); /* s = (e + r*d) / k */ |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 135 | if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 136 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 139 | uECC_vli_nativeToBytes(signature + NUM_ECC_BYTES, NUM_ECC_BYTES, s); |
Andrzej Kurek | fd56f40 | 2020-05-25 11:52:05 -0400 | [diff] [blame] | 140 | return r; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 144 | unsigned hash_size, uint8_t *signature) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 145 | { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 146 | int r; |
Manuel Pégourié-Gonnard | 231bf52 | 2019-11-28 12:22:43 +0100 | [diff] [blame] | 147 | uECC_word_t _random[2*NUM_ECC_WORDS]; |
| 148 | uECC_word_t k[NUM_ECC_WORDS]; |
| 149 | uECC_word_t tries; |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 150 | volatile const uint8_t *private_key_dup = private_key; |
| 151 | volatile const uint8_t *message_hash_dup = message_hash; |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 152 | volatile unsigned hash_size_dup = hash_size; |
| 153 | volatile uint8_t *signature_dup = signature; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 154 | |
| 155 | for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { |
| 156 | /* Generating _random uniformly at random: */ |
| 157 | uECC_RNG_Function rng_function = uECC_get_rng(); |
| 158 | if (!rng_function || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 159 | rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) { |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 160 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // computing k as modular reduction of _random (see FIPS 186.4 B.5.1): |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 164 | uECC_vli_mmod(k, _random, curve_n); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 165 | |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 166 | r = uECC_sign_with_k(private_key, message_hash, hash_size, k, signature); |
| 167 | /* don't keep trying if a fault was detected */ |
| 168 | if (r == UECC_FAULT_DETECTED) { |
Andrzej Kurek | ca60937 | 2020-07-08 03:19:02 -0400 | [diff] [blame] | 169 | mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES); |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 170 | return r; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 171 | } |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 172 | if (r == UECC_SUCCESS) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 173 | if (private_key_dup != private_key || message_hash_dup != message_hash || |
| 174 | hash_size_dup != hash_size || signature_dup != signature) { |
Andrzej Kurek | ca60937 | 2020-07-08 03:19:02 -0400 | [diff] [blame] | 175 | mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES); |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 176 | return UECC_FAULT_DETECTED; |
| 177 | } |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 178 | return UECC_SUCCESS; |
| 179 | } |
| 180 | /* else keep trying */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 181 | } |
Manuel Pégourié-Gonnard | 9d6a535 | 2019-11-25 13:06:05 +0100 | [diff] [blame] | 182 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static bitcount_t smax(bitcount_t a, bitcount_t b) |
| 186 | { |
| 187 | return (a > b ? a : b); |
| 188 | } |
| 189 | |
| 190 | int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash, |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 191 | unsigned hash_size, const uint8_t *signature) |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 192 | { |
| 193 | |
| 194 | uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS]; |
| 195 | uECC_word_t z[NUM_ECC_WORDS]; |
| 196 | uECC_word_t sum[NUM_ECC_WORDS * 2]; |
| 197 | uECC_word_t rx[NUM_ECC_WORDS]; |
| 198 | uECC_word_t ry[NUM_ECC_WORDS]; |
| 199 | uECC_word_t tx[NUM_ECC_WORDS]; |
| 200 | uECC_word_t ty[NUM_ECC_WORDS]; |
| 201 | uECC_word_t tz[NUM_ECC_WORDS]; |
| 202 | const uECC_word_t *points[4]; |
| 203 | const uECC_word_t *point; |
| 204 | bitcount_t num_bits; |
| 205 | bitcount_t i; |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 206 | bitcount_t flow_control; |
Manuel Pégourié-Gonnard | e6d6f17 | 2019-11-06 11:14:38 +0100 | [diff] [blame] | 207 | volatile uECC_word_t diff; |
Andrzej Kurek | 74f7d0f | 2020-07-06 14:28:12 -0400 | [diff] [blame] | 208 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 209 | uECC_word_t _public[NUM_ECC_WORDS * 2]; |
| 210 | uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS]; |
Manuel Pégourié-Gonnard | 1765933 | 2019-11-21 09:27:38 +0100 | [diff] [blame] | 211 | wordcount_t num_words = NUM_ECC_WORDS; |
Manuel Pégourié-Gonnard | 30833f2 | 2019-11-21 09:46:52 +0100 | [diff] [blame] | 212 | wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS); |
Manuel Pégourié-Gonnard | ad166d8 | 2019-11-04 15:37:42 +0100 | [diff] [blame] | 213 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 214 | rx[num_n_words - 1] = 0; |
| 215 | r[num_n_words - 1] = 0; |
| 216 | s[num_n_words - 1] = 0; |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 217 | flow_control = 1; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 218 | |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 219 | uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES); |
| 220 | uECC_vli_bytesToNative(_public + num_words, public_key + NUM_ECC_BYTES, |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 221 | NUM_ECC_BYTES); |
Manuel Pégourié-Gonnard | 72c1764 | 2019-11-21 09:34:09 +0100 | [diff] [blame] | 222 | uECC_vli_bytesToNative(r, signature, NUM_ECC_BYTES); |
| 223 | uECC_vli_bytesToNative(s, signature + NUM_ECC_BYTES, NUM_ECC_BYTES); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 224 | |
| 225 | /* r, s must not be 0. */ |
Manuel Pégourié-Gonnard | f3899fc | 2019-11-04 12:44:43 +0100 | [diff] [blame] | 226 | if (uECC_vli_isZero(r) || uECC_vli_isZero(s)) { |
Manuel Pégourié-Gonnard | 10d8e8e | 2019-11-06 10:30:26 +0100 | [diff] [blame] | 227 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* r, s must be < n. */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 231 | if (uECC_vli_cmp_unsafe(curve_n, r) != 1 || |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 232 | uECC_vli_cmp_unsafe(curve_n, s) != 1) { |
Manuel Pégourié-Gonnard | 10d8e8e | 2019-11-06 10:30:26 +0100 | [diff] [blame] | 233 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 234 | } |
| 235 | |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 236 | flow_control++; |
| 237 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 238 | /* Calculate u1 and u2. */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 239 | uECC_vli_modInv(z, s, curve_n); /* z = 1/s */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 240 | u1[num_n_words - 1] = 0; |
Manuel Pégourié-Gonnard | 1a53371 | 2019-11-21 12:00:43 +0100 | [diff] [blame] | 241 | bits2int(u1, message_hash, hash_size); |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 242 | uECC_vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */ |
| 243 | uECC_vli_modMult(u2, r, z, curve_n); /* u2 = r/s */ |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 244 | |
| 245 | /* Calculate sum = G + Q. */ |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 246 | uECC_vli_set(sum, _public); |
| 247 | uECC_vli_set(sum + num_words, _public + num_words); |
Manuel Pégourié-Gonnard | a611508 | 2019-11-21 10:29:14 +0100 | [diff] [blame] | 248 | uECC_vli_set(tx, curve_G); |
| 249 | uECC_vli_set(ty, curve_G + num_words); |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 250 | uECC_vli_modSub(z, sum, tx, curve_p); /* z = x2 - x1 */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 251 | XYcZ_add(tx, ty, sum, sum + num_words); |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 252 | uECC_vli_modInv(z, z, curve_p); /* z = 1/z */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 253 | apply_z(sum, sum + num_words, z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 254 | |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 255 | flow_control++; |
| 256 | |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 257 | /* Use Shamir's trick to calculate u1*G + u2*Q */ |
| 258 | points[0] = 0; |
Manuel Pégourié-Gonnard | a611508 | 2019-11-21 10:29:14 +0100 | [diff] [blame] | 259 | points[1] = curve_G; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 260 | points[2] = _public; |
| 261 | points[3] = sum; |
Manuel Pégourié-Gonnard | 2bf5a12 | 2019-11-04 12:56:59 +0100 | [diff] [blame] | 262 | num_bits = smax(uECC_vli_numBits(u1), |
| 263 | uECC_vli_numBits(u2)); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 264 | |
| 265 | point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) | |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 266 | ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)]; |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 267 | uECC_vli_set(rx, point); |
| 268 | uECC_vli_set(ry, point + num_words); |
Manuel Pégourié-Gonnard | 94e4849 | 2019-11-04 12:47:28 +0100 | [diff] [blame] | 269 | uECC_vli_clear(z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 270 | z[0] = 1; |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 271 | flow_control++; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 272 | |
| 273 | for (i = num_bits - 2; i >= 0; --i) { |
| 274 | uECC_word_t index; |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 275 | double_jacobian_default(rx, ry, z); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 276 | |
| 277 | index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1); |
| 278 | point = points[index]; |
| 279 | if (point) { |
Manuel Pégourié-Gonnard | cbbb0f0 | 2019-11-04 13:02:04 +0100 | [diff] [blame] | 280 | uECC_vli_set(tx, point); |
| 281 | uECC_vli_set(ty, point + num_words); |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 282 | apply_z(tx, ty, z); |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 283 | uECC_vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */ |
Manuel Pégourié-Gonnard | be5f833 | 2019-11-21 11:02:38 +0100 | [diff] [blame] | 284 | XYcZ_add(tx, ty, rx, ry); |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 285 | uECC_vli_modMult_fast(z, z, tz); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 286 | } |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 287 | flow_control++; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 288 | } |
| 289 | |
Manuel Pégourié-Gonnard | 4d8777c | 2019-11-21 10:02:58 +0100 | [diff] [blame] | 290 | uECC_vli_modInv(z, z, curve_p); /* Z = 1/Z */ |
Manuel Pégourié-Gonnard | c3ec14c | 2019-11-04 12:12:00 +0100 | [diff] [blame] | 291 | apply_z(rx, ry, z); |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 292 | flow_control++; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 293 | |
| 294 | /* v = x1 (mod n) */ |
Manuel Pégourié-Gonnard | 356d859 | 2019-11-21 10:23:05 +0100 | [diff] [blame] | 295 | if (uECC_vli_cmp_unsafe(curve_n, rx) != 1) { |
| 296 | uECC_vli_sub(rx, rx, curve_n); |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Accept only if v == r. */ |
Manuel Pégourié-Gonnard | e6d6f17 | 2019-11-06 11:14:38 +0100 | [diff] [blame] | 300 | diff = uECC_vli_equal(rx, r); |
| 301 | if (diff == 0) { |
Andrzej Kurek | 0919b14 | 2020-07-06 15:28:59 -0400 | [diff] [blame] | 302 | flow_control++; |
| 303 | mbedtls_platform_random_delay(); |
| 304 | |
| 305 | /* Re-check the condition and test if the control flow is as expected. |
| 306 | * 1 (base value) + num_bits - 1 (from the loop) + 5 incrementations. |
| 307 | */ |
Andrzej Kurek | e601bce | 2020-05-29 11:20:04 -0400 | [diff] [blame] | 308 | if (diff == 0 && flow_control == (num_bits + 5)) { |
Manuel Pégourié-Gonnard | e6d6f17 | 2019-11-06 11:14:38 +0100 | [diff] [blame] | 309 | return UECC_SUCCESS; |
| 310 | } |
| 311 | else { |
Manuel Pégourié-Gonnard | 4d6186b | 2019-11-25 10:53:24 +0100 | [diff] [blame] | 312 | return UECC_FAULT_DETECTED; |
Manuel Pégourié-Gonnard | e6d6f17 | 2019-11-06 11:14:38 +0100 | [diff] [blame] | 313 | } |
| 314 | } |
Manuel Pégourié-Gonnard | 10d8e8e | 2019-11-06 10:30:26 +0100 | [diff] [blame] | 315 | |
| 316 | return UECC_FAILURE; |
Jarno Lamsa | 18987a4 | 2019-04-24 15:40:43 +0300 | [diff] [blame] | 317 | } |
Andrzej Kurek | 7e62c31 | 2020-10-14 12:02:40 +0200 | [diff] [blame] | 318 | #endif /* MBEDTLS_USE_TINYCRYPT */ |