blob: 946d334144c456caa04dd9565184586e22a68f1a [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_HAVEGE_C) && defined(POLARSSL_FS_IO)
Paul Bakkerfc36d162011-01-27 16:50:02 +000038#include "polarssl/havege.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000039
Paul Bakkerfc36d162011-01-27 16:50:02 +000040#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <time.h>
42#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000043
Rich Evans18b78c72015-02-11 14:06:19 +000044#if !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000045int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Paul Bakkercce9d772011-11-18 14:26:47 +000047 ((void) argc);
48 ((void) argv);
49
Rich Evansf90016a2015-01-19 14:26:37 +000050 polarssl_printf("POLARSSL_HAVEGE_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000051 return( 0 );
52}
53#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000054int main( int argc, char *argv[] )
55{
56 FILE *f;
57 time_t t;
Paul Bakkera317a982014-06-18 16:44:11 +020058 int i, k, ret = 0;
Paul Bakkerfc36d162011-01-27 16:50:02 +000059 havege_state hs;
60 unsigned char buf[1024];
61
62 if( argc < 2 )
63 {
Rich Evansf90016a2015-01-19 14:26:37 +000064 polarssl_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000065 return( 1 );
66 }
67
68 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
69 {
veggie64a57992015-01-26 10:35:25 -050070 polarssl_printf( "failed to open '%s' for writing.\n", argv[1] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000071 return( 1 );
72 }
73
74 havege_init( &hs );
75
76 t = time( NULL );
77
78 for( i = 0, k = 768; i < k; i++ )
79 {
Paul Bakkera3d195c2011-11-27 21:07:34 +000080 if( havege_random( &hs, buf, sizeof( buf ) ) != 0 )
81 {
Rich Evansf90016a2015-01-19 14:26:37 +000082 polarssl_printf( "Failed to get random from source.\n" );
Paul Bakkera317a982014-06-18 16:44:11 +020083
84 ret = 1;
85 goto exit;
Paul Bakkera3d195c2011-11-27 21:07:34 +000086 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000087
88 fwrite( buf, sizeof( buf ), 1, f );
89
veggie64a57992015-01-26 10:35:25 -050090 polarssl_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
91 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
Paul Bakkerfc36d162011-01-27 16:50:02 +000092 fflush( stdout );
93 }
94
95 if( t == time( NULL ) )
96 t--;
97
Rich Evansf90016a2015-01-19 14:26:37 +000098 polarssl_printf(" \n ");
Paul Bakkerfc754a92011-12-05 13:23:51 +000099
Paul Bakkera317a982014-06-18 16:44:11 +0200100exit:
101 havege_free( &hs );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000102 fclose( f );
Paul Bakkera317a982014-06-18 16:44:11 +0200103 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000104}
Paul Bakker5690efc2011-05-26 13:16:06 +0000105#endif /* POLARSSL_HAVEGE_C */