Xuyi Wang / wolfcrypt

Dependents:   OS

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-2017 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 <wolfcrypt/settings.h>
00028 #include <wolfcrypt/types.h>
00029 #include <wolfcrypt/error-crypt.h>
00030 #include <wolfcrypt/logging.h>
00031 #include <wolfcrypt/wc_port.h>
00032 #ifdef HAVE_ECC
00033     #include <wolfcrypt/ecc.h>
00034 #endif
00035 #ifdef WOLFSSL_ASYNC_CRYPT
00036     #include <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 #ifdef FREESCALE_LTC_TFM
00046     #include <wolfcrypt/port/nxp/ksdk_port.h>
00047 #endif
00048 
00049 #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
00050     #include <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 <wolfcrypt/memory.h>
00059     #include <wolfcrypt/mem_track.h>
00060 #endif
00061 
00062 #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
00063     defined(WOLFSSL_IMX6_CAAM_BLOB)
00064     #include <wolfcrypt/port/caam/wolfcaam.h>
00065 #endif
00066 
00067 #ifdef WOLF_CRYPTO_DEV
00068     #include <wolfcrypt/cryptodev.h>
00069 #endif
00070 
00071 #ifdef _MSC_VER
00072     /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */
00073     #pragma warning(disable: 4996)
00074 #endif
00075 
00076 /* prevent multiple mutex initializations */
00077 static volatile int initRefCount = 0;
00078 
00079 /* Used to initialize state for wolfcrypt
00080    return 0 on success
00081  */
00082 int wolfCrypt_Init(void)
00083 {
00084     int ret = 0;
00085 
00086     if (initRefCount == 0) {
00087         WOLFSSL_ENTER("wolfCrypt_Init");
00088 
00089     #ifdef WOLF_CRYPTO_DEV
00090         wc_CryptoDev_Init();
00091     #endif
00092 
00093     #ifdef WOLFSSL_ASYNC_CRYPT
00094         ret = wolfAsync_HardwareStart();
00095         if (ret != 0) {
00096             WOLFSSL_MSG("Async hardware start failed");
00097             /* don't return failure, allow operation to continue */
00098         }
00099     #endif
00100 
00101     #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
00102         ret = InitMemoryTracker();
00103         if (ret != 0) {
00104             WOLFSSL_MSG("InitMemoryTracker failed");
00105             return ret;
00106         }
00107     #endif
00108 
00109     #if WOLFSSL_CRYPT_HW_MUTEX
00110         /* If crypto hardware mutex protection is enabled, then initialize it */
00111         ret = wolfSSL_CryptHwMutexInit();
00112         if (ret != 0) {
00113             WOLFSSL_MSG("Hw crypt mutex init failed");
00114             return ret;
00115         }
00116     #endif
00117 
00118     /* if defined have fast RSA then initialize Intel IPP */
00119     #ifdef HAVE_FAST_RSA
00120         WOLFSSL_MSG("Attempting to use optimized IPP Library");
00121         if ((ret = ippInit()) != ippStsNoErr) {
00122             /* possible to get a CPU feature support status on optimized IPP
00123               library but still use default library and see competitive speeds */
00124             WOLFSSL_MSG("Warning when trying to set up optimization");
00125             WOLFSSL_MSG(ippGetStatusString(ret));
00126             WOLFSSL_MSG("Using default fast IPP library");
00127             ret = 0;
00128             (void)ret; /* suppress not read warning */
00129         }
00130     #endif
00131 
00132     #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC)
00133         ret = ksdk_port_init();
00134         if (ret != 0) {
00135             WOLFSSL_MSG("KSDK port init failed");
00136             return ret;
00137         }
00138     #endif
00139 
00140     #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A)
00141         atmel_init();
00142     #endif
00143 
00144     #ifdef WOLFSSL_ARMASM
00145         WOLFSSL_MSG("Using ARM hardware acceleration");
00146     #endif
00147 
00148     #if !defined(WOLFCRYPT_ONLY) && \
00149         ( defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) )
00150         wolfSSL_EVP_init();
00151     #endif
00152 
00153     #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
00154         if ((ret = wc_LoggingInit()) != 0) {
00155             WOLFSSL_MSG("Error creating logging mutex");
00156             return ret;
00157         }
00158     #endif
00159 
00160 #ifdef HAVE_ECC
00161     #ifdef ECC_CACHE_CURVE
00162         if ((ret = wc_ecc_curve_cache_init()) != 0) {
00163             WOLFSSL_MSG("Error creating curve cache");
00164             return ret;
00165         }
00166     #endif
00167 #endif
00168 
00169 #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
00170     defined(WOLFSSL_IMX6_CAAM_BLOB)
00171         if ((ret = wc_caamInit()) != 0) {
00172             return ret;
00173         }
00174 #endif
00175 
00176         initRefCount = 1;
00177     }
00178 
00179     return ret;
00180 }
00181 
00182 
00183 /* return success value is the same as wolfCrypt_Init */
00184 int wolfCrypt_Cleanup(void)
00185 {
00186     int ret = 0;
00187 
00188     if (initRefCount == 1) {
00189         WOLFSSL_ENTER("wolfCrypt_Cleanup");
00190 
00191 #ifdef HAVE_ECC
00192     #ifdef FP_ECC
00193         wc_ecc_fp_free();
00194     #endif
00195     #ifdef ECC_CACHE_CURVE
00196         wc_ecc_curve_cache_free();
00197     #endif
00198 #endif /* HAVE_ECC */
00199 
00200     #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
00201         ret = wc_LoggingCleanup();
00202     #endif
00203 
00204     #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY)
00205         ShowMemoryTracker();
00206     #endif
00207 
00208     #ifdef WOLFSSL_ASYNC_CRYPT
00209         wolfAsync_HardwareStop();
00210     #endif
00211 
00212     #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \
00213         defined(WOLFSSL_IMX6_CAAM_BLOB)
00214         wc_caamFree();
00215     #endif
00216 
00217         initRefCount = 0; /* allow re-init */
00218     }
00219 
00220     return ret;
00221 }
00222 
00223 #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
00224     !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
00225 
00226 /* File Handling Helpers */
00227 /* returns 0 if file found, -1 if no files or negative error */
00228 int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
00229 {
00230     int ret = -1; /* default to no files found */
00231 
00232     if (name)
00233         *name = NULL;
00234 
00235     if (ctx == NULL || path == NULL) {
00236         return BAD_FUNC_ARG;
00237     }
00238 
00239     XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
00240 
00241 #ifdef USE_WINDOWS_API
00242     XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4);
00243     XSTRNCAT(ctx->name, "\\*", 3);
00244 
00245     ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData);
00246     if (ctx->hFind == INVALID_HANDLE_VALUE) {
00247         WOLFSSL_MSG("FindFirstFile for path verify locations failed");
00248         return BAD_PATH_ERROR;
00249     }
00250 
00251     do {
00252         if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
00253             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
00254             XSTRNCAT(ctx->name, "\\", 2);
00255             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
00256             if (name)
00257                 *name = ctx->name;
00258             return 0;
00259         }
00260     } while (FindNextFileA(ctx->hFind, &ctx->FindFileData));
00261 #else
00262     ctx->dir = opendir(path);
00263     if (ctx->dir == NULL) {
00264         WOLFSSL_MSG("opendir path verify locations failed");
00265         return BAD_PATH_ERROR;
00266     }
00267 
00268     while ((ctx->entry = readdir(ctx->dir)) != NULL) {
00269         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
00270         XSTRNCAT(ctx->name, "/", 1);
00271         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
00272 
00273         if (stat(ctx->name, &ctx->s) != 0) {
00274             WOLFSSL_MSG("stat on name failed");
00275             ret = BAD_PATH_ERROR;
00276             break;
00277         } else if (S_ISREG(ctx->s.st_mode)) {
00278             if (name)
00279                 *name = ctx->name;
00280             return 0;
00281         }
00282     }
00283 #endif
00284     wc_ReadDirClose(ctx);
00285 
00286     return ret;
00287 }
00288 
00289 /* returns 0 if file found, -1 if no more files */
00290 int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
00291 {
00292     int ret = -1; /* default to no file found */
00293 
00294     if (name)
00295         *name = NULL;
00296 
00297     if (ctx == NULL || path == NULL) {
00298         return BAD_FUNC_ARG;
00299     }
00300 
00301     XMEMSET(ctx->name, 0, MAX_FILENAME_SZ);
00302 
00303 #ifdef USE_WINDOWS_API
00304     while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) {
00305         if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
00306             XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3);
00307             XSTRNCAT(ctx->name, "\\", 2);
00308             XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2);
00309             if (name)
00310                 *name = ctx->name;
00311             return 0;
00312         }
00313     }
00314 #else
00315     while ((ctx->entry = readdir(ctx->dir)) != NULL) {
00316         XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2);
00317         XSTRNCAT(ctx->name, "/", 1);
00318         XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2);
00319 
00320         if (stat(ctx->name, &ctx->s) != 0) {
00321             WOLFSSL_MSG("stat on name failed");
00322             ret = BAD_PATH_ERROR;
00323             break;
00324         } else if (S_ISREG(ctx->s.st_mode)) {
00325             if (name)
00326                 *name = ctx->name;
00327             return 0;
00328         }
00329     }
00330 #endif
00331 
00332     wc_ReadDirClose(ctx);
00333 
00334     return ret;
00335 }
00336 
00337 void wc_ReadDirClose(ReadDirCtx* ctx)
00338 {
00339     if (ctx == NULL) {
00340         return;
00341     }
00342 
00343 #ifdef USE_WINDOWS_API
00344     if (ctx->hFind != INVALID_HANDLE_VALUE) {
00345         FindClose(ctx->hFind);
00346         ctx->hFind = INVALID_HANDLE_VALUE;
00347     }
00348 #else
00349     if (ctx->dir) {
00350         closedir(ctx->dir);
00351         ctx->dir = NULL;
00352     }
00353 #endif
00354 }
00355 
00356 #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
00357 
00358 
00359 wolfSSL_Mutex* wc_InitAndAllocMutex(void)
00360 {
00361     wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL,
00362             DYNAMIC_TYPE_MUTEX);
00363     if (m != NULL) {
00364         if (wc_InitMutex(m) != 0) {
00365             WOLFSSL_MSG("Init Mutex failed");
00366             XFREE(m, NULL, DYNAMIC_TYPE_MUTEX);
00367             m = NULL;
00368         }
00369     }
00370     else {
00371         WOLFSSL_MSG("Memory error with Mutex allocation");
00372     }
00373 
00374     return m;
00375 }
00376 
00377 #ifdef USE_WOLF_STRTOK
00378 /* String token (delim) search. If str is null use nextp. */
00379 char* wc_strtok(char *str, const char *delim, char **nextp)
00380 {
00381     char* ret;
00382     int i, j;
00383 
00384     /* Use next if str is NULL */
00385     if (str == NULL && nextp)
00386         str = *nextp;
00387 
00388     /* verify str input */
00389     if (str == NULL || *str == '\0')
00390         return NULL;
00391 
00392     /* match on entire delim */
00393     for (i = 0; str[i]; i++) {
00394         for (j = 0; delim[j]; j++) {
00395             if (delim[j] == str[i])
00396                 break;
00397         }
00398         if (!delim[j])
00399             break;
00400     }
00401     str += i;
00402     /* if end of string, not found so return NULL */
00403     if (*str == '\0')
00404         return NULL;
00405 
00406     ret = str;
00407 
00408     /* match on first delim */
00409     for (i = 0; str[i]; i++) {
00410         for (j = 0; delim[j]; j++) {
00411             if (delim[j] == str[i])
00412                 break;
00413         }
00414         if (delim[j] == str[i])
00415             break;
00416     }
00417     str += i;
00418 
00419     /* null terminate found string */
00420     if (*str)
00421         *str++ = '\0';
00422 
00423     /* return pointer to next */
00424     if (nextp)
00425         *nextp = str;
00426 
00427     return ret;
00428 }
00429 #endif /* USE_WOLF_STRTOK */
00430 
00431 #ifdef USE_WOLF_STRSEP
00432 char* wc_strsep(char **stringp, const char *delim)
00433 {
00434     char *s, *tok;
00435     const char *spanp;
00436 
00437     /* null check */
00438     if (stringp == NULL || *stringp == NULL)
00439         return NULL;
00440 
00441     s = *stringp;
00442     for (tok = s; *tok; ++tok) {
00443         for (spanp = delim; *spanp; ++spanp) {
00444             /* found delimiter */
00445             if (*tok == *spanp) {
00446                 *tok = '\0'; /* replace delim with null term */
00447                 *stringp = tok + 1; /* return past delim */
00448                 return s;
00449             }
00450         }
00451     }
00452 
00453     *stringp = NULL;
00454     return s;
00455 }
00456 #endif /* USE_WOLF_STRSEP */
00457 
00458 #if WOLFSSL_CRYPT_HW_MUTEX
00459 /* Mutex for protection of cryptography hardware */
00460 static wolfSSL_Mutex wcCryptHwMutex;
00461 static int wcCryptHwMutexInit = 0;
00462 
00463 int wolfSSL_CryptHwMutexInit(void) {
00464     int ret = 0;
00465     if(wcCryptHwMutexInit == 0) {
00466         ret = wc_InitMutex(&wcCryptHwMutex);
00467         if(ret == 0) {
00468             wcCryptHwMutexInit = 1;
00469         }
00470     }
00471     return ret;
00472 }
00473 
00474 int wolfSSL_CryptHwMutexLock(void) {
00475     int ret = BAD_MUTEX_E;
00476 
00477     /* Make sure HW Mutex has been initialized */
00478     wolfSSL_CryptHwMutexInit();
00479 
00480     if(wcCryptHwMutexInit) {
00481         ret = wc_LockMutex(&wcCryptHwMutex);
00482     }
00483     return ret;
00484 }
00485 
00486 int wolfSSL_CryptHwMutexUnLock(void) {
00487     int ret = BAD_MUTEX_E;
00488 
00489     if(wcCryptHwMutexInit) {
00490         ret = wc_UnLockMutex(&wcCryptHwMutex);
00491     }
00492     return ret;
00493 }
00494 #endif /* WOLFSSL_CRYPT_HW_MUTEX */
00495 
00496 
00497 /* ---------------------------------------------------------------------------*/
00498 /* Mutex Ports */
00499 /* ---------------------------------------------------------------------------*/
00500 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
00501     static mutex_cb*     compat_mutex_cb = NULL;
00502 
00503     /* Function that locks or unlocks a mutex based on the flag passed in.
00504      *
00505      * flag lock or unlock i.e. CRYPTO_LOCK
00506      * type the type of lock to unlock or lock
00507      * file name of the file calling
00508      * line the line number from file calling
00509      */
00510     int wc_LockMutex_ex(int flag, int type, const char* file, int line)
00511     {
00512         if (compat_mutex_cb != NULL) {
00513             compat_mutex_cb(flag, type, file, line);
00514             return 0;
00515         }
00516         else {
00517             WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb");
00518             return BAD_STATE_E;
00519         }
00520     }
00521 
00522 
00523     /* Set the callback function to use for locking/unlocking mutex
00524      *
00525      * cb callback function to use
00526      */
00527     int wc_SetMutexCb(mutex_cb* cb)
00528     {
00529         compat_mutex_cb = cb;
00530         return 0;
00531     }
00532 #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */
00533 #ifdef SINGLE_THREADED
00534 
00535     int wc_InitMutex(wolfSSL_Mutex* m)
00536     {
00537         (void)m;
00538         return 0;
00539     }
00540 
00541     int wc_FreeMutex(wolfSSL_Mutex *m)
00542     {
00543         (void)m;
00544         return 0;
00545     }
00546 
00547 
00548     int wc_LockMutex(wolfSSL_Mutex *m)
00549     {
00550         (void)m;
00551         return 0;
00552     }
00553 
00554 
00555     int wc_UnLockMutex(wolfSSL_Mutex *m)
00556     {
00557         (void)m;
00558         return 0;
00559     }
00560 
00561 #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
00562   defined(FREESCALE_FREE_RTOS)
00563 
00564     int wc_InitMutex(wolfSSL_Mutex* m)
00565     {
00566         int iReturn;
00567 
00568         *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex();
00569         if( *m != NULL )
00570             iReturn = 0;
00571         else
00572             iReturn = BAD_MUTEX_E;
00573 
00574         return iReturn;
00575     }
00576 
00577     int wc_FreeMutex(wolfSSL_Mutex* m)
00578     {
00579         vSemaphoreDelete( *m );
00580         return 0;
00581     }
00582 
00583     int wc_LockMutex(wolfSSL_Mutex* m)
00584     {
00585         /* Assume an infinite block, or should there be zero block? */
00586         xSemaphoreTake( *m, portMAX_DELAY );
00587         return 0;
00588     }
00589 
00590     int wc_UnLockMutex(wolfSSL_Mutex* m)
00591     {
00592         xSemaphoreGive( *m );
00593         return 0;
00594     }
00595 
00596 #elif defined(WOLFSSL_SAFERTOS)
00597 
00598     int wc_InitMutex(wolfSSL_Mutex* m)
00599     {
00600         vSemaphoreCreateBinary(m->mutexBuffer, m->mutex);
00601         if (m->mutex == NULL)
00602             return BAD_MUTEX_E;
00603 
00604         return 0;
00605     }
00606 
00607     int wc_FreeMutex(wolfSSL_Mutex* m)
00608     {
00609         (void)m;
00610         return 0;
00611     }
00612 
00613     int wc_LockMutex(wolfSSL_Mutex* m)
00614     {
00615         /* Assume an infinite block */
00616         xSemaphoreTake(m->mutex, portMAX_DELAY);
00617         return 0;
00618     }
00619 
00620     int wc_UnLockMutex(wolfSSL_Mutex* m)
00621     {
00622         xSemaphoreGive(m->mutex);
00623         return 0;
00624     }
00625 
00626 #elif defined(USE_WINDOWS_API)
00627 
00628     int wc_InitMutex(wolfSSL_Mutex* m)
00629     {
00630         InitializeCriticalSection(m);
00631         return 0;
00632     }
00633 
00634 
00635     int wc_FreeMutex(wolfSSL_Mutex* m)
00636     {
00637         DeleteCriticalSection(m);
00638         return 0;
00639     }
00640 
00641 
00642     int wc_LockMutex(wolfSSL_Mutex* m)
00643     {
00644         EnterCriticalSection(m);
00645         return 0;
00646     }
00647 
00648 
00649     int wc_UnLockMutex(wolfSSL_Mutex* m)
00650     {
00651         LeaveCriticalSection(m);
00652         return 0;
00653     }
00654 
00655 #elif defined(WOLFSSL_PTHREADS)
00656 
00657     int wc_InitMutex(wolfSSL_Mutex* m)
00658     {
00659         if (pthread_mutex_init(m, 0) == 0)
00660             return 0;
00661         else
00662             return BAD_MUTEX_E;
00663     }
00664 
00665 
00666     int wc_FreeMutex(wolfSSL_Mutex* m)
00667     {
00668         if (pthread_mutex_destroy(m) == 0)
00669             return 0;
00670         else
00671             return BAD_MUTEX_E;
00672     }
00673 
00674 
00675     int wc_LockMutex(wolfSSL_Mutex* m)
00676     {
00677         if (pthread_mutex_lock(m) == 0)
00678             return 0;
00679         else
00680             return BAD_MUTEX_E;
00681     }
00682 
00683 
00684     int wc_UnLockMutex(wolfSSL_Mutex* m)
00685     {
00686         if (pthread_mutex_unlock(m) == 0)
00687             return 0;
00688         else
00689             return BAD_MUTEX_E;
00690     }
00691 
00692 #elif defined(THREADX)
00693 
00694     int wc_InitMutex(wolfSSL_Mutex* m)
00695     {
00696         if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0)
00697             return 0;
00698         else
00699             return BAD_MUTEX_E;
00700     }
00701 
00702 
00703     int wc_FreeMutex(wolfSSL_Mutex* m)
00704     {
00705         if (tx_mutex_delete(m) == 0)
00706             return 0;
00707         else
00708             return BAD_MUTEX_E;
00709     }
00710 
00711 
00712     int wc_LockMutex(wolfSSL_Mutex* m)
00713     {
00714         if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0)
00715             return 0;
00716         else
00717             return BAD_MUTEX_E;
00718     }
00719 
00720     int wc_UnLockMutex(wolfSSL_Mutex* m)
00721     {
00722         if (tx_mutex_put(m) == 0)
00723             return 0;
00724         else
00725             return BAD_MUTEX_E;
00726     }
00727 
00728 #elif defined(MICRIUM)
00729 
00730     int wc_InitMutex(wolfSSL_Mutex* m)
00731     {
00732         OS_ERR err;
00733 
00734         OSMutexCreate(m, "wolfSSL Mutex", &err);
00735 
00736         if (err == OS_ERR_NONE)
00737             return 0;
00738         else
00739             return BAD_MUTEX_E;
00740     }
00741 
00742     int wc_FreeMutex(wolfSSL_Mutex* m)
00743     {
00744         #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED)
00745             OS_ERR err;
00746 
00747             OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err);
00748 
00749             if (err == OS_ERR_NONE)
00750                 return 0;
00751             else
00752                 return BAD_MUTEX_E;
00753         #else
00754             return 0;
00755         #endif
00756     }
00757 
00758     int wc_LockMutex(wolfSSL_Mutex* m)
00759     {
00760         OS_ERR err;
00761 
00762         OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err);
00763 
00764         if (err == OS_ERR_NONE)
00765             return 0;
00766         else
00767             return BAD_MUTEX_E;
00768     }
00769 
00770     int wc_UnLockMutex(wolfSSL_Mutex* m)
00771     {
00772         OS_ERR err;
00773 
00774         OSMutexPost(m, OS_OPT_POST_NONE, &err);
00775 
00776         if (err == OS_ERR_NONE)
00777             return 0;
00778         else
00779             return BAD_MUTEX_E;
00780     }
00781 
00782 #elif defined(EBSNET)
00783 
00784     int wc_InitMutex(wolfSSL_Mutex* m)
00785     {
00786         if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1)
00787             return BAD_MUTEX_E;
00788         else
00789             return 0;
00790     }
00791 
00792     int wc_FreeMutex(wolfSSL_Mutex* m)
00793     {
00794         rtp_sig_mutex_free(*m);
00795         return 0;
00796     }
00797 
00798     int wc_LockMutex(wolfSSL_Mutex* m)
00799     {
00800         if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0)
00801             return 0;
00802         else
00803             return BAD_MUTEX_E;
00804     }
00805 
00806     int wc_UnLockMutex(wolfSSL_Mutex* m)
00807     {
00808         rtp_sig_mutex_release(*m);
00809         return 0;
00810     }
00811 
00812 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
00813 
00814     int wc_InitMutex(wolfSSL_Mutex* m)
00815     {
00816         if (_mutex_init(m, NULL) == MQX_EOK)
00817             return 0;
00818         else
00819             return BAD_MUTEX_E;
00820     }
00821 
00822     int wc_FreeMutex(wolfSSL_Mutex* m)
00823     {
00824         if (_mutex_destroy(m) == MQX_EOK)
00825             return 0;
00826         else
00827             return BAD_MUTEX_E;
00828     }
00829 
00830     int wc_LockMutex(wolfSSL_Mutex* m)
00831     {
00832         if (_mutex_lock(m) == MQX_EOK)
00833             return 0;
00834         else
00835             return BAD_MUTEX_E;
00836     }
00837 
00838     int wc_UnLockMutex(wolfSSL_Mutex* m)
00839     {
00840         if (_mutex_unlock(m) == MQX_EOK)
00841             return 0;
00842         else
00843             return BAD_MUTEX_E;
00844     }
00845 
00846 #elif defined(WOLFSSL_TIRTOS)
00847     #include <xdc/runtime/Error.h>
00848 
00849     int wc_InitMutex(wolfSSL_Mutex* m)
00850     {
00851         Semaphore_Params params;
00852         Error_Block eb;
00853 
00854         Error_init(&eb);
00855         Semaphore_Params_init(&params);
00856         params.mode = Semaphore_Mode_BINARY;
00857 
00858         *m = Semaphore_create(1, &params, &eb);
00859         if (Error_check(&eb)) {
00860             Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.",
00861                 NULL);
00862             return BAD_MUTEX_E;
00863         }
00864         else
00865             return 0;
00866     }
00867 
00868     int wc_FreeMutex(wolfSSL_Mutex* m)
00869     {
00870         Semaphore_delete(m);
00871 
00872         return 0;
00873     }
00874 
00875     int wc_LockMutex(wolfSSL_Mutex* m)
00876     {
00877         Semaphore_pend(*m, BIOS_WAIT_FOREVER);
00878 
00879         return 0;
00880     }
00881 
00882     int wc_UnLockMutex(wolfSSL_Mutex* m)
00883     {
00884         Semaphore_post(*m);
00885 
00886         return 0;
00887     }
00888 
00889 #elif defined(WOLFSSL_uITRON4)
00890 
00891     int wc_InitMutex(wolfSSL_Mutex* m)
00892     {
00893         int iReturn;
00894         m->sem.sematr  = TA_TFIFO;
00895         m->sem.isemcnt = 1;
00896         m->sem.maxsem  = 1;
00897         m->sem.name    = NULL;
00898 
00899         m->id = acre_sem(&m->sem);
00900         if( m->id != E_OK )
00901             iReturn = 0;
00902         else
00903             iReturn = BAD_MUTEX_E;
00904 
00905         return iReturn;
00906     }
00907 
00908     int wc_FreeMutex(wolfSSL_Mutex* m)
00909     {
00910         del_sem( m->id );
00911         return 0;
00912     }
00913 
00914     int wc_LockMutex(wolfSSL_Mutex* m)
00915     {
00916         wai_sem(m->id);
00917         return 0;
00918     }
00919 
00920     int wc_UnLockMutex(wolfSSL_Mutex* m)
00921     {
00922         sig_sem(m->id);
00923         return 0;
00924     }
00925 
00926     /****  uITRON malloc/free ***/
00927     static ID ID_wolfssl_MPOOL = 0;
00928     static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"};
00929 
00930     int uITRON4_minit(size_t poolsz) {
00931         ER ercd;
00932         wolfssl_MPOOL.mplsz = poolsz;
00933         ercd = acre_mpl(&wolfssl_MPOOL);
00934         if (ercd > 0) {
00935             ID_wolfssl_MPOOL = ercd;
00936             return 0;
00937         } else {
00938             return -1;
00939         }
00940     }
00941 
00942     void *uITRON4_malloc(size_t sz) {
00943         ER ercd;
00944         void *p;
00945         ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p);
00946         if (ercd == E_OK) {
00947             return p;
00948         } else {
00949             return 0;
00950         }
00951     }
00952 
00953     void *uITRON4_realloc(void *p, size_t sz) {
00954       ER ercd;
00955       void *newp;
00956       if(p) {
00957           ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp);
00958           if (ercd == E_OK) {
00959               XMEMCPY(newp, p, sz);
00960               ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00961               if (ercd == E_OK) {
00962                   return newp;
00963               }
00964           }
00965       }
00966       return 0;
00967     }
00968 
00969     void uITRON4_free(void *p) {
00970         ER ercd;
00971         ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p);
00972         if (ercd == E_OK) {
00973             return;
00974         } else {
00975             return;
00976         }
00977     }
00978 
00979 #elif defined(WOLFSSL_uTKERNEL2)
00980 
00981     int wc_InitMutex(wolfSSL_Mutex* m)
00982     {
00983         int iReturn;
00984         m->sem.sematr  = TA_TFIFO;
00985         m->sem.isemcnt = 1;
00986         m->sem.maxsem  = 1;
00987 
00988         m->id = tk_cre_sem(&m->sem);
00989         if( m->id != NULL )
00990             iReturn = 0;
00991         else
00992             iReturn = BAD_MUTEX_E;
00993 
00994         return iReturn;
00995     }
00996 
00997     int wc_FreeMutex(wolfSSL_Mutex* m)
00998     {
00999         tk_del_sem(m->id);
01000         return 0;
01001     }
01002 
01003     int wc_LockMutex(wolfSSL_Mutex* m)
01004     {
01005         tk_wai_sem(m->id, 1, TMO_FEVR);
01006         return 0;
01007     }
01008 
01009     int wc_UnLockMutex(wolfSSL_Mutex* m)
01010     {
01011         tk_sig_sem(m->id, 1);
01012         return 0;
01013     }
01014 
01015     /****  uT-Kernel malloc/free ***/
01016     static ID ID_wolfssl_MPOOL = 0;
01017     static T_CMPL wolfssl_MPOOL = {
01018         NULL,       /* Extended information */
01019         TA_TFIFO,   /* Memory pool attribute */
01020         0,          /* Size of whole memory pool (byte) */
01021         "wolfSSL"   /* Object name (max 8-char) */
01022     };
01023 
01024     int uTKernel_init_mpool(unsigned int sz) {
01025         ER ercd;
01026         wolfssl_MPOOL.mplsz = sz;
01027         ercd = tk_cre_mpl(&wolfssl_MPOOL);
01028         if (ercd > 0) {
01029             ID_wolfssl_MPOOL = ercd;
01030             return 0;
01031         } else {
01032             return (int)ercd;
01033         }
01034     }
01035 
01036     void *uTKernel_malloc(unsigned int sz) {
01037         ER ercd;
01038         void *p;
01039         ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR);
01040         if (ercd == E_OK) {
01041             return p;
01042         } else {
01043             return 0;
01044         }
01045     }
01046 
01047     void *uTKernel_realloc(void *p, unsigned int sz) {
01048       ER ercd;
01049       void *newp;
01050       if (p) {
01051           ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR);
01052           if (ercd == E_OK) {
01053               XMEMCPY(newp, p, sz);
01054               ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
01055               if (ercd == E_OK) {
01056                   return newp;
01057               }
01058           }
01059       }
01060       return 0;
01061     }
01062 
01063     void uTKernel_free(void *p) {
01064         ER ercd;
01065         ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p);
01066         if (ercd == E_OK) {
01067             return;
01068         } else {
01069             return;
01070         }
01071     }
01072 
01073 #elif defined (WOLFSSL_FROSTED)
01074 
01075     int wc_InitMutex(wolfSSL_Mutex* m)
01076     {
01077         *m = mutex_init();
01078         if (*m)
01079             return 0;
01080         else
01081             return -1;
01082     }
01083 
01084     int wc_FreeMutex(wolfSSL_Mutex* m)
01085     {
01086         mutex_destroy(*m);
01087         return(0);
01088     }
01089 
01090     int wc_LockMutex(wolfSSL_Mutex* m)
01091     {
01092         mutex_lock(*m);
01093         return 0;
01094     }
01095 
01096     int wc_UnLockMutex(wolfSSL_Mutex* m)
01097     {
01098         mutex_unlock(*m);
01099         return 0;
01100     }
01101 
01102 #elif defined(WOLFSSL_CMSIS_RTOS)
01103 
01104     #define CMSIS_NMUTEX 10
01105     osMutexDef(wolfSSL_mt0);  osMutexDef(wolfSSL_mt1);  osMutexDef(wolfSSL_mt2);
01106     osMutexDef(wolfSSL_mt3);  osMutexDef(wolfSSL_mt4);  osMutexDef(wolfSSL_mt5);
01107     osMutexDef(wolfSSL_mt6);  osMutexDef(wolfSSL_mt7);  osMutexDef(wolfSSL_mt8);
01108     osMutexDef(wolfSSL_mt9);
01109 
01110     static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0),
01111         osMutex(wolfSSL_mt1),    osMutex(wolfSSL_mt2),   osMutex(wolfSSL_mt3),
01112         osMutex(wolfSSL_mt4),    osMutex(wolfSSL_mt5),   osMutex(wolfSSL_mt6),
01113         osMutex(wolfSSL_mt7),    osMutex(wolfSSL_mt8),   osMutex(wolfSSL_mt9) };
01114 
01115     static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0};
01116 
01117     int wc_InitMutex(wolfSSL_Mutex* m)
01118     {
01119         int i;
01120         for (i=0; i<CMSIS_NMUTEX; i++) {
01121             if(CMSIS_mutexID[i] == 0) {
01122                 CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]);
01123                 (*m) = CMSIS_mutexID[i];
01124             return 0;
01125             }
01126         }
01127         return -1;
01128     }
01129 
01130     int wc_FreeMutex(wolfSSL_Mutex* m)
01131     {
01132         int i;
01133         osMutexDelete   (*m);
01134         for (i=0; i<CMSIS_NMUTEX; i++) {
01135             if(CMSIS_mutexID[i] == (*m)) {
01136                 CMSIS_mutexID[i] = 0;
01137                 return(0);
01138             }
01139         }
01140         return(-1);
01141     }
01142 
01143     int wc_LockMutex(wolfSSL_Mutex* m)
01144     {
01145         osMutexWait(*m, osWaitForever);
01146         return(0);
01147     }
01148 
01149     int wc_UnLockMutex(wolfSSL_Mutex* m)
01150     {
01151         osMutexRelease (*m);
01152         return 0;
01153     }
01154 
01155 #elif defined(WOLFSSL_MDK_ARM)
01156 
01157     int wc_InitMutex(wolfSSL_Mutex* m)
01158     {
01159         os_mut_init (m);
01160         return 0;
01161     }
01162 
01163     int wc_FreeMutex(wolfSSL_Mutex* m)
01164     {
01165         return(0);
01166     }
01167 
01168     int wc_LockMutex(wolfSSL_Mutex* m)
01169     {
01170         os_mut_wait (m, 0xffff);
01171         return(0);
01172     }
01173 
01174     int wc_UnLockMutex(wolfSSL_Mutex* m)
01175     {
01176         os_mut_release (m);
01177         return 0;
01178     }
01179 
01180 #elif defined(INTIME_RTOS)
01181 
01182     int wc_InitMutex(wolfSSL_Mutex* m)
01183     {
01184         int ret = 0;
01185 
01186         if (m == NULL)
01187             return BAD_FUNC_ARG;
01188 
01189         *m = CreateRtSemaphore(
01190             1,                      /* initial unit count */
01191             1,                      /* maximum unit count */
01192             PRIORITY_QUEUING        /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */
01193         );
01194         if (*m == BAD_RTHANDLE) {
01195             ret = GetLastRtError();
01196             if (ret != E_OK)
01197                 ret = BAD_MUTEX_E;
01198         }
01199         return ret;
01200     }
01201 
01202     int wc_FreeMutex(wolfSSL_Mutex* m)
01203     {
01204         int ret = 0;
01205         BOOLEAN del;
01206 
01207         if (m == NULL)
01208             return BAD_FUNC_ARG;
01209 
01210         del = DeleteRtSemaphore(
01211             *m                      /* handle for RT semaphore */
01212         );
01213         if (del != TRUE)
01214             ret = BAD_MUTEX_E;
01215 
01216         return ret;
01217     }
01218 
01219     int wc_LockMutex(wolfSSL_Mutex* m)
01220     {
01221         int ret = 0;
01222         DWORD lck;
01223 
01224         if (m == NULL)
01225             return BAD_FUNC_ARG;
01226 
01227         lck = WaitForRtSemaphore(
01228             *m,                     /* handle for RT semaphore */
01229             1,                      /* number of units to wait for */
01230             WAIT_FOREVER            /* number of milliseconds to wait for units */
01231         );
01232         if (lck == WAIT_FAILED) {
01233             ret = GetLastRtError();
01234             if (ret != E_OK)
01235                 ret = BAD_MUTEX_E;
01236         }
01237         return ret;
01238     }
01239 
01240     int wc_UnLockMutex(wolfSSL_Mutex* m)
01241     {
01242         int ret = 0;
01243         BOOLEAN rel;
01244 
01245         if (m == NULL)
01246             return BAD_FUNC_ARG;
01247 
01248         rel = ReleaseRtSemaphore(
01249             *m,                     /* handle for RT semaphore */
01250             1                       /* number of units to release to semaphore */
01251         );
01252         if (rel != TRUE)
01253             ret = BAD_MUTEX_E;
01254 
01255         return ret;
01256     }
01257 
01258 #elif defined(WOLFSSL_NUCLEUS_1_2)
01259 
01260     int wc_InitMutex(wolfSSL_Mutex* m)
01261     {
01262         /* Call the Nucleus function to create the semaphore */
01263         if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1,
01264                                 NU_PRIORITY) == NU_SUCCESS) {
01265             return 0;
01266         }
01267 
01268         return BAD_MUTEX_E;
01269     }
01270 
01271     int wc_FreeMutex(wolfSSL_Mutex* m)
01272     {
01273         if (NU_Delete_Semaphore(m) == NU_SUCCESS)
01274             return 0;
01275 
01276         return BAD_MUTEX_E;
01277     }
01278 
01279     int wc_LockMutex(wolfSSL_Mutex* m)
01280     {
01281         /* passing suspend task option */
01282         if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS)
01283             return 0;
01284 
01285         return BAD_MUTEX_E;
01286     }
01287 
01288     int wc_UnLockMutex(wolfSSL_Mutex* m)
01289     {
01290         if (NU_Release_Semaphore(m) == NU_SUCCESS)
01291             return 0;
01292 
01293         return BAD_MUTEX_E;
01294     }
01295 
01296 #else
01297     #warning No mutex handling defined
01298 
01299 #endif
01300 
01301 #ifndef NO_ASN_TIME
01302 #if defined(_WIN32_WCE)
01303 time_t windows_time(time_t* timer)
01304 {
01305     SYSTEMTIME     sysTime;
01306     FILETIME       fTime;
01307     ULARGE_INTEGER intTime;
01308     time_t         localTime;
01309 
01310     if (timer == NULL)
01311         timer = &localTime;
01312 
01313     GetSystemTime(&sysTime);
01314     SystemTimeToFileTime(&sysTime, &fTime);
01315 
01316     XMEMCPY(&intTime, &fTime, sizeof(FILETIME));
01317     /* subtract EPOCH */
01318     intTime.QuadPart -= 0x19db1ded53e8000;
01319     /* to secs */
01320     intTime.QuadPart /= 10000000;
01321     *timer = (time_t)intTime.QuadPart;
01322 
01323     return *timer;
01324 }
01325 #endif /*  _WIN32_WCE */
01326 
01327 #if defined(WOLFSSL_GMTIME)
01328 struct tm* gmtime(const time_t* timer)
01329 {
01330     #define YEAR0          1900
01331     #define EPOCH_YEAR     1970
01332     #define SECS_DAY       (24L * 60L * 60L)
01333     #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400)))
01334     #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365)
01335 
01336     static const int _ytab[2][12] =
01337     {
01338         {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
01339         {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
01340     };
01341 
01342     static struct tm st_time;
01343     struct tm* ret = &st_time;
01344     time_t secs = *timer;
01345     unsigned long dayclock, dayno;
01346     int year = EPOCH_YEAR;
01347 
01348     dayclock = (unsigned long)secs % SECS_DAY;
01349     dayno    = (unsigned long)secs / SECS_DAY;
01350 
01351     ret->tm_sec  = (int) dayclock % 60;
01352     ret->tm_min  = (int)(dayclock % 3600) / 60;
01353     ret->tm_hour = (int) dayclock / 3600;
01354     ret->tm_wday = (int) (dayno + 4) % 7;        /* day 0 a Thursday */
01355 
01356     while(dayno >= (unsigned long)YEARSIZE(year)) {
01357         dayno -= YEARSIZE(year);
01358         year++;
01359     }
01360 
01361     ret->tm_year = year - YEAR0;
01362     ret->tm_yday = (int)dayno;
01363     ret->tm_mon  = 0;
01364 
01365     while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) {
01366         dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon];
01367         ret->tm_mon++;
01368     }
01369 
01370     ret->tm_mday  = (int)++dayno;
01371     ret->tm_isdst = 0;
01372 
01373     return ret;
01374 }
01375 #endif /* WOLFSSL_GMTIME */
01376 
01377 
01378 #if defined(HAVE_RTP_SYS)
01379 #define YEAR0          1900
01380 
01381 struct tm* rtpsys_gmtime(const time_t* timer)       /* has a gmtime() but hangs */
01382 {
01383     static struct tm st_time;
01384     struct tm* ret = &st_time;
01385 
01386     DC_RTC_CALENDAR cal;
01387     dc_rtc_time_get(&cal, TRUE);
01388 
01389     ret->tm_year  = cal.year - YEAR0;       /* gm starts at 1900 */
01390     ret->tm_mon   = cal.month - 1;          /* gm starts at 0 */
01391     ret->tm_mday  = cal.day;
01392     ret->tm_hour  = cal.hour;
01393     ret->tm_min   = cal.minute;
01394     ret->tm_sec   = cal.second;
01395 
01396     return ret;
01397 }
01398 
01399 #endif /* HAVE_RTP_SYS */
01400 
01401 
01402 #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
01403 
01404 /*
01405  * time() is just a stub in Microchip libraries. We need our own
01406  * implementation. Use SNTP client to get seconds since epoch.
01407  */
01408 time_t pic32_time(time_t* timer)
01409 {
01410 #ifdef MICROCHIP_TCPIP_V5
01411     DWORD sec = 0;
01412 #else
01413     uint32_t sec = 0;
01414 #endif
01415     time_t localTime;
01416 
01417     if (timer == NULL)
01418         timer = &localTime;
01419 
01420 #ifdef MICROCHIP_MPLAB_HARMONY
01421     sec = TCPIP_SNTP_UTCSecondsGet();
01422 #else
01423     sec = SNTPGetUTCSeconds();
01424 #endif
01425     *timer = (time_t) sec;
01426 
01427     return *timer;
01428 }
01429 
01430 #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */
01431 
01432 #if defined(MICRIUM)
01433 
01434 time_t micrium_time(time_t* timer)
01435 {
01436     CLK_TS_SEC sec;
01437 
01438     Clk_GetTS_Unix(&sec);
01439 
01440     return (time_t) sec;
01441 }
01442 
01443 #endif /* MICRIUM */
01444 
01445 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
01446 
01447 time_t mqx_time(time_t* timer)
01448 {
01449     time_t localTime;
01450     TIME_STRUCT time_s;
01451 
01452     if (timer == NULL)
01453         timer = &localTime;
01454 
01455     _time_get(&time_s);
01456     *timer = (time_t) time_s.SECONDS;
01457 
01458     return *timer;
01459 }
01460 
01461 #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */
01462 
01463 
01464 #if defined(WOLFSSL_TIRTOS)
01465 
01466 time_t XTIME(time_t * timer)
01467 {
01468     time_t sec = 0;
01469 
01470     sec = (time_t) Seconds_get();
01471 
01472     if (timer != NULL)
01473         *timer = sec;
01474 
01475     return sec;
01476 }
01477 
01478 #endif /* WOLFSSL_TIRTOS */
01479 
01480 #if defined(WOLFSSL_XILINX)
01481 #include "xrtcpsu.h"
01482 
01483 time_t XTIME(time_t * timer)
01484 {
01485     time_t sec = 0;
01486     XRtcPsu_Config* con;
01487     XRtcPsu         rtc;
01488 
01489     con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID);
01490     if (con != NULL) {
01491         if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) {
01492             sec = (time_t)XRtcPsu_GetCurrentTime(&rtc);
01493         }
01494         else {
01495             WOLFSSL_MSG("Unable to initialize RTC");
01496         }
01497     }
01498 
01499     if (timer != NULL)
01500         *timer = sec;
01501 
01502     return sec;
01503 }
01504 
01505 #endif /* WOLFSSL_XILINX */
01506 #endif /* !NO_ASN_TIME */
01507 
01508 #ifndef WOLFSSL_LEANPSK
01509 char* mystrnstr(const char* s1, const char* s2, unsigned int n)
01510 {
01511     unsigned int s2_len = (unsigned int)XSTRLEN(s2);
01512 
01513     if (s2_len == 0)
01514         return (char*)s1;
01515 
01516     while (n >= s2_len && s1[0]) {
01517         if (s1[0] == s2[0])
01518             if (XMEMCMP(s1, s2, s2_len) == 0)
01519                 return (char*)s1;
01520         s1++;
01521         n--;
01522     }
01523 
01524     return NULL;
01525 }
01526 #endif
01527 
01528 /* custom memory wrappers */
01529 #ifdef WOLFSSL_NUCLEUS_1_2
01530 
01531     /* system memory pool */
01532     extern NU_MEMORY_POOL System_Memory;
01533 
01534     void* nucleus_malloc(unsigned long size, void* heap, int type)
01535     {
01536         STATUS status;
01537         void*  stack_ptr;
01538 
01539         status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size,
01540                                     NU_NO_SUSPEND);
01541         if (status == NU_SUCCESS) {
01542             return 0;
01543         } else {
01544             return stack_ptr;
01545         }
01546     }
01547 
01548     void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type)
01549     {
01550         STATUS     status;
01551         DM_HEADER* old_header;
01552         word32     old_size, copy_size;
01553         void*      new_mem;
01554 
01555         /* if ptr is NULL, behave like malloc */
01556         new_mem = nucleus_malloc(size, NULL, 0);
01557         if (new_mem == 0 || ptr == 0) {
01558             return new_mem;
01559         }
01560 
01561         /* calculate old memory block size */
01562         /* mem pointers stored in block headers (ref dm_defs.h) */
01563         old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD);
01564         old_size   = (byte*)old_header->dm_next_memory - (byte*)ptr;
01565 
01566         /* copy old to new */
01567         if (old_size < size) {
01568             copy_size = old_size;
01569         } else {
01570             copy_size = size;
01571         }
01572         XMEMCPY(new_mem, ptr, copy_size);
01573 
01574         /* free old */
01575         nucleus_free(ptr, NULL, 0);
01576 
01577         return new_mem;
01578     }
01579 
01580     void nucleus_free(void* ptr, void* heap, int type)
01581     {
01582         if (ptr != NULL)
01583             NU_Deallocate_Memory(ptr);
01584     }
01585 
01586 #endif /* WOLFSSL_NUCLEUS_1_2 */
01587 
01588 #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH)
01589     #include <wolfcrypt/src/port/ti/ti-ccm.c>  /* initialize and Mutex for TI Crypt Engine */
01590     #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */
01591 #endif
01592