blob: eba93dd0e29695a48383e389cd8cdd3e93d06fba [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
2 * \brief Generate random data into a file
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
Manuel Pégourié-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerfc36d162011-01-27 16:50:02 +00007 *
Paul Bakkerfc36d162011-01-27 16:50:02 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerfc36d162011-01-27 16:50:02 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000028
Paul Bakkerfc36d162011-01-27 16:50:02 +000029#include "polarssl/havege.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000030
Paul Bakkerfc36d162011-01-27 16:50:02 +000031#include <time.h>
32#include <stdio.h>
33
Paul Bakker5690efc2011-05-26 13:16:06 +000034#if !defined(POLARSSL_HAVEGE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000035int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000036{
Paul Bakkercce9d772011-11-18 14:26:47 +000037 ((void) argc);
38 ((void) argv);
39
Paul Bakker5690efc2011-05-26 13:16:06 +000040 printf("POLARSSL_HAVEGE_C not defined.\n");
41 return( 0 );
42}
43#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000044int main( int argc, char *argv[] )
45{
46 FILE *f;
47 time_t t;
Paul Bakkera317a982014-06-18 16:44:11 +020048 int i, k, ret = 0;
Paul Bakkerfc36d162011-01-27 16:50:02 +000049 havege_state hs;
50 unsigned char buf[1024];
51
52 if( argc < 2 )
53 {
54 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
55 return( 1 );
56 }
57
58 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
59 {
60 printf( "failed to open '%s' for writing.\n", argv[0] );
61 return( 1 );
62 }
63
64 havege_init( &hs );
65
66 t = time( NULL );
67
68 for( i = 0, k = 768; i < k; i++ )
69 {
Paul Bakkera3d195c2011-11-27 21:07:34 +000070 if( havege_random( &hs, buf, sizeof( buf ) ) != 0 )
71 {
72 printf( "Failed to get random from source.\n" );
Paul Bakkera317a982014-06-18 16:44:11 +020073
74 ret = 1;
75 goto exit;
Paul Bakkera3d195c2011-11-27 21:07:34 +000076 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000077
78 fwrite( buf, sizeof( buf ), 1, f );
79
80 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
81 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
82 fflush( stdout );
83 }
84
85 if( t == time( NULL ) )
86 t--;
87
Paul Bakkerfc754a92011-12-05 13:23:51 +000088 printf(" \n ");
89
Paul Bakkera317a982014-06-18 16:44:11 +020090exit:
91 havege_free( &hs );
Paul Bakkerfc36d162011-01-27 16:50:02 +000092 fclose( f );
Paul Bakkera317a982014-06-18 16:44:11 +020093 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +000094}
Paul Bakker5690efc2011-05-26 13:16:06 +000095#endif /* POLARSSL_HAVEGE_C */