| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 1 | /** | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 2 | *  \brief Use and generate random data into a file via the CTR_DBRG based on AES | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 3 | * | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 4 | *  Copyright (C) 2006-2011, Brainspark B.V. | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 5 | * | 
|  | 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 Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 27 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 28 | #include "polarssl/entropy.h" | 
|  | 29 | #include "polarssl/ctr_drbg.h" | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 30 |  | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 31 | #include <stdio.h> | 
|  | 32 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 33 | #if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C) | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 34 | int main( int argc, char *argv[] ) | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 35 | { | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 36 | ((void) argc); | 
|  | 37 | ((void) argv); | 
|  | 38 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 39 | printf("POLARSSL_CTR_DRBG_C or POLARSSL_ENTROPY_C not defined.\n"); | 
| Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 40 | return( 0 ); | 
|  | 41 | } | 
|  | 42 | #else | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 43 | int main( int argc, char *argv[] ) | 
|  | 44 | { | 
|  | 45 | FILE *f; | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 46 | int i, k, ret; | 
|  | 47 | ctr_drbg_context ctr_drbg; | 
|  | 48 | entropy_context entropy; | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 49 | unsigned char buf[1024]; | 
|  | 50 |  | 
|  | 51 | if( argc < 2 ) | 
|  | 52 | { | 
|  | 53 | fprintf( stderr, "usage: %s <output filename>\n", argv[0] ); | 
|  | 54 | return( 1 ); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) | 
|  | 58 | { | 
|  | 59 | printf( "failed to open '%s' for writing.\n", argv[0] ); | 
|  | 60 | return( 1 ); | 
|  | 61 | } | 
|  | 62 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 63 | entropy_init( &entropy ); | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 64 | ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 ); | 
| Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 65 | if( ret != 0 ) | 
|  | 66 | { | 
|  | 67 | printf( "failed in ctr_drbg_init: %d\n", ret ); | 
|  | 68 | goto cleanup; | 
|  | 69 | } | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 70 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF ); | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 71 |  | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 72 | #if defined(POLARSSL_FS_IO) | 
|  | 73 | ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" ); | 
|  | 74 |  | 
| Paul Bakker | 3f9b650 | 2011-12-15 19:50:22 +0000 | [diff] [blame] | 75 | if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR ) | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 76 | { | 
| Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 77 | printf( "Failed to open seedfile. Generating one.\n" ); | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 78 | ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" ); | 
|  | 79 | if( ret != 0 ) | 
|  | 80 | { | 
| Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 81 | printf( "failed in ctr_drbg_write_seed_file: %d\n", ret ); | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 82 | goto cleanup; | 
|  | 83 | } | 
|  | 84 | } | 
|  | 85 | else if( ret != 0 ) | 
|  | 86 | { | 
| Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 87 | printf( "failed in ctr_drbg_update_seed_file: %d\n", ret ); | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 88 | goto cleanup; | 
|  | 89 | } | 
|  | 90 | #endif | 
|  | 91 |  | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 92 | for( i = 0, k = 768; i < k; i++ ) | 
|  | 93 | { | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 94 | ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) ); | 
|  | 95 | if( ret != 0 ) | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 96 | { | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 97 | printf("failed!\n"); | 
|  | 98 | goto cleanup; | 
| Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 99 | } | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 100 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 101 | fwrite( buf, 1, sizeof( buf ), f ); | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 102 |  | 
|  | 103 | printf( "Generating 32Mb of data in file '%s'... %04.1f" \ | 
|  | 104 | "%% done\r", argv[1], (100 * (float) (i + 1)) / k ); | 
|  | 105 | fflush( stdout ); | 
|  | 106 | } | 
|  | 107 |  | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 108 | ret = 0; | 
|  | 109 |  | 
|  | 110 | cleanup: | 
| Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 111 | printf("\n"); | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 112 |  | 
|  | 113 | fclose( f ); | 
| Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 114 | entropy_free( &entropy ); | 
| Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 115 |  | 
|  | 116 | return( ret ); | 
| Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 117 | } | 
| Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 118 | #endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */ |