Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Platform-specific and custom entropy polling functions |
| 3 | * |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 4 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
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 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 22 | #if defined(__linux__) |
| 23 | /* Ensure that syscall() is available even when compiling with -std=c99 */ |
| 24 | #define _GNU_SOURCE |
| 25 | #endif |
| 26 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 27 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 28 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 29 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 31 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 32 | |
Gilles Peskine | 02b9329 | 2018-06-01 14:38:45 +0200 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 35 | #if defined(MBEDTLS_ENTROPY_C) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 37 | #include "mbedtls/entropy.h" |
| 38 | #include "mbedtls/entropy_poll.h" |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 39 | #include "mbedtls/error.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 40 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_TIMING_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/timing.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 43 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #if defined(MBEDTLS_HAVEGE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 45 | #include "mbedtls/havege.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 46 | #endif |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 47 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 48 | #include "mbedtls/platform.h" |
| 49 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 50 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 51 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 52 | |
| 53 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
Augustin Cavalier | 60bc47d | 2018-04-11 20:27:32 -0400 | [diff] [blame] | 54 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 55 | !defined(__HAIKU__) && !defined(__midipix__) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 56 | #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" |
| 57 | #endif |
| 58 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 59 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 60 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 61 | #if !defined(_WIN32_WINNT) |
| 62 | #define _WIN32_WINNT 0x0400 |
| 63 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 64 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 65 | #include <wincrypt.h> |
| 66 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 68 | size_t *olen ) |
| 69 | { |
| 70 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 71 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 72 | *olen = 0; |
| 73 | |
| 74 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 75 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 76 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 77 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 78 | } |
| 79 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 80 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Attila Molnar | 2791ba1 | 2016-01-26 11:39:26 +0100 | [diff] [blame] | 81 | { |
| 82 | CryptReleaseContext( provider, 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Attila Molnar | 2791ba1 | 2016-01-26 11:39:26 +0100 | [diff] [blame] | 84 | } |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 85 | |
| 86 | CryptReleaseContext( provider, 0 ); |
| 87 | *olen = len; |
| 88 | |
| 89 | return( 0 ); |
| 90 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 91 | #else /* _WIN32 && !EFIX64 && !EFI32 */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 92 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Test for Linux getrandom() support. |
| 95 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper |
| 96 | * available in GNU libc and compatible libc's (eg uClibc). |
| 97 | */ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 98 | #if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)) |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 99 | #include <unistd.h> |
| 100 | #include <sys/syscall.h> |
| 101 | #if defined(SYS_getrandom) |
| 102 | #define HAVE_GETRANDOM |
Hanno Becker | 2751697 | 2018-10-18 11:49:25 +0100 | [diff] [blame] | 103 | #include <errno.h> |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 104 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 105 | static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) |
| 106 | { |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 107 | /* MemSan cannot understand that the syscall writes to the buffer */ |
| 108 | #if defined(__has_feature) |
| 109 | #if __has_feature(memory_sanitizer) |
| 110 | memset( buf, 0, buflen ); |
| 111 | #endif |
| 112 | #endif |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 113 | return( syscall( SYS_getrandom, buf, buflen, flags ) ); |
| 114 | } |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 115 | #endif /* SYS_getrandom */ |
Ørjan Malde | 479d8de | 2020-05-20 09:32:39 +0000 | [diff] [blame] | 116 | #endif /* __linux__ || __midipix__ */ |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 117 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 118 | #include <stdio.h> |
| 119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | int mbedtls_platform_entropy_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 121 | unsigned char *output, size_t len, size_t *olen ) |
| 122 | { |
| 123 | FILE *file; |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 124 | size_t read_len; |
Janos Follath | 24eed8d | 2019-11-22 13:21:35 +0000 | [diff] [blame] | 125 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 126 | ((void) data); |
| 127 | |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 128 | #if defined(HAVE_GETRANDOM) |
Hanno Becker | 2751697 | 2018-10-18 11:49:25 +0100 | [diff] [blame] | 129 | ret = getrandom_wrapper( output, len, 0 ); |
| 130 | if( ret >= 0 ) |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 131 | { |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 132 | *olen = ret; |
| 133 | return( 0 ); |
| 134 | } |
Hanno Becker | 2751697 | 2018-10-18 11:49:25 +0100 | [diff] [blame] | 135 | else if( errno != ENOSYS ) |
| 136 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 137 | /* Fall through if the system call isn't known. */ |
| 138 | #else |
Hanno Becker | 9772da8 | 2018-11-05 11:43:07 +0000 | [diff] [blame] | 139 | ((void) ret); |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 140 | #endif /* HAVE_GETRANDOM */ |
| 141 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 142 | *olen = 0; |
| 143 | |
| 144 | file = fopen( "/dev/urandom", "rb" ); |
| 145 | if( file == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 146 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 148 | read_len = fread( output, 1, len, file ); |
| 149 | if( read_len != len ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 150 | { |
| 151 | fclose( file ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 152 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | fclose( file ); |
| 156 | *olen = len; |
| 157 | |
| 158 | return( 0 ); |
| 159 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 160 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 162 | |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 163 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
Simon Butcher | 4157b60 | 2016-06-12 00:31:33 +0100 | [diff] [blame] | 164 | int mbedtls_null_entropy_poll( void *data, |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 165 | unsigned char *output, size_t len, size_t *olen ) |
| 166 | { |
| 167 | ((void) data); |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 168 | ((void) output); |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 169 | *olen = 0; |
| 170 | |
| 171 | if( len < sizeof(unsigned char) ) |
| 172 | return( 0 ); |
| 173 | |
| 174 | *olen = sizeof(unsigned char); |
| 175 | |
| 176 | return( 0 ); |
| 177 | } |
| 178 | #endif |
| 179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | #if defined(MBEDTLS_TIMING_C) |
| 181 | int mbedtls_hardclock_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 182 | unsigned char *output, size_t len, size_t *olen ) |
| 183 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | unsigned long timer = mbedtls_timing_hardclock(); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 185 | ((void) data); |
| 186 | *olen = 0; |
| 187 | |
| 188 | if( len < sizeof(unsigned long) ) |
| 189 | return( 0 ); |
| 190 | |
| 191 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 192 | *olen = sizeof(unsigned long); |
| 193 | |
| 194 | return( 0 ); |
| 195 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | #endif /* MBEDTLS_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 197 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 198 | #if defined(MBEDTLS_HAVEGE_C) |
| 199 | int mbedtls_havege_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 200 | unsigned char *output, size_t len, size_t *olen ) |
| 201 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 202 | mbedtls_havege_state *hs = (mbedtls_havege_state *) data; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 203 | *olen = 0; |
| 204 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 205 | if( mbedtls_havege_random( hs, output, len ) != 0 ) |
| 206 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 207 | |
| 208 | *olen = len; |
| 209 | |
| 210 | return( 0 ); |
| 211 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | #endif /* MBEDTLS_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 213 | |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 214 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 215 | int mbedtls_nv_seed_poll( void *data, |
| 216 | unsigned char *output, size_t len, size_t *olen ) |
| 217 | { |
| 218 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 219 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
| 220 | ((void) data); |
| 221 | |
| 222 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 223 | |
| 224 | if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) |
| 225 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 226 | |
| 227 | if( len < use_len ) |
| 228 | use_len = len; |
| 229 | |
| 230 | memcpy( output, buf, use_len ); |
| 231 | *olen = use_len; |
| 232 | |
| 233 | return( 0 ); |
| 234 | } |
| 235 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 236 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #endif /* MBEDTLS_ENTROPY_C */ |