blob: fd96258ce72a1da797f55901f6895369b8acc258 [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) && \
Matthias Weisser3f21a352016-08-18 07:55:05 +020048 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010049#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
50#endif
51
Paul Bakkerfa6a6202013-10-28 18:48:30 +010052#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000053
Paul Bakker6083fd22011-12-03 21:45:14 +000054#if !defined(_WIN32_WINNT)
55#define _WIN32_WINNT 0x0400
56#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000057#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000058#include <wincrypt.h>
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000061 size_t *olen )
62{
63 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000064 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000065 *olen = 0;
66
67 if( CryptAcquireContext( &provider, NULL, NULL,
68 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
69 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000071 }
72
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020073 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010074 {
75 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010077 }
Paul Bakker6083fd22011-12-03 21:45:14 +000078
79 CryptReleaseContext( provider, 0 );
80 *olen = len;
81
82 return( 0 );
83}
Paul Bakker9af723c2014-05-01 13:03:14 +020084#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000085
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010086/*
87 * Test for Linux getrandom() support.
88 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
89 * available in GNU libc and compatible libc's (eg uClibc).
90 */
91#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010092#include <unistd.h>
93#include <sys/syscall.h>
94#if defined(SYS_getrandom)
95#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020096
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
98{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020099 /* MemSan cannot understand that the syscall writes to the buffer */
100#if defined(__has_feature)
101#if __has_feature(memory_sanitizer)
102 memset( buf, 0, buflen );
103#endif
104#endif
105
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100106 return( syscall( SYS_getrandom, buf, buflen, flags ) );
107}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100108
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200109#include <sys/utsname.h>
110/* Check if version is at least 3.17.0 */
111static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100112{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200113 int minor;
114 struct utsname un;
115 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100116
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200117 /* Get version information */
118 uname(&un);
119 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100120
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200121 /* Check major version; assume a single digit */
122 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
123 return( -1 );
124
125 if( ver[0] - '0' > 3 )
126 return( 0 );
127
128 /* Ok, so now we know major == 3, check minor.
129 * Assume 1 or 2 digits. */
130 if( ver[2] < '0' || ver[2] > '9' )
131 return( -1 );
132
133 minor = ver[2] - '0';
134
135 if( ver[3] >= '0' && ver[3] <= '9' )
136 minor = 10 * minor + ver[3] - '0';
137 else if( ver [3] != '.' )
138 return( -1 );
139
140 if( minor < 17 )
141 return( -1 );
142
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100143 return( 0 );
144}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200145static int has_getrandom = -1;
146#endif /* SYS_getrandom */
147#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100148
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;
Paul Bakker6083fd22011-12-03 21:45:14 +0000156 ((void) data);
157
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200158#if defined(HAVE_GETRANDOM)
159 if( has_getrandom == -1 )
160 has_getrandom = ( check_version_3_17_plus() == 0 );
161
162 if( has_getrandom )
163 {
164 int ret;
165
166 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200167 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200168
169 *olen = ret;
170 return( 0 );
171 }
172#endif /* HAVE_GETRANDOM */
173
Paul Bakker6083fd22011-12-03 21:45:14 +0000174 *olen = 0;
175
176 file = fopen( "/dev/urandom", "rb" );
177 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200179
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200180 read_len = fread( output, 1, len, file );
181 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000182 {
183 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000185 }
186
187 fclose( file );
188 *olen = len;
189
190 return( 0 );
191}
Paul Bakker9af723c2014-05-01 13:03:14 +0200192#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000194
Simon Butcherab5df402016-06-11 02:31:21 +0100195#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100196int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100197 unsigned char *output, size_t len, size_t *olen )
198{
199 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100200 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100201 *olen = 0;
202
203 if( len < sizeof(unsigned char) )
204 return( 0 );
205
206 *olen = sizeof(unsigned char);
207
208 return( 0 );
209}
210#endif
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#if defined(MBEDTLS_TIMING_C)
213int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000214 unsigned char *output, size_t len, size_t *olen )
215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000217 ((void) data);
218 *olen = 0;
219
220 if( len < sizeof(unsigned long) )
221 return( 0 );
222
223 memcpy( output, &timer, sizeof(unsigned long) );
224 *olen = sizeof(unsigned long);
225
226 return( 0 );
227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230#if defined(MBEDTLS_HAVEGE_C)
231int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000232 unsigned char *output, size_t len, size_t *olen )
233{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000235 *olen = 0;
236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 if( mbedtls_havege_random( hs, output, len ) != 0 )
238 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000239
240 *olen = len;
241
242 return( 0 );
243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000245
Paul Bakker9988d6b2016-06-01 11:29:42 +0100246#if defined(MBEDTLS_ENTROPY_NV_SEED)
247int mbedtls_nv_seed_poll( void *data,
248 unsigned char *output, size_t len, size_t *olen )
249{
250 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
251 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
252 ((void) data);
253
254 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
255
256 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
257 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
258
259 if( len < use_len )
260 use_len = len;
261
262 memcpy( output, buf, use_len );
263 *olen = use_len;
264
265 return( 0 );
266}
267#endif /* MBEDTLS_ENTROPY_NV_SEED */
268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269#endif /* MBEDTLS_ENTROPY_C */