blob: d432a2e6ad18971626a8a6ed56dfedef56001c64 [file] [log] [blame]
Jarno Lamsa18987a42019-04-24 15:40:43 +03001/* ec_dsa.c - TinyCrypt implementation of EC-DSA */
2
Simon Butcher92c3d1f2019-09-09 17:25:08 +01003/*
4 * Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
Jarno Lamsa18987a42019-04-24 15:40:43 +03008/* 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 Kurek0919b142020-07-06 15:28:59 -040014 * this list of conditions and the following disclaimer.
Jarno Lamsa18987a42019-04-24 15:40:43 +030015 * * Redistributions in binary form must reproduce the above copyright notice,
Andrzej Kurek0919b142020-07-06 15:28:59 -040016 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
Jarno Lamsa18987a42019-04-24 15:40:43 +030018 *
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 Kurek0919b142020-07-06 15:28:59 -040037 * - Redistributions of source code must retain the above copyright notice,
38 * this list of conditions and the following disclaimer.
Jarno Lamsa18987a42019-04-24 15:40:43 +030039 *
Andrzej Kurek0919b142020-07-06 15:28:59 -040040 * - 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 Lamsa18987a42019-04-24 15:40:43 +030043 *
Andrzej Kurek0919b142020-07-06 15:28:59 -040044 * - 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 Lamsa18987a42019-04-24 15:40:43 +030047 *
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 Becker36ae7582019-07-23 15:52:35 +010061#if !defined(MBEDTLS_CONFIG_FILE)
62#include "mbedtls/config.h"
63#else
64#include MBEDTLS_CONFIG_FILE
65#endif
66
Jarno Lamsa18987a42019-04-24 15:40:43 +030067#include <tinycrypt/ecc.h>
68#include <tinycrypt/ecc_dsa.h>
Manuel Pégourié-Gonnard72a8c9e2019-11-08 10:21:00 +010069#include "mbedtls/platform_util.h"
Jarno Lamsa18987a42019-04-24 15:40:43 +030070
Jarno Lamsa18987a42019-04-24 15:40:43 +030071static void bits2int(uECC_word_t *native, const uint8_t *bits,
Andrzej Kurek0919b142020-07-06 15:28:59 -040072 unsigned bits_size)
Jarno Lamsa18987a42019-04-24 15:40:43 +030073{
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010074 unsigned num_n_bytes = BITS_TO_BYTES(NUM_ECC_BITS);
Jarno Lamsa18987a42019-04-24 15:40:43 +030075
76 if (bits_size > num_n_bytes) {
77 bits_size = num_n_bytes;
78 }
79
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +010080 uECC_vli_clear(native);
Jarno Lamsa18987a42019-04-24 15:40:43 +030081 uECC_vli_bytesToNative(native, bits, bits_size);
Jarno Lamsa18987a42019-04-24 15:40:43 +030082}
83
84int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
Andrzej Kurek0919b142020-07-06 15:28:59 -040085 unsigned hash_size, uECC_word_t *k, uint8_t *signature)
Jarno Lamsa18987a42019-04-24 15:40:43 +030086{
87
88 uECC_word_t tmp[NUM_ECC_WORDS];
89 uECC_word_t s[NUM_ECC_WORDS];
Jarno Lamsa18987a42019-04-24 15:40:43 +030090 uECC_word_t p[NUM_ECC_WORDS * 2];
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +010091 wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
Andrzej Kurekfd56f402020-05-25 11:52:05 -040092 int r = UECC_FAILURE;
Manuel Pégourié-Gonnardef238282019-11-04 11:19:30 +010093
Jarno Lamsa18987a42019-04-24 15:40:43 +030094
95 /* Make sure 0 < k < curve_n */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +010096 if (uECC_vli_isZero(k) ||
Andrzej Kurek0919b142020-07-06 15:28:59 -040097 uECC_vli_cmp(curve_n, k) != 1) {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +010098 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +030099 }
100
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100101 r = EccPoint_mult_safer(p, curve_G, k);
Andrzej Kurek0919b142020-07-06 15:28:59 -0400102 if (r != UECC_SUCCESS) {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100103 return r;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300104 }
105
106 /* If an RNG function was specified, get a random number
107 to prevent side channel analysis of k. */
Manuel Pégourié-Gonnarda4b42182019-12-18 10:29:58 +0100108 if (!uECC_get_rng()) {
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100109 uECC_vli_clear(tmp);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300110 tmp[0] = 1;
111 }
Andrzej Kurek3a0df032020-06-12 06:32:13 -0400112 else if (uECC_generate_random_int(tmp, curve_n, num_n_words) != UECC_SUCCESS) {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100113 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300114 }
115
116 /* Prevent side channel analysis of uECC_vli_modInv() to determine
117 bits of k / the private key by premultiplying by a random number */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100118 uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */
Andrzej Kurek0919b142020-07-06 15:28:59 -0400119 uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100120 uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300121
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100122 uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300123
124 /* tmp = d: */
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100125 uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(NUM_ECC_BITS));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300126
127 s[num_n_words - 1] = 0;
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100128 uECC_vli_set(s, p);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100129 uECC_vli_modMult(s, tmp, s, curve_n); /* s = r*d */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300130
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100131 bits2int(tmp, message_hash, hash_size);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100132 uECC_vli_modAdd(s, tmp, s, curve_n); /* s = e + r*d */
133 uECC_vli_modMult(s, s, k, curve_n); /* s = (e + r*d) / k */
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100134 if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100135 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300136 }
137
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100138 uECC_vli_nativeToBytes(signature + NUM_ECC_BYTES, NUM_ECC_BYTES, s);
Andrzej Kurekfd56f402020-05-25 11:52:05 -0400139 return r;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300140}
141
142int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400143 unsigned hash_size, uint8_t *signature)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300144{
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100145 int r;
Manuel Pégourié-Gonnard231bf522019-11-28 12:22:43 +0100146 uECC_word_t _random[2*NUM_ECC_WORDS];
147 uECC_word_t k[NUM_ECC_WORDS];
148 uECC_word_t tries;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400149 volatile const uint8_t *private_key_dup = private_key;
150 volatile const uint8_t *message_hash_dup = message_hash;
Andrzej Kurek0919b142020-07-06 15:28:59 -0400151 volatile unsigned hash_size_dup = hash_size;
152 volatile uint8_t *signature_dup = signature;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300153
154 for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
155 /* Generating _random uniformly at random: */
156 uECC_RNG_Function rng_function = uECC_get_rng();
157 if (!rng_function ||
Andrzej Kurek0919b142020-07-06 15:28:59 -0400158 rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) {
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100159 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300160 }
161
162 // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100163 uECC_vli_mmod(k, _random, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300164
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100165 r = uECC_sign_with_k(private_key, message_hash, hash_size, k, signature);
166 /* don't keep trying if a fault was detected */
167 if (r == UECC_FAULT_DETECTED) {
Andrzej Kurekca609372020-07-08 03:19:02 -0400168 mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES);
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100169 return r;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300170 }
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100171 if (r == UECC_SUCCESS) {
Andrzej Kurek0919b142020-07-06 15:28:59 -0400172 if (private_key_dup != private_key || message_hash_dup != message_hash ||
173 hash_size_dup != hash_size || signature_dup != signature) {
Andrzej Kurekca609372020-07-08 03:19:02 -0400174 mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES);
Andrzej Kurek0919b142020-07-06 15:28:59 -0400175 return UECC_FAULT_DETECTED;
176 }
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100177 return UECC_SUCCESS;
178 }
179 /* else keep trying */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300180 }
Manuel Pégourié-Gonnard9d6a5352019-11-25 13:06:05 +0100181 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300182}
183
184static bitcount_t smax(bitcount_t a, bitcount_t b)
185{
186 return (a > b ? a : b);
187}
188
189int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100190 unsigned hash_size, const uint8_t *signature)
Jarno Lamsa18987a42019-04-24 15:40:43 +0300191{
192
193 uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
194 uECC_word_t z[NUM_ECC_WORDS];
195 uECC_word_t sum[NUM_ECC_WORDS * 2];
196 uECC_word_t rx[NUM_ECC_WORDS];
197 uECC_word_t ry[NUM_ECC_WORDS];
198 uECC_word_t tx[NUM_ECC_WORDS];
199 uECC_word_t ty[NUM_ECC_WORDS];
200 uECC_word_t tz[NUM_ECC_WORDS];
201 const uECC_word_t *points[4];
202 const uECC_word_t *point;
203 bitcount_t num_bits;
204 bitcount_t i;
Andrzej Kureke601bce2020-05-29 11:20:04 -0400205 bitcount_t flow_control;
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100206 volatile uECC_word_t diff;
Andrzej Kurek74f7d0f2020-07-06 14:28:12 -0400207
Jarno Lamsa18987a42019-04-24 15:40:43 +0300208 uECC_word_t _public[NUM_ECC_WORDS * 2];
209 uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
Manuel Pégourié-Gonnard17659332019-11-21 09:27:38 +0100210 wordcount_t num_words = NUM_ECC_WORDS;
Manuel Pégourié-Gonnard30833f22019-11-21 09:46:52 +0100211 wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
Manuel Pégourié-Gonnardad166d82019-11-04 15:37:42 +0100212
Jarno Lamsa18987a42019-04-24 15:40:43 +0300213 rx[num_n_words - 1] = 0;
214 r[num_n_words - 1] = 0;
215 s[num_n_words - 1] = 0;
Andrzej Kureke601bce2020-05-29 11:20:04 -0400216 flow_control = 1;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300217
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100218 uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
219 uECC_vli_bytesToNative(_public + num_words, public_key + NUM_ECC_BYTES,
Andrzej Kurek0919b142020-07-06 15:28:59 -0400220 NUM_ECC_BYTES);
Manuel Pégourié-Gonnard72c17642019-11-21 09:34:09 +0100221 uECC_vli_bytesToNative(r, signature, NUM_ECC_BYTES);
222 uECC_vli_bytesToNative(s, signature + NUM_ECC_BYTES, NUM_ECC_BYTES);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300223
224 /* r, s must not be 0. */
Manuel Pégourié-Gonnardf3899fc2019-11-04 12:44:43 +0100225 if (uECC_vli_isZero(r) || uECC_vli_isZero(s)) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100226 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300227 }
228
229 /* r, s must be < n. */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100230 if (uECC_vli_cmp_unsafe(curve_n, r) != 1 ||
Andrzej Kurek0919b142020-07-06 15:28:59 -0400231 uECC_vli_cmp_unsafe(curve_n, s) != 1) {
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100232 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300233 }
234
Andrzej Kureke601bce2020-05-29 11:20:04 -0400235 flow_control++;
236
Jarno Lamsa18987a42019-04-24 15:40:43 +0300237 /* Calculate u1 and u2. */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100238 uECC_vli_modInv(z, s, curve_n); /* z = 1/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300239 u1[num_n_words - 1] = 0;
Manuel Pégourié-Gonnard1a533712019-11-21 12:00:43 +0100240 bits2int(u1, message_hash, hash_size);
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100241 uECC_vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
242 uECC_vli_modMult(u2, r, z, curve_n); /* u2 = r/s */
Jarno Lamsa18987a42019-04-24 15:40:43 +0300243
244 /* Calculate sum = G + Q. */
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100245 uECC_vli_set(sum, _public);
246 uECC_vli_set(sum + num_words, _public + num_words);
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +0100247 uECC_vli_set(tx, curve_G);
248 uECC_vli_set(ty, curve_G + num_words);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100249 uECC_vli_modSub(z, sum, tx, curve_p); /* z = x2 - x1 */
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100250 XYcZ_add(tx, ty, sum, sum + num_words);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100251 uECC_vli_modInv(z, z, curve_p); /* z = 1/z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100252 apply_z(sum, sum + num_words, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300253
Andrzej Kureke601bce2020-05-29 11:20:04 -0400254 flow_control++;
255
Jarno Lamsa18987a42019-04-24 15:40:43 +0300256 /* Use Shamir's trick to calculate u1*G + u2*Q */
257 points[0] = 0;
Manuel Pégourié-Gonnarda6115082019-11-21 10:29:14 +0100258 points[1] = curve_G;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300259 points[2] = _public;
260 points[3] = sum;
Manuel Pégourié-Gonnard2bf5a122019-11-04 12:56:59 +0100261 num_bits = smax(uECC_vli_numBits(u1),
262 uECC_vli_numBits(u2));
Jarno Lamsa18987a42019-04-24 15:40:43 +0300263
264 point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
Andrzej Kurek0919b142020-07-06 15:28:59 -0400265 ((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100266 uECC_vli_set(rx, point);
267 uECC_vli_set(ry, point + num_words);
Manuel Pégourié-Gonnard94e48492019-11-04 12:47:28 +0100268 uECC_vli_clear(z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300269 z[0] = 1;
Andrzej Kureke601bce2020-05-29 11:20:04 -0400270 flow_control++;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300271
272 for (i = num_bits - 2; i >= 0; --i) {
273 uECC_word_t index;
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100274 double_jacobian_default(rx, ry, z);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300275
276 index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
277 point = points[index];
278 if (point) {
Manuel Pégourié-Gonnardcbbb0f02019-11-04 13:02:04 +0100279 uECC_vli_set(tx, point);
280 uECC_vli_set(ty, point + num_words);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100281 apply_z(tx, ty, z);
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100282 uECC_vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */
Manuel Pégourié-Gonnardbe5f8332019-11-21 11:02:38 +0100283 XYcZ_add(tx, ty, rx, ry);
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100284 uECC_vli_modMult_fast(z, z, tz);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300285 }
Andrzej Kureke601bce2020-05-29 11:20:04 -0400286 flow_control++;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300287 }
288
Manuel Pégourié-Gonnard4d8777c2019-11-21 10:02:58 +0100289 uECC_vli_modInv(z, z, curve_p); /* Z = 1/Z */
Manuel Pégourié-Gonnardc3ec14c2019-11-04 12:12:00 +0100290 apply_z(rx, ry, z);
Andrzej Kureke601bce2020-05-29 11:20:04 -0400291 flow_control++;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300292
293 /* v = x1 (mod n) */
Manuel Pégourié-Gonnard356d8592019-11-21 10:23:05 +0100294 if (uECC_vli_cmp_unsafe(curve_n, rx) != 1) {
295 uECC_vli_sub(rx, rx, curve_n);
Jarno Lamsa18987a42019-04-24 15:40:43 +0300296 }
297
298 /* Accept only if v == r. */
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100299 diff = uECC_vli_equal(rx, r);
300 if (diff == 0) {
Andrzej Kurek0919b142020-07-06 15:28:59 -0400301 flow_control++;
302 mbedtls_platform_random_delay();
303
304 /* Re-check the condition and test if the control flow is as expected.
305 * 1 (base value) + num_bits - 1 (from the loop) + 5 incrementations.
306 */
Andrzej Kureke601bce2020-05-29 11:20:04 -0400307 if (diff == 0 && flow_control == (num_bits + 5)) {
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100308 return UECC_SUCCESS;
309 }
310 else {
Manuel Pégourié-Gonnard4d6186b2019-11-25 10:53:24 +0100311 return UECC_FAULT_DETECTED;
Manuel Pégourié-Gonnarde6d6f172019-11-06 11:14:38 +0100312 }
313 }
Manuel Pégourié-Gonnard10d8e8e2019-11-06 10:30:26 +0100314
315 return UECC_FAILURE;
Jarno Lamsa18987a42019-04-24 15:40:43 +0300316}