2019-04-24 14:40:43 +02:00
|
|
|
/* ec_dsa.c - TinyCrypt implementation of EC-DSA */
|
|
|
|
|
2019-09-09 18:25:08 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Arm Limited (or its affiliates), All Rights Reserved.
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
/* Copyright (c) 2014, Kenneth MacKay
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright notice,
|
2020-07-06 21:28:59 +02:00
|
|
|
* this list of conditions and the following disclaimer.
|
2019-04-24 14:40:43 +02:00
|
|
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
2020-07-06 21:28:59 +02:00
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
2019-04-24 14:40:43 +02:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 by Intel Corporation, All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
2020-07-06 21:28:59 +02:00
|
|
|
* - Redistributions of source code must retain the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer.
|
2019-04-24 14:40:43 +02:00
|
|
|
*
|
2020-07-06 21:28:59 +02:00
|
|
|
* - Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2019-04-24 14:40:43 +02:00
|
|
|
*
|
2020-07-06 21:28:59 +02:00
|
|
|
* - Neither the name of Intel Corporation nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
2019-04-24 14:40:43 +02:00
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-07-23 16:52:35 +02:00
|
|
|
#if !defined(MBEDTLS_CONFIG_FILE)
|
|
|
|
#include "mbedtls/config.h"
|
|
|
|
#else
|
|
|
|
#include MBEDTLS_CONFIG_FILE
|
|
|
|
#endif
|
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
#include <tinycrypt/ecc.h>
|
|
|
|
#include <tinycrypt/ecc_dsa.h>
|
2019-11-08 10:21:00 +01:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
static void bits2int(uECC_word_t *native, const uint8_t *bits,
|
2020-07-06 21:28:59 +02:00
|
|
|
unsigned bits_size)
|
2019-04-24 14:40:43 +02:00
|
|
|
{
|
2019-11-21 09:46:52 +01:00
|
|
|
unsigned num_n_bytes = BITS_TO_BYTES(NUM_ECC_BITS);
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
if (bits_size > num_n_bytes) {
|
|
|
|
bits_size = num_n_bytes;
|
|
|
|
}
|
|
|
|
|
2019-11-04 12:47:28 +01:00
|
|
|
uECC_vli_clear(native);
|
2019-04-24 14:40:43 +02:00
|
|
|
uECC_vli_bytesToNative(native, bits, bits_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
|
2020-07-06 21:28:59 +02:00
|
|
|
unsigned hash_size, uECC_word_t *k, uint8_t *signature)
|
2019-04-24 14:40:43 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
uECC_word_t tmp[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t s[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t p[NUM_ECC_WORDS * 2];
|
2019-11-21 09:46:52 +01:00
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
|
2020-05-25 17:52:05 +02:00
|
|
|
int r = UECC_FAILURE;
|
2019-11-04 11:19:30 +01:00
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
/* Make sure 0 < k < curve_n */
|
2019-11-04 12:44:43 +01:00
|
|
|
if (uECC_vli_isZero(k) ||
|
2020-07-06 21:28:59 +02:00
|
|
|
uECC_vli_cmp(curve_n, k) != 1) {
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:00:43 +01:00
|
|
|
r = EccPoint_mult_safer(p, curve_G, k);
|
2020-07-06 21:28:59 +02:00
|
|
|
if (r != UECC_SUCCESS) {
|
2019-11-25 13:06:05 +01:00
|
|
|
return r;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If an RNG function was specified, get a random number
|
|
|
|
to prevent side channel analysis of k. */
|
2019-12-18 10:29:58 +01:00
|
|
|
if (!uECC_get_rng()) {
|
2019-11-04 12:47:28 +01:00
|
|
|
uECC_vli_clear(tmp);
|
2019-04-24 14:40:43 +02:00
|
|
|
tmp[0] = 1;
|
|
|
|
}
|
2020-06-12 12:32:13 +02:00
|
|
|
else if (uECC_generate_random_int(tmp, curve_n, num_n_words) != UECC_SUCCESS) {
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Prevent side channel analysis of uECC_vli_modInv() to determine
|
|
|
|
bits of k / the private key by premultiplying by a random number */
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */
|
2020-07-06 21:28:59 +02:00
|
|
|
uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */
|
2019-04-24 14:40:43 +02:00
|
|
|
|
2019-11-21 09:34:09 +01:00
|
|
|
uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
/* tmp = d: */
|
2019-11-21 09:46:52 +01:00
|
|
|
uECC_vli_bytesToNative(tmp, private_key, BITS_TO_BYTES(NUM_ECC_BITS));
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
s[num_n_words - 1] = 0;
|
2019-11-04 13:02:04 +01:00
|
|
|
uECC_vli_set(s, p);
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modMult(s, tmp, s, curve_n); /* s = r*d */
|
2019-04-24 14:40:43 +02:00
|
|
|
|
2019-11-21 12:00:43 +01:00
|
|
|
bits2int(tmp, message_hash, hash_size);
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modAdd(s, tmp, s, curve_n); /* s = e + r*d */
|
|
|
|
uECC_vli_modMult(s, s, k, curve_n); /* s = (e + r*d) / k */
|
2019-11-21 09:34:09 +01:00
|
|
|
if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) {
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 09:34:09 +01:00
|
|
|
uECC_vli_nativeToBytes(signature + NUM_ECC_BYTES, NUM_ECC_BYTES, s);
|
2020-05-25 17:52:05 +02:00
|
|
|
return r;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
|
2020-07-06 21:28:59 +02:00
|
|
|
unsigned hash_size, uint8_t *signature)
|
2019-04-24 14:40:43 +02:00
|
|
|
{
|
2019-11-25 13:06:05 +01:00
|
|
|
int r;
|
2019-11-28 12:22:43 +01:00
|
|
|
uECC_word_t _random[2*NUM_ECC_WORDS];
|
|
|
|
uECC_word_t k[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t tries;
|
2020-07-06 20:28:12 +02:00
|
|
|
volatile const uint8_t *private_key_dup = private_key;
|
|
|
|
volatile const uint8_t *message_hash_dup = message_hash;
|
2020-07-06 21:28:59 +02:00
|
|
|
volatile unsigned hash_size_dup = hash_size;
|
|
|
|
volatile uint8_t *signature_dup = signature;
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
|
|
|
/* Generating _random uniformly at random: */
|
|
|
|
uECC_RNG_Function rng_function = uECC_get_rng();
|
|
|
|
if (!rng_function ||
|
2020-07-06 21:28:59 +02:00
|
|
|
rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) {
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_mmod(k, _random, curve_n);
|
2019-04-24 14:40:43 +02:00
|
|
|
|
2019-11-25 13:06:05 +01:00
|
|
|
r = uECC_sign_with_k(private_key, message_hash, hash_size, k, signature);
|
|
|
|
/* don't keep trying if a fault was detected */
|
|
|
|
if (r == UECC_FAULT_DETECTED) {
|
2020-07-08 09:19:02 +02:00
|
|
|
mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES);
|
2019-11-25 13:06:05 +01:00
|
|
|
return r;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
2019-11-25 13:06:05 +01:00
|
|
|
if (r == UECC_SUCCESS) {
|
2020-07-06 21:28:59 +02:00
|
|
|
if (private_key_dup != private_key || message_hash_dup != message_hash ||
|
|
|
|
hash_size_dup != hash_size || signature_dup != signature) {
|
2020-07-08 09:19:02 +02:00
|
|
|
mbedtls_platform_memset(signature, 0, 2*NUM_ECC_BYTES);
|
2020-07-06 21:28:59 +02:00
|
|
|
return UECC_FAULT_DETECTED;
|
|
|
|
}
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_SUCCESS;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
2019-11-25 13:06:05 +01:00
|
|
|
/* else keep trying */
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
2019-11-25 13:06:05 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static bitcount_t smax(bitcount_t a, bitcount_t b)
|
|
|
|
{
|
|
|
|
return (a > b ? a : b);
|
|
|
|
}
|
|
|
|
|
|
|
|
int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
|
2019-11-21 12:00:43 +01:00
|
|
|
unsigned hash_size, const uint8_t *signature)
|
2019-04-24 14:40:43 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
uECC_word_t u1[NUM_ECC_WORDS], u2[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t z[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t sum[NUM_ECC_WORDS * 2];
|
|
|
|
uECC_word_t rx[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t ry[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t tx[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t ty[NUM_ECC_WORDS];
|
|
|
|
uECC_word_t tz[NUM_ECC_WORDS];
|
|
|
|
const uECC_word_t *points[4];
|
|
|
|
const uECC_word_t *point;
|
|
|
|
bitcount_t num_bits;
|
|
|
|
bitcount_t i;
|
2020-05-29 17:20:04 +02:00
|
|
|
bitcount_t flow_control;
|
2019-11-06 11:14:38 +01:00
|
|
|
volatile uECC_word_t diff;
|
2020-07-06 20:28:12 +02:00
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
uECC_word_t _public[NUM_ECC_WORDS * 2];
|
|
|
|
uECC_word_t r[NUM_ECC_WORDS], s[NUM_ECC_WORDS];
|
2019-11-21 09:27:38 +01:00
|
|
|
wordcount_t num_words = NUM_ECC_WORDS;
|
2019-11-21 09:46:52 +01:00
|
|
|
wordcount_t num_n_words = BITS_TO_WORDS(NUM_ECC_BITS);
|
2019-11-04 15:37:42 +01:00
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
rx[num_n_words - 1] = 0;
|
|
|
|
r[num_n_words - 1] = 0;
|
|
|
|
s[num_n_words - 1] = 0;
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control = 1;
|
2019-04-24 14:40:43 +02:00
|
|
|
|
2019-11-21 09:34:09 +01:00
|
|
|
uECC_vli_bytesToNative(_public, public_key, NUM_ECC_BYTES);
|
|
|
|
uECC_vli_bytesToNative(_public + num_words, public_key + NUM_ECC_BYTES,
|
2020-07-06 21:28:59 +02:00
|
|
|
NUM_ECC_BYTES);
|
2019-11-21 09:34:09 +01:00
|
|
|
uECC_vli_bytesToNative(r, signature, NUM_ECC_BYTES);
|
|
|
|
uECC_vli_bytesToNative(s, signature + NUM_ECC_BYTES, NUM_ECC_BYTES);
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
/* r, s must not be 0. */
|
2019-11-04 12:44:43 +01:00
|
|
|
if (uECC_vli_isZero(r) || uECC_vli_isZero(s)) {
|
2019-11-06 10:30:26 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* r, s must be < n. */
|
2019-11-21 10:23:05 +01:00
|
|
|
if (uECC_vli_cmp_unsafe(curve_n, r) != 1 ||
|
2020-07-06 21:28:59 +02:00
|
|
|
uECC_vli_cmp_unsafe(curve_n, s) != 1) {
|
2019-11-06 10:30:26 +01:00
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control++;
|
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
/* Calculate u1 and u2. */
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modInv(z, s, curve_n); /* z = 1/s */
|
2019-04-24 14:40:43 +02:00
|
|
|
u1[num_n_words - 1] = 0;
|
2019-11-21 12:00:43 +01:00
|
|
|
bits2int(u1, message_hash, hash_size);
|
2019-11-21 10:23:05 +01:00
|
|
|
uECC_vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
|
|
|
|
uECC_vli_modMult(u2, r, z, curve_n); /* u2 = r/s */
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
/* Calculate sum = G + Q. */
|
2019-11-04 13:02:04 +01:00
|
|
|
uECC_vli_set(sum, _public);
|
|
|
|
uECC_vli_set(sum + num_words, _public + num_words);
|
2019-11-21 10:29:14 +01:00
|
|
|
uECC_vli_set(tx, curve_G);
|
|
|
|
uECC_vli_set(ty, curve_G + num_words);
|
2019-11-21 10:02:58 +01:00
|
|
|
uECC_vli_modSub(z, sum, tx, curve_p); /* z = x2 - x1 */
|
2019-11-21 11:02:38 +01:00
|
|
|
XYcZ_add(tx, ty, sum, sum + num_words);
|
2019-11-21 10:02:58 +01:00
|
|
|
uECC_vli_modInv(z, z, curve_p); /* z = 1/z */
|
2019-11-04 12:12:00 +01:00
|
|
|
apply_z(sum, sum + num_words, z);
|
2019-04-24 14:40:43 +02:00
|
|
|
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control++;
|
|
|
|
|
2019-04-24 14:40:43 +02:00
|
|
|
/* Use Shamir's trick to calculate u1*G + u2*Q */
|
|
|
|
points[0] = 0;
|
2019-11-21 10:29:14 +01:00
|
|
|
points[1] = curve_G;
|
2019-04-24 14:40:43 +02:00
|
|
|
points[2] = _public;
|
|
|
|
points[3] = sum;
|
2019-11-04 12:56:59 +01:00
|
|
|
num_bits = smax(uECC_vli_numBits(u1),
|
|
|
|
uECC_vli_numBits(u2));
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
point = points[(!!uECC_vli_testBit(u1, num_bits - 1)) |
|
2020-07-06 21:28:59 +02:00
|
|
|
((!!uECC_vli_testBit(u2, num_bits - 1)) << 1)];
|
2019-11-04 13:02:04 +01:00
|
|
|
uECC_vli_set(rx, point);
|
|
|
|
uECC_vli_set(ry, point + num_words);
|
2019-11-04 12:47:28 +01:00
|
|
|
uECC_vli_clear(z);
|
2019-04-24 14:40:43 +02:00
|
|
|
z[0] = 1;
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control++;
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
for (i = num_bits - 2; i >= 0; --i) {
|
|
|
|
uECC_word_t index;
|
2019-11-21 11:02:38 +01:00
|
|
|
double_jacobian_default(rx, ry, z);
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
index = (!!uECC_vli_testBit(u1, i)) | ((!!uECC_vli_testBit(u2, i)) << 1);
|
|
|
|
point = points[index];
|
|
|
|
if (point) {
|
2019-11-04 13:02:04 +01:00
|
|
|
uECC_vli_set(tx, point);
|
|
|
|
uECC_vli_set(ty, point + num_words);
|
2019-11-04 12:12:00 +01:00
|
|
|
apply_z(tx, ty, z);
|
2019-11-21 10:02:58 +01:00
|
|
|
uECC_vli_modSub(tz, rx, tx, curve_p); /* Z = x2 - x1 */
|
2019-11-21 11:02:38 +01:00
|
|
|
XYcZ_add(tx, ty, rx, ry);
|
2019-11-04 12:12:00 +01:00
|
|
|
uECC_vli_modMult_fast(z, z, tz);
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control++;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 10:02:58 +01:00
|
|
|
uECC_vli_modInv(z, z, curve_p); /* Z = 1/Z */
|
2019-11-04 12:12:00 +01:00
|
|
|
apply_z(rx, ry, z);
|
2020-05-29 17:20:04 +02:00
|
|
|
flow_control++;
|
2019-04-24 14:40:43 +02:00
|
|
|
|
|
|
|
/* v = x1 (mod n) */
|
2019-11-21 10:23:05 +01:00
|
|
|
if (uECC_vli_cmp_unsafe(curve_n, rx) != 1) {
|
|
|
|
uECC_vli_sub(rx, rx, curve_n);
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept only if v == r. */
|
2019-11-06 11:14:38 +01:00
|
|
|
diff = uECC_vli_equal(rx, r);
|
|
|
|
if (diff == 0) {
|
2020-07-06 21:28:59 +02:00
|
|
|
flow_control++;
|
|
|
|
mbedtls_platform_random_delay();
|
|
|
|
|
|
|
|
/* Re-check the condition and test if the control flow is as expected.
|
|
|
|
* 1 (base value) + num_bits - 1 (from the loop) + 5 incrementations.
|
|
|
|
*/
|
2020-05-29 17:20:04 +02:00
|
|
|
if (diff == 0 && flow_control == (num_bits + 5)) {
|
2019-11-06 11:14:38 +01:00
|
|
|
return UECC_SUCCESS;
|
|
|
|
}
|
|
|
|
else {
|
2019-11-25 10:53:24 +01:00
|
|
|
return UECC_FAULT_DETECTED;
|
2019-11-06 11:14:38 +01:00
|
|
|
}
|
|
|
|
}
|
2019-11-06 10:30:26 +01:00
|
|
|
|
|
|
|
return UECC_FAILURE;
|
2019-04-24 14:40:43 +02:00
|
|
|
}
|