blob: 31f608b8382e89a5533b61b20cf86d1262fac3cd [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) && \
Matthias Weisser3f21a352016-08-18 07:55:05 +020053 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010054#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
55#endif
56
Paul Bakkerfa6a6202013-10-28 18:48:30 +010057#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000058
Paul Bakker6083fd22011-12-03 21:45:14 +000059#if !defined(_WIN32_WINNT)
60#define _WIN32_WINNT 0x0400
61#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000062#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000063#include <wincrypt.h>
64
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000066 size_t *olen )
67{
68 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000069 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000070 *olen = 0;
71
72 if( CryptAcquireContext( &provider, NULL, NULL,
73 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
74 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000076 }
77
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020078 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010079 {
80 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010082 }
Paul Bakker6083fd22011-12-03 21:45:14 +000083
84 CryptReleaseContext( provider, 0 );
85 *olen = len;
86
87 return( 0 );
88}
Paul Bakker9af723c2014-05-01 13:03:14 +020089#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000090
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010091/*
92 * Test for Linux getrandom() support.
93 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
94 * available in GNU libc and compatible libc's (eg uClibc).
95 */
96#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097#include <unistd.h>
98#include <sys/syscall.h>
99#if defined(SYS_getrandom)
100#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200101
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100102static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
103{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200104 /* MemSan cannot understand that the syscall writes to the buffer */
105#if defined(__has_feature)
106#if __has_feature(memory_sanitizer)
107 memset( buf, 0, buflen );
108#endif
109#endif
110
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100111 return( syscall( SYS_getrandom, buf, buflen, flags ) );
112}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100113
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200114#include <sys/utsname.h>
115/* Check if version is at least 3.17.0 */
116static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100117{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200118 int minor;
119 struct utsname un;
120 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100121
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200122 /* Get version information */
123 uname(&un);
124 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100125
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200126 /* Check major version; assume a single digit */
127 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
128 return( -1 );
129
130 if( ver[0] - '0' > 3 )
131 return( 0 );
132
133 /* Ok, so now we know major == 3, check minor.
134 * Assume 1 or 2 digits. */
135 if( ver[2] < '0' || ver[2] > '9' )
136 return( -1 );
137
138 minor = ver[2] - '0';
139
140 if( ver[3] >= '0' && ver[3] <= '9' )
141 minor = 10 * minor + ver[3] - '0';
142 else if( ver [3] != '.' )
143 return( -1 );
144
145 if( minor < 17 )
146 return( -1 );
147
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100148 return( 0 );
149}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200150static int has_getrandom = -1;
151#endif /* SYS_getrandom */
152#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100153
Paul Bakker6083fd22011-12-03 21:45:14 +0000154#include <stdio.h>
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000157 unsigned char *output, size_t len, size_t *olen )
158{
159 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200160 size_t read_len;
Paul Bakker6083fd22011-12-03 21:45:14 +0000161 ((void) data);
162
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200163#if defined(HAVE_GETRANDOM)
164 if( has_getrandom == -1 )
165 has_getrandom = ( check_version_3_17_plus() == 0 );
166
167 if( has_getrandom )
168 {
169 int ret;
170
171 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200172 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200173
174 *olen = ret;
175 return( 0 );
176 }
177#endif /* HAVE_GETRANDOM */
178
Paul Bakker6083fd22011-12-03 21:45:14 +0000179 *olen = 0;
180
181 file = fopen( "/dev/urandom", "rb" );
182 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200184
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200185 read_len = fread( output, 1, len, file );
186 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000187 {
188 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000190 }
191
192 fclose( file );
193 *olen = len;
194
195 return( 0 );
196}
Paul Bakker9af723c2014-05-01 13:03:14 +0200197#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000199
Simon Butcherab5df402016-06-11 02:31:21 +0100200#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100201int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100202 unsigned char *output, size_t len, size_t *olen )
203{
204 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100205 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100206 *olen = 0;
207
208 if( len < sizeof(unsigned char) )
209 return( 0 );
210
211 *olen = sizeof(unsigned char);
212
213 return( 0 );
214}
215#endif
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_TIMING_C)
218int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000219 unsigned char *output, size_t len, size_t *olen )
220{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000222 ((void) data);
223 *olen = 0;
224
225 if( len < sizeof(unsigned long) )
226 return( 0 );
227
228 memcpy( output, &timer, sizeof(unsigned long) );
229 *olen = sizeof(unsigned long);
230
231 return( 0 );
232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235#if defined(MBEDTLS_HAVEGE_C)
236int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000237 unsigned char *output, size_t len, size_t *olen )
238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000240 *olen = 0;
241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 if( mbedtls_havege_random( hs, output, len ) != 0 )
243 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000244
245 *olen = len;
246
247 return( 0 );
248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000250
Paul Bakker9988d6b2016-06-01 11:29:42 +0100251#if defined(MBEDTLS_ENTROPY_NV_SEED)
252int mbedtls_nv_seed_poll( void *data,
253 unsigned char *output, size_t len, size_t *olen )
254{
255 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
256 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
257 ((void) data);
258
259 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
260
261 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
262 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
263
264 if( len < use_len )
265 use_len = len;
266
267 memcpy( output, buf, use_len );
268 *olen = use_len;
269
270 return( 0 );
271}
272#endif /* MBEDTLS_ENTROPY_NV_SEED */
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#endif /* MBEDTLS_ENTROPY_C */