From a752191191c2367c927a3e7af4cfa3c1acfac13c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Mon, 4 Nov 2019 14:31:35 +0100 Subject: [PATCH] Hardcode numwords in vli_cpm_unsafe --- include/tinycrypt/ecc.h | 3 +-- tinycrypt/ecc.c | 21 ++++++++++----------- tinycrypt/ecc_dsa.c | 8 ++++---- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/include/tinycrypt/ecc.h b/include/tinycrypt/ecc.h index ba5dffc58..57c156aae 100644 --- a/include/tinycrypt/ecc.h +++ b/include/tinycrypt/ecc.h @@ -331,8 +331,7 @@ cmpresult_t uECC_vli_cmp(const uECC_word_t *left, const uECC_word_t *right, * @param num_words IN -- number of words * @return the sign of left - right */ -cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right, - wordcount_t num_words); +cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, const uECC_word_t *right); /* * @brief Computes result = (left - right) % mod. diff --git a/tinycrypt/ecc.c b/tinycrypt/ecc.c index 60b565e3c..c223ff509 100644 --- a/tinycrypt/ecc.c +++ b/tinycrypt/ecc.c @@ -162,12 +162,11 @@ void uECC_vli_set(uECC_word_t *dest, const uECC_word_t *src) } cmpresult_t uECC_vli_cmp_unsafe(const uECC_word_t *left, - const uECC_word_t *right, - wordcount_t num_words) + const uECC_word_t *right) { wordcount_t i; - for (i = num_words - 1; i >= 0; --i) { + for (i = NUM_ECC_WORDS - 1; i >= 0; --i) { if (left[i] > right[i]) { return 1; } else if (left[i] < right[i]) { @@ -427,7 +426,7 @@ void uECC_vli_modAdd(uECC_word_t *result, const uECC_word_t *left, wordcount_t num_words) { uECC_word_t carry = uECC_vli_add(result, left, right); - if (carry || uECC_vli_cmp_unsafe(mod, result, num_words) != 1) { + if (carry || uECC_vli_cmp_unsafe(mod, result) != 1) { /* result > mod (result = mod + remainder), so subtract mod to get * remainder. */ uECC_vli_sub(result, result, mod, num_words); @@ -551,7 +550,7 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, uECC_vli_clear(u); u[0] = 1; uECC_vli_clear(v); - while ((cmpResult = uECC_vli_cmp_unsafe(a, b, num_words)) != 0) { + while ((cmpResult = uECC_vli_cmp_unsafe(a, b)) != 0) { if (EVEN(a)) { uECC_vli_rshift1(a, num_words); vli_modInv_update(u, mod, num_words); @@ -561,7 +560,7 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, } else if (cmpResult > 0) { uECC_vli_sub(a, a, b, num_words); uECC_vli_rshift1(a, num_words); - if (uECC_vli_cmp_unsafe(u, v, num_words) < 0) { + if (uECC_vli_cmp_unsafe(u, v) < 0) { uECC_vli_add(u, u, mod); } uECC_vli_sub(u, u, v, num_words); @@ -569,7 +568,7 @@ void uECC_vli_modInv(uECC_word_t *result, const uECC_word_t *input, } else { uECC_vli_sub(b, b, a, num_words); uECC_vli_rshift1(b, num_words); - if (uECC_vli_cmp_unsafe(v, u, num_words) < 0) { + if (uECC_vli_cmp_unsafe(v, u) < 0) { uECC_vli_add(v, v, mod); } uECC_vli_sub(v, v, u, num_words); @@ -742,7 +741,7 @@ void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int*product) while (carry < 0); } else { while (carry || - uECC_vli_cmp_unsafe(curve_secp256r1.p, result, NUM_ECC_WORDS) != 1) { + uECC_vli_cmp_unsafe(curve_secp256r1.p, result) != 1) { carry -= uECC_vli_sub(result, result, curve_secp256r1.p, NUM_ECC_WORDS); } } @@ -1058,8 +1057,8 @@ int uECC_valid_point(const uECC_word_t *point, uECC_Curve curve) } /* x and y must be smaller than p. */ - if (uECC_vli_cmp_unsafe(curve->p, point, num_words) != 1 || - uECC_vli_cmp_unsafe(curve->p, point + num_words, num_words) != 1) { + if (uECC_vli_cmp_unsafe(curve->p, point) != 1 || + uECC_vli_cmp_unsafe(curve->p, point + num_words) != 1) { return -2; } @@ -1084,7 +1083,7 @@ int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve) public_key + curve->num_bytes, curve->num_bytes); - if (uECC_vli_cmp_unsafe(_public, curve->G, NUM_ECC_WORDS * 2) == 0) { + if (memcmp(_public, curve->G, NUM_ECC_WORDS * 2) == 0) { return -4; } diff --git a/tinycrypt/ecc_dsa.c b/tinycrypt/ecc_dsa.c index e302e04ce..7d4053401 100644 --- a/tinycrypt/ecc_dsa.c +++ b/tinycrypt/ecc_dsa.c @@ -102,7 +102,7 @@ static void bits2int(uECC_word_t *native, const uint8_t *bits, } /* Reduce mod curve_n */ - if (uECC_vli_cmp_unsafe(curve->n, native, num_n_words) != 1) { + if (uECC_vli_cmp_unsafe(curve->n, native) != 1) { uECC_vli_sub(native, native, curve->n, num_n_words); } } @@ -236,8 +236,8 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash, } /* r, s must be < n. */ - if (uECC_vli_cmp_unsafe(curve->n, r, num_n_words) != 1 || - uECC_vli_cmp_unsafe(curve->n, s, num_n_words) != 1) { + if (uECC_vli_cmp_unsafe(curve->n, r) != 1 || + uECC_vli_cmp_unsafe(curve->n, s) != 1) { return 0; } @@ -293,7 +293,7 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash, apply_z(rx, ry, z); /* v = x1 (mod n) */ - if (uECC_vli_cmp_unsafe(curve->n, rx, num_n_words) != 1) { + if (uECC_vli_cmp_unsafe(curve->n, rx) != 1) { uECC_vli_sub(rx, rx, curve->n, num_n_words); }