Merge pull request #2735 from jareckib/master
[RRG-proxmark3.git] / common / mbedtls / entropy_poll.c
blobb1c0a326f88f086a79ed3ada8ac177b81eb74874
1 /*
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 */
22 #define _GNU_SOURCE
23 #endif
25 #include "common.h"
27 #include <string.h>
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"
37 #endif
38 #if defined(MBEDTLS_HAVEGE_C)
39 #include "mbedtls/havege.h"
40 #endif
41 #if defined(MBEDTLS_ENTROPY_NV_SEED)
42 #include "mbedtls/platform.h"
43 #endif
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"
51 #endif
53 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
55 #if !defined(_WIN32_WINNT)
56 #define _WIN32_WINNT 0x0400
57 #endif
58 #include <windows.h>
59 #include <wincrypt.h>
61 int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
62 size_t *olen) {
63 HCRYPTPROV provider;
64 ((void) data);
65 *olen = 0;
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);
78 *olen = len;
80 return (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__))
90 #include <unistd.h>
91 #include <sys/syscall.h>
92 #if defined(SYS_getrandom)
93 #define HAVE_GETRANDOM
94 #include <errno.h>
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);
101 #endif
102 #endif
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)
112 #include <errno.h>
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) {
137 int name[2];
138 size_t len;
140 name[0] = CTL_KERN;
141 name[1] = KERN_ARND;
143 while (buflen > 0) {
144 len = buflen > 256 ? 256 : buflen;
145 if (sysctl(name, 2, buf, &len, NULL, 0) == -1)
146 return (-1);
147 buflen -= len;
148 buf += len;
150 return (0);
152 #endif /* KERN_ARND */
153 #endif /* __FreeBSD__ || __NetBSD__ */
155 #include <stdio.h>
157 int mbedtls_platform_entropy_poll(void *data,
158 unsigned char *output, size_t len, size_t *olen) {
159 FILE *file;
160 size_t read_len;
161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
162 ((void) data);
164 #if defined(HAVE_GETRANDOM)
165 ret = getrandom_wrapper(output, len, 0);
166 if (ret >= 0) {
167 *olen = ret;
168 return (0);
169 } else if (errno != ENOSYS)
170 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
171 /* Fall through if the system call isn't known. */
172 #else
173 ((void) ret);
174 #endif /* HAVE_GETRANDOM */
176 #if defined(HAVE_SYSCTL_ARND)
177 ((void) file);
178 ((void) read_len);
179 if (sysctl_arnd_wrapper(output, len) == -1)
180 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
181 *olen = len;
182 return (0);
183 #else
185 *olen = 0;
187 file = fopen("/dev/urandom", "rb");
188 if (file == NULL)
189 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
191 read_len = fread(output, 1, len, file);
192 if (read_len != len) {
193 fclose(file);
194 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
197 fclose(file);
198 *olen = len;
200 return (0);
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) {
209 ((void) data);
210 ((void) output);
212 *olen = 0;
213 if (len < sizeof(unsigned char))
214 return (0);
216 output[0] = 0;
217 *olen = sizeof(unsigned char);
218 return (0);
220 #endif
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();
226 ((void) data);
227 *olen = 0;
229 if (len < sizeof(unsigned long))
230 return (0);
232 memcpy(output, &timer, sizeof(unsigned long));
233 *olen = sizeof(unsigned long);
235 return (0);
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;
243 *olen = 0;
245 if (mbedtls_havege_random(hs, output, len) != 0)
246 return (MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
248 *olen = len;
250 return (0);
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;
259 ((void) data);
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);
266 if (len < use_len)
267 use_len = len;
269 memcpy(output, buf, use_len);
270 *olen = use_len;
272 return (0);
274 #endif /* MBEDTLS_ENTROPY_NV_SEED */
276 #endif /* MBEDTLS_ENTROPY_C */