1 /**
2 * \brief Use and generate multiple entropies calls 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
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
17 #include "mbedtls/entropy.h"
18
19 #include <stdio.h>
20 #endif
21
22 #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
main(void)23 int main(void)
24 {
25 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
26 mbedtls_exit(0);
27 }
28 #else
29
30
main(int argc,char * argv[])31 int main(int argc, char *argv[])
32 {
33 FILE *f;
34 int i, k, ret = 1;
35 int exit_code = MBEDTLS_EXIT_FAILURE;
36 mbedtls_entropy_context entropy;
37 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
38
39 if (argc < 2) {
40 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
41 mbedtls_exit(exit_code);
42 }
43
44 if ((f = fopen(argv[1], "wb+")) == NULL) {
45 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
46 mbedtls_exit(exit_code);
47 }
48
49 mbedtls_entropy_init(&entropy);
50
51 for (i = 0, k = 768; i < k; i++) {
52 ret = mbedtls_entropy_func(&entropy, buf, sizeof(buf));
53 if (ret != 0) {
54 mbedtls_printf(" failed\n ! mbedtls_entropy_func returned -%04X\n",
55 (unsigned int) ret);
56 goto cleanup;
57 }
58
59 fwrite(buf, 1, sizeof(buf), f);
60
61 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
62 "%% done\r",
63 (long) (sizeof(buf) * k / 1024),
64 argv[1],
65 (100 * (float) (i + 1)) / k);
66 fflush(stdout);
67 }
68
69 exit_code = MBEDTLS_EXIT_SUCCESS;
70
71 cleanup:
72 mbedtls_printf("\n");
73
74 fclose(f);
75 mbedtls_entropy_free(&entropy);
76
77 mbedtls_exit(exit_code);
78 }
79 #endif /* MBEDTLS_ENTROPY_C */
80