Merge pull request #88 from minrk/tweetnacl

tweetnacl in sdists
This commit is contained in:
Pieter Hintjens 2016-01-18 11:14:08 +01:00
commit e8095071d9
5 changed files with 62 additions and 26 deletions

View File

@ -26,6 +26,7 @@ if(WITH_TWEETNACL)
tweetnacl/src/tweetnacl.c tweetnacl/src/tweetnacl.c
) )
if(WIN32) if(WIN32)
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/winrandom.c)
else() else()
list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/devurandom.c) list(APPEND TWEETNACL_SOURCES tweetnacl/contrib/randombytes/devurandom.c)
endif() endif()

View File

@ -608,6 +608,7 @@ EXTRA_DIST = \
MAINTAINERS \ MAINTAINERS \
src/libzmq.pc.cmake.in \ src/libzmq.pc.cmake.in \
src/libzmq.vers \ src/libzmq.vers \
tweetnacl \
tools/curve_keygen.cpp tools/curve_keygen.cpp
MAINTAINERCLEANFILES = \ MAINTAINERCLEANFILES = \

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;
}