blob: 94e200d85a691f964739f49e79685cafddae7e16 [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 *
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
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakkerfc36d162011-01-27 16:50:02 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000031
Paul Bakker6083fd22011-12-03 21:45:14 +000032#include "polarssl/entropy.h"
33#include "polarssl/ctr_drbg.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000034
Paul Bakkerfc36d162011-01-27 16:50:02 +000035#include <stdio.h>
36
Paul Bakker6083fd22011-12-03 21:45:14 +000037#if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000038int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000039{
Paul Bakkercce9d772011-11-18 14:26:47 +000040 ((void) argc);
41 ((void) argv);
42
Paul Bakker6083fd22011-12-03 21:45:14 +000043 printf("POLARSSL_CTR_DRBG_C or POLARSSL_ENTROPY_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000044 return( 0 );
45}
46#else
Paul Bakkerfc36d162011-01-27 16:50:02 +000047int main( int argc, char *argv[] )
48{
49 FILE *f;
Paul Bakker6083fd22011-12-03 21:45:14 +000050 int i, k, ret;
51 ctr_drbg_context ctr_drbg;
52 entropy_context entropy;
Paul Bakkerfc36d162011-01-27 16:50:02 +000053 unsigned char buf[1024];
54
55 if( argc < 2 )
56 {
57 fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
58 return( 1 );
59 }
60
61 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
62 {
63 printf( "failed to open '%s' for writing.\n", argv[0] );
64 return( 1 );
65 }
66
Paul Bakker6083fd22011-12-03 21:45:14 +000067 entropy_init( &entropy );
Paul Bakkeref3f8c72013-06-24 13:01:08 +020068 ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000069 if( ret != 0 )
70 {
71 printf( "failed in ctr_drbg_init: %d\n", ret );
72 goto cleanup;
73 }
Paul Bakker6083fd22011-12-03 21:45:14 +000074 ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF );
Paul Bakkerfc36d162011-01-27 16:50:02 +000075
Paul Bakkerfc754a92011-12-05 13:23:51 +000076#if defined(POLARSSL_FS_IO)
77 ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
78
Paul Bakker3f9b6502011-12-15 19:50:22 +000079 if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR )
Paul Bakkerfc754a92011-12-05 13:23:51 +000080 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000081 printf( "Failed to open seedfile. Generating one.\n" );
Paul Bakkerfc754a92011-12-05 13:23:51 +000082 ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
83 if( ret != 0 )
84 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000085 printf( "failed in ctr_drbg_write_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +000086 goto cleanup;
87 }
88 }
89 else if( ret != 0 )
90 {
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000091 printf( "failed in ctr_drbg_update_seed_file: %d\n", ret );
Paul Bakkerfc754a92011-12-05 13:23:51 +000092 goto cleanup;
93 }
94#endif
95
Paul Bakkerfc36d162011-01-27 16:50:02 +000096 for( i = 0, k = 768; i < k; i++ )
97 {
Paul Bakker6083fd22011-12-03 21:45:14 +000098 ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
99 if( ret != 0 )
Paul Bakkera3d195c2011-11-27 21:07:34 +0000100 {
Paul Bakker6083fd22011-12-03 21:45:14 +0000101 printf("failed!\n");
102 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +0000103 }
Paul Bakkerfc36d162011-01-27 16:50:02 +0000104
Paul Bakker6083fd22011-12-03 21:45:14 +0000105 fwrite( buf, 1, sizeof( buf ), f );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000106
107 printf( "Generating 32Mb of data in file '%s'... %04.1f" \
108 "%% done\r", argv[1], (100 * (float) (i + 1)) / k );
109 fflush( stdout );
110 }
111
Paul Bakker6083fd22011-12-03 21:45:14 +0000112 ret = 0;
113
114cleanup:
Paul Bakkerfc754a92011-12-05 13:23:51 +0000115 printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +0000116
117 fclose( f );
Paul Bakkera317a982014-06-18 16:44:11 +0200118 ctr_drbg_free( &ctr_drbg );
Paul Bakker1ffefac2013-09-28 15:23:03 +0200119 entropy_free( &entropy );
Paul Bakker6083fd22011-12-03 21:45:14 +0000120
121 return( ret );
Paul Bakkerfc36d162011-01-27 16:50:02 +0000122}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123#endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */