0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 07:31:03 +08:00

TweetNaCL: add windows randombytes implementation

This commit is contained in:
Anonymous Maarten 2015-10-23 16:13:53 +02:00
parent e182438ad6
commit b2c87b9a70
5 changed files with 104 additions and 47 deletions

View File

@ -3,21 +3,27 @@
cmake_minimum_required(VERSION 2.8) cmake_minimum_required(VERSION 2.8)
project(ZeroMQ) project(ZeroMQ)
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}")
option(WITH_OPENPGM "Build with support for OpenPGM" OFF) option(WITH_OPENPGM "Build with support for OpenPGM" OFF)
if(APPLE) if(APPLE)
option(ZMQ_BUILD_FRAMEWORK "Build as OS X framework" ON) option(ZMQ_BUILD_FRAMEWORK "Build as OS X framework" ON)
endif() endif()
list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_SOURCE_DIR}") option(WITH_SODIUM "Build with libsodium" ON)
find_package(Sodium) option(WITH_TWEETNACL "Build with tweetnacl" ON)
set(USE_TWEETNACL OFF) if(WITH_SODIUM)
find_package(Sodium)
if(SODIUM_FOUND) if(SODIUM_FOUND)
add_definitions(-DHAVE_LIBSODIUM) add_definitions(-DHAVE_LIBSODIUM)
include_directories(${SODIUM_INCLUDE_DIRS}) include_directories(${SODIUM_INCLUDE_DIRS})
else() endif()
endif()
if(WITH_TWEETNACL AND NOT SODIUM_FOUND)
message(STATUS "Building with TweetNaCL")
set(USE_TWEETNACL ON) set(USE_TWEETNACL ON)
add_definitions(-DHAVE_TWEETNACL) add_definitions(-DHAVE_TWEETNACL)
include_directories( include_directories(
@ -29,7 +35,9 @@ else()
tweetnacl/src/tweetnacl.c tweetnacl/src/tweetnacl.c
) )
if(NOT WIN32) if(WIN32)
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/winrandom.c)
else()
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/devurandom.c) list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/devurandom.c)
endif() endif()
endif() endif()

View File

@ -10,18 +10,35 @@ environment:
CMAKE_GENERATOR: "Visual Studio 14 2015" CMAKE_GENERATOR: "Visual Studio 14 2015"
MSVCVERSION: "v140" MSVCVERSION: "v140"
MSVCYEAR: "vs2015" MSVCYEAR: "vs2015"
matrix:
- platform: Win32
configuration: Release
WITH_SODIUM: ON
WITH_TWEETNACL: OFF
- platform: Win32
configuration: Release
WITH_SODIUM: ON
WITH_TWEETNACL: OFF
- platform: x64
configuration: Debug
WITH_SODIUM: ON
WITH_TWEETNACL: OFF
- platform: x64
configuration: Debug
WITH_SODIUM: ON
WITH_TWEETNACL: OFF
- platform: Win32
configuration: Release
WITH_SODIUM: OFF
WITH_TWEETNACL: OFF
- platform: Win32
configuration: Release
WITH_SODIUM: OFF
WITH_TWEETNACL: ON
matrix: matrix:
fast_finish: false fast_finish: false
platform:
- Win32
- x64
configuration:
- Release
- Debug
init: init:
#- ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) #- ps: $blockRdp = $true; iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
- cmake --version - cmake --version
@ -45,8 +62,7 @@ before_build:
- cmd: set LIBZMQ_BUILDDIR=C:\projects\build_libzmq - cmd: set LIBZMQ_BUILDDIR=C:\projects\build_libzmq
- cmd: md "%LIBZMQ_BUILDDIR%" - cmd: md "%LIBZMQ_BUILDDIR%"
- cd "%LIBZMQ_BUILDDIR%" - cd "%LIBZMQ_BUILDDIR%"
- cmd: cmake -D CMAKE_INCLUDE_PATH="%SODIUM_INCLUDE_DIR%" -D CMAKE_LIBRARY_PATH="%SODIUM_LIBRARY_DIR%" -D CMAKE_C_FLAGS_RELEASE="/MT" -D CMAKE_C_FLAGS_DEBUG="/MTd" -G "%CMAKE_GENERATOR%" "%APPVEYOR_BUILD_FOLDER%" - cmd: cmake -D CMAKE_INCLUDE_PATH="%SODIUM_INCLUDE_DIR%" -D CMAKE_LIBRARY_PATH="%SODIUM_LIBRARY_DIR%" -D WITH_SODIUM="%WITH_SODIUM%" -D WITH_TWEETNACL="%WITH_TWEETNACL%" -D CMAKE_C_FLAGS_RELEASE="/MT" -D CMAKE_C_FLAGS_DEBUG="/MTd" -D WITH_SODIUM="%WITH_SODIUM%" -G "%CMAKE_GENERATOR%" "%APPVEYOR_BUILD_FOLDER%"
build: build:
parallel: true parallel: true
@ -56,11 +72,10 @@ build:
after_build: after_build:
- cmd: cd %LIBZMQ_BUILDDIR%\bin\%Configuration%" - cmd: cd %LIBZMQ_BUILDDIR%\bin\%Configuration%"
- cmd: copy "%SODIUM_LIBRARY_DIR%\libsodium.dll" . - cmd: copy "%SODIUM_LIBRARY_DIR%\libsodium.dll" .
# Pack the artifacts
- cmd: 7z a -y -bd -mx=9 libzmq.zip *.exe *.dll - cmd: 7z a -y -bd -mx=9 libzmq.zip *.exe *.dll
- ps: Push-AppveyorArtifact "libzmq.zip" -Filename "libzmq-${env:Platform}-${env:Configuration}.zip" - ps: Push-AppveyorArtifact "libzmq.zip" -Filename "libzmq-${env:Platform}-${env:Configuration}.zip"
test_script:
- cmd: cd "%LIBZMQ_BUILDDIR%" - cmd: cd "%LIBZMQ_BUILDDIR%"
- cmd: ctest -C "%Configuration%" -V - cmd: ctest -C "%Configuration%" -V
test:
none

View File

@ -1,25 +0,0 @@
/*
randombytes/devurandom.h version 20080713
D. J. Bernstein
Public domain.
*/
#ifndef randombytes_devurandom_H
#define randombytes_devurandom_H
#ifdef __cplusplus
extern "C" {
#endif
extern void randombytes(unsigned char *,unsigned long long);
extern int randombytes_close(void);
#ifdef __cplusplus
}
#endif
#ifndef randombytes_implementation
#define randombytes_implementation "devurandom"
#endif
#endif

View File

@ -1,5 +1,21 @@
/*
randombytes/randombytes.h version 20080713
D. J. Bernstein
Public domain.
*/
#ifndef randombytes_H #ifndef randombytes_H
#define randombytes_H #define randombytes_H
#include "devurandom.h"
#ifdef __cplusplus
extern "C" {
#endif
extern void randombytes(unsigned char *,unsigned long long);
extern int randombytes_close(void);
#ifdef __cplusplus
}
#endif
#endif #endif

View File

@ -0,0 +1,43 @@
#include <windows.h>
#include <WinCrypt.h>
#define NCP ((HCRYPTPROV) 0)
HCRYPTPROV hProvider = NCP;
void randombytes(unsigned char *x,unsigned long long xlen)
{
unsigned i;
BOOL ret;
if (hProvider == NCP) {
for(;;) {
ret = CryptAcquireContext(&hProvider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT | CRYPT_SILENT);
if (ret != FALSE) break;
Sleep(1);
}
}
while (xlen > 0) {
if (xlen < 1048576) i = (unsigned) xlen; else i = 1048576;
ret = CryptGenRandom(hProvider, i, x);
if (ret != FALSE) {
Sleep(1);
continue;
}
x += i;
xlen -= i;
}
}
int randombytes_close(void)
{
int rc = -1;
if((hProvider != NCP) && (CryptReleaseContext(hProvider, 0) != FALSE)) {
hProvider = NCP;
rc = 0;
}
return rc;
}