blob: 5234f01741e331b07a6d1e960cf2e296b42ab0e5 [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
Paul Bakker6083fd22011-12-03 21:45:14 +00002 * \brief Use and generate multiple entropies calls into a file
Paul Bakkerfc36d162011-01-27 16:50:02 +00003 *
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
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker6083fd22011-12-03 21:45:14 +000038#include "polarssl/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000039
Paul Bakkerfc36d162011-01-27 16:50:02 +000040#include <stdio.h>
41
Paul Bakker6083fd22011-12-03 21:45:14 +000042#if !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000043int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000044{
Paul Bakkercce9d772011-11-18 14:26:47 +000045 ((void) argc);
46 ((void) argv);
47
Rich Evansf90016a2015-01-19 14:26:37 +000048 polarssl_printf("POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000049 return( 0 );
50}
51#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000052int main( int argc, char *argv[] )
53{
54 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000055 int i, k, ret;
56 entropy_context entropy;
57 unsigned char buf[ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000058
59 if( argc < 2 )
60 {
Rich Evansf90016a2015-01-19 14:26:37 +000061 polarssl_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 {
Rich Evansf90016a2015-01-19 14:26:37 +000067 polarssl_printf( "failed to open '%s' for writing.\n", argv[0] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000068 return( 1 );
69 }
70
Paul Bakker6083fd22011-12-03 21:45:14 +000071 entropy_init( &entropy );
Paul Bakkerfc36d162011-01-27 16:50:02 +000072
73 for( i = 0, k = 768; i < k; i++ )
74 {
Paul Bakker6083fd22011-12-03 21:45:14 +000075 ret = entropy_func( &entropy, buf, sizeof( buf ) );
76 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000077 {
Rich Evansf90016a2015-01-19 14:26:37 +000078 polarssl_printf("failed!\n");
Paul Bakker6083fd22011-12-03 21:45:14 +000079 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000080 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000081
Paul Bakker6083fd22011-12-03 21:45:14 +000082 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +000083
Rich Evansf90016a2015-01-19 14:26:37 +000084 polarssl_printf( "Generating 32Mb of data in file '%s'... %04.1f" \
Paul Bakkerfc36d162011-01-27 16:50:02 +000085 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
86 fflush( stdout );
87 }
88
Paul Bakker6083fd22011-12-03 21:45:14 +000089 ret = 0;
90
91cleanup:
Paul Bakkerfc36d162011-01-27 16:50:02 +000092
93 fclose( f );
Paul Bakker1ffefac2013-09-28 15:23:03 +020094 entropy_free( &entropy );
Paul Bakker6083fd22011-12-03 21:45:14 +000095
96 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +000097}
Paul Bakker6083fd22011-12-03 21:45:14 +000098#endif /* POLARSSL_ENTROPY_C */