blob: 4df60e93aeb5b24c837a560496133c9f21d852ec [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
Paul Bakker6083fd22011-12-03 21:45:14 +00002 * \brief Use and generate multiple entropies calls into a file
Paul Bakkerfc36d162011-01-27 16:50:02 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerfc36d162011-01-27 16:50:02 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5690efc2011-05-26 13:16:06 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000018
Paul Bakkerfc36d162011-01-27 16:50:02 +000019#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000020#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010026 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000027}
28#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010029
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010030
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031int main(int argc, char *argv[])
Paul Bakkerfc36d162011-01-27 16:50:02 +000032{
33 FILE *f;
Andres Amaya Garcia55a0d562018-04-29 20:40:36 +010034 int i, k, ret = 1;
35 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036 mbedtls_entropy_context entropy;
37 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000038
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010039 if (argc < 2) {
40 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
41 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000042 }
43
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 if ((f = fopen(argv[1], "wb+")) == NULL) {
45 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
46 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000047 }
48
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 mbedtls_entropy_init(&entropy);
Paul Bakkerfc36d162011-01-27 16:50:02 +000050
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 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);
Paul Bakker6083fd22011-12-03 21:45:14 +000056 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000057 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000058
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010059 fwrite(buf, 1, sizeof(buf), f);
Paul Bakkerfc36d162011-01-27 16:50:02 +000060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061 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);
Paul Bakkerfc36d162011-01-27 16:50:02 +000067 }
68
Andres Amaya Garcia55a0d562018-04-29 20:40:36 +010069 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker6083fd22011-12-03 21:45:14 +000070
71cleanup:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 mbedtls_printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +000073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 fclose(f);
75 mbedtls_entropy_free(&entropy);
Paul Bakker6083fd22011-12-03 21:45:14 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000078}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079#endif /* MBEDTLS_ENTROPY_C */