1
#pragma clang diagnostic ignored "-Wunknown-warning-option"
2
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
3
#pragma clang diagnostic ignored "-Wsign-conversion"
5
#pragma GCC diagnostic ignored "-Wconversion"
7
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
8
#pragma clang diagnostic ignored "-Wsign-conversion"
10
#pragma GCC diagnostic ignored "-Wconversion"
12
#pragma clang diagnostic ignored "-Wimplicit-int-conversion"
13
#pragma clang diagnostic ignored "-Wsign-conversion"
15
* Platform-specific and custom entropy polling functions
17
* Copyright The Mbed TLS Contributors
18
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
20
* This file is provided under the Apache License 2.0, or the
21
* GNU General Public License v2.0 or later.
26
* Licensed under the Apache License, Version 2.0 (the "License"); you may
27
* not use this file except in compliance with the License.
28
* You may obtain a copy of the License at
30
* http://www.apache.org/licenses/LICENSE-2.0
32
* Unless required by applicable law or agreed to in writing, software
33
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35
* See the License for the specific language governing permissions and
36
* limitations under the License.
41
* GNU General Public License v2.0 or later:
43
* This program is free software; you can redistribute it and/or modify
44
* it under the terms of the GNU General Public License as published by
45
* the Free Software Foundation; either version 2 of the License, or
46
* (at your option) any later version.
48
* This program is distributed in the hope that it will be useful,
49
* but WITHOUT ANY WARRANTY; without even the implied warranty of
50
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51
* GNU General Public License for more details.
53
* You should have received a copy of the GNU General Public License along
54
* with this program; if not, write to the Free Software Foundation, Inc.,
55
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
60
#if defined(__linux__) && !defined(_GNU_SOURCE)
61
/* Ensure that syscall() is available even when compiling with -std=c99 */
65
#if !defined(MBEDTLS_CONFIG_FILE)
66
#include "mbedtls/config.h"
68
#include MBEDTLS_CONFIG_FILE
73
#if defined(MBEDTLS_ENTROPY_C)
75
#include "mbedtls/entropy.h"
76
#include "mbedtls/entropy_poll.h"
78
#if defined(MBEDTLS_TIMING_C)
79
#include "mbedtls/timing.h"
81
#if defined(MBEDTLS_HAVEGE_C)
82
#include "mbedtls/havege.h"
84
#if defined(MBEDTLS_ENTROPY_NV_SEED)
85
#include "mbedtls/platform.h"
88
#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
90
#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
91
!defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
93
#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
96
#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
98
#if !defined(_WIN32_WINNT)
99
#define _WIN32_WINNT 0x0400
102
#include <wincrypt.h>
104
int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
111
if( CryptAcquireContext( &provider, NULL, NULL,
112
PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
114
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
117
if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
119
CryptReleaseContext( provider, 0 );
120
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
123
CryptReleaseContext( provider, 0 );
128
#else /* _WIN32 && !EFIX64 && !EFI32 */
131
* Test for Linux getrandom() support.
132
* Since there is no wrapper in the libc yet, use the generic syscall wrapper
133
* available in GNU libc and compatible libc's (eg uClibc).
135
#if defined(__linux__) && defined(__GLIBC__)
137
#include <sys/syscall.h>
138
#if defined(SYS_getrandom)
139
#define HAVE_GETRANDOM
142
static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
144
/* MemSan cannot understand that the syscall writes to the buffer */
145
#if defined(__has_feature)
146
#if __has_feature(memory_sanitizer)
147
memset( buf, 0, buflen );
150
return( syscall( SYS_getrandom, buf, buflen, flags ) );
152
#endif /* SYS_getrandom */
153
#endif /* __linux__ */
157
int mbedtls_platform_entropy_poll( void *data,
158
unsigned char *output, size_t len, size_t *olen )
165
#if defined(HAVE_GETRANDOM)
166
ret = getrandom_wrapper( output, len, 0 );
172
else if( errno != ENOSYS )
173
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
174
/* Fall through if the system call isn't known. */
177
#endif /* HAVE_GETRANDOM */
181
file = fopen( "/dev/urandom", "rb" );
183
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
185
read_len = fread( output, 1, len, file );
186
if( read_len != len )
189
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
197
#endif /* _WIN32 && !EFIX64 && !EFI32 */
198
#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
200
#if defined(MBEDTLS_TEST_NULL_ENTROPY)
201
int mbedtls_null_entropy_poll( void *data,
202
unsigned char *output, size_t len, size_t *olen )
208
if( len < sizeof(unsigned char) )
211
*olen = sizeof(unsigned char);
217
#if defined(MBEDTLS_TIMING_C)
218
int mbedtls_hardclock_poll( void *data,
219
unsigned char *output, size_t len, size_t *olen )
221
unsigned long timer = mbedtls_timing_hardclock();
225
if( len < sizeof(unsigned long) )
228
memcpy( output, &timer, sizeof(unsigned long) );
229
*olen = sizeof(unsigned long);
233
#endif /* MBEDTLS_TIMING_C */
235
#if defined(MBEDTLS_HAVEGE_C)
236
int mbedtls_havege_poll( void *data,
237
unsigned char *output, size_t len, size_t *olen )
239
mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
242
if( mbedtls_havege_random( hs, output, len ) != 0 )
243
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
249
#endif /* MBEDTLS_HAVEGE_C */
251
#if defined(MBEDTLS_ENTROPY_NV_SEED)
252
int mbedtls_nv_seed_poll( void *data,
253
unsigned char *output, size_t len, size_t *olen )
255
unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
256
size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
259
memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
261
if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
262
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
267
memcpy( output, buf, use_len );
272
#endif /* MBEDTLS_ENTROPY_NV_SEED */
274
#endif /* MBEDTLS_ENTROPY_C */