- Addedd writing and updating of seedfiles as functions to CTR_DRBG
diff --git a/include/polarssl/ctr_drbg.h b/include/polarssl/ctr_drbg.h
index 01a58e1..b2014c2 100644
--- a/include/polarssl/ctr_drbg.h
+++ b/include/polarssl/ctr_drbg.h
@@ -152,7 +152,7 @@
* \param add_len Length of additional data
*/
void ctr_drbg_update( ctr_drbg_context *ctx,
- const unsigned char *additional, size_t add_len );
+ const unsigned char *additional, size_t add_len );
/**
* \brief CTR_DRBG generate random with additional update input
@@ -189,6 +189,30 @@
int ctr_drbg_random( void *p_rng,
unsigned char *output, size_t output_len );
+#if defined(POLARSSL_FS_IO)
+/**
+ * \brief Write a seed file
+ *
+ * \param path Name of the file
+ *
+ * \return 0 if successful, 1 on file error, or
+ * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
+ */
+int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
+
+/**
+ * \brief Read and update a seed file. Seed is added to this
+ * instance
+ *
+ * \param path Name of the file
+ *
+ * \return 0 if successful, 1 on file error,
+ * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
+ * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
+ */
+int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
+#endif
+
/**
* \brief Checkup routine
*
diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c
index e3796a0..6611c3d 100644
--- a/library/ctr_drbg.c
+++ b/library/ctr_drbg.c
@@ -34,6 +34,10 @@
#include "polarssl/ctr_drbg.h"
+#if defined(POLARSSL_FS_IO)
+#include <stdio.h>
+#endif
+
int ctr_drbg_init( ctr_drbg_context *ctx,
int (*f_entropy)(void *, unsigned char *, size_t),
void *p_entropy,
@@ -329,6 +333,59 @@
return ctr_drbg_random_with_add( p_rng, output, output_len, NULL, 0 );
}
+#if defined(POLARSSL_FS_IO)
+int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path )
+{
+ int ret;
+ FILE *f;
+ unsigned char buf[ CTR_DRBG_MAX_INPUT ];
+
+ if( ( f = fopen( path, "wb" ) ) == NULL )
+ return( 1 );
+
+ if( ( ret = ctr_drbg_random( ctx, buf, CTR_DRBG_MAX_INPUT ) ) != 0 )
+ return( ret );
+
+ if( fwrite( buf, 1, CTR_DRBG_MAX_INPUT, f ) != CTR_DRBG_MAX_INPUT )
+ {
+ fclose( f );
+ return( 1 );
+ }
+
+ fclose( f );
+ return( 0 );
+}
+
+int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path )
+{
+ FILE *f;
+ size_t n;
+ unsigned char buf[ CTR_DRBG_MAX_INPUT ];
+
+ if( ( f = fopen( path, "rb" ) ) == NULL )
+ return( 1 );
+
+ fseek( f, 0, SEEK_END );
+ n = (size_t) ftell( f );
+ fseek( f, 0, SEEK_SET );
+
+ if( n > CTR_DRBG_MAX_INPUT )
+ return( POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG );
+
+ if( fread( buf, 1, n, f ) != n )
+ {
+ fclose( f );
+ return( 1 );
+ }
+
+ ctr_drbg_update( ctx, buf, n );
+
+ fclose( f );
+
+ return( ctr_drbg_write_seed_file( ctx, path ) );
+}
+#endif /* POLARSSL_FS_IO */
+
#if defined(POLARSSL_SELF_TEST)
#include <stdio.h>
diff --git a/programs/random/gen_random_ctr_drbg.c b/programs/random/gen_random_ctr_drbg.c
index 5eed520..fb3a340 100644
--- a/programs/random/gen_random_ctr_drbg.c
+++ b/programs/random/gen_random_ctr_drbg.c
@@ -64,6 +64,26 @@
ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (unsigned char *) "RANDOM_GEN", 10 );
ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF );
+#if defined(POLARSSL_FS_IO)
+ ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
+
+ if( ret == 1 )
+ {
+ printf("Failed to open seedfile. Generating one.\n");
+ ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
+ if( ret != 0 )
+ {
+ printf("failed in ctr_drbg_write_seed_file: %d\n", ret );
+ goto cleanup;
+ }
+ }
+ else if( ret != 0 )
+ {
+ printf("failed in ctr_drbg_update_seed_file: %d\n", ret );
+ goto cleanup;
+ }
+#endif
+
for( i = 0, k = 768; i < k; i++ )
{
ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
@@ -83,6 +103,7 @@
ret = 0;
cleanup:
+ printf("\n");
fclose( f );
diff --git a/programs/random/gen_random_havege.c b/programs/random/gen_random_havege.c
index 20246fe..9d3b560 100644
--- a/programs/random/gen_random_havege.c
+++ b/programs/random/gen_random_havege.c
@@ -83,6 +83,8 @@
if( t == time( NULL ) )
t--;
+ printf(" \n ");
+
fclose( f );
return( 0 );
}