1 | /* port.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2020 wolfSSL Inc.
|
---|
4 | *
|
---|
5 | * This file is part of wolfSSL.
|
---|
6 | *
|
---|
7 | * wolfSSL is free software; you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation; either version 2 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * wolfSSL is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with this program; if not, write to the Free Software
|
---|
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | #ifdef HAVE_CONFIG_H
|
---|
24 | #include <config.h>
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #include <wolfssl/wolfcrypt/settings.h>
|
---|
28 | #include <wolfssl/wolfcrypt/types.h>
|
---|
29 | #include <wolfssl/wolfcrypt/error-crypt.h>
|
---|
30 | #include <wolfssl/wolfcrypt/logging.h>
|
---|
31 | #include <wolfssl/wolfcrypt/wc_port.h>
|
---|
32 | #ifdef HAVE_ECC
|
---|
33 | #include <wolfssl/wolfcrypt/ecc.h>
|
---|
34 | #endif
|
---|
35 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
36 | #include <wolfssl/wolfcrypt/async.h>
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | /* IPP header files for library initialization */
|
---|
40 | #ifdef HAVE_FAST_RSA
|
---|
41 | #include <ipp.h>
|
---|
42 | #include <ippcp.h>
|
---|
43 | #endif
|
---|
44 |
|
---|
45 | #ifdef FREESCALE_LTC_TFM
|
---|
46 | #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
|
---|
47 | #endif
|
---|
48 |
|
---|
49 | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
|
---|
50 | #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
|
---|
51 | #endif
|
---|
52 | #if defined(WOLFSSL_RENESAS_TSIP)
|
---|
53 | #include <wolfssl/wolfcrypt/port/Renesas/renesas-tsip-crypt.h>
|
---|
54 | #endif
|
---|
55 | #if defined(WOLFSSL_STSAFEA100)
|
---|
56 | #include <wolfssl/wolfcrypt/port/st/stsafe.h>
|
---|
57 | #endif
|
---|
58 |
|
---|
59 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
60 | #include <wolfssl/openssl/evp.h>
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
---|
64 | #include <wolfssl/wolfcrypt/memory.h>
|
---|
65 | #include <wolfssl/wolfcrypt/mem_track.h>
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
69 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
70 | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
|
---|
71 | #endif
|
---|
72 |
|
---|
73 | #ifdef WOLF_CRYPTO_CB
|
---|
74 | #include <wolfssl/wolfcrypt/cryptocb.h>
|
---|
75 | #endif
|
---|
76 |
|
---|
77 | #ifdef HAVE_INTEL_QA_SYNC
|
---|
78 | #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | #ifdef HAVE_CAVIUM_OCTEON_SYNC
|
---|
82 | #include <wolfssl/wolfcrypt/port/cavium/cavium_octeon_sync.h>
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #ifdef WOLFSSL_SCE
|
---|
86 | #include "hal_data.h"
|
---|
87 | #endif
|
---|
88 |
|
---|
89 | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
|
---|
90 | #include "rpcmem.h"
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifdef _MSC_VER
|
---|
94 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
|
---|
95 | #pragma warning(disable: 4996)
|
---|
96 | #endif
|
---|
97 |
|
---|
98 | /* prevent multiple mutex initializations */
|
---|
99 | static volatile int initRefCount = 0;
|
---|
100 |
|
---|
101 | /* Used to initialize state for wolfcrypt
|
---|
102 | return 0 on success
|
---|
103 | */
|
---|
104 | int wolfCrypt_Init(void)
|
---|
105 | {
|
---|
106 | int ret = 0;
|
---|
107 |
|
---|
108 | if (initRefCount == 0) {
|
---|
109 | WOLFSSL_ENTER("wolfCrypt_Init");
|
---|
110 |
|
---|
111 | #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
---|
112 | {
|
---|
113 | word32 rngMallocFail;
|
---|
114 | time_t seed = time(NULL);
|
---|
115 | srand((word32)seed);
|
---|
116 | rngMallocFail = rand() % 2000; /* max 2000 */
|
---|
117 | printf("\n--- RNG MALLOC FAIL AT %d---\n", rngMallocFail);
|
---|
118 | wolfSSL_SetMemFailCount(rngMallocFail);
|
---|
119 | }
|
---|
120 | #endif
|
---|
121 |
|
---|
122 | #ifdef WOLF_CRYPTO_CB
|
---|
123 | wc_CryptoCb_Init();
|
---|
124 | #endif
|
---|
125 |
|
---|
126 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
127 | ret = wolfAsync_HardwareStart();
|
---|
128 | if (ret != 0) {
|
---|
129 | WOLFSSL_MSG("Async hardware start failed");
|
---|
130 | /* don't return failure, allow operation to continue */
|
---|
131 | }
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | #if defined(WOLFSSL_RENESAS_TSIP_CRYPT)
|
---|
135 | ret = tsip_Open( );
|
---|
136 | if( ret != TSIP_SUCCESS ) {
|
---|
137 | WOLFSSL_MSG("RENESAS TSIP Open failed");
|
---|
138 | /* not return 1 since WOLFSSL_SUCCESS=1*/
|
---|
139 | ret = -1;/* FATAL ERROR */
|
---|
140 | return ret;
|
---|
141 | }
|
---|
142 | #endif
|
---|
143 |
|
---|
144 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
---|
145 | ret = InitMemoryTracker();
|
---|
146 | if (ret != 0) {
|
---|
147 | WOLFSSL_MSG("InitMemoryTracker failed");
|
---|
148 | return ret;
|
---|
149 | }
|
---|
150 | #endif
|
---|
151 |
|
---|
152 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
153 | /* If crypto hardware mutex protection is enabled, then initialize it */
|
---|
154 | ret = wolfSSL_CryptHwMutexInit();
|
---|
155 | if (ret != 0) {
|
---|
156 | WOLFSSL_MSG("Hw crypt mutex init failed");
|
---|
157 | return ret;
|
---|
158 | }
|
---|
159 | #endif
|
---|
160 |
|
---|
161 | /* if defined have fast RSA then initialize Intel IPP */
|
---|
162 | #ifdef HAVE_FAST_RSA
|
---|
163 | WOLFSSL_MSG("Attempting to use optimized IPP Library");
|
---|
164 | if ((ret = ippInit()) != ippStsNoErr) {
|
---|
165 | /* possible to get a CPU feature support status on optimized IPP
|
---|
166 | library but still use default library and see competitive speeds */
|
---|
167 | WOLFSSL_MSG("Warning when trying to set up optimization");
|
---|
168 | WOLFSSL_MSG(ippGetStatusString(ret));
|
---|
169 | WOLFSSL_MSG("Using default fast IPP library");
|
---|
170 | ret = 0;
|
---|
171 | (void)ret; /* suppress not read warning */
|
---|
172 | }
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
|
---|
176 | ret = ksdk_port_init();
|
---|
177 | if (ret != 0) {
|
---|
178 | WOLFSSL_MSG("KSDK port init failed");
|
---|
179 | return ret;
|
---|
180 | }
|
---|
181 | #endif
|
---|
182 |
|
---|
183 | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
|
---|
184 | ret = atmel_init();
|
---|
185 | if (ret != 0) {
|
---|
186 | WOLFSSL_MSG("CryptoAuthLib init failed");
|
---|
187 | return ret;
|
---|
188 | }
|
---|
189 | #endif
|
---|
190 | #if defined(WOLFSSL_CRYPTOCELL)
|
---|
191 | /* enable and initialize the ARM CryptoCell 3xx runtime library */
|
---|
192 | ret = cc310_Init();
|
---|
193 | if (ret != 0) {
|
---|
194 | WOLFSSL_MSG("CRYPTOCELL init failed");
|
---|
195 | return ret;
|
---|
196 | }
|
---|
197 | #endif
|
---|
198 | #if defined(WOLFSSL_STSAFEA100)
|
---|
199 | stsafe_interface_init();
|
---|
200 | #endif
|
---|
201 |
|
---|
202 | #ifdef WOLFSSL_ARMASM
|
---|
203 | WOLFSSL_MSG("Using ARM hardware acceleration");
|
---|
204 | #endif
|
---|
205 |
|
---|
206 | #ifdef WOLFSSL_AFALG
|
---|
207 | WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
|
---|
208 | #endif
|
---|
209 |
|
---|
210 | #if !defined(WOLFCRYPT_ONLY) && defined(OPENSSL_EXTRA)
|
---|
211 | wolfSSL_EVP_init();
|
---|
212 | #endif
|
---|
213 |
|
---|
214 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
|
---|
215 | if ((ret = wc_LoggingInit()) != 0) {
|
---|
216 | WOLFSSL_MSG("Error creating logging mutex");
|
---|
217 | return ret;
|
---|
218 | }
|
---|
219 | #endif
|
---|
220 |
|
---|
221 | #ifdef HAVE_ECC
|
---|
222 | #ifdef ECC_CACHE_CURVE
|
---|
223 | if ((ret = wc_ecc_curve_cache_init()) != 0) {
|
---|
224 | WOLFSSL_MSG("Error creating curve cache");
|
---|
225 | return ret;
|
---|
226 | }
|
---|
227 | #endif
|
---|
228 | #endif
|
---|
229 |
|
---|
230 | #ifdef WOLFSSL_SCE
|
---|
231 | ret = (int)WOLFSSL_SCE_GSCE_HANDLE.p_api->open(
|
---|
232 | WOLFSSL_SCE_GSCE_HANDLE.p_ctrl, WOLFSSL_SCE_GSCE_HANDLE.p_cfg);
|
---|
233 | if (ret == SSP_ERR_CRYPTO_SCE_ALREADY_OPEN) {
|
---|
234 | WOLFSSL_MSG("SCE already open");
|
---|
235 | ret = 0;
|
---|
236 | }
|
---|
237 | if (ret != SSP_SUCCESS) {
|
---|
238 | WOLFSSL_MSG("Error opening SCE");
|
---|
239 | return -1; /* FATAL_ERROR */
|
---|
240 | }
|
---|
241 | #endif
|
---|
242 |
|
---|
243 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
244 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
245 | if ((ret = wc_caamInit()) != 0) {
|
---|
246 | return ret;
|
---|
247 | }
|
---|
248 | #endif
|
---|
249 |
|
---|
250 | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
|
---|
251 | if ((ret = wolfSSL_InitHandle()) != 0) {
|
---|
252 | return ret;
|
---|
253 | }
|
---|
254 | rpcmem_init();
|
---|
255 | #endif
|
---|
256 | }
|
---|
257 | initRefCount++;
|
---|
258 |
|
---|
259 | return ret;
|
---|
260 | }
|
---|
261 |
|
---|
262 |
|
---|
263 | /* return success value is the same as wolfCrypt_Init */
|
---|
264 | int wolfCrypt_Cleanup(void)
|
---|
265 | {
|
---|
266 | int ret = 0;
|
---|
267 |
|
---|
268 | initRefCount--;
|
---|
269 | if (initRefCount < 0)
|
---|
270 | initRefCount = 0;
|
---|
271 |
|
---|
272 | if (initRefCount == 0) {
|
---|
273 | WOLFSSL_ENTER("wolfCrypt_Cleanup");
|
---|
274 |
|
---|
275 | #ifdef HAVE_ECC
|
---|
276 | #ifdef FP_ECC
|
---|
277 | wc_ecc_fp_free();
|
---|
278 | #endif
|
---|
279 | #ifdef ECC_CACHE_CURVE
|
---|
280 | wc_ecc_curve_cache_free();
|
---|
281 | #endif
|
---|
282 | #endif /* HAVE_ECC */
|
---|
283 |
|
---|
284 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
|
---|
285 | ret = wc_LoggingCleanup();
|
---|
286 | #endif
|
---|
287 |
|
---|
288 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
---|
289 | ShowMemoryTracker();
|
---|
290 | #endif
|
---|
291 |
|
---|
292 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
293 | wolfAsync_HardwareStop();
|
---|
294 | #endif
|
---|
295 | #ifdef WOLFSSL_SCE
|
---|
296 | WOLFSSL_SCE_GSCE_HANDLE.p_api->close(WOLFSSL_SCE_GSCE_HANDLE.p_ctrl);
|
---|
297 | #endif
|
---|
298 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
299 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
300 | wc_caamFree();
|
---|
301 | #endif
|
---|
302 | #if defined(WOLFSSL_CRYPTOCELL)
|
---|
303 | cc310_Free();
|
---|
304 | #endif
|
---|
305 | #if defined(WOLFSSL_RENESAS_TSIP_CRYPT)
|
---|
306 | tsip_Close();
|
---|
307 | #endif
|
---|
308 | #if defined(WOLFSSL_DSP) && !defined(WOLFSSL_DSP_BUILD)
|
---|
309 | rpcmem_deinit();
|
---|
310 | wolfSSL_CleanupHandle();
|
---|
311 | #endif
|
---|
312 | }
|
---|
313 |
|
---|
314 | return ret;
|
---|
315 | }
|
---|
316 |
|
---|
317 | #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
|
---|
318 | !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
319 |
|
---|
320 | /* File Handling Helpers */
|
---|
321 | /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
|
---|
322 | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
|
---|
323 | {
|
---|
324 | int ret = WC_READDIR_NOFILE; /* default to no files found */
|
---|
325 | int pathLen = 0;
|
---|
326 | int dnameLen = 0;
|
---|
327 |
|
---|
328 | if (name)
|
---|
329 | *name = NULL;
|
---|
330 |
|
---|
331 | if (ctx == NULL || path == NULL) {
|
---|
332 | return BAD_FUNC_ARG;
|
---|
333 | }
|
---|
334 |
|
---|
335 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
|
---|
336 | pathLen = (int)XSTRLEN(path);
|
---|
337 |
|
---|
338 | #ifdef USE_WINDOWS_API
|
---|
339 | if (pathLen > MAX_FILENAME_SZ - 3)
|
---|
340 | return BAD_PATH_ERROR;
|
---|
341 |
|
---|
342 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
|
---|
343 | XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
|
---|
344 |
|
---|
345 | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
|
---|
346 | if (ctx->hFind == INVALID_HANDLE_VALUE) {
|
---|
347 | WOLFSSL_MSG("FindFirstFile for path verify locations failed");
|
---|
348 | return BAD_PATH_ERROR;
|
---|
349 | }
|
---|
350 |
|
---|
351 | do {
|
---|
352 | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
---|
353 | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
|
---|
354 |
|
---|
355 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
356 | return BAD_PATH_ERROR;
|
---|
357 | }
|
---|
358 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
359 | ctx->name[pathLen] = '\\';
|
---|
360 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
361 | ctx->FindFileData.cFileName,
|
---|
362 | MAX_FILENAME_SZ - pathLen - 1);
|
---|
363 | if (name)
|
---|
364 | *name = ctx->name;
|
---|
365 | return 0;
|
---|
366 | }
|
---|
367 | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
|
---|
368 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
369 | if (fs_opendir(&ctx->dir, path) != 0) {
|
---|
370 | WOLFSSL_MSG("opendir path verify locations failed");
|
---|
371 | return BAD_PATH_ERROR;
|
---|
372 | }
|
---|
373 | ctx->dirp = &ctx->dir;
|
---|
374 |
|
---|
375 | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
|
---|
376 | dnameLen = (int)XSTRLEN(ctx->entry.name);
|
---|
377 |
|
---|
378 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
379 | ret = BAD_PATH_ERROR;
|
---|
380 | break;
|
---|
381 | }
|
---|
382 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
383 | ctx->name[pathLen] = '/';
|
---|
384 |
|
---|
385 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
386 | * of earlier check it is known that dnameLen is less than
|
---|
387 | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
|
---|
388 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
|
---|
389 | if (fs_stat(ctx->name, &ctx->s) != 0) {
|
---|
390 | WOLFSSL_MSG("stat on name failed");
|
---|
391 | ret = BAD_PATH_ERROR;
|
---|
392 | break;
|
---|
393 | } else if (ctx->s.type == FS_DIR_ENTRY_FILE) {
|
---|
394 | if (name)
|
---|
395 | *name = ctx->name;
|
---|
396 | return 0;
|
---|
397 | }
|
---|
398 | }
|
---|
399 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
400 | ctx->dir = m2mb_fs_opendir((const CHAR*)path);
|
---|
401 | if (ctx->dir == NULL) {
|
---|
402 | WOLFSSL_MSG("opendir path verify locations failed");
|
---|
403 | return BAD_PATH_ERROR;
|
---|
404 | }
|
---|
405 |
|
---|
406 | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
|
---|
407 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
408 |
|
---|
409 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
410 | ret = BAD_PATH_ERROR;
|
---|
411 | break;
|
---|
412 | }
|
---|
413 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
414 | ctx->name[pathLen] = '/';
|
---|
415 |
|
---|
416 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
417 | * of earlier check it is known that dnameLen is less than
|
---|
418 | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
|
---|
419 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
|
---|
420 |
|
---|
421 | if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
|
---|
422 | WOLFSSL_MSG("stat on name failed");
|
---|
423 | ret = BAD_PATH_ERROR;
|
---|
424 | break;
|
---|
425 | }
|
---|
426 | else if (ctx->s.st_mode & M2MB_S_IFREG) {
|
---|
427 | if (name)
|
---|
428 | *name = ctx->name;
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | #else
|
---|
433 | ctx->dir = opendir(path);
|
---|
434 | if (ctx->dir == NULL) {
|
---|
435 | WOLFSSL_MSG("opendir path verify locations failed");
|
---|
436 | return BAD_PATH_ERROR;
|
---|
437 | }
|
---|
438 |
|
---|
439 | while ((ctx->entry = readdir(ctx->dir)) != NULL) {
|
---|
440 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
441 |
|
---|
442 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
443 | ret = BAD_PATH_ERROR;
|
---|
444 | break;
|
---|
445 | }
|
---|
446 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
447 | ctx->name[pathLen] = '/';
|
---|
448 |
|
---|
449 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
450 | * of earlier check it is known that dnameLen is less than
|
---|
451 | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
|
---|
452 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
|
---|
453 | if (stat(ctx->name, &ctx->s) != 0) {
|
---|
454 | WOLFSSL_MSG("stat on name failed");
|
---|
455 | ret = BAD_PATH_ERROR;
|
---|
456 | break;
|
---|
457 | } else if (S_ISREG(ctx->s.st_mode)) {
|
---|
458 | if (name)
|
---|
459 | *name = ctx->name;
|
---|
460 | return 0;
|
---|
461 | }
|
---|
462 | }
|
---|
463 | #endif
|
---|
464 | wc_ReadDirClose(ctx);
|
---|
465 |
|
---|
466 | return ret;
|
---|
467 | }
|
---|
468 |
|
---|
469 | /* returns 0 if file found, WC_READDIR_NOFILE if no more files */
|
---|
470 | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
|
---|
471 | {
|
---|
472 | int ret = WC_READDIR_NOFILE; /* default to no file found */
|
---|
473 | int pathLen = 0;
|
---|
474 | int dnameLen = 0;
|
---|
475 |
|
---|
476 | if (name)
|
---|
477 | *name = NULL;
|
---|
478 |
|
---|
479 | if (ctx == NULL || path == NULL) {
|
---|
480 | return BAD_FUNC_ARG;
|
---|
481 | }
|
---|
482 |
|
---|
483 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
|
---|
484 | pathLen = (int)XSTRLEN(path);
|
---|
485 |
|
---|
486 | #ifdef USE_WINDOWS_API
|
---|
487 | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
|
---|
488 | if (!(ctx->FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
|
---|
489 | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
|
---|
490 |
|
---|
491 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
492 | return BAD_PATH_ERROR;
|
---|
493 | }
|
---|
494 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
495 | ctx->name[pathLen] = '\\';
|
---|
496 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
497 | ctx->FindFileData.cFileName,
|
---|
498 | MAX_FILENAME_SZ - pathLen - 1);
|
---|
499 | if (name)
|
---|
500 | *name = ctx->name;
|
---|
501 | return 0;
|
---|
502 | }
|
---|
503 | }
|
---|
504 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
505 | while ((fs_readdir(&ctx->dir, &ctx->entry)) != 0) {
|
---|
506 | dnameLen = (int)XSTRLEN(ctx->entry.name);
|
---|
507 |
|
---|
508 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
509 | ret = BAD_PATH_ERROR;
|
---|
510 | break;
|
---|
511 | }
|
---|
512 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
513 | ctx->name[pathLen] = '/';
|
---|
514 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
515 | * of earlier check it is known that dnameLen is less than
|
---|
516 | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
|
---|
517 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry.name, dnameLen + 1);
|
---|
518 |
|
---|
519 | if (fs_stat(ctx->name, &ctx->s) != 0) {
|
---|
520 | WOLFSSL_MSG("stat on name failed");
|
---|
521 | ret = BAD_PATH_ERROR;
|
---|
522 | break;
|
---|
523 | } else if (ctx->s.type == FS_DIR_ENTRY_FILE) {
|
---|
524 | if (name)
|
---|
525 | *name = ctx->name;
|
---|
526 | return 0;
|
---|
527 | }
|
---|
528 | }
|
---|
529 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
530 | while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
|
---|
531 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
532 |
|
---|
533 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
534 | ret = BAD_PATH_ERROR;
|
---|
535 | break;
|
---|
536 | }
|
---|
537 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
538 | ctx->name[pathLen] = '/';
|
---|
539 |
|
---|
540 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
541 | * of earlier check it is known that dnameLen is less than
|
---|
542 | * MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
|
---|
543 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
|
---|
544 |
|
---|
545 | if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
|
---|
546 | WOLFSSL_MSG("stat on name failed");
|
---|
547 | ret = BAD_PATH_ERROR;
|
---|
548 | break;
|
---|
549 | }
|
---|
550 | else if (ctx->s.st_mode & M2MB_S_IFREG) {
|
---|
551 | if (name)
|
---|
552 | *name = ctx->name;
|
---|
553 | return 0;
|
---|
554 | }
|
---|
555 | }
|
---|
556 | #else
|
---|
557 | while ((ctx->entry = readdir(ctx->dir)) != NULL) {
|
---|
558 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
559 |
|
---|
560 | if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
|
---|
561 | ret = BAD_PATH_ERROR;
|
---|
562 | break;
|
---|
563 | }
|
---|
564 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
565 | ctx->name[pathLen] = '/';
|
---|
566 | /* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
|
---|
567 | * of earlier check it is known that dnameLen is less than
|
---|
568 | * MAX_FILENAME_SZ - (pathLen + 2) so that dnameLen +1 will fit */
|
---|
569 | XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
|
---|
570 |
|
---|
571 | if (stat(ctx->name, &ctx->s) != 0) {
|
---|
572 | WOLFSSL_MSG("stat on name failed");
|
---|
573 | ret = BAD_PATH_ERROR;
|
---|
574 | break;
|
---|
575 | } else if (S_ISREG(ctx->s.st_mode)) {
|
---|
576 | if (name)
|
---|
577 | *name = ctx->name;
|
---|
578 | return 0;
|
---|
579 | }
|
---|
580 | }
|
---|
581 | #endif
|
---|
582 |
|
---|
583 | wc_ReadDirClose(ctx);
|
---|
584 |
|
---|
585 | return ret;
|
---|
586 | }
|
---|
587 |
|
---|
588 | void wc_ReadDirClose(ReadDirCtx* ctx)
|
---|
589 | {
|
---|
590 | if (ctx == NULL) {
|
---|
591 | return;
|
---|
592 | }
|
---|
593 |
|
---|
594 | #ifdef USE_WINDOWS_API
|
---|
595 | if (ctx->hFind != INVALID_HANDLE_VALUE) {
|
---|
596 | FindClose(ctx->hFind);
|
---|
597 | ctx->hFind = INVALID_HANDLE_VALUE;
|
---|
598 | }
|
---|
599 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
600 | if (ctx->dirp) {
|
---|
601 | fs_closedir(ctx->dirp);
|
---|
602 | ctx->dirp = NULL;
|
---|
603 | }
|
---|
604 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
605 | if (ctx->dir) {
|
---|
606 | m2mb_fs_closedir(ctx->dir);
|
---|
607 | ctx->dir = NULL;
|
---|
608 | }
|
---|
609 | #else
|
---|
610 | if (ctx->dir) {
|
---|
611 | closedir(ctx->dir);
|
---|
612 | ctx->dir = NULL;
|
---|
613 | }
|
---|
614 | #endif
|
---|
615 | }
|
---|
616 |
|
---|
617 | #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
|
---|
618 |
|
---|
619 | #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_ZEPHYR)
|
---|
620 | XFILE z_fs_open(const char* filename, const char* perm)
|
---|
621 | {
|
---|
622 | XFILE file;
|
---|
623 |
|
---|
624 | file = XMALLOC(sizeof(*file), NULL, DYNAMIC_TYPE_FILE);
|
---|
625 | if (file != NULL) {
|
---|
626 | if (fs_open(file, filename) != 0) {
|
---|
627 | XFREE(file, NULL, DYNAMIC_TYPE_FILE);
|
---|
628 | file = NULL;
|
---|
629 | }
|
---|
630 | }
|
---|
631 |
|
---|
632 | return file;
|
---|
633 | }
|
---|
634 |
|
---|
635 | int z_fs_close(XFILE file)
|
---|
636 | {
|
---|
637 | int ret;
|
---|
638 |
|
---|
639 | if (file == NULL)
|
---|
640 | return -1;
|
---|
641 | ret = (fs_close(file) == 0) ? 0 : -1;
|
---|
642 |
|
---|
643 | XFREE(file, NULL, DYNAMIC_TYPE_FILE);
|
---|
644 |
|
---|
645 | return ret;
|
---|
646 | }
|
---|
647 |
|
---|
648 | #endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */
|
---|
649 |
|
---|
650 |
|
---|
651 | wolfSSL_Mutex* wc_InitAndAllocMutex(void)
|
---|
652 | {
|
---|
653 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
|
---|
654 | DYNAMIC_TYPE_MUTEX);
|
---|
655 | if (m != NULL) {
|
---|
656 | if (wc_InitMutex(m) != 0) {
|
---|
657 | WOLFSSL_MSG("Init Mutex failed");
|
---|
658 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
|
---|
659 | m = NULL;
|
---|
660 | }
|
---|
661 | }
|
---|
662 | else {
|
---|
663 | WOLFSSL_MSG("Memory error with Mutex allocation");
|
---|
664 | }
|
---|
665 |
|
---|
666 | return m;
|
---|
667 | }
|
---|
668 |
|
---|
669 | #ifdef USE_WOLF_STRTOK
|
---|
670 | /* String token (delim) search. If str is null use nextp. */
|
---|
671 | char* wc_strtok(char *str, const char *delim, char **nextp)
|
---|
672 | {
|
---|
673 | char* ret;
|
---|
674 | int i, j;
|
---|
675 |
|
---|
676 | /* Use next if str is NULL */
|
---|
677 | if (str == NULL && nextp)
|
---|
678 | str = *nextp;
|
---|
679 |
|
---|
680 | /* verify str input */
|
---|
681 | if (str == NULL || *str == '\0')
|
---|
682 | return NULL;
|
---|
683 |
|
---|
684 | /* match on entire delim */
|
---|
685 | for (i = 0; str[i]; i++) {
|
---|
686 | for (j = 0; delim[j]; j++) {
|
---|
687 | if (delim[j] == str[i])
|
---|
688 | break;
|
---|
689 | }
|
---|
690 | if (!delim[j])
|
---|
691 | break;
|
---|
692 | }
|
---|
693 | str += i;
|
---|
694 | /* if end of string, not found so return NULL */
|
---|
695 | if (*str == '\0')
|
---|
696 | return NULL;
|
---|
697 |
|
---|
698 | ret = str;
|
---|
699 |
|
---|
700 | /* match on first delim */
|
---|
701 | for (i = 0; str[i]; i++) {
|
---|
702 | for (j = 0; delim[j]; j++) {
|
---|
703 | if (delim[j] == str[i])
|
---|
704 | break;
|
---|
705 | }
|
---|
706 | if (delim[j] == str[i])
|
---|
707 | break;
|
---|
708 | }
|
---|
709 | str += i;
|
---|
710 |
|
---|
711 | /* null terminate found string */
|
---|
712 | if (*str)
|
---|
713 | *str++ = '\0';
|
---|
714 |
|
---|
715 | /* return pointer to next */
|
---|
716 | if (nextp)
|
---|
717 | *nextp = str;
|
---|
718 |
|
---|
719 | return ret;
|
---|
720 | }
|
---|
721 | #endif /* USE_WOLF_STRTOK */
|
---|
722 |
|
---|
723 | #ifdef USE_WOLF_STRSEP
|
---|
724 | char* wc_strsep(char **stringp, const char *delim)
|
---|
725 | {
|
---|
726 | char *s, *tok;
|
---|
727 | const char *spanp;
|
---|
728 |
|
---|
729 | /* null check */
|
---|
730 | if (stringp == NULL || *stringp == NULL)
|
---|
731 | return NULL;
|
---|
732 |
|
---|
733 | s = *stringp;
|
---|
734 | for (tok = s; *tok; ++tok) {
|
---|
735 | for (spanp = delim; *spanp; ++spanp) {
|
---|
736 | /* found delimiter */
|
---|
737 | if (*tok == *spanp) {
|
---|
738 | *tok = '\0'; /* replace delim with null term */
|
---|
739 | *stringp = tok + 1; /* return past delim */
|
---|
740 | return s;
|
---|
741 | }
|
---|
742 | }
|
---|
743 | }
|
---|
744 |
|
---|
745 | *stringp = NULL;
|
---|
746 | return s;
|
---|
747 | }
|
---|
748 | #endif /* USE_WOLF_STRSEP */
|
---|
749 |
|
---|
750 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
751 | /* Mutex for protection of cryptography hardware */
|
---|
752 | static wolfSSL_Mutex wcCryptHwMutex;
|
---|
753 | static int wcCryptHwMutexInit = 0;
|
---|
754 |
|
---|
755 | int wolfSSL_CryptHwMutexInit(void) {
|
---|
756 | int ret = 0;
|
---|
757 | if(wcCryptHwMutexInit == 0) {
|
---|
758 | ret = wc_InitMutex(&wcCryptHwMutex);
|
---|
759 | if(ret == 0) {
|
---|
760 | wcCryptHwMutexInit = 1;
|
---|
761 | }
|
---|
762 | }
|
---|
763 | return ret;
|
---|
764 | }
|
---|
765 |
|
---|
766 | int wolfSSL_CryptHwMutexLock(void) {
|
---|
767 | int ret = BAD_MUTEX_E;
|
---|
768 |
|
---|
769 | /* Make sure HW Mutex has been initialized */
|
---|
770 | wolfSSL_CryptHwMutexInit();
|
---|
771 |
|
---|
772 | if(wcCryptHwMutexInit) {
|
---|
773 | ret = wc_LockMutex(&wcCryptHwMutex);
|
---|
774 | }
|
---|
775 | return ret;
|
---|
776 | }
|
---|
777 |
|
---|
778 | int wolfSSL_CryptHwMutexUnLock(void) {
|
---|
779 | int ret = BAD_MUTEX_E;
|
---|
780 |
|
---|
781 | if(wcCryptHwMutexInit) {
|
---|
782 | ret = wc_UnLockMutex(&wcCryptHwMutex);
|
---|
783 | }
|
---|
784 | return ret;
|
---|
785 | }
|
---|
786 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */
|
---|
787 |
|
---|
788 |
|
---|
789 | /* ---------------------------------------------------------------------------*/
|
---|
790 | /* Mutex Ports */
|
---|
791 | /* ---------------------------------------------------------------------------*/
|
---|
792 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
793 | static mutex_cb* compat_mutex_cb = NULL;
|
---|
794 |
|
---|
795 | /* Function that locks or unlocks a mutex based on the flag passed in.
|
---|
796 | *
|
---|
797 | * flag lock or unlock i.e. CRYPTO_LOCK
|
---|
798 | * type the type of lock to unlock or lock
|
---|
799 | * file name of the file calling
|
---|
800 | * line the line number from file calling
|
---|
801 | */
|
---|
802 | int wc_LockMutex_ex(int flag, int type, const char* file, int line)
|
---|
803 | {
|
---|
804 | if (compat_mutex_cb != NULL) {
|
---|
805 | compat_mutex_cb(flag, type, file, line);
|
---|
806 | return 0;
|
---|
807 | }
|
---|
808 | else {
|
---|
809 | WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
|
---|
810 | return BAD_STATE_E;
|
---|
811 | }
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | /* Set the callback function to use for locking/unlocking mutex
|
---|
816 | *
|
---|
817 | * cb callback function to use
|
---|
818 | */
|
---|
819 | int wc_SetMutexCb(mutex_cb* cb)
|
---|
820 | {
|
---|
821 | compat_mutex_cb = cb;
|
---|
822 | return 0;
|
---|
823 | }
|
---|
824 | #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
|
---|
825 | #ifdef SINGLE_THREADED
|
---|
826 |
|
---|
827 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
828 | {
|
---|
829 | (void)m;
|
---|
830 | return 0;
|
---|
831 | }
|
---|
832 |
|
---|
833 | int wc_FreeMutex(wolfSSL_Mutex *m)
|
---|
834 | {
|
---|
835 | (void)m;
|
---|
836 | return 0;
|
---|
837 | }
|
---|
838 |
|
---|
839 |
|
---|
840 | int wc_LockMutex(wolfSSL_Mutex *m)
|
---|
841 | {
|
---|
842 | (void)m;
|
---|
843 | return 0;
|
---|
844 | }
|
---|
845 |
|
---|
846 |
|
---|
847 | int wc_UnLockMutex(wolfSSL_Mutex *m)
|
---|
848 | {
|
---|
849 | (void)m;
|
---|
850 | return 0;
|
---|
851 | }
|
---|
852 |
|
---|
853 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
|
---|
854 | defined(FREESCALE_FREE_RTOS)
|
---|
855 |
|
---|
856 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
857 | {
|
---|
858 | int iReturn;
|
---|
859 |
|
---|
860 | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
|
---|
861 | if( *m != NULL )
|
---|
862 | iReturn = 0;
|
---|
863 | else
|
---|
864 | iReturn = BAD_MUTEX_E;
|
---|
865 |
|
---|
866 | return iReturn;
|
---|
867 | }
|
---|
868 |
|
---|
869 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
870 | {
|
---|
871 | vSemaphoreDelete( *m );
|
---|
872 | return 0;
|
---|
873 | }
|
---|
874 |
|
---|
875 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
876 | {
|
---|
877 | /* Assume an infinite block, or should there be zero block? */
|
---|
878 | xSemaphoreTake( *m, portMAX_DELAY );
|
---|
879 | return 0;
|
---|
880 | }
|
---|
881 |
|
---|
882 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
883 | {
|
---|
884 | xSemaphoreGive( *m );
|
---|
885 | return 0;
|
---|
886 | }
|
---|
887 |
|
---|
888 | #elif defined(WOLFSSL_SAFERTOS)
|
---|
889 |
|
---|
890 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
891 | {
|
---|
892 | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
|
---|
893 | if (m->mutex == NULL)
|
---|
894 | return BAD_MUTEX_E;
|
---|
895 |
|
---|
896 | return 0;
|
---|
897 | }
|
---|
898 |
|
---|
899 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
900 | {
|
---|
901 | (void)m;
|
---|
902 | return 0;
|
---|
903 | }
|
---|
904 |
|
---|
905 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
906 | {
|
---|
907 | /* Assume an infinite block */
|
---|
908 | xSemaphoreTake(m->mutex, portMAX_DELAY);
|
---|
909 | return 0;
|
---|
910 | }
|
---|
911 |
|
---|
912 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
913 | {
|
---|
914 | xSemaphoreGive(m->mutex);
|
---|
915 | return 0;
|
---|
916 | }
|
---|
917 |
|
---|
918 | #elif defined(USE_WINDOWS_API)
|
---|
919 |
|
---|
920 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
921 | {
|
---|
922 | InitializeCriticalSection(m);
|
---|
923 | return 0;
|
---|
924 | }
|
---|
925 |
|
---|
926 |
|
---|
927 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
928 | {
|
---|
929 | DeleteCriticalSection(m);
|
---|
930 | return 0;
|
---|
931 | }
|
---|
932 |
|
---|
933 |
|
---|
934 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
935 | {
|
---|
936 | EnterCriticalSection(m);
|
---|
937 | return 0;
|
---|
938 | }
|
---|
939 |
|
---|
940 |
|
---|
941 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
942 | {
|
---|
943 | LeaveCriticalSection(m);
|
---|
944 | return 0;
|
---|
945 | }
|
---|
946 |
|
---|
947 | #elif defined(WOLFSSL_PTHREADS)
|
---|
948 |
|
---|
949 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
950 | {
|
---|
951 | if (pthread_mutex_init(m, 0) == 0)
|
---|
952 | return 0;
|
---|
953 | else
|
---|
954 | return BAD_MUTEX_E;
|
---|
955 | }
|
---|
956 |
|
---|
957 |
|
---|
958 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
959 | {
|
---|
960 | if (pthread_mutex_destroy(m) == 0)
|
---|
961 | return 0;
|
---|
962 | else
|
---|
963 | return BAD_MUTEX_E;
|
---|
964 | }
|
---|
965 |
|
---|
966 |
|
---|
967 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
968 | {
|
---|
969 | if (pthread_mutex_lock(m) == 0)
|
---|
970 | return 0;
|
---|
971 | else
|
---|
972 | return BAD_MUTEX_E;
|
---|
973 | }
|
---|
974 |
|
---|
975 |
|
---|
976 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
977 | {
|
---|
978 | if (pthread_mutex_unlock(m) == 0)
|
---|
979 | return 0;
|
---|
980 | else
|
---|
981 | return BAD_MUTEX_E;
|
---|
982 | }
|
---|
983 |
|
---|
984 | #elif defined(WOLFSSL_VXWORKS)
|
---|
985 |
|
---|
986 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
987 | {
|
---|
988 | if (m) {
|
---|
989 | if ((*m = semMCreate(0)) != SEM_ID_NULL)
|
---|
990 | return 0;
|
---|
991 | }
|
---|
992 | return BAD_MUTEX_E;
|
---|
993 | }
|
---|
994 |
|
---|
995 |
|
---|
996 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
997 | {
|
---|
998 | if (m) {
|
---|
999 | if (semDelete(*m) == OK)
|
---|
1000 | return 0;
|
---|
1001 | }
|
---|
1002 | return BAD_MUTEX_E;
|
---|
1003 | }
|
---|
1004 |
|
---|
1005 |
|
---|
1006 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1007 | {
|
---|
1008 | if (m) {
|
---|
1009 | if (semTake(*m, WAIT_FOREVER) == OK)
|
---|
1010 | return 0;
|
---|
1011 | }
|
---|
1012 | return BAD_MUTEX_E;
|
---|
1013 | }
|
---|
1014 |
|
---|
1015 |
|
---|
1016 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1017 | {
|
---|
1018 | if (m) {
|
---|
1019 | if (semGive(*m) == OK)
|
---|
1020 | return 0;
|
---|
1021 | }
|
---|
1022 | return BAD_MUTEX_E;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | #elif defined(THREADX)
|
---|
1026 |
|
---|
1027 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1028 | {
|
---|
1029 | if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
|
---|
1030 | return 0;
|
---|
1031 | else
|
---|
1032 | return BAD_MUTEX_E;
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 |
|
---|
1036 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1037 | {
|
---|
1038 | if (tx_mutex_delete(m) == 0)
|
---|
1039 | return 0;
|
---|
1040 | else
|
---|
1041 | return BAD_MUTEX_E;
|
---|
1042 | }
|
---|
1043 |
|
---|
1044 |
|
---|
1045 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1046 | {
|
---|
1047 | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
|
---|
1048 | return 0;
|
---|
1049 | else
|
---|
1050 | return BAD_MUTEX_E;
|
---|
1051 | }
|
---|
1052 |
|
---|
1053 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1054 | {
|
---|
1055 | if (tx_mutex_put(m) == 0)
|
---|
1056 | return 0;
|
---|
1057 | else
|
---|
1058 | return BAD_MUTEX_E;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | #elif defined(WOLFSSL_DEOS)
|
---|
1062 |
|
---|
1063 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1064 | {
|
---|
1065 | mutexStatus mutStat;
|
---|
1066 | /*
|
---|
1067 | The empty string "" denotes an anonymous mutex, so objects do not cause name collisions.
|
---|
1068 | `protectWolfSSLTemp` in an XML configuration element template describing a mutex.
|
---|
1069 | */
|
---|
1070 | if (m) {
|
---|
1071 | mutStat = createMutex("", "protectWolfSSLTemp", m);
|
---|
1072 | if (mutStat == mutexSuccess)
|
---|
1073 | return 0;
|
---|
1074 | else{
|
---|
1075 | WOLFSSL_MSG("wc_InitMutex failed");
|
---|
1076 | return mutStat;
|
---|
1077 | }
|
---|
1078 | }
|
---|
1079 | return BAD_MUTEX_E;
|
---|
1080 | }
|
---|
1081 |
|
---|
1082 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1083 | {
|
---|
1084 | mutexStatus mutStat;
|
---|
1085 | if (m) {
|
---|
1086 | mutStat = deleteMutex(*m);
|
---|
1087 | if (mutStat == mutexSuccess)
|
---|
1088 | return 0;
|
---|
1089 | else{
|
---|
1090 | WOLFSSL_MSG("wc_FreeMutex failed");
|
---|
1091 | return mutStat;
|
---|
1092 | }
|
---|
1093 | }
|
---|
1094 | return BAD_MUTEX_E;
|
---|
1095 | }
|
---|
1096 |
|
---|
1097 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1098 | {
|
---|
1099 | mutexStatus mutStat;
|
---|
1100 | if (m) {
|
---|
1101 | mutStat = lockMutex(*m);
|
---|
1102 | if (mutStat == mutexSuccess)
|
---|
1103 | return 0;
|
---|
1104 | else{
|
---|
1105 | WOLFSSL_MSG("wc_LockMutex failed");
|
---|
1106 | return mutStat;
|
---|
1107 | }
|
---|
1108 | }
|
---|
1109 | return BAD_MUTEX_E;
|
---|
1110 | }
|
---|
1111 |
|
---|
1112 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1113 | {
|
---|
1114 | mutexStatus mutStat;
|
---|
1115 | if (m) {
|
---|
1116 | mutStat = unlockMutex(*m);
|
---|
1117 | if (mutStat== mutexSuccess)
|
---|
1118 | return 0;
|
---|
1119 | else{
|
---|
1120 | WOLFSSL_MSG("wc_UnLockMutex failed");
|
---|
1121 | return mutStat;
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 | return BAD_MUTEX_E;
|
---|
1125 | }
|
---|
1126 |
|
---|
1127 | #elif defined(MICRIUM)
|
---|
1128 |
|
---|
1129 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1130 | {
|
---|
1131 | OS_ERR err;
|
---|
1132 |
|
---|
1133 | OSMutexCreate(m, "wolfSSL Mutex", &err);
|
---|
1134 |
|
---|
1135 | if (err == OS_ERR_NONE)
|
---|
1136 | return 0;
|
---|
1137 | else
|
---|
1138 | return BAD_MUTEX_E;
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1142 | {
|
---|
1143 | #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
|
---|
1144 | OS_ERR err;
|
---|
1145 |
|
---|
1146 | OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
|
---|
1147 |
|
---|
1148 | if (err == OS_ERR_NONE)
|
---|
1149 | return 0;
|
---|
1150 | else
|
---|
1151 | return BAD_MUTEX_E;
|
---|
1152 | #else
|
---|
1153 | return 0;
|
---|
1154 | #endif
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1158 | {
|
---|
1159 | OS_ERR err;
|
---|
1160 |
|
---|
1161 | OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
|
---|
1162 |
|
---|
1163 | if (err == OS_ERR_NONE)
|
---|
1164 | return 0;
|
---|
1165 | else
|
---|
1166 | return BAD_MUTEX_E;
|
---|
1167 | }
|
---|
1168 |
|
---|
1169 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1170 | {
|
---|
1171 | OS_ERR err;
|
---|
1172 |
|
---|
1173 | OSMutexPost(m, OS_OPT_POST_NONE, &err);
|
---|
1174 |
|
---|
1175 | if (err == OS_ERR_NONE)
|
---|
1176 | return 0;
|
---|
1177 | else
|
---|
1178 | return BAD_MUTEX_E;
|
---|
1179 | }
|
---|
1180 |
|
---|
1181 | #elif defined(EBSNET)
|
---|
1182 |
|
---|
1183 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1184 | {
|
---|
1185 | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
|
---|
1186 | return BAD_MUTEX_E;
|
---|
1187 | else
|
---|
1188 | return 0;
|
---|
1189 | }
|
---|
1190 |
|
---|
1191 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1192 | {
|
---|
1193 | rtp_sig_mutex_free(*m);
|
---|
1194 | return 0;
|
---|
1195 | }
|
---|
1196 |
|
---|
1197 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1198 | {
|
---|
1199 | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
|
---|
1200 | return 0;
|
---|
1201 | else
|
---|
1202 | return BAD_MUTEX_E;
|
---|
1203 | }
|
---|
1204 |
|
---|
1205 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1206 | {
|
---|
1207 | rtp_sig_mutex_release(*m);
|
---|
1208 | return 0;
|
---|
1209 | }
|
---|
1210 |
|
---|
1211 | int ebsnet_fseek(int a, long b, int c)
|
---|
1212 | {
|
---|
1213 | int retval;
|
---|
1214 |
|
---|
1215 | retval = vf_lseek(a, b, c);
|
---|
1216 | if (retval > 0)
|
---|
1217 | retval = 0;
|
---|
1218 | else
|
---|
1219 | retval = -1;
|
---|
1220 |
|
---|
1221 | return(retval);
|
---|
1222 | }
|
---|
1223 |
|
---|
1224 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
1225 |
|
---|
1226 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1227 | {
|
---|
1228 | if (_mutex_init(m, NULL) == MQX_EOK)
|
---|
1229 | return 0;
|
---|
1230 | else
|
---|
1231 | return BAD_MUTEX_E;
|
---|
1232 | }
|
---|
1233 |
|
---|
1234 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1235 | {
|
---|
1236 | if (_mutex_destroy(m) == MQX_EOK)
|
---|
1237 | return 0;
|
---|
1238 | else
|
---|
1239 | return BAD_MUTEX_E;
|
---|
1240 | }
|
---|
1241 |
|
---|
1242 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1243 | {
|
---|
1244 | if (_mutex_lock(m) == MQX_EOK)
|
---|
1245 | return 0;
|
---|
1246 | else
|
---|
1247 | return BAD_MUTEX_E;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1251 | {
|
---|
1252 | if (_mutex_unlock(m) == MQX_EOK)
|
---|
1253 | return 0;
|
---|
1254 | else
|
---|
1255 | return BAD_MUTEX_E;
|
---|
1256 | }
|
---|
1257 |
|
---|
1258 | #elif defined(WOLFSSL_TIRTOS)
|
---|
1259 | #include <xdc/runtime/Error.h>
|
---|
1260 |
|
---|
1261 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1262 | {
|
---|
1263 | Semaphore_Params params;
|
---|
1264 | Error_Block eb;
|
---|
1265 |
|
---|
1266 | Error_init(&eb);
|
---|
1267 | Semaphore_Params_init(¶ms);
|
---|
1268 | params.mode = Semaphore_Mode_BINARY;
|
---|
1269 |
|
---|
1270 | *m = Semaphore_create(1, ¶ms, &eb);
|
---|
1271 | if (Error_check(&eb)) {
|
---|
1272 | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
|
---|
1273 | NULL);
|
---|
1274 | return BAD_MUTEX_E;
|
---|
1275 | }
|
---|
1276 | else
|
---|
1277 | return 0;
|
---|
1278 | }
|
---|
1279 |
|
---|
1280 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1281 | {
|
---|
1282 | Semaphore_delete(m);
|
---|
1283 |
|
---|
1284 | return 0;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1288 | {
|
---|
1289 | Semaphore_pend(*m, BIOS_WAIT_FOREVER);
|
---|
1290 |
|
---|
1291 | return 0;
|
---|
1292 | }
|
---|
1293 |
|
---|
1294 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1295 | {
|
---|
1296 | Semaphore_post(*m);
|
---|
1297 |
|
---|
1298 | return 0;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | #elif defined(WOLFSSL_uITRON4)
|
---|
1302 |
|
---|
1303 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1304 | {
|
---|
1305 | int iReturn;
|
---|
1306 | m->sem.sematr = TA_TFIFO;
|
---|
1307 | m->sem.isemcnt = 1;
|
---|
1308 | m->sem.maxsem = 1;
|
---|
1309 | m->sem.name = NULL;
|
---|
1310 |
|
---|
1311 | m->id = acre_sem(&m->sem);
|
---|
1312 | if( m->id != E_OK )
|
---|
1313 | iReturn = 0;
|
---|
1314 | else
|
---|
1315 | iReturn = BAD_MUTEX_E;
|
---|
1316 |
|
---|
1317 | return iReturn;
|
---|
1318 | }
|
---|
1319 |
|
---|
1320 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1321 | {
|
---|
1322 | del_sem( m->id );
|
---|
1323 | return 0;
|
---|
1324 | }
|
---|
1325 |
|
---|
1326 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1327 | {
|
---|
1328 | wai_sem(m->id);
|
---|
1329 | return 0;
|
---|
1330 | }
|
---|
1331 |
|
---|
1332 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1333 | {
|
---|
1334 | sig_sem(m->id);
|
---|
1335 | return 0;
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | /**** uITRON malloc/free ***/
|
---|
1339 | static ID ID_wolfssl_MPOOL = 0;
|
---|
1340 | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
|
---|
1341 |
|
---|
1342 | int uITRON4_minit(size_t poolsz) {
|
---|
1343 | ER ercd;
|
---|
1344 | wolfssl_MPOOL.mplsz = poolsz;
|
---|
1345 | ercd = acre_mpl(&wolfssl_MPOOL);
|
---|
1346 | if (ercd > 0) {
|
---|
1347 | ID_wolfssl_MPOOL = ercd;
|
---|
1348 | return 0;
|
---|
1349 | } else {
|
---|
1350 | return -1;
|
---|
1351 | }
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | void *uITRON4_malloc(size_t sz) {
|
---|
1355 | ER ercd;
|
---|
1356 | void *p = NULL;
|
---|
1357 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
|
---|
1358 | if (ercd == E_OK) {
|
---|
1359 | return p;
|
---|
1360 | } else {
|
---|
1361 | return 0;
|
---|
1362 | }
|
---|
1363 | }
|
---|
1364 |
|
---|
1365 | void *uITRON4_realloc(void *p, size_t sz) {
|
---|
1366 | ER ercd;
|
---|
1367 | void *newp;
|
---|
1368 | if(p) {
|
---|
1369 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
|
---|
1370 | if (ercd == E_OK) {
|
---|
1371 | XMEMCPY(newp, p, sz);
|
---|
1372 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1373 | if (ercd == E_OK) {
|
---|
1374 | return newp;
|
---|
1375 | }
|
---|
1376 | }
|
---|
1377 | }
|
---|
1378 | return 0;
|
---|
1379 | }
|
---|
1380 |
|
---|
1381 | void uITRON4_free(void *p) {
|
---|
1382 | ER ercd;
|
---|
1383 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1384 | if (ercd == E_OK) {
|
---|
1385 | return;
|
---|
1386 | } else {
|
---|
1387 | return;
|
---|
1388 | }
|
---|
1389 | }
|
---|
1390 |
|
---|
1391 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
1392 |
|
---|
1393 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1394 | {
|
---|
1395 | int iReturn;
|
---|
1396 | m->sem.sematr = TA_TFIFO;
|
---|
1397 | m->sem.isemcnt = 1;
|
---|
1398 | m->sem.maxsem = 1;
|
---|
1399 |
|
---|
1400 | m->id = tk_cre_sem(&m->sem);
|
---|
1401 | if( m->id != NULL )
|
---|
1402 | iReturn = 0;
|
---|
1403 | else
|
---|
1404 | iReturn = BAD_MUTEX_E;
|
---|
1405 |
|
---|
1406 | return iReturn;
|
---|
1407 | }
|
---|
1408 |
|
---|
1409 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1410 | {
|
---|
1411 | tk_del_sem(m->id);
|
---|
1412 | return 0;
|
---|
1413 | }
|
---|
1414 |
|
---|
1415 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1416 | {
|
---|
1417 | tk_wai_sem(m->id, 1, TMO_FEVR);
|
---|
1418 | return 0;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1422 | {
|
---|
1423 | tk_sig_sem(m->id, 1);
|
---|
1424 | return 0;
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 | /**** uT-Kernel malloc/free ***/
|
---|
1428 | static ID ID_wolfssl_MPOOL = 0;
|
---|
1429 | static T_CMPL wolfssl_MPOOL = {
|
---|
1430 | NULL, /* Extended information */
|
---|
1431 | TA_TFIFO, /* Memory pool attribute */
|
---|
1432 | 0, /* Size of whole memory pool (byte) */
|
---|
1433 | "wolfSSL" /* Object name (max 8-char) */
|
---|
1434 | };
|
---|
1435 |
|
---|
1436 | int uTKernel_init_mpool(unsigned int sz) {
|
---|
1437 | ER ercd;
|
---|
1438 | wolfssl_MPOOL.mplsz = sz;
|
---|
1439 | ercd = tk_cre_mpl(&wolfssl_MPOOL);
|
---|
1440 | if (ercd > 0) {
|
---|
1441 | ID_wolfssl_MPOOL = ercd;
|
---|
1442 | return 0;
|
---|
1443 | } else {
|
---|
1444 | return (int)ercd;
|
---|
1445 | }
|
---|
1446 | }
|
---|
1447 |
|
---|
1448 | void *uTKernel_malloc(unsigned int sz) {
|
---|
1449 | ER ercd;
|
---|
1450 | void *p = NULL;
|
---|
1451 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
|
---|
1452 | if (ercd == E_OK) {
|
---|
1453 | return p;
|
---|
1454 | } else {
|
---|
1455 | return 0;
|
---|
1456 | }
|
---|
1457 | }
|
---|
1458 |
|
---|
1459 | void *uTKernel_realloc(void *p, unsigned int sz) {
|
---|
1460 | ER ercd;
|
---|
1461 | void *newp;
|
---|
1462 | if (p) {
|
---|
1463 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
|
---|
1464 | if (ercd == E_OK) {
|
---|
1465 | XMEMCPY(newp, p, sz);
|
---|
1466 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1467 | if (ercd == E_OK) {
|
---|
1468 | return newp;
|
---|
1469 | }
|
---|
1470 | }
|
---|
1471 | }
|
---|
1472 | return 0;
|
---|
1473 | }
|
---|
1474 |
|
---|
1475 | void uTKernel_free(void *p) {
|
---|
1476 | ER ercd;
|
---|
1477 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1478 | if (ercd == E_OK) {
|
---|
1479 | return;
|
---|
1480 | } else {
|
---|
1481 | return;
|
---|
1482 | }
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | #elif defined (WOLFSSL_FROSTED)
|
---|
1486 |
|
---|
1487 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1488 | {
|
---|
1489 | *m = mutex_init();
|
---|
1490 | if (*m)
|
---|
1491 | return 0;
|
---|
1492 | else
|
---|
1493 | return -1;
|
---|
1494 | }
|
---|
1495 |
|
---|
1496 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1497 | {
|
---|
1498 | mutex_destroy(*m);
|
---|
1499 | return(0);
|
---|
1500 | }
|
---|
1501 |
|
---|
1502 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1503 | {
|
---|
1504 | mutex_lock(*m);
|
---|
1505 | return 0;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1509 | {
|
---|
1510 | mutex_unlock(*m);
|
---|
1511 | return 0;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
1515 |
|
---|
1516 | #define CMSIS_NMUTEX 10
|
---|
1517 | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
|
---|
1518 | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
|
---|
1519 | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
|
---|
1520 | osMutexDef(wolfSSL_mt9);
|
---|
1521 |
|
---|
1522 | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
|
---|
1523 | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
|
---|
1524 | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
|
---|
1525 | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
|
---|
1526 |
|
---|
1527 | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
|
---|
1528 |
|
---|
1529 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1530 | {
|
---|
1531 | int i;
|
---|
1532 | for (i=0; i<CMSIS_NMUTEX; i++) {
|
---|
1533 | if(CMSIS_mutexID[i] == 0) {
|
---|
1534 | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
|
---|
1535 | (*m) = CMSIS_mutexID[i];
|
---|
1536 | return 0;
|
---|
1537 | }
|
---|
1538 | }
|
---|
1539 | return -1;
|
---|
1540 | }
|
---|
1541 |
|
---|
1542 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1543 | {
|
---|
1544 | int i;
|
---|
1545 | osMutexDelete (*m);
|
---|
1546 | for (i=0; i<CMSIS_NMUTEX; i++) {
|
---|
1547 | if(CMSIS_mutexID[i] == (*m)) {
|
---|
1548 | CMSIS_mutexID[i] = 0;
|
---|
1549 | return(0);
|
---|
1550 | }
|
---|
1551 | }
|
---|
1552 | return(-1);
|
---|
1553 | }
|
---|
1554 |
|
---|
1555 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1556 | {
|
---|
1557 | osMutexWait(*m, osWaitForever);
|
---|
1558 | return(0);
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1562 | {
|
---|
1563 | osMutexRelease (*m);
|
---|
1564 | return 0;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | #elif defined(WOLFSSL_CMSIS_RTOSv2)
|
---|
1568 | int wc_InitMutex(wolfSSL_Mutex *m)
|
---|
1569 | {
|
---|
1570 | static const osMutexAttr_t attr = {
|
---|
1571 | "wolfSSL_mutex", osMutexRecursive, NULL, 0};
|
---|
1572 |
|
---|
1573 | if ((*m = osMutexNew(&attr)) != NULL)
|
---|
1574 | return 0;
|
---|
1575 | else
|
---|
1576 | return BAD_MUTEX_E;
|
---|
1577 | }
|
---|
1578 |
|
---|
1579 | int wc_FreeMutex(wolfSSL_Mutex *m)
|
---|
1580 | {
|
---|
1581 | if (osMutexDelete(*m) == osOK)
|
---|
1582 | return 0;
|
---|
1583 | else
|
---|
1584 | return BAD_MUTEX_E;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | int wc_LockMutex(wolfSSL_Mutex *m)
|
---|
1589 | {
|
---|
1590 | if (osMutexAcquire(*m, osWaitForever) == osOK)
|
---|
1591 | return 0;
|
---|
1592 | else
|
---|
1593 | return BAD_MUTEX_E;
|
---|
1594 | }
|
---|
1595 |
|
---|
1596 | int wc_UnLockMutex(wolfSSL_Mutex *m)
|
---|
1597 | {
|
---|
1598 | if (osMutexRelease(*m) == osOK)
|
---|
1599 | return 0;
|
---|
1600 | else
|
---|
1601 | return BAD_MUTEX_E;
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
1605 |
|
---|
1606 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1607 | {
|
---|
1608 | os_mut_init (m);
|
---|
1609 | return 0;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1613 | {
|
---|
1614 | return(0);
|
---|
1615 | }
|
---|
1616 |
|
---|
1617 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1618 | {
|
---|
1619 | os_mut_wait (m, 0xffff);
|
---|
1620 | return(0);
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1624 | {
|
---|
1625 | os_mut_release (m);
|
---|
1626 | return 0;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | #elif defined(INTIME_RTOS)
|
---|
1630 |
|
---|
1631 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1632 | {
|
---|
1633 | int ret = 0;
|
---|
1634 |
|
---|
1635 | if (m == NULL)
|
---|
1636 | return BAD_FUNC_ARG;
|
---|
1637 |
|
---|
1638 | *m = CreateRtSemaphore(
|
---|
1639 | 1, /* initial unit count */
|
---|
1640 | 1, /* maximum unit count */
|
---|
1641 | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
|
---|
1642 | );
|
---|
1643 | if (*m == BAD_RTHANDLE) {
|
---|
1644 | ret = GetLastRtError();
|
---|
1645 | if (ret != E_OK)
|
---|
1646 | ret = BAD_MUTEX_E;
|
---|
1647 | }
|
---|
1648 | return ret;
|
---|
1649 | }
|
---|
1650 |
|
---|
1651 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1652 | {
|
---|
1653 | int ret = 0;
|
---|
1654 | BOOLEAN del;
|
---|
1655 |
|
---|
1656 | if (m == NULL)
|
---|
1657 | return BAD_FUNC_ARG;
|
---|
1658 |
|
---|
1659 | del = DeleteRtSemaphore(
|
---|
1660 | *m /* handle for RT semaphore */
|
---|
1661 | );
|
---|
1662 | if (del != TRUE)
|
---|
1663 | ret = BAD_MUTEX_E;
|
---|
1664 |
|
---|
1665 | return ret;
|
---|
1666 | }
|
---|
1667 |
|
---|
1668 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1669 | {
|
---|
1670 | int ret = 0;
|
---|
1671 | DWORD lck;
|
---|
1672 |
|
---|
1673 | if (m == NULL)
|
---|
1674 | return BAD_FUNC_ARG;
|
---|
1675 |
|
---|
1676 | lck = WaitForRtSemaphore(
|
---|
1677 | *m, /* handle for RT semaphore */
|
---|
1678 | 1, /* number of units to wait for */
|
---|
1679 | WAIT_FOREVER /* number of milliseconds to wait for units */
|
---|
1680 | );
|
---|
1681 | if (lck == WAIT_FAILED) {
|
---|
1682 | ret = GetLastRtError();
|
---|
1683 | if (ret != E_OK)
|
---|
1684 | ret = BAD_MUTEX_E;
|
---|
1685 | }
|
---|
1686 | return ret;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1690 | {
|
---|
1691 | int ret = 0;
|
---|
1692 | BOOLEAN rel;
|
---|
1693 |
|
---|
1694 | if (m == NULL)
|
---|
1695 | return BAD_FUNC_ARG;
|
---|
1696 |
|
---|
1697 | rel = ReleaseRtSemaphore(
|
---|
1698 | *m, /* handle for RT semaphore */
|
---|
1699 | 1 /* number of units to release to semaphore */
|
---|
1700 | );
|
---|
1701 | if (rel != TRUE)
|
---|
1702 | ret = BAD_MUTEX_E;
|
---|
1703 |
|
---|
1704 | return ret;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
1708 |
|
---|
1709 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1710 | {
|
---|
1711 | /* Call the Nucleus function to create the semaphore */
|
---|
1712 | if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
|
---|
1713 | NU_PRIORITY) == NU_SUCCESS) {
|
---|
1714 | return 0;
|
---|
1715 | }
|
---|
1716 |
|
---|
1717 | return BAD_MUTEX_E;
|
---|
1718 | }
|
---|
1719 |
|
---|
1720 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1721 | {
|
---|
1722 | if (NU_Delete_Semaphore(m) == NU_SUCCESS)
|
---|
1723 | return 0;
|
---|
1724 |
|
---|
1725 | return BAD_MUTEX_E;
|
---|
1726 | }
|
---|
1727 |
|
---|
1728 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1729 | {
|
---|
1730 | /* passing suspend task option */
|
---|
1731 | if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
|
---|
1732 | return 0;
|
---|
1733 |
|
---|
1734 | return BAD_MUTEX_E;
|
---|
1735 | }
|
---|
1736 |
|
---|
1737 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1738 | {
|
---|
1739 | if (NU_Release_Semaphore(m) == NU_SUCCESS)
|
---|
1740 | return 0;
|
---|
1741 |
|
---|
1742 | return BAD_MUTEX_E;
|
---|
1743 | }
|
---|
1744 |
|
---|
1745 | #elif defined(WOLFSSL_ZEPHYR)
|
---|
1746 |
|
---|
1747 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1748 | {
|
---|
1749 | k_mutex_init(m);
|
---|
1750 |
|
---|
1751 | return 0;
|
---|
1752 | }
|
---|
1753 |
|
---|
1754 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1755 | {
|
---|
1756 | return 0;
|
---|
1757 | }
|
---|
1758 |
|
---|
1759 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1760 | {
|
---|
1761 | int ret = 0;
|
---|
1762 |
|
---|
1763 | if (k_mutex_lock(m, K_FOREVER) != 0)
|
---|
1764 | ret = BAD_MUTEX_E;
|
---|
1765 |
|
---|
1766 | return ret;
|
---|
1767 | }
|
---|
1768 |
|
---|
1769 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1770 | {
|
---|
1771 | k_mutex_unlock(m);
|
---|
1772 |
|
---|
1773 | return 0;
|
---|
1774 | }
|
---|
1775 |
|
---|
1776 | #elif defined(WOLFSSL_TELIT_M2MB)
|
---|
1777 |
|
---|
1778 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1779 | {
|
---|
1780 | M2MB_OS_RESULT_E osRes;
|
---|
1781 | M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
|
---|
1782 | UINT32 inheritVal = 1;
|
---|
1783 |
|
---|
1784 | osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
|
---|
1785 | CMDS_ARGS(
|
---|
1786 | M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
|
---|
1787 | M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
|
---|
1788 | M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
|
---|
1789 | )
|
---|
1790 | );
|
---|
1791 | if (osRes != M2MB_OS_SUCCESS) {
|
---|
1792 | return BAD_MUTEX_E;
|
---|
1793 | }
|
---|
1794 |
|
---|
1795 | osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
|
---|
1796 | if (osRes != M2MB_OS_SUCCESS) {
|
---|
1797 | return BAD_MUTEX_E;
|
---|
1798 | }
|
---|
1799 |
|
---|
1800 | return 0;
|
---|
1801 | }
|
---|
1802 |
|
---|
1803 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1804 | {
|
---|
1805 | M2MB_OS_RESULT_E osRes;
|
---|
1806 |
|
---|
1807 | if (m == NULL)
|
---|
1808 | return BAD_MUTEX_E;
|
---|
1809 |
|
---|
1810 | osRes = m2mb_os_mtx_deinit(*m);
|
---|
1811 | if (osRes != M2MB_OS_SUCCESS) {
|
---|
1812 | return BAD_MUTEX_E;
|
---|
1813 | }
|
---|
1814 |
|
---|
1815 | return 0;
|
---|
1816 | }
|
---|
1817 |
|
---|
1818 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1819 | {
|
---|
1820 | M2MB_OS_RESULT_E osRes;
|
---|
1821 |
|
---|
1822 | if (m == NULL)
|
---|
1823 | return BAD_MUTEX_E;
|
---|
1824 |
|
---|
1825 | osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
|
---|
1826 | if (osRes != M2MB_OS_SUCCESS) {
|
---|
1827 | return BAD_MUTEX_E;
|
---|
1828 | }
|
---|
1829 |
|
---|
1830 | return 0;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1834 | {
|
---|
1835 | M2MB_OS_RESULT_E osRes;
|
---|
1836 |
|
---|
1837 | if (m == NULL)
|
---|
1838 | return BAD_MUTEX_E;
|
---|
1839 |
|
---|
1840 | osRes = m2mb_os_mtx_put(*m);
|
---|
1841 | if (osRes != M2MB_OS_SUCCESS) {
|
---|
1842 | return BAD_MUTEX_E;
|
---|
1843 | }
|
---|
1844 |
|
---|
1845 | return 0;
|
---|
1846 | }
|
---|
1847 |
|
---|
1848 | #else
|
---|
1849 | #warning No mutex handling defined
|
---|
1850 |
|
---|
1851 | #endif
|
---|
1852 |
|
---|
1853 | #ifndef NO_ASN_TIME
|
---|
1854 | #if defined(_WIN32_WCE)
|
---|
1855 | time_t windows_time(time_t* timer)
|
---|
1856 | {
|
---|
1857 | SYSTEMTIME sysTime;
|
---|
1858 | FILETIME fTime;
|
---|
1859 | ULARGE_INTEGER intTime;
|
---|
1860 | time_t localTime;
|
---|
1861 |
|
---|
1862 | if (timer == NULL)
|
---|
1863 | timer = &localTime;
|
---|
1864 |
|
---|
1865 | GetSystemTime(&sysTime);
|
---|
1866 | SystemTimeToFileTime(&sysTime, &fTime);
|
---|
1867 |
|
---|
1868 | XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
|
---|
1869 | /* subtract EPOCH */
|
---|
1870 | intTime.QuadPart -= 0x19db1ded53e8000;
|
---|
1871 | /* to secs */
|
---|
1872 | intTime.QuadPart /= 10000000;
|
---|
1873 | *timer = (time_t)intTime.QuadPart;
|
---|
1874 |
|
---|
1875 | return *timer;
|
---|
1876 | }
|
---|
1877 | #endif /* _WIN32_WCE */
|
---|
1878 |
|
---|
1879 | #if defined(WOLFSSL_APACHE_MYNEWT)
|
---|
1880 | #include "os/os_time.h"
|
---|
1881 |
|
---|
1882 | time_t mynewt_time(time_t* timer)
|
---|
1883 | {
|
---|
1884 | time_t now;
|
---|
1885 | struct os_timeval tv;
|
---|
1886 | os_gettimeofday(&tv, NULL);
|
---|
1887 | now = (time_t)tv.tv_sec;
|
---|
1888 | if(timer != NULL) {
|
---|
1889 | *timer = now;
|
---|
1890 | }
|
---|
1891 | return now;
|
---|
1892 | }
|
---|
1893 | #endif /* WOLFSSL_APACHE_MYNEWT */
|
---|
1894 |
|
---|
1895 | #if defined(WOLFSSL_GMTIME)
|
---|
1896 | struct tm* gmtime(const time_t* timer)
|
---|
1897 | {
|
---|
1898 | #define YEAR0 1900
|
---|
1899 | #define EPOCH_YEAR 1970
|
---|
1900 | #define SECS_DAY (24L * 60L * 60L)
|
---|
1901 | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
|
---|
1902 | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
|
---|
1903 |
|
---|
1904 | static const int _ytab[2][12] =
|
---|
1905 | {
|
---|
1906 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
---|
1907 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
---|
1908 | };
|
---|
1909 |
|
---|
1910 | static struct tm st_time;
|
---|
1911 | struct tm* ret = &st_time;
|
---|
1912 | time_t secs = *timer;
|
---|
1913 | unsigned long dayclock, dayno;
|
---|
1914 | int year = EPOCH_YEAR;
|
---|
1915 |
|
---|
1916 | dayclock = (unsigned long)secs % SECS_DAY;
|
---|
1917 | dayno = (unsigned long)secs / SECS_DAY;
|
---|
1918 |
|
---|
1919 | ret->tm_sec = (int) dayclock % 60;
|
---|
1920 | ret->tm_min = (int)(dayclock % 3600) / 60;
|
---|
1921 | ret->tm_hour = (int) dayclock / 3600;
|
---|
1922 | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
|
---|
1923 |
|
---|
1924 | while(dayno >= (unsigned long)YEARSIZE(year)) {
|
---|
1925 | dayno -= YEARSIZE(year);
|
---|
1926 | year++;
|
---|
1927 | }
|
---|
1928 |
|
---|
1929 | ret->tm_year = year - YEAR0;
|
---|
1930 | ret->tm_yday = (int)dayno;
|
---|
1931 | ret->tm_mon = 0;
|
---|
1932 |
|
---|
1933 | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
|
---|
1934 | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
|
---|
1935 | ret->tm_mon++;
|
---|
1936 | }
|
---|
1937 |
|
---|
1938 | ret->tm_mday = (int)++dayno;
|
---|
1939 | ret->tm_isdst = 0;
|
---|
1940 |
|
---|
1941 | return ret;
|
---|
1942 | }
|
---|
1943 | #endif /* WOLFSSL_GMTIME */
|
---|
1944 |
|
---|
1945 |
|
---|
1946 | #if defined(HAVE_RTP_SYS)
|
---|
1947 | #define YEAR0 1900
|
---|
1948 |
|
---|
1949 | struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
|
---|
1950 | {
|
---|
1951 | static struct tm st_time;
|
---|
1952 | struct tm* ret = &st_time;
|
---|
1953 |
|
---|
1954 | DC_RTC_CALENDAR cal;
|
---|
1955 | dc_rtc_time_get(&cal, TRUE);
|
---|
1956 |
|
---|
1957 | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
|
---|
1958 | ret->tm_mon = cal.month - 1; /* gm starts at 0 */
|
---|
1959 | ret->tm_mday = cal.day;
|
---|
1960 | ret->tm_hour = cal.hour;
|
---|
1961 | ret->tm_min = cal.minute;
|
---|
1962 | ret->tm_sec = cal.second;
|
---|
1963 |
|
---|
1964 | return ret;
|
---|
1965 | }
|
---|
1966 |
|
---|
1967 | #endif /* HAVE_RTP_SYS */
|
---|
1968 |
|
---|
1969 |
|
---|
1970 | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
|
---|
1971 |
|
---|
1972 | /*
|
---|
1973 | * time() is just a stub in Microchip libraries. We need our own
|
---|
1974 | * implementation. Use SNTP client to get seconds since epoch.
|
---|
1975 | */
|
---|
1976 | time_t pic32_time(time_t* timer)
|
---|
1977 | {
|
---|
1978 | #ifdef MICROCHIP_TCPIP_V5
|
---|
1979 | DWORD sec = 0;
|
---|
1980 | #else
|
---|
1981 | uint32_t sec = 0;
|
---|
1982 | #endif
|
---|
1983 | time_t localTime;
|
---|
1984 |
|
---|
1985 | if (timer == NULL)
|
---|
1986 | timer = &localTime;
|
---|
1987 |
|
---|
1988 | #ifdef MICROCHIP_MPLAB_HARMONY
|
---|
1989 | sec = TCPIP_SNTP_UTCSecondsGet();
|
---|
1990 | #else
|
---|
1991 | sec = SNTPGetUTCSeconds();
|
---|
1992 | #endif
|
---|
1993 | *timer = (time_t) sec;
|
---|
1994 |
|
---|
1995 | return *timer;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
|
---|
1999 |
|
---|
2000 | #if defined(WOLFSSL_DEOS)
|
---|
2001 |
|
---|
2002 | time_t deos_time(time_t* timer)
|
---|
2003 | {
|
---|
2004 | const uint32_t systemTickTimeInHz = 1000000 / systemTickInMicroseconds();
|
---|
2005 | uint32_t *systemTickPtr = systemTickPointer();
|
---|
2006 |
|
---|
2007 | if (timer != NULL)
|
---|
2008 | *timer = *systemTickPtr/systemTickTimeInHz;
|
---|
2009 |
|
---|
2010 | #if defined(CURRENT_UNIX_TIMESTAMP)
|
---|
2011 | /* CURRENT_UNIX_TIMESTAMP is seconds since Jan 01 1970. (UTC) */
|
---|
2012 | return (time_t) *systemTickPtr/systemTickTimeInHz + CURRENT_UNIX_TIMESTAMP;
|
---|
2013 | #else
|
---|
2014 | return (time_t) *systemTickPtr/systemTickTimeInHz;
|
---|
2015 | #endif
|
---|
2016 | }
|
---|
2017 | #endif /* WOLFSSL_DEOS */
|
---|
2018 |
|
---|
2019 | #if defined(MICRIUM)
|
---|
2020 |
|
---|
2021 | time_t micrium_time(time_t* timer)
|
---|
2022 | {
|
---|
2023 | CLK_TS_SEC sec;
|
---|
2024 |
|
---|
2025 | Clk_GetTS_Unix(&sec);
|
---|
2026 |
|
---|
2027 | if (timer != NULL)
|
---|
2028 | *timer = sec;
|
---|
2029 |
|
---|
2030 | return (time_t) sec;
|
---|
2031 | }
|
---|
2032 |
|
---|
2033 | #endif /* MICRIUM */
|
---|
2034 |
|
---|
2035 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
2036 |
|
---|
2037 | time_t mqx_time(time_t* timer)
|
---|
2038 | {
|
---|
2039 | time_t localTime;
|
---|
2040 | TIME_STRUCT time_s;
|
---|
2041 |
|
---|
2042 | if (timer == NULL)
|
---|
2043 | timer = &localTime;
|
---|
2044 |
|
---|
2045 | _time_get(&time_s);
|
---|
2046 | *timer = (time_t) time_s.SECONDS;
|
---|
2047 |
|
---|
2048 | return *timer;
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 | #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
|
---|
2052 |
|
---|
2053 |
|
---|
2054 | #if defined(WOLFSSL_TIRTOS) && defined(USER_TIME)
|
---|
2055 |
|
---|
2056 | time_t XTIME(time_t * timer)
|
---|
2057 | {
|
---|
2058 | time_t sec = 0;
|
---|
2059 |
|
---|
2060 | sec = (time_t) Seconds_get();
|
---|
2061 |
|
---|
2062 | if (timer != NULL)
|
---|
2063 | *timer = sec;
|
---|
2064 |
|
---|
2065 | return sec;
|
---|
2066 | }
|
---|
2067 |
|
---|
2068 | #endif /* WOLFSSL_TIRTOS */
|
---|
2069 |
|
---|
2070 | #if defined(WOLFSSL_XILINX)
|
---|
2071 | #include "xrtcpsu.h"
|
---|
2072 |
|
---|
2073 | time_t XTIME(time_t * timer)
|
---|
2074 | {
|
---|
2075 | time_t sec = 0;
|
---|
2076 | XRtcPsu_Config* con;
|
---|
2077 | XRtcPsu rtc;
|
---|
2078 |
|
---|
2079 | con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
|
---|
2080 | if (con != NULL) {
|
---|
2081 | if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
|
---|
2082 | sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
|
---|
2083 | }
|
---|
2084 | else {
|
---|
2085 | WOLFSSL_MSG("Unable to initialize RTC");
|
---|
2086 | }
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | if (timer != NULL)
|
---|
2090 | *timer = sec;
|
---|
2091 |
|
---|
2092 | return sec;
|
---|
2093 | }
|
---|
2094 |
|
---|
2095 | #endif /* WOLFSSL_XILINX */
|
---|
2096 |
|
---|
2097 | #if defined(WOLFSSL_ZEPHYR)
|
---|
2098 |
|
---|
2099 | time_t z_time(time_t * timer)
|
---|
2100 | {
|
---|
2101 | struct timespec ts;
|
---|
2102 |
|
---|
2103 | if (clock_gettime(CLOCK_REALTIME, &ts) == 0)
|
---|
2104 | if (timer != NULL)
|
---|
2105 | *timer = ts.tv_sec;
|
---|
2106 |
|
---|
2107 | return ts.tv_sec;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | #endif /* WOLFSSL_ZEPHYR */
|
---|
2111 |
|
---|
2112 |
|
---|
2113 | #if defined(WOLFSSL_WICED)
|
---|
2114 | #ifndef WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME
|
---|
2115 | #error Please define WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME at build time.
|
---|
2116 | #endif /* WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME */
|
---|
2117 |
|
---|
2118 | time_t wiced_pseudo_unix_epoch_time(time_t * timer)
|
---|
2119 | {
|
---|
2120 | time_t epoch_time;
|
---|
2121 | /* The time() function return uptime on WICED platform. */
|
---|
2122 | epoch_time = time(NULL) + WOLFSSL_WICED_PSEUDO_UNIX_EPOCH_TIME;
|
---|
2123 |
|
---|
2124 | if (timer != NULL) {
|
---|
2125 | *timer = epoch_time;
|
---|
2126 | }
|
---|
2127 | return epoch_time;
|
---|
2128 | }
|
---|
2129 | #endif /* WOLFSSL_WICED */
|
---|
2130 |
|
---|
2131 | #ifdef WOLFSSL_TELIT_M2MB
|
---|
2132 | time_t m2mb_xtime(time_t * timer)
|
---|
2133 | {
|
---|
2134 | time_t myTime = 0;
|
---|
2135 | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
|
---|
2136 | if (fd != -1) {
|
---|
2137 | M2MB_RTC_TIMEVAL_T timeval;
|
---|
2138 |
|
---|
2139 | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
|
---|
2140 |
|
---|
2141 | myTime = timeval.sec;
|
---|
2142 |
|
---|
2143 | m2mb_rtc_close(fd);
|
---|
2144 | }
|
---|
2145 | return myTime;
|
---|
2146 | }
|
---|
2147 | #ifdef WOLFSSL_TLS13
|
---|
2148 | time_t m2mb_xtime_ms(time_t * timer)
|
---|
2149 | {
|
---|
2150 | time_t myTime = 0;
|
---|
2151 | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
|
---|
2152 | if (fd != -1) {
|
---|
2153 | M2MB_RTC_TIMEVAL_T timeval;
|
---|
2154 |
|
---|
2155 | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
|
---|
2156 |
|
---|
2157 | myTime = timeval.sec + timeval.msec;
|
---|
2158 |
|
---|
2159 | m2mb_rtc_close(fd);
|
---|
2160 | }
|
---|
2161 | return myTime;
|
---|
2162 | }
|
---|
2163 | #endif /* WOLFSSL_TLS13 */
|
---|
2164 | #ifndef NO_CRYPT_BENCHMARK
|
---|
2165 | double m2mb_xtime_bench(int reset)
|
---|
2166 | {
|
---|
2167 | double myTime = 0;
|
---|
2168 | INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
|
---|
2169 | if (fd != -1) {
|
---|
2170 | M2MB_RTC_TIMEVAL_T timeval;
|
---|
2171 |
|
---|
2172 | m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
|
---|
2173 |
|
---|
2174 | myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
|
---|
2175 |
|
---|
2176 | m2mb_rtc_close(fd);
|
---|
2177 | }
|
---|
2178 | return myTime;
|
---|
2179 | }
|
---|
2180 | #endif /* !NO_CRYPT_BENCHMARK */
|
---|
2181 | #endif /* WOLFSSL_TELIT_M2MB */
|
---|
2182 |
|
---|
2183 | #endif /* !NO_ASN_TIME */
|
---|
2184 |
|
---|
2185 | #ifndef WOLFSSL_LEANPSK
|
---|
2186 | char* mystrnstr(const char* s1, const char* s2, unsigned int n)
|
---|
2187 | {
|
---|
2188 | unsigned int s2_len = (unsigned int)XSTRLEN(s2);
|
---|
2189 |
|
---|
2190 | if (s2_len == 0)
|
---|
2191 | return (char*)s1;
|
---|
2192 |
|
---|
2193 | while (n >= s2_len && s1[0]) {
|
---|
2194 | if (s1[0] == s2[0])
|
---|
2195 | if (XMEMCMP(s1, s2, s2_len) == 0)
|
---|
2196 | return (char*)s1;
|
---|
2197 | s1++;
|
---|
2198 | n--;
|
---|
2199 | }
|
---|
2200 |
|
---|
2201 | return NULL;
|
---|
2202 | }
|
---|
2203 | #endif
|
---|
2204 |
|
---|
2205 | /* custom memory wrappers */
|
---|
2206 | #ifdef WOLFSSL_NUCLEUS_1_2
|
---|
2207 |
|
---|
2208 | /* system memory pool */
|
---|
2209 | extern NU_MEMORY_POOL System_Memory;
|
---|
2210 |
|
---|
2211 | void* nucleus_malloc(unsigned long size, void* heap, int type)
|
---|
2212 | {
|
---|
2213 | STATUS status;
|
---|
2214 | void* stack_ptr;
|
---|
2215 |
|
---|
2216 | status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
|
---|
2217 | NU_NO_SUSPEND);
|
---|
2218 | if (status == NU_SUCCESS) {
|
---|
2219 | return 0;
|
---|
2220 | } else {
|
---|
2221 | return stack_ptr;
|
---|
2222 | }
|
---|
2223 | }
|
---|
2224 |
|
---|
2225 | void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
|
---|
2226 | {
|
---|
2227 | DM_HEADER* old_header;
|
---|
2228 | word32 old_size, copy_size;
|
---|
2229 | void* new_mem;
|
---|
2230 |
|
---|
2231 | /* if ptr is NULL, behave like malloc */
|
---|
2232 | new_mem = nucleus_malloc(size, NULL, 0);
|
---|
2233 | if (new_mem == 0 || ptr == 0) {
|
---|
2234 | return new_mem;
|
---|
2235 | }
|
---|
2236 |
|
---|
2237 | /* calculate old memory block size */
|
---|
2238 | /* mem pointers stored in block headers (ref dm_defs.h) */
|
---|
2239 | old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
|
---|
2240 | old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
|
---|
2241 |
|
---|
2242 | /* copy old to new */
|
---|
2243 | if (old_size < size) {
|
---|
2244 | copy_size = old_size;
|
---|
2245 | } else {
|
---|
2246 | copy_size = size;
|
---|
2247 | }
|
---|
2248 | XMEMCPY(new_mem, ptr, copy_size);
|
---|
2249 |
|
---|
2250 | /* free old */
|
---|
2251 | nucleus_free(ptr, NULL, 0);
|
---|
2252 |
|
---|
2253 | return new_mem;
|
---|
2254 | }
|
---|
2255 |
|
---|
2256 | void nucleus_free(void* ptr, void* heap, int type)
|
---|
2257 | {
|
---|
2258 | if (ptr != NULL)
|
---|
2259 | NU_Deallocate_Memory(ptr);
|
---|
2260 | }
|
---|
2261 |
|
---|
2262 | #endif /* WOLFSSL_NUCLEUS_1_2 */
|
---|
2263 |
|
---|
2264 | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
|
---|
2265 | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
|
---|
2266 | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
|
---|
2267 | #endif
|
---|
2268 |
|
---|
2269 | #if defined(WOLFSSL_CRYPTOCELL)
|
---|
2270 | #define WOLFSSL_CRYPTOCELL_C
|
---|
2271 | #include <wolfcrypt/src/port/arm/cryptoCell.c> /* CC310, RTC and RNG */
|
---|
2272 | #if !defined(NO_SHA256)
|
---|
2273 | #define WOLFSSL_CRYPTOCELL_HASH_C
|
---|
2274 | #include <wolfcrypt/src/port/arm/cryptoCellHash.c> /* sha256 */
|
---|
2275 | #endif
|
---|
2276 | #endif
|
---|