1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * \brief Use and generate random data into a file via the CTR_DBRG based on AES 3a8e1175bSopenharmony_ci * 4a8e1175bSopenharmony_ci * Copyright The Mbed TLS Contributors 5a8e1175bSopenharmony_ci * SPDX-License-Identifier: Apache-2.0 6a8e1175bSopenharmony_ci * 7a8e1175bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); you may 8a8e1175bSopenharmony_ci * not use this file except in compliance with the License. 9a8e1175bSopenharmony_ci * You may obtain a copy of the License at 10a8e1175bSopenharmony_ci * 11a8e1175bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 12a8e1175bSopenharmony_ci * 13a8e1175bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 14a8e1175bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15a8e1175bSopenharmony_ci * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16a8e1175bSopenharmony_ci * See the License for the specific language governing permissions and 17a8e1175bSopenharmony_ci * limitations under the License. 18a8e1175bSopenharmony_ci */ 19a8e1175bSopenharmony_ci 20a8e1175bSopenharmony_ci#include "mbedtls/build_info.h" 21a8e1175bSopenharmony_ci 22a8e1175bSopenharmony_ci#include "mbedtls/platform.h" 23a8e1175bSopenharmony_ci 24a8e1175bSopenharmony_ci#if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \ 25a8e1175bSopenharmony_ci defined(MBEDTLS_FS_IO) 26a8e1175bSopenharmony_ci#include "mbedtls/entropy.h" 27a8e1175bSopenharmony_ci#include "mbedtls/ctr_drbg.h" 28a8e1175bSopenharmony_ci 29a8e1175bSopenharmony_ci#include <stdio.h> 30a8e1175bSopenharmony_ci#endif 31a8e1175bSopenharmony_ci 32a8e1175bSopenharmony_ci#if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \ 33a8e1175bSopenharmony_ci !defined(MBEDTLS_FS_IO) 34a8e1175bSopenharmony_ciint main(void) 35a8e1175bSopenharmony_ci{ 36a8e1175bSopenharmony_ci mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n"); 37a8e1175bSopenharmony_ci mbedtls_exit(0); 38a8e1175bSopenharmony_ci} 39a8e1175bSopenharmony_ci#else 40a8e1175bSopenharmony_ci 41a8e1175bSopenharmony_ci 42a8e1175bSopenharmony_ciint main(int argc, char *argv[]) 43a8e1175bSopenharmony_ci{ 44a8e1175bSopenharmony_ci FILE *f; 45a8e1175bSopenharmony_ci int i, k, ret = 1; 46a8e1175bSopenharmony_ci int exit_code = MBEDTLS_EXIT_FAILURE; 47a8e1175bSopenharmony_ci mbedtls_ctr_drbg_context ctr_drbg; 48a8e1175bSopenharmony_ci mbedtls_entropy_context entropy; 49a8e1175bSopenharmony_ci unsigned char buf[1024]; 50a8e1175bSopenharmony_ci 51a8e1175bSopenharmony_ci mbedtls_ctr_drbg_init(&ctr_drbg); 52a8e1175bSopenharmony_ci 53a8e1175bSopenharmony_ci if (argc < 2) { 54a8e1175bSopenharmony_ci mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]); 55a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 56a8e1175bSopenharmony_ci } 57a8e1175bSopenharmony_ci 58a8e1175bSopenharmony_ci if ((f = fopen(argv[1], "wb+")) == NULL) { 59a8e1175bSopenharmony_ci mbedtls_printf("failed to open '%s' for writing.\n", argv[1]); 60a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 61a8e1175bSopenharmony_ci } 62a8e1175bSopenharmony_ci 63a8e1175bSopenharmony_ci mbedtls_entropy_init(&entropy); 64a8e1175bSopenharmony_ci ret = mbedtls_ctr_drbg_seed(&ctr_drbg, 65a8e1175bSopenharmony_ci mbedtls_entropy_func, 66a8e1175bSopenharmony_ci &entropy, 67a8e1175bSopenharmony_ci (const unsigned char *) "RANDOM_GEN", 68a8e1175bSopenharmony_ci 10); 69a8e1175bSopenharmony_ci if (ret != 0) { 70a8e1175bSopenharmony_ci mbedtls_printf("failed in mbedtls_ctr_drbg_seed: %d\n", ret); 71a8e1175bSopenharmony_ci goto cleanup; 72a8e1175bSopenharmony_ci } 73a8e1175bSopenharmony_ci mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF); 74a8e1175bSopenharmony_ci 75a8e1175bSopenharmony_ci#if defined(MBEDTLS_FS_IO) 76a8e1175bSopenharmony_ci ret = mbedtls_ctr_drbg_update_seed_file(&ctr_drbg, "seedfile"); 77a8e1175bSopenharmony_ci 78a8e1175bSopenharmony_ci if (ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) { 79a8e1175bSopenharmony_ci mbedtls_printf("Failed to open seedfile. Generating one.\n"); 80a8e1175bSopenharmony_ci ret = mbedtls_ctr_drbg_write_seed_file(&ctr_drbg, "seedfile"); 81a8e1175bSopenharmony_ci if (ret != 0) { 82a8e1175bSopenharmony_ci mbedtls_printf("failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret); 83a8e1175bSopenharmony_ci goto cleanup; 84a8e1175bSopenharmony_ci } 85a8e1175bSopenharmony_ci } else if (ret != 0) { 86a8e1175bSopenharmony_ci mbedtls_printf("failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret); 87a8e1175bSopenharmony_ci goto cleanup; 88a8e1175bSopenharmony_ci } 89a8e1175bSopenharmony_ci#endif 90a8e1175bSopenharmony_ci 91a8e1175bSopenharmony_ci for (i = 0, k = 768; i < k; i++) { 92a8e1175bSopenharmony_ci ret = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sizeof(buf)); 93a8e1175bSopenharmony_ci if (ret != 0) { 94a8e1175bSopenharmony_ci mbedtls_printf("failed!\n"); 95a8e1175bSopenharmony_ci goto cleanup; 96a8e1175bSopenharmony_ci } 97a8e1175bSopenharmony_ci 98a8e1175bSopenharmony_ci fwrite(buf, 1, sizeof(buf), f); 99a8e1175bSopenharmony_ci 100a8e1175bSopenharmony_ci mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \ 101a8e1175bSopenharmony_ci "%% done\r", 102a8e1175bSopenharmony_ci (long) (sizeof(buf) * k / 1024), 103a8e1175bSopenharmony_ci argv[1], 104a8e1175bSopenharmony_ci (100 * (float) (i + 1)) / k); 105a8e1175bSopenharmony_ci fflush(stdout); 106a8e1175bSopenharmony_ci } 107a8e1175bSopenharmony_ci 108a8e1175bSopenharmony_ci exit_code = MBEDTLS_EXIT_SUCCESS; 109a8e1175bSopenharmony_ci 110a8e1175bSopenharmony_cicleanup: 111a8e1175bSopenharmony_ci mbedtls_printf("\n"); 112a8e1175bSopenharmony_ci 113a8e1175bSopenharmony_ci fclose(f); 114a8e1175bSopenharmony_ci mbedtls_ctr_drbg_free(&ctr_drbg); 115a8e1175bSopenharmony_ci mbedtls_entropy_free(&entropy); 116a8e1175bSopenharmony_ci 117a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 118a8e1175bSopenharmony_ci} 119a8e1175bSopenharmony_ci#endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */ 120