gen_random_ctr_drbg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 OR GPL-2.0-or-later
  6. *
  7. * This file is provided under the Apache License 2.0, or the
  8. * GNU General Public License v2.0 or later.
  9. *
  10. * **********
  11. * Apache License 2.0:
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  14. * not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  21. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. *
  25. * **********
  26. *
  27. * **********
  28. * GNU General Public License v2.0 or later:
  29. *
  30. * This program is free software; you can redistribute it and/or modify
  31. * it under the terms of the GNU General Public License as published by
  32. * the Free Software Foundation; either version 2 of the License, or
  33. * (at your option) any later version.
  34. *
  35. * This program is distributed in the hope that it will be useful,
  36. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38. * GNU General Public License for more details.
  39. *
  40. * You should have received a copy of the GNU General Public License along
  41. * with this program; if not, write to the Free Software Foundation, Inc.,
  42. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  43. *
  44. * **********
  45. */
  46. #if !defined(MBEDTLS_CONFIG_FILE)
  47. #include "mbedtls/config.h"
  48. #else
  49. #include MBEDTLS_CONFIG_FILE
  50. #endif
  51. #if defined(MBEDTLS_PLATFORM_C)
  52. #include "mbedtls/platform.h"
  53. #else
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #define mbedtls_fprintf fprintf
  57. #define mbedtls_printf printf
  58. #define mbedtls_exit exit
  59. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  60. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  61. #endif /* MBEDTLS_PLATFORM_C */
  62. #if defined(MBEDTLS_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C) && \
  63. defined(MBEDTLS_FS_IO)
  64. #include "mbedtls/entropy.h"
  65. #include "mbedtls/ctr_drbg.h"
  66. #include <stdio.h>
  67. #endif
  68. #if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C) || \
  69. !defined(MBEDTLS_FS_IO)
  70. int main( void )
  71. {
  72. mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
  73. mbedtls_exit( 0 );
  74. }
  75. #else
  76. int main( int argc, char *argv[] )
  77. {
  78. FILE *f;
  79. int i, k, ret = 1;
  80. int exit_code = MBEDTLS_EXIT_FAILURE;
  81. mbedtls_ctr_drbg_context ctr_drbg;
  82. mbedtls_entropy_context entropy;
  83. unsigned char buf[1024];
  84. mbedtls_ctr_drbg_init( &ctr_drbg );
  85. if( argc < 2 )
  86. {
  87. mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  88. mbedtls_exit( exit_code );
  89. }
  90. if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  91. {
  92. mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
  93. mbedtls_exit( exit_code );
  94. }
  95. mbedtls_entropy_init( &entropy );
  96. ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
  97. if( ret != 0 )
  98. {
  99. mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
  100. goto cleanup;
  101. }
  102. mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
  103. #if defined(MBEDTLS_FS_IO)
  104. ret = mbedtls_ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
  105. if( ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR )
  106. {
  107. mbedtls_printf( "Failed to open seedfile. Generating one.\n" );
  108. ret = mbedtls_ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
  109. if( ret != 0 )
  110. {
  111. mbedtls_printf( "failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret );
  112. goto cleanup;
  113. }
  114. }
  115. else if( ret != 0 )
  116. {
  117. mbedtls_printf( "failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret );
  118. goto cleanup;
  119. }
  120. #endif
  121. for( i = 0, k = 768; i < k; i++ )
  122. {
  123. ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
  124. if( ret != 0 )
  125. {
  126. mbedtls_printf("failed!\n");
  127. goto cleanup;
  128. }
  129. fwrite( buf, 1, sizeof( buf ), f );
  130. mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
  131. "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
  132. fflush( stdout );
  133. }
  134. exit_code = MBEDTLS_EXIT_SUCCESS;
  135. cleanup:
  136. mbedtls_printf("\n");
  137. fclose( f );
  138. mbedtls_ctr_drbg_free( &ctr_drbg );
  139. mbedtls_entropy_free( &entropy );
  140. mbedtls_exit( exit_code );
  141. }
  142. #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */