blob: be4f31ef89a7e4ff635f7c406eaff3f70ddc4e44 [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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020022# define _GNU_SOURCE
Nicholas Wilson2682edf2017-12-05 12:08:15 +000023#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
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020031# include "mbedtls/entropy.h"
32# include "entropy_poll.h"
33# include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035# if defined(MBEDTLS_TIMING_C)
36# include "mbedtls/timing.h"
37# endif
38# if defined(MBEDTLS_ENTROPY_NV_SEED)
39# include "mbedtls/platform.h"
40# endif
Paul Bakker6083fd22011-12-03 21:45:14 +000041
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020042# if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010043
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020044# if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
45 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
46 !defined(__HAIKU__) && !defined(__midipix__)
47# error \
48 "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in mbedtls_config.h"
49# endif
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010050
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051# if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053# if !defined(_WIN32_WINNT)
54# define _WIN32_WINNT 0x0400
55# endif
56# include <windows.h>
57# include <wincrypt.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000058
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059int mbedtls_platform_entropy_poll(void *data,
60 unsigned char *output,
61 size_t len,
62 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +000063{
64 HCRYPTPROV provider;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065 ((void)data);
Paul Bakker6083fd22011-12-03 21:45:14 +000066 *olen = 0;
67
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020068 if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL,
69 CRYPT_VERIFYCONTEXT) == FALSE) {
70 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +000071 }
72
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020073 if (CryptGenRandom(provider, (DWORD)len, output) == FALSE) {
74 CryptReleaseContext(provider, 0);
75 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Attila Molnar2791ba12016-01-26 11:39:26 +010076 }
Paul Bakker6083fd22011-12-03 21:45:14 +000077
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020078 CryptReleaseContext(provider, 0);
Paul Bakker6083fd22011-12-03 21:45:14 +000079 *olen = len;
80
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081 return 0;
Paul Bakker6083fd22011-12-03 21:45:14 +000082}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020083# else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000084
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010085/*
86 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +020087 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010088 * available in GNU libc and compatible libc's (eg uClibc).
89 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020090# if ((defined(__linux__) && defined(__GLIBC__)) || \
91 defined(__midipix__))
92# include <unistd.h>
93# include <sys/syscall.h>
94# if defined(SYS_getrandom)
95# define HAVE_GETRANDOM
96# include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020097
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020098static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010099{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200100 /* MemSan cannot understand that the syscall writes to the buffer */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200101# if defined(__has_feature)
102# if __has_feature(memory_sanitizer)
103 memset(buf, 0, buflen);
104# endif
105# endif
106 return syscall(SYS_getrandom, buf, buflen, flags);
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100107}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200108# endif /* SYS_getrandom */
109# endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100110
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200111# if defined(__FreeBSD__) || defined(__DragonFly__)
112# include <sys/param.h>
113# if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
114 (defined(__DragonFly__) && __DragonFly_version >= 500700)
115# include <errno.h>
116# include <sys/random.h>
117# define HAVE_GETRANDOM
118static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
David Carlier2b8c2652020-12-28 15:51:14 +0000119{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200120 return getrandom(buf, buflen, flags);
David Carlier2b8c2652020-12-28 15:51:14 +0000121}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200122# endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) || \
123 (__DragonFly__ && __DragonFly_version >= 500700) */
124# endif /* __FreeBSD__ || __DragonFly__ */
David Carlier2b8c2652020-12-28 15:51:14 +0000125
nia9f5312c2020-06-11 13:32:13 +0100126/*
127 * Some BSD systems provide KERN_ARND.
128 * This is equivalent to reading from /dev/urandom, only it doesn't require an
129 * open file descriptor, and provides up to 256 bytes per call (basically the
130 * same as getentropy(), but with a longer history).
131 *
132 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
133 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134# if (defined(__FreeBSD__) || defined(__NetBSD__)) && \
135 !defined(HAVE_GETRANDOM)
136# include <sys/param.h>
137# include <sys/sysctl.h>
138# if defined(KERN_ARND)
139# define HAVE_SYSCTL_ARND
nia9f5312c2020-06-11 13:32:13 +0100140
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200141static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
nia9f5312c2020-06-11 13:32:13 +0100142{
143 int name[2];
144 size_t len;
145
146 name[0] = CTL_KERN;
147 name[1] = KERN_ARND;
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149 while (buflen > 0) {
nia9f5312c2020-06-11 13:32:13 +0100150 len = buflen > 256 ? 256 : buflen;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200151 if (sysctl(name, 2, buf, &len, NULL, 0) == -1)
152 return -1;
nia9f5312c2020-06-11 13:32:13 +0100153 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100154 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100155 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200156 return 0;
nia9f5312c2020-06-11 13:32:13 +0100157}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200158# endif /* KERN_ARND */
159# endif /* __FreeBSD__ || __NetBSD__ */
nia9f5312c2020-06-11 13:32:13 +0100160
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200161# include <stdio.h>
Paul Bakker6083fd22011-12-03 21:45:14 +0000162
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163int mbedtls_platform_entropy_poll(void *data,
164 unsigned char *output,
165 size_t len,
166 size_t *olen)
Paul Bakker6083fd22011-12-03 21:45:14 +0000167{
168 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200169 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200171 ((void)data);
Paul Bakker6083fd22011-12-03 21:45:14 +0000172
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200173# if defined(HAVE_GETRANDOM)
174 ret = getrandom_wrapper(output, len, 0);
175 if (ret >= 0) {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200176 *olen = ret;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200177 return 0;
178 } else if (errno != ENOSYS)
179 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
180 /* Fall through if the system call isn't known. */
181# else
182 ((void)ret);
183# endif /* HAVE_GETRANDOM */
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200184
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200185# if defined(HAVE_SYSCTL_ARND)
186 ((void)file);
187 ((void)read_len);
188 if (sysctl_arnd_wrapper(output, len) == -1)
189 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
nia9f5312c2020-06-11 13:32:13 +0100190 *olen = len;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200191 return 0;
192# else
nia9f5312c2020-06-11 13:32:13 +0100193
Paul Bakker6083fd22011-12-03 21:45:14 +0000194 *olen = 0;
195
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200196 file = fopen("/dev/urandom", "rb");
197 if (file == NULL)
198 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker9af723c2014-05-01 13:03:14 +0200199
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200 read_len = fread(output, 1, len, file);
201 if (read_len != len) {
202 fclose(file);
203 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000204 }
205
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200206 fclose(file);
Paul Bakker6083fd22011-12-03 21:45:14 +0000207 *olen = len;
208
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200209 return 0;
210# endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000211}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200212# endif /* _WIN32 && !EFIX64 && !EFI32 */
213# endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000214
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200215# if defined(MBEDTLS_ENTROPY_NV_SEED)
216int mbedtls_nv_seed_poll(void *data,
217 unsigned char *output,
218 size_t len,
219 size_t *olen)
Paul Bakker9988d6b2016-06-01 11:29:42 +0100220{
221 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
222 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 ((void)data);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100224
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100226
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0)
228 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100229
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200230 if (len < use_len)
231 use_len = len;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100232
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200233 memcpy(output, buf, use_len);
Paul Bakker9988d6b2016-06-01 11:29:42 +0100234 *olen = use_len;
235
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 return 0;
Paul Bakker9988d6b2016-06-01 11:29:42 +0100237}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200238# endif /* MBEDTLS_ENTROPY_NV_SEED */
Paul Bakker9988d6b2016-06-01 11:29:42 +0100239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_ENTROPY_C */