blob: 4ad878eef6cb01902da62817f361d6df4bc6c04a [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 *
7 * This file is provided under the Apache License 2.0, or the
8 * GNU General Public License v2.0 or later.
9 *
10 * **********
11 * Apache License 2.0:
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020012 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Paul Bakker6083fd22011-12-03 21:45:14 +000024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
26 *
27 * **********
28 * GNU General Public License v2.0 or later:
29 *
30 * This program is free software; you can redistribute it and/or modify
31 * it under the terms of the GNU General Public License as published by
32 * the Free Software Foundation; either version 2 of the License, or
33 * (at your option) any later version.
34 *
35 * This program is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
38 * GNU General Public License for more details.
39 *
40 * You should have received a copy of the GNU General Public License along
41 * with this program; if not, write to the Free Software Foundation, Inc.,
42 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
43 *
44 * **********
Paul Bakker6083fd22011-12-03 21:45:14 +000045 */
46
Nicholas Wilson2682edf2017-12-05 12:08:15 +000047#if defined(__linux__)
48/* Ensure that syscall() is available even when compiling with -std=c99 */
49#define _GNU_SOURCE
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000053#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020054#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000057
Gilles Peskine02b93292018-06-01 14:38:45 +020058#include <string.h>
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000061
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000062#include "mbedtls/entropy.h"
63#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000066#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000067#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000070#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010071#if defined(MBEDTLS_ENTROPY_NV_SEED)
72#include "mbedtls/platform.h"
73#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010076
77#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040078 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
79 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010080#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
81#endif
82
Paul Bakkerfa6a6202013-10-28 18:48:30 +010083#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000084
Paul Bakker6083fd22011-12-03 21:45:14 +000085#if !defined(_WIN32_WINNT)
86#define _WIN32_WINNT 0x0400
87#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000088#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000089#include <wincrypt.h>
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000092 size_t *olen )
93{
94 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000095 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000096 *olen = 0;
97
98 if( CryptAcquireContext( &provider, NULL, NULL,
99 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000102 }
103
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200104 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +0100105 {
106 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +0100108 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000109
110 CryptReleaseContext( provider, 0 );
111 *olen = len;
112
113 return( 0 );
114}
Paul Bakker9af723c2014-05-01 13:03:14 +0200115#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +0000116
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100117/*
118 * Test for Linux getrandom() support.
119 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
120 * available in GNU libc and compatible libc's (eg uClibc).
121 */
122#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100123#include <unistd.h>
124#include <sys/syscall.h>
125#if defined(SYS_getrandom)
126#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100127#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200128
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100129static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
130{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200131 /* MemSan cannot understand that the syscall writes to the buffer */
132#if defined(__has_feature)
133#if __has_feature(memory_sanitizer)
134 memset( buf, 0, buflen );
135#endif
136#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100137 return( syscall( SYS_getrandom, buf, buflen, flags ) );
138}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200139#endif /* SYS_getrandom */
140#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100141
Paul Bakker6083fd22011-12-03 21:45:14 +0000142#include <stdio.h>
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000145 unsigned char *output, size_t len, size_t *olen )
146{
147 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200148 size_t read_len;
Hanno Becker27516972018-10-18 11:49:25 +0100149 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000150 ((void) data);
151
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200152#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100153 ret = getrandom_wrapper( output, len, 0 );
154 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200155 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200156 *olen = ret;
157 return( 0 );
158 }
Hanno Becker27516972018-10-18 11:49:25 +0100159 else if( errno != ENOSYS )
160 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
161 /* Fall through if the system call isn't known. */
162#else
Hanno Becker9772da82018-11-05 11:43:07 +0000163 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200164#endif /* HAVE_GETRANDOM */
165
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 *olen = 0;
167
168 file = fopen( "/dev/urandom", "rb" );
169 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200171
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200172 read_len = fread( output, 1, len, file );
173 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000174 {
175 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 }
178
179 fclose( file );
180 *olen = len;
181
182 return( 0 );
183}
Paul Bakker9af723c2014-05-01 13:03:14 +0200184#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000186
Simon Butcherab5df402016-06-11 02:31:21 +0100187#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100188int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100189 unsigned char *output, size_t len, size_t *olen )
190{
191 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100192 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100193 *olen = 0;
194
195 if( len < sizeof(unsigned char) )
196 return( 0 );
197
198 *olen = sizeof(unsigned char);
199
200 return( 0 );
201}
202#endif
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#if defined(MBEDTLS_TIMING_C)
205int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000206 unsigned char *output, size_t len, size_t *olen )
207{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000209 ((void) data);
210 *olen = 0;
211
212 if( len < sizeof(unsigned long) )
213 return( 0 );
214
215 memcpy( output, &timer, sizeof(unsigned long) );
216 *olen = sizeof(unsigned long);
217
218 return( 0 );
219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_HAVEGE_C)
223int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000224 unsigned char *output, size_t len, size_t *olen )
225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000227 *olen = 0;
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 if( mbedtls_havege_random( hs, output, len ) != 0 )
230 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000231
232 *olen = len;
233
234 return( 0 );
235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000237
Paul Bakker9988d6b2016-06-01 11:29:42 +0100238#if defined(MBEDTLS_ENTROPY_NV_SEED)
239int mbedtls_nv_seed_poll( void *data,
240 unsigned char *output, size_t len, size_t *olen )
241{
242 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
243 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
244 ((void) data);
245
246 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
247
248 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
249 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
250
251 if( len < use_len )
252 use_len = len;
253
254 memcpy( output, buf, use_len );
255 *olen = use_len;
256
257 return( 0 );
258}
259#endif /* MBEDTLS_ENTROPY_NV_SEED */
260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_ENTROPY_C */