blob: 79efb87e9d4418d20651792f9724b433f72aadff [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, 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
40
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Manuel Pégourié-Gonnard325ce092016-02-22 10:33:34 +010042
43#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
44 !defined(__APPLE__) && !defined(_WIN32)
45#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
46#endif
47
Paul Bakkerfa6a6202013-10-28 18:48:30 +010048#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000049
Paul Bakker6083fd22011-12-03 21:45:14 +000050#if !defined(_WIN32_WINNT)
51#define _WIN32_WINNT 0x0400
52#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000053#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000054#include <wincrypt.h>
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000057 size_t *olen )
58{
59 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000060 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000061 *olen = 0;
62
63 if( CryptAcquireContext( &provider, NULL, NULL,
64 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
65 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000067 }
68
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020069 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Attila Molnar2791ba12016-01-26 11:39:26 +010070 {
71 CryptReleaseContext( provider, 0 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Attila Molnar2791ba12016-01-26 11:39:26 +010073 }
Paul Bakker6083fd22011-12-03 21:45:14 +000074
75 CryptReleaseContext( provider, 0 );
76 *olen = len;
77
78 return( 0 );
79}
Paul Bakker9af723c2014-05-01 13:03:14 +020080#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000081
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010082/*
83 * Test for Linux getrandom() support.
84 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
85 * available in GNU libc and compatible libc's (eg uClibc).
86 */
87#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010088#include <unistd.h>
89#include <sys/syscall.h>
90#if defined(SYS_getrandom)
91#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020092
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010093static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
94{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020095 /* MemSan cannot understand that the syscall writes to the buffer */
96#if defined(__has_feature)
97#if __has_feature(memory_sanitizer)
98 memset( buf, 0, buflen );
99#endif
100#endif
101
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100102 return( syscall( SYS_getrandom, buf, buflen, flags ) );
103}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100104
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200105#include <sys/utsname.h>
106/* Check if version is at least 3.17.0 */
107static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100108{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200109 int minor;
110 struct utsname un;
111 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100112
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200113 /* Get version information */
114 uname(&un);
115 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100116
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200117 /* Check major version; assume a single digit */
118 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
119 return( -1 );
120
121 if( ver[0] - '0' > 3 )
122 return( 0 );
123
124 /* Ok, so now we know major == 3, check minor.
125 * Assume 1 or 2 digits. */
126 if( ver[2] < '0' || ver[2] > '9' )
127 return( -1 );
128
129 minor = ver[2] - '0';
130
131 if( ver[3] >= '0' && ver[3] <= '9' )
132 minor = 10 * minor + ver[3] - '0';
133 else if( ver [3] != '.' )
134 return( -1 );
135
136 if( minor < 17 )
137 return( -1 );
138
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100139 return( 0 );
140}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200141static int has_getrandom = -1;
142#endif /* SYS_getrandom */
143#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100144
Paul Bakker6083fd22011-12-03 21:45:14 +0000145#include <stdio.h>
146
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000148 unsigned char *output, size_t len, size_t *olen )
149{
150 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200151 size_t read_len;
Paul Bakker6083fd22011-12-03 21:45:14 +0000152 ((void) data);
153
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200154#if defined(HAVE_GETRANDOM)
155 if( has_getrandom == -1 )
156 has_getrandom = ( check_version_3_17_plus() == 0 );
157
158 if( has_getrandom )
159 {
160 int ret;
161
162 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200163 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200164
165 *olen = ret;
166 return( 0 );
167 }
168#endif /* HAVE_GETRANDOM */
169
Paul Bakker6083fd22011-12-03 21:45:14 +0000170 *olen = 0;
171
172 file = fopen( "/dev/urandom", "rb" );
173 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200175
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200176 read_len = fread( output, 1, len, file );
177 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 {
179 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 }
182
183 fclose( file );
184 *olen = len;
185
186 return( 0 );
187}
Paul Bakker9af723c2014-05-01 13:03:14 +0200188#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000190
Janos Follath53de7842016-06-08 15:29:18 +0100191#if defined(MBEDTLS_TEST_WO_ENTROPY)
192int mbedtls_zero_entropy_poll( void *data,
193 unsigned char *output, size_t len, size_t *olen )
194{
195 ((void) data);
196 *olen = 0;
197
198 if( len < sizeof(unsigned char) )
199 return( 0 );
200
201 *olen = sizeof(unsigned char);
202
203 return( 0 );
204}
205#endif
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_TIMING_C)
208int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000209 unsigned char *output, size_t len, size_t *olen )
210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200211 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000212 ((void) data);
213 *olen = 0;
214
215 if( len < sizeof(unsigned long) )
216 return( 0 );
217
218 memcpy( output, &timer, sizeof(unsigned long) );
219 *olen = sizeof(unsigned long);
220
221 return( 0 );
222}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_HAVEGE_C)
226int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000227 unsigned char *output, size_t len, size_t *olen )
228{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000230 *olen = 0;
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 if( mbedtls_havege_random( hs, output, len ) != 0 )
233 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000234
235 *olen = len;
236
237 return( 0 );
238}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241#endif /* MBEDTLS_ENTROPY_C */