blob: dcf9ceb7ed2359f777b9b66cc7c0dc66c61d707c [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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)
35#include "polarssl/timing.h"
36#endif
37#if defined(POLARSSL_HAVEGE_C)
38#include "polarssl/havege.h"
39#endif
40
41#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
Paul Bakkerfa6a6202013-10-28 18:48:30 +010042#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker6083fd22011-12-03 21:45:14 +000043
Paul Bakker6083fd22011-12-03 21:45:14 +000044#if !defined(_WIN32_WINNT)
45#define _WIN32_WINNT 0x0400
46#endif
Paul Bakker4a2bd0d2012-11-02 11:06:08 +000047#include <windows.h>
Paul Bakker6083fd22011-12-03 21:45:14 +000048#include <wincrypt.h>
49
50int platform_entropy_poll( void *data, unsigned char *output, size_t len,
51 size_t *olen )
52{
53 HCRYPTPROV provider;
Paul Bakker6bcfc672011-12-05 13:54:00 +000054 ((void) data);
Paul Bakker6083fd22011-12-03 21:45:14 +000055 *olen = 0;
56
57 if( CryptAcquireContext( &provider, NULL, NULL,
58 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
59 {
Paul Bakkerd8bb8262014-06-17 14:06:49 +020060 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000061 }
62
Paul Bakkerb9cfaa02013-10-11 18:58:55 +020063 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
Paul Bakkerd8bb8262014-06-17 14:06:49 +020064 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +000065
66 CryptReleaseContext( provider, 0 );
67 *olen = len;
68
69 return( 0 );
70}
Paul Bakker9af723c2014-05-01 13:03:14 +020071#else /* _WIN32 && !EFIX64 && !EFI32 */
Paul Bakker6083fd22011-12-03 21:45:14 +000072
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +010073/*
74 * Test for Linux getrandom() support.
75 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
76 * available in GNU libc and compatible libc's (eg uClibc).
77 */
78#if defined(__linux__) && defined(__GLIBC__)
79#include <linux/version.h>
80#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}
88#endif /* SYS_getrandom */
89#endif /* __linux__ */
90
91#if defined(HAVE_GETRANDOM)
92
93#include <errno.h>
94
95int platform_entropy_poll( void *data,
96 unsigned char *output, size_t len, size_t *olen )
97{
98 int ret;
99 ((void) data);
100
101 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
102 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
103
104 *olen = ret;
105 return( 0 );
106}
107
108#else /* HAVE_GETRANDOM */
109
Paul Bakker6083fd22011-12-03 21:45:14 +0000110#include <stdio.h>
111
112int platform_entropy_poll( void *data,
113 unsigned char *output, size_t len, size_t *olen )
114{
115 FILE *file;
116 size_t ret;
117 ((void) data);
118
119 *olen = 0;
120
121 file = fopen( "/dev/urandom", "rb" );
122 if( file == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200123 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker9af723c2014-05-01 13:03:14 +0200124
Paul Bakker6083fd22011-12-03 21:45:14 +0000125 ret = fread( output, 1, len, file );
126 if( ret != len )
127 {
128 fclose( file );
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200129 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000130 }
131
132 fclose( file );
133 *olen = len;
134
135 return( 0 );
136}
Manuel Pégourié-Gonnard18292452015-01-09 14:34:13 +0100137#endif /* HAVE_GETRANDOM */
Paul Bakker9af723c2014-05-01 13:03:14 +0200138#endif /* _WIN32 && !EFIX64 && !EFI32 */
139#endif /* !POLARSSL_NO_PLATFORM_ENTROPY */
Paul Bakker6083fd22011-12-03 21:45:14 +0000140
141#if defined(POLARSSL_TIMING_C)
142int hardclock_poll( void *data,
143 unsigned char *output, size_t len, size_t *olen )
144{
145 unsigned long timer = hardclock();
146 ((void) data);
147 *olen = 0;
148
149 if( len < sizeof(unsigned long) )
150 return( 0 );
151
152 memcpy( output, &timer, sizeof(unsigned long) );
153 *olen = sizeof(unsigned long);
154
155 return( 0 );
156}
Paul Bakker9af723c2014-05-01 13:03:14 +0200157#endif /* POLARSSL_TIMING_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000158
159#if defined(POLARSSL_HAVEGE_C)
160int havege_poll( void *data,
161 unsigned char *output, size_t len, size_t *olen )
162{
163 havege_state *hs = (havege_state *) data;
164 *olen = 0;
165
166 if( havege_random( hs, output, len ) != 0 )
Paul Bakkerd8bb8262014-06-17 14:06:49 +0200167 return( POLARSSL_ERR_ENTROPY_SOURCE_FAILED );
Paul Bakker6083fd22011-12-03 21:45:14 +0000168
169 *olen = len;
170
171 return( 0 );
172}
Paul Bakker9af723c2014-05-01 13:03:14 +0200173#endif /* POLARSSL_HAVEGE_C */
Paul Bakker6083fd22011-12-03 21:45:14 +0000174
175#endif /* POLARSSL_ENTROPY_C */