1 /**
2  *  \brief Use and generate multiple entropies calls 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 #include "mbedtls/build_info.h"
21 
22 #if defined(MBEDTLS_PLATFORM_C)
23 #include "mbedtls/platform.h"
24 #else
25 #include <stdio.h>
26 #include <stdlib.h>
27 #define mbedtls_fprintf         fprintf
28 #define mbedtls_printf          printf
29 #define mbedtls_exit            exit
30 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
31 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
32 #endif /* MBEDTLS_PLATFORM_C */
33 
34 #if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
35 #include "mbedtls/entropy.h"
36 
37 #include <stdio.h>
38 #endif
39 
40 #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
main(void)41 int main( void )
42 {
43     mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
44     mbedtls_exit( 0 );
45 }
46 #else
47 
48 
main(int argc,char * argv[])49 int main( int argc, char *argv[] )
50 {
51     FILE *f;
52     int i, k, ret = 1;
53     int exit_code = MBEDTLS_EXIT_FAILURE;
54     mbedtls_entropy_context entropy;
55     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
56 
57     if( argc < 2 )
58     {
59         mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
60         mbedtls_exit( exit_code );
61     }
62 
63     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
64     {
65         mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
66         mbedtls_exit( exit_code );
67     }
68 
69     mbedtls_entropy_init( &entropy );
70 
71     for( i = 0, k = 768; i < k; i++ )
72     {
73         ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
74         if( ret != 0 )
75         {
76             mbedtls_printf( "  failed\n  !  mbedtls_entropy_func returned -%04X\n",
77                             (unsigned int) ret );
78             goto cleanup;
79         }
80 
81         fwrite( buf, 1, sizeof( buf ), f );
82 
83         mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
84                 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
85         fflush( stdout );
86     }
87 
88     exit_code = MBEDTLS_EXIT_SUCCESS;
89 
90 cleanup:
91     mbedtls_printf( "\n" );
92 
93     fclose( f );
94     mbedtls_entropy_free( &entropy );
95 
96     mbedtls_exit( exit_code );
97 }
98 #endif /* MBEDTLS_ENTROPY_C */
99