blob: 02b25a272128293ef1ab0c5c9793a6508d9dbe84 [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/entropy.h"
31#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#if defined(MBEDTLS_TIMING_C)
Rich Evans00ab4702015-02-06 13:43:58 +000034#include <string.h>
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000035#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000039#endif
Paul Bakker9988d6b2016-06-01 11:29:42 +010040#if defined(MBEDTLS_ENTROPY_NV_SEED)
41#include "mbedtls/platform.h"
42#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010045
46#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
47 !defined(__APPLE__) && !defined(_WIN32)
48#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
49#endif
50
Paul Bakkerfa6a6202013-10-28 18:48:30 +010051#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000052
Paul Bakker6083fd22011-12-03 21:45:14 +000053#if !defined(_WIN32_WINNT)
54#define _WIN32_WINNT 0x0400
55#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000056#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000057#include <wincrypt.h>
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000060 size_t *olen )
61{
62 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000063 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000064 *olen = 0;
65
66 if( CryptAcquireContext( &provider, NULL, NULL,
67 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
68 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000070 }
71
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020072 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010073 {
74 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010076 }
Paul Bakker6083fd22011-12-03 21:45:14 +000077
78 CryptReleaseContext( provider, 0 );
79 *olen = len;
80
81 return( 0 );
82}
Paul Bakker9af723c2014-05-01 13:03:14 +020083#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000084
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010085/*
86 * Test for Linux getrandom() support.
87 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
88 * available in GNU libc and compatible libc's (eg uClibc).
89 */
90#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010091#include <unistd.h>
92#include <sys/syscall.h>
93#if defined(SYS_getrandom)
94#define HAVE_GETRANDOM
Hanno Becker6b891972018-10-18 11:49:25 +010095#include <errno.h>
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
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100105 return( syscall( SYS_getrandom, buf, buflen, flags ) );
106}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200107#endif /* SYS_getrandom */
108#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100109
Paul Bakker6083fd22011-12-03 21:45:14 +0000110#include <stdio.h>
111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000113 unsigned char *output, size_t len, size_t *olen )
114{
115 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200116 size_t read_len;
Hanno Becker6b891972018-10-18 11:49:25 +0100117 int ret;
Paul Bakker6083fd22011-12-03 21:45:14 +0000118 ((void) data);
119
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200120#if defined(HAVE_GETRANDOM)
Hanno Becker6b891972018-10-18 11:49:25 +0100121 ret = getrandom_wrapper( output, len, 0 );
122 if( ret >= 0 )
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200123 {
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200124 *olen = ret;
125 return( 0 );
126 }
Hanno Becker6b891972018-10-18 11:49:25 +0100127 else if( errno != ENOSYS )
128 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
129 /* Fall through if the system call isn't known. */
130#else
Hanno Beckerbeedec82018-11-05 11:43:07 +0000131 ((void) ret);
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200132#endif /* HAVE_GETRANDOM */
133
Paul Bakker6083fd22011-12-03 21:45:14 +0000134 *olen = 0;
135
136 file = fopen( "/dev/urandom", "rb" );
137 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200138 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200139
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200140 read_len = fread( output, 1, len, file );
141 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000142 {
143 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000145 }
146
147 fclose( file );
148 *olen = len;
149
150 return( 0 );
151}
Paul Bakker9af723c2014-05-01 13:03:14 +0200152#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000154
Simon Butcherab5df402016-06-11 02:31:21 +0100155#if defined(MBEDTLS_TEST_NULL_ENTROPY)
Simon Butcher4157b602016-06-12 00:31:33 +0100156int mbedtls_null_entropy_poll( void *data,
Janos Follath53de7842016-06-08 15:29:18 +0100157 unsigned char *output, size_t len, size_t *olen )
158{
159 ((void) data);
Simon Butcherab5df402016-06-11 02:31:21 +0100160 ((void) output);
Janos Follath53de7842016-06-08 15:29:18 +0100161 *olen = 0;
162
163 if( len < sizeof(unsigned char) )
164 return( 0 );
165
166 *olen = sizeof(unsigned char);
167
168 return( 0 );
169}
170#endif
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172#if defined(MBEDTLS_TIMING_C)
173int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000174 unsigned char *output, size_t len, size_t *olen )
175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 ((void) data);
178 *olen = 0;
179
180 if( len < sizeof(unsigned long) )
181 return( 0 );
182
183 memcpy( output, &timer, sizeof(unsigned long) );
184 *olen = sizeof(unsigned long);
185
186 return( 0 );
187}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190#if defined(MBEDTLS_HAVEGE_C)
191int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000192 unsigned char *output, size_t len, size_t *olen )
193{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000195 *olen = 0;
196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 if( mbedtls_havege_random( hs, output, len ) != 0 )
198 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000199
200 *olen = len;
201
202 return( 0 );
203}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000205
Paul Bakker9988d6b2016-06-01 11:29:42 +0100206#if defined(MBEDTLS_ENTROPY_NV_SEED)
207int mbedtls_nv_seed_poll( void *data,
208 unsigned char *output, size_t len, size_t *olen )
209{
210 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
211 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
212 ((void) data);
213
214 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
215
216 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
217 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
218
219 if( len < use_len )
220 use_len = len;
221
222 memcpy( output, buf, use_len );
223 *olen = use_len;
224
225 return( 0 );
226}
227#endif /* MBEDTLS_ENTROPY_NV_SEED */
228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229#endif /* MBEDTLS_ENTROPY_C */