Merge pull request #3095 from ethanlooney/sixth_branch

Added additional tests for curve25519 and fixed a print format error from previous tests.
pull/3102/head
Chris Conlon 2020-07-02 10:07:55 -05:00 committed by GitHub
commit 655022cfc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 432 additions and 2 deletions

View File

@ -15562,7 +15562,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);
@ -15588,7 +15588,6 @@ static int test_wc_curve25519_export_key_raw (void)
}
#endif
printf(resultFmt, passed );
fflush( stdout );
return 0;
@ -15925,6 +15924,430 @@ static int test_wc_curve25519_export_key_raw_ex (void)
#endif
return 0;
} /* end of test_wc_curve25519_export_key_raw_ex */
/*
* 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) {
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) {
/*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);
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 wc_ed448_make_key().
*/
@ -33804,6 +34227,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);