Increase the Hamming distance of uECC_generate_random_int returns

Signed-off-by: Andrzej Kurek <andrzej.kurek@arm.com>
This commit is contained in:
Andrzej Kurek 2020-06-12 06:32:13 -04:00
parent 8f52a8a8c0
commit 3a0df03364
No known key found for this signature in database
GPG key ID: 89A90840DC388527
3 changed files with 8 additions and 7 deletions

View file

@ -155,7 +155,8 @@ extern const uECC_word_t curve_b[NUM_ECC_WORDS];
* @param random OUT -- random integer in the range 0 < random < top * @param random OUT -- random integer in the range 0 < random < top
* @param top IN -- upper limit * @param top IN -- upper limit
* @param num_words IN -- number of words * @param num_words IN -- number of words
* @return a random integer in the range 0 < random < top * @return UECC_SUCCESS in case of success
* @return UECC_FAILURE upon failure
*/ */
int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top, int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
wordcount_t num_words); wordcount_t num_words);

View file

@ -1080,7 +1080,7 @@ int EccPoint_mult_safer(uECC_word_t * result, const uECC_word_t * point,
/* If an RNG function was specified, get a random initial Z value to /* If an RNG function was specified, get a random initial Z value to
* protect against side-channel attacks such as Template SPA */ * protect against side-channel attacks such as Template SPA */
if (g_rng_function) { if (g_rng_function) {
if (!uECC_generate_random_int(k2[carry], curve_p, num_words)) { if (uECC_generate_random_int(k2[carry], curve_p, num_words) != UECC_SUCCESS) {
r = UECC_FAILURE; r = UECC_FAILURE;
goto clear_and_out; goto clear_and_out;
} }
@ -1165,21 +1165,21 @@ int uECC_generate_random_int(uECC_word_t *random, const uECC_word_t *top,
bitcount_t num_bits = uECC_vli_numBits(top); bitcount_t num_bits = uECC_vli_numBits(top);
if (!g_rng_function) { if (!g_rng_function) {
return 0; return UECC_FAILURE;
} }
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) { for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
if (g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE) != num_words * uECC_WORD_SIZE) { if (g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE) != num_words * uECC_WORD_SIZE) {
return 0; return UECC_FAILURE;
} }
random[num_words - 1] &= random[num_words - 1] &=
mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits)); mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
if (!uECC_vli_isZero(random) && if (!uECC_vli_isZero(random) &&
uECC_vli_cmp(top, random) == 1) { uECC_vli_cmp(top, random) == 1) {
return 1; return UECC_SUCCESS;
} }
} }
return 0; return UECC_FAILURE;
} }

View file

@ -109,7 +109,7 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
uECC_vli_clear(tmp); uECC_vli_clear(tmp);
tmp[0] = 1; tmp[0] = 1;
} }
else if (!uECC_generate_random_int(tmp, curve_n, num_n_words)) { else if (uECC_generate_random_int(tmp, curve_n, num_n_words) != UECC_SUCCESS) {
return UECC_FAILURE; return UECC_FAILURE;
} }