📄 random.c
字号:
/*
Legal Notice: Some portions of the source code contained in this file were
derived from the source code of Encryption for the Masses 2.02a, which is
Copyright (c) 1998-2000 Paul Le Roux and which is governed by the 'License
Agreement for Encryption for the Masses'. Modifications and additions to
the original source code (contained in this file) and all other portions of
this file are Copyright (c) 2003-2008 TrueCrypt Foundation and are governed
by the TrueCrypt License 2.6 the full text of which is contained in the
file License.txt included in TrueCrypt binary and source code distribution
packages. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include "Tcdefs.h"
#include "Crc.h"
#include "Random.h"
#include "Apidrvr.h"
unsigned __int8 buffer[RNG_POOL_SIZE];
unsigned char *pRandPool = NULL;
static BOOL bRandDidInit = FALSE;
int nRandIndex = 0, randPoolReadIndex = 0;
int HashFunction = DEFAULT_HASH_ALGORITHM;
BOOL bDidSlowPoll = FALSE; /* We do the slow poll only once */
BOOL volatile bFastPollEnabled = TRUE; /* Used to reduce CPU load when performing benchmarks */
BOOL volatile bRandmixEnabled = TRUE; /* Used to reduce CPU load when performing benchmarks */
/* Macro to add a single byte to the pool */
#define RandaddByte(x) {\
if (nRandIndex == RNG_POOL_SIZE) nRandIndex = 0;\
pRandPool[nRandIndex] = (unsigned char) ((unsigned char)x + pRandPool[nRandIndex]); \
if (nRandIndex % RANDMIX_BYTE_INTERVAL == 0) Randmix();\
nRandIndex++; \
}
/* Macro to add four bytes to the pool */
#define RandaddInt32(x) RandAddInt((unsigned __int32)x);
void RandAddInt (unsigned __int32 x)
{
RandaddByte(x);
RandaddByte((x >> 8));
RandaddByte((x >> 16));
RandaddByte((x >> 24));
}
#include <tlhelp32.h>
#include "Dlgcode.h"
HHOOK hMouse = NULL; /* Mouse hook for the random number generator */
HHOOK hKeyboard = NULL; /* Keyboard hook for the random number generator */
/* Variables for thread control, the thread is used to gather up info about
the system in in the background */
CRITICAL_SECTION critRandProt; /* The critical section */
BOOL volatile bThreadTerminate = FALSE; /* This variable is shared among thread's so its made volatile */
/* Network library handle for the SlowPoll function */
HANDLE hNetAPI32 = NULL;
// CryptoAPI
HCRYPTPROV hCryptProv;
/* Init the random number generator, setup the hooks, and start the thread */
int
Randinit ()
{
if(bRandDidInit) return 0;
InitializeCriticalSection (&critRandProt);
bRandDidInit = TRUE;
pRandPool = (unsigned char *) TCalloc (RANDOMPOOL_ALLOCSIZE);
if (pRandPool == NULL)
goto error;
else
memset (pRandPool, 0, RANDOMPOOL_ALLOCSIZE);
VirtualLock (pRandPool, RANDOMPOOL_ALLOCSIZE);
hKeyboard = SetWindowsHookEx (WH_KEYBOARD, (HOOKPROC)&KeyboardProc, NULL, GetCurrentThreadId ());
if (hKeyboard == 0) handleWin32Error (0);
hMouse = SetWindowsHookEx (WH_MOUSE, (HOOKPROC)&MouseProc, NULL, GetCurrentThreadId ());
if (hMouse == 0)
{
handleWin32Error (0);
goto error;
}
if (!CryptAcquireContext (&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0)
&& !CryptAcquireContext (&hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
hCryptProv = 0;
}
if (_beginthread (ThreadSafeThreadFunction, 0, NULL) == -1)
goto error;
return 0;
error:
Randfree ();
return 1;
}
/* Close everything down, including the thread which is closed down by
setting a flag which eventually causes the thread function to exit */
void
Randfree ()
{
if (bRandDidInit == FALSE)
return;
EnterCriticalSection (&critRandProt);
if (hMouse != 0)
UnhookWindowsHookEx (hMouse);
if (hKeyboard != 0)
UnhookWindowsHookEx (hKeyboard);
bThreadTerminate = TRUE;
LeaveCriticalSection (&critRandProt);
for (;;)
{
Sleep (250);
EnterCriticalSection (&critRandProt);
if (bThreadTerminate == FALSE)
{
LeaveCriticalSection (&critRandProt);
break;
}
LeaveCriticalSection (&critRandProt);
}
if (hNetAPI32 != 0)
{
FreeLibrary (hNetAPI32);
hNetAPI32 = NULL;
}
if (hCryptProv)
{
CryptReleaseContext (hCryptProv, 0);
hCryptProv = 0;
}
hMouse = NULL;
hKeyboard = NULL;
bThreadTerminate = FALSE;
DeleteCriticalSection (&critRandProt);
bRandDidInit = FALSE;
if (pRandPool != NULL)
{
burn (pRandPool, RANDOMPOOL_ALLOCSIZE);
TCfree (pRandPool);
pRandPool = NULL;
}
nRandIndex = 0;
}
void RandSetHashFunction (int hash_algo_id)
{
HashFunction = hash_algo_id;
}
int RandGetHashFunction (void)
{
return HashFunction;
}
/* The random pool mixing function */
BOOL Randmix ()
{
if (bRandmixEnabled)
{
unsigned char hashOutputBuffer [MAX_DIGESTSIZE];
WHIRLPOOL_CTX wctx;
RMD160_CTX rctx;
sha512_ctx sctx;
int poolIndex, digestIndex, digestSize;
switch (HashFunction)
{
case RIPEMD160:
digestSize = RIPEMD160_DIGESTSIZE;
break;
case SHA512:
digestSize = SHA512_DIGESTSIZE;
break;
case WHIRLPOOL:
digestSize = WHIRLPOOL_DIGESTSIZE;
break;
default:
return FALSE;
}
if (RNG_POOL_SIZE % digestSize)
TC_THROW_FATAL_EXCEPTION;
for (poolIndex = 0; poolIndex < RNG_POOL_SIZE; poolIndex += digestSize)
{
/* Compute the message digest of the entire pool using the selected hash function. */
switch (HashFunction)
{
case RIPEMD160:
RMD160Init(&rctx);
RMD160Update(&rctx, pRandPool, RNG_POOL_SIZE);
RMD160Final(hashOutputBuffer, &rctx);
break;
case SHA512:
sha512_begin (&sctx);
sha512_hash (pRandPool, RNG_POOL_SIZE, &sctx);
sha512_end (hashOutputBuffer, &sctx);
break;
case WHIRLPOOL:
WHIRLPOOL_init (&wctx);
WHIRLPOOL_add (pRandPool, RNG_POOL_SIZE * 8, &wctx);
WHIRLPOOL_finalize (&wctx, hashOutputBuffer);
break;
default:
// Unknown/wrong ID
TC_THROW_FATAL_EXCEPTION;
}
/* XOR the resultant message digest to the pool at the poolIndex position. */
for (digestIndex = 0; digestIndex < digestSize; digestIndex++)
{
pRandPool [poolIndex + digestIndex] ^= hashOutputBuffer [digestIndex];
}
}
/* Prevent leaks */
burn (hashOutputBuffer, MAX_DIGESTSIZE);
switch (HashFunction)
{
case RIPEMD160:
burn (&rctx, sizeof(rctx));
break;
case SHA512:
burn (&sctx, sizeof(sctx));
break;
case WHIRLPOOL:
burn (&wctx, sizeof(wctx));
break;
default:
// Unknown/wrong ID
TC_THROW_FATAL_EXCEPTION;
}
}
return TRUE;
}
/* Add a buffer to the pool */
void
RandaddBuf (void *buf, int len)
{
int i;
for (i = 0; i < len; i++)
{
RandaddByte (((unsigned char *) buf)[i]);
}
}
BOOL
RandpeekBytes (unsigned char *buf, int len)
{
if (!bRandDidInit)
return FALSE;
if (len > RNG_POOL_SIZE)
{
Error ("ERR_NOT_ENOUGH_RANDOM_DATA");
len = RNG_POOL_SIZE;
}
EnterCriticalSection (&critRandProt);
memcpy (buf, pRandPool, len);
LeaveCriticalSection (&critRandProt);
return TRUE;
}
/* Get len random bytes from the pool (max. RNG_POOL_SIZE bytes per a single call) */
BOOL
RandgetBytes (unsigned char *buf, int len, BOOL forceSlowPoll)
{
int i;
BOOL ret = TRUE;
if (HashFunction == 0)
return FALSE;
EnterCriticalSection (&critRandProt);
if (bDidSlowPoll == FALSE || forceSlowPoll)
{
if (!SlowPoll ())
ret = FALSE;
else
bDidSlowPoll = TRUE;
}
if (!FastPoll ())
ret = FALSE;
/* There's never more than RNG_POOL_SIZE worth of randomess */
if (len > RNG_POOL_SIZE)
{
Error ("ERR_NOT_ENOUGH_RANDOM_DATA");
len = RNG_POOL_SIZE;
return FALSE;
}
// Requested number of bytes is copied from pool to output buffer,
// pool is rehashed, and output buffer is XORed with new data from pool
for (i = 0; i < len; i++)
{
buf[i] = pRandPool[randPoolReadIndex++];
if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0;
}
/* Invert the pool */
for (i = 0; i < RNG_POOL_SIZE / 4; i++)
{
((unsigned __int32 *) pRandPool)[i] = ~((unsigned __int32 *) pRandPool)[i];
}
// Mix the pool
if (!FastPoll ())
ret = FALSE;
// XOR the current pool content into the output buffer to prevent pool state leaks
for (i = 0; i < len; i++)
{
buf[i] ^= pRandPool[randPoolReadIndex++];
if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0;
}
LeaveCriticalSection (&critRandProt);
return ret;
}
/* Capture the mouse, and as long as the event is not the same as the last
two events, add the crc of the event, and the crc of the time difference
between this event and the last + the current time to the pool.
The role of CRC-32 is merely to perform diffusion. Note that the output
of CRC-32 is subsequently processed using a cryptographically secure hash
algorithm. */
LRESULT CALLBACK
MouseProc (int nCode, WPARAM wParam, LPARAM lParam)
{
static DWORD dwLastTimer;
static unsigned __int32 lastCrc, lastCrc2;
MOUSEHOOKSTRUCT *lpMouse = (MOUSEHOOKSTRUCT *) lParam;
if (nCode < 0)
return CallNextHookEx (hMouse, nCode, wParam, lParam);
else
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -