2 * \brief Generate random data into a file
4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.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 */
40 #if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
41 #include "mbedtls/havege.h"
47 #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
50 mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
55 #if defined(MBEDTLS_CHECK_PARAMS)
56 #include "mbedtls/platform_util.h"
57 void mbedtls_param_failed( const char *failure_condition,
61 mbedtls_printf( "%s:%i: Input param failed - %s\n",
62 file, line, failure_condition );
63 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
67 int main( int argc, char *argv[] )
72 int exit_code = MBEDTLS_EXIT_FAILURE;
73 mbedtls_havege_state hs;
74 unsigned char buf[1024];
78 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
82 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
84 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
88 mbedtls_havege_init( &hs );
92 for( i = 0, k = 768; i < k; i++ )
94 if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
96 mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
101 fwrite( buf, sizeof( buf ), 1, f );
103 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
104 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
108 if( t == time( NULL ) )
111 mbedtls_printf(" \n ");
113 exit_code = MBEDTLS_EXIT_SUCCESS;
116 mbedtls_havege_free( &hs );
120 #endif /* MBEDTLS_HAVEGE_C */