gen_random_ctr_drbg.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * \brief Use and generate random data into a file via the CTR_DBRG based on AES
  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. #include "mbedtls/build_info.h"
  20. #include "mbedtls/platform.h"
  21. #if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
  22. defined(MBEDTLS_FS_IO)
  23. #include "mbedtls/entropy.h"
  24. #include "mbedtls/ctr_drbg.h"
  25. #include <stdio.h>
  26. #endif
  27. #if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
  28. !defined(MBEDTLS_FS_IO)
  29. int main(void)
  30. {
  31. mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
  32. mbedtls_exit(0);
  33. }
  34. #else
  35. int main(int argc, char *argv[])
  36. {
  37. FILE *f;
  38. int i, k, ret = 1;
  39. int exit_code = MBEDTLS_EXIT_FAILURE;
  40. mbedtls_ctr_drbg_context ctr_drbg;
  41. mbedtls_entropy_context entropy;
  42. unsigned char buf[1024];
  43. mbedtls_ctr_drbg_init(&ctr_drbg);
  44. if (argc < 2) {
  45. mbedtls_fprintf(stderr, "usage: %s <output filename>\n", argv[0]);
  46. mbedtls_exit(exit_code);
  47. }
  48. if ((f = fopen(argv[1], "wb+")) == NULL) {
  49. mbedtls_printf("failed to open '%s' for writing.\n", argv[1]);
  50. mbedtls_exit(exit_code);
  51. }
  52. mbedtls_entropy_init(&entropy);
  53. ret = mbedtls_ctr_drbg_seed(&ctr_drbg,
  54. mbedtls_entropy_func,
  55. &entropy,
  56. (const unsigned char *) "RANDOM_GEN",
  57. 10);
  58. if (ret != 0) {
  59. mbedtls_printf("failed in mbedtls_ctr_drbg_seed: %d\n", ret);
  60. goto cleanup;
  61. }
  62. mbedtls_ctr_drbg_set_prediction_resistance(&ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF);
  63. #if defined(MBEDTLS_FS_IO)
  64. ret = mbedtls_ctr_drbg_update_seed_file(&ctr_drbg, "seedfile");
  65. if (ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR) {
  66. mbedtls_printf("Failed to open seedfile. Generating one.\n");
  67. ret = mbedtls_ctr_drbg_write_seed_file(&ctr_drbg, "seedfile");
  68. if (ret != 0) {
  69. mbedtls_printf("failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret);
  70. goto cleanup;
  71. }
  72. } else if (ret != 0) {
  73. mbedtls_printf("failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret);
  74. goto cleanup;
  75. }
  76. #endif
  77. for (i = 0, k = 768; i < k; i++) {
  78. ret = mbedtls_ctr_drbg_random(&ctr_drbg, buf, sizeof(buf));
  79. if (ret != 0) {
  80. mbedtls_printf("failed!\n");
  81. goto cleanup;
  82. }
  83. fwrite(buf, 1, sizeof(buf), f);
  84. mbedtls_printf("Generating %ldkb of data in file '%s'... %04.1f" \
  85. "%% done\r",
  86. (long) (sizeof(buf) * k / 1024),
  87. argv[1],
  88. (100 * (float) (i + 1)) / k);
  89. fflush(stdout);
  90. }
  91. exit_code = MBEDTLS_EXIT_SUCCESS;
  92. cleanup:
  93. mbedtls_printf("\n");
  94. fclose(f);
  95. mbedtls_ctr_drbg_free(&ctr_drbg);
  96. mbedtls_entropy_free(&entropy);
  97. mbedtls_exit(exit_code);
  98. }
  99. #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */