diff --git a/CMakeLists.txt b/CMakeLists.txt index 23cb3ae..879a405 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,16 +11,15 @@ # ################################################################################### -cmake_minimum_required(VERSION 2.8 FATAL_ERROR) +cmake_minimum_required(VERSION 3.5) -project(bcrypt) +project(bcrypt VERSION 1.0.0 LANGUAGES C CXX ) -enable_language(ASM) - -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}) +if(MSVC ) + enable_language(ASM_MASM) +else() + enable_language(ASM) +endif() # just doing cmake . will build a shared or static lib and honor existing environment setting # to force build static, cmake . -DBUILD_SHARED_LIBS=Off @@ -35,10 +34,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 @@ -46,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) @@ -57,18 +68,24 @@ include_directories( ${CMAKE_CURRENT_SOURCE_DIR}/include) add_library( ${PROJECT_NAME} ${SRCFILES} + src/wrapper.h ) -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) 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) @@ -85,9 +102,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) diff --git a/src/bcrypt.c b/src/bcrypt.c index 5483857..5f366a8 100644 --- a/src/bcrypt.c +++ b/src/bcrypt.c @@ -15,12 +15,12 @@ #include #include #include -#ifdef _WIN32 -#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. @@ -29,9 +29,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 */ @@ -42,6 +42,7 @@ typedef __int64 ssize_t; #define RANDBYTES (16) +#if !defined(_WIN32) && !defined(_WIN64) static int try_close(int fd) { int ret; @@ -65,7 +66,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; @@ -79,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 @@ -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