2011-12-03 22:45:14 +01:00
|
|
|
/*
|
|
|
|
* Platform-specific and custom entropy polling functions
|
|
|
|
*
|
2014-05-01 13:03:14 +02:00
|
|
|
* Copyright (C) 2006-2014, Brainspark B.V.
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* This file is part of PolarSSL (http://www.polarssl.org)
|
|
|
|
* Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2011-12-03 22:45:14 +01:00
|
|
|
#include "polarssl/config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#if defined(POLARSSL_ENTROPY_C)
|
|
|
|
|
|
|
|
#include "polarssl/entropy.h"
|
|
|
|
#include "polarssl/entropy_poll.h"
|
|
|
|
|
|
|
|
#if defined(POLARSSL_TIMING_C)
|
|
|
|
#include "polarssl/timing.h"
|
|
|
|
#endif
|
|
|
|
#if defined(POLARSSL_HAVEGE_C)
|
|
|
|
#include "polarssl/havege.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
|
2013-10-28 18:48:30 +01:00
|
|
|
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#if !defined(_WIN32_WINNT)
|
|
|
|
#define _WIN32_WINNT 0x0400
|
|
|
|
#endif
|
2012-11-02 12:06:08 +01:00
|
|
|
#include <windows.h>
|
2011-12-03 22:45:14 +01:00
|
|
|
#include <wincrypt.h>
|
|
|
|
|
|
|
|
int platform_entropy_poll( void *data, unsigned char *output, size_t len,
|
|
|
|
size_t *olen )
|
|
|
|
{
|
|
|
|
HCRYPTPROV provider;
|
2011-12-05 14:54:00 +01:00
|
|
|
((void) data);
|
2011-12-03 22:45:14 +01:00
|
|
|
*olen = 0;
|
|
|
|
|
|
|
|
if( CryptAcquireContext( &provider, NULL, NULL,
|
|
|
|
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
|
|
|
|
{
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
2011-12-03 22:45:14 +01:00
|
|
|
}
|
|
|
|
|
2013-10-11 18:58:55 +02:00
|
|
|
if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
CryptReleaseContext( provider, 0 );
|
|
|
|
*olen = len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#else /* _WIN32 && !EFIX64 && !EFI32 */
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
int platform_entropy_poll( void *data,
|
|
|
|
unsigned char *output, size_t len, size_t *olen )
|
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
size_t ret;
|
|
|
|
((void) data);
|
|
|
|
|
|
|
|
*olen = 0;
|
|
|
|
|
|
|
|
file = fopen( "/dev/urandom", "rb" );
|
|
|
|
if( file == NULL )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
2014-05-01 13:03:14 +02:00
|
|
|
|
2011-12-03 22:45:14 +01:00
|
|
|
ret = fread( output, 1, len, file );
|
|
|
|
if( ret != len )
|
|
|
|
{
|
|
|
|
fclose( file );
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
2011-12-03 22:45:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose( file );
|
|
|
|
*olen = len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* _WIN32 && !EFIX64 && !EFI32 */
|
|
|
|
#endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#if defined(POLARSSL_TIMING_C)
|
|
|
|
int hardclock_poll( void *data,
|
|
|
|
unsigned char *output, size_t len, size_t *olen )
|
|
|
|
{
|
|
|
|
unsigned long timer = hardclock();
|
|
|
|
((void) data);
|
|
|
|
*olen = 0;
|
|
|
|
|
|
|
|
if( len < sizeof(unsigned long) )
|
|
|
|
return( 0 );
|
|
|
|
|
|
|
|
memcpy( output, &timer, sizeof(unsigned long) );
|
|
|
|
*olen = sizeof(unsigned long);
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_TIMING_C */
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#if defined(POLARSSL_HAVEGE_C)
|
|
|
|
int havege_poll( void *data,
|
|
|
|
unsigned char *output, size_t len, size_t *olen )
|
|
|
|
{
|
|
|
|
havege_state *hs = (havege_state *) data;
|
|
|
|
*olen = 0;
|
|
|
|
|
|
|
|
if( havege_random( hs, output, len ) != 0 )
|
2014-06-17 14:06:49 +02:00
|
|
|
return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
*olen = len;
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_HAVEGE_C */
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
#endif /* POLARSSL_ENTROPY_C */
|