gen_random_havege.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * \brief Generate random data 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_HAVEGE_C) && defined(MBEDTLS_FS_IO)
  36. #include "mbedtls/havege.h"
  37. #include <stdio.h>
  38. #include <time.h>
  39. #endif
  40. #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
  41. int main( void )
  42. {
  43. mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
  44. mbedtls_exit( 0 );
  45. }
  46. #else
  47. int main( int argc, char *argv[] )
  48. {
  49. FILE *f;
  50. time_t t;
  51. int i, k, ret = 1;
  52. int exit_code = MBEDTLS_EXIT_FAILURE;
  53. mbedtls_havege_state hs;
  54. unsigned char buf[1024];
  55. if( argc < 2 )
  56. {
  57. mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  58. mbedtls_exit( 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. mbedtls_exit( exit_code );
  64. }
  65. mbedtls_havege_init( &hs );
  66. t = time( NULL );
  67. for( i = 0, k = 768; i < k; i++ )
  68. {
  69. if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
  70. {
  71. mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
  72. ( unsigned int ) -ret );
  73. goto exit;
  74. }
  75. fwrite( buf, sizeof( buf ), 1, 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. if( t == time( NULL ) )
  81. t--;
  82. mbedtls_printf(" \n ");
  83. exit_code = MBEDTLS_EXIT_SUCCESS;
  84. exit:
  85. mbedtls_havege_free( &hs );
  86. fclose( f );
  87. mbedtls_exit( exit_code );
  88. }
  89. #endif /* MBEDTLS_HAVEGE_C */