1 /* 2 * Platform-specific and custom entropy polling functions 3 * 4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 5 * SPDX-License-Identifier: GPL-2.0 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program; if not, write to the Free Software Foundation, Inc., 19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * This file is part of mbed TLS (https://tls.mbed.org) 22 */ 23 24 #if !defined(MBEDTLS_CONFIG_FILE) 25 #include "mbedtls/config.h" 26 #else 27 #include MBEDTLS_CONFIG_FILE 28 #endif 29 30 #if defined(MBEDTLS_ENTROPY_C) 31 32 #include "mbedtls/entropy.h" 33 #include "mbedtls/entropy_poll.h" 34 35 #if defined(MBEDTLS_TIMING_C) 36 #include <string.h> 37 #include "mbedtls/timing.h" 38 #endif 39 #if defined(MBEDTLS_HAVEGE_C) 40 #include "mbedtls/havege.h" 41 #endif 42 #if defined(MBEDTLS_ENTROPY_NV_SEED) 43 #include "mbedtls/platform.h" 44 #endif 45 46 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 47 48 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ 49 !defined(__APPLE__) && !defined(_WIN32) 50 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" 51 #endif 52 53 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 54 55 #if !defined(_WIN32_WINNT) 56 #define _WIN32_WINNT 0x0400 57 #endif 58 #include <windows.h> 59 #include <wincrypt.h> 60 61 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len, 62 size_t *olen ) 63 { 64 HCRYPTPROV provider; 65 ((void) data); 66 *olen = 0; 67 68 if( CryptAcquireContext( &provider, NULL, NULL, 69 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) 70 { 71 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 72 } 73 74 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) 75 { 76 CryptReleaseContext( provider, 0 ); 77 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 78 } 79 80 CryptReleaseContext( provider, 0 ); 81 *olen = len; 82 83 return( 0 ); 84 } 85 #else /* _WIN32 && !EFIX64 && !EFI32 */ 86 87 /* 88 * Test for Linux getrandom() support. 89 * Since there is no wrapper in the libc yet, use the generic syscall wrapper 90 * available in GNU libc and compatible libc's (eg uClibc). 91 */ 92 #if defined(__linux__) && defined(__GLIBC__) 93 #include <unistd.h> 94 #include <sys/syscall.h> 95 #if defined(SYS_getrandom) 96 #define HAVE_GETRANDOM 97 #include <errno.h> 98 99 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) 100 { 101 /* MemSan cannot understand that the syscall writes to the buffer */ 102 #if defined(__has_feature) 103 #if __has_feature(memory_sanitizer) 104 memset( buf, 0, buflen ); 105 #endif 106 #endif 107 return( syscall( SYS_getrandom, buf, buflen, flags ) ); 108 } 109 #endif /* SYS_getrandom */ 110 #endif /* __linux__ */ 111 112 #include <stdio.h> 113 114 int mbedtls_platform_entropy_poll( void *data, 115 unsigned char *output, size_t len, size_t *olen ) 116 { 117 FILE *file; 118 size_t read_len; 119 int ret; 120 ((void) data); 121 122 #if defined(HAVE_GETRANDOM) 123 ret = getrandom_wrapper( output, len, 0 ); 124 if( ret >= 0 ) 125 { 126 *olen = ret; 127 return( 0 ); 128 } 129 else if( errno != ENOSYS ) 130 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 131 /* Fall through if the system call isn't known. */ 132 #else 133 ((void) ret); 134 #endif /* HAVE_GETRANDOM */ 135 136 *olen = 0; 137 138 file = fopen( "/dev/urandom", "rb" ); 139 if( file == NULL ) 140 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 141 142 read_len = fread( output, 1, len, file ); 143 if( read_len != len ) 144 { 145 fclose( file ); 146 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 147 } 148 149 fclose( file ); 150 *olen = len; 151 152 return( 0 ); 153 } 154 #endif /* _WIN32 && !EFIX64 && !EFI32 */ 155 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ 156 157 #if defined(MBEDTLS_TEST_NULL_ENTROPY) 158 int mbedtls_null_entropy_poll( void *data, 159 unsigned char *output, size_t len, size_t *olen ) 160 { 161 ((void) data); 162 ((void) output); 163 *olen = 0; 164 165 if( len < sizeof(unsigned char) ) 166 return( 0 ); 167 168 *olen = sizeof(unsigned char); 169 170 return( 0 ); 171 } 172 #endif 173 174 #if defined(MBEDTLS_TIMING_C) 175 int mbedtls_hardclock_poll( void *data, 176 unsigned char *output, size_t len, size_t *olen ) 177 { 178 unsigned long timer = mbedtls_timing_hardclock(); 179 ((void) data); 180 *olen = 0; 181 182 if( len < sizeof(unsigned long) ) 183 return( 0 ); 184 185 memcpy( output, &timer, sizeof(unsigned long) ); 186 *olen = sizeof(unsigned long); 187 188 return( 0 ); 189 } 190 #endif /* MBEDTLS_TIMING_C */ 191 192 #if defined(MBEDTLS_HAVEGE_C) 193 int mbedtls_havege_poll( void *data, 194 unsigned char *output, size_t len, size_t *olen ) 195 { 196 mbedtls_havege_state *hs = (mbedtls_havege_state *) data; 197 *olen = 0; 198 199 if( mbedtls_havege_random( hs, output, len ) != 0 ) 200 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 201 202 *olen = len; 203 204 return( 0 ); 205 } 206 #endif /* MBEDTLS_HAVEGE_C */ 207 208 #if defined(MBEDTLS_ENTROPY_NV_SEED) 209 int mbedtls_nv_seed_poll( void *data, 210 unsigned char *output, size_t len, size_t *olen ) 211 { 212 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; 213 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; 214 ((void) data); 215 216 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); 217 218 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) 219 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 220 221 if( len < use_len ) 222 use_len = len; 223 224 memcpy( output, buf, use_len ); 225 *olen = use_len; 226 227 return( 0 ); 228 } 229 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 230 231 #endif /* MBEDTLS_ENTROPY_C */ 232