blob: 62fb4afbf510a2413060f6c9d415b5c420ef89a3 [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
Gilles Peskinedb09ef62020-06-03 01:43:33 +020027#include "common.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000028
Gilles Peskine02b93292018-06-01 14:38:45 +020029#include <string.h>
30
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000033#include "mbedtls/entropy.h"
34#include "mbedtls/entropy_poll.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000035#include "mbedtls/error.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000042#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010043#if defined(MBEDTLS_ENTROPY_NV_SEED)
44#include "mbedtls/platform.h"
45#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010048
49#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040050 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
Ørjan Malde479d8de2020-05-20 09:32:39 +000051 !defined(__HAIKU__) && !defined(__midipix__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010052#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
53#endif
54
Paul Bakkerfa6a6202013-10-28 18:48:30 +010055#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000056
Paul Bakker6083fd22011-12-03 21:45:14 +000057#if !defined(_WIN32_WINNT)
58#define _WIN32_WINNT 0x0400
59#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000060#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000061#include <wincrypt.h>
62
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000064 size_t *olen )
65{
66 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000067 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000068 *olen = 0;
69
70 if( CryptAcquireContext( &provider, NULL, NULL,
71 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
72 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000074 }
75
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020076 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010077 {
78 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010080 }
Paul Bakker6083fd22011-12-03 21:45:14 +000081
82 CryptReleaseContext( provider, 0 );
83 *olen = len;
84
85 return( 0 );
86}
Paul Bakker9af723c2014-05-01 13:03:14 +020087#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000088
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010089/*
90 * Test for Linux getrandom() support.
91 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
92 * available in GNU libc and compatible libc's (eg uClibc).
93 */
Ørjan Malde479d8de2020-05-20 09:32:39 +000094#if ((defined(__linux__) && defined(__GLIBC__)) || defined(__midipix__))
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010095#include <unistd.h>
96#include <sys/syscall.h>
97#if defined(SYS_getrandom)
98#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +010099#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200100
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100101static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
102{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200103 /* MemSan cannot understand that the syscall writes to the buffer */
104#if defined(__has_feature)
105#if __has_feature(memory_sanitizer)
106 memset( buf, 0, buflen );
107#endif
108#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100109 return( syscall( SYS_getrandom, buf, buflen, flags ) );
110}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200111#endif /* SYS_getrandom */
Ørjan Malde479d8de2020-05-20 09:32:39 +0000112#endif /* __linux__ || __midipix__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100113
nia9f5312c2020-06-11 13:32:13 +0100114/*
115 * Some BSD systems provide KERN_ARND.
116 * This is equivalent to reading from /dev/urandom, only it doesn't require an
117 * open file descriptor, and provides up to 256 bytes per call (basically the
118 * same as getentropy(), but with a longer history).
119 *
120 * Documentation: https://netbsd.gw.com/cgi-bin/man-cgi?sysctl+7
121 */
122#if (defined(__FreeBSD__) || defined(__NetBSD__)) && !defined(HAVE_GETRANDOM)
123#include <sys/param.h>
124#include <sys/sysctl.h>
125#if defined(KERN_ARND)
126#define HAVE_SYSCTL_ARND
127
nia8373c862020-06-24 17:15:02 +0100128static int sysctl_arnd_wrapper( unsigned char *buf, size_t buflen )
nia9f5312c2020-06-11 13:32:13 +0100129{
130 int name[2];
131 size_t len;
132
133 name[0] = CTL_KERN;
134 name[1] = KERN_ARND;
135
136 while( buflen > 0 )
137 {
138 len = buflen > 256 ? 256 : buflen;
nia8373c862020-06-24 17:15:02 +0100139 if( sysctl(name, 2, buf, &len, NULL, 0) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100140 return( -1 );
141 buflen -= len;
nia8373c862020-06-24 17:15:02 +0100142 buf += len;
nia9f5312c2020-06-11 13:32:13 +0100143 }
144 return( 0 );
145}
146#endif /* KERN_ARND */
147#endif /* __FreeBSD__ || __NetBSD__ */
148
Paul Bakker6083fd22011-12-03 21:45:14 +0000149#include <stdio.h>
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 unsigned char *output, size_t len, size_t *olen )
153{
154 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200155 size_t read_len;
Janos Follath24eed8d2019-11-22 13:21:35 +0000156 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker6083fd22011-12-03 21:45:14 +0000157 ((void) data);
158
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200159#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100160 ret = getrandom_wrapper( output, len, 0 );
161 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200162 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200163 *olen = ret;
164 return( 0 );
165 }
Hanno Becker27516972018-10-18 11:49:25 +0100166 else if( errno != ENOSYS )
167 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
168 /* Fall through if the system call isn't known. */
169#else
Hanno Becker9772da82018-11-05 11:43:07 +0000170 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200171#endif /* HAVE_GETRANDOM */
172
nia9f5312c2020-06-11 13:32:13 +0100173#if defined(HAVE_SYSCTL_ARND)
174 ((void) file);
175 ((void) read_len);
niaf4d9f212020-06-19 16:14:27 +0100176 if( sysctl_arnd_wrapper( output, len ) == -1 )
nia9f5312c2020-06-11 13:32:13 +0100177 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
178 *olen = len;
179 return( 0 );
180#else
181
Paul Bakker6083fd22011-12-03 21:45:14 +0000182 *olen = 0;
183
184 file = fopen( "/dev/urandom", "rb" );
185 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200187
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200188 read_len = fread( output, 1, len, file );
189 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000190 {
191 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000193 }
194
195 fclose( file );
196 *olen = len;
197
198 return( 0 );
nia9f5312c2020-06-11 13:32:13 +0100199#endif /* HAVE_SYSCTL_ARND */
Paul Bakker6083fd22011-12-03 21:45:14 +0000200}
Paul Bakker9af723c2014-05-01 13:03:14 +0200201#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000203
Simon Butcherab5df402016-06-11 02:31:21 +0100204#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100205int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100206 unsigned char *output, size_t len, size_t *olen )
207{
208 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100209 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100210 *olen = 0;
211
212 if( len < sizeof(unsigned char) )
213 return( 0 );
214
215 *olen = sizeof(unsigned char);
216
217 return( 0 );
218}
219#endif
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_TIMING_C)
222int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000223 unsigned char *output, size_t len, size_t *olen )
224{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000226 ((void) data);
227 *olen = 0;
228
229 if( len < sizeof(unsigned long) )
230 return( 0 );
231
232 memcpy( output, &timer, sizeof(unsigned long) );
233 *olen = sizeof(unsigned long);
234
235 return( 0 );
236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#if defined(MBEDTLS_HAVEGE_C)
240int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000241 unsigned char *output, size_t len, size_t *olen )
242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000244 *olen = 0;
245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246 if( mbedtls_havege_random( hs, output, len ) != 0 )
247 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000248
249 *olen = len;
250
251 return( 0 );
252}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000254
Paul Bakker9988d6b2016-06-01 11:29:42 +0100255#if defined(MBEDTLS_ENTROPY_NV_SEED)
256int mbedtls_nv_seed_poll( void *data,
257 unsigned char *output, size_t len, size_t *olen )
258{
259 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
260 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
261 ((void) data);
262
263 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
264
265 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
266 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
267
268 if( len < use_len )
269 use_len = len;
270
271 memcpy( output, buf, use_len );
272 *olen = use_len;
273
274 return( 0 );
275}
276#endif /* MBEDTLS_ENTROPY_NV_SEED */
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278#endif /* MBEDTLS_ENTROPY_C */