Fixed endianness in Curve25519 implementation (no consequence on security). Also added new Curve25519 code for 64-bit platforms.

This commit is contained in:
Thomas Pornin 2018-12-08 21:04:07 +01:00
parent fd98320c82
commit 52a69fe3de
10 changed files with 708 additions and 27 deletions

View file

@ -8570,7 +8570,7 @@ test_EC_p256_m31(void)
}
const struct {
const char *scalar;
const char *scalar_le;
const char *u_in;
const char *u_out;
} C25519_KAT[] = {
@ -8583,6 +8583,20 @@ const struct {
{ 0, 0, 0 }
};
static void
revbytes(unsigned char *buf, size_t len)
{
size_t u;
for (u = 0; u < (len >> 1); u ++) {
unsigned t;
t = buf[u];
buf[u] = buf[len - 1 - u];
buf[len - 1 - u] = t;
}
}
static void
test_EC_c25519(const char *name, const br_ec_impl *iec)
{
@ -8592,8 +8606,9 @@ test_EC_c25519(const char *name, const br_ec_impl *iec)
printf("Test %s: ", name);
fflush(stdout);
for (v = 0; C25519_KAT[v].scalar; v ++) {
hextobin(bk, C25519_KAT[v].scalar);
for (v = 0; C25519_KAT[v].scalar_le; v ++) {
hextobin(bk, C25519_KAT[v].scalar_le);
revbytes(bk, sizeof bk);
hextobin(bu, C25519_KAT[v].u_in);
hextobin(br, C25519_KAT[v].u_out);
if (!iec->mul(bu, sizeof bu, bk, sizeof bk, BR_EC_curve25519)) {
@ -8614,11 +8629,13 @@ test_EC_c25519(const char *name, const br_ec_impl *iec)
bu[0] = 0x09;
memcpy(bk, bu, sizeof bu);
for (i = 1; i <= 1000; i ++) {
revbytes(bk, sizeof bk);
if (!iec->mul(bu, sizeof bu, bk, sizeof bk, BR_EC_curve25519)) {
fprintf(stderr, "Curve25519 multiplication failed"
" (iter=%d)\n", i);
exit(EXIT_FAILURE);
}
revbytes(bk, sizeof bk);
for (v = 0; v < sizeof bu; v ++) {
unsigned t;
@ -8681,6 +8698,22 @@ test_EC_c25519_m31(void)
(uint32_t)1 << BR_EC_curve25519);
}
static void
test_EC_c25519_m62(void)
{
const br_ec_impl *ec;
ec = br_ec_c25519_m62_get();
if (ec != NULL) {
test_EC_c25519("EC_c25519_m62", ec);
test_EC_keygen("EC_c25519_m62", ec,
(uint32_t)1 << BR_EC_curve25519);
} else {
printf("Test EC_c25519_m62: UNAVAILABLE\n");
printf("Test EC_c25519_m62 keygen: UNAVAILABLE\n");
}
}
static const unsigned char EC_P256_PUB_POINT[] = {
0x04, 0x60, 0xFE, 0xD4, 0xBA, 0x25, 0x5A, 0x9D,
0x31, 0xC9, 0x61, 0xEB, 0x74, 0xC6, 0x35, 0x6D,
@ -9352,6 +9385,7 @@ static const struct {
STU(EC_c25519_i31),
STU(EC_c25519_m15),
STU(EC_c25519_m31),
STU(EC_c25519_m62),
STU(ECDSA_i15),
STU(ECDSA_i31),
STU(modpow_i31),

View file

@ -1088,6 +1088,19 @@ test_speed_ec_c25519_m31(void)
&br_ec_c25519_m31, &br_curve25519);
}
static void
test_speed_ec_c25519_m62(void)
{
const br_ec_impl *ec;
ec = br_ec_c25519_m62_get();
if (ec != NULL) {
test_speed_ec_inner("EC c25519_m62", ec, &br_curve25519);
} else {
printf("%-30s UNAVAILABLE\n", "EC c25519_m62");
}
}
static void
test_speed_ecdsa_inner(const char *name,
const br_ec_impl *impl, const br_ec_curve_def *cd,
@ -1606,6 +1619,7 @@ static const struct {
STU(ec_c25519_i31),
STU(ec_c25519_m15),
STU(ec_c25519_m31),
STU(ec_c25519_m62),
STU(ecdsa_p256_m15),
STU(ecdsa_p256_m31),
STU(ecdsa_i15),