Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, Brainspark B.V. |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 5 | * |
| 6 | * This file is part of PolarSSL (http://www.polarssl.org) |
| 7 | * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org> |
| 8 | * |
| 9 | * All rights reserved. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | */ |
| 25 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 27 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 28 | #else |
| 29 | #include POLARSSL_CONFIG_FILE |
| 30 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 31 | |
| 32 | #if defined(POLARSSL_ENTROPY_C) |
| 33 | |
| 34 | #include "polarssl/entropy.h" |
| 35 | #include "polarssl/entropy_poll.h" |
| 36 | |
| 37 | #if defined(POLARSSL_TIMING_C) |
| 38 | #include "polarssl/timing.h" |
| 39 | #endif |
| 40 | #if defined(POLARSSL_HAVEGE_C) |
| 41 | #include "polarssl/havege.h" |
| 42 | #endif |
| 43 | |
| 44 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 45 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 46 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 47 | #if !defined(_WIN32_WINNT) |
| 48 | #define _WIN32_WINNT 0x0400 |
| 49 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 50 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 51 | #include <wincrypt.h> |
| 52 | |
| 53 | int platform_entropy_poll( void *data, unsigned char *output, size_t len, |
| 54 | size_t *olen ) |
| 55 | { |
| 56 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 57 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 58 | *olen = 0; |
| 59 | |
| 60 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 61 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 62 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 63 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 66 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 67 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 68 | |
| 69 | CryptReleaseContext( provider, 0 ); |
| 70 | *olen = len; |
| 71 | |
| 72 | return( 0 ); |
| 73 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 74 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 75 | |
| 76 | #include <stdio.h> |
| 77 | |
| 78 | int platform_entropy_poll( void *data, |
| 79 | unsigned char *output, size_t len, size_t *olen ) |
| 80 | { |
| 81 | FILE *file; |
| 82 | size_t ret; |
| 83 | ((void) data); |
| 84 | |
| 85 | *olen = 0; |
| 86 | |
| 87 | file = fopen( "/dev/urandom", "rb" ); |
| 88 | if( file == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 89 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 90 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 91 | ret = fread( output, 1, len, file ); |
| 92 | if( ret != len ) |
| 93 | { |
| 94 | fclose( file ); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 95 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | fclose( file ); |
| 99 | *olen = len; |
| 100 | |
| 101 | return( 0 ); |
| 102 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 103 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
| 104 | #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 105 | |
| 106 | #if defined(POLARSSL_TIMING_C) |
| 107 | int hardclock_poll( void *data, |
| 108 | unsigned char *output, size_t len, size_t *olen ) |
| 109 | { |
| 110 | unsigned long timer = hardclock(); |
| 111 | ((void) data); |
| 112 | *olen = 0; |
| 113 | |
| 114 | if( len < sizeof(unsigned long) ) |
| 115 | return( 0 ); |
| 116 | |
| 117 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 118 | *olen = sizeof(unsigned long); |
| 119 | |
| 120 | return( 0 ); |
| 121 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 122 | #endif /* POLARSSL_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 123 | |
| 124 | #if defined(POLARSSL_HAVEGE_C) |
| 125 | int havege_poll( void *data, |
| 126 | unsigned char *output, size_t len, size_t *olen ) |
| 127 | { |
| 128 | havege_state *hs = (havege_state *) data; |
| 129 | *olen = 0; |
| 130 | |
| 131 | if( havege_random( hs, output, len ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 132 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 133 | |
| 134 | *olen = len; |
| 135 | |
| 136 | return( 0 ); |
| 137 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 138 | #endif /* POLARSSL_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 139 | |
| 140 | #endif /* POLARSSL_ENTROPY_C */ |