blob: 38bd423619b51eeaf04b14da7e4dc6ee6d7974c7 [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
Paul Bakker6083fd22011-12-03 21:45:14 +000029#include "polarssl/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000030
Paul Bakkerfc36d162011-01-27 16:50:02 +000031#include <stdio.h>
32
Paul Bakker6083fd22011-12-03 21:45:14 +000033#if !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000034int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000035{
Paul Bakkercce9d772011-11-18 14:26:47 +000036 ((void) argc);
37 ((void) argv);
38
Paul Bakker6083fd22011-12-03 21:45:14 +000039 printf("POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000040 return( 0 );
41}
42#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000043int main( int argc, char *argv[] )
44{
45 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000046 int i, k, ret;
47 entropy_context entropy;
48 unsigned char buf[ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000049
50 if( argc < 2 )
51 {
52 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
53 return( 1 );
54 }
55
56 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
57 {
58 printf( "failed to open '%s' for writing.\n", argv[0] );
59 return( 1 );
60 }
61
Paul Bakker6083fd22011-12-03 21:45:14 +000062 entropy_init( &entropy );
Paul Bakkerfc36d162011-01-27 16:50:02 +000063
64 for( i = 0, k = 768; i < k; i++ )
65 {
Paul Bakker6083fd22011-12-03 21:45:14 +000066 ret = entropy_func( &entropy, buf, sizeof( buf ) );
67 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000068 {
Paul Bakker6083fd22011-12-03 21:45:14 +000069 printf("failed!\n");
70 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000071 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000072
Paul Bakker6083fd22011-12-03 21:45:14 +000073 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +000074
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
Paul Bakker6083fd22011-12-03 21:45:14 +000080 ret = 0;
81
82cleanup:
Paul Bakkerfc36d162011-01-27 16:50:02 +000083
84 fclose( f );
Paul Bakker1ffefac2013-09-28 15:23:03 +020085 entropy_free( &entropy );
Paul Bakker6083fd22011-12-03 21:45:14 +000086
87 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +000088}
Paul Bakker6083fd22011-12-03 21:45:14 +000089#endif /* POLARSSL_ENTROPY_C */