2 * Platform-specific and custom entropy polling functions
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if defined(__linux__)
23 /* Ensure that syscall() is available even when compiling with -std=c99 */
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "mbedtls/config.h"
30 #include MBEDTLS_CONFIG_FILE
35 #if defined(MBEDTLS_ENTROPY_C)
37 #include "mbedtls/entropy.h"
38 #include "mbedtls/entropy_poll.h"
40 #if defined(MBEDTLS_TIMING_C)
41 #include "mbedtls/timing.h"
43 #if defined(MBEDTLS_HAVEGE_C)
44 #include "mbedtls/havege.h"
46 #if defined(MBEDTLS_ENTROPY_NV_SEED)
47 #include "mbedtls/platform.h"
50 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
52 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
53 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
55 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
58 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
60 #if !defined(_WIN32_WINNT)
61 #define _WIN32_WINNT 0x0400
66 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
73 if( CryptAcquireContext( &provider, NULL, NULL,
74 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
76 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
79 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
81 CryptReleaseContext( provider, 0 );
82 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
85 CryptReleaseContext( provider, 0 );
90 #else /* _WIN32 && !EFIX64 && !EFI32 */
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).
97 #if defined(__linux__) && defined(__GLIBC__)
99 #include <sys/syscall.h>
100 #if defined(SYS_getrandom)
101 #define HAVE_GETRANDOM
104 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
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 );
112 return( syscall( SYS_getrandom, buf, buflen, flags ) );
114 #endif /* SYS_getrandom */
115 #endif /* __linux__ */
119 int mbedtls_platform_entropy_poll( void *data,
120 unsigned char *output, size_t len, size_t *olen )
127 #if defined(HAVE_GETRANDOM)
128 ret = getrandom_wrapper( output, len, 0 );
134 else if( errno != ENOSYS )
135 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
136 /* Fall through if the system call isn't known. */
139 #endif /* HAVE_GETRANDOM */
143 file = fopen( "/dev/urandom", "rb" );
145 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
147 read_len = fread( output, 1, len, file );
148 if( read_len != len )
151 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
159 #endif /* _WIN32 && !EFIX64 && !EFI32 */
160 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
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 )
170 if( len < sizeof(unsigned char) )
173 *olen = sizeof(unsigned char);
179 #if defined(MBEDTLS_TIMING_C)
180 int mbedtls_hardclock_poll( void *data,
181 unsigned char *output, size_t len, size_t *olen )
183 unsigned long timer = mbedtls_timing_hardclock();
187 if( len < sizeof(unsigned long) )
190 memcpy( output, &timer, sizeof(unsigned long) );
191 *olen = sizeof(unsigned long);
195 #endif /* MBEDTLS_TIMING_C */
197 #if defined(MBEDTLS_HAVEGE_C)
198 int mbedtls_havege_poll( void *data,
199 unsigned char *output, size_t len, size_t *olen )
201 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
204 if( mbedtls_havege_random( hs, output, len ) != 0 )
205 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
211 #endif /* MBEDTLS_HAVEGE_C */
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 )
217 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
218 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
221 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
223 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
224 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
229 memcpy( output, buf, use_len );
234 #endif /* MBEDTLS_ENTROPY_NV_SEED */
236 #endif /* MBEDTLS_ENTROPY_C */