From b7e682e677f5764e2f0d8c1111956d3cae6a62aa Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Mon, 29 Jun 2020 09:30:17 -0700 Subject: [PATCH 1/6] Added more tests to api.c for curve448 --- tests/api.c | 316 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 313 insertions(+), 3 deletions(-) diff --git a/tests/api.c b/tests/api.c index 04c279ec6..e0b1b56e3 100644 --- a/tests/api.c +++ b/tests/api.c @@ -16654,6 +16654,7 @@ static int test_wc_curve448_init (void) return ret; } /* END test_wc_curve448_init and wc_curve_448_free*/ + /* * Testing wc_curve448_make_key */ @@ -16720,10 +16721,235 @@ static int test_wc_curve448_make_key (void) #endif return ret; } /*END test_wc_curve448_make_key*/ +/* + * Testing test_wc_curve448_shared_secret_ex + */ +static int test_wc_curve448_shared_secret_ex (void) //ethan-3 +{ + int ret = 0; +#if defined(HAVE_CURVE448) + WC_RNG rng; + curve448_key private_key, public_key; + byte out[CURVE448_KEY_SIZE]; + word32 outLen = sizeof(out); + int endian = EC448_BIG_ENDIAN; + + printf(testingFmt, "wc_curve448_shared_secret_ex()"); + + ret = wc_curve448_init(&private_key); + if (ret == 0) { + ret = wc_InitRng(&rng); + if (ret == 0){ + ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key); + } + if (wc_FreeRng(&rng) && ret == 0) { + ret = WOLFSSL_FATAL_ERROR; + } + } + ret = wc_curve448_init(&public_key); + if (ret == 0) { + ret = wc_InitRng(&rng); + if (ret == 0){ + ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key); + } + } + + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out, + &outLen, endian); + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, + 0, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(NULL, &public_key, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(&private_key, NULL, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out, + NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve448_free(&private_key); + wc_curve448_free(&public_key); + wc_FreeRng(&rng); +#endif + return ret; +} /*END test_wc_curve448_shared_secret_ex*/ + + +/* + * Testing test_wc_curve448_export_public_ex + */ +static int test_wc_curve448_export_public_ex (void) +{ + int ret = 0; +#if defined(HAVE_CURVE448) + + WC_RNG rng; + curve448_key key; + byte out[CURVE448_KEY_SIZE]; + word32 outLen = sizeof(out); + int endian = EC448_BIG_ENDIAN; + + printf(testingFmt, "wc_curve448_export_public_ex()"); + + ret = wc_curve448_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key); + if (ret == 0){ + ret = wc_curve448_export_public(&key, out, &outLen); + } + if (ret == 0) { + ret = wc_curve448_export_public_ex(&key, out, &outLen, endian); + } + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_public_ex(&key, out, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve448_export_public_ex(&key, out, &outLen, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve448_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +} /*END test_wc_curve448_export_public_ex*/ +/* + * Testing test_wc_curve448_export_private_raw_ex + */ +static int test_wc_curve448_export_private_raw_ex (void) +{ + + int ret = 0; +#if defined(HAVE_CURVE448) + + WC_RNG rng; + curve448_key key; + byte out[CURVE448_KEY_SIZE]; + word32 outLen = sizeof(out); + int endian = EC448_BIG_ENDIAN; + + printf(testingFmt, "wc_curve448_export_private_raw_ex()"); + + ret = wc_curve448_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian); + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve448_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +}/*END test_wc_curve448_export_private_raw_ex*/ /* * Testing test_wc_curve448_import_private_raw_ex */ -static int test_wc_curve448_import_private_raw_ex(void) +static int test_wc_curve448_import_private_raw_ex (void) { int ret = 0; #if defined(HAVE_CURVE448) @@ -16808,6 +17034,85 @@ static int test_wc_curve448_import_private_raw_ex(void) #endif return ret; } /*END test_wc_curve448_import_private_raw_ex*/ +/* + * Testing test_curve448_export_key_raw + */ +static int test_wc_curve448_export_key_raw (void) +{ + int ret = 0; +#if defined(HAVE_CURVE448) + WC_RNG rng; + curve448_key key; + byte priv[CURVE448_KEY_SIZE]; + byte pub[CURVE448_KEY_SIZE]; + word32 privSz = sizeof(priv); + word32 pubSz = sizeof(pub); + + printf(testingFmt, "wc_curve448_export_key_raw()"); + + ret = wc_curve448_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key); + if (ret == 0){ + ret = wc_curve448_export_private_raw(&key, priv, &privSz); + } + if (ret == 0){ + ret = wc_curve448_export_public(&key, pub, &pubSz); + } + if (ret == 0) { + ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz); + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve448_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +}/*END test_wc_curve448_import_private_raw_ex*/ + + +/* + * Testing test_wc_curve448_import_private + */ +static int test_wc_curve448_import_private (void) +{ + int ret = 0; +#if defined(HAVE_CURVE448) + + curve448_key key; + WC_RNG rng; + byte priv[CURVE448_KEY_SIZE]; + word32 privSz = sizeof(priv); + + printf(testingFmt, "wc_curve448_import_private()"); + + ret = wc_curve448_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key); + if (ret == 0){ + ret = wc_curve448_export_private_raw(&key, priv, &privSz); + } + } + if (ret == 0){ + ret = wc_curve448_import_private(priv, privSz, &key); + } + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve448_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +} /*END test_wc_curve448_import*/ /* * Testing test_wc_curve448_size. */ @@ -33503,10 +33808,15 @@ void ApiTest(void) AssertIntEQ(test_wc_ed448_size(), 0); AssertIntEQ(test_wc_ed448_exportKey(), 0); AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0); + AssertIntEQ(test_wc_curve448_make_key (), 0); + AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0); + AssertIntEQ(test_wc_curve448_export_public_ex (), 0); + AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0); + AssertIntEQ(test_wc_curve448_export_key_raw (), 0); + AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0); + AssertIntEQ(test_wc_curve448_import_private (), 0); AssertIntEQ(test_wc_curve448_init(), 0); AssertIntEQ(test_wc_curve448_size (), 0); - AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0); - AssertIntEQ(test_wc_curve448_make_key (), 0); AssertIntEQ(test_wc_ecc_make_key(), 0); AssertIntEQ(test_wc_ecc_init(), 0); AssertIntEQ(test_wc_ecc_check_key(), 0); From 78efb48acfa65072f26c26674c27f782f80084c3 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Mon, 29 Jun 2020 09:48:22 -0700 Subject: [PATCH 2/6] Added two more tests to hit xmemset lines --- tests/api.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tests/api.c b/tests/api.c index e0b1b56e3..16d8c954d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -16931,6 +16931,10 @@ static int test_wc_curve448_export_private_raw_ex (void) ret = 0; } } + if (ret == 0) { + ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, + EC448_LITTLE_ENDIAN); + } outLen = outLen - 2; if (ret == 0) { ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian); @@ -16978,7 +16982,7 @@ static int test_wc_curve448_import_private_raw_ex (void) } if (ret == 0) { ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, - &key, endian); + &key, endian); } } /*test bad cases*/ @@ -17004,25 +17008,30 @@ static int test_wc_curve448_import_private_raw_ex (void) } if (ret == 0) { ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, - NULL, endian); + NULL, endian); if (ret == BAD_FUNC_ARG) { ret = 0; } } if (ret == 0) { ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz, - &key, endian); + &key, endian); if (ret == ECC_BAD_ARG_E) { ret = 0; } } if (ret == 0) { ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0, - &key, endian); + &key, endian); if (ret == ECC_BAD_ARG_E) { ret = 0; } } + if (ret == 0) { + ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz, + &key, EC448_LITTLE_ENDIAN); + + } if (wc_FreeRng(&rng) != 0 && ret == 0) { ret = WOLFSSL_FATAL_ERROR; From 4ad904909c436909a87567107d7365e4a0e0c2a1 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Mon, 29 Jun 2020 12:55:42 -0700 Subject: [PATCH 3/6] Added a return check --- tests/api.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/api.c b/tests/api.c index 16d8c954d..e65490740 100644 --- a/tests/api.c +++ b/tests/api.c @@ -16746,7 +16746,9 @@ static int test_wc_curve448_shared_secret_ex (void) //ethan-3 ret = WOLFSSL_FATAL_ERROR; } } - ret = wc_curve448_init(&public_key); + if (ret == 0){ + ret = wc_curve448_init(&public_key); + } if (ret == 0) { ret = wc_InitRng(&rng); if (ret == 0){ From a59560a1d5555f50a1d91dd9f153ecb2bc1c7ae6 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 1 Jul 2020 09:32:03 -0700 Subject: [PATCH 4/6] Added tests to curve25519.c and fixed a print error from previous curve25519 tests --- tests/api.c | 450 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 436 insertions(+), 14 deletions(-) diff --git a/tests/api.c b/tests/api.c index e65490740..1eb34dcba 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15362,6 +15362,429 @@ static int test_wc_curve25519_init (void) return ret; } /* END test_wc_curve25519_init and wc_curve_25519_free*/ +/* + * Testing wc_curve25519_make_key + */ +static int test_wc_curve25519_make_key (void) +{ + int ret = 0; +#if defined(HAVE_CURVE25519) + WC_RNG rng; + curve25519_key key; + int keysize; + + + printf(testingFmt, "wc_curve25519_make_key()"); + + ret = wc_curve25519_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key); + if (ret == 0) { + keysize = wc_curve25519_size(&key); + if (keysize != CURVE25519_KEYSIZE) { + ret = SSL_FATAL_ERROR; + } + } + if (ret == 0) { + ret = wc_curve25519_make_key(&rng, keysize, &key); + } + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve25519_make_key(NULL, 0, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_make_key(&rng, keysize, NULL); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_make_key(NULL, keysize, &key); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_make_key(&rng, 0, &key); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); + wc_FreeRng(&rng); +#endif + return ret; +} /*END test_wc_curve25519_make_key*/ +/* + * Testing wc_curve25519_shared_secret_ex + */ +static int test_wc_curve25519_shared_secret_ex (void) +{ + int ret = 0; +#if defined(HAVE_CURVE25519) + WC_RNG rng; + curve25519_key private_key, public_key; + byte out[CURVE25519_KEYSIZE]; + word32 outLen = sizeof(out); + int endian = EC25519_BIG_ENDIAN; + + + printf(testingFmt, "wc_curve25519_shared_secret_ex()"); + + ret = wc_curve25519_init(&private_key); + if (ret == 0) { + ret = wc_InitRng(&rng); + if (ret == 0) { + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key); + } + } + if (ret == 0) { + if (ret == 0) { + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key); + } + } + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, + &outLen, endian); + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL, + 0, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, + NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + + if (ret == 0) { + public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F; + ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, + &outLen, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, + &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&private_key); + wc_curve25519_free(&public_key); + wc_FreeRng(&rng); +#endif + return ret; +} /*END test_wc_curve25519_shared_secret_ex*/ +/* + * Testing test_wc_curve25519_export_public_ex + */ +static int test_wc_curve25519_export_public_ex (void) +{ + int ret = 0; +#if defined(HAVE_CURVE25519) + + WC_RNG rng; + curve25519_key key; + byte out[CURVE25519_KEYSIZE]; + word32 outLen = sizeof(out); + int endian = EC25519_BIG_ENDIAN; + + printf(testingFmt, "wc_curve25519_export_public_ex()"); + + ret = wc_curve25519_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key); + if (ret == 0){ + ret = wc_curve25519_export_public(&key, out, &outLen); + } + if (ret == 0) { + ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian); + } + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_public_ex(&key, out, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +} /*END test_wc_curve25519_export_public_ex*/ +/* + * Testing test_wc_curve25519_import_private_raw_ex + */ +static int test_wc_curve25519_import_private_raw_ex (void) +{ + int ret = 0; +#if defined(HAVE_CURVE25519) + WC_RNG rng; + curve25519_key key; + byte priv[CURVE25519_KEYSIZE]; + byte pub[CURVE25519_KEYSIZE]; + word32 privSz = sizeof(priv); + word32 pubSz = sizeof(pub); + int endian = EC25519_BIG_ENDIAN; + + + printf(testingFmt, "wc_curve25519_import_private_raw_ex()"); + + ret = wc_curve25519_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key); + if (ret == 0){ + ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian); + } + if (ret == 0){ + ret = wc_curve25519_export_public(&key, pub, &pubSz); + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz, + &key, endian); + } + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL, + endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz, + &key, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz, + &key, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz, + NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz, + &key, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0, + &key, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz, + &key, EC25519_LITTLE_ENDIAN); + + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + +#endif + return ret; +} /*END test_wc_curve25519_import_private_raw_ex*/ +/* + * Testing test_wc_curve25519_import_private + */ +static int test_wc_curve25519_import_private (void) +{ + int ret = 0; +#if defined(HAVE_CURVE25519) + + curve25519_key key; + WC_RNG rng; + byte priv[CURVE25519_KEYSIZE]; + word32 privSz = sizeof(priv); + + printf(testingFmt, "wc_curve25519_import_private()"); + + ret = wc_curve25519_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key); + if (ret == 0){ + ret = wc_curve25519_export_private_raw(&key, priv, &privSz); + } + } + if (ret == 0){ + ret = wc_curve25519_import_private(priv, privSz, &key); + } + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +} /*END test_wc_curve25519_import*/ +/* + * Testing test_wc_curve25519_export_private_raw_ex + */ +static int test_wc_curve25519_export_private_raw_ex (void) +{ + + int ret = 0; +#if defined(HAVE_CURVE25519) + + WC_RNG rng; + curve25519_key key; + byte out[CURVE25519_KEYSIZE]; + word32 outLen = sizeof(out); + int endian = EC25519_BIG_ENDIAN; + + printf(testingFmt, "wc_curve25519_export_private_raw_ex()"); + + ret = wc_curve25519_init(&key); + if (ret == 0) { + ret = wc_InitRng(&rng); + } + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian); + } + /*test bad cases*/ + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian); + if (ret == BAD_FUNC_ARG) { + ret = 0; + } + } + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, + EC25519_LITTLE_ENDIAN); + } + outLen = outLen - 2; + if (ret == 0) { + ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian); + if (ret == ECC_BAD_ARG_E) { + ret = 0; + } + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); + wc_FreeRng(&rng); +#endif + return ret; + +}/*END test_wc_curve25519_export_private_raw_ex*/ /* * Testing test_wc_curve25519_size. */ @@ -15386,7 +15809,6 @@ static int test_wc_curve25519_size (void) if (ret != 0) { ret = wc_curve25519_size(NULL); } - printf(resultFmt, ret == 0 ? passed : failed); wc_curve25519_free(&key); #endif @@ -15562,7 +15984,7 @@ static int test_wc_curve25519_export_key_raw (void) if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) && 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){ - printf(testingFmt,"passed"); + printf(resultFmt, passed ); fflush( stdout ); wc_curve25519_free(&key); wc_FreeRng(&rng); @@ -15587,8 +16009,7 @@ static int test_wc_curve25519_export_key_raw (void) return 1; } -#endif - printf(resultFmt, passed ); +#endif fflush( stdout ); return 0; @@ -16724,7 +17145,7 @@ static int test_wc_curve448_make_key (void) /* * Testing test_wc_curve448_shared_secret_ex */ -static int test_wc_curve448_shared_secret_ex (void) //ethan-3 +static int test_wc_curve448_shared_secret_ex (void) { int ret = 0; #if defined(HAVE_CURVE448) @@ -16741,16 +17162,12 @@ static int test_wc_curve448_shared_secret_ex (void) //ethan-3 ret = wc_InitRng(&rng); if (ret == 0){ ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key); - } - if (wc_FreeRng(&rng) && ret == 0) { - ret = WOLFSSL_FATAL_ERROR; } } if (ret == 0){ ret = wc_curve448_init(&public_key); } if (ret == 0) { - ret = wc_InitRng(&rng); if (ret == 0){ ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key); } @@ -16789,7 +17206,7 @@ static int test_wc_curve448_shared_secret_ex (void) //ethan-3 ret = 0; } } - if (ret == 0) { + if (ret == 0) { ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out, NULL, endian); if (ret == BAD_FUNC_ARG) { @@ -17061,7 +17478,7 @@ static int test_wc_curve448_export_key_raw (void) printf(testingFmt, "wc_curve448_export_key_raw()"); - ret = wc_curve448_init(&key); + ret = wc_curve448_init(&key); if (ret == 0) { ret = wc_InitRng(&rng); } @@ -17085,9 +17502,7 @@ static int test_wc_curve448_export_key_raw (void) #endif return ret; -}/*END test_wc_curve448_import_private_raw_ex*/ - - +}/*END test_wc_curve448_export_key_raw*/ /* * Testing test_wc_curve448_import_private */ @@ -33810,6 +34225,13 @@ void ApiTest(void) AssertIntEQ(test_wc_curve25519_size(), 0); AssertIntEQ(test_wc_curve25519_export_key_raw(), 0); AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0); + AssertIntEQ(test_wc_curve25519_size (), 0); + AssertIntEQ(test_wc_curve25519_make_key (), 0); + AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0); + AssertIntEQ(test_wc_curve25519_export_public_ex (), 0); + AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0); + AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0); + AssertIntEQ(test_wc_curve25519_import_private (), 0); AssertIntEQ(test_wc_ed448_make_key(), 0); AssertIntEQ(test_wc_ed448_init(), 0); AssertIntEQ(test_wc_ed448_sign_msg(), 0); From f526a1112642dafcc9b3094da84d2a773bf84e0f Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 1 Jul 2020 10:19:40 -0700 Subject: [PATCH 5/6] Added additional tests for curve25519 and fixed a print format error from previous tests --- tests/api.c | 1133 ++++++++++++++++++++++++++------------------------- 1 file changed, 567 insertions(+), 566 deletions(-) diff --git a/tests/api.c b/tests/api.c index 1eb34dcba..2714b371d 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15362,6 +15362,568 @@ static int test_wc_curve25519_init (void) return ret; } /* END test_wc_curve25519_init and wc_curve_25519_free*/ +/* + * Testing test_wc_curve25519_size. + */ +static int test_wc_curve25519_size (void) +{ + int ret = 0; + +#if defined(HAVE_CURVE25519) + + curve25519_key key; + + printf(testingFmt, "wc_curve25519_size()"); + + ret = wc_curve25519_init(&key); + + /* Test good args for wc_curve25519_size */ + if (ret == 0) { + ret = wc_curve25519_size(&key); + } + + /* Test bad args for wc_curve25519_size */ + if (ret != 0) { + ret = wc_curve25519_size(NULL); + } + + printf(resultFmt, ret == 0 ? passed : failed); + wc_curve25519_free(&key); +#endif + return ret; + +} /* END test_wc_curve25519_size*/ + +/* + * Testing test_wc_curve25519_export_key_raw(). + */ +static int test_wc_curve25519_export_key_raw (void) +{ + +#if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT) + + curve25519_key key; + WC_RNG rng; + + byte privateKey[CURVE25519_KEYSIZE]; + byte publicKey[CURVE25519_KEYSIZE]; + word32 prvkSz; + word32 pubkSz; + + byte prik[CURVE25519_KEYSIZE]; + byte pubk[CURVE25519_KEYSIZE]; + word32 prksz; + word32 pbksz; + + printf(testingFmt, "wc_curve25519_export_key_raw()"); + + + if(0 != wc_InitRng(&rng)){ + printf(testingFmt, "failed due to wc_InitRng"); + fflush( stdout ); + return 1; + } + + if(0 != wc_curve25519_init(&key)){ + printf(testingFmt, "failed due to wc_curve25519_init"); + fflush( stdout ); + wc_FreeRng(&rng); + return 1; + } + + if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){ + printf(testingFmt, "failed due to wc_curve25519_make_key"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + /* + bad-argument-test cases + target function sould return BAD_FUNC_ARG + */ + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( + NULL , privateKey, &prvkSz, publicKey, &pubkSz)){ + + printf(testingFmt,"failed at bad-arg-case-1."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( + &key , NULL, &prvkSz, publicKey, &pubkSz)){ + + printf(testingFmt,"failed at bad-arg-case-2."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( + &key , privateKey, NULL, publicKey, &pubkSz)){ + + printf(testingFmt,"failed at bad-arg-case-3."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( + &key , privateKey, &prvkSz, NULL, &pubkSz)){ + + printf(testingFmt,"failed at bad-arg-case-4."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( + &key , privateKey, &prvkSz, publicKey, NULL )){ + + printf(testingFmt,"failed at bad-arg-case-5."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + /* + cross-testing + */ + prksz = CURVE25519_KEYSIZE; + + if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){ + + printf(testingFmt,"failed due to wc_curve25519_export_private_raw"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + pbksz = CURVE25519_KEYSIZE; + + if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){ + + printf(testingFmt,"failed due to wc_curve25519_export_public"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz, + publicKey, &pubkSz)){ + + printf(testingFmt,"failed due to wc_curve25519_export_key_raw"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + if((prksz == CURVE25519_KEYSIZE) && + (pbksz == CURVE25519_KEYSIZE) && + (prvkSz == CURVE25519_KEYSIZE) && + (pubkSz == CURVE25519_KEYSIZE)){ + + if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) && + 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){ + + printf(resultFmt,passed); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 0; + + } + else{ + + printf(testingFmt,"failed due to key-contents-inconsistency."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + } + else{ + + printf(testingFmt,"failed due to bad-key-size."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + +#endif + fflush( stdout ); + + return 0; +} /* end of test_wc_curve25519_export_key_raw */ + +/* + * Testing test_wc_curve25519_export_key_raw_ex(). + */ +static int test_wc_curve25519_export_key_raw_ex (void) +{ + +#if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT) + + curve25519_key key; + WC_RNG rng; + + byte privateKey[CURVE25519_KEYSIZE]; + byte publicKey[CURVE25519_KEYSIZE]; + word32 prvkSz; + word32 pubkSz; + + byte prik[CURVE25519_KEYSIZE]; + byte pubk[CURVE25519_KEYSIZE]; + word32 prksz; + word32 pbksz; + + printf(testingFmt, "wc_curve25519_export_key_raw_ex()"); + + if(0 != wc_InitRng(&rng)){ + printf(testingFmt, "failed due to wc_InitRng"); + fflush( stdout ); + return 1; + } + + if(0 != wc_curve25519_init(&key)){ + printf(testingFmt, "failed due to wc_curve25519_init"); + fflush( stdout ); + wc_FreeRng(&rng); + return 1; + } + + if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){ + printf(testingFmt, "failed due to wc_curve25519_make_key"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + /* + bad-argument-test cases + target function sould return BAD_FUNC_ARG + */ + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey, + &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-1."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL, + &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-2."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey, + NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-3."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-4."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-5."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey, + &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-6."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz, + publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-7."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-8."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-9."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){ + + printf(testingFmt,"failed at bad-arg-case-10."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + /* illegal value for endien */ + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){ + + printf(testingFmt,"failed at bad-arg-case-11."); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + /* + cross-testing + */ + prksz = CURVE25519_KEYSIZE; + + if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){ + + printf(testingFmt,"failed due to wc_curve25519_export_private_raw"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + pbksz = CURVE25519_KEYSIZE; + + if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){ + + printf(testingFmt,"failed due to wc_curve25519_export_public"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz, + publicKey, &pubkSz, EC25519_BIG_ENDIAN)) { + + printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + if( prksz == CURVE25519_KEYSIZE && + pbksz == CURVE25519_KEYSIZE && + prvkSz == CURVE25519_KEYSIZE && + pubkSz == CURVE25519_KEYSIZE ){ + + if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) && + 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){ + + if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ + + if( prvkSz == CURVE25519_KEYSIZE && + pubkSz == CURVE25519_KEYSIZE ){ + + ; /* proceed to the next test */ + } + else{ + + printf(testingFmt,"failed due to key-size-inconsistency"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + } + else{ + + printf(testingFmt, + "failed due to wc_curve25519_export_key_raw_ex"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + } + else{ + + printf(testingFmt,"failed due to key-contents-inconsistency"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + } + else{ + + printf(testingFmt,"failed due to bad-key-size"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + + /* + try once with another endian + */ + + prvkSz = CURVE25519_KEYSIZE; + pubkSz = CURVE25519_KEYSIZE; + + if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey, + &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ + + if( prvkSz == CURVE25519_KEYSIZE && + pubkSz == CURVE25519_KEYSIZE ){ + + /* no more test*/ + printf(resultFmt, passed ); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 0; + } + else{ + + printf(testingFmt,"failed due to key-size-inconsistency"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + } + } + else{ + + printf(testingFmt, + "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)"); + fflush( stdout ); + wc_curve25519_free(&key); + wc_FreeRng(&rng); + return 1; + + } + +#endif + return 0; +} /* end of test_wc_curve25519_export_key_raw_ex */ /* * Testing wc_curve25519_make_key */ @@ -15785,567 +16347,6 @@ static int test_wc_curve25519_export_private_raw_ex (void) return ret; }/*END test_wc_curve25519_export_private_raw_ex*/ -/* - * Testing test_wc_curve25519_size. - */ -static int test_wc_curve25519_size (void) -{ - int ret = 0; - -#if defined(HAVE_CURVE25519) - - curve25519_key key; - - printf(testingFmt, "wc_curve25519_size()"); - - ret = wc_curve25519_init(&key); - - /* Test good args for wc_curve25519_size */ - if (ret == 0) { - ret = wc_curve25519_size(&key); - } - - /* Test bad args for wc_curve25519_size */ - if (ret != 0) { - ret = wc_curve25519_size(NULL); - } - printf(resultFmt, ret == 0 ? passed : failed); - wc_curve25519_free(&key); -#endif - return ret; - -} /* END test_wc_curve25519_size*/ - -/* - * Testing test_wc_curve25519_export_key_raw(). - */ -static int test_wc_curve25519_export_key_raw (void) -{ - -#if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT) - - curve25519_key key; - WC_RNG rng; - - byte privateKey[CURVE25519_KEYSIZE]; - byte publicKey[CURVE25519_KEYSIZE]; - word32 prvkSz; - word32 pubkSz; - - byte prik[CURVE25519_KEYSIZE]; - byte pubk[CURVE25519_KEYSIZE]; - word32 prksz; - word32 pbksz; - - printf(testingFmt, "wc_curve25519_export_key_raw()"); - - - if(0 != wc_InitRng(&rng)){ - printf(testingFmt, "failed due to wc_InitRng"); - fflush( stdout ); - return 1; - } - - if(0 != wc_curve25519_init(&key)){ - printf(testingFmt, "failed due to wc_curve25519_init"); - fflush( stdout ); - wc_FreeRng(&rng); - return 1; - } - - if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){ - printf(testingFmt, "failed due to wc_curve25519_make_key"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - /* - bad-argument-test cases - target function sould return BAD_FUNC_ARG - */ - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( - NULL , privateKey, &prvkSz, publicKey, &pubkSz)){ - - printf(testingFmt,"failed at bad-arg-case-1."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( - &key , NULL, &prvkSz, publicKey, &pubkSz)){ - - printf(testingFmt,"failed at bad-arg-case-2."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( - &key , privateKey, NULL, publicKey, &pubkSz)){ - - printf(testingFmt,"failed at bad-arg-case-3."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( - &key , privateKey, &prvkSz, NULL, &pubkSz)){ - - printf(testingFmt,"failed at bad-arg-case-4."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw( - &key , privateKey, &prvkSz, publicKey, NULL )){ - - printf(testingFmt,"failed at bad-arg-case-5."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - /* - cross-testing - */ - prksz = CURVE25519_KEYSIZE; - - if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){ - - printf(testingFmt,"failed due to wc_curve25519_export_private_raw"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - pbksz = CURVE25519_KEYSIZE; - - if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){ - - printf(testingFmt,"failed due to wc_curve25519_export_public"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz, - publicKey, &pubkSz)){ - - printf(testingFmt,"failed due to wc_curve25519_export_key_raw"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - if((prksz == CURVE25519_KEYSIZE) && - (pbksz == CURVE25519_KEYSIZE) && - (prvkSz == CURVE25519_KEYSIZE) && - (pubkSz == CURVE25519_KEYSIZE)){ - - if( 0 == XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) && - 0 == XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)){ - - printf(resultFmt, passed ); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 0; - - } - else{ - - printf(testingFmt,"failed due to key-contents-inconsistency."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - } - else{ - - printf(testingFmt,"failed due to bad-key-size."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - -#endif - fflush( stdout ); - - return 0; -} /* end of test_wc_curve25519_export_key_raw */ - -/* - * Testing test_wc_curve25519_export_key_raw_ex(). - */ -static int test_wc_curve25519_export_key_raw_ex (void) -{ - -#if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT) - - curve25519_key key; - WC_RNG rng; - - byte privateKey[CURVE25519_KEYSIZE]; - byte publicKey[CURVE25519_KEYSIZE]; - word32 prvkSz; - word32 pubkSz; - - byte prik[CURVE25519_KEYSIZE]; - byte pubk[CURVE25519_KEYSIZE]; - word32 prksz; - word32 pbksz; - - printf(testingFmt, "wc_curve25519_export_key_raw_ex()"); - - if(0 != wc_InitRng(&rng)){ - printf(testingFmt, "failed due to wc_InitRng"); - fflush( stdout ); - return 1; - } - - if(0 != wc_curve25519_init(&key)){ - printf(testingFmt, "failed due to wc_curve25519_init"); - fflush( stdout ); - wc_FreeRng(&rng); - return 1; - } - - if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){ - printf(testingFmt, "failed due to wc_curve25519_make_key"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - /* - bad-argument-test cases - target function sould return BAD_FUNC_ARG - */ - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey, - &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-1."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL, - &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-2."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey, - NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-3."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-4."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-5."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey, - &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-6."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz, - publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-7."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-8."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-9."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){ - - printf(testingFmt,"failed at bad-arg-case-10."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - /* illegal value for endien */ - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10 )){ - - printf(testingFmt,"failed at bad-arg-case-11."); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - /* - cross-testing - */ - prksz = CURVE25519_KEYSIZE; - - if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){ - - printf(testingFmt,"failed due to wc_curve25519_export_private_raw"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - pbksz = CURVE25519_KEYSIZE; - - if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){ - - printf(testingFmt,"failed due to wc_curve25519_export_public"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz, - publicKey, &pubkSz, EC25519_BIG_ENDIAN)) { - - printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - if( prksz == CURVE25519_KEYSIZE && - pbksz == CURVE25519_KEYSIZE && - prvkSz == CURVE25519_KEYSIZE && - pubkSz == CURVE25519_KEYSIZE ){ - - if( 0 == XMEMCMP( privateKey, prik, CURVE25519_KEYSIZE ) && - 0 == XMEMCMP( publicKey, pubk, CURVE25519_KEYSIZE )){ - - if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){ - - if( prvkSz == CURVE25519_KEYSIZE && - pubkSz == CURVE25519_KEYSIZE ){ - - ; /* proceed to the next test */ - } - else{ - - printf(testingFmt,"failed due to key-size-inconsistency"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - } - else{ - - printf(testingFmt, - "failed due to wc_curve25519_export_key_raw_ex"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - } - else{ - - printf(testingFmt,"failed due to key-contents-inconsistency"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - } - else{ - - printf(testingFmt,"failed due to bad-key-size"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - - /* - try once with another endian - */ - - prvkSz = CURVE25519_KEYSIZE; - pubkSz = CURVE25519_KEYSIZE; - - if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey, - &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){ - - if( prvkSz == CURVE25519_KEYSIZE && - pubkSz == CURVE25519_KEYSIZE ){ - - /* no more test*/ - printf(resultFmt, passed ); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 0; - } - else{ - - printf(testingFmt,"failed due to key-size-inconsistency"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - } - } - else{ - - printf(testingFmt, - "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)"); - fflush( stdout ); - wc_curve25519_free(&key); - wc_FreeRng(&rng); - return 1; - - } - -#endif - return 0; -} /* end of test_wc_curve25519_export_key_raw_ex */ /* * Testing wc_ed448_make_key(). */ @@ -17162,7 +17163,7 @@ static int test_wc_curve448_shared_secret_ex (void) ret = wc_InitRng(&rng); if (ret == 0){ ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key); - } + } } if (ret == 0){ ret = wc_curve448_init(&public_key); @@ -17206,7 +17207,7 @@ static int test_wc_curve448_shared_secret_ex (void) ret = 0; } } - if (ret == 0) { + if (ret == 0) { ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out, NULL, endian); if (ret == BAD_FUNC_ARG) { @@ -17229,8 +17230,6 @@ static int test_wc_curve448_shared_secret_ex (void) #endif return ret; } /*END test_wc_curve448_shared_secret_ex*/ - - /* * Testing test_wc_curve448_export_public_ex */ @@ -17502,7 +17501,9 @@ static int test_wc_curve448_export_key_raw (void) #endif return ret; -}/*END test_wc_curve448_export_key_raw*/ +}/*END test_wc_curve448_import_private_raw_ex*/ + + /* * Testing test_wc_curve448_import_private */ From 3242fa36690842cae079fc6b080d4ceb886581a8 Mon Sep 17 00:00:00 2001 From: Ethan Looney Date: Wed, 1 Jul 2020 16:01:50 -0700 Subject: [PATCH 6/6] Fixed formatting, redundant if's and added a comment explaining why a value was chosen. --- tests/api.c | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/tests/api.c b/tests/api.c index 2714b371d..d17faec91 100644 --- a/tests/api.c +++ b/tests/api.c @@ -15931,9 +15931,9 @@ static int test_wc_curve25519_make_key (void) { int ret = 0; #if defined(HAVE_CURVE25519) - WC_RNG rng; + WC_RNG rng; curve25519_key key; - int keysize; + int keysize; printf(testingFmt, "wc_curve25519_make_key()"); @@ -16006,13 +16006,12 @@ static int test_wc_curve25519_shared_secret_ex (void) if (ret == 0) { ret = wc_InitRng(&rng); if (ret == 0) { - ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key); + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key); } } if (ret == 0) { - if (ret == 0) { - ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key); - } + ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key); + } if (ret == 0) { ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, @@ -16056,6 +16055,8 @@ static int test_wc_curve25519_shared_secret_ex (void) } if (ret == 0) { + /*curve25519.c is checking for public_key size less than or equal to 0x7f, + *increasing to 0x8f checks for error being returned*/ public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F; ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out, &outLen, endian); @@ -16089,11 +16090,11 @@ static int test_wc_curve25519_export_public_ex (void) int ret = 0; #if defined(HAVE_CURVE25519) - WC_RNG rng; + WC_RNG rng; curve25519_key key; - byte out[CURVE25519_KEYSIZE]; - word32 outLen = sizeof(out); - int endian = EC25519_BIG_ENDIAN; + byte out[CURVE25519_KEYSIZE]; + word32 outLen = sizeof(out); + int endian = EC25519_BIG_ENDIAN; printf(testingFmt, "wc_curve25519_export_public_ex()"); @@ -16158,8 +16159,8 @@ static int test_wc_curve25519_import_private_raw_ex (void) { int ret = 0; #if defined(HAVE_CURVE25519) - WC_RNG rng; - curve25519_key key; + WC_RNG rng; + curve25519_key key; byte priv[CURVE25519_KEYSIZE]; byte pub[CURVE25519_KEYSIZE]; word32 privSz = sizeof(priv); @@ -16181,7 +16182,7 @@ static int test_wc_curve25519_import_private_raw_ex (void) } if (ret == 0){ ret = wc_curve25519_export_public(&key, pub, &pubSz); - } + } if (ret == 0) { ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz, &key, endian); @@ -16251,7 +16252,7 @@ static int test_wc_curve25519_import_private (void) int ret = 0; #if defined(HAVE_CURVE25519) - curve25519_key key; + curve25519_key key; WC_RNG rng; byte priv[CURVE25519_KEYSIZE]; word32 privSz = sizeof(priv); @@ -16288,11 +16289,11 @@ static int test_wc_curve25519_export_private_raw_ex (void) int ret = 0; #if defined(HAVE_CURVE25519) - WC_RNG rng; + WC_RNG rng; curve25519_key key; - byte out[CURVE25519_KEYSIZE]; - word32 outLen = sizeof(out); - int endian = EC25519_BIG_ENDIAN; + byte out[CURVE25519_KEYSIZE]; + word32 outLen = sizeof(out); + int endian = EC25519_BIG_ENDIAN; printf(testingFmt, "wc_curve25519_export_private_raw_ex()");