blob: 38c104c92db0482f5b8154c4f6165e1686fe0f8b [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 Bakkercce9d772011-11-18 14:26:47 +00004 * Copyright (C) 2006-2011, Brainspark B.V.
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
26#include "polarssl/config.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000027
Paul Bakker6083fd22011-12-03 21:45:14 +000028#include "polarssl/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000029
Paul Bakkerfc36d162011-01-27 16:50:02 +000030#include <stdio.h>
31
Paul Bakker6083fd22011-12-03 21:45:14 +000032#if !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000033int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000034{
Paul Bakkercce9d772011-11-18 14:26:47 +000035 ((void) argc);
36 ((void) argv);
37
Paul Bakker6083fd22011-12-03 21:45:14 +000038 printf("POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000039 return( 0 );
40}
41#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000042int main( int argc, char *argv[] )
43{
44 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000045 int i, k, ret;
46 entropy_context entropy;
47 unsigned char buf[ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000048
49 if( argc < 2 )
50 {
51 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
52 return( 1 );
53 }
54
55 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
56 {
57 printf( "failed to open '%s' for writing.\n", argv[0] );
58 return( 1 );
59 }
60
Paul Bakker6083fd22011-12-03 21:45:14 +000061 entropy_init( &entropy );
Paul Bakkerfc36d162011-01-27 16:50:02 +000062
63 for( i = 0, k = 768; i < k; i++ )
64 {
Paul Bakker6083fd22011-12-03 21:45:14 +000065 ret = entropy_func( &entropy, buf, sizeof( buf ) );
66 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000067 {
Paul Bakker6083fd22011-12-03 21:45:14 +000068 printf("failed!\n");
69 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000070 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000071
Paul Bakker6083fd22011-12-03 21:45:14 +000072 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +000073
74 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
75 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
76 fflush( stdout );
77 }
78
Paul Bakker6083fd22011-12-03 21:45:14 +000079 ret = 0;
80
81cleanup:
Paul Bakkerfc36d162011-01-27 16:50:02 +000082
83 fclose( f );
Paul Bakker6083fd22011-12-03 21:45:14 +000084
85 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +000086}
Paul Bakker6083fd22011-12-03 21:45:14 +000087#endif /* POLARSSL_ENTROPY_C */