Improve the usage of uECC_RNG_Function

Since the mbed TLS implementation of rng wrapper returns the size of random
data generated upon success - check for it explicitly.
Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2020-06-08 11:00:51 -04:00
parent 478b05c34c
commit 090365fe60
No known key found for this signature in database
GPG Key ID: 89A90840DC388527
4 changed files with 8 additions and 8 deletions

View File

@ -163,9 +163,9 @@ int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
/* uECC_RNG_Function type
* The RNG function should fill 'size' random bytes into 'dest'. It should
* return 1 if 'dest' was filled with random data, or 0 if the random data could
* not be generated. The filled-in values should be either truly random, or from
* a cryptographically-secure PRNG.
* return 'size' if 'dest' was filled with random data of 'size' length, or 0
* if the random data could not be generated. The filled-in values should be
* either truly random, or from a cryptographically-secure PRNG.
*
* A correctly functioning RNG function must be set (using uECC_set_rng())
* before calling uECC_make_key() or uECC_sign().
@ -181,8 +181,8 @@ typedef int(*uECC_RNG_Function)(uint8_t *dest, unsigned int size);
/*
* @brief Set the function that will be used to generate random bytes. The RNG
* function should return 1 if the random data was generated, or 0 if the random
* data could not be generated.
* function should return 'size' if the random data of length 'size' was
* generated, or 0 if the random data could not be generated.
*
* @note On platforms where there is no predefined RNG function, this must be
* called before uECC_make_key() or uECC_sign() are used.

View File

@ -1169,7 +1169,7 @@ int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
}
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
if (g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE) != num_words * uECC_WORD_SIZE) {
return 0;
}
random[num_words - 1] &=

View File

@ -119,7 +119,7 @@ int uECC_make_key(uint8_t *public_key, uint8_t *private_key)
/* Generating _private uniformly at random: */
uECC_RNG_Function rng_function = uECC_get_rng();
if (!rng_function ||
!rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE)) {
rng_function((uint8_t *)_random, 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) != 2 * NUM_ECC_WORDS*uECC_WORD_SIZE) {
return UECC_FAILURE;
}

View File

@ -151,7 +151,7 @@ int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
/* Generating _random uniformly at random: */
uECC_RNG_Function rng_function = uECC_get_rng();
if (!rng_function ||
!rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE)) {
rng_function((uint8_t *)_random, 2*NUM_ECC_WORDS*uECC_WORD_SIZE) != 2*NUM_ECC_WORDS*uECC_WORD_SIZE) {
return UECC_FAILURE;
}