1 /**
2 * \brief Use and generate random data into a file via the CTR_DBRG based on AES
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_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
41 defined(MBEDTLS_FS_IO)
42 #include "mbedtls/entropy.h"
43 #include "mbedtls/ctr_drbg.h"
44
45 #include <stdio.h>
46 #endif
47
48 #if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
49 !defined(MBEDTLS_FS_IO)
main(void)50 int main( void )
51 {
52 mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
53 return( 0 );
54 }
55 #else
56
57 #if defined(MBEDTLS_CHECK_PARAMS)
58 #include "mbedtls/platform_util.h"
mbedtls_param_failed(const char * failure_condition,const char * file,int line)59 void mbedtls_param_failed( const char *failure_condition,
60 const char *file,
61 int line )
62 {
63 mbedtls_printf( "%s:%i: Input param failed - %s\n",
64 file, line, failure_condition );
65 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
66 }
67 #endif
68
main(int argc,char * argv[])69 int main( int argc, char *argv[] )
70 {
71 FILE *f;
72 int i, k, ret = 1;
73 int exit_code = MBEDTLS_EXIT_FAILURE;
74 mbedtls_ctr_drbg_context ctr_drbg;
75 mbedtls_entropy_context entropy;
76 unsigned char buf[1024];
77
78 mbedtls_ctr_drbg_init( &ctr_drbg );
79
80 if( argc < 2 )
81 {
82 mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
83 return( exit_code );
84 }
85
86 if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
87 {
88 mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
89 return( exit_code );
90 }
91
92 mbedtls_entropy_init( &entropy );
93 ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
94 if( ret != 0 )
95 {
96 mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
97 goto cleanup;
98 }
99 mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
100
101 #if defined(MBEDTLS_FS_IO)
102 ret = mbedtls_ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
103
104 if( ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR )
105 {
106 mbedtls_printf( "Failed to open seedfile. Generating one.\n" );
107 ret = mbedtls_ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
108 if( ret != 0 )
109 {
110 mbedtls_printf( "failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret );
111 goto cleanup;
112 }
113 }
114 else if( ret != 0 )
115 {
116 mbedtls_printf( "failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret );
117 goto cleanup;
118 }
119 #endif
120
121 for( i = 0, k = 768; i < k; i++ )
122 {
123 ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
124 if( ret != 0 )
125 {
126 mbedtls_printf("failed!\n");
127 goto cleanup;
128 }
129
130 fwrite( buf, 1, sizeof( buf ), f );
131
132 mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
133 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
134 fflush( stdout );
135 }
136
137 exit_code = MBEDTLS_EXIT_SUCCESS;
138
139 cleanup:
140 mbedtls_printf("\n");
141
142 fclose( f );
143 mbedtls_ctr_drbg_free( &ctr_drbg );
144 mbedtls_entropy_free( &entropy );
145
146 return( exit_code );
147 }
148 #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */
149