gen_random_havege.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * \brief Generate random data 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_HAVEGE_C) && defined(MBEDTLS_FS_IO)
  38. #include "mbedtls/havege.h"
  39. #include <stdio.h>
  40. #include <time.h>
  41. #endif
  42. #if !defined(MBEDTLS_HAVEGE_C) || !defined(MBEDTLS_FS_IO)
  43. int main( void )
  44. {
  45. mbedtls_printf("MBEDTLS_HAVEGE_C not defined.\n");
  46. return( 0 );
  47. }
  48. #else
  49. int main( int argc, char *argv[] )
  50. {
  51. FILE *f;
  52. time_t t;
  53. int i, k, ret = 1;
  54. int exit_code = MBEDTLS_EXIT_FAILURE;
  55. mbedtls_havege_state hs;
  56. unsigned char buf[1024];
  57. if( argc < 2 )
  58. {
  59. mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  60. return( exit_code );
  61. }
  62. if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  63. {
  64. mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
  65. return( exit_code );
  66. }
  67. mbedtls_havege_init( &hs );
  68. t = time( NULL );
  69. for( i = 0, k = 768; i < k; i++ )
  70. {
  71. if( ( ret = mbedtls_havege_random( &hs, buf, sizeof( buf ) ) ) != 0 )
  72. {
  73. mbedtls_printf( " failed\n ! mbedtls_havege_random returned -0x%04X",
  74. -ret );
  75. goto exit;
  76. }
  77. fwrite( buf, sizeof( buf ), 1, f );
  78. mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
  79. "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
  80. fflush( stdout );
  81. }
  82. if( t == time( NULL ) )
  83. t--;
  84. mbedtls_printf(" \n ");
  85. exit_code = MBEDTLS_EXIT_SUCCESS;
  86. exit:
  87. mbedtls_havege_free( &hs );
  88. fclose( f );
  89. return( exit_code );
  90. }
  91. #endif /* MBEDTLS_HAVEGE_C */