Example program to test AES-GCM functionality. Used for a workshop

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers entropy_poll.c Source File

entropy_poll.c

00001 /*
00002  *  Platform-specific and custom entropy polling functions
00003  *
00004  *  Copyright (C) 2006-2014, Brainspark B.V.
00005  *
00006  *  This file is part of PolarSSL (http://www.polarssl.org)
00007  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
00008  *
00009  *  All rights reserved.
00010  *
00011  *  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version.
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU General Public License for more details.
00020  *
00021  *  You should have received a copy of the GNU General Public License along
00022  *  with this program; if not, write to the Free Software Foundation, Inc.,
00023  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00024  */
00025 
00026 #if !defined(POLARSSL_CONFIG_FILE)
00027 #include "polarssl/config.h"
00028 #else
00029 #include POLARSSL_CONFIG_FILE
00030 #endif
00031 
00032 #if defined(POLARSSL_ENTROPY_C)
00033 
00034 #include "polarssl/entropy.h"
00035 #include "polarssl/entropy_poll.h"
00036 
00037 #if defined(POLARSSL_TIMING_C)
00038 #include "polarssl/timing.h"
00039 #endif
00040 #if defined(POLARSSL_HAVEGE_C)
00041 #include "polarssl/havege.h"
00042 #endif
00043 
00044 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
00045 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
00046 
00047 #if !defined(_WIN32_WINNT)
00048 #define _WIN32_WINNT 0x0400
00049 #endif
00050 #include <windows.h>
00051 #include <wincrypt.h>
00052 
00053 int platform_entropy_poll( void *data, unsigned char *output, size_t len,
00054                            size_t *olen )
00055 {
00056     HCRYPTPROV provider;
00057     ((void) data);
00058     *olen = 0;
00059 
00060     if( CryptAcquireContext( &provider, NULL, NULL,
00061                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
00062     {
00063         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00064     }
00065 
00066     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
00067         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00068 
00069     CryptReleaseContext( provider, 0 );
00070     *olen = len;
00071 
00072     return( 0 );
00073 }
00074 #else /* _WIN32 && !EFIX64 && !EFI32 */
00075 
00076 #include <stdio.h>
00077 
00078 int platform_entropy_poll( void *data,
00079                            unsigned char *output, size_t len, size_t *olen )
00080 {
00081     FILE *file;
00082     size_t ret;
00083     ((void) data);
00084 
00085     *olen = 0;
00086 
00087     file = fopen( "/dev/urandom", "rb" );
00088     if( file == NULL )
00089         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00090 
00091     ret = fread( output, 1, len, file );
00092     if( ret != len )
00093     {
00094         fclose( file );
00095         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00096     }
00097 
00098     fclose( file );
00099     *olen = len;
00100 
00101     return( 0 );
00102 }
00103 #endif /* _WIN32 && !EFIX64 && !EFI32 */
00104 #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
00105 
00106 #if defined(POLARSSL_TIMING_C)
00107 int hardclock_poll( void *data,
00108                     unsigned char *output, size_t len, size_t *olen )
00109 {
00110     unsigned long timer = hardclock();
00111     ((void) data);
00112     *olen = 0;
00113 
00114     if( len < sizeof(unsigned long) )
00115         return( 0 );
00116 
00117     memcpy( output, &timer, sizeof(unsigned long) );
00118     *olen = sizeof(unsigned long);
00119 
00120     return( 0 );
00121 }
00122 #endif /* POLARSSL_TIMING_C */
00123 
00124 #if defined(POLARSSL_HAVEGE_C)
00125 int havege_poll( void *data,
00126                  unsigned char *output, size_t len, size_t *olen )
00127 {
00128     havege_state *hs = (havege_state *) data;
00129     *olen = 0;
00130 
00131     if( havege_random( hs, output, len ) != 0 )
00132         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00133 
00134     *olen = len;
00135 
00136     return( 0 );
00137 }
00138 #endif /* POLARSSL_HAVEGE_C */
00139 
00140 #endif /* POLARSSL_ENTROPY_C */
00141 
00142