blob: 8d98d895a07877bbd668e9a33fafa87f1a96f3c5 [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000028
29#if defined(POLARSSL_ENTROPY_C)
30
31#include "polarssl/entropy.h"
32#include "polarssl/entropy_poll.h"
33
34#if defined(POLARSSL_TIMING_C)
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <string.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000036#include "polarssl/timing.h"
37#endif
38#if defined(POLARSSL_HAVEGE_C)
39#include "polarssl/havege.h"
40#endif
41
42#if !defined(POLARSSL_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
51int platform_entropy_poll( void *data, unsigned char *output, size_t len,
52 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 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +020061 return( POLARSSL_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 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020065 return( POLARSSL_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}
89#endif /* SYS_getrandom */
90#endif /* __linux__ */
91
92#if defined(HAVE_GETRANDOM)
93
94#include <errno.h>
95
96int platform_entropy_poll( void *data,
97 unsigned char *output, size_t len, size_t *olen )
98{
99 int ret;
100 ((void) data);
101
102 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
103 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
104
105 *olen = ret;
106 return( 0 );
107}
108
109#else /* HAVE_GETRANDOM */
110
Paul Bakker6083fd22011-12-03 21:45:14 +0000111#include <stdio.h>
112
113int platform_entropy_poll( void *data,
114 unsigned char *output, size_t len, size_t *olen )
115{
116 FILE *file;
117 size_t ret;
118 ((void) data);
119
120 *olen = 0;
121
122 file = fopen( "/dev/urandom", "rb" );
123 if( file == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200124 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200125
Paul Bakker6083fd22011-12-03 21:45:14 +0000126 ret = fread( output, 1, len, file );
127 if( ret != len )
128 {
129 fclose( file );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200130 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000131 }
132
133 fclose( file );
134 *olen = len;
135
136 return( 0 );
137}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100138#endif /* HAVE_GETRANDOM */
Paul Bakker9af723c2014-05-01 13:03:14 +0200139#endif /* _WIN32 && !EFIX64 && !EFI32 */
140#endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000141
142#if defined(POLARSSL_TIMING_C)
143int hardclock_poll( void *data,
144 unsigned char *output, size_t len, size_t *olen )
145{
146 unsigned long timer = hardclock();
147 ((void) data);
148 *olen = 0;
149
150 if( len < sizeof(unsigned long) )
151 return( 0 );
152
153 memcpy( output, &timer, sizeof(unsigned long) );
154 *olen = sizeof(unsigned long);
155
156 return( 0 );
157}
Paul Bakker9af723c2014-05-01 13:03:14 +0200158#endif /* POLARSSL_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000159
160#if defined(POLARSSL_HAVEGE_C)
161int havege_poll( void *data,
162 unsigned char *output, size_t len, size_t *olen )
163{
164 havege_state *hs = (havege_state *) data;
165 *olen = 0;
166
167 if( havege_random( hs, output, len ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200168 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000169
170 *olen = len;
171
172 return( 0 );
173}
Paul Bakker9af723c2014-05-01 13:03:14 +0200174#endif /* POLARSSL_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000175
176#endif /* POLARSSL_ENTROPY_C */