blob: 8148affb50cfcd195451438e9101f4268c10f0e4 [file] [log] [blame]
Edison Aic6672fd2018-02-28 15:01:47 +08001// SPDX-License-Identifier: Apache-2.0
Jens Wiklander817466c2018-05-22 13:49:31 +02002/*
3 * Platform-specific and custom entropy polling functions
4 *
5 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
Jens Wiklander817466c2018-05-22 13:49:31 +02006 *
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
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
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.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
Jens Wiklander3d3b0592019-03-20 15:30:29 +010022#if defined(__linux__)
23/* Ensure that syscall() is available even when compiling with -std=c99 */
24#define _GNU_SOURCE
25#endif
26
Jens Wiklander817466c2018-05-22 13:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
28#include "mbedtls/config.h"
29#else
30#include MBEDTLS_CONFIG_FILE
31#endif
32
Jens Wiklander3d3b0592019-03-20 15:30:29 +010033#include <string.h>
34
Jens Wiklander817466c2018-05-22 13:49:31 +020035#if defined(MBEDTLS_ENTROPY_C)
36
37#include "mbedtls/entropy.h"
38#include "mbedtls/entropy_poll.h"
Jerome Forissier11fa71b2020-04-20 17:17:56 +020039#include "mbedtls/error.h"
Jens Wiklander817466c2018-05-22 13:49:31 +020040
41#if defined(MBEDTLS_TIMING_C)
Jens Wiklander817466c2018-05-22 13:49:31 +020042#include "mbedtls/timing.h"
43#endif
44#if defined(MBEDTLS_HAVEGE_C)
45#include "mbedtls/havege.h"
46#endif
47#if defined(MBEDTLS_ENTROPY_NV_SEED)
48#include "mbedtls/platform.h"
49#endif
50
51#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
52
53#if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
Jens Wiklander3d3b0592019-03-20 15:30:29 +010054 !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && \
55 !defined(__HAIKU__)
Jens Wiklander817466c2018-05-22 13:49:31 +020056#error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
57#endif
58
59#if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
60
61#if !defined(_WIN32_WINNT)
62#define _WIN32_WINNT 0x0400
63#endif
64#include <windows.h>
65#include <wincrypt.h>
66
67int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
68 size_t *olen )
69{
70 HCRYPTPROV provider;
71 ((void) data);
72 *olen = 0;
73
74 if( CryptAcquireContext( &provider, NULL, NULL,
75 PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
76 {
77 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
78 }
79
80 if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
81 {
82 CryptReleaseContext( provider, 0 );
83 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
84 }
85
86 CryptReleaseContext( provider, 0 );
87 *olen = len;
88
89 return( 0 );
90}
91#else /* _WIN32 && !EFIX64 && !EFI32 */
92
93/*
94 * Test for Linux getrandom() support.
95 * Since there is no wrapper in the libc yet, use the generic syscall wrapper
96 * available in GNU libc and compatible libc's (eg uClibc).
97 */
98#if defined(__linux__) && defined(__GLIBC__)
99#include <unistd.h>
100#include <sys/syscall.h>
101#if defined(SYS_getrandom)
102#define HAVE_GETRANDOM
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100103#include <errno.h>
Jens Wiklander817466c2018-05-22 13:49:31 +0200104
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
Jens Wiklander817466c2018-05-22 13:49:31 +0200113 return( syscall( SYS_getrandom, buf, buflen, flags ) );
114}
Jens Wiklander817466c2018-05-22 13:49:31 +0200115#endif /* SYS_getrandom */
116#endif /* __linux__ */
117
118#include <stdio.h>
119
120int mbedtls_platform_entropy_poll( void *data,
121 unsigned char *output, size_t len, size_t *olen )
122{
123 FILE *file;
124 size_t read_len;
Jerome Forissier11fa71b2020-04-20 17:17:56 +0200125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jens Wiklander817466c2018-05-22 13:49:31 +0200126 ((void) data);
127
128#if defined(HAVE_GETRANDOM)
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100129 ret = getrandom_wrapper( output, len, 0 );
130 if( ret >= 0 )
Jens Wiklander817466c2018-05-22 13:49:31 +0200131 {
Jens Wiklander817466c2018-05-22 13:49:31 +0200132 *olen = ret;
133 return( 0 );
134 }
Jens Wiklander3d3b0592019-03-20 15:30:29 +0100135 else if( errno != ENOSYS )
136 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
137 /* Fall through if the system call isn't known. */
138#else
139 ((void) ret);
Jens Wiklander817466c2018-05-22 13:49:31 +0200140#endif /* HAVE_GETRANDOM */
141
142 *olen = 0;
143
144 file = fopen( "/dev/urandom", "rb" );
145 if( file == NULL )
146 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
147
148 read_len = fread( output, 1, len, file );
149 if( read_len != len )
150 {
151 fclose( file );
152 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
153 }
154
155 fclose( file );
156 *olen = len;
157
158 return( 0 );
159}
160#endif /* _WIN32 && !EFIX64 && !EFI32 */
161#endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
162
163#if defined(MBEDTLS_TEST_NULL_ENTROPY)
164int mbedtls_null_entropy_poll( void *data,
165 unsigned char *output, size_t len, size_t *olen )
166{
167 ((void) data);
168 ((void) output);
169 *olen = 0;
170
171 if( len < sizeof(unsigned char) )
172 return( 0 );
173
174 *olen = sizeof(unsigned char);
175
176 return( 0 );
177}
178#endif
179
180#if defined(MBEDTLS_TIMING_C)
181int mbedtls_hardclock_poll( void *data,
182 unsigned char *output, size_t len, size_t *olen )
183{
184 unsigned long timer = mbedtls_timing_hardclock();
185 ((void) data);
186 *olen = 0;
187
188 if( len < sizeof(unsigned long) )
189 return( 0 );
190
191 memcpy( output, &timer, sizeof(unsigned long) );
192 *olen = sizeof(unsigned long);
193
194 return( 0 );
195}
196#endif /* MBEDTLS_TIMING_C */
197
198#if defined(MBEDTLS_HAVEGE_C)
199int mbedtls_havege_poll( void *data,
200 unsigned char *output, size_t len, size_t *olen )
201{
202 mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
203 *olen = 0;
204
205 if( mbedtls_havege_random( hs, output, len ) != 0 )
206 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
207
208 *olen = len;
209
210 return( 0 );
211}
212#endif /* MBEDTLS_HAVEGE_C */
213
214#if defined(MBEDTLS_ENTROPY_NV_SEED)
215int mbedtls_nv_seed_poll( void *data,
216 unsigned char *output, size_t len, size_t *olen )
217{
218 unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
219 size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
220 ((void) data);
221
222 memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
223
224 if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
225 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
226
227 if( len < use_len )
228 use_len = len;
229
230 memcpy( output, buf, use_len );
231 *olen = use_len;
232
233 return( 0 );
234}
235#endif /* MBEDTLS_ENTROPY_NV_SEED */
236
237#endif /* MBEDTLS_ENTROPY_C */