Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
| 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
| 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 | |
| 26 | #include "polarssl/config.h" |
| 27 | |
| 28 | #if defined(POLARSSL_ENTROPY_C) |
| 29 | |
| 30 | #include "polarssl/entropy.h" |
| 31 | #include "polarssl/entropy_poll.h" |
| 32 | |
| 33 | #if defined(POLARSSL_TIMING_C) |
| 34 | #include "polarssl/timing.h" |
| 35 | #endif |
| 36 | #if defined(POLARSSL_HAVEGE_C) |
| 37 | #include "polarssl/havege.h" |
| 38 | #endif |
| 39 | |
| 40 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 41 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 42 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 43 | #if !defined(_WIN32_WINNT) |
| 44 | #define _WIN32_WINNT 0x0400 |
| 45 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 46 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 47 | #include <wincrypt.h> |
| 48 | |
| 49 | int platform_entropy_poll( void *data, unsigned char *output, size_t len, |
| 50 | size_t *olen ) |
| 51 | { |
| 52 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 53 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 54 | *olen = 0; |
| 55 | |
| 56 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 57 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 58 | { |
| 59 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 60 | } |
| 61 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 62 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 63 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 64 | |
| 65 | CryptReleaseContext( provider, 0 ); |
| 66 | *olen = len; |
| 67 | |
| 68 | return( 0 ); |
| 69 | } |
| 70 | #else |
| 71 | |
| 72 | #include <stdio.h> |
| 73 | |
| 74 | int platform_entropy_poll( void *data, |
| 75 | unsigned char *output, size_t len, size_t *olen ) |
| 76 | { |
| 77 | FILE *file; |
| 78 | size_t ret; |
| 79 | ((void) data); |
| 80 | |
| 81 | *olen = 0; |
| 82 | |
| 83 | file = fopen( "/dev/urandom", "rb" ); |
| 84 | if( file == NULL ) |
| 85 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 86 | |
| 87 | ret = fread( output, 1, len, file ); |
| 88 | if( ret != len ) |
| 89 | { |
| 90 | fclose( file ); |
| 91 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 92 | } |
| 93 | |
| 94 | fclose( file ); |
| 95 | *olen = len; |
| 96 | |
| 97 | return( 0 ); |
| 98 | } |
| 99 | #endif |
| 100 | #endif |
| 101 | |
| 102 | #if defined(POLARSSL_TIMING_C) |
| 103 | int hardclock_poll( void *data, |
| 104 | unsigned char *output, size_t len, size_t *olen ) |
| 105 | { |
| 106 | unsigned long timer = hardclock(); |
| 107 | ((void) data); |
| 108 | *olen = 0; |
| 109 | |
| 110 | if( len < sizeof(unsigned long) ) |
| 111 | return( 0 ); |
| 112 | |
| 113 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 114 | *olen = sizeof(unsigned long); |
| 115 | |
| 116 | return( 0 ); |
| 117 | } |
| 118 | #endif |
| 119 | |
| 120 | #if defined(POLARSSL_HAVEGE_C) |
| 121 | int havege_poll( void *data, |
| 122 | unsigned char *output, size_t len, size_t *olen ) |
| 123 | { |
| 124 | havege_state *hs = (havege_state *) data; |
| 125 | *olen = 0; |
| 126 | |
| 127 | if( havege_random( hs, output, len ) != 0 ) |
| 128 | return POLARSSL_ERR_ENTROPY_SOURCE_FAILED; |
| 129 | |
| 130 | *olen = len; |
| 131 | |
| 132 | return( 0 ); |
| 133 | } |
| 134 | #endif |
| 135 | |
| 136 | #endif /* POLARSSL_ENTROPY_C */ |