2 * Platform-specific and custom entropy polling functions
4 * Copyright The Mbed TLS Contributors
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.
20 #if defined(__linux__) && !defined(_GNU_SOURCE)
21 /* Ensure that syscall() is available even when compiling with -std=c99 */
29 #if defined(MBEDTLS_ENTROPY_C)
31 #include "mbedtls/entropy.h"
32 #include "mbedtls/entropy_poll.h"
33 #include "mbedtls/error.h"
35 #if defined(MBEDTLS_TIMING_C)
36 #include "mbedtls/timing.h"
38 #if defined(MBEDTLS_HAVEGE_C)
39 #include "mbedtls/havege.h"
41 #if defined(MBEDTLS_ENTROPY_NV_SEED)
42 #include "mbedtls/platform.h"
45 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
47 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
48 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
49 !defined(__HAIKU__) && !defined(__midipix__)
50 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
53 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
55 #if !defined(_WIN32_WINNT)
56 #define _WIN32_WINNT 0x0400
61 int mbedtls_platform_entropy_poll(void *data
, unsigned char *output
, size_t len
,
67 if (CryptAcquireContext(&provider
, NULL
, NULL
,
68 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
) == FALSE
) {
69 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
72 if (CryptGenRandom(provider
, (DWORD
) len
, output
) == FALSE
) {
73 CryptReleaseContext(provider
, 0);
74 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
77 CryptReleaseContext(provider
, 0);
82 #else /* _WIN32 && !EFIX64 && !EFI32 */
85 * Test for Linux getrandom() support.
86 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
87 * available in GNU libc and compatible libc's (eg uClibc).
89 #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
91 #include <sys/syscall.h>
92 #if defined(SYS_getrandom)
93 #define HAVE_GETRANDOM
96 static int getrandom_wrapper(void *buf
, size_t buflen
, unsigned int flags
) {
97 /* MemSan cannot understand that the syscall writes to the buffer */
98 #if defined(__has_feature)
99 #if __has_feature(memory_sanitizer)
100 memset(buf
, 0, buflen
);
103 return (syscall(SYS_getrandom
, buf
, buflen
, flags
));
105 #endif /* SYS_getrandom */
106 #endif /* __linux__ || __midipix__ */
108 #if defined(__FreeBSD__) || defined(__DragonFly__)
109 #include <sys/param.h>
110 #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
111 (defined(__DragonFly__) && __DragonFly_version >= 500700)
113 #include <sys/random.h>
114 #define HAVE_GETRANDOM
115 static int getrandom_wrapper(void *buf
, size_t buflen
, unsigned int flags
) {
116 return getrandom(buf
, buflen
, flags
);
118 #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
119 (__DragonFly__ && __DragonFly_version >= 500700) */
120 #endif /* __FreeBSD__ || __DragonFly__ */
123 * Some BSD systems provide KERN_ARND.
124 * This is equivalent to reading from /dev/urandom, only it doesn't require an
125 * open file descriptor, and provides up to 256 bytes per call (basically the
126 * same as getentropy(), but with a longer history).
128 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
130 #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
131 #include <sys/param.h>
132 #include <sys/sysctl.h>
133 #if defined(KERN_ARND)
134 #define HAVE_SYSCTL_ARND
136 static int sysctl_arnd_wrapper(unsigned char *buf
, size_t buflen
) {
144 len
= buflen
> 256 ? 256 : buflen
;
145 if (sysctl(name
, 2, buf
, &len
, NULL
, 0) == -1)
152 #endif /* KERN_ARND */
153 #endif /* __FreeBSD__ || __NetBSD__ */
157 int mbedtls_platform_entropy_poll(void *data
,
158 unsigned char *output
, size_t len
, size_t *olen
) {
161 int ret
= MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED
;
164 #if defined(HAVE_GETRANDOM)
165 ret
= getrandom_wrapper(output
, len
, 0);
169 } else if (errno
!= ENOSYS
)
170 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
171 /* Fall through if the system call isn't known. */
174 #endif /* HAVE_GETRANDOM */
176 #if defined(HAVE_SYSCTL_ARND)
179 if (sysctl_arnd_wrapper(output
, len
) == -1)
180 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
187 file
= fopen("/dev/urandom", "rb");
189 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
191 read_len
= fread(output
, 1, len
, file
);
192 if (read_len
!= len
) {
194 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
201 #endif /* HAVE_SYSCTL_ARND */
203 #endif /* _WIN32 && !EFIX64 && !EFI32 */
204 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
206 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
207 int mbedtls_null_entropy_poll(void *data
,
208 unsigned char *output
, size_t len
, size_t *olen
) {
213 if (len
< sizeof(unsigned char))
217 *olen
= sizeof(unsigned char);
222 #if defined(MBEDTLS_TIMING_C)
223 int mbedtls_hardclock_poll(void *data
,
224 unsigned char *output
, size_t len
, size_t *olen
) {
225 unsigned long timer
= mbedtls_timing_hardclock();
229 if (len
< sizeof(unsigned long))
232 memcpy(output
, &timer
, sizeof(unsigned long));
233 *olen
= sizeof(unsigned long);
237 #endif /* MBEDTLS_TIMING_C */
239 #if defined(MBEDTLS_HAVEGE_C)
240 int mbedtls_havege_poll(void *data
,
241 unsigned char *output
, size_t len
, size_t *olen
) {
242 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) data
;
245 if (mbedtls_havege_random(hs
, output
, len
) != 0)
246 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
252 #endif /* MBEDTLS_HAVEGE_C */
254 #if defined(MBEDTLS_ENTROPY_NV_SEED)
255 int mbedtls_nv_seed_poll(void *data
,
256 unsigned char *output
, size_t len
, size_t *olen
) {
257 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
258 size_t use_len
= MBEDTLS_ENTROPY_BLOCK_SIZE
;
261 memset(buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
263 if (mbedtls_nv_seed_read(buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) < 0)
264 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
269 memcpy(output
, buf
, use_len
);
274 #endif /* MBEDTLS_ENTROPY_NV_SEED */
276 #endif /* MBEDTLS_ENTROPY_C */