blob: 3420616a06c694cd87656043ecc63bde33ac2bfb [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"
32#include "mbedtls/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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010041#include "mbedtls/platform.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010044
45#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040046 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000047 !defined(__HAIKU__) && !defined(__midipix__)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010048#error \
49 "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010050#endif
51
Paul Bakkerfa6a6202013-10-28 18:48:30 +010052#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000053
Paul Bakker6083fd22011-12-03 21:45:14 +000054#if !defined(_WIN32_WINNT)
55#define _WIN32_WINNT 0x0400
56#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000057#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000058#include <wincrypt.h>
59
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010060int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
61 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +000062{
63 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000064 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000065 *olen = 0;
66
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067 if (CryptAcquireContext(&provider, NULL, NULL,
68 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
69 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +000070 }
71
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 if (CryptGenRandom(provider, (DWORD) len, output) == FALSE) {
73 CryptReleaseContext(provider, 0);
74 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Attila Molnar2791ba12016-01-26 11:39:26 +010075 }
Paul Bakker6083fd22011-12-03 21:45:14 +000076
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010077 CryptReleaseContext(provider, 0);
Paul Bakker6083fd22011-12-03 21:45:14 +000078 *olen = len;
79
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010080 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +000081}
Paul Bakker9af723c2014-05-01 13:03:14 +020082#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000083
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010084/*
85 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +020086 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010087 * available in GNU libc and compatible libc's (eg uClibc).
88 */
Gilles Peskinec6468ee2020-09-30 22:11:13 +020089#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010090#include <unistd.h>
91#include <sys/syscall.h>
92#if defined(SYS_getrandom)
93#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +010094#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020098 /* MemSan cannot understand that the syscall writes to the buffer */
99#if defined(__has_feature)
100#if __has_feature(memory_sanitizer)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 memset(buf, 0, buflen);
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200102#endif
103#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 return syscall(SYS_getrandom, buf, buflen, flags);
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200106#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000107#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100108
David Carlier2b8c2652020-12-28 15:51:14 +0000109#if defined(__FreeBSD__) || defined(__DragonFly__)
110#include <sys/param.h>
111#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
112 (defined(__DragonFly__) && __DragonFly_version >= 500700)
113#include <errno.h>
114#include <sys/random.h>
115#define HAVE_GETRANDOM
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
David Carlier2b8c2652020-12-28 15:51:14 +0000117{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 return getrandom(buf, buflen, flags);
David Carlier2b8c2652020-12-28 15:51:14 +0000119}
120#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
121 (__DragonFly__ && __DragonFly_version >= 500700) */
122#endif /* __FreeBSD__ || __DragonFly__ */
123
nia9f5312c2020-06-11 13:32:13 +0100124/*
125 * Some BSD systems provide KERN_ARND.
126 * This is equivalent to reading from /dev/urandom, only it doesn't require an
127 * open file descriptor, and provides up to 256 bytes per call (basically the
128 * same as getentropy(), but with a longer history).
129 *
130 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
131 */
132#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
133#include <sys/param.h>
134#include <sys/sysctl.h>
135#if defined(KERN_ARND)
136#define HAVE_SYSCTL_ARND
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
nia9f5312c2020-06-11 13:32:13 +0100139{
140 int name[2];
141 size_t len;
142
143 name[0] = CTL_KERN;
144 name[1] = KERN_ARND;
145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 while (buflen > 0) {
nia9f5312c2020-06-11 13:32:13 +0100147 len = buflen > 256 ? 256 : buflen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
149 return -1;
150 }
nia9f5312c2020-06-11 13:32:13 +0100151 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100152 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100153 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 return 0;
nia9f5312c2020-06-11 13:32:13 +0100155}
156#endif /* KERN_ARND */
157#endif /* __FreeBSD__ || __NetBSD__ */
158
Paul Bakker6083fd22011-12-03 21:45:14 +0000159#include <stdio.h>
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161int mbedtls_platform_entropy_poll(void *data,
162 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000163{
164 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200165 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 ((void) data);
168
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200169#if defined(HAVE_GETRANDOM)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 ret = getrandom_wrapper(output, len, 0);
171 if (ret >= 0) {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200172 *olen = ret;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 return 0;
174 } else if (errno != ENOSYS) {
175 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200176 }
Hanno Becker27516972018-10-18 11:49:25 +0100177 /* Fall through if the system call isn't known. */
178#else
Hanno Becker9772da82018-11-05 11:43:07 +0000179 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200180#endif /* HAVE_GETRANDOM */
181
nia9f5312c2020-06-11 13:32:13 +0100182#if defined(HAVE_SYSCTL_ARND)
183 ((void) file);
184 ((void) read_len);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 if (sysctl_arnd_wrapper(output, len) == -1) {
186 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
187 }
nia9f5312c2020-06-11 13:32:13 +0100188 *olen = len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 return 0;
nia9f5312c2020-06-11 13:32:13 +0100190#else
191
Paul Bakker6083fd22011-12-03 21:45:14 +0000192 *olen = 0;
193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 file = fopen("/dev/urandom", "rb");
195 if (file == NULL) {
196 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000197 }
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 read_len = fread(output, 1, len, file);
200 if (read_len != len) {
201 fclose(file);
202 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
203 }
204
205 fclose(file);
Paul Bakker6083fd22011-12-03 21:45:14 +0000206 *olen = len;
207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 return 0;
nia9f5312c2020-06-11 13:32:13 +0100209#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000210}
Paul Bakker9af723c2014-05-01 13:03:14 +0200211#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000213
Simon Butcherab5df402016-06-11 02:31:21 +0100214#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215int mbedtls_null_entropy_poll(void *data,
216 unsigned char *output, size_t len, size_t *olen)
Janos Follath53de7842016-06-08 15:29:18 +0100217{
218 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100219 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100220
Gilles Peskine8e1e46e2021-02-08 22:02:12 +0100221 *olen = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if (len < sizeof(unsigned char)) {
223 return 0;
224 }
Janos Follath53de7842016-06-08 15:29:18 +0100225
Gilles Peskine8e1e46e2021-02-08 22:02:12 +0100226 output[0] = 0;
Janos Follath53de7842016-06-08 15:29:18 +0100227 *olen = sizeof(unsigned char);
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100228 return 0;
Janos Follath53de7842016-06-08 15:29:18 +0100229}
230#endif
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_TIMING_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233int mbedtls_hardclock_poll(void *data,
234 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 ((void) data);
238 *olen = 0;
239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (len < sizeof(unsigned long)) {
241 return 0;
242 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000243
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100244 memcpy(output, &timer, sizeof(unsigned long));
Paul Bakker6083fd22011-12-03 21:45:14 +0000245 *olen = sizeof(unsigned long);
246
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100247 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_HAVEGE_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252int mbedtls_havege_poll(void *data,
253 unsigned char *output, size_t len, size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000254{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000256 *olen = 0;
257
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (mbedtls_havege_random(hs, output, len) != 0) {
259 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
260 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000261
262 *olen = len;
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +0000265}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000267
Paul Bakker9988d6b2016-06-01 11:29:42 +0100268#if defined(MBEDTLS_ENTROPY_NV_SEED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269int mbedtls_nv_seed_poll(void *data,
270 unsigned char *output, size_t len, size_t *olen)
Paul Bakker9988d6b2016-06-01 11:29:42 +0100271{
272 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
273 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
274 ((void) data);
275
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
279 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
280 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100281
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (len < use_len) {
283 use_len = len;
284 }
Paul Bakker9988d6b2016-06-01 11:29:42 +0100285
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 memcpy(output, buf, use_len);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100287 *olen = use_len;
288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 return 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100290}
291#endif /* MBEDTLS_ENTROPY_NV_SEED */
292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_ENTROPY_C */