source: azure_iot_hub_f767zi/trunk/wolfssl-4.7.0/wolfcrypt/src/wc_port.c

Last change on this file was 464, checked in by coas-nagasima, 3 years ago

WolfSSLとAzure IoT SDKを更新

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