Files
nwipe/src/prng.c
PartialVolume 76a7be696c Fix non functional isaac prng
Since at least 2013 (the initial nwipe commit),
isaac has never functioned. When the issac prng
was selected in the GUI, nwipe used the mersenne
twister prng instead. Not that you would ever
have known, as there were no log entries saying which
prng was being actively used.

However, I don't believe this was just an nwipe
issue, looking at the code for DBAN's dwipe the
same function nwipe_isaac_read( NWIPE_PRNG_READ_SIGNATURE )
exists as it does in nwipe. In both cases the function
has no code that actually does anything.

This patch populates this function and brings isaac
back to life !

This bug was also responsible for verification errors
when the option prng=isaac was used on the command
line. Worse still, if you used prng=isaac on the
command line then wiped using method=prng, no verification
and no blanking you would expect to see random data. You
don't, instead you would see either all zeros or mainly
zeros because the uninitialised buffer that should have
contained random data instead contained initialised text
data such as partial log entries. This patch and previously
submitted patches fix all these problems related to the
isaac implementation.

A separate commit will fix the GUI prng selection which
was leading everybody to believe isaac was being used
when in fact it was mersenne all along.
2021-05-30 22:30:31 +01:00

171 lines
5.0 KiB
C

/*
* prng.c: Pseudo Random Number Generator abstractions for nwipe.
*
* Copyright Darik Horn <dajhorn-dban@vanadac.com>.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "nwipe.h"
#include "prng.h"
#include "context.h"
#include "logging.h"
#include "mt19937ar-cok/mt19937ar-cok.h"
#include "isaac_rand/isaac_rand.h"
nwipe_prng_t nwipe_twister = { "Mersenne Twister (mt19937ar-cok)", nwipe_twister_init, nwipe_twister_read };
nwipe_prng_t nwipe_isaac = { "ISAAC (rand.c 20010626)", nwipe_isaac_init, nwipe_isaac_read };
int nwipe_u32tobuffer( u8* buffer, u32 rand, int len )
{
/*
* Print given number of bytes from unsigned integer number to a byte stream buffer starting with low-endian.
*/
int i;
u8 c; // single char
if( len > sizeof( u32 ) )
{
nwipe_log( NWIPE_LOG_FATAL, "Tried to print longer number than the value passed." );
len = sizeof( u32 );
}
for( i = 0; i < len; i++ )
{
c = rand & 0xFFUL;
rand = rand >> 8;
buffer[i] = c;
}
return 0;
}
int nwipe_twister_init( NWIPE_PRNG_INIT_SIGNATURE )
{
nwipe_log( NWIPE_LOG_NOTICE, "Initialising Mersenne Twister prng" );
if( *state == NULL )
{
/* This is the first time that we have been called. */
*state = malloc( sizeof( twister_state_t ) );
}
twister_init( (twister_state_t*) *state, (u32*) ( seed->s ), seed->length / sizeof( u32 ) );
return 0;
}
int nwipe_twister_read( NWIPE_PRNG_READ_SIGNATURE )
{
u32 i = 0;
u32 ii;
u32 words = count / SIZE_OF_TWISTER; // the values of twister_genrand_int32 is strictly 4 bytes
u32 remain = count % SIZE_OF_TWISTER; // the values of twister_genrand_int32 is strictly 4 bytes
/* Twister returns 4-bytes per call, so progress by 4 bytes. */
for( ii = 0; ii < words; ++ii )
{
nwipe_u32tobuffer( (u8*) ( buffer + i ), twister_genrand_int32( (twister_state_t*) *state ), SIZE_OF_TWISTER );
i = i + SIZE_OF_TWISTER;
}
/* If there is some remainder copy only relevant number of bytes to not
* overflow the buffer. */
if( remain > 0 )
{
nwipe_u32tobuffer( (u8*) ( buffer + i ), twister_genrand_int32( (twister_state_t*) *state ), remain );
}
return 0;
}
int nwipe_isaac_init( NWIPE_PRNG_INIT_SIGNATURE )
{
int count;
randctx* isaac_state = *state;
nwipe_log( NWIPE_LOG_NOTICE, "Initialising Isaac prng" );
if( *state == NULL )
{
/* This is the first time that we have been called. */
*state = malloc( sizeof( randctx ) );
isaac_state = *state;
/* Check the memory allocation. */
if( isaac_state == 0 )
{
nwipe_perror( errno, __FUNCTION__, "malloc" );
nwipe_log( NWIPE_LOG_FATAL, "Unable to allocate memory for the isaac state." );
return -1;
}
}
/* Take the minimum of the isaac seed size and available entropy. */
if( sizeof( isaac_state->randrsl ) < seed->length )
{
count = sizeof( isaac_state->randrsl );
}
else
{
memset( isaac_state->randrsl, 0, sizeof( isaac_state->randrsl ) );
count = seed->length;
}
if( count == 0 )
{
/* Start ISACC without a seed. */
randinit( isaac_state, 0 );
}
else
{
/* Seed the ISAAC state with entropy. */
memcpy( isaac_state->randrsl, seed->s, count );
/* The second parameter indicates that randrsl is non-empty. */
randinit( isaac_state, 1 );
}
return 0;
}
int nwipe_isaac_read( NWIPE_PRNG_READ_SIGNATURE )
{
u32 i = 0;
u32 ii;
u32 words = count / SIZE_OF_ISAAC; // the values of isaac is strictly 4 bytes
u32 remain = count % SIZE_OF_ISAAC; // the values of isaac is strictly 4 bytes
randctx* isaac_state = *state;
/* Isaac returns 4-bytes per call, so progress by 4 bytes. */
for( ii = 0; ii < words; ++ii )
{
/* get the next 32bit random number */
isaac( isaac_state );
nwipe_u32tobuffer( (u8*) ( buffer + i ), isaac_state->randrsl[0], SIZE_OF_ISAAC );
i = i + SIZE_OF_ISAAC;
}
/* If there is some remainder copy only relevant number of bytes to not overflow the buffer. */
if( remain > 0 )
{
/* get the next 32bit random number */
isaac( isaac_state );
nwipe_u32tobuffer( (u8*) ( buffer + i ), isaac_state->randrsl[0], SIZE_OF_ISAAC );
}
return 0;
}