blob: 92c757ec73eaa019bfae47432c70801e333b1897 [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__)
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
84static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
85{
86 return( syscall( SYS_getrandom, buf, buflen, flags ) );
87}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010088
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020089#include <sys/utsname.h>
90/* Check if version is at least 3.17.0 */
91static int check_version_3_17_plus( void )
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010092{
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020093 int minor;
94 struct utsname un;
95 const char *ver;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010096
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +020097 /* Get version information */
98 uname(&un);
99 ver = un.release;
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100100
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200101 /* Check major version; assume a single digit */
102 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
103 return( -1 );
104
105 if( ver[0] - '0' > 3 )
106 return( 0 );
107
108 /* Ok, so now we know major == 3, check minor.
109 * Assume 1 or 2 digits. */
110 if( ver[2] < '0' || ver[2] > '9' )
111 return( -1 );
112
113 minor = ver[2] - '0';
114
115 if( ver[3] >= '0' && ver[3] <= '9' )
116 minor = 10 * minor + ver[3] - '0';
117 else if( ver [3] != '.' )
118 return( -1 );
119
120 if( minor < 17 )
121 return( -1 );
122
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100123 return( 0 );
124}
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200125static int has_getrandom = -1;
126#endif /* SYS_getrandom */
127#endif /* __linux__ */
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100128
Paul Bakker6083fd22011-12-03 21:45:14 +0000129#include <stdio.h>
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_platform_entropy_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000132 unsigned char *output, size_t len, size_t *olen )
133{
134 FILE *file;
135 size_t ret;
136 ((void) data);
137
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200138#if defined(HAVE_GETRANDOM)
139 if( has_getrandom == -1 )
140 has_getrandom = ( check_version_3_17_plus() == 0 );
141
142 if( has_getrandom )
143 {
144 int ret;
145
146 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
Manuel Pégourié-Gonnard9f145de2015-05-04 15:03:50 +0200147 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Manuel Pégourié-Gonnardd97828e2015-04-29 14:03:28 +0200148
149 *olen = ret;
150 return( 0 );
151 }
152#endif /* HAVE_GETRANDOM */
153
Paul Bakker6083fd22011-12-03 21:45:14 +0000154 *olen = 0;
155
156 file = fopen( "/dev/urandom", "rb" );
157 if( file == NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200159
Paul Bakker6083fd22011-12-03 21:45:14 +0000160 ret = fread( output, 1, len, file );
161 if( ret != len )
162 {
163 fclose( file );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000165 }
166
167 fclose( file );
168 *olen = len;
169
170 return( 0 );
171}
Paul Bakker9af723c2014-05-01 13:03:14 +0200172#endif /* _WIN32 && !EFIX64 && !EFI32 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175#if defined(MBEDTLS_TIMING_C)
176int mbedtls_hardclock_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000177 unsigned char *output, size_t len, size_t *olen )
178{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 unsigned long timer = mbedtls_timing_hardclock();
Paul Bakker6083fd22011-12-03 21:45:14 +0000180 ((void) data);
181 *olen = 0;
182
183 if( len < sizeof(unsigned long) )
184 return( 0 );
185
186 memcpy( output, &timer, sizeof(unsigned long) );
187 *olen = sizeof(unsigned long);
188
189 return( 0 );
190}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191#endif /* MBEDTLS_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193#if defined(MBEDTLS_HAVEGE_C)
194int mbedtls_havege_poll( void *data,
Paul Bakker6083fd22011-12-03 21:45:14 +0000195 unsigned char *output, size_t len, size_t *olen )
196{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
Paul Bakker6083fd22011-12-03 21:45:14 +0000198 *olen = 0;
199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 if( mbedtls_havege_random( hs, output, len ) != 0 )
201 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000202
203 *olen = len;
204
205 return( 0 );
206}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#endif /* MBEDTLS_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209#endif /* MBEDTLS_ENTROPY_C */