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(__linux__)
23 /* Ensure that syscall() is available even when compiling with -std=c99 */
24 #define _GNU_SOURCE
25 #endif
26 
27 #if !defined(MBEDTLS_CONFIG_FILE)
28 #include "mbedtls/config.h"
29 #else
30 #include MBEDTLS_CONFIG_FILE
31 #endif
32 
33 #include <string.h>
34 
35 #if defined(MBEDTLS_ENTROPY_C)
36 
37 #include "mbedtls/entropy.h"
38 #include "mbedtls/entropy_poll.h"
39 
40 #if defined(MBEDTLS_TIMING_C)
41 #include "mbedtls/timing.h"
42 #endif
43 #if defined(MBEDTLS_HAVEGE_C)
44 #include "mbedtls/havege.h"
45 #endif
46 #if defined(MBEDTLS_ENTROPY_NV_SEED)
47 #include "mbedtls/platform.h"
48 #endif
49 
50 #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
51 
52 #if !defined(unix) && !defined(__unix__) && !defined(__unix) && \
53     !defined(__APPLE__) && !defined(_WIN32) && !defined(__QNXNTO__) && !defined(__CSKY__) && \
54     !defined(__HAIKU__)
55 #error "Platform entropy sources only work on Unix and Windows, see MBEDTLS_NO_PLATFORM_ENTROPY in config.h"
56 #endif
57 
58 #if defined(_WIN32) && !defined(EFIX64) && !defined(EFI32)
59 
60 #if !defined(_WIN32_WINNT)
61 #define _WIN32_WINNT 0x0400
62 #endif
63 #include <windows.h>
64 #include <wincrypt.h>
65 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)66 int mbedtls_platform_entropy_poll( void *data, unsigned char *output, size_t len,
67                            size_t *olen )
68 {
69     HCRYPTPROV provider;
70     ((void) data);
71     *olen = 0;
72 
73     if( CryptAcquireContext( &provider, NULL, NULL,
74                               PROV_RSA_FULL, CRYPT_VERIFYCONTEXT ) == FALSE )
75     {
76         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
77     }
78 
79     if( CryptGenRandom( provider, (DWORD) len, output ) == FALSE )
80     {
81         CryptReleaseContext( provider, 0 );
82         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
83     }
84 
85     CryptReleaseContext( provider, 0 );
86     *olen = len;
87 
88     return( 0 );
89 }
90 #else /* _WIN32 && !EFIX64 && !EFI32 */
91 
92 /*
93  * Test for Linux getrandom() support.
94  * Since there is no wrapper in the libc yet, use the generic syscall wrapper
95  * available in GNU libc and compatible libc's (eg uClibc).
96  */
97 #if defined(__linux__) && defined(__GLIBC__)
98 #include <unistd.h>
99 #include <sys/syscall.h>
100 #if defined(SYS_getrandom)
101 #define HAVE_GETRANDOM
102 #include <errno.h>
103 
getrandom_wrapper(void * buf,size_t buflen,unsigned int flags)104 static int getrandom_wrapper( void *buf, size_t buflen, unsigned int flags )
105 {
106     /* MemSan cannot understand that the syscall writes to the buffer */
107 #if defined(__has_feature)
108 #if __has_feature(memory_sanitizer)
109     memset( buf, 0, buflen );
110 #endif
111 #endif
112     return( syscall( SYS_getrandom, buf, buflen, flags ) );
113 }
114 #endif /* SYS_getrandom */
115 #endif /* __linux__ */
116 
117 #include <stdio.h>
118 
mbedtls_platform_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)119 int mbedtls_platform_entropy_poll( void *data,
120                            unsigned char *output, size_t len, size_t *olen )
121 {
122     FILE *file;
123     size_t read_len;
124     int ret;
125     ((void) data);
126 
127 #if defined(HAVE_GETRANDOM)
128     ret = getrandom_wrapper( output, len, 0 );
129     if( ret >= 0 )
130     {
131         *olen = ret;
132         return( 0 );
133     }
134     else if( errno != ENOSYS )
135         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
136     /* Fall through if the system call isn't known. */
137 #else
138     ((void) ret);
139 #endif /* HAVE_GETRANDOM */
140 
141     *olen = 0;
142 
143 #if !defined(__CSKY__)
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 #else
157     ((void) file);
158     extern void * memcpy( void *, const void *, size_t );
159 	//FIXME: fack random
160 	memcpy(output, (void *)&read_len, len);
161 #endif
162     *olen = len;
163 
164     return( 0 );
165 }
166 #endif /* _WIN32 && !EFIX64 && !EFI32 */
167 #endif /* !MBEDTLS_NO_PLATFORM_ENTROPY */
168 
169 #if defined(MBEDTLS_TEST_NULL_ENTROPY)
mbedtls_null_entropy_poll(void * data,unsigned char * output,size_t len,size_t * olen)170 int mbedtls_null_entropy_poll( void *data,
171                     unsigned char *output, size_t len, size_t *olen )
172 {
173     ((void) data);
174     ((void) output);
175     *olen = 0;
176 
177     if( len < sizeof(unsigned char) )
178         return( 0 );
179 
180     *olen = sizeof(unsigned char);
181 
182     return( 0 );
183 }
184 #endif
185 
186 #if defined(MBEDTLS_TIMING_C)
mbedtls_hardclock_poll(void * data,unsigned char * output,size_t len,size_t * olen)187 int mbedtls_hardclock_poll( void *data,
188                     unsigned char *output, size_t len, size_t *olen )
189 {
190     unsigned long timer = mbedtls_timing_hardclock();
191     ((void) data);
192     *olen = 0;
193 
194     if( len < sizeof(unsigned long) )
195         return( 0 );
196 
197     memcpy( output, &timer, sizeof(unsigned long) );
198     *olen = sizeof(unsigned long);
199 
200     return( 0 );
201 }
202 #endif /* MBEDTLS_TIMING_C */
203 
204 #if defined(MBEDTLS_HAVEGE_C)
mbedtls_havege_poll(void * data,unsigned char * output,size_t len,size_t * olen)205 int mbedtls_havege_poll( void *data,
206                  unsigned char *output, size_t len, size_t *olen )
207 {
208     mbedtls_havege_state *hs = (mbedtls_havege_state *) data;
209     *olen = 0;
210 
211     if( mbedtls_havege_random( hs, output, len ) != 0 )
212         return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
213 
214     *olen = len;
215 
216     return( 0 );
217 }
218 #endif /* MBEDTLS_HAVEGE_C */
219 
220 #if defined(MBEDTLS_ENTROPY_NV_SEED)
mbedtls_nv_seed_poll(void * data,unsigned char * output,size_t len,size_t * olen)221 int mbedtls_nv_seed_poll( void *data,
222                           unsigned char *output, size_t len, size_t *olen )
223 {
224     unsigned char buf[MBEDTLS_ENTROPY_BLOCK_SIZE];
225     size_t use_len = MBEDTLS_ENTROPY_BLOCK_SIZE;
226     ((void) data);
227 
228     memset( buf, 0, MBEDTLS_ENTROPY_BLOCK_SIZE );
229 
230     if( mbedtls_nv_seed_read( buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) < 0 )
231       return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
232 
233     if( len < use_len )
234       use_len = len;
235 
236     memcpy( output, buf, use_len );
237     *olen = use_len;
238 
239     return( 0 );
240 }
241 #endif /* MBEDTLS_ENTROPY_NV_SEED */
242 
243 #endif /* MBEDTLS_ENTROPY_C */
244