source: UsbWattMeter/trunk/wolfssl-3.7.0/wolfcrypt/src/wc_port.c@ 164

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

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 18.1 KB
RevLine 
[164]1/* port.c
2 *
3 * Copyright (C) 2006-2015 wolfSSL Inc.
4 *
5 * This file is part of wolfSSL. (formerly known as CyaSSL)
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-1301, USA
20 */
21
22#ifdef HAVE_CONFIG_H
23 #include <config.h>
24#endif
25
26#include <wolfssl/wolfcrypt/settings.h>
27#include <wolfssl/wolfcrypt/types.h>
28#include <wolfssl/wolfcrypt/error-crypt.h>
29#include <wolfssl/wolfcrypt/logging.h>
30
31/* IPP header files for library initialization */
32#ifdef HAVE_FAST_RSA
33#include <ipp.h>
34#include <ippcp.h>
35#endif
36
37#ifdef _MSC_VER
38 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
39 #pragma warning(disable: 4996)
40#endif
41
42
43/* Used to initialize state for wolfcrypt
44 return 0 on success
45 */
46int wolfcrypt_Init()
47{
48 /* if defined have fast RSA then initialize Intel IPP */
49 #ifdef HAVE_FAST_RSA
50 WOLFSSL_MSG("Setting up IPP Library");
51 if (ippInit() != ippStsNoErr) {
52 WOLFSSL_MSG("Error setting up optimized Intel library to use!");
53 return -1;
54 }
55 #endif
56
57 return 0;
58}
59
60
61#if WOLFSSL_CRYPT_HW_MUTEX
62/* Mutex for protection of cryptograpghy hardware */
63static wolfSSL_Mutex wcCryptHwMutex;
64static int wcCryptHwMutexInit = 0;
65
66int wolfSSL_CryptHwMutexInit(void) {
67 int ret = 0;
68 if(wcCryptHwMutexInit == 0) {
69 ret = InitMutex(&wcCryptHwMutex);
70 if(ret == 0) {
71 wcCryptHwMutexInit = 1;
72 }
73 }
74 return ret;
75}
76
77int wolfSSL_CryptHwMutexLock(void) {
78 int ret = BAD_MUTEX_E;
79
80 /* Make sure HW Mutex has been initialized */
81 wolfSSL_CryptHwMutexInit();
82
83 if(wcCryptHwMutexInit) {
84 ret = LockMutex(&wcCryptHwMutex);
85 }
86 return ret;
87}
88
89int wolfSSL_CryptHwMutexUnLock(void) {
90 int ret = BAD_MUTEX_E;
91
92 if(wcCryptHwMutexInit) {
93 ret = UnLockMutex(&wcCryptHwMutex);
94 }
95 return ret;
96}
97#endif /* WOLFSSL_CRYPT_HW_MUTEX */
98
99
100#ifdef SINGLE_THREADED
101
102int InitMutex(wolfSSL_Mutex* m)
103{
104 (void)m;
105 return 0;
106}
107
108
109int FreeMutex(wolfSSL_Mutex *m)
110{
111 (void)m;
112 return 0;
113}
114
115
116int LockMutex(wolfSSL_Mutex *m)
117{
118 (void)m;
119 return 0;
120}
121
122
123int UnLockMutex(wolfSSL_Mutex *m)
124{
125 (void)m;
126 return 0;
127}
128
129#else /* MULTI_THREAD */
130
131 #if defined(FREERTOS) || defined(FREERTOS_TCP)
132
133 int InitMutex(wolfSSL_Mutex* m)
134 {
135 int iReturn;
136
137 *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
138 if( *m != NULL )
139 iReturn = 0;
140 else
141 iReturn = BAD_MUTEX_E;
142
143 return iReturn;
144 }
145
146 int FreeMutex(wolfSSL_Mutex* m)
147 {
148 vSemaphoreDelete( *m );
149 return 0;
150 }
151
152 int LockMutex(wolfSSL_Mutex* m)
153 {
154 /* Assume an infinite block, or should there be zero block? */
155 xSemaphoreTake( *m, portMAX_DELAY );
156 return 0;
157 }
158
159 int UnLockMutex(wolfSSL_Mutex* m)
160 {
161 xSemaphoreGive( *m );
162 return 0;
163 }
164
165 #elif defined(WOLFSSL_SAFERTOS)
166
167 int InitMutex(wolfSSL_Mutex* m)
168 {
169 vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
170 if (m->mutex == NULL)
171 return BAD_MUTEX_E;
172
173 return 0;
174 }
175
176 int FreeMutex(wolfSSL_Mutex* m)
177 {
178 (void)m;
179 return 0;
180 }
181
182 int LockMutex(wolfSSL_Mutex* m)
183 {
184 /* Assume an infinite block */
185 xSemaphoreTake(m->mutex, portMAX_DELAY);
186 return 0;
187 }
188
189 int UnLockMutex(wolfSSL_Mutex* m)
190 {
191 xSemaphoreGive(m->mutex);
192 return 0;
193 }
194
195
196 #elif defined(USE_WINDOWS_API)
197
198 int InitMutex(wolfSSL_Mutex* m)
199 {
200 InitializeCriticalSection(m);
201 return 0;
202 }
203
204
205 int FreeMutex(wolfSSL_Mutex* m)
206 {
207 DeleteCriticalSection(m);
208 return 0;
209 }
210
211
212 int LockMutex(wolfSSL_Mutex* m)
213 {
214 EnterCriticalSection(m);
215 return 0;
216 }
217
218
219 int UnLockMutex(wolfSSL_Mutex* m)
220 {
221 LeaveCriticalSection(m);
222 return 0;
223 }
224
225 #elif defined(WOLFSSL_PTHREADS)
226
227 int InitMutex(wolfSSL_Mutex* m)
228 {
229 if (pthread_mutex_init(m, 0) == 0)
230 return 0;
231 else
232 return BAD_MUTEX_E;
233 }
234
235
236 int FreeMutex(wolfSSL_Mutex* m)
237 {
238 if (pthread_mutex_destroy(m) == 0)
239 return 0;
240 else
241 return BAD_MUTEX_E;
242 }
243
244
245 int LockMutex(wolfSSL_Mutex* m)
246 {
247 if (pthread_mutex_lock(m) == 0)
248 return 0;
249 else
250 return BAD_MUTEX_E;
251 }
252
253
254 int UnLockMutex(wolfSSL_Mutex* m)
255 {
256 if (pthread_mutex_unlock(m) == 0)
257 return 0;
258 else
259 return BAD_MUTEX_E;
260 }
261
262 #elif defined(THREADX)
263
264 int InitMutex(wolfSSL_Mutex* m)
265 {
266 if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
267 return 0;
268 else
269 return BAD_MUTEX_E;
270 }
271
272
273 int FreeMutex(wolfSSL_Mutex* m)
274 {
275 if (tx_mutex_delete(m) == 0)
276 return 0;
277 else
278 return BAD_MUTEX_E;
279 }
280
281
282 int LockMutex(wolfSSL_Mutex* m)
283 {
284 if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
285 return 0;
286 else
287 return BAD_MUTEX_E;
288 }
289
290
291 int UnLockMutex(wolfSSL_Mutex* m)
292 {
293 if (tx_mutex_put(m) == 0)
294 return 0;
295 else
296 return BAD_MUTEX_E;
297 }
298
299 #elif defined(MICRIUM)
300
301 int InitMutex(wolfSSL_Mutex* m)
302 {
303 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
304 if (NetSecure_OS_MutexCreate(m) == 0)
305 return 0;
306 else
307 return BAD_MUTEX_E;
308 #else
309 return 0;
310 #endif
311 }
312
313
314 int FreeMutex(wolfSSL_Mutex* m)
315 {
316 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
317 if (NetSecure_OS_FreeMutex(m) == 0)
318 return 0;
319 else
320 return BAD_MUTEX_E;
321 #else
322 return 0;
323 #endif
324 }
325
326
327 int LockMutex(wolfSSL_Mutex* m)
328 {
329 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
330 if (NetSecure_OS_LockMutex(m) == 0)
331 return 0;
332 else
333 return BAD_MUTEX_E;
334 #else
335 return 0;
336 #endif
337 }
338
339
340 int UnLockMutex(wolfSSL_Mutex* m)
341 {
342 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
343 if (NetSecure_OS_UnLockMutex(m) == 0)
344 return 0;
345 else
346 return BAD_MUTEX_E;
347 #else
348 return 0;
349 #endif
350
351 }
352
353 #elif defined(EBSNET)
354
355 int InitMutex(wolfSSL_Mutex* m)
356 {
357 if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
358 return BAD_MUTEX_E;
359 else
360 return 0;
361 }
362
363 int FreeMutex(wolfSSL_Mutex* m)
364 {
365 rtp_sig_mutex_free(*m);
366 return 0;
367 }
368
369 int LockMutex(wolfSSL_Mutex* m)
370 {
371 if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
372 return 0;
373 else
374 return BAD_MUTEX_E;
375 }
376
377 int UnLockMutex(wolfSSL_Mutex* m)
378 {
379 rtp_sig_mutex_release(*m);
380 return 0;
381 }
382
383 #elif defined(FREESCALE_MQX)
384
385 int InitMutex(wolfSSL_Mutex* m)
386 {
387 if (_mutex_init(m, NULL) == MQX_EOK)
388 return 0;
389 else
390 return BAD_MUTEX_E;
391 }
392
393 int FreeMutex(wolfSSL_Mutex* m)
394 {
395 if (_mutex_destroy(m) == MQX_EOK)
396 return 0;
397 else
398 return BAD_MUTEX_E;
399 }
400
401 int LockMutex(wolfSSL_Mutex* m)
402 {
403 if (_mutex_lock(m) == MQX_EOK)
404 return 0;
405 else
406 return BAD_MUTEX_E;
407 }
408
409 int UnLockMutex(wolfSSL_Mutex* m)
410 {
411 if (_mutex_unlock(m) == MQX_EOK)
412 return 0;
413 else
414 return BAD_MUTEX_E;
415 }
416
417 #elif defined (WOLFSSL_TIRTOS)
418 #include <xdc/runtime/Error.h>
419 int InitMutex(wolfSSL_Mutex* m)
420 {
421 Semaphore_Params params;
422 Error_Block eb;
423 Error_init(&eb);
424 Semaphore_Params_init(&params);
425 params.mode = Semaphore_Mode_BINARY;
426
427 *m = Semaphore_create(1, &params, &eb);
428 if( Error_check( &eb ) )
429 {
430 Error_raise( &eb, Error_E_generic, "Failed to Create the semaphore.",NULL);
431 } else return 0;
432 }
433
434 int FreeMutex(wolfSSL_Mutex* m)
435 {
436 Semaphore_delete(m);
437
438 return 0;
439 }
440
441 int LockMutex(wolfSSL_Mutex* m)
442 {
443 Semaphore_pend(*m, BIOS_WAIT_FOREVER);
444
445 return 0;
446 }
447
448 int UnLockMutex(wolfSSL_Mutex* m)
449 {
450 Semaphore_post(*m);
451
452 return 0;
453 }
454
455 #elif defined(WOLFSSL_LWIP)
456
457 int InitMutex(wolfSSL_Mutex* m)
458 {
459 err_t err;
460 err = sys_mutex_new(m);
461 return (err == ERR_OK) ? 0 : -1;
462 }
463
464 int FreeMutex(wolfSSL_Mutex* m)
465 {
466 sys_mutex_free(m);
467 return 0;
468 }
469
470 int LockMutex(wolfSSL_Mutex* m)
471 {
472 sys_mutex_lock(m);
473 return 0;
474 }
475
476 int UnLockMutex(wolfSSL_Mutex* m)
477 {
478 sys_mutex_unlock(m);
479 return 0;
480 }
481
482 int uITRON4_minit(size_t poolsz)
483 {
484 return 0;
485 }
486
487 void *uITRON4_malloc(size_t sz)
488 {
489 return sys_malloc(sz);
490 }
491
492 void *uITRON4_realloc(void *p, size_t sz)
493 {
494 return sys_realloc(p, sz);
495 }
496
497 void uITRON4_free(void *p)
498 {
499 sys_free(p);
500 }
501
502 #elif defined(WOLFSSL_uITRON4)
503 #include "stddef.h"
504 #include "kernel.h"
505 int InitMutex(wolfSSL_Mutex* m)
506 {
507 int iReturn;
508 m->sem.sematr = TA_TFIFO ;
509 m->sem.isemcnt = 1 ;
510 m->sem.maxsem = 1 ;
511 m->sem.name = NULL ;
512
513 m->id = acre_sem(&m->sem);
514 if( m->id != E_OK )
515 iReturn = 0;
516 else
517 iReturn = BAD_MUTEX_E;
518
519 return iReturn;
520 }
521
522 int FreeMutex(wolfSSL_Mutex* m)
523 {
524 del_sem( m->id );
525 return 0;
526 }
527
528 int LockMutex(wolfSSL_Mutex* m)
529 {
530 wai_sem(m->id);
531 return 0;
532 }
533
534 int UnLockMutex(wolfSSL_Mutex* m)
535 {
536 sig_sem(m->id);
537 return 0;
538 }
539
540 /**** uITRON malloc/free ***/
541 static ID ID_wolfssl_MPOOL = 0 ;
542 static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
543
544 int uITRON4_minit(size_t poolsz) {
545 ER ercd;
546 wolfssl_MPOOL.mplsz = poolsz ;
547 ercd = acre_mpl(&wolfssl_MPOOL);
548 if (ercd > 0) {
549 ID_wolfssl_MPOOL = ercd;
550 return 0;
551 } else {
552 return -1;
553 }
554 }
555
556 void *uITRON4_malloc(size_t sz) {
557 ER ercd;
558 void *p ;
559 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
560 if (ercd == E_OK) {
561 return p;
562 } else {
563 return 0 ;
564 }
565 }
566
567 void *uITRON4_realloc(void *p, size_t sz) {
568 ER ercd;
569 void *newp ;
570 if(p) {
571 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
572 if (ercd == E_OK) {
573 XMEMCPY(newp, p, sz) ;
574 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
575 if (ercd == E_OK) {
576 return newp;
577 }
578 }
579 }
580 return 0 ;
581 }
582
583 void uITRON4_free(void *p) {
584 ER ercd;
585 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
586 if (ercd == E_OK) {
587 return ;
588 } else {
589 return ;
590 }
591 }
592
593#elif defined(WOLFSSL_uTKERNEL2)
594 #include "tk/tkernel.h"
595 int InitMutex(wolfSSL_Mutex* m)
596 {
597 int iReturn;
598 m->sem.sematr = TA_TFIFO ;
599 m->sem.isemcnt = 1 ;
600 m->sem.maxsem = 1 ;
601
602 m->id = tk_cre_sem(&m->sem);
603 if( m->id != NULL )
604 iReturn = 0;
605 else
606 iReturn = BAD_MUTEX_E;
607
608 return iReturn;
609 }
610
611 int FreeMutex(wolfSSL_Mutex* m)
612 {
613 tk_del_sem( m->id );
614 return 0;
615 }
616
617 int LockMutex(wolfSSL_Mutex* m)
618 {
619 tk_wai_sem(m->id, 1, TMO_FEVR);
620 return 0;
621 }
622
623 int UnLockMutex(wolfSSL_Mutex* m)
624 {
625 tk_sig_sem(m->id, 1);
626 return 0;
627 }
628
629 /**** uT-Kernel malloc/free ***/
630 static ID ID_wolfssl_MPOOL = 0 ;
631 static T_CMPL wolfssl_MPOOL =
632 {(void *)NULL,
633 TA_TFIFO , 0, "wolfSSL_MPOOL"};
634
635 int uTKernel_init_mpool(unsigned int sz) {
636 ER ercd;
637 wolfssl_MPOOL.mplsz = sz ;
638 ercd = tk_cre_mpl(&wolfssl_MPOOL);
639 if (ercd > 0) {
640 ID_wolfssl_MPOOL = ercd;
641 return 0;
642 } else {
643 return -1;
644 }
645 }
646
647 void *uTKernel_malloc(unsigned int sz) {
648 ER ercd;
649 void *p ;
650 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
651 if (ercd == E_OK) {
652 return p;
653 } else {
654 return 0 ;
655 }
656 }
657
658 void *uTKernel_realloc(void *p, unsigned int sz) {
659 ER ercd;
660 void *newp ;
661 if(p) {
662 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
663 if (ercd == E_OK) {
664 XMEMCPY(newp, p, sz) ;
665 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
666 if (ercd == E_OK) {
667 return newp;
668 }
669 }
670 }
671 return 0 ;
672 }
673
674 void uTKernel_free(void *p) {
675 ER ercd;
676 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
677 if (ercd == E_OK) {
678 return ;
679 } else {
680 return ;
681 }
682 }
683
684 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_CMSIS_RTOS)
685
686 #if defined(WOLFSSL_CMSIS_RTOS)
687 #include "cmsis_os.h"
688 #define CMSIS_NMUTEX 10
689 osMutexDef(wolfSSL_mt0) ; osMutexDef(wolfSSL_mt1) ; osMutexDef(wolfSSL_mt2) ;
690 osMutexDef(wolfSSL_mt3) ; osMutexDef(wolfSSL_mt4) ; osMutexDef(wolfSSL_mt5) ;
691 osMutexDef(wolfSSL_mt6) ; osMutexDef(wolfSSL_mt7) ; osMutexDef(wolfSSL_mt8) ;
692 osMutexDef(wolfSSL_mt9) ;
693
694 static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
695 osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3),
696 osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6),
697 osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) } ;
698
699 static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0} ;
700
701 int InitMutex(wolfSSL_Mutex* m)
702 {
703 int i ;
704 for (i=0; i<CMSIS_NMUTEX; i++) {
705 if(CMSIS_mutexID[i] == 0) {
706 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]) ;
707 (*m) = CMSIS_mutexID[i] ;
708 return 0 ;
709 }
710 }
711 return -1 ;
712 }
713
714 int FreeMutex(wolfSSL_Mutex* m)
715 {
716 int i ;
717 osMutexDelete (*m) ;
718 for (i=0; i<CMSIS_NMUTEX; i++) {
719 if(CMSIS_mutexID[i] == (*m)) {
720 CMSIS_mutexID[i] = 0 ;
721 return(0) ;
722 }
723 }
724 return(-1) ;
725 }
726
727 int LockMutex(wolfSSL_Mutex* m)
728 {
729 osMutexWait(*m, osWaitForever) ;
730 return(0) ;
731 }
732
733 int UnLockMutex(wolfSSL_Mutex* m)
734 {
735 osMutexRelease (*m);
736 return 0;
737 }
738 #else
739 int InitMutex(wolfSSL_Mutex* m)
740 {
741 os_mut_init (m);
742 return 0;
743 }
744
745 int FreeMutex(wolfSSL_Mutex* m)
746 {
747 return(0) ;
748 }
749
750 int LockMutex(wolfSSL_Mutex* m)
751 {
752 os_mut_wait (m, 0xffff);
753 return(0) ;
754 }
755
756 int UnLockMutex(wolfSSL_Mutex* m)
757 {
758 os_mut_release (m);
759 return 0;
760 }
761 #endif
762 #endif /* USE_WINDOWS_API */
763
764#endif /* SINGLE_THREADED */
765
766#if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
767 #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
768 #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
769#endif
Note: See TracBrowser for help on using the repository browser.