blob: 00f869186e7a8fd43ce4fe51871ed46c6be99f0b [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 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_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010017 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/entropy.h"
19#include "mbedtls/ctr_drbg.h"
Paul Bakker5690efc2011-05-26 13:16:06 +000020
Paul Bakkerfc36d162011-01-27 16:50:02 +000021#include <stdio.h>
Rich Evans18b78c72015-02-11 14:06:19 +000022#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010025 !defined(MBEDTLS_FS_IO)
26int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000027{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010029 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000030}
31#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000032
Simon Butcher63cb97e2018-12-06 17:43:31 +000033
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034int main(int argc, char *argv[])
Paul Bakkerfc36d162011-01-27 16:50:02 +000035{
36 FILE *f;
Andres Amaya Garcia73d4a5f2018-04-29 20:46:55 +010037 int i, k, ret = 1;
38 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039 mbedtls_ctr_drbg_context ctr_drbg;
40 mbedtls_entropy_context entropy;
Paul Bakkerfc36d162011-01-27 16:50:02 +000041 unsigned char buf[1024];
42
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010043 mbedtls_ctr_drbg_init(&ctr_drbg);
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020044
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010045 if (argc < 2) {
46 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
47 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000048 }
49
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010050 if ((f = fopen(argv[1], "wb+")) == NULL) {
51 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
52 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000053 }
54
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 mbedtls_entropy_init(&entropy);
56 ret = mbedtls_ctr_drbg_seed(&ctr_drbg,
57 mbedtls_entropy_func,
58 &entropy,
59 (const unsigned char *) "RANDOM_GEN",
60 10);
61 if (ret != 0) {
62 mbedtls_printf("failed in mbedtls_ctr_drbg_seed: %d\n", ret);
Paul Bakkerfb3a83f2011-12-15 20:05:53 +000063 goto cleanup;
64 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010065 mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF);
Paul Bakkerfc36d162011-01-27 16:50:02 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010068 ret = mbedtls_ctr_drbg_update_seed_file(&ctr_drbg, "seedfile");
Paul Bakkerfc754a92011-12-05 13:23:51 +000069
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010070 if (ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) {
71 mbedtls_printf("Failed to open seedfile. Generating one.\n");
72 ret = mbedtls_ctr_drbg_write_seed_file(&ctr_drbg, "seedfile");
73 if (ret != 0) {
74 mbedtls_printf("failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret);
Paul Bakkerfc754a92011-12-05 13:23:51 +000075 goto cleanup;
76 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 } else if (ret != 0) {
78 mbedtls_printf("failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret);
Paul Bakkerfc754a92011-12-05 13:23:51 +000079 goto cleanup;
80 }
81#endif
82
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 for (i = 0, k = 768; i < k; i++) {
84 ret = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sizeof(buf));
85 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_printf("failed!\n");
Paul Bakker6083fd22011-12-03 21:45:14 +000087 goto cleanup;
Paul Bakkera3d195c2011-11-27 21:07:34 +000088 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000089
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 fwrite(buf, 1, sizeof(buf), f);
Paul Bakkerfc36d162011-01-27 16:50:02 +000091
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
93 "%% done\r",
94 (long) (sizeof(buf) * k / 1024),
95 argv[1],
96 (100 * (float) (i + 1)) / k);
97 fflush(stdout);
Paul Bakkerfc36d162011-01-27 16:50:02 +000098 }
99
Andres Amaya Garcia73d4a5f2018-04-29 20:46:55 +0100100 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker6083fd22011-12-03 21:45:14 +0000101
102cleanup:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_printf("\n");
Paul Bakkerfc36d162011-01-27 16:50:02 +0000104
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 fclose(f);
106 mbedtls_ctr_drbg_free(&ctr_drbg);
107 mbedtls_entropy_free(&entropy);
Paul Bakker6083fd22011-12-03 21:45:14 +0000108
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +0000110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111#endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */