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