3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-22 06:29:23 +01:00

unit: updated ecc unit tests to use byte conversion

This commit is contained in:
James Prestwood 2018-05-04 15:11:53 -07:00 committed by Denis Kenzior
parent 2f0c37dae6
commit d810e7ab45

View File

@ -8,10 +8,14 @@
#include "src/ecc.h" #include "src/ecc.h"
#define HEX2BUF(s) ((uint64_t *) l_util_from_hexstring(s, NULL)) #define HEX2BUF(s, buf) { \
unsigned char *tmp = l_util_from_hexstring(s, NULL); \
memcpy(buf, tmp, 32); \
l_free(tmp); \
}
#define CURVE_P_32_STR "FFFFFFFF000000010000000000000000"\ #define CURVE_P_32_STR "ffffffffffffffffffffffff00000000"\
"00000000FFFFFFFFFFFFFFFFFFFFFFFF" "000000000000000001000000ffffffff"
enum ecc_test_type { enum ecc_test_type {
TEST_ADD = 0, TEST_ADD = 0,
@ -40,194 +44,197 @@ struct ecc_test_data {
/* (a + b) mod c */ /* (a + b) mod c */
struct ecc_test_data add_test = { struct ecc_test_data add_test = {
.type = TEST_ADD, .type = TEST_ADD,
.a = "a86c9f9e8694ffebbee433936784c0edacebd4725a95fd734098444362d5e1ca", .a = "cae1d5624344984073fd955a72d4ebacedc084679333e4beebff94869e9f6ca8",
.b = "6184c8ce87b6ccd1de1da88ffa79b2257893994cea3fbf338ae3159de82aa093", .b = "93a02ae89d15e38a33bf3fea4c99937825b279fa8fa81dded1ccb687cec88461",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "0af1676d0d4bccbc9d02dc2262fe7213" .result = "5e82004ae05a7bcaa7bcd545bf6e7f25"
"257f6ebf45d5bca7ca7b5ae04a00825e" "1372fe6222dc029dbccc4b0d6d67f10a"
}; };
/* (a - b) mod c */ /* (a - b) mod c */
struct ecc_test_data sub_test = { struct ecc_test_data sub_test = {
.type = TEST_SUB, .type = TEST_SUB,
.a = "a86c9f9e8694ffebbee433936784c0edacebd4725a95fd734098444362d5e1ca", .a = "cae1d5624344984073fd955a72d4ebacedc084679333e4beebff94869e9f6ca8",
.b = "6184c8ce87b6ccd1de1da88ffa79b2257893994cea3fbf338ae3159de82aa093", .b = "93a02ae89d15e38a33bf3fea4c99937825b279fa8fa81dded1ccb687cec88461",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "47e8d6cffedd321ae0c68b036d0a0ec8" .result = "3741aa79a62eb4b6403e5570263b5834"
"34583b2670553e40b6b42ea679aa4137" "c80e0a6d038bc6e01a32ddfecfd6e847"
}; };
/* (a * b) mod c */ /* (a * b) mod c */
struct ecc_test_data mult_test = { struct ecc_test_data mult_test = {
.type = TEST_MULT, .type = TEST_MULT,
.a = "a86c9f9e8694ffebbee433936784c0edacebd4725a95fd734098444362d5e1ca", .a = "cae1d5624344984073fd955a72d4ebacedc084679333e4beebff94869e9f6ca8",
.b = "6184c8ce87b6ccd1de1da88ffa79b2257893994cea3fbf338ae3159de82aa093", .b = "93a02ae89d15e38a33bf3fea4c99937825b279fa8fa81dded1ccb687cec88461",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "b5aa2ffc5ea754a6f62097c1282e072c" .result = "a31ff5c7d65d8bd806b0407f27d1f1bc"
"bcf1d1277f40b006d88b5dd6c7f51fa3" "2c072e28c19720f6a654a75efc2faab5"
}; };
/* (a^-1) mod c */ /* (a^-1) mod c */
struct ecc_test_data inv_test = { struct ecc_test_data inv_test = {
.type = TEST_INV, .type = TEST_INV,
.a = "a86c9f9e8694ffebbee433936784c0edacebd4725a95fd734098444362d5e1ca", .a = "cae1d5624344984073fd955a72d4ebacedc084679333e4beebff94869e9f6ca8",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "c50ba653449ad70fb17ae85567983c63" .result = "48faaac115571047ead565911fc334fd"
"fd34c31f9165d5ea47105715c1aafa48" "633c986755e87ab10fd79a4453a60bc5"
}; };
/* (a^-1) mod c */ /* (a^-1) mod c */
struct ecc_test_data inv_test2 = { struct ecc_test_data inv_test2 = {
.type = TEST_INV, .type = TEST_INV,
.a = "184423e8cda58cfaf8c03af9da31bb9f5c2f4d7f3f0b72a9799c3ab6105c8e69", .a = "698e5c10b63a9c79a9720b3f7f4d2f5c9fbb31daf93ac0f8fa8ca5cde8234418",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "2bb4d9671dfcb7db5e34478a8a70a0c5" .result = "5fd113c3b6053c38e54e5917826c8520"
"20856c8217594ee5383c05b6c313d15f" "c5a0708a8a47345edbb7fc1d67d9b42b"
}; };
/* (a ^ b) mod c */ /* (a ^ b) mod c */
struct ecc_test_data exp_test = { struct ecc_test_data exp_test = {
.type = TEST_EXP, .type = TEST_EXP,
.a = "a86c9f9e8694ffebbee433936784c0edacebd4725a95fd734098444362d5e1ca", .a = "cae1d5624344984073fd955a72d4ebacedc084679333e4beebff94869e9f6ca8",
.b = "6184c8ce87b6ccd1de1da88ffa79b2257893994cea3fbf338ae3159de82aa093", .b = "93a02ae89d15e38a33bf3fea4c99937825b279fa8fa81dded1ccb687cec88461",
.mod = CURVE_P_32_STR, .mod = CURVE_P_32_STR,
.result = "5a4d81b5ffbf089e79958ce1f80d96b4" .result = "415b2e00b2dfd0bf4889a64398c0fe6f"
"6ffec09843a68948bfd0dfb2002e5b41" "b4960df8e18c95799e08bfffb5814d5a"
}; };
struct ecc_test_data point_add_test = { struct ecc_test_data point_add_test = {
.type = TEST_POINT_ADD, .type = TEST_POINT_ADD,
.ax = "df5294c307b02dd667e49dfdd6c6a24f" .ax = "d36b6768a3279fbe23a5bf5cc19b13354"
"35139bc15cbfa523be9f27a368676bd3", "fa2c6d6fd9de467d62db007c39452df",
.ay = "c480972a157533f44c0f2fefb0dd184c" .ay = "4d601e7be3efd7f357452de7584274c54"
"c5744258e72d4557f3d7efe37b1e604d", "c18ddb0ef2f0f4cf43375152a9780c4",
.bx = "e0296a73bd051756ceb6410c1c8980f0" .bx = "c833c5d3ab916ed37f16597ace5dcf41f"
"41cf5dce7a59167fd36e91abd3c533c8", "080891c0c41b6ce561705bd736a29e0",
.by = "4aa0404d653adde7e6b8a71005df3351" .by = "9d266e5ba8ba3e8d9679238f44a376b05"
"b076a3448f2379968d3ebaa85b6e269d", "133df0510a7b8e6e7dd3a654d40a04a",
.mod = CURVE_P_32_STR, .rx = "24c4ede340dbdd144ccaaea67e5b1fca"
.rx = "01315318631812cd4f1111dc26aab387c" "87b3aa26dc11114fcd12186318533101",
"a1f5b7ea6aeca4c14dddb40e3edc424", .ry = "1d96391fb2942bf286e9251c257b960e"
.ry = "14556f1f7ef8af98c86f4bffcad4237d0" "7d23d4caff4b6fc898aff87e1f6f5514"
"e967b251c25e986f22b94b21f39961d"
}; };
struct ecc_test_data point_mult_test = { struct ecc_test_data point_mult_test = {
.type = TEST_SCALAR_MULT, .type = TEST_SCALAR_MULT,
.ax = "67ac46c3e9e10ba9262c76065314c7bf6" .ax = "768bc2f17fbf4e49282fbd4068994562b"
"245996840bd2f28494ebf7ff1c28b76", "fc7145306762c26a90be1e9c346ac67",
.ay = "6184c8ce87b6ccd1de1da88ffa79b2257" .ay = "93a02ae89d15e38a33bf3fea4c9993782"
"893994cea3fbf338ae3159de82aa093", "5b279fa8fa81dded1ccb687cec88461",
.scalar = "8b0532ad431bd6701f34aa8eac6c829c" .scalar = "7521d940aa073c1675114ed27b866561"
"6165867bd24e1175163c07aa40d92175", "9c826cac8eaa341f70d61b43ad32058b",
.mod = CURVE_P_32_STR, .rx = "d4c80de349966df5542c984e80885d36"
.rx = "66feed4d184383dc0f6afe4fb7ce65a93" "a965ceb74ffe6a0fdc8343184dedfe66",
"65d88804e982c54f56d9649e30dc8d4", .ry = "6d3a1ac3d1d392413286a0e00e94b01e"
.ry = "3b0f88a5c3c97fcc399d3bf5c72384ae1" "ae8423c7f53b9d39cc7fc9c3a5880f3b"
"eb0940ee0a086324192d3d1c31a3a6d"
}; };
static void run_test(const void *arg) static void run_test(const void *arg)
{ {
const struct ecc_test_data *data = arg; const struct ecc_test_data *data = arg;
uint64_t *a = NULL, *b = NULL, *c = NULL, *d = NULL, *mod = NULL; uint64_t a[NUM_ECC_DIGITS], b[NUM_ECC_DIGITS], mod[NUM_ECC_DIGITS],
uint64_t result[NUM_ECC_DIGITS]; scalar[NUM_ECC_DIGITS], result[NUM_ECC_DIGITS],
uint64_t rx[NUM_ECC_DIGITS] = { 0 }, ry[NUM_ECC_DIGITS] = { 0 }; check[NUM_ECC_DIGITS];
uint64_t *check = NULL; struct ecc_point point1, point2, point_ret;
struct ecc_point point1;
struct ecc_point point2;
struct ecc_point point_ret;
memset(result, 0, sizeof(result)); memset(result, 0, sizeof(result));
if (data->a) {
HEX2BUF(data->a, a);
ecc_be2native(a);
}
if (data->b) {
HEX2BUF(data->b, b);
ecc_be2native(b);
}
if (data->mod) {
HEX2BUF(data->mod, mod);
ecc_be2native(mod);
}
if (data->ax) {
HEX2BUF(data->ax, point1.x);
ecc_be2native(point1.x);
}
if (data->ay) {
HEX2BUF(data->ay, point1.y);
ecc_be2native(point1.y);
}
if (data->bx) {
HEX2BUF(data->bx, point2.x);
ecc_be2native(point2.x);
}
if (data->by) {
HEX2BUF(data->by, point2.y);
ecc_be2native(point2.y);
}
if (data->scalar) {
HEX2BUF(data->scalar, scalar);
ecc_be2native(scalar);
}
switch (data->type) { switch (data->type) {
case TEST_ADD: case TEST_ADD:
a = HEX2BUF(data->a);
b = HEX2BUF(data->b);
mod = HEX2BUF(data->mod);
vli_mod_add(result, a, b, mod); vli_mod_add(result, a, b, mod);
break; break;
case TEST_SUB: case TEST_SUB:
a = HEX2BUF(data->a);
b = HEX2BUF(data->b);
mod = HEX2BUF(data->mod);
vli_mod_sub(result, a, b, mod); vli_mod_sub(result, a, b, mod);
break; break;
case TEST_MULT: case TEST_MULT:
a = HEX2BUF(data->a);
b = HEX2BUF(data->b);
mod = HEX2BUF(data->mod);
vli_mod_mult_fast(result, a, b); vli_mod_mult_fast(result, a, b);
break; break;
case TEST_INV: case TEST_INV:
a = HEX2BUF(data->a);
mod = HEX2BUF(data->mod);
vli_mod_inv(result, a, mod); vli_mod_inv(result, a, mod);
break; break;
case TEST_EXP: case TEST_EXP:
a = HEX2BUF(data->a);
b = HEX2BUF(data->b);
mod = HEX2BUF(data->mod);
vli_mod_exp(result, a, b, mod); vli_mod_exp(result, a, b, mod);
break; break;
case TEST_POINT_ADD: case TEST_POINT_ADD:
a = HEX2BUF(data->ax);
b = HEX2BUF(data->ay);
c = HEX2BUF(data->bx);
d = HEX2BUF(data->by);
memcpy(point1.x, a, 32);
memcpy(point1.y, b, 32);
memcpy(point2.x, c, 32);
memcpy(point2.y, d, 32);
assert(ecc_valid_point(&point1) == true); assert(ecc_valid_point(&point1) == true);
assert(ecc_valid_point(&point2) == true); assert(ecc_valid_point(&point2) == true);
mod = HEX2BUF(data->mod);
memcpy(rx, a, 32);
memcpy(ry, b, 32);
ecc_point_add(&point_ret, &point1, &point2); ecc_point_add(&point_ret, &point1, &point2);
break; break;
case TEST_SCALAR_MULT: case TEST_SCALAR_MULT:
a = HEX2BUF(data->ax);
b = HEX2BUF(data->ay);
c = HEX2BUF(data->scalar);
mod = HEX2BUF(data->mod);
memcpy(point1.x, a, 32);
memcpy(point1.y, b, 32);
assert(ecc_valid_point(&point1) == true); assert(ecc_valid_point(&point1) == true);
ecc_point_mult(&point_ret, &point1, c, NULL, vli_num_bits(c)); ecc_point_mult(&point_ret, &point1, scalar, NULL,
vli_num_bits(scalar));
break; break;
} }
if (data->type <= TEST_EXP) { if (data->type <= TEST_EXP) {
check = HEX2BUF(data->result); HEX2BUF(data->result, check);
ecc_native2be(check);
assert(memcmp(result, check, 32) == 0); assert(memcmp(result, check, 32) == 0);
} else { } else {
uint64_t *checkx = HEX2BUF(data->rx); uint64_t checkx[NUM_ECC_DIGITS];
uint64_t *checky = HEX2BUF(data->ry); uint64_t checky[NUM_ECC_DIGITS];
HEX2BUF(data->rx, checkx);
ecc_native2be(checkx);
HEX2BUF(data->ry, checky);
ecc_native2be(checky);
assert(memcmp(checkx, point_ret.x, 32) == 0); assert(memcmp(checkx, point_ret.x, 32) == 0);
assert(memcmp(checky, point_ret.y, 32) == 0); assert(memcmp(checky, point_ret.y, 32) == 0);
assert(ecc_valid_point(&point_ret) == true); assert(ecc_valid_point(&point_ret) == true);
l_free(checkx);
l_free(checky);
} }
l_free(a);
l_free(b);
l_free(c);
l_free(d);
l_free(mod);
l_free(check);
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])