blob: 58d7368adfeb0244044427126ecf30508745dbf7 [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
Paul Bakker6083fd22011-12-03 21:45:14 +00002 * \brief Use and generate random data into a file via the CTR_DBRG based on AES
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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://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
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_fprintf fprintf
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Rich Evans18b78c72015-02-11 14:06:19 +000037#if defined(POLARSSL_CTR_DRBG_C) && defined(POLARSSL_ENTROPY_C) &&\
38 defined(POLARSSL_FS_IO)
Paul Bakker6083fd22011-12-03 21:45:14 +000039#include "polarssl/entropy.h"
40#include "polarssl/ctr_drbg.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000041
Paul Bakkerfc36d162011-01-27 16:50:02 +000042#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000043#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000044
Rich Evans18b78c72015-02-11 14:06:19 +000045#if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C) ||\
46 !defined(POLARSSL_FS_IO)
Paul Bakkercce9d772011-11-18 14:26:47 +000047int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Paul Bakkercce9d772011-11-18 14:26:47 +000049 ((void) argc);
50 ((void) argv);
51
Rich Evans18b78c72015-02-11 14:06:19 +000052 polarssl_printf("POLARSSL_CTR_DRBG_C and/or POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000053 return( 0 );
54}
55#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000056int main( int argc, char *argv[] )
57{
58 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000059 int i, k, ret;
60 ctr_drbg_context ctr_drbg;
61 entropy_context entropy;
Paul Bakkerfc36d162011-01-27 16:50:02 +000062 unsigned char buf[1024];
63
64 if( argc < 2 )
65 {
Rich Evansf90016a2015-01-19 14:26:37 +000066 polarssl_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000067 return( 1 );
68 }
69
70 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
71 {
veggie64a57992015-01-26 10:35:25 -050072 polarssl_printf( "failed to open '%s' for writing.\n", argv[1] );
Paul Bakkerfc36d162011-01-27 16:50:02 +000073 return( 1 );
74 }
75
Paul Bakker6083fd22011-12-03 21:45:14 +000076 entropy_init( &entropy );
Paul Bakkeref3f8c72013-06-24 13:01:08 +020077 ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000078 if( ret != 0 )
79 {
Rich Evansf90016a2015-01-19 14:26:37 +000080 polarssl_printf( "failed in ctr_drbg_init: %d\n", ret );
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000081 goto cleanup;
82 }
Paul Bakker6083fd22011-12-03 21:45:14 +000083 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF );
Paul Bakkerfc36d162011-01-27 16:50:02 +000084
Paul Bakkerfc754a92011-12-05 13:23:51 +000085#if defined(POLARSSL_FS_IO)
86 ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
87
Paul Bakker3f9b6502011-12-15 19:50:22 +000088 if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR )
Paul Bakkerfc754a92011-12-05 13:23:51 +000089 {
Rich Evansf90016a2015-01-19 14:26:37 +000090 polarssl_printf( "Failed to open seedfile. Generating one.\n" );
Paul Bakkerfc754a92011-12-05 13:23:51 +000091 ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
92 if( ret != 0 )
93 {
Rich Evansf90016a2015-01-19 14:26:37 +000094 polarssl_printf( "failed in ctr_drbg_write_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +000095 goto cleanup;
96 }
97 }
98 else if( ret != 0 )
99 {
Rich Evansf90016a2015-01-19 14:26:37 +0000100 polarssl_printf( "failed in ctr_drbg_update_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +0000101 goto cleanup;
102 }
103#endif
104
Paul Bakkerfc36d162011-01-27 16:50:02 +0000105 for( i = 0, k = 768; i < k; i++ )
106 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000107 ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
108 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000109 {
Rich Evansf90016a2015-01-19 14:26:37 +0000110 polarssl_printf("failed!\n");
Paul Bakker6083fd22011-12-03 21:45:14 +0000111 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000112 }
Paul Bakkerfc36d162011-01-27 16:50:02 +0000113
Paul Bakker6083fd22011-12-03 21:45:14 +0000114 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000115
veggie64a57992015-01-26 10:35:25 -0500116 polarssl_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
117 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000118 fflush( stdout );
119 }
120
Paul Bakker6083fd22011-12-03 21:45:14 +0000121 ret = 0;
122
123cleanup:
Rich Evansf90016a2015-01-19 14:26:37 +0000124 polarssl_printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +0000125
126 fclose( f );
Paul Bakkera317a982014-06-18 16:44:11 +0200127 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200128 entropy_free( &entropy );
Paul Bakker6083fd22011-12-03 21:45:14 +0000129
130 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000131}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000132#endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */