From 17c985bf2febecc0f86f2979092f41e44bd18c47 Mon Sep 17 00:00:00 2001 From: Gabriel Espinoza Date: Thu, 17 Apr 2025 11:16:36 -0700 Subject: [PATCH 1/5] added microsoft visual c++ compatibility to CMakeLists.txt --- CMakeLists.txt | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 23cb3ae..8a0e029 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,7 +15,11 @@ cmake_minimum_required(VERSION 2.8 FATAL_ERROR) project(bcrypt) -enable_language(ASM) +if(MSVC ) + enable_language(ASM_MASM) +else() + enable_language(ASM) +endif() set(MYLIB_VERSION_MAJOR 1) set(MYLIB_VERSION_MINOR 0) @@ -35,10 +39,15 @@ endif () set( CMAKE_COLOR_MAKEFILE ON ) -set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall --std=c++11 -O3" ) -set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3" ) +set(CMAKE_CXX_STANDARD 11) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +if (NOT MSVC) + set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -O3" ) + set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -O3" ) + set( CMAKE_ASM_FLAGS "${CXXFLAGS} -x assembler-with-cpp") +endif() -set( CMAKE_ASM_FLAGS "${CXXFLAGS} -x assembler-with-cpp") set( SRCFILES ${CMAKE_CURRENT_SOURCE_DIR}/src/bcrypt.c From 921b1e912c31cd2dc4b6e763fab68a0836697cdd Mon Sep 17 00:00:00 2001 From: Gabriel Espinoza Date: Thu, 19 Jun 2025 16:27:46 -0700 Subject: [PATCH 2/5] removed cmake redundant version variables fix cmake warnings --- CMakeLists.txt | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a0e029..94164ea 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,9 +11,9 @@ # ################################################################################### -cmake_minimum_required(VERSION 2.8 FATAL_ERROR) +cmake_minimum_required(VERSION 3.5) -project(bcrypt) +project(bcrypt VERSION 1.0.0 ) if(MSVC ) enable_language(ASM_MASM) @@ -21,11 +21,6 @@ else() enable_language(ASM) endif() -set(MYLIB_VERSION_MAJOR 1) -set(MYLIB_VERSION_MINOR 0) -set(MYLIB_VERSION_PATCH 0) -set(MYLIB_VERSION_STRING ${MYLIB_VERSION_MAJOR}.${MYLIB_VERSION_MINOR}.${MYLIB_VERSION_PATCH}) - # just doing cmake . will build a shared or static lib and honor existing environment setting # to force build static, cmake . -DBUILD_SHARED_LIBS=Off # to force build shared, cmake . -DBUILD_SHARED_LIBS=On @@ -68,7 +63,7 @@ add_library( ${SRCFILES} ) -set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${MYLIB_VERSION_STRING} SOVERSION ${MYLIB_VERSION_MAJOR}) +set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER include/bcrypt/BCrypt.hpp) @@ -94,9 +89,6 @@ SET(CPACK_GENERATOR "DEB") SET(CPACK_SET_DESTDIR ON) SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "Manuel Romei") -SET(CPACK_PACKAGE_VERSION "1.0.0") -SET(CPACK_PACKAGE_VERSION_MAJOR "1") -SET(CPACK_PACKAGE_VERSION_MINOR "0") -SET(CPACK_PACKAGE_VERSION_PATCH "0") + INCLUDE(CPack) From 1d4aaae228626e9376c3a68ebac60e27671f3ba5 Mon Sep 17 00:00:00 2001 From: Gabriel Espinoza Date: Thu, 19 Jun 2025 17:14:55 -0700 Subject: [PATCH 3/5] fixed msvc warnings --- CMakeLists.txt | 10 +++++++++- src/bcrypt.c | 17 ++++++++--------- src/crypt_blowfish.c | 2 +- src/wrapper.h | 8 ++++++++ 4 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 src/wrapper.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 94164ea..33cbafb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,7 +13,7 @@ cmake_minimum_required(VERSION 3.5) -project(bcrypt VERSION 1.0.0 ) +project(bcrypt VERSION 1.0.0 LANGUAGES C CXX ) if(MSVC ) enable_language(ASM_MASM) @@ -50,6 +50,13 @@ set( SRCFILES ${CMAKE_CURRENT_SOURCE_DIR}/src/crypt_gensalt.c ${CMAKE_CURRENT_SOURCE_DIR}/src/wrapper.c ${CMAKE_CURRENT_SOURCE_DIR}/src/x86.S + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/bcrypt.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/BCrypt.hpp + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/crypt.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/crypt_blowfish.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/crypt_gensalt.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/ow-crypt.h + ${CMAKE_CURRENT_SOURCE_DIR}/include/bcrypt/ow-crypt.h ) if (NOT WIN32) @@ -61,6 +68,7 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include) add_library( ${PROJECT_NAME} ${SRCFILES} + src/wrapper.h ) set_target_properties(${PROJECT_NAME} PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION ${PROJECT_VERSION_MAJOR}) diff --git a/src/bcrypt.c b/src/bcrypt.c index 5483857..c8757f3 100644 --- a/src/bcrypt.c +++ b/src/bcrypt.c @@ -16,6 +16,8 @@ #include #include #ifdef _WIN32 +#include // For _close and _read +#pragma warning(disable: 4996) // Disable deprecation warnings for _close and _read #elif _WIN64 #else #include @@ -29,9 +31,9 @@ typedef __int32 ssize_t; #elif defined(_WIN32) && defined(_WIN64) typedef __int64 ssize_t; #endif -#define BCRYPT_HASHSIZE 60 #include "../include/bcrypt/bcrypt.h" +#include "wrapper.h" #include #include /* CryptAcquireContext, CryptGenRandom */ @@ -65,7 +67,7 @@ static int try_read(int fd, char *out, size_t count) { for (;;) { errno = 0; - partial = read(fd, out + total, count - total); + partial = read(fd, out + total, (unsigned int)(count - total)); if (partial == -1 && errno == EINTR) continue; break; @@ -92,10 +94,9 @@ static int timing_safe_strcmp(const char *str1, const char *str2) const unsigned char *u1; const unsigned char *u2; int ret; - int i; - int len1 = strlen(str1); - int len2 = strlen(str2); + size_t len1 = strlen(str1); + size_t len2 = strlen(str2); /* In our context both strings should always have the same length * because they will be hashed passwords. */ @@ -107,7 +108,7 @@ static int timing_safe_strcmp(const char *str1, const char *str2) u2 = (const unsigned char *)str2; ret = 0; - for (i = 0; i < len1; ++i) + for (size_t i = 0; i < len1; ++i) ret |= (u1[i] ^ u2[i]); return ret; @@ -115,7 +116,6 @@ static int timing_safe_strcmp(const char *str1, const char *str2) int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) { - int fd; char input[RANDBYTES]; int workf; char *aux; @@ -123,7 +123,6 @@ int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) // Note: Windows does not have /dev/urandom sadly. #if defined(_WIN32) || defined(_WIN64) HCRYPTPROV p; - ULONG i; // Acquire a crypt context for generating random bytes. if (CryptAcquireContext(&p, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) { @@ -139,7 +138,7 @@ int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE]) } #else // Get random bytes on Unix/Linux. - fd = open("/dev/urandom", O_RDONLY); + int fd = open("/dev/urandom", O_RDONLY); if (fd == -1) return 1; diff --git a/src/crypt_blowfish.c b/src/crypt_blowfish.c index 63ccfbd..3fec936 100644 --- a/src/crypt_blowfish.c +++ b/src/crypt_blowfish.c @@ -900,7 +900,7 @@ char *_crypt_gensalt_blowfish_rn(const char *prefix, unsigned long count, output[1] = '2'; output[2] = prefix[2]; output[3] = '$'; - output[4] = '0' + count / 10; + output[4] = (char)('0' + count / 10); output[5] = '0' + count % 10; output[6] = '$'; diff --git a/src/wrapper.h b/src/wrapper.h new file mode 100644 index 0000000..12aa15d --- /dev/null +++ b/src/wrapper.h @@ -0,0 +1,8 @@ +#ifndef WRAPPER_H +#define WRAPPER_H + +char *crypt_rn(const char *key, const char *setting, void *data, int size); +char *crypt_gensalt_rn(const char *prefix, unsigned long count, + const char *input, int size, char *output, int output_size); + +#endif // WRAPPER_H From e8090b9e6e788574ab3f7df32ce8c2b0e714c5b6 Mon Sep 17 00:00:00 2001 From: Gabriel Espinoza Date: Thu, 19 Jun 2025 17:22:24 -0700 Subject: [PATCH 4/5] made test optional --- CMakeLists.txt | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 33cbafb..879a405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -78,9 +78,14 @@ set_target_properties(${PROJECT_NAME} PROPERTIES PUBLIC_HEADER include/bcrypt/BC target_include_directories(${PROJECT_NAME} PRIVATE include) target_include_directories(${PROJECT_NAME} PRIVATE src) -add_executable( ${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp) +# optional test +option(TEST "Build test executable" OFF) +if(TEST) + add_executable( ${PROJECT_NAME}_test ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp) + target_link_libraries( ${PROJECT_NAME}_test ${PROJECT_NAME}) +endif() + -target_link_libraries( ${PROJECT_NAME}_test ${PROJECT_NAME}) include(GNUInstallDirs) From b284481823318cdd8bbfa06d857ba6eeca80e89a Mon Sep 17 00:00:00 2001 From: Gabriel Espinoza Date: Fri, 20 Jun 2025 09:55:55 -0700 Subject: [PATCH 5/5] fix MinGW warnings --- src/bcrypt.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/bcrypt.c b/src/bcrypt.c index c8757f3..5f366a8 100644 --- a/src/bcrypt.c +++ b/src/bcrypt.c @@ -15,14 +15,12 @@ #include #include #include -#ifdef _WIN32 -#include // For _close and _read -#pragma warning(disable: 4996) // Disable deprecation warnings for _close and _read -#elif _WIN64 -#else +#include + +#if !defined(_WIN32) && !defined(_WIN64) #include #endif -#include + #if defined(_WIN32) || defined(_WIN64) // On windows we need to generate random bytes differently. @@ -44,6 +42,7 @@ typedef __int64 ssize_t; #define RANDBYTES (16) +#if !defined(_WIN32) && !defined(_WIN64) static int try_close(int fd) { int ret; @@ -81,6 +80,7 @@ static int try_read(int fd, char *out, size_t count) return 0; } +#endif /* * This is a best effort implementation. Nothing prevents a compiler from