blob: 88d03a3cb66a61442d4877e078870b119e41cdfb [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 *
Paul Bakker530927b2015-02-13 14:24:10 +01004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://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 Bakker6083fd22011-12-03 21:45:14 +000025#include "polarssl/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000026
Paul Bakkerfc36d162011-01-27 16:50:02 +000027#include <stdio.h>
28
Paul Bakker6083fd22011-12-03 21:45:14 +000029#if !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000030int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000031{
Paul Bakkercce9d772011-11-18 14:26:47 +000032 ((void) argc);
33 ((void) argv);
34
Paul Bakker6083fd22011-12-03 21:45:14 +000035 printf("POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000036 return( 0 );
37}
38#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000039int main( int argc, char *argv[] )
40{
41 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000042 int i, k, ret;
43 entropy_context entropy;
44 unsigned char buf[ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000045
46 if( argc < 2 )
47 {
48 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
49 return( 1 );
50 }
51
52 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
53 {
54 printf( "failed to open '%s' for writing.\n", argv[0] );
55 return( 1 );
56 }
57
Paul Bakker6083fd22011-12-03 21:45:14 +000058 entropy_init( &entropy );
Paul Bakkerfc36d162011-01-27 16:50:02 +000059
60 for( i = 0, k = 768; i < k; i++ )
61 {
Paul Bakker6083fd22011-12-03 21:45:14 +000062 ret = entropy_func( &entropy, buf, sizeof( buf ) );
63 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000064 {
Paul Bakker6083fd22011-12-03 21:45:14 +000065 printf("failed!\n");
66 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000067 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000068
Paul Bakker6083fd22011-12-03 21:45:14 +000069 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +000070
71 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
72 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
73 fflush( stdout );
74 }
75
Paul Bakker6083fd22011-12-03 21:45:14 +000076 ret = 0;
77
78cleanup:
Paul Bakkerfc36d162011-01-27 16:50:02 +000079
80 fclose( f );
Paul Bakker6083fd22011-12-03 21:45:14 +000081
82 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +000083}
Paul Bakker6083fd22011-12-03 21:45:14 +000084#endif /* POLARSSL_ENTROPY_C */