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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_ENTROPY_C) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/entropy.h" |
| 31 | #include "mbedtls/entropy_poll.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_TIMING_C) |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 34 | #include <string.h> |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 35 | #include "mbedtls/timing.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 36 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_HAVEGE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 38 | #include "mbedtls/havege.h" |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 39 | #endif |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 40 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 41 | #include "mbedtls/platform.h" |
| 42 | #endif |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 45 | |
| 46 | #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \ |
Matthias Weisser | 3f21a35 | 2016-08-18 07:55:05 +0200 | [diff] [blame] | 47 | !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) |
Manuel Pégourié-Gonnard | 325ce09 | 2016-02-22 10:33:34 +0100 | [diff] [blame] | 48 | #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h" |
| 49 | #endif |
| 50 | |
Paul Bakker | fa6a620 | 2013-10-28 18:48:30 +0100 | [diff] [blame] | 51 | #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 52 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 53 | #if !defined(_WIN32_WINNT) |
| 54 | #define _WIN32_WINNT 0x0400 |
| 55 | #endif |
Paul Bakker | 4a2bd0d | 2012-11-02 11:06:08 +0000 | [diff] [blame] | 56 | #include <windows.h> |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 57 | #include <wincrypt.h> |
| 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | 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] | 60 | size_t *olen ) |
| 61 | { |
| 62 | HCRYPTPROV provider; |
Paul Bakker | 6bcfc67 | 2011-12-05 13:54:00 +0000 | [diff] [blame] | 63 | ((void) data); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 64 | *olen = 0; |
| 65 | |
| 66 | if( CryptAcquireContext( &provider, NULL, NULL, |
| 67 | PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE ) |
| 68 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 72 | if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE ) |
Attila Molnar | 2791ba1 | 2016-01-26 11:39:26 +0100 | [diff] [blame] | 73 | { |
| 74 | CryptReleaseContext( provider, 0 ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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 | |
| 78 | CryptReleaseContext( provider, 0 ); |
| 79 | *olen = len; |
| 80 | |
| 81 | return( 0 ); |
| 82 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +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. |
| 87 | * Since there is no wrapper in the libc yet, use the generic syscall wrapper |
| 88 | * available in GNU libc and compatible libc's (eg uClibc). |
| 89 | */ |
| 90 | #if defined(__linux__) && defined(__GLIBC__) |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 91 | #include <unistd.h> |
| 92 | #include <sys/syscall.h> |
| 93 | #if defined(SYS_getrandom) |
| 94 | #define HAVE_GETRANDOM |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 95 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 96 | static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags ) |
| 97 | { |
Manuel Pégourié-Gonnard | bcf13ba | 2015-06-22 18:06:17 +0200 | [diff] [blame] | 98 | /* MemSan cannot understand that the syscall writes to the buffer */ |
| 99 | #if defined(__has_feature) |
| 100 | #if __has_feature(memory_sanitizer) |
| 101 | memset( buf, 0, buflen ); |
| 102 | #endif |
| 103 | #endif |
| 104 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 105 | return( syscall( SYS_getrandom, buf, buflen, flags ) ); |
| 106 | } |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 107 | |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 108 | #include <sys/utsname.h> |
| 109 | /* Check if version is at least 3.17.0 */ |
| 110 | static int check_version_3_17_plus( void ) |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 111 | { |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 112 | int minor; |
| 113 | struct utsname un; |
| 114 | const char *ver; |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 116 | /* Get version information */ |
| 117 | uname(&un); |
| 118 | ver = un.release; |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 120 | /* Check major version; assume a single digit */ |
| 121 | if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' ) |
| 122 | return( -1 ); |
| 123 | |
| 124 | if( ver[0] - '0' > 3 ) |
| 125 | return( 0 ); |
| 126 | |
| 127 | /* Ok, so now we know major == 3, check minor. |
| 128 | * Assume 1 or 2 digits. */ |
| 129 | if( ver[2] < '0' || ver[2] > '9' ) |
| 130 | return( -1 ); |
| 131 | |
| 132 | minor = ver[2] - '0'; |
| 133 | |
| 134 | if( ver[3] >= '0' && ver[3] <= '9' ) |
| 135 | minor = 10 * minor + ver[3] - '0'; |
| 136 | else if( ver [3] != '.' ) |
| 137 | return( -1 ); |
| 138 | |
| 139 | if( minor < 17 ) |
| 140 | return( -1 ); |
| 141 | |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 142 | return( 0 ); |
| 143 | } |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 144 | static int has_getrandom = -1; |
| 145 | #endif /* SYS_getrandom */ |
| 146 | #endif /* __linux__ */ |
Manuel Pégourié-Gonnard | 1829245 | 2015-01-09 14:34:13 +0100 | [diff] [blame] | 147 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 148 | #include <stdio.h> |
| 149 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | int mbedtls_platform_entropy_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 151 | unsigned char *output, size_t len, size_t *olen ) |
| 152 | { |
| 153 | FILE *file; |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 154 | size_t read_len; |
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) |
| 158 | if( has_getrandom == -1 ) |
| 159 | has_getrandom = ( check_version_3_17_plus() == 0 ); |
| 160 | |
| 161 | if( has_getrandom ) |
| 162 | { |
| 163 | int ret; |
| 164 | |
| 165 | if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 ) |
Manuel Pégourié-Gonnard | 9f145de | 2015-05-04 15:03:50 +0200 | [diff] [blame] | 166 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Manuel Pégourié-Gonnard | d97828e | 2015-04-29 14:03:28 +0200 | [diff] [blame] | 167 | |
| 168 | *olen = ret; |
| 169 | return( 0 ); |
| 170 | } |
| 171 | #endif /* HAVE_GETRANDOM */ |
| 172 | |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 173 | *olen = 0; |
| 174 | |
| 175 | file = fopen( "/dev/urandom", "rb" ); |
| 176 | if( file == NULL ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 178 | |
Manuel Pégourié-Gonnard | ea35666 | 2015-08-27 12:02:40 +0200 | [diff] [blame] | 179 | read_len = fread( output, 1, len, file ); |
| 180 | if( read_len != len ) |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 181 | { |
| 182 | fclose( file ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | fclose( file ); |
| 187 | *olen = len; |
| 188 | |
| 189 | return( 0 ); |
| 190 | } |
Paul Bakker | 9af723c | 2014-05-01 13:03:14 +0200 | [diff] [blame] | 191 | #endif /* _WIN32 && !EFIX64 && !EFI32 */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 193 | |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_TEST_NULL_ENTROPY) |
Simon Butcher | 4157b60 | 2016-06-12 00:31:33 +0100 | [diff] [blame] | 195 | int mbedtls_null_entropy_poll( void *data, |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 196 | unsigned char *output, size_t len, size_t *olen ) |
| 197 | { |
| 198 | ((void) data); |
Simon Butcher | ab5df40 | 2016-06-11 02:31:21 +0100 | [diff] [blame] | 199 | ((void) output); |
Janos Follath | 53de784 | 2016-06-08 15:29:18 +0100 | [diff] [blame] | 200 | *olen = 0; |
| 201 | |
| 202 | if( len < sizeof(unsigned char) ) |
| 203 | return( 0 ); |
| 204 | |
| 205 | *olen = sizeof(unsigned char); |
| 206 | |
| 207 | return( 0 ); |
| 208 | } |
| 209 | #endif |
| 210 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | #if defined(MBEDTLS_TIMING_C) |
| 212 | int mbedtls_hardclock_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 213 | unsigned char *output, size_t len, size_t *olen ) |
| 214 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | unsigned long timer = mbedtls_timing_hardclock(); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 216 | ((void) data); |
| 217 | *olen = 0; |
| 218 | |
| 219 | if( len < sizeof(unsigned long) ) |
| 220 | return( 0 ); |
| 221 | |
| 222 | memcpy( output, &timer, sizeof(unsigned long) ); |
| 223 | *olen = sizeof(unsigned long); |
| 224 | |
| 225 | return( 0 ); |
| 226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #endif /* MBEDTLS_TIMING_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | #if defined(MBEDTLS_HAVEGE_C) |
| 230 | int mbedtls_havege_poll( void *data, |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 231 | unsigned char *output, size_t len, size_t *olen ) |
| 232 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_havege_state *hs = (mbedtls_havege_state *) data; |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 234 | *olen = 0; |
| 235 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | if( mbedtls_havege_random( hs, output, len ) != 0 ) |
| 237 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 238 | |
| 239 | *olen = len; |
| 240 | |
| 241 | return( 0 ); |
| 242 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | #endif /* MBEDTLS_HAVEGE_C */ |
Paul Bakker | 6083fd2 | 2011-12-03 21:45:14 +0000 | [diff] [blame] | 244 | |
Paul Bakker | 9988d6b | 2016-06-01 11:29:42 +0100 | [diff] [blame] | 245 | #if defined(MBEDTLS_ENTROPY_NV_SEED) |
| 246 | int mbedtls_nv_seed_poll( void *data, |
| 247 | unsigned char *output, size_t len, size_t *olen ) |
| 248 | { |
| 249 | unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE]; |
| 250 | size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE; |
| 251 | ((void) data); |
| 252 | |
| 253 | memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE ); |
| 254 | |
| 255 | if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 ) |
| 256 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 257 | |
| 258 | if( len < use_len ) |
| 259 | use_len = len; |
| 260 | |
| 261 | memcpy( output, buf, use_len ); |
| 262 | *olen = use_len; |
| 263 | |
| 264 | return( 0 ); |
| 265 | } |
| 266 | #endif /* MBEDTLS_ENTROPY_NV_SEED */ |
| 267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | #endif /* MBEDTLS_ENTROPY_C */ |