/src/wolfssl-heapmath/wolfcrypt/src/wc_port.c
Line | Count | Source |
1 | | /* port.c |
2 | | * |
3 | | * Copyright (C) 2006-2026 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 3 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 | | #if (defined(__linux__) || defined(__ANDROID__)) && \ |
23 | | !defined(WOLFSSL_LINUXKM) && !defined(WOLFSSL_ZEPHYR) && \ |
24 | | !defined(_GNU_SOURCE) |
25 | | #define _GNU_SOURCE 1 |
26 | | #elif defined(__FreeBSD__) |
27 | | /* for __FreeBSD_version */ |
28 | | #include <sys/param.h> |
29 | | #elif (defined(__CYGWIN__) || defined(__MSYS__)) && !defined(_GNU_SOURCE) |
30 | | /* dladdr and Dl_info, for the RNG fork handler pin, hide behind it */ |
31 | | #define _GNU_SOURCE 1 |
32 | | #endif |
33 | | |
34 | | /* |
35 | | wolfCrypt Porting Build Options: |
36 | | |
37 | | Threading/Mutex options: |
38 | | * SINGLE_THREADED: No-op mutex/threading implementations default: off |
39 | | * WOLFSSL_PTHREADS: Use pthread-based mutex/threading default: off |
40 | | * (auto-detected on most POSIX systems) |
41 | | * WOLFSSL_MUTEX_INITIALIZER: Use static mutex initialization default: off |
42 | | * WC_MUTEX_OPS_INLINE: Use inlined mutex operations default: off |
43 | | * WOLFSSL_USER_MUTEX: User-provided mutex implementation default: off |
44 | | * WOLFSSL_COND: Enable condition variable support default: off |
45 | | * WOLFSSL_USE_RWLOCK: Enable reader-writer lock support default: off |
46 | | * WOLFSSL_THREAD_NO_JOIN: Create threads without join default: off |
47 | | * WOLFSSL_ALGO_HW_MUTEX: Per-algorithm hardware mutex locks default: off |
48 | | * Controls AES, hash, PK, and RNG mutexes. |
49 | | * WOLFSSL_CRYPT_HW_MUTEX: Cryptography hardware mutex default: off |
50 | | * Master control for all HW mutex init. |
51 | | * NO_AES_MUTEX: Disable AES hardware mutex default: off |
52 | | * NO_HASH_MUTEX: Disable hash hardware mutex default: off |
53 | | * NO_PK_MUTEX: Disable public-key hardware mutex default: off |
54 | | * NO_RNG_MUTEX: Disable RNG hardware mutex default: off |
55 | | * |
56 | | * Memory options: |
57 | | * USE_WOLFSSL_MEMORY: Enable custom memory allocation hooks default: on |
58 | | * WOLFSSL_STATIC_MEMORY: Use static memory pools instead of default: off |
59 | | * dynamic allocation. |
60 | | * WOLFSSL_TRACK_MEMORY: Enable memory allocation tracking default: off |
61 | | * WOLFSSL_TRACK_MEMORY_VERBOSE: Verbose memory tracking output default: off |
62 | | * WOLFSSL_FORCE_MALLOC_FAIL_TEST: Force malloc failures for default: off |
63 | | * testing error handling paths. |
64 | | * WOLFSSL_MEM_FAIL_COUNT: Count malloc failures for testing default: off |
65 | | * WOLFSSL_CHECK_MEM_ZERO: Verify sensitive memory is zeroed default: off |
66 | | * on free. Debug tool for key material. |
67 | | * |
68 | | * Filesystem options: |
69 | | * NO_FILESYSTEM: Disable all filesystem operations default: off |
70 | | * NO_WOLFSSL_DIR: Disable directory listing/iteration default: off |
71 | | * |
72 | | * Time options: |
73 | | * WOLFSSL_GMTIME: Provide custom gmtime implementation default: off |
74 | | * HAVE_TIME_T_TYPE: Platform provides time_t default: auto |
75 | | * TIME_OVERRIDES: Application provides custom time funcs default: off |
76 | | * USER_TICKS: Application provides tick counter default: off |
77 | | * USE_WOLF_TM: Use wolfSSL struct tm definition default: off |
78 | | * |
79 | | * String function options: |
80 | | * STRING_USER: User provides all string functions default: off |
81 | | * USE_WOLF_STRTOK: Use wolfSSL strtok implementation default: off |
82 | | * USE_WOLF_STRSEP: Use wolfSSL strsep implementation default: off |
83 | | * USE_WOLF_STRLCPY: Use wolfSSL strlcpy implementation default: off |
84 | | * USE_WOLF_STRLCAT: Use wolfSSL strlcat implementation default: off |
85 | | * USE_WOLF_STRCASECMP: Use wolfSSL strcasecmp implementation default: off |
86 | | * USE_WOLF_STRNCASECMP:Use wolfSSL strncasecmp implementation default: off |
87 | | * USE_WOLF_STRDUP: Use wolfSSL strdup implementation default: off |
88 | | * |
89 | | * Atomic operation options: |
90 | | * WOLFSSL_ATOMIC_OPS: Enable atomic operations for thread default: off |
91 | | * safety without full mutexes. |
92 | | * WOLFSSL_USER_DEFINED_ATOMICS: User-provided atomic impl default: off |
93 | | * WOLFSSL_HAVE_ATOMIC_H: Has C11 atomic.h header default: off |
94 | | * |
95 | | * Socket options: |
96 | | * HAVE_ACCEPT4: Use accept4() for close-on-exec accept default: auto |
97 | | * (configure/CMake detect it; set it for |
98 | | * musl or another unrecognised libc) |
99 | | * |
100 | | * General options: |
101 | | * WOLFCRYPT_ONLY: Exclude TLS/SSL, wolfCrypt only build default: off |
102 | | * WOLFSSL_LEANPSK: Lean PSK build, minimal features default: off |
103 | | * WOLF_C89: C89 compatibility mode default: off |
104 | | * WOLFSSL_SMALL_STACK: Reduce stack usage by allocating from default: off |
105 | | * heap instead. Slower but needed for |
106 | | * constrained environments. |
107 | | * DEBUG_WOLFSSL_VERBOSE: Enable verbose debug logging default: off |
108 | | */ |
109 | | |
110 | | #include <wolfssl/wolfcrypt/libwolfssl_sources.h> |
111 | | |
112 | | #ifdef __APPLE__ |
113 | | #include <AvailabilityMacros.h> |
114 | | #endif |
115 | | |
116 | | #include <wolfssl/wolfcrypt/cpuid.h> |
117 | | #ifdef HAVE_ENTROPY_MEMUSE |
118 | | #include <wolfssl/wolfcrypt/wolfentropy.h> |
119 | | #endif |
120 | | #ifdef HAVE_ECC |
121 | | #include <wolfssl/wolfcrypt/ecc.h> |
122 | | #endif |
123 | | #ifdef WOLFSSL_ASYNC_CRYPT |
124 | | #include <wolfssl/wolfcrypt/async.h> |
125 | | #endif |
126 | | #ifndef WC_NO_RNG |
127 | | /* random.h defines HAVE_HASHDRBG itself, so no HAVE_HASHDRBG test here */ |
128 | | #include <wolfssl/wolfcrypt/random.h> |
129 | | #ifdef WC_RNG_LOCK_ATFORK |
130 | | #include <dlfcn.h> /* the pin, which the handlers require */ |
131 | | #include <pthread.h> /* pthread_atfork, cancel state */ |
132 | | #include <semaphore.h> /* the one unlock a fork child may call */ |
133 | | #include <errno.h> /* EINTR from sem_wait */ |
134 | | #endif |
135 | | #endif |
136 | | |
137 | | #ifdef FREESCALE_LTC_TFM |
138 | | #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h> |
139 | | #endif |
140 | | |
141 | | #if defined(WOLFSSL_MAX3266X) || defined(WOLFSSL_MAX3266X_OLD) |
142 | | #include <wolfssl/wolfcrypt/port/maxim/max3266x.h> |
143 | | #ifdef WOLF_CRYPTO_CB |
144 | | #include <wolfssl/wolfcrypt/port/maxim/max3266x-cryptocb.h> |
145 | | #endif |
146 | | #endif |
147 | | |
148 | | #ifdef WOLFSSL_PSOC6_CRYPTO |
149 | | #include <wolfssl/wolfcrypt/port/cypress/psoc6_crypto.h> |
150 | | #endif |
151 | | |
152 | | #ifdef MAXQ10XX_MODULE_INIT |
153 | | #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h> |
154 | | #endif |
155 | | |
156 | | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ |
157 | | defined(WOLFSSL_ATECC608A) || \ |
158 | | defined(WOLFSSL_MICROCHIP_TA100) |
159 | | #include <wolfssl/wolfcrypt/port/atmel/atmel.h> |
160 | | #endif |
161 | | #if defined(WOLFSSL_RENESAS_TSIP) |
162 | | #include <wolfssl/wolfcrypt/port/Renesas/renesas_tsip_internal.h> |
163 | | #endif |
164 | | #if defined(WOLFSSL_RENESAS_FSPSM) |
165 | | #include <wolfssl/wolfcrypt/port/Renesas/renesas_fspsm_internal.h> |
166 | | #endif |
167 | | #if defined(WOLFSSL_RENESAS_RX64_HASH) |
168 | | #include <wolfssl/wolfcrypt/port/Renesas/renesas-rx64-hw-crypt.h> |
169 | | #endif |
170 | | #ifdef WOLFSSL_STSAFE |
171 | | #include <wolfssl/wolfcrypt/port/st/stsafe.h> |
172 | | #endif |
173 | | |
174 | | #if defined(WOLFSSL_TROPIC01) |
175 | | #include <wolfssl/wolfcrypt/port/tropicsquare/tropic01.h> |
176 | | #endif |
177 | | |
178 | | #if defined(WOLFSSL_SILABS_CRYPTOCB) |
179 | | #include <wolfssl/wolfcrypt/port/silabs/silabs_cryptocb.h> |
180 | | #endif |
181 | | |
182 | | #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) \ |
183 | | && !defined(WOLFCRYPT_ONLY) |
184 | | #include <wolfssl/openssl/evp.h> |
185 | | #endif |
186 | | |
187 | | #include <wolfssl/wolfcrypt/memory.h> |
188 | | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) |
189 | | #include <wolfssl/wolfcrypt/mem_track.h> |
190 | | #endif |
191 | | |
192 | | #if defined(WOLFSSL_CAAM) |
193 | | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h> |
194 | | #endif |
195 | | #if defined(WOLFSSL_SEC_QORIQ) |
196 | | #include <wolfssl/wolfcrypt/port/nxp/sec_qoriq.h> |
197 | | #endif |
198 | | #if defined(HAVE_ARIA) |
199 | | #include <wolfssl/wolfcrypt/port/aria/aria-cryptocb.h> |
200 | | #endif |
201 | | #if defined(WOLFSSL_DEVCRYPTO) |
202 | | #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h> |
203 | | #endif |
204 | | #ifdef WOLFSSL_IMXRT_DCP |
205 | | #include <wolfssl/wolfcrypt/port/nxp/dcp_port.h> |
206 | | #endif |
207 | | |
208 | | #ifdef WOLFSSL_NXP_CASPER |
209 | | #include <wolfssl/wolfcrypt/port/nxp/casper_port.h> |
210 | | #endif |
211 | | #ifdef WOLFSSL_NXP_HASHCRYPT |
212 | | #include <wolfssl/wolfcrypt/port/nxp/hashcrypt_port.h> |
213 | | #endif |
214 | | |
215 | | #ifdef WOLFSSL_TI_AM64X_R5 |
216 | | #include <wolfssl/wolfcrypt/port/ti/ti-sa2ul_r5_port.h> |
217 | | #endif |
218 | | |
219 | | #ifdef WOLF_CRYPTO_CB |
220 | | #include <wolfssl/wolfcrypt/cryptocb.h> |
221 | | #endif |
222 | | |
223 | | #if defined(WOLFSSL_VERSAL_GEN2_ASU) |
224 | | #include <wolfssl/wolfcrypt/port/xilinx/versal_gen2_asu/asu_cryptocb.h> |
225 | | #endif |
226 | | #if defined(WOLFSSL_ALTERA_FCS) && defined(WOLF_CRYPTO_CB) |
227 | | #include <wolfssl/wolfcrypt/port/altera/altera_fcs.h> |
228 | | #endif |
229 | | |
230 | | #ifdef HAVE_INTEL_QA_SYNC |
231 | | #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h> |
232 | | #endif |
233 | | |
234 | | #ifdef HAVE_CAVIUM_OCTEON_SYNC |
235 | | #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h> |
236 | | #endif |
237 | | |
238 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT) |
239 | | #include <wolfssl/wolfcrypt/port/nxp/se050_port.h> |
240 | | #endif |
241 | | |
242 | | #ifdef WOLFSSL_ELS_PKC |
243 | | #include <wolfssl/wolfcrypt/port/nxp/els_pkc_port.h> |
244 | | #endif |
245 | | |
246 | | #ifdef WOLFSSL_SCE |
247 | | #include "hal_data.h" |
248 | | #endif |
249 | | |
250 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
251 | | #include "rpcmem.h" |
252 | | #endif |
253 | | |
254 | | #ifdef _MSC_VER |
255 | | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
256 | | #pragma warning(disable: 4996) |
257 | | #endif |
258 | | |
259 | | #if defined(WOLFSSL_HAVE_PSA) |
260 | | #include <wolfssl/wolfcrypt/port/psa/psa.h> |
261 | | #endif |
262 | | |
263 | | |
264 | | #if defined(FREERTOS) && defined(WOLFSSL_ESPIDF) |
265 | | #include <freertos/FreeRTOS.h> |
266 | | #include <freertos/task.h> |
267 | | /* The Espressif-specific platform include: */ |
268 | | #include <pthread.h> |
269 | | #endif |
270 | | |
271 | | #if defined(WOLFSSL_ZEPHYR) |
272 | | #if defined(CONFIG_BOARD_NATIVE_POSIX) || defined(CONFIG_BOARD_NATIVE_SIM) |
273 | | #include "native_rtc.h" |
274 | | #endif |
275 | | #endif |
276 | | |
277 | | /* Internal APIs for counting initialization depth, with initialization/cleanup |
278 | | * races fully mitigated |
279 | | */ |
280 | | int wc_local_InitUp(wc_init_state_t *s) |
281 | 6.16k | { |
282 | 6.16k | union wc_init_state_bitfields exp_wc_init_state, new_wc_init_state; |
283 | 6.16k | exp_wc_init_state.u = WOLFSSL_ATOMIC_LOAD(*s); |
284 | | |
285 | | /* Mitigate races on init/shutdown by looping, unless |
286 | | * WC_INIT_ERROR_WHEN_CONTENDED. |
287 | | */ |
288 | 6.16k | for (;;) { |
289 | 6.16k | wc_static_assert(WC_INIT_STATE_STATE_BITS < sizeof(WC_ATOMIC_UINT_ARG) * 8); |
290 | 6.16k | #ifdef CHAR_BIT |
291 | 6.16k | wc_static_assert(WC_INIT_STATE_STATE_BITS + WC_INIT_STATE_COUNT_BITS <= |
292 | 6.16k | sizeof(WC_ATOMIC_UINT_ARG) * CHAR_BIT); |
293 | 6.16k | #endif |
294 | 6.16k | if (exp_wc_init_state.c.count == |
295 | 6.16k | (((WC_ATOMIC_UINT_ARG)1 << WC_INIT_STATE_COUNT_BITS) |
296 | 6.16k | - (WC_ATOMIC_UINT_ARG)1)) |
297 | 0 | { |
298 | 0 | return SEQ_OVERFLOW_E; |
299 | 0 | } |
300 | 6.16k | new_wc_init_state = exp_wc_init_state; |
301 | 6.16k | if (exp_wc_init_state.c.state == WC_INIT_STATE_UNINITED) { |
302 | 6.16k | if (exp_wc_init_state.c.count != 0) |
303 | 0 | return BAD_STATE_E; |
304 | 6.16k | new_wc_init_state.c.state = WC_INIT_STATE_INITING; |
305 | 6.16k | } |
306 | 0 | else if (exp_wc_init_state.c.state >= WC_INIT_STATE_BAD_STATE) { |
307 | 0 | wc_static_assert(WC_INIT_STATE_BAD_STATE > WC_INIT_STATE_UNINITED && |
308 | 0 | WC_INIT_STATE_BAD_STATE > WC_INIT_STATE_INITING && |
309 | 0 | WC_INIT_STATE_BAD_STATE > WC_INIT_STATE_INITED && |
310 | 0 | WC_INIT_STATE_BAD_STATE > WC_INIT_STATE_CLEANING_UP); |
311 | 0 | return BAD_STATE_E; |
312 | 0 | } |
313 | 0 | else { |
314 | 0 | if (exp_wc_init_state.c.count == 0) |
315 | 0 | return BAD_STATE_E; |
316 | | /* Force expected state to _INITED -- if actual value upon cmpxchg |
317 | | * doesn't match (normally either _INITING or _CLEANING_UP), we'll |
318 | | * spin until the transient state resolves to _INITED or _UNINITED |
319 | | * (when the competing thread calls wc_local_InitUpDone() or |
320 | | * wc_local_InitDownDone(), respectively). |
321 | | */ |
322 | 0 | exp_wc_init_state.c.state = WC_INIT_STATE_INITED; |
323 | 0 | new_wc_init_state.c.state = WC_INIT_STATE_INITED; |
324 | 0 | } |
325 | 6.16k | ++new_wc_init_state.c.count; |
326 | | /* if another thread entered _STATE_INITING or _CLEANING_UP, this will |
327 | | * fail and spin. |
328 | | */ |
329 | 6.16k | if (wolfSSL_Atomic_Uint_CompareExchange(s, |
330 | 6.16k | &exp_wc_init_state.u, |
331 | 6.16k | new_wc_init_state.u)) |
332 | 6.16k | break; |
333 | | #ifdef WC_INIT_ERROR_WHEN_CONTENDED |
334 | | return BUSY_E; |
335 | | #else |
336 | 0 | WC_RELAX_LONG_LOOP(); /* not really long. */ |
337 | 0 | #endif |
338 | 0 | } |
339 | 6.16k | return new_wc_init_state.c.state; |
340 | 6.16k | } |
341 | | |
342 | | int wc_local_InitUpDone(wc_init_state_t *s) |
343 | 6.16k | { |
344 | 6.16k | union wc_init_state_bitfields cur_wc_init_state; |
345 | 6.16k | cur_wc_init_state.u = WOLFSSL_ATOMIC_LOAD(*s); |
346 | 6.16k | if (cur_wc_init_state.c.state != WC_INIT_STATE_INITING) |
347 | 0 | return BAD_FUNC_ARG; |
348 | 6.16k | cur_wc_init_state.c.state = WC_INIT_STATE_INITED; |
349 | | /* Note, because WC_INIT_STATE_INITING functions as a mutex on the module |
350 | | * state, we can use a plain _STORE() to release the module into its _INITED |
351 | | * state. |
352 | | */ |
353 | 6.16k | WOLFSSL_ATOMIC_STORE(*s, cur_wc_init_state.u); |
354 | 6.16k | return 0; |
355 | 6.16k | } |
356 | | |
357 | | int wc_local_InitDown(wc_init_state_t *s) |
358 | 6.15k | { |
359 | 6.15k | union wc_init_state_bitfields exp_wc_init_state, new_wc_init_state; |
360 | | |
361 | 6.15k | exp_wc_init_state.u = WOLFSSL_ATOMIC_LOAD(*s); |
362 | | |
363 | | /* Mitigate races on init/shutdown by looping, unless |
364 | | * WC_INIT_ERROR_WHEN_CONTENDED. |
365 | | */ |
366 | 6.15k | for (;;) { |
367 | 6.15k | if (exp_wc_init_state.c.state >= WC_INIT_STATE_BAD_STATE) { |
368 | | /* wc_static_assert above in wc_local_InitUp() protects the logic of |
369 | | * the inequality test. |
370 | | */ |
371 | 0 | return BAD_STATE_E; |
372 | 0 | } |
373 | 6.15k | else if (exp_wc_init_state.c.state == WC_INIT_STATE_UNINITED) { |
374 | 0 | if (exp_wc_init_state.c.count == 0) { |
375 | | /* thread attempted to wc_local_InitDown() without a matching |
376 | | * previous wc_local_InitUp(). |
377 | | */ |
378 | 0 | return ALREADY_E; /* backward compat */ |
379 | 0 | } |
380 | 0 | else { |
381 | | /* nonzero .count with _STATE_UNINITED is impossible. */ |
382 | 0 | return BAD_STATE_E; |
383 | 0 | } |
384 | 0 | } |
385 | 6.15k | else if (exp_wc_init_state.c.state == WC_INIT_STATE_INITING) { |
386 | | /* _INITING is impossible here unless a thread calls |
387 | | * wc_local_InitDown() before (or without) successfully calling |
388 | | * wc_local_InitUpDone(). |
389 | | */ |
390 | 0 | return BAD_FUNC_ARG; |
391 | 0 | } |
392 | 6.15k | else if (exp_wc_init_state.c.state == WC_INIT_STATE_CLEANING_UP) { |
393 | 0 | if (exp_wc_init_state.c.count == 1) { |
394 | | /* thread attempted to wc_local_InitDown() without a matching |
395 | | * previous wc_local_InitUp(). |
396 | | */ |
397 | 0 | return ALREADY_E; /* backward compat */ |
398 | 0 | } |
399 | 0 | else { |
400 | | /* _CLEANING_UP is impossible with .count != 1. */ |
401 | 0 | return BAD_STATE_E; |
402 | 0 | } |
403 | 0 | } |
404 | 6.15k | else if (exp_wc_init_state.c.count == 0) { |
405 | | /* zero count with state != _UNINITED is impossible. */ |
406 | 0 | return BAD_STATE_E; |
407 | 0 | } |
408 | 6.15k | new_wc_init_state = exp_wc_init_state; |
409 | 6.15k | if (exp_wc_init_state.c.count == 1) { |
410 | 6.15k | new_wc_init_state.c.state = WC_INIT_STATE_CLEANING_UP; |
411 | | /* don't zero until end. */ |
412 | 6.15k | } |
413 | 0 | else |
414 | 0 | --new_wc_init_state.c.count; |
415 | 6.15k | if (wolfSSL_Atomic_Uint_CompareExchange(s, |
416 | 6.15k | &exp_wc_init_state.u, |
417 | 6.15k | new_wc_init_state.u)) |
418 | 6.15k | break; |
419 | | #ifdef WC_INIT_ERROR_WHEN_CONTENDED |
420 | | return BUSY_E; |
421 | | #else |
422 | 0 | WC_RELAX_LONG_LOOP(); /* not really long. */ |
423 | 0 | #endif |
424 | 0 | } |
425 | | |
426 | 6.15k | return new_wc_init_state.c.state; |
427 | 6.15k | } |
428 | | |
429 | | int wc_local_InitDownDone(wc_init_state_t *s) |
430 | 6.15k | { |
431 | 6.15k | union wc_init_state_bitfields cur_wc_init_state; |
432 | 6.15k | cur_wc_init_state.u = WOLFSSL_ATOMIC_LOAD(*s); |
433 | 6.15k | if (cur_wc_init_state.c.state != WC_INIT_STATE_CLEANING_UP) |
434 | 0 | return BAD_FUNC_ARG; |
435 | 6.15k | cur_wc_init_state.c.state = WC_INIT_STATE_UNINITED; |
436 | 6.15k | cur_wc_init_state.c.count = 0; |
437 | | /* Note, because WC_INIT_STATE_CLEANING_UP functions as a mutex on the |
438 | | * module state, we can use a plain _STORE() to release the module into its |
439 | | * _UNINITED state. |
440 | | */ |
441 | 6.15k | WOLFSSL_ATOMIC_STORE(*s, cur_wc_init_state.u); |
442 | 6.15k | return 0; |
443 | 6.15k | } |
444 | | |
445 | | #ifdef WOLF_CRYPTO_CB |
446 | | static int wc_local_InitDownAbort(wc_init_state_t* s) |
447 | 0 | { |
448 | 0 | union wc_init_state_bitfields cur_wc_init_state; |
449 | |
|
450 | 0 | cur_wc_init_state.u = WOLFSSL_ATOMIC_LOAD(*s); |
451 | 0 | if (cur_wc_init_state.c.state != WC_INIT_STATE_CLEANING_UP || |
452 | 0 | cur_wc_init_state.c.count != 1) { |
453 | 0 | return BAD_STATE_E; |
454 | 0 | } |
455 | 0 | cur_wc_init_state.c.state = WC_INIT_STATE_INITED; |
456 | 0 | WOLFSSL_ATOMIC_STORE(*s, cur_wc_init_state.u); |
457 | 0 | return 0; |
458 | 0 | } |
459 | | #endif /* WOLF_CRYPTO_CB */ |
460 | | |
461 | | static WC_DECLARE_INIT_STATE(wolfcrypt_init_state); |
462 | | |
463 | | #if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT) |
464 | | int aarch64_use_sb = 0; |
465 | | #endif |
466 | | |
467 | | #ifdef WC_RNG_HAVE_AUTO_LOCK |
468 | | /* Cancellation off while a lock is held, so a cancel cannot strand it. Where |
469 | | * the platform has no cancellation both are empty and the value is unused. |
470 | | */ |
471 | | WOLFSSL_LOCAL int wc_CancelDisable(void) |
472 | 1.02M | { |
473 | 1.02M | #ifdef PTHREAD_CANCEL_DISABLE |
474 | 1.02M | int old = PTHREAD_CANCEL_ENABLE; /* what a failed call leaves behind */ |
475 | 1.02M | (void)pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old); |
476 | | #else |
477 | | int old = 0; |
478 | | #endif |
479 | 1.02M | return old; |
480 | 1.02M | } |
481 | | |
482 | | WOLFSSL_LOCAL void wc_CancelRestore(int state) |
483 | 1.02M | { |
484 | 1.02M | #ifdef PTHREAD_CANCEL_DISABLE |
485 | 1.02M | (void)pthread_setcancelstate(state, NULL); |
486 | | #else |
487 | | (void)state; |
488 | | #endif |
489 | 1.02M | } |
490 | | #endif /* WC_RNG_HAVE_AUTO_LOCK */ |
491 | | |
492 | | #ifdef WC_RNG_LOCK_ATFORK |
493 | | #if !defined(RTLD_NOLOAD) || !defined(RTLD_NODELETE) |
494 | | #error "WC_RNG_AUTOFORK needs RTLD_NOLOAD and RTLD_NODELETE" |
495 | | #endif |
496 | | /* The fork handlers can never be unregistered, so the image that holds them |
497 | | * is pinned against dlclose() before they are registered. */ |
498 | | WOLFSSL_LOCAL void wc_PinImage(void* fn) |
499 | 41 | { |
500 | 41 | Dl_info info; |
501 | 41 | const char* name; /* a pointer on most libcs, an array on Cygwin */ |
502 | 41 | if (dladdr(fn, &info) == 0 || (name = info.dli_fname) == NULL || |
503 | 41 | name[0] == '\0' || |
504 | 41 | dlopen(name, RTLD_NOLOAD | RTLD_NODELETE | RTLD_LAZY) == NULL) { |
505 | | /* no dlopen() handle means no dlclose() can reach this image */ |
506 | 41 | WOLFSSL_MSG("RNG fork handlers: no dlopen handle, nothing to pin"); |
507 | 41 | } |
508 | 41 | } |
509 | | |
510 | | /* One lock per object, held with an unnamed semaphore so a fork child can |
511 | | * release it: sem_post() is async-signal-safe, pthread_mutex_unlock() is not. */ |
512 | | struct wc_ForkLock { |
513 | | sem_t sem; |
514 | | void* heap; |
515 | | struct wc_ForkLock* next; |
516 | | struct wc_ForkLock** prev; /* the link that leads here */ |
517 | | /* Read once per lock acquire, written once per fork: a load, never a |
518 | | * read-modify-write, so no contended line and no bus traffic. */ |
519 | | wolfSSL_Atomic_Int broken; /* fails closed; a lone child or dead sem */ |
520 | | /* Set by prepare, cleared by parent and child: no other thread reads it. */ |
521 | | int forkState; /* one of the WC_FORK_LOCK_* values */ |
522 | | int cancel; /* the holder's cancel state, back on exit */ |
523 | | }; |
524 | | |
525 | | /* Real thread local storage, not the do-nothing THREAD_LS_T fallback. */ |
526 | | #if defined(HAVE_THREAD_LS) && !defined(NO_THREAD_LS) && \ |
527 | | !defined(FREERTOS) && !defined(FREERTOS_TCP) && !defined(WOLFSSL_ZEPHYR) |
528 | | #define WC_FORK_LOCK_HAVE_TLS |
529 | | #endif |
530 | | |
531 | | #ifndef WC_FORK_LOCK_HAVE_TLS |
532 | | /* Without it the prepare handler cannot tell which locks this thread |
533 | | * holds, and a fork() from a seed callback would wait on itself. */ |
534 | | #error "the RNG fork handlers need thread local storage" |
535 | | #endif |
536 | | /* What this thread holds. Only this thread touches it, so no atomics. |
537 | | * The library never nests these; the spare slots cover a callback that does. */ |
538 | 888k | #define WC_FORK_MINE_MAX 4 /* past this, taking the lock is refused */ |
539 | | static THREAD_LS_T wc_ForkLock* forkMine[WC_FORK_MINE_MAX]; |
540 | | |
541 | | /* Returns 0 when there is no slot left to record it in. */ |
542 | | static int ForkMineAdd(wc_ForkLock* lock) |
543 | 444k | { |
544 | 444k | int i; |
545 | 444k | for (i = 0; i < WC_FORK_MINE_MAX; i++) { |
546 | 444k | if (forkMine[i] == NULL) { |
547 | 444k | forkMine[i] = lock; |
548 | 444k | return 1; |
549 | 444k | } |
550 | 444k | } |
551 | 0 | return 0; |
552 | 444k | } |
553 | | |
554 | | static void ForkMineDrop(wc_ForkLock* lock) |
555 | 444k | { |
556 | 444k | int i; |
557 | 444k | for (i = 0; i < WC_FORK_MINE_MAX; i++) { |
558 | 444k | if (forkMine[i] == lock) { |
559 | 444k | forkMine[i] = NULL; |
560 | 444k | return; |
561 | 444k | } |
562 | 444k | } |
563 | 444k | } |
564 | | |
565 | | /* Does the calling thread hold this one? */ |
566 | | static int ForkMineHeld(const wc_ForkLock* lock) |
567 | 0 | { |
568 | 0 | int i; |
569 | 0 | for (i = 0; i < WC_FORK_MINE_MAX; i++) { |
570 | 0 | if (forkMine[i] == lock) { |
571 | 0 | return 1; |
572 | 0 | } |
573 | 0 | } |
574 | 0 | return 0; |
575 | 0 | } |
576 | | |
577 | | static wc_ForkLock* forkList = NULL; /* every live lock, under forkListSem */ |
578 | | static sem_t forkListSem; |
579 | | static wolfSSL_Atomic_Int forkListDead = |
580 | | WOLFSSL_ATOMIC_INITIALIZER(0); /* registry unusable: handlers back off */ |
581 | | static pthread_once_t forkOnce = PTHREAD_ONCE_INIT; |
582 | | static int forkOnceRet = 0; |
583 | | |
584 | | /* sem_wait() is a cancellation point; a cancel here would strand the lock. */ |
585 | | static int ForkSemWait(sem_t* s) |
586 | 580k | { |
587 | 580k | int ret = 0; |
588 | 580k | int old = wc_CancelDisable(); |
589 | 580k | while (sem_wait(s) != 0) { |
590 | 0 | if (errno != EINTR) { |
591 | 0 | ret = BAD_MUTEX_E; |
592 | 0 | break; |
593 | 0 | } |
594 | 0 | } |
595 | 580k | wc_CancelRestore(old); |
596 | 580k | return ret; |
597 | 580k | } |
598 | | |
599 | | /* Before fork(): the forking thread takes the registry and every lock. */ |
600 | | static void ForkPrepare(void) |
601 | 0 | { |
602 | 0 | wc_ForkLock* n; |
603 | 0 | if (WOLFSSL_ATOMIC_LOAD(forkListDead)) |
604 | 0 | return; |
605 | 0 | if (ForkSemWait(&forkListSem) != 0) { |
606 | 0 | WOLFSSL_ATOMIC_STORE(forkListDead, 1); /* nothing held, and never again */ |
607 | 0 | return; |
608 | 0 | } |
609 | 0 | for (n = forkList; n != NULL; n = n->next) { |
610 | 0 | if (WOLFSSL_ATOMIC_LOAD(n->broken)) { |
611 | 0 | continue; |
612 | 0 | } |
613 | | /* Waiting on one this thread already holds would never return. The |
614 | | * child's only thread is this one, and it releases it as usual. */ |
615 | 0 | if (ForkMineHeld(n)) { |
616 | 0 | n->forkState = WC_FORK_LOCK_OWNED; |
617 | 0 | continue; |
618 | 0 | } |
619 | 0 | if (ForkSemWait(&n->sem) != 0) { |
620 | 0 | WOLFSSL_ATOMIC_STORE(n->broken, 1); |
621 | 0 | } |
622 | 0 | else { |
623 | 0 | n->forkState = WC_FORK_LOCK_TAKEN; |
624 | 0 | } |
625 | 0 | } |
626 | 0 | } |
627 | | |
628 | | /* After fork() in the parent: give back exactly what prepare took. |
629 | | * Re-reading broken here could post a lock prepare never held. */ |
630 | | static void ForkParent(void) |
631 | 0 | { |
632 | 0 | wc_ForkLock* n; |
633 | 0 | if (WOLFSSL_ATOMIC_LOAD(forkListDead)) |
634 | 0 | return; |
635 | 0 | for (n = forkList; n != NULL; n = n->next) { |
636 | 0 | if (n->forkState == WC_FORK_LOCK_TAKEN) { |
637 | 0 | (void)sem_post(&n->sem); |
638 | 0 | } |
639 | | /* An owned one needs nothing: its holder still has it. */ |
640 | 0 | n->forkState = WC_FORK_LOCK_UNTAKEN; |
641 | 0 | } |
642 | 0 | (void)sem_post(&forkListSem); |
643 | 0 | } |
644 | | |
645 | | /* Child after fork(): stores and sem_post() only, all POSIX allows here. |
646 | | * An untaken lock fails closed; an owned one is freed by this thread. */ |
647 | | static void ForkChild(void) |
648 | 0 | { |
649 | 0 | wc_ForkLock* n; |
650 | 0 | for (n = forkList; n != NULL; n = n->next) { /* forward links stay whole */ |
651 | 0 | if (n->forkState == WC_FORK_LOCK_TAKEN) { |
652 | 0 | (void)sem_post(&n->sem); |
653 | 0 | } |
654 | 0 | else if (n->forkState == WC_FORK_LOCK_UNTAKEN) { |
655 | 0 | WOLFSSL_ATOMIC_STORE(n->broken, 1); |
656 | 0 | } |
657 | 0 | n->forkState = WC_FORK_LOCK_UNTAKEN; |
658 | 0 | } |
659 | 0 | if (!WOLFSSL_ATOMIC_LOAD(forkListDead)) |
660 | 0 | (void)sem_post(&forkListSem); |
661 | 0 | } |
662 | | |
663 | | static void ForkLockInitOnce(void) |
664 | 41 | { |
665 | | /* pin first: the handlers can never be unregistered */ |
666 | 41 | wc_PinImage((void*)(wc_ptr_t)ForkPrepare); |
667 | 41 | forkOnceRet = (sem_init(&forkListSem, 0, 1) == 0) ? 0 : BAD_MUTEX_E; |
668 | 41 | if (forkOnceRet == 0 && |
669 | 41 | pthread_atfork(ForkPrepare, ForkParent, ForkChild) != 0) |
670 | 0 | { |
671 | 0 | (void)sem_destroy(&forkListSem); |
672 | 0 | forkOnceRet = MEMORY_E; |
673 | 0 | } |
674 | 41 | } |
675 | | |
676 | | WOLFSSL_LOCAL int wc_ForkLockInit(void) |
677 | 74.1k | { |
678 | 74.1k | (void)pthread_once(&forkOnce, ForkLockInitOnce); |
679 | 74.1k | return forkOnceRet; |
680 | 74.1k | } |
681 | | |
682 | | WOLFSSL_LOCAL int wc_ForkLock_New(wc_ForkLock** lock, void* heap) |
683 | 68.0k | { |
684 | 68.0k | wc_ForkLock* n; |
685 | 68.0k | int ret = wc_ForkLockInit(); |
686 | 68.0k | if (ret != 0) |
687 | 0 | return ret; |
688 | 68.0k | if (WOLFSSL_ATOMIC_LOAD(forkListDead)) { |
689 | 0 | WOLFSSL_MSG("wc_ForkLock_New: registry dead since a fork"); |
690 | 0 | return BAD_MUTEX_E; |
691 | 0 | } |
692 | 68.0k | n = (wc_ForkLock*)XMALLOC(sizeof(*n), heap, DYNAMIC_TYPE_RNG); |
693 | 68.0k | if (n == NULL) |
694 | 13 | return MEMORY_E; |
695 | 68.0k | XMEMSET(n, 0, sizeof(*n)); |
696 | 68.0k | n->heap = heap; |
697 | 68.0k | if (ForkSemWait(&forkListSem) != 0) { |
698 | 0 | XFREE(n, heap, DYNAMIC_TYPE_RNG); |
699 | 0 | return BAD_MUTEX_E; |
700 | 0 | } |
701 | 68.0k | ret = (sem_init(&n->sem, 0, 1) == 0) ? 0 : BAD_MUTEX_E; |
702 | 68.0k | if (ret == 0) { |
703 | 68.0k | n->next = forkList; |
704 | 68.0k | n->prev = &forkList; |
705 | 68.0k | if (forkList != NULL) |
706 | 51.8k | forkList->prev = &n->next; |
707 | 68.0k | forkList = n; |
708 | 68.0k | } |
709 | 68.0k | (void)sem_post(&forkListSem); |
710 | 68.0k | if (ret != 0) { |
711 | 0 | XFREE(n, heap, DYNAMIC_TYPE_RNG); |
712 | 0 | return ret; |
713 | 0 | } |
714 | 68.0k | *lock = n; |
715 | 68.0k | return 0; |
716 | 68.0k | } |
717 | | |
718 | | /* Safe on a NULL lock that was never created. */ |
719 | | WOLFSSL_LOCAL void wc_ForkLock_Free(wc_ForkLock** lock) |
720 | 68.1k | { |
721 | 68.1k | wc_ForkLock* n; |
722 | 68.1k | if (lock == NULL || *lock == NULL) |
723 | 203 | return; |
724 | 67.9k | n = *lock; |
725 | 67.9k | if (WOLFSSL_ATOMIC_LOAD(forkListDead) || ForkSemWait(&forkListSem) != 0) { |
726 | | /* No registry to unlink under, so the node stays on the list and |
727 | | * is leaked; broken keeps every later handler off it. */ |
728 | 0 | WOLFSSL_ATOMIC_STORE(n->broken, 1); |
729 | 0 | WOLFSSL_MSG("wc_ForkLock_Free: registry unavailable, node leaked"); |
730 | 0 | *lock = NULL; |
731 | 0 | return; |
732 | 0 | } |
733 | 67.9k | *n->prev = n->next; |
734 | 67.9k | if (n->next != NULL) |
735 | 51.8k | n->next->prev = n->prev; |
736 | 67.9k | (void)sem_post(&forkListSem); |
737 | 67.9k | (void)sem_destroy(&n->sem); |
738 | 67.9k | XFREE(n, n->heap, DYNAMIC_TYPE_RNG); |
739 | 67.9k | *lock = NULL; |
740 | 67.9k | } |
741 | | |
742 | | /* Cancellation stays off while the lock is held: a reseed reads a device, |
743 | | * a cancellation point, and a cancelled holder would strand every fork(). */ |
744 | | WOLFSSL_API int wc_ForkLock_Enter(wc_ForkLock* lock) |
745 | 444k | { |
746 | 444k | int old; |
747 | 444k | int ret; |
748 | 444k | if (lock == NULL) |
749 | 0 | return 0; |
750 | 444k | if (WOLFSSL_ATOMIC_LOAD(lock->broken)) |
751 | 0 | return BAD_MUTEX_E; |
752 | 444k | old = wc_CancelDisable(); |
753 | 444k | ret = ForkSemWait(&lock->sem); |
754 | 444k | if (ret != 0) { |
755 | 0 | wc_CancelRestore(old); |
756 | 0 | } |
757 | 444k | else if (!ForkMineAdd(lock)) { |
758 | | /* Unrecorded means prepare would wait on a lock this thread holds, |
759 | | * so refuse it here rather than hand back a fork() that hangs. */ |
760 | 0 | (void)sem_post(&lock->sem); |
761 | 0 | wc_CancelRestore(old); |
762 | 0 | ret = BAD_MUTEX_E; |
763 | 0 | } |
764 | 444k | else { |
765 | 444k | lock->cancel = old; |
766 | 444k | } |
767 | 444k | return ret; |
768 | 444k | } |
769 | | |
770 | | WOLFSSL_API void wc_ForkLock_Exit(wc_ForkLock* lock) |
771 | 444k | { |
772 | 444k | int old; |
773 | 444k | if (lock == NULL) |
774 | 0 | return; |
775 | 444k | old = lock->cancel; |
776 | 444k | ForkMineDrop(lock); |
777 | 444k | (void)sem_post(&lock->sem); |
778 | 444k | wc_CancelRestore(old); |
779 | 444k | } |
780 | | |
781 | | WOLFSSL_API void wc_ForkLock_SetBroken(wc_ForkLock* lock, int broken) |
782 | 0 | { |
783 | 0 | if (lock != NULL) |
784 | 0 | WOLFSSL_ATOMIC_STORE(lock->broken, broken); |
785 | 0 | } |
786 | | #endif |
787 | | |
788 | | #ifdef WC_BARRIER_DATA_USES_SINK |
789 | | /* Callee of the portable WC_BARRIER_DATA() arm. It is reached through a |
790 | | * pointer read with a volatile load, so link time optimization cannot see |
791 | | * that the body is empty and drop the escape of the buffer address. */ |
792 | | static void wc_BarrierDataSinkImpl(void* p) |
793 | | { |
794 | | (void)p; |
795 | | } |
796 | | static void (* const wc_BarrierDataSinkPtr)(void*) = wc_BarrierDataSinkImpl; |
797 | | |
798 | | WC_NO_INLINE void wc_BarrierDataSink(void* p) |
799 | | { |
800 | | void (*fn)(void*) = |
801 | | *(void (* const volatile *)(void*))&wc_BarrierDataSinkPtr; |
802 | | fn(p); |
803 | | } |
804 | | #endif /* WC_BARRIER_DATA_USES_SINK */ |
805 | | |
806 | | /* Used to initialize state for wolfcrypt |
807 | | return 0 on success |
808 | | */ |
809 | | WOLFSSL_ABI |
810 | | int wolfCrypt_Init(void) |
811 | 6.14k | { |
812 | 6.14k | int ret; |
813 | 6.14k | #if defined(HAVE_THREAD_LS) && !defined(NO_THREAD_LS) && defined(__GNUC__) |
814 | | /* If thread-local storage is available, use it to prevent deadlock on |
815 | | * recursion. We only do this when __GNUC__ -- this code is known to cause |
816 | | * internal compiler faults on Watcom, and is probably problematic on other |
817 | | * non-_GNUC__ targets besides. |
818 | | */ |
819 | 6.14k | static THREAD_LS_T int in_init = 0; |
820 | 6.14k | if (in_init) |
821 | 0 | return DEADLOCK_AVERTED_E; |
822 | 6.14k | #define WOLFCRYPT_INIT_RAISE_BAD_STATE() do { \ |
823 | 0 | in_init = 0; \ |
824 | 0 | WC_INIT_STATE_RAISE_BAD_STATE(wolfcrypt_init_state); \ |
825 | 0 | return ret; \ |
826 | 0 | } while (0) |
827 | | #else |
828 | | #define WOLFCRYPT_INIT_RAISE_BAD_STATE() do { \ |
829 | | WC_INIT_STATE_RAISE_BAD_STATE(wolfcrypt_init_state); \ |
830 | | return ret; \ |
831 | | } while (0) |
832 | | #endif |
833 | | |
834 | 6.14k | ret = wc_local_InitUp(&wolfcrypt_init_state); |
835 | 6.14k | if (ret < 0) |
836 | 0 | return ret; |
837 | 6.14k | else if (ret == WC_INIT_STATE_INITED) |
838 | 0 | return 0; |
839 | 6.14k | else { |
840 | 6.14k | WOLFSSL_ENTER("wolfCrypt_Init"); |
841 | | |
842 | 6.14k | #if defined(HAVE_THREAD_LS) && !defined(NO_THREAD_LS) && defined(__GNUC__) |
843 | 6.14k | in_init = 1; |
844 | 6.14k | #endif |
845 | | |
846 | | #if defined(__aarch64__) && defined(WOLFSSL_ARMASM_BARRIER_DETECT) |
847 | | aarch64_use_sb = IS_AARCH64_SB(cpuid_get_flags()); |
848 | | #endif |
849 | | |
850 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
851 | | /* Initialize the mutex for access to the list of memory locations that |
852 | | * must be freed. */ |
853 | | wc_MemZero_Init(); |
854 | | #endif |
855 | | #ifdef WOLFSSL_MEM_FAIL_COUNT |
856 | | wc_MemFailCount_Init(); |
857 | | #endif |
858 | | |
859 | | #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST |
860 | | { |
861 | | word32 rngMallocFail; |
862 | | time_t seed = time(NULL); |
863 | | srand((word32)seed); |
864 | | rngMallocFail = rand() % 2000; /* max 2000 */ |
865 | | fprintf(stderr, "\n--- RNG MALLOC FAIL AT %u ---\n", rngMallocFail); |
866 | | wolfSSL_SetMemFailCount(rngMallocFail); |
867 | | } |
868 | | #endif |
869 | | |
870 | 6.14k | #ifdef WOLF_CRYPTO_CB |
871 | 6.14k | wc_CryptoCb_Init(); |
872 | 6.14k | #endif |
873 | | |
874 | | #ifdef WOLFSSL_ASYNC_CRYPT |
875 | | ret = wolfAsync_HardwareStart(); |
876 | | if (ret != 0) { |
877 | | WOLFSSL_MSG("Async hardware start failed"); |
878 | | /* don't return failure, allow operation to continue */ |
879 | | } |
880 | | #endif |
881 | | |
882 | | #if defined(WOLFSSL_RENESAS_TSIP) |
883 | | ret = tsip_Open( ); |
884 | | if( ret != TSIP_SUCCESS ) { |
885 | | WOLFSSL_MSG("RENESAS TSIP Open failed"); |
886 | | /* not return 1 since WOLFSSL_SUCCESS=1*/ |
887 | | ret = WC_FAILURE; |
888 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
889 | | } |
890 | | #endif |
891 | | |
892 | | #if defined(WOLFSSL_RENESAS_RX64_HASH) |
893 | | ret = rx64_hw_Open(); |
894 | | if( ret != 0 ) { |
895 | | WOLFSSL_MSG("Renesas RX64 HW Open failed"); |
896 | | /* not return 1 since WOLFSSL_SUCCESS=1*/ |
897 | | ret = WC_FAILURE; |
898 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
899 | | } |
900 | | #endif |
901 | | |
902 | | #if defined(WOLFSSL_RENESAS_FSPSM) |
903 | | ret = wc_fspsm_Open( ); |
904 | | if( ret != FSP_SUCCESS ) { |
905 | | WOLFSSL_MSG("RENESAS SCE Open failed"); |
906 | | /* not return 1 since WOLFSSL_SUCCESS=1*/ |
907 | | ret = WC_FAILURE; |
908 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
909 | | } |
910 | | #endif |
911 | | |
912 | | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
913 | | ret = InitMemoryTracker(); |
914 | | if (ret != 0) { |
915 | | WOLFSSL_MSG("InitMemoryTracker failed"); |
916 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
917 | | } |
918 | | #endif |
919 | | |
920 | | #if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && defined(WOLFSSL_LINUXKM) |
921 | | ret = wc_linuxkm_allocate_svr_states(); |
922 | | if (ret != 0) { |
923 | | WOLFSSL_MSG("wc_linuxkm_allocate_svr_states failed"); |
924 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
925 | | } |
926 | | #endif |
927 | | |
928 | | #if WOLFSSL_CRYPT_HW_MUTEX |
929 | | /* If crypto hardware mutex protection is enabled, then initialize it */ |
930 | | ret = wolfSSL_CryptHwMutexInit(); |
931 | | if (ret != 0) { |
932 | | WOLFSSL_MSG("Hw crypt mutex init failed"); |
933 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
934 | | } |
935 | | #endif |
936 | | |
937 | 6.14k | #ifdef WC_RNG_LOCK_ATFORK |
938 | | /* here, before the app has threads, so no fork can race it */ |
939 | 6.14k | ret = wc_ForkLockInit(); |
940 | 6.14k | if (ret != 0) { |
941 | 0 | WOLFSSL_MSG("RNG fork handler registration failed"); |
942 | 0 | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
943 | 0 | } |
944 | 6.14k | #endif |
945 | | |
946 | | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC) |
947 | | ret = ksdk_port_init(); |
948 | | if (ret != 0) { |
949 | | WOLFSSL_MSG("KSDK port init failed"); |
950 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
951 | | } |
952 | | #endif |
953 | | |
954 | | /* Crypto Callbacks only works on AES for MAX32666/5 HW */ |
955 | | #if defined(MAX3266X_AES) && defined(WOLF_CRYPTO_CB) |
956 | | ret = wc_CryptoCb_RegisterDevice(WOLFSSL_MAX3266X_DEVID, wc_MxcCryptoCb, |
957 | | NULL); |
958 | | if (ret != 0) { |
959 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
960 | | } |
961 | | #endif |
962 | | |
963 | | /* Register the Versal Gen2 ASU device so wolfCrypt operations route to the |
964 | | * ASU hardware. Registering also brings the ASU client up. */ |
965 | | #if defined(WOLFSSL_VERSAL_GEN2_ASU) && defined(WOLF_CRYPTO_CB) |
966 | | ret = wc_AsuCryptoCb_RegisterDevice(WOLFSSL_VERSAL_GEN2_ASU_DEVID); |
967 | | if (ret != 0) { |
968 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
969 | | } |
970 | | #endif |
971 | | |
972 | | /* Register the Silicon Labs Secure Element device so wolfCrypt operations |
973 | | * route to the SE. sl_se_init() runs further down in this function. */ |
974 | | #if defined(WOLFSSL_SILABS_CRYPTOCB) && defined(WOLF_CRYPTO_CB) |
975 | | ret = wc_SilabsCryptoCb_RegisterDevice(WOLFSSL_SILABS_DEVID); |
976 | | if (ret != 0) { |
977 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
978 | | } |
979 | | #endif |
980 | | /* The Agilex 5 SDM is an optional accelerator: contexts on its devId use |
981 | | * software when it is absent, and its device key APIs report the error. */ |
982 | | #if defined(WOLFSSL_ALTERA_FCS) && defined(WOLF_CRYPTO_CB) |
983 | | ret = wc_AlteraFcsCryptoCb_RegisterDeviceMask( |
984 | | WOLFSSL_ALTERA_FCS_DEVID, WOLFSSL_ALTERA_FCS_AUTO_MASK); |
985 | | if (ret != 0) { |
986 | | WOLFSSL_MSG("Altera FCS unavailable; using software fallback"); |
987 | | ret = 0; |
988 | | } |
989 | | #endif |
990 | | #if defined(MAX3266X_RTC) |
991 | | ret = wc_MXC_RTC_Init(); |
992 | | if (ret != 0) { |
993 | | WOLFSSL_MSG("MXC RTC Init Failed"); |
994 | | ret = WC_HW_E; |
995 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
996 | | } |
997 | | #endif |
998 | | |
999 | | #if defined(WOLFSSL_TI_AM64X_R5) |
1000 | | ret = ti_sa2ul_port_init(); |
1001 | | if (ret != 0) { |
1002 | | WOLFSSL_MSG("TI AM64x Init Failed"); |
1003 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1004 | | } |
1005 | | #endif |
1006 | | |
1007 | | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) || \ |
1008 | | defined(WOLFSSL_ATECC608A) || defined(WOLFSSL_MICROCHIP_TA100) |
1009 | | ret = atmel_init(); |
1010 | | if (ret != 0) { |
1011 | | WOLFSSL_MSG("CryptoAuthLib init failed"); |
1012 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1013 | | } |
1014 | | #endif |
1015 | | #ifdef WOLFSSL_STSAFE |
1016 | | ret = stsafe_interface_init(); |
1017 | | if (ret != 0) { |
1018 | | WOLFSSL_MSG("STSAFE init failed"); |
1019 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1020 | | } |
1021 | | #endif |
1022 | | #if defined(WOLFSSL_TROPIC01) |
1023 | | ret = Tropic01_Init(); |
1024 | | if (ret != 0) { |
1025 | | WOLFSSL_MSG("Tropic01 init failed"); |
1026 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1027 | | } |
1028 | | #endif |
1029 | | #if defined(WOLFSSL_PSOC6_CRYPTO) |
1030 | | ret = psoc6_crypto_port_init(); |
1031 | | if (ret != 0) { |
1032 | | WOLFSSL_MSG("PSoC6 crypto engine init failed"); |
1033 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1034 | | } |
1035 | | #endif |
1036 | | |
1037 | | #ifdef MAXQ10XX_MODULE_INIT |
1038 | | ret = maxq10xx_port_init(); |
1039 | | if (ret != 0) { |
1040 | | WOLFSSL_MSG("MAXQ10xx port init failed"); |
1041 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1042 | | } |
1043 | | #endif |
1044 | | |
1045 | | #ifdef WOLFSSL_SILABS_SE_TYPES |
1046 | | /* init handles if it is already initialized */ |
1047 | | ret = sl_se_init(); |
1048 | | if (ret != 0) { |
1049 | | WOLFSSL_MSG("SiLabs SE Manager init failed"); |
1050 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1051 | | } |
1052 | | #endif |
1053 | | |
1054 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT) |
1055 | | /* An application may need runtime SCP03 keys to open the SE05x |
1056 | | * before calling wolfCrypt_Init(). Keep that configured session |
1057 | | * instead of trying to replace it with the compiled-in defaults. */ |
1058 | | if (wc_se050_get_session() == NULL) { |
1059 | | ret = wc_se050_init(NULL); |
1060 | | if (ret != 0) { |
1061 | | WOLFSSL_MSG("SE050 init failed"); |
1062 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1063 | | } |
1064 | | } |
1065 | | #endif |
1066 | | |
1067 | | #ifdef WOLFSSL_ELS_PKC |
1068 | | ret = wc_ElsPkc_Init(); |
1069 | | if (ret != 0) { |
1070 | | WOLFSSL_MSG("EdgeLock (ELS/PKC) init failed"); |
1071 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1072 | | } |
1073 | | #endif |
1074 | | |
1075 | | #ifdef WOLFSSL_ARMASM |
1076 | | WOLFSSL_MSG("Using ARM hardware acceleration"); |
1077 | | #endif |
1078 | | |
1079 | | #ifdef WOLFSSL_AFALG |
1080 | | WOLFSSL_MSG("Using AF_ALG for crypto acceleration"); |
1081 | | #endif |
1082 | | |
1083 | | #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA) |
1084 | | wolfSSL_EVP_init(); |
1085 | | #endif |
1086 | | |
1087 | | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
1088 | | if ((ret = wc_LoggingInit()) != 0) { |
1089 | | WOLFSSL_MSG("Error creating logging mutex"); |
1090 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1091 | | } |
1092 | | #endif |
1093 | | |
1094 | | #if defined(WOLFSSL_HAVE_PSA) |
1095 | | if ((ret = wc_psa_init()) != 0) { |
1096 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1097 | | } |
1098 | | #endif |
1099 | | |
1100 | | #if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE) |
1101 | | /* A failure here should not happen, but if it does the actual RNG seed |
1102 | | * call will fail. This init is for a shared crypt provider handle for |
1103 | | * RNG */ |
1104 | | (void)wc_WinCryptHandleInit(); |
1105 | | #endif |
1106 | | |
1107 | | #ifdef HAVE_ENTROPY_MEMUSE |
1108 | | ret = Entropy_Init(); |
1109 | | if (ret != 0) { |
1110 | | WOLFSSL_MSG("Error initializing entropy"); |
1111 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1112 | | } |
1113 | | #endif |
1114 | | |
1115 | 6.14k | #ifdef HAVE_ECC |
1116 | | #ifdef FP_ECC |
1117 | | wc_ecc_fp_init(); |
1118 | | #endif |
1119 | | #ifdef ECC_CACHE_CURVE |
1120 | | if ((ret = wc_ecc_curve_cache_init()) != 0) { |
1121 | | WOLFSSL_MSG("Error creating curve cache"); |
1122 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1123 | | } |
1124 | | #endif |
1125 | | #if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \ |
1126 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0))) |
1127 | | if ((ret = wc_ecc_oid_cache_init()) != 0) { |
1128 | | WOLFSSL_MSG("Error creating ECC oid cache"); |
1129 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1130 | | } |
1131 | | #endif |
1132 | 6.14k | #endif |
1133 | | |
1134 | | #ifdef WOLFSSL_SCE |
1135 | | ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open( |
1136 | | WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg); |
1137 | | if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) { |
1138 | | WOLFSSL_MSG("SCE already open"); |
1139 | | ret = 0; |
1140 | | } |
1141 | | if (ret != SSP_SUCCESS) { |
1142 | | WOLFSSL_MSG("Error opening SCE"); |
1143 | | ret = WC_FAILURE; |
1144 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1145 | | } |
1146 | | #endif |
1147 | | |
1148 | | #if defined(WOLFSSL_DEVCRYPTO) |
1149 | | if ((ret = wc_DevCryptoInit()) != 0) { |
1150 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1151 | | } |
1152 | | #endif |
1153 | | |
1154 | | #if defined(WOLFSSL_CAAM) |
1155 | | if ((ret = wc_caamInit()) != 0) { |
1156 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1157 | | } |
1158 | | #endif |
1159 | | |
1160 | | #if defined(WOLFSSL_SEC_QORIQ) |
1161 | | /* A part without the security engine is not an error: the SEC is |
1162 | | * only fitted on the "E" orderable variants, and everything simply |
1163 | | * stays in software there. */ |
1164 | | ret = wc_SecQoriqInit(); |
1165 | | if (ret == WC_NO_ERR_TRACE(NOT_COMPILED_IN)) { |
1166 | | ret = 0; |
1167 | | } |
1168 | | else if (ret != 0) { |
1169 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1170 | | } |
1171 | | #endif |
1172 | | |
1173 | | #if defined(HAVE_ARIA) |
1174 | | if ((ret = wc_AriaInit()) != 0) { |
1175 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1176 | | } |
1177 | | #endif |
1178 | | |
1179 | | #ifdef WOLFSSL_IMXRT_DCP |
1180 | | if ((ret = wc_dcp_init()) != 0) { |
1181 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1182 | | } |
1183 | | #endif |
1184 | | |
1185 | | #ifdef WOLFSSL_NXP_CASPER |
1186 | | if ((ret = wc_casper_init()) != 0) { |
1187 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1188 | | } |
1189 | | #endif |
1190 | | #ifdef WOLFSSL_NXP_HASHCRYPT |
1191 | | if ((ret = wc_hashcrypt_init()) != 0) { |
1192 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1193 | | } |
1194 | | #endif |
1195 | | |
1196 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
1197 | | if ((ret = wolfSSL_InitHandle()) != 0) { |
1198 | | WOLFCRYPT_INIT_RAISE_BAD_STATE(); |
1199 | | } |
1200 | | rpcmem_init(); |
1201 | | #endif |
1202 | | |
1203 | | |
1204 | 6.14k | #undef WOLFCRYPT_INIT_RAISE_BAD_STATE |
1205 | | |
1206 | 6.14k | #if defined(HAVE_THREAD_LS) && !defined(NO_THREAD_LS) && defined(__GNUC__) |
1207 | 6.14k | in_init = 0; |
1208 | 6.14k | #endif |
1209 | 6.14k | return wc_local_InitUpDone(&wolfcrypt_init_state); |
1210 | 6.14k | } |
1211 | | /* not reached */ |
1212 | 6.14k | } |
1213 | | |
1214 | | #if defined(WOLFSSL_TRACK_MEMORY_VERBOSE) && !defined(WOLFSSL_STATIC_MEMORY) |
1215 | | long wolfCrypt_heap_peakAllocs_checkpoint(void) { |
1216 | | long ret = ourMemStats.peakAllocsTripOdometer; |
1217 | | ourMemStats.peakAllocsTripOdometer = ourMemStats.totalAllocs - |
1218 | | ourMemStats.totalDeallocs; |
1219 | | return ret; |
1220 | | } |
1221 | | long wolfCrypt_heap_peakBytes_checkpoint(void) { |
1222 | | long ret = ourMemStats.peakBytesTripOdometer; |
1223 | | ourMemStats.peakBytesTripOdometer = ourMemStats.currentBytes; |
1224 | | return ret; |
1225 | | } |
1226 | | #endif |
1227 | | |
1228 | | /* return success value is the same as wolfCrypt_Init */ |
1229 | | WOLFSSL_ABI |
1230 | | int wolfCrypt_Cleanup(void) |
1231 | | { |
1232 | | int ret; |
1233 | | |
1234 | | ret = wc_local_InitDown(&wolfcrypt_init_state); |
1235 | | if (ret < 0) { |
1236 | | if (ret == WC_NO_ERR_TRACE(ALREADY_E)) |
1237 | | WOLFSSL_MSG("wolfCrypt_Cleanup() called during or after prior final cleanup."); |
1238 | | else if (ret == WC_NO_ERR_TRACE(BAD_STATE_E)) |
1239 | | WOLFSSL_MSG("wolfCrypt_Cleanup() failed: bad internal state."); |
1240 | | #ifdef WC_INIT_ERROR_WHEN_CONTENDED |
1241 | | else if (ret == WC_NO_ERR_TRACE(BUSY_E)) |
1242 | | WOLFSSL_MSG("wolfCrypt_Cleanup() failed with BUSY_E -- retry."); |
1243 | | #endif |
1244 | | else |
1245 | | WOLFSSL_MSG("wolfCrypt_Cleanup() failed with unexpected error."); |
1246 | | return ret; |
1247 | | } |
1248 | | else if (ret == WC_INIT_STATE_INITED) |
1249 | | return 0; |
1250 | | else { |
1251 | | #ifdef WOLF_CRYPTO_CB |
1252 | | /* Altera FCS unregisters through this loop too, so an abort on a |
1253 | | * different device's BUSY_E leaves its SDM session intact. */ |
1254 | | ret = wc_CryptoCb_Cleanup(); |
1255 | | if (ret != 0) { |
1256 | | int ret2 = wc_local_InitDownAbort(&wolfcrypt_init_state); |
1257 | | return (ret2 == 0) ? ret : ret2; |
1258 | | } |
1259 | | #endif |
1260 | | ret = 0; |
1261 | | |
1262 | | WOLFSSL_ENTER("wolfCrypt_Cleanup"); |
1263 | | |
1264 | | #ifdef HAVE_ECC |
1265 | | #ifdef FP_ECC |
1266 | | wc_ecc_fp_free(); |
1267 | | #endif |
1268 | | #ifdef ECC_CACHE_CURVE |
1269 | | wc_ecc_curve_cache_free(); |
1270 | | #endif |
1271 | | #if defined(HAVE_OID_ENCODING) && (!defined(HAVE_FIPS) || \ |
1272 | | (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(6,0))) |
1273 | | wc_ecc_oid_cache_free(); |
1274 | | #endif |
1275 | | #endif /* HAVE_ECC */ |
1276 | | |
1277 | | #ifdef WC_RNG_HAVE_GLOBAL_FALLBACK_RNG |
1278 | | (void)wc_RNG_global_fallback_free(); |
1279 | | #endif |
1280 | | |
1281 | | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
1282 | | { |
1283 | | int ret2 = wc_LoggingCleanup(); |
1284 | | if (ret == 0) |
1285 | | ret = ret2; |
1286 | | } |
1287 | | #endif |
1288 | | |
1289 | | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
1290 | | ShowMemoryTracker(); |
1291 | | #endif |
1292 | | |
1293 | | #ifdef WOLFSSL_ASYNC_CRYPT |
1294 | | wolfAsync_HardwareStop(); |
1295 | | #endif |
1296 | | |
1297 | | #ifdef WOLFSSL_RENESAS_TSIP |
1298 | | tsip_Close(); |
1299 | | #endif |
1300 | | |
1301 | | #if defined(WOLFSSL_RENESAS_RX64_HASH) |
1302 | | rx64_hw_Close(); |
1303 | | #endif |
1304 | | |
1305 | | #if defined(WOLFSSL_RENESAS_FSPSM) |
1306 | | wc_fspsm_Close(); |
1307 | | #endif |
1308 | | |
1309 | | #ifdef WOLFSSL_SCE |
1310 | | WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl); |
1311 | | #endif |
1312 | | |
1313 | | #ifdef WOLFSSL_ELS_PKC |
1314 | | { |
1315 | | int ret2 = wc_ElsPkc_Cleanup(); |
1316 | | if (ret == 0) |
1317 | | ret = ret2; |
1318 | | } |
1319 | | #endif |
1320 | | #if defined(WOLFSSL_CAAM) |
1321 | | wc_caamFree(); |
1322 | | #endif |
1323 | | #if defined(WOLFSSL_SEC_QORIQ) |
1324 | | wc_SecQoriqFree(); |
1325 | | #endif |
1326 | | #if defined(WOLFSSL_TI_AM64X_R5) |
1327 | | ti_sa2ul_port_cleanup(); |
1328 | | #endif |
1329 | | /* Unregister before sl_se_deinit(). wc_CryptoCb_Cleanup() further down |
1330 | | * would also clear the device, but it runs after the SE is torn down, so |
1331 | | * the unregister command would reach the callback with no SE behind it. */ |
1332 | | #if defined(WOLFSSL_SILABS_CRYPTOCB) && defined(WOLF_CRYPTO_CB) |
1333 | | { |
1334 | | int ret2 = wc_SilabsCryptoCb_UnRegisterDevice(WOLFSSL_SILABS_DEVID); |
1335 | | if (ret == 0) |
1336 | | ret = ret2; |
1337 | | } |
1338 | | #endif |
1339 | | #ifdef WOLFSSL_SILABS_SE_TYPES |
1340 | | { |
1341 | | int ret2 = sl_se_deinit(); |
1342 | | if (ret == 0) |
1343 | | ret = ret2; |
1344 | | } |
1345 | | #endif |
1346 | | #if defined(WOLFSSL_SE050) && defined(WOLFSSL_SE050_INIT) |
1347 | | if (wc_se050_get_session() != NULL) { |
1348 | | int ret2 = wc_se050_close(); |
1349 | | |
1350 | | /* A session installed with wc_se050_set_config() is owned by the |
1351 | | * application and wc_se050_close() deliberately rejects it. */ |
1352 | | if ((ret == 0) && (ret2 != WC_NO_ERR_TRACE(BAD_STATE_E))) |
1353 | | ret = ret2; |
1354 | | } |
1355 | | #endif |
1356 | | #if defined(WOLFSSL_TROPIC01) |
1357 | | Tropic01_Deinit(); |
1358 | | #endif |
1359 | | #if defined(WOLFSSL_RENESAS_TSIP) |
1360 | | tsip_Close(); |
1361 | | #endif |
1362 | | #if defined(WOLFSSL_DEVCRYPTO) |
1363 | | wc_DevCryptoCleanup(); |
1364 | | #endif |
1365 | | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD) |
1366 | | rpcmem_deinit(); |
1367 | | wolfSSL_CleanupHandle(); |
1368 | | #endif |
1369 | | #if defined(WOLFSSL_USE_SAVE_VECTOR_REGISTERS) && defined(WOLFSSL_LINUXKM) |
1370 | | wc_linuxkm_free_svr_states(); |
1371 | | #endif |
1372 | | |
1373 | | #ifdef HAVE_ENTROPY_MEMUSE |
1374 | | Entropy_Final(); |
1375 | | #endif |
1376 | | |
1377 | | #if defined(USE_WINDOWS_API) && defined(WIN_REUSE_CRYPT_HANDLE) |
1378 | | wc_WinCryptHandleCleanup(); |
1379 | | #endif |
1380 | | |
1381 | | #if defined(WOLFSSL_MEM_FAIL_COUNT) && defined(WOLFCRYPT_ONLY) |
1382 | | wc_MemFailCount_Free(); |
1383 | | #endif |
1384 | | #ifdef WOLFSSL_CHECK_MEM_ZERO |
1385 | | /* Free the mutex for access to the list of memory locations that |
1386 | | * must be freed. */ |
1387 | | wc_MemZero_Free(); |
1388 | | #endif |
1389 | | |
1390 | | |
1391 | | { |
1392 | | int ret2 = wc_local_InitDownDone(&wolfcrypt_init_state); |
1393 | | if (ret == 0) |
1394 | | ret = ret2; |
1395 | | } |
1396 | | |
1397 | | return ret; |
1398 | | } |
1399 | | |
1400 | | /* not reached */ |
1401 | | } |
1402 | | |
1403 | | #ifndef NO_FILESYSTEM |
1404 | | |
1405 | | /* Helpful function to load file into allocated buffer */ |
1406 | | int wc_FileLoad(const char* fname, unsigned char** buf, size_t* bufLen, |
1407 | | void* heap) |
1408 | 0 | { |
1409 | 0 | int ret; |
1410 | 0 | ssize_t fileSz; |
1411 | 0 | XFILE f; |
1412 | |
|
1413 | 0 | if (fname == NULL || buf == NULL || bufLen == NULL) { |
1414 | 0 | return BAD_FUNC_ARG; |
1415 | 0 | } |
1416 | | |
1417 | | /* set defaults */ |
1418 | 0 | *buf = NULL; |
1419 | 0 | *bufLen = 0; |
1420 | | |
1421 | | /* open file (read-only binary) */ |
1422 | 0 | f = XFOPEN(fname, "rb"); |
1423 | 0 | if (!f) { |
1424 | 0 | WOLFSSL_MSG("wc_LoadFile file load error"); |
1425 | 0 | return BAD_PATH_ERROR; |
1426 | 0 | } |
1427 | | |
1428 | 0 | if (XFSEEK(f, 0, XSEEK_END) != 0) { |
1429 | 0 | WOLFSSL_MSG("wc_LoadFile file seek error"); |
1430 | 0 | XFCLOSE(f); |
1431 | 0 | return BAD_PATH_ERROR; |
1432 | 0 | } |
1433 | 0 | fileSz = XFTELL(f); |
1434 | 0 | if (fileSz < 0) { |
1435 | 0 | WOLFSSL_MSG("wc_LoadFile ftell error"); |
1436 | 0 | XFCLOSE(f); |
1437 | 0 | return BAD_PATH_ERROR; |
1438 | 0 | } |
1439 | 0 | if (XFSEEK(f, 0, XSEEK_SET) != 0) { |
1440 | 0 | WOLFSSL_MSG("wc_LoadFile file seek error"); |
1441 | 0 | XFCLOSE(f); |
1442 | 0 | return BAD_PATH_ERROR; |
1443 | 0 | } |
1444 | 0 | if (fileSz > 0) { |
1445 | 0 | *bufLen = (size_t)fileSz; |
1446 | 0 | *buf = (byte*)XMALLOC(*bufLen, heap, DYNAMIC_TYPE_TMP_BUFFER); |
1447 | 0 | if (*buf == NULL) { |
1448 | 0 | WOLFSSL_MSG("wc_LoadFile memory error"); |
1449 | 0 | ret = MEMORY_E; |
1450 | 0 | } |
1451 | 0 | else { |
1452 | 0 | size_t readLen = XFREAD(*buf, 1, *bufLen, f); |
1453 | | |
1454 | | /* check response code */ |
1455 | 0 | ret = (readLen == *bufLen) ? 0 : -1; |
1456 | 0 | } |
1457 | 0 | } |
1458 | 0 | else { |
1459 | 0 | ret = BUFFER_E; |
1460 | 0 | } |
1461 | 0 | XFCLOSE(f); |
1462 | |
|
1463 | 0 | (void)heap; |
1464 | |
|
1465 | 0 | return ret; |
1466 | 0 | } |
1467 | | |
1468 | | #if !defined(NO_WOLFSSL_DIR) && \ |
1469 | | !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) |
1470 | | /* File Handling Helper */ |
1471 | | /* returns 0 if file exists, WC_ISFILEEXIST_NOFILE if file doesn't exist */ |
1472 | | int wc_FileExists(const char* fname) |
1473 | 0 | { |
1474 | 0 | struct ReadDirCtx ctx; |
1475 | |
|
1476 | 0 | XMEMSET(&ctx, 0, sizeof(ctx)); |
1477 | |
|
1478 | 0 | if (fname == NULL) |
1479 | 0 | return 0; |
1480 | | |
1481 | 0 | if (XSTAT(fname, &ctx.s) != 0) { |
1482 | 0 | WOLFSSL_MSG("stat on name failed"); |
1483 | 0 | return BAD_PATH_ERROR; |
1484 | 0 | } else { |
1485 | | #if defined(USE_WINDOWS_API) |
1486 | | if (XS_ISREG(ctx.s.st_mode)) { |
1487 | | return 0; |
1488 | | } |
1489 | | #elif defined(WOLFSSL_ZEPHYR) |
1490 | | if (XS_ISREG(ctx.s.type)) { |
1491 | | return 0; |
1492 | | } |
1493 | | #elif defined(WOLFSSL_TELIT_M2MB) |
1494 | | if (XS_ISREG(ctx.s.st_mode)) { |
1495 | | return 0; |
1496 | | } |
1497 | | #else |
1498 | 0 | if (XS_ISREG(ctx.s.st_mode)) { |
1499 | 0 | return 0; |
1500 | 0 | } |
1501 | 0 | #endif |
1502 | 0 | } |
1503 | 0 | return WC_ISFILEEXIST_NOFILE; |
1504 | 0 | } |
1505 | | |
1506 | | /* File Handling Helpers */ |
1507 | | /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */ |
1508 | | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name) |
1509 | 0 | { |
1510 | 0 | int ret = WC_READDIR_NOFILE; /* default to no files found */ |
1511 | 0 | int pathLen = 0; |
1512 | |
|
1513 | 0 | if (name) |
1514 | 0 | *name = NULL; |
1515 | |
|
1516 | 0 | if (ctx != NULL) |
1517 | 0 | XMEMSET(ctx, 0, sizeof(ReadDirCtx)); |
1518 | |
|
1519 | 0 | if (ctx == NULL || path == NULL) { |
1520 | 0 | return BAD_FUNC_ARG; |
1521 | 0 | } |
1522 | | |
1523 | 0 | pathLen = (int)XSTRLEN(path); |
1524 | |
|
1525 | | #ifdef USE_WINDOWS_API |
1526 | | if (pathLen > MAX_FILENAME_SZ - 3) |
1527 | | return BAD_PATH_ERROR; |
1528 | | |
1529 | | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3); |
1530 | | XSTRNCPY(ctx->name + pathLen, "\\*", (size_t)(MAX_FILENAME_SZ - pathLen)); |
1531 | | |
1532 | | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData); |
1533 | | if (ctx->hFind == INVALID_HANDLE_VALUE) { |
1534 | | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
1535 | | return BAD_PATH_ERROR; |
1536 | | } |
1537 | | |
1538 | | do { |
1539 | | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
1540 | | int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName); |
1541 | | |
1542 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
1543 | | return BAD_PATH_ERROR; |
1544 | | } |
1545 | | XSTRNCPY(ctx->name, path, (size_t)pathLen + 1); |
1546 | | ctx->name[pathLen] = '\\'; |
1547 | | XSTRNCPY(ctx->name + pathLen + 1, |
1548 | | ctx->FindFileData.cFileName, |
1549 | | (size_t)(MAX_FILENAME_SZ - pathLen - 1)); |
1550 | | if (name) |
1551 | | *name = ctx->name; |
1552 | | return 0; |
1553 | | } |
1554 | | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData)); |
1555 | | |
1556 | | #elif defined(INTIME_RTOS) |
1557 | | if (pathLen > MAX_FILENAME_SZ - 3) |
1558 | | return BAD_PATH_ERROR; |
1559 | | |
1560 | | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3); |
1561 | | XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen); |
1562 | | |
1563 | | if (!IntimeFindFirst(ctx->name, &ctx->FindFileData)) { |
1564 | | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
1565 | | return BAD_PATH_ERROR; |
1566 | | } |
1567 | | |
1568 | | do { |
1569 | | int dnameLen = (int)XSTRLEN(IntimeFilename(ctx)); |
1570 | | |
1571 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
1572 | | return BAD_PATH_ERROR; |
1573 | | } |
1574 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1575 | | ctx->name[pathLen] = '\\'; |
1576 | | XSTRNCPY(ctx->name + pathLen + 1, |
1577 | | IntimeFilename(ctx), |
1578 | | MAX_FILENAME_SZ - pathLen - 1); |
1579 | | if (0 == wc_FileExists(ctx->name)) { |
1580 | | if (name) |
1581 | | *name = ctx->name; |
1582 | | return 0; |
1583 | | } |
1584 | | } while (IntimeFindNext(&ctx->FindFileData)); |
1585 | | |
1586 | | #elif defined(WOLFSSL_ZEPHYR) |
1587 | | if (fs_opendir(&ctx->dir, path) != 0) { |
1588 | | WOLFSSL_MSG("opendir path verify locations failed"); |
1589 | | return BAD_PATH_ERROR; |
1590 | | } |
1591 | | ctx->dirp = &ctx->dir; |
1592 | | |
1593 | | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) { |
1594 | | int dnameLen = (int)XSTRLEN(ctx->entry.name); |
1595 | | |
1596 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1597 | | ret = BAD_PATH_ERROR; |
1598 | | break; |
1599 | | } |
1600 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1601 | | ctx->name[pathLen] = '/'; |
1602 | | |
1603 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1604 | | * of earlier check it is known that dnameLen is less than |
1605 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
1606 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1); |
1607 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1608 | | if (name) |
1609 | | *name = ctx->name; |
1610 | | return 0; |
1611 | | } |
1612 | | } |
1613 | | #elif defined(WOLFSSL_TELIT_M2MB) |
1614 | | ctx->dir = m2mb_fs_opendir((const CHAR*)path); |
1615 | | if (ctx->dir == NULL) { |
1616 | | WOLFSSL_MSG("opendir path verify locations failed"); |
1617 | | return BAD_PATH_ERROR; |
1618 | | } |
1619 | | |
1620 | | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) { |
1621 | | int dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
1622 | | |
1623 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1624 | | ret = BAD_PATH_ERROR; |
1625 | | break; |
1626 | | } |
1627 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1628 | | ctx->name[pathLen] = '/'; |
1629 | | |
1630 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1631 | | * of earlier check it is known that dnameLen is less than |
1632 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
1633 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
1634 | | |
1635 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1636 | | if (name) |
1637 | | *name = ctx->name; |
1638 | | return 0; |
1639 | | } |
1640 | | } |
1641 | | #else |
1642 | 0 | ctx->dir = opendir(path); |
1643 | 0 | if (ctx->dir == NULL) { |
1644 | 0 | WOLFSSL_MSG("opendir path verify locations failed"); |
1645 | 0 | return BAD_PATH_ERROR; |
1646 | 0 | } |
1647 | | |
1648 | 0 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
1649 | 0 | int dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
1650 | |
|
1651 | 0 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1652 | 0 | ret = BAD_PATH_ERROR; |
1653 | 0 | break; |
1654 | 0 | } |
1655 | 0 | XSTRNCPY(ctx->name, path, (size_t)pathLen + 1); |
1656 | 0 | ctx->name[pathLen] = '/'; |
1657 | | |
1658 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1659 | | * of earlier check it is known that dnameLen is less than |
1660 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
1661 | 0 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1); |
1662 | 0 | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1663 | 0 | if (name) |
1664 | 0 | *name = ctx->name; |
1665 | 0 | return 0; |
1666 | 0 | } |
1667 | 0 | } |
1668 | 0 | #endif |
1669 | 0 | wc_ReadDirClose(ctx); |
1670 | |
|
1671 | 0 | return ret; |
1672 | 0 | } |
1673 | | |
1674 | | /* returns 0 if file found, WC_READDIR_NOFILE if no more files */ |
1675 | | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name) |
1676 | 0 | { |
1677 | 0 | int ret = WC_READDIR_NOFILE; /* default to no file found */ |
1678 | 0 | int pathLen = 0; |
1679 | |
|
1680 | 0 | if (name) |
1681 | 0 | *name = NULL; |
1682 | |
|
1683 | 0 | if (ctx == NULL || path == NULL) { |
1684 | 0 | return BAD_FUNC_ARG; |
1685 | 0 | } |
1686 | | |
1687 | 0 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
1688 | 0 | pathLen = (int)XSTRLEN(path); |
1689 | |
|
1690 | | #ifdef USE_WINDOWS_API |
1691 | | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) { |
1692 | | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { |
1693 | | int dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName); |
1694 | | |
1695 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
1696 | | return BAD_PATH_ERROR; |
1697 | | } |
1698 | | XSTRNCPY(ctx->name, path, (size_t)pathLen + 1); |
1699 | | ctx->name[pathLen] = '\\'; |
1700 | | XSTRNCPY(ctx->name + pathLen + 1, |
1701 | | ctx->FindFileData.cFileName, |
1702 | | (size_t)(MAX_FILENAME_SZ - pathLen - 1)); |
1703 | | if (name) |
1704 | | *name = ctx->name; |
1705 | | return 0; |
1706 | | } |
1707 | | } |
1708 | | |
1709 | | #elif defined(INTIME_RTOS) |
1710 | | while (IntimeFindNext(&ctx->FindFileData)) { |
1711 | | int dnameLen = (int)XSTRLEN(IntimeFilename(ctx)); |
1712 | | |
1713 | | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) { |
1714 | | return BAD_PATH_ERROR; |
1715 | | } |
1716 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1717 | | ctx->name[pathLen] = '\\'; |
1718 | | XSTRNCPY(ctx->name + pathLen + 1, |
1719 | | IntimeFilename(ctx), |
1720 | | MAX_FILENAME_SZ - pathLen - 1); |
1721 | | if (0 == wc_FileExists(ctx->name)) { |
1722 | | if (name) |
1723 | | *name = ctx->name; |
1724 | | return 0; |
1725 | | } |
1726 | | } |
1727 | | |
1728 | | #elif defined(WOLFSSL_ZEPHYR) |
1729 | | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) { |
1730 | | int dnameLen = (int)XSTRLEN(ctx->entry.name); |
1731 | | |
1732 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1733 | | ret = BAD_PATH_ERROR; |
1734 | | break; |
1735 | | } |
1736 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1737 | | ctx->name[pathLen] = '/'; |
1738 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1739 | | * of earlier check it is known that dnameLen is less than |
1740 | | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */ |
1741 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1); |
1742 | | |
1743 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1744 | | if (name) |
1745 | | *name = ctx->name; |
1746 | | return 0; |
1747 | | } |
1748 | | } |
1749 | | #elif defined(WOLFSSL_TELIT_M2MB) |
1750 | | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) { |
1751 | | int dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
1752 | | |
1753 | | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1754 | | ret = BAD_PATH_ERROR; |
1755 | | break; |
1756 | | } |
1757 | | XSTRNCPY(ctx->name, path, pathLen + 1); |
1758 | | ctx->name[pathLen] = '/'; |
1759 | | |
1760 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1761 | | * of earlier check it is known that dnameLen is less than |
1762 | | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */ |
1763 | | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1); |
1764 | | |
1765 | | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1766 | | if (name) |
1767 | | *name = ctx->name; |
1768 | | return 0; |
1769 | | } |
1770 | | } |
1771 | | #else |
1772 | 0 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
1773 | 0 | int dnameLen = (int)XSTRLEN(ctx->entry->d_name); |
1774 | |
|
1775 | 0 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) { |
1776 | 0 | ret = BAD_PATH_ERROR; |
1777 | 0 | break; |
1778 | 0 | } |
1779 | 0 | XSTRNCPY(ctx->name, path, (size_t)pathLen + 1); |
1780 | 0 | ctx->name[pathLen] = '/'; |
1781 | | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because |
1782 | | * of earlier check it is known that dnameLen is less than |
1783 | | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */ |
1784 | 0 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, (size_t)dnameLen + 1); |
1785 | |
|
1786 | 0 | if ((ret = wc_FileExists(ctx->name)) == 0) { |
1787 | 0 | if (name) |
1788 | 0 | *name = ctx->name; |
1789 | 0 | return 0; |
1790 | 0 | } |
1791 | 0 | } |
1792 | 0 | #endif |
1793 | | |
1794 | 0 | wc_ReadDirClose(ctx); |
1795 | |
|
1796 | 0 | return ret; |
1797 | 0 | } |
1798 | | |
1799 | | void wc_ReadDirClose(ReadDirCtx* ctx) |
1800 | 0 | { |
1801 | 0 | if (ctx == NULL) { |
1802 | 0 | return; |
1803 | 0 | } |
1804 | | |
1805 | | #ifdef USE_WINDOWS_API |
1806 | | if (ctx->hFind != INVALID_HANDLE_VALUE) { |
1807 | | FindClose(ctx->hFind); |
1808 | | ctx->hFind = INVALID_HANDLE_VALUE; |
1809 | | } |
1810 | | |
1811 | | #elif defined(INTIME_RTOS) |
1812 | | IntimeFindClose(&ctx->FindFileData); |
1813 | | |
1814 | | #elif defined(WOLFSSL_ZEPHYR) |
1815 | | if (ctx->dirp) { |
1816 | | fs_closedir(ctx->dirp); |
1817 | | ctx->dirp = NULL; |
1818 | | } |
1819 | | #elif defined(WOLFSSL_TELIT_M2MB) |
1820 | | if (ctx->dir) { |
1821 | | m2mb_fs_closedir(ctx->dir); |
1822 | | ctx->dir = NULL; |
1823 | | } |
1824 | | #else |
1825 | 0 | if (ctx->dir) { |
1826 | 0 | if (closedir(ctx->dir) < 0) |
1827 | 0 | WOLFSSL_MSG("closedir() failed"); |
1828 | 0 | ctx->dir = NULL; |
1829 | 0 | } |
1830 | 0 | #endif |
1831 | 0 | } |
1832 | | |
1833 | | #endif /* !NO_WOLFSSL_DIR */ |
1834 | | #endif /* !NO_FILESYSTEM */ |
1835 | | |
1836 | | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR) |
1837 | | XFILE z_fs_open(const char* filename, const char* mode) |
1838 | | { |
1839 | | XFILE file; |
1840 | | fs_mode_t flags = 0; |
1841 | | |
1842 | | if (mode == NULL) |
1843 | | return NULL; |
1844 | | |
1845 | | /* Parse mode */ |
1846 | | switch (*mode++) { |
1847 | | case 'r': |
1848 | | flags |= FS_O_READ; |
1849 | | break; |
1850 | | case 'w': |
1851 | | flags |= FS_O_WRITE|FS_O_CREATE; |
1852 | | break; |
1853 | | case 'a': |
1854 | | flags |= FS_O_APPEND|FS_O_CREATE; |
1855 | | break; |
1856 | | default: |
1857 | | return NULL; |
1858 | | } |
1859 | | |
1860 | | /* Ignore binary flag */ |
1861 | | if (*mode == 'b') |
1862 | | mode++; |
1863 | | if (*mode == '+') { |
1864 | | flags |= FS_O_READ; |
1865 | | /* Don't add write flag if already appending */ |
1866 | | if (!(flags & FS_O_APPEND)) |
1867 | | flags |= FS_O_RDWR; |
1868 | | } |
1869 | | /* Ignore binary flag */ |
1870 | | if (*mode == 'b') |
1871 | | mode++; |
1872 | | /* Incorrect mode string */ |
1873 | | if (*mode != '\0') |
1874 | | return NULL; |
1875 | | |
1876 | | file = (XFILE)XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE); |
1877 | | if (file != NULL) { |
1878 | | fs_file_t_init(file); |
1879 | | if (fs_open(file, filename, flags) != 0) { |
1880 | | XFREE(file, NULL, DYNAMIC_TYPE_FILE); |
1881 | | file = NULL; |
1882 | | } |
1883 | | } |
1884 | | |
1885 | | return file; |
1886 | | } |
1887 | | |
1888 | | int z_fs_close(XFILE file) |
1889 | | { |
1890 | | int ret; |
1891 | | |
1892 | | if (file == NULL) |
1893 | | return -1; |
1894 | | ret = (fs_close(file) == 0) ? 0 : -1; |
1895 | | |
1896 | | XFREE(file, NULL, DYNAMIC_TYPE_FILE); |
1897 | | |
1898 | | return ret; |
1899 | | } |
1900 | | |
1901 | | /* Rewind the file pointer to the beginning of the file */ |
1902 | | /* This is not a 'rewind' is not supported in Zephyr so */ |
1903 | | /* use fs_seek to move the file pointer to the beginning of the file */ |
1904 | | /* calling it z_fs_rewind to avoid future conflicts if rewind is added */ |
1905 | | int z_fs_rewind(XFILE file) |
1906 | | { |
1907 | | return fs_seek(file, 0, FS_SEEK_SET); |
1908 | | } |
1909 | | |
1910 | | #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */ |
1911 | | |
1912 | | #if !defined(WOLFSSL_USER_MUTEX) |
1913 | | wolfSSL_Mutex* wc_InitAndAllocMutex(void) |
1914 | 0 | { |
1915 | 0 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL, |
1916 | 0 | DYNAMIC_TYPE_MUTEX); |
1917 | 0 | if (m != NULL) { |
1918 | 0 | if (wc_InitMutex(m) != 0) { |
1919 | 0 | WOLFSSL_MSG("Init Mutex failed"); |
1920 | 0 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX); |
1921 | 0 | m = NULL; |
1922 | 0 | } |
1923 | 0 | } |
1924 | 0 | else { |
1925 | 0 | WOLFSSL_MSG("Memory error with Mutex allocation"); |
1926 | 0 | } |
1927 | |
|
1928 | 0 | return m; |
1929 | 0 | } |
1930 | | #endif |
1931 | | |
1932 | | #ifdef USE_WOLF_STRTOK |
1933 | | /* String token (delim) search. If str is null use nextp. */ |
1934 | | char* wc_strtok(char *str, const char *delim, char **nextp) |
1935 | 0 | { |
1936 | 0 | char* ret; |
1937 | 0 | int i, j; |
1938 | | |
1939 | | /* Use next if str is NULL */ |
1940 | 0 | if (str == NULL && nextp) |
1941 | 0 | str = *nextp; |
1942 | | |
1943 | | /* verify str input */ |
1944 | 0 | if (str == NULL || *str == '\0') |
1945 | 0 | return NULL; |
1946 | | |
1947 | | /* match on entire delim */ |
1948 | 0 | for (i = 0; str[i]; i++) { |
1949 | 0 | for (j = 0; delim[j]; j++) { |
1950 | 0 | if (delim[j] == str[i]) |
1951 | 0 | break; |
1952 | 0 | } |
1953 | 0 | if (!delim[j]) |
1954 | 0 | break; |
1955 | 0 | } |
1956 | 0 | str += i; |
1957 | | /* if end of string, not found so return NULL */ |
1958 | 0 | if (*str == '\0') |
1959 | 0 | return NULL; |
1960 | | |
1961 | 0 | ret = str; |
1962 | | |
1963 | | /* match on first delim */ |
1964 | 0 | for (i = 0; str[i]; i++) { |
1965 | 0 | for (j = 0; delim[j]; j++) { |
1966 | 0 | if (delim[j] == str[i]) |
1967 | 0 | break; |
1968 | 0 | } |
1969 | 0 | if (delim[j] == str[i]) |
1970 | 0 | break; |
1971 | 0 | } |
1972 | 0 | str += i; |
1973 | | |
1974 | | /* null terminate found string */ |
1975 | 0 | if (*str) |
1976 | 0 | *str++ = '\0'; |
1977 | | |
1978 | | /* return pointer to next */ |
1979 | 0 | if (nextp) |
1980 | 0 | *nextp = str; |
1981 | |
|
1982 | 0 | return ret; |
1983 | 0 | } |
1984 | | #endif /* USE_WOLF_STRTOK */ |
1985 | | |
1986 | | #ifdef USE_WOLF_STRSEP |
1987 | | char* wc_strsep(char **stringp, const char *delim) |
1988 | 0 | { |
1989 | 0 | char *s, *tok; |
1990 | 0 | const char *spanp; |
1991 | | |
1992 | | /* null check */ |
1993 | 0 | if (stringp == NULL || *stringp == NULL) |
1994 | 0 | return NULL; |
1995 | | |
1996 | 0 | s = *stringp; |
1997 | 0 | for (tok = s; *tok; ++tok) { |
1998 | 0 | for (spanp = delim; *spanp; ++spanp) { |
1999 | | /* found delimiter */ |
2000 | 0 | if (*tok == *spanp) { |
2001 | 0 | *tok = '\0'; /* replace delim with null term */ |
2002 | 0 | *stringp = tok + 1; /* return past delim */ |
2003 | 0 | return s; |
2004 | 0 | } |
2005 | 0 | } |
2006 | 0 | } |
2007 | | |
2008 | 0 | *stringp = NULL; |
2009 | 0 | return s; |
2010 | 0 | } |
2011 | | #endif /* USE_WOLF_STRSEP */ |
2012 | | |
2013 | | #ifdef USE_WOLF_STRLCPY |
2014 | | size_t wc_strlcpy(char *dst, const char *src, size_t dstSize) |
2015 | 0 | { |
2016 | 0 | size_t i = 0; |
2017 | |
|
2018 | 0 | if (dstSize != 0) { |
2019 | | /* Always have to leave a space for NULL */ |
2020 | 0 | for (; i < (dstSize - 1) && *src != '\0'; i++) { |
2021 | 0 | *dst++ = *src++; |
2022 | 0 | } |
2023 | 0 | *dst = '\0'; |
2024 | 0 | } |
2025 | | |
2026 | | /* strlcpy() returns the length of src, not the number of bytes copied, so |
2027 | | * that a caller can detect truncation with (ret >= dstSize). Walk whatever |
2028 | | * did not fit -- src already points at the first byte not copied, and at |
2029 | | * the whole string when dstSize was 0 (which writes nothing). */ |
2030 | 0 | while (*src != '\0') { |
2031 | 0 | i++; |
2032 | 0 | src++; |
2033 | 0 | } |
2034 | |
|
2035 | 0 | return i; /* length of src, excluding the NULL */ |
2036 | 0 | } |
2037 | | #endif /* USE_WOLF_STRLCPY */ |
2038 | | |
2039 | | #ifdef USE_WOLF_STRLCAT |
2040 | | size_t wc_strlcat(char *dst, const char *src, size_t dstSize) |
2041 | 0 | { |
2042 | 0 | size_t dstLen = 0; |
2043 | | |
2044 | | /* Find the end of dst without going past dstSize. XSTRLEN() would run off |
2045 | | * the end of a dst that holds no NUL within dstSize -- the very case this |
2046 | | * bound exists to contain. */ |
2047 | 0 | while (dstLen < dstSize && dst[dstLen] != '\0') { |
2048 | 0 | dstLen++; |
2049 | 0 | } |
2050 | |
|
2051 | 0 | if (dstLen == dstSize) { |
2052 | | /* No NUL within dstSize: the length of dst is taken to be dstSize, |
2053 | | * nothing is appended, and dst is left un-terminated because there is |
2054 | | * no room for the NUL. Only reachable when dstSize is wrong or dst is |
2055 | | * not a C string; returning here is what stops the append from running |
2056 | | * off the end. */ |
2057 | 0 | return dstSize + XSTRLEN(src); |
2058 | 0 | } |
2059 | | |
2060 | | /* Total length attempted: the initial length of dst plus the length of |
2061 | | * src, which is what wc_strlcpy() returns. */ |
2062 | 0 | return dstLen + wc_strlcpy(dst + dstLen, src, dstSize - dstLen); |
2063 | 0 | } |
2064 | | #endif /* USE_WOLF_STRLCAT */ |
2065 | | |
2066 | | #ifdef USE_WOLF_STRCASECMP |
2067 | | int wc_strcasecmp(const char *s1, const char *s2) |
2068 | | { |
2069 | | char c1, c2; |
2070 | | for (;;++s1, ++s2) { |
2071 | | c1 = *s1; |
2072 | | if ((c1 >= 'a') && (c1 <= 'z')) |
2073 | | c1 = (char)(c1 - ('a' - 'A')); |
2074 | | c2 = *s2; |
2075 | | if ((c2 >= 'a') && (c2 <= 'z')) |
2076 | | c2 = (char)(c2 - ('a' - 'A')); |
2077 | | if ((c1 != c2) || (c1 == 0)) |
2078 | | break; |
2079 | | } |
2080 | | return (c1 - c2); |
2081 | | } |
2082 | | #endif /* USE_WOLF_STRCASECMP */ |
2083 | | |
2084 | | #ifdef USE_WOLF_STRNCASECMP |
2085 | | int wc_strncasecmp(const char *s1, const char *s2, size_t n) |
2086 | | { |
2087 | | char c1, c2; |
2088 | | for (c1 = 0, c2 = 0; n > 0; --n, ++s1, ++s2) { |
2089 | | c1 = *s1; |
2090 | | if ((c1 >= 'a') && (c1 <= 'z')) |
2091 | | c1 = (char)(c1 - ('a' - 'A')); |
2092 | | c2 = *s2; |
2093 | | if ((c2 >= 'a') && (c2 <= 'z')) |
2094 | | c2 = (char)(c2 - ('a' - 'A')); |
2095 | | if ((c1 != c2) || (c1 == 0)) |
2096 | | break; |
2097 | | } |
2098 | | return (c1 - c2); |
2099 | | } |
2100 | | #endif /* USE_WOLF_STRNCASECMP */ |
2101 | | |
2102 | | #ifdef USE_WOLF_STRDUP |
2103 | 0 | char* wc_strdup_ex(const char *src, int memType) { |
2104 | 0 | char *ret = NULL; |
2105 | 0 | word32 len = 0; |
2106 | |
|
2107 | 0 | if (src) { |
2108 | 0 | len = (word32)XSTRLEN(src) + 1; /* Add one for null terminator */ |
2109 | 0 | ret = (char*)XMALLOC(len, NULL, memType); |
2110 | 0 | if (ret != NULL) { |
2111 | 0 | XMEMCPY(ret, src, len); |
2112 | 0 | } |
2113 | 0 | } |
2114 | |
|
2115 | 0 | return ret; |
2116 | 0 | } |
2117 | | #endif |
2118 | | |
2119 | | #ifdef WOLFSSL_WIDE_BYTE |
2120 | | |
2121 | | /* Packed octet stream -> one octet per byte cell. See WC_OCTETS_PER_BYTE in |
2122 | | * types.h. in holds the packed stream, low octet of a cell first, and must not |
2123 | | * overlap out. Returns 0, BAD_FUNC_ARG on NULL, BUFFER_E if either buffer is |
2124 | | * short. */ |
2125 | | int wc_UnpackOctets(byte* out, word32 outSz, const byte* in, word32 inSz, |
2126 | | word32 octetSz) |
2127 | | { |
2128 | | word32 i; |
2129 | | |
2130 | | if ((out == NULL) || (in == NULL)) { |
2131 | | return BAD_FUNC_ARG; |
2132 | | } |
2133 | | if ((outSz < octetSz) || (inSz < WC_PACKED_CELLS(octetSz))) { |
2134 | | return BUFFER_E; |
2135 | | } |
2136 | | |
2137 | | for (i = 0; i < octetSz; i++) { |
2138 | | out[i] = WC_OCTET((word32)in[i / WC_OCTETS_PER_BYTE] >> |
2139 | | ((i % WC_OCTETS_PER_BYTE) * 8)); |
2140 | | } |
2141 | | |
2142 | | return 0; |
2143 | | } |
2144 | | |
2145 | | /* Inverse of wc_UnpackOctets(), for flash or a byte-oriented peripheral. in |
2146 | | * holds one octet per cell and must not overlap out. */ |
2147 | | int wc_PackOctets(byte* out, word32 outSz, const byte* in, word32 inSz, |
2148 | | word32 octetSz) |
2149 | | { |
2150 | | word32 i; |
2151 | | |
2152 | | if ((out == NULL) || (in == NULL)) { |
2153 | | return BAD_FUNC_ARG; |
2154 | | } |
2155 | | if ((outSz < WC_PACKED_CELLS(octetSz)) || (inSz < octetSz)) { |
2156 | | return BUFFER_E; |
2157 | | } |
2158 | | |
2159 | | XMEMSET(out, 0, WC_PACKED_CELLS(octetSz)); /* zero-fill partial tail */ |
2160 | | for (i = 0; i < octetSz; i++) { |
2161 | | out[i / WC_OCTETS_PER_BYTE] |= (byte)((word32)WC_OCTET(in[i]) << |
2162 | | ((i % WC_OCTETS_PER_BYTE) * 8)); |
2163 | | } |
2164 | | |
2165 | | return 0; |
2166 | | } |
2167 | | |
2168 | | #endif /* WOLFSSL_WIDE_BYTE */ |
2169 | | |
2170 | | #ifdef WOLFSSL_ATOMIC_OPS |
2171 | | |
2172 | | #if defined(WOLFSSL_USER_DEFINED_ATOMICS) |
2173 | | |
2174 | | #elif defined(SINGLE_THREADED) |
2175 | | |
2176 | | #elif defined(WOLFSSL_BSDKM) |
2177 | | /* Note: using compiler built-ins like __atomic_fetch_add will technically |
2178 | | * build in FreeBSD kernel, but are not commonly used in FreeBSD kernel and |
2179 | | * might not be safe or portable. |
2180 | | * */ |
2181 | | void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, WC_ATOMIC_INT_ARG i) |
2182 | | { |
2183 | | *c = i; |
2184 | | } |
2185 | | |
2186 | | void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG i) |
2187 | | { |
2188 | | *c = i; |
2189 | | } |
2190 | | |
2191 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, |
2192 | | WC_ATOMIC_INT_ARG i) |
2193 | | { |
2194 | | return atomic_fetchadd_int(c, i); |
2195 | | } |
2196 | | |
2197 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, |
2198 | | WC_ATOMIC_INT_ARG i) |
2199 | | { |
2200 | | return atomic_fetchadd_int(c, -i); |
2201 | | } |
2202 | | |
2203 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, |
2204 | | WC_ATOMIC_INT_ARG i) |
2205 | | { |
2206 | | int val = atomic_fetchadd_int(c, i); |
2207 | | return val + i; |
2208 | | } |
2209 | | |
2210 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, |
2211 | | WC_ATOMIC_INT_ARG i) |
2212 | | { |
2213 | | int val = atomic_fetchadd_int(c, -i); |
2214 | | return val - i; |
2215 | | } |
2216 | | |
2217 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c, |
2218 | | WC_ATOMIC_UINT_ARG i) |
2219 | | { |
2220 | | return atomic_fetchadd_int(c, i); |
2221 | | } |
2222 | | |
2223 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c, |
2224 | | WC_ATOMIC_UINT_ARG i) |
2225 | | { |
2226 | | return atomic_fetchadd_int(c, -i); |
2227 | | } |
2228 | | |
2229 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c, |
2230 | | WC_ATOMIC_UINT_ARG i) |
2231 | | { |
2232 | | WC_ATOMIC_UINT_ARG val = atomic_fetchadd_int(c, i); |
2233 | | return val + i; |
2234 | | } |
2235 | | |
2236 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c, |
2237 | | WC_ATOMIC_UINT_ARG i) |
2238 | | { |
2239 | | unsigned int val = atomic_fetchadd_int(c, -i); |
2240 | | return val - i; |
2241 | | } |
2242 | | |
2243 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_Exchange(wolfSSL_Atomic_Int* c, |
2244 | | WC_ATOMIC_INT_ARG new_i) |
2245 | | { |
2246 | | return atomic_swap_int(c, new_i); |
2247 | | } |
2248 | | |
2249 | | int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, |
2250 | | WC_ATOMIC_INT_ARG *expected_i, |
2251 | | WC_ATOMIC_INT_ARG new_i) |
2252 | | { |
2253 | | u_int exp = (u_int) *expected_i; |
2254 | | int ret = atomic_fcmpset_int(c, &exp, new_i); |
2255 | | *expected_i = (int)exp; |
2256 | | return ret; |
2257 | | } |
2258 | | |
2259 | | int wolfSSL_Atomic_Uint_CompareExchange( |
2260 | | wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG *expected_i, |
2261 | | WC_ATOMIC_UINT_ARG new_i) |
2262 | | { |
2263 | | u_int exp = (u_int)*expected_i; |
2264 | | int ret = atomic_fcmpset_int(c, &exp, new_i); |
2265 | | *expected_i = (unsigned int)exp; |
2266 | | return ret; |
2267 | | } |
2268 | | |
2269 | | int wolfSSL_Atomic_Ptr_CompareExchange( |
2270 | | void * volatile *c, void **expected_ptr, void *new_ptr) |
2271 | | { |
2272 | | uintptr_t exp = (uintptr_t)*expected_ptr; |
2273 | | int ret = atomic_fcmpset_ptr((uintptr_t *)c, &exp, (uintptr_t)new_ptr); |
2274 | | *expected_ptr = (void *)exp; |
2275 | | return ret; |
2276 | | } |
2277 | | |
2278 | | #elif defined(HAVE_C___ATOMIC) && defined(WOLFSSL_HAVE_ATOMIC_H) && \ |
2279 | | !defined(__cplusplus) && !defined(NO_STDATOMIC_H) |
2280 | | |
2281 | | /* Default C Implementation */ |
2282 | | void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, WC_ATOMIC_INT_ARG i) |
2283 | 226k | { |
2284 | 226k | atomic_init(c, i); |
2285 | 226k | } |
2286 | | |
2287 | | void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG i) |
2288 | 0 | { |
2289 | 0 | atomic_init(c, i); |
2290 | 0 | } |
2291 | | |
2292 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, |
2293 | | WC_ATOMIC_INT_ARG i) |
2294 | 589 | { |
2295 | 589 | return atomic_fetch_add_explicit(c, i, memory_order_relaxed); |
2296 | 589 | } |
2297 | | |
2298 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, |
2299 | | WC_ATOMIC_INT_ARG i) |
2300 | 60.9k | { |
2301 | 60.9k | return atomic_fetch_sub_explicit(c, i, memory_order_relaxed); |
2302 | 60.9k | } |
2303 | | |
2304 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, |
2305 | | WC_ATOMIC_INT_ARG i) |
2306 | 0 | { |
2307 | 0 | WC_ATOMIC_INT_ARG ret = |
2308 | 0 | atomic_fetch_add_explicit(c, i, memory_order_relaxed); |
2309 | 0 | return ret + i; |
2310 | 0 | } |
2311 | | |
2312 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, |
2313 | | WC_ATOMIC_INT_ARG i) |
2314 | 0 | { |
2315 | 0 | WC_ATOMIC_INT_ARG ret = |
2316 | 0 | atomic_fetch_sub_explicit(c, i, memory_order_relaxed); |
2317 | 0 | return ret - i; |
2318 | 0 | } |
2319 | | |
2320 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_Exchange(wolfSSL_Atomic_Int* c, |
2321 | | WC_ATOMIC_INT_ARG new_i) |
2322 | 0 | { |
2323 | 0 | return atomic_exchange_explicit(c, new_i, memory_order_seq_cst); |
2324 | 0 | } |
2325 | | |
2326 | | int wolfSSL_Atomic_Int_CompareExchange( |
2327 | | wolfSSL_Atomic_Int* c, WC_ATOMIC_INT_ARG *expected_i, |
2328 | | WC_ATOMIC_INT_ARG new_i) |
2329 | 0 | { |
2330 | | /* For the success path, use full synchronization with barriers -- |
2331 | | * "Sequentially-consistent ordering" -- so that all threads see the same |
2332 | | * "single total modification order of all atomic operations" -- but on |
2333 | | * failure we just need to be sure we acquire the value that changed out |
2334 | | * from under us. |
2335 | | */ |
2336 | 0 | return atomic_compare_exchange_strong_explicit( |
2337 | 0 | c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire); |
2338 | 0 | } |
2339 | | |
2340 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c, |
2341 | | WC_ATOMIC_UINT_ARG i) |
2342 | 0 | { |
2343 | 0 | return atomic_fetch_add_explicit(c, i, memory_order_relaxed); |
2344 | 0 | } |
2345 | | |
2346 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c, |
2347 | | WC_ATOMIC_UINT_ARG i) |
2348 | 0 | { |
2349 | 0 | return atomic_fetch_sub_explicit(c, i, memory_order_relaxed); |
2350 | 0 | } |
2351 | | |
2352 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c, |
2353 | | WC_ATOMIC_UINT_ARG i) |
2354 | 0 | { |
2355 | 0 | WC_ATOMIC_UINT_ARG ret = |
2356 | 0 | atomic_fetch_add_explicit(c, i, memory_order_relaxed); |
2357 | 0 | return ret + i; |
2358 | 0 | } |
2359 | | |
2360 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c, |
2361 | | WC_ATOMIC_UINT_ARG i) |
2362 | 0 | { |
2363 | 0 | WC_ATOMIC_UINT_ARG ret = |
2364 | 0 | atomic_fetch_sub_explicit(c, i, memory_order_relaxed); |
2365 | 0 | return ret - i; |
2366 | 0 | } |
2367 | | |
2368 | | int wolfSSL_Atomic_Uint_CompareExchange( |
2369 | | wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG *expected_i, |
2370 | | WC_ATOMIC_UINT_ARG new_i) |
2371 | 12.3k | { |
2372 | | /* For the success path, use full synchronization with barriers -- |
2373 | | * "Sequentially-consistent ordering" -- so that all threads see the same |
2374 | | * "single total modification order of all atomic operations" -- but on |
2375 | | * failure we just need to be sure we acquire the value that changed out |
2376 | | * from under us. |
2377 | | */ |
2378 | 12.3k | return atomic_compare_exchange_strong_explicit( |
2379 | 12.3k | c, expected_i, new_i, memory_order_seq_cst, memory_order_acquire); |
2380 | 12.3k | } |
2381 | | |
2382 | | int wolfSSL_Atomic_Ptr_CompareExchange( |
2383 | | void * volatile *c, void **expected_ptr, void *new_ptr) |
2384 | 0 | { |
2385 | | /* use gcc-built-in __atomic_compare_exchange_n(), not |
2386 | | * atomic_compare_exchange_strong_explicit(), to sidestep _Atomic type |
2387 | | * requirements. |
2388 | | */ |
2389 | 0 | if (__atomic_compare_exchange_n( |
2390 | 0 | c, expected_ptr, new_ptr, |
2391 | | #ifdef WOLF_C89 |
2392 | | 0 /* weak */, |
2393 | | #else |
2394 | 0 | (_Bool)0 /* weak */, |
2395 | 0 | #endif |
2396 | 0 | __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE)) |
2397 | 0 | return 1; |
2398 | 0 | else |
2399 | 0 | return 0; |
2400 | 0 | } |
2401 | | |
2402 | | #elif defined(__GNUC__) && defined(__ATOMIC_RELAXED) |
2403 | | /* direct calls using gcc-style compiler built-ins */ |
2404 | | |
2405 | | void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, WC_ATOMIC_INT_ARG i) |
2406 | | { |
2407 | | *c = i; |
2408 | | } |
2409 | | |
2410 | | void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG i) |
2411 | | { |
2412 | | *c = i; |
2413 | | } |
2414 | | |
2415 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, |
2416 | | WC_ATOMIC_INT_ARG i) |
2417 | | { |
2418 | | return __atomic_fetch_add(c, i, __ATOMIC_RELAXED); |
2419 | | } |
2420 | | |
2421 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, |
2422 | | WC_ATOMIC_INT_ARG i) |
2423 | | { |
2424 | | return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED); |
2425 | | } |
2426 | | |
2427 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, |
2428 | | WC_ATOMIC_INT_ARG i) |
2429 | | { |
2430 | | return __atomic_add_fetch(c, i, __ATOMIC_RELAXED); |
2431 | | } |
2432 | | |
2433 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, |
2434 | | WC_ATOMIC_INT_ARG i) |
2435 | | { |
2436 | | return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED); |
2437 | | } |
2438 | | |
2439 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_Exchange(wolfSSL_Atomic_Int* c, |
2440 | | WC_ATOMIC_INT_ARG new_i) |
2441 | | { |
2442 | | return __atomic_exchange_n(c, new_i, __ATOMIC_SEQ_CST); |
2443 | | } |
2444 | | |
2445 | | int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, |
2446 | | WC_ATOMIC_INT_ARG *expected_i, |
2447 | | WC_ATOMIC_INT_ARG new_i) |
2448 | | { |
2449 | | /* For the success path, use full synchronization with barriers -- |
2450 | | * "Sequentially-consistent ordering" -- so that all threads see the same |
2451 | | * "single total modification order of all atomic operations" -- but on |
2452 | | * failure we just need to be sure we acquire the value that changed out |
2453 | | * from under us. |
2454 | | */ |
2455 | | return __atomic_compare_exchange_n(c, expected_i, new_i, 0 /* weak */, |
2456 | | __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE); |
2457 | | } |
2458 | | |
2459 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c, |
2460 | | WC_ATOMIC_UINT_ARG i) |
2461 | | { |
2462 | | return __atomic_fetch_add(c, i, __ATOMIC_RELAXED); |
2463 | | } |
2464 | | |
2465 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c, |
2466 | | WC_ATOMIC_UINT_ARG i) |
2467 | | { |
2468 | | return __atomic_fetch_sub(c, i, __ATOMIC_RELAXED); |
2469 | | } |
2470 | | |
2471 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c, |
2472 | | WC_ATOMIC_UINT_ARG i) |
2473 | | { |
2474 | | return __atomic_add_fetch(c, i, __ATOMIC_RELAXED); |
2475 | | } |
2476 | | |
2477 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c, |
2478 | | WC_ATOMIC_UINT_ARG i) |
2479 | | { |
2480 | | return __atomic_sub_fetch(c, i, __ATOMIC_RELAXED); |
2481 | | } |
2482 | | |
2483 | | int wolfSSL_Atomic_Uint_CompareExchange( |
2484 | | wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG *expected_i, |
2485 | | WC_ATOMIC_UINT_ARG new_i) |
2486 | | { |
2487 | | /* For the success path, use full synchronization with barriers -- |
2488 | | * "Sequentially-consistent ordering" -- so that all threads see the same |
2489 | | * "single total modification order of all atomic operations" -- but on |
2490 | | * failure we just need to be sure we acquire the value that changed out |
2491 | | * from under us. |
2492 | | */ |
2493 | | return __atomic_compare_exchange_n( |
2494 | | c, expected_i, new_i, 0 /* weak */, __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE); |
2495 | | } |
2496 | | |
2497 | | int wolfSSL_Atomic_Ptr_CompareExchange( |
2498 | | void * volatile *c, void **expected_ptr, void *new_ptr) |
2499 | | { |
2500 | | return __atomic_compare_exchange_n( |
2501 | | c, expected_ptr, new_ptr, 0 /* weak */, |
2502 | | __ATOMIC_SEQ_CST, __ATOMIC_ACQUIRE); |
2503 | | } |
2504 | | |
2505 | | #elif defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API) |
2506 | | |
2507 | | void wolfSSL_Atomic_Int_Init(wolfSSL_Atomic_Int* c, WC_ATOMIC_INT_ARG i) |
2508 | | { |
2509 | | *c = i; |
2510 | | } |
2511 | | |
2512 | | void wolfSSL_Atomic_Uint_Init(wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG i) |
2513 | | { |
2514 | | *c = i; |
2515 | | } |
2516 | | |
2517 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchAdd(wolfSSL_Atomic_Int* c, |
2518 | | WC_ATOMIC_INT_ARG i) |
2519 | | { |
2520 | | return (int)_InterlockedExchangeAdd(c, (long)i); |
2521 | | } |
2522 | | |
2523 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_FetchSub(wolfSSL_Atomic_Int* c, |
2524 | | WC_ATOMIC_INT_ARG i) |
2525 | | { |
2526 | | return (int)_InterlockedExchangeAdd(c, (long)-i); |
2527 | | } |
2528 | | |
2529 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_AddFetch(wolfSSL_Atomic_Int* c, |
2530 | | WC_ATOMIC_INT_ARG i) |
2531 | | { |
2532 | | WC_ATOMIC_INT_ARG ret = |
2533 | | (WC_ATOMIC_INT_ARG)_InterlockedExchangeAdd(c, (long)i); |
2534 | | return ret + i; |
2535 | | } |
2536 | | |
2537 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_SubFetch(wolfSSL_Atomic_Int* c, |
2538 | | WC_ATOMIC_INT_ARG i) |
2539 | | { |
2540 | | WC_ATOMIC_INT_ARG ret = |
2541 | | (WC_ATOMIC_INT_ARG)_InterlockedExchangeAdd(c, (long)-i); |
2542 | | return ret - i; |
2543 | | } |
2544 | | |
2545 | | WC_ATOMIC_INT_ARG wolfSSL_Atomic_Int_Exchange(wolfSSL_Atomic_Int* c, |
2546 | | WC_ATOMIC_INT_ARG new_i) |
2547 | | { |
2548 | | long actual_i = InterlockedExchange(c, (long)new_i); |
2549 | | return (WC_ATOMIC_INT_ARG)actual_i; |
2550 | | } |
2551 | | |
2552 | | int wolfSSL_Atomic_Int_CompareExchange(wolfSSL_Atomic_Int* c, |
2553 | | WC_ATOMIC_INT_ARG *expected_i, |
2554 | | WC_ATOMIC_INT_ARG new_i) |
2555 | | { |
2556 | | long actual_i = InterlockedCompareExchange(c, (long)new_i, |
2557 | | (long)*expected_i); |
2558 | | if (actual_i == (long)*expected_i) { |
2559 | | return 1; |
2560 | | } |
2561 | | else { |
2562 | | *expected_i = (WC_ATOMIC_INT_ARG)actual_i; |
2563 | | return 0; |
2564 | | } |
2565 | | } |
2566 | | |
2567 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchAdd(wolfSSL_Atomic_Uint* c, |
2568 | | WC_ATOMIC_UINT_ARG i) |
2569 | | { |
2570 | | return (WC_ATOMIC_UINT_ARG)_InterlockedExchangeAdd((wolfSSL_Atomic_Int *)c, |
2571 | | (long)i); |
2572 | | } |
2573 | | |
2574 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_FetchSub(wolfSSL_Atomic_Uint* c, |
2575 | | WC_ATOMIC_UINT_ARG i) |
2576 | | { |
2577 | | return (WC_ATOMIC_UINT_ARG)_InterlockedExchangeAdd((wolfSSL_Atomic_Int *)c, |
2578 | | -(long)i); |
2579 | | } |
2580 | | |
2581 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_AddFetch(wolfSSL_Atomic_Uint* c, |
2582 | | WC_ATOMIC_UINT_ARG i) |
2583 | | { |
2584 | | WC_ATOMIC_UINT_ARG ret = (WC_ATOMIC_UINT_ARG)_InterlockedExchangeAdd |
2585 | | ((wolfSSL_Atomic_Int *)c, (long)i); |
2586 | | return ret + i; |
2587 | | } |
2588 | | |
2589 | | WC_ATOMIC_UINT_ARG wolfSSL_Atomic_Uint_SubFetch(wolfSSL_Atomic_Uint* c, |
2590 | | WC_ATOMIC_UINT_ARG i) |
2591 | | { |
2592 | | WC_ATOMIC_UINT_ARG ret = (WC_ATOMIC_UINT_ARG)_InterlockedExchangeAdd |
2593 | | ((wolfSSL_Atomic_Int *)c, -(long)i); |
2594 | | return ret - i; |
2595 | | } |
2596 | | |
2597 | | int wolfSSL_Atomic_Uint_CompareExchange( |
2598 | | wolfSSL_Atomic_Uint* c, WC_ATOMIC_UINT_ARG *expected_i, |
2599 | | WC_ATOMIC_UINT_ARG new_i) |
2600 | | { |
2601 | | long actual_i = InterlockedCompareExchange( |
2602 | | (wolfSSL_Atomic_Int *)c, (long)new_i, (long)*expected_i); |
2603 | | if (actual_i == (long)*expected_i) { |
2604 | | return 1; |
2605 | | } |
2606 | | else { |
2607 | | *expected_i = (WC_ATOMIC_UINT_ARG)actual_i; |
2608 | | return 0; |
2609 | | } |
2610 | | } |
2611 | | |
2612 | | int wolfSSL_Atomic_Ptr_CompareExchange( |
2613 | | void * volatile * c, void **expected_ptr, void *new_ptr) |
2614 | | { |
2615 | | #ifdef _WIN64 |
2616 | | LONG64 actual_ptr = InterlockedCompareExchange64( |
2617 | | (LONG64 *)c, (LONG64)new_ptr, (LONG64)*expected_ptr); |
2618 | | if (actual_ptr == (LONG64)*expected_ptr) { |
2619 | | return 1; |
2620 | | } |
2621 | | else { |
2622 | | *expected_ptr = (void *)actual_ptr; |
2623 | | return 0; |
2624 | | } |
2625 | | #else /* !_WIN64 */ |
2626 | | LONG actual_ptr = InterlockedCompareExchange( |
2627 | | (LONG *)c, (LONG)new_ptr, (LONG)*expected_ptr); |
2628 | | if (actual_ptr == (LONG)*expected_ptr) { |
2629 | | return 1; |
2630 | | } |
2631 | | else { |
2632 | | *expected_ptr = (void *)actual_ptr; |
2633 | | return 0; |
2634 | | } |
2635 | | #endif /* !_WIN64 */ |
2636 | | } |
2637 | | |
2638 | | #endif |
2639 | | |
2640 | | #endif /* WOLFSSL_ATOMIC_OPS */ |
2641 | | |
2642 | | #if !defined(SINGLE_THREADED) |
2643 | | |
2644 | | void wolfSSL_RefWithMutexInit(wolfSSL_RefWithMutex* ref, int* err) |
2645 | 6.18k | { |
2646 | 6.18k | int ret = wc_InitMutex(&ref->mutex); |
2647 | 6.18k | if (ret != 0) { |
2648 | 0 | WOLFSSL_MSG("Failed to create mutex for reference counting!"); |
2649 | 0 | } |
2650 | 6.18k | ref->count = 1; |
2651 | | |
2652 | 6.18k | *err = ret; |
2653 | 6.18k | } |
2654 | | |
2655 | | void wolfSSL_RefWithMutexFree(wolfSSL_RefWithMutex* ref) |
2656 | 6.14k | { |
2657 | 6.14k | if (wc_FreeMutex(&ref->mutex) != 0) { |
2658 | 0 | WOLFSSL_MSG("Failed to free mutex of reference counting!"); |
2659 | 0 | } |
2660 | 6.14k | ref->count = 0; |
2661 | 6.14k | } |
2662 | | |
2663 | | void wolfSSL_RefWithMutexInc(wolfSSL_RefWithMutex* ref, int* err) |
2664 | 50.4k | { |
2665 | 50.4k | int ret = wc_LockMutex(&ref->mutex); |
2666 | 50.4k | if (ret != 0) { |
2667 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference increment!"); |
2668 | 0 | } |
2669 | 50.4k | else { |
2670 | 50.4k | ref->count++; |
2671 | 50.4k | wc_UnLockMutex(&ref->mutex); |
2672 | 50.4k | } |
2673 | 50.4k | *err = ret; |
2674 | 50.4k | } |
2675 | | |
2676 | | void wolfSSL_RefWithMutexInc2(wolfSSL_RefWithMutex* ref, int *new_count, |
2677 | | int* err) |
2678 | 0 | { |
2679 | 0 | int ret = wc_LockMutex(&ref->mutex); |
2680 | 0 | if (ret != 0) { |
2681 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference increment!"); |
2682 | 0 | *new_count = -1; |
2683 | 0 | } |
2684 | 0 | else { |
2685 | 0 | *new_count = ++ref->count; |
2686 | 0 | wc_UnLockMutex(&ref->mutex); |
2687 | 0 | } |
2688 | 0 | *err = ret; |
2689 | 0 | } |
2690 | | |
2691 | | void wolfSSL_RefWithMutexInc_IfAtLeast(wolfSSL_RefWithMutex* ref, |
2692 | | int cur_at_least, int *new_count, |
2693 | | int* err) |
2694 | 0 | { |
2695 | 0 | *err = wc_LockMutex(&ref->mutex); |
2696 | 0 | if (*err != 0) { |
2697 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference increment!"); |
2698 | 0 | *new_count = -1; |
2699 | 0 | } |
2700 | 0 | else { |
2701 | 0 | if (ref->count < cur_at_least) { |
2702 | 0 | *new_count = ref->count; |
2703 | 0 | *err = BAD_STATE_E; |
2704 | 0 | } |
2705 | 0 | else { |
2706 | 0 | *new_count = ++ref->count; |
2707 | 0 | *err = 0; |
2708 | 0 | } |
2709 | 0 | wc_UnLockMutex(&ref->mutex); |
2710 | 0 | } |
2711 | 0 | } |
2712 | | |
2713 | | int wolfSSL_RefWithMutexLock(wolfSSL_RefWithMutex* ref) |
2714 | 50.4k | { |
2715 | 50.4k | return wc_LockMutex(&ref->mutex); |
2716 | 50.4k | } |
2717 | | |
2718 | | int wolfSSL_RefWithMutexUnlock(wolfSSL_RefWithMutex* ref) |
2719 | 50.4k | { |
2720 | 50.4k | return wc_UnLockMutex(&ref->mutex); |
2721 | 50.4k | } |
2722 | | |
2723 | | void wolfSSL_RefWithMutexDec(wolfSSL_RefWithMutex* ref, int* isZero, int* err) |
2724 | 56.6k | { |
2725 | 56.6k | int ret = wc_LockMutex(&ref->mutex); |
2726 | 56.6k | if (ret != 0) { |
2727 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference decrement!"); |
2728 | | /* Can't say count is zero. */ |
2729 | 0 | *isZero = 0; |
2730 | 0 | } |
2731 | 56.6k | else { |
2732 | 56.6k | if (ref->count > 0) { |
2733 | 56.6k | ref->count--; |
2734 | 56.6k | } |
2735 | 56.6k | *isZero = (ref->count == 0); |
2736 | 56.6k | wc_UnLockMutex(&ref->mutex); |
2737 | 56.6k | } |
2738 | 56.6k | *err = ret; |
2739 | 56.6k | } |
2740 | | |
2741 | | void wolfSSL_RefWithMutexDec2(wolfSSL_RefWithMutex* ref, int* new_count, |
2742 | | int* err) |
2743 | 0 | { |
2744 | 0 | int ret = wc_LockMutex(&ref->mutex); |
2745 | 0 | if (ret != 0) { |
2746 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference decrement!"); |
2747 | 0 | *new_count = -1; |
2748 | 0 | } |
2749 | 0 | else { |
2750 | 0 | if (ref->count > 0) { |
2751 | 0 | ref->count--; |
2752 | 0 | } |
2753 | 0 | *new_count = ref->count; |
2754 | 0 | wc_UnLockMutex(&ref->mutex); |
2755 | 0 | } |
2756 | 0 | *err = ret; |
2757 | 0 | } |
2758 | | |
2759 | | void wolfSSL_RefWithMutexDec_IfEquals(wolfSSL_RefWithMutex* ref, |
2760 | | int current_count, int* new_count, |
2761 | | int* err) |
2762 | 0 | { |
2763 | 0 | *err = wc_LockMutex(&ref->mutex); |
2764 | 0 | if (*err != 0) { |
2765 | 0 | WOLFSSL_MSG("Failed to lock mutex for reference decrement!"); |
2766 | 0 | *new_count = -1; |
2767 | 0 | } |
2768 | 0 | else { |
2769 | 0 | if (ref->count != current_count) { |
2770 | 0 | *new_count = ref->count; |
2771 | 0 | *err = BAD_STATE_E; |
2772 | 0 | } |
2773 | 0 | else { |
2774 | 0 | *new_count = --ref->count; |
2775 | 0 | *err = 0; |
2776 | 0 | } |
2777 | 0 | wc_UnLockMutex(&ref->mutex); |
2778 | 0 | } |
2779 | 0 | } |
2780 | | |
2781 | | #endif /* ! SINGLE_THREADED */ |
2782 | | |
2783 | | #if WOLFSSL_CRYPT_HW_MUTEX |
2784 | | /* Mutex for protection of cryptography hardware */ |
2785 | | static wolfSSL_Mutex wcCryptHwMutex |
2786 | | WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wcCryptHwMutex); |
2787 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
2788 | | static int wcCryptHwMutexInit = 0; |
2789 | | #endif |
2790 | | |
2791 | | int wolfSSL_CryptHwMutexInit(void) |
2792 | | { |
2793 | | int ret = 0; |
2794 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
2795 | | if (wcCryptHwMutexInit == 0) { |
2796 | | ret = wc_InitMutex(&wcCryptHwMutex); |
2797 | | if (ret == 0) { |
2798 | | wcCryptHwMutexInit = 1; |
2799 | | } |
2800 | | } |
2801 | | #endif |
2802 | | return ret; |
2803 | | } |
2804 | | int wolfSSL_CryptHwMutexLock(void) |
2805 | | { |
2806 | | /* Make sure HW Mutex has been initialized */ |
2807 | | int ret = wolfSSL_CryptHwMutexInit(); |
2808 | | if (ret == 0) { |
2809 | | ret = wc_LockMutex(&wcCryptHwMutex); |
2810 | | } |
2811 | | return ret; |
2812 | | } |
2813 | | int wolfSSL_CryptHwMutexUnLock(void) |
2814 | | { |
2815 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
2816 | | if (wcCryptHwMutexInit) { |
2817 | | return wc_UnLockMutex(&wcCryptHwMutex); |
2818 | | } |
2819 | | else { |
2820 | | return BAD_MUTEX_E; |
2821 | | } |
2822 | | #else |
2823 | | /* statically initialized (no runtime init flag) -- always valid to unlock */ |
2824 | | return wc_UnLockMutex(&wcCryptHwMutex); |
2825 | | #endif |
2826 | | } |
2827 | | #endif /* WOLFSSL_CRYPT_HW_MUTEX */ |
2828 | | |
2829 | | |
2830 | | #if WOLFSSL_CRYPT_HW_MUTEX && defined(WOLFSSL_ALGO_HW_MUTEX) |
2831 | | /* Mutex for protection of cryptography hardware */ |
2832 | | #ifndef NO_RNG_MUTEX |
2833 | | static wolfSSL_Mutex wcCryptHwRngMutex |
2834 | | WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wcCryptHwRngMutex); |
2835 | | #endif /* NO_RNG_MUTEX */ |
2836 | | #ifndef NO_AES_MUTEX |
2837 | | static wolfSSL_Mutex wcCryptHwAesMutex |
2838 | | WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wcCryptHwAesMutex); |
2839 | | #endif /* NO_AES_MUTEX */ |
2840 | | #ifndef NO_HASH_MUTEX |
2841 | | static wolfSSL_Mutex wcCryptHwHashMutex |
2842 | | WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wcCryptHwHashMutex); |
2843 | | #endif /* NO_HASH_MUTEX */ |
2844 | | #ifndef NO_PK_MUTEX |
2845 | | static wolfSSL_Mutex wcCryptHwPkMutex |
2846 | | WOLFSSL_MUTEX_INITIALIZER_CLAUSE(wcCryptHwPkMutex); |
2847 | | #endif /* NO_PK_MUTEX */ |
2848 | | |
2849 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
2850 | | #ifndef NO_RNG_MUTEX |
2851 | | static int wcCryptHwRngMutexInit = 0; |
2852 | | #endif /* NO_RNG_MUTEX */ |
2853 | | #ifndef NO_AES_MUTEX |
2854 | | static int wcCryptHwAesMutexInit = 0; |
2855 | | #endif /* NO_AES_MUTEX */ |
2856 | | #ifndef NO_HASH_MUTEX |
2857 | | static int wcCryptHwHashMutexInit = 0; |
2858 | | #endif /* NO_HASH_MUTEX */ |
2859 | | #ifndef NO_PK_MUTEX |
2860 | | static int wcCryptHwPkMutexInit = 0; |
2861 | | #endif /* NO_PK_MUTEX */ |
2862 | | #endif /* WOLFSSL_MUTEX_INITIALIZER */ |
2863 | | |
2864 | | |
2865 | | /* Allows ability to switch to different mutex based on enum type */ |
2866 | | /* hw_mutex_algo, expects the dereferenced Ptrs to be set to NULL */ |
2867 | | static int hwAlgoPtrSet(hw_mutex_algo hwAlgo, wolfSSL_Mutex** wcHwAlgoMutexPtr, |
2868 | | int** wcHwAlgoInitPtr) |
2869 | | { |
2870 | | if (*wcHwAlgoMutexPtr != NULL || *wcHwAlgoInitPtr != NULL) { |
2871 | | return BAD_FUNC_ARG; |
2872 | | } |
2873 | | switch (hwAlgo) { |
2874 | | #ifndef NO_RNG_MUTEX |
2875 | | case rng_mutex: |
2876 | | *wcHwAlgoMutexPtr = &wcCryptHwRngMutex; |
2877 | | *wcHwAlgoInitPtr = &wcCryptHwRngMutexInit; |
2878 | | break; |
2879 | | #endif |
2880 | | #ifndef NO_AES_MUTEX |
2881 | | case aes_mutex: |
2882 | | *wcHwAlgoMutexPtr = &wcCryptHwAesMutex; |
2883 | | *wcHwAlgoInitPtr = &wcCryptHwAesMutexInit; |
2884 | | break; |
2885 | | #endif |
2886 | | #ifndef NO_HASH_MUTEX |
2887 | | case hash_mutex: |
2888 | | *wcHwAlgoMutexPtr = &wcCryptHwHashMutex; |
2889 | | *wcHwAlgoInitPtr = &wcCryptHwHashMutexInit; |
2890 | | break; |
2891 | | #endif |
2892 | | #ifndef NO_PK_MUTEX |
2893 | | case pk_mutex: |
2894 | | *wcHwAlgoMutexPtr = &wcCryptHwPkMutex; |
2895 | | *wcHwAlgoInitPtr = &wcCryptHwPkMutexInit; |
2896 | | break; |
2897 | | #endif |
2898 | | default: |
2899 | | return BAD_FUNC_ARG; |
2900 | | } |
2901 | | return 0; |
2902 | | } |
2903 | | |
2904 | | static int hwAlgoMutexInit(hw_mutex_algo hwAlgo) |
2905 | | { |
2906 | | int ret = 0; |
2907 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
2908 | | wolfSSL_Mutex* wcHwAlgoMutexPtr = NULL; |
2909 | | int* wcHwAlgoInitPtr = NULL; |
2910 | | ret = hwAlgoPtrSet(hwAlgo, &wcHwAlgoMutexPtr, &wcHwAlgoInitPtr); |
2911 | | if (ret != 0) { |
2912 | | return ret; |
2913 | | } |
2914 | | if (*wcHwAlgoInitPtr == 0) { |
2915 | | ret = wc_InitMutex(wcHwAlgoMutexPtr); |
2916 | | if (ret == 0) { |
2917 | | *wcHwAlgoInitPtr = 1; |
2918 | | } |
2919 | | } |
2920 | | #endif |
2921 | | return ret; |
2922 | | } |
2923 | | |
2924 | | static int hwAlgoMutexLock(hw_mutex_algo hwAlgo) |
2925 | | { |
2926 | | /* Make sure HW Mutex has been initialized */ |
2927 | | int ret = 0; |
2928 | | wolfSSL_Mutex* wcHwAlgoMutexPtr = NULL; |
2929 | | int* wcHwAlgoInitPtr = NULL; |
2930 | | ret = hwAlgoPtrSet(hwAlgo, &wcHwAlgoMutexPtr, &wcHwAlgoInitPtr); |
2931 | | if (ret != 0) { |
2932 | | return ret; |
2933 | | } |
2934 | | ret = hwAlgoMutexInit(hwAlgo); |
2935 | | if (ret == 0) { |
2936 | | ret = wc_LockMutex(wcHwAlgoMutexPtr); |
2937 | | } |
2938 | | return ret; |
2939 | | } |
2940 | | |
2941 | | static int hwAlgoMutexUnLock(hw_mutex_algo hwAlgo) |
2942 | | { |
2943 | | wolfSSL_Mutex* wcHwAlgoMutexPtr = NULL; |
2944 | | int* wcHwAlgoInitPtr = NULL; |
2945 | | if (hwAlgoPtrSet(hwAlgo, &wcHwAlgoMutexPtr, &wcHwAlgoInitPtr) != 0) { |
2946 | | return BAD_FUNC_ARG; |
2947 | | } |
2948 | | if (*wcHwAlgoInitPtr) { |
2949 | | return wc_UnLockMutex(wcHwAlgoMutexPtr); |
2950 | | } |
2951 | | else { |
2952 | | return BAD_MUTEX_E; |
2953 | | } |
2954 | | } |
2955 | | |
2956 | | /* Wrap around generic hwAlgo* functions and use correct */ |
2957 | | /* global mutex to determine if it can be unlocked/locked */ |
2958 | | #ifndef NO_RNG_MUTEX |
2959 | | int wolfSSL_HwRngMutexInit(void) |
2960 | | { |
2961 | | return hwAlgoMutexInit(rng_mutex); |
2962 | | } |
2963 | | int wolfSSL_HwRngMutexLock(void) |
2964 | | { |
2965 | | return hwAlgoMutexLock(rng_mutex); |
2966 | | } |
2967 | | int wolfSSL_HwRngMutexUnLock(void) |
2968 | | { |
2969 | | return hwAlgoMutexUnLock(rng_mutex); |
2970 | | } |
2971 | | #endif /* NO_RNG_MUTEX */ |
2972 | | |
2973 | | #ifndef NO_AES_MUTEX |
2974 | | int wolfSSL_HwAesMutexInit(void) |
2975 | | { |
2976 | | return hwAlgoMutexInit(aes_mutex); |
2977 | | } |
2978 | | int wolfSSL_HwAesMutexLock(void) |
2979 | | { |
2980 | | return hwAlgoMutexLock(aes_mutex); |
2981 | | } |
2982 | | int wolfSSL_HwAesMutexUnLock(void) |
2983 | | { |
2984 | | return hwAlgoMutexUnLock(aes_mutex); |
2985 | | } |
2986 | | #endif /* NO_AES_MUTEX */ |
2987 | | |
2988 | | #ifndef NO_HASH_MUTEX |
2989 | | int wolfSSL_HwHashMutexInit(void) |
2990 | | { |
2991 | | return hwAlgoMutexInit(hash_mutex); |
2992 | | } |
2993 | | int wolfSSL_HwHashMutexLock(void) |
2994 | | { |
2995 | | return hwAlgoMutexLock(hash_mutex); |
2996 | | } |
2997 | | int wolfSSL_HwHashMutexUnLock(void) |
2998 | | { |
2999 | | return hwAlgoMutexUnLock(hash_mutex); |
3000 | | } |
3001 | | #endif /* NO_HASH_MUTEX */ |
3002 | | |
3003 | | #ifndef NO_PK_MUTEX |
3004 | | int wolfSSL_HwPkMutexInit(void) |
3005 | | { |
3006 | | return hwAlgoMutexInit(pk_mutex); |
3007 | | } |
3008 | | int wolfSSL_HwPkMutexLock(void) |
3009 | | { |
3010 | | return hwAlgoMutexLock(pk_mutex); |
3011 | | } |
3012 | | int wolfSSL_HwPkMutexUnLock(void) |
3013 | | { |
3014 | | return hwAlgoMutexUnLock(pk_mutex); |
3015 | | } |
3016 | | #endif /* NO_PK_MUTEX */ |
3017 | | |
3018 | | #endif /* WOLFSSL_CRYPT_HW_MUTEX && defined(WOLFSSL_ALGO_HW_MUTEX) */ |
3019 | | |
3020 | | /* ---------------------------------------------------------------------------*/ |
3021 | | /* Mutex Ports */ |
3022 | | /* ---------------------------------------------------------------------------*/ |
3023 | | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
3024 | | static mutex_cb* compat_mutex_cb = NULL; |
3025 | | |
3026 | | /* Function that locks or unlocks a mutex based on the flag passed in. |
3027 | | * |
3028 | | * flag lock or unlock i.e. CRYPTO_LOCK |
3029 | | * type the type of lock to unlock or lock |
3030 | | * file name of the file calling |
3031 | | * line the line number from file calling |
3032 | | */ |
3033 | | int wc_LockMutex_ex(int flag, int type, const char* file, int line) |
3034 | | { |
3035 | | if (compat_mutex_cb != NULL) { |
3036 | | compat_mutex_cb(flag, type, file, line); |
3037 | | return 0; |
3038 | | } |
3039 | | else { |
3040 | | WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb"); |
3041 | | return BAD_STATE_E; |
3042 | | } |
3043 | | } |
3044 | | |
3045 | | |
3046 | | /* Set the callback function to use for locking/unlocking mutex |
3047 | | * |
3048 | | * cb callback function to use |
3049 | | */ |
3050 | | int wc_SetMutexCb(mutex_cb* cb) |
3051 | | { |
3052 | | compat_mutex_cb = cb; |
3053 | | return 0; |
3054 | | } |
3055 | | |
3056 | | /* Gets the current callback function in use for locking/unlocking mutex |
3057 | | * |
3058 | | */ |
3059 | | mutex_cb* wc_GetMutexCb(void) |
3060 | | { |
3061 | | return compat_mutex_cb; |
3062 | | } |
3063 | | #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */ |
3064 | | |
3065 | | #ifndef WOLFSSL_MUTEX_INITIALIZER |
3066 | | /* Initialize a static mutex exactly once. |
3067 | | * |
3068 | | * Backstop for callers that reach a subsystem without wolfCrypt_Init(). One |
3069 | | * atomic with three states: 0 = uninitialized, 1 = in progress, 2 = ready. |
3070 | | * Losers of the race wait for the winner rather than failing, so callers do |
3071 | | * not need a retry path. Same pattern as the SP ECC cache locks. |
3072 | | * |
3073 | | * @param [in] m Mutex to initialize. |
3074 | | * @param [in, out] flag Election state, statically zero initialized. |
3075 | | * @return 0 on success, or when the mutex is already initialized. |
3076 | | * @return Error from wc_InitMutex() otherwise. |
3077 | | */ |
3078 | | int wc_local_InitMutexOnce(wolfSSL_Mutex* m, wc_MutexOnceFlag* flag) |
3079 | | { |
3080 | | int ret = 0; |
3081 | | #if defined(WOLFSSL_ATOMIC_OPS) && !defined(SINGLE_THREADED) |
3082 | | WC_ATOMIC_UINT_ARG expected; |
3083 | | |
3084 | | if (WOLFSSL_ATOMIC_LOAD(*flag) == 2) { |
3085 | | return 0; |
3086 | | } |
3087 | | |
3088 | | for (;;) { |
3089 | | expected = 0; |
3090 | | if (wolfSSL_Atomic_Uint_CompareExchange(flag, &expected, 1) == 1) { |
3091 | | /* Won the race. On failure reset to 0 so a later call retries. */ |
3092 | | ret = wc_InitMutex(m); |
3093 | | WOLFSSL_ATOMIC_STORE(*flag, (ret == 0) ? 2U : 0U); |
3094 | | break; |
3095 | | } |
3096 | | if (expected == 2) { |
3097 | | /* Another thread completed the initialization. */ |
3098 | | break; |
3099 | | } |
3100 | | /* Initialization in progress in another thread. */ |
3101 | | WC_RELAX_LONG_LOOP(); |
3102 | | } |
3103 | | #else |
3104 | | /* No atomics to elect with: single-threaded cannot race, and opt-out |
3105 | | * builds keep their existing behavior. */ |
3106 | | if (*flag != 2) { |
3107 | | ret = wc_InitMutex(m); |
3108 | | if (ret == 0) { |
3109 | | *flag = 2; |
3110 | | } |
3111 | | } |
3112 | | #endif |
3113 | | return ret; |
3114 | | } |
3115 | | #endif /* !WOLFSSL_MUTEX_INITIALIZER */ |
3116 | | |
3117 | | #if defined(WC_MUTEX_OPS_INLINE) |
3118 | | |
3119 | | /* defined in headers */ |
3120 | | |
3121 | | #elif defined(SINGLE_THREADED) |
3122 | | |
3123 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3124 | | { |
3125 | | (void)m; |
3126 | | return 0; |
3127 | | } |
3128 | | |
3129 | | int wc_FreeMutex(wolfSSL_Mutex *m) |
3130 | | { |
3131 | | (void)m; |
3132 | | return 0; |
3133 | | } |
3134 | | |
3135 | | |
3136 | | int wc_LockMutex(wolfSSL_Mutex *m) |
3137 | | { |
3138 | | (void)m; |
3139 | | return 0; |
3140 | | } |
3141 | | |
3142 | | |
3143 | | int wc_UnLockMutex(wolfSSL_Mutex *m) |
3144 | | { |
3145 | | (void)m; |
3146 | | return 0; |
3147 | | } |
3148 | | |
3149 | | #elif defined(__WATCOMC__) |
3150 | | |
3151 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3152 | | { |
3153 | | #ifdef __OS2__ |
3154 | | DosCreateMutexSem( NULL, m, 0, FALSE ); |
3155 | | #elif defined(__NT__) |
3156 | | InitializeCriticalSection(m); |
3157 | | #elif defined(__LINUX__) |
3158 | | if (pthread_mutex_init(m, NULL) ) |
3159 | | return BAD_MUTEX_E; |
3160 | | #endif |
3161 | | return 0; |
3162 | | } |
3163 | | |
3164 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3165 | | { |
3166 | | #ifdef __OS2__ |
3167 | | DosCloseMutexSem(*m); |
3168 | | #elif defined(__NT__) |
3169 | | DeleteCriticalSection(m); |
3170 | | #elif defined(__LINUX__) |
3171 | | if (pthread_mutex_destroy(m) ) |
3172 | | return BAD_MUTEX_E; |
3173 | | #endif |
3174 | | return 0; |
3175 | | } |
3176 | | |
3177 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3178 | | { |
3179 | | #ifdef __OS2__ |
3180 | | DosRequestMutexSem(*m, SEM_INDEFINITE_WAIT); |
3181 | | #elif defined(__NT__) |
3182 | | EnterCriticalSection(m); |
3183 | | #elif defined(__LINUX__) |
3184 | | if (pthread_mutex_lock(m) ) |
3185 | | return BAD_MUTEX_E; |
3186 | | #endif |
3187 | | return 0; |
3188 | | } |
3189 | | |
3190 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3191 | | { |
3192 | | #ifdef __OS2__ |
3193 | | DosReleaseMutexSem(*m); |
3194 | | #elif defined(__NT__) |
3195 | | LeaveCriticalSection(m); |
3196 | | #elif defined(__LINUX__) |
3197 | | if (pthread_mutex_unlock(m) ) |
3198 | | return BAD_MUTEX_E; |
3199 | | #endif |
3200 | | return 0; |
3201 | | } |
3202 | | |
3203 | | #if defined(WOLFSSL_USE_RWLOCK) && defined(__LINUX__) |
3204 | | |
3205 | | int wc_InitRwLock(wolfSSL_RwLock* m) |
3206 | | { |
3207 | | if (pthread_rwlock_init(m, NULL) ) |
3208 | | return BAD_MUTEX_E; |
3209 | | return 0; |
3210 | | } |
3211 | | |
3212 | | int wc_FreeRwLock(wolfSSL_RwLock* m) |
3213 | | { |
3214 | | if (pthread_rwlock_destroy(m) ) |
3215 | | return BAD_MUTEX_E; |
3216 | | return 0; |
3217 | | } |
3218 | | |
3219 | | int wc_LockRwLock_Wr(wolfSSL_RwLock* m) |
3220 | | { |
3221 | | if (pthread_rwlock_wrlock(m) ) |
3222 | | return BAD_MUTEX_E; |
3223 | | return 0; |
3224 | | } |
3225 | | |
3226 | | int wc_LockRwLock_Rd(wolfSSL_RwLock* m) |
3227 | | { |
3228 | | if (pthread_rwlock_rdlock(m) ) |
3229 | | return BAD_MUTEX_E; |
3230 | | return 0; |
3231 | | } |
3232 | | |
3233 | | int wc_UnLockRwLock(wolfSSL_RwLock* m) |
3234 | | { |
3235 | | if (pthread_rwlock_unlock(m) == 0) |
3236 | | return BAD_MUTEX_E; |
3237 | | return 0; |
3238 | | } |
3239 | | |
3240 | | #endif |
3241 | | |
3242 | | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ |
3243 | | defined(FREESCALE_FREE_RTOS) |
3244 | | |
3245 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3246 | | { |
3247 | | int iReturn; |
3248 | | |
3249 | | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex(); |
3250 | | if( *m != NULL ) |
3251 | | iReturn = 0; |
3252 | | else |
3253 | | iReturn = BAD_MUTEX_E; |
3254 | | |
3255 | | return iReturn; |
3256 | | } |
3257 | | |
3258 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3259 | | { |
3260 | | vSemaphoreDelete( *m ); |
3261 | | return 0; |
3262 | | } |
3263 | | |
3264 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3265 | | { |
3266 | | /* Assume an infinite block, or should there be zero block? */ |
3267 | | xSemaphoreTake( *m, portMAX_DELAY ); |
3268 | | return 0; |
3269 | | } |
3270 | | |
3271 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3272 | | { |
3273 | | xSemaphoreGive( *m ); |
3274 | | return 0; |
3275 | | } |
3276 | | |
3277 | | #elif defined(RTTHREAD) |
3278 | | |
3279 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3280 | | { |
3281 | | int iReturn; |
3282 | | |
3283 | | *m = ( wolfSSL_Mutex ) rt_mutex_create("mutex",RT_IPC_FLAG_FIFO); |
3284 | | if( *m != NULL ) |
3285 | | iReturn = 0; |
3286 | | else |
3287 | | iReturn = BAD_MUTEX_E; |
3288 | | |
3289 | | |
3290 | | return iReturn; |
3291 | | } |
3292 | | |
3293 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3294 | | { |
3295 | | rt_mutex_delete( *m ); |
3296 | | return 0; |
3297 | | } |
3298 | | |
3299 | | |
3300 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3301 | | { |
3302 | | /* Assume an infinite block, or should there be zero block? */ |
3303 | | return rt_mutex_take( *m, RT_WAITING_FOREVER ); |
3304 | | } |
3305 | | |
3306 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3307 | | { |
3308 | | return rt_mutex_release( *m ); |
3309 | | } |
3310 | | |
3311 | | #elif defined(WOLFSSL_SAFERTOS) |
3312 | | |
3313 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3314 | | { |
3315 | | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex); |
3316 | | if (m->mutex == NULL) |
3317 | | return BAD_MUTEX_E; |
3318 | | |
3319 | | return 0; |
3320 | | } |
3321 | | |
3322 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3323 | | { |
3324 | | (void)m; |
3325 | | return 0; |
3326 | | } |
3327 | | |
3328 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3329 | | { |
3330 | | /* Assume an infinite block */ |
3331 | | xSemaphoreTake(m->mutex, portMAX_DELAY); |
3332 | | return 0; |
3333 | | } |
3334 | | |
3335 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3336 | | { |
3337 | | xSemaphoreGive(m->mutex); |
3338 | | return 0; |
3339 | | } |
3340 | | |
3341 | | #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS) |
3342 | | |
3343 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3344 | | { |
3345 | | InitializeCriticalSection(m); |
3346 | | return 0; |
3347 | | } |
3348 | | |
3349 | | |
3350 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3351 | | { |
3352 | | DeleteCriticalSection(m); |
3353 | | return 0; |
3354 | | } |
3355 | | |
3356 | | |
3357 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3358 | | { |
3359 | | EnterCriticalSection(m); |
3360 | | return 0; |
3361 | | } |
3362 | | |
3363 | | |
3364 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3365 | | { |
3366 | | LeaveCriticalSection(m); |
3367 | | return 0; |
3368 | | } |
3369 | | |
3370 | | #elif defined(MAXQ10XX_MUTEX) |
3371 | | static pthread_mutex_t *wcCryptHwSharedMutexPtr; |
3372 | | static pthread_once_t key_once_own_hw_mutex = PTHREAD_ONCE_INIT; |
3373 | | static pthread_key_t key_own_hw_mutex; |
3374 | | |
3375 | | static void destruct_key(void *buf) |
3376 | | { |
3377 | | if (buf != NULL) { |
3378 | | XFREE(buf, NULL, DYNAMIC_TYPE_OS_BUF); |
3379 | | } |
3380 | | } |
3381 | | |
3382 | | static void make_key_own_hw_mutex(void) |
3383 | | { |
3384 | | (void)pthread_key_create(&key_own_hw_mutex, destruct_key); |
3385 | | } |
3386 | | |
3387 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3388 | | { |
3389 | | int created = 0; |
3390 | | void *addr = NULL; |
3391 | | |
3392 | | if (m != &wcCryptHwMutex) { |
3393 | | if (pthread_mutex_init(m, 0) == 0) { |
3394 | | return 0; |
3395 | | } |
3396 | | return BAD_MUTEX_E; |
3397 | | } |
3398 | | |
3399 | | /* try to open mutex memory */ |
3400 | | int shm_fd = shm_open("/maxq-mutex", O_RDWR, 0666); |
3401 | | if (shm_fd < 0) { |
3402 | | /* create mutex memory */ |
3403 | | shm_fd = shm_open("/maxq-mutex", O_RDWR | O_CREAT | O_EXCL, 0666); |
3404 | | created = 1; |
3405 | | } |
3406 | | |
3407 | | if (shm_fd < 0) { |
3408 | | WOLFSSL_MSG("wc_InitMutex: shm_open() failed"); |
3409 | | return BAD_MUTEX_E; |
3410 | | } |
3411 | | |
3412 | | if (ftruncate(shm_fd, sizeof(pthread_mutex_t))) { |
3413 | | WOLFSSL_MSG("wc_InitMutex: ftruncate() failed"); |
3414 | | return BAD_MUTEX_E; |
3415 | | } |
3416 | | |
3417 | | addr = mmap(NULL, sizeof(pthread_mutex_t), PROT_READ | PROT_WRITE, |
3418 | | MAP_SHARED, shm_fd, 0); |
3419 | | |
3420 | | if (addr == MAP_FAILED) { |
3421 | | WOLFSSL_MSG("wc_InitMutex: mmap() failed"); |
3422 | | return BAD_MUTEX_E; |
3423 | | } |
3424 | | |
3425 | | wcCryptHwSharedMutexPtr = (pthread_mutex_t *)addr; |
3426 | | |
3427 | | if (close(shm_fd)) { |
3428 | | WOLFSSL_MSG("wc_InitMutex: close() failed"); |
3429 | | return BAD_MUTEX_E; |
3430 | | } |
3431 | | |
3432 | | if (created) { |
3433 | | /* initialize mutex */ |
3434 | | pthread_mutexattr_t attr; |
3435 | | if (pthread_mutexattr_init(&attr)) { |
3436 | | WOLFSSL_MSG("wc_InitMutex: pthread_mutexattr_init() failed"); |
3437 | | return BAD_MUTEX_E; |
3438 | | } |
3439 | | |
3440 | | if (pthread_mutexattr_setpshared(&attr, |
3441 | | PTHREAD_PROCESS_SHARED)) { |
3442 | | WOLFSSL_MSG( |
3443 | | "wc_InitMutex: pthread_mutexattr_setpshared() failed"); |
3444 | | return BAD_MUTEX_E; |
3445 | | } |
3446 | | |
3447 | | if (pthread_mutex_init(wcCryptHwSharedMutexPtr, &attr)) { |
3448 | | WOLFSSL_MSG("wc_InitMutex: pthread_mutex_init() failed"); |
3449 | | return BAD_MUTEX_E; |
3450 | | } |
3451 | | } |
3452 | | |
3453 | | if (pthread_once(&key_once_own_hw_mutex, make_key_own_hw_mutex)) { |
3454 | | WOLFSSL_MSG("wc_InitMutex: pthread_once() failed"); |
3455 | | return BAD_MUTEX_E; |
3456 | | } |
3457 | | |
3458 | | return 0; |
3459 | | } |
3460 | | |
3461 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3462 | | { |
3463 | | void *key_ptr = NULL; |
3464 | | if (m != &wcCryptHwMutex) { |
3465 | | if (pthread_mutex_destroy(m) == 0) { |
3466 | | return 0; |
3467 | | } |
3468 | | return BAD_MUTEX_E; |
3469 | | } |
3470 | | |
3471 | | if (wcCryptHwSharedMutexPtr) { |
3472 | | if (munmap((void *)wcCryptHwSharedMutexPtr, |
3473 | | sizeof(pthread_mutex_t))) { |
3474 | | WOLFSSL_MSG("wc_FreeMutex: munmap() failed"); |
3475 | | return BAD_MUTEX_E; |
3476 | | } |
3477 | | |
3478 | | wcCryptHwSharedMutexPtr = NULL; |
3479 | | } |
3480 | | |
3481 | | key_ptr = pthread_getspecific(key_own_hw_mutex); |
3482 | | if (key_ptr) { |
3483 | | *((int *)key_ptr) = 0; |
3484 | | } |
3485 | | |
3486 | | return 0; |
3487 | | } |
3488 | | |
3489 | | static int maxq_LockMutex(wolfSSL_Mutex* m, int trylock) |
3490 | | { |
3491 | | void *key_ptr = NULL; |
3492 | | int ret = 0; |
3493 | | |
3494 | | if (m != &wcCryptHwMutex) { |
3495 | | if (pthread_mutex_lock(m) == 0) { |
3496 | | return 0; |
3497 | | } |
3498 | | return BAD_MUTEX_E; |
3499 | | } |
3500 | | |
3501 | | if (wcCryptHwSharedMutexPtr == NULL) { |
3502 | | return BAD_MUTEX_E; |
3503 | | } |
3504 | | |
3505 | | key_ptr = pthread_getspecific(key_own_hw_mutex); |
3506 | | if (key_ptr == NULL) { |
3507 | | key_ptr = XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_OS_BUF); |
3508 | | if (key_ptr == NULL) { |
3509 | | return MEMORY_E; |
3510 | | } |
3511 | | |
3512 | | memset(key_ptr, 0, sizeof(int)); |
3513 | | |
3514 | | if (pthread_setspecific(key_own_hw_mutex, key_ptr)) { |
3515 | | return THREAD_STORE_SET_E; |
3516 | | } |
3517 | | } |
3518 | | else { |
3519 | | if ((trylock == 0) && (*((int *)key_ptr) > 0)) { |
3520 | | *((int *)key_ptr) = *((int *)key_ptr) + 1; |
3521 | | return 0; |
3522 | | } |
3523 | | } |
3524 | | |
3525 | | if (trylock) { |
3526 | | ret = pthread_mutex_trylock(wcCryptHwSharedMutexPtr); |
3527 | | } |
3528 | | else { |
3529 | | ret = pthread_mutex_lock(wcCryptHwSharedMutexPtr); |
3530 | | } |
3531 | | |
3532 | | if (ret != 0) { |
3533 | | return BAD_MUTEX_E; |
3534 | | } |
3535 | | |
3536 | | *((int *)key_ptr) = 1; |
3537 | | return 0; |
3538 | | } |
3539 | | |
3540 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3541 | | { |
3542 | | return maxq_LockMutex(m, 0); |
3543 | | } |
3544 | | |
3545 | | int maxq_CryptHwMutexTryLock() |
3546 | | { |
3547 | | /* Make sure HW Mutex has been initialized */ |
3548 | | int ret = wolfSSL_CryptHwMutexInit(); |
3549 | | if (ret == 0) { |
3550 | | ret = maxq_LockMutex(&wcCryptHwMutex, 1); |
3551 | | } |
3552 | | return ret; |
3553 | | } |
3554 | | |
3555 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3556 | | { |
3557 | | void *key_ptr = NULL; |
3558 | | |
3559 | | if (m != &wcCryptHwMutex) { |
3560 | | if (pthread_mutex_unlock(m) == 0) { |
3561 | | return 0; |
3562 | | } |
3563 | | return BAD_MUTEX_E; |
3564 | | } |
3565 | | |
3566 | | if (wcCryptHwSharedMutexPtr == NULL) { |
3567 | | return BAD_MUTEX_E; |
3568 | | } |
3569 | | |
3570 | | key_ptr = pthread_getspecific(key_own_hw_mutex); |
3571 | | if (key_ptr) { |
3572 | | if (*((int *)key_ptr) > 0) { |
3573 | | *((int *)key_ptr) = *((int *)key_ptr) - 1; |
3574 | | if (*((int *)key_ptr) > 0) { |
3575 | | return 0; |
3576 | | } |
3577 | | } |
3578 | | } |
3579 | | |
3580 | | if (pthread_mutex_unlock(wcCryptHwSharedMutexPtr) != 0) { |
3581 | | return BAD_MUTEX_E; |
3582 | | } |
3583 | | return 0; |
3584 | | } |
3585 | | |
3586 | | #elif defined(WOLFSSL_PTHREADS) |
3587 | | |
3588 | | #ifdef WOLFSSL_USE_RWLOCK |
3589 | | int wc_InitRwLock(wolfSSL_RwLock* m) |
3590 | | { |
3591 | | if (pthread_rwlock_init(m, NULL) == 0) |
3592 | | return 0; |
3593 | | else |
3594 | | return BAD_MUTEX_E; |
3595 | | } |
3596 | | |
3597 | | int wc_FreeRwLock(wolfSSL_RwLock* m) |
3598 | | { |
3599 | | if (pthread_rwlock_destroy(m) == 0) |
3600 | | return 0; |
3601 | | else |
3602 | | return BAD_MUTEX_E; |
3603 | | } |
3604 | | |
3605 | | int wc_LockRwLock_Wr(wolfSSL_RwLock* m) |
3606 | | { |
3607 | | if (pthread_rwlock_wrlock(m) == 0) |
3608 | | return 0; |
3609 | | else |
3610 | | return BAD_MUTEX_E; |
3611 | | } |
3612 | | |
3613 | | int wc_LockRwLock_Rd(wolfSSL_RwLock* m) |
3614 | | { |
3615 | | if (pthread_rwlock_rdlock(m) == 0) |
3616 | | return 0; |
3617 | | else |
3618 | | return BAD_MUTEX_E; |
3619 | | } |
3620 | | |
3621 | | int wc_UnLockRwLock(wolfSSL_RwLock* m) |
3622 | | { |
3623 | | if (pthread_rwlock_unlock(m) == 0) |
3624 | | return 0; |
3625 | | else |
3626 | | return BAD_MUTEX_E; |
3627 | | } |
3628 | | #endif |
3629 | | |
3630 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3631 | 18.6k | { |
3632 | 18.6k | if (pthread_mutex_init(m, NULL) == 0) |
3633 | 18.6k | return 0; |
3634 | 0 | else |
3635 | 0 | return BAD_MUTEX_E; |
3636 | 18.6k | } |
3637 | | |
3638 | | |
3639 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3640 | 18.4k | { |
3641 | 18.4k | if (pthread_mutex_destroy(m) == 0) |
3642 | 18.4k | return 0; |
3643 | 0 | else |
3644 | 0 | return BAD_MUTEX_E; |
3645 | 18.4k | } |
3646 | | |
3647 | | |
3648 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3649 | 193k | { |
3650 | 193k | if (pthread_mutex_lock(m) == 0) |
3651 | 193k | return 0; |
3652 | 0 | else |
3653 | 0 | return BAD_MUTEX_E; |
3654 | 193k | } |
3655 | | |
3656 | | |
3657 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3658 | 193k | { |
3659 | 193k | if (pthread_mutex_unlock(m) == 0) |
3660 | 193k | return 0; |
3661 | 0 | else |
3662 | 0 | return BAD_MUTEX_E; |
3663 | 193k | } |
3664 | | #elif defined(WOLFSSL_VXWORKS) |
3665 | | |
3666 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3667 | | { |
3668 | | if (m) { |
3669 | | if ((*m = semMCreate(0)) != SEM_ID_NULL) |
3670 | | return 0; |
3671 | | } |
3672 | | return BAD_MUTEX_E; |
3673 | | } |
3674 | | |
3675 | | |
3676 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3677 | | { |
3678 | | if (m) { |
3679 | | if (semDelete(*m) == OK) |
3680 | | return 0; |
3681 | | } |
3682 | | return BAD_MUTEX_E; |
3683 | | } |
3684 | | |
3685 | | |
3686 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3687 | | { |
3688 | | if (m) { |
3689 | | if (semTake(*m, WAIT_FOREVER) == OK) |
3690 | | return 0; |
3691 | | } |
3692 | | return BAD_MUTEX_E; |
3693 | | } |
3694 | | |
3695 | | |
3696 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3697 | | { |
3698 | | if (m) { |
3699 | | if (semGive(*m) == OK) |
3700 | | return 0; |
3701 | | } |
3702 | | return BAD_MUTEX_E; |
3703 | | } |
3704 | | |
3705 | | #elif defined(THREADX) |
3706 | | |
3707 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3708 | | { |
3709 | | if (tx_mutex_create(m, (CHAR*)"wolfSSL Mutex", TX_NO_INHERIT) == 0) |
3710 | | return 0; |
3711 | | else |
3712 | | return BAD_MUTEX_E; |
3713 | | } |
3714 | | |
3715 | | |
3716 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3717 | | { |
3718 | | if (tx_mutex_delete(m) == 0) |
3719 | | return 0; |
3720 | | else |
3721 | | return BAD_MUTEX_E; |
3722 | | } |
3723 | | |
3724 | | |
3725 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3726 | | { |
3727 | | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0) |
3728 | | return 0; |
3729 | | else |
3730 | | return BAD_MUTEX_E; |
3731 | | } |
3732 | | |
3733 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3734 | | { |
3735 | | if (tx_mutex_put(m) == 0) |
3736 | | return 0; |
3737 | | else |
3738 | | return BAD_MUTEX_E; |
3739 | | } |
3740 | | |
3741 | | #elif defined(WOLFSSL_DEOS) |
3742 | | |
3743 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3744 | | { |
3745 | | mutexStatus mutStat; |
3746 | | /* |
3747 | | The empty string "" denotes an anonymous mutex, so objects do not cause name collisions. |
3748 | | `protectWolfSSLTemp` in an XML configuration element template describing a mutex. |
3749 | | */ |
3750 | | if (m) { |
3751 | | mutStat = createMutex("", "protectWolfSSLTemp", m); |
3752 | | if (mutStat == mutexSuccess) |
3753 | | return 0; |
3754 | | else{ |
3755 | | WOLFSSL_MSG("wc_InitMutex failed"); |
3756 | | return mutStat; |
3757 | | } |
3758 | | } |
3759 | | return BAD_MUTEX_E; |
3760 | | } |
3761 | | |
3762 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3763 | | { |
3764 | | mutexStatus mutStat; |
3765 | | if (m) { |
3766 | | mutStat = deleteMutex(*m); |
3767 | | if (mutStat == mutexSuccess) |
3768 | | return 0; |
3769 | | else{ |
3770 | | WOLFSSL_MSG("wc_FreeMutex failed"); |
3771 | | return mutStat; |
3772 | | } |
3773 | | } |
3774 | | return BAD_MUTEX_E; |
3775 | | } |
3776 | | |
3777 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3778 | | { |
3779 | | mutexStatus mutStat; |
3780 | | if (m) { |
3781 | | mutStat = lockMutex(*m); |
3782 | | if (mutStat == mutexSuccess) |
3783 | | return 0; |
3784 | | else{ |
3785 | | WOLFSSL_MSG("wc_LockMutex failed"); |
3786 | | return mutStat; |
3787 | | } |
3788 | | } |
3789 | | return BAD_MUTEX_E; |
3790 | | } |
3791 | | |
3792 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3793 | | { |
3794 | | mutexStatus mutStat; |
3795 | | if (m) { |
3796 | | mutStat = unlockMutex(*m); |
3797 | | if (mutStat== mutexSuccess) |
3798 | | return 0; |
3799 | | else{ |
3800 | | WOLFSSL_MSG("wc_UnLockMutex failed"); |
3801 | | return mutStat; |
3802 | | } |
3803 | | } |
3804 | | return BAD_MUTEX_E; |
3805 | | } |
3806 | | |
3807 | | #elif defined(MICRIUM) |
3808 | | #if (OS_VERSION < 50000) |
3809 | | #define MICRIUM_ERR_TYPE OS_ERR |
3810 | | #define MICRIUM_ERR_NONE OS_ERR_NONE |
3811 | | #define MICRIUM_ERR_CODE(err) err |
3812 | | #else |
3813 | | #define MICRIUM_ERR_TYPE RTOS_ERR |
3814 | | #define MICRIUM_ERR_NONE RTOS_ERR_NONE |
3815 | | #define MICRIUM_ERR_CODE(err) RTOS_ERR_CODE_GET(err) |
3816 | | #endif |
3817 | | |
3818 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3819 | | { |
3820 | | MICRIUM_ERR_TYPE err; |
3821 | | |
3822 | | OSMutexCreate(m, "wolfSSL Mutex", &err); |
3823 | | |
3824 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
3825 | | return 0; |
3826 | | else |
3827 | | return BAD_MUTEX_E; |
3828 | | } |
3829 | | |
3830 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3831 | | { |
3832 | | #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED) |
3833 | | MICRIUM_ERR_TYPE err; |
3834 | | |
3835 | | OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err); |
3836 | | |
3837 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
3838 | | return 0; |
3839 | | else |
3840 | | return BAD_MUTEX_E; |
3841 | | #else |
3842 | | (void)m; |
3843 | | return 0; |
3844 | | #endif |
3845 | | } |
3846 | | |
3847 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3848 | | { |
3849 | | MICRIUM_ERR_TYPE err; |
3850 | | |
3851 | | OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err); |
3852 | | |
3853 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
3854 | | return 0; |
3855 | | else |
3856 | | return BAD_MUTEX_E; |
3857 | | } |
3858 | | |
3859 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3860 | | { |
3861 | | MICRIUM_ERR_TYPE err; |
3862 | | |
3863 | | OSMutexPost(m, OS_OPT_POST_NONE, &err); |
3864 | | |
3865 | | if (MICRIUM_ERR_CODE(err) == MICRIUM_ERR_NONE) |
3866 | | return 0; |
3867 | | else |
3868 | | return BAD_MUTEX_E; |
3869 | | } |
3870 | | |
3871 | | #elif defined(EBSNET) |
3872 | | #if (defined(RTPLATFORM) && (RTPLATFORM != 0)) |
3873 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3874 | | { |
3875 | | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1) |
3876 | | return BAD_MUTEX_E; |
3877 | | else |
3878 | | return 0; |
3879 | | } |
3880 | | |
3881 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3882 | | { |
3883 | | rtp_sig_mutex_free(*m); |
3884 | | return 0; |
3885 | | } |
3886 | | |
3887 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3888 | | { |
3889 | | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0) |
3890 | | return 0; |
3891 | | else |
3892 | | return BAD_MUTEX_E; |
3893 | | } |
3894 | | |
3895 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3896 | | { |
3897 | | rtp_sig_mutex_release(*m); |
3898 | | return 0; |
3899 | | } |
3900 | | |
3901 | | int ebsnet_fseek(int a, long b, int c) |
3902 | | { |
3903 | | int retval; |
3904 | | |
3905 | | retval = vf_lseek(a, b, c); |
3906 | | if (retval > 0) |
3907 | | retval = 0; |
3908 | | else |
3909 | | retval = -1; |
3910 | | |
3911 | | return(retval); |
3912 | | } |
3913 | | #else |
3914 | | static int rtip_semaphore_build(wolfSSL_Mutex *m) |
3915 | | { |
3916 | | KS_SEMAPHORE_BUILD(m) |
3917 | | return(RTP_TRUE); |
3918 | | } |
3919 | | |
3920 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3921 | | { |
3922 | | if (rtip_semaphore_build(m) == RTP_FALSE) |
3923 | | return BAD_MUTEX_E; |
3924 | | else |
3925 | | return 0; |
3926 | | } |
3927 | | |
3928 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3929 | | { |
3930 | | KS_SEMAPHORE_FREE(*m); |
3931 | | return 0; |
3932 | | } |
3933 | | |
3934 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3935 | | { |
3936 | | if (KS_SEMAPHORE_GET(*m)) |
3937 | | return 0; |
3938 | | else |
3939 | | return BAD_MUTEX_E; |
3940 | | } |
3941 | | |
3942 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
3943 | | { |
3944 | | KS_SEMAPHORE_GIVE(*m); |
3945 | | return 0; |
3946 | | } |
3947 | | #endif |
3948 | | int ebsnet_fseek(int a, long b, int c) |
3949 | | { |
3950 | | int retval; |
3951 | | |
3952 | | retval = (int)vf_lseek(a, b, c); |
3953 | | if (retval > 0) |
3954 | | retval = 0; |
3955 | | else |
3956 | | retval = -1; |
3957 | | |
3958 | | return(retval); |
3959 | | } |
3960 | | |
3961 | | int strcasecmp(const char *s1, const char *s2) |
3962 | | { |
3963 | | while (rtp_tolower(*s1) == rtp_tolower(*s2)) { |
3964 | | if (*s1 == '\0' || *s2 == '\0') |
3965 | | break; |
3966 | | s1++; |
3967 | | s2++; |
3968 | | } |
3969 | | |
3970 | | return rtp_tolower(*(unsigned char *) s1) - |
3971 | | rtp_tolower(*(unsigned char *) s2); |
3972 | | } |
3973 | | |
3974 | | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
3975 | | |
3976 | | int wc_InitMutex(wolfSSL_Mutex* m) |
3977 | | { |
3978 | | #if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2)) |
3979 | | if (wolfCrypt_GetMode_fips() == FIPS_MODE_INIT) |
3980 | | return 0; |
3981 | | #endif |
3982 | | if (_mutex_init(m, NULL) == MQX_EOK) |
3983 | | return 0; |
3984 | | else |
3985 | | return BAD_MUTEX_E; |
3986 | | } |
3987 | | |
3988 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
3989 | | { |
3990 | | if (_mutex_destroy(m) == MQX_EOK) |
3991 | | return 0; |
3992 | | else |
3993 | | return BAD_MUTEX_E; |
3994 | | } |
3995 | | |
3996 | | int wc_LockMutex(wolfSSL_Mutex* m) |
3997 | | { |
3998 | | #if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2)) |
3999 | | if (m->VALID != MUTEX_VALID) { |
4000 | | if (_mutex_init(m, NULL) != MQX_EOK) |
4001 | | return BAD_MUTEX_E; |
4002 | | } |
4003 | | #endif |
4004 | | |
4005 | | if (_mutex_lock(m) == MQX_EOK) |
4006 | | return 0; |
4007 | | else |
4008 | | return BAD_MUTEX_E; |
4009 | | } |
4010 | | |
4011 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4012 | | { |
4013 | | #if (defined(HAVE_FIPS) && FIPS_VERSION_EQ(5,2)) |
4014 | | if (m->VALID != MUTEX_VALID) { |
4015 | | if (_mutex_init(m, NULL) != MQX_EOK) |
4016 | | return BAD_MUTEX_E; |
4017 | | } |
4018 | | #endif |
4019 | | |
4020 | | if (_mutex_unlock(m) == MQX_EOK) |
4021 | | return 0; |
4022 | | else |
4023 | | return BAD_MUTEX_E; |
4024 | | } |
4025 | | |
4026 | | #elif defined(WOLFSSL_TIRTOS) |
4027 | | #include <xdc/runtime/Error.h> |
4028 | | |
4029 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4030 | | { |
4031 | | Semaphore_Params params; |
4032 | | Error_Block eb; |
4033 | | |
4034 | | Error_init(&eb); |
4035 | | Semaphore_Params_init(¶ms); |
4036 | | params.mode = Semaphore_Mode_BINARY; |
4037 | | |
4038 | | *m = Semaphore_create(1, ¶ms, &eb); |
4039 | | if (Error_check(&eb)) { |
4040 | | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.", |
4041 | | NULL); |
4042 | | return BAD_MUTEX_E; |
4043 | | } |
4044 | | else |
4045 | | return 0; |
4046 | | } |
4047 | | |
4048 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4049 | | { |
4050 | | Semaphore_delete(m); |
4051 | | |
4052 | | return 0; |
4053 | | } |
4054 | | |
4055 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4056 | | { |
4057 | | Semaphore_pend(*m, BIOS_WAIT_FOREVER); |
4058 | | |
4059 | | return 0; |
4060 | | } |
4061 | | |
4062 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4063 | | { |
4064 | | Semaphore_post(*m); |
4065 | | |
4066 | | return 0; |
4067 | | } |
4068 | | |
4069 | | #elif defined(WOLFSSL_uITRON4) |
4070 | | |
4071 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4072 | | { |
4073 | | int iReturn; |
4074 | | m->sem.sematr = TA_TFIFO; |
4075 | | m->sem.isemcnt = 1; |
4076 | | m->sem.maxsem = 1; |
4077 | | m->sem.name = NULL; |
4078 | | |
4079 | | m->id = acre_sem(&m->sem); |
4080 | | if( m->id != E_OK ) |
4081 | | iReturn = 0; |
4082 | | else |
4083 | | iReturn = BAD_MUTEX_E; |
4084 | | |
4085 | | return iReturn; |
4086 | | } |
4087 | | |
4088 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4089 | | { |
4090 | | del_sem( m->id ); |
4091 | | return 0; |
4092 | | } |
4093 | | |
4094 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4095 | | { |
4096 | | wai_sem(m->id); |
4097 | | return 0; |
4098 | | } |
4099 | | |
4100 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4101 | | { |
4102 | | sig_sem(m->id); |
4103 | | return 0; |
4104 | | } |
4105 | | |
4106 | | /**** uITRON malloc/free ***/ |
4107 | | static ID ID_wolfssl_MPOOL = 0; |
4108 | | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"}; |
4109 | | |
4110 | | int uITRON4_minit(size_t poolsz) { |
4111 | | ER ercd; |
4112 | | wolfssl_MPOOL.mplsz = poolsz; |
4113 | | ercd = acre_mpl(&wolfssl_MPOOL); |
4114 | | if (ercd > 0) { |
4115 | | ID_wolfssl_MPOOL = ercd; |
4116 | | return 0; |
4117 | | } else { |
4118 | | return -1; |
4119 | | } |
4120 | | } |
4121 | | |
4122 | | void *uITRON4_malloc(size_t sz) { |
4123 | | ER ercd; |
4124 | | void *p = NULL; |
4125 | | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p); |
4126 | | if (ercd == E_OK) { |
4127 | | return p; |
4128 | | } else { |
4129 | | return 0; |
4130 | | } |
4131 | | } |
4132 | | |
4133 | | void *uITRON4_realloc(void *p, size_t sz) { |
4134 | | ER ercd; |
4135 | | void *newp = NULL; |
4136 | | if(p) { |
4137 | | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp); |
4138 | | if ((ercd == E_OK) && (newp != NULL)) { |
4139 | | XMEMCPY(newp, p, sz); |
4140 | | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
4141 | | if (ercd == E_OK) { |
4142 | | return newp; |
4143 | | } |
4144 | | } |
4145 | | } |
4146 | | return 0; |
4147 | | } |
4148 | | |
4149 | | void uITRON4_free(void *p) { |
4150 | | ER ercd; |
4151 | | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
4152 | | if (ercd == E_OK) { |
4153 | | return; |
4154 | | } else { |
4155 | | return; |
4156 | | } |
4157 | | } |
4158 | | |
4159 | | #elif defined(WOLFSSL_uTKERNEL2) |
4160 | | |
4161 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4162 | | { |
4163 | | int iReturn; |
4164 | | m->sem.sematr = TA_TFIFO; |
4165 | | m->sem.isemcnt = 1; |
4166 | | m->sem.maxsem = 1; |
4167 | | |
4168 | | m->id = tk_cre_sem(&m->sem); |
4169 | | if( m->id != NULL ) |
4170 | | iReturn = 0; |
4171 | | else |
4172 | | iReturn = BAD_MUTEX_E; |
4173 | | |
4174 | | return iReturn; |
4175 | | } |
4176 | | |
4177 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4178 | | { |
4179 | | tk_del_sem(m->id); |
4180 | | return 0; |
4181 | | } |
4182 | | |
4183 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4184 | | { |
4185 | | tk_wai_sem(m->id, 1, TMO_FEVR); |
4186 | | return 0; |
4187 | | } |
4188 | | |
4189 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4190 | | { |
4191 | | tk_sig_sem(m->id, 1); |
4192 | | return 0; |
4193 | | } |
4194 | | |
4195 | | /**** uT-Kernel malloc/free ***/ |
4196 | | static ID ID_wolfssl_MPOOL = 0; |
4197 | | static T_CMPL wolfssl_MPOOL = { |
4198 | | NULL, /* Extended information */ |
4199 | | TA_TFIFO, /* Memory pool attribute */ |
4200 | | 0, /* Size of whole memory pool (byte) */ |
4201 | | "wolfSSL" /* Object name (max 8-char) */ |
4202 | | }; |
4203 | | |
4204 | | int uTKernel_init_mpool(unsigned int sz) { |
4205 | | ER ercd; |
4206 | | wolfssl_MPOOL.mplsz = sz; |
4207 | | ercd = tk_cre_mpl(&wolfssl_MPOOL); |
4208 | | if (ercd > 0) { |
4209 | | ID_wolfssl_MPOOL = ercd; |
4210 | | return 0; |
4211 | | } else { |
4212 | | return (int)ercd; |
4213 | | } |
4214 | | } |
4215 | | |
4216 | | void *uTKernel_malloc(unsigned int sz) { |
4217 | | ER ercd; |
4218 | | void *p = NULL; |
4219 | | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR); |
4220 | | if (ercd == E_OK) { |
4221 | | return p; |
4222 | | } else { |
4223 | | return 0; |
4224 | | } |
4225 | | } |
4226 | | |
4227 | | void *uTKernel_realloc(void *p, unsigned int sz) { |
4228 | | ER ercd; |
4229 | | void *newp = NULL; |
4230 | | if (p) { |
4231 | | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR); |
4232 | | if ((ercd == E_OK) && (newp != NULL)) { |
4233 | | XMEMCPY(newp, p, sz); |
4234 | | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
4235 | | if (ercd == E_OK) { |
4236 | | return newp; |
4237 | | } |
4238 | | } |
4239 | | } |
4240 | | return 0; |
4241 | | } |
4242 | | |
4243 | | void uTKernel_free(void *p) { |
4244 | | ER ercd; |
4245 | | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
4246 | | if (ercd == E_OK) { |
4247 | | return; |
4248 | | } else { |
4249 | | return; |
4250 | | } |
4251 | | } |
4252 | | |
4253 | | #elif defined (WOLFSSL_FROSTED) |
4254 | | |
4255 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4256 | | { |
4257 | | *m = mutex_init(); |
4258 | | if (*m) |
4259 | | return 0; |
4260 | | else |
4261 | | return -1; |
4262 | | } |
4263 | | |
4264 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4265 | | { |
4266 | | mutex_destroy(*m); |
4267 | | return(0); |
4268 | | } |
4269 | | |
4270 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4271 | | { |
4272 | | mutex_lock(*m); |
4273 | | return 0; |
4274 | | } |
4275 | | |
4276 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4277 | | { |
4278 | | mutex_unlock(*m); |
4279 | | return 0; |
4280 | | } |
4281 | | |
4282 | | #elif defined(WOLFSSL_CMSIS_RTOS) |
4283 | | |
4284 | | #ifndef CMSIS_NMUTEX |
4285 | | #define CMSIS_NMUTEX 10 |
4286 | | #endif |
4287 | | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2); |
4288 | | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5); |
4289 | | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8); |
4290 | | osMutexDef(wolfSSL_mt9); |
4291 | | |
4292 | | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0), |
4293 | | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3), |
4294 | | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6), |
4295 | | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) }; |
4296 | | |
4297 | | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0}; |
4298 | | |
4299 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4300 | | { |
4301 | | int i; |
4302 | | |
4303 | | if(!osKernelRunning()) { |
4304 | | return 0; |
4305 | | } |
4306 | | |
4307 | | for (i=0; i<CMSIS_NMUTEX; i++) { |
4308 | | if(CMSIS_mutexID[i] == 0) { |
4309 | | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]); |
4310 | | (*m) = CMSIS_mutexID[i]; |
4311 | | return 0; |
4312 | | } |
4313 | | } |
4314 | | return -1; |
4315 | | } |
4316 | | |
4317 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4318 | | { |
4319 | | int i; |
4320 | | |
4321 | | if(!osKernelRunning()) { |
4322 | | return 0; |
4323 | | } |
4324 | | |
4325 | | osMutexDelete (*m); |
4326 | | for (i=0; i<CMSIS_NMUTEX; i++) { |
4327 | | if(CMSIS_mutexID[i] == (*m)) { |
4328 | | CMSIS_mutexID[i] = 0; |
4329 | | return(0); |
4330 | | } |
4331 | | } |
4332 | | return(-1); |
4333 | | } |
4334 | | |
4335 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4336 | | { |
4337 | | if(osKernelRunning()) { |
4338 | | osMutexWait(*m, osWaitForever); |
4339 | | } |
4340 | | return(0); |
4341 | | } |
4342 | | |
4343 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4344 | | { |
4345 | | if(osKernelRunning()) { |
4346 | | osMutexRelease (*m); |
4347 | | } |
4348 | | return 0; |
4349 | | } |
4350 | | |
4351 | | #elif defined(WOLFSSL_CMSIS_RTOSv2) |
4352 | | int wc_InitMutex(wolfSSL_Mutex *m) |
4353 | | { |
4354 | | static const osMutexAttr_t attr = { |
4355 | | "wolfSSL_mutex", osMutexRecursive, NULL, 0}; |
4356 | | |
4357 | | if ((*m = osMutexNew(&attr)) != NULL) |
4358 | | return 0; |
4359 | | else |
4360 | | return BAD_MUTEX_E; |
4361 | | } |
4362 | | |
4363 | | int wc_FreeMutex(wolfSSL_Mutex *m) |
4364 | | { |
4365 | | if (osMutexDelete(*m) == osOK) |
4366 | | return 0; |
4367 | | else |
4368 | | return BAD_MUTEX_E; |
4369 | | } |
4370 | | |
4371 | | |
4372 | | int wc_LockMutex(wolfSSL_Mutex *m) |
4373 | | { |
4374 | | if (osMutexAcquire(*m, osWaitForever) == osOK) |
4375 | | return 0; |
4376 | | else |
4377 | | return BAD_MUTEX_E; |
4378 | | } |
4379 | | |
4380 | | int wc_UnLockMutex(wolfSSL_Mutex *m) |
4381 | | { |
4382 | | if (osMutexRelease(*m) == osOK) |
4383 | | return 0; |
4384 | | else |
4385 | | return BAD_MUTEX_E; |
4386 | | } |
4387 | | |
4388 | | #elif defined(WOLFSSL_MDK_ARM) |
4389 | | |
4390 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4391 | | { |
4392 | | os_mut_init (m); |
4393 | | return 0; |
4394 | | } |
4395 | | |
4396 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4397 | | { |
4398 | | return(0); |
4399 | | } |
4400 | | |
4401 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4402 | | { |
4403 | | os_mut_wait (m, 0xffff); |
4404 | | return(0); |
4405 | | } |
4406 | | |
4407 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4408 | | { |
4409 | | os_mut_release (m); |
4410 | | return 0; |
4411 | | } |
4412 | | |
4413 | | #elif defined(INTIME_RTOS) |
4414 | | |
4415 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4416 | | { |
4417 | | int ret = 0; |
4418 | | |
4419 | | if (m == NULL) |
4420 | | return BAD_FUNC_ARG; |
4421 | | |
4422 | | *m = CreateRtSemaphore( |
4423 | | 1, /* initial unit count */ |
4424 | | 1, /* maximum unit count */ |
4425 | | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */ |
4426 | | ); |
4427 | | if (*m == BAD_RTHANDLE) { |
4428 | | ret = GetLastRtError(); |
4429 | | if (ret != E_OK) |
4430 | | ret = BAD_MUTEX_E; |
4431 | | } |
4432 | | return ret; |
4433 | | } |
4434 | | |
4435 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4436 | | { |
4437 | | int ret = 0; |
4438 | | BOOLEAN del; |
4439 | | |
4440 | | if (m == NULL) |
4441 | | return BAD_FUNC_ARG; |
4442 | | |
4443 | | del = DeleteRtSemaphore( |
4444 | | *m /* handle for RT semaphore */ |
4445 | | ); |
4446 | | if (del != TRUE) |
4447 | | ret = BAD_MUTEX_E; |
4448 | | |
4449 | | return ret; |
4450 | | } |
4451 | | |
4452 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4453 | | { |
4454 | | int ret = 0; |
4455 | | DWORD lck; |
4456 | | |
4457 | | if (m == NULL) |
4458 | | return BAD_FUNC_ARG; |
4459 | | |
4460 | | lck = WaitForRtSemaphore( |
4461 | | *m, /* handle for RT semaphore */ |
4462 | | 1, /* number of units to wait for */ |
4463 | | WAIT_FOREVER /* number of milliseconds to wait for units */ |
4464 | | ); |
4465 | | if (lck == WAIT_FAILED) { |
4466 | | ret = GetLastRtError(); |
4467 | | if (ret != E_OK) |
4468 | | ret = BAD_MUTEX_E; |
4469 | | } |
4470 | | return ret; |
4471 | | } |
4472 | | |
4473 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4474 | | { |
4475 | | int ret = 0; |
4476 | | BOOLEAN rel; |
4477 | | |
4478 | | if (m == NULL) |
4479 | | return BAD_FUNC_ARG; |
4480 | | |
4481 | | rel = ReleaseRtSemaphore( |
4482 | | *m, /* handle for RT semaphore */ |
4483 | | 1 /* number of units to release to semaphore */ |
4484 | | ); |
4485 | | if (rel != TRUE) |
4486 | | ret = BAD_MUTEX_E; |
4487 | | |
4488 | | return ret; |
4489 | | } |
4490 | | |
4491 | | #elif defined(WOLFSSL_NUCLEUS_1_2) |
4492 | | |
4493 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4494 | | { |
4495 | | /* Call the Nucleus function to create the semaphore */ |
4496 | | if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1, |
4497 | | NU_PRIORITY) == NU_SUCCESS) { |
4498 | | return 0; |
4499 | | } |
4500 | | |
4501 | | return BAD_MUTEX_E; |
4502 | | } |
4503 | | |
4504 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4505 | | { |
4506 | | if (NU_Delete_Semaphore(m) == NU_SUCCESS) |
4507 | | return 0; |
4508 | | |
4509 | | return BAD_MUTEX_E; |
4510 | | } |
4511 | | |
4512 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4513 | | { |
4514 | | /* passing suspend task option */ |
4515 | | if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS) |
4516 | | return 0; |
4517 | | |
4518 | | return BAD_MUTEX_E; |
4519 | | } |
4520 | | |
4521 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4522 | | { |
4523 | | if (NU_Release_Semaphore(m) == NU_SUCCESS) |
4524 | | return 0; |
4525 | | |
4526 | | return BAD_MUTEX_E; |
4527 | | } |
4528 | | |
4529 | | #elif defined(WOLFSSL_ZEPHYR) |
4530 | | |
4531 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4532 | | { |
4533 | | k_mutex_init(m); |
4534 | | |
4535 | | return 0; |
4536 | | } |
4537 | | |
4538 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4539 | | { |
4540 | | return 0; |
4541 | | } |
4542 | | |
4543 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4544 | | { |
4545 | | int ret = 0; |
4546 | | |
4547 | | if (k_mutex_lock(m, K_FOREVER) != 0) |
4548 | | ret = BAD_MUTEX_E; |
4549 | | |
4550 | | return ret; |
4551 | | } |
4552 | | |
4553 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4554 | | { |
4555 | | k_mutex_unlock(m); |
4556 | | |
4557 | | return 0; |
4558 | | } |
4559 | | |
4560 | | #elif defined(WOLFSSL_TELIT_M2MB) |
4561 | | |
4562 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4563 | | { |
4564 | | M2MB_OS_RESULT_E osRes; |
4565 | | M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle; |
4566 | | UINT32 inheritVal = 1; |
4567 | | |
4568 | | osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle, |
4569 | | CMDS_ARGS( |
4570 | | M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL, |
4571 | | M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx", |
4572 | | M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal |
4573 | | ) |
4574 | | ); |
4575 | | if (osRes != M2MB_OS_SUCCESS) { |
4576 | | return BAD_MUTEX_E; |
4577 | | } |
4578 | | |
4579 | | osRes = m2mb_os_mtx_init(m, &mtxAttrHandle); |
4580 | | if (osRes != M2MB_OS_SUCCESS) { |
4581 | | return BAD_MUTEX_E; |
4582 | | } |
4583 | | |
4584 | | return 0; |
4585 | | } |
4586 | | |
4587 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4588 | | { |
4589 | | M2MB_OS_RESULT_E osRes; |
4590 | | |
4591 | | if (m == NULL) |
4592 | | return BAD_MUTEX_E; |
4593 | | |
4594 | | osRes = m2mb_os_mtx_deinit(*m); |
4595 | | if (osRes != M2MB_OS_SUCCESS) { |
4596 | | return BAD_MUTEX_E; |
4597 | | } |
4598 | | |
4599 | | return 0; |
4600 | | } |
4601 | | |
4602 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4603 | | { |
4604 | | M2MB_OS_RESULT_E osRes; |
4605 | | |
4606 | | if (m == NULL) |
4607 | | return BAD_MUTEX_E; |
4608 | | |
4609 | | osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER); |
4610 | | if (osRes != M2MB_OS_SUCCESS) { |
4611 | | return BAD_MUTEX_E; |
4612 | | } |
4613 | | |
4614 | | return 0; |
4615 | | } |
4616 | | |
4617 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4618 | | { |
4619 | | M2MB_OS_RESULT_E osRes; |
4620 | | |
4621 | | if (m == NULL) |
4622 | | return BAD_MUTEX_E; |
4623 | | |
4624 | | osRes = m2mb_os_mtx_put(*m); |
4625 | | if (osRes != M2MB_OS_SUCCESS) { |
4626 | | return BAD_MUTEX_E; |
4627 | | } |
4628 | | |
4629 | | return 0; |
4630 | | } |
4631 | | |
4632 | | #elif defined(WOLFSSL_EMBOS) |
4633 | | |
4634 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4635 | | { |
4636 | | int ret; |
4637 | | |
4638 | | OS_MUTEX_Create((OS_MUTEX*) m); |
4639 | | if (m != NULL) |
4640 | | ret = 0; |
4641 | | else |
4642 | | ret = BAD_MUTEX_E; |
4643 | | |
4644 | | return ret; |
4645 | | } |
4646 | | |
4647 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4648 | | { |
4649 | | OS_MUTEX_Delete((OS_MUTEX*) m); |
4650 | | return 0; |
4651 | | } |
4652 | | |
4653 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4654 | | { |
4655 | | OS_MUTEX_LockBlocked((OS_MUTEX*) m); |
4656 | | return 0; |
4657 | | } |
4658 | | |
4659 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4660 | | { |
4661 | | OS_MUTEX_Unlock((OS_MUTEX*) m); |
4662 | | return 0; |
4663 | | } |
4664 | | |
4665 | | #elif defined(NETOS) |
4666 | | |
4667 | | int wc_InitMutex(wolfSSL_Mutex* m) |
4668 | | { |
4669 | | if (tx_mutex_create(&ready->mutex, "wolfSSL Lock", TX_INHERIT) |
4670 | | == TX_SUCCESS) |
4671 | | return 0; |
4672 | | else |
4673 | | return BAD_MUTEX_E; |
4674 | | } |
4675 | | |
4676 | | int wc_FreeMutex(wolfSSL_Mutex* m) |
4677 | | { |
4678 | | if (tx_mutex_delete(&ready->mutex) == TX_SUCCESS) |
4679 | | return 0; |
4680 | | else |
4681 | | return BAD_MUTEX_E; |
4682 | | } |
4683 | | |
4684 | | int wc_LockMutex(wolfSSL_Mutex* m) |
4685 | | { |
4686 | | |
4687 | | } |
4688 | | |
4689 | | int wc_UnLockMutex(wolfSSL_Mutex* m) |
4690 | | { |
4691 | | |
4692 | | } |
4693 | | |
4694 | | #elif defined(WOLFSSL_USER_MUTEX) |
4695 | | |
4696 | | /* Use user own mutex */ |
4697 | | |
4698 | | /* |
4699 | | int wc_InitMutex(wolfSSL_Mutex* m) { ... } |
4700 | | int wc_FreeMutex(wolfSSL_Mutex *m) { ... } |
4701 | | int wc_LockMutex(wolfSSL_Mutex *m) { ... } |
4702 | | int wc_UnLockMutex(wolfSSL_Mutex *m) { ... } |
4703 | | */ |
4704 | | |
4705 | | #else |
4706 | | #warning No mutex handling defined |
4707 | | |
4708 | | #endif |
4709 | | #if !defined(WOLFSSL_USE_RWLOCK) || defined(SINGLE_THREADED) || \ |
4710 | | (defined(WC_MUTEX_OPS_INLINE) && !defined(WC_RWLOCK_OPS_INLINE)) |
4711 | | int wc_InitRwLock(wolfSSL_RwLock* m) |
4712 | 6.15k | { |
4713 | 6.15k | return wc_InitMutex(m); |
4714 | 6.15k | } |
4715 | | |
4716 | | int wc_FreeRwLock(wolfSSL_RwLock* m) |
4717 | 6.15k | { |
4718 | 6.15k | return wc_FreeMutex(m); |
4719 | 6.15k | } |
4720 | | |
4721 | | int wc_LockRwLock_Wr(wolfSSL_RwLock* m) |
4722 | 75 | { |
4723 | 75 | return wc_LockMutex(m); |
4724 | 75 | } |
4725 | | |
4726 | | int wc_LockRwLock_Rd(wolfSSL_RwLock* m) |
4727 | 183 | { |
4728 | 183 | return wc_LockMutex(m); |
4729 | 183 | } |
4730 | | |
4731 | | int wc_UnLockRwLock(wolfSSL_RwLock* m) |
4732 | 258 | { |
4733 | 258 | return wc_UnLockMutex(m); |
4734 | 258 | } |
4735 | | #endif |
4736 | | |
4737 | | #ifndef NO_ASN_TIME |
4738 | | #if defined(_WIN32_WCE) |
4739 | | time_t windows_time(time_t* timer) |
4740 | | { |
4741 | | SYSTEMTIME sysTime; |
4742 | | FILETIME fTime; |
4743 | | ULARGE_INTEGER intTime; |
4744 | | |
4745 | | |
4746 | | GetSystemTime(&sysTime); |
4747 | | SystemTimeToFileTime(&sysTime, &fTime); |
4748 | | |
4749 | | XMEMCPY(&intTime, &fTime, sizeof(FILETIME)); |
4750 | | /* subtract EPOCH */ |
4751 | | intTime.QuadPart -= 0x19db1ded53e8000; |
4752 | | /* to secs */ |
4753 | | intTime.QuadPart /= 10000000; |
4754 | | |
4755 | | if (timer != NULL) |
4756 | | *timer = (time_t)intTime.QuadPart; |
4757 | | |
4758 | | return (time_t)intTime.QuadPart; |
4759 | | } |
4760 | | #endif /* _WIN32_WCE */ |
4761 | | |
4762 | | #if defined(WOLFSSL_APACHE_MYNEWT) |
4763 | | #include "os/os_time.h" |
4764 | | |
4765 | | time_t mynewt_time(time_t* timer) |
4766 | | { |
4767 | | time_t now; |
4768 | | struct os_timeval tv; |
4769 | | os_gettimeofday(&tv, NULL); |
4770 | | now = (time_t)tv.tv_sec; |
4771 | | if(timer != NULL) { |
4772 | | *timer = now; |
4773 | | } |
4774 | | return now; |
4775 | | } |
4776 | | #endif /* WOLFSSL_APACHE_MYNEWT */ |
4777 | | |
4778 | | #if defined(WOLFSSL_GMTIME) |
4779 | | struct tm* gmtime_r(const time_t* timer, struct tm *ret) |
4780 | | { |
4781 | | #define YEAR0 1900 |
4782 | | #define EPOCH_YEAR 1970 |
4783 | | #define SECS_DAY (24L * 60L * 60L) |
4784 | | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400))) |
4785 | | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) |
4786 | | |
4787 | | static const int _ytab[2][12] = |
4788 | | { |
4789 | | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
4790 | | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
4791 | | }; |
4792 | | |
4793 | | time_t secs = *timer; |
4794 | | unsigned long dayclock, dayno; |
4795 | | int year = EPOCH_YEAR; |
4796 | | |
4797 | | dayclock = (unsigned long)secs % SECS_DAY; |
4798 | | dayno = (unsigned long)secs / SECS_DAY; |
4799 | | |
4800 | | ret->tm_sec = (int) dayclock % 60; |
4801 | | ret->tm_min = (int)(dayclock % 3600) / 60; |
4802 | | ret->tm_hour = (int) dayclock / 3600; |
4803 | | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */ |
4804 | | |
4805 | | while(dayno >= (unsigned long)YEARSIZE(year)) { |
4806 | | dayno -= YEARSIZE(year); |
4807 | | year++; |
4808 | | } |
4809 | | |
4810 | | ret->tm_year = year - YEAR0; |
4811 | | ret->tm_yday = (int)dayno; |
4812 | | ret->tm_mon = 0; |
4813 | | |
4814 | | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) { |
4815 | | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon]; |
4816 | | ret->tm_mon++; |
4817 | | } |
4818 | | |
4819 | | ret->tm_mday = (int)++dayno; |
4820 | | #ifndef WOLFSSL_LINUXKM |
4821 | | ret->tm_isdst = 0; |
4822 | | #endif |
4823 | | |
4824 | | return ret; |
4825 | | } |
4826 | | |
4827 | | struct tm* gmtime(const time_t* timer) { |
4828 | | static struct tm st_time; |
4829 | | return gmtime_r(timer, &st_time); |
4830 | | } |
4831 | | |
4832 | | #endif /* WOLFSSL_GMTIME */ |
4833 | | |
4834 | | |
4835 | | #if defined(HAVE_RTP_SYS) |
4836 | | #define YEAR0 1900 |
4837 | | |
4838 | | struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */ |
4839 | | { |
4840 | | static struct tm st_time; |
4841 | | struct tm* ret = &st_time; |
4842 | | |
4843 | | DC_RTC_CALENDAR cal; |
4844 | | dc_rtc_time_get(&cal, TRUE); |
4845 | | |
4846 | | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */ |
4847 | | ret->tm_mon = cal.month - 1; /* gm starts at 0 */ |
4848 | | ret->tm_mday = cal.day; |
4849 | | ret->tm_hour = cal.hour; |
4850 | | ret->tm_min = cal.minute; |
4851 | | ret->tm_sec = cal.second; |
4852 | | |
4853 | | return ret; |
4854 | | } |
4855 | | |
4856 | | #endif /* HAVE_RTP_SYS */ |
4857 | | |
4858 | | |
4859 | | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) |
4860 | | |
4861 | | /* |
4862 | | * time() is just a stub in Microchip libraries. We need our own |
4863 | | * implementation. Use SNTP client to get seconds since epoch. |
4864 | | */ |
4865 | | time_t pic32_time(time_t* timer) |
4866 | | { |
4867 | | #ifdef MICROCHIP_TCPIP_V5 |
4868 | | DWORD sec = 0; |
4869 | | #else |
4870 | | word32 sec = 0; |
4871 | | #endif |
4872 | | |
4873 | | #ifdef MICROCHIP_MPLAB_HARMONY |
4874 | | sec = TCPIP_SNTP_UTCSecondsGet(); |
4875 | | #else |
4876 | | sec = SNTPGetUTCSeconds(); |
4877 | | #endif |
4878 | | |
4879 | | if (timer != NULL) |
4880 | | *timer = (time_t)sec; |
4881 | | |
4882 | | return (time_t)sec; |
4883 | | } |
4884 | | |
4885 | | #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */ |
4886 | | |
4887 | | #if defined(WOLFSSL_DEOS) || defined(WOLFSSL_DEOS_RTEMS) |
4888 | | |
4889 | | time_t deos_time(time_t* timer) |
4890 | | { |
4891 | | const word32 systemTickTimeInHz = 1000000 / systemTickInMicroseconds(); |
4892 | | const volatile word32 *systemTickPtr = systemTickPointer(); |
4893 | | |
4894 | | if (timer != NULL) |
4895 | | *timer = *systemTickPtr/systemTickTimeInHz; |
4896 | | |
4897 | | #if defined(CURRENT_UNIX_TIMESTAMP) |
4898 | | /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */ |
4899 | | return (time_t) (*systemTickPtr/systemTickTimeInHz) + CURRENT_UNIX_TIMESTAMP; |
4900 | | #else |
4901 | | return (time_t) *systemTickPtr/systemTickTimeInHz; |
4902 | | #endif |
4903 | | } |
4904 | | #endif /* WOLFSSL_DEOS || WOLFSSL_DEOS_RTEMS */ |
4905 | | |
4906 | | #if defined(FREESCALE_RTC) |
4907 | | #include "fsl_rtc.h" |
4908 | | time_t fsl_time(time_t* t) |
4909 | | { |
4910 | | *t = RTC_GetSecondsTimerCount(RTC); |
4911 | | return *t; |
4912 | | } |
4913 | | #endif |
4914 | | |
4915 | | #if defined(FREESCALE_SNVS_RTC) |
4916 | | time_t fsl_time(time_t* t) |
4917 | | { |
4918 | | struct tm tm_time; |
4919 | | time_t ret; |
4920 | | |
4921 | | snvs_hp_rtc_datetime_t rtcDate; |
4922 | | snvs_hp_rtc_config_t snvsRtcConfig; |
4923 | | |
4924 | | SNVS_HP_RTC_GetDefaultConfig(&snvsRtcConfig); |
4925 | | SNVS_HP_RTC_Init(SNVS, &snvsRtcConfig); |
4926 | | |
4927 | | SNVS_HP_RTC_GetDatetime(SNVS, &rtcDate); |
4928 | | |
4929 | | tm_time.tm_year = rtcDate.year; |
4930 | | tm_time.tm_mon = rtcDate.month; |
4931 | | tm_time.tm_mday = rtcDate.day; |
4932 | | tm_time.tm_hour = rtcDate.hour; |
4933 | | tm_time.tm_min = rtcDate.minute; |
4934 | | tm_time.tm_sec = rtcDate.second; |
4935 | | |
4936 | | ret = mktime(&tm_time); |
4937 | | if (t != NULL) |
4938 | | *t = ret; |
4939 | | return ret; |
4940 | | } |
4941 | | #endif |
4942 | | |
4943 | | #if defined(MICRIUM) |
4944 | | |
4945 | | time_t micrium_time(time_t* timer) |
4946 | | { |
4947 | | CLK_TS_SEC sec; |
4948 | | |
4949 | | Clk_GetTS_Unix(&sec); |
4950 | | |
4951 | | if (timer != NULL) |
4952 | | *timer = sec; |
4953 | | |
4954 | | return (time_t) sec; |
4955 | | } |
4956 | | |
4957 | | #endif /* MICRIUM */ |
4958 | | |
4959 | | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
4960 | | |
4961 | | time_t mqx_time(time_t* timer) |
4962 | | { |
4963 | | TIME_STRUCT time_s; |
4964 | | |
4965 | | _time_get(&time_s); |
4966 | | |
4967 | | if (timer != NULL) |
4968 | | *timer = (time_t)time_s.SECONDS; |
4969 | | |
4970 | | return (time_t)time_s.SECONDS; |
4971 | | } |
4972 | | |
4973 | | #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */ |
4974 | | |
4975 | | #if defined(MAX3266X_RTC) |
4976 | | #define XTIME wc_MXC_RTC_Time |
4977 | | #endif |
4978 | | |
4979 | | #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME) |
4980 | | |
4981 | | time_t XTIME(time_t * timer) |
4982 | | { |
4983 | | time_t sec = 0; |
4984 | | |
4985 | | sec = (time_t) Seconds_get(); |
4986 | | |
4987 | | if (timer != NULL) |
4988 | | *timer = sec; |
4989 | | |
4990 | | return sec; |
4991 | | } |
4992 | | |
4993 | | #endif /* WOLFSSL_TIRTOS */ |
4994 | | |
4995 | | #if defined(WOLFSSL_XILINX) |
4996 | | #include "xrtcpsu.h" |
4997 | | |
4998 | | time_t xilinx_time(time_t * timer) |
4999 | | { |
5000 | | time_t sec = 0; |
5001 | | XRtcPsu_Config* con; |
5002 | | XRtcPsu rtc; |
5003 | | |
5004 | | con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID); |
5005 | | if (con != NULL) { |
5006 | | if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) { |
5007 | | sec = (time_t)XRtcPsu_GetCurrentTime(&rtc); |
5008 | | } |
5009 | | else { |
5010 | | WOLFSSL_MSG("Unable to initialize RTC"); |
5011 | | } |
5012 | | } |
5013 | | |
5014 | | if (timer != NULL) |
5015 | | *timer = sec; |
5016 | | |
5017 | | return sec; |
5018 | | } |
5019 | | |
5020 | | #endif /* WOLFSSL_XILINX */ |
5021 | | |
5022 | | #if defined(WOLFSSL_ZEPHYR) |
5023 | | |
5024 | | time_t z_time(time_t * timer) |
5025 | | { |
5026 | | #if defined(CONFIG_BOARD_NATIVE_POSIX) || defined(CONFIG_BOARD_NATIVE_SIM) |
5027 | | |
5028 | | /* native_sim: read the simulator RTC for a real host wall-clock. The |
5029 | | * real-target RTC/libc gate below is compiled out under the host libc. */ |
5030 | | uint32_t nsec = 0; |
5031 | | uint64_t sec = 0; |
5032 | | |
5033 | | native_rtc_gettime(RTC_CLOCK_PSEUDOHOSTREALTIME, &nsec, &sec); |
5034 | | |
5035 | | if (timer != NULL) |
5036 | | *timer = sec; |
5037 | | |
5038 | | return sec; |
5039 | | |
5040 | | #else |
5041 | | |
5042 | | struct timespec ts = { 0 }; |
5043 | | |
5044 | | #if defined(CONFIG_RTC) && \ |
5045 | | (defined(CONFIG_PICOLIBC) || defined(CONFIG_NEWLIB_LIBC)) |
5046 | | |
5047 | | /* Try to obtain the actual time from an RTC */ |
5048 | | static const struct device *rtc = DEVICE_DT_GET(DT_NODELABEL(rtc)); |
5049 | | |
5050 | | if (device_is_ready(rtc)) { |
5051 | | struct rtc_time rtc_time; |
5052 | | struct tm *tm_time = rtc_time_to_tm(&rtc_time); |
5053 | | |
5054 | | int ret = rtc_get_time(rtc, &rtc_time); |
5055 | | |
5056 | | if (ret == 0) { |
5057 | | time_t epochTime = mktime(tm_time); |
5058 | | |
5059 | | if (timer != NULL) |
5060 | | *timer = epochTime; |
5061 | | |
5062 | | return epochTime; |
5063 | | } |
5064 | | } |
5065 | | #endif /* CONFIG_RTC && (CONFIG_PICOLIBC || CONFIG_NEWLIB_LIBC) */ |
5066 | | |
5067 | | /* Fallback to uptime since boot. This works for relative times, but |
5068 | | * not for ASN.1 date validation */ |
5069 | | #ifdef SYS_CLOCK_REALTIME |
5070 | | if (sys_clock_gettime(SYS_CLOCK_REALTIME, &ts) == 0) |
5071 | | #else |
5072 | | if (clock_gettime(CLOCK_REALTIME, &ts) == 0) |
5073 | | #endif |
5074 | | if (timer != NULL) |
5075 | | *timer = ts.tv_sec; |
5076 | | |
5077 | | return ts.tv_sec; |
5078 | | |
5079 | | #endif /* CONFIG_BOARD_NATIVE_POSIX || CONFIG_BOARD_NATIVE_SIM */ |
5080 | | } |
5081 | | |
5082 | | #endif /* WOLFSSL_ZEPHYR */ |
5083 | | |
5084 | | |
5085 | | #if defined(WOLFSSL_WICED) |
5086 | | #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME |
5087 | | #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time. |
5088 | | #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */ |
5089 | | |
5090 | | time_t wiced_pseudo_unix_epoch_time(time_t * timer) |
5091 | | { |
5092 | | time_t epoch_time; |
5093 | | /* The time() function return uptime on WICED platform. */ |
5094 | | epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME; |
5095 | | |
5096 | | if (timer != NULL) { |
5097 | | *timer = epoch_time; |
5098 | | } |
5099 | | return epoch_time; |
5100 | | } |
5101 | | #endif /* WOLFSSL_WICED */ |
5102 | | |
5103 | | #ifdef WOLFSSL_TELIT_M2MB |
5104 | | time_t m2mb_xtime(time_t * timer) |
5105 | | { |
5106 | | time_t myTime = 0; |
5107 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
5108 | | if (fd != -1) { |
5109 | | M2MB_RTC_TIMEVAL_T timeval; |
5110 | | |
5111 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
5112 | | |
5113 | | myTime = timeval.sec; |
5114 | | |
5115 | | m2mb_rtc_close(fd); |
5116 | | } |
5117 | | return myTime; |
5118 | | } |
5119 | | #ifdef WOLFSSL_TLS13 |
5120 | | time_t m2mb_xtime_ms(time_t * timer) |
5121 | | { |
5122 | | time_t myTime = 0; |
5123 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
5124 | | if (fd != -1) { |
5125 | | M2MB_RTC_TIMEVAL_T timeval; |
5126 | | |
5127 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
5128 | | |
5129 | | myTime = timeval.sec + timeval.msec; |
5130 | | |
5131 | | m2mb_rtc_close(fd); |
5132 | | } |
5133 | | return myTime; |
5134 | | } |
5135 | | #endif /* WOLFSSL_TLS13 */ |
5136 | | #ifndef NO_CRYPT_BENCHMARK |
5137 | | double m2mb_xtime_bench(int reset) |
5138 | | { |
5139 | | double myTime = 0; |
5140 | | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0); |
5141 | | if (fd != -1) { |
5142 | | M2MB_RTC_TIMEVAL_T timeval; |
5143 | | |
5144 | | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval); |
5145 | | |
5146 | | myTime = (double)timeval.sec + ((double)timeval.msec / 1000); |
5147 | | |
5148 | | m2mb_rtc_close(fd); |
5149 | | } |
5150 | | return myTime; |
5151 | | } |
5152 | | #endif /* !NO_CRYPT_BENCHMARK */ |
5153 | | #endif /* WOLFSSL_TELIT_M2MB */ |
5154 | | |
5155 | | |
5156 | | #if defined(WOLFSSL_LINUXKM) |
5157 | | /* linuxkm XTIME() implementation lives in linuxkm/module_hooks.c */ |
5158 | | #endif /* WOLFSSL_LINUXKM */ |
5159 | | |
5160 | | #ifdef HAL_RTC_MODULE_ENABLED |
5161 | | extern RTC_HandleTypeDef hrtc; |
5162 | | time_t stm32_hal_time(time_t *t1) |
5163 | | { |
5164 | | struct tm tm_time; |
5165 | | time_t ret; |
5166 | | RTC_TimeTypeDef time; |
5167 | | RTC_DateTypeDef date; |
5168 | | |
5169 | | XMEMSET(&tm_time, 0, sizeof(struct tm)); |
5170 | | |
5171 | | /* order of GetTime followed by GetDate required here due to STM32 HW |
5172 | | * requirement */ |
5173 | | HAL_RTC_GetTime(&hrtc, &time, FORMAT_BIN); |
5174 | | HAL_RTC_GetDate(&hrtc, &date, FORMAT_BIN); |
5175 | | |
5176 | | /* RTC year is 0-99 and "struct tm" is 1900+, so assume after year 2000 */ |
5177 | | tm_time.tm_year = date.Year + 100; |
5178 | | /* RTC month is 1-12 and "struct tm" is 0-12, so subtract 1 */ |
5179 | | tm_time.tm_mon = date.Month - 1; |
5180 | | tm_time.tm_mday = date.Date; |
5181 | | tm_time.tm_hour = time.Hours; |
5182 | | tm_time.tm_min = time.Minutes; |
5183 | | tm_time.tm_sec = time.Seconds; |
5184 | | |
5185 | | ret = mktime(&tm_time); |
5186 | | if (t1 != NULL) |
5187 | | *t1 = ret; |
5188 | | return ret; |
5189 | | } |
5190 | | #endif /* HAL_RTC_MODULE_ENABLED */ |
5191 | | |
5192 | | #endif /* !NO_ASN_TIME */ |
5193 | | |
5194 | | #if (!defined(WOLFSSL_LEANPSK) && !defined(STRING_USER)) || \ |
5195 | | defined(USE_WOLF_STRNSTR) |
5196 | | char* wolfSSL_strnstr(const char* s1, const char* s2, size_t n) |
5197 | 72.3k | { |
5198 | 72.3k | size_t s2_len = XSTRLEN(s2); |
5199 | | |
5200 | 72.3k | if (s2_len == 0) |
5201 | 0 | return (char *)(wc_ptr_t)s1; |
5202 | | |
5203 | 15.5M | while (n >= s2_len && s1[0]) { |
5204 | 15.5M | if (s1[0] == s2[0]) |
5205 | 667k | if (XMEMCMP(s1, s2, s2_len) == 0) |
5206 | 66.5k | return (char *)(wc_ptr_t)s1; |
5207 | 15.5M | s1++; |
5208 | 15.5M | n--; |
5209 | 15.5M | } |
5210 | | |
5211 | 5.79k | return NULL; |
5212 | 72.3k | } |
5213 | | #endif |
5214 | | |
5215 | | |
5216 | | /* custom memory wrappers */ |
5217 | | #ifdef WOLFSSL_NUCLEUS_1_2 |
5218 | | |
5219 | | /* system memory pool */ |
5220 | | extern NU_MEMORY_POOL System_Memory; |
5221 | | |
5222 | | void* nucleus_malloc(unsigned long size, void* heap, int type) |
5223 | | { |
5224 | | STATUS status; |
5225 | | void* stack_ptr; |
5226 | | |
5227 | | status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size, |
5228 | | NU_NO_SUSPEND); |
5229 | | if (status == NU_SUCCESS) { |
5230 | | return 0; |
5231 | | } else { |
5232 | | return stack_ptr; |
5233 | | } |
5234 | | } |
5235 | | |
5236 | | void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type) |
5237 | | { |
5238 | | DM_HEADER* old_header; |
5239 | | word32 old_size, copy_size; |
5240 | | void* new_mem; |
5241 | | |
5242 | | /* if ptr is NULL, behave like malloc */ |
5243 | | new_mem = nucleus_malloc(size, NULL, 0); |
5244 | | if (new_mem == 0 || ptr == 0) { |
5245 | | return new_mem; |
5246 | | } |
5247 | | |
5248 | | /* calculate old memory block size */ |
5249 | | /* mem pointers stored in block headers (ref dm_defs.h) */ |
5250 | | old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD); |
5251 | | old_size = (byte*)old_header->dm_next_memory - (byte*)ptr; |
5252 | | |
5253 | | /* copy old to new */ |
5254 | | if (old_size < size) { |
5255 | | copy_size = old_size; |
5256 | | } else { |
5257 | | copy_size = size; |
5258 | | } |
5259 | | XMEMCPY(new_mem, ptr, copy_size); |
5260 | | |
5261 | | /* free old */ |
5262 | | nucleus_free(ptr, NULL, 0); |
5263 | | |
5264 | | return new_mem; |
5265 | | } |
5266 | | |
5267 | | void nucleus_free(void* ptr, void* heap, int type) |
5268 | | { |
5269 | | if (ptr != NULL) |
5270 | | NU_Deallocate_Memory(ptr); |
5271 | | } |
5272 | | |
5273 | | #endif /* WOLFSSL_NUCLEUS_1_2 */ |
5274 | | |
5275 | | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) |
5276 | | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */ |
5277 | | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */ |
5278 | | #endif |
5279 | | |
5280 | | #ifndef SINGLE_THREADED |
5281 | | |
5282 | | /* Environment-specific multi-thread implementation check */ |
5283 | | #if defined(__WATCOMC__) |
5284 | | |
5285 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5286 | | THREAD_CB cb, void* arg) |
5287 | | { |
5288 | | if (thread == NULL || cb == NULL) |
5289 | | return BAD_FUNC_ARG; |
5290 | | #if defined(__OS2__) |
5291 | | *thread = _beginthread(cb, NULL, 0, arg); |
5292 | | if (*thread == INVALID_THREAD_VAL) { |
5293 | | return MEMORY_E; |
5294 | | } |
5295 | | #elif defined(__NT__) |
5296 | | /* Use _beginthreadex instead of _beginthread because of: |
5297 | | * _beginthreadex is safer to use than _beginthread. If the thread |
5298 | | * that's generated by _beginthread exits quickly, the handle that's |
5299 | | * returned to the caller of _beginthread might be invalid or point |
5300 | | * to another thread. However, the handle that's returned by |
5301 | | * _beginthreadex has to be closed by the caller of _beginthreadex, |
5302 | | * so it's guaranteed to be a valid handle if _beginthreadex didn't |
5303 | | * return an error.*/ |
5304 | | *thread = _beginthreadex(NULL, 0, cb, arg, 0, NULL); |
5305 | | if (*thread == 0) { |
5306 | | *thread = INVALID_THREAD_VAL; |
5307 | | return MEMORY_E; |
5308 | | } |
5309 | | #elif defined(__LINUX__) |
5310 | | if (pthread_create(thread, NULL, cb, arg)) |
5311 | | return MEMORY_E; |
5312 | | #endif |
5313 | | return 0; |
5314 | | } |
5315 | | |
5316 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5317 | | { |
5318 | | int ret = 0; |
5319 | | |
5320 | | if (thread == INVALID_THREAD_VAL) |
5321 | | return BAD_FUNC_ARG; |
5322 | | #if defined(__OS2__) |
5323 | | DosWaitThread(&thread, DCWW_WAIT); |
5324 | | #elif defined(__NT__) |
5325 | | /* We still want to attempt to close the thread handle even on error */ |
5326 | | if (WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_FAILED) |
5327 | | ret = MEMORY_E; |
5328 | | if (CloseHandle((HANDLE)thread) == 0) |
5329 | | ret = MEMORY_E; |
5330 | | #elif defined(__LINUX__) |
5331 | | if (pthread_join(thread, NULL) != 0) |
5332 | | ret = MEMORY_E; |
5333 | | #endif |
5334 | | return ret; |
5335 | | } |
5336 | | |
5337 | | #if defined(WOLFSSL_THREAD_NO_JOIN) |
5338 | | int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg) |
5339 | | { |
5340 | | THREAD_TYPE thread; |
5341 | | int ret = 0; |
5342 | | |
5343 | | if (cb == NULL) |
5344 | | return BAD_FUNC_ARG; |
5345 | | #if defined(__OS2__) |
5346 | | thread = _beginthread(cb, NULL, 0, arg); |
5347 | | if (thread == INVALID_THREAD_VAL) |
5348 | | ret = MEMORY_E; |
5349 | | #elif defined(__NT__) |
5350 | | thread = _beginthread(cb, 0, arg); |
5351 | | if (thread == -1L) |
5352 | | ret = MEMORY_E; |
5353 | | #elif defined(__LINUX__) |
5354 | | XMEMSET(&thread, 0, sizeof(thread)); |
5355 | | ret = wolfSSL_NewThread(&thread, cb, arg); |
5356 | | if (ret == 0) |
5357 | | ret = pthread_detach(thread); |
5358 | | #endif |
5359 | | return ret; |
5360 | | } |
5361 | | #endif |
5362 | | |
5363 | | #ifdef WOLFSSL_COND |
5364 | | int wolfSSL_CondInit(COND_TYPE* cond) |
5365 | | { |
5366 | | if (cond == NULL) |
5367 | | return BAD_FUNC_ARG; |
5368 | | #if defined(__MACH__) |
5369 | | cond->cond = dispatch_semaphore_create(0); |
5370 | | if (cond->cond == NULL) |
5371 | | return MEMORY_E; |
5372 | | |
5373 | | /* dispatch_release() fails hard, with Trace/BPT trap signal, if the |
5374 | | * sem's internal count is less than the value passed in with |
5375 | | * dispatch_semaphore_create(). work around this by initializing |
5376 | | * with 0, then incrementing it afterwards. |
5377 | | */ |
5378 | | if (dispatch_semaphore_signal(s->sem) < 0) { |
5379 | | dispatch_release(s->sem); |
5380 | | return MEMORY_E; |
5381 | | } |
5382 | | #elif defined(__OS2__) |
5383 | | DosCreateMutexSem( NULL, &cond->mutex, 0, FALSE ); |
5384 | | DosCreateEventSem( NULL, &cond->cond, DCE_POSTONE, FALSE ); |
5385 | | #elif defined(__NT__) |
5386 | | cond->cond = CreateEventA(NULL, FALSE, FALSE, NULL); |
5387 | | if (cond->cond == NULL) |
5388 | | return MEMORY_E; |
5389 | | |
5390 | | if (wc_InitMutex(&cond->mutex) != 0) { |
5391 | | if (CloseHandle(cond->cond) == 0) |
5392 | | return MEMORY_E; |
5393 | | return MEMORY_E; |
5394 | | } |
5395 | | #elif defined(__LINUX__) |
5396 | | if (pthread_mutex_init(&cond->mutex, NULL) != 0) |
5397 | | return MEMORY_E; |
5398 | | |
5399 | | if (pthread_cond_init(&cond->cond, NULL) != 0) { |
5400 | | /* Keep compilers happy that we are using the return code */ |
5401 | | if (pthread_mutex_destroy(&cond->mutex) != 0) |
5402 | | return MEMORY_E; |
5403 | | return MEMORY_E; |
5404 | | } |
5405 | | #endif |
5406 | | return 0; |
5407 | | } |
5408 | | |
5409 | | int wolfSSL_CondFree(COND_TYPE* cond) |
5410 | | { |
5411 | | if (cond == NULL) |
5412 | | return BAD_FUNC_ARG; |
5413 | | #if defined(__MACH__) |
5414 | | dispatch_release(cond->cond); |
5415 | | #elif defined(__OS2__) |
5416 | | DosCloseMutexSem(cond->mutex); |
5417 | | DosCloseEventSem(cond->cond); |
5418 | | #elif defined(__NT__) |
5419 | | if (CloseHandle(cond->cond) == 0) |
5420 | | return MEMORY_E; |
5421 | | #elif defined(__LINUX__) |
5422 | | if (pthread_mutex_destroy(&cond->mutex) != 0) |
5423 | | return MEMORY_E; |
5424 | | |
5425 | | if (pthread_cond_destroy(&cond->cond) != 0) |
5426 | | return MEMORY_E; |
5427 | | #endif |
5428 | | return 0; |
5429 | | } |
5430 | | |
5431 | | int wolfSSL_CondStart(COND_TYPE* cond) |
5432 | | { |
5433 | | if (cond == NULL) |
5434 | | return BAD_FUNC_ARG; |
5435 | | #if defined(__MACH__) |
5436 | | #elif defined(__OS2__) |
5437 | | #elif defined(__NT__) |
5438 | | if (wc_LockMutex(&cond->mutex) != 0) |
5439 | | return BAD_MUTEX_E; |
5440 | | #elif defined(__LINUX__) |
5441 | | if (pthread_mutex_lock(&cond->mutex) != 0) |
5442 | | return BAD_MUTEX_E; |
5443 | | #endif |
5444 | | return 0; |
5445 | | } |
5446 | | |
5447 | | int wolfSSL_CondSignal(COND_TYPE* cond) |
5448 | | { |
5449 | | if (cond == NULL) |
5450 | | return BAD_FUNC_ARG; |
5451 | | #if defined(__MACH__) |
5452 | | dispatch_semaphore_signal(cond->cond); |
5453 | | #elif defined(__OS2__) |
5454 | | #elif defined(__NT__) |
5455 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5456 | | return BAD_MUTEX_E; |
5457 | | |
5458 | | if (SetEvent(cond->cond) == 0) |
5459 | | return MEMORY_E; |
5460 | | |
5461 | | if (wc_LockMutex(&cond->mutex) != 0) |
5462 | | return BAD_MUTEX_E; |
5463 | | #elif defined(__LINUX__) |
5464 | | if (pthread_cond_signal(&cond->cond) != 0) |
5465 | | return MEMORY_E; |
5466 | | #endif |
5467 | | return 0; |
5468 | | } |
5469 | | |
5470 | | int wolfSSL_CondWait(COND_TYPE* cond) |
5471 | | { |
5472 | | if (cond == NULL) |
5473 | | return BAD_FUNC_ARG; |
5474 | | #if defined(__MACH__) |
5475 | | dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER); |
5476 | | #elif defined(__OS2__) |
5477 | | #elif defined(__NT__) |
5478 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5479 | | return BAD_MUTEX_E; |
5480 | | |
5481 | | if (WaitForSingleObject(cond->cond, INFINITE) == WAIT_FAILED) |
5482 | | return MEMORY_E; |
5483 | | |
5484 | | if (wc_LockMutex(&cond->mutex) != 0) |
5485 | | return BAD_MUTEX_E; |
5486 | | #elif defined(__LINUX__) |
5487 | | if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) |
5488 | | return MEMORY_E; |
5489 | | #endif |
5490 | | return 0; |
5491 | | } |
5492 | | |
5493 | | int wolfSSL_CondEnd(COND_TYPE* cond) |
5494 | | { |
5495 | | if (cond == NULL) |
5496 | | return BAD_FUNC_ARG; |
5497 | | #if defined(__OS2__) |
5498 | | #elif defined(__NT__) |
5499 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5500 | | return BAD_MUTEX_E; |
5501 | | #elif defined(__LINUX__) |
5502 | | if (pthread_mutex_unlock(&cond->mutex) != 0) |
5503 | | return BAD_MUTEX_E; |
5504 | | #endif |
5505 | | return 0; |
5506 | | } |
5507 | | #endif /* WOLFSSL_COND */ |
5508 | | |
5509 | | |
5510 | | #elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS) && \ |
5511 | | !defined(_WIN32_WCE) |
5512 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5513 | | THREAD_CB cb, void* arg) |
5514 | | { |
5515 | | if (thread == NULL || cb == NULL) |
5516 | | return BAD_FUNC_ARG; |
5517 | | |
5518 | | /* Use _beginthreadex instead of _beginthread because of: |
5519 | | * _beginthreadex is safer to use than _beginthread. If the thread |
5520 | | * that's generated by _beginthread exits quickly, the handle that's |
5521 | | * returned to the caller of _beginthread might be invalid or point |
5522 | | * to another thread. However, the handle that's returned by |
5523 | | * _beginthreadex has to be closed by the caller of _beginthreadex, |
5524 | | * so it's guaranteed to be a valid handle if _beginthreadex didn't |
5525 | | * return an error.*/ |
5526 | | *thread = _beginthreadex(NULL, 0, cb, arg, 0, NULL); |
5527 | | if (*thread == 0) { |
5528 | | *thread = INVALID_THREAD_VAL; |
5529 | | return MEMORY_E; |
5530 | | } |
5531 | | |
5532 | | return 0; |
5533 | | } |
5534 | | |
5535 | | #ifdef WOLFSSL_THREAD_NO_JOIN |
5536 | | int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg) |
5537 | | { |
5538 | | THREAD_TYPE thread; |
5539 | | |
5540 | | if (cb == NULL) |
5541 | | return BAD_FUNC_ARG; |
5542 | | |
5543 | | thread = _beginthread(cb, 0, arg); |
5544 | | if (thread == -1L) { |
5545 | | return MEMORY_E; |
5546 | | } |
5547 | | |
5548 | | return 0; |
5549 | | } |
5550 | | #endif |
5551 | | |
5552 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5553 | | { |
5554 | | int ret = 0; |
5555 | | |
5556 | | if (thread == INVALID_THREAD_VAL) |
5557 | | return BAD_FUNC_ARG; |
5558 | | |
5559 | | /* We still want to attempt to close the thread handle even on error */ |
5560 | | if (WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_FAILED) |
5561 | | ret = MEMORY_E; |
5562 | | |
5563 | | if (CloseHandle((HANDLE)thread) == 0) |
5564 | | ret = MEMORY_E; |
5565 | | |
5566 | | return ret; |
5567 | | } |
5568 | | |
5569 | | #ifdef WOLFSSL_COND |
5570 | | int wolfSSL_CondInit(COND_TYPE* cond) |
5571 | | { |
5572 | | if (cond == NULL) |
5573 | | return BAD_FUNC_ARG; |
5574 | | |
5575 | | cond->cond = CreateEventA(NULL, FALSE, FALSE, NULL); |
5576 | | if (cond->cond == NULL) |
5577 | | return MEMORY_E; |
5578 | | |
5579 | | if (wc_InitMutex(&cond->mutex) != 0) { |
5580 | | if (CloseHandle(cond->cond) == 0) |
5581 | | return MEMORY_E; |
5582 | | return MEMORY_E; |
5583 | | } |
5584 | | |
5585 | | return 0; |
5586 | | } |
5587 | | |
5588 | | int wolfSSL_CondFree(COND_TYPE* cond) |
5589 | | { |
5590 | | if (cond == NULL) |
5591 | | return BAD_FUNC_ARG; |
5592 | | |
5593 | | if (CloseHandle(cond->cond) == 0) |
5594 | | return MEMORY_E; |
5595 | | |
5596 | | return 0; |
5597 | | } |
5598 | | |
5599 | | int wolfSSL_CondStart(COND_TYPE* cond) |
5600 | | { |
5601 | | if (cond == NULL) |
5602 | | return BAD_FUNC_ARG; |
5603 | | |
5604 | | if (wc_LockMutex(&cond->mutex) != 0) |
5605 | | return BAD_MUTEX_E; |
5606 | | |
5607 | | return 0; |
5608 | | } |
5609 | | |
5610 | | int wolfSSL_CondSignal(COND_TYPE* cond) |
5611 | | { |
5612 | | if (cond == NULL) |
5613 | | return BAD_FUNC_ARG; |
5614 | | |
5615 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5616 | | return BAD_MUTEX_E; |
5617 | | |
5618 | | if (SetEvent(cond->cond) == 0) |
5619 | | return MEMORY_E; |
5620 | | |
5621 | | if (wc_LockMutex(&cond->mutex) != 0) |
5622 | | return BAD_MUTEX_E; |
5623 | | |
5624 | | return 0; |
5625 | | } |
5626 | | |
5627 | | int wolfSSL_CondWait(COND_TYPE* cond) |
5628 | | { |
5629 | | if (cond == NULL) |
5630 | | return BAD_FUNC_ARG; |
5631 | | |
5632 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5633 | | return BAD_MUTEX_E; |
5634 | | |
5635 | | if (WaitForSingleObject(cond->cond, INFINITE) == WAIT_FAILED) |
5636 | | return MEMORY_E; |
5637 | | |
5638 | | if (wc_LockMutex(&cond->mutex) != 0) |
5639 | | return BAD_MUTEX_E; |
5640 | | |
5641 | | return 0; |
5642 | | } |
5643 | | |
5644 | | int wolfSSL_CondEnd(COND_TYPE* cond) |
5645 | | { |
5646 | | if (cond == NULL) |
5647 | | return BAD_FUNC_ARG; |
5648 | | |
5649 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5650 | | return BAD_MUTEX_E; |
5651 | | |
5652 | | return 0; |
5653 | | } |
5654 | | #endif /* WOLFSSL_COND */ |
5655 | | |
5656 | | #elif defined(WOLFSSL_TIRTOS) |
5657 | | |
5658 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5659 | | THREAD_CB cb, void* arg) |
5660 | | { |
5661 | | /* Initialize the defaults and set the parameters. */ |
5662 | | Task_Params taskParams; |
5663 | | Task_Params_init(&taskParams); |
5664 | | taskParams.arg0 = (UArg)arg; |
5665 | | taskParams.stackSize = 65535; |
5666 | | *thread = Task_create((Task_FuncPtr)cb, &taskParams, NULL); |
5667 | | if (*thread == NULL) { |
5668 | | return MEMORY_E; |
5669 | | } |
5670 | | Task_yield(); |
5671 | | return 0; |
5672 | | } |
5673 | | |
5674 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5675 | | { |
5676 | | while(1) { |
5677 | | if (Task_getMode(thread) == Task_Mode_TERMINATED) { |
5678 | | Task_sleep(5); |
5679 | | break; |
5680 | | } |
5681 | | Task_yield(); |
5682 | | } |
5683 | | return 0; |
5684 | | } |
5685 | | |
5686 | | #elif defined(NETOS) |
5687 | | |
5688 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5689 | | THREAD_CB cb, void* arg) |
5690 | | { |
5691 | | /* For backwards compatibility allow using this declaration as well. */ |
5692 | | #ifdef TESTSUITE_THREAD_STACK_SZ |
5693 | | #define WOLFSSL_NETOS_STACK_SZ TESTSUITE_THREAD_STACK_SZ |
5694 | | #endif |
5695 | | /* This can be adjusted by defining in user_settings.h, will default to |
5696 | | * 65k in the event it is undefined */ |
5697 | | #ifndef WOLFSSL_NETOS_STACK_SZ |
5698 | | #define WOLFSSL_NETOS_STACK_SZ 65535 |
5699 | | #endif |
5700 | | int result; |
5701 | | |
5702 | | if (thread == NULL || cb == NULL) |
5703 | | return BAD_FUNC_ARG; |
5704 | | |
5705 | | XMEMSET(thread, 0, sizeof(*thread)); |
5706 | | |
5707 | | thread->tid = (TX_THREAD *)XMALLOC(sizeof(TX_THREAD), NULL, |
5708 | | DYNAMIC_TYPE_OS_BUF); |
5709 | | if (thread->tid == NULL) |
5710 | | return MEMORY_E; |
5711 | | XMEMSET(thread->tid, 0, sizeof(TX_THREAD)); |
5712 | | |
5713 | | thread->threadStack = (void *)XMALLOC(WOLFSSL_NETOS_STACK_SZ, NULL, |
5714 | | DYNAMIC_TYPE_OS_BUF); |
5715 | | if (thread->threadStack == NULL) { |
5716 | | XFREE(thread->tid, NULL, DYNAMIC_TYPE_OS_BUF); |
5717 | | thread->tid = NULL; |
5718 | | return MEMORY_E; |
5719 | | } |
5720 | | |
5721 | | /* first create the idle thread: |
5722 | | * ARGS: |
5723 | | * Param1: pointer to thread |
5724 | | * Param2: name |
5725 | | * Param3 and 4: entry function and input |
5726 | | * Param5: pointer to thread stack |
5727 | | * Param6: stack size |
5728 | | * Param7 and 8: priority level and preempt threshold |
5729 | | * Param9 and 10: time slice and auto-start indicator */ |
5730 | | result = tx_thread_create(thread->tid, |
5731 | | "wolfSSL thread", |
5732 | | (entry_functionType)cb, (ULONG)arg, |
5733 | | thread->threadStack, |
5734 | | WOLFSSL_NETOS_STACK_SZ, |
5735 | | 2, 2, |
5736 | | 1, TX_AUTO_START); |
5737 | | if (result != TX_SUCCESS) { |
5738 | | XFREE(thread->threadStack, NULL, DYNAMIC_TYPE_OS_BUF); |
5739 | | thread->threadStack = NULL; |
5740 | | XFREE(thread->tid, NULL, DYNAMIC_TYPE_OS_BUF); |
5741 | | thread->tid = NULL; |
5742 | | return MEMORY_E; |
5743 | | } |
5744 | | |
5745 | | return 0; |
5746 | | } |
5747 | | |
5748 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5749 | | { |
5750 | | UINT state = TX_READY; |
5751 | | int ret = 0; |
5752 | | |
5753 | | if (thread.tid == NULL) |
5754 | | return BAD_FUNC_ARG; |
5755 | | |
5756 | | /* The thread runs on threadStack, so it has to be done with it before |
5757 | | * the stack is released. Wait for the entry function to return, or for |
5758 | | * the thread to be terminated by someone else. */ |
5759 | | while (ret == 0 && state != TX_COMPLETED && state != TX_TERMINATED) { |
5760 | | if (tx_thread_info_get(thread.tid, TX_NULL, &state, TX_NULL, |
5761 | | TX_NULL, TX_NULL, TX_NULL, TX_NULL, TX_NULL) |
5762 | | != TX_SUCCESS) { |
5763 | | /* The control block is not a thread ThreadX knows about, so |
5764 | | * nothing is running on the stack either. */ |
5765 | | ret = BAD_STATE_E; |
5766 | | } |
5767 | | else if (state != TX_COMPLETED && state != TX_TERMINATED) { |
5768 | | tx_thread_sleep(1); |
5769 | | } |
5770 | | } |
5771 | | |
5772 | | /* Unregister the thread before its control block and stack go away. */ |
5773 | | if (ret == 0 && tx_thread_delete(thread.tid) != TX_SUCCESS) { |
5774 | | /* ThreadX still owns the control block and the thread may still be |
5775 | | * running on the stack, so neither can be released here. */ |
5776 | | WOLFSSL_MSG("tx_thread_delete failed, leaking thread resources"); |
5777 | | return BAD_STATE_E; |
5778 | | } |
5779 | | |
5780 | | XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_OS_BUF); |
5781 | | thread.threadStack = NULL; |
5782 | | XFREE(thread.tid, NULL, DYNAMIC_TYPE_OS_BUF); |
5783 | | thread.tid = NULL; |
5784 | | |
5785 | | return ret; |
5786 | | } |
5787 | | |
5788 | | #elif defined(WOLFSSL_ZEPHYR) |
5789 | | |
5790 | | void* wolfsslThreadHeapHint = NULL; |
5791 | | |
5792 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5793 | | THREAD_CB cb, void* arg) |
5794 | | { |
5795 | | #ifndef WOLFSSL_ZEPHYR_STACK_SZ |
5796 | | #define WOLFSSL_ZEPHYR_STACK_SZ (48*1024) |
5797 | | #endif |
5798 | | |
5799 | | if (thread == NULL || cb == NULL) |
5800 | | return BAD_FUNC_ARG; |
5801 | | |
5802 | | XMEMSET(thread, 0, sizeof(*thread)); |
5803 | | |
5804 | | thread->tid = (struct k_thread*)XMALLOC( |
5805 | | Z_KERNEL_STACK_SIZE_ADJUST(sizeof(struct k_thread)), |
5806 | | wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER); |
5807 | | if (thread->tid == NULL) { |
5808 | | WOLFSSL_MSG("error: XMALLOC thread->tid failed"); |
5809 | | return MEMORY_E; |
5810 | | } |
5811 | | |
5812 | | /* TODO: Use the following once k_thread_stack_alloc makes it into a |
5813 | | * release. |
5814 | | * thread->threadStack = k_thread_stack_alloc(WOLFSSL_ZEPHYR_STACK_SZ, |
5815 | | * 0); |
5816 | | */ |
5817 | | thread->threadStack = (void*)XMALLOC( |
5818 | | Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), |
5819 | | wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER); |
5820 | | if (thread->threadStack == NULL) { |
5821 | | XFREE(thread->tid, wolfsslThreadHeapHint, |
5822 | | DYNAMIC_TYPE_TMP_BUFFER); |
5823 | | thread->tid = NULL; |
5824 | | |
5825 | | WOLFSSL_MSG("error: XMALLOC thread->threadStack failed"); |
5826 | | return MEMORY_E; |
5827 | | } |
5828 | | |
5829 | | /* k_thread_create does not return any error codes */ |
5830 | | /* Casting to k_thread_entry_t should be fine since we just ignore the |
5831 | | * extra arguments being passed in */ |
5832 | | k_thread_create(thread->tid, thread->threadStack, |
5833 | | WOLFSSL_ZEPHYR_STACK_SZ, (k_thread_entry_t)cb, arg, NULL, NULL, |
5834 | | 5, 0, K_NO_WAIT); |
5835 | | |
5836 | | return 0; |
5837 | | } |
5838 | | |
5839 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5840 | | { |
5841 | | int ret = 0; |
5842 | | int err; |
5843 | | |
5844 | | err = k_thread_join(thread.tid, K_FOREVER); |
5845 | | if (err != 0) |
5846 | | ret = MEMORY_E; |
5847 | | |
5848 | | XFREE(thread.tid, wolfsslThreadHeapHint, |
5849 | | DYNAMIC_TYPE_TMP_BUFFER); |
5850 | | thread.tid = NULL; |
5851 | | |
5852 | | /* TODO: Use the following once k_thread_stack_free makes it into a |
5853 | | * release. |
5854 | | * err = k_thread_stack_free(thread.threadStack); |
5855 | | * if (err != 0) |
5856 | | * ret = MEMORY_E; |
5857 | | */ |
5858 | | XFREE(thread.threadStack, wolfsslThreadHeapHint, |
5859 | | DYNAMIC_TYPE_TMP_BUFFER); |
5860 | | thread.threadStack = NULL; |
5861 | | |
5862 | | /* No thread resources to free. Everything is stored in thread.tid */ |
5863 | | |
5864 | | return ret; |
5865 | | } |
5866 | | |
5867 | | #ifdef WOLFSSL_COND |
5868 | | /* Native Zephyr condition variables (k_condvar) over a k_mutex; no POSIX |
5869 | | * pthread layer required. Semantics mirror the pthread implementation. */ |
5870 | | int wolfSSL_CondInit(COND_TYPE* cond) |
5871 | | { |
5872 | | int ret; |
5873 | | |
5874 | | if (cond == NULL) |
5875 | | return BAD_FUNC_ARG; |
5876 | | |
5877 | | ret = wc_InitMutex(&cond->mutex); |
5878 | | if (ret == 0) { |
5879 | | /* k_condvar_init always returns 0 on Zephyr. */ |
5880 | | (void)k_condvar_init(&cond->cond); |
5881 | | } |
5882 | | |
5883 | | return ret; |
5884 | | } |
5885 | | |
5886 | | int wolfSSL_CondFree(COND_TYPE* cond) |
5887 | | { |
5888 | | if (cond == NULL) |
5889 | | return BAD_FUNC_ARG; |
5890 | | |
5891 | | /* k_condvar has no destroy; just release the backing mutex. */ |
5892 | | return wc_FreeMutex(&cond->mutex); |
5893 | | } |
5894 | | |
5895 | | int wolfSSL_CondStart(COND_TYPE* cond) |
5896 | | { |
5897 | | if (cond == NULL) |
5898 | | return BAD_FUNC_ARG; |
5899 | | |
5900 | | if (wc_LockMutex(&cond->mutex) != 0) |
5901 | | return BAD_MUTEX_E; |
5902 | | |
5903 | | return 0; |
5904 | | } |
5905 | | |
5906 | | int wolfSSL_CondSignal(COND_TYPE* cond) |
5907 | | { |
5908 | | if (cond == NULL) |
5909 | | return BAD_FUNC_ARG; |
5910 | | |
5911 | | /* Caller holds cond->mutex; wake a single waiter. */ |
5912 | | (void)k_condvar_signal(&cond->cond); |
5913 | | |
5914 | | return 0; |
5915 | | } |
5916 | | |
5917 | | int wolfSSL_CondWait(COND_TYPE* cond) |
5918 | | { |
5919 | | if (cond == NULL) |
5920 | | return BAD_FUNC_ARG; |
5921 | | |
5922 | | /* Atomically releases the mutex, blocks, and re-acquires it on wake, |
5923 | | * matching pthread_cond_wait semantics. */ |
5924 | | if (k_condvar_wait(&cond->cond, &cond->mutex, K_FOREVER) != 0) |
5925 | | return BAD_MUTEX_E; |
5926 | | |
5927 | | return 0; |
5928 | | } |
5929 | | |
5930 | | int wolfSSL_CondEnd(COND_TYPE* cond) |
5931 | | { |
5932 | | if (cond == NULL) |
5933 | | return BAD_FUNC_ARG; |
5934 | | |
5935 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
5936 | | return BAD_MUTEX_E; |
5937 | | |
5938 | | return 0; |
5939 | | } |
5940 | | #endif /* WOLFSSL_COND */ |
5941 | | |
5942 | | #elif defined(WOLFSSL_PTHREADS) || \ |
5943 | | (defined(FREERTOS) && defined(WOLFSSL_ESPIDF)) |
5944 | | |
5945 | | int wolfSSL_NewThread(THREAD_TYPE* thread, |
5946 | | THREAD_CB cb, void* arg) |
5947 | 0 | { |
5948 | 0 | if (thread == NULL || cb == NULL) |
5949 | 0 | return BAD_FUNC_ARG; |
5950 | | |
5951 | 0 | if (pthread_create(thread, NULL, cb, arg) != 0) |
5952 | 0 | return MEMORY_E; |
5953 | | |
5954 | 0 | return 0; |
5955 | 0 | } |
5956 | | |
5957 | | #ifdef WOLFSSL_THREAD_NO_JOIN |
5958 | | int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg) |
5959 | 0 | { |
5960 | 0 | THREAD_TYPE thread; |
5961 | 0 | int ret; |
5962 | 0 | XMEMSET(&thread, 0, sizeof(thread)); |
5963 | 0 | ret = wolfSSL_NewThread(&thread, cb, arg); |
5964 | 0 | if (ret == 0) |
5965 | 0 | ret = pthread_detach(thread); |
5966 | 0 | return ret; |
5967 | 0 | } |
5968 | | #endif |
5969 | | |
5970 | | int wolfSSL_JoinThread(THREAD_TYPE thread) |
5971 | 0 | { |
5972 | 0 | if (thread == INVALID_THREAD_VAL) |
5973 | 0 | return BAD_FUNC_ARG; |
5974 | | |
5975 | 0 | if (pthread_join(thread, NULL) != 0) |
5976 | 0 | return MEMORY_E; |
5977 | | |
5978 | 0 | return 0; |
5979 | 0 | } |
5980 | | |
5981 | | #ifdef WOLFSSL_COND |
5982 | | #if defined(__APPLE__) && MAC_OS_X_VERSION_MIN_REQUIRED >= 1060 \ |
5983 | | && !defined(__ppc__) |
5984 | | /* Apple style dispatch semaphore */ |
5985 | | int wolfSSL_CondInit(COND_TYPE* cond) |
5986 | | { |
5987 | | if (cond == NULL) |
5988 | | return BAD_FUNC_ARG; |
5989 | | |
5990 | | /* dispatch_release() fails hard, with Trace/BPT trap signal, if the |
5991 | | * sem's internal count is less than the value passed in with |
5992 | | * dispatch_semaphore_create(). work around this by initing |
5993 | | * with 0, then incrementing it afterwards. |
5994 | | */ |
5995 | | cond->cond = dispatch_semaphore_create(0); |
5996 | | if (cond->cond == NULL) |
5997 | | return MEMORY_E; |
5998 | | |
5999 | | if (wc_InitMutex(&cond->mutex) != 0) { |
6000 | | dispatch_release(cond->cond); |
6001 | | return MEMORY_E; |
6002 | | } |
6003 | | |
6004 | | return 0; |
6005 | | } |
6006 | | |
6007 | | int wolfSSL_CondFree(COND_TYPE* cond) |
6008 | | { |
6009 | | if (cond == NULL) |
6010 | | return BAD_FUNC_ARG; |
6011 | | |
6012 | | dispatch_release(cond->cond); |
6013 | | cond->cond = NULL; |
6014 | | |
6015 | | if (wc_FreeMutex(&cond->mutex) != 0) { |
6016 | | return MEMORY_E; |
6017 | | } |
6018 | | |
6019 | | return 0; |
6020 | | } |
6021 | | |
6022 | | int wolfSSL_CondStart(COND_TYPE* cond) |
6023 | | { |
6024 | | if (cond == NULL) |
6025 | | return BAD_FUNC_ARG; |
6026 | | |
6027 | | if (wc_LockMutex(&cond->mutex) != 0) |
6028 | | return BAD_MUTEX_E; |
6029 | | |
6030 | | return 0; |
6031 | | } |
6032 | | |
6033 | | int wolfSSL_CondSignal(COND_TYPE* cond) |
6034 | | { |
6035 | | if (cond == NULL) |
6036 | | return BAD_FUNC_ARG; |
6037 | | |
6038 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
6039 | | return BAD_MUTEX_E; |
6040 | | |
6041 | | dispatch_semaphore_signal(cond->cond); |
6042 | | |
6043 | | if (wc_LockMutex(&cond->mutex) != 0) |
6044 | | return BAD_MUTEX_E; |
6045 | | |
6046 | | return 0; |
6047 | | } |
6048 | | |
6049 | | int wolfSSL_CondWait(COND_TYPE* cond) |
6050 | | { |
6051 | | if (cond == NULL) |
6052 | | return BAD_FUNC_ARG; |
6053 | | |
6054 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
6055 | | return BAD_MUTEX_E; |
6056 | | |
6057 | | dispatch_semaphore_wait(cond->cond, DISPATCH_TIME_FOREVER); |
6058 | | |
6059 | | if (wc_LockMutex(&cond->mutex) != 0) |
6060 | | return BAD_MUTEX_E; |
6061 | | |
6062 | | return 0; |
6063 | | } |
6064 | | |
6065 | | int wolfSSL_CondEnd(COND_TYPE* cond) |
6066 | | { |
6067 | | if (cond == NULL) |
6068 | | return BAD_FUNC_ARG; |
6069 | | |
6070 | | if (wc_UnLockMutex(&cond->mutex) != 0) |
6071 | | return BAD_MUTEX_E; |
6072 | | |
6073 | | return 0; |
6074 | | } |
6075 | | |
6076 | | #else /* Generic POSIX conditional */ |
6077 | | |
6078 | | int wolfSSL_CondInit(COND_TYPE* cond) |
6079 | 0 | { |
6080 | 0 | if (cond == NULL) |
6081 | 0 | return BAD_FUNC_ARG; |
6082 | | |
6083 | 0 | if (pthread_mutex_init(&cond->mutex, NULL) != 0) |
6084 | 0 | return MEMORY_E; |
6085 | | |
6086 | 0 | if (pthread_cond_init(&cond->cond, NULL) != 0) { |
6087 | | /* Keep compilers happy that we are using the return code */ |
6088 | 0 | if (pthread_mutex_destroy(&cond->mutex) != 0) |
6089 | 0 | return MEMORY_E; |
6090 | 0 | return MEMORY_E; |
6091 | 0 | } |
6092 | | |
6093 | 0 | return 0; |
6094 | 0 | } |
6095 | | |
6096 | | int wolfSSL_CondFree(COND_TYPE* cond) |
6097 | 0 | { |
6098 | 0 | int ret = 0; |
6099 | |
|
6100 | 0 | if (cond == NULL) |
6101 | 0 | return BAD_FUNC_ARG; |
6102 | | |
6103 | 0 | if (pthread_mutex_destroy(&cond->mutex) != 0) |
6104 | 0 | ret = MEMORY_E; |
6105 | |
|
6106 | 0 | if (pthread_cond_destroy(&cond->cond) != 0) |
6107 | 0 | ret = MEMORY_E; |
6108 | |
|
6109 | 0 | return ret; |
6110 | 0 | } |
6111 | | |
6112 | | int wolfSSL_CondStart(COND_TYPE* cond) |
6113 | 0 | { |
6114 | 0 | if (cond == NULL) |
6115 | 0 | return BAD_FUNC_ARG; |
6116 | | |
6117 | 0 | if (pthread_mutex_lock(&cond->mutex) != 0) |
6118 | 0 | return BAD_MUTEX_E; |
6119 | | |
6120 | 0 | return 0; |
6121 | 0 | } |
6122 | | |
6123 | | int wolfSSL_CondSignal(COND_TYPE* cond) |
6124 | 0 | { |
6125 | 0 | if (cond == NULL) |
6126 | 0 | return BAD_FUNC_ARG; |
6127 | | |
6128 | 0 | if (pthread_cond_signal(&cond->cond) != 0) |
6129 | 0 | return MEMORY_E; |
6130 | | |
6131 | 0 | return 0; |
6132 | 0 | } |
6133 | | |
6134 | | int wolfSSL_CondWait(COND_TYPE* cond) |
6135 | 0 | { |
6136 | 0 | if (cond == NULL) |
6137 | 0 | return BAD_FUNC_ARG; |
6138 | | |
6139 | 0 | if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) |
6140 | 0 | return MEMORY_E; |
6141 | | |
6142 | 0 | return 0; |
6143 | 0 | } |
6144 | | |
6145 | | int wolfSSL_CondEnd(COND_TYPE* cond) |
6146 | 0 | { |
6147 | 0 | if (cond == NULL) |
6148 | 0 | return BAD_FUNC_ARG; |
6149 | | |
6150 | 0 | if (pthread_mutex_unlock(&cond->mutex) != 0) |
6151 | 0 | return BAD_MUTEX_E; |
6152 | | |
6153 | 0 | return 0; |
6154 | 0 | } |
6155 | | |
6156 | | #endif /* __MACH__ */ |
6157 | | #endif /* WOLFSSL_COND */ |
6158 | | |
6159 | | #endif /* Environment check */ |
6160 | | |
6161 | | #endif /* not SINGLE_THREADED */ |
6162 | | |
6163 | | #if (defined(__unix__) || defined(__APPLE__)) && \ |
6164 | | !defined(WOLFSSL_KERNEL_MODE) && !defined(WOLFSSL_ZEPHYR) && \ |
6165 | | !defined(WOLFSSL_SGX) && !defined(WOLFSSL_OPTEE) |
6166 | | |
6167 | | #include <fcntl.h> |
6168 | | #include <errno.h> |
6169 | | #include <sys/socket.h> |
6170 | | #include <unistd.h> |
6171 | | |
6172 | | #ifndef O_CLOEXEC |
6173 | | #define O_CLOEXEC 0 |
6174 | | #endif |
6175 | | #ifndef SOCK_CLOEXEC |
6176 | | #define SOCK_CLOEXEC 0 |
6177 | | #endif |
6178 | | |
6179 | | /* accept4(): HAVE_ACCEPT4 is set by the configure/CMake probe, or in |
6180 | | * user_settings.h for a libc not recognised here, such as musl. Recognised: |
6181 | | * glibc 2.10, uClibc-ng, bionic API 21, FreeBSD 10. */ |
6182 | | #if (defined(__linux__) || defined(__ANDROID__)) && \ |
6183 | | (defined(HAVE_ACCEPT4) || \ |
6184 | | (defined(__USE_GNU) && \ |
6185 | | ((defined(__GLIBC__) && \ |
6186 | | (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 10))) || \ |
6187 | | (defined(__UCLIBC_LINUX_SPECIFIC__) && (__UCLIBC_MAJOR__ >= 1)) || \ |
6188 | | (defined(__ANDROID_API__) && (__ANDROID_API__ >= 21))))) |
6189 | | #define WC_HAVE_ACCEPT4 |
6190 | | #elif defined(__FreeBSD__) && defined(__BSD_VISIBLE) && __BSD_VISIBLE && \ |
6191 | | (__FreeBSD_version >= 1000000) |
6192 | | #define WC_HAVE_ACCEPT4 |
6193 | | #endif |
6194 | | |
6195 | | void wc_set_cloexec(int fd) |
6196 | 0 | { |
6197 | 0 | #ifdef FD_CLOEXEC |
6198 | 0 | int fdFlags; |
6199 | 0 | if (fd < 0) |
6200 | 0 | return; |
6201 | 0 | fdFlags = fcntl(fd, F_GETFD); |
6202 | 0 | if (fdFlags >= 0) |
6203 | 0 | (void)fcntl(fd, F_SETFD, fdFlags | FD_CLOEXEC); |
6204 | | #else |
6205 | | (void)fd; |
6206 | | #endif |
6207 | 0 | } |
6208 | | |
6209 | | int wc_open_cloexec(const char* path, int flags) |
6210 | 0 | { |
6211 | 0 | int fd = open(path, flags | O_CLOEXEC); |
6212 | 0 | #ifdef FD_CLOEXEC |
6213 | 0 | if (fd < 0 && errno == EINVAL) { |
6214 | 0 | fd = open(path, flags); |
6215 | 0 | wc_set_cloexec(fd); |
6216 | 0 | } |
6217 | 0 | #endif |
6218 | 0 | return fd; |
6219 | 0 | } |
6220 | | |
6221 | | /* As wc_open_cloexec(), for flags with O_CREAT, where open() needs a mode. */ |
6222 | | int wc_open_cloexec_mode(const char* path, int flags, int mode) |
6223 | 0 | { |
6224 | 0 | int fd = open(path, flags | O_CLOEXEC, mode); |
6225 | 0 | #ifdef FD_CLOEXEC |
6226 | 0 | if (fd < 0 && errno == EINVAL) { |
6227 | 0 | fd = open(path, flags, mode); |
6228 | 0 | wc_set_cloexec(fd); |
6229 | 0 | } |
6230 | 0 | #endif |
6231 | 0 | return fd; |
6232 | 0 | } |
6233 | | |
6234 | | #if !defined(NO_FILESYSTEM) && defined(XFDOPEN) |
6235 | | #include <sys/stat.h> |
6236 | | |
6237 | | /* Truncate or create path for writing, owner read/write only. */ |
6238 | | XFILE wc_fopen_owner_only(const char* path) |
6239 | 0 | { |
6240 | 0 | XFILE file; |
6241 | 0 | int fd = wc_open_cloexec_mode(path, O_RDWR | O_CREAT | O_TRUNC, |
6242 | 0 | S_IRUSR | S_IWUSR); |
6243 | 0 | if (fd < 0) |
6244 | 0 | return XBADFILE; |
6245 | | |
6246 | 0 | #ifndef WOLFSSL_NO_FCHMOD |
6247 | 0 | if (fchmod(fd, S_IRUSR | S_IWUSR) != 0) { |
6248 | 0 | close(fd); |
6249 | 0 | return XBADFILE; |
6250 | 0 | } |
6251 | 0 | #endif |
6252 | | |
6253 | 0 | file = XFDOPEN(fd, "w+b"); |
6254 | 0 | if (file == XBADFILE) |
6255 | 0 | close(fd); |
6256 | |
|
6257 | 0 | return file; |
6258 | 0 | } |
6259 | | #endif /* !NO_FILESYSTEM && XFDOPEN */ |
6260 | | |
6261 | | int wc_socket_cloexec(int domain, int type, int protocol) |
6262 | 0 | { |
6263 | 0 | int fd = socket(domain, type | SOCK_CLOEXEC, protocol); |
6264 | 0 | #ifdef FD_CLOEXEC |
6265 | 0 | if (fd < 0 && errno == EINVAL) { |
6266 | 0 | fd = socket(domain, type, protocol); |
6267 | 0 | wc_set_cloexec(fd); |
6268 | 0 | } |
6269 | 0 | #endif |
6270 | 0 | return fd; |
6271 | 0 | } |
6272 | | |
6273 | | int wc_accept_cloexec(int sockfd, void* addr, void* addrlen) |
6274 | 0 | { |
6275 | 0 | int fd; |
6276 | 0 | #ifdef WC_HAVE_ACCEPT4 |
6277 | 0 | fd = accept4(sockfd, (struct sockaddr*)addr, (socklen_t*)addrlen, |
6278 | 0 | SOCK_CLOEXEC); |
6279 | 0 | if (fd >= 0) |
6280 | 0 | return fd; |
6281 | 0 | if (errno != ENOSYS && errno != EINVAL) |
6282 | 0 | return fd; |
6283 | 0 | #endif |
6284 | 0 | fd = accept(sockfd, (struct sockaddr*)addr, (socklen_t*)addrlen); |
6285 | 0 | wc_set_cloexec(fd); |
6286 | 0 | return fd; |
6287 | 0 | } |
6288 | | |
6289 | | #endif /* (__unix__ || __APPLE__) && !WOLFSSL_KERNEL_MODE && !WOLFSSL_ZEPHYR && |
6290 | | * !WOLFSSL_SGX */ |
6291 | | |
6292 | | #if defined(WOLFSSL_LINUXKM) && defined(CONFIG_ARM64) && \ |
6293 | | defined(WC_SYM_RELOC_TABLES) |
6294 | | #ifndef CONFIG_ARCH_TEGRA |
6295 | | |
6296 | | #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) |
6297 | | noinstr void my__alt_cb_patch_nops(struct alt_instr *alt, __le32 *origptr, |
6298 | | __le32 *updptr, int nr_inst) |
6299 | | { |
6300 | | return WC_PIE_INDIRECT_SYM(alt_cb_patch_nops) |
6301 | | (alt, origptr, updptr, nr_inst); |
6302 | | } |
6303 | | #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0) */ |
6304 | | |
6305 | | void my__queued_spin_lock_slowpath(struct qspinlock *lock, u32 val) |
6306 | | { |
6307 | | return WC_PIE_INDIRECT_SYM(queued_spin_lock_slowpath) |
6308 | | (lock, val); |
6309 | | } |
6310 | | #endif |
6311 | | #endif |