Skip to content

Commit

Permalink
refactor!: return NC_SUCCESS when validating secret key
Browse files Browse the repository at this point in the history
  • Loading branch information
VnUgE committed May 29, 2024
1 parent 718be80 commit 88c9095
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 11 deletions.
17 changes: 15 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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
Expand All @@ -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()

###########################
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# noscrypt

# noscrypt

<h4 align="left">
<a href="https://github.com/VnUgE/noscrypt/blob/master/LICENSE">
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion include/noscrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 3 additions & 1 deletion src/noscrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
12 changes: 7 additions & 5 deletions tests/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 88c9095

Please sign in to comment.