blob: b639bf6dc14262713a5f7bb98c77f31beb5f0fa1 [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
2 * \brief Generate random data into a file
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.polarssl.org)
Paul Bakkerfc36d162011-01-27 16:50:02 +00007 *
8 * 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
23#include "polarssl/config.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000024
Paul Bakkerfc36d162011-01-27 16:50:02 +000025#include "polarssl/havege.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000026
Paul Bakkerfc36d162011-01-27 16:50:02 +000027#include <time.h>
28#include <stdio.h>
29
Paul Bakker5690efc2011-05-26 13:16:06 +000030#if !defined(POLARSSL_HAVEGE_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000031int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000032{
Paul Bakkercce9d772011-11-18 14:26:47 +000033 ((void) argc);
34 ((void) argv);
35
Paul Bakker5690efc2011-05-26 13:16:06 +000036 printf("POLARSSL_HAVEGE_C not defined.\n");
37 return( 0 );
38}
39#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000040int main( int argc, char *argv[] )
41{
42 FILE *f;
43 time_t t;
Paul Bakkera3d195c2011-11-27 21:07:34 +000044 int i, k;
Paul Bakkerfc36d162011-01-27 16:50:02 +000045 havege_state hs;
46 unsigned char buf[1024];
47
48 if( argc < 2 )
49 {
50 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
51 return( 1 );
52 }
53
54 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
55 {
56 printf( "failed to open '%s' for writing.\n", argv[0] );
57 return( 1 );
58 }
59
60 havege_init( &hs );
61
62 t = time( NULL );
63
64 for( i = 0, k = 768; i < k; i++ )
65 {
Paul Bakkera3d195c2011-11-27 21:07:34 +000066 if( havege_random( &hs, buf, sizeof( buf ) ) != 0 )
67 {
68 printf( "Failed to get random from source.\n" );
69 fclose( f );
70 return( 1 );
71 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000072
73 fwrite( buf, sizeof( buf ), 1, f );
74
75 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
76 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
77 fflush( stdout );
78 }
79
80 if( t == time( NULL ) )
81 t--;
82
Paul Bakkerfc754a92011-12-05 13:23:51 +000083 printf(" \n ");
84
Paul Bakkerfc36d162011-01-27 16:50:02 +000085 fclose( f );
86 return( 0 );
87}
Paul Bakker5690efc2011-05-26 13:16:06 +000088#endif /* POLARSSL_HAVEGE_C */