blob: 308c53cefd5d59257f56c6a76ac30485949d4cdf [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000027
Gilles Peskine02b93292018-06-01 14:38:45 +020028#include <string.h>
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/entropy.h"
33#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010041#if defined(MBEDTLS_ENTROPY_NV_SEED)
42#include "mbedtls/platform.h"
43#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010046
47#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040048 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
49 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010050#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
51#endif
52
Paul Bakkerfa6a6202013-10-28 18:48:30 +010053#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000054
Paul Bakker6083fd22011-12-03 21:45:14 +000055#if !defined(_WIN32_WINNT)
56#define _WIN32_WINNT 0x0400
57#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000058#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000059#include <wincrypt.h>
60
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000062 size_t *olen )
63{
64 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000065 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000066 *olen = 0;
67
68 if( CryptAcquireContext( &provider, NULL, NULL,
69 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
70 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000072 }
73
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020074 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010075 {
76 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010078 }
Paul Bakker6083fd22011-12-03 21:45:14 +000079
80 CryptReleaseContext( provider, 0 );
81 *olen = len;
82
83 return( 0 );
84}
Paul Bakker9af723c2014-05-01 13:03:14 +020085#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000086
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010087/*
88 * Test for Linux getrandom() support.
89 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
90 * available in GNU libc and compatible libc's (eg uClibc).
91 */
92#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010093#include <unistd.h>
94#include <sys/syscall.h>
95#if defined(SYS_getrandom)
96#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020097
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010098static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
99{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200100 /* MemSan cannot understand that the syscall writes to the buffer */
101#if defined(__has_feature)
102#if __has_feature(memory_sanitizer)
103 memset( buf, 0, buflen );
104#endif
105#endif
106
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100107 return( syscall( SYS_getrandom, buf, buflen, flags ) );
108}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100109
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200110#include <sys/utsname.h>
111/* Check if version is at least 3.17.0 */
112static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100113{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200114 int minor;
115 struct utsname un;
116 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100117
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200118 /* Get version information */
119 uname(&un);
120 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100121
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200122 /* Check major version; assume a single digit */
123 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
124 return( -1 );
125
126 if( ver[0] - '0' > 3 )
127 return( 0 );
128
129 /* Ok, so now we know major == 3, check minor.
130 * Assume 1 or 2 digits. */
131 if( ver[2] < '0' || ver[2] > '9' )
132 return( -1 );
133
134 minor = ver[2] - '0';
135
136 if( ver[3] >= '0' && ver[3] <= '9' )
137 minor = 10 * minor + ver[3] - '0';
138 else if( ver [3] != '.' )
139 return( -1 );
140
141 if( minor < 17 )
142 return( -1 );
143
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100144 return( 0 );
145}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200146static int has_getrandom = -1;
147#endif /* SYS_getrandom */
148#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100149
Paul Bakker6083fd22011-12-03 21:45:14 +0000150#include <stdio.h>
151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000153 unsigned char *output, size_t len, size_t *olen )
154{
155 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200156 size_t read_len;
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)
160 if( has_getrandom == -1 )
161 has_getrandom = ( check_version_3_17_plus() == 0 );
162
163 if( has_getrandom )
164 {
165 int ret;
166
167 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200168 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200169
170 *olen = ret;
171 return( 0 );
172 }
173#endif /* HAVE_GETRANDOM */
174
Paul Bakker6083fd22011-12-03 21:45:14 +0000175 *olen = 0;
176
177 file = fopen( "/dev/urandom", "rb" );
178 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200180
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200181 read_len = fread( output, 1, len, file );
182 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000183 {
184 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000186 }
187
188 fclose( file );
189 *olen = len;
190
191 return( 0 );
192}
Paul Bakker9af723c2014-05-01 13:03:14 +0200193#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000195
Simon Butcherab5df402016-06-11 02:31:21 +0100196#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100197int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100198 unsigned char *output, size_t len, size_t *olen )
199{
200 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100201 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100202 *olen = 0;
203
204 if( len < sizeof(unsigned char) )
205 return( 0 );
206
207 *olen = sizeof(unsigned char);
208
209 return( 0 );
210}
211#endif
212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#if defined(MBEDTLS_TIMING_C)
214int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000215 unsigned char *output, size_t len, size_t *olen )
216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000218 ((void) data);
219 *olen = 0;
220
221 if( len < sizeof(unsigned long) )
222 return( 0 );
223
224 memcpy( output, &timer, sizeof(unsigned long) );
225 *olen = sizeof(unsigned long);
226
227 return( 0 );
228}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000230
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#if defined(MBEDTLS_HAVEGE_C)
232int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000233 unsigned char *output, size_t len, size_t *olen )
234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000236 *olen = 0;
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 if( mbedtls_havege_random( hs, output, len ) != 0 )
239 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000240
241 *olen = len;
242
243 return( 0 );
244}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000246
Paul Bakker9988d6b2016-06-01 11:29:42 +0100247#if defined(MBEDTLS_ENTROPY_NV_SEED)
248int mbedtls_nv_seed_poll( void *data,
249 unsigned char *output, size_t len, size_t *olen )
250{
251 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
252 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
253 ((void) data);
254
255 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
256
257 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
258 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
259
260 if( len < use_len )
261 use_len = len;
262
263 memcpy( output, buf, use_len );
264 *olen = use_len;
265
266 return( 0 );
267}
268#endif /* MBEDTLS_ENTROPY_NV_SEED */
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#endif /* MBEDTLS_ENTROPY_C */