wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wc_port.c Source File

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 #include <wolfssl/wolfcrypt/wc_port.h>
00032 #ifdef HAVE_ECC
00033     #include <wolfssl/wolfcrypt/ecc.h>
00034 #endif
00035 #ifdef WOLFSSL_ASYNC_CRYPT
00036     #include <wolfssl/wolfcrypt/async.h>
00037 #endif
00038 
00039 /* IPP header files for library initialization */
00040 #ifdef HAVE_FAST_RSA
00041 #include <ipp.h>
00042 #include <ippcp.h>
00043 #endif
00044 
00045 #if defined(FREESCALE_LTC_TFM)
00046     #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h>
00047 #endif
00048 
00049 #ifdef WOLFSSL_ATMEL
00050     #include <wolfssl/wolfcrypt/port/atmel/atmel.h>
00051 #endif
00052 
00053 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
00054     #include <wolfssl/openssl/evp.h>
00055 #endif
00056 
00057 #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY)
00058     #include <wolfssl/wolfcrypt/mem_track.h>
00059 #endif
00060 
00061 #ifdef _MSC_VER
00062     /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
00063     #pragma warning(disable: 4996)
00064 #endif
00065 
00066 /* prevent multiple mutex initializations */
00067 static volatile int initRefCount = 0;
00068 
00069 /* Used to initialize state for wolfcrypt
00070    return 0 on success
00071  */
00072 int wolfCrypt_Init(void)
00073 {
00074     int ret = 0;
00075 
00076     if (initRefCount == 0) {
00077         WOLFSSL_ENTER("wolfCrypt_Init");
00078 
00079     #ifdef WOLFSSL_ASYNC_CRYPT
00080         ret = wolfAsync_HardwareStart();
00081         if (ret != 0) {
00082             WOLFSSL_MSG("Async hardware start failed");
00083             return ret;
00084         }
00085     #endif
00086 
00087     #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
00088         ret = InitMemoryTracker();
00089         if (ret != 0) {
00090             WOLFSSL_MSG("InitMemoryTracker failed");
00091             return ret;
00092         }
00093     #endif
00094 
00095     #if WOLFSSL_CRYPT_HW_MUTEX
00096         /* If crypto hardware mutex protection is enabled, then initialize it */
00097         ret = wolfSSL_CryptHwMutexInit();
00098         if (ret != 0) {
00099             WOLFSSL_MSG("Hw crypt mutex init failed");
00100             return ret;
00101         }
00102     #endif
00103 
00104     /* if defined have fast RSA then initialize Intel IPP */
00105     #ifdef HAVE_FAST_RSA
00106         WOLFSSL_MSG("Attempting to use optimized IPP Library");
00107         if ((ret = ippInit()) != ippStsNoErr) {
00108             /* possible to get a CPU feature support status on optimized IPP
00109               library but still use default library and see competitive speeds */
00110             WOLFSSL_MSG("Warning when trying to set up optimization");
00111             WOLFSSL_MSG(ippGetStatusString(ret));
00112             WOLFSSL_MSG("Using default fast IPP library");
00113             ret = 0;
00114             (void)ret; /* suppress not read warning */
00115         }
00116     #endif
00117 
00118     #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
00119         ret = ksdk_port_init();
00120         if (ret != 0) {
00121             WOLFSSL_MSG("KSDK port init failed");
00122             return ret;
00123         }
00124     #endif
00125 
00126     #ifdef WOLFSSL_ATMEL
00127         atmel_init();
00128     #endif
00129 
00130     #ifdef WOLFSSL_ARMASM
00131         WOLFSSL_MSG("Using ARM hardware acceleration");
00132     #endif
00133 
00134     #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
00135         wolfSSL_EVP_init();
00136     #endif
00137 
00138     #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
00139         if ((ret = wc_LoggingInit()) != 0) {
00140             WOLFSSL_MSG("Error creating logging mutex");
00141             return ret;
00142         }
00143     #endif
00144 
00145 #ifdef HAVE_ECC
00146     #ifdef ECC_CACHE_CURVE
00147         if ((ret = wc_ecc_curve_cache_init()) != 0) {
00148             WOLFSSL_MSG("Error creating curve cache");
00149             return ret;
00150         }
00151     #endif
00152 #endif
00153 
00154         initRefCount = 1;
00155     }
00156 
00157     return ret;
00158 }
00159 
00160 
00161 /* return success value is the same as wolfCrypt_Init */
00162 int wolfCrypt_Cleanup(void)
00163 {
00164     int ret = 0;
00165 
00166     if (initRefCount == 1) {
00167         WOLFSSL_ENTER("wolfCrypt_Cleanup");
00168 
00169 #ifdef HAVE_ECC
00170     #ifdef FP_ECC
00171         wc_ecc_fp_free();
00172     #endif
00173     #ifdef ECC_CACHE_CURVE
00174         wc_ecc_curve_cache_free();
00175     #endif
00176 #endif /* HAVE_ECC */
00177 
00178     #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
00179         ret = wc_LoggingCleanup();
00180     #endif
00181 
00182     #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
00183         ShowMemoryTracker();
00184     #endif
00185 
00186     #ifdef WOLFSSL_ASYNC_CRYPT
00187         wolfAsync_HardwareStop();
00188     #endif
00189 
00190         initRefCount = 0; /* allow re-init */
00191     }
00192 
00193     return ret;
00194 }
00195 
00196 #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
00197 
00198 /* File Handling Helpers */
00199 int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
00200 {
00201     int ret = 0;
00202 
00203     if (name)
00204         *name = NULL;
00205 
00206     if (ctx == NULL || path == NULL) {
00207         return BAD_FUNC_ARG;
00208     }
00209 
00210     XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
00211 
00212 #ifdef USE_WINDOWS_API
00213     XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4);
00214     XSTRNCAT(ctx->name, "\\*", 3);
00215 
00216     ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
00217     if (ctx->hFind == INVALID_HANDLE_VALUE) {
00218         WOLFSSL_MSG("FindFirstFile for path verify locations failed");
00219         return BAD_PATH_ERROR;
00220     }
00221 
00222     do {
00223         if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
00224             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
00225             XSTRNCAT(ctx->name, "\\", 2);
00226             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
00227             if (name)
00228                 *name = ctx->name;
00229             return 0;
00230         }
00231     } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
00232 #else
00233     ctx->dir = opendir(path);
00234     if (ctx->dir == NULL) {
00235         WOLFSSL_MSG("opendir path verify locations failed");
00236         return BAD_PATH_ERROR;
00237     }
00238 
00239     while ((ctx->entry = readdir(ctx->dir)) != NULL) {
00240         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
00241         XSTRNCAT(ctx->name, "/", 1);
00242         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
00243 
00244         if (stat(ctx->name, &ctx->s) != 0) {
00245             WOLFSSL_MSG("stat on name failed");
00246             ret = BAD_PATH_ERROR;
00247             break;
00248         } else if (ctx->s.st_mode & S_IFREG) {
00249             if (name)
00250                 *name = ctx->name;
00251             return 0;
00252         }
00253     }
00254 #endif
00255     wc_ReadDirClose(ctx);
00256 
00257     return ret;
00258 }
00259 
00260 int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
00261 {
00262     int ret = -1;
00263 
00264     if (name)
00265         *name = NULL;
00266 
00267     if (ctx == NULL || path == NULL) {
00268         return BAD_FUNC_ARG;
00269     }
00270 
00271     XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
00272 
00273 #ifdef USE_WINDOWS_API
00274     while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
00275         if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
00276             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
00277             XSTRNCAT(ctx->name, "\\", 2);
00278             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
00279             if (name)
00280                 *name = ctx->name;
00281             return 0;
00282         }
00283     }
00284 #else
00285     while ((ctx->entry = readdir(ctx->dir)) != NULL) {
00286         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
00287         XSTRNCAT(ctx->name, "/", 1);
00288         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
00289 
00290         if (stat(ctx->name, &ctx->s) != 0) {
00291             WOLFSSL_MSG("stat on name failed");
00292             ret = BAD_PATH_ERROR;
00293             break;
00294         } else if (ctx->s.st_mode & S_IFREG) {
00295             if (name)
00296                 *name = ctx->name;
00297             return 0;
00298         }
00299     }
00300 #endif
00301 
00302     wc_ReadDirClose(ctx);
00303 
00304     return ret;
00305 }
00306 
00307 void wc_ReadDirClose(ReadDirCtx* ctx)
00308 {
00309     if (ctx == NULL) {
00310         return;
00311     }
00312 
00313 #ifdef USE_WINDOWS_API
00314     if (ctx->hFind != INVALID_HANDLE_VALUE) {
00315         FindClose(ctx->hFind);
00316         ctx->hFind = INVALID_HANDLE_VALUE;
00317     }
00318 #else
00319     if (ctx->dir) {
00320         closedir(ctx->dir);
00321         ctx->dir = NULL;
00322     }
00323 #endif
00324 }
00325 
00326 #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
00327 
00328 
00329 wolfSSL_Mutex* wc_InitAndAllocMutex()
00330 {
00331     wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
00332             DYNAMIC_TYPE_MUTEX);
00333     if (m != NULL) {
00334         if (wc_InitMutex(m) != 0) {
00335             WOLFSSL_MSG("Init Mutex failed");
00336             XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
00337             m = NULL;
00338         }
00339     }
00340     else {
00341         WOLFSSL_MSG("Memory error with Mutex allocation");
00342     }
00343 
00344     return m;
00345 }
00346 
00347 
00348 #if WOLFSSL_CRYPT_HW_MUTEX
00349 /* Mutex for protection of cryptography hardware */
00350 static wolfSSL_Mutex wcCryptHwMutex;
00351 static int wcCryptHwMutexInit = 0;
00352 
00353 int wolfSSL_CryptHwMutexInit(void) {
00354     int ret = 0;
00355     if(wcCryptHwMutexInit == 0) {
00356         ret = wc_InitMutex(&wcCryptHwMutex);
00357         if(ret == 0) {
00358             wcCryptHwMutexInit = 1;
00359         }
00360     }
00361     return ret;
00362 }
00363 
00364 int wolfSSL_CryptHwMutexLock(void) {
00365     int ret = BAD_MUTEX_E;
00366 
00367     /* Make sure HW Mutex has been initialized */
00368     wolfSSL_CryptHwMutexInit();
00369 
00370     if(wcCryptHwMutexInit) {
00371         ret = wc_LockMutex(&wcCryptHwMutex);
00372     }
00373     return ret;
00374 }
00375 
00376 int wolfSSL_CryptHwMutexUnLock(void) {
00377     int ret = BAD_MUTEX_E;
00378 
00379     if(wcCryptHwMutexInit) {
00380         ret = wc_UnLockMutex(&wcCryptHwMutex);
00381     }
00382     return ret;
00383 }
00384 #endif /* WOLFSSL_CRYPT_HW_MUTEX */
00385 
00386 
00387 /* ---------------------------------------------------------------------------*/
00388 /* Mutex Ports */
00389 /* ---------------------------------------------------------------------------*/
00390 #ifdef SINGLE_THREADED
00391 
00392     int wc_InitMutex(wolfSSL_Mutex* m)
00393     {
00394         (void)m;
00395         return 0;
00396     }
00397 
00398     int wc_FreeMutex(wolfSSL_Mutex *m)
00399     {
00400         (void)m;
00401         return 0;
00402     }
00403 
00404 
00405     int wc_LockMutex(wolfSSL_Mutex *m)
00406     {
00407         (void)m;
00408         return 0;
00409     }
00410 
00411 
00412     int wc_UnLockMutex(wolfSSL_Mutex *m)
00413     {
00414         (void)m;
00415         return 0;
00416     }
00417 
00418 #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
00419   defined(FREESCALE_FREE_RTOS)
00420 
00421     int wc_InitMutex(wolfSSL_Mutex* m)
00422     {
00423         int iReturn;
00424 
00425         *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
00426         if( *m != NULL )
00427             iReturn = 0;
00428         else
00429             iReturn = BAD_MUTEX_E;
00430 
00431         return iReturn;
00432     }
00433 
00434     int wc_FreeMutex(wolfSSL_Mutex* m)
00435     {
00436         vSemaphoreDelete( *m );
00437         return 0;
00438     }
00439 
00440     int wc_LockMutex(wolfSSL_Mutex* m)
00441     {
00442         /* Assume an infinite block, or should there be zero block? */
00443         xSemaphoreTake( *m, portMAX_DELAY );
00444         return 0;
00445     }
00446 
00447     int wc_UnLockMutex(wolfSSL_Mutex* m)
00448     {
00449         xSemaphoreGive( *m );
00450         return 0;
00451     }
00452 
00453 #elif defined(WOLFSSL_SAFERTOS)
00454 
00455     int wc_InitMutex(wolfSSL_Mutex* m)
00456     {
00457         vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
00458         if (m->mutex == NULL)
00459             return BAD_MUTEX_E;
00460 
00461         return 0;
00462     }
00463 
00464     int wc_FreeMutex(wolfSSL_Mutex* m)
00465     {
00466         (void)m;
00467         return 0;
00468     }
00469 
00470     int wc_LockMutex(wolfSSL_Mutex* m)
00471     {
00472         /* Assume an infinite block */
00473         xSemaphoreTake(m->mutex, portMAX_DELAY);
00474         return 0;
00475     }
00476 
00477     int wc_UnLockMutex(wolfSSL_Mutex* m)
00478     {
00479         xSemaphoreGive(m->mutex);
00480         return 0;
00481     }
00482 
00483 #elif defined(USE_WINDOWS_API)
00484 
00485     int wc_InitMutex(wolfSSL_Mutex* m)
00486     {
00487         InitializeCriticalSection(m);
00488         return 0;
00489     }
00490 
00491 
00492     int wc_FreeMutex(wolfSSL_Mutex* m)
00493     {
00494         DeleteCriticalSection(m);
00495         return 0;
00496     }
00497 
00498 
00499     int wc_LockMutex(wolfSSL_Mutex* m)
00500     {
00501         EnterCriticalSection(m);
00502         return 0;
00503     }
00504 
00505 
00506     int wc_UnLockMutex(wolfSSL_Mutex* m)
00507     {
00508         LeaveCriticalSection(m);
00509         return 0;
00510     }
00511 
00512 #elif defined(WOLFSSL_PTHREADS)
00513 
00514     int wc_InitMutex(wolfSSL_Mutex* m)
00515     {
00516         if (pthread_mutex_init(m, 0) == 0)
00517             return 0;
00518         else
00519             return BAD_MUTEX_E;
00520     }
00521 
00522 
00523     int wc_FreeMutex(wolfSSL_Mutex* m)
00524     {
00525         if (pthread_mutex_destroy(m) == 0)
00526             return 0;
00527         else
00528             return BAD_MUTEX_E;
00529     }
00530 
00531 
00532     int wc_LockMutex(wolfSSL_Mutex* m)
00533     {
00534         if (pthread_mutex_lock(m) == 0)
00535             return 0;
00536         else
00537             return BAD_MUTEX_E;
00538     }
00539 
00540 
00541     int wc_UnLockMutex(wolfSSL_Mutex* m)
00542     {
00543         if (pthread_mutex_unlock(m) == 0)
00544             return 0;
00545         else
00546             return BAD_MUTEX_E;
00547     }
00548 
00549 #elif defined(THREADX)
00550 
00551     int wc_InitMutex(wolfSSL_Mutex* m)
00552     {
00553         if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
00554             return 0;
00555         else
00556             return BAD_MUTEX_E;
00557     }
00558 
00559 
00560     int wc_FreeMutex(wolfSSL_Mutex* m)
00561     {
00562         if (tx_mutex_delete(m) == 0)
00563             return 0;
00564         else
00565             return BAD_MUTEX_E;
00566     }
00567 
00568 
00569     int wc_LockMutex(wolfSSL_Mutex* m)
00570     {
00571         if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
00572             return 0;
00573         else
00574             return BAD_MUTEX_E;
00575     }
00576 
00577     int wc_UnLockMutex(wolfSSL_Mutex* m)
00578     {
00579         if (tx_mutex_put(m) == 0)
00580             return 0;
00581         else
00582             return BAD_MUTEX_E;
00583     }
00584 
00585 #elif defined(MICRIUM)
00586 
00587     int wc_InitMutex(wolfSSL_Mutex* m)
00588     {
00589         #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00590             if (NetSecure_OS_MutexCreate(m) == 0)
00591                 return 0;
00592             else
00593                 return BAD_MUTEX_E;
00594         #else
00595             return 0;
00596         #endif
00597     }
00598 
00599     int wc_FreeMutex(wolfSSL_Mutex* m)
00600     {
00601         #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00602             if (NetSecure_OS_wc_FreeMutex(m) == 0)
00603                 return 0;
00604             else
00605                 return BAD_MUTEX_E;
00606         #else
00607             return 0;
00608         #endif
00609     }
00610 
00611     int wc_LockMutex(wolfSSL_Mutex* m)
00612     {
00613         #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00614             if (NetSecure_OS_wc_LockMutex(m) == 0)
00615                 return 0;
00616             else
00617                 return BAD_MUTEX_E;
00618         #else
00619             return 0;
00620         #endif
00621     }
00622 
00623     int wc_UnLockMutex(wolfSSL_Mutex* m)
00624     {
00625         #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED)
00626             if (NetSecure_OS_wc_UnLockMutex(m) == 0)
00627                 return 0;
00628             else
00629                 return BAD_MUTEX_E;
00630         #else
00631             return 0;
00632         #endif
00633 
00634     }
00635 
00636 #elif defined(EBSNET)
00637 
00638     int wc_InitMutex(wolfSSL_Mutex* m)
00639     {
00640         if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
00641             return BAD_MUTEX_E;
00642         else
00643             return 0;
00644     }
00645 
00646     int wc_FreeMutex(wolfSSL_Mutex* m)
00647     {
00648         rtp_sig_mutex_free(*m);
00649         return 0;
00650     }
00651 
00652     int wc_LockMutex(wolfSSL_Mutex* m)
00653     {
00654         if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
00655             return 0;
00656         else
00657             return BAD_MUTEX_E;
00658     }
00659 
00660     int wc_UnLockMutex(wolfSSL_Mutex* m)
00661     {
00662         rtp_sig_mutex_release(*m);
00663         return 0;
00664     }
00665 
00666 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
00667 
00668     int wc_InitMutex(wolfSSL_Mutex* m)
00669     {
00670         if (_mutex_init(m, NULL) == MQX_EOK)
00671             return 0;
00672         else
00673             return BAD_MUTEX_E;
00674     }
00675 
00676     int wc_FreeMutex(wolfSSL_Mutex* m)
00677     {
00678         if (_mutex_destroy(m) == MQX_EOK)
00679             return 0;
00680         else
00681             return BAD_MUTEX_E;
00682     }
00683 
00684     int wc_LockMutex(wolfSSL_Mutex* m)
00685     {
00686         if (_mutex_lock(m) == MQX_EOK)
00687             return 0;
00688         else
00689             return BAD_MUTEX_E;
00690     }
00691 
00692     int wc_UnLockMutex(wolfSSL_Mutex* m)
00693     {
00694         if (_mutex_unlock(m) == MQX_EOK)
00695             return 0;
00696         else
00697             return BAD_MUTEX_E;
00698     }
00699 
00700 #elif defined(WOLFSSL_TIRTOS)
00701     #include <xdc/runtime/Error.h>
00702 
00703     int wc_InitMutex(wolfSSL_Mutex* m)
00704     {
00705         Semaphore_Params params;
00706         Error_Block eb;
00707 
00708         Error_init(&eb);
00709         Semaphore_Params_init(&params);
00710         params.mode = Semaphore_Mode_BINARY;
00711 
00712         *m = Semaphore_create(1, &params, &eb);
00713         if (Error_check(&eb)) {
00714             Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
00715                 NULL);
00716             return BAD_MUTEX_E;
00717         }
00718         else
00719             return 0;
00720     }
00721 
00722     int wc_FreeMutex(wolfSSL_Mutex* m)
00723     {
00724         Semaphore_delete(m);
00725 
00726         return 0;
00727     }
00728 
00729     int wc_LockMutex(wolfSSL_Mutex* m)
00730     {
00731         Semaphore_pend(*m, BIOS_WAIT_FOREVER);
00732 
00733         return 0;
00734     }
00735 
00736     int wc_UnLockMutex(wolfSSL_Mutex* m)
00737     {
00738         Semaphore_post(*m);
00739 
00740         return 0;
00741     }
00742 
00743 #elif defined(WOLFSSL_uITRON4)
00744 
00745     int wc_InitMutex(wolfSSL_Mutex* m)
00746     {
00747         int iReturn;
00748         m->sem.sematr  = TA_TFIFO;
00749         m->sem.isemcnt = 1;
00750         m->sem.maxsem  = 1;
00751         m->sem.name    = NULL;
00752 
00753         m->id = acre_sem(&m->sem);
00754         if( m->id != E_OK )
00755             iReturn = 0;
00756         else
00757             iReturn = BAD_MUTEX_E;
00758 
00759         return iReturn;
00760     }
00761 
00762     int wc_FreeMutex(wolfSSL_Mutex* m)
00763     {
00764         del_sem( m->id );
00765         return 0;
00766     }
00767 
00768     int wc_LockMutex(wolfSSL_Mutex* m)
00769     {
00770         wai_sem(m->id);
00771         return 0;
00772     }
00773 
00774     int wc_UnLockMutex(wolfSSL_Mutex* m)
00775     {
00776         sig_sem(m->id);
00777         return 0;
00778     }
00779 
00780     /****  uITRON malloc/free ***/
00781     static ID ID_wolfssl_MPOOL = 0;
00782     static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
00783 
00784     int uITRON4_minit(size_t poolsz) {
00785         ER ercd;
00786         wolfssl_MPOOL.mplsz = poolsz;
00787         ercd = acre_mpl(&wolfssl_MPOOL);
00788         if (ercd > 0) {
00789             ID_wolfssl_MPOOL = ercd;
00790             return 0;
00791         } else {
00792             return -1;
00793         }
00794     }
00795 
00796     void *uITRON4_malloc(size_t sz) {
00797         ER ercd;
00798         void *p;
00799         ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
00800         if (ercd == E_OK) {
00801             return p;
00802         } else {
00803             return 0;
00804         }
00805     }
00806 
00807     void *uITRON4_realloc(void *p, size_t sz) {
00808       ER ercd;
00809       void *newp;
00810       if(p) {
00811           ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
00812           if (ercd == E_OK) {
00813               XMEMCPY(newp, p, sz);
00814               ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00815               if (ercd == E_OK) {
00816                   return newp;
00817               }
00818           }
00819       }
00820       return 0;
00821     }
00822 
00823     void uITRON4_free(void *p) {
00824         ER ercd;
00825         ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00826         if (ercd == E_OK) {
00827             return;
00828         } else {
00829             return;
00830         }
00831     }
00832 
00833 #elif defined(WOLFSSL_uTKERNEL2)
00834 
00835     int wc_InitMutex(wolfSSL_Mutex* m)
00836     {
00837         int iReturn;
00838         m->sem.sematr  = TA_TFIFO;
00839         m->sem.isemcnt = 1;
00840         m->sem.maxsem  = 1;
00841 
00842         m->id = tk_cre_sem(&m->sem);
00843         if( m->id != NULL )
00844             iReturn = 0;
00845         else
00846             iReturn = BAD_MUTEX_E;
00847 
00848         return iReturn;
00849     }
00850 
00851     int wc_FreeMutex(wolfSSL_Mutex* m)
00852     {
00853         tk_del_sem(m->id);
00854         return 0;
00855     }
00856 
00857     int wc_LockMutex(wolfSSL_Mutex* m)
00858     {
00859         tk_wai_sem(m->id, 1, TMO_FEVR);
00860         return 0;
00861     }
00862 
00863     int wc_UnLockMutex(wolfSSL_Mutex* m)
00864     {
00865         tk_sig_sem(m->id, 1);
00866         return 0;
00867     }
00868 
00869     /****  uT-Kernel malloc/free ***/
00870     static ID ID_wolfssl_MPOOL = 0;
00871     static T_CMPL wolfssl_MPOOL = {
00872         NULL,       /* Extended information */
00873         TA_TFIFO,   /* Memory pool attribute */
00874         0,          /* Size of whole memory pool (byte) */
00875         "wolfSSL"   /* Object name (max 8-char) */
00876     };
00877 
00878     int uTKernel_init_mpool(unsigned int sz) {
00879         ER ercd;
00880         wolfssl_MPOOL.mplsz = sz;
00881         ercd = tk_cre_mpl(&wolfssl_MPOOL);
00882         if (ercd > 0) {
00883             ID_wolfssl_MPOOL = ercd;
00884             return 0;
00885         } else {
00886             return (int)ercd;
00887         }
00888     }
00889 
00890     void *uTKernel_malloc(unsigned int sz) {
00891         ER ercd;
00892         void *p;
00893         ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
00894         if (ercd == E_OK) {
00895             return p;
00896         } else {
00897             return 0;
00898         }
00899     }
00900 
00901     void *uTKernel_realloc(void *p, unsigned int sz) {
00902       ER ercd;
00903       void *newp;
00904       if (p) {
00905           ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
00906           if (ercd == E_OK) {
00907               XMEMCPY(newp, p, sz);
00908               ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00909               if (ercd == E_OK) {
00910                   return newp;
00911               }
00912           }
00913       }
00914       return 0;
00915     }
00916 
00917     void uTKernel_free(void *p) {
00918         ER ercd;
00919         ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00920         if (ercd == E_OK) {
00921             return;
00922         } else {
00923             return;
00924         }
00925     }
00926 
00927 #elif defined (WOLFSSL_FROSTED)
00928 
00929     int wc_InitMutex(wolfSSL_Mutex* m)
00930     {
00931         *m = mutex_init();
00932         if (*m)
00933             return 0;
00934         else
00935             return -1;
00936     }
00937 
00938     int wc_FreeMutex(wolfSSL_Mutex* m)
00939     {
00940         mutex_destroy(*m);
00941         return(0);
00942     }
00943 
00944     int wc_LockMutex(wolfSSL_Mutex* m)
00945     {
00946         mutex_lock(*m);
00947         return 0;
00948     }
00949 
00950     int wc_UnLockMutex(wolfSSL_Mutex* m)
00951     {
00952         mutex_unlock(*m);
00953         return 0;
00954     }
00955 
00956 #elif defined(WOLFSSL_CMSIS_RTOS)
00957 
00958     #define CMSIS_NMUTEX 10
00959     osMutexDef(wolfSSL_mt0);  osMutexDef(wolfSSL_mt1);  osMutexDef(wolfSSL_mt2);
00960     osMutexDef(wolfSSL_mt3);  osMutexDef(wolfSSL_mt4);  osMutexDef(wolfSSL_mt5);
00961     osMutexDef(wolfSSL_mt6);  osMutexDef(wolfSSL_mt7);  osMutexDef(wolfSSL_mt8);
00962     osMutexDef(wolfSSL_mt9);
00963 
00964     static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
00965         osMutex(wolfSSL_mt1),    osMutex(wolfSSL_mt2),   osMutex(wolfSSL_mt3),
00966         osMutex(wolfSSL_mt4),    osMutex(wolfSSL_mt5),   osMutex(wolfSSL_mt6),
00967         osMutex(wolfSSL_mt7),    osMutex(wolfSSL_mt8),   osMutex(wolfSSL_mt9) };
00968 
00969     static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
00970 
00971     int wc_InitMutex(wolfSSL_Mutex* m)
00972     {
00973         int i;
00974         for (i=0; i<CMSIS_NMUTEX; i++) {
00975             if(CMSIS_mutexID[i] == 0) {
00976                 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
00977                 (*m) = CMSIS_mutexID[i];
00978             return 0;
00979             }
00980         }
00981         return -1;
00982     }
00983 
00984     int wc_FreeMutex(wolfSSL_Mutex* m)
00985     {
00986         int i;
00987         osMutexDelete   (*m);
00988         for (i=0; i<CMSIS_NMUTEX; i++) {
00989             if(CMSIS_mutexID[i] == (*m)) {
00990                 CMSIS_mutexID[i] = 0;
00991                 return(0);
00992             }
00993         }
00994         return(-1);
00995     }
00996 
00997     int wc_LockMutex(wolfSSL_Mutex* m)
00998     {
00999         osMutexWait(*m, osWaitForever);
01000         return(0);
01001     }
01002 
01003     int wc_UnLockMutex(wolfSSL_Mutex* m)
01004     {
01005         osMutexRelease (*m);
01006         return 0;
01007     }
01008 
01009 #elif defined(WOLFSSL_MDK_ARM)
01010 
01011     int wc_InitMutex(wolfSSL_Mutex* m)
01012     {
01013         os_mut_init (m);
01014         return 0;
01015     }
01016 
01017     int wc_FreeMutex(wolfSSL_Mutex* m)
01018     {
01019         return(0);
01020     }
01021 
01022     int wc_LockMutex(wolfSSL_Mutex* m)
01023     {
01024         os_mut_wait (m, 0xffff);
01025         return(0);
01026     }
01027 
01028     int wc_UnLockMutex(wolfSSL_Mutex* m)
01029     {
01030         os_mut_release (m);
01031         return 0;
01032     }
01033 
01034 #elif defined(INTIME_RTOS)
01035 
01036     int wc_InitMutex(wolfSSL_Mutex* m)
01037     {
01038         int ret = 0;
01039 
01040         if (m == NULL)
01041             return BAD_FUNC_ARG;
01042 
01043         *m = CreateRtSemaphore(
01044             1,                      /* initial unit count */
01045             1,                      /* maximum unit count */
01046             PRIORITY_QUEUING        /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
01047         );
01048         if (*m == BAD_RTHANDLE) {
01049             ret = GetLastRtError();
01050             if (ret != E_OK)
01051                 ret = BAD_MUTEX_E;
01052         }
01053         return ret;
01054     }
01055 
01056     int wc_FreeMutex(wolfSSL_Mutex* m)
01057     {
01058         int ret = 0;
01059         BOOLEAN del;
01060 
01061         if (m == NULL)
01062             return BAD_FUNC_ARG;
01063 
01064         del = DeleteRtSemaphore(
01065             *m                      /* handle for RT semaphore */
01066         );
01067         if (del != TRUE)
01068             ret = BAD_MUTEX_E;
01069 
01070         return ret;
01071     }
01072 
01073     int wc_LockMutex(wolfSSL_Mutex* m)
01074     {
01075         int ret = 0;
01076         DWORD lck;
01077 
01078         if (m == NULL)
01079             return BAD_FUNC_ARG;
01080 
01081         lck = WaitForRtSemaphore(
01082             *m,                     /* handle for RT semaphore */
01083             1,                      /* number of units to wait for */
01084             WAIT_FOREVER            /* number of milliseconds to wait for units */
01085         );
01086         if (lck == WAIT_FAILED) {
01087             ret = GetLastRtError();
01088             if (ret != E_OK)
01089                 ret = BAD_MUTEX_E;
01090         }
01091         return ret;
01092     }
01093 
01094     int wc_UnLockMutex(wolfSSL_Mutex* m)
01095     {
01096         int ret = 0;
01097         BOOLEAN rel;
01098 
01099         if (m == NULL)
01100             return BAD_FUNC_ARG;
01101 
01102         rel = ReleaseRtSemaphore(
01103             *m,                     /* handle for RT semaphore */
01104             1                       /* number of units to release to semaphore */
01105         );
01106         if (rel != TRUE)
01107             ret = BAD_MUTEX_E;
01108 
01109         return ret;
01110     }
01111 
01112 #else
01113     #warning No mutex handling defined
01114 
01115 #endif
01116 
01117 
01118 #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
01119     #include <wolfcrypt/src/port/ti/ti-ccm.c>  /* initialize and Mutex for TI Crypt Engine */
01120     #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
01121 #endif
01122