Return and propagate UECC_FAULT_DETECTED
This commit first changes the return convention of EccPoint_mult_safer() so
that it properly reports when faults are detected. Then all functions that
call it need to be changed to (1) follow the same return convention and (2)
properly propagate UECC_FAULT_DETECTED when it occurs.
Here's the reverse call graph from EccPoint_mult_safer() to the rest of the
library (where return values are translated to the MBEDTLS_ERR_ space) and test
functions (where expected return values are asserted explicitly).
EccPoint_mult_safer()
EccPoint_compute_public_key()
uECC_compute_public_key()
pkparse.c
tests/suites/test_suite_pkparse.function
uECC_make_key_with_d()
uECC_make_key()
ssl_cli.c
ssl_srv.c
tests/suites/test_suite_pk.function
tests/suites/test_suite_tinycrypt.function
uECC_shared_secret()
ssl_tls.c
tests/suites/test_suite_tinycrypt.function
uECC_sign_with_k()
uECC_sign()
pk.c
tests/suites/test_suite_tinycrypt.function
Note: in uECC_sign_with_k() a test for uECC_vli_isZero(p) is suppressed
because it is redundant with a more thorough test (point validity) done at the
end of EccPoint_mult_safer(). This redundancy was introduced in a previous
commit but not noticed earlier.
diff --git a/tinycrypt/ecc_dh.c b/tinycrypt/ecc_dh.c
index 9fe03ca..3070ecf 100644
--- a/tinycrypt/ecc_dh.c
+++ b/tinycrypt/ecc_dh.c
@@ -75,7 +75,7 @@
int uECC_make_key_with_d(uint8_t *public_key, uint8_t *private_key,
unsigned int *d)
{
-
+ int ret;
uECC_word_t _private[NUM_ECC_WORDS];
uECC_word_t _public[NUM_ECC_WORDS * 2];
@@ -85,30 +85,32 @@
mbedtls_platform_memcpy (_private, d, NUM_ECC_BYTES);
/* Computing public-key from private: */
- if (EccPoint_compute_public_key(_public, _private)) {
-
- /* Converting buffers to correct bit order: */
- uECC_vli_nativeToBytes(private_key,
- BITS_TO_BYTES(NUM_ECC_BITS),
- _private);
- uECC_vli_nativeToBytes(public_key,
- NUM_ECC_BYTES,
- _public);
- uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
- NUM_ECC_BYTES,
- _public + NUM_ECC_WORDS);
-
- /* erasing temporary buffer used to store secret: */
- mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
-
- return 1;
+ ret = EccPoint_compute_public_key(_public, _private);
+ if (ret != UECC_SUCCESS) {
+ goto exit;
}
- return 0;
+
+ /* Converting buffers to correct bit order: */
+ uECC_vli_nativeToBytes(private_key,
+ BITS_TO_BYTES(NUM_ECC_BITS),
+ _private);
+ uECC_vli_nativeToBytes(public_key,
+ NUM_ECC_BYTES,
+ _public);
+ uECC_vli_nativeToBytes(public_key + NUM_ECC_BYTES,
+ NUM_ECC_BYTES,
+ _public + NUM_ECC_WORDS);
+
+exit:
+ /* erasing temporary buffer used to store secret: */
+ mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
+
+ return ret;
}
int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
{
-
+ int ret;
uECC_word_t _random[NUM_ECC_WORDS * 2];
uECC_word_t _private[NUM_ECC_WORDS];
uECC_word_t _public[NUM_ECC_WORDS * 2];
@@ -119,14 +121,19 @@
uECC_RNG_Function rng_function = uECC_get_rng();
if (!rng_function ||
!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
- return 0;
+ return UECC_FAILURE;
}
/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
uECC_vli_mmod(_private, _random, curve_n);
/* Computing public-key from private: */
- if (EccPoint_compute_public_key(_public, _private)) {
+ ret = EccPoint_compute_public_key(_public, _private);
+ /* don't try again if a fault was detected */
+ if (ret == UECC_FAULT_DETECTED) {
+ return ret;
+ }
+ if (ret == UECC_SUCCESS) {
/* Converting buffers to correct bit order: */
uECC_vli_nativeToBytes(private_key,
@@ -142,10 +149,10 @@
/* erasing temporary buffer that stored secret: */
mbedtls_platform_memset(_private, 0, NUM_ECC_BYTES);
- return 1;
+ return UECC_SUCCESS;
}
}
- return 0;
+ return UECC_FAILURE;
}
int uECC_shared_secret(const uint8_t *public_key, const uint8_t *private_key,