aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/hex.h54
-rw-r--r--tests/test.c365
2 files changed, 320 insertions, 99 deletions
diff --git a/tests/hex.h b/tests/hex.h
index 5e90ce9..3cfe559 100644
--- a/tests/hex.h
+++ b/tests/hex.h
@@ -28,18 +28,12 @@
#include <nc-util.h>
-typedef struct hexBytes
-{
- uint8_t* data;
- size_t length;
-} HexBytes;
-
-/* Deferred list of HexBytes to be freed on exit */
-static HexBytes* _hdeferList[10];
+/* Deferred list of span_t to be freed on exit */
+static span_t _hdeferList[20];
static size_t _hdeferListIndex = 0;
/*
- Allocates a HexBytes and decodes the hexadecimal string into it's binary
+ Allocates a span_t and decodes the hexadecimal string into it's binary
representation. The string must be a valid hexadecimal string and the length
and may not be NULL. The length may be known at compile time and can be used
to assert the length of the string literal.
@@ -48,34 +42,34 @@ static size_t _hdeferListIndex = 0;
*/
#define FromHexString(str, len) _fromHexString(str, sizeof(str) - 1); STATIC_ASSERT(sizeof(str)/2 == len && len > 0, "Invalid length hex string literal");
-static HexBytes* __allocHexBytes(size_t length)
+static span_t __allocHexBytes(size_t length)
{
- HexBytes* hexBytes;
+ span_t hexBytes;
length /= 2;
- hexBytes = (HexBytes*)malloc(length + sizeof(HexBytes));
- if(!hexBytes)
+ hexBytes.data = malloc(length);
+
+ if(!hexBytes.data)
{
- return NULL;
+ return hexBytes;
}
- hexBytes->length = length;
- /* data starts after the structure size */
- hexBytes-> data = ((uint8_t*)hexBytes) + sizeof(HexBytes);
+ hexBytes.size = length;
/* add new value to deferred cleanup list */
_hdeferList[_hdeferListIndex++] = hexBytes;
return hexBytes;
}
-static HexBytes* _fromHexString(const char* hexLiteral, size_t strLen)
+static span_t _fromHexString(const char* hexLiteral, uint32_t strLen)
{
- HexBytes* hexBytes;
+ span_t hexBytes;
size_t i;
if(!hexLiteral)
{
- return NULL;
+ ncSpanInit(&hexBytes, NULL, 0);
+ return hexBytes;
}
/* alloc the raw bytes */
@@ -90,14 +84,14 @@ static HexBytes* _fromHexString(const char* hexLiteral, size_t strLen)
byteString[0] = hexLiteral[i];
byteString[1] = hexLiteral[i + 1];
- hexBytes->data[i / 2] = (uint8_t)strtol(byteString, NULL, 16);
+ hexBytes.data[i / 2] = (uint8_t)strtol(byteString, NULL, 16);
}
return hexBytes;
}
/*
- Frees all the HexBytes that were allocated by the
+ Frees all the span_t that were allocated by the
FromHexString function. To be called at the end of
the program.
*/
@@ -105,8 +99,8 @@ static void FreeHexBytes(void)
{
while(_hdeferListIndex > 0)
{
- free(_hdeferList[--_hdeferListIndex]);
- _hdeferList[_hdeferListIndex] = NULL;
+ free(_hdeferList[--_hdeferListIndex].data);
+ memset(&_hdeferList[_hdeferListIndex], 0, sizeof(span_t));
}
}
@@ -127,18 +121,18 @@ static void PrintHexRaw(void* bytes, size_t len)
}
/*
-* Prints the value of the HexBytes as a hexadecimal string
-* @param hexBytes A pointer to the HexBytes structure to print the value of
+* Prints the value of the span_t as a hexadecimal string
+* @param hexBytes A pointer to the span_t structure to print the value of
*/
-static void PrintHexBytes(HexBytes* hexBytes)
+static void PrintHexBytes(span_t hexBytes)
{
- if (!hexBytes)
+ if (ncSpanIsValid(hexBytes))
{
- puts("NULL");
+ PrintHexRaw(hexBytes.data, hexBytes.size);
}
else
{
- PrintHexRaw(hexBytes->data, hexBytes->length);
+ puts("NULL");
}
}
diff --git a/tests/test.c b/tests/test.c
index 084ac13..b4cdef1 100644
--- a/tests/test.c
+++ b/tests/test.c
@@ -44,7 +44,7 @@
/*Prints a string literal to the console*/
#define PRINTL(x) puts(x); puts("\n");
-#define ENSURE(x) if(!(x)) { puts("Assumption failed!\n"); return 1; }
+#define ENSURE(x) if(!(x)) { printf("Test assumption failed on line %d\n", __LINE__); return 1; }
#define TEST(x, expected) printf("\tTesting %s\n", #x); if(((long)x) != ((long)expected)) \
{ printf("FAILED: Expected %ld but got %ld @ callsite %s. Line: %d \n", ((long)expected), ((long)x), #x, __LINE__); return 1; }
@@ -68,28 +68,31 @@
/*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)
-#define ARG_ERROR_POS_3 NCResultWithArgPosition(E_NULL_PTR, 0x03)
-#define ARG_ERROR_POS_4 NCResultWithArgPosition(E_NULL_PTR, 0x04)
-#define ARG_ERROR_POS_5 NCResultWithArgPosition(E_NULL_PTR, 0x05)
-#define ARG_ERROR_POS_6 NCResultWithArgPosition(E_NULL_PTR, 0x06)
+#define ARG_ERROR(pos) NCResultWithArgPosition(E_NULL_PTR, pos)
+#define ARG_ERROR_POS_1 ARG_ERROR(0x01)
+#define ARG_ERROR_POS_2 ARG_ERROR(0x02)
+#define ARG_ERROR_POS_3 ARG_ERROR(0x03)
+#define ARG_ERROR_POS_4 ARG_ERROR(0x04)
+#define ARG_ERROR_POS_5 ARG_ERROR(0x05)
+#define ARG_ERROR_POS_6 ARG_ERROR(0x06)
#define ARG_RANGE_ERROR_POS_0 E_ARGUMENT_OUT_OF_RANGE
-#define ARG_RANGE_ERROR_POS_1 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x01)
-#define ARG_RANGE_ERROR_POS_2 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x02)
-#define ARG_RANGE_ERROR_POS_3 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x03)
-#define ARG_RANGE_ERROR_POS_4 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x04)
-#define ARG_RANGE_ERROR_POS_5 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x05)
-#define ARG_RANGE_ERROR_POS_6 NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, 0x06)
+#define ARG_RANGE_ERROR(pos) NCResultWithArgPosition(E_ARGUMENT_OUT_OF_RANGE, pos)
+#define ARG_RANGE_ERROR_POS_1 ARG_RANGE_ERROR(0x01)
+#define ARG_RANGE_ERROR_POS_2 ARG_RANGE_ERROR(0x02)
+#define ARG_RANGE_ERROR_POS_3 ARG_RANGE_ERROR(0x03)
+#define ARG_RANGE_ERROR_POS_4 ARG_RANGE_ERROR(0x04)
+#define ARG_RANGE_ERROR_POS_5 ARG_RANGE_ERROR(0x05)
+#define ARG_RANGE_ERROR_POS_6 ARG_RANGE_ERROR(0x06)
#define ARG_INVALID_ERROR_POS_0 E_INVALID_ARG
-#define ARG_INVALID_ERROR_POS_1 NCResultWithArgPosition(E_INVALID_ARG, 0x01)
-#define ARG_INVALID_ERROR_POS_2 NCResultWithArgPosition(E_INVALID_ARG, 0x02)
-#define ARG_INVALID_ERROR_POS_3 NCResultWithArgPosition(E_INVALID_ARG, 0x03)
-#define ARG_INVALID_ERROR_POS_4 NCResultWithArgPosition(E_INVALID_ARG, 0x04)
-#define ARG_INVALID_ERROR_POS_5 NCResultWithArgPosition(E_INVALID_ARG, 0x05)
-#define ARG_INVALID_ERROR_POS_6 NCResultWithArgPosition(E_INVALID_ARG, 0x06)
+#define ARG_INVALID_ERROR(pos) NCResultWithArgPosition(E_INVALID_ARG, pos)
+#define ARG_INVALID_ERROR_POS_1 ARG_INVALID_ERROR(0x01)
+#define ARG_INVALID_ERROR_POS_2 ARG_INVALID_ERROR(0x02)
+#define ARG_INVALID_ERROR_POS_3 ARG_INVALID_ERROR(0x03)
+#define ARG_INVALID_ERROR_POS_4 ARG_INVALID_ERROR(0x04)
+#define ARG_INVALID_ERROR_POS_5 ARG_INVALID_ERROR(0x05)
+#define ARG_INVALID_ERROR_POS_6 ARG_INVALID_ERROR(0x06)
static int RunTests(void);
static void FillRandomData(void* pbBuffer, size_t length);
@@ -98,6 +101,10 @@ static int InitKepair(const NCContext* context, NCSecretKey* secKey, NCPublicKey
static int TestKnownKeys(const NCContext* context);
static int TestCorrectEncryption(const NCContext* context);
+#ifdef NC_ENABLE_UTILS
+static int TestUtilFunctions(const NCContext * libCtx);
+#endif
+
#ifndef NC_INPUT_VALIDATION_OFF
static int TestPublicApiArgumentValidation(void);
#endif
@@ -164,6 +171,13 @@ static int RunTests(void)
return 1;
}
+#ifdef NC_ENABLE_UTILS
+ if (TestUtilFunctions(ctx) != 0)
+ {
+ return 1;
+ }
+#endif
+
TEST(NCDestroyContext(ctx), NC_SUCCESS)
PRINTL("\nSUCCESS All tests passed")
@@ -199,7 +213,7 @@ static int TestEcdsa(const NCContext* context, NCSecretKey* secKey, NCPublicKey*
uint8_t sigEntropy[32];
uint8_t invalidSig[64];
- HexBytes* digestHex;
+ span_t digestHex;
PRINTL("TEST: Ecdsa")
@@ -213,8 +227,8 @@ static int TestEcdsa(const NCContext* context, NCSecretKey* secKey, NCPublicKey*
/* Test signing just the message digest */
{
uint8_t sig[64];
- TEST(NCSignDigest(context, secKey, sigEntropy, digestHex->data, sig), NC_SUCCESS);
- TEST(NCVerifyDigest(context, pubKey, digestHex->data, sig), NC_SUCCESS);
+ TEST(NCSignDigest(context, secKey, sigEntropy, digestHex.data, sig), NC_SUCCESS);
+ TEST(NCVerifyDigest(context, pubKey, digestHex.data, sig), NC_SUCCESS);
}
/* Sign and verify the raw message */
@@ -231,7 +245,7 @@ static int TestEcdsa(const NCContext* context, NCSecretKey* secKey, NCPublicKey*
/* Ensure operations succeed but dont print them as test cases */
ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen32(message), sig1) == NC_SUCCESS);
- ENSURE(NCSignDigest(context, secKey, sigEntropy, digestHex->data, sig2) == NC_SUCCESS);
+ ENSURE(NCSignDigest(context, secKey, sigEntropy, digestHex.data, sig2) == NC_SUCCESS);
/* Perform test */
TEST(memcmp(sig1, sig2, 64), 0);
@@ -242,18 +256,18 @@ static int TestEcdsa(const NCContext* context, NCSecretKey* secKey, NCPublicKey*
uint8_t sig[64];
ENSURE(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen32(message), sig) == NC_SUCCESS);
- TEST(NCVerifyDigest(context, pubKey, digestHex->data, sig), NC_SUCCESS);
+ TEST(NCVerifyDigest(context, pubKey, digestHex.data, sig), NC_SUCCESS);
/* Now invert test, zero signature to ensure its overwritten */
ZERO_FILL(sig, sizeof(sig));
- ENSURE(NCSignDigest(context, secKey, sigEntropy, digestHex->data, sig) == NC_SUCCESS);
+ ENSURE(NCSignDigest(context, secKey, sigEntropy, digestHex.data, sig) == NC_SUCCESS);
TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen32(message), sig), NC_SUCCESS);
}
/* test verification of invalid signature */
{
- TEST(NCVerifyDigest(context, pubKey, digestHex->data, invalidSig), E_INVALID_ARG);
+ TEST(NCVerifyDigest(context, pubKey, digestHex.data, invalidSig), E_INVALID_ARG);
}
PRINTL("\nPASSED: Ecdsa tests completed")
@@ -273,21 +287,79 @@ static int TestPublicApiArgumentValidation()
uint8_t nonce[NC_ENCRYPTION_NONCE_SIZE];
NCEncryptionArgs cryptoData;
- cryptoData.dataSize = sizeof(zero32);
- cryptoData.inputData = zero32;
- cryptoData.outputData = sig64; /*just an arbitrary writeable buffer*/
- cryptoData.nonce32 = nonce;
- cryptoData.hmacKeyOut32 = hmacKeyOut;
- cryptoData.version = NC_ENC_VERSION_NIP44;
PRINTL("TEST: Public API argument validation tests")
+ {
+ /*
+ * Test arguments for encryption properties
+ */
+
+ uint8_t testBuff32[32];
+
+ TEST(NCSetEncryptionProperty(NULL, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44), ARG_ERROR_POS_0)
+ TEST(NCSetEncryptionProperty(&cryptoData, 0, 1), E_INVALID_ARG)
+
+ TEST(NCSetEncryptionData(NULL, zero32, sig64, sizeof(zero32)), ARG_ERROR_POS_0)
+ TEST(NCSetEncryptionData(&cryptoData, NULL, sig64, sizeof(zero32)), ARG_ERROR_POS_1)
+ TEST(NCSetEncryptionData(&cryptoData, zero32, NULL, sizeof(zero32)), ARG_ERROR_POS_2)
+ TEST(NCSetEncryptionData(&cryptoData, zero32, sig64, 0), ARG_RANGE_ERROR_POS_3)
+
+ /* Setting any version specific value should fail */
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, nonce, sizeof(nonce)), E_VERSION_NOT_SUPPORTED)
+
+ /* Set to nip44 to continue nip44 tests */
+ TEST(NCSetEncryptionProperty(&cryptoData, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44), NC_SUCCESS)
+
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, 0, nonce, sizeof(nonce)), E_INVALID_ARG)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, NULL, sizeof(nonce)), ARG_ERROR_POS_2)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, nonce, 0), ARG_RANGE_ERROR_POS_3)
+ /* Nonce size should fail if smaller than the required nonce size */
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, nonce, NC_ENCRYPTION_NONCE_SIZE - 1), ARG_RANGE_ERROR_POS_3)
+
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, 0, hmacKeyOut, sizeof(hmacKeyOut)), E_INVALID_ARG)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_MAC_KEY, NULL, sizeof(hmacKeyOut)), ARG_ERROR_POS_2)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_MAC_KEY, hmacKeyOut, 0), ARG_RANGE_ERROR_POS_3)
+ /* Key size should fail if smaller than the required nip44 key size */
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_MAC_KEY, hmacKeyOut, NC_HMAC_KEY_SIZE - 1), ARG_RANGE_ERROR_POS_3)
+
+ /* Test for nip04 */
+
+ /* Any nip04 specific properties should fail since nip44 has already been set */
+
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_IV, testBuff32, sizeof(testBuff32)), E_VERSION_NOT_SUPPORTED)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_KEY, testBuff32, sizeof(testBuff32)), E_VERSION_NOT_SUPPORTED)
+
+ /* Set to nip04 to continue nip04 tests */
+ ENSURE(NCSetEncryptionProperty(&cryptoData, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP04) == NC_SUCCESS)
+
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_IV, NULL, sizeof(testBuff32)), ARG_ERROR_POS_2)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_IV, testBuff32, 0), ARG_RANGE_ERROR_POS_3)
+ /* IV size should fail if smaller than IV */
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_IV, testBuff32, NC_NIP04_AES_IV_SIZE - 1), ARG_RANGE_ERROR_POS_3)
+
+
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_KEY, NULL, sizeof(testBuff32)), ARG_ERROR_POS_2)
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_KEY, testBuff32, 0), ARG_RANGE_ERROR_POS_3)
+ /* Key size should fail if smaller than the required nip04 key size */
+ TEST(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP04_KEY, testBuff32, NC_NIP04_AES_KEY_SIZE - 1), ARG_RANGE_ERROR_POS_3)
+ }
+
+ /* Prep the crypto structure for proper usage */
+ ENSURE(NCSetEncryptionProperty(&cryptoData, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44) == NC_SUCCESS);
+ ENSURE(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, nonce, sizeof(nonce)) == NC_SUCCESS);
+ ENSURE(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_MAC_KEY, hmacKeyOut, sizeof(hmacKeyOut)) == NC_SUCCESS);
+
+ /* Assign the encryption material */
+ ENSURE(NCSetEncryptionData(&cryptoData, zero32, sig64, sizeof(zero32)) == NC_SUCCESS);
+
+
FillRandomData(ctxRandom, 32);
FillRandomData(nonce, sizeof(nonce));
/*
* Alloc context structure on the heap before use.
- * THIS WILL LEAK IN THE CURRENT CONFIG ALWAYS FEE UNDER NORMAL CONDITIONS
+ * THIS WILL LEAK IN THE CURRENT CONFIG ALWAYS FREE UNDER NORMAL CONDITIONS
*/
ctx = (NCContext*)malloc(NCGetContextStructSize());
TASSERT(ctx != NULL)
@@ -308,27 +380,27 @@ static int TestPublicApiArgumentValidation()
TEST(NCDestroyContext(NULL), ARG_ERROR_POS_0)
/*reinit*/
- TEST(NCReInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
- TEST(NCReInitContext(ctx, NULL), ARG_ERROR_POS_1)
+ TEST(NCReInitContext(NULL, ctxRandom), ARG_ERROR_POS_0)
+ TEST(NCReInitContext(ctx, NULL), ARG_ERROR_POS_1)
/*Test null secret key*/
- TEST(NCGetPublicKey(ctx, NULL, &pubKey), ARG_ERROR_POS_1)
- TEST(NCGetPublicKey(ctx, &secKey, NULL), ARG_ERROR_POS_2)
+ TEST(NCGetPublicKey(ctx, NULL, &pubKey), ARG_ERROR_POS_1)
+ TEST(NCGetPublicKey(ctx, &secKey, NULL), ARG_ERROR_POS_2)
/*Test null secret key*/
TEST(NCValidateSecretKey(NULL, &secKey), ARG_ERROR_POS_0)
- TEST(NCValidateSecretKey(ctx, NULL), ARG_ERROR_POS_1)
+ TEST(NCValidateSecretKey(ctx, NULL), ARG_ERROR_POS_1)
/* Should fail with a zero key */
- TEST(NCValidateSecretKey(ctx, NCToSecKey(zero32)), E_OPERATION_FAILED)
+ TEST(NCValidateSecretKey(ctx, NCByteCastToSecretKey(zero32)), E_OPERATION_FAILED)
/*Verify sig64 args test*/
- TEST(NCVerifyDigest(NULL, &pubKey, zero32, sig64), ARG_ERROR_POS_0)
+ 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(NCVerifyData(NULL, &pubKey, zero32, 32, sig64), ARG_ERROR_POS_0)
+ 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_RANGE_ERROR_POS_3)
@@ -336,24 +408,24 @@ static int TestPublicApiArgumentValidation()
/*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)
- TEST(NCSignData(ctx, &secKey, zero32, NULL, 32, sig64), ARG_ERROR_POS_3)
- TEST(NCSignData(ctx, &secKey, zero32, zero32, 0, sig64), ARG_RANGE_ERROR_POS_4)
- TEST(NCSignData(ctx, &secKey, zero32, zero32, 32, NULL), ARG_ERROR_POS_5)
+ TEST(NCSignData(ctx, NULL, zero32, zero32, 32, sig64), ARG_ERROR_POS_1)
+ TEST(NCSignData(ctx, &secKey, NULL, zero32, 32, sig64), ARG_ERROR_POS_2)
+ TEST(NCSignData(ctx, &secKey, zero32, NULL, 32, sig64), ARG_ERROR_POS_3)
+ TEST(NCSignData(ctx, &secKey, zero32, zero32, 0, sig64), ARG_RANGE_ERROR_POS_4)
+ TEST(NCSignData(ctx, &secKey, zero32, zero32, 32, NULL), ARG_ERROR_POS_5)
/*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(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(NCEncrypt(NULL, &secKey, &pubKey, &cryptoData), ARG_ERROR_POS_0)
- TEST(NCEncrypt(ctx, NULL, &pubKey, &cryptoData), ARG_ERROR_POS_1)
- TEST(NCEncrypt(ctx, &secKey, NULL, &cryptoData), ARG_ERROR_POS_2)
- TEST(NCEncrypt(ctx, &secKey, &pubKey, NULL), ARG_ERROR_POS_3)
+ TEST(NCEncrypt(ctx, NULL, &pubKey, &cryptoData), ARG_ERROR_POS_1)
+ TEST(NCEncrypt(ctx, &secKey, NULL, &cryptoData), ARG_ERROR_POS_2)
+ TEST(NCEncrypt(ctx, &secKey, &pubKey, NULL), ARG_ERROR_POS_3)
/*Test invalid data size*/
cryptoData.dataSize = 0;
@@ -432,7 +504,7 @@ static int TestPublicApiArgumentValidation()
static int TestKnownKeys(const NCContext* context)
{
NCPublicKey pubKey;
- HexBytes* secKey1, * pubKey1, * secKey2, * pubKey2;
+ span_t secKey1, pubKey1, secKey2, pubKey2;
PRINTL("TEST: Known keys")
@@ -443,18 +515,18 @@ static int TestKnownKeys(const NCContext* context)
pubKey2 = FromHexString("421181660af5d39eb95e48a0a66c41ae393ba94ffeca94703ef81afbed724e5a", sizeof(NCPublicKey));
/*Test known keys*/
- TEST(NCValidateSecretKey(context, NCToSecKey(secKey1->data)), NC_SUCCESS);
+ TEST(NCValidateSecretKey(context, NCByteCastToSecretKey(secKey1.data)), NC_SUCCESS);
/* Recover a public key from secret key 1 */
- TEST(NCGetPublicKey(context, NCToSecKey(secKey1->data), &pubKey), NC_SUCCESS);
+ TEST(NCGetPublicKey(context, NCByteCastToSecretKey(secKey1.data), &pubKey), NC_SUCCESS);
/* Ensure the public key matches the known public key value */
- TEST(memcmp(pubKey1->data, &pubKey, sizeof(pubKey)), 0);
+ TEST(memcmp(pubKey1.data, &pubKey, sizeof(pubKey)), 0);
/* Repeat with second key */
- TEST(NCValidateSecretKey(context, (NCSecretKey*)secKey2->data), NC_SUCCESS);
- TEST(NCGetPublicKey(context, (NCSecretKey*)secKey2->data, &pubKey), NC_SUCCESS);
- TEST(memcmp(pubKey2->data, &pubKey, sizeof(pubKey)), 0);
+ TEST(NCValidateSecretKey(context, NCByteCastToSecretKey(secKey2.data)), NC_SUCCESS);
+ TEST(NCGetPublicKey(context, NCByteCastToSecretKey(secKey2.data), &pubKey), NC_SUCCESS);
+ TEST(memcmp(pubKey2.data, &pubKey, sizeof(pubKey)), 0);
PRINTL("\nPASSED: Known keys tests completed")
return 0;
@@ -481,21 +553,20 @@ static int TestCorrectEncryption(const NCContext* context)
NCEncryptionArgs cryptoData;
NCMacVerifyArgs macVerifyArgs;
- /* setup the crypto data structure */
- cryptoData.dataSize = TEST_ENC_DATA_SIZE;
- cryptoData.inputData = plainText;
- cryptoData.outputData = cipherText;
- cryptoData.nonce32 = nonce;
- cryptoData.hmacKeyOut32 = hmacKeyOut;
- cryptoData.version = NC_ENC_VERSION_NIP44;
+ PRINTL("TEST: Correct encryption")
+
+ ENSURE(NCSetEncryptionProperty(&cryptoData, NC_ENC_SET_VERSION, NC_ENC_VERSION_NIP44) == NC_SUCCESS);
+ ENSURE(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_NONCE, nonce, sizeof(nonce)) == NC_SUCCESS);
+ ENSURE(NCSetEncryptionPropertyEx(&cryptoData, NC_ENC_SET_NIP44_MAC_KEY, hmacKeyOut, NC_HMAC_KEY_SIZE) == NC_SUCCESS);
+
+ /* Assign the encryption material */
+ ENSURE(NCSetEncryptionData(&cryptoData, plainText, cipherText, TEST_ENC_DATA_SIZE) == NC_SUCCESS);
macVerifyArgs.nonce32 = nonce; /* nonce is shared */
macVerifyArgs.mac32 = mac;
macVerifyArgs.payload = cipherText;
macVerifyArgs.payloadSize = TEST_ENC_DATA_SIZE;
- PRINTL("TEST: Correct encryption")
-
/* init a sending and receiving key */
FillRandomData(&secKey1, sizeof(NCSecretKey));
FillRandomData(&secKey2, sizeof(NCSecretKey));
@@ -532,6 +603,162 @@ static int TestCorrectEncryption(const NCContext* context)
return 0;
}
+#ifdef NC_ENABLE_UTILS
+
+#include <noscryptutil.h>
+
+/* Padding tests taken from the nip44 repo vectors.json file */
+static const uint32_t _padTestActual[24] = { 16, 32, 33, 37, 45, 49, 64, 65, 100, 111, 200, 250, 320, 383, 384, 400, 500, 512, 515, 700, 800, 900, 1020, 65536 };
+static const uint32_t _padTestExpected[24] = { 32, 32, 64, 64, 64, 64, 64, 96, 128, 128, 224, 256, 320, 384, 384, 448, 512, 512, 640, 768, 896, 1024, 1024, 65536 };
+
+static int TestUtilNip44Encryption(
+ const NCContext* libCtx,
+ span_t sendKey,
+ span_t recvKey,
+ span_t nonce,
+ span_t expected,
+ span_t plainText
+)
+{
+ NCPublicKey recvPubKey;
+ uint8_t* outData;
+
+ ENSURE(NCValidateSecretKey(libCtx, NCByteCastToSecretKey(sendKey.data)) == NC_SUCCESS);
+ ENSURE(NCGetPublicKey(libCtx, NCByteCastToSecretKey(recvKey.data), &recvPubKey) == NC_SUCCESS);
+
+ /* Alloc cipher in nip44 encryption mode */
+ NCUtilCipherContext* ctx = NCUtilCipherAlloc(
+ NC_ENC_VERSION_NIP44,
+ NC_UTIL_CIPHER_MODE_ENCRYPT | NC_UTIL_CIPHER_ZERO_ON_FREE
+ );
+
+ ENSURE(ctx != NULL);
+
+ TEST(NCUtilCipherInit(ctx, plainText.data, plainText.size), NC_SUCCESS);
+
+ /* Nonce is required for nip44 encryption */
+ TEST(NCUtilCipherSetProperty(ctx, NC_ENC_SET_NIP44_NONCE, nonce.data, nonce.size), NC_SUCCESS);
+
+ /* Cipher update should return the */
+ TEST(NCUtilCipherUpdate(ctx, libCtx, NCByteCastToSecretKey(sendKey.data), &recvPubKey), NC_SUCCESS);
+
+ NCResult cipherOutputSize = NCUtilCipherGetOutputSize(ctx);
+
+ TEST(cipherOutputSize, expected.size);
+
+ outData = (uint8_t*)malloc(cipherOutputSize);
+ TASSERT(outData != NULL);
+
+ /* Read the encrypted payload to test */
+ TEST(NCUtilCipherReadOutput(ctx, outData, cipherOutputSize), cipherOutputSize);
+
+ /* Ensure encrypted payload matches */
+ TEST(memcmp(outData, expected.data, cipherOutputSize), 0);
+
+ free(outData);
+
+ /* Free encryption memory */
+ NCUtilCipherFree(ctx);
+
+ return 0;
+}
+
+static int TestUtilNip44Decryption(
+ const NCContext* libCtx,
+ span_t sendKey,
+ span_t recvKey,
+ span_t payload,
+ span_t expectedPt
+)
+{
+ NCPublicKey recvPubKey;
+ uint8_t* outData;
+
+ ENSURE(NCValidateSecretKey(libCtx, NCByteCastToSecretKey(sendKey.data)) == NC_SUCCESS);
+ ENSURE(NCGetPublicKey(libCtx, NCByteCastToSecretKey(recvKey.data), &recvPubKey) == NC_SUCCESS);
+
+ /* Alloc cipher in nip44 decryption mode */
+ NCUtilCipherContext* ctx = NCUtilCipherAlloc(
+ NC_ENC_VERSION_NIP44,
+ NC_UTIL_CIPHER_MODE_DECRYPT | NC_UTIL_CIPHER_ZERO_ON_FREE
+ );
+
+ ENSURE(ctx != NULL);
+
+ /* submit encrypted payload for ciphertext */
+ TEST(NCUtilCipherInit(ctx, payload.data, payload.size), NC_SUCCESS);
+
+ TEST(NCUtilCipherUpdate(ctx, libCtx, NCByteCastToSecretKey(sendKey.data), &recvPubKey), NC_SUCCESS);
+
+ NCResult plaintextSize = NCUtilCipherGetOutputSize(ctx);
+
+ TEST(plaintextSize, expectedPt.size);
+
+ outData = (uint8_t*)malloc(plaintextSize);
+
+ TASSERT(outData != NULL);
+
+ /* Read the encrypted payload to test */
+ TEST(NCUtilCipherReadOutput(ctx, outData, plaintextSize), plaintextSize);
+
+ /* Ensure encrypted payload matches */
+ TEST(memcmp(outData, expectedPt.data, plaintextSize), 0);
+
+ free(outData);
+
+ /* Free encryption memory */
+ NCUtilCipherFree(ctx);
+
+ return 0;
+}
+
+static int TestUtilFunctions(const NCContext* libCtx)
+{
+ PRINTL("TEST: Util functions")
+
+ for (int i = 0; i < 24; i++)
+ {
+ int32_t totalSize = _padTestExpected[i] + 67;
+
+ TEST(NCUtilGetEncryptionPaddedSize(NC_ENC_VERSION_NIP44, _padTestActual[i]), _padTestExpected[i]);
+ TEST(NCUtilGetEncryptionBufferSize(NC_ENC_VERSION_NIP44, _padTestActual[i]), totalSize);
+ }
+ {
+ PRINTL("TEST: NIP-44 util encryption")
+
+ /* From the nip44 vectors file */
+ span_t sendKey = FromHexString("0000000000000000000000000000000000000000000000000000000000000001", sizeof(NCSecretKey));
+ span_t recvKey = FromHexString("0000000000000000000000000000000000000000000000000000000000000002", sizeof(NCSecretKey));
+ span_t nonce = FromHexString("0000000000000000000000000000000000000000000000000000000000000001", NC_ENCRYPTION_NONCE_SIZE);
+ span_t payload = FromHexString("02000000000000000000000000000000000000000000000000000000000000000179ed06e5548ad3ff58ca920e6c0b4329f6040230f7e6e5641f20741780f0adc35a09794259929a02bb06ad8e8cf709ee4ccc567e9d514cdf5781af27a3e905e55b1b", 99);
+ span_t plainText = FromHexString("61", 1);
+
+ if (TestUtilNip44Encryption(libCtx, sendKey, recvKey, nonce, payload, plainText) != 0)
+ {
+ return 1;
+ }
+ }
+ {
+ PRINTL("TEST: NIP-44 util decryption");
+
+ /* From the nip44 vectors file */
+ span_t sendKey = FromHexString("0000000000000000000000000000000000000000000000000000000000000001", sizeof(NCSecretKey));
+ span_t recvKey = FromHexString("0000000000000000000000000000000000000000000000000000000000000002", sizeof(NCSecretKey));
+ span_t payload = FromHexString("02000000000000000000000000000000000000000000000000000000000000000179ed06e5548ad3ff58ca920e6c0b4329f6040230f7e6e5641f20741780f0adc35a09794259929a02bb06ad8e8cf709ee4ccc567e9d514cdf5781af27a3e905e55b1b", 99);
+ span_t plainText = FromHexString("61", 1);
+
+ if (TestUtilNip44Decryption(libCtx, sendKey, recvKey, payload, plainText) != 0)
+ {
+ return 1;
+ }
+ }
+
+ PRINTL("\nPASSED: Util functions tests completed")
+ return 0;
+}
+
+#endif
+
static void FillRandomData(void* pbBuffer, size_t length)
{