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 | a658a40 | 2015-01-23 09:45:19 +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 | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 37 | #if defined(POLARSSL_CTR_DRBG_C) && defined(POLARSSL_ENTROPY_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 | defined(POLARSSL_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 39 | #include "mbedtls/entropy.h" |
| 40 | #include "mbedtls/ctr_drbg.h" |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 41 | |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 42 | #include <stdio.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | #endif |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 44 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 45 | #if !defined(POLARSSL_CTR_DRBG_C) || !defined(POLARSSL_ENTROPY_C) || \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 46 | !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 47 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | { |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 49 | polarssl_printf("POLARSSL_CTR_DRBG_C and/or POLARSSL_ENTROPY_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 50 | return( 0 ); |
| 51 | } |
| 52 | #else |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 53 | int main( int argc, char *argv[] ) |
| 54 | { |
| 55 | FILE *f; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 56 | int i, k, ret; |
| 57 | ctr_drbg_context ctr_drbg; |
| 58 | entropy_context entropy; |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 59 | unsigned char buf[1024]; |
| 60 | |
| 61 | if( argc < 2 ) |
| 62 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 63 | polarssl_fprintf( stderr, "usage: %s <output filename>\n", argv[0] ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 64 | return( 1 ); |
| 65 | } |
| 66 | |
| 67 | if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) |
| 68 | { |
veggie | 64a5799 | 2015-01-26 10:35:25 -0500 | [diff] [blame] | 69 | polarssl_printf( "failed to open '%s' for writing.\n", argv[1] ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 70 | return( 1 ); |
| 71 | } |
| 72 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 73 | entropy_init( &entropy ); |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 74 | 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] | 75 | if( ret != 0 ) |
| 76 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 77 | polarssl_printf( "failed in ctr_drbg_init: %d\n", ret ); |
Paul Bakker | fb3a83f | 2011-12-15 20:05:53 +0000 | [diff] [blame] | 78 | goto cleanup; |
| 79 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 80 | ctr_drbg_set_prediction_resistance( &ctr_drbg, CTR_DRBG_PR_OFF ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 81 | |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 82 | #if defined(POLARSSL_FS_IO) |
| 83 | ret = ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" ); |
| 84 | |
Paul Bakker | 3f9b650 | 2011-12-15 19:50:22 +0000 | [diff] [blame] | 85 | if( ret == POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR ) |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 86 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 87 | polarssl_printf( "Failed to open seedfile. Generating one.\n" ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 88 | ret = ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" ); |
| 89 | if( ret != 0 ) |
| 90 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 91 | polarssl_printf( "failed in ctr_drbg_write_seed_file: %d\n", ret ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 92 | goto cleanup; |
| 93 | } |
| 94 | } |
| 95 | else if( ret != 0 ) |
| 96 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 97 | polarssl_printf( "failed in ctr_drbg_update_seed_file: %d\n", ret ); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 98 | goto cleanup; |
| 99 | } |
| 100 | #endif |
| 101 | |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 102 | for( i = 0, k = 768; i < k; i++ ) |
| 103 | { |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 104 | ret = ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) ); |
| 105 | if( ret != 0 ) |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 106 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 107 | polarssl_printf("failed!\n"); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 108 | goto cleanup; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 109 | } |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 110 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 111 | fwrite( buf, 1, sizeof( buf ), f ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 112 | |
veggie | 64a5799 | 2015-01-26 10:35:25 -0500 | [diff] [blame] | 113 | polarssl_printf( "Generating %ldkb of data in file '%s'... %04.1f" \ |
| 114 | "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 115 | fflush( stdout ); |
| 116 | } |
| 117 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 118 | ret = 0; |
| 119 | |
| 120 | cleanup: |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 121 | polarssl_printf("\n"); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 122 | |
| 123 | fclose( f ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 124 | ctr_drbg_free( &ctr_drbg ); |
Paul Bakker | 1ffefac | 2013-09-28 15:23:03 +0200 | [diff] [blame] | 125 | entropy_free( &entropy ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 126 | |
| 127 | return( ret ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 128 | } |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 129 | #endif /* POLARSSL_CTR_DRBG_C && POLARSSL_ENTROPY_C */ |