Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 085ab04 | 2015-01-23 11:06:27 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://www.polarssl.org) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 28 | |
| 29 | #if defined(POLARSSL_ENTROPY_C) |
| 30 | |
| 31 | #include "polarssl/entropy.h" |
| 32 | #include "polarssl/entropy_poll.h" |
| 33 | |
| 34 | #if defined(POLARSSL_TIMING_C) |
| 35 | #include "polarssl/timing.h" |
| 36 | #endif |
| 37 | #if defined(POLARSSL_HAVEGE_C) |
| 38 | #include "polarssl/havege.h" |
| 39 | #endif |
| 40 | |
| 41 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 42 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 44 | #if !defined(_WIN32_WINNT) |
| 45 | #define _WIN32_WINNT 0x0400 |
| 46 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 47 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 48 | #include <wincrypt.h> |
| 49 | |
| 50 | int platform_entropy_poll( void *data, unsigned char *output, size_t len, |
| 51 | size_t *olen ) |
| 52 | { |
| 53 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 54 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 55 | *olen = 0; |
| 56 | |
| 57 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 58 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 59 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 60 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 63 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 64 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 65 | |
| 66 | CryptReleaseContext( provider, 0 ); |
| 67 | *olen = len; |
| 68 | |
| 69 | return( 0 ); |
| 70 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 71 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 72 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Test for Linux getrandom() support. |
| 75 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper |
| 76 | * available in GNU libc and compatible libc's (eg uClibc). |
| 77 | */ |
| 78 | #if defined(__linux__) && defined(__GLIBC__) |
| 79 | #include <linux/version.h> |
| 80 | #include <unistd.h> |
| 81 | #include <sys/syscall.h> |
| 82 | #if defined(SYS_getrandom) |
| 83 | #define HAVE_GETRANDOM |
| 84 | static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) |
| 85 | { |
| 86 | return( syscall( SYS_getrandom, buf, buflen, flags ) ); |
| 87 | } |
| 88 | #endif /* SYS_getrandom */ |
| 89 | #endif /* __linux__ */ |
| 90 | |
| 91 | #if defined(HAVE_GETRANDOM) |
| 92 | |
| 93 | #include <errno.h> |
| 94 | |
| 95 | int platform_entropy_poll( void *data, |
| 96 | unsigned char *output, size_t len, size_t *olen ) |
| 97 | { |
| 98 | int ret; |
| 99 | ((void) data); |
| 100 | |
| 101 | if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) |
| 102 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 103 | |
| 104 | *olen = ret; |
| 105 | return( 0 ); |
| 106 | } |
| 107 | |
| 108 | #else /* HAVE_GETRANDOM */ |
| 109 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 110 | #include <stdio.h> |
| 111 | |
| 112 | int platform_entropy_poll( void *data, |
| 113 | unsigned char *output, size_t len, size_t *olen ) |
| 114 | { |
| 115 | FILE *file; |
| 116 | size_t ret; |
| 117 | ((void) data); |
| 118 | |
| 119 | *olen = 0; |
| 120 | |
| 121 | file = fopen( "/dev/urandom", "rb" ); |
| 122 | if( file == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 123 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 124 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 125 | ret = fread( output, 1, len, file ); |
| 126 | if( ret != len ) |
| 127 | { |
| 128 | fclose( file ); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 129 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | fclose( file ); |
| 133 | *olen = len; |
| 134 | |
| 135 | return( 0 ); |
| 136 | } |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 137 | #endif /* HAVE_GETRANDOM */ |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 138 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
| 139 | #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 140 | |
| 141 | #if defined(POLARSSL_TIMING_C) |
| 142 | int hardclock_poll( void *data, |
| 143 | unsigned char *output, size_t len, size_t *olen ) |
| 144 | { |
| 145 | unsigned long timer = hardclock(); |
| 146 | ((void) data); |
| 147 | *olen = 0; |
| 148 | |
| 149 | if( len < sizeof(unsigned long) ) |
| 150 | return( 0 ); |
| 151 | |
| 152 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 153 | *olen = sizeof(unsigned long); |
| 154 | |
| 155 | return( 0 ); |
| 156 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 157 | #endif /* POLARSSL_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 158 | |
| 159 | #if defined(POLARSSL_HAVEGE_C) |
| 160 | int havege_poll( void *data, |
| 161 | unsigned char *output, size_t len, size_t *olen ) |
| 162 | { |
| 163 | havege_state *hs = (havege_state *) data; |
| 164 | *olen = 0; |
| 165 | |
| 166 | if( havege_random( hs, output, len ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 167 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 168 | |
| 169 | *olen = len; |
| 170 | |
| 171 | return( 0 ); |
| 172 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 173 | #endif /* POLARSSL_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 174 | |
| 175 | #endif /* POLARSSL_ENTROPY_C */ |