1 /*
2  *  Platform-specific and custom entropy polling functions
3  *
4  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: GPL-2.0
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License along
18  *  with this program; if not, write to the Free Software Foundation, Inc.,
19  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  *  This file is part of mbed TLS (https://tls.mbed.org)
22  */
23 
24 #if defined(__linux__)
25 /* Ensure that syscall() is available even when compiling with -std=c99 */
26 #define _GNU_SOURCE
27 #endif
28 
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
31 #else
32 #include MBEDTLS_CONFIG_FILE
33 #endif
34 
35 #include <string.h>
36 
37 #if defined(MBEDTLS_ENTROPY_C)
38 
39 #include "mbedtls/entropy.h"
40 #include "mbedtls/entropy_poll.h"
41 
42 #if defined(MBEDTLS_TIMING_C)
43 #include "mbedtls/timing.h"
44 #endif
45 #if defined(MBEDTLS_HAVEGE_C)
46 #include "mbedtls/havege.h"
47 #endif
48 #if defined(MBEDTLS_ENTROPY_NV_SEED)
49 #include "mbedtls/platform.h"
50 #endif
51 
52 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
53 
54 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
55     !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
56     !defined(__HAIKU__)
57 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
58 #endif
59 
60 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
61 
62 #if !defined(_WIN32_WINNT)
63 #define _WIN32_WINNT 0x0400
64 #endif
65 #include <windows.h>
66 #include <wincrypt.h>
67 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)68 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
69                            size_t *olen )
70 {
71     HCRYPTPROV provider;
72     ((void) data);
73     *olen = 0;
74 
75     if( CryptAcquireContext( &provider, NULL, NULL,
76                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
77     {
78         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
79     }
80 
81     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
82     {
83         CryptReleaseContext( provider, 0 );
84         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
85     }
86 
87     CryptReleaseContext( provider, 0 );
88     *olen = len;
89 
90     return( 0 );
91 }
92 #else /* _WIN32 && !EFIX64 && !EFI32 */
93 
94 /*
95  * Test for Linux getrandom() support.
96  * Since there is no wrapper in the libc yet, use the generic syscall wrapper
97  * available in GNU libc and compatible libc's (eg uClibc).
98  */
99 #if defined(__linux__) && defined(__GLIBC__)
100 #include <unistd.h>
101 #include <sys/syscall.h>
102 #if defined(SYS_getrandom)
103 #define HAVE_GETRANDOM
104 #include <errno.h>
105 
getrandom_wrapper(void * buf,size_t buflen,unsigned int flags)106 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
107 {
108     /* MemSan cannot understand that the syscall writes to the buffer */
109 #if defined(__has_feature)
110 #if __has_feature(memory_sanitizer)
111     memset( buf, 0, buflen );
112 #endif
113 #endif
114     return( syscall( SYS_getrandom, buf, buflen, flags ) );
115 }
116 #endif /* SYS_getrandom */
117 #endif /* __linux__ */
118 
119 #include <stdio.h>
120 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)121 int mbedtls_platform_entropy_poll( void *data,
122                            unsigned char *output, size_t len, size_t *olen )
123 {
124     FILE *file;
125     size_t read_len;
126     int ret;
127     ((void) data);
128 
129 #if defined(HAVE_GETRANDOM)
130     ret = getrandom_wrapper( output, len, 0 );
131     if( ret >= 0 )
132     {
133         *olen = ret;
134         return( 0 );
135     }
136     else if( errno != ENOSYS )
137         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
138     /* Fall through if the system call isn't known. */
139 #else
140     ((void) ret);
141 #endif /* HAVE_GETRANDOM */
142 
143     *olen = 0;
144 
145     file = fopen( "/dev/urandom", "rb" );
146     if( file == NULL )
147         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
148 
149     read_len = fread( output, 1, len, file );
150     if( read_len != len )
151     {
152         fclose( file );
153         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
154     }
155 
156     fclose( file );
157     *olen = len;
158 
159     return( 0 );
160 }
161 #endif /* _WIN32 && !EFIX64 && !EFI32 */
162 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
163 
164 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
mbedtls_null_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)165 int mbedtls_null_entropy_poll( void *data,
166                     unsigned char *output, size_t len, size_t *olen )
167 {
168     ((void) data);
169     ((void) output);
170     *olen = 0;
171 
172     if( len < sizeof(unsigned char) )
173         return( 0 );
174 
175     *olen = sizeof(unsigned char);
176 
177     return( 0 );
178 }
179 #endif
180 
181 #if defined(MBEDTLS_TIMING_C)
mbedtls_hardclock_poll(void * data,unsigned char * output,size_t len,size_t * olen)182 int mbedtls_hardclock_poll( void *data,
183                     unsigned char *output, size_t len, size_t *olen )
184 {
185     unsigned long timer = mbedtls_timing_hardclock();
186     ((void) data);
187     *olen = 0;
188 
189     if( len < sizeof(unsigned long) )
190         return( 0 );
191 
192     memcpy( output, &timer, sizeof(unsigned long) );
193     *olen = sizeof(unsigned long);
194 
195     return( 0 );
196 }
197 #endif /* MBEDTLS_TIMING_C */
198 
199 #if defined(MBEDTLS_HAVEGE_C)
mbedtls_havege_poll(void * data,unsigned char * output,size_t len,size_t * olen)200 int mbedtls_havege_poll( void *data,
201                  unsigned char *output, size_t len, size_t *olen )
202 {
203     mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
204     *olen = 0;
205 
206     if( mbedtls_havege_random( hs, output, len ) != 0 )
207         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
208 
209     *olen = len;
210 
211     return( 0 );
212 }
213 #endif /* MBEDTLS_HAVEGE_C */
214 
215 #if defined(MBEDTLS_ENTROPY_NV_SEED)
mbedtls_nv_seed_poll(void * data,unsigned char * output,size_t len,size_t * olen)216 int mbedtls_nv_seed_poll( void *data,
217                           unsigned char *output, size_t len, size_t *olen )
218 {
219     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
220     size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
221     ((void) data);
222 
223     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
224 
225     if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
226       return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
227 
228     if( len < use_len )
229       use_len = len;
230 
231     memcpy( output, buf, use_len );
232     *olen = use_len;
233 
234     return( 0 );
235 }
236 #endif /* MBEDTLS_ENTROPY_NV_SEED */
237 
238 #endif /* MBEDTLS_ENTROPY_C */
239