blob: 1b57c8bdc0ce2e4777891afd3af2a4640a18d483 [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
2 * \brief Generate random data into a file
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_fprintf fprintf
34#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000045int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000048 return( 0 );
49}
50#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000051int main( int argc, char *argv[] )
52{
53 FILE *f;
54 time_t t;
Paul Bakkera317a982014-06-18 16:44:11 +020055 int i, k, ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_havege_state hs;
Paul Bakkerfc36d162011-01-27 16:50:02 +000057 unsigned char buf[1024];
58
59 if( argc < 2 )
60 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000062 return( 1 );
63 }
64
65 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
66 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000068 return( 1 );
69 }
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 mbedtls_havege_init( &hs );
Paul Bakkerfc36d162011-01-27 16:50:02 +000072
73 t = time( NULL );
74
75 for( i = 0, k = 768; i < k; i++ )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 if( mbedtls_havege_random( &hs, buf, sizeof( buf ) ) != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 mbedtls_printf( "Failed to get random from source.\n" );
Paul Bakkera317a982014-06-18 16:44:11 +020080
81 ret = 1;
82 goto exit;
Paul Bakkera3d195c2011-11-27 21:07:34 +000083 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000084
85 fwrite( buf, sizeof( buf ), 1, f );
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
veggie64a57992015-01-26 10:35:25 -050088 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
Paul Bakkerfc36d162011-01-27 16:50:02 +000089 fflush( stdout );
90 }
91
92 if( t == time( NULL ) )
93 t--;
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 mbedtls_printf(" \n ");
Paul Bakkerfc754a92011-12-05 13:23:51 +000096
Paul Bakkera317a982014-06-18 16:44:11 +020097exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_havege_free( &hs );
Paul Bakkerfc36d162011-01-27 16:50:02 +000099 fclose( f );
Paul Bakkera317a982014-06-18 16:44:11 +0200100 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102#endif /* MBEDTLS_HAVEGE_C */