From 88c9095743a12cf8fc1793c607ba3a1e4fa86483 Mon Sep 17 00:00:00 2001 From: vnugent Date: Wed, 29 May 2024 13:25:51 -0400 Subject: refactor!: return NC_SUCCESS when validating secret key --- CMakeLists.txt | 17 +++++++++++++++-- README.md | 3 ++- Taskfile.yaml | 2 +- include/noscrypt.h | 2 +- src/noscrypt.c | 4 +++- tests/test.c | 12 +++++++----- 6 files changed, 29 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fb2c30f..7a1f707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,7 +21,7 @@ option(NC_DISABLE_INPUT_VALIDATION "Disables public function input validation" O option(NC_FETCH_MBEDTLS "Fetch Mbed-TLS from it's source repository locally" OFF) option(NC_FETCH_SECP256K1 "Fetch and locally build secp256k1 source code" ON) option(NC_INCLUDE_MONOCYPHER "Statically link to vendored monocypher library" ON) -set(CRYPTO_LIB "" CACHE STRING "The crypto library to link to (mbedtls, openssl)") +set(CRYPTO_LIB "" CACHE STRING "The crypto library to link to (mbedtls, openssl, bcrypt)") set(CRYPTO_LIB_DIR "" CACHE STRING "The path to the crypto library if it's not globally available") set(SECP256K1_LIB_DIR "" CACHE STRING "An optional path to search for the secp256k1 library if not globally installed") @@ -354,7 +354,11 @@ endif() target_compile_definitions(${_NC_PROJ_NAME} PRIVATE ${NC_PROJ_DEFINTIONS}) target_compile_definitions(${_NC_PROJ_NAME}_static PRIVATE ${NC_PROJ_DEFINTIONS}) -#TESTS +############################ +# +# TESTS +# +########################### if(NC_BUILD_TESTS) #add test executable and link to shared library for more realistic usage @@ -365,6 +369,15 @@ if(NC_BUILD_TESTS) #enable c11 for testing target_compile_features(nctest PRIVATE c_std_11) + + enable_testing() + + add_test( + NAME nctest + COMMAND nctest + CONFIGURATIONS ${CMAKE_BUILD_TYPE} + ) + endif() ########################### diff --git a/README.md b/README.md index 67dba3c..e351cb2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -# noscrypt + +# noscrypt

diff --git a/Taskfile.yaml b/Taskfile.yaml index eb16de0..a79921c 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -42,7 +42,7 @@ tasks: desc: "Builds a local copy of the library in a debug configuration, then runs the test executable" cmds: - task: build-debug - - cmd: cd {{.CMAKE_BUILD_DIR}} && {{if eq OS "windows"}}debug/nctest.exe{{else}}./nctest{{end}} + - cmd: cd {{.CMAKE_BUILD_DIR}} && ctest -C Debug --verbose install: desc: "Uses cmake to install the library on your system" diff --git a/include/noscrypt.h b/include/noscrypt.h index 036d0bd..cdc74fe 100644 --- a/include/noscrypt.h +++ b/include/noscrypt.h @@ -314,7 +314,7 @@ NC_EXPORT NCResult NC_CC NCGetPublicKey( is functionally the same as calling secp256k1_ec_seckey_verify. * @param ctx A pointer to the existing library context * @param sk A pointer to the secret key to verify -* @return 1 if the secret key is valid, 0 if it is not, otherwise an error code +* @return NC_SUCCESS if the secret key is valid, otherwise an error code */ NC_EXPORT NCResult NC_CC NCValidateSecretKey( const NCContext* ctx, diff --git a/src/noscrypt.c b/src/noscrypt.c index 258ba8d..f1aabd4 100644 --- a/src/noscrypt.c +++ b/src/noscrypt.c @@ -542,7 +542,9 @@ NC_EXPORT NCResult NC_CC NCValidateSecretKey(const NCContext* ctx, const NCSecre CHECK_CONTEXT_STATE(ctx, 0) /* Validate the secret key */ - return secp256k1_ec_seckey_verify(ctx->secpCtx, sk->key); + return secp256k1_ec_seckey_verify(ctx->secpCtx, sk->key) == 1 + ? NC_SUCCESS + : E_OPERATION_FAILED; } /* Ecdsa Functions */ diff --git a/tests/test.c b/tests/test.c index 0fa5bc1..7ed7ea5 100644 --- a/tests/test.c +++ b/tests/test.c @@ -184,7 +184,7 @@ static int InitKepair(const NCContext* context, NCSecretKey* secKey, NCPublicKey ENSURE(memcmp(zero32, secKey, 32) != 0); /* Ensure the key is valid, result should be 1 on success */ - TEST(NCValidateSecretKey(context, secKey), 1); + TEST(NCValidateSecretKey(context, secKey), NC_SUCCESS); /* Generate a public key from the secret key */ TEST(NCGetPublicKey(context, secKey, pubKey), NC_SUCCESS); @@ -318,6 +318,8 @@ static int TestPublicApiArgumentValidation() /*Test null secret key*/ 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), NULL), E_OPERATION_FAILED) /*Verify sig64 args test*/ TEST(NCVerifyDigest(NULL, &pubKey, zero32, sig64), ARG_ERROR_POS_0) @@ -441,7 +443,7 @@ static int TestKnownKeys(const NCContext* context) pubKey2 = FromHexString("421181660af5d39eb95e48a0a66c41ae393ba94ffeca94703ef81afbed724e5a", sizeof(NCPublicKey)); /*Test known keys*/ - TEST(NCValidateSecretKey(context, NCToSecKey(secKey1->data)), 1); + TEST(NCValidateSecretKey(context, NCToSecKey(secKey1->data)), NC_SUCCESS); /* Recover a public key from secret key 1 */ TEST(NCGetPublicKey(context, NCToSecKey(secKey1->data), &pubKey), NC_SUCCESS); @@ -450,7 +452,7 @@ static int TestKnownKeys(const NCContext* context) TEST(memcmp(pubKey1->data, &pubKey, sizeof(pubKey)), 0); /* Repeat with second key */ - TEST(NCValidateSecretKey(context, (NCSecretKey*)secKey2->data), 1); + 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); @@ -501,8 +503,8 @@ static int TestCorrectEncryption(const NCContext* context) /* nonce is shared */ FillRandomData(nonce, sizeof(nonce)); - ENSURE(NCValidateSecretKey(context, &secKey1) == 1); - ENSURE(NCValidateSecretKey(context, &secKey2) == 1); + ENSURE(NCValidateSecretKey(context, &secKey1) == NC_SUCCESS); + ENSURE(NCValidateSecretKey(context, &secKey2) == NC_SUCCESS); ENSURE(NCGetPublicKey(context, &secKey1, &pubKey1) == NC_SUCCESS); ENSURE(NCGetPublicKey(context, &secKey2, &pubKey2) == NC_SUCCESS); -- cgit