blob: f11a0b82292c19528a3cab3a7070c7e702fcd7fd [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
Simon Butcherdef90f42018-03-14 17:02:16 +000055/* Visual Studio 2010 and earlier issue a warning when both <stdint.h> and
56 * <intsafe.h> are included, as they redefine a number of <TYPE>_MAX constants.
57 * These constants are guaranteed to be the same, though, so we suppress the
58 * warning when including intsafe.h.
Kevin Kane0ec1e682016-12-15 09:27:16 -080059 */
60#pragma warning( push )
61#pragma warning( disable : 4005 )
62#endif
63#include <intsafe.h>
Simon Butchere068aa72018-03-14 15:10:31 +000064#if defined(_MSC_VER) && _MSC_VER <= 1600
Kevin Kane0ec1e682016-12-15 09:27:16 -080065#pragma warning( pop )
66#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000067
Gilles Peskine449bd832023-01-11 14:50:10 +010068int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
69 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +000070{
Kevin Kane0ec1e682016-12-15 09:27:16 -080071 ULONG len_as_ulong = 0;
Paul Bakker6bcfc672011-12-05 13:54:00 +000072 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000073 *olen = 0;
74
Kevin Kane0ec1e682016-12-15 09:27:16 -080075 /*
Simon Butcherdef90f42018-03-14 17:02:16 +000076 * BCryptGenRandom takes ULONG for size, which is smaller than size_t on
77 * 64-bit Windows platforms. Ensure len's value can be safely converted into
78 * a ULONG.
Kevin Kane0ec1e682016-12-15 09:27:16 -080079 */
80 if ( FAILED( SizeTToULong( len, &len_as_ulong ) ) )
81 {
82 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000083 }
84
Kevin Kane0ec1e682016-12-15 09:27:16 -080085 if ( !BCRYPT_SUCCESS( BCryptGenRandom( NULL, output, len_as_ulong, BCRYPT_USE_SYSTEM_PREFERRED_RNG ) ) )
86 {
87 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010088 }
Paul Bakker6083fd22011-12-03 21:45:14 +000089
Paul Bakker6083fd22011-12-03 21:45:14 +000090 *olen = len;
91
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +000093}
Steve Lhomme954553f2023-06-16 13:41:48 +020094#else /* !_WIN32_WINNT_WINXP */
Antonio de Angelis1ee4d122023-08-16 12:26:37 +010095#error "Entropy not available before Windows XP, use MBEDTLS_NO_PLATFORM_ENTROPY"
Steve Lhomme954553f2023-06-16 13:41:48 +020096#endif /* !_WIN32_WINNT_WINXP */
Paul Bakker9af723c2014-05-01 13:03:14 +020097#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000098
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010099/*
100 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +0200101 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100102 * available in GNU libc and compatible libc's (eg uClibc).
103 */
Gilles Peskinec6468ee2020-09-30 22:11:13 +0200104#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105#include <unistd.h>
106#include <sys/syscall.h>
107#if defined(SYS_getrandom)
108#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100109#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100112{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200113 /* MemSan cannot understand that the syscall writes to the buffer */
114#if defined(__has_feature)
115#if __has_feature(memory_sanitizer)
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 memset(buf, 0, buflen);
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200117#endif
118#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return syscall(SYS_getrandom, buf, buflen, flags);
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100120}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200121#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000122#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100123
David Carlier2b8c2652020-12-28 15:51:14 +0000124#if defined(__FreeBSD__) || defined(__DragonFly__)
125#include <sys/param.h>
126#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
127 (defined(__DragonFly__) && __DragonFly_version >= 500700)
128#include <errno.h>
129#include <sys/random.h>
130#define HAVE_GETRANDOM
Gilles Peskine449bd832023-01-11 14:50:10 +0100131static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
David Carlier2b8c2652020-12-28 15:51:14 +0000132{
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 return getrandom(buf, buflen, flags);
David Carlier2b8c2652020-12-28 15:51:14 +0000134}
135#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
136 (__DragonFly__ && __DragonFly_version >= 500700) */
137#endif /* __FreeBSD__ || __DragonFly__ */
138
nia9f5312c2020-06-11 13:32:13 +0100139/*
140 * Some BSD systems provide KERN_ARND.
141 * This is equivalent to reading from /dev/urandom, only it doesn't require an
142 * open file descriptor, and provides up to 256 bytes per call (basically the
143 * same as getentropy(), but with a longer history).
144 *
145 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
146 */
147#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
148#include <sys/param.h>
149#include <sys/sysctl.h>
150#if defined(KERN_ARND)
151#define HAVE_SYSCTL_ARND
152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
nia9f5312c2020-06-11 13:32:13 +0100154{
155 int name[2];
156 size_t len;
157
158 name[0] = CTL_KERN;
159 name[1] = KERN_ARND;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 while (buflen > 0) {
nia9f5312c2020-06-11 13:32:13 +0100162 len = buflen > 256 ? 256 : buflen;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
164 return -1;
165 }
nia9f5312c2020-06-11 13:32:13 +0100166 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100167 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100168 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 return 0;
nia9f5312c2020-06-11 13:32:13 +0100170}
171#endif /* KERN_ARND */
172#endif /* __FreeBSD__ || __NetBSD__ */
173
Paul Bakker6083fd22011-12-03 21:45:14 +0000174#include <stdio.h>
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176int mbedtls_platform_entropy_poll(void *data,
177 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000178{
179 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200180 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000182 ((void) data);
183
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200184#if defined(HAVE_GETRANDOM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 ret = getrandom_wrapper(output, len, 0);
186 if (ret >= 0) {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200187 *olen = ret;
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 return 0;
189 } else if (errno != ENOSYS) {
190 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200191 }
Hanno Becker27516972018-10-18 11:49:25 +0100192 /* Fall through if the system call isn't known. */
193#else
Hanno Becker9772da82018-11-05 11:43:07 +0000194 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200195#endif /* HAVE_GETRANDOM */
196
nia9f5312c2020-06-11 13:32:13 +0100197#if defined(HAVE_SYSCTL_ARND)
198 ((void) file);
199 ((void) read_len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (sysctl_arnd_wrapper(output, len) == -1) {
201 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
202 }
nia9f5312c2020-06-11 13:32:13 +0100203 *olen = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return 0;
nia9f5312c2020-06-11 13:32:13 +0100205#else
206
Paul Bakker6083fd22011-12-03 21:45:14 +0000207 *olen = 0;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 file = fopen("/dev/urandom", "rb");
210 if (file == NULL) {
211 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000212 }
213
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
215 mbedtls_setbuf(file, NULL);
216
217 read_len = fread(output, 1, len, file);
218 if (read_len != len) {
219 fclose(file);
220 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
221 }
222
223 fclose(file);
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 *olen = len;
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 return 0;
nia9f5312c2020-06-11 13:32:13 +0100227#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000228}
Paul Bakker9af723c2014-05-01 13:03:14 +0200229#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000231
Paul Bakker9988d6b2016-06-01 11:29:42 +0100232#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100233int mbedtls_nv_seed_poll(void *data,
234 unsigned char *output, size_t len, size_t *olen)
Paul Bakker9988d6b2016-06-01 11:29:42 +0100235{
236 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
237 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
238 ((void) data);
239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
243 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
244 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100245
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 if (len < use_len) {
247 use_len = len;
248 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 memcpy(output, buf, use_len);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100251 *olen = use_len;
252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 return 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100254}
255#endif /* MBEDTLS_ENTROPY_NV_SEED */
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_ENTROPY_C */