blob: 0eecf0ad492f83ddf2173f3a8e6ff2ce3da59706 [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
Paul Bakker6083fd22011-12-03 21:45:14 +00002 * \brief Use and generate random data into a file via the CTR_DBRG based on AES
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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010013 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/entropy.h"
15#include "mbedtls/ctr_drbg.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_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
Gilles Peskine449bd832023-01-11 14:50:10 +010021 !defined(MBEDTLS_FS_IO)
22int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000023{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024 mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010025 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000026}
27#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000028
Simon Butcher63cb97e2018-12-06 17:43:31 +000029
Gilles Peskine449bd832023-01-11 14:50:10 +010030int main(int argc, char *argv[])
Paul Bakkerfc36d162011-01-27 16:50:02 +000031{
32 FILE *f;
Andres Amaya Garcia73d4a5f2018-04-29 20:46:55 +010033 int i, k, ret = 1;
34 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_ctr_drbg_context ctr_drbg;
36 mbedtls_entropy_context entropy;
Paul Bakkerfc36d162011-01-27 16:50:02 +000037 unsigned char buf[1024];
38
Gilles Peskine449bd832023-01-11 14:50:10 +010039 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020040
Gilles Peskine449bd832023-01-11 14:50:10 +010041 if (argc < 2) {
42 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
43 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000044 }
45
Gilles Peskine449bd832023-01-11 14:50:10 +010046 if ((f = fopen(argv[1], "wb+")) == NULL) {
47 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
48 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000049 }
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051 mbedtls_entropy_init(&entropy);
52 ret = mbedtls_ctr_drbg_seed(&ctr_drbg,
53 mbedtls_entropy_func,
54 &entropy,
55 (const unsigned char *) "RANDOM_GEN",
56 10);
57 if (ret != 0) {
58 mbedtls_printf("failed in mbedtls_ctr_drbg_seed: %d\n", ret);
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000059 goto cleanup;
60 }
Gilles Peskine449bd832023-01-11 14:50:10 +010061 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF);
Paul Bakkerfc36d162011-01-27 16:50:02 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010064 ret = mbedtls_ctr_drbg_update_seed_file(&ctr_drbg, "seedfile");
Paul Bakkerfc754a92011-12-05 13:23:51 +000065
Gilles Peskine449bd832023-01-11 14:50:10 +010066 if (ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) {
67 mbedtls_printf("Failed to open seedfile. Generating one.\n");
68 ret = mbedtls_ctr_drbg_write_seed_file(&ctr_drbg, "seedfile");
69 if (ret != 0) {
70 mbedtls_printf("failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret);
Paul Bakkerfc754a92011-12-05 13:23:51 +000071 goto cleanup;
72 }
Gilles Peskine449bd832023-01-11 14:50:10 +010073 } else if (ret != 0) {
74 mbedtls_printf("failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret);
Paul Bakkerfc754a92011-12-05 13:23:51 +000075 goto cleanup;
76 }
77#endif
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 for (i = 0, k = 768; i < k; i++) {
80 ret = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sizeof(buf));
81 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_printf("failed!\n");
Paul Bakker6083fd22011-12-03 21:45:14 +000083 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000084 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 fwrite(buf, 1, sizeof(buf), f);
Paul Bakkerfc36d162011-01-27 16:50:02 +000087
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
89 "%% done\r",
90 (long) (sizeof(buf) * k / 1024),
91 argv[1],
92 (100 * (float) (i + 1)) / k);
93 fflush(stdout);
Paul Bakkerfc36d162011-01-27 16:50:02 +000094 }
95
Andres Amaya Garcia73d4a5f2018-04-29 20:46:55 +010096 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker6083fd22011-12-03 21:45:14 +000097
98cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +0000100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 fclose(f);
102 mbedtls_ctr_drbg_free(&ctr_drbg);
103 mbedtls_entropy_free(&entropy);
Paul Bakker6083fd22011-12-03 21:45:14 +0000104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +0000106}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107#endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */