PolarSSL v1.1.4
entropy_poll.c
Go to the documentation of this file.
00001 /*
00002  *  Platform-specific and custom entropy polling functions
00003  *
00004  *  Copyright (C) 2006-2011, 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 #include "polarssl/config.h"
00027 
00028 #if defined(POLARSSL_ENTROPY_C)
00029 
00030 #include "polarssl/entropy.h"
00031 #include "polarssl/entropy_poll.h"
00032 
00033 #if defined(POLARSSL_TIMING_C)
00034 #include "polarssl/timing.h"
00035 #endif
00036 #if defined(POLARSSL_HAVEGE_C)
00037 #include "polarssl/havege.h"
00038 #endif
00039 
00040 #if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
00041 #if defined(_WIN32)
00042 
00043 #include <windows.h>
00044 #if !defined(_WIN32_WINNT)
00045 #define _WIN32_WINNT 0x0400
00046 #endif
00047 #include <wincrypt.h>
00048 
00049 int platform_entropy_poll( void *data, unsigned char *output, size_t len,
00050                            size_t *olen )
00051 {
00052     HCRYPTPROV provider;
00053     ((void) data);
00054     *olen = 0;
00055 
00056     if( CryptAcquireContext( &provider, NULL, NULL,
00057                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
00058     {
00059         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00060     }
00061 
00062     if( CryptGenRandom( provider, len, output ) == FALSE )
00063         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00064 
00065     CryptReleaseContext( provider, 0 );
00066     *olen = len;
00067 
00068     return( 0 );
00069 }
00070 #else
00071 
00072 #include <stdio.h>
00073 
00074 int platform_entropy_poll( void *data,
00075                            unsigned char *output, size_t len, size_t *olen )
00076 {
00077     FILE *file;
00078     size_t ret;
00079     ((void) data);
00080 
00081     *olen = 0;
00082 
00083     file = fopen( "/dev/urandom", "rb" );
00084     if( file == NULL )
00085         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00086     
00087     ret = fread( output, 1, len, file );
00088     if( ret != len )
00089     {
00090         fclose( file );
00091         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00092     }
00093 
00094     fclose( file );
00095     *olen = len;
00096 
00097     return( 0 );
00098 }
00099 #endif
00100 #endif
00101 
00102 #if defined(POLARSSL_TIMING_C)
00103 int hardclock_poll( void *data,
00104                     unsigned char *output, size_t len, size_t *olen )
00105 {
00106     unsigned long timer = hardclock();
00107     ((void) data);
00108     *olen = 0;
00109 
00110     if( len < sizeof(unsigned long) )
00111         return( 0 );
00112 
00113     memcpy( output, &timer, sizeof(unsigned long) );
00114     *olen = sizeof(unsigned long);
00115 
00116     return( 0 );
00117 }
00118 #endif
00119 
00120 #if defined(POLARSSL_HAVEGE_C)
00121 int havege_poll( void *data,
00122                  unsigned char *output, size_t len, size_t *olen )
00123 {
00124     havege_state *hs = (havege_state *) data;
00125     *olen = 0;
00126 
00127     if( havege_random( hs, output, len ) != 0 )
00128         return POLARSSL_ERR_ENTROPY_SOURCE_FAILED;
00129 
00130     *olen = len;
00131 
00132     return( 0 );
00133 }
00134 #endif
00135 
00136 #endif /* POLARSSL_ENTROPY_C */