Skip to content

Commit 3c006a7

Browse files
committed
FIX: Potentially SPA-vulnerability
cherry picked from: kmackay/micro-ecc@1b5f5ce
1 parent ceed8dd commit 3c006a7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

src/core/u-uECC.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,7 @@ static int uECC_sign_with_k(const uint8_t *private_key,
12391239
uECC_word_t tmp[uECC_MAX_WORDS];
12401240
uECC_word_t s[uECC_MAX_WORDS];
12411241
uECC_word_t *k2[2] = {tmp, s};
1242+
uECC_word_t *initial_Z = 0;
12421243
#if uECC_VLI_NATIVE_LITTLE_ENDIAN
12431244
uECC_word_t *p = (uECC_word_t *)signature;
12441245
#else
@@ -1255,7 +1256,15 @@ static int uECC_sign_with_k(const uint8_t *private_key,
12551256
}
12561257

12571258
carry = regularize_k(k, tmp, s, curve);
1258-
EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve);
1259+
/* If an RNG function was specified, try to get a random initial Z value to improve
1260+
protection against side-channel attacks. */
1261+
if (g_rng_function) {
1262+
if (!uECC_generate_random_int(k2[carry], curve->p, num_words)) {
1263+
return 0;
1264+
}
1265+
initial_Z = k2[carry];
1266+
}
1267+
EccPoint_mult(p, curve->G, k2[!carry], initial_Z, num_n_bits + 1, curve);
12591268
if (uECC_vli_isZero(p, num_words)) {
12601269
return 0;
12611270
}

0 commit comments

Comments
 (0)