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