From dc71f861df8929deee300368b88ef47d45560695 Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 1 Jul 2024 15:05:34 -0400 Subject: fix: #7 fix confusing inline functions --- CMakeLists.txt | 2 ++ include/noscrypt.h | 39 +++++++-------------------------------- src/noscrypt.c | 45 +++++++++++++++++++++++++++++++++++++++++++-- tests/test.c | 6 +++--- 4 files changed, 55 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a1f707..53fbe29 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -142,6 +142,8 @@ if(CRYPTO_LIB STREQUAL "") elseif(OPENSSL_FOUND) set(CRYPTO_LIB "openssl") endif() + + message(STATUS "No crypto library was specified, defaulting to ${CRYPTO_LIB}") endif() #Include mbedtls if enabled diff --git a/include/noscrypt.h b/include/noscrypt.h index f408dfc..bdfaa9f 100644 --- a/include/noscrypt.h +++ b/include/noscrypt.h @@ -212,29 +212,17 @@ typedef struct nc_mac_verify { */ /* -* A helper function to cast a buffer to a NCSecretKey struct -* @param key The buffer to cast -* @return A pointer to the NCSecretKey struct +* Casts a buffer pointer to a NCSecretKey pointer */ -static _nc_fn_inline NCSecretKey* NCToSecKey(uint8_t key[NC_SEC_KEY_SIZE]) -{ - return (NCSecretKey*)key; -} +#define NCByteCastToSecretKey(key) (NCSecretKey*)key /* -* A helper function to cast a buffer to a NCPublicKey struct -* @param key The buffer to cast -* @return A pointer to the NCPublicKey struct +* Casts a buffer pointer to a NCPublicKey pointer */ -static _nc_fn_inline NCPublicKey* NCToPubKey(uint8_t key[NC_PUBKEY_SIZE]) -{ - return (NCPublicKey*)key; -} +#define NCByteCastToPublicKey(key) (NCPublicKey*)key -static _nc_fn_inline NCResult NCResultWithArgPosition(NCResult err, uint8_t argPosition) -{ - return -(((NCResult)argPosition << NC_ARG_POSITION_OFFSET) | -err); -} + +NC_EXPORT NCResult NC_CC NCResultWithArgPosition(NCResult err, uint8_t argPosition); /* * Parses an error code and returns the error code and the argument position @@ -243,20 +231,7 @@ that caused the error. * @param argPositionOut A pointer to the argument position to write to * @return The error code */ -static _nc_fn_inline int NCParseErrorCode(NCResult result, uint8_t* argPositionOut) -{ - NCResult asPositive; - int code; - - /* convert result to a positive value*/ - asPositive = -result; - - /* Get the error code from the lower 8 bits and the argument position from the upper 8 bits*/ - code = -(asPositive & NC_ERROR_CODE_MASK); - *argPositionOut = (asPositive >> NC_ARG_POSITION_OFFSET) & 0xFF; - - return code; -} +NC_EXPORT int NC_CC NCParseErrorCode(NCResult result, uint8_t* argPositionOut); /*-------------------------------------- * LIB CONTEXT API diff --git a/src/noscrypt.c b/src/noscrypt.c index 4254ee2..c523262 100644 --- a/src/noscrypt.c +++ b/src/noscrypt.c @@ -442,6 +442,36 @@ Cleanup: /* * EXTERNAL API FUNCTIONS */ + + +NC_EXPORT NCResult NC_CC NCResultWithArgPosition(NCResult err, uint8_t argPosition) +{ + return -(((NCResult)argPosition << NC_ARG_POSITION_OFFSET) | -err); +} + + +NC_EXPORT int NC_CC NCParseErrorCode(NCResult result, uint8_t* argPositionOut) +{ + NCResult asPositive; + int code; + + /* convert result to a positive value*/ + asPositive = -result; + + /* Get the error code from the lower 8 bits and the argument position from the upper 8 bits*/ + code = -(asPositive & NC_ERROR_CODE_MASK); + *argPositionOut = (asPositive >> NC_ARG_POSITION_OFFSET) & 0xFF; + + return code; +} + +/* ============================= +* +* Context functions +* +* ============================= +*/ + NC_EXPORT uint32_t NC_CC NCGetContextStructSize(void) { return sizeof(NCContext); @@ -499,7 +529,13 @@ NC_EXPORT NCResult NC_CC NCDestroyContext(NCContext* ctx) return NC_SUCCESS; } -/* KEY Functions */ +/* ============================= +* +* ECDSA functions +* +* ============================= +*/ + NC_EXPORT NCResult NC_CC NCGetPublicKey( const NCContext* ctx, const NCSecretKey* sk, @@ -681,7 +717,12 @@ NC_EXPORT NCResult NC_CC NCVerifyData( return NCVerifyDigest(ctx, pk, digest, sig64); } -/* ECDH Functions */ +/* ============================= +* +* ECDH functions +* +* ============================= +*/ NC_EXPORT NCResult NC_CC NCGetSharedSecret( const NCContext* ctx, diff --git a/tests/test.c b/tests/test.c index 6e3851e..5feae79 100644 --- a/tests/test.c +++ b/tests/test.c @@ -378,7 +378,7 @@ static int TestPublicApiArgumentValidation() TEST(NCValidateSecretKey(NULL, &secKey), ARG_ERROR_POS_0) 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) @@ -502,10 +502,10 @@ 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); -- cgit