blob: ae3929a426e88723af0f8ba4db2ec0a7a7675340 [file] [log] [blame]
Paul Bakker6083fd22011-12-03 21:45:14 +00001/*
2 * Platform-specific and custom entropy polling functions
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2014, 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__)
80#include <linux/version.h>
81#include <unistd.h>
82#include <sys/syscall.h>
83#if defined(SYS_getrandom)
84#define HAVE_GETRANDOM
85static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
86{
87 return( syscall( SYS_getrandom, buf, buflen, flags ) );
88}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010089
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020090#include <sys/utsname.h>
91/* Check if version is at least 3.17.0 */
92static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010093{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020094 int minor;
95 struct utsname un;
96 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010097
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020098 /* Get version information */
99 uname(&un);
100 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100101
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200102 /* Check major version; assume a single digit */
103 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
104 return( -1 );
105
106 if( ver[0] - '0' > 3 )
107 return( 0 );
108
109 /* Ok, so now we know major == 3, check minor.
110 * Assume 1 or 2 digits. */
111 if( ver[2] < '0' || ver[2] > '9' )
112 return( -1 );
113
114 minor = ver[2] - '0';
115
116 if( ver[3] >= '0' && ver[3] <= '9' )
117 minor = 10 * minor + ver[3] - '0';
118 else if( ver [3] != '.' )
119 return( -1 );
120
121 if( minor < 17 )
122 return( -1 );
123
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100124 return( 0 );
125}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200126static int has_getrandom = -1;
127#endif /* SYS_getrandom */
128#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100129
Paul Bakker6083fd22011-12-03 21:45:14 +0000130#include <stdio.h>
131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000133 unsigned char *output, size_t len, size_t *olen )
134{
135 FILE *file;
136 size_t ret;
137 ((void) data);
138
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200139#if defined(HAVE_GETRANDOM)
140 if( has_getrandom == -1 )
141 has_getrandom = ( check_version_3_17_plus() == 0 );
142
143 if( has_getrandom )
144 {
145 int ret;
146
147 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200148 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200149
150 *olen = ret;
151 return( 0 );
152 }
153#endif /* HAVE_GETRANDOM */
154
Paul Bakker6083fd22011-12-03 21:45:14 +0000155 *olen = 0;
156
157 file = fopen( "/dev/urandom", "rb" );
158 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200160
Paul Bakker6083fd22011-12-03 21:45:14 +0000161 ret = fread( output, 1, len, file );
162 if( ret != len )
163 {
164 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000166 }
167
168 fclose( file );
169 *olen = len;
170
171 return( 0 );
172}
Paul Bakker9af723c2014-05-01 13:03:14 +0200173#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176#if defined(MBEDTLS_TIMING_C)
177int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000178 unsigned char *output, size_t len, size_t *olen )
179{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000181 ((void) data);
182 *olen = 0;
183
184 if( len < sizeof(unsigned long) )
185 return( 0 );
186
187 memcpy( output, &timer, sizeof(unsigned long) );
188 *olen = sizeof(unsigned long);
189
190 return( 0 );
191}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194#if defined(MBEDTLS_HAVEGE_C)
195int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000196 unsigned char *output, size_t len, size_t *olen )
197{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000199 *olen = 0;
200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 if( mbedtls_havege_random( hs, output, len ) != 0 )
202 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000203
204 *olen = len;
205
206 return( 0 );
207}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#endif /* MBEDTLS_ENTROPY_C */