1 /**
2 * \brief Use and generate multiple entropies calls 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_ENTROPY_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/entropy.h"
42
43 #include <stdio.h>
44 #endif
45
46 #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
main(void)47 int main( void )
48 {
49 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
50 return( 0 );
51 }
52 #else
53
54 #if defined(MBEDTLS_CHECK_PARAMS)
55 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)56 void mbedtls_param_failed( const char *failure_condition,
57 const char *file,
58 int line )
59 {
60 mbedtls_printf( "%s:%i: Input param failed - %s\n",
61 file, line, failure_condition );
62 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
63 }
64 #endif
65
main(int argc,char * argv[])66 int main( int argc, char *argv[] )
67 {
68 FILE *f;
69 int i, k, ret = 1;
70 int exit_code = MBEDTLS_EXIT_FAILURE;
71 mbedtls_entropy_context entropy;
72 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
73
74 if( argc < 2 )
75 {
76 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
77 return( exit_code );
78 }
79
80 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
81 {
82 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
83 return( exit_code );
84 }
85
86 mbedtls_entropy_init( &entropy );
87
88 for( i = 0, k = 768; i < k; i++ )
89 {
90 ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
91 if( ret != 0 )
92 {
93 mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n",
94 ret );
95 goto cleanup;
96 }
97
98 fwrite( buf, 1, sizeof( buf ), f );
99
100 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
101 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
102 fflush( stdout );
103 }
104
105 exit_code = MBEDTLS_EXIT_SUCCESS;
106
107 cleanup:
108 mbedtls_printf( "\n" );
109
110 fclose( f );
111 mbedtls_entropy_free( &entropy );
112
113 return( exit_code );
114 }
115 #endif /* MBEDTLS_ENTROPY_C */
116