blob: f0b47805b3c9a0b23f6757e812449bd84981e7bf [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é-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerfc36d162011-01-27 16:50:02 +00005 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00006 * This file is part of mbed TLS (https://www.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"
26#include "polarssl/ctr_drbg.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000027
Paul Bakkerfc36d162011-01-27 16:50:02 +000028#include <stdio.h>
29
Paul Bakker6083fd22011-12-03 21:45:14 +000030#if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000031int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000032{
Paul Bakkercce9d772011-11-18 14:26:47 +000033 ((void) argc);
34 ((void) argv);
35
Paul Bakker6083fd22011-12-03 21:45:14 +000036 printf("POLARSSL_CTR_DRBG_C or POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000037 return( 0 );
38}
39#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000040int main( int argc, char *argv[] )
41{
42 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000043 int i, k, ret;
44 ctr_drbg_context ctr_drbg;
45 entropy_context entropy;
Paul Bakkerfc36d162011-01-27 16:50:02 +000046 unsigned char buf[1024];
47
48 if( argc < 2 )
49 {
50 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
51 return( 1 );
52 }
53
54 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
55 {
56 printf( "failed to open '%s' for writing.\n", argv[0] );
57 return( 1 );
58 }
59
Paul Bakker6083fd22011-12-03 21:45:14 +000060 entropy_init( &entropy );
Paul Bakkere0225e42013-06-06 12:52:24 +020061 ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000062 if( ret != 0 )
63 {
64 printf( "failed in ctr_drbg_init: %d\n", ret );
65 goto cleanup;
66 }
Paul Bakker6083fd22011-12-03 21:45:14 +000067 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF );
Paul Bakkerfc36d162011-01-27 16:50:02 +000068
Paul Bakkerfc754a92011-12-05 13:23:51 +000069#if defined(POLARSSL_FS_IO)
70 ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
71
Paul Bakker3f9b6502011-12-15 19:50:22 +000072 if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR )
Paul Bakkerfc754a92011-12-05 13:23:51 +000073 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000074 printf( "Failed to open seedfile. Generating one.\n" );
Paul Bakkerfc754a92011-12-05 13:23:51 +000075 ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
76 if( ret != 0 )
77 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000078 printf( "failed in ctr_drbg_write_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +000079 goto cleanup;
80 }
81 }
82 else if( ret != 0 )
83 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000084 printf( "failed in ctr_drbg_update_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +000085 goto cleanup;
86 }
87#endif
88
Paul Bakkerfc36d162011-01-27 16:50:02 +000089 for( i = 0, k = 768; i < k; i++ )
90 {
Paul Bakker6083fd22011-12-03 21:45:14 +000091 ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
92 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +000093 {
Paul Bakker6083fd22011-12-03 21:45:14 +000094 printf("failed!\n");
95 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000096 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000097
Paul Bakker6083fd22011-12-03 21:45:14 +000098 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +000099
100 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
101 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
102 fflush( stdout );
103 }
104
Paul Bakker6083fd22011-12-03 21:45:14 +0000105 ret = 0;
106
107cleanup:
Paul Bakkerfc754a92011-12-05 13:23:51 +0000108 printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +0000109
110 fclose( f );
Paul Bakker6083fd22011-12-03 21:45:14 +0000111
112 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000113}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114#endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */