1 /**
2  *  \brief Generate random data into a file
3  *
4  *  Copyright The Mbed TLS Contributors
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 
20 #if !defined(MBEDTLS_CONFIG_FILE)
21 #include "mbedtls/config.h"
22 #else
23 #include MBEDTLS_CONFIG_FILE
24 #endif
25 
26 #if defined(MBEDTLS_PLATFORM_C)
27 #include "mbedtls/platform.h"
28 #else
29 #include <stdio.h>
30 #include <stdlib.h>
31 #define mbedtls_fprintf         fprintf
32 #define mbedtls_printf          printf
33 #define mbedtls_exit            exit
34 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
35 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
36 #endif /* MBEDTLS_PLATFORM_C */
37 
38 #if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
39 #include "mbedtls/havege.h"
40 
41 #include <stdio.h>
42 #include <time.h>
43 #endif
44 
45 #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
main(void)46 int main( void )
47 {
48     mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
49     mbedtls_exit( 0 );
50 }
51 #else
52 
53 
main(int argc,char * argv[])54 int main( int argc, char *argv[] )
55 {
56     FILE *f;
57     time_t t;
58     int i, k, ret = 1;
59     int exit_code = MBEDTLS_EXIT_FAILURE;
60     mbedtls_havege_state hs;
61     unsigned char buf[1024];
62 
63     if( argc < 2 )
64     {
65         mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
66         mbedtls_exit( exit_code );
67     }
68 
69     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
70     {
71         mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
72         mbedtls_exit( exit_code );
73     }
74 
75     mbedtls_havege_init( &hs );
76 
77     t = time( NULL );
78 
79     for( i = 0, k = 768; i < k; i++ )
80     {
81         if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
82         {
83             mbedtls_printf( " failed\n  !  mbedtls_havege_random returned -0x%04X",
84                             ( unsigned int ) -ret );
85             goto exit;
86         }
87 
88         fwrite( buf, sizeof( buf ), 1, f );
89 
90         mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
91                 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
92         fflush( stdout );
93     }
94 
95     if( t == time( NULL ) )
96         t--;
97 
98     mbedtls_printf(" \n ");
99 
100     exit_code = MBEDTLS_EXIT_SUCCESS;
101 
102 exit:
103     mbedtls_havege_free( &hs );
104     fclose( f );
105     mbedtls_exit( exit_code );
106 }
107 #endif /* MBEDTLS_HAVEGE_C */
108