blob: 611768cd8556d588abc1cafd0088c0409a332fc9 [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker6083fd22011-12-03 21:45:14 +00006 */
7
nilesh.kale2a0a6282024-04-02 18:10:39 +05308#if defined(__linux__) || defined(__midipix__)
Nicholas Wilson2682edf2017-12-05 12:08:15 +00009/* Ensure that syscall() is available even when compiling with -std=c99 */
nilesh.kale2a0a6282024-04-02 18:10:39 +053010#if !defined(_GNU_SOURCE)
Nicholas Wilson2682edf2017-12-05 12:08:15 +000011#define _GNU_SOURCE
12#endif
nilesh.kale2a0a6282024-04-02 18:10:39 +053013#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000014
Gilles Peskinedb09ef62020-06-03 01:43:33 +020015#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000016
Gilles Peskine02b93292018-06-01 14:38:45 +020017#include <string.h>
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000020
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/entropy.h"
Chris Jonesea0a8652021-03-09 19:11:19 +000022#include "entropy_poll.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000023#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000027#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010028#include "mbedtls/platform.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010031
32#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040033 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
v1gnesh3c129dd2024-01-22 15:59:49 +053034 !defined(__HAIKU__) && !defined(__midipix__) && !defined(__MVS__)
Gilles Peskine449bd832023-01-11 14:50:10 +010035#error \
36 "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 +010037#endif
38
Paul Bakkerfa6a6202013-10-28 18:48:30 +010039#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000040
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000041#include <windows.h>
Kevin Kane0ec1e682016-12-15 09:27:16 -080042#include <bcrypt.h>
Kevin Kane0ec1e682016-12-15 09:27:16 -080043#include <intsafe.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000044
Gilles Peskine449bd832023-01-11 14:50:10 +010045int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
46 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +000047{
Paul Bakker6bcfc672011-12-05 13:54:00 +000048 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000049 *olen = 0;
50
Kevin Kane0ec1e682016-12-15 09:27:16 -080051 /*
Simon Butcherdef90f42018-03-14 17:02:16 +000052 * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
Minos Galanakise8a5d1a2023-09-08 14:47:37 +010053 * 64-bit Windows platforms. Extract entropy in chunks of len (dependent
54 * on ULONG_MAX) size.
Kevin Kane0ec1e682016-12-15 09:27:16 -080055 */
Minos Galanakis2c6e5612023-09-06 15:00:58 +010056 while (len != 0) {
57 unsigned long ulong_bytes =
58 (len > ULONG_MAX) ? ULONG_MAX : (unsigned long) len;
Paul Bakker6083fd22011-12-03 21:45:14 +000059
Minos Galanakis2c6e5612023-09-06 15:00:58 +010060 if (!BCRYPT_SUCCESS(BCryptGenRandom(NULL, output, ulong_bytes,
61 BCRYPT_USE_SYSTEM_PREFERRED_RNG))) {
62 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
63 }
Paul Bakker6083fd22011-12-03 21:45:14 +000064
Minos Galanakis2c6e5612023-09-06 15:00:58 +010065 *olen += ulong_bytes;
66 len -= ulong_bytes;
67 }
Paul Bakker6083fd22011-12-03 21:45:14 +000068
Gilles Peskine449bd832023-01-11 14:50:10 +010069 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +000070}
Paul Bakker9af723c2014-05-01 13:03:14 +020071#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000072
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010073/*
74 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +020075 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010076 * available in GNU libc and compatible libc's (eg uClibc).
77 */
Gilles Peskinec6468ee2020-09-30 22:11:13 +020078#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010079#include <unistd.h>
80#include <sys/syscall.h>
81#if defined(SYS_getrandom)
82#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +010083#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010086{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020087 /* MemSan cannot understand that the syscall writes to the buffer */
88#if defined(__has_feature)
89#if __has_feature(memory_sanitizer)
Gilles Peskine449bd832023-01-11 14:50:10 +010090 memset(buf, 0, buflen);
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020091#endif
92#endif
Dave Rodgmanb2e84192023-11-03 23:32:39 +000093 return (int) syscall(SYS_getrandom, buf, buflen, flags);
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010094}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020095#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +000096#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097
David Carlier2b8c2652020-12-28 15:51:14 +000098#if defined(__FreeBSD__) || defined(__DragonFly__)
99#include <sys/param.h>
100#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
101 (defined(__DragonFly__) && __DragonFly_version >= 500700)
102#include <errno.h>
103#include <sys/random.h>
104#define HAVE_GETRANDOM
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
David Carlier2b8c2652020-12-28 15:51:14 +0000106{
Dave Rodgmanb2e84192023-11-03 23:32:39 +0000107 return (int) getrandom(buf, buflen, flags);
David Carlier2b8c2652020-12-28 15:51:14 +0000108}
109#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
110 (__DragonFly__ && __DragonFly_version >= 500700) */
111#endif /* __FreeBSD__ || __DragonFly__ */
112
nia9f5312c2020-06-11 13:32:13 +0100113/*
114 * Some BSD systems provide KERN_ARND.
115 * This is equivalent to reading from /dev/urandom, only it doesn't require an
116 * open file descriptor, and provides up to 256 bytes per call (basically the
117 * same as getentropy(), but with a longer history).
118 *
119 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
120 */
121#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
122#include <sys/param.h>
123#include <sys/sysctl.h>
124#if defined(KERN_ARND)
125#define HAVE_SYSCTL_ARND
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
nia9f5312c2020-06-11 13:32:13 +0100128{
129 int name[2];
130 size_t len;
131
132 name[0] = CTL_KERN;
133 name[1] = KERN_ARND;
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 while (buflen > 0) {
nia9f5312c2020-06-11 13:32:13 +0100136 len = buflen > 256 ? 256 : buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
138 return -1;
139 }
nia9f5312c2020-06-11 13:32:13 +0100140 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100141 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100142 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100143 return 0;
nia9f5312c2020-06-11 13:32:13 +0100144}
145#endif /* KERN_ARND */
146#endif /* __FreeBSD__ || __NetBSD__ */
147
Paul Bakker6083fd22011-12-03 21:45:14 +0000148#include <stdio.h>
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150int mbedtls_platform_entropy_poll(void *data,
151 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000152{
153 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200154 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000156 ((void) data);
157
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200158#if defined(HAVE_GETRANDOM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 ret = getrandom_wrapper(output, len, 0);
160 if (ret >= 0) {
Dave Rodgmanb2e84192023-11-03 23:32:39 +0000161 *olen = (size_t) ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return 0;
163 } else if (errno != ENOSYS) {
164 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200165 }
Hanno Becker27516972018-10-18 11:49:25 +0100166 /* Fall through if the system call isn't known. */
167#else
Hanno Becker9772da82018-11-05 11:43:07 +0000168 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200169#endif /* HAVE_GETRANDOM */
170
nia9f5312c2020-06-11 13:32:13 +0100171#if defined(HAVE_SYSCTL_ARND)
172 ((void) file);
173 ((void) read_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 if (sysctl_arnd_wrapper(output, len) == -1) {
175 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
176 }
nia9f5312c2020-06-11 13:32:13 +0100177 *olen = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return 0;
nia9f5312c2020-06-11 13:32:13 +0100179#else
180
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 *olen = 0;
182
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 file = fopen("/dev/urandom", "rb");
184 if (file == NULL) {
185 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000186 }
187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
189 mbedtls_setbuf(file, NULL);
190
191 read_len = fread(output, 1, len, file);
192 if (read_len != len) {
193 fclose(file);
194 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
195 }
196
197 fclose(file);
Paul Bakker6083fd22011-12-03 21:45:14 +0000198 *olen = len;
199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 return 0;
nia9f5312c2020-06-11 13:32:13 +0100201#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000202}
Paul Bakker9af723c2014-05-01 13:03:14 +0200203#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000205
Paul Bakker9988d6b2016-06-01 11:29:42 +0100206#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100207int mbedtls_nv_seed_poll(void *data,
208 unsigned char *output, size_t len, size_t *olen)
Paul Bakker9988d6b2016-06-01 11:29:42 +0100209{
210 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
211 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
212 ((void) data);
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100215
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
217 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
218 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (len < use_len) {
221 use_len = len;
222 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 memcpy(output, buf, use_len);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100225 *olen = use_len;
226
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 return 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100228}
229#endif /* MBEDTLS_ENTROPY_NV_SEED */
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_ENTROPY_C */