blob: eb85b62690430afe0bc97098e126d4e7f1a83c25 [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 Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerfc36d162011-01-27 16:50:02 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/entropy.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000016
Paul Bakkerfc36d162011-01-27 16:50:02 +000017#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000018#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000019
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010021int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000022{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010024 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000025}
26#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010027
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010028
Gilles Peskine449bd832023-01-11 14:50:10 +010029int main(int argc, char *argv[])
Paul Bakkerfc36d162011-01-27 16:50:02 +000030{
31 FILE *f;
Andres Amaya Garcia55a0d562018-04-29 20:40:36 +010032 int i, k, ret = 1;
33 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_entropy_context entropy;
35 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
Paul Bakkerfc36d162011-01-27 16:50:02 +000036
Gilles Peskine449bd832023-01-11 14:50:10 +010037 if (argc < 2) {
38 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
39 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000040 }
41
Gilles Peskine449bd832023-01-11 14:50:10 +010042 if ((f = fopen(argv[1], "wb+")) == NULL) {
43 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
44 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000045 }
46
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_entropy_init(&entropy);
Paul Bakkerfc36d162011-01-27 16:50:02 +000048
Gilles Peskine449bd832023-01-11 14:50:10 +010049 for (i = 0, k = 768; i < k; i++) {
50 ret = mbedtls_entropy_func(&entropy, buf, sizeof(buf));
51 if (ret != 0) {
52 mbedtls_printf(" failed\n ! mbedtls_entropy_func returned -%04X\n",
53 (unsigned int) ret);
Paul Bakker6083fd22011-12-03 21:45:14 +000054 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000055 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 fwrite(buf, 1, sizeof(buf), f);
Paul Bakkerfc36d162011-01-27 16:50:02 +000058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
60 "%% done\r",
61 (long) (sizeof(buf) * k / 1024),
62 argv[1],
63 (100 * (float) (i + 1)) / k);
64 fflush(stdout);
Paul Bakkerfc36d162011-01-27 16:50:02 +000065 }
66
Andres Amaya Garcia55a0d562018-04-29 20:40:36 +010067 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker6083fd22011-12-03 21:45:14 +000068
69cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +000071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 fclose(f);
73 mbedtls_entropy_free(&entropy);
Paul Bakker6083fd22011-12-03 21:45:14 +000074
Gilles Peskine449bd832023-01-11 14:50:10 +010075 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#endif /* MBEDTLS_ENTROPY_C */