gen_random_ctr_drbg.c 3.9 KB

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