Move n from struct curve to its own constant

This commit is contained in:
Manuel Pégourié-Gonnard 2019-11-21 10:23:05 +01:00
parent 4d8777cbb6
commit 356d8594d7
4 changed files with 31 additions and 29 deletions

View File

@ -124,7 +124,6 @@ typedef uint64_t uECC_dword_t;
struct uECC_Curve_t; struct uECC_Curve_t;
typedef const struct uECC_Curve_t * uECC_Curve; typedef const struct uECC_Curve_t * uECC_Curve;
struct uECC_Curve_t { struct uECC_Curve_t {
uECC_word_t n[NUM_ECC_WORDS];
uECC_word_t G[NUM_ECC_WORDS * 2]; uECC_word_t G[NUM_ECC_WORDS * 2];
uECC_word_t b[NUM_ECC_WORDS]; uECC_word_t b[NUM_ECC_WORDS];
}; };
@ -155,15 +154,11 @@ void vli_mmod_fast_secp256r1(unsigned int *result, unsigned int *product);
#define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8) #define BITS_TO_BYTES(num_bits) ((num_bits + 7) / 8)
extern const uECC_word_t curve_p[NUM_ECC_WORDS]; extern const uECC_word_t curve_p[NUM_ECC_WORDS];
extern const uECC_word_t curve_n[NUM_ECC_WORDS];
/* definition of curve NIST p-256: */ /* definition of curve NIST p-256: */
static const struct uECC_Curve_t curve_secp256r1 = { static const struct uECC_Curve_t curve_secp256r1 = {
{ {
BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
}, {
BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4), BYTES_TO_WORDS_8(96, C2, 98, D8, 45, 39, A1, F4),
BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77), BYTES_TO_WORDS_8(A0, 33, EB, 2D, 81, 7D, 03, 77),
BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8), BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8),

View File

@ -75,6 +75,12 @@ const uECC_word_t curve_p[NUM_ECC_WORDS] = {
BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00), BYTES_TO_WORDS_8(00, 00, 00, 00, 00, 00, 00, 00),
BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF) BYTES_TO_WORDS_8(01, 00, 00, 00, FF, FF, FF, FF)
}; };
const uECC_word_t curve_n[NUM_ECC_WORDS] = {
BYTES_TO_WORDS_8(51, 25, 63, FC, C2, CA, B9, F3),
BYTES_TO_WORDS_8(84, 9E, 17, A7, AD, FA, E6, BC),
BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF),
BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF)
};
/* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform /* IMPORTANT: Make sure a cryptographically-secure PRNG is set and the platform
* has access to enough entropy in order to feed the PRNG regularly. */ * has access to enough entropy in order to feed the PRNG regularly. */
@ -933,13 +939,12 @@ static uECC_word_t regularize_k(const uECC_word_t * const k, uECC_word_t *k0,
wordcount_t num_n_words = NUM_ECC_WORDS; wordcount_t num_n_words = NUM_ECC_WORDS;
bitcount_t num_n_bits = NUM_ECC_BITS; bitcount_t num_n_bits = NUM_ECC_BITS;
const uECC_Curve curve = uECC_secp256r1();
uECC_word_t carry = uECC_vli_add(k0, k, curve->n) || uECC_word_t carry = uECC_vli_add(k0, k, curve_n) ||
(num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) && (num_n_bits < ((bitcount_t)num_n_words * uECC_WORD_SIZE * 8) &&
uECC_vli_testBit(k0, num_n_bits)); uECC_vli_testBit(k0, num_n_bits));
uECC_vli_add(k1, k0, curve->n); uECC_vli_add(k1, k0, curve_n);
return carry; return carry;
} }
@ -1116,7 +1121,7 @@ int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key,
return 0; return 0;
} }
if (uECC_vli_cmp(curve->n, _private) != 1) { if (uECC_vli_cmp(curve_n, _private) != 1) {
return 0; return 0;
} }

View File

@ -123,7 +123,7 @@ int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve)
} }
/* computing modular reduction of _random (see FIPS 186.4 B.4.1): */ /* computing modular reduction of _random (see FIPS 186.4 B.4.1): */
uECC_vli_mmod(_private, _random, curve->n); uECC_vli_mmod(_private, _random, curve_n);
/* Computing public-key from private: */ /* Computing public-key from private: */
if (EccPoint_compute_public_key(_public, _private, curve)) { if (EccPoint_compute_public_key(_public, _private, curve)) {

View File

@ -84,6 +84,8 @@ static void bits2int(uECC_word_t *native, const uint8_t *bits,
uECC_word_t carry; uECC_word_t carry;
uECC_word_t *ptr; uECC_word_t *ptr;
(void) curve;
if (bits_size > num_n_bytes) { if (bits_size > num_n_bytes) {
bits_size = num_n_bytes; bits_size = num_n_bytes;
} }
@ -103,8 +105,8 @@ static void bits2int(uECC_word_t *native, const uint8_t *bits,
} }
/* Reduce mod curve_n */ /* Reduce mod curve_n */
if (uECC_vli_cmp_unsafe(curve->n, native) != 1) { if (uECC_vli_cmp_unsafe(curve_n, native) != 1) {
uECC_vli_sub(native, native, curve->n); uECC_vli_sub(native, native, curve_n);
} }
} }
@ -122,7 +124,7 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
/* Make sure 0 < k < curve_n */ /* Make sure 0 < k < curve_n */
if (uECC_vli_isZero(k) || if (uECC_vli_isZero(k) ||
uECC_vli_cmp(curve->n, k) != 1) { uECC_vli_cmp(curve_n, k) != 1) {
return 0; return 0;
} }
@ -137,15 +139,15 @@ 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)) {
return 0; return 0;
} }
/* Prevent side channel analysis of uECC_vli_modInv() to determine /* Prevent side channel analysis of uECC_vli_modInv() to determine
bits of k / the private key by premultiplying by a random number */ bits of k / the private key by premultiplying by a random number */
uECC_vli_modMult(k, k, tmp, curve->n); /* k' = rand * k */ uECC_vli_modMult(k, k, tmp, curve_n); /* k' = rand * k */
uECC_vli_modInv(k, k, curve->n); /* k = 1 / k' */ uECC_vli_modInv(k, k, curve_n); /* k = 1 / k' */
uECC_vli_modMult(k, k, tmp, curve->n); /* k = 1 / k */ uECC_vli_modMult(k, k, tmp, curve_n); /* k = 1 / k */
uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */ uECC_vli_nativeToBytes(signature, NUM_ECC_BYTES, p); /* store r */
@ -154,11 +156,11 @@ int uECC_sign_with_k(const uint8_t *private_key, const uint8_t *message_hash,
s[num_n_words - 1] = 0; s[num_n_words - 1] = 0;
uECC_vli_set(s, p); uECC_vli_set(s, p);
uECC_vli_modMult(s, tmp, s, curve->n); /* s = r*d */ uECC_vli_modMult(s, tmp, s, curve_n); /* s = r*d */
bits2int(tmp, message_hash, hash_size, curve); bits2int(tmp, message_hash, hash_size, curve);
uECC_vli_modAdd(s, tmp, s, curve->n); /* s = e + r*d */ uECC_vli_modAdd(s, tmp, s, curve_n); /* s = e + r*d */
uECC_vli_modMult(s, s, k, curve->n); /* s = (e + r*d) / k */ uECC_vli_modMult(s, s, k, curve_n); /* s = (e + r*d) / k */
if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) { if (uECC_vli_numBits(s) > (bitcount_t)NUM_ECC_BYTES * 8) {
return 0; return 0;
} }
@ -183,7 +185,7 @@ int uECC_sign(const uint8_t *private_key, const uint8_t *message_hash,
} }
// computing k as modular reduction of _random (see FIPS 186.4 B.5.1): // computing k as modular reduction of _random (see FIPS 186.4 B.5.1):
uECC_vli_mmod(k, _random, curve->n); uECC_vli_mmod(k, _random, curve_n);
if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature, if (uECC_sign_with_k(private_key, message_hash, hash_size, k, signature,
curve)) { curve)) {
@ -241,17 +243,17 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
} }
/* r, s must be < n. */ /* r, s must be < n. */
if (uECC_vli_cmp_unsafe(curve->n, r) != 1 || if (uECC_vli_cmp_unsafe(curve_n, r) != 1 ||
uECC_vli_cmp_unsafe(curve->n, s) != 1) { uECC_vli_cmp_unsafe(curve_n, s) != 1) {
return UECC_FAILURE; return UECC_FAILURE;
} }
/* Calculate u1 and u2. */ /* Calculate u1 and u2. */
uECC_vli_modInv(z, s, curve->n); /* z = 1/s */ uECC_vli_modInv(z, s, curve_n); /* z = 1/s */
u1[num_n_words - 1] = 0; u1[num_n_words - 1] = 0;
bits2int(u1, message_hash, hash_size, curve); bits2int(u1, message_hash, hash_size, curve);
uECC_vli_modMult(u1, u1, z, curve->n); /* u1 = e/s */ uECC_vli_modMult(u1, u1, z, curve_n); /* u1 = e/s */
uECC_vli_modMult(u2, r, z, curve->n); /* u2 = r/s */ uECC_vli_modMult(u2, r, z, curve_n); /* u2 = r/s */
/* Calculate sum = G + Q. */ /* Calculate sum = G + Q. */
uECC_vli_set(sum, _public); uECC_vli_set(sum, _public);
@ -298,8 +300,8 @@ int uECC_verify(const uint8_t *public_key, const uint8_t *message_hash,
apply_z(rx, ry, z); apply_z(rx, ry, z);
/* v = x1 (mod n) */ /* v = x1 (mod n) */
if (uECC_vli_cmp_unsafe(curve->n, rx) != 1) { if (uECC_vli_cmp_unsafe(curve_n, rx) != 1) {
uECC_vli_sub(rx, rx, curve->n); uECC_vli_sub(rx, rx, curve_n);
} }
/* Accept only if v == r. */ /* Accept only if v == r. */