ReactOS 0.4.15-dev-8348-gc1b9bb5
entropy_poll.c
Go to the documentation of this file.
1/*
2 * Platform-specific and custom entropy polling functions
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 *
25 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
45 */
46
47#if defined(__linux__) && !defined(_GNU_SOURCE)
48/* Ensure that syscall() is available even when compiling with -std=c99 */
49#define _GNU_SOURCE
50#endif
51
52#if !defined(MBEDTLS_CONFIG_FILE)
53#include "mbedtls/config.h"
54#else
55#include MBEDTLS_CONFIG_FILE
56#endif
57
58#include <string.h>
59
60#if defined(MBEDTLS_ENTROPY_C)
61
62#include "mbedtls/entropy.h"
64
65#if defined(MBEDTLS_TIMING_C)
66#include "mbedtls/timing.h"
67#endif
68#if defined(MBEDTLS_HAVEGE_C)
69#include "mbedtls/havege.h"
70#endif
71#if defined(MBEDTLS_ENTROPY_NV_SEED)
72#include "mbedtls/platform.h"
73#endif
74
75#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
76
77#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
78 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
79 !defined(__HAIKU__)
80#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
81#endif
82
83#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
84
85#if !defined(_WIN32_WINNT)
86#define _WIN32_WINNT 0x0400
87#endif
88#include <windows.h>
89#include <wincrypt.h>
90
91int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
92 size_t *olen )
93{
94 HCRYPTPROV provider;
95 ((void) data);
96 *olen = 0;
97
98 if( CryptAcquireContext( &provider, NULL, NULL,
100 {
102 }
103
104 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
105 {
106 CryptReleaseContext( provider, 0 );
108 }
109
110 CryptReleaseContext( provider, 0 );
111 *olen = len;
112
113 return( 0 );
114}
115#else /* _WIN32 && !EFIX64 && !EFI32 */
116
117/*
118 * Test for Linux getrandom() support.
119 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
120 * available in GNU libc and compatible libc's (eg uClibc).
121 */
122#if defined(__linux__) && defined(__GLIBC__)
123#include <unistd.h>
124#include <sys/syscall.h>
125#if defined(SYS_getrandom)
126#define HAVE_GETRANDOM
127#include <errno.h>
128
129static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
130{
131 /* MemSan cannot understand that the syscall writes to the buffer */
132#if defined(__has_feature)
133#if __has_feature(memory_sanitizer)
134 memset( buf, 0, buflen );
135#endif
136#endif
137 return( syscall( SYS_getrandom, buf, buflen, flags ) );
138}
139#endif /* SYS_getrandom */
140#endif /* __linux__ */
141
142#include <stdio.h>
143
145 unsigned char *output, size_t len, size_t *olen )
146{
147 FILE *file;
148 size_t read_len;
149 int ret;
150 ((void) data);
151
152#if defined(HAVE_GETRANDOM)
153 ret = getrandom_wrapper( output, len, 0 );
154 if( ret >= 0 )
155 {
156 *olen = ret;
157 return( 0 );
158 }
159 else if( errno != ENOSYS )
161 /* Fall through if the system call isn't known. */
162#else
163 ((void) ret);
164#endif /* HAVE_GETRANDOM */
165
166 *olen = 0;
167
168 file = fopen( "/dev/urandom", "rb" );
169 if( file == NULL )
171
172 read_len = fread( output, 1, len, file );
173 if( read_len != len )
174 {
175 fclose( file );
177 }
178
179 fclose( file );
180 *olen = len;
181
182 return( 0 );
183}
184#endif /* _WIN32 && !EFIX64 && !EFI32 */
185#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
186
187#if defined(MBEDTLS_TEST_NULL_ENTROPY)
188int mbedtls_null_entropy_poll( void *data,
189 unsigned char *output, size_t len, size_t *olen )
190{
191 ((void) data);
192 ((void) output);
193 *olen = 0;
194
195 if( len < sizeof(unsigned char) )
196 return( 0 );
197
198 *olen = sizeof(unsigned char);
199
200 return( 0 );
201}
202#endif
203
204#if defined(MBEDTLS_TIMING_C)
206 unsigned char *output, size_t len, size_t *olen )
207{
208 unsigned long timer = mbedtls_timing_hardclock();
209 ((void) data);
210 *olen = 0;
211
212 if( len < sizeof(unsigned long) )
213 return( 0 );
214
215 memcpy( output, &timer, sizeof(unsigned long) );
216 *olen = sizeof(unsigned long);
217
218 return( 0 );
219}
220#endif /* MBEDTLS_TIMING_C */
221
222#if defined(MBEDTLS_HAVEGE_C)
223int mbedtls_havege_poll( void *data,
224 unsigned char *output, size_t len, size_t *olen )
225{
227 *olen = 0;
228
229 if( mbedtls_havege_random( hs, output, len ) != 0 )
231
232 *olen = len;
233
234 return( 0 );
235}
236#endif /* MBEDTLS_HAVEGE_C */
237
238#if defined(MBEDTLS_ENTROPY_NV_SEED)
239int mbedtls_nv_seed_poll( void *data,
240 unsigned char *output, size_t len, size_t *olen )
241{
242 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
243 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
244 ((void) data);
245
247
248 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
250
251 if( len < use_len )
252 use_len = len;
253
254 memcpy( output, buf, use_len );
255 *olen = use_len;
256
257 return( 0 );
258}
259#endif /* MBEDTLS_ENTROPY_NV_SEED */
260
261#endif /* MBEDTLS_ENTROPY_C */
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
BOOL WINAPI CryptGenRandom(HCRYPTPROV hProv, DWORD dwLen, BYTE *pbBuffer)
Definition: crypt.c:700
BOOL WINAPI CryptReleaseContext(HCRYPTPROV hProv, DWORD dwFlags)
Definition: crypt.c:648
unsigned char
Definition: typeof.h:29
Entropy accumulator implementation.
#define MBEDTLS_ENTROPY_BLOCK_SIZE
Definition: entropy.h:105
#define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
Definition: entropy.h:78
Platform-specific and custom entropy polling functions.
int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback that provides 0 entropy.
int mbedtls_hardclock_poll(void *data, unsigned char *output, size_t len, size_t *olen)
mbedtls_timing_hardclock-based entropy poll callback
unsigned long DWORD
Definition: ntddk_ex.h:95
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
GLbitfield flags
Definition: glext.h:7161
GLenum GLsizei len
Definition: glext.h:6722
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
int mbedtls_havege_random(void *p_rng, unsigned char *output, size_t len)
HAVEGE rand function.
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define long
Definition: qsort.c:33
#define ENOSYS
Definition: errno.h:57
#define errno
Definition: errno.h:18
Configuration options (set of defines)
This file contains the definitions and functions of the Mbed TLS platform abstraction layer.
#define memset(x, y, z)
Definition: compat.h:39
Definition: fci.c:127
HAVEGE state structure.
Definition: havege.h:70
Portable interface to timeouts and to the CPU cycle counter.
unsigned long mbedtls_timing_hardclock(void)
Return the CPU cycle counter value.
int ret
#define PROV_RSA_FULL
Definition: wincrypt.h:2039
#define CRYPT_VERIFYCONTEXT
Definition: wincrypt.h:2069
ULONG_PTR HCRYPTPROV
Definition: wincrypt.h:46
#define CryptAcquireContext
Definition: wincrypt.h:4164