gen_entropy.c 2.8 KB

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