1 /**
2 * \brief Generate random data into a file
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47 #if !defined(MBEDTLS_CONFIG_FILE)
48 #include "mbedtls/config.h"
49 #else
50 #include MBEDTLS_CONFIG_FILE
51 #endif
52
53 #if defined(MBEDTLS_PLATFORM_C)
54 #include "mbedtls/platform.h"
55 #else
56 #include <stdio.h>
57 #include <stdlib.h>
58 #define mbedtls_fprintf fprintf
59 #define mbedtls_printf printf
60 #define mbedtls_exit exit
61 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
62 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
63 #endif /* MBEDTLS_PLATFORM_C */
64
65 #if defined(MBEDTLS_HAVEGE_C) && defined(MBEDTLS_FS_IO)
66 #include "mbedtls/havege.h"
67
68 #include <stdio.h>
69 #include <time.h>
70 #endif
71
72 #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
main(void)73 int main( void )
74 {
75 mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
76 mbedtls_exit( 0 );
77 }
78 #else
79
80
main(int argc,char * argv[])81 int main( int argc, char *argv[] )
82 {
83 FILE *f;
84 time_t t;
85 int i, k, ret = 1;
86 int exit_code = MBEDTLS_EXIT_FAILURE;
87 mbedtls_havege_state hs;
88 unsigned char buf[1024];
89
90 if( argc < 2 )
91 {
92 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
93 mbedtls_exit( exit_code );
94 }
95
96 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
97 {
98 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
99 mbedtls_exit( exit_code );
100 }
101
102 mbedtls_havege_init( &hs );
103
104 t = time( NULL );
105
106 for( i = 0, k = 768; i < k; i++ )
107 {
108 if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
109 {
110 mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
111 -ret );
112 goto exit;
113 }
114
115 fwrite( buf, sizeof( buf ), 1, f );
116
117 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
118 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
119 fflush( stdout );
120 }
121
122 if( t == time( NULL ) )
123 t--;
124
125 mbedtls_printf(" \n ");
126
127 exit_code = MBEDTLS_EXIT_SUCCESS;
128
129 exit:
130 mbedtls_havege_free( &hs );
131 fclose( f );
132 mbedtls_exit( exit_code );
133 }
134 #endif /* MBEDTLS_HAVEGE_C */
135