Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \brief Generate random data into a file |
| 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) |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 24 | #include "polarssl/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) |
| 30 | #include "polarssl/platform.h" |
| 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 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 37 | #if defined(POLARSSL_HAVEGE_C) && defined(POLARSSL_FS_IO) |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 38 | #include "polarssl/havege.h" |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 39 | |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 40 | #include <stdio.h> |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 41 | #include <time.h> |
| 42 | #endif |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 43 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 44 | #if !defined(POLARSSL_HAVEGE_C) || !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 45 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 46 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 47 | polarssl_printf("POLARSSL_HAVEGE_C not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | return( 0 ); |
| 49 | } |
| 50 | #else |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 51 | int main( int argc, char *argv[] ) |
| 52 | { |
| 53 | FILE *f; |
| 54 | time_t t; |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 55 | int i, k, ret = 0; |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 56 | havege_state hs; |
| 57 | unsigned char buf[1024]; |
| 58 | |
| 59 | if( argc < 2 ) |
| 60 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 61 | polarssl_fprintf( stderr, "usage: %s <output filename>\n", argv[0] ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 62 | return( 1 ); |
| 63 | } |
| 64 | |
| 65 | if( ( f = fopen( argv[1], "wb+" ) ) == NULL ) |
| 66 | { |
veggie | 64a5799 | 2015-01-26 10:35:25 -0500 | [diff] [blame] | 67 | polarssl_printf( "failed to open '%s' for writing.\n", argv[1] ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 68 | return( 1 ); |
| 69 | } |
| 70 | |
| 71 | havege_init( &hs ); |
| 72 | |
| 73 | t = time( NULL ); |
| 74 | |
| 75 | for( i = 0, k = 768; i < k; i++ ) |
| 76 | { |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 77 | if( havege_random( &hs, buf, sizeof( buf ) ) != 0 ) |
| 78 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 79 | polarssl_printf( "Failed to get random from source.\n" ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 80 | |
| 81 | ret = 1; |
| 82 | goto exit; |
Paul Bakker | a3d195c | 2011-11-27 21:07:34 +0000 | [diff] [blame] | 83 | } |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 84 | |
| 85 | fwrite( buf, sizeof( buf ), 1, f ); |
| 86 | |
veggie | 64a5799 | 2015-01-26 10:35:25 -0500 | [diff] [blame] | 87 | polarssl_printf( "Generating %ldkb of data in file '%s'... %04.1f" \ |
| 88 | "%% 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] | 89 | fflush( stdout ); |
| 90 | } |
| 91 | |
| 92 | if( t == time( NULL ) ) |
| 93 | t--; |
| 94 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 95 | polarssl_printf(" \n "); |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 96 | |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 97 | exit: |
| 98 | havege_free( &hs ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 99 | fclose( f ); |
Paul Bakker | a317a98 | 2014-06-18 16:44:11 +0200 | [diff] [blame] | 100 | return( ret ); |
Paul Bakker | fc36d16 | 2011-01-27 16:50:02 +0000 | [diff] [blame] | 101 | } |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 102 | #endif /* POLARSSL_HAVEGE_C */ |