From f533694023133552d0d42933d779c95a5854343f Mon Sep 17 00:00:00 2001 From: vnugent Date: Mon, 6 May 2024 21:50:29 -0400 Subject: feat: CMake install & fetch-content test & updates --- CMakeLists.txt | 95 +++++++++++++++++++++++++++++++++++++----------------- Taskfile.yaml | 8 +++-- noscrypt.build.sln | 14 -------- tests/test.c | 12 ++++--- 4 files changed, 78 insertions(+), 51 deletions(-) delete mode 100644 noscrypt.build.sln diff --git a/CMakeLists.txt b/CMakeLists.txt index 1b8ce3d..b2a3e30 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,13 @@ cmake_minimum_required (VERSION 3.10) -project(noscrypt C) +project( + noscrypt + LANGUAGES C + DESCRIPTION "A compact, C90 cross-platform, cryptography library built specifically for nostr" + HOMEPAGE_URL "https://www.vaughnnugent.com/resources/software/modules/noscrypt" +) +set(_NC_PROJ_NAME "noscrypt") option(NC_BUILD_TESTS "Build tests" OFF) option(NC_DISABLE_INPUT_VALIDATION "Disables public function input validation" OFF) @@ -86,22 +92,22 @@ set(NOSCRYPT_HEADERS ) #static/shared library -add_library(${CMAKE_PROJECT_NAME} SHARED ${NOSCRYPT_SRCS} ${NOSCRYPT_HEADERS}) -add_library(${CMAKE_PROJECT_NAME}_static STATIC ${NOSCRYPT_SRCS} ${NOSCRYPT_HEADERS}) -target_compile_features(${CMAKE_PROJECT_NAME} PUBLIC c_std_90) #force compiler to use c90 standard for library +add_library(${_NC_PROJ_NAME} SHARED ${NOSCRYPT_SRCS} ${NOSCRYPT_HEADERS}) +add_library(${_NC_PROJ_NAME}_static STATIC ${NOSCRYPT_SRCS} ${NOSCRYPT_HEADERS}) +target_compile_features(${_NC_PROJ_NAME} PUBLIC c_std_90) #force compiler to use c90 standard for library #link libsecp256k1 if(MSVC) - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE secp256k1) - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE secp256k1) + target_link_libraries(${_NC_PROJ_NAME} PRIVATE secp256k1) + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE secp256k1) else() - target_link_libraries(${CMAKE_PROJECT_NAME} INTERFACE secp256k1) - target_link_libraries(${CMAKE_PROJECT_NAME}_static INTERFACE secp256k1) + target_link_libraries(${_NC_PROJ_NAME} INTERFACE secp256k1) + target_link_libraries(${_NC_PROJ_NAME}_static INTERFACE secp256k1) endif() #include secp256k1 headers -target_include_directories(${CMAKE_PROJECT_NAME} SYSTEM PUBLIC vendor/secp256k1/include) -target_include_directories(${CMAKE_PROJECT_NAME}_static SYSTEM PUBLIC vendor/secp256k1/include) +target_include_directories(${_NC_PROJ_NAME} SYSTEM PUBLIC vendor/secp256k1/include) +target_include_directories(${_NC_PROJ_NAME}_static SYSTEM PUBLIC vendor/secp256k1/include) ############################################# # @@ -151,13 +157,13 @@ if(CRYPTO_LIB STREQUAL "mbedtls") message(STATUS "Linking to MbedTLS crypto library") #include mbedtls headers - target_include_directories(${CMAKE_PROJECT_NAME} SYSTEM PUBLIC vendor/mbedtls/include) - target_include_directories(${CMAKE_PROJECT_NAME}_static SYSTEM PUBLIC vendor/mbedtls/include) + target_include_directories(${_NC_PROJ_NAME} SYSTEM PUBLIC vendor/mbedtls/include) + target_include_directories(${_NC_PROJ_NAME}_static SYSTEM PUBLIC vendor/mbedtls/include) if(NC_FETCH_MBEDTLS) #link to included mbedtls - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE mbedcrypto PRIVATE mbedtls) - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE mbedcrypto PRIVATE mbedtls) + target_link_libraries(${_NC_PROJ_NAME} PRIVATE mbedcrypto PRIVATE mbedtls) + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE mbedcrypto PRIVATE mbedtls) else() #find the library find_library(MBEDTLS_LIB_CRYPTO @@ -174,8 +180,8 @@ if(CRYPTO_LIB STREQUAL "mbedtls") message(STATUS "Found mbedtls tls library at ${MBEDTLS_LIB_TLS}") #link to the library - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE ${MBEDTLS_LIB_CRYPTO} PRIVATE ${MBEDTLS_LIB_TLS}) - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE ${MBEDTLS_LIB_CRYPTO} PRIVATE ${MBEDTLS_LIB_TLS}) + target_link_libraries(${_NC_PROJ_NAME} PRIVATE ${MBEDTLS_LIB_CRYPTO} PRIVATE ${MBEDTLS_LIB_TLS}) + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE ${MBEDTLS_LIB_CRYPTO} PRIVATE ${MBEDTLS_LIB_TLS}) endif() #enable mbedtls crypto library bindings @@ -187,13 +193,13 @@ elseif(CRYPTO_LIB STREQUAL "openssl") find_package(OpenSSL REQUIRED) #include openssl headers - target_include_directories(${CMAKE_PROJECT_NAME} SYSTEM PUBLIC vendor/openssl/include) - target_include_directories(${CMAKE_PROJECT_NAME}_static SYSTEM PUBLIC vendor/openssl/include) + target_include_directories(${_NC_PROJ_NAME} SYSTEM PUBLIC vendor/openssl/include) + target_include_directories(${_NC_PROJ_NAME}_static SYSTEM PUBLIC vendor/openssl/include) #link to openssl message(STATUS "Linking to OpenSSL crypto library") - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE OpenSSL::Crypto) - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE OpenSSL::Crypto) + target_link_libraries(${_NC_PROJ_NAME} PRIVATE OpenSSL::Crypto) + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE OpenSSL::Crypto) #enable openssl crypto library bindings list(APPEND NC_PROJ_DEFINTIONS OPENSSL_CRYPTO_LIB) @@ -202,8 +208,8 @@ elseif(CRYPTO_LIB STREQUAL "bcrypt") if(MSVC) #link bcrypt for Windows platforms - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE "bcrypt.lib") - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE "bcrypt.lib") + target_link_libraries(${_NC_PROJ_NAME} PRIVATE "bcrypt.lib") + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE "bcrypt.lib") else() message(FATAL_ERROR "Bcrypt is only supported on Windows platforms") endif() @@ -225,7 +231,7 @@ endif() if(MSVC) #global windows cl flags - target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE + target_compile_options(${_NC_PROJ_NAME} PRIVATE /sdl #enable additional security checks /TC #compile as c /GS #buffer security check @@ -253,12 +259,12 @@ if(MSVC) #configure gcc flags elseif(CMAKE_COMPILER_IS_GNUCC) - target_compile_options(${CMAKE_PROJECT_NAME} PRIVATE -Wextra -fstack-protector) + target_compile_options(${_NC_PROJ_NAME} PRIVATE -Wextra -fstack-protector) #if debug build enable additional debug flags if(build_type STREQUAL "debug") target_compile_options( - ${CMAKE_PROJECT_NAME} + ${_NC_PROJ_NAME} PRIVATE -g @@ -289,8 +295,8 @@ if(NC_INCLUDE_MONOCYPHER) "vendor/monocypher/monocypher.h" ) - target_link_libraries(${CMAKE_PROJECT_NAME} PRIVATE monocypher) - target_link_libraries(${CMAKE_PROJECT_NAME}_static PRIVATE monocypher) + target_link_libraries(${_NC_PROJ_NAME} PRIVATE monocypher) + target_link_libraries(${_NC_PROJ_NAME}_static PRIVATE monocypher) #share mc header with project target_include_directories(monocypher SYSTEM PUBLIC vendor/monocypher) @@ -319,21 +325,50 @@ if(NC_INCLUDE_MONOCYPHER) endif() #Set NC variables to both projects -target_compile_definitions(${CMAKE_PROJECT_NAME} PRIVATE ${NC_PROJ_DEFINTIONS}) -target_compile_definitions(${CMAKE_PROJECT_NAME}_static PRIVATE ${NC_PROJ_DEFINTIONS}) +target_compile_definitions(${_NC_PROJ_NAME} PRIVATE ${NC_PROJ_DEFINTIONS}) +target_compile_definitions(${_NC_PROJ_NAME}_static PRIVATE ${NC_PROJ_DEFINTIONS}) #TESTS if(NC_BUILD_TESTS) #add test executable and link to library add_executable(nctest tests/test.c) - target_link_libraries(nctest ${CMAKE_PROJECT_NAME}_static) + target_link_libraries(nctest ${_NC_PROJ_NAME}_static) target_include_directories(nctest PRIVATE include) #enable c11 for testing target_compile_features(nctest PRIVATE c_std_11) endif() +########################### +# +# Installing +# +########################### + +#export shared library +install(TARGETS ${_NC_PROJ_NAME} + EXPORT MyLibraryTargets + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + INCLUDES DESTINATION include +) + +#export static library +install(TARGETS ${_NC_PROJ_NAME}_static + EXPORT MyLibraryTargets + LIBRARY DESTINATION lib + ARCHIVE DESTINATION lib + RUNTIME DESTINATION bin + INCLUDES DESTINATION include +) + +install(FILES + include/noscrypt.h + include/platform.h + DESTINATION noscrypt +) # Enable Hot Reload for MSVC compilers if supported. if (POLICY CMP0141) diff --git a/Taskfile.yaml b/Taskfile.yaml index 6524c5c..2fbda5a 100644 --- a/Taskfile.yaml +++ b/Taskfile.yaml @@ -43,13 +43,17 @@ tasks: cmds: - task: build-debug - cmd: cd {{.CMAKE_BUILD_DIR}} && {{if eq OS "windows"}}debug/nctest.exe{{else}}./nctest{{end}} + + install: + desc: "Uses cmake to install the library on your system" + cmds: + - cmd: cmake --install {{.CMAKE_BUILD_DIR}} {{.CLI_ARGS}} #CI ONLY!! #called by build pipeline to build module build: - desc: "DO NOT RUN! CI Only" cmds: - echo "building project {{.PROJECT_NAME}}" - cmd: powershell -Command "mkdir bin/ -Force" @@ -109,7 +113,7 @@ tasks: desc: "Packs up the project source code and creates a tarball in the builds binary directory" vars: TARGET_SOURCE: '{{.PROJECT_DIR}}/{{.BINARY_DIR}}/{{.PROJECT_NAME}}-src.tgz' - SOURCE_FILES: 'CMakeLists.txt src include license tests Taskfile.yaml' + SOURCE_FILES: 'CMakeLists.txt src include license tests vendor readme.md Taskfile.yaml' cmds: #tar up the source diff --git a/noscrypt.build.sln b/noscrypt.build.sln deleted file mode 100644 index 58ea566..0000000 --- a/noscrypt.build.sln +++ /dev/null @@ -1,14 +0,0 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.31903.59 -MinimumVisualStudioVersion = 10.0.40219.1 -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/tests/test.c b/tests/test.c index 3aaf067..bcda32f 100644 --- a/tests/test.c +++ b/tests/test.c @@ -205,23 +205,24 @@ static int TestEcdsa(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKe FillRandomData(invalidSig, sizeof(invalidSig)); FillRandomData(sigEntropy, sizeof(sigEntropy)); + /* This is the sha256 digest of the message charater buffer above */ digestHex = FromHexString("58884db8f9b2d5583a54b44daeccf029af4dd2874aa5e3dc0e55febebab55d18", 32); - /* Sign and verify sig64 */ + /* 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); } - /* Sign and verify raw data */ + /* Sign and verify the raw message */ { uint8_t sig[64]; TEST(NCSignData(context, secKey, sigEntropy, (uint8_t*)message, strlen32(message), sig), NC_SUCCESS); TEST(NCVerifyData(context, pubKey, (uint8_t*)message, strlen32(message), sig), NC_SUCCESS); } - /* ensure the signature is the same for signing data and sig64 */ + /* Tests that signing the message and it's digest result in the same signature */ { uint8_t sig1[64]; uint8_t sig2[64]; @@ -234,7 +235,7 @@ static int TestEcdsa(NCContext* context, NCSecretKey* secKey, NCPublicKey* pubKe TEST(memcmp(sig1, sig2, 64), 0); } - /* Try signing data then veriyfing the sig64 */ + /* Checks that the signature raw message can be verified against the digest of the message */ { uint8_t sig[64]; @@ -292,7 +293,8 @@ static int TestPublicApiArgumentValidation(void) /* * Test null context * NOTE: This is never freed, this shouldnt be an issue - * for testing, but this will leak memory. + * for testing, but this will leak memory. (libsecp256k2 + * allocates internally) */ TEST(NCDestroyContext(NULL), ARG_ERROR_POS_0) -- cgit