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