2 * Platform-specific and custom entropy polling functions
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: GPL-2.0
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.
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.
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.
21 * This file is part of mbed TLS (https://tls.mbed.org)
24 #if defined(__linux__)
25 /* Ensure that syscall() is available even when compiling with -std=c99 */
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
32 #include MBEDTLS_CONFIG_FILE
37 #if defined(MBEDTLS_ENTROPY_C)
39 #include "mbedtls/entropy.h"
40 #include "mbedtls/entropy_poll.h"
42 #if defined(MBEDTLS_TIMING_C)
43 #include "mbedtls/timing.h"
45 #if defined(MBEDTLS_HAVEGE_C)
46 #include "mbedtls/havege.h"
48 #if defined(MBEDTLS_ENTROPY_NV_SEED)
49 #include "mbedtls/platform.h"
52 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
54 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
55 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
57 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
60 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
62 #if !defined(_WIN32_WINNT)
63 #define _WIN32_WINNT 0x0400
68 int mbedtls_platform_entropy_poll( void *data
, unsigned char *output
, size_t len
,
75 if( CryptAcquireContext( &provider
, NULL
, NULL
,
76 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
) == FALSE
)
78 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
81 if( CryptGenRandom( provider
, (DWORD
) len
, output
) == FALSE
)
83 CryptReleaseContext( provider
, 0 );
84 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
87 CryptReleaseContext( provider
, 0 );
92 #else /* _WIN32 && !EFIX64 && !EFI32 */
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).
99 #if defined(__linux__) && defined(__GLIBC__)
101 #include <sys/syscall.h>
102 #if defined(SYS_getrandom)
103 #define HAVE_GETRANDOM
105 static int getrandom_wrapper( void *buf
, size_t buflen
, unsigned int flags
)
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
);
114 return( syscall( SYS_getrandom
, buf
, buflen
, flags
) );
117 #include <sys/utsname.h>
118 /* Check if version is at least 3.17.0 */
119 static int check_version_3_17_plus( void )
125 /* Get version information */
129 /* Check major version; assume a single digit */
130 if( ver
[0] < '3' || ver
[0] > '9' || ver
[1] != '.' )
133 if( ver
[0] - '0' > 3 )
136 /* Ok, so now we know major == 3, check minor.
137 * Assume 1 or 2 digits. */
138 if( ver
[2] < '0' || ver
[2] > '9' )
141 minor
= ver
[2] - '0';
143 if( ver
[3] >= '0' && ver
[3] <= '9' )
144 minor
= 10 * minor
+ ver
[3] - '0';
145 else if( ver
[3] != '.' )
153 static int has_getrandom
= -1;
154 #endif /* SYS_getrandom */
155 #endif /* __linux__ */
159 int mbedtls_platform_entropy_poll( void *data
,
160 unsigned char *output
, size_t len
, size_t *olen
)
166 #if defined(HAVE_GETRANDOM)
167 if( has_getrandom
== -1 )
168 has_getrandom
= ( check_version_3_17_plus() == 0 );
174 if( ( ret
= getrandom_wrapper( output
, len
, 0 ) ) < 0 )
175 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
180 #endif /* HAVE_GETRANDOM */
184 file
= fopen( "/dev/urandom", "rb" );
186 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
188 read_len
= fread( output
, 1, len
, file
);
189 if( read_len
!= len
)
192 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
200 #endif /* _WIN32 && !EFIX64 && !EFI32 */
201 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
203 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
204 int mbedtls_null_entropy_poll( void *data
,
205 unsigned char *output
, size_t len
, size_t *olen
)
211 if( len
< sizeof(unsigned char) )
214 *olen
= sizeof(unsigned char);
220 #if defined(MBEDTLS_TIMING_C)
221 int mbedtls_hardclock_poll( void *data
,
222 unsigned char *output
, size_t len
, size_t *olen
)
224 unsigned long timer
= mbedtls_timing_hardclock();
228 if( len
< sizeof(unsigned long) )
231 memcpy( output
, &timer
, sizeof(unsigned long) );
232 *olen
= sizeof(unsigned long);
236 #endif /* MBEDTLS_TIMING_C */
238 #if defined(MBEDTLS_HAVEGE_C)
239 int mbedtls_havege_poll( void *data
,
240 unsigned char *output
, size_t len
, size_t *olen
)
242 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) data
;
245 if( mbedtls_havege_random( hs
, output
, len
) != 0 )
246 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
252 #endif /* MBEDTLS_HAVEGE_C */
254 #if defined(MBEDTLS_ENTROPY_NV_SEED)
255 int mbedtls_nv_seed_poll( void *data
,
256 unsigned char *output
, size_t len
, size_t *olen
)
258 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
259 size_t use_len
= MBEDTLS_ENTROPY_BLOCK_SIZE
;
262 memset( buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
264 if( mbedtls_nv_seed_read( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) < 0 )
265 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
270 memcpy( output
, buf
, use_len
);
275 #endif /* MBEDTLS_ENTROPY_NV_SEED */
277 #endif /* MBEDTLS_ENTROPY_C */