1 /*
2  *  Platform-specific and custom entropy polling functions
3  *
4  *  Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
5  *  SPDX-License-Identifier: Apache-2.0
6  *
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 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_ENTROPY_C)
29 
30 #include "mbedtls/entropy.h"
31 #include "mbedtls/entropy_poll.h"
32 
33 #if defined(MBEDTLS_TIMING_C)
34 #include <string.h>
35 #include "mbedtls/timing.h"
36 #endif
37 #if defined(MBEDTLS_HAVEGE_C)
38 #include "mbedtls/havege.h"
39 #endif
40 #if defined(MBEDTLS_ENTROPY_NV_SEED)
41 #include "mbedtls/platform.h"
42 #endif
43 
44 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
45 
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"
49 #endif
50 
51 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
52 
53 #if !defined(_WIN32_WINNT)
54 #define _WIN32_WINNT 0x0400
55 #endif
56 #include <windows.h>
57 #include <wincrypt.h>
58 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)59 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
60                            size_t *olen )
61 {
62     HCRYPTPROV provider;
63     ((void) data);
64     *olen = 0;
65 
66     if( CryptAcquireContext( &provider, NULL, NULL,
67                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
68     {
69         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
70     }
71 
72     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
73     {
74         CryptReleaseContext( provider, 0 );
75         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
76     }
77 
78     CryptReleaseContext( provider, 0 );
79     *olen = len;
80 
81     return( 0 );
82 }
83 #else /* _WIN32 && !EFIX64 && !EFI32 */
84 
85 /*
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).
89  */
90 #if defined(__linux__) && defined(__GLIBC__)
91 #include <unistd.h>
92 #include <sys/syscall.h>
93 #if defined(SYS_getrandom)
94 #define HAVE_GETRANDOM
95 
getrandom_wrapper(void * buf,size_t buflen,unsigned int flags)96 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
97 {
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 );
102 #endif
103 #endif
104 
105     return( syscall( SYS_getrandom, buf, buflen, flags ) );
106 }
107 
108 #include <sys/utsname.h>
109 /* Check if version is at least 3.17.0 */
check_version_3_17_plus(void)110 static int check_version_3_17_plus( void )
111 {
112     int minor;
113     struct utsname un;
114     const char *ver;
115 
116     /* Get version information */
117     uname(&un);
118     ver = un.release;
119 
120     /* Check major version; assume a single digit */
121     if( ver[0] < '3' || ver[0] > '9' || ver [1] != '.' )
122         return( -1 );
123 
124     if( ver[0] - '0' > 3 )
125         return( 0 );
126 
127     /* Ok, so now we know major == 3, check minor.
128      * Assume 1 or 2 digits. */
129     if( ver[2] < '0' || ver[2] > '9' )
130         return( -1 );
131 
132     minor = ver[2] - '0';
133 
134     if( ver[3] >= '0' && ver[3] <= '9' )
135         minor = 10 * minor + ver[3] - '0';
136     else if( ver [3] != '.' )
137         return( -1 );
138 
139     if( minor < 17 )
140         return( -1 );
141 
142     return( 0 );
143 }
144 static int has_getrandom = -1;
145 #endif /* SYS_getrandom */
146 #endif /* __linux__ */
147 
148 #include <stdio.h>
149 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)150 int mbedtls_platform_entropy_poll( void *data,
151                            unsigned char *output, size_t len, size_t *olen )
152 {
153     FILE *file;
154     size_t read_len;
155     ((void) data);
156 
157 #if defined(HAVE_GETRANDOM)
158     if( has_getrandom == -1 )
159         has_getrandom = ( check_version_3_17_plus() == 0 );
160 
161     if( has_getrandom )
162     {
163         int ret;
164 
165         if( ( ret = getrandom_wrapper( output, len, 0 ) ) < 0 )
166             return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
167 
168         *olen = ret;
169         return( 0 );
170     }
171 #endif /* HAVE_GETRANDOM */
172 
173     *olen = 0;
174 
175     file = fopen( "/dev/urandom", "rb" );
176     if( file == NULL )
177         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
178 
179     read_len = fread( output, 1, len, file );
180     if( read_len != len )
181     {
182         fclose( file );
183         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
184     }
185 
186     fclose( file );
187     *olen = len;
188 
189     return( 0 );
190 }
191 #endif /* _WIN32 && !EFIX64 && !EFI32 */
192 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
193 
194 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
mbedtls_null_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)195 int mbedtls_null_entropy_poll( void *data,
196                     unsigned char *output, size_t len, size_t *olen )
197 {
198     ((void) data);
199     ((void) output);
200     *olen = 0;
201 
202     if( len < sizeof(unsigned char) )
203         return( 0 );
204 
205     *olen = sizeof(unsigned char);
206 
207     return( 0 );
208 }
209 #endif
210 
211 #if defined(MBEDTLS_TIMING_C)
mbedtls_hardclock_poll(void * data,unsigned char * output,size_t len,size_t * olen)212 int mbedtls_hardclock_poll( void *data,
213                     unsigned char *output, size_t len, size_t *olen )
214 {
215     unsigned long timer = mbedtls_timing_hardclock();
216     ((void) data);
217     *olen = 0;
218 
219     if( len < sizeof(unsigned long) )
220         return( 0 );
221 
222     memcpy( output, &timer, sizeof(unsigned long) );
223     *olen = sizeof(unsigned long);
224 
225     return( 0 );
226 }
227 #endif /* MBEDTLS_TIMING_C */
228 
229 #if defined(MBEDTLS_HAVEGE_C)
mbedtls_havege_poll(void * data,unsigned char * output,size_t len,size_t * olen)230 int mbedtls_havege_poll( void *data,
231                  unsigned char *output, size_t len, size_t *olen )
232 {
233     mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
234     *olen = 0;
235 
236     if( mbedtls_havege_random( hs, output, len ) != 0 )
237         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
238 
239     *olen = len;
240 
241     return( 0 );
242 }
243 #endif /* MBEDTLS_HAVEGE_C */
244 
245 #if defined(MBEDTLS_ENTROPY_NV_SEED)
mbedtls_nv_seed_poll(void * data,unsigned char * output,size_t len,size_t * olen)246 int mbedtls_nv_seed_poll( void *data,
247                           unsigned char *output, size_t len, size_t *olen )
248 {
249     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
250     size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
251     ((void) data);
252 
253     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
254 
255     if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
256       return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
257 
258     if( len < use_len )
259       use_len = len;
260 
261     memcpy( output, buf, use_len );
262     *olen = use_len;
263 
264     return( 0 );
265 }
266 #endif /* MBEDTLS_ENTROPY_NV_SEED */
267 
268 #endif /* MBEDTLS_ENTROPY_C */
269