Subversion Repositories Kolibri OS

Rev

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

  1. /**
  2.  *  \brief Use and generate random data into a file via the CTR_DBRG based on AES
  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_CTR_DRBG_C) && defined(MBEDTLS_ENTROPY_C)/* && \
  43.  defined(MBEDTLS_FS_IO)*/
  44. #include "mbedtls/entropy.h"
  45. #include "mbedtls/ctr_drbg.h"
  46.  
  47. #include <stdio.h>
  48. #endif
  49.  
  50. #if !defined(MBEDTLS_CTR_DRBG_C) || !defined(MBEDTLS_ENTROPY_C)/* || \
  51.  !defined(MBEDTLS_FS_IO)*/
  52. int main( void )
  53. {
  54.     mbedtls_printf("MBEDTLS_CTR_DRBG_C and/or MBEDTLS_ENTROPY_C and/or MBEDTLS_FS_IO not defined.\n");
  55.     return( 0 );
  56. }
  57. #else
  58.  
  59.  
  60. int main( int argc, char *argv[] )
  61. {
  62.     FILE *f;
  63.     int i, k, ret = 1;
  64.     int exit_code = MBEDTLS_EXIT_FAILURE;
  65.     mbedtls_ctr_drbg_context ctr_drbg;
  66.     mbedtls_entropy_context entropy;
  67.     unsigned char buf[1024];
  68.  
  69.     mbedtls_ctr_drbg_init( &ctr_drbg );
  70.  
  71.     if( argc < 2 )
  72.     {
  73.         //mbedtls_fprintf( stderr, "usage: %s <output filename>\n", argv[0] );
  74.         mbedtls_printf( "usage: %s <output filename>\n", argv[0] );
  75.         return( exit_code );
  76.     }
  77.  
  78.     if( ( f = fopen( argv[1], "wb+" ) ) == NULL )
  79.     {
  80.         mbedtls_printf( "failed to open '%s' for writing.\n", argv[1] );
  81.         return( exit_code );
  82.     }
  83.  
  84.     mbedtls_entropy_init( &entropy );
  85.     ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) "RANDOM_GEN", 10 );
  86.     if( ret != 0 )
  87.     {
  88.         mbedtls_printf( "failed in mbedtls_ctr_drbg_seed: %d\n", ret );
  89.         goto cleanup;
  90.     }
  91.     mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_OFF );
  92.  
  93. /*#if defined(MBEDTLS_FS_IO)
  94.     ret = mbedtls_ctr_drbg_update_seed_file( &ctr_drbg, "seedfile" );
  95.  
  96.     if( ret == MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR )
  97.     {
  98.         mbedtls_printf( "Failed to open seedfile. Generating one.\n" );
  99.         ret = mbedtls_ctr_drbg_write_seed_file( &ctr_drbg, "seedfile" );
  100.         if( ret != 0 )
  101.         {
  102.             mbedtls_printf( "failed in mbedtls_ctr_drbg_write_seed_file: %d\n", ret );
  103.             goto cleanup;
  104.         }
  105.     }
  106.     else if( ret != 0 )
  107.     {
  108.         mbedtls_printf( "failed in mbedtls_ctr_drbg_update_seed_file: %d\n", ret );
  109.         goto cleanup;
  110.     }
  111. #endif*/
  112.  
  113.     for( i = 0, k = 768; i < k; i++ )
  114.     {
  115.         ret = mbedtls_ctr_drbg_random( &ctr_drbg, buf, sizeof( buf ) );
  116.         if( ret != 0 )
  117.         {
  118.             mbedtls_printf("failed!\n");
  119.             goto cleanup;
  120.         }
  121.  
  122.         fwrite( buf, 1, sizeof( buf ), f );
  123.  
  124.         mbedtls_printf( "Generating %ldkb of data in file '%s'... %04.1f" \
  125.                 "%% done\r", (long)(sizeof(buf) * k / 1024), argv[1], (100 * (float) (i + 1)) / k );
  126.         fflush( stdout );
  127.     }
  128.  
  129.     exit_code = MBEDTLS_EXIT_SUCCESS;
  130.  
  131. cleanup:
  132.     mbedtls_printf("\n");
  133.  
  134.     fclose( f );
  135.     mbedtls_ctr_drbg_free( &ctr_drbg );
  136.     mbedtls_entropy_free( &entropy );
  137.  
  138.     return( exit_code );
  139. }
  140. #endif /* MBEDTLS_CTR_DRBG_C && MBEDTLS_ENTROPY_C */
  141.