xref: /reactos/dll/3rdparty/mbedtls/entropy_poll.c (revision 5100859e)
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(MBEDTLS_CONFIG_FILE)
25 #include "mbedtls/config.h"
26 #else
27 #include MBEDTLS_CONFIG_FILE
28 #endif
29 
30 #if defined(MBEDTLS_ENTROPY_C)
31 
32 #include "mbedtls/entropy.h"
33 #include "mbedtls/entropy_poll.h"
34 
35 #if defined(MBEDTLS_TIMING_C)
36 #include <string.h>
37 #include "mbedtls/timing.h"
38 #endif
39 #if defined(MBEDTLS_HAVEGE_C)
40 #include "mbedtls/havege.h"
41 #endif
42 #if defined(MBEDTLS_ENTROPY_NV_SEED)
43 #include "mbedtls/platform.h"
44 #endif
45 
46 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
47 
48 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
49     !defined(__APPLE__) && !defined(_WIN32)
50 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
51 #endif
52 
53 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
54 
55 #if !defined(_WIN32_WINNT)
56 #define _WIN32_WINNT 0x0400
57 #endif
58 #include <windows.h>
59 #include <wincrypt.h>
60 
61 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
62                            size_t *olen )
63 {
64     HCRYPTPROV provider;
65     ((void) data);
66     *olen = 0;
67 
68     if( CryptAcquireContext( &provider, NULL, NULL,
69                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
70     {
71         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
72     }
73 
74     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
75     {
76         CryptReleaseContext( provider, 0 );
77         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
78     }
79 
80     CryptReleaseContext( provider, 0 );
81     *olen = len;
82 
83     return( 0 );
84 }
85 #else /* _WIN32 && !EFIX64 && !EFI32 */
86 
87 /*
88  * Test for Linux getrandom() support.
89  * Since there is no wrapper in the libc yet, use the generic syscall wrapper
90  * available in GNU libc and compatible libc's (eg uClibc).
91  */
92 #if defined(__linux__) && defined(__GLIBC__)
93 #include <unistd.h>
94 #include <sys/syscall.h>
95 #if defined(SYS_getrandom)
96 #define HAVE_GETRANDOM
97 
98 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
99 {
100     /* MemSan cannot understand that the syscall writes to the buffer */
101 #if defined(__has_feature)
102 #if __has_feature(memory_sanitizer)
103     memset( buf, 0, buflen );
104 #endif
105 #endif
106 
107     return( syscall( SYS_getrandom, buf, buflen, flags ) );
108 }
109 
110 #include <sys/utsname.h>
111 /* Check if version is at least 3.17.0 */
112 static int check_version_3_17_plus( void )
113 {
114     int minor;
115     struct utsname un;
116     const char *ver;
117 
118     /* Get version information */
119     uname(&un);
120     ver = un.release;
121 
122     /* Check major version; assume a single digit */
123     if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
124         return( -1 );
125 
126     if( ver[0] - '0' > 3 )
127         return( 0 );
128 
129     /* Ok, so now we know major == 3, check minor.
130      * Assume 1 or 2 digits. */
131     if( ver[2] < '0' || ver[2] > '9' )
132         return( -1 );
133 
134     minor = ver[2] - '0';
135 
136     if( ver[3] >= '0' && ver[3] <= '9' )
137         minor = 10 * minor + ver[3] - '0';
138     else if( ver [3] != '.' )
139         return( -1 );
140 
141     if( minor < 17 )
142         return( -1 );
143 
144     return( 0 );
145 }
146 static int has_getrandom = -1;
147 #endif /* SYS_getrandom */
148 #endif /* __linux__ */
149 
150 #include <stdio.h>
151 
152 int mbedtls_platform_entropy_poll( void *data,
153                            unsigned char *output, size_t len, size_t *olen )
154 {
155     FILE *file;
156     size_t read_len;
157     ((void) data);
158 
159 #if defined(HAVE_GETRANDOM)
160     if( has_getrandom == -1 )
161         has_getrandom = ( check_version_3_17_plus() == 0 );
162 
163     if( has_getrandom )
164     {
165         int ret;
166 
167         if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
168             return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
169 
170         *olen = ret;
171         return( 0 );
172     }
173 #endif /* HAVE_GETRANDOM */
174 
175     *olen = 0;
176 
177     file = fopen( "/dev/urandom", "rb" );
178     if( file == NULL )
179         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
180 
181     read_len = fread( output, 1, len, file );
182     if( read_len != len )
183     {
184         fclose( file );
185         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
186     }
187 
188     fclose( file );
189     *olen = len;
190 
191     return( 0 );
192 }
193 #endif /* _WIN32 && !EFIX64 && !EFI32 */
194 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
195 
196 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
197 int mbedtls_null_entropy_poll( void *data,
198                     unsigned char *output, size_t len, size_t *olen )
199 {
200     ((void) data);
201     ((void) output);
202     *olen = 0;
203 
204     if( len < sizeof(unsigned char) )
205         return( 0 );
206 
207     *olen = sizeof(unsigned char);
208 
209     return( 0 );
210 }
211 #endif
212 
213 #if defined(MBEDTLS_TIMING_C)
214 int mbedtls_hardclock_poll( void *data,
215                     unsigned char *output, size_t len, size_t *olen )
216 {
217     unsigned long timer = mbedtls_timing_hardclock();
218     ((void) data);
219     *olen = 0;
220 
221     if( len < sizeof(unsigned long) )
222         return( 0 );
223 
224     memcpy( output, &timer, sizeof(unsigned long) );
225     *olen = sizeof(unsigned long);
226 
227     return( 0 );
228 }
229 #endif /* MBEDTLS_TIMING_C */
230 
231 #if defined(MBEDTLS_HAVEGE_C)
232 int mbedtls_havege_poll( void *data,
233                  unsigned char *output, size_t len, size_t *olen )
234 {
235     mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
236     *olen = 0;
237 
238     if( mbedtls_havege_random( hs, output, len ) != 0 )
239         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
240 
241     *olen = len;
242 
243     return( 0 );
244 }
245 #endif /* MBEDTLS_HAVEGE_C */
246 
247 #if defined(MBEDTLS_ENTROPY_NV_SEED)
248 int mbedtls_nv_seed_poll( void *data,
249                           unsigned char *output, size_t len, size_t *olen )
250 {
251     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
252     size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
253     ((void) data);
254 
255     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
256 
257     if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
258       return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
259 
260     if( len < use_len )
261       use_len = len;
262 
263     memcpy( output, buf, use_len );
264     *olen = use_len;
265 
266     return( 0 );
267 }
268 #endif /* MBEDTLS_ENTROPY_NV_SEED */
269 
270 #endif /* MBEDTLS_ENTROPY_C */
271