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 |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Gilles Peskine | a1f9ef0 | 2020-09-30 22:18:13 +0200 | [diff] [blame] | 8 | #if defined(__linux__) && !defined(_GNU_SOURCE) |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 9 | /* Ensure that syscall() is available even when compiling with -std=c99 */ |
| 10 | #define _GNU_SOURCE |
| 11 | #endif |
| 12 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 13 | #include "common.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 14 | |
Gilles Peskine | 02b9329 | 2018-06-01 14:38:45 +0200 | [diff] [blame] | 15 | #include <string.h> |
| 16 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 17 | #if defined(MBEDTLS_ENTROPY_C) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 19 | #include "mbedtls/entropy.h" |
| 20 | #include "mbedtls/entropy_poll.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 21 | #include "mbedtls/error.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 22 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 24 | #include "mbedtls/timing.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 25 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 26 | #if defined(MBEDTLS_HAVEGE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 27 | #include "mbedtls/havege.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 28 | #endif |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 30 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 31 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 32 | |
| 33 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
Augustin Cavalier | 60bc47d | 2018-04-11 20:27:32 -0400 | [diff] [blame] | 34 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 35 | !defined(__HAIKU__) && !defined(__midipix__) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 36 | #error \ |
| 37 | "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 38 | #endif |
| 39 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 40 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 41 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 42 | #if !defined(_WIN32_WINNT) |
| 43 | #define _WIN32_WINNT 0x0400 |
| 44 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 45 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 46 | #include <wincrypt.h> |
| 47 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len, |
| 49 | size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 50 | { |
| 51 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 52 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 53 | *olen = 0; |
| 54 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 55 | if (CryptAcquireContext(&provider, NULL, NULL, |
| 56 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) == FALSE) { |
| 57 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 60 | if (CryptGenRandom(provider, (DWORD) len, output) == FALSE) { |
| 61 | CryptReleaseContext(provider, 0); |
| 62 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Attila Molnar | 2791ba1 | 2016-01-26 11:39:26 +0100 | [diff] [blame] | 63 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 64 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 65 | CryptReleaseContext(provider, 0); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 66 | *olen = len; |
| 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 68 | return 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 69 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 70 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 71 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 72 | /* |
| 73 | * Test for Linux getrandom() support. |
Gilles Peskine | c6468ee | 2020-09-30 22:11:13 +0200 | [diff] [blame] | 74 | * 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] | 75 | * available in GNU libc and compatible libc's (eg uClibc). |
| 76 | */ |
Gilles Peskine | c6468ee | 2020-09-30 22:11:13 +0200 | [diff] [blame] | 77 | #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)) |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 78 | #include <unistd.h> |
| 79 | #include <sys/syscall.h> |
| 80 | #if defined(SYS_getrandom) |
| 81 | #define HAVE_GETRANDOM |
Hanno Becker | 2751697 | 2018-10-18 11:49:25 +0100 | [diff] [blame] | 82 | #include <errno.h> |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | 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] | 85 | { |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 86 | /* MemSan cannot understand that the syscall writes to the buffer */ |
| 87 | #if defined(__has_feature) |
| 88 | #if __has_feature(memory_sanitizer) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | memset(buf, 0, buflen); |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 90 | #endif |
| 91 | #endif |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 92 | return syscall(SYS_getrandom, buf, buflen, flags); |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 93 | } |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 94 | #endif /* SYS_getrandom */ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 95 | #endif /* __linux__ || __midipix__ */ |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 96 | |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 97 | #if defined(__FreeBSD__) || defined(__DragonFly__) |
| 98 | #include <sys/param.h> |
| 99 | #if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \ |
| 100 | (defined(__DragonFly__) && __DragonFly_version >= 500700) |
| 101 | #include <errno.h> |
| 102 | #include <sys/random.h> |
| 103 | #define HAVE_GETRANDOM |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | static int getrandom_wrapper(void *buf, size_t buflen, unsigned int flags) |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 105 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | return getrandom(buf, buflen, flags); |
David Carlier | 2b8c265 | 2020-12-28 15:51:14 +0000 | [diff] [blame] | 107 | } |
| 108 | #endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) || |
| 109 | (__DragonFly__ && __DragonFly_version >= 500700) */ |
| 110 | #endif /* __FreeBSD__ || __DragonFly__ */ |
| 111 | |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 112 | /* |
| 113 | * Some BSD systems provide KERN_ARND. |
| 114 | * This is equivalent to reading from /dev/urandom, only it doesn't require an |
| 115 | * open file descriptor, and provides up to 256 bytes per call (basically the |
| 116 | * same as getentropy(), but with a longer history). |
| 117 | * |
| 118 | * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7 |
| 119 | */ |
| 120 | #if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM) |
| 121 | #include <sys/param.h> |
| 122 | #include <sys/sysctl.h> |
| 123 | #if defined(KERN_ARND) |
| 124 | #define HAVE_SYSCTL_ARND |
| 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | static int sysctl_arnd_wrapper(unsigned char *buf, size_t buflen) |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 127 | { |
| 128 | int name[2]; |
| 129 | size_t len; |
| 130 | |
| 131 | name[0] = CTL_KERN; |
| 132 | name[1] = KERN_ARND; |
| 133 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 134 | while (buflen > 0) { |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 135 | len = buflen > 256 ? 256 : buflen; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | if (sysctl(name, 2, buf, &len, NULL, 0) == -1) { |
| 137 | return -1; |
| 138 | } |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 139 | buflen -= len; |
nia | 8373c86 | 2020-06-24 17:15:02 +0100 | [diff] [blame] | 140 | buf += len; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 141 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 142 | return 0; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 143 | } |
| 144 | #endif /* KERN_ARND */ |
| 145 | #endif /* __FreeBSD__ || __NetBSD__ */ |
| 146 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 147 | #include <stdio.h> |
| 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | int mbedtls_platform_entropy_poll(void *data, |
| 150 | unsigned char *output, size_t len, size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 151 | { |
| 152 | FILE *file; |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 153 | size_t read_len; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 154 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 155 | ((void) data); |
| 156 | |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 157 | #if defined(HAVE_GETRANDOM) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | ret = getrandom_wrapper(output, len, 0); |
| 159 | if (ret >= 0) { |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 160 | *olen = ret; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | return 0; |
| 162 | } else if (errno != ENOSYS) { |
| 163 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 164 | } |
Hanno Becker | 2751697 | 2018-10-18 11:49:25 +0100 | [diff] [blame] | 165 | /* Fall through if the system call isn't known. */ |
| 166 | #else |
Hanno Becker | 9772da8 | 2018-11-05 11:43:07 +0000 | [diff] [blame] | 167 | ((void) ret); |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 168 | #endif /* HAVE_GETRANDOM */ |
| 169 | |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 170 | #if defined(HAVE_SYSCTL_ARND) |
| 171 | ((void) file); |
| 172 | ((void) read_len); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 173 | if (sysctl_arnd_wrapper(output, len) == -1) { |
| 174 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 175 | } |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 176 | *olen = len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 177 | return 0; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 178 | #else |
| 179 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 180 | *olen = 0; |
| 181 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 182 | file = fopen("/dev/urandom", "rb"); |
| 183 | if (file == NULL) { |
| 184 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | read_len = fread(output, 1, len, file); |
| 188 | if (read_len != len) { |
| 189 | fclose(file); |
| 190 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 191 | } |
| 192 | |
| 193 | fclose(file); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 194 | *olen = len; |
| 195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | return 0; |
nia | 9f5312c | 2020-06-11 13:32:13 +0100 | [diff] [blame] | 197 | #endif /* HAVE_SYSCTL_ARND */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 198 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 199 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 201 | |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 202 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | int mbedtls_null_entropy_poll(void *data, |
| 204 | unsigned char *output, size_t len, size_t *olen) |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 205 | { |
| 206 | ((void) data); |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 207 | ((void) output); |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 208 | |
Gilles Peskine | 8e1e46e | 2021-02-08 22:02:12 +0100 | [diff] [blame] | 209 | *olen = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | if (len < sizeof(unsigned char)) { |
| 211 | return 0; |
| 212 | } |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 213 | |
Gilles Peskine | 8e1e46e | 2021-02-08 22:02:12 +0100 | [diff] [blame] | 214 | output[0] = 0; |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 215 | *olen = sizeof(unsigned char); |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | return 0; |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 217 | } |
| 218 | #endif |
| 219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | #if defined(MBEDTLS_TIMING_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 221 | int mbedtls_hardclock_poll(void *data, |
| 222 | unsigned char *output, size_t len, size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 223 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | unsigned long timer = mbedtls_timing_hardclock(); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 225 | ((void) data); |
| 226 | *olen = 0; |
| 227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | if (len < sizeof(unsigned long)) { |
| 229 | return 0; |
| 230 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 231 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | memcpy(output, &timer, sizeof(unsigned long)); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 233 | *olen = sizeof(unsigned long); |
| 234 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 235 | return 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 236 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #endif /* MBEDTLS_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 238 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | #if defined(MBEDTLS_HAVEGE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 240 | int mbedtls_havege_poll(void *data, |
| 241 | unsigned char *output, size_t len, size_t *olen) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 242 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_havege_state *hs = (mbedtls_havege_state *) data; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 244 | *olen = 0; |
| 245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | if (mbedtls_havege_random(hs, output, len) != 0) { |
| 247 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 248 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 249 | |
| 250 | *olen = len; |
| 251 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 252 | return 0; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 253 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | #endif /* MBEDTLS_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 255 | |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 256 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 257 | int mbedtls_nv_seed_poll(void *data, |
| 258 | unsigned char *output, size_t len, size_t *olen) |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 259 | { |
| 260 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 261 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
| 262 | ((void) data); |
| 263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | memset(buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE); |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 265 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | if (mbedtls_nv_seed_read(buf, MBEDTLS_ENTROPY_BLOCK_SIZE) < 0) { |
| 267 | return MBEDTLS_ERR_ENTROPY_SOURCE_FAILED; |
| 268 | } |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 269 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 270 | if (len < use_len) { |
| 271 | use_len = len; |
| 272 | } |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 273 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 274 | memcpy(output, buf, use_len); |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 275 | *olen = use_len; |
| 276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | return 0; |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 278 | } |
| 279 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 280 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | #endif /* MBEDTLS_ENTROPY_C */ |