1/** 2 * \brief Use and generate multiple entropies calls into a file 3 * 4 * Copyright The Mbed TLS Contributors 5 * SPDX-License-Identifier: Apache-2.0 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); you may 8 * not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20#include "mbedtls/build_info.h" 21 22#include "mbedtls/platform.h" 23 24#if defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_FS_IO) 25#include "mbedtls/entropy.h" 26 27#include <stdio.h> 28#endif 29 30#if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO) 31int main(void) 32{ 33 mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n"); 34 mbedtls_exit(0); 35} 36#else 37 38 39int main(int argc, char *argv[]) 40{ 41 FILE *f; 42 int i, k, ret = 1; 43 int exit_code = MBEDTLS_EXIT_FAILURE; 44 mbedtls_entropy_context entropy; 45 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; 46 47 if (argc < 2) { 48 mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]); 49 mbedtls_exit(exit_code); 50 } 51 52 if ((f = fopen(argv[1], "wb+")) == NULL) { 53 mbedtls_printf("failed to open '%s' for writing.\n", argv[1]); 54 mbedtls_exit(exit_code); 55 } 56 57 mbedtls_entropy_init(&entropy); 58 59 for (i = 0, k = 768; i < k; i++) { 60 ret = mbedtls_entropy_func(&entropy, buf, sizeof(buf)); 61 if (ret != 0) { 62 mbedtls_printf(" failed\n ! mbedtls_entropy_func returned -%04X\n", 63 (unsigned int) ret); 64 goto cleanup; 65 } 66 67 fwrite(buf, 1, sizeof(buf), f); 68 69 mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \ 70 "%% done\r", 71 (long) (sizeof(buf) * k / 1024), 72 argv[1], 73 (100 * (float) (i + 1)) / k); 74 fflush(stdout); 75 } 76 77 exit_code = MBEDTLS_EXIT_SUCCESS; 78 79cleanup: 80 mbedtls_printf("\n"); 81 82 fclose(f); 83 mbedtls_entropy_free(&entropy); 84 85 mbedtls_exit(exit_code); 86} 87#endif /* MBEDTLS_ENTROPY_C */ 88