blob: aaff26f48e5b6b7252c19a994cdc7a15259f9147 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Paul Bakker9988d6b2016-06-01 11:29:42 +01004 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000020 */
21
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022#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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
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.
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 Malde479d8de2020-05-20 09:32:39 +000098#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010099#include <unistd.h>
100#include <sys/syscall.h>
101#if defined(SYS_getrandom)
102#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100103#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200104
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
106{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200107 /* 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é-Gonnard18292452015-01-09 14:34:13 +0100113 return( syscall( SYS_getrandom, buf, buflen, flags ) );
114}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200115#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000116#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100117
nia9f5312c2020-06-11 13:32:13 +0100118/*
119 * Some BSD systems provide KERN_ARND.
120 * This is equivalent to reading from /dev/urandom, only it doesn't require an
121 * open file descriptor, and provides up to 256 bytes per call (basically the
122 * same as getentropy(), but with a longer history).
123 *
124 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
125 */
126#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
127#include <sys/param.h>
128#include <sys/sysctl.h>
129#if defined(KERN_ARND)
130#define HAVE_SYSCTL_ARND
131
niaf4d9f212020-06-19 16:14:27 +0100132static int sysctl_arnd_wrapper( void *buf, size_t buflen )
nia9f5312c2020-06-11 13:32:13 +0100133{
niae3fdcfa2020-06-23 21:03:01 +0100134 unsigned char *output = buf;
nia9f5312c2020-06-11 13:32:13 +0100135 int name[2];
136 size_t len;
137
138 name[0] = CTL_KERN;
139 name[1] = KERN_ARND;
140
141 while( buflen > 0 )
142 {
143 len = buflen > 256 ? 256 : buflen;
niae3fdcfa2020-06-23 21:03:01 +0100144 if( sysctl(name, 2, output, &len, NULL, 0) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100145 return( -1 );
146 buflen -= len;
niae3fdcfa2020-06-23 21:03:01 +0100147 output += len;
nia9f5312c2020-06-11 13:32:13 +0100148 }
149 return( 0 );
150}
151#endif /* KERN_ARND */
152#endif /* __FreeBSD__ || __NetBSD__ */
153
Paul Bakker6083fd22011-12-03 21:45:14 +0000154#include <stdio.h>
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000157 unsigned char *output, size_t len, size_t *olen )
158{
159 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200160 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 ((void) data);
163
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200164#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100165 ret = getrandom_wrapper( output, len, 0 );
166 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200167 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200168 *olen = ret;
169 return( 0 );
170 }
Hanno Becker27516972018-10-18 11:49:25 +0100171 else if( errno != ENOSYS )
172 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
173 /* Fall through if the system call isn't known. */
174#else
Hanno Becker9772da82018-11-05 11:43:07 +0000175 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200176#endif /* HAVE_GETRANDOM */
177
nia9f5312c2020-06-11 13:32:13 +0100178#if defined(HAVE_SYSCTL_ARND)
179 ((void) file);
180 ((void) read_len);
niaf4d9f212020-06-19 16:14:27 +0100181 if( sysctl_arnd_wrapper( output, len ) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100182 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
183 *olen = len;
184 return( 0 );
185#else
186
Paul Bakker6083fd22011-12-03 21:45:14 +0000187 *olen = 0;
188
189 file = fopen( "/dev/urandom", "rb" );
190 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200192
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200193 read_len = fread( output, 1, len, file );
194 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000195 {
196 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000198 }
199
200 fclose( file );
201 *olen = len;
202
203 return( 0 );
nia9f5312c2020-06-11 13:32:13 +0100204#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000205}
Paul Bakker9af723c2014-05-01 13:03:14 +0200206#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000208
Simon Butcherab5df402016-06-11 02:31:21 +0100209#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100210int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100211 unsigned char *output, size_t len, size_t *olen )
212{
213 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100214 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100215 *olen = 0;
216
217 if( len < sizeof(unsigned char) )
218 return( 0 );
219
220 *olen = sizeof(unsigned char);
221
222 return( 0 );
223}
224#endif
225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_TIMING_C)
227int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000228 unsigned char *output, size_t len, size_t *olen )
229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000231 ((void) data);
232 *olen = 0;
233
234 if( len < sizeof(unsigned long) )
235 return( 0 );
236
237 memcpy( output, &timer, sizeof(unsigned long) );
238 *olen = sizeof(unsigned long);
239
240 return( 0 );
241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#if defined(MBEDTLS_HAVEGE_C)
245int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000246 unsigned char *output, size_t len, size_t *olen )
247{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000249 *olen = 0;
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 if( mbedtls_havege_random( hs, output, len ) != 0 )
252 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000253
254 *olen = len;
255
256 return( 0 );
257}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000259
Paul Bakker9988d6b2016-06-01 11:29:42 +0100260#if defined(MBEDTLS_ENTROPY_NV_SEED)
261int mbedtls_nv_seed_poll( void *data,
262 unsigned char *output, size_t len, size_t *olen )
263{
264 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
265 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
266 ((void) data);
267
268 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
269
270 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
271 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
272
273 if( len < use_len )
274 use_len = len;
275
276 memcpy( output, buf, use_len );
277 *olen = use_len;
278
279 return( 0 );
280}
281#endif /* MBEDTLS_ENTROPY_NV_SEED */
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_ENTROPY_C */