blob: 0f992f34c79fc4dfaf36a88ae6ce4032a43fc09e [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker6083fd22011-12-03 21:45:14 +000018 */
19
Nicholas Wilson2682edf2017-12-05 12:08:15 +000020#if defined(__linux__)
okhowang(王沛文)9c46a602020-09-03 16:55:01 +080021#if !defined(_GNU_SOURCE)
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022/* Ensure that syscall() is available even when compiling with -std=c99 */
23#define _GNU_SOURCE
24#endif
okhowang(王沛文)9c46a602020-09-03 16:55:01 +080025#include <features.h>
26#if __GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 25)
27#define HAVE_SYS_RANDOM 1
28#endif
29#endif
Nicholas Wilson2682edf2017-12-05 12:08:15 +000030
Gilles Peskinedb09ef62020-06-03 01:43:33 +020031#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Gilles Peskine02b93292018-06-01 14:38:45 +020033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/entropy.h"
38#include "mbedtls/entropy_poll.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000039#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000043#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000046#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010047#if defined(MBEDTLS_ENTROPY_NV_SEED)
48#include "mbedtls/platform.h"
49#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010052
53#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040054 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000055 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010056#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
57#endif
58
Paul Bakkerfa6a6202013-10-28 18:48:30 +010059#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000060
Paul Bakker6083fd22011-12-03 21:45:14 +000061#if !defined(_WIN32_WINNT)
62#define _WIN32_WINNT 0x0400
63#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000064#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000065#include <wincrypt.h>
66
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000068 size_t *olen )
69{
70 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000071 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000072 *olen = 0;
73
74 if( CryptAcquireContext( &provider, NULL, NULL,
75 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
76 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000078 }
79
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020080 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010081 {
82 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010084 }
Paul Bakker6083fd22011-12-03 21:45:14 +000085
86 CryptReleaseContext( provider, 0 );
87 *olen = len;
88
89 return( 0 );
90}
Paul Bakker9af723c2014-05-01 13:03:14 +020091#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000092
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010093/*
94 * Test for Linux getrandom() support.
okhowang(王沛文)9c46a602020-09-03 16:55:01 +080095 * When the C library is GNU libc and its version is greater than 2.25,
96 * include sys/random.h to use getrandom(),
97 * otherwise use the generic use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010098 * available in GNU libc and compatible libc's (eg uClibc).
99 */
okhowang(王沛文)9c46a602020-09-03 16:55:01 +0800100#if HAVE_SYS_RANDOM
101#include <sys/random.h>
102#include <errno.h>
103#define HAVE_GETRANDOM
104#elif (defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105#include <unistd.h>
106#include <sys/syscall.h>
107#if defined(SYS_getrandom)
108#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100109#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200110
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100111static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
112{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200113 /* MemSan cannot understand that the syscall writes to the buffer */
114#if defined(__has_feature)
115#if __has_feature(memory_sanitizer)
116 memset( buf, 0, buflen );
117#endif
118#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100119 return( syscall( SYS_getrandom, buf, buflen, flags ) );
120}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200121#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000122#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100123
nia9f5312c2020-06-11 13:32:13 +0100124/*
125 * Some BSD systems provide KERN_ARND.
126 * This is equivalent to reading from /dev/urandom, only it doesn't require an
127 * open file descriptor, and provides up to 256 bytes per call (basically the
128 * same as getentropy(), but with a longer history).
129 *
130 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
131 */
132#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
133#include <sys/param.h>
134#include <sys/sysctl.h>
135#if defined(KERN_ARND)
136#define HAVE_SYSCTL_ARND
137
nia8373c862020-06-24 17:15:02 +0100138static int sysctl_arnd_wrapper( unsigned char *buf, size_t buflen )
nia9f5312c2020-06-11 13:32:13 +0100139{
140 int name[2];
141 size_t len;
142
143 name[0] = CTL_KERN;
144 name[1] = KERN_ARND;
145
146 while( buflen > 0 )
147 {
148 len = buflen > 256 ? 256 : buflen;
nia8373c862020-06-24 17:15:02 +0100149 if( sysctl(name, 2, buf, &len, NULL, 0) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100150 return( -1 );
151 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100152 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100153 }
154 return( 0 );
155}
156#endif /* KERN_ARND */
157#endif /* __FreeBSD__ || __NetBSD__ */
158
Paul Bakker6083fd22011-12-03 21:45:14 +0000159#include <stdio.h>
160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 unsigned char *output, size_t len, size_t *olen )
163{
164 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200165 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000166 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 ((void) data);
168
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200169#if defined(HAVE_GETRANDOM)
okhowang(王沛文)9c46a602020-09-03 16:55:01 +0800170#if HAVE_SYS_RANDOM
171 ret = getrandom(output, len, 0);
172#else
Hanno Becker27516972018-10-18 11:49:25 +0100173 ret = getrandom_wrapper( output, len, 0 );
okhowang(王沛文)9c46a602020-09-03 16:55:01 +0800174#endif
Hanno Becker27516972018-10-18 11:49:25 +0100175 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200176 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200177 *olen = ret;
178 return( 0 );
179 }
Hanno Becker27516972018-10-18 11:49:25 +0100180 else if( errno != ENOSYS )
181 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
182 /* Fall through if the system call isn't known. */
183#else
Hanno Becker9772da82018-11-05 11:43:07 +0000184 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200185#endif /* HAVE_GETRANDOM */
186
nia9f5312c2020-06-11 13:32:13 +0100187#if defined(HAVE_SYSCTL_ARND)
188 ((void) file);
189 ((void) read_len);
niaf4d9f212020-06-19 16:14:27 +0100190 if( sysctl_arnd_wrapper( output, len ) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100191 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
192 *olen = len;
193 return( 0 );
194#else
195
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 *olen = 0;
197
198 file = fopen( "/dev/urandom", "rb" );
199 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200201
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200202 read_len = fread( output, 1, len, file );
203 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000204 {
205 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000207 }
208
209 fclose( file );
210 *olen = len;
211
212 return( 0 );
nia9f5312c2020-06-11 13:32:13 +0100213#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000214}
Paul Bakker9af723c2014-05-01 13:03:14 +0200215#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000217
Simon Butcherab5df402016-06-11 02:31:21 +0100218#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100219int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100220 unsigned char *output, size_t len, size_t *olen )
221{
222 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100223 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100224 *olen = 0;
225
226 if( len < sizeof(unsigned char) )
227 return( 0 );
228
229 *olen = sizeof(unsigned char);
230
231 return( 0 );
232}
233#endif
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_TIMING_C)
236int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 unsigned char *output, size_t len, size_t *olen )
238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000240 ((void) data);
241 *olen = 0;
242
243 if( len < sizeof(unsigned long) )
244 return( 0 );
245
246 memcpy( output, &timer, sizeof(unsigned long) );
247 *olen = sizeof(unsigned long);
248
249 return( 0 );
250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#if defined(MBEDTLS_HAVEGE_C)
254int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000255 unsigned char *output, size_t len, size_t *olen )
256{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000258 *olen = 0;
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 if( mbedtls_havege_random( hs, output, len ) != 0 )
261 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000262
263 *olen = len;
264
265 return( 0 );
266}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000268
Paul Bakker9988d6b2016-06-01 11:29:42 +0100269#if defined(MBEDTLS_ENTROPY_NV_SEED)
270int mbedtls_nv_seed_poll( void *data,
271 unsigned char *output, size_t len, size_t *olen )
272{
273 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
274 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
275 ((void) data);
276
277 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
278
279 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
280 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
281
282 if( len < use_len )
283 use_len = len;
284
285 memcpy( output, buf, use_len );
286 *olen = use_len;
287
288 return( 0 );
289}
290#endif /* MBEDTLS_ENTROPY_NV_SEED */
291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292#endif /* MBEDTLS_ENTROPY_C */