2 * Platform-specific and custom entropy polling functions
4 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_ENTROPY_C)
30 #include "mbedtls/entropy.h"
31 #include "mbedtls/entropy_poll.h"
33 #if defined(MBEDTLS_TIMING_C)
35 #include "mbedtls/timing.h"
37 #if defined(MBEDTLS_HAVEGE_C)
38 #include "mbedtls/havege.h"
40 #if defined(MBEDTLS_ENTROPY_NV_SEED)
41 #include "mbedtls/platform.h"
44 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
46 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
47 !defined(__APPLE__) && !defined(_WIN32)
48 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
51 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
53 #if !defined(_WIN32_WINNT)
54 #define _WIN32_WINNT 0x0400
59 int mbedtls_platform_entropy_poll( void *data
, unsigned char *output
, size_t len
,
66 if( CryptAcquireContext( &provider
, NULL
, NULL
,
67 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
) == FALSE
)
69 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
72 if( CryptGenRandom( provider
, (DWORD
) len
, output
) == FALSE
)
74 CryptReleaseContext( provider
, 0 );
75 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
78 CryptReleaseContext( provider
, 0 );
83 #else /* _WIN32 && !EFIX64 && !EFI32 */
86 * Test for Linux getrandom() support.
87 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
88 * available in GNU libc and compatible libc's (eg uClibc).
90 #if defined(__linux__) && defined(__GLIBC__)
92 #include <sys/syscall.h>
93 #if defined(SYS_getrandom)
94 #define HAVE_GETRANDOM
96 static int getrandom_wrapper( void *buf
, size_t buflen
, unsigned int flags
)
98 /* MemSan cannot understand that the syscall writes to the buffer */
99 #if defined(__has_feature)
100 #if __has_feature(memory_sanitizer)
101 memset( buf
, 0, buflen
);
105 return( syscall( SYS_getrandom
, buf
, buflen
, flags
) );
108 #include <sys/utsname.h>
109 /* Check if version is at least 3.17.0 */
110 static int check_version_3_17_plus( void )
116 /* Get version information */
120 /* Check major version; assume a single digit */
121 if( ver
[0] < '3' || ver
[0] > '9' || ver
[1] != '.' )
124 if( ver
[0] - '0' > 3 )
127 /* Ok, so now we know major == 3, check minor.
128 * Assume 1 or 2 digits. */
129 if( ver
[2] < '0' || ver
[2] > '9' )
132 minor
= ver
[2] - '0';
134 if( ver
[3] >= '0' && ver
[3] <= '9' )
135 minor
= 10 * minor
+ ver
[3] - '0';
136 else if( ver
[3] != '.' )
144 static int has_getrandom
= -1;
145 #endif /* SYS_getrandom */
146 #endif /* __linux__ */
150 int mbedtls_platform_entropy_poll( void *data
,
151 unsigned char *output
, size_t len
, size_t *olen
)
157 #if defined(HAVE_GETRANDOM)
158 if( has_getrandom
== -1 )
159 has_getrandom
= ( check_version_3_17_plus() == 0 );
165 if( ( ret
= getrandom_wrapper( output
, len
, 0 ) ) < 0 )
166 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
171 #endif /* HAVE_GETRANDOM */
175 file
= fopen( "/dev/urandom", "rb" );
177 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
179 read_len
= fread( output
, 1, len
, file
);
180 if( read_len
!= len
)
183 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
191 #endif /* _WIN32 && !EFIX64 && !EFI32 */
192 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
194 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
195 int mbedtls_null_entropy_poll( void *data
,
196 unsigned char *output
, size_t len
, size_t *olen
)
202 if( len
< sizeof(unsigned char) )
205 *olen
= sizeof(unsigned char);
211 #if defined(MBEDTLS_TIMING_C)
212 int mbedtls_hardclock_poll( void *data
,
213 unsigned char *output
, size_t len
, size_t *olen
)
215 unsigned long timer
= mbedtls_timing_hardclock();
219 if( len
< sizeof(unsigned long) )
222 memcpy( output
, &timer
, sizeof(unsigned long) );
223 *olen
= sizeof(unsigned long);
227 #endif /* MBEDTLS_TIMING_C */
229 #if defined(MBEDTLS_HAVEGE_C)
230 int mbedtls_havege_poll( void *data
,
231 unsigned char *output
, size_t len
, size_t *olen
)
233 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) data
;
236 if( mbedtls_havege_random( hs
, output
, len
) != 0 )
237 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
243 #endif /* MBEDTLS_HAVEGE_C */
245 #if defined(MBEDTLS_ENTROPY_NV_SEED)
246 int mbedtls_nv_seed_poll( void *data
,
247 unsigned char *output
, size_t len
, size_t *olen
)
249 unsigned char buf
[MBEDTLS_ENTROPY_BLOCK_SIZE
];
250 size_t use_len
= MBEDTLS_ENTROPY_BLOCK_SIZE
;
253 memset( buf
, 0, MBEDTLS_ENTROPY_BLOCK_SIZE
);
255 if( mbedtls_nv_seed_read( buf
, MBEDTLS_ENTROPY_BLOCK_SIZE
) < 0 )
256 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
261 memcpy( output
, buf
, use_len
);
266 #endif /* MBEDTLS_ENTROPY_NV_SEED */
268 #endif /* MBEDTLS_ENTROPY_C */