blob: ae3d2d5b6671c92516bcde76ac54f014208efb10 [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
Hanno Becker27516972018-10-18 11:49:25 +0100102#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200103
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100104static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
105{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200106 /* MemSan cannot understand that the syscall writes to the buffer */
107#if defined(__has_feature)
108#if __has_feature(memory_sanitizer)
109 memset( buf, 0, buflen );
110#endif
111#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100112 return( syscall( SYS_getrandom, buf, buflen, flags ) );
113}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200114#endif /* SYS_getrandom */
115#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100116
Paul Bakker6083fd22011-12-03 21:45:14 +0000117#include <stdio.h>
118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000120 unsigned char *output, size_t len, size_t *olen )
121{
122 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200123 size_t read_len;
Hanno Becker27516972018-10-18 11:49:25 +0100124 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000125 ((void) data);
126
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200127#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100128 ret = getrandom_wrapper( output, len, 0 );
129 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200130 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200131 *olen = ret;
132 return( 0 );
133 }
Hanno Becker27516972018-10-18 11:49:25 +0100134 else if( errno != ENOSYS )
135 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
136 /* Fall through if the system call isn't known. */
137#else
138 ((void) ret;
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200139#endif /* HAVE_GETRANDOM */
140
Paul Bakker6083fd22011-12-03 21:45:14 +0000141 *olen = 0;
142
143 file = fopen( "/dev/urandom", "rb" );
144 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200146
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200147 read_len = fread( output, 1, len, file );
148 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000149 {
150 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 }
153
154 fclose( file );
155 *olen = len;
156
157 return( 0 );
158}
Paul Bakker9af723c2014-05-01 13:03:14 +0200159#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000161
Simon Butcherab5df402016-06-11 02:31:21 +0100162#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100163int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100164 unsigned char *output, size_t len, size_t *olen )
165{
166 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100167 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100168 *olen = 0;
169
170 if( len < sizeof(unsigned char) )
171 return( 0 );
172
173 *olen = sizeof(unsigned char);
174
175 return( 0 );
176}
177#endif
178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179#if defined(MBEDTLS_TIMING_C)
180int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 unsigned char *output, size_t len, size_t *olen )
182{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000184 ((void) data);
185 *olen = 0;
186
187 if( len < sizeof(unsigned long) )
188 return( 0 );
189
190 memcpy( output, &timer, sizeof(unsigned long) );
191 *olen = sizeof(unsigned long);
192
193 return( 0 );
194}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#if defined(MBEDTLS_HAVEGE_C)
198int mbedtls_havege_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 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000202 *olen = 0;
203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 if( mbedtls_havege_random( hs, output, len ) != 0 )
205 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000206
207 *olen = len;
208
209 return( 0 );
210}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000212
Paul Bakker9988d6b2016-06-01 11:29:42 +0100213#if defined(MBEDTLS_ENTROPY_NV_SEED)
214int mbedtls_nv_seed_poll( void *data,
215 unsigned char *output, size_t len, size_t *olen )
216{
217 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
218 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
219 ((void) data);
220
221 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
222
223 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
224 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
225
226 if( len < use_len )
227 use_len = len;
228
229 memcpy( output, buf, use_len );
230 *olen = use_len;
231
232 return( 0 );
233}
234#endif /* MBEDTLS_ENTROPY_NV_SEED */
235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_ENTROPY_C */