1a8e1175bSopenharmony_ci/** 2a8e1175bSopenharmony_ci * \brief Use and generate multiple entropies calls into a file 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_ENTROPY_C) && defined(MBEDTLS_FS_IO) 25a8e1175bSopenharmony_ci#include "mbedtls/entropy.h" 26a8e1175bSopenharmony_ci 27a8e1175bSopenharmony_ci#include <stdio.h> 28a8e1175bSopenharmony_ci#endif 29a8e1175bSopenharmony_ci 30a8e1175bSopenharmony_ci#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) 31a8e1175bSopenharmony_ciint main(void) 32a8e1175bSopenharmony_ci{ 33a8e1175bSopenharmony_ci mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n"); 34a8e1175bSopenharmony_ci mbedtls_exit(0); 35a8e1175bSopenharmony_ci} 36a8e1175bSopenharmony_ci#else 37a8e1175bSopenharmony_ci 38a8e1175bSopenharmony_ci 39a8e1175bSopenharmony_ciint main(int argc, char *argv[]) 40a8e1175bSopenharmony_ci{ 41a8e1175bSopenharmony_ci FILE *f; 42a8e1175bSopenharmony_ci int i, k, ret = 1; 43a8e1175bSopenharmony_ci int exit_code = MBEDTLS_EXIT_FAILURE; 44a8e1175bSopenharmony_ci mbedtls_entropy_context entropy; 45a8e1175bSopenharmony_ci unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; 46a8e1175bSopenharmony_ci 47a8e1175bSopenharmony_ci if (argc < 2) { 48a8e1175bSopenharmony_ci mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]); 49a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 50a8e1175bSopenharmony_ci } 51a8e1175bSopenharmony_ci 52a8e1175bSopenharmony_ci if ((f = fopen(argv[1], "wb+")) == NULL) { 53a8e1175bSopenharmony_ci mbedtls_printf("failed to open '%s' for writing.\n", argv[1]); 54a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 55a8e1175bSopenharmony_ci } 56a8e1175bSopenharmony_ci 57a8e1175bSopenharmony_ci mbedtls_entropy_init(&entropy); 58a8e1175bSopenharmony_ci 59a8e1175bSopenharmony_ci for (i = 0, k = 768; i < k; i++) { 60a8e1175bSopenharmony_ci ret = mbedtls_entropy_func(&entropy, buf, sizeof(buf)); 61a8e1175bSopenharmony_ci if (ret != 0) { 62a8e1175bSopenharmony_ci mbedtls_printf(" failed\n ! mbedtls_entropy_func returned -%04X\n", 63a8e1175bSopenharmony_ci (unsigned int) ret); 64a8e1175bSopenharmony_ci goto cleanup; 65a8e1175bSopenharmony_ci } 66a8e1175bSopenharmony_ci 67a8e1175bSopenharmony_ci fwrite(buf, 1, sizeof(buf), f); 68a8e1175bSopenharmony_ci 69a8e1175bSopenharmony_ci mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \ 70a8e1175bSopenharmony_ci "%% done\r", 71a8e1175bSopenharmony_ci (long) (sizeof(buf) * k / 1024), 72a8e1175bSopenharmony_ci argv[1], 73a8e1175bSopenharmony_ci (100 * (float) (i + 1)) / k); 74a8e1175bSopenharmony_ci fflush(stdout); 75a8e1175bSopenharmony_ci } 76a8e1175bSopenharmony_ci 77a8e1175bSopenharmony_ci exit_code = MBEDTLS_EXIT_SUCCESS; 78a8e1175bSopenharmony_ci 79a8e1175bSopenharmony_cicleanup: 80a8e1175bSopenharmony_ci mbedtls_printf("\n"); 81a8e1175bSopenharmony_ci 82a8e1175bSopenharmony_ci fclose(f); 83a8e1175bSopenharmony_ci mbedtls_entropy_free(&entropy); 84a8e1175bSopenharmony_ci 85a8e1175bSopenharmony_ci mbedtls_exit(exit_code); 86a8e1175bSopenharmony_ci} 87a8e1175bSopenharmony_ci#endif /* MBEDTLS_ENTROPY_C */ 88