blob: 58a23db347599a72a97656b6dddb57ac4067d41a [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +000047 */
48
Nicholas Wilson2682edf2017-12-05 12:08:15 +000049#if defined(__linux__)
50/* Ensure that syscall() is available even when compiling with -std=c99 */
51#define _GNU_SOURCE
52#endif
53
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020056#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020058#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000059
Gilles Peskine02b93292018-06-01 14:38:45 +020060#include <string.h>
61
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000063
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000064#include "mbedtls/entropy.h"
65#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000068#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000069#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000071#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000072#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010073#if defined(MBEDTLS_ENTROPY_NV_SEED)
74#include "mbedtls/platform.h"
75#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010078
79#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040080 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
81 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010082#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
83#endif
84
Paul Bakkerfa6a6202013-10-28 18:48:30 +010085#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000086
Paul Bakker6083fd22011-12-03 21:45:14 +000087#if !defined(_WIN32_WINNT)
88#define _WIN32_WINNT 0x0400
89#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000090#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000091#include <wincrypt.h>
92
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000094 size_t *olen )
95{
96 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000097 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000098 *olen = 0;
99
100 if( CryptAcquireContext( &provider, NULL, NULL,
101 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000104 }
105
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200106 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +0100107 {
108 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +0100110 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000111
112 CryptReleaseContext( provider, 0 );
113 *olen = len;
114
115 return( 0 );
116}
Paul Bakker9af723c2014-05-01 13:03:14 +0200117#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +0000118
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100119/*
120 * Test for Linux getrandom() support.
121 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
122 * available in GNU libc and compatible libc's (eg uClibc).
123 */
124#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100125#include <unistd.h>
126#include <sys/syscall.h>
127#if defined(SYS_getrandom)
128#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100129#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200130
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100131static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
132{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200133 /* MemSan cannot understand that the syscall writes to the buffer */
134#if defined(__has_feature)
135#if __has_feature(memory_sanitizer)
136 memset( buf, 0, buflen );
137#endif
138#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100139 return( syscall( SYS_getrandom, buf, buflen, flags ) );
140}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200141#endif /* SYS_getrandom */
142#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100143
Paul Bakker6083fd22011-12-03 21:45:14 +0000144#include <stdio.h>
145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000147 unsigned char *output, size_t len, size_t *olen )
148{
149 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200150 size_t read_len;
Hanno Becker27516972018-10-18 11:49:25 +0100151 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 ((void) data);
153
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200154#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100155 ret = getrandom_wrapper( output, len, 0 );
156 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200157 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200158 *olen = ret;
159 return( 0 );
160 }
Hanno Becker27516972018-10-18 11:49:25 +0100161 else if( errno != ENOSYS )
162 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
163 /* Fall through if the system call isn't known. */
164#else
Hanno Becker9772da82018-11-05 11:43:07 +0000165 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200166#endif /* HAVE_GETRANDOM */
167
Paul Bakker6083fd22011-12-03 21:45:14 +0000168 *olen = 0;
169
170 file = fopen( "/dev/urandom", "rb" );
171 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200173
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200174 read_len = fread( output, 1, len, file );
175 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000176 {
177 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 }
180
181 fclose( file );
182 *olen = len;
183
184 return( 0 );
185}
Paul Bakker9af723c2014-05-01 13:03:14 +0200186#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000188
Simon Butcherab5df402016-06-11 02:31:21 +0100189#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100190int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100191 unsigned char *output, size_t len, size_t *olen )
192{
193 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100194 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100195 *olen = 0;
196
197 if( len < sizeof(unsigned char) )
198 return( 0 );
199
200 *olen = sizeof(unsigned char);
201
202 return( 0 );
203}
204#endif
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206#if defined(MBEDTLS_TIMING_C)
207int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000208 unsigned char *output, size_t len, size_t *olen )
209{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000211 ((void) data);
212 *olen = 0;
213
214 if( len < sizeof(unsigned long) )
215 return( 0 );
216
217 memcpy( output, &timer, sizeof(unsigned long) );
218 *olen = sizeof(unsigned long);
219
220 return( 0 );
221}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#if defined(MBEDTLS_HAVEGE_C)
225int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000226 unsigned char *output, size_t len, size_t *olen )
227{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000229 *olen = 0;
230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 if( mbedtls_havege_random( hs, output, len ) != 0 )
232 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000233
234 *olen = len;
235
236 return( 0 );
237}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000239
Paul Bakker9988d6b2016-06-01 11:29:42 +0100240#if defined(MBEDTLS_ENTROPY_NV_SEED)
241int mbedtls_nv_seed_poll( void *data,
242 unsigned char *output, size_t len, size_t *olen )
243{
244 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
245 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
246 ((void) data);
247
248 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
249
250 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
251 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
252
253 if( len < use_len )
254 use_len = len;
255
256 memcpy( output, buf, use_len );
257 *olen = use_len;
258
259 return( 0 );
260}
261#endif /* MBEDTLS_ENTROPY_NV_SEED */
262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#endif /* MBEDTLS_ENTROPY_C */