blob: e5cf97034bc0f5a4f8cc68dc7520f5f6c7bf7afc [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
Paul Bakker6083fd22011-12-03 21:45:14 +000018 */
19
Gilles Peskinea1f9ef02020-09-30 22:18:13 +020020#if defined(__linux__) && !defined(_GNU_SOURCE)
Nicholas Wilson2682edf2017-12-05 12:08:15 +000021/* Ensure that syscall() is available even when compiling with -std=c99 */
22#define _GNU_SOURCE
23#endif
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000026
Gilles Peskine02b93292018-06-01 14:38:45 +020027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/entropy.h"
Chris Jonesea0a8652021-03-09 19:11:19 +000032#include "entropy_poll.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000037#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010038#include "mbedtls/platform.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010041
42#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040043 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000044 !defined(__HAIKU__) && !defined(__midipix__)
Gilles Peskine449bd832023-01-11 14:50:10 +010045#error \
46 "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in mbedtls_config.h"
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010047#endif
48
Paul Bakkerfa6a6202013-10-28 18:48:30 +010049#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000050
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000051#include <windows.h>
Steve Lhomme954553f2023-06-16 13:41:48 +020052#if _WIN32_WINNT >= 0x0501 /* _WIN32_WINNT_WINXP */
Kevin Kane0ec1e682016-12-15 09:27:16 -080053#include <bcrypt.h>
Simon Butchere068aa72018-03-14 15:10:31 +000054#if defined(_MSC_VER) && _MSC_VER <= 1600
Minos Galanakis12b493f2023-08-11 15:22:45 +010055#define MBEDTLS_POP_TARGET_PRAGMA
56#endif
57#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Simon Butcherdef90f42018-03-14 17:02:16 +000058/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
59 * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
60 * These constants are guaranteed to be the same, though, so we suppress the
61 * warning when including intsafe.h.
Kevin Kane0ec1e682016-12-15 09:27:16 -080062 */
Minos Galanakisa277b212023-08-09 16:32:22 +010063#pragma warning(push)
64#pragma warning(disable : 4005)
Kevin Kane0ec1e682016-12-15 09:27:16 -080065#endif
66#include <intsafe.h>
Minos Galanakis12b493f2023-08-11 15:22:45 +010067#if defined(MBEDTLS_POP_TARGET_PRAGMA)
Minos Galanakisa277b212023-08-09 16:32:22 +010068#pragma warning(pop)
Kevin Kane0ec1e682016-12-15 09:27:16 -080069#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000070
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
72 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +000073{
Kevin Kane0ec1e682016-12-15 09:27:16 -080074 ULONG len_as_ulong = 0;
Paul Bakker6bcfc672011-12-05 13:54:00 +000075 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000076 *olen = 0;
77
Kevin Kane0ec1e682016-12-15 09:27:16 -080078 /*
Simon Butcherdef90f42018-03-14 17:02:16 +000079 * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
80 * 64-bit Windows platforms. Ensure len's value can be safely converted into
81 * a ULONG.
Kevin Kane0ec1e682016-12-15 09:27:16 -080082 */
Minos Galanakisa277b212023-08-09 16:32:22 +010083 if (FAILED(SizeTToULong(len, &len_as_ulong))) {
84 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +000085 }
86
Minos Galanakisa277b212023-08-09 16:32:22 +010087 if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, len_as_ulong,
88 BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
89 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Attila Molnar2791ba12016-01-26 11:39:26 +010090 }
Paul Bakker6083fd22011-12-03 21:45:14 +000091
Paul Bakker6083fd22011-12-03 21:45:14 +000092 *olen = len;
93
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +000095}
Steve Lhomme954553f2023-06-16 13:41:48 +020096#else /* !_WIN32_WINNT_WINXP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +010097#error "Entropy not available before Windows XP, use MBEDTLS_NO_PLATFORM_ENTROPY"
Steve Lhomme954553f2023-06-16 13:41:48 +020098#endif /* !_WIN32_WINNT_WINXP */
Paul Bakker9af723c2014-05-01 13:03:14 +020099#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +0000100
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100101/*
102 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +0200103 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100104 * available in GNU libc and compatible libc's (eg uClibc).
105 */
Gilles Peskinec6468ee2020-09-30 22:11:13 +0200106#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100107#include <unistd.h>
108#include <sys/syscall.h>
109#if defined(SYS_getrandom)
110#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100111#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100114{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200115 /* MemSan cannot understand that the syscall writes to the buffer */
116#if defined(__has_feature)
117#if __has_feature(memory_sanitizer)
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 memset(buf, 0, buflen);
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200119#endif
120#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return syscall(SYS_getrandom, buf, buflen, flags);
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100122}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200123#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000124#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100125
David Carlier2b8c2652020-12-28 15:51:14 +0000126#if defined(__FreeBSD__) || defined(__DragonFly__)
127#include <sys/param.h>
128#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
129 (defined(__DragonFly__) && __DragonFly_version >= 500700)
130#include <errno.h>
131#include <sys/random.h>
132#define HAVE_GETRANDOM
Gilles Peskine449bd832023-01-11 14:50:10 +0100133static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
David Carlier2b8c2652020-12-28 15:51:14 +0000134{
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 return getrandom(buf, buflen, flags);
David Carlier2b8c2652020-12-28 15:51:14 +0000136}
137#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
138 (__DragonFly__ && __DragonFly_version >= 500700) */
139#endif /* __FreeBSD__ || __DragonFly__ */
140
nia9f5312c2020-06-11 13:32:13 +0100141/*
142 * Some BSD systems provide KERN_ARND.
143 * This is equivalent to reading from /dev/urandom, only it doesn't require an
144 * open file descriptor, and provides up to 256 bytes per call (basically the
145 * same as getentropy(), but with a longer history).
146 *
147 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
148 */
149#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
150#include <sys/param.h>
151#include <sys/sysctl.h>
152#if defined(KERN_ARND)
153#define HAVE_SYSCTL_ARND
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
nia9f5312c2020-06-11 13:32:13 +0100156{
157 int name[2];
158 size_t len;
159
160 name[0] = CTL_KERN;
161 name[1] = KERN_ARND;
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 while (buflen > 0) {
nia9f5312c2020-06-11 13:32:13 +0100164 len = buflen > 256 ? 256 : buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
166 return -1;
167 }
nia9f5312c2020-06-11 13:32:13 +0100168 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100169 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100170 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 return 0;
nia9f5312c2020-06-11 13:32:13 +0100172}
173#endif /* KERN_ARND */
174#endif /* __FreeBSD__ || __NetBSD__ */
175
Paul Bakker6083fd22011-12-03 21:45:14 +0000176#include <stdio.h>
177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178int mbedtls_platform_entropy_poll(void *data,
179 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000180{
181 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200182 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 ((void) data);
185
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200186#if defined(HAVE_GETRANDOM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 ret = getrandom_wrapper(output, len, 0);
188 if (ret >= 0) {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200189 *olen = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 return 0;
191 } else if (errno != ENOSYS) {
192 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200193 }
Hanno Becker27516972018-10-18 11:49:25 +0100194 /* Fall through if the system call isn't known. */
195#else
Hanno Becker9772da82018-11-05 11:43:07 +0000196 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200197#endif /* HAVE_GETRANDOM */
198
nia9f5312c2020-06-11 13:32:13 +0100199#if defined(HAVE_SYSCTL_ARND)
200 ((void) file);
201 ((void) read_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 if (sysctl_arnd_wrapper(output, len) == -1) {
203 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
204 }
nia9f5312c2020-06-11 13:32:13 +0100205 *olen = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 return 0;
nia9f5312c2020-06-11 13:32:13 +0100207#else
208
Paul Bakker6083fd22011-12-03 21:45:14 +0000209 *olen = 0;
210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 file = fopen("/dev/urandom", "rb");
212 if (file == NULL) {
213 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000214 }
215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
217 mbedtls_setbuf(file, NULL);
218
219 read_len = fread(output, 1, len, file);
220 if (read_len != len) {
221 fclose(file);
222 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
223 }
224
225 fclose(file);
Paul Bakker6083fd22011-12-03 21:45:14 +0000226 *olen = len;
227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 return 0;
nia9f5312c2020-06-11 13:32:13 +0100229#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000230}
Paul Bakker9af723c2014-05-01 13:03:14 +0200231#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000233
Paul Bakker9988d6b2016-06-01 11:29:42 +0100234#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_nv_seed_poll(void *data,
236 unsigned char *output, size_t len, size_t *olen)
Paul Bakker9988d6b2016-06-01 11:29:42 +0100237{
238 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
239 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
240 ((void) data);
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
245 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
246 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 if (len < use_len) {
249 use_len = len;
250 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100251
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 memcpy(output, buf, use_len);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100253 *olen = use_len;
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 return 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100256}
257#endif /* MBEDTLS_ENTROPY_NV_SEED */
258
Minos Galanakis12b493f2023-08-11 15:22:45 +0100259#if defined(MBEDTLS_POP_TARGET_PRAGMA)
260#undef MBEDTLS_POP_TARGET_PRAGMA
261#endif
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* MBEDTLS_ENTROPY_C */