1 /**
2 * \brief Generate random data into a file
3 *
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #include <stdlib.h>
33 #define mbedtls_fprintf fprintf
34 #define mbedtls_printf printf
35 #define mbedtls_exit exit
36 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
37 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
38 #endif /* MBEDTLS_PLATFORM_C */
39
40 #if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/havege.h"
42
43 #include <stdio.h>
44 #include <time.h>
45 #endif
46
47 #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
main(void)48 int main( void )
49 {
50 mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
51 return( 0 );
52 }
53 #else
54
55 #if defined(MBEDTLS_CHECK_PARAMS)
56 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)57 void mbedtls_param_failed( const char *failure_condition,
58 const char *file,
59 int line )
60 {
61 mbedtls_printf( "%s:%i: Input param failed - %s\n",
62 file, line, failure_condition );
63 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
64 }
65 #endif
66
main(int argc,char * argv[])67 int main( int argc, char *argv[] )
68 {
69 FILE *f;
70 time_t t;
71 int i, k, ret = 1;
72 int exit_code = MBEDTLS_EXIT_FAILURE;
73 mbedtls_havege_state hs;
74 unsigned char buf[1024];
75
76 if( argc < 2 )
77 {
78 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
79 return( exit_code );
80 }
81
82 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
83 {
84 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
85 return( exit_code );
86 }
87
88 mbedtls_havege_init( &hs );
89
90 t = time( NULL );
91
92 for( i = 0, k = 768; i < k; i++ )
93 {
94 if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
95 {
96 mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
97 -ret );
98 goto exit;
99 }
100
101 fwrite( buf, sizeof( buf ), 1, f );
102
103 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
104 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
105 fflush( stdout );
106 }
107
108 if( t == time( NULL ) )
109 t--;
110
111 mbedtls_printf(" \n ");
112
113 exit_code = MBEDTLS_EXIT_SUCCESS;
114
115 exit:
116 mbedtls_havege_free( &hs );
117 fclose( f );
118 return( exit_code );
119 }
120 #endif /* MBEDTLS_HAVEGE_C */
121