blob: 3dc2cbf7aa88e2e41ee33b19cb8a445aa44a68a3 [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é-Gonnard967a2a52015-01-22 14:28:16 +00006 * This file is part of mbed TLS (http://www.polarssl.org)
Paul Bakker6083fd22011-12-03 21:45:14 +00007 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker6083fd22011-12-03 21:45:14 +000027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker6083fd22011-12-03 21:45:14 +000031
32#if defined(POLARSSL_ENTROPY_C)
33
34#include "polarssl/entropy.h"
35#include "polarssl/entropy_poll.h"
36
37#if defined(POLARSSL_TIMING_C)
38#include "polarssl/timing.h"
39#endif
40#if defined(POLARSSL_HAVEGE_C)
41#include "polarssl/havege.h"
42#endif
43
44#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010045#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000046
Paul Bakker6083fd22011-12-03 21:45:14 +000047#if !defined(_WIN32_WINNT)
48#define _WIN32_WINNT 0x0400
49#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000050#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000051#include <wincrypt.h>
52
53int platform_entropy_poll( void *data, unsigned char *output, size_t len,
54 size_t *olen )
55{
56 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000057 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000058 *olen = 0;
59
60 if( CryptAcquireContext( &provider, NULL, NULL,
61 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
62 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +020063 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000064 }
65
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020066 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020067 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000068
69 CryptReleaseContext( provider, 0 );
70 *olen = len;
71
72 return( 0 );
73}
Paul Bakker9af723c2014-05-01 13:03:14 +020074#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000075
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010076/*
77 * Test for Linux getrandom() support.
78 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
79 * available in GNU libc and compatible libc's (eg uClibc).
80 */
81#if defined(__linux__) && defined(__GLIBC__)
82#include <linux/version.h>
83#include <unistd.h>
84#include <sys/syscall.h>
85#if defined(SYS_getrandom)
86#define HAVE_GETRANDOM
87static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
88{
89 return( syscall( SYS_getrandom, buf, buflen, flags ) );
90}
91#endif /* SYS_getrandom */
92#endif /* __linux__ */
93
94#if defined(HAVE_GETRANDOM)
95
96#include <errno.h>
97
98int platform_entropy_poll( void *data,
99 unsigned char *output, size_t len, size_t *olen )
100{
101 int ret;
102 ((void) data);
103
104 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
105 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
106
107 *olen = ret;
108 return( 0 );
109}
110
111#else /* HAVE_GETRANDOM */
112
Paul Bakker6083fd22011-12-03 21:45:14 +0000113#include <stdio.h>
114
115int platform_entropy_poll( void *data,
116 unsigned char *output, size_t len, size_t *olen )
117{
118 FILE *file;
119 size_t ret;
120 ((void) data);
121
122 *olen = 0;
123
124 file = fopen( "/dev/urandom", "rb" );
125 if( file == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200126 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200127
Paul Bakker6083fd22011-12-03 21:45:14 +0000128 ret = fread( output, 1, len, file );
129 if( ret != len )
130 {
131 fclose( file );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200132 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000133 }
134
135 fclose( file );
136 *olen = len;
137
138 return( 0 );
139}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100140#endif /* HAVE_GETRANDOM */
Paul Bakker9af723c2014-05-01 13:03:14 +0200141#endif /* _WIN32 && !EFIX64 && !EFI32 */
142#endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000143
144#if defined(POLARSSL_TIMING_C)
145int hardclock_poll( void *data,
146 unsigned char *output, size_t len, size_t *olen )
147{
148 unsigned long timer = hardclock();
149 ((void) data);
150 *olen = 0;
151
152 if( len < sizeof(unsigned long) )
153 return( 0 );
154
155 memcpy( output, &timer, sizeof(unsigned long) );
156 *olen = sizeof(unsigned long);
157
158 return( 0 );
159}
Paul Bakker9af723c2014-05-01 13:03:14 +0200160#endif /* POLARSSL_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000161
162#if defined(POLARSSL_HAVEGE_C)
163int havege_poll( void *data,
164 unsigned char *output, size_t len, size_t *olen )
165{
166 havege_state *hs = (havege_state *) data;
167 *olen = 0;
168
169 if( havege_random( hs, output, len ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200170 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000171
172 *olen = len;
173
174 return( 0 );
175}
Paul Bakker9af723c2014-05-01 13:03:14 +0200176#endif /* POLARSSL_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000177
178#endif /* POLARSSL_ENTROPY_C */