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