blob: b5024c83fa7387d3d45e9fac3f882f3e392773d9 [file] [log] [blame]
Jens Wiklander817466c2018-05-22 13:49:31 +02001/*
2 * Platform-specific and custom entropy polling functions
3 *
Jerome Forissier79013242021-07-28 10:24:04 +02004 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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.
Jens Wiklander817466c2018-05-22 13:49:31 +020018 */
19
Jerome Forissier79013242021-07-28 10:24:04 +020020#if defined(__linux__) && !defined(_GNU_SOURCE)
Jens Wiklander3d3b0592019-03-20 15:30:29 +010021/* Ensure that syscall() is available even when compiling with -std=c99 */
22#define _GNU_SOURCE
23#endif
24
Jerome Forissier79013242021-07-28 10:24:04 +020025#include "common.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020026
Jens Wiklander3d3b0592019-03-20 15:30:29 +010027#include <string.h>
28
Jens Wiklander817466c2018-05-22 13:49:31 +020029#if defined(MBEDTLS_ENTROPY_C)
30
31#include "mbedtls/entropy.h"
Jens Wiklander32b31802023-10-06 16:59:46 +020032#include "entropy_poll.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020033#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020034
35#if defined(MBEDTLS_TIMING_C)
Jens Wiklander817466c2018-05-22 13:49:31 +020036#include "mbedtls/timing.h"
37#endif
Jens Wiklander817466c2018-05-22 13:49:31 +020038#include "mbedtls/platform.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020039
40#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
41
42#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010043 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Jerome Forissier79013242021-07-28 10:24:04 +020044 !defined(__HAIKU__) && !defined(__midipix__)
Jens Wiklander32b31802023-10-06 16:59:46 +020045#error \
46 "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in mbedtls_config.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020047#endif
48
49#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
50
51#if !defined(_WIN32_WINNT)
52#define _WIN32_WINNT 0x0400
53#endif
54#include <windows.h>
55#include <wincrypt.h>
56
Jens Wiklander32b31802023-10-06 16:59:46 +020057int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len,
58 size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +020059{
60 HCRYPTPROV provider;
61 ((void) data);
62 *olen = 0;
63
Jens Wiklander32b31802023-10-06 16:59:46 +020064 if (CryptAcquireContext(&provider, NULL, NULL,
65 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) {
66 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +020067 }
68
Jens Wiklander32b31802023-10-06 16:59:46 +020069 if (CryptGenRandom(provider, (DWORD) len, output) == FALSE) {
70 CryptReleaseContext(provider, 0);
71 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +020072 }
73
Jens Wiklander32b31802023-10-06 16:59:46 +020074 CryptReleaseContext(provider, 0);
Jens Wiklander817466c2018-05-22 13:49:31 +020075 *olen = len;
76
Jens Wiklander32b31802023-10-06 16:59:46 +020077 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +020078}
79#else /* _WIN32 && !EFIX64 && !EFI32 */
80
81/*
82 * Test for Linux getrandom() support.
83 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
84 * available in GNU libc and compatible libc's (eg uClibc).
85 */
Jerome Forissier79013242021-07-28 10:24:04 +020086#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Jens Wiklander817466c2018-05-22 13:49:31 +020087#include <unistd.h>
88#include <sys/syscall.h>
89#if defined(SYS_getrandom)
90#define HAVE_GETRANDOM
Jens Wiklander3d3b0592019-03-20 15:30:29 +010091#include <errno.h>
Jens Wiklander817466c2018-05-22 13:49:31 +020092
Jens Wiklander32b31802023-10-06 16:59:46 +020093static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Jens Wiklander817466c2018-05-22 13:49:31 +020094{
95 /* MemSan cannot understand that the syscall writes to the buffer */
96#if defined(__has_feature)
97#if __has_feature(memory_sanitizer)
Jens Wiklander32b31802023-10-06 16:59:46 +020098 memset(buf, 0, buflen);
Jens Wiklander817466c2018-05-22 13:49:31 +020099#endif
100#endif
Jens Wiklander32b31802023-10-06 16:59:46 +0200101 return syscall(SYS_getrandom, buf, buflen, flags);
Jens Wiklander817466c2018-05-22 13:49:31 +0200102}
Jens Wiklander817466c2018-05-22 13:49:31 +0200103#endif /* SYS_getrandom */
Jerome Forissier79013242021-07-28 10:24:04 +0200104#endif /* __linux__ || __midipix__ */
105
106#if defined(__FreeBSD__) || defined(__DragonFly__)
107#include <sys/param.h>
108#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
109 (defined(__DragonFly__) && __DragonFly_version >= 500700)
110#include <errno.h>
111#include <sys/random.h>
112#define HAVE_GETRANDOM
Jens Wiklander32b31802023-10-06 16:59:46 +0200113static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags)
Jerome Forissier79013242021-07-28 10:24:04 +0200114{
Jens Wiklander32b31802023-10-06 16:59:46 +0200115 return getrandom(buf, buflen, flags);
Jerome Forissier79013242021-07-28 10:24:04 +0200116}
117#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
118 (__DragonFly__ && __DragonFly_version >= 500700) */
119#endif /* __FreeBSD__ || __DragonFly__ */
120
121/*
122 * Some BSD systems provide KERN_ARND.
123 * This is equivalent to reading from /dev/urandom, only it doesn't require an
124 * open file descriptor, and provides up to 256 bytes per call (basically the
125 * same as getentropy(), but with a longer history).
126 *
127 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
128 */
129#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
130#include <sys/param.h>
131#include <sys/sysctl.h>
132#if defined(KERN_ARND)
133#define HAVE_SYSCTL_ARND
134
Jens Wiklander32b31802023-10-06 16:59:46 +0200135static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen)
Jerome Forissier79013242021-07-28 10:24:04 +0200136{
137 int name[2];
138 size_t len;
139
140 name[0] = CTL_KERN;
141 name[1] = KERN_ARND;
142
Jens Wiklander32b31802023-10-06 16:59:46 +0200143 while (buflen > 0) {
Jerome Forissier79013242021-07-28 10:24:04 +0200144 len = buflen > 256 ? 256 : buflen;
Jens Wiklander32b31802023-10-06 16:59:46 +0200145 if (sysctl(name, 2, buf, &len, NULL, 0) == -1) {
146 return -1;
147 }
Jerome Forissier79013242021-07-28 10:24:04 +0200148 buflen -= len;
149 buf += len;
150 }
Jens Wiklander32b31802023-10-06 16:59:46 +0200151 return 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200152}
153#endif /* KERN_ARND */
154#endif /* __FreeBSD__ || __NetBSD__ */
Jens Wiklander817466c2018-05-22 13:49:31 +0200155
156#include <stdio.h>
157
Jens Wiklander32b31802023-10-06 16:59:46 +0200158int mbedtls_platform_entropy_poll(void *data,
159 unsigned char *output, size_t len, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200160{
161 FILE *file;
162 size_t read_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200163 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200164 ((void) data);
165
166#if defined(HAVE_GETRANDOM)
Jens Wiklander32b31802023-10-06 16:59:46 +0200167 ret = getrandom_wrapper(output, len, 0);
168 if (ret >= 0) {
Jens Wiklander817466c2018-05-22 13:49:31 +0200169 *olen = ret;
Jens Wiklander32b31802023-10-06 16:59:46 +0200170 return 0;
171 } else if (errno != ENOSYS) {
172 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200173 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100174 /* Fall through if the system call isn't known. */
175#else
176 ((void) ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200177#endif /* HAVE_GETRANDOM */
178
Jerome Forissier79013242021-07-28 10:24:04 +0200179#if defined(HAVE_SYSCTL_ARND)
180 ((void) file);
181 ((void) read_len);
Jens Wiklander32b31802023-10-06 16:59:46 +0200182 if (sysctl_arnd_wrapper(output, len) == -1) {
183 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
184 }
Jerome Forissier79013242021-07-28 10:24:04 +0200185 *olen = len;
Jens Wiklander32b31802023-10-06 16:59:46 +0200186 return 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200187#else
188
Jens Wiklander817466c2018-05-22 13:49:31 +0200189 *olen = 0;
190
Jens Wiklander32b31802023-10-06 16:59:46 +0200191 file = fopen("/dev/urandom", "rb");
192 if (file == NULL) {
193 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200194 }
195
Jens Wiklander32b31802023-10-06 16:59:46 +0200196 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
197 mbedtls_setbuf(file, NULL);
198
199 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);
Jens Wiklander817466c2018-05-22 13:49:31 +0200206 *olen = len;
207
Jens Wiklander32b31802023-10-06 16:59:46 +0200208 return 0;
Jerome Forissier79013242021-07-28 10:24:04 +0200209#endif /* HAVE_SYSCTL_ARND */
Jens Wiklander817466c2018-05-22 13:49:31 +0200210}
211#endif /* _WIN32 && !EFIX64 && !EFI32 */
212#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
213
Jens Wiklander817466c2018-05-22 13:49:31 +0200214#if defined(MBEDTLS_ENTROPY_NV_SEED)
Jens Wiklander32b31802023-10-06 16:59:46 +0200215int mbedtls_nv_seed_poll(void *data,
216 unsigned char *output, size_t len, size_t *olen)
Jens Wiklander817466c2018-05-22 13:49:31 +0200217{
218 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
219 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
220 ((void) data);
221
Jens Wiklander32b31802023-10-06 16:59:46 +0200222 memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE);
Jens Wiklander817466c2018-05-22 13:49:31 +0200223
Jens Wiklander32b31802023-10-06 16:59:46 +0200224 if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) {
225 return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED;
226 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200227
Jens Wiklander32b31802023-10-06 16:59:46 +0200228 if (len < use_len) {
229 use_len = len;
230 }
Jens Wiklander817466c2018-05-22 13:49:31 +0200231
Jens Wiklander32b31802023-10-06 16:59:46 +0200232 memcpy(output, buf, use_len);
Jens Wiklander817466c2018-05-22 13:49:31 +0200233 *olen = use_len;
234
Jens Wiklander32b31802023-10-06 16:59:46 +0200235 return 0;
Jens Wiklander817466c2018-05-22 13:49:31 +0200236}
237#endif /* MBEDTLS_ENTROPY_NV_SEED */
238
239#endif /* MBEDTLS_ENTROPY_C */