/src/wolfssl-disable-fastmath/wolfcrypt/src/wc_port.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* port.c |
2 | | * |
3 | | * Copyright (C) 2006-2022 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 | | #ifdef HAVE_CONFIG_H |
24 | | #include <config.h> |
25 | | #endif |
26 | | |
27 | | #include <wolfssl/wolfcrypt/settings.h> |
28 | | #include <wolfssl/wolfcrypt/types.h> |
29 | | #include <wolfssl/wolfcrypt/error-crypt.h> |
30 | | #include <wolfssl/wolfcrypt/logging.h> |
31 | | #include <wolfssl/wolfcrypt/wc_port.h> |
32 | | #ifdef HAVE_ECC |
33 | | #include <wolfssl/wolfcrypt/ecc.h> |
34 | | #endif |
35 | | #ifdef WOLFSSL_ASYNC_CRYPT |
36 | | #include <wolfssl/wolfcrypt/async.h> |
37 | | #endif |
38 | | |
39 | | /* IPP header files for library initialization */ |
40 | | #ifdef HAVE_FAST_RSA |
41 | | #include <ipp.h> |
42 | | #include <ippcp.h> |
43 | | #endif |
44 | | |
45 | | #ifdef FREESCALE_LTC_TFM |
46 | | #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h> |
47 | | #endif |
48 | | |
49 | | #ifdef WOLFSSL_PSOC6_CRYPTO |
50 | | #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h> |
51 | | #endif |
52 | | |
53 | | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ |
54 | | defined(WOLFSSL_ATECC608A) |
55 | | #include <wolfssl/wolfcrypt/port/atmel/atmel.h> |
56 | | #endif |
57 | | #if defined(WOLFSSL_RENESAS_TSIP) |
58 | | #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h> |
59 | | #endif |
60 | | #if defined(WOLFSSL_RENESAS_SCE) |
61 | | #include <wolfssl/wolfcrypt/port/Renesas/renesas-sce-crypt.h> |
62 | | #endif |
63 | | #if defined(WOLFSSL_STSAFEA100) |
64 | | #include <wolfssl/wolfcrypt/port/st/stsafe.h> |
65 | | #endif |
66 | | |
67 | | #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) \ |
68 | | && !defined(WOLFCRYPT_ONLY) |
69 | | #include <wolfssl/openssl/evp.h> |
70 | | #endif |
71 | | |
72 | | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) |
73 | | #include <wolfssl/wolfcrypt/memory.h> |
74 | | #include <wolfssl/wolfcrypt/mem_track.h> |
75 | | #endif |
76 | | |
77 | | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
78 | | defined(WOLFSSL_IMX6UL_CAAM) || defined(WOLFSSL_IMX6_CAAM_BLOB) || \ |
79 | | defined(WOLFSSL_SECO_CAAM) |
80 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
81 | | #endif |
82 | | #if defined(WOLFSSL_DEVCRYPTO) |
83 | | #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h> |
84 | | #endif |
85 | | #ifdef WOLFSSL_IMXRT_DCP |
86 | | #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h> |
87 | | #endif |
88 | | |
89 | | #ifdef WOLF_CRYPTO_CB |
90 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
91 | | #endif |
92 | | |
93 | | #ifdef HAVE_INTEL_QA_SYNC |
94 | | #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h> |
95 | | #endif |
96 | | |
97 | | #ifdef HAVE_CAVIUM_OCTEON_SYNC |
98 | | #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h> |
99 | | #endif |
100 | | |
101 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT) |
102 | | #include <wolfssl/wolfcrypt/port/nxp/se050_port.h> |
103 | | #endif |
104 | | |
105 | | #ifdef WOLFSSL_SCE |
106 | | #include "hal_data.h" |
107 | | #endif |
108 | | |
109 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
110 | | #include "rpcmem.h" |
111 | | #endif |
112 | | |
113 | | #ifdef _MSC_VER |
114 | | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
115 | | #pragma warning(disable: 4996) |
116 | | #endif |
117 | | |
118 | | #if defined(WOLFSSL_HAVE_PSA) |
119 | | #include <wolfssl/wolfcrypt/port/psa/psa.h> |
120 | | #endif |
121 | | |
122 | | |
123 | | /* prevent multiple mutex initializations */ |
124 | | static volatile int initRefCount = 0; |
125 | | |
126 | | /* Used to initialize state for wolfcrypt |
127 | | return 0 on success |
128 | | */ |
129 | | WOLFSSL_ABI |
130 | | int wolfCrypt_Init(void) |
131 | 7.60k | { |
132 | 7.60k | int ret = 0; |
133 | 7.60k | if (initRefCount == 0) { |
134 | 7.60k | WOLFSSL_ENTER("wolfCrypt_Init"); |
135 | | |
136 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
137 | | /* Initialize the mutex for access to the list of memory locations that |
138 | | * must be freed. */ |
139 | | wc_MemZero_Init(); |
140 | | #endif |
141 | | |
142 | | #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST |
143 | | { |
144 | | word32 rngMallocFail; |
145 | | time_t seed = time(NULL); |
146 | | srand((word32)seed); |
147 | | rngMallocFail = rand() % 2000; /* max 2000 */ |
148 | | fprintf(stderr, "\n--- RNG MALLOC FAIL AT %u ---\n", rngMallocFail); |
149 | | wolfSSL_SetMemFailCount(rngMallocFail); |
150 | | } |
151 | | #endif |
152 | | |
153 | 7.60k | #ifdef WOLF_CRYPTO_CB |
154 | 7.60k | wc_CryptoCb_Init(); |
155 | 7.60k | #endif |
156 | | |
157 | | #ifdef WOLFSSL_ASYNC_CRYPT |
158 | | ret = wolfAsync_HardwareStart(); |
159 | | if (ret != 0) { |
160 | | WOLFSSL_MSG("Async hardware start failed"); |
161 | | /* don't return failure, allow operation to continue */ |
162 | | } |
163 | | #endif |
164 | | |
165 | | #if defined(WOLFSSL_RENESAS_TSIP_CRYPT) |
166 | | ret = tsip_Open( ); |
167 | | if( ret != TSIP_SUCCESS ) { |
168 | | WOLFSSL_MSG("RENESAS TSIP Open failed"); |
169 | | /* not return 1 since WOLFSSL_SUCCESS=1*/ |
170 | | ret = -1;/* FATAL ERROR */ |
171 | | return ret; |
172 | | } |
173 | | #endif |
174 | | |
175 | | #if defined(WOLFSSL_RENESAS_SCEPROTECT) |
176 | | ret = wc_sce_Open( ); |
177 | | if( ret != FSP_SUCCESS ) { |
178 | | WOLFSSL_MSG("RENESAS SCE Open failed"); |
179 | | /* not return 1 since WOLFSSL_SUCCESS=1*/ |
180 | | ret = -1;/* FATAL ERROR */ |
181 | | return ret; |
182 | | } |
183 | | #endif |
184 | | |
185 | | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
186 | | ret = InitMemoryTracker(); |
187 | | if (ret != 0) { |
188 | | WOLFSSL_MSG("InitMemoryTracker failed"); |
189 | | return ret; |
190 | | } |
191 | | #endif |
192 | | |
193 | | #if defined(WOLFSSL_LINUXKM_SIMD_X86) |
194 | | ret = allocate_wolfcrypt_linuxkm_fpu_states(); |
195 | | if (ret != 0) { |
196 | | WOLFSSL_MSG("allocate_wolfcrypt_linuxkm_fpu_states failed"); |
197 | | return ret; |
198 | | } |
199 | | #endif |
200 | | |
201 | | #if WOLFSSL_CRYPT_HW_MUTEX |
202 | | /* If crypto hardware mutex protection is enabled, then initialize it */ |
203 | | ret = wolfSSL_CryptHwMutexInit(); |
204 | | if (ret != 0) { |
205 | | WOLFSSL_MSG("Hw crypt mutex init failed"); |
206 | | return ret; |
207 | | } |
208 | | #endif |
209 | | |
210 | | /* if defined have fast RSA then initialize Intel IPP */ |
211 | | #ifdef HAVE_FAST_RSA |
212 | | WOLFSSL_MSG("Attempting to use optimized IPP Library"); |
213 | | if ((ret = ippInit()) != ippStsNoErr) { |
214 | | /* possible to get a CPU feature support status on optimized IPP |
215 | | library but still use default library and see competitive speeds */ |
216 | | WOLFSSL_MSG("Warning when trying to set up optimization"); |
217 | | WOLFSSL_MSG(ippGetStatusString(ret)); |
218 | | WOLFSSL_MSG("Using default fast IPP library"); |
219 | | ret = 0; |
220 | | (void)ret; /* suppress not read warning */ |
221 | | } |
222 | | #endif |
223 | | |
224 | | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC) |
225 | | ret = ksdk_port_init(); |
226 | | if (ret != 0) { |
227 | | WOLFSSL_MSG("KSDK port init failed"); |
228 | | return ret; |
229 | | } |
230 | | #endif |
231 | | |
232 | | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ |
233 | | defined(WOLFSSL_ATECC608A) |
234 | | ret = atmel_init(); |
235 | | if (ret != 0) { |
236 | | WOLFSSL_MSG("CryptoAuthLib init failed"); |
237 | | return ret; |
238 | | } |
239 | | #endif |
240 | | #if defined(WOLFSSL_CRYPTOCELL) |
241 | | /* enable and initialize the ARM CryptoCell 3xx runtime library */ |
242 | | ret = cc310_Init(); |
243 | | if (ret != 0) { |
244 | | WOLFSSL_MSG("CRYPTOCELL init failed"); |
245 | | return ret; |
246 | | } |
247 | | #endif |
248 | | #if defined(WOLFSSL_STSAFEA100) |
249 | | stsafe_interface_init(); |
250 | | #endif |
251 | | |
252 | | #if defined(WOLFSSL_PSOC6_CRYPTO) |
253 | | ret = psoc6_crypto_port_init(); |
254 | | if (ret != 0) { |
255 | | WOLFSSL_MSG("PSoC6 crypto engine init failed"); |
256 | | return ret; |
257 | | } |
258 | | #endif |
259 | | |
260 | | #ifdef WOLFSSL_SILABS_SE_ACCEL |
261 | | /* init handles if it is already initialized */ |
262 | | ret = sl_se_init(); |
263 | | #endif |
264 | | |
265 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT) |
266 | | ret = wc_se050_init(NULL); |
267 | | #endif |
268 | | |
269 | | #ifdef WOLFSSL_ARMASM |
270 | | WOLFSSL_MSG("Using ARM hardware acceleration"); |
271 | | #endif |
272 | | |
273 | | #ifdef WOLFSSL_AFALG |
274 | | WOLFSSL_MSG("Using AF_ALG for crypto acceleration"); |
275 | | #endif |
276 | | |
277 | | #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA) |
278 | | wolfSSL_EVP_init(); |
279 | | #endif |
280 | | |
281 | | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
282 | | if ((ret = wc_LoggingInit()) != 0) { |
283 | | WOLFSSL_MSG("Error creating logging mutex"); |
284 | | return ret; |
285 | | } |
286 | | #endif |
287 | | |
288 | | #if defined(WOLFSSL_HAVE_PSA) |
289 | | if ((ret = wc_psa_init()) != 0) |
290 | | return ret; |
291 | | #endif |
292 | | |
293 | 7.60k | #ifdef HAVE_ECC |
294 | | #ifdef FP_ECC |
295 | | wc_ecc_fp_init(); |
296 | | #endif |
297 | | #ifdef ECC_CACHE_CURVE |
298 | | if ((ret = wc_ecc_curve_cache_init()) != 0) { |
299 | | WOLFSSL_MSG("Error creating curve cache"); |
300 | | return ret; |
301 | | } |
302 | | #endif |
303 | 7.60k | #endif |
304 | | |
305 | | #ifdef WOLFSSL_SCE |
306 | | ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open( |
307 | | WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg); |
308 | | if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) { |
309 | | WOLFSSL_MSG("SCE already open"); |
310 | | ret = 0; |
311 | | } |
312 | | if (ret != SSP_SUCCESS) { |
313 | | WOLFSSL_MSG("Error opening SCE"); |
314 | | return -1; /* FATAL_ERROR */ |
315 | | } |
316 | | #endif |
317 | | |
318 | | #if defined(WOLFSSL_DEVCRYPTO) |
319 | | if ((ret = wc_DevCryptoInit()) != 0) { |
320 | | return ret; |
321 | | } |
322 | | #endif |
323 | | |
324 | | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
325 | | defined(WOLFSSL_IMX6UL_CAAM) || defined(WOLFSSL_IMX6_CAAM_BLOB) || \ |
326 | | defined(WOLFSSL_SECO_CAAM) |
327 | | if ((ret = wc_caamInit()) != 0) { |
328 | | return ret; |
329 | | } |
330 | | #endif |
331 | | |
332 | | #ifdef WOLFSSL_IMXRT_DCP |
333 | | if ((ret = wc_dcp_init()) != 0) { |
334 | | return ret; |
335 | | } |
336 | | #endif |
337 | | |
338 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
339 | | if ((ret = wolfSSL_InitHandle()) != 0) { |
340 | | return ret; |
341 | | } |
342 | | rpcmem_init(); |
343 | | #endif |
344 | 7.60k | } |
345 | 7.60k | initRefCount++; |
346 | | |
347 | 7.60k | return ret; |
348 | 7.60k | } |
349 | | |
350 | | #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE |
351 | | long wolfCrypt_heap_peakAllocs_checkpoint(void) { |
352 | | long ret = ourMemStats.peakAllocsTripOdometer; |
353 | | ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs - |
354 | | ourMemStats.totalDeallocs; |
355 | | return ret; |
356 | | } |
357 | | long wolfCrypt_heap_peakBytes_checkpoint(void) { |
358 | | long ret = ourMemStats.peakBytesTripOdometer; |
359 | | ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes; |
360 | | return ret; |
361 | | } |
362 | | #endif |
363 | | |
364 | | /* return success value is the same as wolfCrypt_Init */ |
365 | | WOLFSSL_ABI |
366 | | int wolfCrypt_Cleanup(void) |
367 | 7.60k | { |
368 | 7.60k | int ret = 0; |
369 | | |
370 | 7.60k | initRefCount--; |
371 | 7.60k | if (initRefCount < 0) |
372 | 0 | initRefCount = 0; |
373 | | |
374 | 7.60k | if (initRefCount == 0) { |
375 | 7.60k | WOLFSSL_ENTER("wolfCrypt_Cleanup"); |
376 | | |
377 | 7.60k | #ifdef HAVE_ECC |
378 | | #ifdef FP_ECC |
379 | | wc_ecc_fp_free(); |
380 | | #endif |
381 | | #ifdef ECC_CACHE_CURVE |
382 | | wc_ecc_curve_cache_free(); |
383 | | #endif |
384 | 7.60k | #endif /* HAVE_ECC */ |
385 | | |
386 | | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
387 | | ret = wc_LoggingCleanup(); |
388 | | #endif |
389 | | |
390 | | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
391 | | ShowMemoryTracker(); |
392 | | #endif |
393 | | |
394 | | #ifdef WOLFSSL_ASYNC_CRYPT |
395 | | wolfAsync_HardwareStop(); |
396 | | #endif |
397 | | |
398 | | #ifdef WOLFSSL_RENESAS_TSIP |
399 | | tsip_Close(); |
400 | | #endif |
401 | | |
402 | | #ifdef WOLFSSL_RENESAS_SCEPROTECT |
403 | | wc_sce_Close(); |
404 | | #endif |
405 | | |
406 | | #ifdef WOLFSSL_SCE |
407 | | WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl); |
408 | | #endif |
409 | | |
410 | | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
411 | | defined(WOLFSSL_IMX6_CAAM_BLOB) || \ |
412 | | defined(WOLFSSL_SECO_CAAM) |
413 | | wc_caamFree(); |
414 | | #endif |
415 | | #if defined(WOLFSSL_CRYPTOCELL) |
416 | | cc310_Free(); |
417 | | #endif |
418 | | #ifdef WOLFSSL_SILABS_SE_ACCEL |
419 | | ret = sl_se_deinit(); |
420 | | #endif |
421 | | #if defined(WOLFSSL_RENESAS_TSIP_CRYPT) |
422 | | tsip_Close(); |
423 | | #endif |
424 | | #if defined(WOLFSSL_DEVCRYPTO) |
425 | | wc_DevCryptoCleanup(); |
426 | | #endif |
427 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
428 | | rpcmem_deinit(); |
429 | | wolfSSL_CleanupHandle(); |
430 | | #endif |
431 | | #if defined(WOLFSSL_LINUXKM_SIMD_X86) |
432 | | free_wolfcrypt_linuxkm_fpu_states(); |
433 | | #endif |
434 | | |
435 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
436 | | /* Free the mutex for access to the list of memory locations that |
437 | | * must be freed. */ |
438 | | wc_MemZero_Free(); |
439 | | #endif |
440 | 7.60k | } |
441 | | |
442 | 7.60k | return ret; |
443 | 7.60k | } |
444 | | |
445 | | #ifndef NO_FILESYSTEM |
446 | | |
447 | | /* Helpful function to load file into allocated buffer */ |
448 | | int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen, |
449 | | void* heap) |
450 | 0 | { |
451 | 0 | int ret; |
452 | 0 | size_t fileSz; |
453 | 0 | XFILE f; |
454 | |
|
455 | 0 | if (fname == NULL || buf == NULL || bufLen == NULL) { |
456 | 0 | return BAD_FUNC_ARG; |
457 | 0 | } |
458 | | |
459 | | /* set defaults */ |
460 | 0 | *buf = NULL; |
461 | 0 | *bufLen = 0; |
462 | | |
463 | | /* open file (read-only binary) */ |
464 | 0 | f = XFOPEN(fname, "rb"); |
465 | 0 | if (!f) { |
466 | 0 | WOLFSSL_MSG("wc_LoadFile file load error"); |
467 | 0 | return BAD_PATH_ERROR; |
468 | 0 | } |
469 | | |
470 | 0 | if (XFSEEK(f, 0, XSEEK_END) != 0) { |
471 | 0 | WOLFSSL_MSG("wc_LoadFile file seek error"); |
472 | 0 | XFCLOSE(f); |
473 | 0 | return BAD_PATH_ERROR; |
474 | 0 | } |
475 | 0 | fileSz = XFTELL(f); |
476 | 0 | XREWIND(f); |
477 | 0 | if (fileSz > 0) { |
478 | 0 | *bufLen = fileSz; |
479 | 0 | *buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER); |
480 | 0 | if (*buf == NULL) { |
481 | 0 | WOLFSSL_MSG("wc_LoadFile memory error"); |
482 | 0 | ret = MEMORY_E; |
483 | 0 | } |
484 | 0 | else { |
485 | 0 | size_t readLen = XFREAD(*buf, 1, *bufLen, f); |
486 | | |
487 | | /* check response code */ |
488 | 0 | ret = (readLen == *bufLen) ? 0 : -1; |
489 | 0 | } |
490 | 0 | } |
491 | 0 | else { |
492 | 0 | ret = BUFFER_E; |
493 | 0 | } |
494 | 0 | XFCLOSE(f); |
495 | |
|
496 | 0 | (void)heap; |
497 | |
|
498 | 0 | return ret; |
499 | 0 | } |
500 | | |
501 | | #if !defined(NO_WOLFSSL_DIR) && \ |
502 | | !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) |
503 | | /* File Handling Helper */ |
504 | | /* returns 0 if file exists, WC_ISFILEEXIST_NOFILE if file doesn't exist */ |
505 | | int wc_FileExists(const char* fname) |
506 | 0 | { |
507 | 0 | struct ReadDirCtx ctx; |
508 | |
|
509 | 0 | XMEMSET(&ctx, 0, sizeof(ctx)); |
510 | |
|
511 | 0 | if (fname == NULL) |
512 | 0 | return 0; |
513 | | |
514 | 0 | if (XSTAT(fname, &ctx.s) != 0) { |
515 | 0 | WOLFSSL_MSG("stat on name failed"); |
516 | 0 | return BAD_PATH_ERROR; |
517 | 0 | } else { |
518 | | #if defined(USE_WINDOWS_API) |
519 | | if (XS_ISREG(ctx.s.st_mode)) { |
520 | | return 0; |
521 | | } |
522 | | #elif defined(WOLFSSL_ZEPHYR) |
523 | | if (XS_ISREG(ctx.s.type)) { |
524 | | return 0; |
525 | | } |
526 | | #elif defined(WOLFSSL_TELIT_M2MB) |
527 | | if (XS_ISREG(ctx.s.st_mode)) { |
528 | | return 0; |
529 | | } |
530 | | #else |
531 | 0 | if (XS_ISREG(ctx.s.st_mode)) { |
532 | 0 | return 0; |
533 | 0 | } |
534 | 0 | #endif |
535 | 0 | } |
536 | 0 | return WC_ISFILEEXIST_NOFILE; |
537 | 0 | } |
538 | | |
539 | | /* File Handling Helpers */ |
540 | | /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */ |
541 | | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name) |
542 | 0 | { |
543 | 0 | int ret = WC_READDIR_NOFILE; /* default to no files found */ |
544 | 0 | int pathLen = 0; |
545 | 0 | int dnameLen = 0; |
546 | |
|
547 | 0 | if (name) |
548 | 0 | *name = NULL; |
549 | |
|
550 | 0 | if (ctx == NULL || path == NULL) { |
551 | 0 | return BAD_FUNC_ARG; |
552 | 0 | } |
553 | | |
554 | 0 | XMEMSET(ctx, 0, sizeof(ReadDirCtx)); |
555 | 0 | pathLen = (int)XSTRLEN(path); |
556 | |
|
557 | | #ifdef USE_WINDOWS_API |
558 | | if (pathLen > MAX_FILENAME_SZ - 3) |
559 | | return BAD_PATH_ERROR; |
560 | | |
561 | | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3); |
562 | | XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen); |
563 | | |
564 | | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData); |
565 | | if (ctx->hFind == INVALID_HANDLE_VALUE) { |
566 | | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
567 | | return BAD_PATH_ERROR; |
568 | | } |
569 | | |
570 | | do { |
571 | | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
572 | | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName); |
573 | | |
574 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
575 | | return BAD_PATH_ERROR; |
576 | | } |
577 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
578 | | ctx->name[pathLen] = '\\'; |
579 | | XSTRNCPY(ctx->name + pathLen + 1, |
580 | | ctx->FindFileData.cFileName, |
581 | | MAX_FILENAME_SZ - pathLen - 1); |
582 | | if (name) |
583 | | *name = ctx->name; |
584 | | return 0; |
585 | | } |
586 | | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData)); |
587 | | |
588 | | #elif defined(INTIME_RTOS) |
589 | | if (pathLen > MAX_FILENAME_SZ - 3) |
590 | | return BAD_PATH_ERROR; |
591 | | |
592 | | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3); |
593 | | XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen); |
594 | | |
595 | | if (!IntimeFindFirst(ctx->name, &ctx->FindFileData)) { |
596 | | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
597 | | return BAD_PATH_ERROR; |
598 | | } |
599 | | |
600 | | do { |
601 | | dnameLen = (int)XSTRLEN(IntimeFilename(ctx)); |
602 | | |
603 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
604 | | return BAD_PATH_ERROR; |
605 | | } |
606 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
607 | | ctx->name[pathLen] = '\\'; |
608 | | XSTRNCPY(ctx->name + pathLen + 1, |
609 | | IntimeFilename(ctx), |
610 | | MAX_FILENAME_SZ - pathLen - 1); |
611 | | if (0 == wc_FileExists(ctx->name)) { |
612 | | if (name) |
613 | | *name = ctx->name; |
614 | | return 0; |
615 | | } |
616 | | } while (IntimeFindNext(&ctx->FindFileData)); |
617 | | |
618 | | #elif defined(WOLFSSL_ZEPHYR) |
619 | | if (fs_opendir(&ctx->dir, path) != 0) { |
620 | | WOLFSSL_MSG("opendir path verify locations failed"); |
621 | | return BAD_PATH_ERROR; |
622 | | } |
623 | | ctx->dirp = &ctx->dir; |
624 | | |
625 | | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) { |
626 | | dnameLen = (int)XSTRLEN(ctx->entry.name); |
627 | | |
628 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
629 | | ret = BAD_PATH_ERROR; |
630 | | break; |
631 | | } |
632 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
633 | | ctx->name[pathLen] = '/'; |
634 | | |
635 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
636 | | * of earlier check it is known that dnameLen is less than |
637 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
638 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1); |
639 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
640 | | if (name) |
641 | | *name = ctx->name; |
642 | | return 0; |
643 | | } |
644 | | } |
645 | | #elif defined(WOLFSSL_TELIT_M2MB) |
646 | | ctx->dir = m2mb_fs_opendir((const CHAR*)path); |
647 | | if (ctx->dir == NULL) { |
648 | | WOLFSSL_MSG("opendir path verify locations failed"); |
649 | | return BAD_PATH_ERROR; |
650 | | } |
651 | | |
652 | | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) { |
653 | | dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
654 | | |
655 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
656 | | ret = BAD_PATH_ERROR; |
657 | | break; |
658 | | } |
659 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
660 | | ctx->name[pathLen] = '/'; |
661 | | |
662 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
663 | | * of earlier check it is known that dnameLen is less than |
664 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
665 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
666 | | |
667 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
668 | | if (name) |
669 | | *name = ctx->name; |
670 | | return 0; |
671 | | } |
672 | | } |
673 | | #else |
674 | 0 | ctx->dir = opendir(path); |
675 | 0 | if (ctx->dir == NULL) { |
676 | 0 | WOLFSSL_MSG("opendir path verify locations failed"); |
677 | 0 | return BAD_PATH_ERROR; |
678 | 0 | } |
679 | | |
680 | 0 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
681 | 0 | dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
682 | |
|
683 | 0 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
684 | 0 | ret = BAD_PATH_ERROR; |
685 | 0 | break; |
686 | 0 | } |
687 | 0 | XSTRNCPY(ctx->name, path, pathLen + 1); |
688 | 0 | ctx->name[pathLen] = '/'; |
689 | | |
690 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
691 | | * of earlier check it is known that dnameLen is less than |
692 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
693 | 0 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
694 | 0 | if ((ret = wc_FileExists(ctx->name)) == 0) { |
695 | 0 | if (name) |
696 | 0 | *name = ctx->name; |
697 | 0 | return 0; |
698 | 0 | } |
699 | 0 | } |
700 | 0 | #endif |
701 | 0 | wc_ReadDirClose(ctx); |
702 | |
|
703 | 0 | return ret; |
704 | 0 | } |
705 | | |
706 | | /* returns 0 if file found, WC_READDIR_NOFILE if no more files */ |
707 | | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name) |
708 | 0 | { |
709 | 0 | int ret = WC_READDIR_NOFILE; /* default to no file found */ |
710 | 0 | int pathLen = 0; |
711 | 0 | int dnameLen = 0; |
712 | |
|
713 | 0 | if (name) |
714 | 0 | *name = NULL; |
715 | |
|
716 | 0 | if (ctx == NULL || path == NULL) { |
717 | 0 | return BAD_FUNC_ARG; |
718 | 0 | } |
719 | | |
720 | 0 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
721 | 0 | pathLen = (int)XSTRLEN(path); |
722 | |
|
723 | | #ifdef USE_WINDOWS_API |
724 | | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) { |
725 | | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
726 | | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName); |
727 | | |
728 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
729 | | return BAD_PATH_ERROR; |
730 | | } |
731 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
732 | | ctx->name[pathLen] = '\\'; |
733 | | XSTRNCPY(ctx->name + pathLen + 1, |
734 | | ctx->FindFileData.cFileName, |
735 | | MAX_FILENAME_SZ - pathLen - 1); |
736 | | if (name) |
737 | | *name = ctx->name; |
738 | | return 0; |
739 | | } |
740 | | } |
741 | | |
742 | | #elif defined(INTIME_RTOS) |
743 | | while (IntimeFindNext(&ctx->FindFileData)) { |
744 | | dnameLen = (int)XSTRLEN(IntimeFilename(ctx)); |
745 | | |
746 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
747 | | return BAD_PATH_ERROR; |
748 | | } |
749 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
750 | | ctx->name[pathLen] = '\\'; |
751 | | XSTRNCPY(ctx->name + pathLen + 1, |
752 | | IntimeFilename(ctx), |
753 | | MAX_FILENAME_SZ - pathLen - 1); |
754 | | if (0 == wc_FileExists(ctx->name)) { |
755 | | if (name) |
756 | | *name = ctx->name; |
757 | | return 0; |
758 | | } |
759 | | } |
760 | | |
761 | | #elif defined(WOLFSSL_ZEPHYR) |
762 | | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) { |
763 | | dnameLen = (int)XSTRLEN(ctx->entry.name); |
764 | | |
765 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
766 | | ret = BAD_PATH_ERROR; |
767 | | break; |
768 | | } |
769 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
770 | | ctx->name[pathLen] = '/'; |
771 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
772 | | * of earlier check it is known that dnameLen is less than |
773 | | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */ |
774 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1); |
775 | | |
776 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
777 | | if (name) |
778 | | *name = ctx->name; |
779 | | return 0; |
780 | | } |
781 | | } |
782 | | #elif defined(WOLFSSL_TELIT_M2MB) |
783 | | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) { |
784 | | dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
785 | | |
786 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
787 | | ret = BAD_PATH_ERROR; |
788 | | break; |
789 | | } |
790 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
791 | | ctx->name[pathLen] = '/'; |
792 | | |
793 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
794 | | * of earlier check it is known that dnameLen is less than |
795 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
796 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
797 | | |
798 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
799 | | if (name) |
800 | | *name = ctx->name; |
801 | | return 0; |
802 | | } |
803 | | } |
804 | | #else |
805 | 0 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
806 | 0 | dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
807 | |
|
808 | 0 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
809 | 0 | ret = BAD_PATH_ERROR; |
810 | 0 | break; |
811 | 0 | } |
812 | 0 | XSTRNCPY(ctx->name, path, pathLen + 1); |
813 | 0 | ctx->name[pathLen] = '/'; |
814 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
815 | | * of earlier check it is known that dnameLen is less than |
816 | | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */ |
817 | 0 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
818 | |
|
819 | 0 | if ((ret = wc_FileExists(ctx->name)) == 0) { |
820 | 0 | if (name) |
821 | 0 | *name = ctx->name; |
822 | 0 | return 0; |
823 | 0 | } |
824 | 0 | } |
825 | 0 | #endif |
826 | | |
827 | 0 | wc_ReadDirClose(ctx); |
828 | |
|
829 | 0 | return ret; |
830 | 0 | } |
831 | | |
832 | | void wc_ReadDirClose(ReadDirCtx* ctx) |
833 | 0 | { |
834 | 0 | if (ctx == NULL) { |
835 | 0 | return; |
836 | 0 | } |
837 | | |
838 | | #ifdef USE_WINDOWS_API |
839 | | if (ctx->hFind != INVALID_HANDLE_VALUE) { |
840 | | FindClose(ctx->hFind); |
841 | | ctx->hFind = INVALID_HANDLE_VALUE; |
842 | | } |
843 | | |
844 | | #elif defined(INTIME_RTOS) |
845 | | IntimeFindClose(&ctx->FindFileData); |
846 | | |
847 | | #elif defined(WOLFSSL_ZEPHYR) |
848 | | if (ctx->dirp) { |
849 | | fs_closedir(ctx->dirp); |
850 | | ctx->dirp = NULL; |
851 | | } |
852 | | #elif defined(WOLFSSL_TELIT_M2MB) |
853 | | if (ctx->dir) { |
854 | | m2mb_fs_closedir(ctx->dir); |
855 | | ctx->dir = NULL; |
856 | | } |
857 | | #else |
858 | 0 | if (ctx->dir) { |
859 | 0 | if (closedir(ctx->dir) < 0) |
860 | 0 | WOLFSSL_MSG("closedir() failed"); |
861 | 0 | ctx->dir = NULL; |
862 | 0 | } |
863 | 0 | #endif |
864 | 0 | } |
865 | | |
866 | | #endif /* !NO_WOLFSSL_DIR */ |
867 | | #endif /* !NO_FILESYSTEM */ |
868 | | |
869 | | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR) |
870 | | XFILE z_fs_open(const char* filename, const char* perm) |
871 | | { |
872 | | XFILE file; |
873 | | |
874 | | file = (XFILE)XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE); |
875 | | if (file != NULL) { |
876 | | if (fs_open(file, filename) != 0) { |
877 | | XFREE(file, NULL, DYNAMIC_TYPE_FILE); |
878 | | file = NULL; |
879 | | } |
880 | | } |
881 | | |
882 | | return file; |
883 | | } |
884 | | |
885 | | int z_fs_close(XFILE file) |
886 | | { |
887 | | int ret; |
888 | | |
889 | | if (file == NULL) |
890 | | return -1; |
891 | | ret = (fs_close(file) == 0) ? 0 : -1; |
892 | | |
893 | | XFREE(file, NULL, DYNAMIC_TYPE_FILE); |
894 | | |
895 | | return ret; |
896 | | } |
897 | | |
898 | | #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */ |
899 | | |
900 | | #if !defined(WOLFSSL_USER_MUTEX) |
901 | | wolfSSL_Mutex* wc_InitAndAllocMutex(void) |
902 | 0 | { |
903 | 0 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL, |
904 | 0 | DYNAMIC_TYPE_MUTEX); |
905 | 0 | if (m != NULL) { |
906 | 0 | if (wc_InitMutex(m) != 0) { |
907 | 0 | WOLFSSL_MSG("Init Mutex failed"); |
908 | 0 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX); |
909 | 0 | m = NULL; |
910 | 0 | } |
911 | 0 | } |
912 | 0 | else { |
913 | 0 | WOLFSSL_MSG("Memory error with Mutex allocation"); |
914 | 0 | } |
915 | |
|
916 | 0 | return m; |
917 | 0 | } |
918 | | #endif |
919 | | |
920 | | #ifdef USE_WOLF_STRTOK |
921 | | /* String token (delim) search. If str is null use nextp. */ |
922 | | char* wc_strtok(char *str, const char *delim, char **nextp) |
923 | 0 | { |
924 | 0 | char* ret; |
925 | 0 | int i, j; |
926 | | |
927 | | /* Use next if str is NULL */ |
928 | 0 | if (str == NULL && nextp) |
929 | 0 | str = *nextp; |
930 | | |
931 | | /* verify str input */ |
932 | 0 | if (str == NULL || *str == '\0') |
933 | 0 | return NULL; |
934 | | |
935 | | /* match on entire delim */ |
936 | 0 | for (i = 0; str[i]; i++) { |
937 | 0 | for (j = 0; delim[j]; j++) { |
938 | 0 | if (delim[j] == str[i]) |
939 | 0 | break; |
940 | 0 | } |
941 | 0 | if (!delim[j]) |
942 | 0 | break; |
943 | 0 | } |
944 | 0 | str += i; |
945 | | /* if end of string, not found so return NULL */ |
946 | 0 | if (*str == '\0') |
947 | 0 | return NULL; |
948 | | |
949 | 0 | ret = str; |
950 | | |
951 | | /* match on first delim */ |
952 | 0 | for (i = 0; str[i]; i++) { |
953 | 0 | for (j = 0; delim[j]; j++) { |
954 | 0 | if (delim[j] == str[i]) |
955 | 0 | break; |
956 | 0 | } |
957 | 0 | if (delim[j] == str[i]) |
958 | 0 | break; |
959 | 0 | } |
960 | 0 | str += i; |
961 | | |
962 | | /* null terminate found string */ |
963 | 0 | if (*str) |
964 | 0 | *str++ = '\0'; |
965 | | |
966 | | /* return pointer to next */ |
967 | 0 | if (nextp) |
968 | 0 | *nextp = str; |
969 | |
|
970 | 0 | return ret; |
971 | 0 | } |
972 | | #endif /* USE_WOLF_STRTOK */ |
973 | | |
974 | | #ifdef USE_WOLF_STRSEP |
975 | | char* wc_strsep(char **stringp, const char *delim) |
976 | 0 | { |
977 | 0 | char *s, *tok; |
978 | 0 | const char *spanp; |
979 | | |
980 | | /* null check */ |
981 | 0 | if (stringp == NULL || *stringp == NULL) |
982 | 0 | return NULL; |
983 | | |
984 | 0 | s = *stringp; |
985 | 0 | for (tok = s; *tok; ++tok) { |
986 | 0 | for (spanp = delim; *spanp; ++spanp) { |
987 | | /* found delimiter */ |
988 | 0 | if (*tok == *spanp) { |
989 | 0 | *tok = '\0'; /* replace delim with null term */ |
990 | 0 | *stringp = tok + 1; /* return past delim */ |
991 | 0 | return s; |
992 | 0 | } |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | 0 | *stringp = NULL; |
997 | 0 | return s; |
998 | 0 | } |
999 | | #endif /* USE_WOLF_STRSEP */ |
1000 | | |
1001 | | #ifdef USE_WOLF_STRLCPY |
1002 | | size_t wc_strlcpy(char *dst, const char *src, size_t dstSize) |
1003 | 0 | { |
1004 | 0 | size_t i; |
1005 | |
|
1006 | 0 | if (!dstSize) |
1007 | 0 | return 0; |
1008 | | |
1009 | | /* Always have to leave a space for NULL */ |
1010 | 0 | for (i = 0; i < (dstSize - 1) && *src != '\0'; i++) { |
1011 | 0 | *dst++ = *src++; |
1012 | 0 | } |
1013 | 0 | *dst = '\0'; |
1014 | |
|
1015 | 0 | return i; /* return length without NULL */ |
1016 | 0 | } |
1017 | | #endif /* USE_WOLF_STRLCPY */ |
1018 | | |
1019 | | #ifdef USE_WOLF_STRLCAT |
1020 | | size_t wc_strlcat(char *dst, const char *src, size_t dstSize) |
1021 | 0 | { |
1022 | 0 | size_t dstLen; |
1023 | |
|
1024 | 0 | if (!dstSize) |
1025 | 0 | return 0; |
1026 | | |
1027 | 0 | dstLen = XSTRLEN(dst); |
1028 | |
|
1029 | 0 | if (dstSize < dstLen) |
1030 | 0 | return dstLen + XSTRLEN(src); |
1031 | | |
1032 | 0 | return dstLen + wc_strlcpy(dst + dstLen, src, dstSize - dstLen); |
1033 | 0 | } |
1034 | | #endif /* USE_WOLF_STRLCAT */ |
1035 | | |
1036 | | #ifndef SINGLE_THREADED |
1037 | | /* TODO: use atomic operations instead of mutex */ |
1038 | | void wolfSSL_RefInit(wolfSSL_Ref* ref, int* err) |
1039 | 0 | { |
1040 | 0 | int ret = wc_InitMutex(&ref->mutex); |
1041 | 0 | if (ret != 0) { |
1042 | 0 | WOLFSSL_MSG("Failed to create mutex for reference counting!"); |
1043 | 0 | } |
1044 | 0 | ref->count = 1; |
1045 | |
|
1046 | 0 | *err = ret; |
1047 | 0 | } |
1048 | | |
1049 | | void wolfSSL_RefFree(wolfSSL_Ref* ref) |
1050 | 0 | { |
1051 | 0 | if (wc_FreeMutex(&ref->mutex) != 0) { |
1052 | 0 | WOLFSSL_MSG("Failed to free mutex of reference counting!"); |
1053 | 0 | } |
1054 | 0 | } |
1055 | | |
1056 | | void wolfSSL_RefInc(wolfSSL_Ref* ref, int* err) |
1057 | 0 | { |
1058 | 0 | int ret = wc_LockMutex(&ref->mutex); |
1059 | 0 | if (ret != 0) { |
1060 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference increment!"); |
1061 | 0 | } |
1062 | 0 | else { |
1063 | 0 | ref->count++; |
1064 | 0 | wc_UnLockMutex(&ref->mutex); |
1065 | 0 | } |
1066 | 0 | *err = ret; |
1067 | 0 | } |
1068 | | |
1069 | | void wolfSSL_RefDec(wolfSSL_Ref* ref, int* isZero, int* err) |
1070 | 0 | { |
1071 | 0 | int ret = wc_LockMutex(&ref->mutex); |
1072 | 0 | if (ret != 0) { |
1073 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference decrement!"); |
1074 | 0 | } |
1075 | 0 | else { |
1076 | 0 | ref->count--; |
1077 | 0 | *isZero = (ref->count == 0); |
1078 | 0 | wc_UnLockMutex(&ref->mutex); |
1079 | 0 | } |
1080 | 0 | *err = ret; |
1081 | 0 | } |
1082 | | #endif |
1083 | | |
1084 | | #if WOLFSSL_CRYPT_HW_MUTEX |
1085 | | /* Mutex for protection of cryptography hardware */ |
1086 | | static wolfSSL_Mutex wcCryptHwMutex; |
1087 | | static int wcCryptHwMutexInit = 0; |
1088 | | |
1089 | | int wolfSSL_CryptHwMutexInit(void) |
1090 | | { |
1091 | | int ret = 0; |
1092 | | if (wcCryptHwMutexInit == 0) { |
1093 | | ret = wc_InitMutex(&wcCryptHwMutex); |
1094 | | if (ret == 0) { |
1095 | | wcCryptHwMutexInit = 1; |
1096 | | } |
1097 | | } |
1098 | | return ret; |
1099 | | } |
1100 | | int wolfSSL_CryptHwMutexLock(void) |
1101 | | { |
1102 | | int ret = BAD_MUTEX_E; |
1103 | | /* Make sure HW Mutex has been initialized */ |
1104 | | ret = wolfSSL_CryptHwMutexInit(); |
1105 | | if (ret == 0) { |
1106 | | ret = wc_LockMutex(&wcCryptHwMutex); |
1107 | | } |
1108 | | return ret; |
1109 | | } |
1110 | | int wolfSSL_CryptHwMutexUnLock(void) |
1111 | | { |
1112 | | int ret = BAD_MUTEX_E; |
1113 | | if (wcCryptHwMutexInit) { |
1114 | | ret = wc_UnLockMutex(&wcCryptHwMutex); |
1115 | | } |
1116 | | return ret; |
1117 | | } |
1118 | | #endif /* WOLFSSL_CRYPT_HW_MUTEX */ |
1119 | | |
1120 | | |
1121 | | /* ---------------------------------------------------------------------------*/ |
1122 | | /* Mutex Ports */ |
1123 | | /* ---------------------------------------------------------------------------*/ |
1124 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
1125 | | static mutex_cb* compat_mutex_cb = NULL; |
1126 | | |
1127 | | /* Function that locks or unlocks a mutex based on the flag passed in. |
1128 | | * |
1129 | | * flag lock or unlock i.e. CRYPTO_LOCK |
1130 | | * type the type of lock to unlock or lock |
1131 | | * file name of the file calling |
1132 | | * line the line number from file calling |
1133 | | */ |
1134 | | int wc_LockMutex_ex(int flag, int type, const char* file, int line) |
1135 | | { |
1136 | | if (compat_mutex_cb != NULL) { |
1137 | | compat_mutex_cb(flag, type, file, line); |
1138 | | return 0; |
1139 | | } |
1140 | | else { |
1141 | | WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb"); |
1142 | | return BAD_STATE_E; |
1143 | | } |
1144 | | } |
1145 | | |
1146 | | |
1147 | | /* Set the callback function to use for locking/unlocking mutex |
1148 | | * |
1149 | | * cb callback function to use |
1150 | | */ |
1151 | | int wc_SetMutexCb(mutex_cb* cb) |
1152 | | { |
1153 | | compat_mutex_cb = cb; |
1154 | | return 0; |
1155 | | } |
1156 | | #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */ |
1157 | | #ifdef SINGLE_THREADED |
1158 | | |
1159 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1160 | | { |
1161 | | (void)m; |
1162 | | return 0; |
1163 | | } |
1164 | | |
1165 | | int wc_FreeMutex(wolfSSL_Mutex *m) |
1166 | | { |
1167 | | (void)m; |
1168 | | return 0; |
1169 | | } |
1170 | | |
1171 | | |
1172 | | int wc_LockMutex(wolfSSL_Mutex *m) |
1173 | | { |
1174 | | (void)m; |
1175 | | return 0; |
1176 | | } |
1177 | | |
1178 | | |
1179 | | int wc_UnLockMutex(wolfSSL_Mutex *m) |
1180 | | { |
1181 | | (void)m; |
1182 | | return 0; |
1183 | | } |
1184 | | |
1185 | | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ |
1186 | | defined(FREESCALE_FREE_RTOS) |
1187 | | |
1188 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1189 | | { |
1190 | | int iReturn; |
1191 | | |
1192 | | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex(); |
1193 | | if( *m != NULL ) |
1194 | | iReturn = 0; |
1195 | | else |
1196 | | iReturn = BAD_MUTEX_E; |
1197 | | |
1198 | | return iReturn; |
1199 | | } |
1200 | | |
1201 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1202 | | { |
1203 | | vSemaphoreDelete( *m ); |
1204 | | return 0; |
1205 | | } |
1206 | | |
1207 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1208 | | { |
1209 | | /* Assume an infinite block, or should there be zero block? */ |
1210 | | xSemaphoreTake( *m, portMAX_DELAY ); |
1211 | | return 0; |
1212 | | } |
1213 | | |
1214 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1215 | | { |
1216 | | xSemaphoreGive( *m ); |
1217 | | return 0; |
1218 | | } |
1219 | | |
1220 | | #elif defined(RTTHREAD) |
1221 | | |
1222 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1223 | | { |
1224 | | int iReturn; |
1225 | | |
1226 | | *m = ( wolfSSL_Mutex ) rt_mutex_create("mutex",RT_IPC_FLAG_FIFO); |
1227 | | if( *m != NULL ) |
1228 | | iReturn = 0; |
1229 | | else |
1230 | | iReturn = BAD_MUTEX_E; |
1231 | | |
1232 | | |
1233 | | return iReturn; |
1234 | | } |
1235 | | |
1236 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1237 | | { |
1238 | | rt_mutex_delete( *m ); |
1239 | | return 0; |
1240 | | } |
1241 | | |
1242 | | |
1243 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1244 | | { |
1245 | | /* Assume an infinite block, or should there be zero block? */ |
1246 | | return rt_mutex_take( *m, RT_WAITING_FOREVER ); |
1247 | | } |
1248 | | |
1249 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1250 | | { |
1251 | | return rt_mutex_release( *m ); |
1252 | | } |
1253 | | |
1254 | | #elif defined(WOLFSSL_SAFERTOS) |
1255 | | |
1256 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1257 | | { |
1258 | | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex); |
1259 | | if (m->mutex == NULL) |
1260 | | return BAD_MUTEX_E; |
1261 | | |
1262 | | return 0; |
1263 | | } |
1264 | | |
1265 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1266 | | { |
1267 | | (void)m; |
1268 | | return 0; |
1269 | | } |
1270 | | |
1271 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1272 | | { |
1273 | | /* Assume an infinite block */ |
1274 | | xSemaphoreTake(m->mutex, portMAX_DELAY); |
1275 | | return 0; |
1276 | | } |
1277 | | |
1278 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1279 | | { |
1280 | | xSemaphoreGive(m->mutex); |
1281 | | return 0; |
1282 | | } |
1283 | | |
1284 | | #elif defined(USE_WINDOWS_API) |
1285 | | |
1286 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1287 | | { |
1288 | | InitializeCriticalSection(m); |
1289 | | return 0; |
1290 | | } |
1291 | | |
1292 | | |
1293 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1294 | | { |
1295 | | DeleteCriticalSection(m); |
1296 | | return 0; |
1297 | | } |
1298 | | |
1299 | | |
1300 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1301 | | { |
1302 | | EnterCriticalSection(m); |
1303 | | return 0; |
1304 | | } |
1305 | | |
1306 | | |
1307 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1308 | | { |
1309 | | LeaveCriticalSection(m); |
1310 | | return 0; |
1311 | | } |
1312 | | |
1313 | | #elif defined(WOLFSSL_PTHREADS) |
1314 | | |
1315 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1316 | 155k | { |
1317 | 155k | if (pthread_mutex_init(m, 0) == 0) |
1318 | 155k | return 0; |
1319 | 0 | else |
1320 | 0 | return BAD_MUTEX_E; |
1321 | 155k | } |
1322 | | |
1323 | | |
1324 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1325 | 155k | { |
1326 | 155k | if (pthread_mutex_destroy(m) == 0) |
1327 | 155k | return 0; |
1328 | 0 | else |
1329 | 0 | return BAD_MUTEX_E; |
1330 | 155k | } |
1331 | | |
1332 | | |
1333 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1334 | 328k | { |
1335 | 328k | if (pthread_mutex_lock(m) == 0) |
1336 | 328k | return 0; |
1337 | 0 | else |
1338 | 0 | return BAD_MUTEX_E; |
1339 | 328k | } |
1340 | | |
1341 | | |
1342 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1343 | 328k | { |
1344 | 328k | if (pthread_mutex_unlock(m) == 0) |
1345 | 328k | return 0; |
1346 | 0 | else |
1347 | 0 | return BAD_MUTEX_E; |
1348 | 328k | } |
1349 | | |
1350 | | #elif defined(WOLFSSL_LINUXKM) |
1351 | | |
1352 | | /* Linux kernel mutex routines are voids, alas. */ |
1353 | | |
1354 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1355 | | { |
1356 | | mutex_init(m); |
1357 | | return 0; |
1358 | | } |
1359 | | |
1360 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1361 | | { |
1362 | | mutex_destroy(m); |
1363 | | return 0; |
1364 | | } |
1365 | | |
1366 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1367 | | { |
1368 | | mutex_lock(m); |
1369 | | return 0; |
1370 | | } |
1371 | | |
1372 | | |
1373 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1374 | | { |
1375 | | mutex_unlock(m); |
1376 | | return 0; |
1377 | | } |
1378 | | |
1379 | | #elif defined(WOLFSSL_VXWORKS) |
1380 | | |
1381 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1382 | | { |
1383 | | if (m) { |
1384 | | if ((*m = semMCreate(0)) != SEM_ID_NULL) |
1385 | | return 0; |
1386 | | } |
1387 | | return BAD_MUTEX_E; |
1388 | | } |
1389 | | |
1390 | | |
1391 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1392 | | { |
1393 | | if (m) { |
1394 | | if (semDelete(*m) == OK) |
1395 | | return 0; |
1396 | | } |
1397 | | return BAD_MUTEX_E; |
1398 | | } |
1399 | | |
1400 | | |
1401 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1402 | | { |
1403 | | if (m) { |
1404 | | if (semTake(*m, WAIT_FOREVER) == OK) |
1405 | | return 0; |
1406 | | } |
1407 | | return BAD_MUTEX_E; |
1408 | | } |
1409 | | |
1410 | | |
1411 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1412 | | { |
1413 | | if (m) { |
1414 | | if (semGive(*m) == OK) |
1415 | | return 0; |
1416 | | } |
1417 | | return BAD_MUTEX_E; |
1418 | | } |
1419 | | |
1420 | | #elif defined(THREADX) |
1421 | | |
1422 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1423 | | { |
1424 | | if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0) |
1425 | | return 0; |
1426 | | else |
1427 | | return BAD_MUTEX_E; |
1428 | | } |
1429 | | |
1430 | | |
1431 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1432 | | { |
1433 | | if (tx_mutex_delete(m) == 0) |
1434 | | return 0; |
1435 | | else |
1436 | | return BAD_MUTEX_E; |
1437 | | } |
1438 | | |
1439 | | |
1440 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1441 | | { |
1442 | | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0) |
1443 | | return 0; |
1444 | | else |
1445 | | return BAD_MUTEX_E; |
1446 | | } |
1447 | | |
1448 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1449 | | { |
1450 | | if (tx_mutex_put(m) == 0) |
1451 | | return 0; |
1452 | | else |
1453 | | return BAD_MUTEX_E; |
1454 | | } |
1455 | | |
1456 | | #elif defined(WOLFSSL_DEOS) |
1457 | | |
1458 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1459 | | { |
1460 | | mutexStatus mutStat; |
1461 | | /* |
1462 | | The empty string "" denotes an anonymous mutex, so objects do not cause name collisions. |
1463 | | `protectWolfSSLTemp` in an XML configuration element template describing a mutex. |
1464 | | */ |
1465 | | if (m) { |
1466 | | mutStat = createMutex("", "protectWolfSSLTemp", m); |
1467 | | if (mutStat == mutexSuccess) |
1468 | | return 0; |
1469 | | else{ |
1470 | | WOLFSSL_MSG("wc_InitMutex failed"); |
1471 | | return mutStat; |
1472 | | } |
1473 | | } |
1474 | | return BAD_MUTEX_E; |
1475 | | } |
1476 | | |
1477 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1478 | | { |
1479 | | mutexStatus mutStat; |
1480 | | if (m) { |
1481 | | mutStat = deleteMutex(*m); |
1482 | | if (mutStat == mutexSuccess) |
1483 | | return 0; |
1484 | | else{ |
1485 | | WOLFSSL_MSG("wc_FreeMutex failed"); |
1486 | | return mutStat; |
1487 | | } |
1488 | | } |
1489 | | return BAD_MUTEX_E; |
1490 | | } |
1491 | | |
1492 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1493 | | { |
1494 | | mutexStatus mutStat; |
1495 | | if (m) { |
1496 | | mutStat = lockMutex(*m); |
1497 | | if (mutStat == mutexSuccess) |
1498 | | return 0; |
1499 | | else{ |
1500 | | WOLFSSL_MSG("wc_LockMutex failed"); |
1501 | | return mutStat; |
1502 | | } |
1503 | | } |
1504 | | return BAD_MUTEX_E; |
1505 | | } |
1506 | | |
1507 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1508 | | { |
1509 | | mutexStatus mutStat; |
1510 | | if (m) { |
1511 | | mutStat = unlockMutex(*m); |
1512 | | if (mutStat== mutexSuccess) |
1513 | | return 0; |
1514 | | else{ |
1515 | | WOLFSSL_MSG("wc_UnLockMutex failed"); |
1516 | | return mutStat; |
1517 | | } |
1518 | | } |
1519 | | return BAD_MUTEX_E; |
1520 | | } |
1521 | | |
1522 | | #elif defined(MICRIUM) |
1523 | | #if (OS_VERSION < 50000) |
1524 | | #define MICRIUM_ERR_TYPE OS_ERR |
1525 | | #define MICRIUM_ERR_NONE OS_ERR_NONE |
1526 | | #define MICRIUM_ERR_CODE(err) err |
1527 | | #else |
1528 | | #define MICRIUM_ERR_TYPE RTOS_ERR |
1529 | | #define MICRIUM_ERR_NONE RTOS_ERR_NONE |
1530 | | #define MICRIUM_ERR_CODE(err) RTOS_ERR_CODE_GET(err) |
1531 | | #endif |
1532 | | |
1533 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1534 | | { |
1535 | | MICRIUM_ERR_TYPE err; |
1536 | | |
1537 | | OSMutexCreate(m, "wolfSSL Mutex", &err); |
1538 | | |
1539 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
1540 | | return 0; |
1541 | | else |
1542 | | return BAD_MUTEX_E; |
1543 | | } |
1544 | | |
1545 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1546 | | { |
1547 | | #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED) |
1548 | | MICRIUM_ERR_TYPE err; |
1549 | | |
1550 | | OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err); |
1551 | | |
1552 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
1553 | | return 0; |
1554 | | else |
1555 | | return BAD_MUTEX_E; |
1556 | | #else |
1557 | | (void)m; |
1558 | | return 0; |
1559 | | #endif |
1560 | | } |
1561 | | |
1562 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1563 | | { |
1564 | | MICRIUM_ERR_TYPE err; |
1565 | | |
1566 | | OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err); |
1567 | | |
1568 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
1569 | | return 0; |
1570 | | else |
1571 | | return BAD_MUTEX_E; |
1572 | | } |
1573 | | |
1574 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1575 | | { |
1576 | | MICRIUM_ERR_TYPE err; |
1577 | | |
1578 | | OSMutexPost(m, OS_OPT_POST_NONE, &err); |
1579 | | |
1580 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
1581 | | return 0; |
1582 | | else |
1583 | | return BAD_MUTEX_E; |
1584 | | } |
1585 | | |
1586 | | #elif defined(EBSNET) |
1587 | | |
1588 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1589 | | { |
1590 | | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1) |
1591 | | return BAD_MUTEX_E; |
1592 | | else |
1593 | | return 0; |
1594 | | } |
1595 | | |
1596 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1597 | | { |
1598 | | rtp_sig_mutex_free(*m); |
1599 | | return 0; |
1600 | | } |
1601 | | |
1602 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1603 | | { |
1604 | | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0) |
1605 | | return 0; |
1606 | | else |
1607 | | return BAD_MUTEX_E; |
1608 | | } |
1609 | | |
1610 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1611 | | { |
1612 | | rtp_sig_mutex_release(*m); |
1613 | | return 0; |
1614 | | } |
1615 | | |
1616 | | int ebsnet_fseek(int a, long b, int c) |
1617 | | { |
1618 | | int retval; |
1619 | | |
1620 | | retval = vf_lseek(a, b, c); |
1621 | | if (retval > 0) |
1622 | | retval = 0; |
1623 | | else |
1624 | | retval = -1; |
1625 | | |
1626 | | return(retval); |
1627 | | } |
1628 | | |
1629 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
1630 | | |
1631 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1632 | | { |
1633 | | if (_mutex_init(m, NULL) == MQX_EOK) |
1634 | | return 0; |
1635 | | else |
1636 | | return BAD_MUTEX_E; |
1637 | | } |
1638 | | |
1639 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1640 | | { |
1641 | | if (_mutex_destroy(m) == MQX_EOK) |
1642 | | return 0; |
1643 | | else |
1644 | | return BAD_MUTEX_E; |
1645 | | } |
1646 | | |
1647 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1648 | | { |
1649 | | if (_mutex_lock(m) == MQX_EOK) |
1650 | | return 0; |
1651 | | else |
1652 | | return BAD_MUTEX_E; |
1653 | | } |
1654 | | |
1655 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1656 | | { |
1657 | | if (_mutex_unlock(m) == MQX_EOK) |
1658 | | return 0; |
1659 | | else |
1660 | | return BAD_MUTEX_E; |
1661 | | } |
1662 | | |
1663 | | #elif defined(WOLFSSL_TIRTOS) |
1664 | | #include <xdc/runtime/Error.h> |
1665 | | |
1666 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1667 | | { |
1668 | | Semaphore_Params params; |
1669 | | Error_Block eb; |
1670 | | |
1671 | | Error_init(&eb); |
1672 | | Semaphore_Params_init(¶ms); |
1673 | | params.mode = Semaphore_Mode_BINARY; |
1674 | | |
1675 | | *m = Semaphore_create(1, ¶ms, &eb); |
1676 | | if (Error_check(&eb)) { |
1677 | | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.", |
1678 | | NULL); |
1679 | | return BAD_MUTEX_E; |
1680 | | } |
1681 | | else |
1682 | | return 0; |
1683 | | } |
1684 | | |
1685 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1686 | | { |
1687 | | Semaphore_delete(m); |
1688 | | |
1689 | | return 0; |
1690 | | } |
1691 | | |
1692 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1693 | | { |
1694 | | Semaphore_pend(*m, BIOS_WAIT_FOREVER); |
1695 | | |
1696 | | return 0; |
1697 | | } |
1698 | | |
1699 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1700 | | { |
1701 | | Semaphore_post(*m); |
1702 | | |
1703 | | return 0; |
1704 | | } |
1705 | | |
1706 | | #elif defined(WOLFSSL_uITRON4) |
1707 | | |
1708 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1709 | | { |
1710 | | int iReturn; |
1711 | | m->sem.sematr = TA_TFIFO; |
1712 | | m->sem.isemcnt = 1; |
1713 | | m->sem.maxsem = 1; |
1714 | | m->sem.name = NULL; |
1715 | | |
1716 | | m->id = acre_sem(&m->sem); |
1717 | | if( m->id != E_OK ) |
1718 | | iReturn = 0; |
1719 | | else |
1720 | | iReturn = BAD_MUTEX_E; |
1721 | | |
1722 | | return iReturn; |
1723 | | } |
1724 | | |
1725 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1726 | | { |
1727 | | del_sem( m->id ); |
1728 | | return 0; |
1729 | | } |
1730 | | |
1731 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1732 | | { |
1733 | | wai_sem(m->id); |
1734 | | return 0; |
1735 | | } |
1736 | | |
1737 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1738 | | { |
1739 | | sig_sem(m->id); |
1740 | | return 0; |
1741 | | } |
1742 | | |
1743 | | /**** uITRON malloc/free ***/ |
1744 | | static ID ID_wolfssl_MPOOL = 0; |
1745 | | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"}; |
1746 | | |
1747 | | int uITRON4_minit(size_t poolsz) { |
1748 | | ER ercd; |
1749 | | wolfssl_MPOOL.mplsz = poolsz; |
1750 | | ercd = acre_mpl(&wolfssl_MPOOL); |
1751 | | if (ercd > 0) { |
1752 | | ID_wolfssl_MPOOL = ercd; |
1753 | | return 0; |
1754 | | } else { |
1755 | | return -1; |
1756 | | } |
1757 | | } |
1758 | | |
1759 | | void *uITRON4_malloc(size_t sz) { |
1760 | | ER ercd; |
1761 | | void *p = NULL; |
1762 | | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p); |
1763 | | if (ercd == E_OK) { |
1764 | | return p; |
1765 | | } else { |
1766 | | return 0; |
1767 | | } |
1768 | | } |
1769 | | |
1770 | | void *uITRON4_realloc(void *p, size_t sz) { |
1771 | | ER ercd; |
1772 | | void *newp = NULL; |
1773 | | if(p) { |
1774 | | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp); |
1775 | | if ((ercd == E_OK) && (newp != NULL)) { |
1776 | | XMEMCPY(newp, p, sz); |
1777 | | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
1778 | | if (ercd == E_OK) { |
1779 | | return newp; |
1780 | | } |
1781 | | } |
1782 | | } |
1783 | | return 0; |
1784 | | } |
1785 | | |
1786 | | void uITRON4_free(void *p) { |
1787 | | ER ercd; |
1788 | | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
1789 | | if (ercd == E_OK) { |
1790 | | return; |
1791 | | } else { |
1792 | | return; |
1793 | | } |
1794 | | } |
1795 | | |
1796 | | #elif defined(WOLFSSL_uTKERNEL2) |
1797 | | |
1798 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1799 | | { |
1800 | | int iReturn; |
1801 | | m->sem.sematr = TA_TFIFO; |
1802 | | m->sem.isemcnt = 1; |
1803 | | m->sem.maxsem = 1; |
1804 | | |
1805 | | m->id = tk_cre_sem(&m->sem); |
1806 | | if( m->id != NULL ) |
1807 | | iReturn = 0; |
1808 | | else |
1809 | | iReturn = BAD_MUTEX_E; |
1810 | | |
1811 | | return iReturn; |
1812 | | } |
1813 | | |
1814 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1815 | | { |
1816 | | tk_del_sem(m->id); |
1817 | | return 0; |
1818 | | } |
1819 | | |
1820 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1821 | | { |
1822 | | tk_wai_sem(m->id, 1, TMO_FEVR); |
1823 | | return 0; |
1824 | | } |
1825 | | |
1826 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1827 | | { |
1828 | | tk_sig_sem(m->id, 1); |
1829 | | return 0; |
1830 | | } |
1831 | | |
1832 | | /**** uT-Kernel malloc/free ***/ |
1833 | | static ID ID_wolfssl_MPOOL = 0; |
1834 | | static T_CMPL wolfssl_MPOOL = { |
1835 | | NULL, /* Extended information */ |
1836 | | TA_TFIFO, /* Memory pool attribute */ |
1837 | | 0, /* Size of whole memory pool (byte) */ |
1838 | | "wolfSSL" /* Object name (max 8-char) */ |
1839 | | }; |
1840 | | |
1841 | | int uTKernel_init_mpool(unsigned int sz) { |
1842 | | ER ercd; |
1843 | | wolfssl_MPOOL.mplsz = sz; |
1844 | | ercd = tk_cre_mpl(&wolfssl_MPOOL); |
1845 | | if (ercd > 0) { |
1846 | | ID_wolfssl_MPOOL = ercd; |
1847 | | return 0; |
1848 | | } else { |
1849 | | return (int)ercd; |
1850 | | } |
1851 | | } |
1852 | | |
1853 | | void *uTKernel_malloc(unsigned int sz) { |
1854 | | ER ercd; |
1855 | | void *p = NULL; |
1856 | | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR); |
1857 | | if (ercd == E_OK) { |
1858 | | return p; |
1859 | | } else { |
1860 | | return 0; |
1861 | | } |
1862 | | } |
1863 | | |
1864 | | void *uTKernel_realloc(void *p, unsigned int sz) { |
1865 | | ER ercd; |
1866 | | void *newp = NULL; |
1867 | | if (p) { |
1868 | | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR); |
1869 | | if ((ercd == E_OK) && (newp != NULL)) { |
1870 | | XMEMCPY(newp, p, sz); |
1871 | | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
1872 | | if (ercd == E_OK) { |
1873 | | return newp; |
1874 | | } |
1875 | | } |
1876 | | } |
1877 | | return 0; |
1878 | | } |
1879 | | |
1880 | | void uTKernel_free(void *p) { |
1881 | | ER ercd; |
1882 | | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
1883 | | if (ercd == E_OK) { |
1884 | | return; |
1885 | | } else { |
1886 | | return; |
1887 | | } |
1888 | | } |
1889 | | |
1890 | | #elif defined (WOLFSSL_FROSTED) |
1891 | | |
1892 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1893 | | { |
1894 | | *m = mutex_init(); |
1895 | | if (*m) |
1896 | | return 0; |
1897 | | else |
1898 | | return -1; |
1899 | | } |
1900 | | |
1901 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1902 | | { |
1903 | | mutex_destroy(*m); |
1904 | | return(0); |
1905 | | } |
1906 | | |
1907 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1908 | | { |
1909 | | mutex_lock(*m); |
1910 | | return 0; |
1911 | | } |
1912 | | |
1913 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1914 | | { |
1915 | | mutex_unlock(*m); |
1916 | | return 0; |
1917 | | } |
1918 | | |
1919 | | #elif defined(WOLFSSL_CMSIS_RTOS) |
1920 | | |
1921 | | #define CMSIS_NMUTEX 10 |
1922 | | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2); |
1923 | | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5); |
1924 | | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8); |
1925 | | osMutexDef(wolfSSL_mt9); |
1926 | | |
1927 | | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0), |
1928 | | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3), |
1929 | | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6), |
1930 | | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) }; |
1931 | | |
1932 | | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0}; |
1933 | | |
1934 | | int wc_InitMutex(wolfSSL_Mutex* m) |
1935 | | { |
1936 | | int i; |
1937 | | for (i=0; i<CMSIS_NMUTEX; i++) { |
1938 | | if(CMSIS_mutexID[i] == 0) { |
1939 | | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]); |
1940 | | (*m) = CMSIS_mutexID[i]; |
1941 | | return 0; |
1942 | | } |
1943 | | } |
1944 | | return -1; |
1945 | | } |
1946 | | |
1947 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
1948 | | { |
1949 | | int i; |
1950 | | osMutexDelete (*m); |
1951 | | for (i=0; i<CMSIS_NMUTEX; i++) { |
1952 | | if(CMSIS_mutexID[i] == (*m)) { |
1953 | | CMSIS_mutexID[i] = 0; |
1954 | | return(0); |
1955 | | } |
1956 | | } |
1957 | | return(-1); |
1958 | | } |
1959 | | |
1960 | | int wc_LockMutex(wolfSSL_Mutex* m) |
1961 | | { |
1962 | | osMutexWait(*m, osWaitForever); |
1963 | | return(0); |
1964 | | } |
1965 | | |
1966 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
1967 | | { |
1968 | | osMutexRelease (*m); |
1969 | | return 0; |
1970 | | } |
1971 | | |
1972 | | #elif defined(WOLFSSL_CMSIS_RTOSv2) |
1973 | | int wc_InitMutex(wolfSSL_Mutex *m) |
1974 | | { |
1975 | | static const osMutexAttr_t attr = { |
1976 | | "wolfSSL_mutex", osMutexRecursive, NULL, 0}; |
1977 | | |
1978 | | if ((*m = osMutexNew(&attr)) != NULL) |
1979 | | return 0; |
1980 | | else |
1981 | | return BAD_MUTEX_E; |
1982 | | } |
1983 | | |
1984 | | int wc_FreeMutex(wolfSSL_Mutex *m) |
1985 | | { |
1986 | | if (osMutexDelete(*m) == osOK) |
1987 | | return 0; |
1988 | | else |
1989 | | return BAD_MUTEX_E; |
1990 | | } |
1991 | | |
1992 | | |
1993 | | int wc_LockMutex(wolfSSL_Mutex *m) |
1994 | | { |
1995 | | if (osMutexAcquire(*m, osWaitForever) == osOK) |
1996 | | return 0; |
1997 | | else |
1998 | | return BAD_MUTEX_E; |
1999 | | } |
2000 | | |
2001 | | int wc_UnLockMutex(wolfSSL_Mutex *m) |
2002 | | { |
2003 | | if (osMutexRelease(*m) == osOK) |
2004 | | return 0; |
2005 | | else |
2006 | | return BAD_MUTEX_E; |
2007 | | } |
2008 | | |
2009 | | #elif defined(WOLFSSL_MDK_ARM) |
2010 | | |
2011 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2012 | | { |
2013 | | os_mut_init (m); |
2014 | | return 0; |
2015 | | } |
2016 | | |
2017 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2018 | | { |
2019 | | return(0); |
2020 | | } |
2021 | | |
2022 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2023 | | { |
2024 | | os_mut_wait (m, 0xffff); |
2025 | | return(0); |
2026 | | } |
2027 | | |
2028 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2029 | | { |
2030 | | os_mut_release (m); |
2031 | | return 0; |
2032 | | } |
2033 | | |
2034 | | #elif defined(INTIME_RTOS) |
2035 | | |
2036 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2037 | | { |
2038 | | int ret = 0; |
2039 | | |
2040 | | if (m == NULL) |
2041 | | return BAD_FUNC_ARG; |
2042 | | |
2043 | | *m = CreateRtSemaphore( |
2044 | | 1, /* initial unit count */ |
2045 | | 1, /* maximum unit count */ |
2046 | | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */ |
2047 | | ); |
2048 | | if (*m == BAD_RTHANDLE) { |
2049 | | ret = GetLastRtError(); |
2050 | | if (ret != E_OK) |
2051 | | ret = BAD_MUTEX_E; |
2052 | | } |
2053 | | return ret; |
2054 | | } |
2055 | | |
2056 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2057 | | { |
2058 | | int ret = 0; |
2059 | | BOOLEAN del; |
2060 | | |
2061 | | if (m == NULL) |
2062 | | return BAD_FUNC_ARG; |
2063 | | |
2064 | | del = DeleteRtSemaphore( |
2065 | | *m /* handle for RT semaphore */ |
2066 | | ); |
2067 | | if (del != TRUE) |
2068 | | ret = BAD_MUTEX_E; |
2069 | | |
2070 | | return ret; |
2071 | | } |
2072 | | |
2073 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2074 | | { |
2075 | | int ret = 0; |
2076 | | DWORD lck; |
2077 | | |
2078 | | if (m == NULL) |
2079 | | return BAD_FUNC_ARG; |
2080 | | |
2081 | | lck = WaitForRtSemaphore( |
2082 | | *m, /* handle for RT semaphore */ |
2083 | | 1, /* number of units to wait for */ |
2084 | | WAIT_FOREVER /* number of milliseconds to wait for units */ |
2085 | | ); |
2086 | | if (lck == WAIT_FAILED) { |
2087 | | ret = GetLastRtError(); |
2088 | | if (ret != E_OK) |
2089 | | ret = BAD_MUTEX_E; |
2090 | | } |
2091 | | return ret; |
2092 | | } |
2093 | | |
2094 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2095 | | { |
2096 | | int ret = 0; |
2097 | | BOOLEAN rel; |
2098 | | |
2099 | | if (m == NULL) |
2100 | | return BAD_FUNC_ARG; |
2101 | | |
2102 | | rel = ReleaseRtSemaphore( |
2103 | | *m, /* handle for RT semaphore */ |
2104 | | 1 /* number of units to release to semaphore */ |
2105 | | ); |
2106 | | if (rel != TRUE) |
2107 | | ret = BAD_MUTEX_E; |
2108 | | |
2109 | | return ret; |
2110 | | } |
2111 | | |
2112 | | #elif defined(WOLFSSL_NUCLEUS_1_2) |
2113 | | |
2114 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2115 | | { |
2116 | | /* Call the Nucleus function to create the semaphore */ |
2117 | | if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1, |
2118 | | NU_PRIORITY) == NU_SUCCESS) { |
2119 | | return 0; |
2120 | | } |
2121 | | |
2122 | | return BAD_MUTEX_E; |
2123 | | } |
2124 | | |
2125 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2126 | | { |
2127 | | if (NU_Delete_Semaphore(m) == NU_SUCCESS) |
2128 | | return 0; |
2129 | | |
2130 | | return BAD_MUTEX_E; |
2131 | | } |
2132 | | |
2133 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2134 | | { |
2135 | | /* passing suspend task option */ |
2136 | | if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS) |
2137 | | return 0; |
2138 | | |
2139 | | return BAD_MUTEX_E; |
2140 | | } |
2141 | | |
2142 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2143 | | { |
2144 | | if (NU_Release_Semaphore(m) == NU_SUCCESS) |
2145 | | return 0; |
2146 | | |
2147 | | return BAD_MUTEX_E; |
2148 | | } |
2149 | | |
2150 | | #elif defined(WOLFSSL_ZEPHYR) |
2151 | | |
2152 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2153 | | { |
2154 | | k_mutex_init(m); |
2155 | | |
2156 | | return 0; |
2157 | | } |
2158 | | |
2159 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2160 | | { |
2161 | | return 0; |
2162 | | } |
2163 | | |
2164 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2165 | | { |
2166 | | int ret = 0; |
2167 | | |
2168 | | if (k_mutex_lock(m, K_FOREVER) != 0) |
2169 | | ret = BAD_MUTEX_E; |
2170 | | |
2171 | | return ret; |
2172 | | } |
2173 | | |
2174 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2175 | | { |
2176 | | k_mutex_unlock(m); |
2177 | | |
2178 | | return 0; |
2179 | | } |
2180 | | |
2181 | | #elif defined(WOLFSSL_TELIT_M2MB) |
2182 | | |
2183 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2184 | | { |
2185 | | M2MB_OS_RESULT_E osRes; |
2186 | | M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle; |
2187 | | UINT32 inheritVal = 1; |
2188 | | |
2189 | | osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle, |
2190 | | CMDS_ARGS( |
2191 | | M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL, |
2192 | | M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx", |
2193 | | M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal |
2194 | | ) |
2195 | | ); |
2196 | | if (osRes != M2MB_OS_SUCCESS) { |
2197 | | return BAD_MUTEX_E; |
2198 | | } |
2199 | | |
2200 | | osRes = m2mb_os_mtx_init(m, &mtxAttrHandle); |
2201 | | if (osRes != M2MB_OS_SUCCESS) { |
2202 | | return BAD_MUTEX_E; |
2203 | | } |
2204 | | |
2205 | | return 0; |
2206 | | } |
2207 | | |
2208 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2209 | | { |
2210 | | M2MB_OS_RESULT_E osRes; |
2211 | | |
2212 | | if (m == NULL) |
2213 | | return BAD_MUTEX_E; |
2214 | | |
2215 | | osRes = m2mb_os_mtx_deinit(*m); |
2216 | | if (osRes != M2MB_OS_SUCCESS) { |
2217 | | return BAD_MUTEX_E; |
2218 | | } |
2219 | | |
2220 | | return 0; |
2221 | | } |
2222 | | |
2223 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2224 | | { |
2225 | | M2MB_OS_RESULT_E osRes; |
2226 | | |
2227 | | if (m == NULL) |
2228 | | return BAD_MUTEX_E; |
2229 | | |
2230 | | osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER); |
2231 | | if (osRes != M2MB_OS_SUCCESS) { |
2232 | | return BAD_MUTEX_E; |
2233 | | } |
2234 | | |
2235 | | return 0; |
2236 | | } |
2237 | | |
2238 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2239 | | { |
2240 | | M2MB_OS_RESULT_E osRes; |
2241 | | |
2242 | | if (m == NULL) |
2243 | | return BAD_MUTEX_E; |
2244 | | |
2245 | | osRes = m2mb_os_mtx_put(*m); |
2246 | | if (osRes != M2MB_OS_SUCCESS) { |
2247 | | return BAD_MUTEX_E; |
2248 | | } |
2249 | | |
2250 | | return 0; |
2251 | | } |
2252 | | |
2253 | | #elif defined(WOLFSSL_EMBOS) |
2254 | | |
2255 | | int wc_InitMutex(wolfSSL_Mutex* m) |
2256 | | { |
2257 | | int ret; |
2258 | | |
2259 | | OS_MUTEX_Create((OS_MUTEX*) m); |
2260 | | if (m != NULL) |
2261 | | ret = 0; |
2262 | | else |
2263 | | ret = BAD_MUTEX_E; |
2264 | | |
2265 | | return ret; |
2266 | | } |
2267 | | |
2268 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
2269 | | { |
2270 | | OS_MUTEX_Delete((OS_MUTEX*) m); |
2271 | | return 0; |
2272 | | } |
2273 | | |
2274 | | int wc_LockMutex(wolfSSL_Mutex* m) |
2275 | | { |
2276 | | OS_MUTEX_LockBlocked((OS_MUTEX*) m); |
2277 | | return 0; |
2278 | | } |
2279 | | |
2280 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
2281 | | { |
2282 | | OS_MUTEX_Unlock((OS_MUTEX*) m); |
2283 | | return 0; |
2284 | | } |
2285 | | |
2286 | | #elif defined(WOLFSSL_USER_MUTEX) |
2287 | | |
2288 | | /* Use user own mutex */ |
2289 | | |
2290 | | /* |
2291 | | int wc_InitMutex(wolfSSL_Mutex* m) { ... } |
2292 | | int wc_FreeMutex(wolfSSL_Mutex *m) { ... } |
2293 | | int wc_LockMutex(wolfSSL_Mutex *m) { ... } |
2294 | | int wc_UnLockMutex(wolfSSL_Mutex *m) { ... } |
2295 | | */ |
2296 | | |
2297 | | #else |
2298 | | #warning No mutex handling defined |
2299 | | |
2300 | | #endif |
2301 | | |
2302 | | #ifndef NO_ASN_TIME |
2303 | | #if defined(_WIN32_WCE) |
2304 | | time_t windows_time(time_t* timer) |
2305 | | { |
2306 | | SYSTEMTIME sysTime; |
2307 | | FILETIME fTime; |
2308 | | ULARGE_INTEGER intTime; |
2309 | | |
2310 | | |
2311 | | GetSystemTime(&sysTime); |
2312 | | SystemTimeToFileTime(&sysTime, &fTime); |
2313 | | |
2314 | | XMEMCPY(&intTime, &fTime, sizeof(FILETIME)); |
2315 | | /* subtract EPOCH */ |
2316 | | intTime.QuadPart -= 0x19db1ded53e8000; |
2317 | | /* to secs */ |
2318 | | intTime.QuadPart /= 10000000; |
2319 | | |
2320 | | if (timer != NULL) |
2321 | | *timer = (time_t)intTime.QuadPart; |
2322 | | |
2323 | | return (time_t)intTime.QuadPart; |
2324 | | } |
2325 | | #endif /* _WIN32_WCE */ |
2326 | | |
2327 | | #if defined(WOLFSSL_APACHE_MYNEWT) |
2328 | | #include "os/os_time.h" |
2329 | | |
2330 | | time_t mynewt_time(time_t* timer) |
2331 | | { |
2332 | | time_t now; |
2333 | | struct os_timeval tv; |
2334 | | os_gettimeofday(&tv, NULL); |
2335 | | now = (time_t)tv.tv_sec; |
2336 | | if(timer != NULL) { |
2337 | | *timer = now; |
2338 | | } |
2339 | | return now; |
2340 | | } |
2341 | | #endif /* WOLFSSL_APACHE_MYNEWT */ |
2342 | | |
2343 | | #if defined(WOLFSSL_GMTIME) |
2344 | | struct tm* gmtime(const time_t* timer) |
2345 | | { |
2346 | | #define YEAR0 1900 |
2347 | | #define EPOCH_YEAR 1970 |
2348 | | #define SECS_DAY (24L * 60L * 60L) |
2349 | | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400))) |
2350 | | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) |
2351 | | |
2352 | | static const int _ytab[2][12] = |
2353 | | { |
2354 | | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
2355 | | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
2356 | | }; |
2357 | | |
2358 | | static struct tm st_time; |
2359 | | struct tm* ret = &st_time; |
2360 | | time_t secs = *timer; |
2361 | | unsigned long dayclock, dayno; |
2362 | | int year = EPOCH_YEAR; |
2363 | | |
2364 | | dayclock = (unsigned long)secs % SECS_DAY; |
2365 | | dayno = (unsigned long)secs / SECS_DAY; |
2366 | | |
2367 | | ret->tm_sec = (int) dayclock % 60; |
2368 | | ret->tm_min = (int)(dayclock % 3600) / 60; |
2369 | | ret->tm_hour = (int) dayclock / 3600; |
2370 | | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */ |
2371 | | |
2372 | | while(dayno >= (unsigned long)YEARSIZE(year)) { |
2373 | | dayno -= YEARSIZE(year); |
2374 | | year++; |
2375 | | } |
2376 | | |
2377 | | ret->tm_year = year - YEAR0; |
2378 | | ret->tm_yday = (int)dayno; |
2379 | | ret->tm_mon = 0; |
2380 | | |
2381 | | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) { |
2382 | | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon]; |
2383 | | ret->tm_mon++; |
2384 | | } |
2385 | | |
2386 | | ret->tm_mday = (int)++dayno; |
2387 | | #ifndef WOLFSSL_LINUXKM |
2388 | | ret->tm_isdst = 0; |
2389 | | #endif |
2390 | | |
2391 | | return ret; |
2392 | | } |
2393 | | #endif /* WOLFSSL_GMTIME */ |
2394 | | |
2395 | | |
2396 | | #if defined(HAVE_RTP_SYS) |
2397 | | #define YEAR0 1900 |
2398 | | |
2399 | | struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */ |
2400 | | { |
2401 | | static struct tm st_time; |
2402 | | struct tm* ret = &st_time; |
2403 | | |
2404 | | DC_RTC_CALENDAR cal; |
2405 | | dc_rtc_time_get(&cal, TRUE); |
2406 | | |
2407 | | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */ |
2408 | | ret->tm_mon = cal.month - 1; /* gm starts at 0 */ |
2409 | | ret->tm_mday = cal.day; |
2410 | | ret->tm_hour = cal.hour; |
2411 | | ret->tm_min = cal.minute; |
2412 | | ret->tm_sec = cal.second; |
2413 | | |
2414 | | return ret; |
2415 | | } |
2416 | | |
2417 | | #endif /* HAVE_RTP_SYS */ |
2418 | | |
2419 | | |
2420 | | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) |
2421 | | |
2422 | | /* |
2423 | | * time() is just a stub in Microchip libraries. We need our own |
2424 | | * implementation. Use SNTP client to get seconds since epoch. |
2425 | | */ |
2426 | | time_t pic32_time(time_t* timer) |
2427 | | { |
2428 | | #ifdef MICROCHIP_TCPIP_V5 |
2429 | | DWORD sec = 0; |
2430 | | #else |
2431 | | word32 sec = 0; |
2432 | | #endif |
2433 | | |
2434 | | #ifdef MICROCHIP_MPLAB_HARMONY |
2435 | | sec = TCPIP_SNTP_UTCSecondsGet(); |
2436 | | #else |
2437 | | sec = SNTPGetUTCSeconds(); |
2438 | | #endif |
2439 | | |
2440 | | if (timer != NULL) |
2441 | | *timer = (time_t)sec; |
2442 | | |
2443 | | return (time_t)sec; |
2444 | | } |
2445 | | |
2446 | | #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */ |
2447 | | |
2448 | | #if defined(WOLFSSL_DEOS) |
2449 | | |
2450 | | time_t deos_time(time_t* timer) |
2451 | | { |
2452 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
2453 | | const volatile word32 *systemTickPtr = systemTickPointer(); |
2454 | | |
2455 | | if (timer != NULL) |
2456 | | *timer = *systemTickPtr/systemTickTimeInHz; |
2457 | | |
2458 | | #if defined(CURRENT_UNIX_TIMESTAMP) |
2459 | | /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */ |
2460 | | return (time_t) (*systemTickPtr/systemTickTimeInHz) + CURRENT_UNIX_TIMESTAMP; |
2461 | | #else |
2462 | | return (time_t) *systemTickPtr/systemTickTimeInHz; |
2463 | | #endif |
2464 | | } |
2465 | | #endif /* WOLFSSL_DEOS */ |
2466 | | |
2467 | | #if defined(MICRIUM) |
2468 | | |
2469 | | time_t micrium_time(time_t* timer) |
2470 | | { |
2471 | | CLK_TS_SEC sec; |
2472 | | |
2473 | | Clk_GetTS_Unix(&sec); |
2474 | | |
2475 | | if (timer != NULL) |
2476 | | *timer = sec; |
2477 | | |
2478 | | return (time_t) sec; |
2479 | | } |
2480 | | |
2481 | | #endif /* MICRIUM */ |
2482 | | |
2483 | | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
2484 | | |
2485 | | time_t mqx_time(time_t* timer) |
2486 | | { |
2487 | | TIME_STRUCT time_s; |
2488 | | |
2489 | | _time_get(&time_s); |
2490 | | |
2491 | | if (timer != NULL) |
2492 | | *timer = (time_t)time_s.SECONDS; |
2493 | | |
2494 | | return (time_t)time_s.SECONDS; |
2495 | | } |
2496 | | |
2497 | | #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */ |
2498 | | |
2499 | | |
2500 | | #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME) |
2501 | | |
2502 | | time_t XTIME(time_t * timer) |
2503 | | { |
2504 | | time_t sec = 0; |
2505 | | |
2506 | | sec = (time_t) Seconds_get(); |
2507 | | |
2508 | | if (timer != NULL) |
2509 | | *timer = sec; |
2510 | | |
2511 | | return sec; |
2512 | | } |
2513 | | |
2514 | | #endif /* WOLFSSL_TIRTOS */ |
2515 | | |
2516 | | #if defined(WOLFSSL_XILINX) |
2517 | | #include "xrtcpsu.h" |
2518 | | |
2519 | | time_t xilinx_time(time_t * timer) |
2520 | | { |
2521 | | time_t sec = 0; |
2522 | | XRtcPsu_Config* con; |
2523 | | XRtcPsu rtc; |
2524 | | |
2525 | | con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID); |
2526 | | if (con != NULL) { |
2527 | | if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) { |
2528 | | sec = (time_t)XRtcPsu_GetCurrentTime(&rtc); |
2529 | | } |
2530 | | else { |
2531 | | WOLFSSL_MSG("Unable to initialize RTC"); |
2532 | | } |
2533 | | } |
2534 | | |
2535 | | if (timer != NULL) |
2536 | | *timer = sec; |
2537 | | |
2538 | | return sec; |
2539 | | } |
2540 | | |
2541 | | #endif /* WOLFSSL_XILINX */ |
2542 | | |
2543 | | #if defined(WOLFSSL_ZEPHYR) |
2544 | | |
2545 | | time_t z_time(time_t * timer) |
2546 | | { |
2547 | | struct timespec ts; |
2548 | | |
2549 | | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
2550 | | if (timer != NULL) |
2551 | | *timer = ts.tv_sec; |
2552 | | |
2553 | | return ts.tv_sec; |
2554 | | } |
2555 | | |
2556 | | #endif /* WOLFSSL_ZEPHYR */ |
2557 | | |
2558 | | |
2559 | | #if defined(WOLFSSL_WICED) |
2560 | | #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME |
2561 | | #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time. |
2562 | | #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */ |
2563 | | |
2564 | | time_t wiced_pseudo_unix_epoch_time(time_t * timer) |
2565 | | { |
2566 | | time_t epoch_time; |
2567 | | /* The time() function return uptime on WICED platform. */ |
2568 | | epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME; |
2569 | | |
2570 | | if (timer != NULL) { |
2571 | | *timer = epoch_time; |
2572 | | } |
2573 | | return epoch_time; |
2574 | | } |
2575 | | #endif /* WOLFSSL_WICED */ |
2576 | | |
2577 | | #ifdef WOLFSSL_TELIT_M2MB |
2578 | | time_t m2mb_xtime(time_t * timer) |
2579 | | { |
2580 | | time_t myTime = 0; |
2581 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
2582 | | if (fd != -1) { |
2583 | | M2MB_RTC_TIMEVAL_T timeval; |
2584 | | |
2585 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
2586 | | |
2587 | | myTime = timeval.sec; |
2588 | | |
2589 | | m2mb_rtc_close(fd); |
2590 | | } |
2591 | | return myTime; |
2592 | | } |
2593 | | #ifdef WOLFSSL_TLS13 |
2594 | | time_t m2mb_xtime_ms(time_t * timer) |
2595 | | { |
2596 | | time_t myTime = 0; |
2597 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
2598 | | if (fd != -1) { |
2599 | | M2MB_RTC_TIMEVAL_T timeval; |
2600 | | |
2601 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
2602 | | |
2603 | | myTime = timeval.sec + timeval.msec; |
2604 | | |
2605 | | m2mb_rtc_close(fd); |
2606 | | } |
2607 | | return myTime; |
2608 | | } |
2609 | | #endif /* WOLFSSL_TLS13 */ |
2610 | | #ifndef NO_CRYPT_BENCHMARK |
2611 | | double m2mb_xtime_bench(int reset) |
2612 | | { |
2613 | | double myTime = 0; |
2614 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
2615 | | if (fd != -1) { |
2616 | | M2MB_RTC_TIMEVAL_T timeval; |
2617 | | |
2618 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
2619 | | |
2620 | | myTime = (double)timeval.sec + ((double)timeval.msec / 1000); |
2621 | | |
2622 | | m2mb_rtc_close(fd); |
2623 | | } |
2624 | | return myTime; |
2625 | | } |
2626 | | #endif /* !NO_CRYPT_BENCHMARK */ |
2627 | | #endif /* WOLFSSL_TELIT_M2MB */ |
2628 | | |
2629 | | |
2630 | | #if defined(WOLFSSL_LINUXKM) |
2631 | | time_t time(time_t * timer) |
2632 | | { |
2633 | | time_t ret; |
2634 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0) |
2635 | | struct timespec ts; |
2636 | | getnstimeofday(&ts); |
2637 | | ret = ts.tv_sec; |
2638 | | #else |
2639 | | struct timespec64 ts; |
2640 | | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0) |
2641 | | ts = current_kernel_time64(); |
2642 | | #else |
2643 | | ktime_get_coarse_real_ts64(&ts); |
2644 | | #endif |
2645 | | ret = ts.tv_sec; |
2646 | | #endif |
2647 | | if (timer) |
2648 | | *timer = ret; |
2649 | | return ret; |
2650 | | } |
2651 | | #endif /* WOLFSSL_LINUXKM */ |
2652 | | |
2653 | | #ifdef HAL_RTC_MODULE_ENABLED |
2654 | | extern RTC_HandleTypeDef hrtc; |
2655 | | time_t stm32_hal_time(time_t *t1) |
2656 | | { |
2657 | | struct tm tm_time; |
2658 | | time_t ret; |
2659 | | RTC_TimeTypeDef time; |
2660 | | RTC_DateTypeDef date; |
2661 | | |
2662 | | /* order of GetTime followed by GetDate required here due to STM32 HW |
2663 | | * requirement */ |
2664 | | HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN); |
2665 | | HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN); |
2666 | | |
2667 | | tm_time.tm_year = date.Year; |
2668 | | tm_time.tm_mon = date.Month - 1; /* gm starts at 0 */ |
2669 | | tm_time.tm_mday = date.Date; |
2670 | | tm_time.tm_hour = time.Hours; |
2671 | | tm_time.tm_min = time.Minutes; |
2672 | | tm_time.tm_sec = time.Seconds; |
2673 | | |
2674 | | ret = mktime(&tm_time); |
2675 | | if (t1 != NULL) |
2676 | | *t1 = ret; |
2677 | | return ret; |
2678 | | } |
2679 | | #endif /* HAL_RTC_MODULE_ENABLED */ |
2680 | | |
2681 | | #endif /* !NO_ASN_TIME */ |
2682 | | |
2683 | | #if !defined(WOLFSSL_LEANPSK) && !defined(STRING_USER) |
2684 | | char* mystrnstr(const char* s1, const char* s2, unsigned int n) |
2685 | 97.3k | { |
2686 | 97.3k | unsigned int s2_len = (unsigned int)XSTRLEN(s2); |
2687 | | |
2688 | 97.3k | if (s2_len == 0) |
2689 | 0 | return (char*)s1; |
2690 | | |
2691 | 19.2M | while (n >= s2_len && s1[0]) { |
2692 | 19.2M | if (s1[0] == s2[0]) |
2693 | 475k | if (XMEMCMP(s1, s2, s2_len) == 0) |
2694 | 91.3k | return (char*)s1; |
2695 | 19.1M | s1++; |
2696 | 19.1M | n--; |
2697 | 19.1M | } |
2698 | | |
2699 | 6.06k | return NULL; |
2700 | 97.3k | } |
2701 | | #endif |
2702 | | |
2703 | | /* custom memory wrappers */ |
2704 | | #ifdef WOLFSSL_NUCLEUS_1_2 |
2705 | | |
2706 | | /* system memory pool */ |
2707 | | extern NU_MEMORY_POOL System_Memory; |
2708 | | |
2709 | | void* nucleus_malloc(unsigned long size, void* heap, int type) |
2710 | | { |
2711 | | STATUS status; |
2712 | | void* stack_ptr; |
2713 | | |
2714 | | status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size, |
2715 | | NU_NO_SUSPEND); |
2716 | | if (status == NU_SUCCESS) { |
2717 | | return 0; |
2718 | | } else { |
2719 | | return stack_ptr; |
2720 | | } |
2721 | | } |
2722 | | |
2723 | | void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type) |
2724 | | { |
2725 | | DM_HEADER* old_header; |
2726 | | word32 old_size, copy_size; |
2727 | | void* new_mem; |
2728 | | |
2729 | | /* if ptr is NULL, behave like malloc */ |
2730 | | new_mem = nucleus_malloc(size, NULL, 0); |
2731 | | if (new_mem == 0 || ptr == 0) { |
2732 | | return new_mem; |
2733 | | } |
2734 | | |
2735 | | /* calculate old memory block size */ |
2736 | | /* mem pointers stored in block headers (ref dm_defs.h) */ |
2737 | | old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD); |
2738 | | old_size = (byte*)old_header->dm_next_memory - (byte*)ptr; |
2739 | | |
2740 | | /* copy old to new */ |
2741 | | if (old_size < size) { |
2742 | | copy_size = old_size; |
2743 | | } else { |
2744 | | copy_size = size; |
2745 | | } |
2746 | | XMEMCPY(new_mem, ptr, copy_size); |
2747 | | |
2748 | | /* free old */ |
2749 | | nucleus_free(ptr, NULL, 0); |
2750 | | |
2751 | | return new_mem; |
2752 | | } |
2753 | | |
2754 | | void nucleus_free(void* ptr, void* heap, int type) |
2755 | | { |
2756 | | if (ptr != NULL) |
2757 | | NU_Deallocate_Memory(ptr); |
2758 | | } |
2759 | | |
2760 | | #endif /* WOLFSSL_NUCLEUS_1_2 */ |
2761 | | |
2762 | | #if defined(WOLFSSL_LINUXKM) && defined(HAVE_KVMALLOC) |
2763 | | /* adapted from kvrealloc() draft by Changli Gao, 2010-05-13 */ |
2764 | | void *lkm_realloc(void *ptr, size_t newsize) { |
2765 | | void *nptr; |
2766 | | size_t oldsize; |
2767 | | |
2768 | | if (unlikely(newsize == 0)) { |
2769 | | kvfree(ptr); |
2770 | | return ZERO_SIZE_PTR; |
2771 | | } |
2772 | | |
2773 | | if (unlikely(ptr == NULL)) |
2774 | | return kvmalloc_node(newsize, GFP_KERNEL, NUMA_NO_NODE); |
2775 | | |
2776 | | if (is_vmalloc_addr(ptr)) { |
2777 | | /* no way to discern the size of the old allocation, |
2778 | | * because the kernel doesn't export find_vm_area(). if |
2779 | | * it did, we could then call get_vm_area_size() on the |
2780 | | * returned struct vm_struct. |
2781 | | */ |
2782 | | return NULL; |
2783 | | } else { |
2784 | | #ifndef __PIE__ |
2785 | | struct page *page; |
2786 | | |
2787 | | page = virt_to_head_page(ptr); |
2788 | | if (PageSlab(page) || PageCompound(page)) { |
2789 | | if (newsize < PAGE_SIZE) |
2790 | | #endif /* ! __PIE__ */ |
2791 | | return krealloc(ptr, newsize, GFP_KERNEL); |
2792 | | #ifndef __PIE__ |
2793 | | oldsize = ksize(ptr); |
2794 | | } else { |
2795 | | oldsize = page->private; |
2796 | | if (newsize <= oldsize) |
2797 | | return ptr; |
2798 | | } |
2799 | | #endif /* ! __PIE__ */ |
2800 | | } |
2801 | | |
2802 | | nptr = kvmalloc_node(newsize, GFP_KERNEL, NUMA_NO_NODE); |
2803 | | if (nptr != NULL) { |
2804 | | memcpy(nptr, ptr, oldsize); |
2805 | | kvfree(ptr); |
2806 | | } |
2807 | | |
2808 | | return nptr; |
2809 | | } |
2810 | | #endif /* WOLFSSL_LINUXKM && HAVE_KVMALLOC */ |
2811 | | |
2812 | | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) |
2813 | | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */ |
2814 | | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */ |
2815 | | #endif |
2816 | | |
2817 | | #if defined(WOLFSSL_CRYPTOCELL) |
2818 | | #define WOLFSSL_CRYPTOCELL_C |
2819 | | #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */ |
2820 | | #if !defined(NO_SHA256) |
2821 | | #define WOLFSSL_CRYPTOCELL_HASH_C |
2822 | | #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */ |
2823 | | #endif |
2824 | | #endif |