blob: 6b3ad3501c934fca8eafda14a08f2326738f7926 [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
Paul Bakker6083fd22011-12-03 21:45:14 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 *
Paul Bakker6083fd22011-12-03 21:45:14 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_ENTROPY_C)
Paul Bakker6083fd22011-12-03 21:45:14 +000030
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/entropy.h"
32#include "mbedtls/entropy_poll.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_TIMING_C)
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/timing.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000037#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_HAVEGE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/havege.h"
Paul Bakker6083fd22011-12-03 21:45:14 +000040#endif
41
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010043#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000044
Paul Bakker6083fd22011-12-03 21:45:14 +000045#if !defined(_WIN32_WINNT)
46#define _WIN32_WINNT 0x0400
47#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000048#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000049#include <wincrypt.h>
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
Paul Bakker6083fd22011-12-03 21:45:14 +000052 size_t *olen )
53{
54 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000055 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000056 *olen = 0;
57
58 if( CryptAcquireContext( &provider, NULL, NULL,
59 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
60 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000062 }
63
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020064 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000066
67 CryptReleaseContext( provider, 0 );
68 *olen = len;
69
70 return( 0 );
71}
Paul Bakker9af723c2014-05-01 13:03:14 +020072#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000073
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010074/*
75 * Test for Linux getrandom() support.
76 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
77 * available in GNU libc and compatible libc's (eg uClibc).
78 */
79#if defined(__linux__) && defined(__GLIBC__)
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010080#include <unistd.h>
81#include <sys/syscall.h>
82#if defined(SYS_getrandom)
83#define HAVE_GETRANDOM
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020084
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010085static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
86{
Manuel Pégourié-Gonnardbcf13ba2015-06-22 18:06:17 +020087 /* MemSan cannot understand that the syscall writes to the buffer */
88#if defined(__has_feature)
89#if __has_feature(memory_sanitizer)
90 memset( buf, 0, buflen );
91#endif
92#endif
93
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010094 return( syscall( SYS_getrandom, buf, buflen, flags ) );
95}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010096
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020097#include <sys/utsname.h>
98/* Check if version is at least 3.17.0 */
99static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100100{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200101 int minor;
102 struct utsname un;
103 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100104
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200105 /* Get version information */
106 uname(&un);
107 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100108
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200109 /* Check major version; assume a single digit */
110 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
111 return( -1 );
112
113 if( ver[0] - '0' > 3 )
114 return( 0 );
115
116 /* Ok, so now we know major == 3, check minor.
117 * Assume 1 or 2 digits. */
118 if( ver[2] < '0' || ver[2] > '9' )
119 return( -1 );
120
121 minor = ver[2] - '0';
122
123 if( ver[3] >= '0' && ver[3] <= '9' )
124 minor = 10 * minor + ver[3] - '0';
125 else if( ver [3] != '.' )
126 return( -1 );
127
128 if( minor < 17 )
129 return( -1 );
130
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100131 return( 0 );
132}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200133static int has_getrandom = -1;
134#endif /* SYS_getrandom */
135#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100136
Paul Bakker6083fd22011-12-03 21:45:14 +0000137#include <stdio.h>
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000140 unsigned char *output, size_t len, size_t *olen )
141{
142 FILE *file;
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200143 size_t read_len;
Paul Bakker6083fd22011-12-03 21:45:14 +0000144 ((void) data);
145
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200146#if defined(HAVE_GETRANDOM)
147 if( has_getrandom == -1 )
148 has_getrandom = ( check_version_3_17_plus() == 0 );
149
150 if( has_getrandom )
151 {
152 int ret;
153
154 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200155 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200156
157 *olen = ret;
158 return( 0 );
159 }
160#endif /* HAVE_GETRANDOM */
161
Paul Bakker6083fd22011-12-03 21:45:14 +0000162 *olen = 0;
163
164 file = fopen( "/dev/urandom", "rb" );
165 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200167
Manuel Pégourié-Gonnardea356662015-08-27 12:02:40 +0200168 read_len = fread( output, 1, len, file );
169 if( read_len != len )
Paul Bakker6083fd22011-12-03 21:45:14 +0000170 {
171 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000173 }
174
175 fclose( file );
176 *olen = len;
177
178 return( 0 );
179}
Paul Bakker9af723c2014-05-01 13:03:14 +0200180#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183#if defined(MBEDTLS_TIMING_C)
184int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000185 unsigned char *output, size_t len, size_t *olen )
186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000188 ((void) data);
189 *olen = 0;
190
191 if( len < sizeof(unsigned long) )
192 return( 0 );
193
194 memcpy( output, &timer, sizeof(unsigned long) );
195 *olen = sizeof(unsigned long);
196
197 return( 0 );
198}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_HAVEGE_C)
202int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000203 unsigned char *output, size_t len, size_t *olen )
204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000206 *olen = 0;
207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 if( mbedtls_havege_random( hs, output, len ) != 0 )
209 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000210
211 *olen = len;
212
213 return( 0 );
214}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#endif /* MBEDTLS_ENTROPY_C */