Skip to content

added microsoft visual c++ compatibility to CMakeLists.txt #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 32 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,17 +34,29 @@ 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
${CMAKE_CURRENT_SOURCE_DIR}/src/crypt_blowfish.c
${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)
Expand All @@ -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)

Expand All @@ -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)
25 changes: 12 additions & 13 deletions src/bcrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _WIN32
#elif _WIN64
#else
#include <errno.h>

#if !defined(_WIN32) && !defined(_WIN64)
#include <unistd.h>
#endif
#include <errno.h>


#if defined(_WIN32) || defined(_WIN64)
// On windows we need to generate random bytes differently.
Expand All @@ -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 <windows.h>
#include <wincrypt.h> /* CryptAcquireContext, CryptGenRandom */
Expand All @@ -42,6 +42,7 @@ typedef __int64 ssize_t;

#define RANDBYTES (16)

#if !defined(_WIN32) && !defined(_WIN64)
static int try_close(int fd)
{
int ret;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -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. */
Expand All @@ -107,23 +108,21 @@ 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;
}

int bcrypt_gensalt(int factor, char salt[BCRYPT_HASHSIZE])
{
int fd;
char input[RANDBYTES];
int workf;
char *aux;

// 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) {
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/crypt_blowfish.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = '$';

Expand Down
8 changes: 8 additions & 0 deletions src/wrapper.h
Original file line number Diff line number Diff line change
@@ -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