- Addedd writing and updating of seedfiles as functions to CTR_DRBG

This commit is contained in:
Paul Bakker 2011-12-05 13:23:51 +00:00
parent 1c70d409ad
commit fc754a9178
4 changed files with 105 additions and 1 deletions

View File

@ -189,6 +189,30 @@ int ctr_drbg_random_with_add( void *p_rng,
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
*

View File

@ -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 @@ int ctr_drbg_random( void *p_rng, unsigned char *output, size_t output_len )
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>

View File

@ -64,6 +64,26 @@ int main( int argc, char *argv[] )
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 @@ int main( int argc, char *argv[] )
ret = 0;
cleanup:
printf("\n");
fclose( f );

View File

@ -83,6 +83,8 @@ int main( int argc, char *argv[] )
if( t == time( NULL ) )
t--;
printf(" \n ");
fclose( f );
return( 0 );
}