mirror of
https://github.com/martijnvanbrummelen/nwipe.git
synced 2026-02-20 05:32:14 +00:00
Formatting prng module.
This commit is contained in:
2
.github/workflows/ci.yml
vendored
2
.github/workflows/ci.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
||||
- name: verifying code style
|
||||
# TODO use check-format when all the code has been formatted.
|
||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h && git diff-index --quiet HEAD
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
||||
|
||||
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
2
.github/workflows/ci_ubuntu-16.04.yml
vendored
@@ -23,4 +23,4 @@ jobs:
|
||||
- name: verifying code style
|
||||
# TODO use check-format when all the code has been formatted.
|
||||
# run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && make check-format
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h && git diff-index --quiet HEAD
|
||||
run: export PATH=$PATH:/usr/lib/llvm-5.0/bin && clang-format -i -style=file src/context.h src/device.h src/device.c src/nwipe.c src/nwipe.h src/options.c src/options.h src/version.h src/version.c src/method.h src/method.c src/pass.c src/pass.h src/prng.c src/prng.h && git diff-index --quiet HEAD
|
||||
|
||||
204
src/prng.c
204
src/prng.c
@@ -2,7 +2,7 @@
|
||||
* 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.
|
||||
@@ -14,10 +14,9 @@
|
||||
*
|
||||
* 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.
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*/
|
||||
|
||||
|
||||
#include "nwipe.h"
|
||||
#include "prng.h"
|
||||
#include "logging.h"
|
||||
@@ -25,144 +24,129 @@
|
||||
#include "mt19937ar-cok.h"
|
||||
#include "isaac_rand.h"
|
||||
|
||||
nwipe_prng_t nwipe_twister = {"Mersenne Twister (mt19937ar-cok)", nwipe_twister_init, nwipe_twister_read};
|
||||
|
||||
nwipe_prng_t nwipe_twister =
|
||||
nwipe_prng_t nwipe_isaac = {"ISAAC (rand.c 20010626)", nwipe_isaac_init, nwipe_isaac_read};
|
||||
|
||||
int nwipe_u32tobuffer( u8* buffer, u32 rand, int len )
|
||||
{
|
||||
"Mersenne Twister (mt19937ar-cok)",
|
||||
nwipe_twister_init,
|
||||
nwipe_twister_read
|
||||
};
|
||||
/*
|
||||
* 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 );
|
||||
}
|
||||
|
||||
nwipe_prng_t nwipe_isaac =
|
||||
{
|
||||
"ISAAC (rand.c 20010626)",
|
||||
nwipe_isaac_init,
|
||||
nwipe_isaac_read
|
||||
};
|
||||
|
||||
|
||||
/* Print given number of bytes from unsigned integer number to a byte stream buffer starting with low-endian*/
|
||||
int nwipe_u32tobuffer(u8 *buffer, u32 rand, int len)
|
||||
{
|
||||
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;
|
||||
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 )
|
||||
{
|
||||
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;
|
||||
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
|
||||
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) ;
|
||||
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;
|
||||
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;
|
||||
int count;
|
||||
randctx* isaac_state = *state;
|
||||
|
||||
if( *state == NULL )
|
||||
{
|
||||
/* This is the first time that we have been called. */
|
||||
*state = malloc( sizeof( randctx ) );
|
||||
isaac_state = *state;
|
||||
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;
|
||||
}
|
||||
}
|
||||
/* 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;
|
||||
}
|
||||
|
||||
/* 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 );
|
||||
|
||||
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 );
|
||||
}
|
||||
|
||||
/* The second parameter indicates that randrsl is non-empty. */
|
||||
randinit( isaac_state, 1 );
|
||||
}
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int nwipe_isaac_read( NWIPE_PRNG_READ_SIGNATURE )
|
||||
{
|
||||
/* The purpose of this function is unclear, as it does not do anything except immediately return !
|
||||
* Because the variables in the macro NWIPE_PRNG_READ_SIGNATURE were then unused this throws
|
||||
* up a handful of compiler warnings, related to variables being unused. To stop the compiler warnings
|
||||
* I've simply put in a (void) var so that compiler sees the variable are supposed to be unused.
|
||||
*
|
||||
* As this code works, I thought it best not to remove this function, just in case it servers
|
||||
* some purpose or is there for future use.
|
||||
*/
|
||||
/* The purpose of this function is unclear, as it does not do anything except immediately return !
|
||||
* Because the variables in the macro NWIPE_PRNG_READ_SIGNATURE were then unused this throws
|
||||
* up a handful of compiler warnings, related to variables being unused. To stop the compiler warnings
|
||||
* I've simply put in a (void) var so that compiler sees the variable are supposed to be unused.
|
||||
*
|
||||
* As this code works, I thought it best not to remove this function, just in case it serves
|
||||
* some purpose or is there for future use.
|
||||
*/
|
||||
|
||||
(void) state;
|
||||
(void) buffer;
|
||||
(void) count;
|
||||
(void) state;
|
||||
(void) buffer;
|
||||
(void) count;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* eof */
|
||||
|
||||
27
src/prng.h
27
src/prng.h
@@ -14,7 +14,7 @@
|
||||
*
|
||||
* 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.
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
*/
|
||||
|
||||
@@ -22,25 +22,25 @@
|
||||
#define PRNG_H_
|
||||
|
||||
/* A chunk of random data. */
|
||||
typedef struct /* nwipe_entropy_t */
|
||||
typedef struct
|
||||
{
|
||||
size_t length; /* Length of the entropy string in bytes. */
|
||||
u8* s; /* The actual bytes of the entropy string. */
|
||||
size_t length; // Length of the entropy string in bytes.
|
||||
u8* s; // The actual bytes of the entropy string.
|
||||
} nwipe_entropy_t;
|
||||
|
||||
#define NWIPE_PRNG_INIT_SIGNATURE void** state, nwipe_entropy_t* seed
|
||||
#define NWIPE_PRNG_READ_SIGNATURE void** state, void* buffer, size_t count
|
||||
#define NWIPE_PRNG_INIT_SIGNATURE void **state, nwipe_entropy_t *seed
|
||||
#define NWIPE_PRNG_READ_SIGNATURE void **state, void *buffer, size_t count
|
||||
|
||||
/* Function pointers for PRNG actions. */
|
||||
typedef int(*nwipe_prng_init_t)( NWIPE_PRNG_INIT_SIGNATURE );
|
||||
typedef int(*nwipe_prng_read_t)( NWIPE_PRNG_READ_SIGNATURE );
|
||||
typedef int ( *nwipe_prng_init_t )( NWIPE_PRNG_INIT_SIGNATURE );
|
||||
typedef int ( *nwipe_prng_read_t )( NWIPE_PRNG_READ_SIGNATURE );
|
||||
|
||||
/* The generic PRNG definition. */
|
||||
typedef struct /* nwipe_prng_t */
|
||||
typedef struct
|
||||
{
|
||||
const char* label; /* The name of the pseudo random number generator. */
|
||||
nwipe_prng_init_t init; /* Inialize the prng state with the seed. */
|
||||
nwipe_prng_read_t read; /* Read data from the prng. */
|
||||
const char* label; // The name of the pseudo random number generator.
|
||||
nwipe_prng_init_t init; // Inialize the prng state with the seed.
|
||||
nwipe_prng_read_t read; // Read data from the prng.
|
||||
} nwipe_prng_t;
|
||||
|
||||
/* Mersenne Twister prototypes. */
|
||||
@@ -54,7 +54,4 @@ int nwipe_isaac_read( NWIPE_PRNG_READ_SIGNATURE );
|
||||
/* Size of the twister is not derived from the architecture, but it is strictly 4 bytes */
|
||||
#define SIZE_OF_TWISTER 4
|
||||
|
||||
|
||||
#endif /* PRNG_H_ */
|
||||
|
||||
/* eof */
|
||||
|
||||
Reference in New Issue
Block a user