Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Gilles Peskine | a1f9ef0 | 2020-09-30 22:18:13 +0200 | [diff] [blame] | 20 | #if defined(__linux__) && !defined(_GNU_SOURCE) |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 21 | /* Ensure that syscall() is available even when compiling with -std=c99 */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 22 | # define _GNU_SOURCE |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 23 | #endif |
| 24 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 25 | #include "common.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 26 | |
Gilles Peskine | 02b9329 | 2018-06-01 14:38:45 +0200 | [diff] [blame] | 27 | #include <string.h> |
| 28 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | #if defined(MBEDTLS_ENTROPY_C) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 30 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 31 | # include "mbedtls/entropy.h" |
| 32 | # include "entropy_poll.h" |
| 33 | # include "mbedtls/error.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 34 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 35 | # 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 Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 41 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 42 | # if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 43 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 44 | # 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é-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 50 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 51 | # if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 52 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 53 | # if !defined(_WIN32_WINNT) |
| 54 | # define _WIN32_WINNT 0x0400 |
| 55 | # endif |
| 56 | # include <windows.h> |
| 57 | # include <wincrypt.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 58 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 59 | int mbedtls_platform_entropy_poll(void *data, |
| 60 | unsigned char *output, |
| 61 | size_t len, |
| 62 | size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 63 | { |
| 64 | HCRYPTPROV provider; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 65 | ((void)data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 66 | *olen = 0; |
| 67 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 68 | if (CryptAcquireContext(&provider, NULL, NULL, PROV_RSA_FULL, |
| 69 | CRYPT_VERIFYCONTEXT) == FALSE) { |
| 70 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 73 | if (CryptGenRandom(provider, (DWORD)len, output) == FALSE) { |
| 74 | CryptReleaseContext(provider, 0); |
| 75 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Attila Molnar | 2791ba1 | 2016-01-26 11:39:26 +0100 | [diff] [blame] | 76 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 77 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 78 | CryptReleaseContext(provider, 0); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 79 | *olen = len; |
| 80 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 81 | return 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 82 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 83 | # else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 84 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 85 | /* |
| 86 | * Test for Linux getrandom() support. |
Gilles Peskine | c6468ee | 2020-09-30 22:11:13 +0200 | [diff] [blame] | 87 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 88 | * available in GNU libc and compatible libc's (eg uClibc). |
| 89 | */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 90 | # 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é-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 97 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 98 | static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 99 | { |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 100 | /* MemSan cannot understand that the syscall writes to the buffer */ |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 101 | # 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é-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 107 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 108 | # endif /* SYS_getrandom */ |
| 109 | # endif /* __linux__ || __midipix__ */ |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 110 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 111 | # 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 |
| 118 | static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 119 | { |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 120 | return getrandom(buf, buflen, flags); |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 121 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 122 | # endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) || \ |
| 123 | (__DragonFly__ && __DragonFly_version >= 500700) */ |
| 124 | # endif /* __FreeBSD__ || __DragonFly__ */ |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 125 | |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 126 | /* |
| 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 Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 134 | # 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 |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 140 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 141 | static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen) |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 142 | { |
| 143 | int name[2]; |
| 144 | size_t len; |
| 145 | |
| 146 | name[0] = CTL_KERN; |
| 147 | name[1] = KERN_ARND; |
| 148 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 149 | while (buflen > 0) { |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 150 | len = buflen > 256 ? 256 : buflen; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 151 | if (sysctl(name, 2, buf, &len, NULL, 0) == -1) |
| 152 | return -1; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 153 | buflen -= len; |
nia | 8373c86 | 2020-06-24 17:15:02 +0100 | [diff] [blame] | 154 | buf += len; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 155 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 156 | return 0; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 157 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 158 | # endif /* KERN_ARND */ |
| 159 | # endif /* __FreeBSD__ || __NetBSD__ */ |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 160 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 161 | # include <stdio.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 162 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 163 | int mbedtls_platform_entropy_poll(void *data, |
| 164 | unsigned char *output, |
| 165 | size_t len, |
| 166 | size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 167 | { |
| 168 | FILE *file; |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 169 | size_t read_len; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 170 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 171 | ((void)data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 172 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 173 | # if defined(HAVE_GETRANDOM) |
| 174 | ret = getrandom_wrapper(output, len, 0); |
| 175 | if (ret >= 0) { |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 176 | *olen = ret; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 177 | 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é-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 184 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 185 | # 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; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 190 | *olen = len; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 191 | return 0; |
| 192 | # else |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 193 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 194 | *olen = 0; |
| 195 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 196 | file = fopen("/dev/urandom", "rb"); |
| 197 | if (file == NULL) |
| 198 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 199 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 200 | read_len = fread(output, 1, len, file); |
| 201 | if (read_len != len) { |
| 202 | fclose(file); |
| 203 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 206 | fclose(file); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 207 | *olen = len; |
| 208 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 209 | return 0; |
| 210 | # endif /* HAVE_SYSCTL_ARND */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 211 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 212 | # endif /* _WIN32 && !EFIX64 && !EFI32 */ |
| 213 | # endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 214 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 215 | # if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 216 | int mbedtls_nv_seed_poll(void *data, |
| 217 | unsigned char *output, |
| 218 | size_t len, |
| 219 | size_t *olen) |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 220 | { |
| 221 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 222 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 223 | ((void)data); |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 224 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 225 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 226 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 227 | if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) |
| 228 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 229 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 230 | if (len < use_len) |
| 231 | use_len = len; |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 232 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 233 | memcpy(output, buf, use_len); |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 234 | *olen = use_len; |
| 235 | |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 236 | return 0; |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 237 | } |
Mateusz Starzyk | c0eabdc | 2021-08-03 14:09:02 +0200 | [diff] [blame^] | 238 | # endif /* MBEDTLS_ENTROPY_NV_SEED */ |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 239 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 240 | #endif /* MBEDTLS_ENTROPY_C */ |