2 * Platform-specific and custom entropy polling functions
4 * Copyright (C) 2006-2015, 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"
41 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
42 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
44 #if !defined(_WIN32_WINNT)
45 #define _WIN32_WINNT 0x0400
50 int mbedtls_platform_entropy_poll( void *data
, unsigned char *output
, size_t len
,
57 if( CryptAcquireContext( &provider
, NULL
, NULL
,
58 PROV_RSA_FULL
, CRYPT_VERIFYCONTEXT
) == FALSE
)
60 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
63 if( CryptGenRandom( provider
, (DWORD
) len
, output
) == FALSE
)
64 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
66 CryptReleaseContext( provider
, 0 );
71 #else /* _WIN32 && !EFIX64 && !EFI32 */
74 * Test for Linux getrandom() support.
75 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
76 * available in GNU libc and compatible libc's (eg uClibc).
78 #if defined(__linux__) && defined(__GLIBC__)
80 #include <sys/syscall.h>
81 #if defined(SYS_getrandom)
82 #define HAVE_GETRANDOM
84 static int getrandom_wrapper( void *buf
, size_t buflen
, unsigned int flags
)
86 /* MemSan cannot understand that the syscall writes to the buffer */
87 #if defined(__has_feature)
88 #if __has_feature(memory_sanitizer)
89 memset( buf
, 0, buflen
);
93 return( syscall( SYS_getrandom
, buf
, buflen
, flags
) );
96 #include <sys/utsname.h>
97 /* Check if version is at least 3.17.0 */
98 static int check_version_3_17_plus( void )
104 /* Get version information */
108 /* Check major version; assume a single digit */
109 if( ver
[0] < '3' || ver
[0] > '9' || ver
[1] != '.' )
112 if( ver
[0] - '0' > 3 )
115 /* Ok, so now we know major == 3, check minor.
116 * Assume 1 or 2 digits. */
117 if( ver
[2] < '0' || ver
[2] > '9' )
120 minor
= ver
[2] - '0';
122 if( ver
[3] >= '0' && ver
[3] <= '9' )
123 minor
= 10 * minor
+ ver
[3] - '0';
124 else if( ver
[3] != '.' )
132 static int has_getrandom
= -1;
133 #endif /* SYS_getrandom */
134 #endif /* __linux__ */
138 int mbedtls_platform_entropy_poll( void *data
,
139 unsigned char *output
, size_t len
, size_t *olen
)
145 #if defined(HAVE_GETRANDOM)
146 if( has_getrandom
== -1 )
147 has_getrandom
= ( check_version_3_17_plus() == 0 );
153 if( ( ret
= getrandom_wrapper( output
, len
, 0 ) ) < 0 )
154 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
159 #endif /* HAVE_GETRANDOM */
163 file
= fopen( "/dev/urandom", "rb" );
165 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
167 read_len
= fread( output
, 1, len
, file
);
168 if( read_len
!= len
)
171 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
179 #endif /* _WIN32 && !EFIX64 && !EFI32 */
180 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
182 #if defined(MBEDTLS_TIMING_C)
183 int mbedtls_hardclock_poll( void *data
,
184 unsigned char *output
, size_t len
, size_t *olen
)
186 unsigned long timer
= mbedtls_timing_hardclock();
190 if( len
< sizeof(unsigned long) )
193 memcpy( output
, &timer
, sizeof(unsigned long) );
194 *olen
= sizeof(unsigned long);
198 #endif /* MBEDTLS_TIMING_C */
200 #if defined(MBEDTLS_HAVEGE_C)
201 int mbedtls_havege_poll( void *data
,
202 unsigned char *output
, size_t len
, size_t *olen
)
204 mbedtls_havege_state
*hs
= (mbedtls_havege_state
*) data
;
207 if( mbedtls_havege_random( hs
, output
, len
) != 0 )
208 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
);
214 #endif /* MBEDTLS_HAVEGE_C */
216 #endif /* MBEDTLS_ENTROPY_C */