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 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 7 | * |
| 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 Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 24 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 25 | #include "polarssl/entropy.h" |
| 26 | #include "polarssl/ctr_drbg.h" |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 27 | |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 30 | #if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 31 | int main( int argc, char *argv[] ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 32 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 33 | ((void) argc); |
| 34 | ((void) argv); |
| 35 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 36 | printf("POLARSSL_CTR_DRBG_C or POLARSSL_ENTROPY_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 37 | return( 0 ); |
| 38 | } |
| 39 | #else |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 40 | int main( int argc, char *argv[] ) |
| 41 | { |
| 42 | FILE *f; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 43 | int i, k, ret; |
| 44 | ctr_drbg_context ctr_drbg; |
| 45 | entropy_context entropy; |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 46 | 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 Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 60 | entropy_init( &entropy ); |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 61 | 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] | 62 | if( ret != 0 ) |
| 63 | { |
| 64 | printf( "failed in ctr_drbg_init: %d\n", ret ); |
| 65 | goto cleanup; |
| 66 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 67 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 68 | |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 69 | #if defined(POLARSSL_FS_IO) |
| 70 | ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" ); |
| 71 | |
Paul Bakker | 3f9b650 | 2011-12-15 19:50:22 +0000 | [diff] [blame] | 72 | if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR ) |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 73 | { |
Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 74 | printf( "Failed to open seedfile. Generating one.\n" ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 75 | ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" ); |
| 76 | if( ret != 0 ) |
| 77 | { |
Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 78 | printf( "failed in ctr_drbg_write_seed_file: %d\n", ret ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 79 | goto cleanup; |
| 80 | } |
| 81 | } |
| 82 | else if( ret != 0 ) |
| 83 | { |
Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 84 | printf( "failed in ctr_drbg_update_seed_file: %d\n", ret ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 85 | goto cleanup; |
| 86 | } |
| 87 | #endif |
| 88 | |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 89 | for( i = 0, k = 768; i < k; i++ ) |
| 90 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 91 | ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) ); |
| 92 | if( ret != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 93 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 94 | printf("failed!\n"); |
| 95 | goto cleanup; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 96 | } |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 97 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 98 | fwrite( buf, 1, sizeof( buf ), f ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 99 | |
| 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 Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 105 | ret = 0; |
| 106 | |
| 107 | cleanup: |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 108 | printf("\n"); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 109 | |
| 110 | fclose( f ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 111 | |
| 112 | return( ret ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 113 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 114 | #endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */ |