1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 11:08:02 -06:00

Implement function RandgetBytesFull that enables generating random bytes of any length.

This commit is contained in:
Mounir IDRASSI
2014-12-08 23:41:29 +01:00
parent fd0e434087
commit 32e72d1117
2 changed files with 55 additions and 21 deletions

View File

@@ -339,7 +339,16 @@ BOOL RandpeekBytes (unsigned char *buf, int len)
/* Get len random bytes from the pool (max. RNG_POOL_SIZE bytes per a single call) */ /* 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) BOOL RandgetBytes (unsigned char *buf, int len, BOOL forceSlowPoll)
{ {
int i; return RandgetBytesFull (buf, len, forceSlowPoll, FALSE);
}
/* Get len random bytes from the pool.
* If allowAnyLength is FALSE, then len must be less or equal to RNG_POOL_SIZE
* If allowAnyLength is TRUE, then len can have any positive value
*/
BOOL RandgetBytesFull ( unsigned char *buf , int len, BOOL forceSlowPoll , BOOL allowAnyLength)
{
int i, looplen;
BOOL ret = TRUE; BOOL ret = TRUE;
if (!bRandDidInit || HashFunction == 0) if (!bRandDidInit || HashFunction == 0)
@@ -359,7 +368,7 @@ BOOL RandgetBytes (unsigned char *buf, int len, BOOL forceSlowPoll)
ret = FALSE; ret = FALSE;
/* There's never more than RNG_POOL_SIZE worth of randomess */ /* There's never more than RNG_POOL_SIZE worth of randomess */
if (len > RNG_POOL_SIZE) if ( (!allowAnyLength) && (len > RNG_POOL_SIZE))
{ {
Error ("ERR_NOT_ENOUGH_RANDOM_DATA"); Error ("ERR_NOT_ENOUGH_RANDOM_DATA");
len = RNG_POOL_SIZE; len = RNG_POOL_SIZE;
@@ -367,9 +376,22 @@ BOOL RandgetBytes (unsigned char *buf, int len, BOOL forceSlowPoll)
return FALSE; return FALSE;
} }
// Requested number of bytes is copied from pool to output buffer, while (len > 0)
{
if (len > RNG_POOL_SIZE)
{
looplen = RNG_POOL_SIZE;
len -= RNG_POOL_SIZE;
}
else
{
looplen = len;
len = 0;
}
// this loop number of bytes is copied from pool to output buffer,
// pool is rehashed, and output buffer is XORed with new data from pool // pool is rehashed, and output buffer is XORed with new data from pool
for (i = 0; i < len; i++) for (i = 0; i < looplen; i++)
{ {
buf[i] = pRandPool[randPoolReadIndex++]; buf[i] = pRandPool[randPoolReadIndex++];
if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0; if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0;
@@ -386,12 +408,16 @@ BOOL RandgetBytes (unsigned char *buf, int len, BOOL forceSlowPoll)
ret = FALSE; ret = FALSE;
// XOR the current pool content into the output buffer to prevent pool state leaks // XOR the current pool content into the output buffer to prevent pool state leaks
for (i = 0; i < len; i++) for (i = 0; i < looplen; i++)
{ {
buf[i] ^= pRandPool[randPoolReadIndex++]; buf[i] ^= pRandPool[randPoolReadIndex++];
if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0; if (randPoolReadIndex == RNG_POOL_SIZE) randPoolReadIndex = 0;
} }
// increment the pointer for the next loop
buf += looplen;
}
LeaveCriticalSection (&critRandProt); LeaveCriticalSection (&critRandProt);
if (!ret) if (!ret)

View File

@@ -44,8 +44,16 @@ void RandaddBuf ( void *buf , int len );
BOOL FastPoll ( void ); BOOL FastPoll ( void );
BOOL SlowPoll ( void ); BOOL SlowPoll ( void );
BOOL RandpeekBytes ( unsigned char *buf , int len ); BOOL RandpeekBytes ( unsigned char *buf , int len );
/* 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 ); BOOL RandgetBytes ( unsigned char *buf , int len, BOOL forceSlowPoll );
/* Get len random bytes from the pool.
* If allowAnyLength is FALSE, then len must be less or equal to RNG_POOL_SIZE
* If allowAnyLength is TRUE, then len can have any positive value
*/
BOOL RandgetBytesFull ( unsigned char *buf , int len, BOOL forceSlowPoll , BOOL allowAnyLength);
#ifdef _WIN32 #ifdef _WIN32
extern BOOL volatile bFastPollEnabled; extern BOOL volatile bFastPollEnabled;