Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
wc_port.c
00001 /* port.c 00002 * 00003 * Copyright (C) 2006-2016 wolfSSL Inc. 00004 * 00005 * This file is part of wolfSSL. 00006 * 00007 * wolfSSL is free software; you can redistribute it and/or modify 00008 * it under the terms of the GNU General Public License as published by 00009 * the Free Software Foundation; either version 2 of the License, or 00010 * (at your option) any later version. 00011 * 00012 * wolfSSL is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU General Public License 00018 * along with this program; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 00020 */ 00021 00022 00023 #ifdef HAVE_CONFIG_H 00024 #include <config.h> 00025 #endif 00026 00027 #include <wolfssl/wolfcrypt/settings.h> 00028 #include <wolfssl/wolfcrypt/types.h> 00029 #include <wolfssl/wolfcrypt/error-crypt.h> 00030 #include <wolfssl/wolfcrypt/logging.h> 00031 00032 /* IPP header files for library initialization */ 00033 #ifdef HAVE_FAST_RSA 00034 #include <ipp.h> 00035 #include <ippcp.h> 00036 #endif 00037 00038 #ifdef _MSC_VER 00039 /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ 00040 #pragma warning(disable: 4996) 00041 #endif 00042 00043 00044 /* Used to initialize state for wolfcrypt 00045 return 0 on success 00046 */ 00047 int wolfCrypt_Init() 00048 { 00049 int ret = 0; 00050 #if WOLFSSL_CRYPT_HW_MUTEX 00051 /* If crypto hardware mutex protection is enabled, then initialize it */ 00052 wolfSSL_CryptHwMutexInit(); 00053 #endif 00054 00055 /* if defined have fast RSA then initialize Intel IPP */ 00056 #ifdef HAVE_FAST_RSA 00057 WOLFSSL_MSG("Attempting to use optimized IPP Library"); 00058 if ((ret = ippInit()) != ippStsNoErr) { 00059 /* possible to get a CPU feature support status on optimized IPP 00060 library but still use default library and see competitive speeds */ 00061 WOLFSSL_MSG("Warning when trying to set up optimization"); 00062 WOLFSSL_MSG(ippGetStatusString(ret)); 00063 WOLFSSL_MSG("Using default fast IPP library"); 00064 ret = 0; 00065 } 00066 #endif 00067 00068 return ret; 00069 } 00070 00071 00072 #if WOLFSSL_CRYPT_HW_MUTEX 00073 /* Mutex for protection of cryptography hardware */ 00074 static wolfSSL_Mutex wcCryptHwMutex; 00075 static int wcCryptHwMutexInit = 0; 00076 00077 int wolfSSL_CryptHwMutexInit(void) { 00078 int ret = 0; 00079 if(wcCryptHwMutexInit == 0) { 00080 ret = InitMutex(&wcCryptHwMutex); 00081 if(ret == 0) { 00082 wcCryptHwMutexInit = 1; 00083 } 00084 } 00085 return ret; 00086 } 00087 00088 int wolfSSL_CryptHwMutexLock(void) { 00089 int ret = BAD_MUTEX_E; 00090 00091 /* Make sure HW Mutex has been initialized */ 00092 wolfSSL_CryptHwMutexInit(); 00093 00094 if(wcCryptHwMutexInit) { 00095 ret = LockMutex(&wcCryptHwMutex); 00096 } 00097 return ret; 00098 } 00099 00100 int wolfSSL_CryptHwMutexUnLock(void) { 00101 int ret = BAD_MUTEX_E; 00102 00103 if(wcCryptHwMutexInit) { 00104 ret = UnLockMutex(&wcCryptHwMutex); 00105 } 00106 return ret; 00107 } 00108 #endif /* WOLFSSL_CRYPT_HW_MUTEX */ 00109 00110 00111 #ifdef SINGLE_THREADED 00112 00113 int InitMutex(wolfSSL_Mutex* m) 00114 { 00115 (void)m; 00116 return 0; 00117 } 00118 00119 00120 int FreeMutex(wolfSSL_Mutex *m) 00121 { 00122 (void)m; 00123 return 0; 00124 } 00125 00126 00127 int LockMutex(wolfSSL_Mutex *m) 00128 { 00129 (void)m; 00130 return 0; 00131 } 00132 00133 00134 int UnLockMutex(wolfSSL_Mutex *m) 00135 { 00136 (void)m; 00137 return 0; 00138 } 00139 00140 #else /* MULTI_THREAD */ 00141 00142 #if defined(FREERTOS) || defined(FREERTOS_TCP) || \ 00143 defined(FREESCALE_FREE_RTOS) 00144 00145 int InitMutex(wolfSSL_Mutex* m) 00146 { 00147 int iReturn; 00148 00149 *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex(); 00150 if( *m != NULL ) 00151 iReturn = 0; 00152 else 00153 iReturn = BAD_MUTEX_E; 00154 00155 return iReturn; 00156 } 00157 00158 int FreeMutex(wolfSSL_Mutex* m) 00159 { 00160 vSemaphoreDelete( *m ); 00161 return 0; 00162 } 00163 00164 int LockMutex(wolfSSL_Mutex* m) 00165 { 00166 /* Assume an infinite block, or should there be zero block? */ 00167 xSemaphoreTake( *m, portMAX_DELAY ); 00168 return 0; 00169 } 00170 00171 int UnLockMutex(wolfSSL_Mutex* m) 00172 { 00173 xSemaphoreGive( *m ); 00174 return 0; 00175 } 00176 00177 #elif defined(WOLFSSL_SAFERTOS) 00178 00179 int InitMutex(wolfSSL_Mutex* m) 00180 { 00181 vSemaphoreCreateBinary(m->mutexBuffer, m->mutex); 00182 if (m->mutex == NULL) 00183 return BAD_MUTEX_E; 00184 00185 return 0; 00186 } 00187 00188 int FreeMutex(wolfSSL_Mutex* m) 00189 { 00190 (void)m; 00191 return 0; 00192 } 00193 00194 int LockMutex(wolfSSL_Mutex* m) 00195 { 00196 /* Assume an infinite block */ 00197 xSemaphoreTake(m->mutex, portMAX_DELAY); 00198 return 0; 00199 } 00200 00201 int UnLockMutex(wolfSSL_Mutex* m) 00202 { 00203 xSemaphoreGive(m->mutex); 00204 return 0; 00205 } 00206 00207 00208 #elif defined(USE_WINDOWS_API) 00209 00210 int InitMutex(wolfSSL_Mutex* m) 00211 { 00212 InitializeCriticalSection(m); 00213 return 0; 00214 } 00215 00216 00217 int FreeMutex(wolfSSL_Mutex* m) 00218 { 00219 DeleteCriticalSection(m); 00220 return 0; 00221 } 00222 00223 00224 int LockMutex(wolfSSL_Mutex* m) 00225 { 00226 EnterCriticalSection(m); 00227 return 0; 00228 } 00229 00230 00231 int UnLockMutex(wolfSSL_Mutex* m) 00232 { 00233 LeaveCriticalSection(m); 00234 return 0; 00235 } 00236 00237 #elif defined(WOLFSSL_PTHREADS) 00238 00239 int InitMutex(wolfSSL_Mutex* m) 00240 { 00241 if (pthread_mutex_init(m, 0) == 0) 00242 return 0; 00243 else 00244 return BAD_MUTEX_E; 00245 } 00246 00247 00248 int FreeMutex(wolfSSL_Mutex* m) 00249 { 00250 if (pthread_mutex_destroy(m) == 0) 00251 return 0; 00252 else 00253 return BAD_MUTEX_E; 00254 } 00255 00256 00257 int LockMutex(wolfSSL_Mutex* m) 00258 { 00259 if (pthread_mutex_lock(m) == 0) 00260 return 0; 00261 else 00262 return BAD_MUTEX_E; 00263 } 00264 00265 00266 int UnLockMutex(wolfSSL_Mutex* m) 00267 { 00268 if (pthread_mutex_unlock(m) == 0) 00269 return 0; 00270 else 00271 return BAD_MUTEX_E; 00272 } 00273 00274 #elif defined(THREADX) 00275 00276 int InitMutex(wolfSSL_Mutex* m) 00277 { 00278 if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0) 00279 return 0; 00280 else 00281 return BAD_MUTEX_E; 00282 } 00283 00284 00285 int FreeMutex(wolfSSL_Mutex* m) 00286 { 00287 if (tx_mutex_delete(m) == 0) 00288 return 0; 00289 else 00290 return BAD_MUTEX_E; 00291 } 00292 00293 00294 int LockMutex(wolfSSL_Mutex* m) 00295 { 00296 if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0) 00297 return 0; 00298 else 00299 return BAD_MUTEX_E; 00300 } 00301 00302 00303 int UnLockMutex(wolfSSL_Mutex* m) 00304 { 00305 if (tx_mutex_put(m) == 0) 00306 return 0; 00307 else 00308 return BAD_MUTEX_E; 00309 } 00310 00311 #elif defined(MICRIUM) 00312 00313 int InitMutex(wolfSSL_Mutex* m) 00314 { 00315 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) 00316 if (NetSecure_OS_MutexCreate(m) == 0) 00317 return 0; 00318 else 00319 return BAD_MUTEX_E; 00320 #else 00321 return 0; 00322 #endif 00323 } 00324 00325 00326 int FreeMutex(wolfSSL_Mutex* m) 00327 { 00328 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) 00329 if (NetSecure_OS_FreeMutex(m) == 0) 00330 return 0; 00331 else 00332 return BAD_MUTEX_E; 00333 #else 00334 return 0; 00335 #endif 00336 } 00337 00338 00339 int LockMutex(wolfSSL_Mutex* m) 00340 { 00341 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) 00342 if (NetSecure_OS_LockMutex(m) == 0) 00343 return 0; 00344 else 00345 return BAD_MUTEX_E; 00346 #else 00347 return 0; 00348 #endif 00349 } 00350 00351 00352 int UnLockMutex(wolfSSL_Mutex* m) 00353 { 00354 #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) 00355 if (NetSecure_OS_UnLockMutex(m) == 0) 00356 return 0; 00357 else 00358 return BAD_MUTEX_E; 00359 #else 00360 return 0; 00361 #endif 00362 00363 } 00364 00365 #elif defined(EBSNET) 00366 00367 int InitMutex(wolfSSL_Mutex* m) 00368 { 00369 if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1) 00370 return BAD_MUTEX_E; 00371 else 00372 return 0; 00373 } 00374 00375 int FreeMutex(wolfSSL_Mutex* m) 00376 { 00377 rtp_sig_mutex_free(*m); 00378 return 0; 00379 } 00380 00381 int LockMutex(wolfSSL_Mutex* m) 00382 { 00383 if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0) 00384 return 0; 00385 else 00386 return BAD_MUTEX_E; 00387 } 00388 00389 int UnLockMutex(wolfSSL_Mutex* m) 00390 { 00391 rtp_sig_mutex_release(*m); 00392 return 0; 00393 } 00394 00395 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) 00396 00397 int InitMutex(wolfSSL_Mutex* m) 00398 { 00399 if (_mutex_init(m, NULL) == MQX_EOK) 00400 return 0; 00401 else 00402 return BAD_MUTEX_E; 00403 } 00404 00405 int FreeMutex(wolfSSL_Mutex* m) 00406 { 00407 if (_mutex_destroy(m) == MQX_EOK) 00408 return 0; 00409 else 00410 return BAD_MUTEX_E; 00411 } 00412 00413 int LockMutex(wolfSSL_Mutex* m) 00414 { 00415 if (_mutex_lock(m) == MQX_EOK) 00416 return 0; 00417 else 00418 return BAD_MUTEX_E; 00419 } 00420 00421 int UnLockMutex(wolfSSL_Mutex* m) 00422 { 00423 if (_mutex_unlock(m) == MQX_EOK) 00424 return 0; 00425 else 00426 return BAD_MUTEX_E; 00427 } 00428 00429 #elif defined (WOLFSSL_TIRTOS) 00430 #include <xdc/runtime/Error.h> 00431 int InitMutex(wolfSSL_Mutex* m) 00432 { 00433 Semaphore_Params params; 00434 Error_Block eb; 00435 Error_init(&eb); 00436 Semaphore_Params_init(¶ms); 00437 params.mode = Semaphore_Mode_BINARY; 00438 00439 *m = Semaphore_create(1, ¶ms, &eb); 00440 if( Error_check( &eb ) ) 00441 { 00442 Error_raise( &eb, Error_E_generic, "Failed to Create the semaphore.",NULL); 00443 } else return 0; 00444 } 00445 00446 int FreeMutex(wolfSSL_Mutex* m) 00447 { 00448 Semaphore_delete(m); 00449 00450 return 0; 00451 } 00452 00453 int LockMutex(wolfSSL_Mutex* m) 00454 { 00455 Semaphore_pend(*m, BIOS_WAIT_FOREVER); 00456 00457 return 0; 00458 } 00459 00460 int UnLockMutex(wolfSSL_Mutex* m) 00461 { 00462 Semaphore_post(*m); 00463 00464 return 0; 00465 } 00466 00467 #elif defined(WOLFSSL_uITRON4) 00468 #include "stddef.h" 00469 #include "kernel.h" 00470 int InitMutex(wolfSSL_Mutex* m) 00471 { 00472 int iReturn; 00473 m->sem.sematr = TA_TFIFO ; 00474 m->sem.isemcnt = 1 ; 00475 m->sem.maxsem = 1 ; 00476 m->sem.name = NULL ; 00477 00478 m->id = acre_sem(&m->sem); 00479 if( m->id != E_OK ) 00480 iReturn = 0; 00481 else 00482 iReturn = BAD_MUTEX_E; 00483 00484 return iReturn; 00485 } 00486 00487 int FreeMutex(wolfSSL_Mutex* m) 00488 { 00489 del_sem( m->id ); 00490 return 0; 00491 } 00492 00493 int LockMutex(wolfSSL_Mutex* m) 00494 { 00495 wai_sem(m->id); 00496 return 0; 00497 } 00498 00499 int UnLockMutex(wolfSSL_Mutex* m) 00500 { 00501 sig_sem(m->id); 00502 return 0; 00503 } 00504 00505 /**** uITRON malloc/free ***/ 00506 static ID ID_wolfssl_MPOOL = 0 ; 00507 static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"}; 00508 00509 int uITRON4_minit(size_t poolsz) { 00510 ER ercd; 00511 wolfssl_MPOOL.mplsz = poolsz ; 00512 ercd = acre_mpl(&wolfssl_MPOOL); 00513 if (ercd > 0) { 00514 ID_wolfssl_MPOOL = ercd; 00515 return 0; 00516 } else { 00517 return -1; 00518 } 00519 } 00520 00521 void *uITRON4_malloc(size_t sz) { 00522 ER ercd; 00523 void *p ; 00524 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p); 00525 if (ercd == E_OK) { 00526 return p; 00527 } else { 00528 return 0 ; 00529 } 00530 } 00531 00532 void *uITRON4_realloc(void *p, size_t sz) { 00533 ER ercd; 00534 void *newp ; 00535 if(p) { 00536 ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp); 00537 if (ercd == E_OK) { 00538 XMEMCPY(newp, p, sz) ; 00539 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); 00540 if (ercd == E_OK) { 00541 return newp; 00542 } 00543 } 00544 } 00545 return 0 ; 00546 } 00547 00548 void uITRON4_free(void *p) { 00549 ER ercd; 00550 ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); 00551 if (ercd == E_OK) { 00552 return ; 00553 } else { 00554 return ; 00555 } 00556 } 00557 00558 #elif defined(WOLFSSL_uTKERNEL2) 00559 #include "tk/tkernel.h" 00560 int InitMutex(wolfSSL_Mutex* m) 00561 { 00562 int iReturn; 00563 m->sem.sematr = TA_TFIFO ; 00564 m->sem.isemcnt = 1 ; 00565 m->sem.maxsem = 1 ; 00566 00567 m->id = tk_cre_sem(&m->sem); 00568 if( m->id != NULL ) 00569 iReturn = 0; 00570 else 00571 iReturn = BAD_MUTEX_E; 00572 00573 return iReturn; 00574 } 00575 00576 int FreeMutex(wolfSSL_Mutex* m) 00577 { 00578 tk_del_sem( m->id ); 00579 return 0; 00580 } 00581 00582 int LockMutex(wolfSSL_Mutex* m) 00583 { 00584 tk_wai_sem(m->id, 1, TMO_FEVR); 00585 return 0; 00586 } 00587 00588 int UnLockMutex(wolfSSL_Mutex* m) 00589 { 00590 tk_sig_sem(m->id, 1); 00591 return 0; 00592 } 00593 00594 /**** uT-Kernel malloc/free ***/ 00595 static ID ID_wolfssl_MPOOL = 0 ; 00596 static T_CMPL wolfssl_MPOOL = 00597 {(void *)NULL, 00598 TA_TFIFO , 0, "wolfSSL_MPOOL"}; 00599 00600 int uTKernel_init_mpool(unsigned int sz) { 00601 ER ercd; 00602 wolfssl_MPOOL.mplsz = sz ; 00603 ercd = tk_cre_mpl(&wolfssl_MPOOL); 00604 if (ercd > 0) { 00605 ID_wolfssl_MPOOL = ercd; 00606 return 0; 00607 } else { 00608 return -1; 00609 } 00610 } 00611 00612 void *uTKernel_malloc(unsigned int sz) { 00613 ER ercd; 00614 void *p ; 00615 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR); 00616 if (ercd == E_OK) { 00617 return p; 00618 } else { 00619 return 0 ; 00620 } 00621 } 00622 00623 void *uTKernel_realloc(void *p, unsigned int sz) { 00624 ER ercd; 00625 void *newp ; 00626 if(p) { 00627 ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR); 00628 if (ercd == E_OK) { 00629 XMEMCPY(newp, p, sz) ; 00630 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); 00631 if (ercd == E_OK) { 00632 return newp; 00633 } 00634 } 00635 } 00636 return 0 ; 00637 } 00638 00639 void uTKernel_free(void *p) { 00640 ER ercd; 00641 ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); 00642 if (ercd == E_OK) { 00643 return ; 00644 } else { 00645 return ; 00646 } 00647 } 00648 00649 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_CMSIS_RTOS) 00650 00651 #if defined(WOLFSSL_CMSIS_RTOS) 00652 #include "cmsis_os.h" 00653 #define CMSIS_NMUTEX 10 00654 osMutexDef(wolfSSL_mt0) ; osMutexDef(wolfSSL_mt1) ; osMutexDef(wolfSSL_mt2) ; 00655 osMutexDef(wolfSSL_mt3) ; osMutexDef(wolfSSL_mt4) ; osMutexDef(wolfSSL_mt5) ; 00656 osMutexDef(wolfSSL_mt6) ; osMutexDef(wolfSSL_mt7) ; osMutexDef(wolfSSL_mt8) ; 00657 osMutexDef(wolfSSL_mt9) ; 00658 00659 static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0), 00660 osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3), 00661 osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6), 00662 osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) } ; 00663 00664 static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0} ; 00665 00666 int InitMutex(wolfSSL_Mutex* m) 00667 { 00668 int i ; 00669 for (i=0; i<CMSIS_NMUTEX; i++) { 00670 if(CMSIS_mutexID[i] == 0) { 00671 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]) ; 00672 (*m) = CMSIS_mutexID[i] ; 00673 return 0 ; 00674 } 00675 } 00676 return -1 ; 00677 } 00678 00679 int FreeMutex(wolfSSL_Mutex* m) 00680 { 00681 int i ; 00682 osMutexDelete (*m) ; 00683 for (i=0; i<CMSIS_NMUTEX; i++) { 00684 if(CMSIS_mutexID[i] == (*m)) { 00685 CMSIS_mutexID[i] = 0 ; 00686 return(0) ; 00687 } 00688 } 00689 return(-1) ; 00690 } 00691 00692 int LockMutex(wolfSSL_Mutex* m) 00693 { 00694 osMutexWait(*m, osWaitForever) ; 00695 return(0) ; 00696 } 00697 00698 int UnLockMutex(wolfSSL_Mutex* m) 00699 { 00700 osMutexRelease (*m); 00701 return 0; 00702 } 00703 #else 00704 int InitMutex(wolfSSL_Mutex* m) 00705 { 00706 os_mut_init (m); 00707 return 0; 00708 } 00709 00710 int FreeMutex(wolfSSL_Mutex* m) 00711 { 00712 return(0) ; 00713 } 00714 00715 int LockMutex(wolfSSL_Mutex* m) 00716 { 00717 os_mut_wait (m, 0xffff); 00718 return(0) ; 00719 } 00720 00721 int UnLockMutex(wolfSSL_Mutex* m) 00722 { 00723 os_mut_release (m); 00724 return 0; 00725 } 00726 #endif 00727 #endif /* USE_WINDOWS_API */ 00728 00729 #endif /* SINGLE_THREADED */ 00730 00731 #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) 00732 #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */ 00733 #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */ 00734 #endif 00735
Generated on Tue Jul 12 2022 15:55:23 by
1.7.2