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