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