blob: a858c1892b3c06b5fe97145d12be1dfcc39b8c03 [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
Gilles Peskinea1f9ef02020-09-30 22:18:13 +020020#if defined(__linux__) && !defined(_GNU_SOURCE)
Nicholas Wilson2682edf2017-12-05 12:08:15 +000021/* Ensure that syscall() is available even when compiling with -std=c99 */
22#define _GNU_SOURCE
23#endif
24
Gilles Peskinedb09ef62020-06-03 01:43:33 +020025#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000026
Gilles Peskine02b93292018-06-01 14:38:45 +020027#include <string.h>
28
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/entropy.h"
32#include "mbedtls/entropy_poll.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000033#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010041#include "mbedtls/platform.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010044
45#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040046 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000047 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010048#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
49#endif
50
Paul Bakkerfa6a6202013-10-28 18:48:30 +010051#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Paul Bakker6083fd22011-12-03 21:45:14 +000053#if !defined(_WIN32_WINNT)
54#define _WIN32_WINNT 0x0400
55#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000056#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000057#include <wincrypt.h>
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000060 size_t *olen )
61{
62 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000063 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000064 *olen = 0;
65
66 if( CryptAcquireContext( &provider, NULL, NULL,
67 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
68 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000070 }
71
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020072 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010073 {
74 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010076 }
Paul Bakker6083fd22011-12-03 21:45:14 +000077
78 CryptReleaseContext( provider, 0 );
79 *olen = len;
80
81 return( 0 );
82}
Paul Bakker9af723c2014-05-01 13:03:14 +020083#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000084
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010085/*
86 * Test for Linux getrandom() support.
Gilles Peskinec6468ee2020-09-30 22:11:13 +020087 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010088 * available in GNU libc and compatible libc's (eg uClibc).
89 */
Gilles Peskinec6468ee2020-09-30 22:11:13 +020090#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010091#include <unistd.h>
92#include <sys/syscall.h>
93#if defined(SYS_getrandom)
94#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +010095#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020096
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
98{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020099 /* MemSan cannot understand that the syscall writes to the buffer */
100#if defined(__has_feature)
101#if __has_feature(memory_sanitizer)
102 memset( buf, 0, buflen );
103#endif
104#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105 return( syscall( SYS_getrandom, buf, buflen, flags ) );
106}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200107#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000108#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100109
David Carlier2b8c2652020-12-28 15:51:14 +0000110#if defined(__FreeBSD__) || defined(__DragonFly__)
111#include <sys/param.h>
112#if (defined(__FreeBSD__) && __FreeBSD_version >= 1200000) || \
113 (defined(__DragonFly__) && __DragonFly_version >= 500700)
114#include <errno.h>
115#include <sys/random.h>
116#define HAVE_GETRANDOM
117static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
118{
119 return getrandom( buf, buflen, flags );
120}
121#endif /* (__FreeBSD__ && __FreeBSD_version >= 1200000) ||
122 (__DragonFly__ && __DragonFly_version >= 500700) */
123#endif /* __FreeBSD__ || __DragonFly__ */
124
nia9f5312c2020-06-11 13:32:13 +0100125/*
126 * Some BSD systems provide KERN_ARND.
127 * This is equivalent to reading from /dev/urandom, only it doesn't require an
128 * open file descriptor, and provides up to 256 bytes per call (basically the
129 * same as getentropy(), but with a longer history).
130 *
131 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
132 */
133#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
134#include <sys/param.h>
135#include <sys/sysctl.h>
136#if defined(KERN_ARND)
137#define HAVE_SYSCTL_ARND
138
nia8373c862020-06-24 17:15:02 +0100139static int sysctl_arnd_wrapper( unsigned char *buf, size_t buflen )
nia9f5312c2020-06-11 13:32:13 +0100140{
141 int name[2];
142 size_t len;
143
144 name[0] = CTL_KERN;
145 name[1] = KERN_ARND;
146
147 while( buflen > 0 )
148 {
149 len = buflen > 256 ? 256 : buflen;
nia8373c862020-06-24 17:15:02 +0100150 if( sysctl(name, 2, buf, &len, NULL, 0) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100151 return( -1 );
152 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100153 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100154 }
155 return( 0 );
156}
157#endif /* KERN_ARND */
158#endif /* __FreeBSD__ || __NetBSD__ */
159
Paul Bakker6083fd22011-12-03 21:45:14 +0000160#include <stdio.h>
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000163 unsigned char *output, size_t len, size_t *olen )
164{
165 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200166 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 ((void) data);
169
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200170#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100171 ret = getrandom_wrapper( output, len, 0 );
172 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200173 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200174 *olen = ret;
175 return( 0 );
176 }
Hanno Becker27516972018-10-18 11:49:25 +0100177 else if( errno != ENOSYS )
178 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
179 /* Fall through if the system call isn't known. */
180#else
Hanno Becker9772da82018-11-05 11:43:07 +0000181 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200182#endif /* HAVE_GETRANDOM */
183
nia9f5312c2020-06-11 13:32:13 +0100184#if defined(HAVE_SYSCTL_ARND)
185 ((void) file);
186 ((void) read_len);
niaf4d9f212020-06-19 16:14:27 +0100187 if( sysctl_arnd_wrapper( output, len ) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100188 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
189 *olen = len;
190 return( 0 );
191#else
192
Paul Bakker6083fd22011-12-03 21:45:14 +0000193 *olen = 0;
194
195 file = fopen( "/dev/urandom", "rb" );
196 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200198
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200199 read_len = fread( output, 1, len, file );
200 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000201 {
202 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000204 }
205
206 fclose( file );
207 *olen = len;
208
209 return( 0 );
nia9f5312c2020-06-11 13:32:13 +0100210#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000211}
Paul Bakker9af723c2014-05-01 13:03:14 +0200212#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000214
Simon Butcherab5df402016-06-11 02:31:21 +0100215#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100216int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100217 unsigned char *output, size_t len, size_t *olen )
218{
219 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100220 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100221
Gilles Peskine8e1e46e2021-02-08 22:02:12 +0100222 *olen = 0;
Janos Follath53de7842016-06-08 15:29:18 +0100223 if( len < sizeof(unsigned char) )
224 return( 0 );
225
Gilles Peskine8e1e46e2021-02-08 22:02:12 +0100226 output[0] = 0;
Janos Follath53de7842016-06-08 15:29:18 +0100227 *olen = sizeof(unsigned char);
Janos Follath53de7842016-06-08 15:29:18 +0100228 return( 0 );
229}
230#endif
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#if defined(MBEDTLS_TIMING_C)
233int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000234 unsigned char *output, size_t len, size_t *olen )
235{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 ((void) data);
238 *olen = 0;
239
240 if( len < sizeof(unsigned long) )
241 return( 0 );
242
243 memcpy( output, &timer, sizeof(unsigned long) );
244 *olen = sizeof(unsigned long);
245
246 return( 0 );
247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_HAVEGE_C)
251int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000252 unsigned char *output, size_t len, size_t *olen )
253{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000255 *olen = 0;
256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 if( mbedtls_havege_random( hs, output, len ) != 0 )
258 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000259
260 *olen = len;
261
262 return( 0 );
263}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000265
Paul Bakker9988d6b2016-06-01 11:29:42 +0100266#if defined(MBEDTLS_ENTROPY_NV_SEED)
267int mbedtls_nv_seed_poll( void *data,
268 unsigned char *output, size_t len, size_t *olen )
269{
270 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
271 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
272 ((void) data);
273
274 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
275
276 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
277 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
278
279 if( len < use_len )
280 use_len = len;
281
282 memcpy( output, buf, use_len );
283 *olen = use_len;
284
285 return( 0 );
286}
287#endif /* MBEDTLS_ENTROPY_NV_SEED */
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_ENTROPY_C */