From ac1e58837f1ba687939f78b5c03cadd346c10ddd Mon Sep 17 00:00:00 2001 From: vnugent Date: Tue, 30 Jan 2024 12:25:05 -0500 Subject: couple more tests, renable range checks, set flags for all projects --- tests/test.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/test.c b/tests/test.c index 499ef50..b44f820 100644 --- a/tests/test.c +++ b/tests/test.c @@ -22,30 +22,41 @@ #include #include #include +#include #include "../src/noscrypt.h" #include "../include/mbedtls/sha256.h" +#include "../include/mbedtls/platform_util.h" #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32) #define IS_WINDOWS #endif #ifdef IS_WINDOWS - - #include + #include #include +#endif + +#ifdef IS_WINDOWS //Prints a string literal to the console #define PRINTL(x) printf(x); printf("\r\n"); #define TEST(x) printf("Testing %s\n", #x); if(!(x)) { printf("Test failed!\n"); return 1; } else { printf("Test passed\n\n"); } #define TASSERT(x) if(!(x)) { printf("ERROR! Internal test assumption failed: %s.\n Aborting tests...\n", #x); ExitProcess(1); } + #define ENSURE(x) if(!(x)) { printf("Assumption failed!\n"); return 1; } #else - #include //Prints a string literal to the console #define PRINTL(x) printf(x); printf("\n"); #define TEST(x) printf("Testing %s\n", #x); if(!(x)) { printf("Test failed!\n"); return 1; } else { printf("Test passed\n\n"); } #define TASSERT(x) if(!(x)) { printf("Internal assumption failed: %s\n", #x); exit(1); } + #define ENSURE(x) if(!(x)) { printf("Assumption failed!\n"); return 1; } +#endif + +#ifdef IS_WINDOWS + #define ZERO_FILL(x, size) SecureZeroMemory(x, size) +#else + #define ZERO_FILL(x, size) memset(x, 0, size) #endif static void FillRandomData(uint8_t* pbBuffer, size_t length); @@ -88,12 +99,15 @@ static void _sha256(const uint8_t* data, size_t length, uint8_t digest[32]) } static const char* message = "Test message to sign"; +static const uint8_t zero32[32] = { 0 }; +static const uint8_t zero64[64] = { 0 }; static int TestEcdsa(NCContext* context) { - uint8_t digestToSign[32]; + uint8_t secretKey[NC_SEC_KEY_SIZE]; uint8_t publicKey[NC_PUBKEY_SIZE]; + uint8_t digestToSign[32]; uint8_t sigEntropy[32]; uint8_t invalidSig[64]; NCSecretKey* secKey; @@ -112,18 +126,22 @@ static int TestEcdsa(NCContext* context) FillRandomData(invalidSig, sizeof(invalidSig)); FillRandomData(sigEntropy, sizeof(sigEntropy)); + //compute sha256 of the test string + _sha256((uint8_t*)message, strlen(message), digestToSign); + //Verify that the secret key is valid for the curve TEST(NCValidateSecretKey(context, secKey) == NC_SUCCESS); //Generate a public key from the secret key TEST(NCGetPublicKey(context, secKey, pubKey) == NC_SUCCESS); + //Ensure not empty + TEST(memcmp(zero32, secretKey, 32) != 0); + TEST(memcmp(zero32, publicKey, 32) != 0); + //Sign and verify digest { uint8_t sig[64]; - - //compute sha256 of the test string - _sha256((uint8_t*)message, strlen(message), digestToSign); TEST(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig) == NC_SUCCESS); TEST(NCVerifyDigest(context, pubKey, digestToSign, sig) == NC_SUCCESS); @@ -136,8 +154,36 @@ static int TestEcdsa(NCContext* context) TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen(message), sig) == NC_SUCCESS); } + //ensure the signature is the same for signing data and digest + { + uint8_t sig1[64]; + uint8_t sig2[64]; + + //Ensure operations succeed but dont print them as test cases + ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig1) == NC_SUCCESS); + ENSURE(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig2) == NC_SUCCESS); + + //Perform test + TEST(memcmp(sig1, sig2, 64) == 0); + } + + //Try signing data then veriyfing the digest + { + uint8_t sig[64]; + + ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig) == NC_SUCCESS); + TEST(NCVerifyDigest(context, pubKey, digestToSign, sig) == NC_SUCCESS); + + //Now invert test, zero signature to ensure its overwritten + ZERO_FILL(sig, sizeof(sig)); + + ENSURE(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig) == NC_SUCCESS); + TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen(message), sig) == NC_SUCCESS); + } + //test verification of invalid signature { + TEST(NCVerifyDigest(context, pubKey, digestToSign, invalidSig) == E_INVALID_ARG); } -- cgit