From 770cc8bcce0568fa4a860c3ca3776831430d433b Mon Sep 17 00:00:00 2001 From: pvn Date: Sat, 20 May 2023 03:31:35 -0400 Subject: [PATCH] Add CMake and Windows Visual Studio build --- CMakeLists.txt | 78 +++++++++++++++++++++++++++++++++++++++++++++++++ base64.h | 12 ++++++++ build.cmake.sh | 26 +++++++++++++++++ clock_gettime.h | 39 +++++++++++++++++++++++++ configurator.c | 4 +++ cursor.h | 11 +++++++ nostril.c | 15 ++++++++++ 7 files changed, 185 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 build.cmake.sh create mode 100644 clock_gettime.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..05450b4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,78 @@ +if (MSVC) + cmake_minimum_required(VERSION 3.26) +else() + cmake_minimum_required(VERSION 3.15) +endif() +set(CMAKE_BUILD_TYPE Debug) +project (nostril C) + +include_directories(${CMAKE_SOURCE_DIR}/ext/secp256k1/include) +add_subdirectory(${CMAKE_SOURCE_DIR}/ext/secp256k1) + +#////////////////////////// +#windows.h header file has macros for min and max, nostril defines max +#////////////////////////// + +if (MSVC) + add_definitions(-DNOMINMAX) + add_definitions(-D_CRT_SECURE_NO_WARNINGS) + add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) +endif() + +#////////////////////////// +#sources +#////////////////////////// + +set(src ${src}) +set(src ${src} hex.h) +set(src ${src} proof.h) +set(src ${src} cursor.h) +set(src ${src} endian.h) +set(src ${src} random.h) +set(src ${src} sha256.h) +set(src ${src} sha256.c) +set(src ${src} base64.h) +set(src ${src} base64.c) +set(src ${src} aes.h) +set(src ${src} aes.c) +if (MSVC) + set(src ${src} clock_gettime.h) +endif() + +#////////////////////////// +#link with libraries +#lib_dep contains a cascade definition of all the libraries needed to link +#////////////////////////// + +set(lib_dep ${lib_dep}) +if (MSVC) + set(lib_dep ${lib_dep} ${CMAKE_BINARY_DIR}/ext/secp256k1/src/Debug/libsecp256k1.lib) + set(lib_dep ${lib_dep} Bcrypt.lib) +else() + set(lib_dep ${lib_dep} ${CMAKE_BINARY_DIR}/ext/secp256k1/src/libsecp256k1.a) +endif() + + +#////////////////////////// +#executables +#////////////////////////// + +# tool to generate file 'config.h' , generate from a shell with +# configurator.exe > config.h +add_executable(configurator configurator.c) + +add_executable(nostril ${src} nostril.c) +target_link_libraries (nostril ${lib_dep}) + +#////////////////////////// +# generate config.h +#////////////////////////// + +add_custom_command( + TARGET configurator + POST_BUILD + COMMAND configurator > config.h + COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_BINARY_DIR}/config.h" ${CMAKE_SOURCE_DIR} + COMMENT "generating config.h" +) + diff --git a/base64.h b/base64.h index cef30d2..fb37d49 100644 --- a/base64.h +++ b/base64.h @@ -6,6 +6,18 @@ #include #include +#if defined(_MSC_VER) +#ifndef _SSIZE_T_DEFINED +#ifdef _WIN64 +typedef unsigned __int64 ssize_t; +#else +typedef _W64 unsigned int ssize_t; +#endif +#define _SSIZE_T_DEFINED +#endif +#endif + + /** * base64_maps_t - structure to hold maps for encode/decode */ diff --git a/build.cmake.sh b/build.cmake.sh new file mode 100644 index 0000000..d680d29 --- /dev/null +++ b/build.cmake.sh @@ -0,0 +1,26 @@ +#!/bin/bash +mkdir -p build +pushd build +if [[ "$OSTYPE" == "linux-gnu"* ]]; then +cmake .. +cmake --build . --target configurator +pushd ext +pushd secp256k1 +make +popd +popd +elif [[ "$OSTYPE" == "darwin"* ]]; then +cmake .. +pushd ext +pushd secp256k1 +make +popd +popd +elif [[ "$OSTYPE" == "msys" ]]; then +cmake .. --fresh +fi +sleep 1 +cmake --build . +popd +exit + diff --git a/clock_gettime.h b/clock_gettime.h new file mode 100644 index 0000000..b3f5a62 --- /dev/null +++ b/clock_gettime.h @@ -0,0 +1,39 @@ +#ifndef CLOCK_GETTIME_H_ +#define CLOCK_GETTIME_H_ + +#include + +#define MS_PER_SEC 1000ULL // MS = milliseconds +#define US_PER_MS 1000ULL // US = microseconds +#define HNS_PER_US 10ULL // HNS = hundred-nanoseconds (e.g., 1 hns = 100 ns) +#define NS_PER_US 1000ULL +#define HNS_PER_SEC (MS_PER_SEC * US_PER_MS * HNS_PER_US) +#define NS_PER_HNS (100ULL) // NS = nanoseconds +#define NS_PER_SEC (MS_PER_SEC * US_PER_MS * NS_PER_US) + +int clock_gettime(int X, struct timespec* tv) +{ + static LARGE_INTEGER ticksPerSec; + LARGE_INTEGER ticks; + + if (!ticksPerSec.QuadPart) + { + QueryPerformanceFrequency(&ticksPerSec); + if (!ticksPerSec.QuadPart) + { + errno = ENOTSUP; + return -1; + } + } + + QueryPerformanceCounter(&ticks); + + tv->tv_sec = (long)(ticks.QuadPart / ticksPerSec.QuadPart); + tv->tv_nsec = (long)(((ticks.QuadPart % ticksPerSec.QuadPart) * NS_PER_SEC) / ticksPerSec.QuadPart); + + return 0; +} + + + +#endif \ No newline at end of file diff --git a/configurator.c b/configurator.c index 68d7fc6..b22e4ea 100644 --- a/configurator.c +++ b/configurator.c @@ -37,7 +37,11 @@ #include #include #include +#ifdef _MSC_VER +#define sleep Sleep +#else #include +#endif #ifdef _MSC_VER #define popen _popen diff --git a/cursor.h b/cursor.h index c9b5fe7..e682f8f 100644 --- a/cursor.h +++ b/cursor.h @@ -6,8 +6,19 @@ #include #include +#ifdef _MSC_VER +# pragma warning(push) +# pragma warning(disable: 4005) +# pragma warning(disable: 4477) +#endif + +#ifdef _MSC_VER +#define unlikely +#define likely +#else #define unlikely(x) __builtin_expect((x),0) #define likely(x) __builtin_expect((x),1) +#endif struct cursor { unsigned char *start; diff --git a/nostril.c b/nostril.c index 58111fd..5bd279e 100644 --- a/nostril.c +++ b/nostril.c @@ -5,7 +5,15 @@ #include #include #include +#ifdef _MSC_VER +#else #include +#endif + +#ifdef _MSC_VER +#include "clock_gettime.h" +#define CLOCK_MONOTONIC 0 +#endif #include "secp256k1.h" #include "secp256k1_ecdh.h" @@ -639,7 +647,11 @@ static int make_encrypted_dm(secp256k1_context *ctx, struct key *key, unsigned char iv[16]; unsigned char compressed_pubkey[33]; int content_len = strlen(ev->content); +#ifdef _MSC_VER + unsigned char* encbuf = malloc(content_len + (content_len % 16) + 1); +#else unsigned char encbuf[content_len + (content_len % 16) + 1]; +#endif struct cursor cur; secp256k1_pubkey pubkey; @@ -714,6 +726,9 @@ static int make_encrypted_dm(secp256k1_context *ctx, struct key *key, cur.p += 65; +#ifdef _MSC_VER + free(encbuf); +#endif return 1; }