aboutsummaryrefslogtreecommitdiff
path: root/tests/test.c
diff options
context:
space:
mode:
authorLibravatar vnugent <public@vaughnnugent.com>2024-04-03 18:26:11 -0400
committerLibravatar vnugent <public@vaughnnugent.com>2024-04-03 18:26:11 -0400
commit7485aa5a43476c47d54ec51fb96751a14e0ed629 (patch)
tree9ae8f27b5e0ed25eb9891877fda123b9027e843f /tests/test.c
parent5184d7db93131056215b9263740f4806495f148c (diff)
Squashed commit of the following:
commit d3328f4152b22b28f24c43dda62464287f1efff5 Author: vnugent <public@vaughnnugent.com> Date: Wed Apr 3 18:22:56 2024 -0400 build: Included dependency and versions in client builds commit b11bc0bac955fd5c6db65f0da48456bf5e748805 Author: vnugent <public@vaughnnugent.com> Date: Wed Apr 3 18:10:08 2024 -0400 fix: Fix c89 compatabilty comments and struct assignment commit 9915bd41799a72413e6b400e150aa9f5fa797e25 Merge: 8e3d6ea 5184d7d Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 30 09:57:30 2024 -0400 Merge branch 'master' into develop commit 8e3d6ea5e3c83fe42cb904b6ccc4fe2b73f76aae Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 30 09:52:55 2024 -0400 refactor!: Some api (struct) changes and updated tests commit e88e8420520204e20802516f01d4488bb0b1d6ea Merge: 490dfee 21f6c0a Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 3 15:02:34 2024 -0500 Merge branch 'master' into develop commit 490dfee4ef22479009627435c6ad728c3cbbab54 Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 3 14:59:25 2024 -0500 test: #3 tests for encryption/description and Macs commit efa97490b7ed47f4e2f05bee52e2b33e14e439e6 Merge: 1b84e3c 120022a Author: vnugent <public@vaughnnugent.com> Date: Sun Mar 3 14:55:48 2024 -0500 merge master commit 1b84e3c7c2e55b1ff9ffdd09b66873e11c131441 Author: vnugent <public@vaughnnugent.com> Date: Sat Mar 2 22:57:36 2024 -0500 fix: #2 constent usage of sizeof() operator on struct types commit 9de5a214c66adea0ef2d0bac63c59449de202a88 Author: vnugent <public@vaughnnugent.com> Date: Fri Mar 1 14:30:36 2024 -0500 perf: avoid nc_key struct copy, cast and verify instead commit b917b761120ed684af28d0707673ffadcf14b8fe Author: vnugent <public@vaughnnugent.com> Date: Mon Feb 12 22:06:50 2024 -0500 fix: found the constant time memcompare function commit 9f85fff3b9f25da7410569ea94f994b88feb3910 Author: vnugent <public@vaughnnugent.com> Date: Fri Feb 9 22:48:35 2024 -0500 feat: added/update MAC functions to sign or verify nip44 payload commit aa5113741bb419b02d6ea416bba571fa3d65db46 Author: vnugent <public@vaughnnugent.com> Date: Wed Feb 7 01:37:53 2024 -0500 add missing hmac-key output buffer commit 55f47d22cc9ce4d1e22b70814d608c7ef3b1bbc9 Author: vnugent <public@vaughnnugent.com> Date: Sun Feb 4 21:08:13 2024 -0500 simple bug fixes, and public api argument validation tests commit 73c5a713fb164ae8b4ac8a891a8020e08eae0a3b Author: vnugent <public@vaughnnugent.com> Date: Fri Feb 2 23:05:48 2024 -0500 update api to return secpvalidate return code instead of internal return codes commit 06c73004e1a39a7ea4ea3a89c22dee0f66adb236 Author: vnugent <public@vaughnnugent.com> Date: Fri Feb 2 19:25:17 2024 -0500 change to lgpl license commit 6e79fdb3b6b6739fc7797d47e55a7691306cf736 Author: vnugent <public@vaughnnugent.com> Date: Wed Jan 31 21:30:49 2024 -0500 move validation macros, and optionally disable them commit ac1e58837f1ba687939f78b5c03cadd346c10ddd Author: vnugent <public@vaughnnugent.com> Date: Tue Jan 30 12:25:05 2024 -0500 couple more tests, renable range checks, set flags for all projects
Diffstat (limited to 'tests/test.c')
-rw-r--r--tests/test.c140
1 files changed, 71 insertions, 69 deletions
diff --git a/tests/test.c b/tests/test.c
index e3188dc..8d3e115 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -67,7 +67,7 @@
#include "hex.h"
-//Pre-computed constants for argument errors
+/*Pre-computed constants for argument errors */
#define ARG_ERROR_POS_0 E_NULL_PTR
#define ARG_ERROR_POS_1 NCResultWithArgPosition(E_NULL_PTR, 0x01)
#define ARG_ERROR_POS_2 NCResultWithArgPosition(E_NULL_PTR, 0x02)
@@ -104,16 +104,17 @@ static int TestPublicApiArgumentValidation(void);
#endif
static const uint8_t zero32[32] = { 0 };
-static const uint8_t zero64[64] = { 0 };
int main(void)
{
int result;
+
result = RunTests();
+ (void)PrintHexBytes; /*avoid unused. I use occasionally for debugging*/
FreeHexBytes();
- return 0;
+ return result;
}
static int RunTests(void)
@@ -127,7 +128,11 @@ static int RunTests(void)
FillRandomData(ctxRandom, 32);
- //Context struct size should aways match the size of the struct returned by NCGetContextStructSize
+ /*
+ * Context struct size should aways match the size of the
+ * struct returned by NCGetContextStructSize
+ */
+
TEST(NCGetContextStructSize(), sizeof(NCContext))
TEST(NCInitContext(&ctx, ctxRandom), NC_SUCCESS)
@@ -181,16 +186,16 @@ static int InitKepair(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubK
{
PRINTL("TEST: Keypair")
- //Get random private key
+ /* Get random private key */
FillRandomData(secKey, sizeof(NCSecretKey));
- //Ensure not empty
+ /* Ensure not empty */
ENSURE(memcmp(zero32, secKey, 32) != 0);
- //Ensure the key is valid, result should be 1 on success
+ /* Ensure the key is valid, result should be 1 on success */
TEST(NCValidateSecretKey(context, secKey), 1);
- //Generate a public key from the secret key
+ /* Generate a public key from the secret key */
TEST(NCGetPublicKey(context, secKey, pubKey), NC_SUCCESS);
PRINTL("\nPASSED: Keypair tests completed")
@@ -206,55 +211,55 @@ static int TestEcdsa(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKe
PRINTL("TEST: Ecdsa")
- //Init a new secret key with random data
+ /*Init a new secret key with random data */
FillRandomData(invalidSig, sizeof(invalidSig));
FillRandomData(sigEntropy, sizeof(sigEntropy));
- //compute sha256 of the test string
+ /* compute sha256 of the test string */
_sha256((uint8_t*)message, strlen(message), digestToSign);
- //Sign and verify sig64
+ /* Sign and verify sig64 */
{
uint8_t sig[64];
TEST(NCSignDigest(context, secKey, sigEntropy, digestToSign, sig), NC_SUCCESS);
TEST(NCVerifyDigest(context, pubKey, digestToSign, sig), NC_SUCCESS);
}
- //Sign and verify raw data
+ /* Sign and verify raw data */
{
uint8_t sig[64];
TEST(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen(message), sig), NC_SUCCESS);
TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen(message), sig), NC_SUCCESS);
}
- //ensure the signature is the same for signing data and sig64
+ /* ensure the signature is the same for signing data and sig64 */
{
uint8_t sig1[64];
uint8_t sig2[64];
- //Ensure operations succeed but dont print them as test cases
+ /* 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
+ /* Perform test */
TEST(memcmp(sig1, sig2, 64), 0);
}
- //Try signing data then veriyfing the sig64
+ /* Try signing data then veriyfing the sig64 */
{
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
+ /* 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 verification of invalid signature */
{
TEST(NCVerifyDigest(context, pubKey, digestToSign, invalidSig), E_INVALID_ARG);
}
@@ -275,51 +280,49 @@ static int TestPublicApiArgumentValidation(void)
uint8_t hmacKeyOut[NC_HMAC_KEY_SIZE];
uint8_t nonce[NC_ENCRYPTION_NONCE_SIZE];
- NCCryptoData cryptoData = {
- .dataSize = sizeof(zero32),
- .inputData = zero32,
- .outputData = sig64, //just an arbitrary writeable buffer
- .nonce32 = nonce
- };
+ NCCryptoData cryptoData;
+ cryptoData.dataSize = sizeof(zero32);
+ cryptoData.inputData = zero32;
+ cryptoData.outputData = sig64; /*just an arbitrary writeable buffer*/
PRINTL("TEST: Public API argument validation tests")
FillRandomData(ctxRandom, 32);
FillRandomData(nonce, sizeof(nonce));
- //Test null context
+ /*Test null context*/
TEST(NCInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
TEST(NCInitContext(&ctx, NULL), ARG_ERROR_POS_1)
- //Test null context
+ /*Test null context*/
TEST(NCDestroyContext(NULL), ARG_ERROR_POS_0)
- //reinit
+ /*reinit*/
TEST(NCReInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
TEST(NCReInitContext(&ctx, NULL), ARG_ERROR_POS_1)
- //Test null secret key
+ /*Test null secret key*/
TEST(NCGetPublicKey(&ctx, NULL, &pubKey), ARG_ERROR_POS_1)
TEST(NCGetPublicKey(&ctx, &secKey, NULL), ARG_ERROR_POS_2)
- //Test null secret key
+ /*Test null secret key*/
TEST(NCValidateSecretKey(NULL, &secKey), ARG_ERROR_POS_0)
TEST(NCValidateSecretKey(&ctx, NULL), ARG_ERROR_POS_1)
- //Verify sig64 args test
+ /*Verify sig64 args test*/
TEST(NCVerifyDigest(NULL, &pubKey, zero32, sig64), ARG_ERROR_POS_0)
TEST(NCVerifyDigest(&ctx, NULL, zero32, sig64), ARG_ERROR_POS_1)
TEST(NCVerifyDigest(&ctx, &pubKey, NULL, sig64), ARG_ERROR_POS_2)
TEST(NCVerifyDigest(&ctx, &pubKey, zero32, NULL), ARG_ERROR_POS_3)
- //Test verify data args
+ /*Test verify data args*/
TEST(NCVerifyData(NULL, &pubKey, zero32, 32, sig64), ARG_ERROR_POS_0)
TEST(NCVerifyData(&ctx, NULL, zero32, 32, sig64), ARG_ERROR_POS_1)
TEST(NCVerifyData(&ctx, &pubKey, NULL, 32, sig64), ARG_ERROR_POS_2)
TEST(NCVerifyData(&ctx, &pubKey, zero32, 0, sig64), ARG_RAMGE_ERROR_POS_3)
TEST(NCVerifyData(&ctx, &pubKey, zero32, 32, NULL), ARG_ERROR_POS_4)
- //Test null sign data args
+ /*Test null sign data args*/
TEST(NCSignData(NULL, &secKey, zero32, zero32, 32, sig64), ARG_ERROR_POS_0)
TEST(NCSignData(&ctx, NULL, zero32, zero32, 32, sig64), ARG_ERROR_POS_1)
TEST(NCSignData(&ctx, &secKey, NULL, zero32, 32, sig64), ARG_ERROR_POS_2)
@@ -327,35 +330,35 @@ static int TestPublicApiArgumentValidation(void)
TEST(NCSignData(&ctx, &secKey, zero32, zero32, 0, sig64), ARG_RAMGE_ERROR_POS_4)
TEST(NCSignData(&ctx, &secKey, zero32, zero32, 32, NULL), ARG_ERROR_POS_5)
- //Test null sign digest args
+ /*Test null sign digest args*/
TEST(NCSignDigest(NULL, &secKey, zero32, zero32, sig64), ARG_ERROR_POS_0)
TEST(NCSignDigest(&ctx, NULL, zero32, zero32, sig64), ARG_ERROR_POS_1)
TEST(NCSignDigest(&ctx, &secKey, NULL, zero32, sig64), ARG_ERROR_POS_2)
TEST(NCSignDigest(&ctx, &secKey, zero32, NULL, sig64), ARG_ERROR_POS_3)
TEST(NCSignDigest(&ctx, &secKey, zero32, zero32, NULL), ARG_ERROR_POS_4)
- //Test null encrypt args
+ /*Test null encrypt args*/
TEST(NCEncrypt(NULL, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_ERROR_POS_0)
TEST(NCEncrypt(&ctx, NULL, &pubKey, hmacKeyOut, &cryptoData), ARG_ERROR_POS_1)
TEST(NCEncrypt(&ctx, &secKey, NULL, hmacKeyOut, &cryptoData), ARG_ERROR_POS_2)
TEST(NCEncrypt(&ctx, &secKey, &pubKey, NULL, &cryptoData), ARG_ERROR_POS_3)
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, NULL), ARG_ERROR_POS_4)
- //Test invalid data size
+ /*Test invalid data size*/
cryptoData.dataSize = 0;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_RAMGE_ERROR_POS_4)
- //Test null input data
+ /*Test null input data */
cryptoData.dataSize = 32;
cryptoData.inputData = NULL;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_INVALID_ERROR_POS_4)
- //Test null output data
+ /*Test null output data */
cryptoData.inputData = zero32;
cryptoData.outputData = NULL;
TEST(NCEncrypt(&ctx, &secKey, &pubKey, hmacKeyOut, &cryptoData), ARG_INVALID_ERROR_POS_4)
- //Decrypt
+ /* Decrypt */
cryptoData.dataSize = 32;
cryptoData.inputData = zero32;
cryptoData.outputData = sig64;
@@ -365,16 +368,16 @@ static int TestPublicApiArgumentValidation(void)
TEST(NCDecrypt(&ctx, &secKey, NULL, &cryptoData), ARG_ERROR_POS_2)
TEST(NCDecrypt(&ctx, &secKey, &pubKey, NULL), ARG_ERROR_POS_3)
- //Test invalid data size
+ /* Test invalid data size */
cryptoData.dataSize = 0;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_RAMGE_ERROR_POS_3)
- //Test null input data
+ /* Test null input data */
cryptoData.dataSize = 32;
cryptoData.inputData = NULL;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_INVALID_ERROR_POS_3)
- //Test null output data
+ /*Test null output data */
cryptoData.inputData = zero32;
cryptoData.outputData = NULL;
TEST(NCDecrypt(&ctx, &secKey, &pubKey, &cryptoData), ARG_INVALID_ERROR_POS_3)
@@ -389,12 +392,11 @@ static int TestPublicApiArgumentValidation(void)
}
{
- NCMacVerifyArgs macArgs = {
- .payload = zero32,
- .payloadSize = 32,
- .mac32 = zero32,
- .nonce32 = zero32
- };
+ NCMacVerifyArgs macArgs;
+ macArgs.payload = zero32;
+ macArgs.payloadSize = 32;
+ macArgs.mac32 = zero32;
+ macArgs.nonce32 = zero32;
TEST(NCVerifyMac(NULL, &secKey, &pubKey, &macArgs), ARG_ERROR_POS_0)
TEST(NCVerifyMac(&ctx, NULL, &pubKey, &macArgs), ARG_ERROR_POS_1)
@@ -417,18 +419,19 @@ static int TestPublicApiArgumentValidation(void)
#endif
static int TestKnownKeys(NCContext* context)
-{
- PRINTL("TEST: Known keys")
-
+{
NCPublicKey pubKey;
+ HexBytes* secKey1, * pubKey1, * secKey2, * pubKey2;
- HexBytes* secKey1 = FromHexString("98c642360e7163a66cee5d9a842b252345b6f3f3e21bd3b7635d5e6c20c7ea36", sizeof(secKey));
- HexBytes* pubKey1 = FromHexString("0db15182c4ad3418b4fbab75304be7ade9cfa430a21c1c5320c9298f54ea5406", sizeof(pubKey));
+ PRINTL("TEST: Known keys")
+
+ secKey1 = FromHexString("98c642360e7163a66cee5d9a842b252345b6f3f3e21bd3b7635d5e6c20c7ea36", sizeof(NCSecretKey));
+ pubKey1 = FromHexString("0db15182c4ad3418b4fbab75304be7ade9cfa430a21c1c5320c9298f54ea5406", sizeof(NCPublicKey));
- HexBytes* secKey2 = FromHexString("3032cb8da355f9e72c9a94bbabae80ca99d3a38de1aed094b432a9fe3432e1f2", sizeof(secKey));
- HexBytes* pubKey2 = FromHexString("421181660af5d39eb95e48a0a66c41ae393ba94ffeca94703ef81afbed724e5a", sizeof(pubKey));
+ secKey2 = FromHexString("3032cb8da355f9e72c9a94bbabae80ca99d3a38de1aed094b432a9fe3432e1f2", sizeof(NCSecretKey));
+ pubKey2 = FromHexString("421181660af5d39eb95e48a0a66c41ae393ba94ffeca94703ef81afbed724e5a", sizeof(NCPublicKey));
- //Test known keys
+ /*Test known keys*/
TEST(NCValidateSecretKey(context, NCToSecKey(secKey1->data)), 1);
/* Recover a public key from secret key 1 */
@@ -463,21 +466,20 @@ static int TestCorrectEncryption(NCContext* context)
uint8_t plainText[TEST_ENC_DATA_SIZE];
uint8_t cipherText[TEST_ENC_DATA_SIZE];
uint8_t decryptedText[TEST_ENC_DATA_SIZE];
+
+ NCCryptoData cryptoData;
+ NCMacVerifyArgs macVerifyArgs;
/* setup the crypto data structure */
- NCCryptoData cryptoData = {
- .dataSize = TEST_ENC_DATA_SIZE,
- .inputData = plainText,
- .outputData = cipherText,
- .nonce32 = nonce
- };
-
- NCMacVerifyArgs macVerifyArgs = {
- .nonce32 = nonce,
- .mac32 = mac,
- .payload = cipherText,
- .payloadSize = TEST_ENC_DATA_SIZE
- };
+ cryptoData.dataSize = TEST_ENC_DATA_SIZE;
+ cryptoData.inputData = plainText;
+ cryptoData.outputData = cipherText;
+ cryptoData.nonce32 = nonce;
+
+ macVerifyArgs.nonce32 = nonce;
+ macVerifyArgs.mac32 = mac;
+ macVerifyArgs.payload = cipherText;
+ macVerifyArgs.payloadSize = TEST_ENC_DATA_SIZE;
PRINTL("TEST: Correct encryption")
@@ -497,7 +499,7 @@ static int TestCorrectEncryption(NCContext* context)
/* Try to encrypt the data from sec1 to pub2 */
TEST(NCEncrypt(context, &secKey1, &pubKey2, hmacKeyOut, &cryptoData), NC_SUCCESS);
- //swap cipher and plain text for decryption
+ /*swap cipher and plain text for decryption */
cryptoData.inputData = cipherText;
cryptoData.outputData = decryptedText;