1 /**
2  *  \brief Use and generate random data into a file via the CTR_DBRG based on AES
3  *
4  *  Copyright The Mbed TLS Contributors
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 
20 #if !defined(MBEDTLS_CONFIG_FILE)
21 #include "mbedtls/config.h"
22 #else
23 #include MBEDTLS_CONFIG_FILE
24 #endif
25 
26 #if defined(MBEDTLS_PLATFORM_C)
27 #include "mbedtls/platform.h"
28 #else
29 #include <stdio.h>
30 #include <stdlib.h>
31 #define mbedtls_fprintf         fprintf
32 #define mbedtls_printf          printf
33 #define mbedtls_exit            exit
34 #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
35 #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
36 #endif /* MBEDTLS_PLATFORM_C */
37 
38 #if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
39  defined(MBEDTLS_FS_IO)
40 #include "mbedtls/entropy.h"
41 #include "mbedtls/ctr_drbg.h"
42 
43 #include <stdio.h>
44 #endif
45 
46 #if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
47  !defined(MBEDTLS_FS_IO)
main(void)48 int main( void )
49 {
50     mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
51     mbedtls_exit( 0 );
52 }
53 #else
54 
55 
main(int argc,char * argv[])56 int main( int argc, char *argv[] )
57 {
58     FILE *f;
59     int i, k, ret = 1;
60     int exit_code = MBEDTLS_EXIT_FAILURE;
61     mbedtls_ctr_drbg_context ctr_drbg;
62     mbedtls_entropy_context entropy;
63     unsigned char buf[1024];
64 
65     mbedtls_ctr_drbg_init( &ctr_drbg );
66 
67     if( argc < 2 )
68     {
69         mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
70         mbedtls_exit( exit_code );
71     }
72 
73     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
74     {
75         mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
76         mbedtls_exit( exit_code );
77     }
78 
79     mbedtls_entropy_init( &entropy );
80     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
81     if( ret != 0 )
82     {
83         mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
84         goto cleanup;
85     }
86     mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
87 
88 #if defined(MBEDTLS_FS_IO)
89     ret = mbedtls_ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
90 
91     if( ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR )
92     {
93         mbedtls_printf( "Failed to open seedfile. Generating one.\n" );
94         ret = mbedtls_ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
95         if( ret != 0 )
96         {
97             mbedtls_printf( "failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret );
98             goto cleanup;
99         }
100     }
101     else if( ret != 0 )
102     {
103         mbedtls_printf( "failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret );
104         goto cleanup;
105     }
106 #endif
107 
108     for( i = 0, k = 768; i < k; i++ )
109     {
110         ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
111         if( ret != 0 )
112         {
113             mbedtls_printf("failed!\n");
114             goto cleanup;
115         }
116 
117         fwrite( buf, 1, sizeof( buf ), f );
118 
119         mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
120                 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
121         fflush( stdout );
122     }
123 
124     exit_code = MBEDTLS_EXIT_SUCCESS;
125 
126 cleanup:
127     mbedtls_printf("\n");
128 
129     fclose( f );
130     mbedtls_ctr_drbg_free( &ctr_drbg );
131     mbedtls_entropy_free( &entropy );
132 
133     mbedtls_exit( exit_code );
134 }
135 #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */
136