gen_random_ctr_drbg.c 3.8 KB

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