Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  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: GPL-2.0
  6.  *
  7.  *  This program is free software; you can redistribute it and/or modify
  8.  *  it under the terms of the GNU General Public License as published by
  9.  *  the Free Software Foundation; either version 2 of the License, or
  10.  *  (at your option) any later version.
  11.  *
  12.  *  This program is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.  *  GNU General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU General Public License along
  18.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  19.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20.  *
  21.  *  This file is part of mbed TLS (https://tls.mbed.org)
  22.  */
  23.  
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29.  
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #define mbedtls_fprintf         fprintf
  36. #define mbedtls_printf          printf
  37. #define mbedtls_exit            exit
  38. #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS
  39. #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE
  40. #endif /* MBEDTLS_PLATFORM_C */
  41.  
  42. #if defined(MBEDTLS_ENTROPY_C)/* && defined(MBEDTLS_FS_IO)*/
  43. #include "mbedtls/entropy.h"
  44.  
  45. #include <stdio.h>
  46. #endif
  47.  
  48. #if !defined(MBEDTLS_ENTROPY_C)/* || !defined(MBEDTLS_FS_IO)*/
  49. int main( void )
  50. {
  51.     mbedtls_printf("MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
  52.     return( 0 );
  53. }
  54. #else
  55.  
  56.  
  57. int main( int argc, char *argv[] )
  58. {
  59.     FILE *f;
  60.     int i, k, ret = 1;
  61.     int exit_code = MBEDTLS_EXIT_FAILURE;
  62.     mbedtls_entropy_context entropy;
  63.     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
  64.  
  65.     if( argc < 2 )
  66.     {
  67.         //mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  68.         mbedtls_printf( "usage: %s <output filename>\n", argv[0] );
  69.         return( exit_code );
  70.     }
  71.  
  72.     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  73.     {
  74.         mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
  75.         return( exit_code );
  76.     }
  77.  
  78.     mbedtls_entropy_init( &entropy );
  79.  
  80.     for( i = 0, k = 768; i < k; i++ )
  81.     {
  82.         ret = mbedtls_entropy_func( &entropy, buf, sizeof( buf ) );
  83.         if( ret != 0 )
  84.         {
  85.             mbedtls_printf( "  failed\n  !  mbedtls_entropy_func returned -%04X\n",
  86.                             ret );
  87.             goto cleanup;
  88.         }
  89.  
  90.         fwrite( buf, 1, sizeof( buf ), f );
  91.  
  92.         mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
  93.                 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
  94.         fflush( stdout );
  95.     }
  96.  
  97.     exit_code = MBEDTLS_EXIT_SUCCESS;
  98.  
  99. cleanup:
  100.     mbedtls_printf( "\n" );
  101.  
  102.     fclose( f );
  103.     mbedtls_entropy_free( &entropy );
  104.  
  105.     return( exit_code );
  106. }
  107. #endif /* MBEDTLS_ENTROPY_C */
  108.