1 | /* port.c
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2017 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 |
|
---|
53 | #if defined(WOLFSSL_STSAFEA100)
|
---|
54 | #include <wolfssl/wolfcrypt/port/st/stsafe.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
58 | #include <wolfssl/openssl/evp.h>
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
|
---|
62 | #include <wolfssl/wolfcrypt/memory.h>
|
---|
63 | #include <wolfssl/wolfcrypt/mem_track.h>
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
67 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
68 | #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
|
---|
69 | #endif
|
---|
70 |
|
---|
71 | #ifdef WOLF_CRYPTO_DEV
|
---|
72 | #include <wolfssl/wolfcrypt/cryptodev.h>
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | #ifdef _MSC_VER
|
---|
76 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
|
---|
77 | #pragma warning(disable: 4996)
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | /* prevent multiple mutex initializations */
|
---|
81 | static volatile int initRefCount = 0;
|
---|
82 |
|
---|
83 | /* Used to initialize state for wolfcrypt
|
---|
84 | return 0 on success
|
---|
85 | */
|
---|
86 | int wolfCrypt_Init(void)
|
---|
87 | {
|
---|
88 | int ret = 0;
|
---|
89 |
|
---|
90 | if (initRefCount == 0) {
|
---|
91 | WOLFSSL_ENTER("wolfCrypt_Init");
|
---|
92 |
|
---|
93 | #ifdef WOLFSSL_FORCE_MALLOC_FAIL_TEST
|
---|
94 | {
|
---|
95 | word32 rngMallocFail;
|
---|
96 | time_t seed = time(NULL);
|
---|
97 | srand((word32)seed);
|
---|
98 | rngMallocFail = rand() % 2000; /* max 2000 */
|
---|
99 | printf("\n--- RNG MALLOC FAIL AT %d---\n", rngMallocFail);
|
---|
100 | wolfSSL_SetMemFailCount(rngMallocFail);
|
---|
101 | }
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | #ifdef WOLF_CRYPTO_DEV
|
---|
105 | wc_CryptoDev_Init();
|
---|
106 | #endif
|
---|
107 |
|
---|
108 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
109 | ret = wolfAsync_HardwareStart();
|
---|
110 | if (ret != 0) {
|
---|
111 | WOLFSSL_MSG("Async hardware start failed");
|
---|
112 | /* don't return failure, allow operation to continue */
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 |
|
---|
116 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
---|
117 | ret = InitMemoryTracker();
|
---|
118 | if (ret != 0) {
|
---|
119 | WOLFSSL_MSG("InitMemoryTracker failed");
|
---|
120 | return ret;
|
---|
121 | }
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
125 | /* If crypto hardware mutex protection is enabled, then initialize it */
|
---|
126 | ret = wolfSSL_CryptHwMutexInit();
|
---|
127 | if (ret != 0) {
|
---|
128 | WOLFSSL_MSG("Hw crypt mutex init failed");
|
---|
129 | return ret;
|
---|
130 | }
|
---|
131 | #endif
|
---|
132 |
|
---|
133 | /* if defined have fast RSA then initialize Intel IPP */
|
---|
134 | #ifdef HAVE_FAST_RSA
|
---|
135 | WOLFSSL_MSG("Attempting to use optimized IPP Library");
|
---|
136 | if ((ret = ippInit()) != ippStsNoErr) {
|
---|
137 | /* possible to get a CPU feature support status on optimized IPP
|
---|
138 | library but still use default library and see competitive speeds */
|
---|
139 | WOLFSSL_MSG("Warning when trying to set up optimization");
|
---|
140 | WOLFSSL_MSG(ippGetStatusString(ret));
|
---|
141 | WOLFSSL_MSG("Using default fast IPP library");
|
---|
142 | ret = 0;
|
---|
143 | (void)ret; /* suppress not read warning */
|
---|
144 | }
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
|
---|
148 | ret = ksdk_port_init();
|
---|
149 | if (ret != 0) {
|
---|
150 | WOLFSSL_MSG("KSDK port init failed");
|
---|
151 | return ret;
|
---|
152 | }
|
---|
153 | #endif
|
---|
154 |
|
---|
155 | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
|
---|
156 | ret = atmel_init();
|
---|
157 | if (ret != 0) {
|
---|
158 | WOLFSSL_MSG("CryptoAuthLib init failed");
|
---|
159 | return ret;
|
---|
160 | }
|
---|
161 | #endif
|
---|
162 |
|
---|
163 | #if defined(WOLFSSL_STSAFEA100)
|
---|
164 | stsafe_interface_init();
|
---|
165 | #endif
|
---|
166 |
|
---|
167 | #ifdef WOLFSSL_ARMASM
|
---|
168 | WOLFSSL_MSG("Using ARM hardware acceleration");
|
---|
169 | #endif
|
---|
170 |
|
---|
171 | #ifdef WOLFSSL_AFALG
|
---|
172 | WOLFSSL_MSG("Using AF_ALG for crypto acceleration");
|
---|
173 | #endif
|
---|
174 |
|
---|
175 | #if !defined(WOLFCRYPT_ONLY) && \
|
---|
176 | ( defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) )
|
---|
177 | wolfSSL_EVP_init();
|
---|
178 | #endif
|
---|
179 |
|
---|
180 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
|
---|
181 | if ((ret = wc_LoggingInit()) != 0) {
|
---|
182 | WOLFSSL_MSG("Error creating logging mutex");
|
---|
183 | return ret;
|
---|
184 | }
|
---|
185 | #endif
|
---|
186 |
|
---|
187 | #ifdef HAVE_ECC
|
---|
188 | #ifdef ECC_CACHE_CURVE
|
---|
189 | if ((ret = wc_ecc_curve_cache_init()) != 0) {
|
---|
190 | WOLFSSL_MSG("Error creating curve cache");
|
---|
191 | return ret;
|
---|
192 | }
|
---|
193 | #endif
|
---|
194 | #endif
|
---|
195 |
|
---|
196 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
197 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
198 | if ((ret = wc_caamInit()) != 0) {
|
---|
199 | return ret;
|
---|
200 | }
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | initRefCount = 1;
|
---|
204 | }
|
---|
205 |
|
---|
206 | return ret;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | /* return success value is the same as wolfCrypt_Init */
|
---|
211 | int wolfCrypt_Cleanup(void)
|
---|
212 | {
|
---|
213 | int ret = 0;
|
---|
214 |
|
---|
215 | if (initRefCount == 1) {
|
---|
216 | WOLFSSL_ENTER("wolfCrypt_Cleanup");
|
---|
217 |
|
---|
218 | #ifdef HAVE_ECC
|
---|
219 | #ifdef FP_ECC
|
---|
220 | wc_ecc_fp_free();
|
---|
221 | #endif
|
---|
222 | #ifdef ECC_CACHE_CURVE
|
---|
223 | wc_ecc_curve_cache_free();
|
---|
224 | #endif
|
---|
225 | #endif /* HAVE_ECC */
|
---|
226 |
|
---|
227 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
|
---|
228 | ret = wc_LoggingCleanup();
|
---|
229 | #endif
|
---|
230 |
|
---|
231 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
|
---|
232 | ShowMemoryTracker();
|
---|
233 | #endif
|
---|
234 |
|
---|
235 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
236 | wolfAsync_HardwareStop();
|
---|
237 | #endif
|
---|
238 |
|
---|
239 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
|
---|
240 | defined(WOLFSSL_IMX6_CAAM_BLOB)
|
---|
241 | wc_caamFree();
|
---|
242 | #endif
|
---|
243 |
|
---|
244 | initRefCount = 0; /* allow re-init */
|
---|
245 | }
|
---|
246 |
|
---|
247 | return ret;
|
---|
248 | }
|
---|
249 |
|
---|
250 | #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
|
---|
251 | !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
|
---|
252 |
|
---|
253 | /* File Handling Helpers */
|
---|
254 | /* returns 0 if file found, WC_READDIR_NOFILE if no files or negative error */
|
---|
255 | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
|
---|
256 | {
|
---|
257 | int ret = WC_READDIR_NOFILE; /* default to no files found */
|
---|
258 | int pathLen = 0;
|
---|
259 | int dnameLen = 0;
|
---|
260 |
|
---|
261 | if (name)
|
---|
262 | *name = NULL;
|
---|
263 |
|
---|
264 | if (ctx == NULL || path == NULL) {
|
---|
265 | return BAD_FUNC_ARG;
|
---|
266 | }
|
---|
267 |
|
---|
268 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
|
---|
269 | pathLen = (int)XSTRLEN(path);
|
---|
270 |
|
---|
271 | #ifdef USE_WINDOWS_API
|
---|
272 | if (pathLen > MAX_FILENAME_SZ - 3)
|
---|
273 | return BAD_PATH_ERROR;
|
---|
274 |
|
---|
275 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 3);
|
---|
276 | XSTRNCPY(ctx->name + pathLen, "\\*", MAX_FILENAME_SZ - pathLen);
|
---|
277 |
|
---|
278 | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
|
---|
279 | if (ctx->hFind == INVALID_HANDLE_VALUE) {
|
---|
280 | WOLFSSL_MSG("FindFirstFile for path verify locations failed");
|
---|
281 | return BAD_PATH_ERROR;
|
---|
282 | }
|
---|
283 |
|
---|
284 | do {
|
---|
285 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
|
---|
286 | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
|
---|
287 |
|
---|
288 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
289 | return BAD_PATH_ERROR;
|
---|
290 | }
|
---|
291 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
292 | ctx->name[pathLen] = '\\';
|
---|
293 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
294 | ctx->FindFileData.cFileName,
|
---|
295 | MAX_FILENAME_SZ - pathLen - 1);
|
---|
296 | if (name)
|
---|
297 | *name = ctx->name;
|
---|
298 | return 0;
|
---|
299 | }
|
---|
300 | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
|
---|
301 | #else
|
---|
302 | ctx->dir = opendir(path);
|
---|
303 | if (ctx->dir == NULL) {
|
---|
304 | WOLFSSL_MSG("opendir path verify locations failed");
|
---|
305 | return BAD_PATH_ERROR;
|
---|
306 | }
|
---|
307 |
|
---|
308 | while ((ctx->entry = readdir(ctx->dir)) != NULL) {
|
---|
309 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
310 |
|
---|
311 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
312 | ret = BAD_PATH_ERROR;
|
---|
313 | break;
|
---|
314 | }
|
---|
315 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
316 | ctx->name[pathLen] = '/';
|
---|
317 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
318 | ctx->entry->d_name, MAX_FILENAME_SZ - pathLen - 1);
|
---|
319 |
|
---|
320 | if (stat(ctx->name, &ctx->s) != 0) {
|
---|
321 | WOLFSSL_MSG("stat on name failed");
|
---|
322 | ret = BAD_PATH_ERROR;
|
---|
323 | break;
|
---|
324 | } else if (S_ISREG(ctx->s.st_mode)) {
|
---|
325 | if (name)
|
---|
326 | *name = ctx->name;
|
---|
327 | return 0;
|
---|
328 | }
|
---|
329 | }
|
---|
330 | #endif
|
---|
331 | wc_ReadDirClose(ctx);
|
---|
332 |
|
---|
333 | return ret;
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* returns 0 if file found, WC_READDIR_NOFILE if no more files */
|
---|
337 | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
|
---|
338 | {
|
---|
339 | int ret = WC_READDIR_NOFILE; /* default to no file found */
|
---|
340 | int pathLen = 0;
|
---|
341 | int dnameLen = 0;
|
---|
342 |
|
---|
343 | if (name)
|
---|
344 | *name = NULL;
|
---|
345 |
|
---|
346 | if (ctx == NULL || path == NULL) {
|
---|
347 | return BAD_FUNC_ARG;
|
---|
348 | }
|
---|
349 |
|
---|
350 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
|
---|
351 | pathLen = (int)XSTRLEN(path);
|
---|
352 |
|
---|
353 | #ifdef USE_WINDOWS_API
|
---|
354 | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
|
---|
355 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
|
---|
356 | dnameLen = (int)XSTRLEN(ctx->FindFileData.cFileName);
|
---|
357 |
|
---|
358 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
359 | return BAD_PATH_ERROR;
|
---|
360 | }
|
---|
361 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
362 | ctx->name[pathLen] = '\\';
|
---|
363 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
364 | ctx->FindFileData.cFileName,
|
---|
365 | MAX_FILENAME_SZ - pathLen - 1);
|
---|
366 | if (name)
|
---|
367 | *name = ctx->name;
|
---|
368 | return 0;
|
---|
369 | }
|
---|
370 | }
|
---|
371 | #else
|
---|
372 | while ((ctx->entry = readdir(ctx->dir)) != NULL) {
|
---|
373 | dnameLen = (int)XSTRLEN(ctx->entry->d_name);
|
---|
374 |
|
---|
375 | if (pathLen + dnameLen + 2 > MAX_FILENAME_SZ) {
|
---|
376 | ret = BAD_PATH_ERROR;
|
---|
377 | break;
|
---|
378 | }
|
---|
379 | XSTRNCPY(ctx->name, path, pathLen + 1);
|
---|
380 | ctx->name[pathLen] = '/';
|
---|
381 | XSTRNCPY(ctx->name + pathLen + 1,
|
---|
382 | ctx->entry->d_name, MAX_FILENAME_SZ - pathLen - 1);
|
---|
383 |
|
---|
384 | if (stat(ctx->name, &ctx->s) != 0) {
|
---|
385 | WOLFSSL_MSG("stat on name failed");
|
---|
386 | ret = BAD_PATH_ERROR;
|
---|
387 | break;
|
---|
388 | } else if (S_ISREG(ctx->s.st_mode)) {
|
---|
389 | if (name)
|
---|
390 | *name = ctx->name;
|
---|
391 | return 0;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | #endif
|
---|
395 |
|
---|
396 | wc_ReadDirClose(ctx);
|
---|
397 |
|
---|
398 | return ret;
|
---|
399 | }
|
---|
400 |
|
---|
401 | void wc_ReadDirClose(ReadDirCtx* ctx)
|
---|
402 | {
|
---|
403 | if (ctx == NULL) {
|
---|
404 | return;
|
---|
405 | }
|
---|
406 |
|
---|
407 | #ifdef USE_WINDOWS_API
|
---|
408 | if (ctx->hFind != INVALID_HANDLE_VALUE) {
|
---|
409 | FindClose(ctx->hFind);
|
---|
410 | ctx->hFind = INVALID_HANDLE_VALUE;
|
---|
411 | }
|
---|
412 | #else
|
---|
413 | if (ctx->dir) {
|
---|
414 | closedir(ctx->dir);
|
---|
415 | ctx->dir = NULL;
|
---|
416 | }
|
---|
417 | #endif
|
---|
418 | }
|
---|
419 |
|
---|
420 | #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
|
---|
421 |
|
---|
422 |
|
---|
423 | wolfSSL_Mutex* wc_InitAndAllocMutex(void)
|
---|
424 | {
|
---|
425 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
|
---|
426 | DYNAMIC_TYPE_MUTEX);
|
---|
427 | if (m != NULL) {
|
---|
428 | if (wc_InitMutex(m) != 0) {
|
---|
429 | WOLFSSL_MSG("Init Mutex failed");
|
---|
430 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
|
---|
431 | m = NULL;
|
---|
432 | }
|
---|
433 | }
|
---|
434 | else {
|
---|
435 | WOLFSSL_MSG("Memory error with Mutex allocation");
|
---|
436 | }
|
---|
437 |
|
---|
438 | return m;
|
---|
439 | }
|
---|
440 |
|
---|
441 | #ifdef USE_WOLF_STRTOK
|
---|
442 | /* String token (delim) search. If str is null use nextp. */
|
---|
443 | char* wc_strtok(char *str, const char *delim, char **nextp)
|
---|
444 | {
|
---|
445 | char* ret;
|
---|
446 | int i, j;
|
---|
447 |
|
---|
448 | /* Use next if str is NULL */
|
---|
449 | if (str == NULL && nextp)
|
---|
450 | str = *nextp;
|
---|
451 |
|
---|
452 | /* verify str input */
|
---|
453 | if (str == NULL || *str == '\0')
|
---|
454 | return NULL;
|
---|
455 |
|
---|
456 | /* match on entire delim */
|
---|
457 | for (i = 0; str[i]; i++) {
|
---|
458 | for (j = 0; delim[j]; j++) {
|
---|
459 | if (delim[j] == str[i])
|
---|
460 | break;
|
---|
461 | }
|
---|
462 | if (!delim[j])
|
---|
463 | break;
|
---|
464 | }
|
---|
465 | str += i;
|
---|
466 | /* if end of string, not found so return NULL */
|
---|
467 | if (*str == '\0')
|
---|
468 | return NULL;
|
---|
469 |
|
---|
470 | ret = str;
|
---|
471 |
|
---|
472 | /* match on first delim */
|
---|
473 | for (i = 0; str[i]; i++) {
|
---|
474 | for (j = 0; delim[j]; j++) {
|
---|
475 | if (delim[j] == str[i])
|
---|
476 | break;
|
---|
477 | }
|
---|
478 | if (delim[j] == str[i])
|
---|
479 | break;
|
---|
480 | }
|
---|
481 | str += i;
|
---|
482 |
|
---|
483 | /* null terminate found string */
|
---|
484 | if (*str)
|
---|
485 | *str++ = '\0';
|
---|
486 |
|
---|
487 | /* return pointer to next */
|
---|
488 | if (nextp)
|
---|
489 | *nextp = str;
|
---|
490 |
|
---|
491 | return ret;
|
---|
492 | }
|
---|
493 | #endif /* USE_WOLF_STRTOK */
|
---|
494 |
|
---|
495 | #ifdef USE_WOLF_STRSEP
|
---|
496 | char* wc_strsep(char **stringp, const char *delim)
|
---|
497 | {
|
---|
498 | char *s, *tok;
|
---|
499 | const char *spanp;
|
---|
500 |
|
---|
501 | /* null check */
|
---|
502 | if (stringp == NULL || *stringp == NULL)
|
---|
503 | return NULL;
|
---|
504 |
|
---|
505 | s = *stringp;
|
---|
506 | for (tok = s; *tok; ++tok) {
|
---|
507 | for (spanp = delim; *spanp; ++spanp) {
|
---|
508 | /* found delimiter */
|
---|
509 | if (*tok == *spanp) {
|
---|
510 | *tok = '\0'; /* replace delim with null term */
|
---|
511 | *stringp = tok + 1; /* return past delim */
|
---|
512 | return s;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | }
|
---|
516 |
|
---|
517 | *stringp = NULL;
|
---|
518 | return s;
|
---|
519 | }
|
---|
520 | #endif /* USE_WOLF_STRSEP */
|
---|
521 |
|
---|
522 | #if WOLFSSL_CRYPT_HW_MUTEX
|
---|
523 | /* Mutex for protection of cryptography hardware */
|
---|
524 | static wolfSSL_Mutex wcCryptHwMutex;
|
---|
525 | static int wcCryptHwMutexInit = 0;
|
---|
526 |
|
---|
527 | int wolfSSL_CryptHwMutexInit(void) {
|
---|
528 | int ret = 0;
|
---|
529 | if(wcCryptHwMutexInit == 0) {
|
---|
530 | ret = wc_InitMutex(&wcCryptHwMutex);
|
---|
531 | if(ret == 0) {
|
---|
532 | wcCryptHwMutexInit = 1;
|
---|
533 | }
|
---|
534 | }
|
---|
535 | return ret;
|
---|
536 | }
|
---|
537 |
|
---|
538 | int wolfSSL_CryptHwMutexLock(void) {
|
---|
539 | int ret = BAD_MUTEX_E;
|
---|
540 |
|
---|
541 | /* Make sure HW Mutex has been initialized */
|
---|
542 | wolfSSL_CryptHwMutexInit();
|
---|
543 |
|
---|
544 | if(wcCryptHwMutexInit) {
|
---|
545 | ret = wc_LockMutex(&wcCryptHwMutex);
|
---|
546 | }
|
---|
547 | return ret;
|
---|
548 | }
|
---|
549 |
|
---|
550 | int wolfSSL_CryptHwMutexUnLock(void) {
|
---|
551 | int ret = BAD_MUTEX_E;
|
---|
552 |
|
---|
553 | if(wcCryptHwMutexInit) {
|
---|
554 | ret = wc_UnLockMutex(&wcCryptHwMutex);
|
---|
555 | }
|
---|
556 | return ret;
|
---|
557 | }
|
---|
558 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */
|
---|
559 |
|
---|
560 |
|
---|
561 | /* ---------------------------------------------------------------------------*/
|
---|
562 | /* Mutex Ports */
|
---|
563 | /* ---------------------------------------------------------------------------*/
|
---|
564 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
---|
565 | static mutex_cb* compat_mutex_cb = NULL;
|
---|
566 |
|
---|
567 | /* Function that locks or unlocks a mutex based on the flag passed in.
|
---|
568 | *
|
---|
569 | * flag lock or unlock i.e. CRYPTO_LOCK
|
---|
570 | * type the type of lock to unlock or lock
|
---|
571 | * file name of the file calling
|
---|
572 | * line the line number from file calling
|
---|
573 | */
|
---|
574 | int wc_LockMutex_ex(int flag, int type, const char* file, int line)
|
---|
575 | {
|
---|
576 | if (compat_mutex_cb != NULL) {
|
---|
577 | compat_mutex_cb(flag, type, file, line);
|
---|
578 | return 0;
|
---|
579 | }
|
---|
580 | else {
|
---|
581 | WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
|
---|
582 | return BAD_STATE_E;
|
---|
583 | }
|
---|
584 | }
|
---|
585 |
|
---|
586 |
|
---|
587 | /* Set the callback function to use for locking/unlocking mutex
|
---|
588 | *
|
---|
589 | * cb callback function to use
|
---|
590 | */
|
---|
591 | int wc_SetMutexCb(mutex_cb* cb)
|
---|
592 | {
|
---|
593 | compat_mutex_cb = cb;
|
---|
594 | return 0;
|
---|
595 | }
|
---|
596 | #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
|
---|
597 | #ifdef SINGLE_THREADED
|
---|
598 |
|
---|
599 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
600 | {
|
---|
601 | (void)m;
|
---|
602 | return 0;
|
---|
603 | }
|
---|
604 |
|
---|
605 | int wc_FreeMutex(wolfSSL_Mutex *m)
|
---|
606 | {
|
---|
607 | (void)m;
|
---|
608 | return 0;
|
---|
609 | }
|
---|
610 |
|
---|
611 |
|
---|
612 | int wc_LockMutex(wolfSSL_Mutex *m)
|
---|
613 | {
|
---|
614 | (void)m;
|
---|
615 | return 0;
|
---|
616 | }
|
---|
617 |
|
---|
618 |
|
---|
619 | int wc_UnLockMutex(wolfSSL_Mutex *m)
|
---|
620 | {
|
---|
621 | (void)m;
|
---|
622 | return 0;
|
---|
623 | }
|
---|
624 |
|
---|
625 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
|
---|
626 | defined(FREESCALE_FREE_RTOS)
|
---|
627 |
|
---|
628 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
629 | {
|
---|
630 | int iReturn;
|
---|
631 |
|
---|
632 | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
|
---|
633 | if( *m != NULL )
|
---|
634 | iReturn = 0;
|
---|
635 | else
|
---|
636 | iReturn = BAD_MUTEX_E;
|
---|
637 |
|
---|
638 | return iReturn;
|
---|
639 | }
|
---|
640 |
|
---|
641 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
642 | {
|
---|
643 | vSemaphoreDelete( *m );
|
---|
644 | return 0;
|
---|
645 | }
|
---|
646 |
|
---|
647 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
648 | {
|
---|
649 | /* Assume an infinite block, or should there be zero block? */
|
---|
650 | xSemaphoreTake( *m, portMAX_DELAY );
|
---|
651 | return 0;
|
---|
652 | }
|
---|
653 |
|
---|
654 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
655 | {
|
---|
656 | xSemaphoreGive( *m );
|
---|
657 | return 0;
|
---|
658 | }
|
---|
659 |
|
---|
660 | #elif defined(WOLFSSL_SAFERTOS)
|
---|
661 |
|
---|
662 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
663 | {
|
---|
664 | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
|
---|
665 | if (m->mutex == NULL)
|
---|
666 | return BAD_MUTEX_E;
|
---|
667 |
|
---|
668 | return 0;
|
---|
669 | }
|
---|
670 |
|
---|
671 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
672 | {
|
---|
673 | (void)m;
|
---|
674 | return 0;
|
---|
675 | }
|
---|
676 |
|
---|
677 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
678 | {
|
---|
679 | /* Assume an infinite block */
|
---|
680 | xSemaphoreTake(m->mutex, portMAX_DELAY);
|
---|
681 | return 0;
|
---|
682 | }
|
---|
683 |
|
---|
684 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
685 | {
|
---|
686 | xSemaphoreGive(m->mutex);
|
---|
687 | return 0;
|
---|
688 | }
|
---|
689 |
|
---|
690 | #elif defined(USE_WINDOWS_API)
|
---|
691 |
|
---|
692 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
693 | {
|
---|
694 | InitializeCriticalSection(m);
|
---|
695 | return 0;
|
---|
696 | }
|
---|
697 |
|
---|
698 |
|
---|
699 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
700 | {
|
---|
701 | DeleteCriticalSection(m);
|
---|
702 | return 0;
|
---|
703 | }
|
---|
704 |
|
---|
705 |
|
---|
706 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
707 | {
|
---|
708 | EnterCriticalSection(m);
|
---|
709 | return 0;
|
---|
710 | }
|
---|
711 |
|
---|
712 |
|
---|
713 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
714 | {
|
---|
715 | LeaveCriticalSection(m);
|
---|
716 | return 0;
|
---|
717 | }
|
---|
718 |
|
---|
719 | #elif defined(WOLFSSL_PTHREADS)
|
---|
720 |
|
---|
721 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
722 | {
|
---|
723 | if (pthread_mutex_init(m, 0) == 0)
|
---|
724 | return 0;
|
---|
725 | else
|
---|
726 | return BAD_MUTEX_E;
|
---|
727 | }
|
---|
728 |
|
---|
729 |
|
---|
730 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
731 | {
|
---|
732 | if (pthread_mutex_destroy(m) == 0)
|
---|
733 | return 0;
|
---|
734 | else
|
---|
735 | return BAD_MUTEX_E;
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
740 | {
|
---|
741 | if (pthread_mutex_lock(m) == 0)
|
---|
742 | return 0;
|
---|
743 | else
|
---|
744 | return BAD_MUTEX_E;
|
---|
745 | }
|
---|
746 |
|
---|
747 |
|
---|
748 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
749 | {
|
---|
750 | if (pthread_mutex_unlock(m) == 0)
|
---|
751 | return 0;
|
---|
752 | else
|
---|
753 | return BAD_MUTEX_E;
|
---|
754 | }
|
---|
755 |
|
---|
756 | #elif defined(WOLFSSL_VXWORKS)
|
---|
757 |
|
---|
758 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
759 | {
|
---|
760 | if (m) {
|
---|
761 | if ((*m = semMCreate(0)) != SEM_ID_NULL)
|
---|
762 | return 0;
|
---|
763 | }
|
---|
764 | return BAD_MUTEX_E;
|
---|
765 | }
|
---|
766 |
|
---|
767 |
|
---|
768 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
769 | {
|
---|
770 | if (m) {
|
---|
771 | if (semDelete(*m) == OK)
|
---|
772 | return 0;
|
---|
773 | }
|
---|
774 | return BAD_MUTEX_E;
|
---|
775 | }
|
---|
776 |
|
---|
777 |
|
---|
778 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
779 | {
|
---|
780 | if (m) {
|
---|
781 | if (semTake(*m, WAIT_FOREVER) == OK)
|
---|
782 | return 0;
|
---|
783 | }
|
---|
784 | return BAD_MUTEX_E;
|
---|
785 | }
|
---|
786 |
|
---|
787 |
|
---|
788 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
789 | {
|
---|
790 | if (m) {
|
---|
791 | if (semGive(*m) == OK)
|
---|
792 | return 0;
|
---|
793 | }
|
---|
794 | return BAD_MUTEX_E;
|
---|
795 | }
|
---|
796 |
|
---|
797 | #elif defined(THREADX)
|
---|
798 |
|
---|
799 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
800 | {
|
---|
801 | if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
|
---|
802 | return 0;
|
---|
803 | else
|
---|
804 | return BAD_MUTEX_E;
|
---|
805 | }
|
---|
806 |
|
---|
807 |
|
---|
808 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
809 | {
|
---|
810 | if (tx_mutex_delete(m) == 0)
|
---|
811 | return 0;
|
---|
812 | else
|
---|
813 | return BAD_MUTEX_E;
|
---|
814 | }
|
---|
815 |
|
---|
816 |
|
---|
817 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
818 | {
|
---|
819 | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
|
---|
820 | return 0;
|
---|
821 | else
|
---|
822 | return BAD_MUTEX_E;
|
---|
823 | }
|
---|
824 |
|
---|
825 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
826 | {
|
---|
827 | if (tx_mutex_put(m) == 0)
|
---|
828 | return 0;
|
---|
829 | else
|
---|
830 | return BAD_MUTEX_E;
|
---|
831 | }
|
---|
832 |
|
---|
833 | #elif defined(MICRIUM)
|
---|
834 |
|
---|
835 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
836 | {
|
---|
837 | OS_ERR err;
|
---|
838 |
|
---|
839 | OSMutexCreate(m, "wolfSSL Mutex", &err);
|
---|
840 |
|
---|
841 | if (err == OS_ERR_NONE)
|
---|
842 | return 0;
|
---|
843 | else
|
---|
844 | return BAD_MUTEX_E;
|
---|
845 | }
|
---|
846 |
|
---|
847 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
848 | {
|
---|
849 | #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
|
---|
850 | OS_ERR err;
|
---|
851 |
|
---|
852 | OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
|
---|
853 |
|
---|
854 | if (err == OS_ERR_NONE)
|
---|
855 | return 0;
|
---|
856 | else
|
---|
857 | return BAD_MUTEX_E;
|
---|
858 | #else
|
---|
859 | return 0;
|
---|
860 | #endif
|
---|
861 | }
|
---|
862 |
|
---|
863 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
864 | {
|
---|
865 | OS_ERR err;
|
---|
866 |
|
---|
867 | OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
|
---|
868 |
|
---|
869 | if (err == OS_ERR_NONE)
|
---|
870 | return 0;
|
---|
871 | else
|
---|
872 | return BAD_MUTEX_E;
|
---|
873 | }
|
---|
874 |
|
---|
875 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
876 | {
|
---|
877 | OS_ERR err;
|
---|
878 |
|
---|
879 | OSMutexPost(m, OS_OPT_POST_NONE, &err);
|
---|
880 |
|
---|
881 | if (err == OS_ERR_NONE)
|
---|
882 | return 0;
|
---|
883 | else
|
---|
884 | return BAD_MUTEX_E;
|
---|
885 | }
|
---|
886 |
|
---|
887 | #elif defined(EBSNET)
|
---|
888 |
|
---|
889 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
890 | {
|
---|
891 | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
|
---|
892 | return BAD_MUTEX_E;
|
---|
893 | else
|
---|
894 | return 0;
|
---|
895 | }
|
---|
896 |
|
---|
897 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
898 | {
|
---|
899 | rtp_sig_mutex_free(*m);
|
---|
900 | return 0;
|
---|
901 | }
|
---|
902 |
|
---|
903 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
904 | {
|
---|
905 | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
|
---|
906 | return 0;
|
---|
907 | else
|
---|
908 | return BAD_MUTEX_E;
|
---|
909 | }
|
---|
910 |
|
---|
911 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
912 | {
|
---|
913 | rtp_sig_mutex_release(*m);
|
---|
914 | return 0;
|
---|
915 | }
|
---|
916 |
|
---|
917 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
918 |
|
---|
919 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
920 | {
|
---|
921 | if (_mutex_init(m, NULL) == MQX_EOK)
|
---|
922 | return 0;
|
---|
923 | else
|
---|
924 | return BAD_MUTEX_E;
|
---|
925 | }
|
---|
926 |
|
---|
927 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
928 | {
|
---|
929 | if (_mutex_destroy(m) == MQX_EOK)
|
---|
930 | return 0;
|
---|
931 | else
|
---|
932 | return BAD_MUTEX_E;
|
---|
933 | }
|
---|
934 |
|
---|
935 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
936 | {
|
---|
937 | if (_mutex_lock(m) == MQX_EOK)
|
---|
938 | return 0;
|
---|
939 | else
|
---|
940 | return BAD_MUTEX_E;
|
---|
941 | }
|
---|
942 |
|
---|
943 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
944 | {
|
---|
945 | if (_mutex_unlock(m) == MQX_EOK)
|
---|
946 | return 0;
|
---|
947 | else
|
---|
948 | return BAD_MUTEX_E;
|
---|
949 | }
|
---|
950 |
|
---|
951 | #elif defined(WOLFSSL_TIRTOS)
|
---|
952 | #include <xdc/runtime/Error.h>
|
---|
953 |
|
---|
954 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
955 | {
|
---|
956 | Semaphore_Params params;
|
---|
957 | Error_Block eb;
|
---|
958 |
|
---|
959 | Error_init(&eb);
|
---|
960 | Semaphore_Params_init(¶ms);
|
---|
961 | params.mode = Semaphore_Mode_BINARY;
|
---|
962 |
|
---|
963 | *m = Semaphore_create(1, ¶ms, &eb);
|
---|
964 | if (Error_check(&eb)) {
|
---|
965 | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
|
---|
966 | NULL);
|
---|
967 | return BAD_MUTEX_E;
|
---|
968 | }
|
---|
969 | else
|
---|
970 | return 0;
|
---|
971 | }
|
---|
972 |
|
---|
973 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
974 | {
|
---|
975 | Semaphore_delete(m);
|
---|
976 |
|
---|
977 | return 0;
|
---|
978 | }
|
---|
979 |
|
---|
980 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
981 | {
|
---|
982 | Semaphore_pend(*m, BIOS_WAIT_FOREVER);
|
---|
983 |
|
---|
984 | return 0;
|
---|
985 | }
|
---|
986 |
|
---|
987 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
988 | {
|
---|
989 | Semaphore_post(*m);
|
---|
990 |
|
---|
991 | return 0;
|
---|
992 | }
|
---|
993 |
|
---|
994 | #elif defined(WOLFSSL_uITRON4)
|
---|
995 |
|
---|
996 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
997 | {
|
---|
998 | int iReturn;
|
---|
999 | m->sem.sematr = TA_TFIFO;
|
---|
1000 | m->sem.isemcnt = 1;
|
---|
1001 | m->sem.maxsem = 1;
|
---|
1002 | m->sem.name = NULL;
|
---|
1003 |
|
---|
1004 | m->id = acre_sem(&m->sem);
|
---|
1005 | if( m->id != E_OK )
|
---|
1006 | iReturn = 0;
|
---|
1007 | else
|
---|
1008 | iReturn = BAD_MUTEX_E;
|
---|
1009 |
|
---|
1010 | return iReturn;
|
---|
1011 | }
|
---|
1012 |
|
---|
1013 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1014 | {
|
---|
1015 | del_sem( m->id );
|
---|
1016 | return 0;
|
---|
1017 | }
|
---|
1018 |
|
---|
1019 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1020 | {
|
---|
1021 | wai_sem(m->id);
|
---|
1022 | return 0;
|
---|
1023 | }
|
---|
1024 |
|
---|
1025 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1026 | {
|
---|
1027 | sig_sem(m->id);
|
---|
1028 | return 0;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 | /**** uITRON malloc/free ***/
|
---|
1032 | static ID ID_wolfssl_MPOOL = 0;
|
---|
1033 | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
|
---|
1034 |
|
---|
1035 | int uITRON4_minit(size_t poolsz) {
|
---|
1036 | ER ercd;
|
---|
1037 | wolfssl_MPOOL.mplsz = poolsz;
|
---|
1038 | ercd = acre_mpl(&wolfssl_MPOOL);
|
---|
1039 | if (ercd > 0) {
|
---|
1040 | ID_wolfssl_MPOOL = ercd;
|
---|
1041 | return 0;
|
---|
1042 | } else {
|
---|
1043 | return -1;
|
---|
1044 | }
|
---|
1045 | }
|
---|
1046 |
|
---|
1047 | void *uITRON4_malloc(size_t sz) {
|
---|
1048 | ER ercd;
|
---|
1049 | void *p;
|
---|
1050 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
|
---|
1051 | if (ercd == E_OK) {
|
---|
1052 | return p;
|
---|
1053 | } else {
|
---|
1054 | return 0;
|
---|
1055 | }
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | void *uITRON4_realloc(void *p, size_t sz) {
|
---|
1059 | ER ercd;
|
---|
1060 | void *newp;
|
---|
1061 | if(p) {
|
---|
1062 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
|
---|
1063 | if (ercd == E_OK) {
|
---|
1064 | XMEMCPY(newp, p, sz);
|
---|
1065 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1066 | if (ercd == E_OK) {
|
---|
1067 | return newp;
|
---|
1068 | }
|
---|
1069 | }
|
---|
1070 | }
|
---|
1071 | return 0;
|
---|
1072 | }
|
---|
1073 |
|
---|
1074 | void uITRON4_free(void *p) {
|
---|
1075 | ER ercd;
|
---|
1076 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1077 | if (ercd == E_OK) {
|
---|
1078 | return;
|
---|
1079 | } else {
|
---|
1080 | return;
|
---|
1081 | }
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | #elif defined(WOLFSSL_uTKERNEL2)
|
---|
1085 |
|
---|
1086 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1087 | {
|
---|
1088 | int iReturn;
|
---|
1089 | m->sem.sematr = TA_TFIFO;
|
---|
1090 | m->sem.isemcnt = 1;
|
---|
1091 | m->sem.maxsem = 1;
|
---|
1092 |
|
---|
1093 | m->id = tk_cre_sem(&m->sem);
|
---|
1094 | if( m->id != NULL )
|
---|
1095 | iReturn = 0;
|
---|
1096 | else
|
---|
1097 | iReturn = BAD_MUTEX_E;
|
---|
1098 |
|
---|
1099 | return iReturn;
|
---|
1100 | }
|
---|
1101 |
|
---|
1102 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1103 | {
|
---|
1104 | tk_del_sem(m->id);
|
---|
1105 | return 0;
|
---|
1106 | }
|
---|
1107 |
|
---|
1108 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1109 | {
|
---|
1110 | tk_wai_sem(m->id, 1, TMO_FEVR);
|
---|
1111 | return 0;
|
---|
1112 | }
|
---|
1113 |
|
---|
1114 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1115 | {
|
---|
1116 | tk_sig_sem(m->id, 1);
|
---|
1117 | return 0;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | /**** uT-Kernel malloc/free ***/
|
---|
1121 | static ID ID_wolfssl_MPOOL = 0;
|
---|
1122 | static T_CMPL wolfssl_MPOOL = {
|
---|
1123 | NULL, /* Extended information */
|
---|
1124 | TA_TFIFO, /* Memory pool attribute */
|
---|
1125 | 0, /* Size of whole memory pool (byte) */
|
---|
1126 | "wolfSSL" /* Object name (max 8-char) */
|
---|
1127 | };
|
---|
1128 |
|
---|
1129 | int uTKernel_init_mpool(unsigned int sz) {
|
---|
1130 | ER ercd;
|
---|
1131 | wolfssl_MPOOL.mplsz = sz;
|
---|
1132 | ercd = tk_cre_mpl(&wolfssl_MPOOL);
|
---|
1133 | if (ercd > 0) {
|
---|
1134 | ID_wolfssl_MPOOL = ercd;
|
---|
1135 | return 0;
|
---|
1136 | } else {
|
---|
1137 | return (int)ercd;
|
---|
1138 | }
|
---|
1139 | }
|
---|
1140 |
|
---|
1141 | void *uTKernel_malloc(unsigned int sz) {
|
---|
1142 | ER ercd;
|
---|
1143 | void *p;
|
---|
1144 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
|
---|
1145 | if (ercd == E_OK) {
|
---|
1146 | return p;
|
---|
1147 | } else {
|
---|
1148 | return 0;
|
---|
1149 | }
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | void *uTKernel_realloc(void *p, unsigned int sz) {
|
---|
1153 | ER ercd;
|
---|
1154 | void *newp;
|
---|
1155 | if (p) {
|
---|
1156 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
|
---|
1157 | if (ercd == E_OK) {
|
---|
1158 | XMEMCPY(newp, p, sz);
|
---|
1159 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1160 | if (ercd == E_OK) {
|
---|
1161 | return newp;
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 | return 0;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | void uTKernel_free(void *p) {
|
---|
1169 | ER ercd;
|
---|
1170 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
|
---|
1171 | if (ercd == E_OK) {
|
---|
1172 | return;
|
---|
1173 | } else {
|
---|
1174 | return;
|
---|
1175 | }
|
---|
1176 | }
|
---|
1177 |
|
---|
1178 | #elif defined (WOLFSSL_FROSTED)
|
---|
1179 |
|
---|
1180 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1181 | {
|
---|
1182 | *m = mutex_init();
|
---|
1183 | if (*m)
|
---|
1184 | return 0;
|
---|
1185 | else
|
---|
1186 | return -1;
|
---|
1187 | }
|
---|
1188 |
|
---|
1189 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1190 | {
|
---|
1191 | mutex_destroy(*m);
|
---|
1192 | return(0);
|
---|
1193 | }
|
---|
1194 |
|
---|
1195 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1196 | {
|
---|
1197 | mutex_lock(*m);
|
---|
1198 | return 0;
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1202 | {
|
---|
1203 | mutex_unlock(*m);
|
---|
1204 | return 0;
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 | #elif defined(WOLFSSL_CMSIS_RTOS)
|
---|
1208 |
|
---|
1209 | #define CMSIS_NMUTEX 10
|
---|
1210 | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
|
---|
1211 | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
|
---|
1212 | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
|
---|
1213 | osMutexDef(wolfSSL_mt9);
|
---|
1214 |
|
---|
1215 | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
|
---|
1216 | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
|
---|
1217 | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
|
---|
1218 | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
|
---|
1219 |
|
---|
1220 | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
|
---|
1221 |
|
---|
1222 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1223 | {
|
---|
1224 | int i;
|
---|
1225 | for (i=0; i<CMSIS_NMUTEX; i++) {
|
---|
1226 | if(CMSIS_mutexID[i] == 0) {
|
---|
1227 | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
|
---|
1228 | (*m) = CMSIS_mutexID[i];
|
---|
1229 | return 0;
|
---|
1230 | }
|
---|
1231 | }
|
---|
1232 | return -1;
|
---|
1233 | }
|
---|
1234 |
|
---|
1235 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1236 | {
|
---|
1237 | int i;
|
---|
1238 | osMutexDelete (*m);
|
---|
1239 | for (i=0; i<CMSIS_NMUTEX; i++) {
|
---|
1240 | if(CMSIS_mutexID[i] == (*m)) {
|
---|
1241 | CMSIS_mutexID[i] = 0;
|
---|
1242 | return(0);
|
---|
1243 | }
|
---|
1244 | }
|
---|
1245 | return(-1);
|
---|
1246 | }
|
---|
1247 |
|
---|
1248 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1249 | {
|
---|
1250 | osMutexWait(*m, osWaitForever);
|
---|
1251 | return(0);
|
---|
1252 | }
|
---|
1253 |
|
---|
1254 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1255 | {
|
---|
1256 | osMutexRelease (*m);
|
---|
1257 | return 0;
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | #elif defined(WOLFSSL_MDK_ARM)
|
---|
1261 |
|
---|
1262 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1263 | {
|
---|
1264 | os_mut_init (m);
|
---|
1265 | return 0;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1269 | {
|
---|
1270 | return(0);
|
---|
1271 | }
|
---|
1272 |
|
---|
1273 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1274 | {
|
---|
1275 | os_mut_wait (m, 0xffff);
|
---|
1276 | return(0);
|
---|
1277 | }
|
---|
1278 |
|
---|
1279 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1280 | {
|
---|
1281 | os_mut_release (m);
|
---|
1282 | return 0;
|
---|
1283 | }
|
---|
1284 |
|
---|
1285 | #elif defined(INTIME_RTOS)
|
---|
1286 |
|
---|
1287 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1288 | {
|
---|
1289 | int ret = 0;
|
---|
1290 |
|
---|
1291 | if (m == NULL)
|
---|
1292 | return BAD_FUNC_ARG;
|
---|
1293 |
|
---|
1294 | *m = CreateRtSemaphore(
|
---|
1295 | 1, /* initial unit count */
|
---|
1296 | 1, /* maximum unit count */
|
---|
1297 | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
|
---|
1298 | );
|
---|
1299 | if (*m == BAD_RTHANDLE) {
|
---|
1300 | ret = GetLastRtError();
|
---|
1301 | if (ret != E_OK)
|
---|
1302 | ret = BAD_MUTEX_E;
|
---|
1303 | }
|
---|
1304 | return ret;
|
---|
1305 | }
|
---|
1306 |
|
---|
1307 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1308 | {
|
---|
1309 | int ret = 0;
|
---|
1310 | BOOLEAN del;
|
---|
1311 |
|
---|
1312 | if (m == NULL)
|
---|
1313 | return BAD_FUNC_ARG;
|
---|
1314 |
|
---|
1315 | del = DeleteRtSemaphore(
|
---|
1316 | *m /* handle for RT semaphore */
|
---|
1317 | );
|
---|
1318 | if (del != TRUE)
|
---|
1319 | ret = BAD_MUTEX_E;
|
---|
1320 |
|
---|
1321 | return ret;
|
---|
1322 | }
|
---|
1323 |
|
---|
1324 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1325 | {
|
---|
1326 | int ret = 0;
|
---|
1327 | DWORD lck;
|
---|
1328 |
|
---|
1329 | if (m == NULL)
|
---|
1330 | return BAD_FUNC_ARG;
|
---|
1331 |
|
---|
1332 | lck = WaitForRtSemaphore(
|
---|
1333 | *m, /* handle for RT semaphore */
|
---|
1334 | 1, /* number of units to wait for */
|
---|
1335 | WAIT_FOREVER /* number of milliseconds to wait for units */
|
---|
1336 | );
|
---|
1337 | if (lck == WAIT_FAILED) {
|
---|
1338 | ret = GetLastRtError();
|
---|
1339 | if (ret != E_OK)
|
---|
1340 | ret = BAD_MUTEX_E;
|
---|
1341 | }
|
---|
1342 | return ret;
|
---|
1343 | }
|
---|
1344 |
|
---|
1345 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1346 | {
|
---|
1347 | int ret = 0;
|
---|
1348 | BOOLEAN rel;
|
---|
1349 |
|
---|
1350 | if (m == NULL)
|
---|
1351 | return BAD_FUNC_ARG;
|
---|
1352 |
|
---|
1353 | rel = ReleaseRtSemaphore(
|
---|
1354 | *m, /* handle for RT semaphore */
|
---|
1355 | 1 /* number of units to release to semaphore */
|
---|
1356 | );
|
---|
1357 | if (rel != TRUE)
|
---|
1358 | ret = BAD_MUTEX_E;
|
---|
1359 |
|
---|
1360 | return ret;
|
---|
1361 | }
|
---|
1362 |
|
---|
1363 | #elif defined(WOLFSSL_NUCLEUS_1_2)
|
---|
1364 |
|
---|
1365 | int wc_InitMutex(wolfSSL_Mutex* m)
|
---|
1366 | {
|
---|
1367 | /* Call the Nucleus function to create the semaphore */
|
---|
1368 | if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
|
---|
1369 | NU_PRIORITY) == NU_SUCCESS) {
|
---|
1370 | return 0;
|
---|
1371 | }
|
---|
1372 |
|
---|
1373 | return BAD_MUTEX_E;
|
---|
1374 | }
|
---|
1375 |
|
---|
1376 | int wc_FreeMutex(wolfSSL_Mutex* m)
|
---|
1377 | {
|
---|
1378 | if (NU_Delete_Semaphore(m) == NU_SUCCESS)
|
---|
1379 | return 0;
|
---|
1380 |
|
---|
1381 | return BAD_MUTEX_E;
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | int wc_LockMutex(wolfSSL_Mutex* m)
|
---|
1385 | {
|
---|
1386 | /* passing suspend task option */
|
---|
1387 | if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
|
---|
1388 | return 0;
|
---|
1389 |
|
---|
1390 | return BAD_MUTEX_E;
|
---|
1391 | }
|
---|
1392 |
|
---|
1393 | int wc_UnLockMutex(wolfSSL_Mutex* m)
|
---|
1394 | {
|
---|
1395 | if (NU_Release_Semaphore(m) == NU_SUCCESS)
|
---|
1396 | return 0;
|
---|
1397 |
|
---|
1398 | return BAD_MUTEX_E;
|
---|
1399 | }
|
---|
1400 |
|
---|
1401 | #else
|
---|
1402 | #warning No mutex handling defined
|
---|
1403 |
|
---|
1404 | #endif
|
---|
1405 |
|
---|
1406 | #ifndef NO_ASN_TIME
|
---|
1407 | #if defined(_WIN32_WCE)
|
---|
1408 | time_t windows_time(time_t* timer)
|
---|
1409 | {
|
---|
1410 | SYSTEMTIME sysTime;
|
---|
1411 | FILETIME fTime;
|
---|
1412 | ULARGE_INTEGER intTime;
|
---|
1413 | time_t localTime;
|
---|
1414 |
|
---|
1415 | if (timer == NULL)
|
---|
1416 | timer = &localTime;
|
---|
1417 |
|
---|
1418 | GetSystemTime(&sysTime);
|
---|
1419 | SystemTimeToFileTime(&sysTime, &fTime);
|
---|
1420 |
|
---|
1421 | XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
|
---|
1422 | /* subtract EPOCH */
|
---|
1423 | intTime.QuadPart -= 0x19db1ded53e8000;
|
---|
1424 | /* to secs */
|
---|
1425 | intTime.QuadPart /= 10000000;
|
---|
1426 | *timer = (time_t)intTime.QuadPart;
|
---|
1427 |
|
---|
1428 | return *timer;
|
---|
1429 | }
|
---|
1430 | #endif /* _WIN32_WCE */
|
---|
1431 |
|
---|
1432 | #if defined(WOLFSSL_APACHE_MYNEWT)
|
---|
1433 | #include "os/os_time.h"
|
---|
1434 |
|
---|
1435 | time_t mynewt_time(time_t* timer)
|
---|
1436 | {
|
---|
1437 | time_t now;
|
---|
1438 | struct os_timeval tv;
|
---|
1439 | os_gettimeofday(&tv, NULL);
|
---|
1440 | now = (time_t)tv.tv_sec;
|
---|
1441 | if(timer != NULL) {
|
---|
1442 | *timer = now;
|
---|
1443 | }
|
---|
1444 | return now;
|
---|
1445 | }
|
---|
1446 | #endif /* WOLFSSL_APACHE_MYNEWT */
|
---|
1447 |
|
---|
1448 | #if defined(WOLFSSL_GMTIME)
|
---|
1449 | struct tm* gmtime(const time_t* timer)
|
---|
1450 | {
|
---|
1451 | #define YEAR0 1900
|
---|
1452 | #define EPOCH_YEAR 1970
|
---|
1453 | #define SECS_DAY (24L * 60L * 60L)
|
---|
1454 | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
|
---|
1455 | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
|
---|
1456 |
|
---|
1457 | static const int _ytab[2][12] =
|
---|
1458 | {
|
---|
1459 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
|
---|
1460 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
---|
1461 | };
|
---|
1462 |
|
---|
1463 | static struct tm st_time;
|
---|
1464 | struct tm* ret = &st_time;
|
---|
1465 | time_t secs = *timer;
|
---|
1466 | unsigned long dayclock, dayno;
|
---|
1467 | int year = EPOCH_YEAR;
|
---|
1468 |
|
---|
1469 | dayclock = (unsigned long)secs % SECS_DAY;
|
---|
1470 | dayno = (unsigned long)secs / SECS_DAY;
|
---|
1471 |
|
---|
1472 | ret->tm_sec = (int) dayclock % 60;
|
---|
1473 | ret->tm_min = (int)(dayclock % 3600) / 60;
|
---|
1474 | ret->tm_hour = (int) dayclock / 3600;
|
---|
1475 | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */
|
---|
1476 |
|
---|
1477 | while(dayno >= (unsigned long)YEARSIZE(year)) {
|
---|
1478 | dayno -= YEARSIZE(year);
|
---|
1479 | year++;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 | ret->tm_year = year - YEAR0;
|
---|
1483 | ret->tm_yday = (int)dayno;
|
---|
1484 | ret->tm_mon = 0;
|
---|
1485 |
|
---|
1486 | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
|
---|
1487 | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
|
---|
1488 | ret->tm_mon++;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | ret->tm_mday = (int)++dayno;
|
---|
1492 | ret->tm_isdst = 0;
|
---|
1493 |
|
---|
1494 | return ret;
|
---|
1495 | }
|
---|
1496 | #endif /* WOLFSSL_GMTIME */
|
---|
1497 |
|
---|
1498 |
|
---|
1499 | #if defined(HAVE_RTP_SYS)
|
---|
1500 | #define YEAR0 1900
|
---|
1501 |
|
---|
1502 | struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */
|
---|
1503 | {
|
---|
1504 | static struct tm st_time;
|
---|
1505 | struct tm* ret = &st_time;
|
---|
1506 |
|
---|
1507 | DC_RTC_CALENDAR cal;
|
---|
1508 | dc_rtc_time_get(&cal, TRUE);
|
---|
1509 |
|
---|
1510 | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */
|
---|
1511 | ret->tm_mon = cal.month - 1; /* gm starts at 0 */
|
---|
1512 | ret->tm_mday = cal.day;
|
---|
1513 | ret->tm_hour = cal.hour;
|
---|
1514 | ret->tm_min = cal.minute;
|
---|
1515 | ret->tm_sec = cal.second;
|
---|
1516 |
|
---|
1517 | return ret;
|
---|
1518 | }
|
---|
1519 |
|
---|
1520 | #endif /* HAVE_RTP_SYS */
|
---|
1521 |
|
---|
1522 |
|
---|
1523 | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
|
---|
1524 |
|
---|
1525 | /*
|
---|
1526 | * time() is just a stub in Microchip libraries. We need our own
|
---|
1527 | * implementation. Use SNTP client to get seconds since epoch.
|
---|
1528 | */
|
---|
1529 | time_t pic32_time(time_t* timer)
|
---|
1530 | {
|
---|
1531 | #ifdef MICROCHIP_TCPIP_V5
|
---|
1532 | DWORD sec = 0;
|
---|
1533 | #else
|
---|
1534 | uint32_t sec = 0;
|
---|
1535 | #endif
|
---|
1536 | time_t localTime;
|
---|
1537 |
|
---|
1538 | if (timer == NULL)
|
---|
1539 | timer = &localTime;
|
---|
1540 |
|
---|
1541 | #ifdef MICROCHIP_MPLAB_HARMONY
|
---|
1542 | sec = TCPIP_SNTP_UTCSecondsGet();
|
---|
1543 | #else
|
---|
1544 | sec = SNTPGetUTCSeconds();
|
---|
1545 | #endif
|
---|
1546 | *timer = (time_t) sec;
|
---|
1547 |
|
---|
1548 | return *timer;
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
|
---|
1552 |
|
---|
1553 | #if defined(MICRIUM)
|
---|
1554 |
|
---|
1555 | time_t micrium_time(time_t* timer)
|
---|
1556 | {
|
---|
1557 | CLK_TS_SEC sec;
|
---|
1558 |
|
---|
1559 | Clk_GetTS_Unix(&sec);
|
---|
1560 |
|
---|
1561 | if (timer != NULL)
|
---|
1562 | *timer = sec;
|
---|
1563 |
|
---|
1564 | return (time_t) sec;
|
---|
1565 | }
|
---|
1566 |
|
---|
1567 | #endif /* MICRIUM */
|
---|
1568 |
|
---|
1569 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
|
---|
1570 |
|
---|
1571 | time_t mqx_time(time_t* timer)
|
---|
1572 | {
|
---|
1573 | time_t localTime;
|
---|
1574 | TIME_STRUCT time_s;
|
---|
1575 |
|
---|
1576 | if (timer == NULL)
|
---|
1577 | timer = &localTime;
|
---|
1578 |
|
---|
1579 | _time_get(&time_s);
|
---|
1580 | *timer = (time_t) time_s.SECONDS;
|
---|
1581 |
|
---|
1582 | return *timer;
|
---|
1583 | }
|
---|
1584 |
|
---|
1585 | #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | #if defined(WOLFSSL_TIRTOS)
|
---|
1589 |
|
---|
1590 | time_t XTIME(time_t * timer)
|
---|
1591 | {
|
---|
1592 | time_t sec = 0;
|
---|
1593 |
|
---|
1594 | sec = (time_t) Seconds_get();
|
---|
1595 |
|
---|
1596 | if (timer != NULL)
|
---|
1597 | *timer = sec;
|
---|
1598 |
|
---|
1599 | return sec;
|
---|
1600 | }
|
---|
1601 |
|
---|
1602 | #endif /* WOLFSSL_TIRTOS */
|
---|
1603 |
|
---|
1604 | #if defined(WOLFSSL_XILINX)
|
---|
1605 | #include "xrtcpsu.h"
|
---|
1606 |
|
---|
1607 | time_t XTIME(time_t * timer)
|
---|
1608 | {
|
---|
1609 | time_t sec = 0;
|
---|
1610 | XRtcPsu_Config* con;
|
---|
1611 | XRtcPsu rtc;
|
---|
1612 |
|
---|
1613 | con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
|
---|
1614 | if (con != NULL) {
|
---|
1615 | if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
|
---|
1616 | sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
|
---|
1617 | }
|
---|
1618 | else {
|
---|
1619 | WOLFSSL_MSG("Unable to initialize RTC");
|
---|
1620 | }
|
---|
1621 | }
|
---|
1622 |
|
---|
1623 | if (timer != NULL)
|
---|
1624 | *timer = sec;
|
---|
1625 |
|
---|
1626 | return sec;
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | #endif /* WOLFSSL_XILINX */
|
---|
1630 | #endif /* !NO_ASN_TIME */
|
---|
1631 |
|
---|
1632 | #ifndef WOLFSSL_LEANPSK
|
---|
1633 | char* mystrnstr(const char* s1, const char* s2, unsigned int n)
|
---|
1634 | {
|
---|
1635 | unsigned int s2_len = (unsigned int)XSTRLEN(s2);
|
---|
1636 |
|
---|
1637 | if (s2_len == 0)
|
---|
1638 | return (char*)s1;
|
---|
1639 |
|
---|
1640 | while (n >= s2_len && s1[0]) {
|
---|
1641 | if (s1[0] == s2[0])
|
---|
1642 | if (XMEMCMP(s1, s2, s2_len) == 0)
|
---|
1643 | return (char*)s1;
|
---|
1644 | s1++;
|
---|
1645 | n--;
|
---|
1646 | }
|
---|
1647 |
|
---|
1648 | return NULL;
|
---|
1649 | }
|
---|
1650 | #endif
|
---|
1651 |
|
---|
1652 | /* custom memory wrappers */
|
---|
1653 | #ifdef WOLFSSL_NUCLEUS_1_2
|
---|
1654 |
|
---|
1655 | /* system memory pool */
|
---|
1656 | extern NU_MEMORY_POOL System_Memory;
|
---|
1657 |
|
---|
1658 | void* nucleus_malloc(unsigned long size, void* heap, int type)
|
---|
1659 | {
|
---|
1660 | STATUS status;
|
---|
1661 | void* stack_ptr;
|
---|
1662 |
|
---|
1663 | status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
|
---|
1664 | NU_NO_SUSPEND);
|
---|
1665 | if (status == NU_SUCCESS) {
|
---|
1666 | return 0;
|
---|
1667 | } else {
|
---|
1668 | return stack_ptr;
|
---|
1669 | }
|
---|
1670 | }
|
---|
1671 |
|
---|
1672 | void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
|
---|
1673 | {
|
---|
1674 | STATUS status;
|
---|
1675 | DM_HEADER* old_header;
|
---|
1676 | word32 old_size, copy_size;
|
---|
1677 | void* new_mem;
|
---|
1678 |
|
---|
1679 | /* if ptr is NULL, behave like malloc */
|
---|
1680 | new_mem = nucleus_malloc(size, NULL, 0);
|
---|
1681 | if (new_mem == 0 || ptr == 0) {
|
---|
1682 | return new_mem;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 | /* calculate old memory block size */
|
---|
1686 | /* mem pointers stored in block headers (ref dm_defs.h) */
|
---|
1687 | old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
|
---|
1688 | old_size = (byte*)old_header->dm_next_memory - (byte*)ptr;
|
---|
1689 |
|
---|
1690 | /* copy old to new */
|
---|
1691 | if (old_size < size) {
|
---|
1692 | copy_size = old_size;
|
---|
1693 | } else {
|
---|
1694 | copy_size = size;
|
---|
1695 | }
|
---|
1696 | XMEMCPY(new_mem, ptr, copy_size);
|
---|
1697 |
|
---|
1698 | /* free old */
|
---|
1699 | nucleus_free(ptr, NULL, 0);
|
---|
1700 |
|
---|
1701 | return new_mem;
|
---|
1702 | }
|
---|
1703 |
|
---|
1704 | void nucleus_free(void* ptr, void* heap, int type)
|
---|
1705 | {
|
---|
1706 | if (ptr != NULL)
|
---|
1707 | NU_Deallocate_Memory(ptr);
|
---|
1708 | }
|
---|
1709 |
|
---|
1710 | #endif /* WOLFSSL_NUCLEUS_1_2 */
|
---|
1711 |
|
---|
1712 | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
|
---|
1713 | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
|
---|
1714 | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
|
---|
1715 | #endif
|
---|