]> git.zerfleddert.de Git - proxmark3-svn/blame - common/mbedtls/entropy_poll.c
Merge pull request #969 from pwpiwi/gcc10_fixes
[proxmark3-svn] / common / mbedtls / entropy_poll.c
CommitLineData
700d8687
OM
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
68int 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
105static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
106{
107 /* MemSan cannot understand that the syscall writes to the buffer */
108#if defined(__has_feature)
109#if __has_feature(memory_sanitizer)
110 memset( buf, 0, buflen );
111#endif
112#endif
113
114 return( syscall( SYS_getrandom, buf, buflen, flags ) );
115}
116
117#include <sys/utsname.h>
118/* Check if version is at least 3.17.0 */
119static int check_version_3_17_plus( void )
120{
121 int minor;
122 struct utsname un;
123 const char *ver;
124
125 /* Get version information */
126 uname(&un);
127 ver = un.release;
128
129 /* Check major version; assume a single digit */
130 if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
131 return( -1 );
132
133 if( ver[0] - '0' > 3 )
134 return( 0 );
135
136 /* Ok, so now we know major == 3, check minor.
137 * Assume 1 or 2 digits. */
138 if( ver[2] < '0' || ver[2] > '9' )
139 return( -1 );
140
141 minor = ver[2] - '0';
142
143 if( ver[3] >= '0' && ver[3] <= '9' )
144 minor = 10 * minor + ver[3] - '0';
145 else if( ver [3] != '.' )
146 return( -1 );
147
148 if( minor < 17 )
149 return( -1 );
150
151 return( 0 );
152}
153static int has_getrandom = -1;
154#endif /* SYS_getrandom */
155#endif /* __linux__ */
156
157#include <stdio.h>
158
159int mbedtls_platform_entropy_poll( void *data,
160 unsigned char *output, size_t len, size_t *olen )
161{
162 FILE *file;
163 size_t read_len;
164 ((void) data);
165
166#if defined(HAVE_GETRANDOM)
167 if( has_getrandom == -1 )
168 has_getrandom = ( check_version_3_17_plus() == 0 );
169
170 if( has_getrandom )
171 {
172 int ret;
173
174 if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
175 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
176
177 *olen = ret;
178 return( 0 );
179 }
180#endif /* HAVE_GETRANDOM */
181
182 *olen = 0;
183
184 file = fopen( "/dev/urandom", "rb" );
185 if( file == NULL )
186 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
187
188 read_len = fread( output, 1, len, file );
189 if( read_len != len )
190 {
191 fclose( file );
192 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
193 }
194
195 fclose( file );
196 *olen = len;
197
198 return( 0 );
199}
200#endif /* _WIN32 && !EFIX64 && !EFI32 */
201#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
202
203#if defined(MBEDTLS_TEST_NULL_ENTROPY)
204int mbedtls_null_entropy_poll( void *data,
205 unsigned char *output, size_t len, size_t *olen )
206{
207 ((void) data);
208 ((void) output);
209 *olen = 0;
210
211 if( len < sizeof(unsigned char) )
212 return( 0 );
213
214 *olen = sizeof(unsigned char);
215
216 return( 0 );
217}
218#endif
219
220#if defined(MBEDTLS_TIMING_C)
221int mbedtls_hardclock_poll( void *data,
222 unsigned char *output, size_t len, size_t *olen )
223{
224 unsigned long timer = mbedtls_timing_hardclock();
225 ((void) data);
226 *olen = 0;
227
228 if( len < sizeof(unsigned long) )
229 return( 0 );
230
231 memcpy( output, &timer, sizeof(unsigned long) );
232 *olen = sizeof(unsigned long);
233
234 return( 0 );
235}
236#endif /* MBEDTLS_TIMING_C */
237
238#if defined(MBEDTLS_HAVEGE_C)
239int mbedtls_havege_poll( void *data,
240 unsigned char *output, size_t len, size_t *olen )
241{
242 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
243 *olen = 0;
244
245 if( mbedtls_havege_random( hs, output, len ) != 0 )
246 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
247
248 *olen = len;
249
250 return( 0 );
251}
252#endif /* MBEDTLS_HAVEGE_C */
253
254#if defined(MBEDTLS_ENTROPY_NV_SEED)
255int mbedtls_nv_seed_poll( void *data,
256 unsigned char *output, size_t len, size_t *olen )
257{
258 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
259 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
260 ((void) data);
261
262 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
263
264 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
265 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
266
267 if( len < use_len )
268 use_len = len;
269
270 memcpy( output, buf, use_len );
271 *olen = use_len;
272
273 return( 0 );
274}
275#endif /* MBEDTLS_ENTROPY_NV_SEED */
276
277#endif /* MBEDTLS_ENTROPY_C */
Impressum, Datenschutz