blob: acecd9b90910fece44d9d36adc540bbcc530ec33 [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>
Manuel Pégourié-Gonnard52207812019-10-02 15:55:23 +020034#include "mbedtls/platform_util.h"
Gilles Peskine02b93292018-06-01 14:38:45 +020035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000037
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/entropy.h"
39#include "mbedtls/entropy_poll.h"
Teppo Järvelin91d79382019-10-02 09:09:31 +030040#include "mbedtls/platform_util.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if defined(MBEDTLS_TIMING_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000044#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000047#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010048#if defined(MBEDTLS_ENTROPY_NV_SEED)
49#include "mbedtls/platform.h"
50#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010053
54#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Augustin Cavalier60bc47d2018-04-11 20:27:32 -040055 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
56 !defined(__HAIKU__)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010057#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
58#endif
59
Paul Bakkerfa6a6202013-10-28 18:48:30 +010060#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000061
Paul Bakker6083fd22011-12-03 21:45:14 +000062#if !defined(_WIN32_WINNT)
63#define _WIN32_WINNT 0x0400
64#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000065#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000066#include <wincrypt.h>
67
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000069 size_t *olen )
70{
71 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000072 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000073 *olen = 0;
74
75 if( CryptAcquireContext( &provider, NULL, NULL,
76 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
77 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000079 }
80
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020081 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010082 {
83 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010085 }
Paul Bakker6083fd22011-12-03 21:45:14 +000086
87 CryptReleaseContext( provider, 0 );
88 *olen = len;
89
90 return( 0 );
91}
Paul Bakker9af723c2014-05-01 13:03:14 +020092#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000093
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010094/*
95 * Test for Linux getrandom() support.
96 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
97 * available in GNU libc and compatible libc's (eg uClibc).
98 */
99#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100100#include <unistd.h>
101#include <sys/syscall.h>
102#if defined(SYS_getrandom)
103#define HAVE_GETRANDOM
Hanno Becker27516972018-10-18 11:49:25 +0100104#include <errno.h>
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200105
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100106static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
107{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200108 /* MemSan cannot understand that the syscall writes to the buffer */
109#if defined(__has_feature)
110#if __has_feature(memory_sanitizer)
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200111 mbedtls_platform_memset( buf, 0, buflen );
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +0200112#endif
113#endif
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100114 return( syscall( SYS_getrandom, buf, buflen, flags ) );
115}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200116#endif /* SYS_getrandom */
117#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100118
Paul Bakker6083fd22011-12-03 21:45:14 +0000119#include <stdio.h>
120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000122 unsigned char *output, size_t len, size_t *olen )
123{
124 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200125 size_t read_len;
Hanno Becker27516972018-10-18 11:49:25 +0100126 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000127 ((void) data);
128
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200129#if defined(HAVE_GETRANDOM)
Hanno Becker27516972018-10-18 11:49:25 +0100130 ret = getrandom_wrapper( output, len, 0 );
131 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200132 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200133 *olen = ret;
134 return( 0 );
135 }
Hanno Becker27516972018-10-18 11:49:25 +0100136 else if( errno != ENOSYS )
137 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
138 /* Fall through if the system call isn't known. */
139#else
Hanno Becker9772da82018-11-05 11:43:07 +0000140 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200141#endif /* HAVE_GETRANDOM */
142
Paul Bakker6083fd22011-12-03 21:45:14 +0000143 *olen = 0;
144
145 file = fopen( "/dev/urandom", "rb" );
146 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200148
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200149 read_len = fread( output, 1, len, file );
150 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000151 {
152 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000154 }
155
156 fclose( file );
157 *olen = len;
158
159 return( 0 );
160}
Paul Bakker9af723c2014-05-01 13:03:14 +0200161#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000163
Simon Butcherab5df402016-06-11 02:31:21 +0100164#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100165int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100166 unsigned char *output, size_t len, size_t *olen )
167{
168 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100169 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100170 *olen = 0;
171
172 if( len < sizeof(unsigned char) )
173 return( 0 );
174
175 *olen = sizeof(unsigned char);
176
177 return( 0 );
178}
179#endif
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#if defined(MBEDTLS_TIMING_C)
182int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000183 unsigned char *output, size_t len, size_t *olen )
184{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000186 ((void) data);
187 *olen = 0;
188
189 if( len < sizeof(unsigned long) )
190 return( 0 );
191
Teppo Järvelin91d79382019-10-02 09:09:31 +0300192 mbedtls_platform_memcpy( output, &timer, sizeof(unsigned long) );
Paul Bakker6083fd22011-12-03 21:45:14 +0000193 *olen = sizeof(unsigned long);
194
195 return( 0 );
196}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#if defined(MBEDTLS_HAVEGE_C)
200int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000201 unsigned char *output, size_t len, size_t *olen )
202{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000204 *olen = 0;
205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 if( mbedtls_havege_random( hs, output, len ) != 0 )
207 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000208
209 *olen = len;
210
211 return( 0 );
212}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000214
Paul Bakker9988d6b2016-06-01 11:29:42 +0100215#if defined(MBEDTLS_ENTROPY_NV_SEED)
216int mbedtls_nv_seed_poll( void *data,
217 unsigned char *output, size_t len, size_t *olen )
218{
219 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
220 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
221 ((void) data);
222
Manuel Pégourié-Gonnard7a346b82019-10-02 14:47:01 +0200223 mbedtls_platform_memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
Paul Bakker9988d6b2016-06-01 11:29:42 +0100224
225 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
226 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
227
228 if( len < use_len )
229 use_len = len;
230
Teppo Järvelin91d79382019-10-02 09:09:31 +0300231 mbedtls_platform_memcpy( output, buf, use_len );
Paul Bakker9988d6b2016-06-01 11:29:42 +0100232 *olen = use_len;
233
234 return( 0 );
235}
236#endif /* MBEDTLS_ENTROPY_NV_SEED */
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#endif /* MBEDTLS_ENTROPY_C */