42#if defined(__MINGW32__) || defined(__MINGW64__)
58#if defined(WIZARDSTOOLKIT_HAVE_GETENTROPY)
59#include <sys/random.h>
65#define PseudoRandomHash SHA2256Hash
66#define RandomEntropyLevel 9
67#define RandomFilename "reservoir.xdm"
68#define RandomFiletype "random"
69#define RandomProtocolMajorVersion 1
70#define RandomProtocolMinorVersion 1
113#if defined(__APPLE__) && !defined(TARGET_OS_IPHONE)
114#include <crt_externs.h>
115#define environ (*_NSGetEnviron())
336static ssize_t
ReadRandom(
int file,
unsigned char *source,
size_t length)
346 for (q=source; length != 0; length-=count)
348 count=(ssize_t) read(file,q,length);
365#define MaxEntropyExtent 64
388#if defined(WIZARDSTOOLKIT_HAVE_GETENTROPY)
407 pid=(ssize_t) getpid();
415#if defined(WIZARDSTOOLKIT_SYSCONF) && defined(_SC_PHYS_PAGES)
420 pages=(ssize_t) sysconf(_SC_PHYS_PAGES);
426#if defined(WIZARDSTOOLKIT_HAVE_GETRUSAGE) && defined(RUSAGE_SELF)
431 if (getrusage(RUSAGE_SELF,&usage) == 0)
438 seconds=time((time_t *) 0);
440#if defined(WIZARDSTOOLKIT_HAVE_GETTIMEOFDAY)
445 if (gettimeofday(&timer,(
struct timezone *) NULL) == 0)
447 seconds=(size_t) timer.tv_sec;
448 nanoseconds=(size_t) (1000UL*timer.tv_usec);
452#if defined(WIZARDSTOOLKIT_HAVE_CLOCK_GETTIME) && defined(CLOCK_REALTIME_HR)
457 if (clock_gettime(CLOCK_REALTIME_HR,&timer) == 0)
459 seconds=timer.tv_sec;
460 nanoseconds=timer.tv_nsec;
471#if defined(WIZARDSTOOLKIT_HAVE_CLOCK)
474#if defined(WIZARDSTOOLKIT_HAVE_TIMES)
479 (void) times(&timer);
480 nanoseconds=timer.tms_utime+timer.tms_stime;
486#if defined(WIZARDSTOOLKIT_WINDOWS_SUPPORT)
500 seconds=NTElapsedTime()+NTUserTime();
504 if (QueryPerformanceCounter(&nanoseconds) != 0)
517 "unable to gather random data: %s",strerror(errno));
545 for (i=0;
environ[i] != (
char *) NULL; i++)
563 if (close(file) == -1)
565 "unable to close file `%s': %s",filename,strerror(errno));
586 0xf9f0014228f223efll)
595 if (close(file) == -1)
597 RandomError,
"unable to close file `%s': %s",filename,
694 "corrupt random reservoir file `%s'",
GetFilePath(file_info));
711 "corrupt random reservoir file `%s'",
GetFilePath(file_info));
732 "corrupt random reservoir file `%s'",
GetFilePath(file_info));
745 "corrupt random reservoir file `%s'",
GetFilePath(file_info));
781#define RandomROTL(x,k) (((x) << (k)) | ((x) >> (64-(k))))
888 }
while (key == range);
889 return((
double) key/range);
1015 "unable to write random reservior `%s': %s",
GetFilePath(file_info),
WizardExport EntropyInfo * AcquireEntropyInfo(const EntropyType entropy, const size_t level)
WizardExport EntropyInfo * DestroyEntropyInfo(EntropyInfo *entropy_info)
WizardExport WizardBooleanType IncreaseEntropy(EntropyInfo *entropy_info, const StringInfo *message, ExceptionInfo *exception)
WizardExport const StringInfo * GetEntropyChaos(const EntropyInfo *entropy_info)
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
#define ThrowFatalException(severity, tag)
WizardExport ExceptionInfo * AcquireExceptionInfo(void)
WizardExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
WizardExport WizardBooleanType ThrowWizardException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *format,...)
WizardExport WizardBooleanType ReadFile64Bits(FileInfo *file_info, WizardSizeType *value)
WizardExport const char * GetFilePath(const FileInfo *file_info)
static size_t GetWizardThreadId(void)
WizardExport FileInfo * AcquireFileInfo(const char *path, const char *relative_path, const FileMode mode, ExceptionInfo *exception)
WizardExport WizardBooleanType WriteFile32Bits(FileInfo *file_info, const size_t value)
WizardExport WizardBooleanType WriteFile64Bits(FileInfo *file_info, const WizardSizeType value)
WizardExport WizardBooleanType WriteFile16Bits(FileInfo *file_info, const unsigned short value)
WizardExport WizardBooleanType ReadFileChunk(FileInfo *file_info, void *data, const size_t length)
WizardExport WizardBooleanType WriteFileChunk(FileInfo *file_info, const void *data, const size_t length)
WizardExport WizardBooleanType ReadFile32Bits(FileInfo *file_info, size_t *value)
WizardExport FileInfo * DestroyFileInfo(FileInfo *file_info, ExceptionInfo *exception)
WizardExport WizardBooleanType ReadFile16Bits(FileInfo *file_info, unsigned short *value)
WizardExport HashInfo * AcquireHashInfo(const HashType hash)
WizardExport WizardBooleanType UpdateHash(HashInfo *hash_info, const StringInfo *message)
WizardExport HashInfo * DestroyHashInfo(HashInfo *hash_info)
WizardExport const StringInfo * GetHashDigest(const HashInfo *hash_info)
WizardExport size_t GetHashDigestsize(const HashInfo *hash_info)
WizardExport WizardBooleanType FinalizeHash(HashInfo *hash_info)
WizardExport void FinalizeHMAC(HMACInfo *hmac_info)
WizardExport HMACInfo * AcquireHMACInfo(const HashType hash)
WizardExport const StringInfo * GetHMACDigest(const HMACInfo *hmac_info)
WizardExport void UpdateHMAC(HMACInfo *hmac_info, const StringInfo *message)
WizardExport HMACInfo * DestroyHMACInfo(HMACInfo *hmac_info)
WizardExport void InitializeHMAC(HMACInfo *hmac_info, const StringInfo *key)
WizardExport size_t GetHMACDigestsize(const HMACInfo *hmac_info)
WizardExport void ResetHMAC(HMACInfo *hmac_info)
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
#define GetWizardModule()
WizardExport StringInfo * GetWizardMagick(const unsigned char *signature, const size_t length)
unsigned char WizardMagick[4]
WizardExport void * AcquireWizardMemory(const size_t size)
WizardExport void * CopyWizardMemory(void *destination, const void *source, const size_t size)
WizardExport void * RelinquishWizardMemory(void *memory)
WizardExport RandomInfo * AcquireRandomInfo(const HashType hash)
WizardExport void SetRandomKey(RandomInfo *random_info, const size_t length, unsigned char *key)
WizardExport double GetPseudoRandomValue(RandomInfo *random_info)
static SemaphoreInfo * random_semaphore
#define RandomProtocolMinorVersion
static StringInfo * GenerateEntropicChaos(RandomInfo *, ExceptionInfo *)
#define RandomProtocolMajorVersion
WizardExport StringInfo * GetRandomKey(RandomInfo *random_info, const size_t length)
#define RandomEntropyLevel
static StringInfo * GetEntropyFromReservoir(RandomInfo *, ExceptionInfo *)
WizardExport RandomInfo * DestroyRandomInfo(RandomInfo *random_info)
static void IncrementRandomNonce(StringInfo *nonce)
WizardExport double GetRandomValue(RandomInfo *random_info)
WizardExport WizardBooleanType RandomComponentGenesis(void)
WizardExport void RandomComponentTerminus(void)
static unsigned long secret_key
static WizardBooleanType gather_true_random
WizardExport void SetRandomSecretKey(const unsigned long key)
static ssize_t ReadRandom(int file, unsigned char *source, size_t length)
WizardExport unsigned long GetRandomSecretKey(const RandomInfo *random_info)
WizardExport void SetRandomTrueRandom(const WizardBooleanType true_random)
static WizardBooleanType SaveEntropyToReservoir(RandomInfo *, ExceptionInfo *)
static RandomInfo * random_info
WizardExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
WizardExport SemaphoreInfo * AcquireSemaphoreInfo(void)
WizardExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
WizardExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
WizardExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
WizardExport WizardSizeType GetStringInfoCRC(const StringInfo *string_info)
WizardExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
WizardExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
WizardExport char * DestroyString(char *string)
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
WizardExport StringInfo * AcquireStringInfo(const size_t length)
WizardExport int CompareStringInfo(const StringInfo *target, const StringInfo *source)
WizardExport StringInfo * CloneStringInfo(const StringInfo *string_info)
WizardExport char * AcquireString(const char *source)
WizardExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
WizardExport void ConcatenateStringInfo(StringInfo *string_info, const StringInfo *source)
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
WizardExport void ResetStringInfo(StringInfo *string_info)
WizardExport StringInfo * StringToStringInfo(const char *string)
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
unsigned short protocol_minor
unsigned short protocol_major
SemaphoreInfo * semaphore
static int open_utf8(const char *path, int flags, mode_t mode)
#define WizardULLConstant(c)
unsigned int WizardStatusType