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 | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://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) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 35 | #include <string.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 36 | #include "polarssl/timing.h" |
| 37 | #endif |
| 38 | #if defined(POLARSSL_HAVEGE_C) |
| 39 | #include "polarssl/havege.h" |
| 40 | #endif |
| 41 | |
| 42 | #if !defined(POLARSSL_NO_PLATFORM_ENTROPY) |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 43 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 44 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 45 | #if !defined(_WIN32_WINNT) |
| 46 | #define _WIN32_WINNT 0x0400 |
| 47 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 48 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 49 | #include <wincrypt.h> |
| 50 | |
| 51 | int platform_entropy_poll( void *data, unsigned char *output, size_t len, |
| 52 | size_t *olen ) |
| 53 | { |
| 54 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 55 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 56 | *olen = 0; |
| 57 | |
| 58 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 59 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 60 | { |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 61 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 64 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 65 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 66 | |
| 67 | CryptReleaseContext( provider, 0 ); |
| 68 | *olen = len; |
| 69 | |
| 70 | return( 0 ); |
| 71 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 72 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 74 | /* |
| 75 | * Test for Linux getrandom() support. |
| 76 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper |
| 77 | * available in GNU libc and compatible libc's (eg uClibc). |
| 78 | */ |
| 79 | #if defined(__linux__) && defined(__GLIBC__) |
| 80 | #include <linux/version.h> |
| 81 | #include <unistd.h> |
| 82 | #include <sys/syscall.h> |
| 83 | #if defined(SYS_getrandom) |
| 84 | #define HAVE_GETRANDOM |
| 85 | static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) |
| 86 | { |
| 87 | return( syscall( SYS_getrandom, buf, buflen, flags ) ); |
| 88 | } |
| 89 | #endif /* SYS_getrandom */ |
| 90 | #endif /* __linux__ */ |
| 91 | |
| 92 | #if defined(HAVE_GETRANDOM) |
| 93 | |
| 94 | #include <errno.h> |
| 95 | |
| 96 | int platform_entropy_poll( void *data, |
| 97 | unsigned char *output, size_t len, size_t *olen ) |
| 98 | { |
| 99 | int ret; |
| 100 | ((void) data); |
| 101 | |
| 102 | if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) |
| 103 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
| 104 | |
| 105 | *olen = ret; |
| 106 | return( 0 ); |
| 107 | } |
| 108 | |
| 109 | #else /* HAVE_GETRANDOM */ |
| 110 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 111 | #include <stdio.h> |
| 112 | |
| 113 | int platform_entropy_poll( void *data, |
| 114 | unsigned char *output, size_t len, size_t *olen ) |
| 115 | { |
| 116 | FILE *file; |
| 117 | size_t ret; |
| 118 | ((void) data); |
| 119 | |
| 120 | *olen = 0; |
| 121 | |
| 122 | file = fopen( "/dev/urandom", "rb" ); |
| 123 | if( file == NULL ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 124 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 125 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 126 | ret = fread( output, 1, len, file ); |
| 127 | if( ret != len ) |
| 128 | { |
| 129 | fclose( file ); |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 130 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | fclose( file ); |
| 134 | *olen = len; |
| 135 | |
| 136 | return( 0 ); |
| 137 | } |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 138 | #endif /* HAVE_GETRANDOM */ |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 139 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
| 140 | #endif /* !POLARSSL_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 141 | |
| 142 | #if defined(POLARSSL_TIMING_C) |
| 143 | int hardclock_poll( void *data, |
| 144 | unsigned char *output, size_t len, size_t *olen ) |
| 145 | { |
| 146 | unsigned long timer = hardclock(); |
| 147 | ((void) data); |
| 148 | *olen = 0; |
| 149 | |
| 150 | if( len < sizeof(unsigned long) ) |
| 151 | return( 0 ); |
| 152 | |
| 153 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 154 | *olen = sizeof(unsigned long); |
| 155 | |
| 156 | return( 0 ); |
| 157 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 158 | #endif /* POLARSSL_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 159 | |
| 160 | #if defined(POLARSSL_HAVEGE_C) |
| 161 | int havege_poll( void *data, |
| 162 | unsigned char *output, size_t len, size_t *olen ) |
| 163 | { |
| 164 | havege_state *hs = (havege_state *) data; |
| 165 | *olen = 0; |
| 166 | |
| 167 | if( havege_random( hs, output, len ) != 0 ) |
Paul Bakker | d8bb826 | 2014-06-17 14:06:49 +0200 | [diff] [blame] | 168 | return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 169 | |
| 170 | *olen = len; |
| 171 | |
| 172 | return( 0 ); |
| 173 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 174 | #endif /* POLARSSL_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 175 | |
| 176 | #endif /* POLARSSL_ENTROPY_C */ |