gen_random_ctr_drbg.c 3.6 KB

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