diff --git a/curve-specific.inc b/curve-specific.inc new file mode 100644 index 0000000..0066bb6 --- /dev/null +++ b/curve-specific.inc @@ -0,0 +1,818 @@ +#define num_bytes_secp160r1 20 +#define num_bytes_secp192r1 24 +#define num_bytes_secp224r1 28 +#define num_bytes_secp256r1 32 +#define num_bytes_secp256k1 32 + +#if (uECC_WORD_SIZE == 1) + +#define num_words_secp160r1 20 +#define num_words_secp192r1 24 +#define num_words_secp224r1 28 +#define num_words_secp256r1 32 +#define num_words_secp256k1 32 + +#define num_n_words_secp160r1 21 + +#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) \ + 0x##a, 0x##b, 0x##c, 0x##d, 0x##e, 0x##f, 0x##g, 0x##h +#define BYTES_TO_WORDS_4(a, b, c, d) 0x##a, 0x##b, 0x##c, 0x##d + +#elif (uECC_WORD_SIZE == 4) + +#define num_words_secp160r1 5 +#define num_words_secp192r1 6 +#define num_words_secp224r1 7 +#define num_words_secp256r1 8 +#define num_words_secp256k1 8 + +#define num_n_words_secp160r1 6 + +#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##d##c##b##a, 0x##h##g##f##e +#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a + +#elif (uECC_WORD_SIZE == 8) + +#define num_words_secp160r1 3 +#define num_words_secp192r1 3 +#define num_words_secp224r1 4 +#define num_words_secp256r1 4 +#define num_words_secp256k1 4 + +#define num_n_words_secp160r1 3 + +#define BYTES_TO_WORDS_8(a, b, c, d, e, f, g, h) 0x##h##g##f##e##d##c##b##a##ull +#define BYTES_TO_WORDS_4(a, b, c, d) 0x##d##c##b##a##ull + +#endif /* uECC_WORD_SIZE */ + +static const struct uECC_Curve_t curve_secp160r1 = { + num_words_secp160r1, + num_n_words_secp160r1, + num_bytes_secp160r1, + { BYTES_TO_WORDS_8(FF, FF, FF, 7F, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_4(FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(57, 22, 75, CA, D3, AE, 27, F9), + BYTES_TO_WORDS_8(C8, F4, 01, 00, 00, 00, 00, 00), + BYTES_TO_WORDS_8(00, 00, 00, 00, 01, 00, 00, 00) }, + { BYTES_TO_WORDS_8(82, FC, CB, 13, B9, 8B, C3, 68), + BYTES_TO_WORDS_8(89, 69, 64, 46, 28, 73, F5, 8E), + BYTES_TO_WORDS_4(68, B5, 96, 4A), + + BYTES_TO_WORDS_8(32, FB, C5, 7A, 37, 51, 23, 04), + BYTES_TO_WORDS_8(12, C9, DC, 59, 7D, 94, 68, 31), + BYTES_TO_WORDS_4(55, 28, A6, 23) }, + { BYTES_TO_WORDS_8(45, FA, 65, C5, AD, D4, D4, 81), + BYTES_TO_WORDS_8(9F, F8, AC, 65, 8B, 7A, BD, 54), + BYTES_TO_WORDS_4(FC, BE, 97, 1C) }, + &double_jacobian_default, + &mod_sqrt_default, + &x_side_default +}; + +static const struct uECC_Curve_t curve_secp192r1 = { + num_words_secp192r1, + num_words_secp192r1, + num_bytes_secp192r1, + { BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FE, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(31, 28, D2, B4, B1, C9, 6B, 14), + BYTES_TO_WORDS_8(36, F8, DE, 99, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(12, 10, FF, 82, FD, 0A, FF, F4), + BYTES_TO_WORDS_8(00, 88, A1, 43, EB, 20, BF, 7C), + BYTES_TO_WORDS_8(F6, 90, 30, B0, 0E, A8, 8D, 18), + + BYTES_TO_WORDS_8(11, 48, 79, 1E, A1, 77, F9, 73), + BYTES_TO_WORDS_8(D5, CD, 24, 6B, ED, 11, 10, 63), + BYTES_TO_WORDS_8(78, DA, C8, FF, 95, 2B, 19, 07) }, + { BYTES_TO_WORDS_8(B1, B9, 46, C1, EC, DE, B8, FE), + BYTES_TO_WORDS_8(49, 30, 24, 72, AB, E9, A7, 0F), + BYTES_TO_WORDS_8(E7, 80, 9C, E5, 19, 05, 21, 64) }, + &double_jacobian_default, + &mod_sqrt_default, + &x_side_default +}; + +static const struct uECC_Curve_t curve_secp224r1 = { + num_words_secp224r1, + num_words_secp224r1, + num_bytes_secp224r1, + { BYTES_TO_WORDS_8(01, 00, 00, 00, 00, 00, 00, 00), + BYTES_TO_WORDS_8(00, 00, 00, 00, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_4(FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(3D, 2A, 5C, 5C, 45, 29, DD, 13), + BYTES_TO_WORDS_8(3E, F0, B8, E0, A2, 16, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_4(FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(21, 1D, 5C, 11, D6, 80, 32, 34), + BYTES_TO_WORDS_8(22, 11, C2, 56, D3, C1, 03, 4A), + BYTES_TO_WORDS_8(B9, 90, 13, 32, 7F, BF, B4, 6B), + BYTES_TO_WORDS_4(BD, 0C, 0E, B7), + + BYTES_TO_WORDS_8(34, 7E, 00, 85, 99, 81, D5, 44), + BYTES_TO_WORDS_8(64, 47, 07, 5A, A0, 75, 43, CD), + BYTES_TO_WORDS_8(E6, DF, 22, 4C, FB, 23, F7, B5), + BYTES_TO_WORDS_4(88, 63, 37, BD) }, + { BYTES_TO_WORDS_8(B4, FF, 55, 23, 43, 39, 0B, 27), + BYTES_TO_WORDS_8(BA, D8, BF, D7, B7, B0, 44, 50), + BYTES_TO_WORDS_8(56, 32, 41, F5, AB, B3, 04, 0C), + BYTES_TO_WORDS_4(85, 0A, 05, B4) }, + &double_jacobian_default, + &mod_sqrt_secp224r1, + &x_side_default +}; + +static const struct uECC_Curve_t curve_secp256r1 = { + num_words_secp256r1, + num_words_secp256r1, + num_bytes_secp256r1, + { BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, 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(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(A0, 33, EB, 2D, 81, 7D, 03, 77), + BYTES_TO_WORDS_8(F2, 40, A4, 63, E5, E6, BC, F8), + BYTES_TO_WORDS_8(47, 42, 2C, E1, F2, D1, 17, 6B), + + BYTES_TO_WORDS_8(F5, 51, BF, 37, 68, 40, B6, CB), + BYTES_TO_WORDS_8(CE, 5E, 31, 6B, 57, 33, CE, 2B), + BYTES_TO_WORDS_8(16, 9E, 0F, 7C, 4A, EB, E7, 8E), + BYTES_TO_WORDS_8(9B, 7F, 1A, FE, E2, 42, E3, 4F) }, + { BYTES_TO_WORDS_8(4B, 60, D2, 27, 3E, 3C, CE, 3B), + BYTES_TO_WORDS_8(F6, B0, 53, CC, B0, 06, 1D, 65), + BYTES_TO_WORDS_8(BC, 86, 98, 76, 55, BD, EB, B3), + BYTES_TO_WORDS_8(E7, 93, 3A, AA, D8, 35, C6, 5A) }, + &double_jacobian_default, + &mod_sqrt_default, + &x_side_default +}; + +static const struct uECC_Curve_t curve_secp256k1 = { + num_words_secp256k1, + num_words_secp256k1, + num_bytes_secp256k1, + { BYTES_TO_WORDS_8(2F, FC, FF, FF, FE, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(41, 41, 36, D0, 8C, 5E, D2, BF), + BYTES_TO_WORDS_8(3B, A0, 48, AF, E6, DC, AE, BA), + BYTES_TO_WORDS_8(FE, FF, FF, FF, FF, FF, FF, FF), + BYTES_TO_WORDS_8(FF, FF, FF, FF, FF, FF, FF, FF) }, + { BYTES_TO_WORDS_8(98, 17, F8, 16, 5B, 81, F2, 59), + BYTES_TO_WORDS_8(D9, 28, CE, 2D, DB, FC, 9B, 02), + BYTES_TO_WORDS_8(07, 0B, 87, CE, 95, 62, A0, 55), + BYTES_TO_WORDS_8(AC, BB, DC, F9, 7E, 66, BE, 79), + + BYTES_TO_WORDS_8(B8, D4, 10, FB, 8F, D0, 47, 9C), + BYTES_TO_WORDS_8(19, 54, 85, A6, 48, B4, 17, FD), + BYTES_TO_WORDS_8(A8, 08, 11, 0E, FC, FB, A4, 5D), + BYTES_TO_WORDS_8(65, C4, A3, 26, 77, DA, 3A, 48) }, + { BYTES_TO_WORDS_8(07, 00, 00, 00, 00, 00, 00, 00), + 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(00, 00, 00, 00, 00, 00, 00, 00) }, + &double_jacobian_secp256k1, + &mod_sqrt_default, + &x_side_secp256k1 +}; + +uECC_Curve uECC_secp160r1(void) { return &curve_secp160r1; } +uECC_Curve uECC_secp192r1(void) { return &curve_secp192r1; } +uECC_Curve uECC_secp224r1(void) { return &curve_secp224r1; } +uECC_Curve uECC_secp256r1(void) { return &curve_secp256r1; } +uECC_Curve uECC_secp256k1(void) { return &curve_secp256k1; } + +// #if !asm_mmod_fast +// +// #if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1) +// /* omega_mult() is defined farther below for the different curves / word sizes */ +// static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right); +// +// /* Computes result = product % curve_p +// see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354 +// +// Note that this only works if log2(omega) < log2(p) / 2 */ +// static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) { +// uECC_word_t tmp[2 * uECC_WORDS]; +// uECC_word_t carry; +// +// vli_clear(tmp); +// vli_clear(tmp + uECC_WORDS); +// +// omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */ +// +// carry = vli_add(result, product, tmp); /* (C, r) = r + q */ +// vli_clear(product); +// omega_mult(product, tmp + uECC_WORDS); /* Rq*c */ +// carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */ +// +// while (carry > 0) { +// --carry; +// vli_sub(result, result, curve_p); +// } +// if (vli_cmp(result, curve_p) > 0) { +// vli_sub(result, result, curve_p); +// } +// } +// +// #endif +// +// #if uECC_CURVE == uECC_secp160r1 +// +// #if uECC_WORD_SIZE == 1 +// static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { +// uint8_t carry; +// uint8_t i; +// +// /* Multiply by (2^31 + 1). */ +// vli_set(result + 4, right); /* 2^32 */ +// vli_rshift1(result + 4); /* 2^31 */ +// result[3] = right[0] << 7; /* get last bit from shift */ +// +// carry = vli_add(result, result, right); /* 2^31 + 1 */ +// for (i = uECC_WORDS; carry; ++i) { +// uint16_t sum = (uint16_t)result[i] + carry; +// result[i] = (uint8_t)sum; +// carry = sum >> 8; +// } +// } +// #elif uECC_WORD_SIZE == 4 +// static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { +// uint32_t carry; +// unsigned i; +// +// /* Multiply by (2^31 + 1). */ +// vli_set(result + 1, right); /* 2^32 */ +// vli_rshift1(result + 1); /* 2^31 */ +// result[0] = right[0] << 31; /* get last bit from shift */ +// +// carry = vli_add(result, result, right); /* 2^31 + 1 */ +// for (i = uECC_WORDS; carry; ++i) { +// uint64_t sum = (uint64_t)result[i] + carry; +// result[i] = (uint32_t)sum; +// carry = sum >> 32; +// } +// } +// #endif /* uECC_WORD_SIZE */ +// +// #elif uECC_CURVE == uECC_secp192r1 +// +// /* Computes result = product % curve_p. +// See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ +// #if uECC_WORD_SIZE == 1 +// static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { +// uint8_t tmp[uECC_WORDS]; +// uint8_t carry; +// +// vli_set(result, product); +// +// vli_set(tmp, &product[24]); +// carry = vli_add(result, result, tmp); +// +// tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; +// tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27]; +// tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; +// tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; +// tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; +// carry += vli_add(result, result, tmp); +// +// tmp[0] = tmp[8] = product[40]; +// tmp[1] = tmp[9] = product[41]; +// tmp[2] = tmp[10] = product[42]; +// tmp[3] = tmp[11] = product[43]; +// tmp[4] = tmp[12] = product[44]; +// tmp[5] = tmp[13] = product[45]; +// tmp[6] = tmp[14] = product[46]; +// tmp[7] = tmp[15] = product[47]; +// tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; +// carry += vli_add(result, result, tmp); +// +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// #elif uECC_WORD_SIZE == 4 +// static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { +// uint32_t tmp[uECC_WORDS]; +// int carry; +// +// vli_set(result, product); +// +// vli_set(tmp, &product[6]); +// carry = vli_add(result, result, tmp); +// +// tmp[0] = tmp[1] = 0; +// tmp[2] = product[6]; +// tmp[3] = product[7]; +// tmp[4] = product[8]; +// tmp[5] = product[9]; +// carry += vli_add(result, result, tmp); +// +// tmp[0] = tmp[2] = product[10]; +// tmp[1] = tmp[3] = product[11]; +// tmp[4] = tmp[5] = 0; +// carry += vli_add(result, result, tmp); +// +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// #else +// static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { +// uint64_t tmp[uECC_WORDS]; +// int carry; +// +// vli_set(result, product); +// +// vli_set(tmp, &product[3]); +// carry = vli_add(result, result, tmp); +// +// tmp[0] = 0; +// tmp[1] = product[3]; +// tmp[2] = product[4]; +// carry += vli_add(result, result, tmp); +// +// tmp[0] = tmp[1] = product[5]; +// tmp[2] = 0; +// carry += vli_add(result, result, tmp); +// +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// #endif /* uECC_WORD_SIZE */ +// +// #elif uECC_CURVE == uECC_secp256r1 +// +// /* Computes result = product % curve_p +// from http://www.nsa.gov/ia/_files/nist-routines.pdf */ +// #if uECC_WORD_SIZE == 1 +// static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { +// uint8_t tmp[uECC_BYTES]; +// int8_t carry; +// +// /* t */ +// vli_set(result, product); +// +// /* s1 */ +// tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; +// tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; +// tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; +// tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; +// tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; +// tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; +// tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; +// tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; +// carry = vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s2 */ +// tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51]; +// tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55]; +// tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59]; +// tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63]; +// tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0; +// carry += vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s3 */ +// tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35]; +// tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39]; +// tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43]; +// tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; +// tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; +// tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; +// tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; +// tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; +// carry += vli_add(result, result, tmp); +// +// /* s4 */ +// tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39]; +// tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43]; +// tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47]; +// tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55]; +// tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59]; +// tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63]; +// tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; +// tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35]; +// carry += vli_add(result, result, tmp); +// +// /* d1 */ +// tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; +// tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; +// tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; +// tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; +// tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; +// tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; +// tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35]; +// tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43]; +// carry -= vli_sub(result, result, tmp); +// +// /* d2 */ +// tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51]; +// tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55]; +// tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59]; +// tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63]; +// tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; +// tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; +// tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39]; +// tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47]; +// carry -= vli_sub(result, result, tmp); +// +// /* d3 */ +// tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55]; +// tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59]; +// tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63]; +// tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35]; +// tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39]; +// tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43]; +// tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; +// tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51]; +// carry -= vli_sub(result, result, tmp); +// +// /* d4 */ +// tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59]; +// tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63]; +// tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; +// tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39]; +// tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43]; +// tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47]; +// tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; +// tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55]; +// carry -= vli_sub(result, result, tmp); +// +// if (carry < 0) { +// do { +// carry += vli_add(result, result, curve_p); +// } while (carry < 0); +// } else { +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// } +// #elif uECC_WORD_SIZE == 4 +// static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { +// uint32_t tmp[uECC_WORDS]; +// int carry; +// +// /* t */ +// vli_set(result, product); +// +// /* s1 */ +// tmp[0] = tmp[1] = tmp[2] = 0; +// tmp[3] = product[11]; +// tmp[4] = product[12]; +// tmp[5] = product[13]; +// tmp[6] = product[14]; +// tmp[7] = product[15]; +// carry = vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s2 */ +// tmp[3] = product[12]; +// tmp[4] = product[13]; +// tmp[5] = product[14]; +// tmp[6] = product[15]; +// tmp[7] = 0; +// carry += vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s3 */ +// tmp[0] = product[8]; +// tmp[1] = product[9]; +// tmp[2] = product[10]; +// tmp[3] = tmp[4] = tmp[5] = 0; +// tmp[6] = product[14]; +// tmp[7] = product[15]; +// carry += vli_add(result, result, tmp); +// +// /* s4 */ +// tmp[0] = product[9]; +// tmp[1] = product[10]; +// tmp[2] = product[11]; +// tmp[3] = product[13]; +// tmp[4] = product[14]; +// tmp[5] = product[15]; +// tmp[6] = product[13]; +// tmp[7] = product[8]; +// carry += vli_add(result, result, tmp); +// +// /* d1 */ +// tmp[0] = product[11]; +// tmp[1] = product[12]; +// tmp[2] = product[13]; +// tmp[3] = tmp[4] = tmp[5] = 0; +// tmp[6] = product[8]; +// tmp[7] = product[10]; +// carry -= vli_sub(result, result, tmp); +// +// /* d2 */ +// tmp[0] = product[12]; +// tmp[1] = product[13]; +// tmp[2] = product[14]; +// tmp[3] = product[15]; +// tmp[4] = tmp[5] = 0; +// tmp[6] = product[9]; +// tmp[7] = product[11]; +// carry -= vli_sub(result, result, tmp); +// +// /* d3 */ +// tmp[0] = product[13]; +// tmp[1] = product[14]; +// tmp[2] = product[15]; +// tmp[3] = product[8]; +// tmp[4] = product[9]; +// tmp[5] = product[10]; +// tmp[6] = 0; +// tmp[7] = product[12]; +// carry -= vli_sub(result, result, tmp); +// +// /* d4 */ +// tmp[0] = product[14]; +// tmp[1] = product[15]; +// tmp[2] = 0; +// tmp[3] = product[9]; +// tmp[4] = product[10]; +// tmp[5] = product[11]; +// tmp[6] = 0; +// tmp[7] = product[13]; +// carry -= vli_sub(result, result, tmp); +// +// if (carry < 0) { +// do { +// carry += vli_add(result, result, curve_p); +// } while (carry < 0); +// } else { +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// } +// #else +// static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { +// uint64_t tmp[uECC_WORDS]; +// int carry; +// +// /* t */ +// vli_set(result, product); +// +// /* s1 */ +// tmp[0] = 0; +// tmp[1] = product[5] & 0xffffffff00000000ull; +// tmp[2] = product[6]; +// tmp[3] = product[7]; +// carry = vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s2 */ +// tmp[1] = product[6] << 32; +// tmp[2] = (product[6] >> 32) | (product[7] << 32); +// tmp[3] = product[7] >> 32; +// carry += vli_add(tmp, tmp, tmp); +// carry += vli_add(result, result, tmp); +// +// /* s3 */ +// tmp[0] = product[4]; +// tmp[1] = product[5] & 0xffffffff; +// tmp[2] = 0; +// tmp[3] = product[7]; +// carry += vli_add(result, result, tmp); +// +// /* s4 */ +// tmp[0] = (product[4] >> 32) | (product[5] << 32); +// tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull); +// tmp[2] = product[7]; +// tmp[3] = (product[6] >> 32) | (product[4] << 32); +// carry += vli_add(result, result, tmp); +// +// /* d1 */ +// tmp[0] = (product[5] >> 32) | (product[6] << 32); +// tmp[1] = (product[6] >> 32); +// tmp[2] = 0; +// tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32); +// carry -= vli_sub(result, result, tmp); +// +// /* d2 */ +// tmp[0] = product[6]; +// tmp[1] = product[7]; +// tmp[2] = 0; +// tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull); +// carry -= vli_sub(result, result, tmp); +// +// /* d3 */ +// tmp[0] = (product[6] >> 32) | (product[7] << 32); +// tmp[1] = (product[7] >> 32) | (product[4] << 32); +// tmp[2] = (product[4] >> 32) | (product[5] << 32); +// tmp[3] = (product[6] << 32); +// carry -= vli_sub(result, result, tmp); +// +// /* d4 */ +// tmp[0] = product[7]; +// tmp[1] = product[4] & 0xffffffff00000000ull; +// tmp[2] = product[5]; +// tmp[3] = product[6] & 0xffffffff00000000ull; +// carry -= vli_sub(result, result, tmp); +// +// if (carry < 0) { +// do { +// carry += vli_add(result, result, curve_p); +// } while (carry < 0); +// } else { +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// } +// #endif /* uECC_WORD_SIZE */ +// +// #elif uECC_CURVE == uECC_secp256k1 +// +// #if uECC_WORD_SIZE == 1 +// static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { +// /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ +// uECC_word_t r0 = 0; +// uECC_word_t r1 = 0; +// uECC_word_t r2 = 0; +// wordcount_t k; +// +// /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ +// muladd(0xD1, right[0], &r0, &r1, &r2); +// result[0] = r0; +// r0 = r1; +// r1 = r2; +// /* r2 is still 0 */ +// +// for (k = 1; k < uECC_WORDS; ++k) { +// muladd(0x03, right[k - 1], &r0, &r1, &r2); +// muladd(0xD1, right[k], &r0, &r1, &r2); +// result[k] = r0; +// r0 = r1; +// r1 = r2; +// r2 = 0; +// } +// muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2); +// result[uECC_WORDS] = r0; +// result[uECC_WORDS + 1] = r1; +// +// result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */ +// } +// #elif uECC_WORD_SIZE == 4 +// static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { +// /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ +// uint32_t carry = 0; +// wordcount_t k; +// +// for (k = 0; k < uECC_WORDS; ++k) { +// uint64_t p = (uint64_t)0x3D1 * right[k] + carry; +// result[k] = (p & 0xffffffff); +// carry = p >> 32; +// } +// result[uECC_WORDS] = carry; +// +// result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */ +// } +// #else +// static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) { +// uECC_word_t r0 = 0; +// uECC_word_t r1 = 0; +// uECC_word_t r2 = 0; +// wordcount_t k; +// +// /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ +// for (k = 0; k < uECC_WORDS; ++k) { +// muladd(0x1000003D1ull, right[k], &r0, &r1, &r2); +// result[k] = r0; +// r0 = r1; +// r1 = r2; +// r2 = 0; +// } +// result[uECC_WORDS] = r0; +// } +// #endif /* uECC_WORD_SIZE */ +// +// #elif uECC_CURVE == uECC_secp224r1 +// +// /* Computes result = product % curve_p +// from http://www.nsa.gov/ia/_files/nist-routines.pdf */ +// #if uECC_WORD_SIZE == 1 +// // TODO it may be faster to use the omega_mult method when fully asm optimized. +// void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { +// uint8_t tmp[uECC_WORDS]; +// int8_t carry; +// +// /* t */ +// vli_set(result, product); +// +// /* s1 */ +// tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; +// tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; +// tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; +// tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; +// tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; +// tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; +// tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43]; +// carry = vli_add(result, result, tmp); +// +// /* s2 */ +// tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; +// tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; +// tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; +// tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; +// carry += vli_add(result, result, tmp); +// +// /* d1 */ +// tmp[0] = product[28]; tmp[1] = product[29]; tmp[2] = product[30]; tmp[3] = product[31]; +// tmp[4] = product[32]; tmp[5] = product[33]; tmp[6] = product[34]; tmp[7] = product[35]; +// tmp[8] = product[36]; tmp[9] = product[37]; tmp[10] = product[38]; tmp[11] = product[39]; +// tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43]; +// tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47]; +// tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51]; +// tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; +// carry -= vli_sub(result, result, tmp); +// +// /* d2 */ +// tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; +// tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; +// tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; +// tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; +// tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; +// tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; +// tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; +// carry -= vli_sub(result, result, tmp); +// +// if (carry < 0) { +// do { +// carry += vli_add(result, result, curve_p); +// } while (carry < 0); +// } else { +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// } +// #elif uECC_WORD_SIZE == 4 +// void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) +// { +// uint32_t tmp[uECC_WORDS]; +// int carry; +// +// /* t */ +// vli_set(result, product); +// +// /* s1 */ +// tmp[0] = tmp[1] = tmp[2] = 0; +// tmp[3] = product[7]; +// tmp[4] = product[8]; +// tmp[5] = product[9]; +// tmp[6] = product[10]; +// carry = vli_add(result, result, tmp); +// +// /* s2 */ +// tmp[3] = product[11]; +// tmp[4] = product[12]; +// tmp[5] = product[13]; +// tmp[6] = 0; +// carry += vli_add(result, result, tmp); +// +// /* d1 */ +// tmp[0] = product[7]; +// tmp[1] = product[8]; +// tmp[2] = product[9]; +// tmp[3] = product[10]; +// tmp[4] = product[11]; +// tmp[5] = product[12]; +// tmp[6] = product[13]; +// carry -= vli_sub(result, result, tmp); +// +// /* d2 */ +// tmp[0] = product[11]; +// tmp[1] = product[12]; +// tmp[2] = product[13]; +// tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0; +// carry -= vli_sub(result, result, tmp); +// +// if (carry < 0) { +// do { +// carry += vli_add(result, result, curve_p); +// } while (carry < 0); +// } else { +// while (carry || vli_cmp(curve_p, result) != 1) { +// carry -= vli_sub(result, result, curve_p); +// } +// } +// } +// #endif /* uECC_WORD_SIZE */ +// +// #endif /* uECC_CURVE */ +// #endif /* !asm_mmod_fast */ diff --git a/platform-specific.inc b/platform-specific.inc new file mode 100644 index 0000000..f259615 --- /dev/null +++ b/platform-specific.inc @@ -0,0 +1,171 @@ +#ifndef uECC_PLATFORM + #if __AVR__ + #define uECC_PLATFORM uECC_avr + #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */ + #define uECC_PLATFORM uECC_arm_thumb2 + #elif defined(__thumb__) + #define uECC_PLATFORM uECC_arm_thumb + #elif defined(__arm__) || defined(_M_ARM) + #define uECC_PLATFORM uECC_arm + #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__) + #define uECC_PLATFORM uECC_x86 + #elif defined(__amd64__) || defined(_M_X64) + #define uECC_PLATFORM uECC_x86_64 + #else + #define uECC_PLATFORM uECC_arch_other + #endif +#endif + +#ifndef uECC_WORD_SIZE + #if uECC_PLATFORM == uECC_avr + #define uECC_WORD_SIZE 1 + #elif (uECC_PLATFORM == uECC_x86_64) + #define uECC_WORD_SIZE 8 + #else + #define uECC_WORD_SIZE 4 + #endif +#endif + +#if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8) + #error "Unsupported value for uECC_WORD_SIZE" +#endif + +#if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1)) + #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm") + #undef uECC_WORD_SIZE + #define uECC_WORD_SIZE 1 +#endif + +#if (uECC_ASM && \ + (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \ + (uECC_WORD_SIZE != 4)) + #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm") + #undef uECC_WORD_SIZE + #define uECC_WORD_SIZE 4 +#endif + +#if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302) + #define SUPPORTS_INT128 1 +#else + #define SUPPORTS_INT128 0 +#endif + +#if (uECC_WORD_SIZE == 1) + +typedef uint8_t uECC_word_t; +typedef uint16_t uECC_dword_t; +typedef uint8_t wordcount_t; +typedef int8_t swordcount_t; +typedef int16_t bitcount_t; +typedef int8_t cmpresult_t; + +#define HIGH_BIT_SET 0x80 +#define uECC_WORD_BITS 8 +#define uECC_WORD_BITS_SHIFT 3 +#define uECC_WORD_BITS_MASK 0x07 + +#elif (uECC_WORD_SIZE == 4) + +typedef uint32_t uECC_word_t; +typedef uint64_t uECC_dword_t; +typedef unsigned wordcount_t; +typedef int swordcount_t; +typedef int bitcount_t; +typedef int cmpresult_t; + +#define HIGH_BIT_SET 0x80000000 +#define uECC_WORD_BITS 32 +#define uECC_WORD_BITS_SHIFT 5 +#define uECC_WORD_BITS_MASK 0x01F + +#elif (uECC_WORD_SIZE == 8) + +typedef uint64_t uECC_word_t; +#if SUPPORTS_INT128 +typedef unsigned __int128 uECC_dword_t; +#endif +typedef unsigned wordcount_t; +typedef int swordcount_t; +typedef int bitcount_t; +typedef int cmpresult_t; + +#define HIGH_BIT_SET 0x8000000000000000ull +#define uECC_WORD_BITS 64 +#define uECC_WORD_BITS_SHIFT 6 +#define uECC_WORD_BITS_MASK 0x03F + +#endif /* uECC_WORD_SIZE */ + +#if (defined(_WIN32) || defined(_WIN64)) +/* Windows */ + +#define WIN32_LEAN_AND_MEAN +#include +#include + +static int default_RNG(uint8_t *dest, unsigned size) { + HCRYPTPROV prov; + if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { + return 0; + } + + CryptGenRandom(prov, size, (BYTE *)dest); + CryptReleaseContext(prov, 0); + return 1; +} + +#elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \ + (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX) + +/* Some POSIX-like system with /dev/urandom or /dev/random. */ +#include +#include +#include + +#ifndef O_CLOEXEC + #define O_CLOEXEC 0 +#endif + +static int default_RNG(uint8_t *dest, unsigned size) { + int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); + if (fd == -1) { + fd = open("/dev/random", O_RDONLY | O_CLOEXEC); + if (fd == -1) { + return 0; + } + } + + char *ptr = (char *)dest; + size_t left = size; + while (left > 0) { + ssize_t bytes_read = read(fd, ptr, left); + if (bytes_read <= 0) { // read failed + close(fd); + return 0; + } + left -= bytes_read; + ptr += bytes_read; + } + + close(fd); + return 1; +} + +#else /* Some other platform */ + +static int default_RNG(uint8_t *dest, unsigned size) { + return 0; +} + +#endif + +// #ifdef __GNUC__ /* Only support GCC inline asm for now */ +// #if (uECC_ASM && (uECC_PLATFORM == uECC_avr)) +// #include "asm_avr.inc" +// #endif +// +// #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \ +// uECC_PLATFORM == uECC_arm_thumb2)) +// #include "asm_arm.inc" +// #endif +// #endif diff --git a/test/test_compress.c b/test/test_compress.c index 2d24569..ad56904 100644 --- a/test/test_compress.c +++ b/test/test_compress.c @@ -9,77 +9,60 @@ #define uECC_TEST_NUMBER_OF_ITERATIONS 256 #endif -#if LPC11XX - -#include "/Projects/lpc11xx/peripherals/uart.h" -#include "/Projects/lpc11xx/peripherals/time.h" - -static uint64_t g_rand = 88172645463325252ull; -int fake_rng(uint8_t *dest, unsigned size) { - while(size) { - g_rand ^= (g_rand << 13); - g_rand ^= (g_rand >> 7); - g_rand ^= (g_rand << 17); - - unsigned amount = (size > 8 ? 8 : size); - memcpy(dest, &g_rand, amount); - dest += amount; - size -= amount; - } - return 1; -} - -#endif - void vli_print(char *str, uint8_t *vli, unsigned int size) { printf("%s ", str); - while (size) { - printf("%02X ", (unsigned)vli[size - 1]); - --size; + for(unsigned i=0; i #include -void vli_print(uint8_t *vli, unsigned int size) { - while (size) { - printf("%02X ", (unsigned)vli[size - 1]); - --size; +void vli_print(char *str, uint8_t *vli, unsigned int size) { + printf("%s ", str); + for(unsigned i=0; i #include -#if LPC11XX - -#include "/Projects/lpc11xx/peripherals/uart.h" -#include "/Projects/lpc11xx/peripherals/time.h" - -static uint64_t g_rand = 88172645463325252ull; -int fake_rng(uint8_t *dest, unsigned size) { - while (size) { - g_rand ^= (g_rand << 13); - g_rand ^= (g_rand >> 7); - g_rand ^= (g_rand << 17); - - unsigned amount = (size > 8 ? 8 : size); - memcpy(dest, &g_rand, amount); - dest += amount; - size -= amount; - } - return 1; -} - -#endif - void vli_print(uint8_t *vli, unsigned int size) { - while (size) { - printf("%02X ", (unsigned)vli[size - 1]); - --size; + for(unsigned i=0; i #include -#if LPC11XX - -#include "/Projects/lpc11xx/peripherals/uart.h" -#include "/Projects/lpc11xx/peripherals/time.h" - -static uint64_t g_rand = 88172645463325252ull; -int fake_rng(uint8_t *dest, unsigned size) { - while (size) { - g_rand ^= (g_rand << 13); - g_rand ^= (g_rand >> 7); - g_rand ^= (g_rand << 17); - - unsigned amount = (size > 8 ? 8 : size); - memcpy(dest, &g_rand, amount); - dest += amount; - size -= amount; - } - return 1; -} - -#endif - int main() { -#if LPC11XX - uartInit(BAUD_115200); - initTime(); + int i, c; + uint8_t private[32] = {0}; + uint8_t public[64] = {0}; + uint8_t hash[32] = {0}; + uint8_t sig[64] = {0}; - uECC_set_rng(&fake_rng); -#endif - - uint8_t public[uECC_BYTES * 2]; - uint8_t private[uECC_BYTES]; - uint8_t hash[uECC_BYTES]; - uint8_t sig[uECC_BYTES * 2]; + const struct uECC_Curve_t * curves[5]; + curves[0] = uECC_secp160r1(); + curves[1] = uECC_secp192r1(); + curves[2] = uECC_secp224r1(); + curves[3] = uECC_secp256r1(); + curves[4] = uECC_secp256k1(); - int i; printf("Testing 256 signatures\n"); - for (i = 0; i < 256; ++i) { - printf("."); - #if !LPC11XX - fflush(stdout); - #endif - - if (!uECC_make_key(public, private)) { - printf("uECC_make_key() failed\n"); - continue; - } - memcpy(hash, public, uECC_BYTES); - - if (!uECC_sign(private, hash, sig)) { - printf("uECC_sign() failed\n"); - continue; - } - - if (!uECC_verify(public, hash, sig)) { - printf("uECC_verify() failed\n"); + for (c = 0; c < 5; ++c) { + for (i = 0; i < 256; ++i) { + printf("."); + fflush(stdout); + + if (!uECC_make_key(public, private, curves[c])) { + printf("uECC_make_key() failed\n"); + return 1; + } + memcpy(hash, public, sizeof(hash)); + + if (!uECC_sign(private, hash, sig, curves[c])) { + printf("uECC_sign() failed\n"); + return 1; + } + + if (!uECC_verify(public, hash, sig, curves[c])) { + printf("uECC_verify() failed\n"); + return 1; + } } + printf("\n"); } - printf("\n"); return 0; } diff --git a/test/test_ecdsa_deterministic.c b/test/test_ecdsa_deterministic.c.example similarity index 55% rename from test/test_ecdsa_deterministic.c rename to test/test_ecdsa_deterministic.c.example index 77d79aa..5e5cde0 100644 --- a/test/test_ecdsa_deterministic.c +++ b/test/test_ecdsa_deterministic.c.example @@ -5,28 +5,6 @@ #include #include -#if LPC11XX - -#include "/Projects/lpc11xx/peripherals/uart.h" -#include "/Projects/lpc11xx/peripherals/time.h" - -static uint64_t g_rand = 88172645463325252ull; -int fake_rng(uint8_t *dest, unsigned size) { - while (size) { - g_rand ^= (g_rand << 13); - g_rand ^= (g_rand >> 7); - g_rand ^= (g_rand << 17); - - unsigned amount = (size > 8 ? 8 : size); - memcpy(dest, &g_rand, amount); - dest += amount; - size -= amount; - } - return 1; -} - -#endif - #define SHA256_BLOCK_LENGTH 64 #define SHA256_DIGEST_LENGTH 32 @@ -63,17 +41,12 @@ static void finish_SHA256(uECC_HashContext *base, uint8_t *hash_result) { } int main() { -#if LPC11XX - uartInit(BAUD_115200); - initTime(); - - uECC_set_rng(&fake_rng); -#endif + int i, c; + uint8_t private[32] = {0}; + uint8_t public[64] = {0}; + uint8_t hash[32] = {0}; + uint8_t sig[64] = {0}; - uint8_t public[uECC_BYTES * 2]; - uint8_t private[uECC_BYTES]; - uint8_t hash[uECC_BYTES]; - uint8_t sig[uECC_BYTES * 2]; uint8_t tmp[2 * SHA256_DIGEST_LENGTH + SHA256_BLOCK_LENGTH]; SHA256_HashContext ctx = {{ &init_SHA256, @@ -83,31 +56,38 @@ int main() { SHA256_DIGEST_LENGTH, tmp }}; + + const struct uECC_Curve_t * curves[5]; + curves[0] = uECC_secp160r1(); + curves[1] = uECC_secp192r1(); + curves[2] = uECC_secp224r1(); + curves[3] = uECC_secp256r1(); + curves[4] = uECC_secp256k1(); - int i; printf("Testing 256 signatures\n"); - for (i = 0; i < 256; ++i) { - printf("."); - #if !LPC11XX - fflush(stdout); - #endif - - if (!uECC_make_key(public, private)) { - printf("uECC_make_key() failed\n"); - continue; - } - memcpy(hash, public, uECC_BYTES); - - if (!uECC_sign_deterministic(private, hash, &ctx.uECC, sig)) { - printf("uECC_sign() failed\n"); - continue; - } - - if (!uECC_verify(public, hash, sig)) { - printf("uECC_verify() failed\n"); + for (c = 0; c < 5; ++c) { + for (i = 0; i < 256; ++i) { + printf("."); + fflush(stdout); + + if (!uECC_make_key(public, private, curves[c])) { + printf("uECC_make_key() failed\n"); + return 1; + } + memcpy(hash, public, sizeof(hash)); + + if (!uECC_sign_deterministic(private, hash, &ctx.uECC, sig, curves[c])) { + printf("uECC_sign() failed\n"); + return 1; + } + + if (!uECC_verify(public, hash, sig, curves[c])) { + printf("uECC_verify() failed\n"); + return 1; + } } + printf("\n"); } - printf("\n"); return 0; } diff --git a/uECC.c b/uECC.c index afd4578..e6f92c6 100644 --- a/uECC.c +++ b/uECC.c @@ -2,468 +2,50 @@ #include "uECC.h" -#ifndef uECC_PLATFORM - #if __AVR__ - #define uECC_PLATFORM uECC_avr - #elif defined(__thumb2__) || defined(_M_ARMT) /* I think MSVC only supports Thumb-2 targets */ - #define uECC_PLATFORM uECC_arm_thumb2 - #elif defined(__thumb__) - #define uECC_PLATFORM uECC_arm_thumb - #elif defined(__arm__) || defined(_M_ARM) - #define uECC_PLATFORM uECC_arm - #elif defined(__i386__) || defined(_M_IX86) || defined(_X86_) || defined(__I86__) - #define uECC_PLATFORM uECC_x86 - #elif defined(__amd64__) || defined(_M_X64) - #define uECC_PLATFORM uECC_x86_64 - #else - #define uECC_PLATFORM uECC_arch_other - #endif -#endif - -#ifndef uECC_WORD_SIZE - #if uECC_PLATFORM == uECC_avr - #define uECC_WORD_SIZE 1 - #elif (uECC_PLATFORM == uECC_x86_64) - #define uECC_WORD_SIZE 8 - #else - #define uECC_WORD_SIZE 4 - #endif -#endif - -#if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp224r1) && (uECC_WORD_SIZE == 8) - #undef uECC_WORD_SIZE - #define uECC_WORD_SIZE 4 - #if (uECC_PLATFORM == uECC_x86_64) - #undef uECC_PLATFORM - #define uECC_PLATFORM uECC_x86 - #endif -#endif - -#if (uECC_WORD_SIZE != 1) && (uECC_WORD_SIZE != 4) && (uECC_WORD_SIZE != 8) - #error "Unsupported value for uECC_WORD_SIZE" -#endif - -#if (uECC_ASM && (uECC_PLATFORM == uECC_avr) && (uECC_WORD_SIZE != 1)) - #pragma message ("uECC_WORD_SIZE must be 1 when using AVR asm") - #undef uECC_WORD_SIZE - #define uECC_WORD_SIZE 1 -#endif - -#if (uECC_ASM && \ - (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb) && \ - (uECC_WORD_SIZE != 4)) - #pragma message ("uECC_WORD_SIZE must be 4 when using ARM asm") - #undef uECC_WORD_SIZE - #define uECC_WORD_SIZE 4 -#endif - -#if __STDC_VERSION__ >= 199901L - #define RESTRICT restrict -#else - #define RESTRICT -#endif - -#if defined(__SIZEOF_INT128__) || ((__clang_major__ * 100 + __clang_minor__) >= 302) - #define SUPPORTS_INT128 1 -#else - #define SUPPORTS_INT128 0 -#endif - #define MAX_TRIES 64 +#include "platform-specific.inc" + #if (uECC_WORD_SIZE == 1) - -typedef uint8_t uECC_word_t; -typedef uint16_t uECC_dword_t; -typedef uint8_t wordcount_t; -typedef int8_t swordcount_t; -typedef int16_t bitcount_t; -typedef int8_t cmpresult_t; - -#define HIGH_BIT_SET 0x80 -#define uECC_WORD_BITS 8 -#define uECC_WORD_BITS_SHIFT 3 -#define uECC_WORD_BITS_MASK 0x07 - -#define uECC_WORDS_1 20 -#define uECC_WORDS_2 24 -#define uECC_WORDS_3 32 -#define uECC_WORDS_4 32 -#define uECC_WORDS_5 28 - -#define uECC_N_WORDS_1 21 -#define uECC_N_WORDS_2 24 -#define uECC_N_WORDS_3 32 -#define uECC_N_WORDS_4 32 -#define uECC_N_WORDS_5 28 - -#define Curve_P_1 {0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_P_2 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_P_3 {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_P_4 {0x2F, 0xFC, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_P_5 {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF} - -#define Curve_B_1 {0x45, 0xFA, 0x65, 0xC5, 0xAD, 0xD4, 0xD4, 0x81, \ - 0x9F, 0xF8, 0xAC, 0x65, 0x8B, 0x7A, 0xBD, 0x54, \ - 0xFC, 0xBE, 0x97, 0x1C} -#define Curve_B_2 {0xB1, 0xB9, 0x46, 0xC1, 0xEC, 0xDE, 0xB8, 0xFE, \ - 0x49, 0x30, 0x24, 0x72, 0xAB, 0xE9, 0xA7, 0x0F, \ - 0xE7, 0x80, 0x9C, 0xE5, 0x19, 0x05, 0x21, 0x64} -#define Curve_B_3 {0x4B, 0x60, 0xD2, 0x27, 0x3E, 0x3C, 0xCE, 0x3B, \ - 0xF6, 0xB0, 0x53, 0xCC, 0xB0, 0x06, 0x1D, 0x65, \ - 0xBC, 0x86, 0x98, 0x76, 0x55, 0xBD, 0xEB, 0xB3, \ - 0xE7, 0x93, 0x3A, 0xAA, 0xD8, 0x35, 0xC6, 0x5A} -#define Curve_B_4 {0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} -#define Curve_B_5 {0xB4, 0xFF, 0x55, 0x23, 0x43, 0x39, 0x0B, 0x27, \ - 0xBA, 0xD8, 0xBF, 0xD7, 0xB7, 0xB0, 0x44, 0x50, \ - 0x56, 0x32, 0x41, 0xF5, 0xAB, 0xB3, 0x04, 0x0C, \ - 0x85, 0x0A, 0x05, 0xB4} - -#define Curve_G_1 { \ - {0x82, 0xFC, 0xCB, 0x13, 0xB9, 0x8B, 0xC3, 0x68, \ - 0x89, 0x69, 0x64, 0x46, 0x28, 0x73, 0xF5, 0x8E, \ - 0x68, 0xB5, 0x96, 0x4A}, \ - {0x32, 0xFB, 0xC5, 0x7A, 0x37, 0x51, 0x23, 0x04, \ - 0x12, 0xC9, 0xDC, 0x59, 0x7D, 0x94, 0x68, 0x31, \ - 0x55, 0x28, 0xA6, 0x23}} - -#define Curve_G_2 { \ - {0x12, 0x10, 0xFF, 0x82, 0xFD, 0x0A, 0xFF, 0xF4, \ - 0x00, 0x88, 0xA1, 0x43, 0xEB, 0x20, 0xBF, 0x7C, \ - 0xF6, 0x90, 0x30, 0xB0, 0x0E, 0xA8, 0x8D, 0x18}, \ - {0x11, 0x48, 0x79, 0x1E, 0xA1, 0x77, 0xF9, 0x73, \ - 0xD5, 0xCD, 0x24, 0x6B, 0xED, 0x11, 0x10, 0x63, \ - 0x78, 0xDA, 0xC8, 0xFF, 0x95, 0x2B, 0x19, 0x07}} - -#define Curve_G_3 { \ - {0x96, 0xC2, 0x98, 0xD8, 0x45, 0x39, 0xA1, 0xF4, \ - 0xA0, 0x33, 0xEB, 0x2D, 0x81, 0x7D, 0x03, 0x77, \ - 0xF2, 0x40, 0xA4, 0x63, 0xE5, 0xE6, 0xBC, 0xF8, \ - 0x47, 0x42, 0x2C, 0xE1, 0xF2, 0xD1, 0x17, 0x6B}, \ - {0xF5, 0x51, 0xBF, 0x37, 0x68, 0x40, 0xB6, 0xCB, \ - 0xCE, 0x5E, 0x31, 0x6B, 0x57, 0x33, 0xCE, 0x2B, \ - 0x16, 0x9E, 0x0F, 0x7C, 0x4A, 0xEB, 0xE7, 0x8E, \ - 0x9B, 0x7F, 0x1A, 0xFE, 0xE2, 0x42, 0xE3, 0x4F}} - -#define Curve_G_4 { \ - {0x98, 0x17, 0xF8, 0x16, 0x5B, 0x81, 0xF2, 0x59, \ - 0xD9, 0x28, 0xCE, 0x2D, 0xDB, 0xFC, 0x9B, 0x02, \ - 0x07, 0x0B, 0x87, 0xCE, 0x95, 0x62, 0xA0, 0x55, \ - 0xAC, 0xBB, 0xDC, 0xF9, 0x7E, 0x66, 0xBE, 0x79}, \ - {0xB8, 0xD4, 0x10, 0xFB, 0x8F, 0xD0, 0x47, 0x9C, \ - 0x19, 0x54, 0x85, 0xA6, 0x48, 0xB4, 0x17, 0xFD, \ - 0xA8, 0x08, 0x11, 0x0E, 0xFC, 0xFB, 0xA4, 0x5D, \ - 0x65, 0xC4, 0xA3, 0x26, 0x77, 0xDA, 0x3A, 0x48}} - -#define Curve_G_5 { \ - {0x21, 0x1D, 0x5C, 0x11, 0xD6, 0x80, 0x32, 0x34, \ - 0x22, 0x11, 0xC2, 0x56, 0xD3, 0xC1, 0x03, 0x4A, \ - 0xB9, 0x90, 0x13, 0x32, 0x7F, 0xBF, 0xB4, 0x6B, \ - 0xBD, 0x0C, 0x0E, 0xB7}, \ - {0x34, 0x7E, 0x00, 0x85, 0x99, 0x81, 0xD5, 0x44, \ - 0x64, 0x47, 0x07, 0x5A, 0xA0, 0x75, 0x43, 0xCD, \ - 0xE6, 0xDF, 0x22, 0x4C, 0xFB, 0x23, 0xF7, 0xB5, \ - 0x88, 0x63, 0x37, 0xBD}} - -#define Curve_N_1 {0x57, 0x22, 0x75, 0xCA, 0xD3, 0xAE, 0x27, 0xF9, \ - 0xC8, 0xF4, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, \ - 0x00, 0x00, 0x00, 0x00, 0x01} -#define Curve_N_2 {0x31, 0x28, 0xD2, 0xB4, 0xB1, 0xC9, 0x6B, 0x14, \ - 0x36, 0xF8, 0xDE, 0x99, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_N_3 {0x51, 0x25, 0x63, 0xFC, 0xC2, 0xCA, 0xB9, 0xF3, \ - 0x84, 0x9E, 0x17, 0xA7, 0xAD, 0xFA, 0xE6, 0xBC, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_N_4 {0x41, 0x41, 0x36, 0xD0, 0x8C, 0x5E, 0xD2, 0xBF, \ - 0x3B, 0xA0, 0x48, 0xAF, 0xE6, 0xDC, 0xAE, 0xBA, \ - 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF} -#define Curve_N_5 {0x3D, 0x2A, 0x5C, 0x5C, 0x45, 0x29, 0xDD, 0x13, \ - 0x3E, 0xF0, 0xB8, 0xE0, 0xA2, 0x16, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ - 0xFF, 0xFF, 0xFF, 0xFF} - + #define uECC_MAX_WORDS 32 #elif (uECC_WORD_SIZE == 4) - -typedef uint32_t uECC_word_t; -typedef uint64_t uECC_dword_t; -typedef unsigned wordcount_t; -typedef int swordcount_t; -typedef int bitcount_t; -typedef int cmpresult_t; - -#define HIGH_BIT_SET 0x80000000 -#define uECC_WORD_BITS 32 -#define uECC_WORD_BITS_SHIFT 5 -#define uECC_WORD_BITS_MASK 0x01F - -#define uECC_WORDS_1 5 -#define uECC_WORDS_2 6 -#define uECC_WORDS_3 8 -#define uECC_WORDS_4 8 -#define uECC_WORDS_5 7 - -#define uECC_N_WORDS_1 6 -#define uECC_N_WORDS_2 6 -#define uECC_N_WORDS_3 8 -#define uECC_N_WORDS_4 8 -#define uECC_N_WORDS_5 7 - -#define Curve_P_1 {0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} -#define Curve_P_2 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} -#define Curve_P_3 {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, \ - 0x00000000, 0x00000000, 0x00000001, 0xFFFFFFFF} -#define Curve_P_4 {0xFFFFFC2F, 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, \ - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} -#define Curve_P_5 {0x00000001, 0x00000000, 0x00000000, 0xFFFFFFFF, \ - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} - -#define Curve_B_1 {0xC565FA45, 0x81D4D4AD, 0x65ACF89F, 0x54BD7A8B, 0x1C97BEFC} -#define Curve_B_2 {0xC146B9B1, 0xFEB8DEEC, 0x72243049, 0x0FA7E9AB, 0xE59C80E7, 0x64210519} -#define Curve_B_3 {0x27D2604B, 0x3BCE3C3E, 0xCC53B0F6, 0x651D06B0, \ - 0x769886BC, 0xB3EBBD55, 0xAA3A93E7, 0x5AC635D8} -#define Curve_B_4 {0x00000007, 0x00000000, 0x00000000, 0x00000000, \ - 0x00000000, 0x00000000, 0x00000000, 0x00000000} -#define Curve_B_5 {0x2355FFB4, 0x270B3943, 0xD7BFD8BA, 0x5044B0B7, \ - 0xF5413256, 0x0C04B3AB, 0xB4050A85} - -#define Curve_G_1 { \ - {0x13CBFC82, 0x68C38BB9, 0x46646989, 0x8EF57328, 0x4A96B568}, \ - {0x7AC5FB32, 0x04235137, 0x59DCC912, 0x3168947D, 0x23A62855}} - -#define Curve_G_2 { \ - {0x82FF1012, 0xF4FF0AFD, 0x43A18800, 0x7CBF20EB, 0xB03090F6, 0x188DA80E}, \ - {0x1E794811, 0x73F977A1, 0x6B24CDD5, 0x631011ED, 0xFFC8DA78, 0x07192B95}} - -#define Curve_G_3 { \ - {0xD898C296, 0xF4A13945, 0x2DEB33A0, 0x77037D81, \ - 0x63A440F2, 0xF8BCE6E5, 0xE12C4247, 0x6B17D1F2}, \ - {0x37BF51F5, 0xCBB64068, 0x6B315ECE, 0x2BCE3357, \ - 0x7C0F9E16, 0x8EE7EB4A, 0xFE1A7F9B, 0x4FE342E2}} - -#define Curve_G_4 { \ - {0x16F81798, 0x59F2815B, 0x2DCE28D9, 0x029BFCDB, \ - 0xCE870B07, 0x55A06295, 0xF9DCBBAC, 0x79BE667E}, \ - {0xFB10D4B8, 0x9C47D08F, 0xA6855419, 0xFD17B448, \ - 0x0E1108A8, 0x5DA4FBFC, 0x26A3C465, 0x483ADA77}} - -#define Curve_G_5 { \ - {0x115C1D21, 0x343280D6, 0x56C21122, 0x4A03C1D3, \ - 0x321390B9, 0x6BB4BF7F, 0xB70E0CBD}, \ - {0x85007E34, 0x44D58199, 0x5A074764, 0xCD4375A0, \ - 0x4C22DFE6, 0xB5F723FB, 0xBD376388}} - -#define Curve_N_1 {0xCA752257, 0xF927AED3, 0x0001F4C8, 0x00000000, 0x00000000, 0x00000001} -#define Curve_N_2 {0xB4D22831, 0x146BC9B1, 0x99DEF836, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} -#define Curve_N_3 {0xFC632551, 0xF3B9CAC2, 0xA7179E84, 0xBCE6FAAD, \ - 0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFFFFFF} -#define Curve_N_4 {0xD0364141, 0xBFD25E8C, 0xAF48A03B, 0xBAAEDCE6, \ - 0xFFFFFFFE, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} -#define Curve_N_5 {0x5C5C2A3D, 0x13DD2945, 0xE0B8F03E, 0xFFFF16A2, \ - 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF} - + #define uECC_MAX_WORDS 8 #elif (uECC_WORD_SIZE == 8) + #define uECC_MAX_WORDS 4 +#endif /* uECC_WORD_SIZE */ -typedef uint64_t uECC_word_t; -#if SUPPORTS_INT128 -typedef unsigned __int128 uECC_dword_t; -#endif -typedef unsigned wordcount_t; -typedef int swordcount_t; -typedef int bitcount_t; -typedef int cmpresult_t; +struct uECC_Curve_t { + wordcount_t num_words; + wordcount_t num_n_words; + wordcount_t num_bytes; + uECC_word_t p[uECC_MAX_WORDS]; + uECC_word_t n[uECC_MAX_WORDS]; + uECC_word_t G[uECC_MAX_WORDS * 2]; + uECC_word_t b[uECC_MAX_WORDS]; + void (*double_jacobian)(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * Z1, + uECC_Curve curve); + void (*mod_sqrt)(uECC_word_t *a, uECC_Curve curve); + void (*x_side)(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve); +}; -#define HIGH_BIT_SET 0x8000000000000000ull -#define uECC_WORD_BITS 64 -#define uECC_WORD_BITS_SHIFT 6 -#define uECC_WORD_BITS_MASK 0x03F +static void double_jacobian_secp256k1(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * Z1, + uECC_Curve curve); +static void double_jacobian_default(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * Z1, + uECC_Curve curve); -#define uECC_WORDS_1 3 -#define uECC_WORDS_2 3 -#define uECC_WORDS_3 4 -#define uECC_WORDS_4 4 -#define uECC_WORDS_5 4 +static void mod_sqrt_default(uECC_word_t *a, uECC_Curve curve); +static void mod_sqrt_secp224r1(uECC_word_t *a, uECC_Curve curve); -#define uECC_N_WORDS_1 3 -#define uECC_N_WORDS_2 3 -#define uECC_N_WORDS_3 4 -#define uECC_N_WORDS_4 4 -#define uECC_N_WORDS_5 4 +static void x_side_default(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve); +static void x_side_secp256k1(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve); -#define Curve_P_1 {0xFFFFFFFF7FFFFFFFull, 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} -#define Curve_P_2 {0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull} -#define Curve_P_3 {0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull, \ - 0x0000000000000000ull, 0xFFFFFFFF00000001ull} -#define Curve_P_4 {0xFFFFFFFEFFFFFC2Full, 0xFFFFFFFFFFFFFFFFull, \ - 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFFFFFFFFFFull} -#define Curve_P_5 {0x0000000000000001ull, 0xFFFFFFFF00000000ull, \ - 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} - -#define Curve_B_1 {0x81D4D4ADC565FA45ull, 0x54BD7A8B65ACF89Full, 0x000000001C97BEFCull} -#define Curve_B_2 {0xFEB8DEECC146B9B1ull, 0x0FA7E9AB72243049ull, 0x64210519E59C80E7ull} -#define Curve_B_3 {0x3BCE3C3E27D2604Bull, 0x651D06B0CC53B0F6ull, \ - 0xB3EBBD55769886BCull, 0x5AC635D8AA3A93E7ull} -#define Curve_B_4 {0x0000000000000007ull, 0x0000000000000000ull, \ - 0x0000000000000000ull, 0x0000000000000000ull} -#define Curve_B_5 {0x270B39432355FFB4ull, 0x5044B0B7D7BFD8BAull, \ - 0x0C04B3ABF5413256ull, 0x00000000B4050A85ull} - -#define Curve_G_1 { \ - {0x68C38BB913CBFC82ull, 0x8EF5732846646989ull, 0x000000004A96B568ull}, \ - {0x042351377AC5FB32ull, 0x3168947D59DCC912ull, 0x0000000023A62855ull}} - -#define Curve_G_2 { \ - {0xF4FF0AFD82FF1012ull, 0x7CBF20EB43A18800ull, 0x188DA80EB03090F6ull}, \ - {0x73F977A11E794811ull, 0x631011ED6B24CDD5ull, 0x07192B95FFC8DA78ull}} - -#define Curve_G_3 { \ - {0xF4A13945D898C296ull, 0x77037D812DEB33A0ull, 0xF8BCE6E563A440F2ull, 0x6B17D1F2E12C4247ull}, \ - {0xCBB6406837BF51F5ull, 0x2BCE33576B315ECEull, 0x8EE7EB4A7C0F9E16ull, 0x4FE342E2FE1A7F9Bull}} - -#define Curve_G_4 { \ - {0x59F2815B16F81798ull, 0x029BFCDB2DCE28D9ull, 0x55A06295CE870B07ull, 0x79BE667EF9DCBBACull}, \ - {0x9C47D08FFB10D4B8ull, 0xFD17B448A6855419ull, 0x5DA4FBFC0E1108A8ull, 0x483ADA7726A3C465ull}} - -#define Curve_G_5 { \ - {0x343280D6115C1D21ull, 0x4A03C1D356C21122ull, 0x6BB4BF7F321390B9ull, 0x00000000B70E0CBDull}, \ - {0x44D5819985007E34ull, 0xCD4375A05A074764ull, 0xB5F723FB4C22DFE6ull, 0x00000000BD376388ull}} - -#define Curve_N_1 {0xF927AED3CA752257ull, 0x000000000001F4C8ull, 0x0000000100000000ull} -#define Curve_N_2 {0x146BC9B1B4D22831ull, 0xFFFFFFFF99DEF836ull, 0xFFFFFFFFFFFFFFFFull} -#define Curve_N_3 {0xF3B9CAC2FC632551ull, 0xBCE6FAADA7179E84ull, \ - 0xFFFFFFFFFFFFFFFFull, 0xFFFFFFFF00000000ull} -#define Curve_N_4 {0xBFD25E8CD0364141ull, 0xBAAEDCE6AF48A03Bull, \ - 0xFFFFFFFFFFFFFFFEull, 0xFFFFFFFFFFFFFFFFull} -#define Curve_N_5 {0x13DD29455C5C2A3Dull, 0xFFFF16A2E0B8F03Eull, \ - 0xFFFFFFFFFFFFFFFFull, 0x00000000FFFFFFFFull} - -#endif /* (uECC_WORD_SIZE == 8) */ - -#define uECC_WORDS uECC_CONCAT(uECC_WORDS_, uECC_CURVE) -#define uECC_N_WORDS uECC_CONCAT(uECC_N_WORDS_, uECC_CURVE) - -typedef struct EccPoint { - uECC_word_t x[uECC_WORDS]; - uECC_word_t y[uECC_WORDS]; -} EccPoint; - -static const uECC_word_t curve_p[uECC_WORDS] = uECC_CONCAT(Curve_P_, uECC_CURVE); -static const uECC_word_t curve_b[uECC_WORDS] = uECC_CONCAT(Curve_B_, uECC_CURVE); -static const EccPoint curve_G = uECC_CONCAT(Curve_G_, uECC_CURVE); -static const uECC_word_t curve_n[uECC_N_WORDS] = uECC_CONCAT(Curve_N_, uECC_CURVE); - -static void vli_clear(uECC_word_t *vli); -static uECC_word_t vli_isZero(const uECC_word_t *vli); -static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit); -static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words); -static void vli_set(uECC_word_t *dest, const uECC_word_t *src); -static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right); -static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right); -static void vli_rshift1(uECC_word_t *vli); -static uECC_word_t vli_add(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right); -static uECC_word_t vli_sub(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right); -static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right); -static void vli_modAdd(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right, - const uECC_word_t *mod); -static void vli_modSub(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right, - const uECC_word_t *mod); -static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product); -static void vli_modMult_fast(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right); -static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod); -#if uECC_SQUARE_FUNC -static void vli_square(uECC_word_t *result, const uECC_word_t *left); -static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left); -#endif - -#if (defined(_WIN32) || defined(_WIN64)) -/* Windows */ - -#define WIN32_LEAN_AND_MEAN -#include -#include - -static int default_RNG(uint8_t *dest, unsigned size) { - HCRYPTPROV prov; - if (!CryptAcquireContext(&prov, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { - return 0; - } - - CryptGenRandom(prov, size, (BYTE *)dest); - CryptReleaseContext(prov, 0); - return 1; -} - -#elif defined(unix) || defined(__linux__) || defined(__unix__) || defined(__unix) || \ - (defined(__APPLE__) && defined(__MACH__)) || defined(uECC_POSIX) - -/* Some POSIX-like system with /dev/urandom or /dev/random. */ -#include -#include -#include - -#ifndef O_CLOEXEC - #define O_CLOEXEC 0 -#endif - -static int default_RNG(uint8_t *dest, unsigned size) { - int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC); - if (fd == -1) { - fd = open("/dev/random", O_RDONLY | O_CLOEXEC); - if (fd == -1) { - return 0; - } - } - - char *ptr = (char *)dest; - size_t left = size; - while (left > 0) { - ssize_t bytes_read = read(fd, ptr, left); - if (bytes_read <= 0) { // read failed - close(fd); - return 0; - } - left -= bytes_read; - ptr += bytes_read; - } - - close(fd); - return 1; -} - -#else /* Some other platform */ - -static int default_RNG(uint8_t *dest, unsigned size) { - return 0; -} - -#endif +#include "curve-specific.inc" static uECC_RNG_Function g_rng_function = &default_RNG; @@ -471,49 +53,31 @@ void uECC_set_rng(uECC_RNG_Function rng_function) { g_rng_function = rng_function; } -#ifdef __GNUC__ /* Only support GCC inline asm for now */ - #if (uECC_ASM && (uECC_PLATFORM == uECC_avr)) - #include "asm_avr.inc" - #endif - - #if (uECC_ASM && (uECC_PLATFORM == uECC_arm || uECC_PLATFORM == uECC_arm_thumb || \ - uECC_PLATFORM == uECC_arm_thumb2)) - #include "asm_arm.inc" - #endif -#endif - -#if !asm_clear -static void vli_clear(uECC_word_t *vli) { +static void vli_clear(uECC_word_t *vli, const wordcount_t num_words) { wordcount_t i; - for (i = 0; i < uECC_WORDS; ++i) { + for (i = 0; i < num_words; ++i) { vli[i] = 0; } } -#endif /* Returns 1 if vli == 0, 0 otherwise. */ -#if !asm_isZero -static uECC_word_t vli_isZero(const uECC_word_t *vli) { +static uECC_word_t vli_isZero(const uECC_word_t *vli, const wordcount_t num_words) { wordcount_t i; - for (i = 0; i < uECC_WORDS; ++i) { + for (i = 0; i < num_words; ++i) { if (vli[i]) { return 0; } } return 1; } -#endif /* Returns nonzero if bit 'bit' of vli is set. */ -#if !asm_testBit static uECC_word_t vli_testBit(const uECC_word_t *vli, bitcount_t bit) { return (vli[bit >> uECC_WORD_BITS_SHIFT] & ((uECC_word_t)1 << (bit & uECC_WORD_BITS_MASK))); } -#endif /* Counts the number of words in vli. */ -#if !asm_numBits -static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) { +static wordcount_t vli_numDigits(const uECC_word_t *vli, const wordcount_t max_words) { swordcount_t i; /* Search from the end until we find a non-zero digit. We do it in reverse because we expect that most digits will be nonzero. */ @@ -524,7 +88,7 @@ static wordcount_t vli_numDigits(const uECC_word_t *vli, wordcount_t max_words) } /* Counts the number of bits required to represent vli. */ -static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) { +static bitcount_t vli_numBits(const uECC_word_t *vli, const wordcount_t max_words) { uECC_word_t i; uECC_word_t digit; @@ -540,23 +104,21 @@ static bitcount_t vli_numBits(const uECC_word_t *vli, wordcount_t max_words) { return (((bitcount_t)(num_digits - 1) << uECC_WORD_BITS_SHIFT) + i); } -#endif /* !asm_numBits */ /* Sets dest = src. */ -#if !asm_set -static void vli_set(uECC_word_t *dest, const uECC_word_t *src) { +static void vli_set(uECC_word_t *dest, const uECC_word_t *src, const wordcount_t num_words) { wordcount_t i; - for (i = 0; i < uECC_WORDS; ++i) { + for (i = 0; i < num_words; ++i) { dest[i] = src[i]; } } -#endif /* Returns sign of left - right. */ -#if !asm_cmp -static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { +static cmpresult_t vli_cmp(const uECC_word_t *left, + const uECC_word_t *right, + const wordcount_t num_words) { swordcount_t i; - for (i = uECC_WORDS - 1; i >= 0; --i) { + for (i = num_words - 1; i >= 0; --i) { if (left[i] > right[i]) { return 1; } else if (left[i] < right[i]) { @@ -565,38 +127,28 @@ static cmpresult_t vli_cmp(const uECC_word_t *left, const uECC_word_t *right) { } return 0; } -#endif - -static cmpresult_t vli_equal(const uECC_word_t *left, const uECC_word_t *right) { - uECC_word_t result = 0; - swordcount_t i; - for (i = uECC_WORDS - 1; i >= 0; --i) { - result |= (left[i] ^ right[i]); - } - return (result == 0); -} /* Computes vli = vli >> 1. */ -#if !asm_rshift1 -static void vli_rshift1(uECC_word_t *vli) { +static void vli_rshift1(uECC_word_t *vli, const wordcount_t num_words) { uECC_word_t *end = vli; uECC_word_t carry = 0; - - vli += uECC_WORDS; + + vli += num_words; while (vli-- > end) { uECC_word_t temp = *vli; *vli = (temp >> 1) | carry; carry = temp << (uECC_WORD_BITS - 1); } } -#endif /* Computes result = left + right, returning carry. Can modify in place. */ -#if !asm_add -static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { +static uECC_word_t vli_add(uECC_word_t *result, + const uECC_word_t *left, + const uECC_word_t *right, + const wordcount_t num_words) { uECC_word_t carry = 0; wordcount_t i; - for (i = 0; i < uECC_WORDS; ++i) { + for (i = 0; i < num_words; ++i) { uECC_word_t sum = left[i] + right[i] + carry; if (sum != left[i]) { carry = (sum < left[i]); @@ -605,14 +157,15 @@ static uECC_word_t vli_add(uECC_word_t *result, const uECC_word_t *left, const u } return carry; } -#endif /* Computes result = left - right, returning borrow. Can modify in place. */ -#if !asm_sub -static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { +static uECC_word_t vli_sub(uECC_word_t *result, + const uECC_word_t *left, + const uECC_word_t *right, + const wordcount_t num_words) { uECC_word_t borrow = 0; wordcount_t i; - for (i = 0; i < uECC_WORDS; ++i) { + for (i = 0; i < num_words; ++i) { uECC_word_t diff = left[i] - right[i] - borrow; if (diff != left[i]) { borrow = (diff > left[i]); @@ -621,9 +174,7 @@ static uECC_word_t vli_sub(uECC_word_t *result, const uECC_word_t *left, const u } return borrow; } -#endif -#if (!asm_mult || (uECC_SQUARE_FUNC && !asm_square) || uECC_CURVE == uECC_secp256k1) static void muladd(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, @@ -663,18 +214,18 @@ static void muladd(uECC_word_t a, *r0 = (uECC_word_t)r01; #endif } -#define muladd_exists 1 -#endif -#if !asm_mult -static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { +static void vli_mult(uECC_word_t *result, + const uECC_word_t *left, + const uECC_word_t *right, + const wordcount_t num_words) { uECC_word_t r0 = 0; uECC_word_t r1 = 0; uECC_word_t r2 = 0; wordcount_t i, k; /* Compute each digit of result in sequence, maintaining the carries. */ - for (k = 0; k < uECC_WORDS; ++k) { + for (k = 0; k < num_words; ++k) { for (i = 0; i <= k; ++i) { muladd(left[i], right[k - i], &r0, &r1, &r2); } @@ -683,8 +234,8 @@ static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_wo r1 = r2; r2 = 0; } - for (k = uECC_WORDS; k < uECC_WORDS * 2 - 1; ++k) { - for (i = (k + 1) - uECC_WORDS; i < uECC_WORDS; ++i) { + for (k = num_words; k < num_words * 2 - 1; ++k) { + for (i = (k + 1) - num_words; i < num_words; ++i) { muladd(left[i], right[k - i], &r0, &r1, &r2); } result[k] = r0; @@ -692,13 +243,11 @@ static void vli_mult(uECC_word_t *result, const uECC_word_t *left, const uECC_wo r1 = r2; r2 = 0; } - result[uECC_WORDS * 2 - 1] = r0; + result[num_words * 2 - 1] = r0; } -#endif #if uECC_SQUARE_FUNC -#if !asm_square static void mul2add(uECC_word_t a, uECC_word_t b, uECC_word_t *r0, @@ -746,15 +295,15 @@ static void mul2add(uECC_word_t a, #endif } -static void vli_square(uECC_word_t *result, const uECC_word_t *left) { +static void vli_square(uECC_word_t *result, const uECC_word_t *left, const wordcount_t num_words) { uECC_word_t r0 = 0; uECC_word_t r1 = 0; uECC_word_t r2 = 0; wordcount_t i, k; - - for (k = 0; k < uECC_WORDS * 2 - 1; ++k) { - uECC_word_t min = (k < uECC_WORDS ? 0 : (k + 1) - uECC_WORDS); + + for (k = 0; k < num_words * 2 - 1; ++k) { + uECC_word_t min = (k < num_words ? 0 : (k + 1) - num_words); for (i = min; i <= k && i <= k - i; ++i) { if (i < k-i) { mul2add(left[i], left[k - i], &r0, &r1, &r2); @@ -767,10 +316,9 @@ static void vli_square(uECC_word_t *result, const uECC_word_t *left) { r1 = r2; r2 = 0; } - - result[uECC_WORDS * 2 - 1] = r0; + + result[num_words * 2 - 1] = r0; } -#endif #else /* uECC_SQUARE_FUNC */ @@ -778,771 +326,168 @@ static void vli_square(uECC_word_t *result, const uECC_word_t *left) { #endif /* uECC_SQUARE_FUNC */ - /* Computes result = (left + right) % mod. Assumes that left < mod and right < mod, and that result does not overlap mod. */ -#if !asm_modAdd static void vli_modAdd(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, - const uECC_word_t *mod) { - uECC_word_t carry = vli_add(result, left, right); - if (carry || vli_cmp(result, mod) >= 0) { + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t carry = vli_add(result, left, right, num_words); + if (carry || vli_cmp(result, mod, num_words) >= 0) { /* result > mod (result = mod + remainder), so subtract mod to get remainder. */ - vli_sub(result, result, mod); + vli_sub(result, result, mod, num_words); } } -#endif /* Computes result = (left - right) % mod. Assumes that left < mod and right < mod, and that result does not overlap mod. */ -#if !asm_modSub static void vli_modSub(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right, - const uECC_word_t *mod) { - uECC_word_t l_borrow = vli_sub(result, left, right); + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t l_borrow = vli_sub(result, left, right, num_words); if (l_borrow) { /* In this case, result == -diff == (max int) - diff. Since -x % d == d - x, we can get the correct result from result + mod (with overflow). */ - vli_add(result, result, mod); - } -} -#endif - -#if !asm_modSub_fast - #define vli_modSub_fast(result, left, right) vli_modSub((result), (left), (right), curve_p) -#endif - -#if !asm_mmod_fast - -#if (uECC_CURVE == uECC_secp160r1 || uECC_CURVE == uECC_secp256k1) -/* omega_mult() is defined farther below for the different curves / word sizes */ -static void omega_mult(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT right); - -/* Computes result = product % curve_p - see http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf page 354 - - Note that this only works if log2(omega) < log2(p) / 2 */ -static void vli_mmod_fast(uECC_word_t *RESTRICT result, uECC_word_t *RESTRICT product) { - uECC_word_t tmp[2 * uECC_WORDS]; - uECC_word_t carry; - - vli_clear(tmp); - vli_clear(tmp + uECC_WORDS); - - omega_mult(tmp, product + uECC_WORDS); /* (Rq, q) = q * c */ - - carry = vli_add(result, product, tmp); /* (C, r) = r + q */ - vli_clear(product); - omega_mult(product, tmp + uECC_WORDS); /* Rq*c */ - carry += vli_add(result, result, product); /* (C1, r) = r + Rq*c */ - - while (carry > 0) { - --carry; - vli_sub(result, result, curve_p); - } - if (vli_cmp(result, curve_p) > 0) { - vli_sub(result, result, curve_p); + vli_add(result, result, mod, num_words); } } -#endif - -#if uECC_CURVE == uECC_secp160r1 - -#if uECC_WORD_SIZE == 1 -static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { - uint8_t carry; - uint8_t i; - - /* Multiply by (2^31 + 1). */ - vli_set(result + 4, right); /* 2^32 */ - vli_rshift1(result + 4); /* 2^31 */ - result[3] = right[0] << 7; /* get last bit from shift */ - - carry = vli_add(result, result, right); /* 2^31 + 1 */ - for (i = uECC_WORDS; carry; ++i) { - uint16_t sum = (uint16_t)result[i] + carry; - result[i] = (uint8_t)sum; - carry = sum >> 8; - } -} -#elif uECC_WORD_SIZE == 4 -static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { - uint32_t carry; - unsigned i; - - /* Multiply by (2^31 + 1). */ - vli_set(result + 1, right); /* 2^32 */ - vli_rshift1(result + 1); /* 2^31 */ - result[0] = right[0] << 31; /* get last bit from shift */ - - carry = vli_add(result, result, right); /* 2^31 + 1 */ - for (i = uECC_WORDS; carry; ++i) { - uint64_t sum = (uint64_t)result[i] + carry; - result[i] = (uint32_t)sum; - carry = sum >> 32; - } -} -#endif /* uECC_WORD_SIZE */ - -#elif uECC_CURVE == uECC_secp192r1 - -/* Computes result = product % curve_p. - See algorithm 5 and 6 from http://www.isys.uni-klu.ac.at/PDF/2001-0126-MT.pdf */ -#if uECC_WORD_SIZE == 1 -static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { - uint8_t tmp[uECC_WORDS]; - uint8_t carry; - - vli_set(result, product); - - vli_set(tmp, &product[24]); - carry = vli_add(result, result, tmp); - - tmp[0] = tmp[1] = tmp[2] = tmp[3] = tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; - tmp[8] = product[24]; tmp[9] = product[25]; tmp[10] = product[26]; tmp[11] = product[27]; - tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; - tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; - tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; - carry += vli_add(result, result, tmp); - - tmp[0] = tmp[8] = product[40]; - tmp[1] = tmp[9] = product[41]; - tmp[2] = tmp[10] = product[42]; - tmp[3] = tmp[11] = product[43]; - tmp[4] = tmp[12] = product[44]; - tmp[5] = tmp[13] = product[45]; - tmp[6] = tmp[14] = product[46]; - tmp[7] = tmp[15] = product[47]; - tmp[16] = tmp[17] = tmp[18] = tmp[19] = tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; - carry += vli_add(result, result, tmp); - - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } -} -#elif uECC_WORD_SIZE == 4 -static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { - uint32_t tmp[uECC_WORDS]; - int carry; - - vli_set(result, product); - - vli_set(tmp, &product[6]); - carry = vli_add(result, result, tmp); - - tmp[0] = tmp[1] = 0; - tmp[2] = product[6]; - tmp[3] = product[7]; - tmp[4] = product[8]; - tmp[5] = product[9]; - carry += vli_add(result, result, tmp); - - tmp[0] = tmp[2] = product[10]; - tmp[1] = tmp[3] = product[11]; - tmp[4] = tmp[5] = 0; - carry += vli_add(result, result, tmp); - - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } -} -#else -static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { - uint64_t tmp[uECC_WORDS]; - int carry; - - vli_set(result, product); - - vli_set(tmp, &product[3]); - carry = vli_add(result, result, tmp); - - tmp[0] = 0; - tmp[1] = product[3]; - tmp[2] = product[4]; - carry += vli_add(result, result, tmp); - - tmp[0] = tmp[1] = product[5]; - tmp[2] = 0; - carry += vli_add(result, result, tmp); - - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } -} -#endif /* uECC_WORD_SIZE */ - -#elif uECC_CURVE == uECC_secp256r1 - -/* Computes result = product % curve_p - from http://www.nsa.gov/ia/_files/nist-routines.pdf */ -#if uECC_WORD_SIZE == 1 -static void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { - uint8_t tmp[uECC_BYTES]; - int8_t carry; - - /* t */ - vli_set(result, product); - - /* s1 */ - tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; - tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; - tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; - tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; - tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; - tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; - tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; - tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; - carry = vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s2 */ - tmp[12] = product[48]; tmp[13] = product[49]; tmp[14] = product[50]; tmp[15] = product[51]; - tmp[16] = product[52]; tmp[17] = product[53]; tmp[18] = product[54]; tmp[19] = product[55]; - tmp[20] = product[56]; tmp[21] = product[57]; tmp[22] = product[58]; tmp[23] = product[59]; - tmp[24] = product[60]; tmp[25] = product[61]; tmp[26] = product[62]; tmp[27] = product[63]; - tmp[28] = tmp[29] = tmp[30] = tmp[31] = 0; - carry += vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s3 */ - tmp[0] = product[32]; tmp[1] = product[33]; tmp[2] = product[34]; tmp[3] = product[35]; - tmp[4] = product[36]; tmp[5] = product[37]; tmp[6] = product[38]; tmp[7] = product[39]; - tmp[8] = product[40]; tmp[9] = product[41]; tmp[10] = product[42]; tmp[11] = product[43]; - tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; - tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; - tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; - tmp[24] = product[56]; tmp[25] = product[57]; tmp[26] = product[58]; tmp[27] = product[59]; - tmp[28] = product[60]; tmp[29] = product[61]; tmp[30] = product[62]; tmp[31] = product[63]; - carry += vli_add(result, result, tmp); - - /* s4 */ - tmp[0] = product[36]; tmp[1] = product[37]; tmp[2] = product[38]; tmp[3] = product[39]; - tmp[4] = product[40]; tmp[5] = product[41]; tmp[6] = product[42]; tmp[7] = product[43]; - tmp[8] = product[44]; tmp[9] = product[45]; tmp[10] = product[46]; tmp[11] = product[47]; - tmp[12] = product[52]; tmp[13] = product[53]; tmp[14] = product[54]; tmp[15] = product[55]; - tmp[16] = product[56]; tmp[17] = product[57]; tmp[18] = product[58]; tmp[19] = product[59]; - tmp[20] = product[60]; tmp[21] = product[61]; tmp[22] = product[62]; tmp[23] = product[63]; - tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; - tmp[28] = product[32]; tmp[29] = product[33]; tmp[30] = product[34]; tmp[31] = product[35]; - carry += vli_add(result, result, tmp); - - /* d1 */ - tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; - tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; - tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; - tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; - tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; - tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; - tmp[24] = product[32]; tmp[25] = product[33]; tmp[26] = product[34]; tmp[27] = product[35]; - tmp[28] = product[40]; tmp[29] = product[41]; tmp[30] = product[42]; tmp[31] = product[43]; - carry -= vli_sub(result, result, tmp); - - /* d2 */ - tmp[0] = product[48]; tmp[1] = product[49]; tmp[2] = product[50]; tmp[3] = product[51]; - tmp[4] = product[52]; tmp[5] = product[53]; tmp[6] = product[54]; tmp[7] = product[55]; - tmp[8] = product[56]; tmp[9] = product[57]; tmp[10] = product[58]; tmp[11] = product[59]; - tmp[12] = product[60]; tmp[13] = product[61]; tmp[14] = product[62]; tmp[15] = product[63]; - tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; - tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; - tmp[24] = product[36]; tmp[25] = product[37]; tmp[26] = product[38]; tmp[27] = product[39]; - tmp[28] = product[44]; tmp[29] = product[45]; tmp[30] = product[46]; tmp[31] = product[47]; - carry -= vli_sub(result, result, tmp); - - /* d3 */ - tmp[0] = product[52]; tmp[1] = product[53]; tmp[2] = product[54]; tmp[3] = product[55]; - tmp[4] = product[56]; tmp[5] = product[57]; tmp[6] = product[58]; tmp[7] = product[59]; - tmp[8] = product[60]; tmp[9] = product[61]; tmp[10] = product[62]; tmp[11] = product[63]; - tmp[12] = product[32]; tmp[13] = product[33]; tmp[14] = product[34]; tmp[15] = product[35]; - tmp[16] = product[36]; tmp[17] = product[37]; tmp[18] = product[38]; tmp[19] = product[39]; - tmp[20] = product[40]; tmp[21] = product[41]; tmp[22] = product[42]; tmp[23] = product[43]; - tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; - tmp[28] = product[48]; tmp[29] = product[49]; tmp[30] = product[50]; tmp[31] = product[51]; - carry -= vli_sub(result, result, tmp); - - /* d4 */ - tmp[0] = product[56]; tmp[1] = product[57]; tmp[2] = product[58]; tmp[3] = product[59]; - tmp[4] = product[60]; tmp[5] = product[61]; tmp[6] = product[62]; tmp[7] = product[63]; - tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; - tmp[12] = product[36]; tmp[13] = product[37]; tmp[14] = product[38]; tmp[15] = product[39]; - tmp[16] = product[40]; tmp[17] = product[41]; tmp[18] = product[42]; tmp[19] = product[43]; - tmp[20] = product[44]; tmp[21] = product[45]; tmp[22] = product[46]; tmp[23] = product[47]; - tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; - tmp[28] = product[52]; tmp[29] = product[53]; tmp[30] = product[54]; tmp[31] = product[55]; - carry -= vli_sub(result, result, tmp); - - if (carry < 0) { - do { - carry += vli_add(result, result, curve_p); - } while (carry < 0); - } else { - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); +/* Computes result = product % mod, where product is 2N words long. */ +/* Currently only designed to work for curve_p or curve_n. */ +static void vli_mmod(uECC_word_t *result, + uECC_word_t *product, + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t mod_multiple[2 * uECC_MAX_WORDS]; + uECC_word_t tmp[2 * uECC_MAX_WORDS]; + uECC_word_t *v[2] = {tmp, product}; + uECC_word_t index; + + // Shift mod so its highest set bit is at the maximum position. + bitcount_t shift = (num_words * 2 * uECC_WORD_BITS) - vli_numBits(mod, num_words); + wordcount_t word_shift = shift / uECC_WORD_BITS; + wordcount_t bit_shift = shift % uECC_WORD_BITS; + uECC_word_t carry = 0; + vli_clear(mod_multiple, word_shift); + if (bit_shift > 0) { + for(index = 0; index < num_words; ++index) { + mod_multiple[word_shift + index] = (mod[index] << bit_shift) | carry; + carry = mod[index] >> (uECC_WORD_BITS - bit_shift); } - } -} -#elif uECC_WORD_SIZE == 4 -static void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) { - uint32_t tmp[uECC_WORDS]; - int carry; - - /* t */ - vli_set(result, product); - - /* s1 */ - tmp[0] = tmp[1] = tmp[2] = 0; - tmp[3] = product[11]; - tmp[4] = product[12]; - tmp[5] = product[13]; - tmp[6] = product[14]; - tmp[7] = product[15]; - carry = vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s2 */ - tmp[3] = product[12]; - tmp[4] = product[13]; - tmp[5] = product[14]; - tmp[6] = product[15]; - tmp[7] = 0; - carry += vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s3 */ - tmp[0] = product[8]; - tmp[1] = product[9]; - tmp[2] = product[10]; - tmp[3] = tmp[4] = tmp[5] = 0; - tmp[6] = product[14]; - tmp[7] = product[15]; - carry += vli_add(result, result, tmp); - - /* s4 */ - tmp[0] = product[9]; - tmp[1] = product[10]; - tmp[2] = product[11]; - tmp[3] = product[13]; - tmp[4] = product[14]; - tmp[5] = product[15]; - tmp[6] = product[13]; - tmp[7] = product[8]; - carry += vli_add(result, result, tmp); - - /* d1 */ - tmp[0] = product[11]; - tmp[1] = product[12]; - tmp[2] = product[13]; - tmp[3] = tmp[4] = tmp[5] = 0; - tmp[6] = product[8]; - tmp[7] = product[10]; - carry -= vli_sub(result, result, tmp); - - /* d2 */ - tmp[0] = product[12]; - tmp[1] = product[13]; - tmp[2] = product[14]; - tmp[3] = product[15]; - tmp[4] = tmp[5] = 0; - tmp[6] = product[9]; - tmp[7] = product[11]; - carry -= vli_sub(result, result, tmp); - - /* d3 */ - tmp[0] = product[13]; - tmp[1] = product[14]; - tmp[2] = product[15]; - tmp[3] = product[8]; - tmp[4] = product[9]; - tmp[5] = product[10]; - tmp[6] = 0; - tmp[7] = product[12]; - carry -= vli_sub(result, result, tmp); - - /* d4 */ - tmp[0] = product[14]; - tmp[1] = product[15]; - tmp[2] = 0; - tmp[3] = product[9]; - tmp[4] = product[10]; - tmp[5] = product[11]; - tmp[6] = 0; - tmp[7] = product[13]; - carry -= vli_sub(result, result, tmp); - - if (carry < 0) { - do { - carry += vli_add(result, result, curve_p); - } while (carry < 0); } else { - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } + vli_set(mod_multiple + word_shift, mod, num_words); } -} -#else -static void vli_mmod_fast(uint64_t *RESTRICT result, uint64_t *RESTRICT product) { - uint64_t tmp[uECC_WORDS]; - int carry; - /* t */ - vli_set(result, product); - - /* s1 */ - tmp[0] = 0; - tmp[1] = product[5] & 0xffffffff00000000ull; - tmp[2] = product[6]; - tmp[3] = product[7]; - carry = vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s2 */ - tmp[1] = product[6] << 32; - tmp[2] = (product[6] >> 32) | (product[7] << 32); - tmp[3] = product[7] >> 32; - carry += vli_add(tmp, tmp, tmp); - carry += vli_add(result, result, tmp); - - /* s3 */ - tmp[0] = product[4]; - tmp[1] = product[5] & 0xffffffff; - tmp[2] = 0; - tmp[3] = product[7]; - carry += vli_add(result, result, tmp); - - /* s4 */ - tmp[0] = (product[4] >> 32) | (product[5] << 32); - tmp[1] = (product[5] >> 32) | (product[6] & 0xffffffff00000000ull); - tmp[2] = product[7]; - tmp[3] = (product[6] >> 32) | (product[4] << 32); - carry += vli_add(result, result, tmp); - - /* d1 */ - tmp[0] = (product[5] >> 32) | (product[6] << 32); - tmp[1] = (product[6] >> 32); - tmp[2] = 0; - tmp[3] = (product[4] & 0xffffffff) | (product[5] << 32); - carry -= vli_sub(result, result, tmp); - - /* d2 */ - tmp[0] = product[6]; - tmp[1] = product[7]; - tmp[2] = 0; - tmp[3] = (product[4] >> 32) | (product[5] & 0xffffffff00000000ull); - carry -= vli_sub(result, result, tmp); - - /* d3 */ - tmp[0] = (product[6] >> 32) | (product[7] << 32); - tmp[1] = (product[7] >> 32) | (product[4] << 32); - tmp[2] = (product[4] >> 32) | (product[5] << 32); - tmp[3] = (product[6] << 32); - carry -= vli_sub(result, result, tmp); - - /* d4 */ - tmp[0] = product[7]; - tmp[1] = product[4] & 0xffffffff00000000ull; - tmp[2] = product[5]; - tmp[3] = product[6] & 0xffffffff00000000ull; - carry -= vli_sub(result, result, tmp); - - if (carry < 0) { - do { - carry += vli_add(result, result, curve_p); - } while (carry < 0); - } else { - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } + for (index = 1; shift >= 0; --shift) { + uECC_word_t borrow = vli_sub(v[1 - index], v[index], mod_multiple, num_words * 2); + index = !(index ^ borrow); /* Swap the index if there was no borrow */ + vli_rshift1(mod_multiple, num_words * 2); } + vli_set(result, v[index], num_words); } -#endif /* uECC_WORD_SIZE */ -#elif uECC_CURVE == uECC_secp256k1 - -#if uECC_WORD_SIZE == 1 -static void omega_mult(uint8_t * RESTRICT result, const uint8_t * RESTRICT right) { - /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ - uECC_word_t r0 = 0; - uECC_word_t r1 = 0; - uECC_word_t r2 = 0; - wordcount_t k; - - /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ - muladd(0xD1, right[0], &r0, &r1, &r2); - result[0] = r0; - r0 = r1; - r1 = r2; - /* r2 is still 0 */ - - for (k = 1; k < uECC_WORDS; ++k) { - muladd(0x03, right[k - 1], &r0, &r1, &r2); - muladd(0xD1, right[k], &r0, &r1, &r2); - result[k] = r0; - r0 = r1; - r1 = r2; - r2 = 0; - } - muladd(0x03, right[uECC_WORDS - 1], &r0, &r1, &r2); - result[uECC_WORDS] = r0; - result[uECC_WORDS + 1] = r1; - - result[4 + uECC_WORDS] = vli_add(result + 4, result + 4, right); /* add the 2^32 multiple */ -} -#elif uECC_WORD_SIZE == 4 -static void omega_mult(uint32_t * RESTRICT result, const uint32_t * RESTRICT right) { - /* Multiply by (2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ - uint32_t carry = 0; - wordcount_t k; - - for (k = 0; k < uECC_WORDS; ++k) { - uint64_t p = (uint64_t)0x3D1 * right[k] + carry; - result[k] = (p & 0xffffffff); - carry = p >> 32; - } - result[uECC_WORDS] = carry; - - result[1 + uECC_WORDS] = vli_add(result + 1, result + 1, right); /* add the 2^32 multiple */ -} -#else -static void omega_mult(uint64_t * RESTRICT result, const uint64_t * RESTRICT right) { - uECC_word_t r0 = 0; - uECC_word_t r1 = 0; - uECC_word_t r2 = 0; - wordcount_t k; - - /* Multiply by (2^32 + 2^9 + 2^8 + 2^7 + 2^6 + 2^4 + 1). */ - for (k = 0; k < uECC_WORDS; ++k) { - muladd(0x1000003D1ull, right[k], &r0, &r1, &r2); - result[k] = r0; - r0 = r1; - r1 = r2; - r2 = 0; - } - result[uECC_WORDS] = r0; -} -#endif /* uECC_WORD_SIZE */ - -#elif uECC_CURVE == uECC_secp224r1 - -/* Computes result = product % curve_p - from http://www.nsa.gov/ia/_files/nist-routines.pdf */ -#if uECC_WORD_SIZE == 1 -// TODO it may be faster to use the omega_mult method when fully asm optimized. -void vli_mmod_fast(uint8_t *RESTRICT result, uint8_t *RESTRICT product) { - uint8_t tmp[uECC_WORDS]; - int8_t carry; - - /* t */ - vli_set(result, product); - - /* s1 */ - tmp[0] = tmp[1] = tmp[2] = tmp[3] = 0; - tmp[4] = tmp[5] = tmp[6] = tmp[7] = 0; - tmp[8] = tmp[9] = tmp[10] = tmp[11] = 0; - tmp[12] = product[28]; tmp[13] = product[29]; tmp[14] = product[30]; tmp[15] = product[31]; - tmp[16] = product[32]; tmp[17] = product[33]; tmp[18] = product[34]; tmp[19] = product[35]; - tmp[20] = product[36]; tmp[21] = product[37]; tmp[22] = product[38]; tmp[23] = product[39]; - tmp[24] = product[40]; tmp[25] = product[41]; tmp[26] = product[42]; tmp[27] = product[43]; - carry = vli_add(result, result, tmp); - - /* s2 */ - tmp[12] = product[44]; tmp[13] = product[45]; tmp[14] = product[46]; tmp[15] = product[47]; - tmp[16] = product[48]; tmp[17] = product[49]; tmp[18] = product[50]; tmp[19] = product[51]; - tmp[20] = product[52]; tmp[21] = product[53]; tmp[22] = product[54]; tmp[23] = product[55]; - tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; - carry += vli_add(result, result, tmp); - - /* d1 */ - tmp[0] = product[28]; tmp[1] = product[29]; tmp[2] = product[30]; tmp[3] = product[31]; - tmp[4] = product[32]; tmp[5] = product[33]; tmp[6] = product[34]; tmp[7] = product[35]; - tmp[8] = product[36]; tmp[9] = product[37]; tmp[10] = product[38]; tmp[11] = product[39]; - tmp[12] = product[40]; tmp[13] = product[41]; tmp[14] = product[42]; tmp[15] = product[43]; - tmp[16] = product[44]; tmp[17] = product[45]; tmp[18] = product[46]; tmp[19] = product[47]; - tmp[20] = product[48]; tmp[21] = product[49]; tmp[22] = product[50]; tmp[23] = product[51]; - tmp[24] = product[52]; tmp[25] = product[53]; tmp[26] = product[54]; tmp[27] = product[55]; - carry -= vli_sub(result, result, tmp); - - /* d2 */ - tmp[0] = product[44]; tmp[1] = product[45]; tmp[2] = product[46]; tmp[3] = product[47]; - tmp[4] = product[48]; tmp[5] = product[49]; tmp[6] = product[50]; tmp[7] = product[51]; - tmp[8] = product[52]; tmp[9] = product[53]; tmp[10] = product[54]; tmp[11] = product[55]; - tmp[12] = tmp[13] = tmp[14] = tmp[15] = 0; - tmp[16] = tmp[17] = tmp[18] = tmp[19] = 0; - tmp[20] = tmp[21] = tmp[22] = tmp[23] = 0; - tmp[24] = tmp[25] = tmp[26] = tmp[27] = 0; - carry -= vli_sub(result, result, tmp); - - if (carry < 0) { - do { - carry += vli_add(result, result, curve_p); - } while (carry < 0); - } else { - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } - } -} -#elif uECC_WORD_SIZE == 4 -void vli_mmod_fast(uint32_t *RESTRICT result, uint32_t *RESTRICT product) -{ - uint32_t tmp[uECC_WORDS]; - int carry; - - /* t */ - vli_set(result, product); - - /* s1 */ - tmp[0] = tmp[1] = tmp[2] = 0; - tmp[3] = product[7]; - tmp[4] = product[8]; - tmp[5] = product[9]; - tmp[6] = product[10]; - carry = vli_add(result, result, tmp); - - /* s2 */ - tmp[3] = product[11]; - tmp[4] = product[12]; - tmp[5] = product[13]; - tmp[6] = 0; - carry += vli_add(result, result, tmp); - - /* d1 */ - tmp[0] = product[7]; - tmp[1] = product[8]; - tmp[2] = product[9]; - tmp[3] = product[10]; - tmp[4] = product[11]; - tmp[5] = product[12]; - tmp[6] = product[13]; - carry -= vli_sub(result, result, tmp); - - /* d2 */ - tmp[0] = product[11]; - tmp[1] = product[12]; - tmp[2] = product[13]; - tmp[3] = tmp[4] = tmp[5] = tmp[6] = 0; - carry -= vli_sub(result, result, tmp); - - if (carry < 0) { - do { - carry += vli_add(result, result, curve_p); - } while (carry < 0); - } else { - while (carry || vli_cmp(curve_p, result) != 1) { - carry -= vli_sub(result, result, curve_p); - } - } -} -#endif /* uECC_WORD_SIZE */ - -#endif /* uECC_CURVE */ -#endif /* !asm_mmod_fast */ - -/* Computes result = (left * right) % curve_p. */ -static void vli_modMult_fast(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right) { - uECC_word_t product[2 * uECC_WORDS]; - vli_mult(product, left, right); - vli_mmod_fast(result, product); +/* Computes result = (left * right) % mod. */ +static void vli_modMult(uECC_word_t *result, + const uECC_word_t *left, + const uECC_word_t *right, + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t product[2 * uECC_MAX_WORDS]; + vli_mult(product, left, right, num_words); + vli_mmod(result, product, mod, num_words); } #if uECC_SQUARE_FUNC /* Computes result = left^2 % curve_p. */ -static void vli_modSquare_fast(uECC_word_t *result, const uECC_word_t *left) { - uECC_word_t product[2 * uECC_WORDS]; - vli_square(product, left); - vli_mmod_fast(result, product); +static void vli_modSquare(uECC_word_t *result, + const uECC_word_t *left, + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t product[2 * uECC_MAX_WORDS]; + vli_square(product, left, num_words); + vli_mmod(result, product, mod, num_words); } #else /* uECC_SQUARE_FUNC */ -#define vli_modSquare_fast(result, left) vli_modMult_fast((result), (left), (left)) - +#define vli_modSquare(result, left, mod, num_words) \ + vli_modMult((result), (left), (left), (mod), (num_words)) + #endif /* uECC_SQUARE_FUNC */ - #define EVEN(vli) (!(vli[0] & 1)) /* Computes result = (1 / input) % mod. All VLIs are the same size. - See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" - https://labs.oracle.com/techrep/2001/smli_tr-2001-95.pdf */ + See "From Euclid's GCD to Montgomery Multiplication to the Great Divide" */ #if !asm_modInv -static void vli_modInv(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { - uECC_word_t a[uECC_WORDS], b[uECC_WORDS], u[uECC_WORDS], v[uECC_WORDS]; - uECC_word_t carry; - cmpresult_t cmpResult; - if (vli_isZero(input)) { - vli_clear(result); +static void vli_modInv_update(uECC_word_t *uv, + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t carry = 0; + if (!EVEN(uv)) { + carry = vli_add(uv, uv, mod, num_words); + } + vli_rshift1(uv, num_words); + if (carry) { + uv[num_words - 1] |= HIGH_BIT_SET; + } +} + +static void vli_modInv(uECC_word_t *result, + const uECC_word_t *input, + const uECC_word_t *mod, + const wordcount_t num_words) { + uECC_word_t a[uECC_MAX_WORDS], b[uECC_MAX_WORDS], u[uECC_MAX_WORDS], v[uECC_MAX_WORDS]; + cmpresult_t cmpResult; + + if (vli_isZero(input, num_words)) { + vli_clear(result, num_words); return; } - vli_set(a, input); - vli_set(b, mod); - vli_clear(u); + vli_set(a, input, num_words); + vli_set(b, mod, num_words); + vli_clear(u, num_words); u[0] = 1; - vli_clear(v); - while ((cmpResult = vli_cmp(a, b)) != 0) { - carry = 0; + vli_clear(v, num_words); + while ((cmpResult = vli_cmp(a, b, num_words)) != 0) { if (EVEN(a)) { - vli_rshift1(a); - if (!EVEN(u)) { - carry = vli_add(u, u, mod); - } - vli_rshift1(u); - if (carry) { - u[uECC_WORDS - 1] |= HIGH_BIT_SET; - } + vli_rshift1(a, num_words); + vli_modInv_update(u, mod, num_words); } else if (EVEN(b)) { - vli_rshift1(b); - if (!EVEN(v)) { - carry = vli_add(v, v, mod); - } - vli_rshift1(v); - if (carry) { - v[uECC_WORDS - 1] |= HIGH_BIT_SET; - } + vli_rshift1(b, num_words); + vli_modInv_update(v, mod, num_words); } else if (cmpResult > 0) { - vli_sub(a, a, b); - vli_rshift1(a); - if (vli_cmp(u, v) < 0) { - vli_add(u, u, mod); - } - vli_sub(u, u, v); - if (!EVEN(u)) { - carry = vli_add(u, u, mod); - } - vli_rshift1(u); - if (carry) { - u[uECC_WORDS - 1] |= HIGH_BIT_SET; + vli_sub(a, a, b, num_words); + vli_rshift1(a, num_words); + if (vli_cmp(u, v, num_words) < 0) { + vli_add(u, u, mod, num_words); } + vli_sub(u, u, v, num_words); + vli_modInv_update(u, mod, num_words); } else { - vli_sub(b, b, a); - vli_rshift1(b); - if (vli_cmp(v, u) < 0) { - vli_add(v, v, mod); - } - vli_sub(v, v, u); - if (!EVEN(v)) { - carry = vli_add(v, v, mod); - } - vli_rshift1(v); - if (carry) { - v[uECC_WORDS - 1] |= HIGH_BIT_SET; + vli_sub(b, b, a, num_words); + vli_rshift1(b, num_words); + if (vli_cmp(v, u, num_words) < 0) { + vli_add(v, v, mod, num_words); } + vli_sub(v, v, u, num_words); + vli_modInv_update(v, mod, num_words); } } - vli_set(result, u); + vli_set(result, u, num_words); } #endif /* !asm_modInv */ /* ------ Point operations ------ */ /* Returns 1 if 'point' is the point at infinity, 0 otherwise. */ -static cmpresult_t EccPoint_isZero(const EccPoint *point) { - return (vli_isZero(point->x) && vli_isZero(point->y)); +static cmpresult_t EccPoint_isZero(const uECC_word_t *point, uECC_Curve curve) { + return vli_isZero(point, curve->num_words * 2); } /* Point multiplication algorithm using Montgomery's ladder with co-Z coordinates. @@ -1550,270 +495,301 @@ From http://eprint.iacr.org/2011/338.pdf */ /* Double in place */ -#if (uECC_CURVE == uECC_secp256k1) -static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - uECC_word_t * RESTRICT Z1) { +static void double_jacobian_secp256k1(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * Z1, + uECC_Curve curve) { /* t1 = X, t2 = Y, t3 = Z */ - uECC_word_t t4[uECC_WORDS]; - uECC_word_t t5[uECC_WORDS]; - - if (vli_isZero(Z1)) { + uECC_word_t t4[uECC_MAX_WORDS]; + uECC_word_t t5[uECC_MAX_WORDS]; + + if (vli_isZero(Z1, curve->num_words)) { return; } - - vli_modSquare_fast(t5, Y1); /* t5 = y1^2 */ - vli_modMult_fast(t4, X1, t5); /* t4 = x1*y1^2 = A */ - vli_modSquare_fast(X1, X1); /* t1 = x1^2 */ - vli_modSquare_fast(t5, t5); /* t5 = y1^4 */ - vli_modMult_fast(Z1, Y1, Z1); /* t3 = y1*z1 = z3 */ - - vli_modAdd(Y1, X1, X1, curve_p); /* t2 = 2*x1^2 */ - vli_modAdd(Y1, Y1, X1, curve_p); /* t2 = 3*x1^2 */ + + vli_modSquare(t5, Y1, curve->p, curve->num_words); /* t5 = y1^2 */ + vli_modMult(t4, X1, t5, curve->p, curve->num_words); /* t4 = x1*y1^2 = A */ + vli_modSquare(X1, X1, curve->p, curve->num_words); /* t1 = x1^2 */ + vli_modSquare(t5, t5, curve->p, curve->num_words); /* t5 = y1^4 */ + vli_modMult(Z1, Y1, Z1, curve->p, curve->num_words); /* t3 = y1*z1 = z3 */ + + vli_modAdd(Y1, X1, X1, curve->p, curve->num_words); /* t2 = 2*x1^2 */ + vli_modAdd(Y1, Y1, X1, curve->p, curve->num_words); /* t2 = 3*x1^2 */ if (vli_testBit(Y1, 0)) { - uECC_word_t carry = vli_add(Y1, Y1, curve_p); - vli_rshift1(Y1); - Y1[uECC_WORDS - 1] |= carry << (uECC_WORD_BITS - 1); + uECC_word_t carry = vli_add(Y1, Y1, curve->p, curve->num_words); + vli_rshift1(Y1, curve->num_words); + Y1[curve->num_words - 1] |= carry << (uECC_WORD_BITS - 1); } else { - vli_rshift1(Y1); + vli_rshift1(Y1, curve->num_words); } /* t2 = 3/2*(x1^2) = B */ - - vli_modSquare_fast(X1, Y1); /* t1 = B^2 */ - vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - A */ - vli_modSub(X1, X1, t4, curve_p); /* t1 = B^2 - 2A = x3 */ - - vli_modSub(t4, t4, X1, curve_p); /* t4 = A - x3 */ - vli_modMult_fast(Y1, Y1, t4); /* t2 = B * (A - x3) */ - vli_modSub(Y1, Y1, t5, curve_p); /* t2 = B * (A - x3) - y1^4 = y3 */ + + vli_modSquare(X1, Y1, curve->p, curve->num_words); /* t1 = B^2 */ + vli_modSub(X1, X1, t4, curve->p, curve->num_words); /* t1 = B^2 - A */ + vli_modSub(X1, X1, t4, curve->p, curve->num_words); /* t1 = B^2 - 2A = x3 */ + + vli_modSub(t4, t4, X1, curve->p, curve->num_words); /* t4 = A - x3 */ + vli_modMult(Y1, Y1, t4, curve->p, curve->num_words); /* t2 = B * (A - x3) */ + vli_modSub(Y1, Y1, t5, curve->p, curve->num_words); /* t2 = B * (A - x3) - y1^4 = y3 */ } -#else -static void EccPoint_double_jacobian(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - uECC_word_t * RESTRICT Z1) { - /* t1 = X, t2 = Y, t3 = Z */ - uECC_word_t t4[uECC_WORDS]; - uECC_word_t t5[uECC_WORDS]; - if (vli_isZero(Z1)) { +static void double_jacobian_default(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * Z1, + uECC_Curve curve) { + /* t1 = X, t2 = Y, t3 = Z */ + uECC_word_t t4[uECC_MAX_WORDS]; + uECC_word_t t5[uECC_MAX_WORDS]; + + if (vli_isZero(Z1, curve->num_words)) { return; } - - vli_modSquare_fast(t4, Y1); /* t4 = y1^2 */ - vli_modMult_fast(t5, X1, t4); /* t5 = x1*y1^2 = A */ - vli_modSquare_fast(t4, t4); /* t4 = y1^4 */ - vli_modMult_fast(Y1, Y1, Z1); /* t2 = y1*z1 = z3 */ - vli_modSquare_fast(Z1, Z1); /* t3 = z1^2 */ - - vli_modAdd(X1, X1, Z1, curve_p); /* t1 = x1 + z1^2 */ - vli_modAdd(Z1, Z1, Z1, curve_p); /* t3 = 2*z1^2 */ - vli_modSub_fast(Z1, X1, Z1); /* t3 = x1 - z1^2 */ - vli_modMult_fast(X1, X1, Z1); /* t1 = x1^2 - z1^4 */ - - vli_modAdd(Z1, X1, X1, curve_p); /* t3 = 2*(x1^2 - z1^4) */ - vli_modAdd(X1, X1, Z1, curve_p); /* t1 = 3*(x1^2 - z1^4) */ + + vli_modSquare(t4, Y1, curve->p, curve->num_words); /* t4 = y1^2 */ + vli_modMult(t5, X1, t4, curve->p, curve->num_words); /* t5 = x1*y1^2 = A */ + vli_modSquare(t4, t4, curve->p, curve->num_words); /* t4 = y1^4 */ + vli_modMult(Y1, Y1, Z1, curve->p, curve->num_words); /* t2 = y1*z1 = z3 */ + vli_modSquare(Z1, Z1, curve->p, curve->num_words); /* t3 = z1^2 */ + + vli_modAdd(X1, X1, Z1, curve->p, curve->num_words); /* t1 = x1 + z1^2 */ + vli_modAdd(Z1, Z1, Z1, curve->p, curve->num_words); /* t3 = 2*z1^2 */ + vli_modSub(Z1, X1, Z1, curve->p, curve->num_words); /* t3 = x1 - z1^2 */ + vli_modMult(X1, X1, Z1, curve->p, curve->num_words); /* t1 = x1^2 - z1^4 */ + + vli_modAdd(Z1, X1, X1, curve->p, curve->num_words); /* t3 = 2*(x1^2 - z1^4) */ + vli_modAdd(X1, X1, Z1, curve->p, curve->num_words); /* t1 = 3*(x1^2 - z1^4) */ if (vli_testBit(X1, 0)) { - uECC_word_t l_carry = vli_add(X1, X1, curve_p); - vli_rshift1(X1); - X1[uECC_WORDS - 1] |= l_carry << (uECC_WORD_BITS - 1); + uECC_word_t l_carry = vli_add(X1, X1, curve->p, curve->num_words); + vli_rshift1(X1, curve->num_words); + X1[curve->num_words - 1] |= l_carry << (uECC_WORD_BITS - 1); } else { - vli_rshift1(X1); + vli_rshift1(X1, curve->num_words); } /* t1 = 3/2*(x1^2 - z1^4) = B */ - - vli_modSquare_fast(Z1, X1); /* t3 = B^2 */ - vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - A */ - vli_modSub_fast(Z1, Z1, t5); /* t3 = B^2 - 2A = x3 */ - vli_modSub_fast(t5, t5, Z1); /* t5 = A - x3 */ - vli_modMult_fast(X1, X1, t5); /* t1 = B * (A - x3) */ - vli_modSub_fast(t4, X1, t4); /* t4 = B * (A - x3) - y1^4 = y3 */ - - vli_set(X1, Z1); - vli_set(Z1, Y1); - vli_set(Y1, t4); + + vli_modSquare(Z1, X1, curve->p, curve->num_words); /* t3 = B^2 */ + vli_modSub(Z1, Z1, t5, curve->p, curve->num_words); /* t3 = B^2 - A */ + vli_modSub(Z1, Z1, t5, curve->p, curve->num_words); /* t3 = B^2 - 2A = x3 */ + vli_modSub(t5, t5, Z1, curve->p, curve->num_words); /* t5 = A - x3 */ + vli_modMult(X1, X1, t5, curve->p, curve->num_words); /* t1 = B * (A - x3) */ + vli_modSub(t4, X1, t4, curve->p, curve->num_words); /* t4 = B * (A - x3) - y1^4 = y3 */ + + vli_set(X1, Z1, curve->num_words); + vli_set(Z1, Y1, curve->num_words); + vli_set(Y1, t4, curve->num_words); } -#endif /* Modify (x1, y1) => (x1 * z^2, y1 * z^3) */ -static void apply_z(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - const uECC_word_t * RESTRICT Z) { - uECC_word_t t1[uECC_WORDS]; +static void apply_z(uECC_word_t * X1, + uECC_word_t * Y1, + const uECC_word_t * const Z, + uECC_Curve curve) { + uECC_word_t t1[uECC_MAX_WORDS]; - vli_modSquare_fast(t1, Z); /* z^2 */ - vli_modMult_fast(X1, X1, t1); /* x1 * z^2 */ - vli_modMult_fast(t1, t1, Z); /* z^3 */ - vli_modMult_fast(Y1, Y1, t1); /* y1 * z^3 */ + vli_modSquare(t1, Z, curve->p, curve->num_words); /* z^2 */ + vli_modMult(X1, X1, t1, curve->p, curve->num_words); /* x1 * z^2 */ + vli_modMult(t1, t1, Z, curve->p, curve->num_words); /* z^3 */ + vli_modMult(Y1, Y1, t1, curve->p, curve->num_words); /* y1 * z^3 */ } /* P = (x1, y1) => 2P, (x2, y2) => P' */ -static void XYcZ_initial_double(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - uECC_word_t * RESTRICT X2, - uECC_word_t * RESTRICT Y2, - const uECC_word_t * RESTRICT initial_Z) { - uECC_word_t z[uECC_WORDS]; +static void XYcZ_initial_double(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * X2, + uECC_word_t * Y2, + const uECC_word_t * const initial_Z, + uECC_Curve curve) { + uECC_word_t z[uECC_MAX_WORDS]; if (initial_Z) { - vli_set(z, initial_Z); + vli_set(z, initial_Z, curve->num_words); } else { - vli_clear(z); + vli_clear(z, curve->num_words); z[0] = 1; } - vli_set(X2, X1); - vli_set(Y2, Y1); + vli_set(X2, X1, curve->num_words); + vli_set(Y2, Y1, curve->num_words); - apply_z(X1, Y1, z); - EccPoint_double_jacobian(X1, Y1, z); - apply_z(X2, Y2, z); + apply_z(X1, Y1, z, curve); + curve->double_jacobian(X1, Y1, z, curve); + apply_z(X2, Y2, z, curve); } /* Input P = (x1, y1, Z), Q = (x2, y2, Z) Output P' = (x1', y1', Z3), P + Q = (x3, y3, Z3) or P => P', Q => P + Q */ -static void XYcZ_add(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - uECC_word_t * RESTRICT X2, - uECC_word_t * RESTRICT Y2) { +static void XYcZ_add(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * X2, + uECC_word_t * Y2, + uECC_Curve curve) { /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ - uECC_word_t t5[uECC_WORDS]; - - vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ - vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ - vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ - vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ - vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ - vli_modSquare_fast(t5, Y2); /* t5 = (y2 - y1)^2 = D */ - - vli_modSub_fast(t5, t5, X1); /* t5 = D - B */ - vli_modSub_fast(t5, t5, X2); /* t5 = D - B - C = x3 */ - vli_modSub_fast(X2, X2, X1); /* t3 = C - B */ - vli_modMult_fast(Y1, Y1, X2); /* t2 = y1*(C - B) */ - vli_modSub_fast(X2, X1, t5); /* t3 = B - x3 */ - vli_modMult_fast(Y2, Y2, X2); /* t4 = (y2 - y1)*(B - x3) */ - vli_modSub_fast(Y2, Y2, Y1); /* t4 = y3 */ - - vli_set(X2, t5); + uECC_word_t t5[uECC_MAX_WORDS]; + + vli_modSub(t5, X2, X1, curve->p, curve->num_words); /* t5 = x2 - x1 */ + vli_modSquare(t5, t5, curve->p, curve->num_words); /* t5 = (x2 - x1)^2 = A */ + vli_modMult(X1, X1, t5, curve->p, curve->num_words); /* t1 = x1*A = B */ + vli_modMult(X2, X2, t5, curve->p, curve->num_words); /* t3 = x2*A = C */ + vli_modSub(Y2, Y2, Y1, curve->p, curve->num_words); /* t4 = y2 - y1 */ + vli_modSquare(t5, Y2, curve->p, curve->num_words); /* t5 = (y2 - y1)^2 = D */ + + vli_modSub(t5, t5, X1, curve->p, curve->num_words); /* t5 = D - B */ + vli_modSub(t5, t5, X2, curve->p, curve->num_words); /* t5 = D - B - C = x3 */ + vli_modSub(X2, X2, X1, curve->p, curve->num_words); /* t3 = C - B */ + vli_modMult(Y1, Y1, X2, curve->p, curve->num_words); /* t2 = y1*(C - B) */ + vli_modSub(X2, X1, t5, curve->p, curve->num_words); /* t3 = B - x3 */ + vli_modMult(Y2, Y2, X2, curve->p, curve->num_words); /* t4 = (y2 - y1)*(B - x3) */ + vli_modSub(Y2, Y2, Y1, curve->p, curve->num_words); /* t4 = y3 */ + + vli_set(X2, t5, curve->num_words); } /* Input P = (x1, y1, Z), Q = (x2, y2, Z) Output P + Q = (x3, y3, Z3), P - Q = (x3', y3', Z3) or P => P - Q, Q => P + Q */ -static void XYcZ_addC(uECC_word_t * RESTRICT X1, - uECC_word_t * RESTRICT Y1, - uECC_word_t * RESTRICT X2, - uECC_word_t * RESTRICT Y2) { +static void XYcZ_addC(uECC_word_t * X1, + uECC_word_t * Y1, + uECC_word_t * X2, + uECC_word_t * Y2, + uECC_Curve curve) { /* t1 = X1, t2 = Y1, t3 = X2, t4 = Y2 */ - uECC_word_t t5[uECC_WORDS]; - uECC_word_t t6[uECC_WORDS]; - uECC_word_t t7[uECC_WORDS]; + uECC_word_t t5[uECC_MAX_WORDS]; + uECC_word_t t6[uECC_MAX_WORDS]; + uECC_word_t t7[uECC_MAX_WORDS]; + + vli_modSub(t5, X2, X1, curve->p, curve->num_words); /* t5 = x2 - x1 */ + vli_modSquare(t5, t5, curve->p, curve->num_words); /* t5 = (x2 - x1)^2 = A */ + vli_modMult(X1, X1, t5, curve->p, curve->num_words); /* t1 = x1*A = B */ + vli_modMult(X2, X2, t5, curve->p, curve->num_words); /* t3 = x2*A = C */ + vli_modAdd(t5, Y2, Y1, curve->p, curve->num_words); /* t5 = y2 + y1 */ + vli_modSub(Y2, Y2, Y1, curve->p, curve->num_words); /* t4 = y2 - y1 */ - vli_modSub_fast(t5, X2, X1); /* t5 = x2 - x1 */ - vli_modSquare_fast(t5, t5); /* t5 = (x2 - x1)^2 = A */ - vli_modMult_fast(X1, X1, t5); /* t1 = x1*A = B */ - vli_modMult_fast(X2, X2, t5); /* t3 = x2*A = C */ - vli_modAdd(t5, Y2, Y1, curve_p); /* t5 = y2 + y1 */ - vli_modSub_fast(Y2, Y2, Y1); /* t4 = y2 - y1 */ - - vli_modSub_fast(t6, X2, X1); /* t6 = C - B */ - vli_modMult_fast(Y1, Y1, t6); /* t2 = y1 * (C - B) = E */ - vli_modAdd(t6, X1, X2, curve_p); /* t6 = B + C */ - vli_modSquare_fast(X2, Y2); /* t3 = (y2 - y1)^2 = D */ - vli_modSub_fast(X2, X2, t6); /* t3 = D - (B + C) = x3 */ - - vli_modSub_fast(t7, X1, X2); /* t7 = B - x3 */ - vli_modMult_fast(Y2, Y2, t7); /* t4 = (y2 - y1)*(B - x3) */ - vli_modSub_fast(Y2, Y2, Y1); /* t4 = (y2 - y1)*(B - x3) - E = y3 */ - - vli_modSquare_fast(t7, t5); /* t7 = (y2 + y1)^2 = F */ - vli_modSub_fast(t7, t7, t6); /* t7 = F - (B + C) = x3' */ - vli_modSub_fast(t6, t7, X1); /* t6 = x3' - B */ - vli_modMult_fast(t6, t6, t5); /* t6 = (y2 + y1)*(x3' - B) */ - vli_modSub_fast(Y1, t6, Y1); /* t2 = (y2 + y1)*(x3' - B) - E = y3' */ - - vli_set(X1, t7); + vli_modSub(t6, X2, X1, curve->p, curve->num_words); /* t6 = C - B */ + vli_modMult(Y1, Y1, t6, curve->p, curve->num_words); /* t2 = y1 * (C - B) = E */ + vli_modAdd(t6, X1, X2, curve->p, curve->num_words); /* t6 = B + C */ + vli_modSquare(X2, Y2, curve->p, curve->num_words); /* t3 = (y2 - y1)^2 = D */ + vli_modSub(X2, X2, t6, curve->p, curve->num_words); /* t3 = D - (B + C) = x3 */ + + vli_modSub(t7, X1, X2, curve->p, curve->num_words); /* t7 = B - x3 */ + vli_modMult(Y2, Y2, t7, curve->p, curve->num_words); /* t4 = (y2 - y1)*(B - x3) */ + vli_modSub(Y2, Y2, Y1, curve->p, curve->num_words); /* t4 = (y2 - y1)*(B - x3) - E = y3 */ + + vli_modSquare(t7, t5, curve->p, curve->num_words); /* t7 = (y2 + y1)^2 = F */ + vli_modSub(t7, t7, t6, curve->p, curve->num_words); /* t7 = F - (B + C) = x3' */ + vli_modSub(t6, t7, X1, curve->p, curve->num_words); /* t6 = x3' - B */ + vli_modMult(t6, t6, t5, curve->p, curve->num_words); /* t6 = (y2 + y1)*(x3' - B) */ + vli_modSub(Y1, t6, Y1, curve->p, curve->num_words); /* t2 = (y2 + y1)*(x3' - B) - E = y3' */ + + vli_set(X1, t7, curve->num_words); } -static void EccPoint_mult(EccPoint * RESTRICT result, - const EccPoint * RESTRICT point, - const uECC_word_t * RESTRICT scalar, - const uECC_word_t * RESTRICT initialZ, - bitcount_t numBits) { +/* result may overlap point. */ +static void EccPoint_mult(uECC_word_t * result, + const uECC_word_t * point, + const uECC_word_t * scalar, + const uECC_word_t * initial_Z, + bitcount_t num_bits, + uECC_Curve curve) { /* R0 and R1 */ - uECC_word_t Rx[2][uECC_WORDS]; - uECC_word_t Ry[2][uECC_WORDS]; - uECC_word_t z[uECC_WORDS]; + uECC_word_t Rx[2][uECC_MAX_WORDS]; + uECC_word_t Ry[2][uECC_MAX_WORDS]; + uECC_word_t z[uECC_MAX_WORDS]; bitcount_t i; uECC_word_t nb; + + vli_set(Rx[1], point, curve->num_words); + vli_set(Ry[1], point + curve->num_words, curve->num_words); - vli_set(Rx[1], point->x); - vli_set(Ry[1], point->y); + XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initial_Z, curve); - XYcZ_initial_double(Rx[1], Ry[1], Rx[0], Ry[0], initialZ); - - for (i = numBits - 2; i > 0; --i) { + for (i = num_bits - 2; i > 0; --i) { nb = !vli_testBit(scalar, i); - XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); - XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); + XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve); + XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve); } nb = !vli_testBit(scalar, 0); - XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb]); - + XYcZ_addC(Rx[1 - nb], Ry[1 - nb], Rx[nb], Ry[nb], curve); + /* Find final 1/Z value. */ - vli_modSub_fast(z, Rx[1], Rx[0]); /* X1 - X0 */ - vli_modMult_fast(z, z, Ry[1 - nb]); /* Yb * (X1 - X0) */ - vli_modMult_fast(z, z, point->x); /* xP * Yb * (X1 - X0) */ - vli_modInv(z, z, curve_p); /* 1 / (xP * Yb * (X1 - X0)) */ - vli_modMult_fast(z, z, point->y); /* yP / (xP * Yb * (X1 - X0)) */ - vli_modMult_fast(z, z, Rx[1 - nb]); /* Xb * yP / (xP * Yb * (X1 - X0)) */ + vli_modSub(z, Rx[1], Rx[0], curve->p, curve->num_words); /* X1 - X0 */ + vli_modMult(z, z, Ry[1 - nb], curve->p, curve->num_words); /* Yb * (X1 - X0) */ + vli_modMult(z, z, point, curve->p, curve->num_words); /* xP * Yb * (X1 - X0) */ + vli_modInv(z, z, curve->p, curve->num_words); /* 1 / (xP * Yb * (X1 - X0)) */ + /* yP / (xP * Yb * (X1 - X0)) */ + vli_modMult(z, z, point + curve->num_words, curve->p, curve->num_words); + vli_modMult(z, z, Rx[1 - nb], curve->p, curve->num_words); /* Xb * yP / (xP * Yb * (X1 - X0)) */ /* End 1/Z calculation */ - XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb]); - apply_z(Rx[0], Ry[0], z); - - vli_set(result->x, Rx[0]); - vli_set(result->y, Ry[0]); + XYcZ_add(Rx[nb], Ry[nb], Rx[1 - nb], Ry[1 - nb], curve); + apply_z(Rx[0], Ry[0], z, curve); + + vli_set(result, Rx[0], curve->num_words); + vli_set(result + curve->num_words, Ry[0], curve->num_words); } -static int EccPoint_compute_public_key(EccPoint *result, uECC_word_t *private) { - uECC_word_t tmp1[uECC_WORDS]; - uECC_word_t tmp2[uECC_WORDS]; +static uECC_word_t regularize_k(const uECC_word_t * const k, + uECC_word_t *k0, + uECC_word_t *k1, + uECC_Curve curve) { + bitcount_t num_bits = vli_numBits(curve->n, curve->num_n_words); + uECC_word_t carry = vli_add(k0, k, curve->n, curve->num_n_words) || + (num_bits < ((bitcount_t)curve->num_n_words * uECC_WORD_SIZE * 8) && + vli_testBit(k0, num_bits)); + vli_add(k1, k0, curve->n, curve->num_n_words); + return carry; +} + +static uECC_word_t EccPoint_compute_public_key(uECC_word_t *result, + uECC_word_t *private, + uECC_Curve curve) { + uECC_word_t tmp1[uECC_MAX_WORDS]; + uECC_word_t tmp2[uECC_MAX_WORDS]; uECC_word_t *p2[2] = {tmp1, tmp2}; uECC_word_t carry; /* Make sure the private key is in the range [1, n-1]. */ - if (vli_isZero(private)) { + if (vli_isZero(private, curve->num_words)) { return 0; } -#if (uECC_CURVE == uECC_secp160r1) - // Don't regularize the bitcount for secp160r1, since it would have a larger performance - // impact (about 2% slower on average) and requires the vli_xxx_n functions, leading to - // a significant increase in code size. - - EccPoint_mult(result, &curve_G, private, 0, vli_numBits(private, uECC_WORDS)); -#else - if (vli_cmp(curve_n, private) != 1) { + if (vli_cmp(curve->n, private, curve->num_n_words) != 1) { return 0; } - + // Regularize the bitcount for the private key so that attackers cannot use a side channel // attack to learn the number of leading zeros. - carry = vli_add(tmp1, private, curve_n); - vli_add(tmp2, tmp1, curve_n); - EccPoint_mult(result, &curve_G, p2[!carry], 0, (uECC_BYTES * 8) + 1); -#endif + carry = regularize_k(private, tmp1, tmp2, curve); - if (EccPoint_isZero(result)) { + EccPoint_mult(result, curve->G, p2[!carry], 0, + vli_numBits(curve->n, curve->num_n_words) + 1, + curve); + + if (EccPoint_isZero(result, curve)) { return 0; } return 1; } -#if uECC_CURVE == uECC_secp224r1 +/* Compute a = sqrt(a) (mod curve_p). */ +static void mod_sqrt_default(uECC_word_t *a, uECC_Curve curve) { + bitcount_t i; + uECC_word_t p1[uECC_MAX_WORDS] = {1}; + uECC_word_t l_result[uECC_MAX_WORDS] = {1}; + + /* When curve->p == 3 (mod 4), we can compute + sqrt(a) = a^((curve->p + 1) / 4) (mod curve->p). */ + vli_add(p1, curve->p, p1, curve->num_words); /* p1 = curve_p + 1 */ + for (i = vli_numBits(p1, curve->num_words) - 1; i > 1; --i) { + vli_modSquare(l_result, l_result, curve->p, curve->num_words); + if (vli_testBit(p1, i)) { + vli_modMult(l_result, l_result, a, curve->p, curve->num_words); + } + } + vli_set(a, l_result, curve->num_words); +} /* Routine 3.2.4 RS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ static void mod_sqrt_secp224r1_rs(uECC_word_t *d1, @@ -1822,15 +798,15 @@ static void mod_sqrt_secp224r1_rs(uECC_word_t *d1, const uECC_word_t *d0, const uECC_word_t *e0, const uECC_word_t *f0) { - uECC_word_t t[uECC_WORDS]; + uECC_word_t t[uECC_MAX_WORDS]; - vli_modSquare_fast(t, d0); /* t <-- d0 ^ 2 */ - vli_modMult_fast(e1, d0, e0); /* e1 <-- d0 * e0 */ - vli_modAdd(d1, t, f0, curve_p); /* d1 <-- t + f0 */ - vli_modAdd(e1, e1, e1, curve_p); /* e1 <-- e1 + e1 */ - vli_modMult_fast(f1, t, f0); /* f1 <-- t * f0 */ - vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ - vli_modAdd(f1, f1, f1, curve_p); /* f1 <-- f1 + f1 */ + vli_modSquare(t, d0, curve_secp224r1.p, num_words_secp224r1); /* t <-- d0 ^ 2 */ + vli_modMult(e1, d0, e0, curve_secp224r1.p, num_words_secp224r1); /* e1 <-- d0 * e0 */ + vli_modAdd(d1, t, f0, curve_secp224r1.p, num_words_secp224r1); /* d1 <-- t + f0 */ + vli_modAdd(e1, e1, e1, curve_secp224r1.p, num_words_secp224r1); /* e1 <-- e1 + e1 */ + vli_modMult(f1, t, f0, curve_secp224r1.p, num_words_secp224r1); /* f1 <-- t * f0 */ + vli_modAdd(f1, f1, f1, curve_secp224r1.p, num_words_secp224r1); /* f1 <-- f1 + f1 */ + vli_modAdd(f1, f1, f1, curve_secp224r1.p, num_words_secp224r1); /* f1 <-- f1 + f1 */ } /* Routine 3.2.5 RSS; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ @@ -1843,9 +819,9 @@ static void mod_sqrt_secp224r1_rss(uECC_word_t *d1, const bitcount_t j) { bitcount_t i; - vli_set(d1, d0); /* d1 <-- d0 */ - vli_set(e1, e0); /* e1 <-- e0 */ - vli_set(f1, f0); /* f1 <-- f0 */ + vli_set(d1, d0, num_words_secp224r1); /* d1 <-- d0 */ + vli_set(e1, e0, num_words_secp224r1); /* e1 <-- e0 */ + vli_set(f1, f0, num_words_secp224r1); /* f1 <-- f0 */ for (i = 1; i <= j; i++) { mod_sqrt_secp224r1_rs(d1, e1, f1, d1, e1, f1); /* RS (d1,e1,f1,d1,e1,f1) */ } @@ -1860,21 +836,23 @@ static void mod_sqrt_secp224r1_rm(uECC_word_t *d2, const uECC_word_t *e0, const uECC_word_t *d1, const uECC_word_t *e1) { - uECC_word_t t1[uECC_WORDS]; - uECC_word_t t2[uECC_WORDS]; + uECC_word_t t1[uECC_MAX_WORDS]; + uECC_word_t t2[uECC_MAX_WORDS]; - vli_modMult_fast(t1, e0, e1); /* t1 <-- e0 * e1 */ - vli_modMult_fast(t1, t1, c); /* t1 <-- t1 * c */ - vli_modSub_fast(t1, curve_p, t1); /* t1 <-- p - t1 */ - vli_modMult_fast(t2, d0, d1); /* t2 <-- d0 * d1 */ - vli_modAdd(t2, t2, t1, curve_p); /* t2 <-- t2 + t1 */ - vli_modMult_fast(t1, d0, e1); /* t1 <-- d0 * e1 */ - vli_modMult_fast(e2, d1, e0); /* e2 <-- d1 * e0 */ - vli_modAdd(e2, e2, t1, curve_p); /* e2 <-- e2 + t1 */ - vli_modSquare_fast(f2, e2); /* f2 <-- e2^2 */ - vli_modMult_fast(f2, f2, c); /* f2 <-- f2 * c */ - vli_modSub_fast(f2, curve_p, f2); /* f2 <-- p - f2 */ - vli_set(d2, t2); /* d2 <-- t2 */ + vli_modMult(t1, e0, e1, curve_secp224r1.p, num_words_secp224r1); /* t1 <-- e0 * e1 */ + vli_modMult(t1, t1, c, curve_secp224r1.p, num_words_secp224r1); /* t1 <-- t1 * c */ + /* t1 <-- p - t1 */ + vli_modSub(t1, curve_secp224r1.p, t1, curve_secp224r1.p, num_words_secp224r1); + vli_modMult(t2, d0, d1, curve_secp224r1.p, num_words_secp224r1); /* t2 <-- d0 * d1 */ + vli_modAdd(t2, t2, t1, curve_secp224r1.p, num_words_secp224r1); /* t2 <-- t2 + t1 */ + vli_modMult(t1, d0, e1, curve_secp224r1.p, num_words_secp224r1); /* t1 <-- d0 * e1 */ + vli_modMult(e2, d1, e0, curve_secp224r1.p, num_words_secp224r1); /* e2 <-- d1 * e0 */ + vli_modAdd(e2, e2, t1, curve_secp224r1.p, num_words_secp224r1); /* e2 <-- e2 + t1 */ + vli_modSquare(f2, e2, curve_secp224r1.p, num_words_secp224r1); /* f2 <-- e2^2 */ + vli_modMult(f2, f2, c, curve_secp224r1.p, num_words_secp224r1); /* f2 <-- f2 * c */ + /* f2 <-- p - f2 */ + vli_modSub(f2, curve_secp224r1.p, f2, curve_secp224r1.p, num_words_secp224r1); + vli_set(d2, t2, num_words_secp224r1); /* d2 <-- t2 */ } /* Routine 3.2.7 RP; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ @@ -1885,87 +863,67 @@ static void mod_sqrt_secp224r1_rp(uECC_word_t *d1, const uECC_word_t *r) { wordcount_t i; wordcount_t pow2i = 1; - uECC_word_t d0[uECC_WORDS]; - uECC_word_t e0[uECC_WORDS] = {1}; /* e0 <-- 1 */ - uECC_word_t f0[uECC_WORDS]; + uECC_word_t d0[uECC_MAX_WORDS]; + uECC_word_t e0[uECC_MAX_WORDS] = {1}; /* e0 <-- 1 */ + uECC_word_t f0[uECC_MAX_WORDS]; - vli_set(d0, r); /* d0 <-- r */ - vli_modSub_fast(f0, curve_p, c); /* f0 <-- p - c */ + vli_set(d0, r, num_words_secp224r1); /* d0 <-- r */ + /* f0 <-- p - c */ + vli_modSub(f0, curve_secp224r1.p, c, curve_secp224r1.p, num_words_secp224r1); for (i = 0; i <= 6; i++) { mod_sqrt_secp224r1_rss(d1, e1, f1, d0, e0, f0, pow2i); /* RSS (d1,e1,f1,d0,e0,f0,2^i) */ mod_sqrt_secp224r1_rm(d1, e1, f1, c, d1, e1, d0, e0); /* RM (d1,e1,f1,c,d1,e1,d0,e0) */ - vli_set(d0, d1); /* d0 <-- d1 */ - vli_set(e0, e1); /* e0 <-- e1 */ - vli_set(f0, f1); /* f0 <-- f1 */ + vli_set(d0, d1, num_words_secp224r1); /* d0 <-- d1 */ + vli_set(e0, e1, num_words_secp224r1); /* e0 <-- e1 */ + vli_set(f0, f1, num_words_secp224r1); /* f0 <-- f1 */ pow2i *= 2; } } /* Compute a = sqrt(a) (mod curve_p). */ /* Routine 3.2.8 mp_mod_sqrt_224; from http://www.nsa.gov/ia/_files/nist-routines.pdf */ -static void mod_sqrt(uECC_word_t *a) { +static void mod_sqrt_secp224r1(uECC_word_t *a, uECC_Curve curve) { bitcount_t i; - uECC_word_t e1[uECC_WORDS]; - uECC_word_t f1[uECC_WORDS]; - uECC_word_t d0[uECC_WORDS]; - uECC_word_t e0[uECC_WORDS]; - uECC_word_t f0[uECC_WORDS]; - uECC_word_t d1[uECC_WORDS]; + uECC_word_t e1[uECC_MAX_WORDS]; + uECC_word_t f1[uECC_MAX_WORDS]; + uECC_word_t d0[uECC_MAX_WORDS]; + uECC_word_t e0[uECC_MAX_WORDS]; + uECC_word_t f0[uECC_MAX_WORDS]; + uECC_word_t d1[uECC_MAX_WORDS]; // s = a; using constant instead of random value mod_sqrt_secp224r1_rp(d0, e0, f0, a, a); /* RP (d0, e0, f0, c, s) */ mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ for (i = 1; i <= 95; i++) { - vli_set(d0, d1); /* d0 <-- d1 */ - vli_set(e0, e1); /* e0 <-- e1 */ - vli_set(f0, f1); /* f0 <-- f1 */ + vli_set(d0, d1, num_words_secp224r1); /* d0 <-- d1 */ + vli_set(e0, e1, num_words_secp224r1); /* e0 <-- e1 */ + vli_set(f0, f1, num_words_secp224r1); /* f0 <-- f1 */ mod_sqrt_secp224r1_rs(d1, e1, f1, d0, e0, f0); /* RS (d1, e1, f1, d0, e0, f0) */ - if (vli_isZero(d1)) { /* if d1 == 0 */ + if (vli_isZero(d1, num_words_secp224r1)) { /* if d1 == 0 */ break; } } - vli_modInv(f1, e0, curve_p); /* f1 <-- 1 / e0 */ - vli_modMult_fast(a, d0, f1); /* a <-- d0 / e0 */ + vli_modInv(f1, e0, curve_secp224r1.p, num_words_secp224r1); /* f1 <-- 1 / e0 */ + vli_modMult(a, d0, f1, curve_secp224r1.p, num_words_secp224r1); /* a <-- d0 / e0 */ } -#else /* uECC_CURVE */ - -/* Compute a = sqrt(a) (mod curve_p). */ -static void mod_sqrt(uECC_word_t *a) { - bitcount_t i; - uECC_word_t p1[uECC_WORDS] = {1}; - uECC_word_t l_result[uECC_WORDS] = {1}; - - /* Since curve_p == 3 (mod 4) for all supported curves, we can - compute sqrt(a) = a^((curve_p + 1) / 4) (mod curve_p). */ - vli_add(p1, curve_p, p1); /* p1 = curve_p + 1 */ - for (i = vli_numBits(p1, uECC_WORDS) - 1; i > 1; --i) { - vli_modSquare_fast(l_result, l_result); - if (vli_testBit(p1, i)) { - vli_modMult_fast(l_result, l_result, a); - } - } - vli_set(a, l_result); -} -#endif /* uECC_CURVE */ - #if uECC_WORD_SIZE == 1 -static void vli_nativeToBytes(uint8_t * RESTRICT dest, const uint8_t * RESTRICT src) { +static void vli_nativeToBytes(uint8_t * dest, const uint8_t * src, uECC_Curve curve) { uint8_t i; - for (i = 0; i < uECC_BYTES; ++i) { - dest[i] = src[(uECC_BYTES - 1) - i]; + for (i = 0; i < curve->num_words; ++i) { + dest[i] = src[(curve->num_words - 1) - i]; } } -#define vli_bytesToNative(dest, src) vli_nativeToBytes((dest), (src)) +#define vli_bytesToNative(dest, src, curve) vli_nativeToBytes((dest), (src), (curve)) #elif uECC_WORD_SIZE == 4 -static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) { +static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native, uECC_Curve curve) { unsigned i; - for (i = 0; i < uECC_WORDS; ++i) { - uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); + for (i = 0; i < curve->num_words; ++i) { + uint8_t *digit = bytes + 4 * (curve->num_words - 1 - i); digit[0] = native[i] >> 24; digit[1] = native[i] >> 16; digit[2] = native[i] >> 8; @@ -1973,10 +931,10 @@ static void vli_nativeToBytes(uint8_t *bytes, const uint32_t *native) { } } -static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) { +static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes, uECC_Curve curve) { unsigned i; - for (i = 0; i < uECC_WORDS; ++i) { - const uint8_t *digit = bytes + 4 * (uECC_WORDS - 1 - i); + for (i = 0; i < curve->num_words; ++i) { + const uint8_t *digit = bytes + 4 * (curve->num_words - 1 - i); native[i] = ((uint32_t)digit[0] << 24) | ((uint32_t)digit[1] << 16) | ((uint32_t)digit[2] << 8) | (uint32_t)digit[3]; } @@ -1984,550 +942,276 @@ static void vli_bytesToNative(uint32_t *native, const uint8_t *bytes) { #else -static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native) { +static void vli_nativeToBytes(uint8_t *bytes, const uint64_t *native, uECC_Curve curve) { unsigned i; - for (i = 0; i < uECC_WORDS; ++i) { - uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); - digit[0] = native[i] >> 56; - digit[1] = native[i] >> 48; - digit[2] = native[i] >> 40; - digit[3] = native[i] >> 32; - digit[4] = native[i] >> 24; - digit[5] = native[i] >> 16; - digit[6] = native[i] >> 8; - digit[7] = native[i]; + for (i = 0; i < curve->num_bytes; ++i) { + unsigned b = curve->num_bytes - 1 - i; + bytes[i] = native[b / 8] >> (8 * (b % 8)); } } -static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes) { +static void vli_bytesToNative(uint64_t *native, const uint8_t *bytes, uECC_Curve curve) { unsigned i; - for (i = 0; i < uECC_WORDS; ++i) { - const uint8_t *digit = bytes + 8 * (uECC_WORDS - 1 - i); - native[i] = ((uint64_t)digit[0] << 56) | ((uint64_t)digit[1] << 48) | - ((uint64_t)digit[2] << 40) | ((uint64_t)digit[3] << 32) | - ((uint64_t)digit[4] << 24) | ((uint64_t)digit[5] << 16) | - ((uint64_t)digit[6] << 8) | (uint64_t)digit[7]; + vli_clear(native, curve->num_words); + for (i = 0; i < curve->num_bytes; ++i) { + unsigned b = curve->num_bytes - 1 - i; + native[b / 8] |= (uint64_t)bytes[i] << (8 * (b % 8)); } } #endif /* uECC_WORD_SIZE */ -int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]) { - uECC_word_t private[uECC_WORDS]; - EccPoint public; +/* Generate a random integer with num_bits bits. The remaining high bits + in the buffer (if any) are zeroed. */ +static cmpresult_t generate_random_int(uECC_word_t *random, + const wordcount_t num_words, + const wordcount_t num_bits) { + if (!g_rng_function((uint8_t *)random, num_words * uECC_WORD_SIZE)) { + return 0; + } + if (num_words * uECC_WORD_SIZE * 8 > num_bits) { + wordcount_t mask = (wordcount_t)-1; + random[num_words - 1] &= mask >> ((bitcount_t)(num_words * uECC_WORD_SIZE * 8 - num_bits)); + } + return 1; +} + +int uECC_make_key(uint8_t *public_key, + uint8_t *private_key, + uECC_Curve curve) { + uECC_word_t private[uECC_MAX_WORDS]; + uECC_word_t public[uECC_MAX_WORDS * 2]; uECC_word_t tries; + + /* Zero out correctly (to compare to curve->n) for secp160r1. */ + private[curve->num_n_words - 1] = 0; + for (tries = 0; tries < MAX_TRIES; ++tries) { - if (g_rng_function((uint8_t *)private, sizeof(private)) && - EccPoint_compute_public_key(&public, private)) { - vli_nativeToBytes(private_key, private); - vli_nativeToBytes(public_key, public.x); - vli_nativeToBytes(public_key + uECC_BYTES, public.y); + if (!generate_random_int(private, curve->num_words, curve->num_bytes * 8)) { + return 0; + } + + if (EccPoint_compute_public_key(public, private, curve)) { + vli_nativeToBytes(private_key, private, curve); + vli_nativeToBytes(public_key, public, curve); + vli_nativeToBytes(public_key + curve->num_bytes, public + curve->num_words, curve); return 1; } } return 0; } -int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2], - const uint8_t private_key[uECC_BYTES], - uint8_t secret[uECC_BYTES]) { - EccPoint public; - EccPoint product; - uECC_word_t private[uECC_WORDS]; - uECC_word_t tmp[uECC_WORDS]; +int uECC_shared_secret(const uint8_t *public_key, + const uint8_t *private_key, + uint8_t *secret, + uECC_Curve curve) { + uECC_word_t public[uECC_MAX_WORDS * 2]; + uECC_word_t private[uECC_MAX_WORDS]; + uECC_word_t tmp[uECC_MAX_WORDS]; uECC_word_t *p2[2] = {private, tmp}; - uECC_word_t random[uECC_WORDS]; uECC_word_t *initial_Z = 0; uECC_word_t tries; uECC_word_t carry; - - // Try to get a random initial Z value to improve protection against side-channel - // attacks. If the RNG fails every time (eg it was not defined), we continue so that - // uECC_shared_secret() can still work without an RNG defined. - for (tries = 0; tries < MAX_TRIES; ++tries) { - if (g_rng_function((uint8_t *)random, sizeof(random)) && !vli_isZero(random)) { - initial_Z = random; - break; - } - } - - vli_bytesToNative(private, private_key); - vli_bytesToNative(public.x, public_key); - vli_bytesToNative(public.y, public_key + uECC_BYTES); - -#if (uECC_CURVE == uECC_secp160r1) - // Don't regularize the bitcount for secp160r1. - EccPoint_mult(&product, &public, private, initial_Z, vli_numBits(private, uECC_WORDS)); -#else + + /* Zero out correctly (for addition with curve->n) for secp160r1. */ + private[curve->num_n_words - 1] = 0; + + vli_bytesToNative(private, private_key, curve); + vli_bytesToNative(public, public_key, curve); + vli_bytesToNative(public + curve->num_words, public_key + curve->num_bytes, curve); + // Regularize the bitcount for the private key so that attackers cannot use a side channel // attack to learn the number of leading zeros. - carry = vli_add(private, private, curve_n); - vli_add(tmp, private, curve_n); - EccPoint_mult(&product, &public, p2[!carry], initial_Z, (uECC_BYTES * 8) + 1); -#endif - - vli_nativeToBytes(secret, product.x); - return !EccPoint_isZero(&product); + carry = regularize_k(private, private, tmp, curve); + + // If an RNG function was specified, try to get a random initial Z value to improve + // protection against side-channel attacks. + if (g_rng_function != &default_RNG) { + for (tries = 0; tries < MAX_TRIES; ++tries) { + if (!generate_random_int(p2[carry], curve->num_words, curve->num_bytes * 8)) { + return 0; + } + + if (!vli_isZero(p2[carry], curve->num_words) && + vli_cmp(curve->p, p2[carry], curve->num_words) == 1) { + initial_Z = p2[carry]; + break; + } + } + } + + EccPoint_mult(public, public, p2[!carry], initial_Z, + vli_numBits(curve->n, curve->num_n_words) + 1, + curve); + vli_nativeToBytes(secret, public, curve); + return !EccPoint_isZero(public, curve); } -void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]) { +void uECC_compress(const uint8_t *public_key, uint8_t *compressed, uECC_Curve curve) { wordcount_t i; - for (i = 0; i < uECC_BYTES; ++i) { + for (i = 0; i < curve->num_bytes; ++i) { compressed[i+1] = public_key[i]; } - compressed[0] = 2 + (public_key[uECC_BYTES * 2 - 1] & 0x01); + compressed[0] = 2 + (public_key[curve->num_bytes * 2 - 1] & 0x01); } /* Computes result = x^3 + ax + b. result must not overlap x. */ -static void curve_x_side(uECC_word_t * RESTRICT result, const uECC_word_t * RESTRICT x) { -#if (uECC_CURVE == uECC_secp256k1) - vli_modSquare_fast(result, x); /* r = x^2 */ - vli_modMult_fast(result, result, x); /* r = x^3 */ - vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 + b */ -#else - uECC_word_t _3[uECC_WORDS] = {3}; /* -a = 3 */ +static void x_side_default(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve) { + uECC_word_t _3[uECC_MAX_WORDS] = {3}; /* -a = 3 */ - vli_modSquare_fast(result, x); /* r = x^2 */ - vli_modSub_fast(result, result, _3); /* r = x^2 - 3 */ - vli_modMult_fast(result, result, x); /* r = x^3 - 3x */ - vli_modAdd(result, result, curve_b, curve_p); /* r = x^3 - 3x + b */ -#endif + vli_modSquare(result, x, curve->p, curve->num_words); /* r = x^2 */ + vli_modSub(result, result, _3, curve->p, curve->num_words); /* r = x^2 - 3 */ + vli_modMult(result, result, x, curve->p, curve->num_words); /* r = x^3 - 3x */ + vli_modAdd(result, result, curve->b, curve->p, curve->num_words); /* r = x^3 - 3x + b */ } -void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]) { - EccPoint point; - vli_bytesToNative(point.x, compressed + 1); - curve_x_side(point.y, point.x); - mod_sqrt(point.y); +/* Computes result = x^3 + b. result must not overlap x. */ +static void x_side_secp256k1(uECC_word_t *result, const uECC_word_t *x, uECC_Curve curve) { + vli_modSquare(result, x, curve->p, curve->num_words); /* r = x^2 */ + vli_modMult(result, result, x, curve->p, curve->num_words); /* r = x^3 */ + vli_modAdd(result, result, curve->b, curve->p, curve->num_words); /* r = x^3 + b */ +} - if ((point.y[0] & 0x01) != (compressed[0] & 0x01)) { - vli_sub(point.y, curve_p, point.y); +void uECC_decompress(const uint8_t *compressed, uint8_t *public_key, uECC_Curve curve) { + uECC_word_t point[uECC_MAX_WORDS * 2]; + uECC_word_t *y = point + curve->num_words; + vli_bytesToNative(point, compressed + 1, curve); + curve->x_side(y, point, curve); + curve->mod_sqrt(y, curve); + + if ((y[0] & 0x01) != (compressed[0] & 0x01)) { + vli_sub(y, curve->p, y, curve->num_words); } - - vli_nativeToBytes(public_key, point.x); - vli_nativeToBytes(public_key + uECC_BYTES, point.y); + + vli_nativeToBytes(public_key, point, curve); + vli_nativeToBytes(public_key + curve->num_bytes, y, curve); } -int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]) { - uECC_word_t tmp1[uECC_WORDS]; - uECC_word_t tmp2[uECC_WORDS]; - EccPoint public; - - vli_bytesToNative(public.x, public_key); - vli_bytesToNative(public.y, public_key + uECC_BYTES); - +int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve) { + uECC_word_t tmp1[uECC_MAX_WORDS]; + uECC_word_t tmp2[uECC_MAX_WORDS]; + uECC_word_t public[uECC_MAX_WORDS * 2]; + + vli_bytesToNative(public, public_key, curve); + vli_bytesToNative(public + curve->num_words, public_key + curve->num_bytes, curve); + // The point at infinity is invalid. - if (EccPoint_isZero(&public)) { + if (EccPoint_isZero(public, curve)) { return 0; } // x and y must be smaller than p. - if (vli_cmp(curve_p, public.x) != 1 || vli_cmp(curve_p, public.y) != 1) { + if (vli_cmp(curve->p, public, curve->num_words) != 1 || + vli_cmp(curve->p, public + curve->num_words, curve->num_words) != 1) { return 0; } - - vli_modSquare_fast(tmp1, public.y); /* tmp1 = y^2 */ - curve_x_side(tmp2, public.x); /* tmp2 = x^3 + ax + b */ - + + vli_modSquare(tmp1, public + curve->num_words, curve->p, curve->num_words); + curve->x_side(tmp2, public, curve); /* tmp2 = x^3 + ax + b */ + /* Make sure that y^2 == x^3 + ax + b */ - return (vli_cmp(tmp1, tmp2) == 0); + return (vli_cmp(tmp1, tmp2, curve->num_words) == 0); } -int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES], - uint8_t public_key[uECC_BYTES * 2]) { - uECC_word_t private[uECC_WORDS]; - EccPoint public; +int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve) { + uECC_word_t private[uECC_MAX_WORDS]; + uECC_word_t public[uECC_MAX_WORDS * 2]; - vli_bytesToNative(private, private_key); + vli_bytesToNative(private, private_key, curve); - if (!EccPoint_compute_public_key(&public, private)) { + if (!EccPoint_compute_public_key(public, private, curve)) { return 0; } - vli_nativeToBytes(public_key, public.x); - vli_nativeToBytes(public_key + uECC_BYTES, public.y); + vli_nativeToBytes(public_key, public, curve); + vli_nativeToBytes(public_key + curve->num_bytes, public + curve->num_words, curve); return 1; } -int uECC_bytes(void) { - return uECC_BYTES; -} - -int uECC_curve(void) { - return uECC_CURVE; -} /* -------- ECDSA code -------- */ -#if (uECC_CURVE == uECC_secp160r1) -static void vli_clear_n(uECC_word_t *vli) { - vli_clear(vli); - vli[uECC_N_WORDS - 1] = 0; -} - -static uECC_word_t vli_isZero_n(const uECC_word_t *vli) { - if (vli[uECC_N_WORDS - 1]) { - return 0; - } - return vli_isZero(vli); -} - -static void vli_set_n(uECC_word_t *dest, const uECC_word_t *src) { - vli_set(dest, src); - dest[uECC_N_WORDS - 1] = src[uECC_N_WORDS - 1]; -} - -static cmpresult_t vli_cmp_n(const uECC_word_t *left, const uECC_word_t *right) { - if (left[uECC_N_WORDS - 1] > right[uECC_N_WORDS - 1]) { - return 1; - } else if (left[uECC_N_WORDS - 1] < right[uECC_N_WORDS - 1]) { - return -1; - } - return vli_cmp(left, right); -} - -static void vli_rshift1_n(uECC_word_t *vli) { - vli_rshift1(vli); - vli[uECC_N_WORDS - 2] |= vli[uECC_N_WORDS - 1] << (uECC_WORD_BITS - 1); - vli[uECC_N_WORDS - 1] = vli[uECC_N_WORDS - 1] >> 1; -} - -static uECC_word_t vli_add_n(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right) { - uECC_word_t carry = vli_add(result, left, right); - uECC_word_t sum = left[uECC_N_WORDS - 1] + right[uECC_N_WORDS - 1] + carry; - if (sum != left[uECC_N_WORDS - 1]) { - carry = (sum < left[uECC_N_WORDS - 1]); - } - result[uECC_N_WORDS - 1] = sum; - return carry; -} - -static uECC_word_t vli_sub_n(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right) { - uECC_word_t borrow = vli_sub(result, left, right); - uECC_word_t diff = left[uECC_N_WORDS - 1] - right[uECC_N_WORDS - 1] - borrow; - if (diff != left[uECC_N_WORDS - 1]) { - borrow = (diff > left[uECC_N_WORDS - 1]); - } - result[uECC_N_WORDS - 1] = diff; - return borrow; -} - -#if !muladd_exists -static void muladd(uECC_word_t a, - uECC_word_t b, - uECC_word_t *r0, - uECC_word_t *r1, - uECC_word_t *r2) { - uECC_dword_t p = (uECC_dword_t)a * b; - uECC_dword_t r01 = ((uECC_dword_t)(*r1) << uECC_WORD_BITS) | *r0; - r01 += p; - *r2 += (r01 < p); - *r1 = r01 >> uECC_WORD_BITS; - *r0 = (uECC_word_t)r01; -} -#define muladd_exists 1 -#endif - -static void vli_mult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { - uECC_word_t r0 = 0; - uECC_word_t r1 = 0; - uECC_word_t r2 = 0; - wordcount_t i, k; - - for (k = 0; k < uECC_N_WORDS * 2 - 1; ++k) { - wordcount_t min = (k < uECC_N_WORDS ? 0 : (k + 1) - uECC_N_WORDS); - wordcount_t max = (k < uECC_N_WORDS ? k : uECC_N_WORDS - 1); - for (i = min; i <= max; ++i) { - muladd(left[i], right[k - i], &r0, &r1, &r2); - } - result[k] = r0; - r0 = r1; - r1 = r2; - r2 = 0; - } - result[uECC_N_WORDS * 2 - 1] = r0; -} - -static void vli_modAdd_n(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right, - const uECC_word_t *mod) { - uECC_word_t carry = vli_add_n(result, left, right); - if (carry || vli_cmp_n(result, mod) >= 0) { - vli_sub_n(result, result, mod); - } -} - -static void vli_modInv_n(uECC_word_t *result, const uECC_word_t *input, const uECC_word_t *mod) { - uECC_word_t a[uECC_N_WORDS], b[uECC_N_WORDS], u[uECC_N_WORDS], v[uECC_N_WORDS]; - uECC_word_t carry; - cmpresult_t cmpResult; - - if (vli_isZero_n(input)) { - vli_clear_n(result); - return; - } - - vli_set_n(a, input); - vli_set_n(b, mod); - vli_clear_n(u); - u[0] = 1; - vli_clear_n(v); - while ((cmpResult = vli_cmp_n(a, b)) != 0) { - carry = 0; - if (EVEN(a)) { - vli_rshift1_n(a); - if (!EVEN(u)) { - carry = vli_add_n(u, u, mod); - } - vli_rshift1_n(u); - if (carry) { - u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; - } - } else if (EVEN(b)) { - vli_rshift1_n(b); - if (!EVEN(v)) { - carry = vli_add_n(v, v, mod); - } - vli_rshift1_n(v); - if (carry) { - v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; - } - } else if (cmpResult > 0) { - vli_sub_n(a, a, b); - vli_rshift1_n(a); - if (vli_cmp_n(u, v) < 0) { - vli_add_n(u, u, mod); - } - vli_sub_n(u, u, v); - if (!EVEN(u)) { - carry = vli_add_n(u, u, mod); - } - vli_rshift1_n(u); - if (carry) { - u[uECC_N_WORDS - 1] |= HIGH_BIT_SET; - } - } else { - vli_sub_n(b, b, a); - vli_rshift1_n(b); - if (vli_cmp_n(v, u) < 0) { - vli_add_n(v, v, mod); - } - vli_sub_n(v, v, u); - if (!EVEN(v)) { - carry = vli_add_n(v, v, mod); - } - vli_rshift1_n(v); - if (carry) { - v[uECC_N_WORDS - 1] |= HIGH_BIT_SET; - } - } - } - vli_set_n(result, u); -} - -static void vli2_rshift1_n(uECC_word_t *vli) { - vli_rshift1_n(vli); - vli[uECC_N_WORDS - 1] |= vli[uECC_N_WORDS] << (uECC_WORD_BITS - 1); - vli_rshift1_n(vli + uECC_N_WORDS); -} - -static uECC_word_t vli2_sub_n(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right) { - uECC_word_t borrow = 0; - wordcount_t i; - for (i = 0; i < uECC_N_WORDS * 2; ++i) { - uECC_word_t diff = left[i] - right[i] - borrow; - if (diff != left[i]) { - borrow = (diff > left[i]); - } - result[i] = diff; - } - return borrow; -} - -/* Computes result = (left * right) % curve_n. */ -static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { - bitcount_t i; - uECC_word_t product[2 * uECC_N_WORDS]; - uECC_word_t modMultiple[2 * uECC_N_WORDS]; - uECC_word_t tmp[2 * uECC_N_WORDS]; - uECC_word_t *v[2] = {tmp, product}; - uECC_word_t index = 1; - - vli_mult_n(product, left, right); - vli_clear_n(modMultiple); - vli_set(modMultiple + uECC_N_WORDS + 1, curve_n); - vli_rshift1(modMultiple + uECC_N_WORDS + 1); - modMultiple[2 * uECC_N_WORDS - 1] |= HIGH_BIT_SET; - modMultiple[uECC_N_WORDS] = HIGH_BIT_SET; - - for (i = 0; - i <= ((((bitcount_t)uECC_N_WORDS) << uECC_WORD_BITS_SHIFT) + (uECC_WORD_BITS - 1)); - ++i) { - uECC_word_t borrow = vli2_sub_n(v[1 - index], v[index], modMultiple); - index = !(index ^ borrow); /* Swap the index if there was no borrow */ - vli2_rshift1_n(modMultiple); - } - vli_set_n(result, v[index]); -} - -#else - -#define vli_cmp_n vli_cmp -#define vli_modInv_n vli_modInv -#define vli_modAdd_n vli_modAdd - -static void vli2_rshift1(uECC_word_t *vli) { - vli_rshift1(vli); - vli[uECC_WORDS - 1] |= vli[uECC_WORDS] << (uECC_WORD_BITS - 1); - vli_rshift1(vli + uECC_WORDS); -} - -static uECC_word_t vli2_sub(uECC_word_t *result, - const uECC_word_t *left, - const uECC_word_t *right) { - uECC_word_t borrow = 0; - wordcount_t i; - for (i = 0; i < uECC_WORDS * 2; ++i) { - uECC_word_t diff = left[i] - right[i] - borrow; - if (diff != left[i]) { - borrow = (diff > left[i]); - } - result[i] = diff; - } - return borrow; -} - -/* Computes result = (left * right) % curve_n. */ -static void vli_modMult_n(uECC_word_t *result, const uECC_word_t *left, const uECC_word_t *right) { - uECC_word_t product[2 * uECC_WORDS]; - uECC_word_t modMultiple[2 * uECC_WORDS]; - uECC_word_t tmp[2 * uECC_WORDS]; - uECC_word_t *v[2] = {tmp, product}; - bitcount_t i; - uECC_word_t index = 1; - - vli_mult(product, left, right); - vli_set(modMultiple + uECC_WORDS, curve_n); /* works if curve_n has its highest bit set */ - vli_clear(modMultiple); - - for (i = 0; i <= uECC_BYTES * 8; ++i) { - uECC_word_t borrow = vli2_sub(v[1 - index], v[index], modMultiple); - index = !(index ^ borrow); /* Swap the index if there was no borrow */ - vli2_rshift1(modMultiple); - } - vli_set(result, v[index]); -} -#endif /* (uECC_CURVE != uECC_secp160r1) */ - -static int uECC_sign_with_k(const uint8_t private_key[uECC_BYTES], - const uint8_t message_hash[uECC_BYTES], - uECC_word_t k[uECC_N_WORDS], - uint8_t signature[uECC_BYTES*2]) { - uECC_word_t tmp[uECC_N_WORDS]; - uECC_word_t s[uECC_N_WORDS]; +static int uECC_sign_with_k(const uint8_t *private_key, + const uint8_t *message_hash, + uECC_word_t *k, + uint8_t *signature, + uECC_Curve curve) { + uECC_word_t tmp[uECC_MAX_WORDS]; + uECC_word_t s[uECC_MAX_WORDS]; uECC_word_t *k2[2] = {tmp, s}; - EccPoint p; + uECC_word_t p[uECC_MAX_WORDS * 2]; uECC_word_t carry; - uECC_word_t tries; - + bitcount_t num_n_bits = vli_numBits(curve->n, curve->num_n_words); + /* Make sure 0 < k < curve_n */ - if (vli_isZero(k) || vli_cmp_n(curve_n, k) != 1) { + if (vli_isZero(k, curve->num_words) || vli_cmp(curve->n, k, curve->num_n_words) != 1) { return 0; } - -#if (uECC_CURVE == uECC_secp160r1) - /* Make sure that we don't leak timing information about k. - See http://eprint.iacr.org/2011/232.pdf */ - vli_add_n(tmp, k, curve_n); - carry = (tmp[uECC_WORDS] & 0x02); - vli_add_n(s, tmp, curve_n); - - /* p = k * G */ - EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 2); -#else - /* Make sure that we don't leak timing information about k. - See http://eprint.iacr.org/2011/232.pdf */ - carry = vli_add(tmp, k, curve_n); - vli_add(s, tmp, curve_n); - - /* p = k * G */ - EccPoint_mult(&p, &curve_G, k2[!carry], 0, (uECC_BYTES * 8) + 1); - - /* r = x1 (mod n) */ - if (vli_cmp(curve_n, p.x) != 1) { - vli_sub(p.x, p.x, curve_n); - } -#endif - if (vli_isZero(p.x)) { + + carry = regularize_k(k, tmp, s, curve); + EccPoint_mult(p, curve->G, k2[!carry], 0, num_n_bits + 1, curve); + if (vli_isZero(p, curve->num_words)) { return 0; } - - // Attempt to get a random number to prevent side channel analysis of k. - // If the RNG fails every time (eg it was not defined), we continue so that - // deterministic signing can still work (with reduced security) without - // an RNG defined. - carry = 0; // use to signal that the RNG succeeded at least once. - for (tries = 0; tries < MAX_TRIES; ++tries) { - if (!g_rng_function((uint8_t *)tmp, sizeof(tmp))) { - continue; - } - carry = 1; - if (!vli_isZero(tmp)) { - break; - } - } - if (!carry) { - vli_clear(tmp); + + /* Attempt to get a random number to prevent side channel analysis of k. */ + if (g_rng_function == &default_RNG) { + vli_clear(tmp, curve->num_n_words); tmp[0] = 1; + } else { + uECC_word_t tries; + for (tries = 0; tries < MAX_TRIES; ++tries) { + if (!generate_random_int(tmp, curve->num_n_words, num_n_bits)) { + return 0; + } + + if (!vli_isZero(tmp, curve->num_n_words) && + vli_cmp(curve->n, tmp, curve->num_n_words) == 1) { + goto got_random; + } + } + return 0; } - +got_random: /* Prevent side channel analysis of vli_modInv() to determine bits of k / the private key by premultiplying by a random number */ - vli_modMult_n(k, k, tmp); /* k' = rand * k */ - vli_modInv_n(k, k, curve_n); /* k = 1 / k' */ - vli_modMult_n(k, k, tmp); /* k = 1 / k */ + vli_modMult(k, k, tmp, curve->n, curve->num_n_words); /* k' = rand * k */ + vli_modInv(k, k, curve->n, curve->num_n_words); /* k = 1 / k' */ + vli_modMult(k, k, tmp, curve->n, curve->num_n_words); /* k = 1 / k */ + + vli_nativeToBytes(signature, p, curve); /* store r */ + + tmp[curve->num_n_words - 1] = 0; + vli_bytesToNative(tmp, private_key, curve); /* tmp = d */ + s[curve->num_n_words - 1] = 0; + vli_set(s, p, curve->num_words); + vli_modMult(s, tmp, s, curve->n, curve->num_n_words); /* s = r*d */ - vli_nativeToBytes(signature, p.x); /* store r */ - - tmp[uECC_N_WORDS - 1] = 0; - vli_bytesToNative(tmp, private_key); /* tmp = d */ - s[uECC_N_WORDS - 1] = 0; - vli_set(s, p.x); - vli_modMult_n(s, tmp, s); /* s = r*d */ - - vli_bytesToNative(tmp, message_hash); - vli_modAdd_n(s, tmp, s, curve_n); /* s = e + r*d */ - vli_modMult_n(s, s, k); /* s = (e + r*d) / k */ -#if (uECC_CURVE == uECC_secp160r1) - if (s[uECC_N_WORDS - 1]) { + vli_bytesToNative(tmp, message_hash, curve); + vli_modAdd(s, tmp, s, curve->n, curve->num_n_words); /* s = e + r*d */ + vli_modMult(s, s, k, curve->n, curve->num_n_words); /* s = (e + r*d) / k */ + if (vli_numBits(s, curve->num_n_words) > (bitcount_t)curve->num_bytes * 8) { return 0; } -#endif - vli_nativeToBytes(signature + uECC_BYTES, s); + vli_nativeToBytes(signature + curve->num_bytes, s, curve); return 1; } -int uECC_sign(const uint8_t private_key[uECC_BYTES], - const uint8_t message_hash[uECC_BYTES], - uint8_t signature[uECC_BYTES*2]) { - uECC_word_t k[uECC_N_WORDS]; +int uECC_sign(const uint8_t *private_key, + const uint8_t *message_hash, + uint8_t *signature, + uECC_Curve curve) { + uECC_word_t k[uECC_MAX_WORDS]; uECC_word_t tries; - + bitcount_t num_n_bits = vli_numBits(curve->n, curve->num_n_words); + for (tries = 0; tries < MAX_TRIES; ++tries) { - if(g_rng_function((uint8_t *)k, sizeof(k))) { - #if (uECC_CURVE == uECC_secp160r1) - k[uECC_WORDS] &= 0x01; - #endif - if (uECC_sign_with_k(private_key, message_hash, k, signature)) { - return 1; - } + if (!generate_random_int(k, curve->num_n_words, num_n_bits)) { + return 0; + } + + if (uECC_sign_with_k(private_key, message_hash, k, signature, curve)) { + return 1; } } return 0; @@ -2582,12 +1266,14 @@ static void update_V(uECC_HashContext *hash_context, uint8_t *K, uint8_t *V) { * We generate a value for k (aka T) directly rather than converting endianness. Layout of hash_context->tmp: | | (1 byte overlapped 0x00 or 0x01) / */ -int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], - const uint8_t message_hash[uECC_BYTES], +int uECC_sign_deterministic(const uint8_t *private_key, + const uint8_t *message_hash, uECC_HashContext *hash_context, - uint8_t signature[uECC_BYTES*2]) { + uint8_t *signature, + uECC_Curve curve) { uint8_t *K = hash_context->tmp; uint8_t *V = K + hash_context->result_size; + bitcount_t num_n_bits = vli_numBits(curve->n, curve->num_n_words); uECC_word_t tries; unsigned i; for (i = 0; i < hash_context->result_size; ++i) { @@ -2599,8 +1285,8 @@ int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], HMAC_init(hash_context, K); V[hash_context->result_size] = 0x00; HMAC_update(hash_context, V, hash_context->result_size + 1); - HMAC_update(hash_context, private_key, uECC_BYTES); - HMAC_update(hash_context, message_hash, uECC_BYTES); + HMAC_update(hash_context, private_key, curve->num_bytes); + HMAC_update(hash_context, message_hash, curve->num_bytes); HMAC_finish(hash_context, K, K); update_V(hash_context, K, V); @@ -2609,27 +1295,29 @@ int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], HMAC_init(hash_context, K); V[hash_context->result_size] = 0x01; HMAC_update(hash_context, V, hash_context->result_size + 1); - HMAC_update(hash_context, private_key, uECC_BYTES); - HMAC_update(hash_context, message_hash, uECC_BYTES); + HMAC_update(hash_context, private_key, curve->num_bytes); + HMAC_update(hash_context, message_hash, curve->num_bytes); HMAC_finish(hash_context, K, K); update_V(hash_context, K, V); for (tries = 0; tries < MAX_TRIES; ++tries) { - uECC_word_t T[uECC_N_WORDS]; + uECC_word_t T[uECC_MAX_WORDS]; uint8_t *T_ptr = (uint8_t *)T; unsigned T_bytes = 0; - while (T_bytes < sizeof(T)) { + while (T_bytes < curve->num_words * uECC_WORD_SIZE) { update_V(hash_context, K, V); for (i = 0; i < hash_context->result_size && T_bytes < sizeof(T); ++i, ++T_bytes) { T_ptr[T_bytes] = V[i]; } } - #if (uECC_CURVE == uECC_secp160r1) - T[uECC_WORDS] &= 0x01; - #endif - - if (uECC_sign_with_k(private_key, message_hash, T, signature)) { + if ((bitcount_t)curve->num_words * uECC_WORD_SIZE * 8 > num_n_bits) { + wordcount_t mask = (wordcount_t)-1; + T[curve->num_words - 1] &= + mask >> ((bitcount_t)(curve->num_words * uECC_WORD_SIZE * 8 - num_n_bits)); + } + + if (uECC_sign_with_k(private_key, message_hash, T, signature, curve)) { return 1; } @@ -2648,96 +1336,100 @@ static bitcount_t smax(bitcount_t a, bitcount_t b) { return (a > b ? a : b); } -int uECC_verify(const uint8_t public_key[uECC_BYTES*2], - const uint8_t hash[uECC_BYTES], - const uint8_t signature[uECC_BYTES*2]) { - uECC_word_t u1[uECC_N_WORDS], u2[uECC_N_WORDS]; - uECC_word_t z[uECC_N_WORDS]; - EccPoint public, sum; - uECC_word_t rx[uECC_WORDS]; - uECC_word_t ry[uECC_WORDS]; - uECC_word_t tx[uECC_WORDS]; - uECC_word_t ty[uECC_WORDS]; - uECC_word_t tz[uECC_WORDS]; - const EccPoint *points[4]; - const EccPoint *point; - bitcount_t numBits; +int uECC_verify(const uint8_t *public_key, + const uint8_t *hash, + const uint8_t *signature, + uECC_Curve curve) { + uECC_word_t u1[uECC_MAX_WORDS], u2[uECC_MAX_WORDS]; + uECC_word_t z[uECC_MAX_WORDS]; + uECC_word_t public[uECC_MAX_WORDS * 2]; + uECC_word_t sum[uECC_MAX_WORDS * 2]; + uECC_word_t rx[uECC_MAX_WORDS]; + uECC_word_t ry[uECC_MAX_WORDS]; + uECC_word_t tx[uECC_MAX_WORDS]; + uECC_word_t ty[uECC_MAX_WORDS]; + uECC_word_t tz[uECC_MAX_WORDS]; + const uECC_word_t *points[4]; + const uECC_word_t *point; + bitcount_t num_bits; bitcount_t i; - uECC_word_t r[uECC_N_WORDS], s[uECC_N_WORDS]; - r[uECC_N_WORDS - 1] = 0; - s[uECC_N_WORDS - 1] = 0; + uECC_word_t r[uECC_MAX_WORDS], s[uECC_MAX_WORDS]; + + + rx[curve->num_n_words - 1] = 0; + r[curve->num_n_words - 1] = 0; + s[curve->num_n_words - 1] = 0; - vli_bytesToNative(public.x, public_key); - vli_bytesToNative(public.y, public_key + uECC_BYTES); - vli_bytesToNative(r, signature); - vli_bytesToNative(s, signature + uECC_BYTES); - - if (vli_isZero(r) || vli_isZero(s)) { /* r, s must not be 0. */ + vli_bytesToNative(public, public_key, curve); + vli_bytesToNative(public + curve->num_words, public_key + curve->num_bytes, curve); + vli_bytesToNative(r, signature, curve); + vli_bytesToNative(s, signature + curve->num_bytes, curve); + + /* r, s must not be 0. */ + if (vli_isZero(r, curve->num_words) || vli_isZero(s, curve->num_words)) { return 0; } -#if (uECC_CURVE != uECC_secp160r1) - if (vli_cmp(curve_n, r) != 1 || vli_cmp(curve_n, s) != 1) { /* r, s must be < n. */ + /* r, s must be < n. */ + if (vli_cmp(curve->n, r, curve->num_n_words) != 1 || + vli_cmp(curve->n, s, curve->num_n_words) != 1) { return 0; } -#endif /* Calculate u1 and u2. */ - vli_modInv_n(z, s, curve_n); /* Z = s^-1 */ - u1[uECC_N_WORDS - 1] = 0; - vli_bytesToNative(u1, hash); - vli_modMult_n(u1, u1, z); /* u1 = e/s */ - vli_modMult_n(u2, r, z); /* u2 = r/s */ - + vli_modInv(z, s, curve->n, curve->num_n_words); /* z = 1/s */ + u1[curve->num_n_words - 1] = 0; + vli_bytesToNative(u1, hash, curve); + vli_modMult(u1, u1, z, curve->n, curve->num_n_words); /* u1 = e/s */ + vli_modMult(u2, r, z, curve->n, curve->num_n_words); /* u2 = r/s */ + /* Calculate sum = G + Q. */ - vli_set(sum.x, public.x); - vli_set(sum.y, public.y); - vli_set(tx, curve_G.x); - vli_set(ty, curve_G.y); - vli_modSub_fast(z, sum.x, tx); /* Z = x2 - x1 */ - XYcZ_add(tx, ty, sum.x, sum.y); - vli_modInv(z, z, curve_p); /* Z = 1/Z */ - apply_z(sum.x, sum.y, z); - + vli_set(sum, public, curve->num_words); + vli_set(sum + curve->num_words, public + curve->num_words, curve->num_words); + vli_set(tx, curve->G, curve->num_words); + vli_set(ty, curve->G + curve->num_words, curve->num_words); + vli_modSub(z, sum, tx, curve->p, curve->num_words); /* z = x2 - x1 */ + XYcZ_add(tx, ty, sum, sum + curve->num_words, curve); + vli_modInv(z, z, curve->p, curve->num_words); /* z = 1/z */ + apply_z(sum, sum + curve->num_words, z, curve); + /* Use Shamir's trick to calculate u1*G + u2*Q */ points[0] = 0; - points[1] = &curve_G; - points[2] = &public; - points[3] = ∑ - numBits = smax(vli_numBits(u1, uECC_N_WORDS), vli_numBits(u2, uECC_N_WORDS)); - - point = points[(!!vli_testBit(u1, numBits - 1)) | ((!!vli_testBit(u2, numBits - 1)) << 1)]; - vli_set(rx, point->x); - vli_set(ry, point->y); - vli_clear(z); + points[1] = curve->G; + points[2] = public; + points[3] = sum; + num_bits = smax(vli_numBits(u1, curve->num_n_words), vli_numBits(u2, curve->num_n_words)); + + point = points[(!!vli_testBit(u1, num_bits - 1)) | ((!!vli_testBit(u2, num_bits - 1)) << 1)]; + vli_set(rx, point, curve->num_words); + vli_set(ry, point + curve->num_words, curve->num_words); + vli_clear(z, curve->num_words); z[0] = 1; - for (i = numBits - 2; i >= 0; --i) { + for (i = num_bits - 2; i >= 0; --i) { uECC_word_t index; - EccPoint_double_jacobian(rx, ry, z); - + curve->double_jacobian(rx, ry, z, curve); + index = (!!vli_testBit(u1, i)) | ((!!vli_testBit(u2, i)) << 1); point = points[index]; if (point) { - vli_set(tx, point->x); - vli_set(ty, point->y); - apply_z(tx, ty, z); - vli_modSub_fast(tz, rx, tx); /* Z = x2 - x1 */ - XYcZ_add(tx, ty, rx, ry); - vli_modMult_fast(z, z, tz); + vli_set(tx, point, curve->num_words); + vli_set(ty, point + curve->num_words, curve->num_words); + apply_z(tx, ty, z, curve); + vli_modSub(tz, rx, tx, curve->p, curve->num_words); /* Z = x2 - x1 */ + XYcZ_add(tx, ty, rx, ry, curve); + vli_modMult(z, z, tz, curve->p, curve->num_words); } } - vli_modInv(z, z, curve_p); /* Z = 1/Z */ - apply_z(rx, ry, z); - + vli_modInv(z, z, curve->p, curve->num_words); /* Z = 1/Z */ + apply_z(rx, ry, z, curve); + /* v = x1 (mod n) */ -#if (uECC_CURVE != uECC_secp160r1) - if (vli_cmp(curve_n, rx) != 1) { - vli_sub(rx, rx, curve_n); + if (vli_cmp(curve->n, rx, curve->num_n_words) != 1) { + vli_sub(rx, rx, curve->n, curve->num_n_words); } -#endif /* Accept only if v == r. */ - return vli_equal(rx, r); + return (vli_cmp(rx, r, curve->num_words) == 0); } diff --git a/uECC.h b/uECC.h index 7864fcb..52fa26a 100644 --- a/uECC.h +++ b/uECC.h @@ -32,39 +32,27 @@ uECC_asm_fast - Use GCC inline assembly optimized for maximum speed. */ #define uECC_ASM uECC_asm_fast #endif -/* Curve selection options. */ -#define uECC_secp160r1 1 -#define uECC_secp192r1 2 -#define uECC_secp256r1 3 -#define uECC_secp256k1 4 -#define uECC_secp224r1 5 -#ifndef uECC_CURVE - #define uECC_CURVE uECC_secp160r1 -#endif - /* uECC_SQUARE_FUNC - If enabled (defined as nonzero), this will cause a specific function to be used for (scalar) squaring instead of the generic multiplication function. This will make things faster by about 8% but increases the code size. */ #ifndef uECC_SQUARE_FUNC - #define uECC_SQUARE_FUNC 1 + #define uECC_SQUARE_FUNC 0 #endif -#define uECC_CONCAT1(a, b) a##b -#define uECC_CONCAT(a, b) uECC_CONCAT1(a, b) - -#define uECC_size_1 20 /* secp160r1 */ -#define uECC_size_2 24 /* secp192r1 */ -#define uECC_size_3 32 /* secp256r1 */ -#define uECC_size_4 32 /* secp256k1 */ -#define uECC_size_5 28 /* secp224r1 */ - -#define uECC_BYTES uECC_CONCAT(uECC_size_, uECC_CURVE) +struct uECC_Curve_t; +typedef const struct uECC_Curve_t * const uECC_Curve; #ifdef __cplusplus extern "C" { #endif +uECC_Curve uECC_secp160r1(void); +uECC_Curve uECC_secp192r1(void); +uECC_Curve uECC_secp224r1(void); +uECC_Curve uECC_secp256r1(void); +uECC_Curve uECC_secp256k1(void); + /* uECC_RNG_Function type The RNG function should fill 'size' random bytes into 'dest'. It should return 1 if 'dest' was filled with random data, or 0 if the random data could not be generated. @@ -104,7 +92,7 @@ Outputs: Returns 1 if the key pair was generated successfully, 0 if an error occurred. */ -int uECC_make_key(uint8_t public_key[uECC_BYTES*2], uint8_t private_key[uECC_BYTES]); +int uECC_make_key(uint8_t *public_key, uint8_t *private_key, uECC_Curve curve); /* uECC_shared_secret() function. Compute a shared secret given your secret key and someone else's public key. @@ -120,9 +108,59 @@ Outputs: Returns 1 if the shared secret was generated successfully, 0 if an error occurred. */ -int uECC_shared_secret(const uint8_t public_key[uECC_BYTES*2], - const uint8_t private_key[uECC_BYTES], - uint8_t secret[uECC_BYTES]); +int uECC_shared_secret(const uint8_t *public_key, + const uint8_t *private_key, + uint8_t *secret, + uECC_Curve curve); + +/* uECC_compress() function. +Compress a public key. + +Inputs: + public_key - The public key to compress. + +Outputs: + compressed - Will be filled in with the compressed public key. +*/ +void uECC_compress(const uint8_t *public_key, uint8_t *compressed, uECC_Curve curve); + +/* uECC_decompress() function. +Decompress a compressed public key. + +Inputs: + compressed - The compressed public key. + +Outputs: + public_key - Will be filled in with the decompressed public key. +*/ +void uECC_decompress(const uint8_t *compressed, uint8_t *public_key, uECC_Curve curve); + +/* uECC_valid_public_key() function. +Check to see if a public key is valid. + +Note that you are not required to check for a valid public key before using any other uECC +functions. However, you may wish to avoid spending CPU time computing a shared secret or +verifying a signature using an invalid public key. + +Inputs: + public_key - The public key to check. + +Returns 1 if the public key is valid, 0 if it is invalid. +*/ +int uECC_valid_public_key(const uint8_t *public_key, uECC_Curve curve); + +/* uECC_compute_public_key() function. +Compute the corresponding public key for a private key. + +Inputs: + private_key - The private key to compute the public key for + +Outputs: + public_key - Will be filled in with the corresponding public key + +Returns 1 if the key was computed successfully, 0 if an error occurred. +*/ +int uECC_compute_public_key(const uint8_t *private_key, uint8_t *public_key, uECC_Curve curve); /* uECC_sign() function. Generate an ECDSA signature for a given hash value. @@ -139,9 +177,10 @@ Outputs: Returns 1 if the signature generated successfully, 0 if an error occurred. */ -int uECC_sign(const uint8_t private_key[uECC_BYTES], - const uint8_t message_hash[uECC_BYTES], - uint8_t signature[uECC_BYTES*2]); +int uECC_sign(const uint8_t *private_key, + const uint8_t *message_hash, + uint8_t *signature, + uECC_Curve curve); /* uECC_HashContext structure. This is used to pass in an arbitrary hash function to uECC_sign_deterministic(). @@ -211,10 +250,11 @@ Outputs: Returns 1 if the signature generated successfully, 0 if an error occurred. */ -int uECC_sign_deterministic(const uint8_t private_key[uECC_BYTES], - const uint8_t message_hash[uECC_BYTES], +int uECC_sign_deterministic(const uint8_t *private_key, + const uint8_t *message_hash, uECC_HashContext *hash_context, - uint8_t signature[uECC_BYTES*2]); + uint8_t *signature, + uECC_Curve curve); /* uECC_verify() function. Verify an ECDSA signature. @@ -229,70 +269,10 @@ Inputs: Returns 1 if the signature is valid, 0 if it is invalid. */ -int uECC_verify(const uint8_t public_key[uECC_BYTES*2], - const uint8_t hash[uECC_BYTES], - const uint8_t signature[uECC_BYTES*2]); - -/* uECC_compress() function. -Compress a public key. - -Inputs: - public_key - The public key to compress. - -Outputs: - compressed - Will be filled in with the compressed public key. -*/ -void uECC_compress(const uint8_t public_key[uECC_BYTES*2], uint8_t compressed[uECC_BYTES+1]); - -/* uECC_decompress() function. -Decompress a compressed public key. - -Inputs: - compressed - The compressed public key. - -Outputs: - public_key - Will be filled in with the decompressed public key. -*/ -void uECC_decompress(const uint8_t compressed[uECC_BYTES+1], uint8_t public_key[uECC_BYTES*2]); - -/* uECC_valid_public_key() function. -Check to see if a public key is valid. - -Note that you are not required to check for a valid public key before using any other uECC -functions. However, you may wish to avoid spending CPU time computing a shared secret or -verifying a signature using an invalid public key. - -Inputs: - public_key - The public key to check. - -Returns 1 if the public key is valid, 0 if it is invalid. -*/ -int uECC_valid_public_key(const uint8_t public_key[uECC_BYTES*2]); - -/* uECC_compute_public_key() function. -Compute the corresponding public key for a private key. - -Inputs: - private_key - The private key to compute the public key for - -Outputs: - public_key - Will be filled in with the corresponding public key - -Returns 1 if the key was computed successfully, 0 if an error occurred. -*/ -int uECC_compute_public_key(const uint8_t private_key[uECC_BYTES], - uint8_t public_key[uECC_BYTES * 2]); - - -/* uECC_bytes() function. -Returns the value of uECC_BYTES. Helpful for foreign-interfaces to higher-level languages. -*/ -int uECC_bytes(void); - -/* uECC_curve() function. -Returns the value of uECC_CURVE. Helpful for foreign-interfaces to higher-level languages. -*/ -int uECC_curve(void); +int uECC_verify(const uint8_t *private_key, + const uint8_t *hash, + const uint8_t *signature, + uECC_Curve curve); #ifdef __cplusplus } /* end of extern "C" */