Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
entropy_poll.c
00001 /* 00002 * Platform-specific and custom entropy polling functions 00003 * 00004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved 00005 * SPDX-License-Identifier: Apache-2.0 00006 * 00007 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00008 * not use this file except in compliance with the License. 00009 * You may obtain a copy of the License at 00010 * 00011 * http://www.apache.org/licenses/LICENSE-2.0 00012 * 00013 * Unless required by applicable law or agreed to in writing, software 00014 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00015 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00016 * See the License for the specific language governing permissions and 00017 * limitations under the License. 00018 * 00019 * This file is part of mbed TLS (https://tls.mbed.org) 00020 */ 00021 00022 #if defined(__linux__) 00023 /* Ensure that syscall() is available even when compiling with -std=c99 */ 00024 #define _GNU_SOURCE 00025 #endif 00026 00027 #if !defined(MBEDTLS_CONFIG_FILE) 00028 #include "mbedtls/config.h" 00029 #else 00030 #include MBEDTLS_CONFIG_FILE 00031 #endif 00032 00033 #include <string.h> 00034 00035 #if defined(MBEDTLS_ENTROPY_C) 00036 00037 #include "mbedtls/entropy.h" 00038 #include "mbedtls/entropy_poll.h" 00039 00040 #if defined(MBEDTLS_TIMING_C) 00041 #include "mbedtls/timing.h" 00042 #endif 00043 #if defined(MBEDTLS_HAVEGE_C) 00044 #include "mbedtls/havege.h" 00045 #endif 00046 #if defined(MBEDTLS_ENTROPY_NV_SEED) 00047 #include "mbedtls/platform.h" 00048 #endif 00049 00050 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) 00051 00052 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ 00053 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ 00054 !defined(__HAIKU__) 00055 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" 00056 #endif 00057 00058 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) 00059 00060 #if !defined(_WIN32_WINNT) 00061 #define _WIN32_WINNT 0x0400 00062 #endif 00063 #include <windows.h> 00064 #include <wincrypt.h> 00065 00066 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len, 00067 size_t *olen ) 00068 { 00069 HCRYPTPROV provider; 00070 ((void) data); 00071 *olen = 0; 00072 00073 if( CryptAcquireContext( &provider, NULL, NULL, 00074 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) 00075 { 00076 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00077 } 00078 00079 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) 00080 { 00081 CryptReleaseContext( provider, 0 ); 00082 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00083 } 00084 00085 CryptReleaseContext( provider, 0 ); 00086 *olen = len; 00087 00088 return( 0 ); 00089 } 00090 #else /* _WIN32 && !EFIX64 && !EFI32 */ 00091 00092 /* 00093 * Test for Linux getrandom() support. 00094 * Since there is no wrapper in the libc yet, use the generic syscall wrapper 00095 * available in GNU libc and compatible libc's (eg uClibc). 00096 */ 00097 #if defined(__linux__) && defined(__GLIBC__) 00098 #include <unistd.h> 00099 #include <sys/syscall.h> 00100 #if defined(SYS_getrandom) 00101 #define HAVE_GETRANDOM 00102 00103 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) 00104 { 00105 /* MemSan cannot understand that the syscall writes to the buffer */ 00106 #if defined(__has_feature) 00107 #if __has_feature(memory_sanitizer) 00108 memset( buf, 0, buflen ); 00109 #endif 00110 #endif 00111 00112 return( syscall( SYS_getrandom, buf, buflen, flags ) ); 00113 } 00114 00115 #include <sys/utsname.h> 00116 /* Check if version is at least 3.17.0 */ 00117 static int check_version_3_17_plus( void ) 00118 { 00119 int minor; 00120 struct utsname un; 00121 const char *ver; 00122 00123 /* Get version information */ 00124 uname(&un); 00125 ver = un.release; 00126 00127 /* Check major version; assume a single digit */ 00128 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' ) 00129 return( -1 ); 00130 00131 if( ver[0] - '0' > 3 ) 00132 return( 0 ); 00133 00134 /* Ok, so now we know major == 3, check minor. 00135 * Assume 1 or 2 digits. */ 00136 if( ver[2] < '0' || ver[2] > '9' ) 00137 return( -1 ); 00138 00139 minor = ver[2] - '0'; 00140 00141 if( ver[3] >= '0' && ver[3] <= '9' ) 00142 minor = 10 * minor + ver[3] - '0'; 00143 else if( ver [3] != '.' ) 00144 return( -1 ); 00145 00146 if( minor < 17 ) 00147 return( -1 ); 00148 00149 return( 0 ); 00150 } 00151 static int has_getrandom = -1; 00152 #endif /* SYS_getrandom */ 00153 #endif /* __linux__ */ 00154 00155 #include <stdio.h> 00156 00157 int mbedtls_platform_entropy_poll( void *data, 00158 unsigned char *output, size_t len, size_t *olen ) 00159 { 00160 FILE *file; 00161 size_t read_len; 00162 ((void) data); 00163 00164 #if defined(HAVE_GETRANDOM) 00165 if( has_getrandom == -1 ) 00166 has_getrandom = ( check_version_3_17_plus() == 0 ); 00167 00168 if( has_getrandom ) 00169 { 00170 int ret; 00171 00172 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) 00173 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00174 00175 *olen = ret; 00176 return( 0 ); 00177 } 00178 #endif /* HAVE_GETRANDOM */ 00179 00180 *olen = 0; 00181 00182 file = fopen( "/dev/urandom", "rb" ); 00183 if( file == NULL ) 00184 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00185 00186 read_len = fread( output, 1, len, file ); 00187 if( read_len != len ) 00188 { 00189 fclose( file ); 00190 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00191 } 00192 00193 fclose( file ); 00194 *olen = len; 00195 00196 return( 0 ); 00197 } 00198 #endif /* _WIN32 && !EFIX64 && !EFI32 */ 00199 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ 00200 00201 #if defined(MBEDTLS_TEST_NULL_ENTROPY) 00202 int mbedtls_null_entropy_poll( void *data, 00203 unsigned char *output, size_t len, size_t *olen ) 00204 { 00205 ((void) data); 00206 ((void) output); 00207 *olen = 0; 00208 00209 if( len < sizeof(unsigned char) ) 00210 return( 0 ); 00211 00212 *olen = sizeof(unsigned char); 00213 00214 return( 0 ); 00215 } 00216 #endif 00217 00218 #if defined(MBEDTLS_TIMING_C) 00219 int mbedtls_hardclock_poll( void *data, 00220 unsigned char *output, size_t len, size_t *olen ) 00221 { 00222 unsigned long timer = mbedtls_timing_hardclock(); 00223 ((void) data); 00224 *olen = 0; 00225 00226 if( len < sizeof(unsigned long) ) 00227 return( 0 ); 00228 00229 memcpy( output, &timer, sizeof(unsigned long) ); 00230 *olen = sizeof(unsigned long); 00231 00232 return( 0 ); 00233 } 00234 #endif /* MBEDTLS_TIMING_C */ 00235 00236 #if defined(MBEDTLS_HAVEGE_C) 00237 int mbedtls_havege_poll( void *data, 00238 unsigned char *output, size_t len, size_t *olen ) 00239 { 00240 mbedtls_havege_state *hs = (mbedtls_havege_state *) data; 00241 *olen = 0; 00242 00243 if( mbedtls_havege_random( hs, output, len ) != 0 ) 00244 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00245 00246 *olen = len; 00247 00248 return( 0 ); 00249 } 00250 #endif /* MBEDTLS_HAVEGE_C */ 00251 00252 #if defined(MBEDTLS_ENTROPY_NV_SEED) 00253 int mbedtls_nv_seed_poll( void *data, 00254 unsigned char *output, size_t len, size_t *olen ) 00255 { 00256 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; 00257 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; 00258 ((void) data); 00259 00260 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); 00261 00262 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) 00263 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); 00264 00265 if( len < use_len ) 00266 use_len = len; 00267 00268 memcpy( output, buf, use_len ); 00269 *olen = use_len; 00270 00271 return( 0 ); 00272 } 00273 #endif /* MBEDTLS_ENTROPY_NV_SEED */ 00274 00275 #endif /* MBEDTLS_ENTROPY_C */
Generated on Tue Aug 9 2022 00:37:06 by
1.7.2