From 41f349ff802901dc8bd4db8324a462d371713f7f Mon Sep 17 00:00:00 2001 From: Johan Pascal Date: Wed, 2 Jun 2021 14:33:36 +0200 Subject: Fix windows build and tests + shorter intermediate cmake target name --- .gitignore | 2 ++ CMakeLists.txt | 11 +++--- src/CMakeLists.txt | 10 ++++-- src/generator/curve25519/CMakeLists.txt | 6 ++-- src/generator/ed448goldilocks/CMakeLists.txt | 6 ++-- src/include/word.h | 2 +- src/per_curve/eddsa.tmpl.hxx | 8 ++--- src/per_curve/point.tmpl.hxx | 7 ++++ src/per_curve/scalar.tmpl.c | 2 +- src/public_include/decaf/secure_buffer.hxx | 27 +++++++++++---- src/spongerng.c | 51 ++++++++++++++++++++++------ test/bench_decaf.cxx | 2 +- test/shakesum.c | 1 + test/test_decaf.cxx | 4 +-- 14 files changed, 101 insertions(+), 38 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..42dca46 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +src/generator/curve_data.pyc +src/generator/__pycache__/ diff --git a/CMakeLists.txt b/CMakeLists.txt index b0f5f76..b243e8d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,13 +10,16 @@ project(DECAF VERSION 1.0 LANGUAGES C CXX) set(DECAF_SO_VERSION "0") -find_package( PythonInterp 2.7 REQUIRED ) +find_package(PythonInterp 3 REQUIRED) option(ENABLE_SHARED "Build shared library." ON) option(ENABLE_STATIC "Build static library." ON) option(ENABLE_STRICT "Build with strict compile options." YES) option(ENABLE_TESTS "Enable compilation of tests." OFF) option(GENERATED_SOURCE_PATH "Where the generated source code is stored, default in the building tree" OFF) +if (CMAKE_SYSTEM_NAME STREQUAL "WindowsStore") + set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -nostdlib") +endif() if (GENERATED_SOURCE_PATH) set(GSOURCE_PATH ${GENERATED_SOURCE_PATH}) @@ -46,12 +49,12 @@ set(STRICT_OPTIONS_C ) set(STRICT_OPTIONS_CXX ) if(MSVC) if(ENABLE_STRICT) - set(STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP} /WX") + set(STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP} /WX /Zc:__cplusplus") endif() else() - set(STRICT_OPTIONS_CXX "${STRICT_OPTIONS_CXX} -std=c++11 -O2") + set(STRICT_OPTIONS_CXX "${STRICT_OPTIONS_CXX} -std=c++14 -O2") set(STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP} -Wall -Wuninitialized -Wno-deprecated-declarations -Wno-missing-field-initializers") - set(STRICT_OPTIONS_C "${STRICT_OPTIONS_C} -std=c99 -O2 -Wstrict-prototypes -Wno-error=strict-prototypes -fvisibility=hidden") + set(STRICT_OPTIONS_C "${STRICT_OPTIONS_C} -std=c99 -O2 -Wstrict-prototypes -Wno-error=strict-prototypes -fvisibility=hidden -Wno-error=implicit-function-declaration -Wno-error=attributes") if(CMAKE_C_COMPILER_ID MATCHES "Clang") set(STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP} -Wno-error=unknown-warning-option -Qunused-arguments -Wno-tautological-compare") set(STRICT_OPTIONS_CPP "${STRICT_OPTIONS_CPP} -Wno-unused-function -Wno-pass-failed") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c7f16f9..c9ede56 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,12 +36,18 @@ set(TARGET_ARCH_DIR_P25519 arch_32) # availables: arch_32, arch_arm_32, arch_neon, arch_ref64, arch_x86_64 set(TARGET_ARCH_DIR_P448 arch_32) -if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64") # x86_64 + +if(MSVC)# On MSVC Windows, Processor is always AMD64 on both platforms (x86/x64) + set(MSVC_ARCH ${CMAKE_CXX_COMPILER_ARCHITECTURE_ID})# ${MSVC_ARCH} MATCHES "X64" +else() + set(MSVC_ARCH ${CMAKE_SYSTEM_PROCESSOR})# just to have a value +endif() +if(${CMAKE_SYSTEM_PROCESSOR} MATCHES "x86_64" AND NOT MSVC)#Decaf doesn't support 64bits on MSVC yet message("Target architecture is x86_64") set(TARGET_ARCH_DIR arch_x86_64) set(TARGET_ARCH_DIR_P25519 arch_x86_64) set(TARGET_ARCH_DIR_P448 arch_x86_64) -elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arch64") # shall be arm64 bits, stick to ref64 +elseif(${CMAKE_SYSTEM_PROCESSOR} MATCHES "arch64") # shall be arm64 bits, stick to ref64. message("Target architecture is 64 bits general purpose(arm64 shall use this)") set(TARGET_ARCH_DIR arch_ref64) set(TARGET_ARCH_DIR_P25519 arch_ref64) diff --git a/src/generator/curve25519/CMakeLists.txt b/src/generator/curve25519/CMakeLists.txt index d657b87..41ef0b4 100644 --- a/src/generator/curve25519/CMakeLists.txt +++ b/src/generator/curve25519/CMakeLists.txt @@ -75,13 +75,13 @@ add_custom_command( COMMENT "Generating code for include/decaf/ed255.hxx" ) -add_custom_target(generatorCurve25519AndGenTablesCommon DEPENDS +add_custom_target(genC25519nTab DEPENDS ${GSOURCE_PATH}/c/curve25519/decaf.c ${GSOURCE_PATH}/c/curve25519/scalar.c ) add_custom_target(generatorCurve25519 DEPENDS generatorP25519 - generatorCurve25519AndGenTablesCommon + genC25519nTab ${GSOURCE_PATH}/c/curve25519/elligator.c ${GSOURCE_PATH}/c/curve25519/eddsa.c ${GSOURCE_PATH}/include/decaf/point_255.hxx @@ -98,7 +98,7 @@ add_executable(decaf_gen_tables_curve25519 EXCLUDE_FROM_ALL ${GSOURCE_PATH}/c/cu ${GSOURCE_PATH}/c/curve25519/scalar.c ${PROJECT_SOURCE_DIR}/src/utils.c $) -add_dependencies(decaf_gen_tables_curve25519 generatorCurve25519AndGenTablesCommon) +add_dependencies(decaf_gen_tables_curve25519 genC25519nTab) add_custom_target(decaf_tables_curve25519 COMMAND decaf_gen_tables_curve25519 > ${PROJECT_SOURCE_DIR}/src/curve25519/decaf_tables.c diff --git a/src/generator/ed448goldilocks/CMakeLists.txt b/src/generator/ed448goldilocks/CMakeLists.txt index d5ea932..22eda4a 100644 --- a/src/generator/ed448goldilocks/CMakeLists.txt +++ b/src/generator/ed448goldilocks/CMakeLists.txt @@ -82,13 +82,13 @@ add_custom_command( COMMENT "Generating code for include/decaf/ed448.hxx" ) -add_custom_target(generatorEd448AndGenTablesCommon DEPENDS +add_custom_target(genEd448nTab DEPENDS ${GSOURCE_PATH}/c/ed448goldilocks/scalar.c ${GSOURCE_PATH}/c/ed448goldilocks/decaf.c ) add_custom_target(generatorEd448goldilocks DEPENDS generatorP448 - generatorEd448AndGenTablesCommon + genEd448nTab ${GSOURCE_PATH}/c/ed448goldilocks/elligator.c ${GSOURCE_PATH}/c/ed448goldilocks/eddsa.c ${GSOURCE_PATH}/include/decaf/point_448.hxx @@ -105,7 +105,7 @@ add_executable(decaf_gen_tables_ed448goldilocks EXCLUDE_FROM_ALL ${GSOURCE_PATH} ${GSOURCE_PATH}/c/ed448goldilocks/scalar.c ${PROJECT_SOURCE_DIR}/src/utils.c $) -add_dependencies(decaf_gen_tables_ed448goldilocks generatorEd448AndGenTablesCommon) +add_dependencies(decaf_gen_tables_ed448goldilocks genEd448nTab) add_custom_target(decaf_tables_ed448goldilocks COMMAND decaf_gen_tables_ed448goldilocks > ${PROJECT_SOURCE_DIR}/src/ed448goldilocks/decaf_tables.c diff --git a/src/include/word.h b/src/include/word.h index 1d547a6..adfdb82 100644 --- a/src/include/word.h +++ b/src/include/word.h @@ -151,7 +151,7 @@ DECAF_INLINE mask_t bit_to_mask(uint8_t bit) { br_set_to_mask(mask_t x) { return vdupq_n_u32(x); } -#elif _WIN64 || __amd64__ || __X86_64__ || __aarch64__ +#elif __amd64__ || __X86_64__ || __aarch64__ /* || _WIN64 -> WIN64 does not support int128 so force the build on arch32 default so do not use this define for _WIN64*/ #define VECTOR_ALIGNED __attribute__((aligned(8))) typedef uint64_t big_register_t, uint64xn_t; diff --git a/src/per_curve/eddsa.tmpl.hxx b/src/per_curve/eddsa.tmpl.hxx index 1d15e71..7adde57 100644 --- a/src/per_curve/eddsa.tmpl.hxx +++ b/src/per_curve/eddsa.tmpl.hxx @@ -130,7 +130,7 @@ public: message.size(), 0, context.data(), - context.size() + static_cast(context.size()) ); return out; } @@ -147,7 +147,7 @@ public: ((const CRTP*)this)->keypair_, (const decaf_ed$(gf_shortname)_prehash_ctx_s*)ph.wrapped, ph.context_.data(), - ph.context_.size() + static_cast(ph.context_.size()) ); return out; } @@ -266,7 +266,7 @@ public: message.size(), 0, context.data(), - context.size() + static_cast(context.size()) ); } @@ -319,7 +319,7 @@ public: ((const CRTP*)this)->pub_.data(), (const decaf_ed$(gf_shortname)_prehash_ctx_s*)ph.wrapped, ph.context_.data(), - ph.context_.size() + static_cast(ph.context_.size()) )) { throw CryptoException(); } diff --git a/src/per_curve/point.tmpl.hxx b/src/per_curve/point.tmpl.hxx index 2e5c11d..bed64aa 100644 --- a/src/per_curve/point.tmpl.hxx +++ b/src/per_curve/point.tmpl.hxx @@ -122,7 +122,14 @@ public: /** Assign from signed int. */ inline Scalar& operator=(int64_t w) DECAF_NOEXCEPT { +#ifdef _MSC_VER +#pragma warning ( push) +#pragma warning ( disable : 4146) +#endif Scalar t(-(uint64_t)INT_MIN); +#ifdef _MSC_VER +#pragma warning ( pop) +#endif $(c_ns)_scalar_set_unsigned(s,(uint64_t)w - (uint64_t)INT_MIN); *this -= t; return *this; diff --git a/src/per_curve/scalar.tmpl.c b/src/per_curve/scalar.tmpl.c index 24d32cf..a911e19 100644 --- a/src/per_curve/scalar.tmpl.c +++ b/src/per_curve/scalar.tmpl.c @@ -216,7 +216,7 @@ API_NS(scalar_eq) ( static DECAF_INLINE void scalar_decode_short ( scalar_t s, const unsigned char *ser, - unsigned int nbytes + size_t nbytes ) { unsigned int i,j,k=0; for (i=0; i= 201103L - ((unsigned char *)&(s)[0]) - #else +#if __cplusplus >= 201103L ((unsigned char *)(s.data())) - #endif - ), size_(s.size()), zero_on_destroy_(false) {} +#else + ((unsigned char *)&(s)[0]) +#endif + ), size_(s.size()), zero_on_destroy_(false) {} /** Block from std::vector */ template inline Block(const std::vector &s) - : data_(((unsigned char *)&(s)[0])), size_(s.size()), zero_on_destroy_(false) {} + : data_( +#if __cplusplus >= 201103L + ((unsigned char *)(s.data())) +#else + ((unsigned char *)&(s)[0]) +#endif + ), size_(s.size()), zero_on_destroy_(false) {} /** Get const data */ inline const unsigned char *data() const DECAF_NOEXCEPT { return data_; } @@ -405,7 +411,11 @@ protected: inline void clear() DECAF_NOEXCEPT { if (is_mine) { really_bzero(ours.mine, T::size()); +#ifdef _MSC_VER + _aligned_free(ours.mine); +#else free(ours.mine); +#endif // _MSC_VER ours.yours = T::default_value(); is_mine = false; } @@ -482,6 +492,11 @@ template void SanitizingAllocator::deallocate(T* p, size_t size) DECAF_NOEXCEPT { if (p==NULL) return; really_bzero(reinterpret_cast(p), size); +#ifdef _MSC_VER + if (alignment) + _aligned_free(reinterpret_cast(p)); + else +#endif // _MSC_VER free(reinterpret_cast(p)); } diff --git a/src/spongerng.c b/src/spongerng.c index bba336e..c44d446 100644 --- a/src/spongerng.c +++ b/src/spongerng.c @@ -9,6 +9,18 @@ * @warning The SpongeRNG code isn't stable. Future versions are likely to * have different outputs. Of course, this only matters in deterministic mode. */ +#if defined(_MSC_VER) +#define _CRT_RAND_S +#include +# include +#include +#define open _open +#define read _read +#define close _close +typedef SSIZE_T ssize_t; +#else +# include +#endif #define __STDC_WANT_LIB_EXT1__ 1 /* for memset_s */ #include @@ -22,16 +34,7 @@ #include #include #include -#if defined(_MSC_VER) -# include -#include -#define open _open -#define read _read -#define close _close -typedef SSIZE_T ssize_t; -#else -# include -#endif + /** Get entropy from a CPU, preferably in the form of RDRAND, but possibly instead from RDTSC. */ static void get_cpu_entropy(uint8_t *entropy, size_t len) { @@ -161,12 +164,35 @@ decaf_error_t decaf_spongerng_init_from_file ( prng->sponge->params->remaining = !deterministic; /* A bit of a hack; this param is ignored for SHAKE */ if (!len) return DECAF_FAILURE; +#if defined _MSC_VER + /* no /dev/urandom on windows, use rand_s instead */ + if (strcmp(file, "/dev/urandom") == 0) { + unsigned int r; + uint8_t buffer[sizeof(unsigned int)]; + errno_t err; + while (len) { + int i; + err = rand_s(&r); + if (err != 0) { + return DECAF_FAILURE; + } + for (i = 0; i < sizeof(unsigned int); i++) { + buffer[i] = (uint8_t)(r & 0xff); + r >>= 8; + } + size_t consumed = (len > sizeof(buffer)) ? sizeof(buffer) : len; + decaf_sha3_update(prng->sponge, buffer, consumed); + len -= consumed; + } + } else { +#endif /* _MSC_VER */ + int fd = open(file, O_RDONLY); if (fd < 0) return DECAF_FAILURE; uint8_t buffer[128]; while (len) { - ssize_t red = read(fd, buffer, (len > sizeof(buffer)) ? sizeof(buffer) : len); + ssize_t red = read(fd, buffer, (len > sizeof(buffer)) ? sizeof(buffer) : (unsigned int)len); if (red <= 0) { close(fd); return DECAF_FAILURE; @@ -175,6 +201,9 @@ decaf_error_t decaf_spongerng_init_from_file ( len -= red; }; close(fd); +#if defined _MSC_VER + } +#endif /* _MSC_VER */ const uint8_t nope; decaf_spongerng_stir(prng,&nope,0); diff --git a/test/bench_decaf.cxx b/test/bench_decaf.cxx index 88a26c8..7c04ef6 100644 --- a/test/bench_decaf.cxx +++ b/test/bench_decaf.cxx @@ -102,7 +102,7 @@ public: if (strlen(s) < 25) printf("%*s",int(25-strlen(s)),""); fflush(stdout); i = j = 0; - ntests = NTESTS * factor; + ntests = static_cast(NTESTS * factor); nsamples = NSAMPLES; begin = now(); tsc_begin = rdtsc(); diff --git a/test/shakesum.c b/test/shakesum.c index 02d9397..e7a0d12 100644 --- a/test/shakesum.c +++ b/test/shakesum.c @@ -13,6 +13,7 @@ #include #include typedef SSIZE_T ssize_t; +#define read _read #else #include #endif diff --git a/test/test_decaf.cxx b/test/test_decaf.cxx index 80f8368..40f8f80 100644 --- a/test/test_decaf.cxx +++ b/test/test_decaf.cxx @@ -72,8 +72,8 @@ static void print(const char *name, const Scalar &x) { static void hexprint(const char *name, const SecureBuffer &buffer) { printf(" %s = 0x", name); - for (int i=buffer.size()-1; i>=0; i--) { - printf("%02x", buffer[i]); + for (auto i = buffer.rbegin(); i!= buffer.rend(); ++i) { + printf("%02x", *i); } printf("\n"); } -- cgit v1.2.3-59-g8ed1b