mirror of
https://github.com/kmackay/micro-ecc.git
synced 2026-10-02 13:57:35 +00:00
Addressed review comments to PR #58. Main change:
Removed clamping via subtraction update in the uECC_generate_random_int function.
This commit is contained in:
committed by
Ken MacKay
parent
7b115fdf93
commit
12651728ea
@@ -4,7 +4,7 @@
|
|||||||
#include "uECC_vli.h"
|
#include "uECC_vli.h"
|
||||||
|
|
||||||
#ifndef uECC_RNG_MAX_TRIES
|
#ifndef uECC_RNG_MAX_TRIES
|
||||||
#define uECC_RNG_MAX_TRIES 4
|
#define uECC_RNG_MAX_TRIES 64
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if uECC_SUPPORTS_secp160r1
|
#if uECC_SUPPORTS_secp160r1
|
||||||
@@ -845,14 +845,10 @@ uECC_VLI_API void uECC_vli_bytesToNative(uECC_word_t *native,
|
|||||||
#endif /* uECC_WORD_SIZE */
|
#endif /* uECC_WORD_SIZE */
|
||||||
|
|
||||||
/* Generates a random integer in the range 0 < random < top.
|
/* Generates a random integer in the range 0 < random < top.
|
||||||
Both, random and top, have num_words words. */
|
Both random and top have num_words words. */
|
||||||
uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random,
|
uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random,
|
||||||
const uECC_word_t *top,
|
const uECC_word_t *top,
|
||||||
wordcount_t num_words) {
|
wordcount_t num_words) {
|
||||||
uECC_word_t random1[uECC_MAX_WORDS];
|
|
||||||
uECC_word_t random2[uECC_MAX_WORDS];
|
|
||||||
uECC_word_t *p2[2] = {random1, random2};
|
|
||||||
uECC_word_t borrow;
|
|
||||||
uECC_word_t mask = (uECC_word_t)-1;
|
uECC_word_t mask = (uECC_word_t)-1;
|
||||||
uECC_word_t tries;
|
uECC_word_t tries;
|
||||||
bitcount_t num_bits = uECC_vli_numBits(top, num_words);
|
bitcount_t num_bits = uECC_vli_numBits(top, num_words);
|
||||||
@@ -862,14 +858,14 @@ uECC_VLI_API int uECC_generate_random_int(uECC_word_t *random,
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
for (tries = 0; tries < uECC_RNG_MAX_TRIES; ++tries) {
|
||||||
if (g_rng_function((uint8_t *)random1, num_words * uECC_WORD_SIZE)) {
|
if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
random[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
|
||||||
|
|
||||||
random1[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits));
|
if (!uECC_vli_isZero(random, num_words) &&
|
||||||
|
uECC_vli_cmp(top, random, num_words) == 1) {
|
||||||
borrow = uECC_vli_sub(random2, random1, top, num_words);
|
|
||||||
|
|
||||||
if (!uECC_vli_isZero(p2[!borrow], num_words)) {
|
|
||||||
uECC_vli_set(random, p2[!borrow], num_words);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1081,10 +1077,8 @@ static int uECC_sign_with_k(const uint8_t *private_key,
|
|||||||
if (!g_rng_function) {
|
if (!g_rng_function) {
|
||||||
uECC_vli_clear(tmp, num_n_words);
|
uECC_vli_clear(tmp, num_n_words);
|
||||||
tmp[0] = 1;
|
tmp[0] = 1;
|
||||||
} else {
|
} else if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
|
||||||
if (!uECC_generate_random_int(tmp, curve->n, num_n_words)) {
|
return 0;
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Prevent side channel analysis of uECC_vli_modInv() to determine
|
/* Prevent side channel analysis of uECC_vli_modInv() to determine
|
||||||
|
|||||||
+1
-1
@@ -156,7 +156,7 @@ void uECC_point_mult(uECC_word_t *result,
|
|||||||
uECC_Curve curve);
|
uECC_Curve curve);
|
||||||
|
|
||||||
/* Generates a random integer in the range 0 < random < top.
|
/* Generates a random integer in the range 0 < random < top.
|
||||||
Both, random and top, have num_words words. */
|
Both random and top have num_words words. */
|
||||||
int uECC_generate_random_int(uECC_word_t *random,
|
int uECC_generate_random_int(uECC_word_t *random,
|
||||||
const uECC_word_t *top,
|
const uECC_word_t *top,
|
||||||
wordcount_t num_words);
|
wordcount_t num_words);
|
||||||
|
|||||||
Reference in New Issue
Block a user