gen_entropy.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * \brief Use and generate multiple entropies calls into a file
  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_ENTROPY_C) && defined(MBEDTLS_FS_IO)
  36. #include "mbedtls/entropy.h"
  37. #include <stdio.h>
  38. #endif
  39. #if !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_FS_IO)
  40. int main( void )
  41. {
  42. mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
  43. mbedtls_exit( 0 );
  44. }
  45. #else
  46. int main( int argc, char *argv[] )
  47. {
  48. FILE *f;
  49. int i, k, ret = 1;
  50. int exit_code = MBEDTLS_EXIT_FAILURE;
  51. mbedtls_entropy_context entropy;
  52. unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  53. if( argc < 2 )
  54. {
  55. mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  56. mbedtls_exit( exit_code );
  57. }
  58. if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  59. {
  60. mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
  61. mbedtls_exit( exit_code );
  62. }
  63. mbedtls_entropy_init( &entropy );
  64. for( i = 0, k = 768; i < k; i++ )
  65. {
  66. ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
  67. if( ret != 0 )
  68. {
  69. mbedtls_printf( " failed\n ! mbedtls_entropy_func returned -%04X\n",
  70. (unsigned int) ret );
  71. goto cleanup;
  72. }
  73. fwrite( buf, 1, sizeof( buf ), f );
  74. mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
  75. "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
  76. fflush( stdout );
  77. }
  78. exit_code = MBEDTLS_EXIT_SUCCESS;
  79. cleanup:
  80. mbedtls_printf( "\n" );
  81. fclose( f );
  82. mbedtls_entropy_free( &entropy );
  83. mbedtls_exit( exit_code );
  84. }
  85. #endif /* MBEDTLS_ENTROPY_C */