1 | /* wc_port.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | #ifndef WOLF_CRYPT_PORT_H
|
---|
25 | #define WOLF_CRYPT_PORT_H
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
28 | #include <wolfssl/wolfcrypt/visibility.h>
|
---|
29 |
|
---|
30 | #ifdef __cplusplus
|
---|
31 | extern "C" {
|
---|
32 | #endif
|
---|
33 |
|
---|
34 |
|
---|
35 | #ifdef USE_WINDOWS_API
|
---|
36 | #ifdef WOLFSSL_GAME_BUILD
|
---|
37 | #include "system/xtl.h"
|
---|
38 | #else
|
---|
39 | #ifndef WIN32_LEAN_AND_MEAN
|
---|
40 | #define WIN32_LEAN_AND_MEAN
|
---|
41 | #endif
|
---|
42 | #ifndef WOLFSSL_SGX
|
---|
43 | #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
|
---|
44 | /* On WinCE winsock2.h must be included before windows.h */
|
---|
45 | #include <winsock2.h>
|
---|
46 | #endif
|
---|
47 | #include <windows.h>
|
---|
48 | #endif /* WOLFSSL_SGX */
|
---|
49 | #endif
|
---|
50 | #elif defined(THREADX)
|
---|
51 | #ifndef SINGLE_THREADED
|
---|
52 | #ifdef NEED_THREADX_TYPES
|
---|
53 | #include <types.h>
|
---|
54 | #endif
|
---|
55 | #include <tx_api.h>
|
---|
56 | #endif
|
---|
57 | #elif defined(MICRIUM)
|
---|
58 | /* do nothing, just don't pick Unix */
|
---|
59 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
|
---|
60 | /* do nothing */
|
---|
61 | #elif defined(EBSNET)
|
---|
62 | /* do nothing */
|
---|
63 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
64 | /* do nothing */
|
---|
65 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
66 | #include "fsl_os_abstraction.h"
|
---|
67 | #elif defined(WOLFSSL_uITRON4)
|
---|
68 | #include "stddef.h"
|
---|
69 | #include "kernel.h"
|
---|
70 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
71 | #include "tk/tkernel.h"
|
---|
72 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
73 | #if defined(WOLFSSL_MDK5)
|
---|
74 | #include "cmsis_os.h"
|
---|
75 | #else
|
---|
76 | #include <rtl.h>
|
---|
77 | #endif
|
---|
78 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
79 | #include "cmsis_os.h"
|
---|
80 | #elif defined(WOLFSSL_TIRTOS)
|
---|
81 | #include <ti/sysbios/BIOS.h>
|
---|
82 | #include <ti/sysbios/knl/Semaphore.h>
|
---|
83 | #elif defined(WOLFSSL_FROSTED)
|
---|
84 | #include <semaphore.h>
|
---|
85 | #elif defined(INTIME_RTOS)
|
---|
86 | #include <rt.h>
|
---|
87 | #include <io.h>
|
---|
88 | #else
|
---|
89 | #ifndef SINGLE_THREADED
|
---|
90 | #define WOLFSSL_PTHREADS
|
---|
91 | #include <pthread.h>
|
---|
92 | #endif
|
---|
93 | #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
|
---|
94 | #include <unistd.h> /* for close of BIO */
|
---|
95 | #endif
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /* For FIPS keep the function names the same */
|
---|
99 | #ifdef HAVE_FIPS
|
---|
100 | #define wc_InitMutex InitMutex
|
---|
101 | #define wc_FreeMutex FreeMutex
|
---|
102 | #define wc_LockMutex LockMutex
|
---|
103 | #define wc_UnLockMutex UnLockMutex
|
---|
104 | #endif /* HAVE_FIPS */
|
---|
105 |
|
---|
106 | #ifdef SINGLE_THREADED
|
---|
107 | typedef int wolfSSL_Mutex;
|
---|
108 | #else /* MULTI_THREADED */
|
---|
109 | /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
|
---|
110 | #if defined(FREERTOS)
|
---|
111 | typedef xSemaphoreHandle wolfSSL_Mutex;
|
---|
112 | #elif defined(FREERTOS_TCP)
|
---|
113 | #include "FreeRTOS.h"
|
---|
114 | #include "semphr.h"
|
---|
115 | typedef SemaphoreHandle_t wolfSSL_Mutex;
|
---|
116 | #elif defined(WOLFSSL_SAFERTOS)
|
---|
117 | typedef struct wolfSSL_Mutex {
|
---|
118 | signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
|
---|
119 | xSemaphoreHandle mutex;
|
---|
120 | } wolfSSL_Mutex;
|
---|
121 | #elif defined(USE_WINDOWS_API)
|
---|
122 | typedef CRITICAL_SECTION wolfSSL_Mutex;
|
---|
123 | #elif defined(WOLFSSL_PTHREADS)
|
---|
124 | typedef pthread_mutex_t wolfSSL_Mutex;
|
---|
125 | #elif defined(THREADX)
|
---|
126 | typedef TX_MUTEX wolfSSL_Mutex;
|
---|
127 | #elif defined(MICRIUM)
|
---|
128 | typedef OS_MUTEX wolfSSL_Mutex;
|
---|
129 | #elif defined(EBSNET)
|
---|
130 | typedef RTP_MUTEX wolfSSL_Mutex;
|
---|
131 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
132 | typedef MUTEX_STRUCT wolfSSL_Mutex;
|
---|
133 | #elif defined(FREESCALE_FREE_RTOS)
|
---|
134 | typedef mutex_t wolfSSL_Mutex;
|
---|
135 | #elif defined(WOLFSSL_uITRON4)
|
---|
136 | typedef struct wolfSSL_Mutex {
|
---|
137 | T_CSEM sem ;
|
---|
138 | ID id ;
|
---|
139 | } wolfSSL_Mutex;
|
---|
140 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
141 | typedef struct wolfSSL_Mutex {
|
---|
142 | T_CSEM sem ;
|
---|
143 | ID id ;
|
---|
144 | } wolfSSL_Mutex;
|
---|
145 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
146 | #if defined(WOLFSSL_CMSIS_RTOS)
|
---|
147 | typedef osMutexId wolfSSL_Mutex;
|
---|
148 | #else
|
---|
149 | typedef OS_MUT wolfSSL_Mutex;
|
---|
150 | #endif
|
---|
151 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
152 | typedef osMutexId wolfSSL_Mutex;
|
---|
153 | #elif defined(WOLFSSL_TIRTOS)
|
---|
154 | typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
|
---|
155 | #elif defined(WOLFSSL_FROSTED)
|
---|
156 | typedef mutex_t * wolfSSL_Mutex;
|
---|
157 | #elif defined(INTIME_RTOS)
|
---|
158 | typedef RTHANDLE wolfSSL_Mutex;
|
---|
159 | #else
|
---|
160 | #error Need a mutex type in multithreaded mode
|
---|
161 | #endif /* USE_WINDOWS_API */
|
---|
162 | #endif /* SINGLE_THREADED */
|
---|
163 |
|
---|
164 | /* Enable crypt HW mutex for Freescale MMCAU or PIC32MZ */
|
---|
165 | #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ)
|
---|
166 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
167 | #define WOLFSSL_CRYPT_HW_MUTEX 1
|
---|
168 | #endif
|
---|
169 | #endif /* FREESCALE_MMCAU */
|
---|
170 |
|
---|
171 | #ifndef WOLFSSL_CRYPT_HW_MUTEX
|
---|
172 | #define WOLFSSL_CRYPT_HW_MUTEX 0
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
176 | /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
|
---|
177 | however it's recommended to call this directly on Hw init to avoid possible
|
---|
178 | race condition where two calls to wolfSSL_CryptHwMutexLock are made at
|
---|
179 | the same time. */
|
---|
180 | int wolfSSL_CryptHwMutexInit(void);
|
---|
181 | int wolfSSL_CryptHwMutexLock(void);
|
---|
182 | int wolfSSL_CryptHwMutexUnLock(void);
|
---|
183 | #else
|
---|
184 | /* Define stubs, since HW mutex is disabled */
|
---|
185 | #define wolfSSL_CryptHwMutexInit() 0 /* Success */
|
---|
186 | #define wolfSSL_CryptHwMutexLock() 0 /* Success */
|
---|
187 | #define wolfSSL_CryptHwMutexUnLock() 0 /* Success */
|
---|
188 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */
|
---|
189 |
|
---|
190 | /* Mutex functions */
|
---|
191 | WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*);
|
---|
192 | WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
|
---|
193 | WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*);
|
---|
194 | WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);
|
---|
195 | WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*);
|
---|
196 |
|
---|
197 | /* main crypto initialization function */
|
---|
198 | WOLFSSL_API int wolfCrypt_Init(void);
|
---|
199 | WOLFSSL_API int wolfCrypt_Cleanup(void);
|
---|
200 |
|
---|
201 | /* filesystem abstraction layer, used by ssl.c */
|
---|
202 | #ifndef NO_FILESYSTEM
|
---|
203 |
|
---|
204 | #if defined(EBSNET)
|
---|
205 | #include "vfapi.h"
|
---|
206 | #include "vfile.h"
|
---|
207 |
|
---|
208 | #define XFILE int
|
---|
209 | #define XFOPEN(NAME, MODE) vf_open((const char *)NAME, VO_RDONLY, 0);
|
---|
210 | #define XFSEEK vf_lseek
|
---|
211 | #define XFTELL vf_tell
|
---|
212 | #define XREWIND vf_rewind
|
---|
213 | #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
|
---|
214 | #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
|
---|
215 | #define XFCLOSE vf_close
|
---|
216 | #define XSEEK_END VSEEK_END
|
---|
217 | #define XBADFILE -1
|
---|
218 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
219 | #elif defined(LSR_FS)
|
---|
220 | #include <fs.h>
|
---|
221 | #define XFILE struct fs_file*
|
---|
222 | #define XFOPEN(NAME, MODE) fs_open((char*)NAME);
|
---|
223 | #define XFSEEK(F, O, W) (void)F
|
---|
224 | #define XFTELL(F) (F)->len
|
---|
225 | #define XREWIND(F) (void)F
|
---|
226 | #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
|
---|
227 | #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
|
---|
228 | #define XFCLOSE fs_close
|
---|
229 | #define XSEEK_END 0
|
---|
230 | #define XBADFILE NULL
|
---|
231 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
232 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
233 | #define XFILE MQX_FILE_PTR
|
---|
234 | #define XFOPEN fopen
|
---|
235 | #define XFSEEK fseek
|
---|
236 | #define XFTELL ftell
|
---|
237 | #define XREWIND(F) fseek(F, 0, IO_SEEK_SET)
|
---|
238 | #define XFREAD fread
|
---|
239 | #define XFWRITE fwrite
|
---|
240 | #define XFCLOSE fclose
|
---|
241 | #define XSEEK_END IO_SEEK_END
|
---|
242 | #define XBADFILE NULL
|
---|
243 | #define XFGETS fgets
|
---|
244 | #elif defined(MICRIUM)
|
---|
245 | #include <fs_api.h>
|
---|
246 | #define XFILE FS_FILE*
|
---|
247 | #define XFOPEN fs_fopen
|
---|
248 | #define XFSEEK fs_fseek
|
---|
249 | #define XFTELL fs_ftell
|
---|
250 | #define XREWIND fs_rewind
|
---|
251 | #define XFREAD fs_fread
|
---|
252 | #define XFWRITE fs_fwrite
|
---|
253 | #define XFCLOSE fs_fclose
|
---|
254 | #define XSEEK_END FS_SEEK_END
|
---|
255 | #define XBADFILE NULL
|
---|
256 | #define XFGETS(b,s,f) -2 /* Not ported yet */
|
---|
257 | #else
|
---|
258 | /* stdio, default case */
|
---|
259 | #include <stdio.h>
|
---|
260 | #define XFILE FILE*
|
---|
261 | #if defined(WOLFSSL_MDK_ARM)
|
---|
262 | extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
|
---|
263 | #define XFOPEN wolfSSL_fopen
|
---|
264 | #else
|
---|
265 | #define XFOPEN fopen
|
---|
266 | #endif
|
---|
267 | #define XFSEEK fseek
|
---|
268 | #define XFTELL ftell
|
---|
269 | #define XREWIND rewind
|
---|
270 | #define XFREAD fread
|
---|
271 | #define XFWRITE fwrite
|
---|
272 | #define XFCLOSE fclose
|
---|
273 | #define XSEEK_END SEEK_END
|
---|
274 | #define XBADFILE NULL
|
---|
275 | #define XFGETS fgets
|
---|
276 |
|
---|
277 | #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)
|
---|
278 | #include <dirent.h>
|
---|
279 | #include <unistd.h>
|
---|
280 | #include <sys/stat.h>
|
---|
281 | #endif
|
---|
282 | #endif
|
---|
283 |
|
---|
284 | #ifndef MAX_FILENAME_SZ
|
---|
285 | #define MAX_FILENAME_SZ 256 /* max file name length */
|
---|
286 | #endif
|
---|
287 | #ifndef MAX_PATH
|
---|
288 | #define MAX_PATH 256
|
---|
289 | #endif
|
---|
290 |
|
---|
291 | #if !defined(NO_WOLFSSL_DIR)
|
---|
292 | typedef struct ReadDirCtx {
|
---|
293 | #ifdef USE_WINDOWS_API
|
---|
294 | WIN32_FIND_DATAA FindFileData;
|
---|
295 | HANDLE hFind;
|
---|
296 | #else
|
---|
297 | struct dirent* entry;
|
---|
298 | DIR* dir;
|
---|
299 | struct stat s;
|
---|
300 | #endif
|
---|
301 | char name[MAX_FILENAME_SZ];
|
---|
302 | } ReadDirCtx;
|
---|
303 |
|
---|
304 | WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
|
---|
305 | WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
|
---|
306 | WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
|
---|
307 | #endif /* !NO_WOLFSSL_DIR */
|
---|
308 |
|
---|
309 | #endif /* !NO_FILESYSTEM */
|
---|
310 |
|
---|
311 | #ifdef USE_WOLF_STRTOK
|
---|
312 | WOLFSSL_LOCAL char* wc_strtok(char *str, const char *delim, char **nextp);
|
---|
313 | #endif
|
---|
314 |
|
---|
315 | /* Windows API defines its own min() macro. */
|
---|
316 | #if defined(USE_WINDOWS_API)
|
---|
317 | #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
318 | #define WOLFSSL_HAVE_MIN
|
---|
319 | #endif /* min */
|
---|
320 | #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
|
---|
321 | #define WOLFSSL_HAVE_MAX
|
---|
322 | #endif /* max */
|
---|
323 | #endif /* USE_WINDOWS_API */
|
---|
324 |
|
---|
325 |
|
---|
326 | #ifdef __cplusplus
|
---|
327 | } /* extern "C" */
|
---|
328 | #endif
|
---|
329 |
|
---|
330 | #endif /* WOLF_CRYPT_PORT_H */
|
---|
331 |
|
---|