blob: 5d93a49cbd2eb8f03f25b87c3bec70c2e42eda7c [file] [log] [blame]
Paul Bakkerfc36d162011-01-27 16:50:02 +00001/**
2 * \brief Generate random data into a file
3 *
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_HAVEGE_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000017#include "mbedtls/havege.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#include <time.h>
21#endif
Paul Bakkerfc36d162011-01-27 16:50:02 +000022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010024int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000025{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010027 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000028}
29#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010030
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010031
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010032int main(int argc, char *argv[])
Paul Bakkerfc36d162011-01-27 16:50:02 +000033{
34 FILE *f;
35 time_t t;
Andres Amaya Garcia28abd8e2018-04-30 22:09:18 +010036 int i, k, ret = 1;
37 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038 mbedtls_havege_state hs;
Paul Bakkerfc36d162011-01-27 16:50:02 +000039 unsigned char buf[1024];
40
Gilles Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +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 Peskine1b6c09a2023-01-11 14:52:35 +010051 mbedtls_havege_init(&hs);
Paul Bakkerfc36d162011-01-27 16:50:02 +000052
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053 t = time(NULL);
Paul Bakkerfc36d162011-01-27 16:50:02 +000054
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010055 for (i = 0, k = 768; i < k; i++) {
56 if ((ret = mbedtls_havege_random(&hs, buf, sizeof(buf))) != 0) {
57 mbedtls_printf(" failed\n ! mbedtls_havege_random returned -0x%04X",
58 (unsigned int) -ret);
Paul Bakkera317a982014-06-18 16:44:11 +020059 goto exit;
Paul Bakkera3d195c2011-11-27 21:07:34 +000060 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 fwrite(buf, sizeof(buf), 1, f);
Paul Bakkerfc36d162011-01-27 16:50:02 +000063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
65 "%% done\r",
66 (long) (sizeof(buf) * k / 1024),
67 argv[1],
68 (100 * (float) (i + 1)) / k);
69 fflush(stdout);
Paul Bakkerfc36d162011-01-27 16:50:02 +000070 }
71
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 if (t == time(NULL)) {
Paul Bakkerfc36d162011-01-27 16:50:02 +000073 t--;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 }
Paul Bakkerfc36d162011-01-27 16:50:02 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_printf(" \n ");
Paul Bakkerfc754a92011-12-05 13:23:51 +000077
Andres Amaya Garcia28abd8e2018-04-30 22:09:18 +010078 exit_code = MBEDTLS_EXIT_SUCCESS;
79
Paul Bakkera317a982014-06-18 16:44:11 +020080exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 mbedtls_havege_free(&hs);
82 fclose(f);
83 mbedtls_exit(exit_code);
Paul Bakkerfc36d162011-01-27 16:50:02 +000084}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#endif /* MBEDTLS_HAVEGE_C */