blob: dbb57008e32282df15137b1840a4986a9997b85a [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Bence Szépkúti44bfbe32020-08-19 16:54:51 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkúti4e9f7122020-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úti4e9f7122020-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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000054
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000055#include "mbedtls/entropy.h"
56#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#if defined(MBEDTLS_TIMING_C)
Rich Evans00ab4702015-02-06 13:43:58 +000059#include <string.h>
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000060#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000061#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000063#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000064#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010065#if defined(MBEDTLS_ENTROPY_NV_SEED)
66#include "mbedtls/platform.h"
67#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010070
71#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
72 !defined(__APPLE__) && !defined(_WIN32)
73#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
74#endif
75
Paul Bakkerfa6a6202013-10-28 18:48:30 +010076#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000077
Paul Bakker6083fd22011-12-03 21:45:14 +000078#if !defined(_WIN32_WINNT)
79#define _WIN32_WINNT 0x0400
80#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000081#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000082#include <wincrypt.h>
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000085 size_t *olen )
86{
87 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000088 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000089 *olen = 0;
90
91 if( CryptAcquireContext( &provider, NULL, NULL,
92 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
93 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000095 }
96
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020097 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010098 {
99 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +0100101 }
Paul Bakker6083fd22011-12-03 21:45:14 +0000102
103 CryptReleaseContext( provider, 0 );
104 *olen = len;
105
106 return( 0 );
107}
Paul Bakker9af723c2014-05-01 13:03:14 +0200108#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +0000109
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100110/*
111 * Test for Linux getrandom() support.
112 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
113 * available in GNU libc and compatible libc's (eg uClibc).
114 */
115#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100116#include <unistd.h>
117#include <sys/syscall.h>
118#if defined(SYS_getrandom)
119#define HAVE_GETRANDOM
Hanno Becker6b891972018-10-18 11:49:25 +0100120#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200121
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100122static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
123{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200124 /* MemSan cannot understand that the syscall writes to the buffer */
125#if defined(__has_feature)
126#if __has_feature(memory_sanitizer)
127 memset( buf, 0, buflen );
128#endif
129#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100130 return( syscall( SYS_getrandom, buf, buflen, flags ) );
131}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200132#endif /* SYS_getrandom */
133#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100134
Paul Bakker6083fd22011-12-03 21:45:14 +0000135#include <stdio.h>
136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000138 unsigned char *output, size_t len, size_t *olen )
139{
140 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200141 size_t read_len;
Hanno Becker6b891972018-10-18 11:49:25 +0100142 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000143 ((void) data);
144
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200145#if defined(HAVE_GETRANDOM)
Hanno Becker6b891972018-10-18 11:49:25 +0100146 ret = getrandom_wrapper( output, len, 0 );
147 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200148 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200149 *olen = ret;
150 return( 0 );
151 }
Hanno Becker6b891972018-10-18 11:49:25 +0100152 else if( errno != ENOSYS )
153 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
154 /* Fall through if the system call isn't known. */
155#else
Hanno Beckerbeedec82018-11-05 11:43:07 +0000156 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200157#endif /* HAVE_GETRANDOM */
158
Paul Bakker6083fd22011-12-03 21:45:14 +0000159 *olen = 0;
160
161 file = fopen( "/dev/urandom", "rb" );
162 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200164
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200165 read_len = fread( output, 1, len, file );
166 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000167 {
168 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000170 }
171
172 fclose( file );
173 *olen = len;
174
175 return( 0 );
176}
Paul Bakker9af723c2014-05-01 13:03:14 +0200177#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000179
Simon Butcherab5df402016-06-11 02:31:21 +0100180#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100181int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100182 unsigned char *output, size_t len, size_t *olen )
183{
184 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100185 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100186 *olen = 0;
187
188 if( len < sizeof(unsigned char) )
189 return( 0 );
190
191 *olen = sizeof(unsigned char);
192
193 return( 0 );
194}
195#endif
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_TIMING_C)
198int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000199 unsigned char *output, size_t len, size_t *olen )
200{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000202 ((void) data);
203 *olen = 0;
204
205 if( len < sizeof(unsigned long) )
206 return( 0 );
207
208 memcpy( output, &timer, sizeof(unsigned long) );
209 *olen = sizeof(unsigned long);
210
211 return( 0 );
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#if defined(MBEDTLS_HAVEGE_C)
216int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000217 unsigned char *output, size_t len, size_t *olen )
218{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000220 *olen = 0;
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 if( mbedtls_havege_random( hs, output, len ) != 0 )
223 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000224
225 *olen = len;
226
227 return( 0 );
228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000230
Paul Bakker9988d6b2016-06-01 11:29:42 +0100231#if defined(MBEDTLS_ENTROPY_NV_SEED)
232int mbedtls_nv_seed_poll( void *data,
233 unsigned char *output, size_t len, size_t *olen )
234{
235 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
236 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
237 ((void) data);
238
239 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
240
241 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
242 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
243
244 if( len < use_len )
245 use_len = len;
246
247 memcpy( output, buf, use_len );
248 *olen = use_len;
249
250 return( 0 );
251}
252#endif /* MBEDTLS_ENTROPY_NV_SEED */
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_ENTROPY_C */