source: asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/wolfcrypt/src/wc_port.c@ 352

Last change on this file since 352 was 352, checked in by coas-nagasima, 6 years ago

arm向けASP3版ECNLを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-csrc;charset=UTF-8
File size: 26.2 KB
Line 
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#if defined(FREESCALE_LTC_TFM)
46 #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
47#endif
48
49#ifdef WOLFSSL_ATMEL
50 #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
51#endif
52
53#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
54 #include <wolfssl/openssl/evp.h>
55#endif
56
57#if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
58 #include <wolfssl/wolfcrypt/memory.h>
59 #include <wolfssl/wolfcrypt/mem_track.h>
60#endif
61
62#ifdef _MSC_VER
63 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
64 #pragma warning(disable: 4996)
65#endif
66
67/* prevent multiple mutex initializations */
68static volatile int initRefCount = 0;
69
70/* Used to initialize state for wolfcrypt
71 return 0 on success
72 */
73int wolfCrypt_Init(void)
74{
75 int ret = 0;
76
77 if (initRefCount == 0) {
78 WOLFSSL_ENTER("wolfCrypt_Init");
79
80 #ifdef WOLFSSL_ASYNC_CRYPT
81 ret = wolfAsync_HardwareStart();
82 if (ret != 0) {
83 WOLFSSL_MSG("Async hardware start failed");
84 /* don't return failure, allow operation to continue */
85 }
86 #endif
87
88 #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
89 ret = InitMemoryTracker();
90 if (ret != 0) {
91 WOLFSSL_MSG("InitMemoryTracker failed");
92 return ret;
93 }
94 #endif
95
96 #if WOLFSSL_CRYPT_HW_MUTEX
97 /* If crypto hardware mutex protection is enabled, then initialize it */
98 ret = wolfSSL_CryptHwMutexInit();
99 if (ret != 0) {
100 WOLFSSL_MSG("Hw crypt mutex init failed");
101 return ret;
102 }
103 #endif
104
105 /* if defined have fast RSA then initialize Intel IPP */
106 #ifdef HAVE_FAST_RSA
107 WOLFSSL_MSG("Attempting to use optimized IPP Library");
108 if ((ret = ippInit()) != ippStsNoErr) {
109 /* possible to get a CPU feature support status on optimized IPP
110 library but still use default library and see competitive speeds */
111 WOLFSSL_MSG("Warning when trying to set up optimization");
112 WOLFSSL_MSG(ippGetStatusString(ret));
113 WOLFSSL_MSG("Using default fast IPP library");
114 ret = 0;
115 (void)ret; /* suppress not read warning */
116 }
117 #endif
118
119 #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
120 ret = ksdk_port_init();
121 if (ret != 0) {
122 WOLFSSL_MSG("KSDK port init failed");
123 return ret;
124 }
125 #endif
126
127 #ifdef WOLFSSL_ATMEL
128 atmel_init();
129 #endif
130
131 #ifdef WOLFSSL_ARMASM
132 WOLFSSL_MSG("Using ARM hardware acceleration");
133 #endif
134
135 #if !defined(WOLFCRYPT_ONLY) && \
136 ( defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) )
137 wolfSSL_EVP_init();
138 #endif
139
140 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
141 if ((ret = wc_LoggingInit()) != 0) {
142 WOLFSSL_MSG("Error creating logging mutex");
143 return ret;
144 }
145 #endif
146
147#ifdef HAVE_ECC
148 #ifdef ECC_CACHE_CURVE
149 if ((ret = wc_ecc_curve_cache_init()) != 0) {
150 WOLFSSL_MSG("Error creating curve cache");
151 return ret;
152 }
153 #endif
154#endif
155
156 initRefCount = 1;
157 }
158
159 return ret;
160}
161
162
163/* return success value is the same as wolfCrypt_Init */
164int wolfCrypt_Cleanup(void)
165{
166 int ret = 0;
167
168 if (initRefCount == 1) {
169 WOLFSSL_ENTER("wolfCrypt_Cleanup");
170
171#ifdef HAVE_ECC
172 #ifdef FP_ECC
173 wc_ecc_fp_free();
174 #endif
175 #ifdef ECC_CACHE_CURVE
176 wc_ecc_curve_cache_free();
177 #endif
178#endif /* HAVE_ECC */
179
180 #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
181 ret = wc_LoggingCleanup();
182 #endif
183
184 #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
185 ShowMemoryTracker();
186 #endif
187
188 #ifdef WOLFSSL_ASYNC_CRYPT
189 wolfAsync_HardwareStop();
190 #endif
191
192 initRefCount = 0; /* allow re-init */
193 }
194
195 return ret;
196}
197
198#if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
199
200/* File Handling Helpers */
201/* returns 0 if file found, -1 if no files or negative error */
202int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
203{
204 int ret = -1; /* default to no files found */
205
206 if (name)
207 *name = NULL;
208
209 if (ctx == NULL || path == NULL) {
210 return BAD_FUNC_ARG;
211 }
212
213 XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
214
215#ifdef USE_WINDOWS_API
216 XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4);
217 XSTRNCAT(ctx->name, "\\*", 3);
218
219 ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
220 if (ctx->hFind == INVALID_HANDLE_VALUE) {
221 WOLFSSL_MSG("FindFirstFile for path verify locations failed");
222 return BAD_PATH_ERROR;
223 }
224
225 do {
226 if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
227 XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
228 XSTRNCAT(ctx->name, "\\", 2);
229 XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
230 if (name)
231 *name = ctx->name;
232 return 0;
233 }
234 } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
235#else
236 ctx->dir = opendir(path);
237 if (ctx->dir == NULL) {
238 WOLFSSL_MSG("opendir path verify locations failed");
239 return BAD_PATH_ERROR;
240 }
241
242 while ((ctx->entry = readdir(ctx->dir)) != NULL) {
243 XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
244 XSTRNCAT(ctx->name, "/", 1);
245 XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
246
247 if (stat(ctx->name, &ctx->s) != 0) {
248 WOLFSSL_MSG("stat on name failed");
249 ret = BAD_PATH_ERROR;
250 break;
251 } else if (ctx->s.st_mode & S_IFREG) {
252 if (name)
253 *name = ctx->name;
254 return 0;
255 }
256 }
257#endif
258 wc_ReadDirClose(ctx);
259
260 return ret;
261}
262
263/* returns 0 if file found, -1 if no more files */
264int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
265{
266 int ret = -1; /* default to no file found */
267
268 if (name)
269 *name = NULL;
270
271 if (ctx == NULL || path == NULL) {
272 return BAD_FUNC_ARG;
273 }
274
275 XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
276
277#ifdef USE_WINDOWS_API
278 while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
279 if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
280 XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
281 XSTRNCAT(ctx->name, "\\", 2);
282 XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
283 if (name)
284 *name = ctx->name;
285 return 0;
286 }
287 }
288#else
289 while ((ctx->entry = readdir(ctx->dir)) != NULL) {
290 XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
291 XSTRNCAT(ctx->name, "/", 1);
292 XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
293
294 if (stat(ctx->name, &ctx->s) != 0) {
295 WOLFSSL_MSG("stat on name failed");
296 ret = BAD_PATH_ERROR;
297 break;
298 } else if (ctx->s.st_mode & S_IFREG) {
299 if (name)
300 *name = ctx->name;
301 return 0;
302 }
303 }
304#endif
305
306 wc_ReadDirClose(ctx);
307
308 return ret;
309}
310
311void wc_ReadDirClose(ReadDirCtx* ctx)
312{
313 if (ctx == NULL) {
314 return;
315 }
316
317#ifdef USE_WINDOWS_API
318 if (ctx->hFind != INVALID_HANDLE_VALUE) {
319 FindClose(ctx->hFind);
320 ctx->hFind = INVALID_HANDLE_VALUE;
321 }
322#else
323 if (ctx->dir) {
324 closedir(ctx->dir);
325 ctx->dir = NULL;
326 }
327#endif
328}
329
330#endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
331
332
333wolfSSL_Mutex* wc_InitAndAllocMutex(void)
334{
335 wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
336 DYNAMIC_TYPE_MUTEX);
337 if (m != NULL) {
338 if (wc_InitMutex(m) != 0) {
339 WOLFSSL_MSG("Init Mutex failed");
340 XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
341 m = NULL;
342 }
343 }
344 else {
345 WOLFSSL_MSG("Memory error with Mutex allocation");
346 }
347
348 return m;
349}
350
351#ifdef USE_WOLF_STRTOK
352/* String token (delim) search. If str is null use nextp. */
353char* wc_strtok(char *str, const char *delim, char **nextp)
354{
355 char* ret;
356 int i, j;
357
358 /* Use next if str is NULL */
359 if (str == NULL && nextp)
360 str = *nextp;
361
362 /* verify str input */
363 if (str == NULL || *str == '\0')
364 return NULL;
365
366 /* match on entire delim */
367 for (i = 0; str[i]; i++) {
368 for (j = 0; delim[j]; j++) {
369 if (delim[j] == str[i])
370 break;
371 }
372 if (!delim[j])
373 break;
374 }
375 str += i;
376 /* if end of string, not found so return NULL */
377 if (*str == '\0')
378 return NULL;
379
380 ret = str;
381
382 /* match on first delim */
383 for (i = 0; str[i]; i++) {
384 for (j = 0; delim[j]; j++) {
385 if (delim[j] == str[i])
386 break;
387 }
388 if (delim[j] == str[i])
389 break;
390 }
391 str += i;
392
393 /* null terminate found string */
394 if (*str)
395 *str++ = '\0';
396
397 /* return pointer to next */
398 if (nextp)
399 *nextp = str;
400
401 return ret;
402}
403#endif /* USE_WOLF_STRTOK */
404
405#if WOLFSSL_CRYPT_HW_MUTEX
406/* Mutex for protection of cryptography hardware */
407static wolfSSL_Mutex wcCryptHwMutex;
408static int wcCryptHwMutexInit = 0;
409
410int wolfSSL_CryptHwMutexInit(void) {
411 int ret = 0;
412 if(wcCryptHwMutexInit == 0) {
413 ret = wc_InitMutex(&wcCryptHwMutex);
414 if(ret == 0) {
415 wcCryptHwMutexInit = 1;
416 }
417 }
418 return ret;
419}
420
421int wolfSSL_CryptHwMutexLock(void) {
422 int ret = BAD_MUTEX_E;
423
424 /* Make sure HW Mutex has been initialized */
425 wolfSSL_CryptHwMutexInit();
426
427 if(wcCryptHwMutexInit) {
428 ret = wc_LockMutex(&wcCryptHwMutex);
429 }
430 return ret;
431}
432
433int wolfSSL_CryptHwMutexUnLock(void) {
434 int ret = BAD_MUTEX_E;
435
436 if(wcCryptHwMutexInit) {
437 ret = wc_UnLockMutex(&wcCryptHwMutex);
438 }
439 return ret;
440}
441#endif /* WOLFSSL_CRYPT_HW_MUTEX */
442
443
444/* ---------------------------------------------------------------------------*/
445/* Mutex Ports */
446/* ---------------------------------------------------------------------------*/
447#ifdef SINGLE_THREADED
448
449 int wc_InitMutex(wolfSSL_Mutex* m)
450 {
451 (void)m;
452 return 0;
453 }
454
455 int wc_FreeMutex(wolfSSL_Mutex *m)
456 {
457 (void)m;
458 return 0;
459 }
460
461
462 int wc_LockMutex(wolfSSL_Mutex *m)
463 {
464 (void)m;
465 return 0;
466 }
467
468
469 int wc_UnLockMutex(wolfSSL_Mutex *m)
470 {
471 (void)m;
472 return 0;
473 }
474
475#elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
476 defined(FREESCALE_FREE_RTOS)
477
478 int wc_InitMutex(wolfSSL_Mutex* m)
479 {
480 int iReturn;
481
482 *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
483 if( *m != NULL )
484 iReturn = 0;
485 else
486 iReturn = BAD_MUTEX_E;
487
488 return iReturn;
489 }
490
491 int wc_FreeMutex(wolfSSL_Mutex* m)
492 {
493 vSemaphoreDelete( *m );
494 return 0;
495 }
496
497 int wc_LockMutex(wolfSSL_Mutex* m)
498 {
499 /* Assume an infinite block, or should there be zero block? */
500 xSemaphoreTake( *m, portMAX_DELAY );
501 return 0;
502 }
503
504 int wc_UnLockMutex(wolfSSL_Mutex* m)
505 {
506 xSemaphoreGive( *m );
507 return 0;
508 }
509
510#elif defined(WOLFSSL_SAFERTOS)
511
512 int wc_InitMutex(wolfSSL_Mutex* m)
513 {
514 vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
515 if (m->mutex == NULL)
516 return BAD_MUTEX_E;
517
518 return 0;
519 }
520
521 int wc_FreeMutex(wolfSSL_Mutex* m)
522 {
523 (void)m;
524 return 0;
525 }
526
527 int wc_LockMutex(wolfSSL_Mutex* m)
528 {
529 /* Assume an infinite block */
530 xSemaphoreTake(m->mutex, portMAX_DELAY);
531 return 0;
532 }
533
534 int wc_UnLockMutex(wolfSSL_Mutex* m)
535 {
536 xSemaphoreGive(m->mutex);
537 return 0;
538 }
539
540#elif defined(USE_WINDOWS_API)
541
542 int wc_InitMutex(wolfSSL_Mutex* m)
543 {
544 InitializeCriticalSection(m);
545 return 0;
546 }
547
548
549 int wc_FreeMutex(wolfSSL_Mutex* m)
550 {
551 DeleteCriticalSection(m);
552 return 0;
553 }
554
555
556 int wc_LockMutex(wolfSSL_Mutex* m)
557 {
558 EnterCriticalSection(m);
559 return 0;
560 }
561
562
563 int wc_UnLockMutex(wolfSSL_Mutex* m)
564 {
565 LeaveCriticalSection(m);
566 return 0;
567 }
568
569#elif defined(WOLFSSL_PTHREADS)
570
571 int wc_InitMutex(wolfSSL_Mutex* m)
572 {
573 if (pthread_mutex_init(m, 0) == 0)
574 return 0;
575 else
576 return BAD_MUTEX_E;
577 }
578
579
580 int wc_FreeMutex(wolfSSL_Mutex* m)
581 {
582 if (pthread_mutex_destroy(m) == 0)
583 return 0;
584 else
585 return BAD_MUTEX_E;
586 }
587
588
589 int wc_LockMutex(wolfSSL_Mutex* m)
590 {
591 if (pthread_mutex_lock(m) == 0)
592 return 0;
593 else
594 return BAD_MUTEX_E;
595 }
596
597
598 int wc_UnLockMutex(wolfSSL_Mutex* m)
599 {
600 if (pthread_mutex_unlock(m) == 0)
601 return 0;
602 else
603 return BAD_MUTEX_E;
604 }
605
606#elif defined(THREADX)
607
608 int wc_InitMutex(wolfSSL_Mutex* m)
609 {
610 if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
611 return 0;
612 else
613 return BAD_MUTEX_E;
614 }
615
616
617 int wc_FreeMutex(wolfSSL_Mutex* m)
618 {
619 if (tx_mutex_delete(m) == 0)
620 return 0;
621 else
622 return BAD_MUTEX_E;
623 }
624
625
626 int wc_LockMutex(wolfSSL_Mutex* m)
627 {
628 if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
629 return 0;
630 else
631 return BAD_MUTEX_E;
632 }
633
634 int wc_UnLockMutex(wolfSSL_Mutex* m)
635 {
636 if (tx_mutex_put(m) == 0)
637 return 0;
638 else
639 return BAD_MUTEX_E;
640 }
641
642#elif defined(MICRIUM)
643
644 int wc_InitMutex(wolfSSL_Mutex* m)
645 {
646 OS_ERR err;
647
648 OSMutexCreate(m, "wolfSSL Mutex", &err);
649
650 if (err == OS_ERR_NONE)
651 return 0;
652 else
653 return BAD_MUTEX_E;
654 }
655
656 int wc_FreeMutex(wolfSSL_Mutex* m)
657 {
658 #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
659 OS_ERR err;
660
661 OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
662
663 if (err == OS_ERR_NONE)
664 return 0;
665 else
666 return BAD_MUTEX_E;
667 #else
668 return 0;
669 #endif
670 }
671
672 int wc_LockMutex(wolfSSL_Mutex* m)
673 {
674 OS_ERR err;
675
676 OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
677
678 if (err == OS_ERR_NONE)
679 return 0;
680 else
681 return BAD_MUTEX_E;
682 }
683
684 int wc_UnLockMutex(wolfSSL_Mutex* m)
685 {
686 OS_ERR err;
687
688 OSMutexPost(m, OS_OPT_POST_NONE, &err);
689
690 if (err == OS_ERR_NONE)
691 return 0;
692 else
693 return BAD_MUTEX_E;
694 }
695
696#elif defined(EBSNET)
697
698 int wc_InitMutex(wolfSSL_Mutex* m)
699 {
700 if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
701 return BAD_MUTEX_E;
702 else
703 return 0;
704 }
705
706 int wc_FreeMutex(wolfSSL_Mutex* m)
707 {
708 rtp_sig_mutex_free(*m);
709 return 0;
710 }
711
712 int wc_LockMutex(wolfSSL_Mutex* m)
713 {
714 if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
715 return 0;
716 else
717 return BAD_MUTEX_E;
718 }
719
720 int wc_UnLockMutex(wolfSSL_Mutex* m)
721 {
722 rtp_sig_mutex_release(*m);
723 return 0;
724 }
725
726#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
727
728 int wc_InitMutex(wolfSSL_Mutex* m)
729 {
730 if (_mutex_init(m, NULL) == MQX_EOK)
731 return 0;
732 else
733 return BAD_MUTEX_E;
734 }
735
736 int wc_FreeMutex(wolfSSL_Mutex* m)
737 {
738 if (_mutex_destroy(m) == MQX_EOK)
739 return 0;
740 else
741 return BAD_MUTEX_E;
742 }
743
744 int wc_LockMutex(wolfSSL_Mutex* m)
745 {
746 if (_mutex_lock(m) == MQX_EOK)
747 return 0;
748 else
749 return BAD_MUTEX_E;
750 }
751
752 int wc_UnLockMutex(wolfSSL_Mutex* m)
753 {
754 if (_mutex_unlock(m) == MQX_EOK)
755 return 0;
756 else
757 return BAD_MUTEX_E;
758 }
759
760#elif defined(WOLFSSL_TIRTOS)
761 #include <xdc/runtime/Error.h>
762
763 int wc_InitMutex(wolfSSL_Mutex* m)
764 {
765 Semaphore_Params params;
766 Error_Block eb;
767
768 Error_init(&eb);
769 Semaphore_Params_init(&params);
770 params.mode = Semaphore_Mode_BINARY;
771
772 *m = Semaphore_create(1, &params, &eb);
773 if (Error_check(&eb)) {
774 Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
775 NULL);
776 return BAD_MUTEX_E;
777 }
778 else
779 return 0;
780 }
781
782 int wc_FreeMutex(wolfSSL_Mutex* m)
783 {
784 Semaphore_delete(m);
785
786 return 0;
787 }
788
789 int wc_LockMutex(wolfSSL_Mutex* m)
790 {
791 Semaphore_pend(*m, BIOS_WAIT_FOREVER);
792
793 return 0;
794 }
795
796 int wc_UnLockMutex(wolfSSL_Mutex* m)
797 {
798 Semaphore_post(*m);
799
800 return 0;
801 }
802
803#elif defined(WOLFSSL_uITRON4)
804
805 int wc_InitMutex(wolfSSL_Mutex* m)
806 {
807 int iReturn;
808 m->sem.sematr = TA_TFIFO;
809 m->sem.isemcnt = 1;
810 m->sem.maxsem = 1;
811 m->sem.name = NULL;
812
813 m->id = acre_sem(&m->sem);
814 if( m->id != E_OK )
815 iReturn = 0;
816 else
817 iReturn = BAD_MUTEX_E;
818
819 return iReturn;
820 }
821
822 int wc_FreeMutex(wolfSSL_Mutex* m)
823 {
824 del_sem( m->id );
825 return 0;
826 }
827
828 int wc_LockMutex(wolfSSL_Mutex* m)
829 {
830 wai_sem(m->id);
831 return 0;
832 }
833
834 int wc_UnLockMutex(wolfSSL_Mutex* m)
835 {
836 sig_sem(m->id);
837 return 0;
838 }
839
840 /**** uITRON malloc/free ***/
841 static ID ID_wolfssl_MPOOL = 0;
842 static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
843
844 int uITRON4_minit(size_t poolsz) {
845 ER ercd;
846 wolfssl_MPOOL.mplsz = poolsz;
847 ercd = acre_mpl(&wolfssl_MPOOL);
848 if (ercd > 0) {
849 ID_wolfssl_MPOOL = ercd;
850 return 0;
851 } else {
852 return -1;
853 }
854 }
855
856 void *uITRON4_malloc(size_t sz) {
857 ER ercd;
858 void *p;
859 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
860 if (ercd == E_OK) {
861 return p;
862 } else {
863 return 0;
864 }
865 }
866
867 void *uITRON4_realloc(void *p, size_t sz) {
868 ER ercd;
869 void *newp;
870 if(p) {
871 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
872 if (ercd == E_OK) {
873 XMEMCPY(newp, p, sz);
874 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
875 if (ercd == E_OK) {
876 return newp;
877 }
878 }
879 }
880 return 0;
881 }
882
883 void uITRON4_free(void *p) {
884 ER ercd;
885 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
886 if (ercd == E_OK) {
887 return;
888 } else {
889 return;
890 }
891 }
892
893#elif defined(WOLFSSL_uTKERNEL2)
894
895 int wc_InitMutex(wolfSSL_Mutex* m)
896 {
897 int iReturn;
898 m->sem.sematr = TA_TFIFO;
899 m->sem.isemcnt = 1;
900 m->sem.maxsem = 1;
901
902 m->id = tk_cre_sem(&m->sem);
903 if( m->id != NULL )
904 iReturn = 0;
905 else
906 iReturn = BAD_MUTEX_E;
907
908 return iReturn;
909 }
910
911 int wc_FreeMutex(wolfSSL_Mutex* m)
912 {
913 tk_del_sem(m->id);
914 return 0;
915 }
916
917 int wc_LockMutex(wolfSSL_Mutex* m)
918 {
919 tk_wai_sem(m->id, 1, TMO_FEVR);
920 return 0;
921 }
922
923 int wc_UnLockMutex(wolfSSL_Mutex* m)
924 {
925 tk_sig_sem(m->id, 1);
926 return 0;
927 }
928
929 /**** uT-Kernel malloc/free ***/
930 static ID ID_wolfssl_MPOOL = 0;
931 static T_CMPL wolfssl_MPOOL = {
932 NULL, /* Extended information */
933 TA_TFIFO, /* Memory pool attribute */
934 0, /* Size of whole memory pool (byte) */
935 "wolfSSL" /* Object name (max 8-char) */
936 };
937
938 int uTKernel_init_mpool(unsigned int sz) {
939 ER ercd;
940 wolfssl_MPOOL.mplsz = sz;
941 ercd = tk_cre_mpl(&wolfssl_MPOOL);
942 if (ercd > 0) {
943 ID_wolfssl_MPOOL = ercd;
944 return 0;
945 } else {
946 return (int)ercd;
947 }
948 }
949
950 void *uTKernel_malloc(unsigned int sz) {
951 ER ercd;
952 void *p;
953 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
954 if (ercd == E_OK) {
955 return p;
956 } else {
957 return 0;
958 }
959 }
960
961 void *uTKernel_realloc(void *p, unsigned int sz) {
962 ER ercd;
963 void *newp;
964 if (p) {
965 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
966 if (ercd == E_OK) {
967 XMEMCPY(newp, p, sz);
968 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
969 if (ercd == E_OK) {
970 return newp;
971 }
972 }
973 }
974 return 0;
975 }
976
977 void uTKernel_free(void *p) {
978 ER ercd;
979 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
980 if (ercd == E_OK) {
981 return;
982 } else {
983 return;
984 }
985 }
986
987#elif defined (WOLFSSL_FROSTED)
988
989 int wc_InitMutex(wolfSSL_Mutex* m)
990 {
991 *m = mutex_init();
992 if (*m)
993 return 0;
994 else
995 return -1;
996 }
997
998 int wc_FreeMutex(wolfSSL_Mutex* m)
999 {
1000 mutex_destroy(*m);
1001 return(0);
1002 }
1003
1004 int wc_LockMutex(wolfSSL_Mutex* m)
1005 {
1006 mutex_lock(*m);
1007 return 0;
1008 }
1009
1010 int wc_UnLockMutex(wolfSSL_Mutex* m)
1011 {
1012 mutex_unlock(*m);
1013 return 0;
1014 }
1015
1016#elif defined(WOLFSSL_CMSIS_RTOS)
1017
1018 #define CMSIS_NMUTEX 10
1019 osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2);
1020 osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5);
1021 osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8);
1022 osMutexDef(wolfSSL_mt9);
1023
1024 static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
1025 osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
1026 osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
1027 osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) };
1028
1029 static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
1030
1031 int wc_InitMutex(wolfSSL_Mutex* m)
1032 {
1033 int i;
1034 for (i=0; i<CMSIS_NMUTEX; i++) {
1035 if(CMSIS_mutexID[i] == 0) {
1036 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
1037 (*m) = CMSIS_mutexID[i];
1038 return 0;
1039 }
1040 }
1041 return -1;
1042 }
1043
1044 int wc_FreeMutex(wolfSSL_Mutex* m)
1045 {
1046 int i;
1047 osMutexDelete (*m);
1048 for (i=0; i<CMSIS_NMUTEX; i++) {
1049 if(CMSIS_mutexID[i] == (*m)) {
1050 CMSIS_mutexID[i] = 0;
1051 return(0);
1052 }
1053 }
1054 return(-1);
1055 }
1056
1057 int wc_LockMutex(wolfSSL_Mutex* m)
1058 {
1059 osMutexWait(*m, osWaitForever);
1060 return(0);
1061 }
1062
1063 int wc_UnLockMutex(wolfSSL_Mutex* m)
1064 {
1065 osMutexRelease (*m);
1066 return 0;
1067 }
1068
1069#elif defined(WOLFSSL_MDK_ARM)
1070
1071 int wc_InitMutex(wolfSSL_Mutex* m)
1072 {
1073 os_mut_init (m);
1074 return 0;
1075 }
1076
1077 int wc_FreeMutex(wolfSSL_Mutex* m)
1078 {
1079 return(0);
1080 }
1081
1082 int wc_LockMutex(wolfSSL_Mutex* m)
1083 {
1084 os_mut_wait (m, 0xffff);
1085 return(0);
1086 }
1087
1088 int wc_UnLockMutex(wolfSSL_Mutex* m)
1089 {
1090 os_mut_release (m);
1091 return 0;
1092 }
1093
1094#elif defined(INTIME_RTOS)
1095
1096 int wc_InitMutex(wolfSSL_Mutex* m)
1097 {
1098 int ret = 0;
1099
1100 if (m == NULL)
1101 return BAD_FUNC_ARG;
1102
1103 *m = CreateRtSemaphore(
1104 1, /* initial unit count */
1105 1, /* maximum unit count */
1106 PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
1107 );
1108 if (*m == BAD_RTHANDLE) {
1109 ret = GetLastRtError();
1110 if (ret != E_OK)
1111 ret = BAD_MUTEX_E;
1112 }
1113 return ret;
1114 }
1115
1116 int wc_FreeMutex(wolfSSL_Mutex* m)
1117 {
1118 int ret = 0;
1119 BOOLEAN del;
1120
1121 if (m == NULL)
1122 return BAD_FUNC_ARG;
1123
1124 del = DeleteRtSemaphore(
1125 *m /* handle for RT semaphore */
1126 );
1127 if (del != TRUE)
1128 ret = BAD_MUTEX_E;
1129
1130 return ret;
1131 }
1132
1133 int wc_LockMutex(wolfSSL_Mutex* m)
1134 {
1135 int ret = 0;
1136 DWORD lck;
1137
1138 if (m == NULL)
1139 return BAD_FUNC_ARG;
1140
1141 lck = WaitForRtSemaphore(
1142 *m, /* handle for RT semaphore */
1143 1, /* number of units to wait for */
1144 WAIT_FOREVER /* number of milliseconds to wait for units */
1145 );
1146 if (lck == WAIT_FAILED) {
1147 ret = GetLastRtError();
1148 if (ret != E_OK)
1149 ret = BAD_MUTEX_E;
1150 }
1151 return ret;
1152 }
1153
1154 int wc_UnLockMutex(wolfSSL_Mutex* m)
1155 {
1156 int ret = 0;
1157 BOOLEAN rel;
1158
1159 if (m == NULL)
1160 return BAD_FUNC_ARG;
1161
1162 rel = ReleaseRtSemaphore(
1163 *m, /* handle for RT semaphore */
1164 1 /* number of units to release to semaphore */
1165 );
1166 if (rel != TRUE)
1167 ret = BAD_MUTEX_E;
1168
1169 return ret;
1170 }
1171
1172#else
1173 #warning No mutex handling defined
1174
1175#endif
1176
1177
1178#if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
1179 #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
1180 #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
1181#endif
Note: See TracBrowser for help on using the repository browser.