blob: 040aa117dce8b69649d348e6ca6e6fa0e7f8d490 [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
Nicholas Wilson2682edf2017-12-05 12:08:15 +000022#if defined(__linux__)
23/* Ensure that syscall() is available even when compiling with -std=c99 */
24#define _GNU_SOURCE
25#endif
26
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Gilles Peskine02b93292018-06-01 14:38:45 +020033#include <string.h>
34
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000036
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000037#include "mbedtls/entropy.h"
38#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000045#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010046#if defined(MBEDTLS_ENTROPY_NV_SEED)
47#include "mbedtls/platform.h"
48#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010051
52#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040053 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
54 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010055#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
56#endif
57
Paul Bakkerfa6a6202013-10-28 18:48:30 +010058#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000059
Paul Bakker6083fd22011-12-03 21:45:14 +000060#if !defined(_WIN32_WINNT)
61#define _WIN32_WINNT 0x0400
62#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000063#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000064#include <wincrypt.h>
65
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000067 size_t *olen )
68{
69 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000070 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000071 *olen = 0;
72
73 if( CryptAcquireContext( &provider, NULL, NULL,
74 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
75 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000077 }
78
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020079 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010080 {
81 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010083 }
Paul Bakker6083fd22011-12-03 21:45:14 +000084
85 CryptReleaseContext( provider, 0 );
86 *olen = len;
87
88 return( 0 );
89}
Paul Bakker9af723c2014-05-01 13:03:14 +020090#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000091
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010092/*
93 * Test for Linux getrandom() support.
94 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
95 * available in GNU libc and compatible libc's (eg uClibc).
96 */
97#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010098#include <unistd.h>
99#include <sys/syscall.h>
100#if defined(SYS_getrandom)
101#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200102
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100103static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
104{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200105 /* MemSan cannot understand that the syscall writes to the buffer */
106#if defined(__has_feature)
107#if __has_feature(memory_sanitizer)
108 memset( buf, 0, buflen );
109#endif
110#endif
111
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100112 return( syscall( SYS_getrandom, buf, buflen, flags ) );
113}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100114
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200115#include <sys/utsname.h>
116/* Check if version is at least 3.17.0 */
117static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100118{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200119 int minor;
120 struct utsname un;
121 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100122
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200123 /* Get version information */
124 uname(&un);
125 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100126
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200127 /* Check major version; assume a single digit */
128 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
129 return( -1 );
130
131 if( ver[0] - '0' > 3 )
132 return( 0 );
133
134 /* Ok, so now we know major == 3, check minor.
135 * Assume 1 or 2 digits. */
136 if( ver[2] < '0' || ver[2] > '9' )
137 return( -1 );
138
139 minor = ver[2] - '0';
140
141 if( ver[3] >= '0' && ver[3] <= '9' )
142 minor = 10 * minor + ver[3] - '0';
143 else if( ver [3] != '.' )
144 return( -1 );
145
146 if( minor < 17 )
147 return( -1 );
148
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100149 return( 0 );
150}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200151static int has_getrandom = -1;
152#endif /* SYS_getrandom */
153#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100154
Paul Bakker6083fd22011-12-03 21:45:14 +0000155#include <stdio.h>
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000158 unsigned char *output, size_t len, size_t *olen )
159{
160 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200161 size_t read_len;
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 ((void) data);
163
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200164#if defined(HAVE_GETRANDOM)
165 if( has_getrandom == -1 )
166 has_getrandom = ( check_version_3_17_plus() == 0 );
167
168 if( has_getrandom )
169 {
170 int ret;
171
172 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200173 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200174
175 *olen = ret;
176 return( 0 );
177 }
178#endif /* HAVE_GETRANDOM */
179
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 *olen = 0;
181
182 file = fopen( "/dev/urandom", "rb" );
183 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200185
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200186 read_len = fread( output, 1, len, file );
187 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000188 {
189 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000191 }
192
193 fclose( file );
194 *olen = len;
195
196 return( 0 );
197}
Paul Bakker9af723c2014-05-01 13:03:14 +0200198#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000200
Simon Butcherab5df402016-06-11 02:31:21 +0100201#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100202int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100203 unsigned char *output, size_t len, size_t *olen )
204{
205 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100206 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100207 *olen = 0;
208
209 if( len < sizeof(unsigned char) )
210 return( 0 );
211
212 *olen = sizeof(unsigned char);
213
214 return( 0 );
215}
216#endif
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218#if defined(MBEDTLS_TIMING_C)
219int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000220 unsigned char *output, size_t len, size_t *olen )
221{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000223 ((void) data);
224 *olen = 0;
225
226 if( len < sizeof(unsigned long) )
227 return( 0 );
228
229 memcpy( output, &timer, sizeof(unsigned long) );
230 *olen = sizeof(unsigned long);
231
232 return( 0 );
233}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#if defined(MBEDTLS_HAVEGE_C)
237int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000238 unsigned char *output, size_t len, size_t *olen )
239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000241 *olen = 0;
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 if( mbedtls_havege_random( hs, output, len ) != 0 )
244 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000245
246 *olen = len;
247
248 return( 0 );
249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000251
Paul Bakker9988d6b2016-06-01 11:29:42 +0100252#if defined(MBEDTLS_ENTROPY_NV_SEED)
253int mbedtls_nv_seed_poll( void *data,
254 unsigned char *output, size_t len, size_t *olen )
255{
256 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
257 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
258 ((void) data);
259
260 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
261
262 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
263 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
264
265 if( len < use_len )
266 use_len = len;
267
268 memcpy( output, buf, use_len );
269 *olen = use_len;
270
271 return( 0 );
272}
273#endif /* MBEDTLS_ENTROPY_NV_SEED */
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275#endif /* MBEDTLS_ENTROPY_C */