diff --git a/src/Makefile.am b/src/Makefile.am index 6dec022..ac652c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,5 +6,5 @@ AM_LDFLAGS = # this lists the binaries to produce, the (non-PHONY, binary) targets in # the previous manual Makefile bin_PROGRAMS = nwipe -nwipe_SOURCES = context.h logging.h options.h prng.h version.h temperature.h nwipe.c gui.c method.h pass.c device.c gui.h isaac_rand/isaac_standard.h isaac_rand/isaac_rand.h isaac_rand/isaac_rand.c isaac_rand/isaac64.h isaac_rand/isaac64.c mt19937ar-cok/mt19937ar-cok.c nwipe.h mt19937ar-cok/mt19937ar-cok.h xor/xoroshiro256_prng.h xor/xoroshiro256_prng.c pass.h device.h logging.c method.c options.c prng.c version.c temperature.c PDFGen/pdfgen.h PDFGen/pdfgen.c create_pdf.c create_pdf.h embedded_images/shred_db.jpg.c embedded_images/shred_db.jpg.h embedded_images/tick_erased.jpg.c embedded_images/tick_erased.jpg.h embedded_images/redcross.c embedded_images/redcross.h hpa_dco.h hpa_dco.c miscellaneous.h miscellaneous.c embedded_images/nwipe_exclamation.jpg.h embedded_images/nwipe_exclamation.jpg.c conf.h conf.c customers.h customers.c hddtemp_scsi/hddtemp.h hddtemp_scsi/scsi.h hddtemp_scsi/scsicmds.h hddtemp_scsi/get_scsi_temp.c hddtemp_scsi/scsi.c hddtemp_scsi/scsicmds.c +nwipe_SOURCES = context.h logging.h options.h prng.h version.h temperature.h nwipe.c gui.c method.h pass.c device.c gui.h isaac_rand/isaac_standard.h isaac_rand/isaac_rand.h isaac_rand/isaac_rand.c isaac_rand/isaac64.h isaac_rand/isaac64.c mt19937ar-cok/mt19937ar-cok.c nwipe.h mt19937ar-cok/mt19937ar-cok.h alfg/add_lagg_fibonacci_prng.h alfg/add_lagg_fibonacci_prng.c xor/xoroshiro256_prng.h xor/xoroshiro256_prng.c pass.h device.h logging.c method.c options.c prng.c version.c temperature.c PDFGen/pdfgen.h PDFGen/pdfgen.c create_pdf.c create_pdf.h embedded_images/shred_db.jpg.c embedded_images/shred_db.jpg.h embedded_images/tick_erased.jpg.c embedded_images/tick_erased.jpg.h embedded_images/redcross.c embedded_images/redcross.h hpa_dco.h hpa_dco.c miscellaneous.h miscellaneous.c embedded_images/nwipe_exclamation.jpg.h embedded_images/nwipe_exclamation.jpg.c conf.h conf.c customers.h customers.c hddtemp_scsi/hddtemp.h hddtemp_scsi/scsi.h hddtemp_scsi/scsicmds.h hddtemp_scsi/get_scsi_temp.c hddtemp_scsi/scsi.c hddtemp_scsi/scsicmds.c nwipe_LDADD = $(PARTED_LIBS) $(LIBCONFIG) diff --git a/src/alfg/add_lagg_fibonacci_prng.c b/src/alfg/add_lagg_fibonacci_prng.c new file mode 100644 index 0000000..e84f90e --- /dev/null +++ b/src/alfg/add_lagg_fibonacci_prng.c @@ -0,0 +1,79 @@ +/* + * Additive Lagged Fibonacci Generator (ALFG) Implementation + * Author: Fabian Druschke + * Date: 2024-03-13 + * + * This is an implementation of the Additive Lagged Fibonacci Generator (ALFG), + * a pseudorandom number generator known for its simplicity and good statistical properties + * for a wide range of applications. ALFGs are particularly noted for their long periods + * and efficiency in generating sequences of random numbers. However, like many other PRNGs, + * they are not suitable for cryptographic purposes due to their predictability. + * + * As the author of this implementation, I, Fabian Druschke, hereby release this work into + * the public domain. I dedicate any and all copyright interest in this work to the public + * domain, making it free to use for anyone for any purpose without any conditions, unless + * such conditions are required by law. + * + * This software is provided "as is", without warranty of any kind, express or implied, + * including but not limited to the warranties of merchantability, fitness for a particular + * purpose, and noninfringement. In no event shall the authors be liable for any claim, + * damages, or other liability, whether in an action of contract, tort, or otherwise, arising + * from, out of, or in connection with the software or the use or other dealings in the software. + * + * Note: This implementation is designed for non-cryptographic applications and should not be + * used where cryptographic security is required. + */ + +#include "add_lagg_fibonacci_prng.h" +#include +#include + +#define STATE_SIZE 64 // Size of the state array, sufficient for a high period +#define LAG_BIG 55 // Large lag, e.g., 55 +#define LAG_SMALL 24 // Small lag, e.g., 24 +#define MODULUS ( 1ULL << 48 ) // Modulus for the operations, here 2^48 for simple handling + +void add_lagg_fibonacci_init( add_lagg_fibonacci_state_t* state, uint64_t init_key[], unsigned long key_length ) +{ + // Simple initialization: Fill the state with the key values and then with a linear combination of them + for( unsigned long i = 0; i < STATE_SIZE; i++ ) + { + if( i < key_length ) + { + state->s[i] = init_key[i]; + } + else + { + // Simple method to generate further state values. Should be improved for serious applications. + state->s[i] = ( 6364136223846793005ULL * state->s[i - 1] + 1 ) % MODULUS; + } + } + state->index = 0; // Initialize the index for the first generation +} + +void add_lagg_fibonacci_genrand_uint256_to_buf( add_lagg_fibonacci_state_t* state, unsigned char* bufpos ) +{ + uint64_t* buf_as_uint64 = (uint64_t*) bufpos; // Interprets bufpos as a uint64_t array for direct assignment + int64_t result; // Use signed integer to handle potential negative results from subtraction + + for (int i = 0; i < 4; i++) { + // Subtract the two previous numbers in the sequence + result = (int64_t)state->s[(state->index + LAG_BIG) % STATE_SIZE] - (int64_t)state->s[(state->index + LAG_SMALL) % STATE_SIZE]; + + // Handle borrow if result is negative + if (result < 0) { + result += MODULUS; + // Optionally set a borrow flag or adjust the next operation based on borrow logic + } + + // Store the result (after adjustment) back into the state, ensuring it's positive and within range + state->s[state->index] = (uint64_t)result; + + // Write the result into buf_as_uint64 + buf_as_uint64[i] = state->s[state->index]; + + // Update the index for the next round + state->index = (state->index + 1) % STATE_SIZE; + } +} + diff --git a/src/alfg/add_lagg_fibonacci_prng.h b/src/alfg/add_lagg_fibonacci_prng.h new file mode 100644 index 0000000..eed42d1 --- /dev/null +++ b/src/alfg/add_lagg_fibonacci_prng.h @@ -0,0 +1,43 @@ +/* + * Additive Lagged Fibonacci Generator (ALFG) Implementation definitions + * Author: Fabian Druschke + * Date: 2024-03-13 + * + * This is an implementation of the Additive Lagged Fibonacci Generator (ALFG), + * a pseudorandom number generator known for its simplicity and good statistical properties + * for a wide range of applications. ALFGs are particularly noted for their long periods + * and efficiency in generating sequences of random numbers. However, like many other PRNGs, + * they are not suitable for cryptographic purposes due to their predictability. + * + * As the author of this implementation, I, Fabian Druschke, hereby release this work into + * the public domain. I dedicate any and all copyright interest in this work to the public + * domain, making it free to use for anyone for any purpose without any conditions, unless + * such conditions are required by law. + * + * This software is provided "as is", without warranty of any kind, express or implied, + * including but not limited to the warranties of merchantability, fitness for a particular + * purpose, and noninfringement. In no event shall the authors be liable for any claim, + * damages, or other liability, whether in an action of contract, tort, or otherwise, arising + * from, out of, or in connection with the software or the use or other dealings in the software. + * + * Note: This implementation is designed for non-cryptographic applications and should not be + * used where cryptographic security is required. + */ + +#ifndef ADD_LAGG_FIBONACCI_PRNG_H +#define ADD_LAGG_FIBONACCI_PRNG_H + +#include + +// State definition for the Additive Lagged Fibonacci Generator +typedef struct +{ + uint64_t s[64]; // State array + unsigned int index; // Current index in the state array +} add_lagg_fibonacci_state_t; + +// Function prototypes +void add_lagg_fibonacci_init( add_lagg_fibonacci_state_t* state, uint64_t init_key[], unsigned long key_length ); +void add_lagg_fibonacci_genrand_uint256_to_buf( add_lagg_fibonacci_state_t* state, unsigned char* bufpos ); + +#endif // ADD_LAGG_FIBONACCI_PRNG_H diff --git a/src/gui.c b/src/gui.c index 609be69..68aadd7 100644 --- a/src/gui.c +++ b/src/gui.c @@ -1601,6 +1601,7 @@ void nwipe_gui_prng( void ) extern nwipe_prng_t nwipe_isaac64; extern nwipe_prng_t nwipe_aes_ctr_prng; extern nwipe_prng_t nwipe_xoroshiro256_prng; + extern nwipe_prng_t nwipe_add_lagg_fibonacci_prng; extern int terminate_signal; @@ -1639,10 +1640,14 @@ void nwipe_gui_prng( void ) { focus = 2; } - if( nwipe_options.prng == &nwipe_xoroshiro256_prng ) + if( nwipe_options.prng == &nwipe_add_lagg_fibonacci_prng ) { focus = 3; } + if( nwipe_options.prng == &nwipe_xoroshiro256_prng ) + { + focus = 4; + } do { /* Clear the main window. */ @@ -1657,6 +1662,7 @@ void nwipe_gui_prng( void ) mvwprintw( main_window, yy++, tab1, " %s", nwipe_twister.label ); mvwprintw( main_window, yy++, tab1, " %s", nwipe_isaac.label ); mvwprintw( main_window, yy++, tab1, " %s", nwipe_isaac64.label ); + mvwprintw( main_window, yy++, tab1, " %s", nwipe_add_lagg_fibonacci_prng.label ); mvwprintw( main_window, yy++, tab1, " %s", nwipe_xoroshiro256_prng.label ); yy++; @@ -1740,8 +1746,53 @@ void nwipe_gui_prng( void ) tab1, "Performs best on a 64-bit CPU. Use ISAAC if this system has a 32-bit CPU. " ); break; - + case 3: + + mvwprintw( main_window, + yy++, + tab1, + "ALFG (Additive Lagged Fibonacci Generator), is a class of PRNGs utilizing" ); + mvwprintw( main_window, + yy++, + tab1, + "the Fibonacci sequence with additive operations between lagged values. While" ); + mvwprintw( main_window, + yy++, + tab1, + "they offer a good balance between speed and randomness, it's important to note" ); + mvwprintw( main_window, + yy++, + tab1, + "that they provide lower levels of security, making them less suitable for" ); + mvwprintw( main_window, + yy++, + tab1, + "cryptographic applications. Their periodicity depends on the choice of lags" ); + mvwprintw( main_window, + yy++, + tab1, + "and arithmetic operations, potentially achieving large values, often 2^N or" ); + mvwprintw( main_window, + yy++, + tab1, + "higher, where N is the bit length of the states. " ); + mvwprintw( main_window, + yy++, + tab1, + " " ); + mvwprintw( main_window, + yy++, + tab1, + "Efficient on CPUs of any bit width, particularly suited for non-cryptographic" ); + mvwprintw( main_window, + yy++, + tab1, + "applications requiring long sequences with a good speed-randomness trade-off. " ); + break; + + case 4: + mvwprintw( main_window, yy++, tab1, @@ -1850,6 +1901,10 @@ void nwipe_gui_prng( void ) nwipe_options.prng = &nwipe_isaac64; } if( focus == 3 ) + { + nwipe_options.prng = &nwipe_add_lagg_fibonacci_prng; + } + if( focus == 4 ) { nwipe_options.prng = &nwipe_xoroshiro256_prng; } diff --git a/src/options.c b/src/options.c index a1e99dc..fd706c9 100644 --- a/src/options.c +++ b/src/options.c @@ -42,6 +42,7 @@ int nwipe_options_parse( int argc, char** argv ) extern nwipe_prng_t nwipe_twister; extern nwipe_prng_t nwipe_isaac; extern nwipe_prng_t nwipe_isaac64; + extern nwipe_prng_t nwipe_add_lagg_fibonacci_prng; extern nwipe_prng_t nwipe_xoroshiro256_prng; /* The getopt() result holder. */ @@ -491,6 +492,11 @@ int nwipe_options_parse( int argc, char** argv ) nwipe_options.prng = &nwipe_isaac64; break; } + if( strcmp( optarg, "add_lagg_fibonacci_prng" ) == 0 ) + { + nwipe_options.prng = &nwipe_add_lagg_fibonacci_prng; + break; + } if( strcmp( optarg, "xoroshiro256_prng" ) == 0 ) { nwipe_options.prng = &nwipe_xoroshiro256_prng; @@ -545,6 +551,7 @@ void nwipe_options_log( void ) extern nwipe_prng_t nwipe_twister; extern nwipe_prng_t nwipe_isaac; extern nwipe_prng_t nwipe_isaac64; + extern nwipe_prng_t nwipe_add_lagg_fibonacci_prng; extern nwipe_prng_t nwipe_xoroshiro256_prng; /** @@ -597,26 +604,35 @@ void nwipe_options_log( void ) { nwipe_log( NWIPE_LOG_NOTICE, " prng = Mersenne Twister" ); } - if( nwipe_options.prng == &nwipe_xoroshiro256_prng ) - { - nwipe_log( NWIPE_LOG_NOTICE, " prng = XORoshiro-256 (EXPERIMENTAL!)" ); - } - else { - if( nwipe_options.prng == &nwipe_isaac ) + if( nwipe_options.prng == &nwipe_add_lagg_fibonacci_prng ) { - nwipe_log( NWIPE_LOG_NOTICE, " prng = Isaac" ); + nwipe_log( NWIPE_LOG_NOTICE, " prng = Lagged Fibonacci generator (EXPERIMENTAL!)" ); } else { - if( nwipe_options.prng == &nwipe_isaac64 ) + if( nwipe_options.prng == &nwipe_xoroshiro256_prng ) { - nwipe_log( NWIPE_LOG_NOTICE, " prng = Isaac64" ); + nwipe_log( NWIPE_LOG_NOTICE, " prng = XORoshiro-256 (EXPERIMENTAL!)" ); } else { - nwipe_log( NWIPE_LOG_NOTICE, " prng = Undefined" ); + if( nwipe_options.prng == &nwipe_isaac ) + { + nwipe_log( NWIPE_LOG_NOTICE, " prng = Isaac" ); + } + else + { + if( nwipe_options.prng == &nwipe_isaac64 ) + { + nwipe_log( NWIPE_LOG_NOTICE, " prng = Isaac64" ); + } + else + { + nwipe_log( NWIPE_LOG_NOTICE, " prng = Undefined" ); + } + } } } } @@ -693,7 +709,7 @@ void display_help() puts( " -l, --logfile=FILE Filename to log to. Default is STDOUT\n" ); puts( " -P, --PDFreportpath=PATH Path to write PDF reports to. Default is \".\"" ); puts( " If set to \"noPDF\" no PDF reports are written.\n" ); - puts( " -p, --prng=METHOD PRNG option (mersenne|twister|isaac|isaac64)\n" ); + puts( " -p, --prng=METHOD PRNG option (mersenne|twister|isaac|isaac64|add_lagg_fibonacci_prng)\n" ); puts( " -q, --quiet Anonymize logs and the GUI by removing unique data, i.e." ); puts( " serial numbers, LU WWN Device ID, and SMBIOS/DMI data" ); puts( " XXXXXX = S/N exists, ????? = S/N not obtainable\n" ); diff --git a/src/prng.c b/src/prng.c index d9fc407..c0707e2 100644 --- a/src/prng.c +++ b/src/prng.c @@ -25,6 +25,7 @@ #include "mt19937ar-cok/mt19937ar-cok.h" #include "isaac_rand/isaac_rand.h" #include "isaac_rand/isaac64.h" +#include "alfg/add_lagg_fibonacci_prng.h" //Lagged Fibonacci generator prototype #include "xor/xoroshiro256_prng.h" //XORoshiro-256 prototype nwipe_prng_t nwipe_twister = { "Mersenne Twister (mt19937ar-cok)", nwipe_twister_init, nwipe_twister_read }; @@ -32,6 +33,10 @@ nwipe_prng_t nwipe_twister = { "Mersenne Twister (mt19937ar-cok)", nwipe_twister nwipe_prng_t nwipe_isaac = { "ISAAC (rand.c 20010626)", nwipe_isaac_init, nwipe_isaac_read }; nwipe_prng_t nwipe_isaac64 = { "ISAAC-64 (isaac64.c)", nwipe_isaac64_init, nwipe_isaac64_read }; +/* ALFG PRNG Structure */ +nwipe_prng_t nwipe_add_lagg_fibonacci_prng = { "Lagged Fibonacci generator", + nwipe_add_lagg_fibonacci_prng_init, + nwipe_add_lagg_fibonacci_prng_read }; /* XOROSHIRO-256 PRNG Structure */ nwipe_prng_t nwipe_xoroshiro256_prng = { "XORoshiro-256", nwipe_xoroshiro256_prng_init, nwipe_xoroshiro256_prng_read }; @@ -255,8 +260,21 @@ int nwipe_isaac64_read( NWIPE_PRNG_READ_SIGNATURE ) return 0; } -/* EXPERIMENTAL implementation of XORoroshiro256 algorithm to provide high-quality, but a lot of random numbers */ +/* EXPERIMENTAL implementation of Lagged Fibonacci generator a lot of random numbers */ +int nwipe_add_lagg_fibonacci_prng_init( NWIPE_PRNG_INIT_SIGNATURE ) +{ + if( *state == NULL ) + { + nwipe_log( NWIPE_LOG_NOTICE, "Initialising Lagged Fibonacci generator PRNG" ); + *state = malloc( sizeof( add_lagg_fibonacci_state_t ) ); + } + add_lagg_fibonacci_init( + (add_lagg_fibonacci_state_t*) *state, (unsigned long*) ( seed->s ), seed->length / sizeof( unsigned long ) ); + return 0; +} + +/* EXPERIMENTAL implementation of XORoroshiro256 algorithm to provide high-quality, but a lot of random numbers */ int nwipe_xoroshiro256_prng_init( NWIPE_PRNG_INIT_SIGNATURE ) { nwipe_log( NWIPE_LOG_NOTICE, "Initialising XORoroshiro-256 PRNG" ); @@ -272,6 +290,32 @@ int nwipe_xoroshiro256_prng_init( NWIPE_PRNG_INIT_SIGNATURE ) return 0; } +int nwipe_add_lagg_fibonacci_prng_read( NWIPE_PRNG_READ_SIGNATURE ) +{ + u8* restrict bufpos = buffer; + size_t words = count / SIZE_OF_ADD_LAGG_FIBONACCI_PRNG; + + /* Loop to fill the buffer with blocks directly from the Fibonacci algorithm */ + for( size_t ii = 0; ii < words; ++ii ) + { + add_lagg_fibonacci_genrand_uint256_to_buf( (add_lagg_fibonacci_state_t*) *state, bufpos ); + bufpos += SIZE_OF_ADD_LAGG_FIBONACCI_PRNG; // Move to the next block + } + + /* Handle remaining bytes if count is not a multiple of SIZE_OF_ADD_LAGG_FIBONACCI_PRNG */ + const size_t remain = count % SIZE_OF_ADD_LAGG_FIBONACCI_PRNG; + if( remain > 0 ) + { + unsigned char temp_output[16]; // Temporary buffer for the last block + add_lagg_fibonacci_genrand_uint256_to_buf( (add_lagg_fibonacci_state_t*) *state, temp_output ); + + // Copy the remaining bytes + memcpy( bufpos, temp_output, remain ); + } + + return 0; // Success +} + int nwipe_xoroshiro256_prng_read( NWIPE_PRNG_READ_SIGNATURE ) { u8* restrict bufpos = buffer; @@ -290,6 +334,7 @@ int nwipe_xoroshiro256_prng_read( NWIPE_PRNG_READ_SIGNATURE ) { unsigned char temp_output[16]; // Temporary buffer for the last block xoroshiro256_genrand_uint256_to_buf( (xoroshiro256_state_t*) *state, temp_output ); + // Copy the remaining bytes memcpy( bufpos, temp_output, remain ); } diff --git a/src/prng.h b/src/prng.h index c48ee24..a9add09 100644 --- a/src/prng.h +++ b/src/prng.h @@ -55,6 +55,10 @@ int nwipe_isaac_read( NWIPE_PRNG_READ_SIGNATURE ); int nwipe_isaac64_init( NWIPE_PRNG_INIT_SIGNATURE ); int nwipe_isaac64_read( NWIPE_PRNG_READ_SIGNATURE ); +/* ALFG prototypes. */ +int nwipe_add_lagg_fibonacci_prng_init( NWIPE_PRNG_INIT_SIGNATURE ); +int nwipe_add_lagg_fibonacci_prng_read( NWIPE_PRNG_READ_SIGNATURE ); + /* XOROSHIRO-256 prototypes. */ int nwipe_xoroshiro256_prng_init( NWIPE_PRNG_INIT_SIGNATURE ); int nwipe_xoroshiro256_prng_read( NWIPE_PRNG_READ_SIGNATURE ); @@ -66,6 +70,9 @@ int nwipe_xoroshiro256_prng_read( NWIPE_PRNG_READ_SIGNATURE ); #define SIZE_OF_ISAAC 4 #define SIZE_OF_ISAAC64 8 +/* Size of the Lagged Fibonacci generator is not derived from the architecture, but it is strictly 32 bytes */ +#define SIZE_OF_ADD_LAGG_FIBONACCI_PRNG 32 + /* Size of the XOROSHIRO-256 is not derived from the architecture, but it is strictly 32 bytes */ #define SIZE_OF_XOROSHIRO256_PRNG 32