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.
src/wc_port.c@0:1387ff3eed4a, 2019-11-20 (annotated)
- Committer:
- sPymbed
- Date:
- Wed Nov 20 13:28:01 2019 +0000
- Revision:
- 0:1387ff3eed4a
initial version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sPymbed | 0:1387ff3eed4a | 1 | /* port.c |
| sPymbed | 0:1387ff3eed4a | 2 | * |
| sPymbed | 0:1387ff3eed4a | 3 | * Copyright (C) 2006-2017 wolfSSL Inc. |
| sPymbed | 0:1387ff3eed4a | 4 | * |
| sPymbed | 0:1387ff3eed4a | 5 | * This file is part of wolfSSL. |
| sPymbed | 0:1387ff3eed4a | 6 | * |
| sPymbed | 0:1387ff3eed4a | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
| sPymbed | 0:1387ff3eed4a | 8 | * it under the terms of the GNU General Public License as published by |
| sPymbed | 0:1387ff3eed4a | 9 | * the Free Software Foundation; either version 2 of the License, or |
| sPymbed | 0:1387ff3eed4a | 10 | * (at your option) any later version. |
| sPymbed | 0:1387ff3eed4a | 11 | * |
| sPymbed | 0:1387ff3eed4a | 12 | * wolfSSL is distributed in the hope that it will be useful, |
| sPymbed | 0:1387ff3eed4a | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| sPymbed | 0:1387ff3eed4a | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| sPymbed | 0:1387ff3eed4a | 15 | * GNU General Public License for more details. |
| sPymbed | 0:1387ff3eed4a | 16 | * |
| sPymbed | 0:1387ff3eed4a | 17 | * You should have received a copy of the GNU General Public License |
| sPymbed | 0:1387ff3eed4a | 18 | * along with this program; if not, write to the Free Software |
| sPymbed | 0:1387ff3eed4a | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
| sPymbed | 0:1387ff3eed4a | 20 | */ |
| sPymbed | 0:1387ff3eed4a | 21 | |
| sPymbed | 0:1387ff3eed4a | 22 | |
| sPymbed | 0:1387ff3eed4a | 23 | #ifdef HAVE_CONFIG_H |
| sPymbed | 0:1387ff3eed4a | 24 | #include <config.h> |
| sPymbed | 0:1387ff3eed4a | 25 | #endif |
| sPymbed | 0:1387ff3eed4a | 26 | |
| sPymbed | 0:1387ff3eed4a | 27 | #include <wolfcrypt/settings.h> |
| sPymbed | 0:1387ff3eed4a | 28 | #include <wolfcrypt/types.h> |
| sPymbed | 0:1387ff3eed4a | 29 | #include <wolfcrypt/error-crypt.h> |
| sPymbed | 0:1387ff3eed4a | 30 | #include <wolfcrypt/logging.h> |
| sPymbed | 0:1387ff3eed4a | 31 | #include <wolfcrypt/wc_port.h> |
| sPymbed | 0:1387ff3eed4a | 32 | #ifdef HAVE_ECC |
| sPymbed | 0:1387ff3eed4a | 33 | #include <wolfcrypt/ecc.h> |
| sPymbed | 0:1387ff3eed4a | 34 | #endif |
| sPymbed | 0:1387ff3eed4a | 35 | #ifdef WOLFSSL_ASYNC_CRYPT |
| sPymbed | 0:1387ff3eed4a | 36 | #include <wolfcrypt/async.h> |
| sPymbed | 0:1387ff3eed4a | 37 | #endif |
| sPymbed | 0:1387ff3eed4a | 38 | |
| sPymbed | 0:1387ff3eed4a | 39 | /* IPP header files for library initialization */ |
| sPymbed | 0:1387ff3eed4a | 40 | #ifdef HAVE_FAST_RSA |
| sPymbed | 0:1387ff3eed4a | 41 | #include <ipp.h> |
| sPymbed | 0:1387ff3eed4a | 42 | #include <ippcp.h> |
| sPymbed | 0:1387ff3eed4a | 43 | #endif |
| sPymbed | 0:1387ff3eed4a | 44 | |
| sPymbed | 0:1387ff3eed4a | 45 | #ifdef FREESCALE_LTC_TFM |
| sPymbed | 0:1387ff3eed4a | 46 | #include <wolfcrypt/port/nxp/ksdk_port.h> |
| sPymbed | 0:1387ff3eed4a | 47 | #endif |
| sPymbed | 0:1387ff3eed4a | 48 | |
| sPymbed | 0:1387ff3eed4a | 49 | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) |
| sPymbed | 0:1387ff3eed4a | 50 | #include <wolfcrypt/port/atmel/atmel.h> |
| sPymbed | 0:1387ff3eed4a | 51 | #endif |
| sPymbed | 0:1387ff3eed4a | 52 | |
| sPymbed | 0:1387ff3eed4a | 53 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
| sPymbed | 0:1387ff3eed4a | 54 | #include <wolfssl/openssl/evp.h> |
| sPymbed | 0:1387ff3eed4a | 55 | #endif |
| sPymbed | 0:1387ff3eed4a | 56 | |
| sPymbed | 0:1387ff3eed4a | 57 | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) |
| sPymbed | 0:1387ff3eed4a | 58 | #include <wolfcrypt/memory.h> |
| sPymbed | 0:1387ff3eed4a | 59 | #include <wolfcrypt/mem_track.h> |
| sPymbed | 0:1387ff3eed4a | 60 | #endif |
| sPymbed | 0:1387ff3eed4a | 61 | |
| sPymbed | 0:1387ff3eed4a | 62 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
| sPymbed | 0:1387ff3eed4a | 63 | defined(WOLFSSL_IMX6_CAAM_BLOB) |
| sPymbed | 0:1387ff3eed4a | 64 | #include <wolfcrypt/port/caam/wolfcaam.h> |
| sPymbed | 0:1387ff3eed4a | 65 | #endif |
| sPymbed | 0:1387ff3eed4a | 66 | |
| sPymbed | 0:1387ff3eed4a | 67 | #ifdef WOLF_CRYPTO_DEV |
| sPymbed | 0:1387ff3eed4a | 68 | #include <wolfcrypt/cryptodev.h> |
| sPymbed | 0:1387ff3eed4a | 69 | #endif |
| sPymbed | 0:1387ff3eed4a | 70 | |
| sPymbed | 0:1387ff3eed4a | 71 | #ifdef _MSC_VER |
| sPymbed | 0:1387ff3eed4a | 72 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
| sPymbed | 0:1387ff3eed4a | 73 | #pragma warning(disable: 4996) |
| sPymbed | 0:1387ff3eed4a | 74 | #endif |
| sPymbed | 0:1387ff3eed4a | 75 | |
| sPymbed | 0:1387ff3eed4a | 76 | /* prevent multiple mutex initializations */ |
| sPymbed | 0:1387ff3eed4a | 77 | static volatile int initRefCount = 0; |
| sPymbed | 0:1387ff3eed4a | 78 | |
| sPymbed | 0:1387ff3eed4a | 79 | /* Used to initialize state for wolfcrypt |
| sPymbed | 0:1387ff3eed4a | 80 | return 0 on success |
| sPymbed | 0:1387ff3eed4a | 81 | */ |
| sPymbed | 0:1387ff3eed4a | 82 | int wolfCrypt_Init(void) |
| sPymbed | 0:1387ff3eed4a | 83 | { |
| sPymbed | 0:1387ff3eed4a | 84 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 85 | |
| sPymbed | 0:1387ff3eed4a | 86 | if (initRefCount == 0) { |
| sPymbed | 0:1387ff3eed4a | 87 | WOLFSSL_ENTER("wolfCrypt_Init"); |
| sPymbed | 0:1387ff3eed4a | 88 | |
| sPymbed | 0:1387ff3eed4a | 89 | #ifdef WOLF_CRYPTO_DEV |
| sPymbed | 0:1387ff3eed4a | 90 | wc_CryptoDev_Init(); |
| sPymbed | 0:1387ff3eed4a | 91 | #endif |
| sPymbed | 0:1387ff3eed4a | 92 | |
| sPymbed | 0:1387ff3eed4a | 93 | #ifdef WOLFSSL_ASYNC_CRYPT |
| sPymbed | 0:1387ff3eed4a | 94 | ret = wolfAsync_HardwareStart(); |
| sPymbed | 0:1387ff3eed4a | 95 | if (ret != 0) { |
| sPymbed | 0:1387ff3eed4a | 96 | WOLFSSL_MSG("Async hardware start failed"); |
| sPymbed | 0:1387ff3eed4a | 97 | /* don't return failure, allow operation to continue */ |
| sPymbed | 0:1387ff3eed4a | 98 | } |
| sPymbed | 0:1387ff3eed4a | 99 | #endif |
| sPymbed | 0:1387ff3eed4a | 100 | |
| sPymbed | 0:1387ff3eed4a | 101 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
| sPymbed | 0:1387ff3eed4a | 102 | ret = InitMemoryTracker(); |
| sPymbed | 0:1387ff3eed4a | 103 | if (ret != 0) { |
| sPymbed | 0:1387ff3eed4a | 104 | WOLFSSL_MSG("InitMemoryTracker failed"); |
| sPymbed | 0:1387ff3eed4a | 105 | return ret; |
| sPymbed | 0:1387ff3eed4a | 106 | } |
| sPymbed | 0:1387ff3eed4a | 107 | #endif |
| sPymbed | 0:1387ff3eed4a | 108 | |
| sPymbed | 0:1387ff3eed4a | 109 | #if WOLFSSL_CRYPT_HW_MUTEX |
| sPymbed | 0:1387ff3eed4a | 110 | /* If crypto hardware mutex protection is enabled, then initialize it */ |
| sPymbed | 0:1387ff3eed4a | 111 | ret = wolfSSL_CryptHwMutexInit(); |
| sPymbed | 0:1387ff3eed4a | 112 | if (ret != 0) { |
| sPymbed | 0:1387ff3eed4a | 113 | WOLFSSL_MSG("Hw crypt mutex init failed"); |
| sPymbed | 0:1387ff3eed4a | 114 | return ret; |
| sPymbed | 0:1387ff3eed4a | 115 | } |
| sPymbed | 0:1387ff3eed4a | 116 | #endif |
| sPymbed | 0:1387ff3eed4a | 117 | |
| sPymbed | 0:1387ff3eed4a | 118 | /* if defined have fast RSA then initialize Intel IPP */ |
| sPymbed | 0:1387ff3eed4a | 119 | #ifdef HAVE_FAST_RSA |
| sPymbed | 0:1387ff3eed4a | 120 | WOLFSSL_MSG("Attempting to use optimized IPP Library"); |
| sPymbed | 0:1387ff3eed4a | 121 | if ((ret = ippInit()) != ippStsNoErr) { |
| sPymbed | 0:1387ff3eed4a | 122 | /* possible to get a CPU feature support status on optimized IPP |
| sPymbed | 0:1387ff3eed4a | 123 | library but still use default library and see competitive speeds */ |
| sPymbed | 0:1387ff3eed4a | 124 | WOLFSSL_MSG("Warning when trying to set up optimization"); |
| sPymbed | 0:1387ff3eed4a | 125 | WOLFSSL_MSG(ippGetStatusString(ret)); |
| sPymbed | 0:1387ff3eed4a | 126 | WOLFSSL_MSG("Using default fast IPP library"); |
| sPymbed | 0:1387ff3eed4a | 127 | ret = 0; |
| sPymbed | 0:1387ff3eed4a | 128 | (void)ret; /* suppress not read warning */ |
| sPymbed | 0:1387ff3eed4a | 129 | } |
| sPymbed | 0:1387ff3eed4a | 130 | #endif |
| sPymbed | 0:1387ff3eed4a | 131 | |
| sPymbed | 0:1387ff3eed4a | 132 | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC) |
| sPymbed | 0:1387ff3eed4a | 133 | ret = ksdk_port_init(); |
| sPymbed | 0:1387ff3eed4a | 134 | if (ret != 0) { |
| sPymbed | 0:1387ff3eed4a | 135 | WOLFSSL_MSG("KSDK port init failed"); |
| sPymbed | 0:1387ff3eed4a | 136 | return ret; |
| sPymbed | 0:1387ff3eed4a | 137 | } |
| sPymbed | 0:1387ff3eed4a | 138 | #endif |
| sPymbed | 0:1387ff3eed4a | 139 | |
| sPymbed | 0:1387ff3eed4a | 140 | #if defined(WOLFSSL_ATMEL) || defined(WOLFSSL_ATECC508A) |
| sPymbed | 0:1387ff3eed4a | 141 | atmel_init(); |
| sPymbed | 0:1387ff3eed4a | 142 | #endif |
| sPymbed | 0:1387ff3eed4a | 143 | |
| sPymbed | 0:1387ff3eed4a | 144 | #ifdef WOLFSSL_ARMASM |
| sPymbed | 0:1387ff3eed4a | 145 | WOLFSSL_MSG("Using ARM hardware acceleration"); |
| sPymbed | 0:1387ff3eed4a | 146 | #endif |
| sPymbed | 0:1387ff3eed4a | 147 | |
| sPymbed | 0:1387ff3eed4a | 148 | #if !defined(WOLFCRYPT_ONLY) && \ |
| sPymbed | 0:1387ff3eed4a | 149 | ( defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) ) |
| sPymbed | 0:1387ff3eed4a | 150 | wolfSSL_EVP_init(); |
| sPymbed | 0:1387ff3eed4a | 151 | #endif |
| sPymbed | 0:1387ff3eed4a | 152 | |
| sPymbed | 0:1387ff3eed4a | 153 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
| sPymbed | 0:1387ff3eed4a | 154 | if ((ret = wc_LoggingInit()) != 0) { |
| sPymbed | 0:1387ff3eed4a | 155 | WOLFSSL_MSG("Error creating logging mutex"); |
| sPymbed | 0:1387ff3eed4a | 156 | return ret; |
| sPymbed | 0:1387ff3eed4a | 157 | } |
| sPymbed | 0:1387ff3eed4a | 158 | #endif |
| sPymbed | 0:1387ff3eed4a | 159 | |
| sPymbed | 0:1387ff3eed4a | 160 | #ifdef HAVE_ECC |
| sPymbed | 0:1387ff3eed4a | 161 | #ifdef ECC_CACHE_CURVE |
| sPymbed | 0:1387ff3eed4a | 162 | if ((ret = wc_ecc_curve_cache_init()) != 0) { |
| sPymbed | 0:1387ff3eed4a | 163 | WOLFSSL_MSG("Error creating curve cache"); |
| sPymbed | 0:1387ff3eed4a | 164 | return ret; |
| sPymbed | 0:1387ff3eed4a | 165 | } |
| sPymbed | 0:1387ff3eed4a | 166 | #endif |
| sPymbed | 0:1387ff3eed4a | 167 | #endif |
| sPymbed | 0:1387ff3eed4a | 168 | |
| sPymbed | 0:1387ff3eed4a | 169 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
| sPymbed | 0:1387ff3eed4a | 170 | defined(WOLFSSL_IMX6_CAAM_BLOB) |
| sPymbed | 0:1387ff3eed4a | 171 | if ((ret = wc_caamInit()) != 0) { |
| sPymbed | 0:1387ff3eed4a | 172 | return ret; |
| sPymbed | 0:1387ff3eed4a | 173 | } |
| sPymbed | 0:1387ff3eed4a | 174 | #endif |
| sPymbed | 0:1387ff3eed4a | 175 | |
| sPymbed | 0:1387ff3eed4a | 176 | initRefCount = 1; |
| sPymbed | 0:1387ff3eed4a | 177 | } |
| sPymbed | 0:1387ff3eed4a | 178 | |
| sPymbed | 0:1387ff3eed4a | 179 | return ret; |
| sPymbed | 0:1387ff3eed4a | 180 | } |
| sPymbed | 0:1387ff3eed4a | 181 | |
| sPymbed | 0:1387ff3eed4a | 182 | |
| sPymbed | 0:1387ff3eed4a | 183 | /* return success value is the same as wolfCrypt_Init */ |
| sPymbed | 0:1387ff3eed4a | 184 | int wolfCrypt_Cleanup(void) |
| sPymbed | 0:1387ff3eed4a | 185 | { |
| sPymbed | 0:1387ff3eed4a | 186 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 187 | |
| sPymbed | 0:1387ff3eed4a | 188 | if (initRefCount == 1) { |
| sPymbed | 0:1387ff3eed4a | 189 | WOLFSSL_ENTER("wolfCrypt_Cleanup"); |
| sPymbed | 0:1387ff3eed4a | 190 | |
| sPymbed | 0:1387ff3eed4a | 191 | #ifdef HAVE_ECC |
| sPymbed | 0:1387ff3eed4a | 192 | #ifdef FP_ECC |
| sPymbed | 0:1387ff3eed4a | 193 | wc_ecc_fp_free(); |
| sPymbed | 0:1387ff3eed4a | 194 | #endif |
| sPymbed | 0:1387ff3eed4a | 195 | #ifdef ECC_CACHE_CURVE |
| sPymbed | 0:1387ff3eed4a | 196 | wc_ecc_curve_cache_free(); |
| sPymbed | 0:1387ff3eed4a | 197 | #endif |
| sPymbed | 0:1387ff3eed4a | 198 | #endif /* HAVE_ECC */ |
| sPymbed | 0:1387ff3eed4a | 199 | |
| sPymbed | 0:1387ff3eed4a | 200 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
| sPymbed | 0:1387ff3eed4a | 201 | ret = wc_LoggingCleanup(); |
| sPymbed | 0:1387ff3eed4a | 202 | #endif |
| sPymbed | 0:1387ff3eed4a | 203 | |
| sPymbed | 0:1387ff3eed4a | 204 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
| sPymbed | 0:1387ff3eed4a | 205 | ShowMemoryTracker(); |
| sPymbed | 0:1387ff3eed4a | 206 | #endif |
| sPymbed | 0:1387ff3eed4a | 207 | |
| sPymbed | 0:1387ff3eed4a | 208 | #ifdef WOLFSSL_ASYNC_CRYPT |
| sPymbed | 0:1387ff3eed4a | 209 | wolfAsync_HardwareStop(); |
| sPymbed | 0:1387ff3eed4a | 210 | #endif |
| sPymbed | 0:1387ff3eed4a | 211 | |
| sPymbed | 0:1387ff3eed4a | 212 | #if defined(WOLFSSL_IMX6_CAAM) || defined(WOLFSSL_IMX6_CAAM_RNG) || \ |
| sPymbed | 0:1387ff3eed4a | 213 | defined(WOLFSSL_IMX6_CAAM_BLOB) |
| sPymbed | 0:1387ff3eed4a | 214 | wc_caamFree(); |
| sPymbed | 0:1387ff3eed4a | 215 | #endif |
| sPymbed | 0:1387ff3eed4a | 216 | |
| sPymbed | 0:1387ff3eed4a | 217 | initRefCount = 0; /* allow re-init */ |
| sPymbed | 0:1387ff3eed4a | 218 | } |
| sPymbed | 0:1387ff3eed4a | 219 | |
| sPymbed | 0:1387ff3eed4a | 220 | return ret; |
| sPymbed | 0:1387ff3eed4a | 221 | } |
| sPymbed | 0:1387ff3eed4a | 222 | |
| sPymbed | 0:1387ff3eed4a | 223 | #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \ |
| sPymbed | 0:1387ff3eed4a | 224 | !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) |
| sPymbed | 0:1387ff3eed4a | 225 | |
| sPymbed | 0:1387ff3eed4a | 226 | /* File Handling Helpers */ |
| sPymbed | 0:1387ff3eed4a | 227 | /* returns 0 if file found, -1 if no files or negative error */ |
| sPymbed | 0:1387ff3eed4a | 228 | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name) |
| sPymbed | 0:1387ff3eed4a | 229 | { |
| sPymbed | 0:1387ff3eed4a | 230 | int ret = -1; /* default to no files found */ |
| sPymbed | 0:1387ff3eed4a | 231 | |
| sPymbed | 0:1387ff3eed4a | 232 | if (name) |
| sPymbed | 0:1387ff3eed4a | 233 | *name = NULL; |
| sPymbed | 0:1387ff3eed4a | 234 | |
| sPymbed | 0:1387ff3eed4a | 235 | if (ctx == NULL || path == NULL) { |
| sPymbed | 0:1387ff3eed4a | 236 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 237 | } |
| sPymbed | 0:1387ff3eed4a | 238 | |
| sPymbed | 0:1387ff3eed4a | 239 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
| sPymbed | 0:1387ff3eed4a | 240 | |
| sPymbed | 0:1387ff3eed4a | 241 | #ifdef USE_WINDOWS_API |
| sPymbed | 0:1387ff3eed4a | 242 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4); |
| sPymbed | 0:1387ff3eed4a | 243 | XSTRNCAT(ctx->name, "\\*", 3); |
| sPymbed | 0:1387ff3eed4a | 244 | |
| sPymbed | 0:1387ff3eed4a | 245 | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData); |
| sPymbed | 0:1387ff3eed4a | 246 | if (ctx->hFind == INVALID_HANDLE_VALUE) { |
| sPymbed | 0:1387ff3eed4a | 247 | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
| sPymbed | 0:1387ff3eed4a | 248 | return BAD_PATH_ERROR; |
| sPymbed | 0:1387ff3eed4a | 249 | } |
| sPymbed | 0:1387ff3eed4a | 250 | |
| sPymbed | 0:1387ff3eed4a | 251 | do { |
| sPymbed | 0:1387ff3eed4a | 252 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { |
| sPymbed | 0:1387ff3eed4a | 253 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3); |
| sPymbed | 0:1387ff3eed4a | 254 | XSTRNCAT(ctx->name, "\\", 2); |
| sPymbed | 0:1387ff3eed4a | 255 | XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2); |
| sPymbed | 0:1387ff3eed4a | 256 | if (name) |
| sPymbed | 0:1387ff3eed4a | 257 | *name = ctx->name; |
| sPymbed | 0:1387ff3eed4a | 258 | return 0; |
| sPymbed | 0:1387ff3eed4a | 259 | } |
| sPymbed | 0:1387ff3eed4a | 260 | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData)); |
| sPymbed | 0:1387ff3eed4a | 261 | #else |
| sPymbed | 0:1387ff3eed4a | 262 | ctx->dir = opendir(path); |
| sPymbed | 0:1387ff3eed4a | 263 | if (ctx->dir == NULL) { |
| sPymbed | 0:1387ff3eed4a | 264 | WOLFSSL_MSG("opendir path verify locations failed"); |
| sPymbed | 0:1387ff3eed4a | 265 | return BAD_PATH_ERROR; |
| sPymbed | 0:1387ff3eed4a | 266 | } |
| sPymbed | 0:1387ff3eed4a | 267 | |
| sPymbed | 0:1387ff3eed4a | 268 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
| sPymbed | 0:1387ff3eed4a | 269 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2); |
| sPymbed | 0:1387ff3eed4a | 270 | XSTRNCAT(ctx->name, "/", 1); |
| sPymbed | 0:1387ff3eed4a | 271 | XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2); |
| sPymbed | 0:1387ff3eed4a | 272 | |
| sPymbed | 0:1387ff3eed4a | 273 | if (stat(ctx->name, &ctx->s) != 0) { |
| sPymbed | 0:1387ff3eed4a | 274 | WOLFSSL_MSG("stat on name failed"); |
| sPymbed | 0:1387ff3eed4a | 275 | ret = BAD_PATH_ERROR; |
| sPymbed | 0:1387ff3eed4a | 276 | break; |
| sPymbed | 0:1387ff3eed4a | 277 | } else if (S_ISREG(ctx->s.st_mode)) { |
| sPymbed | 0:1387ff3eed4a | 278 | if (name) |
| sPymbed | 0:1387ff3eed4a | 279 | *name = ctx->name; |
| sPymbed | 0:1387ff3eed4a | 280 | return 0; |
| sPymbed | 0:1387ff3eed4a | 281 | } |
| sPymbed | 0:1387ff3eed4a | 282 | } |
| sPymbed | 0:1387ff3eed4a | 283 | #endif |
| sPymbed | 0:1387ff3eed4a | 284 | wc_ReadDirClose(ctx); |
| sPymbed | 0:1387ff3eed4a | 285 | |
| sPymbed | 0:1387ff3eed4a | 286 | return ret; |
| sPymbed | 0:1387ff3eed4a | 287 | } |
| sPymbed | 0:1387ff3eed4a | 288 | |
| sPymbed | 0:1387ff3eed4a | 289 | /* returns 0 if file found, -1 if no more files */ |
| sPymbed | 0:1387ff3eed4a | 290 | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name) |
| sPymbed | 0:1387ff3eed4a | 291 | { |
| sPymbed | 0:1387ff3eed4a | 292 | int ret = -1; /* default to no file found */ |
| sPymbed | 0:1387ff3eed4a | 293 | |
| sPymbed | 0:1387ff3eed4a | 294 | if (name) |
| sPymbed | 0:1387ff3eed4a | 295 | *name = NULL; |
| sPymbed | 0:1387ff3eed4a | 296 | |
| sPymbed | 0:1387ff3eed4a | 297 | if (ctx == NULL || path == NULL) { |
| sPymbed | 0:1387ff3eed4a | 298 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 299 | } |
| sPymbed | 0:1387ff3eed4a | 300 | |
| sPymbed | 0:1387ff3eed4a | 301 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
| sPymbed | 0:1387ff3eed4a | 302 | |
| sPymbed | 0:1387ff3eed4a | 303 | #ifdef USE_WINDOWS_API |
| sPymbed | 0:1387ff3eed4a | 304 | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) { |
| sPymbed | 0:1387ff3eed4a | 305 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { |
| sPymbed | 0:1387ff3eed4a | 306 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3); |
| sPymbed | 0:1387ff3eed4a | 307 | XSTRNCAT(ctx->name, "\\", 2); |
| sPymbed | 0:1387ff3eed4a | 308 | XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2); |
| sPymbed | 0:1387ff3eed4a | 309 | if (name) |
| sPymbed | 0:1387ff3eed4a | 310 | *name = ctx->name; |
| sPymbed | 0:1387ff3eed4a | 311 | return 0; |
| sPymbed | 0:1387ff3eed4a | 312 | } |
| sPymbed | 0:1387ff3eed4a | 313 | } |
| sPymbed | 0:1387ff3eed4a | 314 | #else |
| sPymbed | 0:1387ff3eed4a | 315 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
| sPymbed | 0:1387ff3eed4a | 316 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2); |
| sPymbed | 0:1387ff3eed4a | 317 | XSTRNCAT(ctx->name, "/", 1); |
| sPymbed | 0:1387ff3eed4a | 318 | XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2); |
| sPymbed | 0:1387ff3eed4a | 319 | |
| sPymbed | 0:1387ff3eed4a | 320 | if (stat(ctx->name, &ctx->s) != 0) { |
| sPymbed | 0:1387ff3eed4a | 321 | WOLFSSL_MSG("stat on name failed"); |
| sPymbed | 0:1387ff3eed4a | 322 | ret = BAD_PATH_ERROR; |
| sPymbed | 0:1387ff3eed4a | 323 | break; |
| sPymbed | 0:1387ff3eed4a | 324 | } else if (S_ISREG(ctx->s.st_mode)) { |
| sPymbed | 0:1387ff3eed4a | 325 | if (name) |
| sPymbed | 0:1387ff3eed4a | 326 | *name = ctx->name; |
| sPymbed | 0:1387ff3eed4a | 327 | return 0; |
| sPymbed | 0:1387ff3eed4a | 328 | } |
| sPymbed | 0:1387ff3eed4a | 329 | } |
| sPymbed | 0:1387ff3eed4a | 330 | #endif |
| sPymbed | 0:1387ff3eed4a | 331 | |
| sPymbed | 0:1387ff3eed4a | 332 | wc_ReadDirClose(ctx); |
| sPymbed | 0:1387ff3eed4a | 333 | |
| sPymbed | 0:1387ff3eed4a | 334 | return ret; |
| sPymbed | 0:1387ff3eed4a | 335 | } |
| sPymbed | 0:1387ff3eed4a | 336 | |
| sPymbed | 0:1387ff3eed4a | 337 | void wc_ReadDirClose(ReadDirCtx* ctx) |
| sPymbed | 0:1387ff3eed4a | 338 | { |
| sPymbed | 0:1387ff3eed4a | 339 | if (ctx == NULL) { |
| sPymbed | 0:1387ff3eed4a | 340 | return; |
| sPymbed | 0:1387ff3eed4a | 341 | } |
| sPymbed | 0:1387ff3eed4a | 342 | |
| sPymbed | 0:1387ff3eed4a | 343 | #ifdef USE_WINDOWS_API |
| sPymbed | 0:1387ff3eed4a | 344 | if (ctx->hFind != INVALID_HANDLE_VALUE) { |
| sPymbed | 0:1387ff3eed4a | 345 | FindClose(ctx->hFind); |
| sPymbed | 0:1387ff3eed4a | 346 | ctx->hFind = INVALID_HANDLE_VALUE; |
| sPymbed | 0:1387ff3eed4a | 347 | } |
| sPymbed | 0:1387ff3eed4a | 348 | #else |
| sPymbed | 0:1387ff3eed4a | 349 | if (ctx->dir) { |
| sPymbed | 0:1387ff3eed4a | 350 | closedir(ctx->dir); |
| sPymbed | 0:1387ff3eed4a | 351 | ctx->dir = NULL; |
| sPymbed | 0:1387ff3eed4a | 352 | } |
| sPymbed | 0:1387ff3eed4a | 353 | #endif |
| sPymbed | 0:1387ff3eed4a | 354 | } |
| sPymbed | 0:1387ff3eed4a | 355 | |
| sPymbed | 0:1387ff3eed4a | 356 | #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */ |
| sPymbed | 0:1387ff3eed4a | 357 | |
| sPymbed | 0:1387ff3eed4a | 358 | |
| sPymbed | 0:1387ff3eed4a | 359 | wolfSSL_Mutex* wc_InitAndAllocMutex(void) |
| sPymbed | 0:1387ff3eed4a | 360 | { |
| sPymbed | 0:1387ff3eed4a | 361 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL, |
| sPymbed | 0:1387ff3eed4a | 362 | DYNAMIC_TYPE_MUTEX); |
| sPymbed | 0:1387ff3eed4a | 363 | if (m != NULL) { |
| sPymbed | 0:1387ff3eed4a | 364 | if (wc_InitMutex(m) != 0) { |
| sPymbed | 0:1387ff3eed4a | 365 | WOLFSSL_MSG("Init Mutex failed"); |
| sPymbed | 0:1387ff3eed4a | 366 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX); |
| sPymbed | 0:1387ff3eed4a | 367 | m = NULL; |
| sPymbed | 0:1387ff3eed4a | 368 | } |
| sPymbed | 0:1387ff3eed4a | 369 | } |
| sPymbed | 0:1387ff3eed4a | 370 | else { |
| sPymbed | 0:1387ff3eed4a | 371 | WOLFSSL_MSG("Memory error with Mutex allocation"); |
| sPymbed | 0:1387ff3eed4a | 372 | } |
| sPymbed | 0:1387ff3eed4a | 373 | |
| sPymbed | 0:1387ff3eed4a | 374 | return m; |
| sPymbed | 0:1387ff3eed4a | 375 | } |
| sPymbed | 0:1387ff3eed4a | 376 | |
| sPymbed | 0:1387ff3eed4a | 377 | #ifdef USE_WOLF_STRTOK |
| sPymbed | 0:1387ff3eed4a | 378 | /* String token (delim) search. If str is null use nextp. */ |
| sPymbed | 0:1387ff3eed4a | 379 | char* wc_strtok(char *str, const char *delim, char **nextp) |
| sPymbed | 0:1387ff3eed4a | 380 | { |
| sPymbed | 0:1387ff3eed4a | 381 | char* ret; |
| sPymbed | 0:1387ff3eed4a | 382 | int i, j; |
| sPymbed | 0:1387ff3eed4a | 383 | |
| sPymbed | 0:1387ff3eed4a | 384 | /* Use next if str is NULL */ |
| sPymbed | 0:1387ff3eed4a | 385 | if (str == NULL && nextp) |
| sPymbed | 0:1387ff3eed4a | 386 | str = *nextp; |
| sPymbed | 0:1387ff3eed4a | 387 | |
| sPymbed | 0:1387ff3eed4a | 388 | /* verify str input */ |
| sPymbed | 0:1387ff3eed4a | 389 | if (str == NULL || *str == '\0') |
| sPymbed | 0:1387ff3eed4a | 390 | return NULL; |
| sPymbed | 0:1387ff3eed4a | 391 | |
| sPymbed | 0:1387ff3eed4a | 392 | /* match on entire delim */ |
| sPymbed | 0:1387ff3eed4a | 393 | for (i = 0; str[i]; i++) { |
| sPymbed | 0:1387ff3eed4a | 394 | for (j = 0; delim[j]; j++) { |
| sPymbed | 0:1387ff3eed4a | 395 | if (delim[j] == str[i]) |
| sPymbed | 0:1387ff3eed4a | 396 | break; |
| sPymbed | 0:1387ff3eed4a | 397 | } |
| sPymbed | 0:1387ff3eed4a | 398 | if (!delim[j]) |
| sPymbed | 0:1387ff3eed4a | 399 | break; |
| sPymbed | 0:1387ff3eed4a | 400 | } |
| sPymbed | 0:1387ff3eed4a | 401 | str += i; |
| sPymbed | 0:1387ff3eed4a | 402 | /* if end of string, not found so return NULL */ |
| sPymbed | 0:1387ff3eed4a | 403 | if (*str == '\0') |
| sPymbed | 0:1387ff3eed4a | 404 | return NULL; |
| sPymbed | 0:1387ff3eed4a | 405 | |
| sPymbed | 0:1387ff3eed4a | 406 | ret = str; |
| sPymbed | 0:1387ff3eed4a | 407 | |
| sPymbed | 0:1387ff3eed4a | 408 | /* match on first delim */ |
| sPymbed | 0:1387ff3eed4a | 409 | for (i = 0; str[i]; i++) { |
| sPymbed | 0:1387ff3eed4a | 410 | for (j = 0; delim[j]; j++) { |
| sPymbed | 0:1387ff3eed4a | 411 | if (delim[j] == str[i]) |
| sPymbed | 0:1387ff3eed4a | 412 | break; |
| sPymbed | 0:1387ff3eed4a | 413 | } |
| sPymbed | 0:1387ff3eed4a | 414 | if (delim[j] == str[i]) |
| sPymbed | 0:1387ff3eed4a | 415 | break; |
| sPymbed | 0:1387ff3eed4a | 416 | } |
| sPymbed | 0:1387ff3eed4a | 417 | str += i; |
| sPymbed | 0:1387ff3eed4a | 418 | |
| sPymbed | 0:1387ff3eed4a | 419 | /* null terminate found string */ |
| sPymbed | 0:1387ff3eed4a | 420 | if (*str) |
| sPymbed | 0:1387ff3eed4a | 421 | *str++ = '\0'; |
| sPymbed | 0:1387ff3eed4a | 422 | |
| sPymbed | 0:1387ff3eed4a | 423 | /* return pointer to next */ |
| sPymbed | 0:1387ff3eed4a | 424 | if (nextp) |
| sPymbed | 0:1387ff3eed4a | 425 | *nextp = str; |
| sPymbed | 0:1387ff3eed4a | 426 | |
| sPymbed | 0:1387ff3eed4a | 427 | return ret; |
| sPymbed | 0:1387ff3eed4a | 428 | } |
| sPymbed | 0:1387ff3eed4a | 429 | #endif /* USE_WOLF_STRTOK */ |
| sPymbed | 0:1387ff3eed4a | 430 | |
| sPymbed | 0:1387ff3eed4a | 431 | #ifdef USE_WOLF_STRSEP |
| sPymbed | 0:1387ff3eed4a | 432 | char* wc_strsep(char **stringp, const char *delim) |
| sPymbed | 0:1387ff3eed4a | 433 | { |
| sPymbed | 0:1387ff3eed4a | 434 | char *s, *tok; |
| sPymbed | 0:1387ff3eed4a | 435 | const char *spanp; |
| sPymbed | 0:1387ff3eed4a | 436 | |
| sPymbed | 0:1387ff3eed4a | 437 | /* null check */ |
| sPymbed | 0:1387ff3eed4a | 438 | if (stringp == NULL || *stringp == NULL) |
| sPymbed | 0:1387ff3eed4a | 439 | return NULL; |
| sPymbed | 0:1387ff3eed4a | 440 | |
| sPymbed | 0:1387ff3eed4a | 441 | s = *stringp; |
| sPymbed | 0:1387ff3eed4a | 442 | for (tok = s; *tok; ++tok) { |
| sPymbed | 0:1387ff3eed4a | 443 | for (spanp = delim; *spanp; ++spanp) { |
| sPymbed | 0:1387ff3eed4a | 444 | /* found delimiter */ |
| sPymbed | 0:1387ff3eed4a | 445 | if (*tok == *spanp) { |
| sPymbed | 0:1387ff3eed4a | 446 | *tok = '\0'; /* replace delim with null term */ |
| sPymbed | 0:1387ff3eed4a | 447 | *stringp = tok + 1; /* return past delim */ |
| sPymbed | 0:1387ff3eed4a | 448 | return s; |
| sPymbed | 0:1387ff3eed4a | 449 | } |
| sPymbed | 0:1387ff3eed4a | 450 | } |
| sPymbed | 0:1387ff3eed4a | 451 | } |
| sPymbed | 0:1387ff3eed4a | 452 | |
| sPymbed | 0:1387ff3eed4a | 453 | *stringp = NULL; |
| sPymbed | 0:1387ff3eed4a | 454 | return s; |
| sPymbed | 0:1387ff3eed4a | 455 | } |
| sPymbed | 0:1387ff3eed4a | 456 | #endif /* USE_WOLF_STRSEP */ |
| sPymbed | 0:1387ff3eed4a | 457 | |
| sPymbed | 0:1387ff3eed4a | 458 | #if WOLFSSL_CRYPT_HW_MUTEX |
| sPymbed | 0:1387ff3eed4a | 459 | /* Mutex for protection of cryptography hardware */ |
| sPymbed | 0:1387ff3eed4a | 460 | static wolfSSL_Mutex wcCryptHwMutex; |
| sPymbed | 0:1387ff3eed4a | 461 | static int wcCryptHwMutexInit = 0; |
| sPymbed | 0:1387ff3eed4a | 462 | |
| sPymbed | 0:1387ff3eed4a | 463 | int wolfSSL_CryptHwMutexInit(void) { |
| sPymbed | 0:1387ff3eed4a | 464 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 465 | if(wcCryptHwMutexInit == 0) { |
| sPymbed | 0:1387ff3eed4a | 466 | ret = wc_InitMutex(&wcCryptHwMutex); |
| sPymbed | 0:1387ff3eed4a | 467 | if(ret == 0) { |
| sPymbed | 0:1387ff3eed4a | 468 | wcCryptHwMutexInit = 1; |
| sPymbed | 0:1387ff3eed4a | 469 | } |
| sPymbed | 0:1387ff3eed4a | 470 | } |
| sPymbed | 0:1387ff3eed4a | 471 | return ret; |
| sPymbed | 0:1387ff3eed4a | 472 | } |
| sPymbed | 0:1387ff3eed4a | 473 | |
| sPymbed | 0:1387ff3eed4a | 474 | int wolfSSL_CryptHwMutexLock(void) { |
| sPymbed | 0:1387ff3eed4a | 475 | int ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 476 | |
| sPymbed | 0:1387ff3eed4a | 477 | /* Make sure HW Mutex has been initialized */ |
| sPymbed | 0:1387ff3eed4a | 478 | wolfSSL_CryptHwMutexInit(); |
| sPymbed | 0:1387ff3eed4a | 479 | |
| sPymbed | 0:1387ff3eed4a | 480 | if(wcCryptHwMutexInit) { |
| sPymbed | 0:1387ff3eed4a | 481 | ret = wc_LockMutex(&wcCryptHwMutex); |
| sPymbed | 0:1387ff3eed4a | 482 | } |
| sPymbed | 0:1387ff3eed4a | 483 | return ret; |
| sPymbed | 0:1387ff3eed4a | 484 | } |
| sPymbed | 0:1387ff3eed4a | 485 | |
| sPymbed | 0:1387ff3eed4a | 486 | int wolfSSL_CryptHwMutexUnLock(void) { |
| sPymbed | 0:1387ff3eed4a | 487 | int ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 488 | |
| sPymbed | 0:1387ff3eed4a | 489 | if(wcCryptHwMutexInit) { |
| sPymbed | 0:1387ff3eed4a | 490 | ret = wc_UnLockMutex(&wcCryptHwMutex); |
| sPymbed | 0:1387ff3eed4a | 491 | } |
| sPymbed | 0:1387ff3eed4a | 492 | return ret; |
| sPymbed | 0:1387ff3eed4a | 493 | } |
| sPymbed | 0:1387ff3eed4a | 494 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */ |
| sPymbed | 0:1387ff3eed4a | 495 | |
| sPymbed | 0:1387ff3eed4a | 496 | |
| sPymbed | 0:1387ff3eed4a | 497 | /* ---------------------------------------------------------------------------*/ |
| sPymbed | 0:1387ff3eed4a | 498 | /* Mutex Ports */ |
| sPymbed | 0:1387ff3eed4a | 499 | /* ---------------------------------------------------------------------------*/ |
| sPymbed | 0:1387ff3eed4a | 500 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
| sPymbed | 0:1387ff3eed4a | 501 | static mutex_cb* compat_mutex_cb = NULL; |
| sPymbed | 0:1387ff3eed4a | 502 | |
| sPymbed | 0:1387ff3eed4a | 503 | /* Function that locks or unlocks a mutex based on the flag passed in. |
| sPymbed | 0:1387ff3eed4a | 504 | * |
| sPymbed | 0:1387ff3eed4a | 505 | * flag lock or unlock i.e. CRYPTO_LOCK |
| sPymbed | 0:1387ff3eed4a | 506 | * type the type of lock to unlock or lock |
| sPymbed | 0:1387ff3eed4a | 507 | * file name of the file calling |
| sPymbed | 0:1387ff3eed4a | 508 | * line the line number from file calling |
| sPymbed | 0:1387ff3eed4a | 509 | */ |
| sPymbed | 0:1387ff3eed4a | 510 | int wc_LockMutex_ex(int flag, int type, const char* file, int line) |
| sPymbed | 0:1387ff3eed4a | 511 | { |
| sPymbed | 0:1387ff3eed4a | 512 | if (compat_mutex_cb != NULL) { |
| sPymbed | 0:1387ff3eed4a | 513 | compat_mutex_cb(flag, type, file, line); |
| sPymbed | 0:1387ff3eed4a | 514 | return 0; |
| sPymbed | 0:1387ff3eed4a | 515 | } |
| sPymbed | 0:1387ff3eed4a | 516 | else { |
| sPymbed | 0:1387ff3eed4a | 517 | WOLFSSL_MSG("Mutex call back function not set. Call wc_SetMutexCb"); |
| sPymbed | 0:1387ff3eed4a | 518 | return BAD_STATE_E; |
| sPymbed | 0:1387ff3eed4a | 519 | } |
| sPymbed | 0:1387ff3eed4a | 520 | } |
| sPymbed | 0:1387ff3eed4a | 521 | |
| sPymbed | 0:1387ff3eed4a | 522 | |
| sPymbed | 0:1387ff3eed4a | 523 | /* Set the callback function to use for locking/unlocking mutex |
| sPymbed | 0:1387ff3eed4a | 524 | * |
| sPymbed | 0:1387ff3eed4a | 525 | * cb callback function to use |
| sPymbed | 0:1387ff3eed4a | 526 | */ |
| sPymbed | 0:1387ff3eed4a | 527 | int wc_SetMutexCb(mutex_cb* cb) |
| sPymbed | 0:1387ff3eed4a | 528 | { |
| sPymbed | 0:1387ff3eed4a | 529 | compat_mutex_cb = cb; |
| sPymbed | 0:1387ff3eed4a | 530 | return 0; |
| sPymbed | 0:1387ff3eed4a | 531 | } |
| sPymbed | 0:1387ff3eed4a | 532 | #endif /* defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) */ |
| sPymbed | 0:1387ff3eed4a | 533 | #ifdef SINGLE_THREADED |
| sPymbed | 0:1387ff3eed4a | 534 | |
| sPymbed | 0:1387ff3eed4a | 535 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 536 | { |
| sPymbed | 0:1387ff3eed4a | 537 | (void)m; |
| sPymbed | 0:1387ff3eed4a | 538 | return 0; |
| sPymbed | 0:1387ff3eed4a | 539 | } |
| sPymbed | 0:1387ff3eed4a | 540 | |
| sPymbed | 0:1387ff3eed4a | 541 | int wc_FreeMutex(wolfSSL_Mutex *m) |
| sPymbed | 0:1387ff3eed4a | 542 | { |
| sPymbed | 0:1387ff3eed4a | 543 | (void)m; |
| sPymbed | 0:1387ff3eed4a | 544 | return 0; |
| sPymbed | 0:1387ff3eed4a | 545 | } |
| sPymbed | 0:1387ff3eed4a | 546 | |
| sPymbed | 0:1387ff3eed4a | 547 | |
| sPymbed | 0:1387ff3eed4a | 548 | int wc_LockMutex(wolfSSL_Mutex *m) |
| sPymbed | 0:1387ff3eed4a | 549 | { |
| sPymbed | 0:1387ff3eed4a | 550 | (void)m; |
| sPymbed | 0:1387ff3eed4a | 551 | return 0; |
| sPymbed | 0:1387ff3eed4a | 552 | } |
| sPymbed | 0:1387ff3eed4a | 553 | |
| sPymbed | 0:1387ff3eed4a | 554 | |
| sPymbed | 0:1387ff3eed4a | 555 | int wc_UnLockMutex(wolfSSL_Mutex *m) |
| sPymbed | 0:1387ff3eed4a | 556 | { |
| sPymbed | 0:1387ff3eed4a | 557 | (void)m; |
| sPymbed | 0:1387ff3eed4a | 558 | return 0; |
| sPymbed | 0:1387ff3eed4a | 559 | } |
| sPymbed | 0:1387ff3eed4a | 560 | |
| sPymbed | 0:1387ff3eed4a | 561 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ |
| sPymbed | 0:1387ff3eed4a | 562 | defined(FREESCALE_FREE_RTOS) |
| sPymbed | 0:1387ff3eed4a | 563 | |
| sPymbed | 0:1387ff3eed4a | 564 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 565 | { |
| sPymbed | 0:1387ff3eed4a | 566 | int iReturn; |
| sPymbed | 0:1387ff3eed4a | 567 | |
| sPymbed | 0:1387ff3eed4a | 568 | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex(); |
| sPymbed | 0:1387ff3eed4a | 569 | if( *m != NULL ) |
| sPymbed | 0:1387ff3eed4a | 570 | iReturn = 0; |
| sPymbed | 0:1387ff3eed4a | 571 | else |
| sPymbed | 0:1387ff3eed4a | 572 | iReturn = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 573 | |
| sPymbed | 0:1387ff3eed4a | 574 | return iReturn; |
| sPymbed | 0:1387ff3eed4a | 575 | } |
| sPymbed | 0:1387ff3eed4a | 576 | |
| sPymbed | 0:1387ff3eed4a | 577 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 578 | { |
| sPymbed | 0:1387ff3eed4a | 579 | vSemaphoreDelete( *m ); |
| sPymbed | 0:1387ff3eed4a | 580 | return 0; |
| sPymbed | 0:1387ff3eed4a | 581 | } |
| sPymbed | 0:1387ff3eed4a | 582 | |
| sPymbed | 0:1387ff3eed4a | 583 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 584 | { |
| sPymbed | 0:1387ff3eed4a | 585 | /* Assume an infinite block, or should there be zero block? */ |
| sPymbed | 0:1387ff3eed4a | 586 | xSemaphoreTake( *m, portMAX_DELAY ); |
| sPymbed | 0:1387ff3eed4a | 587 | return 0; |
| sPymbed | 0:1387ff3eed4a | 588 | } |
| sPymbed | 0:1387ff3eed4a | 589 | |
| sPymbed | 0:1387ff3eed4a | 590 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 591 | { |
| sPymbed | 0:1387ff3eed4a | 592 | xSemaphoreGive( *m ); |
| sPymbed | 0:1387ff3eed4a | 593 | return 0; |
| sPymbed | 0:1387ff3eed4a | 594 | } |
| sPymbed | 0:1387ff3eed4a | 595 | |
| sPymbed | 0:1387ff3eed4a | 596 | #elif defined(WOLFSSL_SAFERTOS) |
| sPymbed | 0:1387ff3eed4a | 597 | |
| sPymbed | 0:1387ff3eed4a | 598 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 599 | { |
| sPymbed | 0:1387ff3eed4a | 600 | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex); |
| sPymbed | 0:1387ff3eed4a | 601 | if (m->mutex == NULL) |
| sPymbed | 0:1387ff3eed4a | 602 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 603 | |
| sPymbed | 0:1387ff3eed4a | 604 | return 0; |
| sPymbed | 0:1387ff3eed4a | 605 | } |
| sPymbed | 0:1387ff3eed4a | 606 | |
| sPymbed | 0:1387ff3eed4a | 607 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 608 | { |
| sPymbed | 0:1387ff3eed4a | 609 | (void)m; |
| sPymbed | 0:1387ff3eed4a | 610 | return 0; |
| sPymbed | 0:1387ff3eed4a | 611 | } |
| sPymbed | 0:1387ff3eed4a | 612 | |
| sPymbed | 0:1387ff3eed4a | 613 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 614 | { |
| sPymbed | 0:1387ff3eed4a | 615 | /* Assume an infinite block */ |
| sPymbed | 0:1387ff3eed4a | 616 | xSemaphoreTake(m->mutex, portMAX_DELAY); |
| sPymbed | 0:1387ff3eed4a | 617 | return 0; |
| sPymbed | 0:1387ff3eed4a | 618 | } |
| sPymbed | 0:1387ff3eed4a | 619 | |
| sPymbed | 0:1387ff3eed4a | 620 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 621 | { |
| sPymbed | 0:1387ff3eed4a | 622 | xSemaphoreGive(m->mutex); |
| sPymbed | 0:1387ff3eed4a | 623 | return 0; |
| sPymbed | 0:1387ff3eed4a | 624 | } |
| sPymbed | 0:1387ff3eed4a | 625 | |
| sPymbed | 0:1387ff3eed4a | 626 | #elif defined(USE_WINDOWS_API) |
| sPymbed | 0:1387ff3eed4a | 627 | |
| sPymbed | 0:1387ff3eed4a | 628 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 629 | { |
| sPymbed | 0:1387ff3eed4a | 630 | InitializeCriticalSection(m); |
| sPymbed | 0:1387ff3eed4a | 631 | return 0; |
| sPymbed | 0:1387ff3eed4a | 632 | } |
| sPymbed | 0:1387ff3eed4a | 633 | |
| sPymbed | 0:1387ff3eed4a | 634 | |
| sPymbed | 0:1387ff3eed4a | 635 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 636 | { |
| sPymbed | 0:1387ff3eed4a | 637 | DeleteCriticalSection(m); |
| sPymbed | 0:1387ff3eed4a | 638 | return 0; |
| sPymbed | 0:1387ff3eed4a | 639 | } |
| sPymbed | 0:1387ff3eed4a | 640 | |
| sPymbed | 0:1387ff3eed4a | 641 | |
| sPymbed | 0:1387ff3eed4a | 642 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 643 | { |
| sPymbed | 0:1387ff3eed4a | 644 | EnterCriticalSection(m); |
| sPymbed | 0:1387ff3eed4a | 645 | return 0; |
| sPymbed | 0:1387ff3eed4a | 646 | } |
| sPymbed | 0:1387ff3eed4a | 647 | |
| sPymbed | 0:1387ff3eed4a | 648 | |
| sPymbed | 0:1387ff3eed4a | 649 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 650 | { |
| sPymbed | 0:1387ff3eed4a | 651 | LeaveCriticalSection(m); |
| sPymbed | 0:1387ff3eed4a | 652 | return 0; |
| sPymbed | 0:1387ff3eed4a | 653 | } |
| sPymbed | 0:1387ff3eed4a | 654 | |
| sPymbed | 0:1387ff3eed4a | 655 | #elif defined(WOLFSSL_PTHREADS) |
| sPymbed | 0:1387ff3eed4a | 656 | |
| sPymbed | 0:1387ff3eed4a | 657 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 658 | { |
| sPymbed | 0:1387ff3eed4a | 659 | if (pthread_mutex_init(m, 0) == 0) |
| sPymbed | 0:1387ff3eed4a | 660 | return 0; |
| sPymbed | 0:1387ff3eed4a | 661 | else |
| sPymbed | 0:1387ff3eed4a | 662 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 663 | } |
| sPymbed | 0:1387ff3eed4a | 664 | |
| sPymbed | 0:1387ff3eed4a | 665 | |
| sPymbed | 0:1387ff3eed4a | 666 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 667 | { |
| sPymbed | 0:1387ff3eed4a | 668 | if (pthread_mutex_destroy(m) == 0) |
| sPymbed | 0:1387ff3eed4a | 669 | return 0; |
| sPymbed | 0:1387ff3eed4a | 670 | else |
| sPymbed | 0:1387ff3eed4a | 671 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 672 | } |
| sPymbed | 0:1387ff3eed4a | 673 | |
| sPymbed | 0:1387ff3eed4a | 674 | |
| sPymbed | 0:1387ff3eed4a | 675 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 676 | { |
| sPymbed | 0:1387ff3eed4a | 677 | if (pthread_mutex_lock(m) == 0) |
| sPymbed | 0:1387ff3eed4a | 678 | return 0; |
| sPymbed | 0:1387ff3eed4a | 679 | else |
| sPymbed | 0:1387ff3eed4a | 680 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 681 | } |
| sPymbed | 0:1387ff3eed4a | 682 | |
| sPymbed | 0:1387ff3eed4a | 683 | |
| sPymbed | 0:1387ff3eed4a | 684 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 685 | { |
| sPymbed | 0:1387ff3eed4a | 686 | if (pthread_mutex_unlock(m) == 0) |
| sPymbed | 0:1387ff3eed4a | 687 | return 0; |
| sPymbed | 0:1387ff3eed4a | 688 | else |
| sPymbed | 0:1387ff3eed4a | 689 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 690 | } |
| sPymbed | 0:1387ff3eed4a | 691 | |
| sPymbed | 0:1387ff3eed4a | 692 | #elif defined(THREADX) |
| sPymbed | 0:1387ff3eed4a | 693 | |
| sPymbed | 0:1387ff3eed4a | 694 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 695 | { |
| sPymbed | 0:1387ff3eed4a | 696 | if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0) |
| sPymbed | 0:1387ff3eed4a | 697 | return 0; |
| sPymbed | 0:1387ff3eed4a | 698 | else |
| sPymbed | 0:1387ff3eed4a | 699 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 700 | } |
| sPymbed | 0:1387ff3eed4a | 701 | |
| sPymbed | 0:1387ff3eed4a | 702 | |
| sPymbed | 0:1387ff3eed4a | 703 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 704 | { |
| sPymbed | 0:1387ff3eed4a | 705 | if (tx_mutex_delete(m) == 0) |
| sPymbed | 0:1387ff3eed4a | 706 | return 0; |
| sPymbed | 0:1387ff3eed4a | 707 | else |
| sPymbed | 0:1387ff3eed4a | 708 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 709 | } |
| sPymbed | 0:1387ff3eed4a | 710 | |
| sPymbed | 0:1387ff3eed4a | 711 | |
| sPymbed | 0:1387ff3eed4a | 712 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 713 | { |
| sPymbed | 0:1387ff3eed4a | 714 | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0) |
| sPymbed | 0:1387ff3eed4a | 715 | return 0; |
| sPymbed | 0:1387ff3eed4a | 716 | else |
| sPymbed | 0:1387ff3eed4a | 717 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 718 | } |
| sPymbed | 0:1387ff3eed4a | 719 | |
| sPymbed | 0:1387ff3eed4a | 720 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 721 | { |
| sPymbed | 0:1387ff3eed4a | 722 | if (tx_mutex_put(m) == 0) |
| sPymbed | 0:1387ff3eed4a | 723 | return 0; |
| sPymbed | 0:1387ff3eed4a | 724 | else |
| sPymbed | 0:1387ff3eed4a | 725 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 726 | } |
| sPymbed | 0:1387ff3eed4a | 727 | |
| sPymbed | 0:1387ff3eed4a | 728 | #elif defined(MICRIUM) |
| sPymbed | 0:1387ff3eed4a | 729 | |
| sPymbed | 0:1387ff3eed4a | 730 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 731 | { |
| sPymbed | 0:1387ff3eed4a | 732 | OS_ERR err; |
| sPymbed | 0:1387ff3eed4a | 733 | |
| sPymbed | 0:1387ff3eed4a | 734 | OSMutexCreate(m, "wolfSSL Mutex", &err); |
| sPymbed | 0:1387ff3eed4a | 735 | |
| sPymbed | 0:1387ff3eed4a | 736 | if (err == OS_ERR_NONE) |
| sPymbed | 0:1387ff3eed4a | 737 | return 0; |
| sPymbed | 0:1387ff3eed4a | 738 | else |
| sPymbed | 0:1387ff3eed4a | 739 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 740 | } |
| sPymbed | 0:1387ff3eed4a | 741 | |
| sPymbed | 0:1387ff3eed4a | 742 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 743 | { |
| sPymbed | 0:1387ff3eed4a | 744 | #if (OS_CFG_MUTEX_DEL_EN == DEF_ENABLED) |
| sPymbed | 0:1387ff3eed4a | 745 | OS_ERR err; |
| sPymbed | 0:1387ff3eed4a | 746 | |
| sPymbed | 0:1387ff3eed4a | 747 | OSMutexDel(m, OS_OPT_DEL_ALWAYS, &err); |
| sPymbed | 0:1387ff3eed4a | 748 | |
| sPymbed | 0:1387ff3eed4a | 749 | if (err == OS_ERR_NONE) |
| sPymbed | 0:1387ff3eed4a | 750 | return 0; |
| sPymbed | 0:1387ff3eed4a | 751 | else |
| sPymbed | 0:1387ff3eed4a | 752 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 753 | #else |
| sPymbed | 0:1387ff3eed4a | 754 | return 0; |
| sPymbed | 0:1387ff3eed4a | 755 | #endif |
| sPymbed | 0:1387ff3eed4a | 756 | } |
| sPymbed | 0:1387ff3eed4a | 757 | |
| sPymbed | 0:1387ff3eed4a | 758 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 759 | { |
| sPymbed | 0:1387ff3eed4a | 760 | OS_ERR err; |
| sPymbed | 0:1387ff3eed4a | 761 | |
| sPymbed | 0:1387ff3eed4a | 762 | OSMutexPend(m, 0, OS_OPT_PEND_BLOCKING, NULL, &err); |
| sPymbed | 0:1387ff3eed4a | 763 | |
| sPymbed | 0:1387ff3eed4a | 764 | if (err == OS_ERR_NONE) |
| sPymbed | 0:1387ff3eed4a | 765 | return 0; |
| sPymbed | 0:1387ff3eed4a | 766 | else |
| sPymbed | 0:1387ff3eed4a | 767 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 768 | } |
| sPymbed | 0:1387ff3eed4a | 769 | |
| sPymbed | 0:1387ff3eed4a | 770 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 771 | { |
| sPymbed | 0:1387ff3eed4a | 772 | OS_ERR err; |
| sPymbed | 0:1387ff3eed4a | 773 | |
| sPymbed | 0:1387ff3eed4a | 774 | OSMutexPost(m, OS_OPT_POST_NONE, &err); |
| sPymbed | 0:1387ff3eed4a | 775 | |
| sPymbed | 0:1387ff3eed4a | 776 | if (err == OS_ERR_NONE) |
| sPymbed | 0:1387ff3eed4a | 777 | return 0; |
| sPymbed | 0:1387ff3eed4a | 778 | else |
| sPymbed | 0:1387ff3eed4a | 779 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 780 | } |
| sPymbed | 0:1387ff3eed4a | 781 | |
| sPymbed | 0:1387ff3eed4a | 782 | #elif defined(EBSNET) |
| sPymbed | 0:1387ff3eed4a | 783 | |
| sPymbed | 0:1387ff3eed4a | 784 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 785 | { |
| sPymbed | 0:1387ff3eed4a | 786 | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1) |
| sPymbed | 0:1387ff3eed4a | 787 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 788 | else |
| sPymbed | 0:1387ff3eed4a | 789 | return 0; |
| sPymbed | 0:1387ff3eed4a | 790 | } |
| sPymbed | 0:1387ff3eed4a | 791 | |
| sPymbed | 0:1387ff3eed4a | 792 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 793 | { |
| sPymbed | 0:1387ff3eed4a | 794 | rtp_sig_mutex_free(*m); |
| sPymbed | 0:1387ff3eed4a | 795 | return 0; |
| sPymbed | 0:1387ff3eed4a | 796 | } |
| sPymbed | 0:1387ff3eed4a | 797 | |
| sPymbed | 0:1387ff3eed4a | 798 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 799 | { |
| sPymbed | 0:1387ff3eed4a | 800 | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0) |
| sPymbed | 0:1387ff3eed4a | 801 | return 0; |
| sPymbed | 0:1387ff3eed4a | 802 | else |
| sPymbed | 0:1387ff3eed4a | 803 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 804 | } |
| sPymbed | 0:1387ff3eed4a | 805 | |
| sPymbed | 0:1387ff3eed4a | 806 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 807 | { |
| sPymbed | 0:1387ff3eed4a | 808 | rtp_sig_mutex_release(*m); |
| sPymbed | 0:1387ff3eed4a | 809 | return 0; |
| sPymbed | 0:1387ff3eed4a | 810 | } |
| sPymbed | 0:1387ff3eed4a | 811 | |
| sPymbed | 0:1387ff3eed4a | 812 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
| sPymbed | 0:1387ff3eed4a | 813 | |
| sPymbed | 0:1387ff3eed4a | 814 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 815 | { |
| sPymbed | 0:1387ff3eed4a | 816 | if (_mutex_init(m, NULL) == MQX_EOK) |
| sPymbed | 0:1387ff3eed4a | 817 | return 0; |
| sPymbed | 0:1387ff3eed4a | 818 | else |
| sPymbed | 0:1387ff3eed4a | 819 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 820 | } |
| sPymbed | 0:1387ff3eed4a | 821 | |
| sPymbed | 0:1387ff3eed4a | 822 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 823 | { |
| sPymbed | 0:1387ff3eed4a | 824 | if (_mutex_destroy(m) == MQX_EOK) |
| sPymbed | 0:1387ff3eed4a | 825 | return 0; |
| sPymbed | 0:1387ff3eed4a | 826 | else |
| sPymbed | 0:1387ff3eed4a | 827 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 828 | } |
| sPymbed | 0:1387ff3eed4a | 829 | |
| sPymbed | 0:1387ff3eed4a | 830 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 831 | { |
| sPymbed | 0:1387ff3eed4a | 832 | if (_mutex_lock(m) == MQX_EOK) |
| sPymbed | 0:1387ff3eed4a | 833 | return 0; |
| sPymbed | 0:1387ff3eed4a | 834 | else |
| sPymbed | 0:1387ff3eed4a | 835 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 836 | } |
| sPymbed | 0:1387ff3eed4a | 837 | |
| sPymbed | 0:1387ff3eed4a | 838 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 839 | { |
| sPymbed | 0:1387ff3eed4a | 840 | if (_mutex_unlock(m) == MQX_EOK) |
| sPymbed | 0:1387ff3eed4a | 841 | return 0; |
| sPymbed | 0:1387ff3eed4a | 842 | else |
| sPymbed | 0:1387ff3eed4a | 843 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 844 | } |
| sPymbed | 0:1387ff3eed4a | 845 | |
| sPymbed | 0:1387ff3eed4a | 846 | #elif defined(WOLFSSL_TIRTOS) |
| sPymbed | 0:1387ff3eed4a | 847 | #include <xdc/runtime/Error.h> |
| sPymbed | 0:1387ff3eed4a | 848 | |
| sPymbed | 0:1387ff3eed4a | 849 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 850 | { |
| sPymbed | 0:1387ff3eed4a | 851 | Semaphore_Params params; |
| sPymbed | 0:1387ff3eed4a | 852 | Error_Block eb; |
| sPymbed | 0:1387ff3eed4a | 853 | |
| sPymbed | 0:1387ff3eed4a | 854 | Error_init(&eb); |
| sPymbed | 0:1387ff3eed4a | 855 | Semaphore_Params_init(¶ms); |
| sPymbed | 0:1387ff3eed4a | 856 | params.mode = Semaphore_Mode_BINARY; |
| sPymbed | 0:1387ff3eed4a | 857 | |
| sPymbed | 0:1387ff3eed4a | 858 | *m = Semaphore_create(1, ¶ms, &eb); |
| sPymbed | 0:1387ff3eed4a | 859 | if (Error_check(&eb)) { |
| sPymbed | 0:1387ff3eed4a | 860 | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.", |
| sPymbed | 0:1387ff3eed4a | 861 | NULL); |
| sPymbed | 0:1387ff3eed4a | 862 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 863 | } |
| sPymbed | 0:1387ff3eed4a | 864 | else |
| sPymbed | 0:1387ff3eed4a | 865 | return 0; |
| sPymbed | 0:1387ff3eed4a | 866 | } |
| sPymbed | 0:1387ff3eed4a | 867 | |
| sPymbed | 0:1387ff3eed4a | 868 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 869 | { |
| sPymbed | 0:1387ff3eed4a | 870 | Semaphore_delete(m); |
| sPymbed | 0:1387ff3eed4a | 871 | |
| sPymbed | 0:1387ff3eed4a | 872 | return 0; |
| sPymbed | 0:1387ff3eed4a | 873 | } |
| sPymbed | 0:1387ff3eed4a | 874 | |
| sPymbed | 0:1387ff3eed4a | 875 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 876 | { |
| sPymbed | 0:1387ff3eed4a | 877 | Semaphore_pend(*m, BIOS_WAIT_FOREVER); |
| sPymbed | 0:1387ff3eed4a | 878 | |
| sPymbed | 0:1387ff3eed4a | 879 | return 0; |
| sPymbed | 0:1387ff3eed4a | 880 | } |
| sPymbed | 0:1387ff3eed4a | 881 | |
| sPymbed | 0:1387ff3eed4a | 882 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 883 | { |
| sPymbed | 0:1387ff3eed4a | 884 | Semaphore_post(*m); |
| sPymbed | 0:1387ff3eed4a | 885 | |
| sPymbed | 0:1387ff3eed4a | 886 | return 0; |
| sPymbed | 0:1387ff3eed4a | 887 | } |
| sPymbed | 0:1387ff3eed4a | 888 | |
| sPymbed | 0:1387ff3eed4a | 889 | #elif defined(WOLFSSL_uITRON4) |
| sPymbed | 0:1387ff3eed4a | 890 | |
| sPymbed | 0:1387ff3eed4a | 891 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 892 | { |
| sPymbed | 0:1387ff3eed4a | 893 | int iReturn; |
| sPymbed | 0:1387ff3eed4a | 894 | m->sem.sematr = TA_TFIFO; |
| sPymbed | 0:1387ff3eed4a | 895 | m->sem.isemcnt = 1; |
| sPymbed | 0:1387ff3eed4a | 896 | m->sem.maxsem = 1; |
| sPymbed | 0:1387ff3eed4a | 897 | m->sem.name = NULL; |
| sPymbed | 0:1387ff3eed4a | 898 | |
| sPymbed | 0:1387ff3eed4a | 899 | m->id = acre_sem(&m->sem); |
| sPymbed | 0:1387ff3eed4a | 900 | if( m->id != E_OK ) |
| sPymbed | 0:1387ff3eed4a | 901 | iReturn = 0; |
| sPymbed | 0:1387ff3eed4a | 902 | else |
| sPymbed | 0:1387ff3eed4a | 903 | iReturn = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 904 | |
| sPymbed | 0:1387ff3eed4a | 905 | return iReturn; |
| sPymbed | 0:1387ff3eed4a | 906 | } |
| sPymbed | 0:1387ff3eed4a | 907 | |
| sPymbed | 0:1387ff3eed4a | 908 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 909 | { |
| sPymbed | 0:1387ff3eed4a | 910 | del_sem( m->id ); |
| sPymbed | 0:1387ff3eed4a | 911 | return 0; |
| sPymbed | 0:1387ff3eed4a | 912 | } |
| sPymbed | 0:1387ff3eed4a | 913 | |
| sPymbed | 0:1387ff3eed4a | 914 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 915 | { |
| sPymbed | 0:1387ff3eed4a | 916 | wai_sem(m->id); |
| sPymbed | 0:1387ff3eed4a | 917 | return 0; |
| sPymbed | 0:1387ff3eed4a | 918 | } |
| sPymbed | 0:1387ff3eed4a | 919 | |
| sPymbed | 0:1387ff3eed4a | 920 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 921 | { |
| sPymbed | 0:1387ff3eed4a | 922 | sig_sem(m->id); |
| sPymbed | 0:1387ff3eed4a | 923 | return 0; |
| sPymbed | 0:1387ff3eed4a | 924 | } |
| sPymbed | 0:1387ff3eed4a | 925 | |
| sPymbed | 0:1387ff3eed4a | 926 | /**** uITRON malloc/free ***/ |
| sPymbed | 0:1387ff3eed4a | 927 | static ID ID_wolfssl_MPOOL = 0; |
| sPymbed | 0:1387ff3eed4a | 928 | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"}; |
| sPymbed | 0:1387ff3eed4a | 929 | |
| sPymbed | 0:1387ff3eed4a | 930 | int uITRON4_minit(size_t poolsz) { |
| sPymbed | 0:1387ff3eed4a | 931 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 932 | wolfssl_MPOOL.mplsz = poolsz; |
| sPymbed | 0:1387ff3eed4a | 933 | ercd = acre_mpl(&wolfssl_MPOOL); |
| sPymbed | 0:1387ff3eed4a | 934 | if (ercd > 0) { |
| sPymbed | 0:1387ff3eed4a | 935 | ID_wolfssl_MPOOL = ercd; |
| sPymbed | 0:1387ff3eed4a | 936 | return 0; |
| sPymbed | 0:1387ff3eed4a | 937 | } else { |
| sPymbed | 0:1387ff3eed4a | 938 | return -1; |
| sPymbed | 0:1387ff3eed4a | 939 | } |
| sPymbed | 0:1387ff3eed4a | 940 | } |
| sPymbed | 0:1387ff3eed4a | 941 | |
| sPymbed | 0:1387ff3eed4a | 942 | void *uITRON4_malloc(size_t sz) { |
| sPymbed | 0:1387ff3eed4a | 943 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 944 | void *p; |
| sPymbed | 0:1387ff3eed4a | 945 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p); |
| sPymbed | 0:1387ff3eed4a | 946 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 947 | return p; |
| sPymbed | 0:1387ff3eed4a | 948 | } else { |
| sPymbed | 0:1387ff3eed4a | 949 | return 0; |
| sPymbed | 0:1387ff3eed4a | 950 | } |
| sPymbed | 0:1387ff3eed4a | 951 | } |
| sPymbed | 0:1387ff3eed4a | 952 | |
| sPymbed | 0:1387ff3eed4a | 953 | void *uITRON4_realloc(void *p, size_t sz) { |
| sPymbed | 0:1387ff3eed4a | 954 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 955 | void *newp; |
| sPymbed | 0:1387ff3eed4a | 956 | if(p) { |
| sPymbed | 0:1387ff3eed4a | 957 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp); |
| sPymbed | 0:1387ff3eed4a | 958 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 959 | XMEMCPY(newp, p, sz); |
| sPymbed | 0:1387ff3eed4a | 960 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
| sPymbed | 0:1387ff3eed4a | 961 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 962 | return newp; |
| sPymbed | 0:1387ff3eed4a | 963 | } |
| sPymbed | 0:1387ff3eed4a | 964 | } |
| sPymbed | 0:1387ff3eed4a | 965 | } |
| sPymbed | 0:1387ff3eed4a | 966 | return 0; |
| sPymbed | 0:1387ff3eed4a | 967 | } |
| sPymbed | 0:1387ff3eed4a | 968 | |
| sPymbed | 0:1387ff3eed4a | 969 | void uITRON4_free(void *p) { |
| sPymbed | 0:1387ff3eed4a | 970 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 971 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
| sPymbed | 0:1387ff3eed4a | 972 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 973 | return; |
| sPymbed | 0:1387ff3eed4a | 974 | } else { |
| sPymbed | 0:1387ff3eed4a | 975 | return; |
| sPymbed | 0:1387ff3eed4a | 976 | } |
| sPymbed | 0:1387ff3eed4a | 977 | } |
| sPymbed | 0:1387ff3eed4a | 978 | |
| sPymbed | 0:1387ff3eed4a | 979 | #elif defined(WOLFSSL_uTKERNEL2) |
| sPymbed | 0:1387ff3eed4a | 980 | |
| sPymbed | 0:1387ff3eed4a | 981 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 982 | { |
| sPymbed | 0:1387ff3eed4a | 983 | int iReturn; |
| sPymbed | 0:1387ff3eed4a | 984 | m->sem.sematr = TA_TFIFO; |
| sPymbed | 0:1387ff3eed4a | 985 | m->sem.isemcnt = 1; |
| sPymbed | 0:1387ff3eed4a | 986 | m->sem.maxsem = 1; |
| sPymbed | 0:1387ff3eed4a | 987 | |
| sPymbed | 0:1387ff3eed4a | 988 | m->id = tk_cre_sem(&m->sem); |
| sPymbed | 0:1387ff3eed4a | 989 | if( m->id != NULL ) |
| sPymbed | 0:1387ff3eed4a | 990 | iReturn = 0; |
| sPymbed | 0:1387ff3eed4a | 991 | else |
| sPymbed | 0:1387ff3eed4a | 992 | iReturn = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 993 | |
| sPymbed | 0:1387ff3eed4a | 994 | return iReturn; |
| sPymbed | 0:1387ff3eed4a | 995 | } |
| sPymbed | 0:1387ff3eed4a | 996 | |
| sPymbed | 0:1387ff3eed4a | 997 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 998 | { |
| sPymbed | 0:1387ff3eed4a | 999 | tk_del_sem(m->id); |
| sPymbed | 0:1387ff3eed4a | 1000 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1001 | } |
| sPymbed | 0:1387ff3eed4a | 1002 | |
| sPymbed | 0:1387ff3eed4a | 1003 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1004 | { |
| sPymbed | 0:1387ff3eed4a | 1005 | tk_wai_sem(m->id, 1, TMO_FEVR); |
| sPymbed | 0:1387ff3eed4a | 1006 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1007 | } |
| sPymbed | 0:1387ff3eed4a | 1008 | |
| sPymbed | 0:1387ff3eed4a | 1009 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1010 | { |
| sPymbed | 0:1387ff3eed4a | 1011 | tk_sig_sem(m->id, 1); |
| sPymbed | 0:1387ff3eed4a | 1012 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1013 | } |
| sPymbed | 0:1387ff3eed4a | 1014 | |
| sPymbed | 0:1387ff3eed4a | 1015 | /**** uT-Kernel malloc/free ***/ |
| sPymbed | 0:1387ff3eed4a | 1016 | static ID ID_wolfssl_MPOOL = 0; |
| sPymbed | 0:1387ff3eed4a | 1017 | static T_CMPL wolfssl_MPOOL = { |
| sPymbed | 0:1387ff3eed4a | 1018 | NULL, /* Extended information */ |
| sPymbed | 0:1387ff3eed4a | 1019 | TA_TFIFO, /* Memory pool attribute */ |
| sPymbed | 0:1387ff3eed4a | 1020 | 0, /* Size of whole memory pool (byte) */ |
| sPymbed | 0:1387ff3eed4a | 1021 | "wolfSSL" /* Object name (max 8-char) */ |
| sPymbed | 0:1387ff3eed4a | 1022 | }; |
| sPymbed | 0:1387ff3eed4a | 1023 | |
| sPymbed | 0:1387ff3eed4a | 1024 | int uTKernel_init_mpool(unsigned int sz) { |
| sPymbed | 0:1387ff3eed4a | 1025 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 1026 | wolfssl_MPOOL.mplsz = sz; |
| sPymbed | 0:1387ff3eed4a | 1027 | ercd = tk_cre_mpl(&wolfssl_MPOOL); |
| sPymbed | 0:1387ff3eed4a | 1028 | if (ercd > 0) { |
| sPymbed | 0:1387ff3eed4a | 1029 | ID_wolfssl_MPOOL = ercd; |
| sPymbed | 0:1387ff3eed4a | 1030 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1031 | } else { |
| sPymbed | 0:1387ff3eed4a | 1032 | return (int)ercd; |
| sPymbed | 0:1387ff3eed4a | 1033 | } |
| sPymbed | 0:1387ff3eed4a | 1034 | } |
| sPymbed | 0:1387ff3eed4a | 1035 | |
| sPymbed | 0:1387ff3eed4a | 1036 | void *uTKernel_malloc(unsigned int sz) { |
| sPymbed | 0:1387ff3eed4a | 1037 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 1038 | void *p; |
| sPymbed | 0:1387ff3eed4a | 1039 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR); |
| sPymbed | 0:1387ff3eed4a | 1040 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 1041 | return p; |
| sPymbed | 0:1387ff3eed4a | 1042 | } else { |
| sPymbed | 0:1387ff3eed4a | 1043 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1044 | } |
| sPymbed | 0:1387ff3eed4a | 1045 | } |
| sPymbed | 0:1387ff3eed4a | 1046 | |
| sPymbed | 0:1387ff3eed4a | 1047 | void *uTKernel_realloc(void *p, unsigned int sz) { |
| sPymbed | 0:1387ff3eed4a | 1048 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 1049 | void *newp; |
| sPymbed | 0:1387ff3eed4a | 1050 | if (p) { |
| sPymbed | 0:1387ff3eed4a | 1051 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR); |
| sPymbed | 0:1387ff3eed4a | 1052 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 1053 | XMEMCPY(newp, p, sz); |
| sPymbed | 0:1387ff3eed4a | 1054 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
| sPymbed | 0:1387ff3eed4a | 1055 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 1056 | return newp; |
| sPymbed | 0:1387ff3eed4a | 1057 | } |
| sPymbed | 0:1387ff3eed4a | 1058 | } |
| sPymbed | 0:1387ff3eed4a | 1059 | } |
| sPymbed | 0:1387ff3eed4a | 1060 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1061 | } |
| sPymbed | 0:1387ff3eed4a | 1062 | |
| sPymbed | 0:1387ff3eed4a | 1063 | void uTKernel_free(void *p) { |
| sPymbed | 0:1387ff3eed4a | 1064 | ER ercd; |
| sPymbed | 0:1387ff3eed4a | 1065 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
| sPymbed | 0:1387ff3eed4a | 1066 | if (ercd == E_OK) { |
| sPymbed | 0:1387ff3eed4a | 1067 | return; |
| sPymbed | 0:1387ff3eed4a | 1068 | } else { |
| sPymbed | 0:1387ff3eed4a | 1069 | return; |
| sPymbed | 0:1387ff3eed4a | 1070 | } |
| sPymbed | 0:1387ff3eed4a | 1071 | } |
| sPymbed | 0:1387ff3eed4a | 1072 | |
| sPymbed | 0:1387ff3eed4a | 1073 | #elif defined (WOLFSSL_FROSTED) |
| sPymbed | 0:1387ff3eed4a | 1074 | |
| sPymbed | 0:1387ff3eed4a | 1075 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1076 | { |
| sPymbed | 0:1387ff3eed4a | 1077 | *m = mutex_init(); |
| sPymbed | 0:1387ff3eed4a | 1078 | if (*m) |
| sPymbed | 0:1387ff3eed4a | 1079 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1080 | else |
| sPymbed | 0:1387ff3eed4a | 1081 | return -1; |
| sPymbed | 0:1387ff3eed4a | 1082 | } |
| sPymbed | 0:1387ff3eed4a | 1083 | |
| sPymbed | 0:1387ff3eed4a | 1084 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1085 | { |
| sPymbed | 0:1387ff3eed4a | 1086 | mutex_destroy(*m); |
| sPymbed | 0:1387ff3eed4a | 1087 | return(0); |
| sPymbed | 0:1387ff3eed4a | 1088 | } |
| sPymbed | 0:1387ff3eed4a | 1089 | |
| sPymbed | 0:1387ff3eed4a | 1090 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1091 | { |
| sPymbed | 0:1387ff3eed4a | 1092 | mutex_lock(*m); |
| sPymbed | 0:1387ff3eed4a | 1093 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1094 | } |
| sPymbed | 0:1387ff3eed4a | 1095 | |
| sPymbed | 0:1387ff3eed4a | 1096 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1097 | { |
| sPymbed | 0:1387ff3eed4a | 1098 | mutex_unlock(*m); |
| sPymbed | 0:1387ff3eed4a | 1099 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1100 | } |
| sPymbed | 0:1387ff3eed4a | 1101 | |
| sPymbed | 0:1387ff3eed4a | 1102 | #elif defined(WOLFSSL_CMSIS_RTOS) |
| sPymbed | 0:1387ff3eed4a | 1103 | |
| sPymbed | 0:1387ff3eed4a | 1104 | #define CMSIS_NMUTEX 10 |
| sPymbed | 0:1387ff3eed4a | 1105 | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2); |
| sPymbed | 0:1387ff3eed4a | 1106 | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5); |
| sPymbed | 0:1387ff3eed4a | 1107 | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8); |
| sPymbed | 0:1387ff3eed4a | 1108 | osMutexDef(wolfSSL_mt9); |
| sPymbed | 0:1387ff3eed4a | 1109 | |
| sPymbed | 0:1387ff3eed4a | 1110 | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0), |
| sPymbed | 0:1387ff3eed4a | 1111 | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3), |
| sPymbed | 0:1387ff3eed4a | 1112 | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6), |
| sPymbed | 0:1387ff3eed4a | 1113 | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) }; |
| sPymbed | 0:1387ff3eed4a | 1114 | |
| sPymbed | 0:1387ff3eed4a | 1115 | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0}; |
| sPymbed | 0:1387ff3eed4a | 1116 | |
| sPymbed | 0:1387ff3eed4a | 1117 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1118 | { |
| sPymbed | 0:1387ff3eed4a | 1119 | int i; |
| sPymbed | 0:1387ff3eed4a | 1120 | for (i=0; i<CMSIS_NMUTEX; i++) { |
| sPymbed | 0:1387ff3eed4a | 1121 | if(CMSIS_mutexID[i] == 0) { |
| sPymbed | 0:1387ff3eed4a | 1122 | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]); |
| sPymbed | 0:1387ff3eed4a | 1123 | (*m) = CMSIS_mutexID[i]; |
| sPymbed | 0:1387ff3eed4a | 1124 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1125 | } |
| sPymbed | 0:1387ff3eed4a | 1126 | } |
| sPymbed | 0:1387ff3eed4a | 1127 | return -1; |
| sPymbed | 0:1387ff3eed4a | 1128 | } |
| sPymbed | 0:1387ff3eed4a | 1129 | |
| sPymbed | 0:1387ff3eed4a | 1130 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1131 | { |
| sPymbed | 0:1387ff3eed4a | 1132 | int i; |
| sPymbed | 0:1387ff3eed4a | 1133 | osMutexDelete (*m); |
| sPymbed | 0:1387ff3eed4a | 1134 | for (i=0; i<CMSIS_NMUTEX; i++) { |
| sPymbed | 0:1387ff3eed4a | 1135 | if(CMSIS_mutexID[i] == (*m)) { |
| sPymbed | 0:1387ff3eed4a | 1136 | CMSIS_mutexID[i] = 0; |
| sPymbed | 0:1387ff3eed4a | 1137 | return(0); |
| sPymbed | 0:1387ff3eed4a | 1138 | } |
| sPymbed | 0:1387ff3eed4a | 1139 | } |
| sPymbed | 0:1387ff3eed4a | 1140 | return(-1); |
| sPymbed | 0:1387ff3eed4a | 1141 | } |
| sPymbed | 0:1387ff3eed4a | 1142 | |
| sPymbed | 0:1387ff3eed4a | 1143 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1144 | { |
| sPymbed | 0:1387ff3eed4a | 1145 | osMutexWait(*m, osWaitForever); |
| sPymbed | 0:1387ff3eed4a | 1146 | return(0); |
| sPymbed | 0:1387ff3eed4a | 1147 | } |
| sPymbed | 0:1387ff3eed4a | 1148 | |
| sPymbed | 0:1387ff3eed4a | 1149 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1150 | { |
| sPymbed | 0:1387ff3eed4a | 1151 | osMutexRelease (*m); |
| sPymbed | 0:1387ff3eed4a | 1152 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1153 | } |
| sPymbed | 0:1387ff3eed4a | 1154 | |
| sPymbed | 0:1387ff3eed4a | 1155 | #elif defined(WOLFSSL_MDK_ARM) |
| sPymbed | 0:1387ff3eed4a | 1156 | |
| sPymbed | 0:1387ff3eed4a | 1157 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1158 | { |
| sPymbed | 0:1387ff3eed4a | 1159 | os_mut_init (m); |
| sPymbed | 0:1387ff3eed4a | 1160 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1161 | } |
| sPymbed | 0:1387ff3eed4a | 1162 | |
| sPymbed | 0:1387ff3eed4a | 1163 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1164 | { |
| sPymbed | 0:1387ff3eed4a | 1165 | return(0); |
| sPymbed | 0:1387ff3eed4a | 1166 | } |
| sPymbed | 0:1387ff3eed4a | 1167 | |
| sPymbed | 0:1387ff3eed4a | 1168 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1169 | { |
| sPymbed | 0:1387ff3eed4a | 1170 | os_mut_wait (m, 0xffff); |
| sPymbed | 0:1387ff3eed4a | 1171 | return(0); |
| sPymbed | 0:1387ff3eed4a | 1172 | } |
| sPymbed | 0:1387ff3eed4a | 1173 | |
| sPymbed | 0:1387ff3eed4a | 1174 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1175 | { |
| sPymbed | 0:1387ff3eed4a | 1176 | os_mut_release (m); |
| sPymbed | 0:1387ff3eed4a | 1177 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1178 | } |
| sPymbed | 0:1387ff3eed4a | 1179 | |
| sPymbed | 0:1387ff3eed4a | 1180 | #elif defined(INTIME_RTOS) |
| sPymbed | 0:1387ff3eed4a | 1181 | |
| sPymbed | 0:1387ff3eed4a | 1182 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1183 | { |
| sPymbed | 0:1387ff3eed4a | 1184 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 1185 | |
| sPymbed | 0:1387ff3eed4a | 1186 | if (m == NULL) |
| sPymbed | 0:1387ff3eed4a | 1187 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 1188 | |
| sPymbed | 0:1387ff3eed4a | 1189 | *m = CreateRtSemaphore( |
| sPymbed | 0:1387ff3eed4a | 1190 | 1, /* initial unit count */ |
| sPymbed | 0:1387ff3eed4a | 1191 | 1, /* maximum unit count */ |
| sPymbed | 0:1387ff3eed4a | 1192 | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */ |
| sPymbed | 0:1387ff3eed4a | 1193 | ); |
| sPymbed | 0:1387ff3eed4a | 1194 | if (*m == BAD_RTHANDLE) { |
| sPymbed | 0:1387ff3eed4a | 1195 | ret = GetLastRtError(); |
| sPymbed | 0:1387ff3eed4a | 1196 | if (ret != E_OK) |
| sPymbed | 0:1387ff3eed4a | 1197 | ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1198 | } |
| sPymbed | 0:1387ff3eed4a | 1199 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1200 | } |
| sPymbed | 0:1387ff3eed4a | 1201 | |
| sPymbed | 0:1387ff3eed4a | 1202 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1203 | { |
| sPymbed | 0:1387ff3eed4a | 1204 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 1205 | BOOLEAN del; |
| sPymbed | 0:1387ff3eed4a | 1206 | |
| sPymbed | 0:1387ff3eed4a | 1207 | if (m == NULL) |
| sPymbed | 0:1387ff3eed4a | 1208 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 1209 | |
| sPymbed | 0:1387ff3eed4a | 1210 | del = DeleteRtSemaphore( |
| sPymbed | 0:1387ff3eed4a | 1211 | *m /* handle for RT semaphore */ |
| sPymbed | 0:1387ff3eed4a | 1212 | ); |
| sPymbed | 0:1387ff3eed4a | 1213 | if (del != TRUE) |
| sPymbed | 0:1387ff3eed4a | 1214 | ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1215 | |
| sPymbed | 0:1387ff3eed4a | 1216 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1217 | } |
| sPymbed | 0:1387ff3eed4a | 1218 | |
| sPymbed | 0:1387ff3eed4a | 1219 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1220 | { |
| sPymbed | 0:1387ff3eed4a | 1221 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 1222 | DWORD lck; |
| sPymbed | 0:1387ff3eed4a | 1223 | |
| sPymbed | 0:1387ff3eed4a | 1224 | if (m == NULL) |
| sPymbed | 0:1387ff3eed4a | 1225 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 1226 | |
| sPymbed | 0:1387ff3eed4a | 1227 | lck = WaitForRtSemaphore( |
| sPymbed | 0:1387ff3eed4a | 1228 | *m, /* handle for RT semaphore */ |
| sPymbed | 0:1387ff3eed4a | 1229 | 1, /* number of units to wait for */ |
| sPymbed | 0:1387ff3eed4a | 1230 | WAIT_FOREVER /* number of milliseconds to wait for units */ |
| sPymbed | 0:1387ff3eed4a | 1231 | ); |
| sPymbed | 0:1387ff3eed4a | 1232 | if (lck == WAIT_FAILED) { |
| sPymbed | 0:1387ff3eed4a | 1233 | ret = GetLastRtError(); |
| sPymbed | 0:1387ff3eed4a | 1234 | if (ret != E_OK) |
| sPymbed | 0:1387ff3eed4a | 1235 | ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1236 | } |
| sPymbed | 0:1387ff3eed4a | 1237 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1238 | } |
| sPymbed | 0:1387ff3eed4a | 1239 | |
| sPymbed | 0:1387ff3eed4a | 1240 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1241 | { |
| sPymbed | 0:1387ff3eed4a | 1242 | int ret = 0; |
| sPymbed | 0:1387ff3eed4a | 1243 | BOOLEAN rel; |
| sPymbed | 0:1387ff3eed4a | 1244 | |
| sPymbed | 0:1387ff3eed4a | 1245 | if (m == NULL) |
| sPymbed | 0:1387ff3eed4a | 1246 | return BAD_FUNC_ARG; |
| sPymbed | 0:1387ff3eed4a | 1247 | |
| sPymbed | 0:1387ff3eed4a | 1248 | rel = ReleaseRtSemaphore( |
| sPymbed | 0:1387ff3eed4a | 1249 | *m, /* handle for RT semaphore */ |
| sPymbed | 0:1387ff3eed4a | 1250 | 1 /* number of units to release to semaphore */ |
| sPymbed | 0:1387ff3eed4a | 1251 | ); |
| sPymbed | 0:1387ff3eed4a | 1252 | if (rel != TRUE) |
| sPymbed | 0:1387ff3eed4a | 1253 | ret = BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1254 | |
| sPymbed | 0:1387ff3eed4a | 1255 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1256 | } |
| sPymbed | 0:1387ff3eed4a | 1257 | |
| sPymbed | 0:1387ff3eed4a | 1258 | #elif defined(WOLFSSL_NUCLEUS_1_2) |
| sPymbed | 0:1387ff3eed4a | 1259 | |
| sPymbed | 0:1387ff3eed4a | 1260 | int wc_InitMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1261 | { |
| sPymbed | 0:1387ff3eed4a | 1262 | /* Call the Nucleus function to create the semaphore */ |
| sPymbed | 0:1387ff3eed4a | 1263 | if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1, |
| sPymbed | 0:1387ff3eed4a | 1264 | NU_PRIORITY) == NU_SUCCESS) { |
| sPymbed | 0:1387ff3eed4a | 1265 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1266 | } |
| sPymbed | 0:1387ff3eed4a | 1267 | |
| sPymbed | 0:1387ff3eed4a | 1268 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1269 | } |
| sPymbed | 0:1387ff3eed4a | 1270 | |
| sPymbed | 0:1387ff3eed4a | 1271 | int wc_FreeMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1272 | { |
| sPymbed | 0:1387ff3eed4a | 1273 | if (NU_Delete_Semaphore(m) == NU_SUCCESS) |
| sPymbed | 0:1387ff3eed4a | 1274 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1275 | |
| sPymbed | 0:1387ff3eed4a | 1276 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1277 | } |
| sPymbed | 0:1387ff3eed4a | 1278 | |
| sPymbed | 0:1387ff3eed4a | 1279 | int wc_LockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1280 | { |
| sPymbed | 0:1387ff3eed4a | 1281 | /* passing suspend task option */ |
| sPymbed | 0:1387ff3eed4a | 1282 | if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS) |
| sPymbed | 0:1387ff3eed4a | 1283 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1284 | |
| sPymbed | 0:1387ff3eed4a | 1285 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1286 | } |
| sPymbed | 0:1387ff3eed4a | 1287 | |
| sPymbed | 0:1387ff3eed4a | 1288 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
| sPymbed | 0:1387ff3eed4a | 1289 | { |
| sPymbed | 0:1387ff3eed4a | 1290 | if (NU_Release_Semaphore(m) == NU_SUCCESS) |
| sPymbed | 0:1387ff3eed4a | 1291 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1292 | |
| sPymbed | 0:1387ff3eed4a | 1293 | return BAD_MUTEX_E; |
| sPymbed | 0:1387ff3eed4a | 1294 | } |
| sPymbed | 0:1387ff3eed4a | 1295 | |
| sPymbed | 0:1387ff3eed4a | 1296 | #else |
| sPymbed | 0:1387ff3eed4a | 1297 | #warning No mutex handling defined |
| sPymbed | 0:1387ff3eed4a | 1298 | |
| sPymbed | 0:1387ff3eed4a | 1299 | #endif |
| sPymbed | 0:1387ff3eed4a | 1300 | |
| sPymbed | 0:1387ff3eed4a | 1301 | #ifndef NO_ASN_TIME |
| sPymbed | 0:1387ff3eed4a | 1302 | #if defined(_WIN32_WCE) |
| sPymbed | 0:1387ff3eed4a | 1303 | time_t windows_time(time_t* timer) |
| sPymbed | 0:1387ff3eed4a | 1304 | { |
| sPymbed | 0:1387ff3eed4a | 1305 | SYSTEMTIME sysTime; |
| sPymbed | 0:1387ff3eed4a | 1306 | FILETIME fTime; |
| sPymbed | 0:1387ff3eed4a | 1307 | ULARGE_INTEGER intTime; |
| sPymbed | 0:1387ff3eed4a | 1308 | time_t localTime; |
| sPymbed | 0:1387ff3eed4a | 1309 | |
| sPymbed | 0:1387ff3eed4a | 1310 | if (timer == NULL) |
| sPymbed | 0:1387ff3eed4a | 1311 | timer = &localTime; |
| sPymbed | 0:1387ff3eed4a | 1312 | |
| sPymbed | 0:1387ff3eed4a | 1313 | GetSystemTime(&sysTime); |
| sPymbed | 0:1387ff3eed4a | 1314 | SystemTimeToFileTime(&sysTime, &fTime); |
| sPymbed | 0:1387ff3eed4a | 1315 | |
| sPymbed | 0:1387ff3eed4a | 1316 | XMEMCPY(&intTime, &fTime, sizeof(FILETIME)); |
| sPymbed | 0:1387ff3eed4a | 1317 | /* subtract EPOCH */ |
| sPymbed | 0:1387ff3eed4a | 1318 | intTime.QuadPart -= 0x19db1ded53e8000; |
| sPymbed | 0:1387ff3eed4a | 1319 | /* to secs */ |
| sPymbed | 0:1387ff3eed4a | 1320 | intTime.QuadPart /= 10000000; |
| sPymbed | 0:1387ff3eed4a | 1321 | *timer = (time_t)intTime.QuadPart; |
| sPymbed | 0:1387ff3eed4a | 1322 | |
| sPymbed | 0:1387ff3eed4a | 1323 | return *timer; |
| sPymbed | 0:1387ff3eed4a | 1324 | } |
| sPymbed | 0:1387ff3eed4a | 1325 | #endif /* _WIN32_WCE */ |
| sPymbed | 0:1387ff3eed4a | 1326 | |
| sPymbed | 0:1387ff3eed4a | 1327 | #if defined(WOLFSSL_GMTIME) |
| sPymbed | 0:1387ff3eed4a | 1328 | struct tm* gmtime(const time_t* timer) |
| sPymbed | 0:1387ff3eed4a | 1329 | { |
| sPymbed | 0:1387ff3eed4a | 1330 | #define YEAR0 1900 |
| sPymbed | 0:1387ff3eed4a | 1331 | #define EPOCH_YEAR 1970 |
| sPymbed | 0:1387ff3eed4a | 1332 | #define SECS_DAY (24L * 60L * 60L) |
| sPymbed | 0:1387ff3eed4a | 1333 | #define LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) %400))) |
| sPymbed | 0:1387ff3eed4a | 1334 | #define YEARSIZE(year) (LEAPYEAR(year) ? 366 : 365) |
| sPymbed | 0:1387ff3eed4a | 1335 | |
| sPymbed | 0:1387ff3eed4a | 1336 | static const int _ytab[2][12] = |
| sPymbed | 0:1387ff3eed4a | 1337 | { |
| sPymbed | 0:1387ff3eed4a | 1338 | {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, |
| sPymbed | 0:1387ff3eed4a | 1339 | {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} |
| sPymbed | 0:1387ff3eed4a | 1340 | }; |
| sPymbed | 0:1387ff3eed4a | 1341 | |
| sPymbed | 0:1387ff3eed4a | 1342 | static struct tm st_time; |
| sPymbed | 0:1387ff3eed4a | 1343 | struct tm* ret = &st_time; |
| sPymbed | 0:1387ff3eed4a | 1344 | time_t secs = *timer; |
| sPymbed | 0:1387ff3eed4a | 1345 | unsigned long dayclock, dayno; |
| sPymbed | 0:1387ff3eed4a | 1346 | int year = EPOCH_YEAR; |
| sPymbed | 0:1387ff3eed4a | 1347 | |
| sPymbed | 0:1387ff3eed4a | 1348 | dayclock = (unsigned long)secs % SECS_DAY; |
| sPymbed | 0:1387ff3eed4a | 1349 | dayno = (unsigned long)secs / SECS_DAY; |
| sPymbed | 0:1387ff3eed4a | 1350 | |
| sPymbed | 0:1387ff3eed4a | 1351 | ret->tm_sec = (int) dayclock % 60; |
| sPymbed | 0:1387ff3eed4a | 1352 | ret->tm_min = (int)(dayclock % 3600) / 60; |
| sPymbed | 0:1387ff3eed4a | 1353 | ret->tm_hour = (int) dayclock / 3600; |
| sPymbed | 0:1387ff3eed4a | 1354 | ret->tm_wday = (int) (dayno + 4) % 7; /* day 0 a Thursday */ |
| sPymbed | 0:1387ff3eed4a | 1355 | |
| sPymbed | 0:1387ff3eed4a | 1356 | while(dayno >= (unsigned long)YEARSIZE(year)) { |
| sPymbed | 0:1387ff3eed4a | 1357 | dayno -= YEARSIZE(year); |
| sPymbed | 0:1387ff3eed4a | 1358 | year++; |
| sPymbed | 0:1387ff3eed4a | 1359 | } |
| sPymbed | 0:1387ff3eed4a | 1360 | |
| sPymbed | 0:1387ff3eed4a | 1361 | ret->tm_year = year - YEAR0; |
| sPymbed | 0:1387ff3eed4a | 1362 | ret->tm_yday = (int)dayno; |
| sPymbed | 0:1387ff3eed4a | 1363 | ret->tm_mon = 0; |
| sPymbed | 0:1387ff3eed4a | 1364 | |
| sPymbed | 0:1387ff3eed4a | 1365 | while(dayno >= (unsigned long)_ytab[LEAPYEAR(year)][ret->tm_mon]) { |
| sPymbed | 0:1387ff3eed4a | 1366 | dayno -= _ytab[LEAPYEAR(year)][ret->tm_mon]; |
| sPymbed | 0:1387ff3eed4a | 1367 | ret->tm_mon++; |
| sPymbed | 0:1387ff3eed4a | 1368 | } |
| sPymbed | 0:1387ff3eed4a | 1369 | |
| sPymbed | 0:1387ff3eed4a | 1370 | ret->tm_mday = (int)++dayno; |
| sPymbed | 0:1387ff3eed4a | 1371 | ret->tm_isdst = 0; |
| sPymbed | 0:1387ff3eed4a | 1372 | |
| sPymbed | 0:1387ff3eed4a | 1373 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1374 | } |
| sPymbed | 0:1387ff3eed4a | 1375 | #endif /* WOLFSSL_GMTIME */ |
| sPymbed | 0:1387ff3eed4a | 1376 | |
| sPymbed | 0:1387ff3eed4a | 1377 | |
| sPymbed | 0:1387ff3eed4a | 1378 | #if defined(HAVE_RTP_SYS) |
| sPymbed | 0:1387ff3eed4a | 1379 | #define YEAR0 1900 |
| sPymbed | 0:1387ff3eed4a | 1380 | |
| sPymbed | 0:1387ff3eed4a | 1381 | struct tm* rtpsys_gmtime(const time_t* timer) /* has a gmtime() but hangs */ |
| sPymbed | 0:1387ff3eed4a | 1382 | { |
| sPymbed | 0:1387ff3eed4a | 1383 | static struct tm st_time; |
| sPymbed | 0:1387ff3eed4a | 1384 | struct tm* ret = &st_time; |
| sPymbed | 0:1387ff3eed4a | 1385 | |
| sPymbed | 0:1387ff3eed4a | 1386 | DC_RTC_CALENDAR cal; |
| sPymbed | 0:1387ff3eed4a | 1387 | dc_rtc_time_get(&cal, TRUE); |
| sPymbed | 0:1387ff3eed4a | 1388 | |
| sPymbed | 0:1387ff3eed4a | 1389 | ret->tm_year = cal.year - YEAR0; /* gm starts at 1900 */ |
| sPymbed | 0:1387ff3eed4a | 1390 | ret->tm_mon = cal.month - 1; /* gm starts at 0 */ |
| sPymbed | 0:1387ff3eed4a | 1391 | ret->tm_mday = cal.day; |
| sPymbed | 0:1387ff3eed4a | 1392 | ret->tm_hour = cal.hour; |
| sPymbed | 0:1387ff3eed4a | 1393 | ret->tm_min = cal.minute; |
| sPymbed | 0:1387ff3eed4a | 1394 | ret->tm_sec = cal.second; |
| sPymbed | 0:1387ff3eed4a | 1395 | |
| sPymbed | 0:1387ff3eed4a | 1396 | return ret; |
| sPymbed | 0:1387ff3eed4a | 1397 | } |
| sPymbed | 0:1387ff3eed4a | 1398 | |
| sPymbed | 0:1387ff3eed4a | 1399 | #endif /* HAVE_RTP_SYS */ |
| sPymbed | 0:1387ff3eed4a | 1400 | |
| sPymbed | 0:1387ff3eed4a | 1401 | |
| sPymbed | 0:1387ff3eed4a | 1402 | #if defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP) |
| sPymbed | 0:1387ff3eed4a | 1403 | |
| sPymbed | 0:1387ff3eed4a | 1404 | /* |
| sPymbed | 0:1387ff3eed4a | 1405 | * time() is just a stub in Microchip libraries. We need our own |
| sPymbed | 0:1387ff3eed4a | 1406 | * implementation. Use SNTP client to get seconds since epoch. |
| sPymbed | 0:1387ff3eed4a | 1407 | */ |
| sPymbed | 0:1387ff3eed4a | 1408 | time_t pic32_time(time_t* timer) |
| sPymbed | 0:1387ff3eed4a | 1409 | { |
| sPymbed | 0:1387ff3eed4a | 1410 | #ifdef MICROCHIP_TCPIP_V5 |
| sPymbed | 0:1387ff3eed4a | 1411 | DWORD sec = 0; |
| sPymbed | 0:1387ff3eed4a | 1412 | #else |
| sPymbed | 0:1387ff3eed4a | 1413 | uint32_t sec = 0; |
| sPymbed | 0:1387ff3eed4a | 1414 | #endif |
| sPymbed | 0:1387ff3eed4a | 1415 | time_t localTime; |
| sPymbed | 0:1387ff3eed4a | 1416 | |
| sPymbed | 0:1387ff3eed4a | 1417 | if (timer == NULL) |
| sPymbed | 0:1387ff3eed4a | 1418 | timer = &localTime; |
| sPymbed | 0:1387ff3eed4a | 1419 | |
| sPymbed | 0:1387ff3eed4a | 1420 | #ifdef MICROCHIP_MPLAB_HARMONY |
| sPymbed | 0:1387ff3eed4a | 1421 | sec = TCPIP_SNTP_UTCSecondsGet(); |
| sPymbed | 0:1387ff3eed4a | 1422 | #else |
| sPymbed | 0:1387ff3eed4a | 1423 | sec = SNTPGetUTCSeconds(); |
| sPymbed | 0:1387ff3eed4a | 1424 | #endif |
| sPymbed | 0:1387ff3eed4a | 1425 | *timer = (time_t) sec; |
| sPymbed | 0:1387ff3eed4a | 1426 | |
| sPymbed | 0:1387ff3eed4a | 1427 | return *timer; |
| sPymbed | 0:1387ff3eed4a | 1428 | } |
| sPymbed | 0:1387ff3eed4a | 1429 | |
| sPymbed | 0:1387ff3eed4a | 1430 | #endif /* MICROCHIP_TCPIP || MICROCHIP_TCPIP_V5 */ |
| sPymbed | 0:1387ff3eed4a | 1431 | |
| sPymbed | 0:1387ff3eed4a | 1432 | #if defined(MICRIUM) |
| sPymbed | 0:1387ff3eed4a | 1433 | |
| sPymbed | 0:1387ff3eed4a | 1434 | time_t micrium_time(time_t* timer) |
| sPymbed | 0:1387ff3eed4a | 1435 | { |
| sPymbed | 0:1387ff3eed4a | 1436 | CLK_TS_SEC sec; |
| sPymbed | 0:1387ff3eed4a | 1437 | |
| sPymbed | 0:1387ff3eed4a | 1438 | Clk_GetTS_Unix(&sec); |
| sPymbed | 0:1387ff3eed4a | 1439 | |
| sPymbed | 0:1387ff3eed4a | 1440 | return (time_t) sec; |
| sPymbed | 0:1387ff3eed4a | 1441 | } |
| sPymbed | 0:1387ff3eed4a | 1442 | |
| sPymbed | 0:1387ff3eed4a | 1443 | #endif /* MICRIUM */ |
| sPymbed | 0:1387ff3eed4a | 1444 | |
| sPymbed | 0:1387ff3eed4a | 1445 | #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
| sPymbed | 0:1387ff3eed4a | 1446 | |
| sPymbed | 0:1387ff3eed4a | 1447 | time_t mqx_time(time_t* timer) |
| sPymbed | 0:1387ff3eed4a | 1448 | { |
| sPymbed | 0:1387ff3eed4a | 1449 | time_t localTime; |
| sPymbed | 0:1387ff3eed4a | 1450 | TIME_STRUCT time_s; |
| sPymbed | 0:1387ff3eed4a | 1451 | |
| sPymbed | 0:1387ff3eed4a | 1452 | if (timer == NULL) |
| sPymbed | 0:1387ff3eed4a | 1453 | timer = &localTime; |
| sPymbed | 0:1387ff3eed4a | 1454 | |
| sPymbed | 0:1387ff3eed4a | 1455 | _time_get(&time_s); |
| sPymbed | 0:1387ff3eed4a | 1456 | *timer = (time_t) time_s.SECONDS; |
| sPymbed | 0:1387ff3eed4a | 1457 | |
| sPymbed | 0:1387ff3eed4a | 1458 | return *timer; |
| sPymbed | 0:1387ff3eed4a | 1459 | } |
| sPymbed | 0:1387ff3eed4a | 1460 | |
| sPymbed | 0:1387ff3eed4a | 1461 | #endif /* FREESCALE_MQX || FREESCALE_KSDK_MQX */ |
| sPymbed | 0:1387ff3eed4a | 1462 | |
| sPymbed | 0:1387ff3eed4a | 1463 | |
| sPymbed | 0:1387ff3eed4a | 1464 | #if defined(WOLFSSL_TIRTOS) |
| sPymbed | 0:1387ff3eed4a | 1465 | |
| sPymbed | 0:1387ff3eed4a | 1466 | time_t XTIME(time_t * timer) |
| sPymbed | 0:1387ff3eed4a | 1467 | { |
| sPymbed | 0:1387ff3eed4a | 1468 | time_t sec = 0; |
| sPymbed | 0:1387ff3eed4a | 1469 | |
| sPymbed | 0:1387ff3eed4a | 1470 | sec = (time_t) Seconds_get(); |
| sPymbed | 0:1387ff3eed4a | 1471 | |
| sPymbed | 0:1387ff3eed4a | 1472 | if (timer != NULL) |
| sPymbed | 0:1387ff3eed4a | 1473 | *timer = sec; |
| sPymbed | 0:1387ff3eed4a | 1474 | |
| sPymbed | 0:1387ff3eed4a | 1475 | return sec; |
| sPymbed | 0:1387ff3eed4a | 1476 | } |
| sPymbed | 0:1387ff3eed4a | 1477 | |
| sPymbed | 0:1387ff3eed4a | 1478 | #endif /* WOLFSSL_TIRTOS */ |
| sPymbed | 0:1387ff3eed4a | 1479 | |
| sPymbed | 0:1387ff3eed4a | 1480 | #if defined(WOLFSSL_XILINX) |
| sPymbed | 0:1387ff3eed4a | 1481 | #include "xrtcpsu.h" |
| sPymbed | 0:1387ff3eed4a | 1482 | |
| sPymbed | 0:1387ff3eed4a | 1483 | time_t XTIME(time_t * timer) |
| sPymbed | 0:1387ff3eed4a | 1484 | { |
| sPymbed | 0:1387ff3eed4a | 1485 | time_t sec = 0; |
| sPymbed | 0:1387ff3eed4a | 1486 | XRtcPsu_Config* con; |
| sPymbed | 0:1387ff3eed4a | 1487 | XRtcPsu rtc; |
| sPymbed | 0:1387ff3eed4a | 1488 | |
| sPymbed | 0:1387ff3eed4a | 1489 | con = XRtcPsu_LookupConfig(XPAR_XRTCPSU_0_DEVICE_ID); |
| sPymbed | 0:1387ff3eed4a | 1490 | if (con != NULL) { |
| sPymbed | 0:1387ff3eed4a | 1491 | if (XRtcPsu_CfgInitialize(&rtc, con, con->BaseAddr) == XST_SUCCESS) { |
| sPymbed | 0:1387ff3eed4a | 1492 | sec = (time_t)XRtcPsu_GetCurrentTime(&rtc); |
| sPymbed | 0:1387ff3eed4a | 1493 | } |
| sPymbed | 0:1387ff3eed4a | 1494 | else { |
| sPymbed | 0:1387ff3eed4a | 1495 | WOLFSSL_MSG("Unable to initialize RTC"); |
| sPymbed | 0:1387ff3eed4a | 1496 | } |
| sPymbed | 0:1387ff3eed4a | 1497 | } |
| sPymbed | 0:1387ff3eed4a | 1498 | |
| sPymbed | 0:1387ff3eed4a | 1499 | if (timer != NULL) |
| sPymbed | 0:1387ff3eed4a | 1500 | *timer = sec; |
| sPymbed | 0:1387ff3eed4a | 1501 | |
| sPymbed | 0:1387ff3eed4a | 1502 | return sec; |
| sPymbed | 0:1387ff3eed4a | 1503 | } |
| sPymbed | 0:1387ff3eed4a | 1504 | |
| sPymbed | 0:1387ff3eed4a | 1505 | #endif /* WOLFSSL_XILINX */ |
| sPymbed | 0:1387ff3eed4a | 1506 | #endif /* !NO_ASN_TIME */ |
| sPymbed | 0:1387ff3eed4a | 1507 | |
| sPymbed | 0:1387ff3eed4a | 1508 | #ifndef WOLFSSL_LEANPSK |
| sPymbed | 0:1387ff3eed4a | 1509 | char* mystrnstr(const char* s1, const char* s2, unsigned int n) |
| sPymbed | 0:1387ff3eed4a | 1510 | { |
| sPymbed | 0:1387ff3eed4a | 1511 | unsigned int s2_len = (unsigned int)XSTRLEN(s2); |
| sPymbed | 0:1387ff3eed4a | 1512 | |
| sPymbed | 0:1387ff3eed4a | 1513 | if (s2_len == 0) |
| sPymbed | 0:1387ff3eed4a | 1514 | return (char*)s1; |
| sPymbed | 0:1387ff3eed4a | 1515 | |
| sPymbed | 0:1387ff3eed4a | 1516 | while (n >= s2_len && s1[0]) { |
| sPymbed | 0:1387ff3eed4a | 1517 | if (s1[0] == s2[0]) |
| sPymbed | 0:1387ff3eed4a | 1518 | if (XMEMCMP(s1, s2, s2_len) == 0) |
| sPymbed | 0:1387ff3eed4a | 1519 | return (char*)s1; |
| sPymbed | 0:1387ff3eed4a | 1520 | s1++; |
| sPymbed | 0:1387ff3eed4a | 1521 | n--; |
| sPymbed | 0:1387ff3eed4a | 1522 | } |
| sPymbed | 0:1387ff3eed4a | 1523 | |
| sPymbed | 0:1387ff3eed4a | 1524 | return NULL; |
| sPymbed | 0:1387ff3eed4a | 1525 | } |
| sPymbed | 0:1387ff3eed4a | 1526 | #endif |
| sPymbed | 0:1387ff3eed4a | 1527 | |
| sPymbed | 0:1387ff3eed4a | 1528 | /* custom memory wrappers */ |
| sPymbed | 0:1387ff3eed4a | 1529 | #ifdef WOLFSSL_NUCLEUS_1_2 |
| sPymbed | 0:1387ff3eed4a | 1530 | |
| sPymbed | 0:1387ff3eed4a | 1531 | /* system memory pool */ |
| sPymbed | 0:1387ff3eed4a | 1532 | extern NU_MEMORY_POOL System_Memory; |
| sPymbed | 0:1387ff3eed4a | 1533 | |
| sPymbed | 0:1387ff3eed4a | 1534 | void* nucleus_malloc(unsigned long size, void* heap, int type) |
| sPymbed | 0:1387ff3eed4a | 1535 | { |
| sPymbed | 0:1387ff3eed4a | 1536 | STATUS status; |
| sPymbed | 0:1387ff3eed4a | 1537 | void* stack_ptr; |
| sPymbed | 0:1387ff3eed4a | 1538 | |
| sPymbed | 0:1387ff3eed4a | 1539 | status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size, |
| sPymbed | 0:1387ff3eed4a | 1540 | NU_NO_SUSPEND); |
| sPymbed | 0:1387ff3eed4a | 1541 | if (status == NU_SUCCESS) { |
| sPymbed | 0:1387ff3eed4a | 1542 | return 0; |
| sPymbed | 0:1387ff3eed4a | 1543 | } else { |
| sPymbed | 0:1387ff3eed4a | 1544 | return stack_ptr; |
| sPymbed | 0:1387ff3eed4a | 1545 | } |
| sPymbed | 0:1387ff3eed4a | 1546 | } |
| sPymbed | 0:1387ff3eed4a | 1547 | |
| sPymbed | 0:1387ff3eed4a | 1548 | void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type) |
| sPymbed | 0:1387ff3eed4a | 1549 | { |
| sPymbed | 0:1387ff3eed4a | 1550 | STATUS status; |
| sPymbed | 0:1387ff3eed4a | 1551 | DM_HEADER* old_header; |
| sPymbed | 0:1387ff3eed4a | 1552 | word32 old_size, copy_size; |
| sPymbed | 0:1387ff3eed4a | 1553 | void* new_mem; |
| sPymbed | 0:1387ff3eed4a | 1554 | |
| sPymbed | 0:1387ff3eed4a | 1555 | /* if ptr is NULL, behave like malloc */ |
| sPymbed | 0:1387ff3eed4a | 1556 | new_mem = nucleus_malloc(size, NULL, 0); |
| sPymbed | 0:1387ff3eed4a | 1557 | if (new_mem == 0 || ptr == 0) { |
| sPymbed | 0:1387ff3eed4a | 1558 | return new_mem; |
| sPymbed | 0:1387ff3eed4a | 1559 | } |
| sPymbed | 0:1387ff3eed4a | 1560 | |
| sPymbed | 0:1387ff3eed4a | 1561 | /* calculate old memory block size */ |
| sPymbed | 0:1387ff3eed4a | 1562 | /* mem pointers stored in block headers (ref dm_defs.h) */ |
| sPymbed | 0:1387ff3eed4a | 1563 | old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD); |
| sPymbed | 0:1387ff3eed4a | 1564 | old_size = (byte*)old_header->dm_next_memory - (byte*)ptr; |
| sPymbed | 0:1387ff3eed4a | 1565 | |
| sPymbed | 0:1387ff3eed4a | 1566 | /* copy old to new */ |
| sPymbed | 0:1387ff3eed4a | 1567 | if (old_size < size) { |
| sPymbed | 0:1387ff3eed4a | 1568 | copy_size = old_size; |
| sPymbed | 0:1387ff3eed4a | 1569 | } else { |
| sPymbed | 0:1387ff3eed4a | 1570 | copy_size = size; |
| sPymbed | 0:1387ff3eed4a | 1571 | } |
| sPymbed | 0:1387ff3eed4a | 1572 | XMEMCPY(new_mem, ptr, copy_size); |
| sPymbed | 0:1387ff3eed4a | 1573 | |
| sPymbed | 0:1387ff3eed4a | 1574 | /* free old */ |
| sPymbed | 0:1387ff3eed4a | 1575 | nucleus_free(ptr, NULL, 0); |
| sPymbed | 0:1387ff3eed4a | 1576 | |
| sPymbed | 0:1387ff3eed4a | 1577 | return new_mem; |
| sPymbed | 0:1387ff3eed4a | 1578 | } |
| sPymbed | 0:1387ff3eed4a | 1579 | |
| sPymbed | 0:1387ff3eed4a | 1580 | void nucleus_free(void* ptr, void* heap, int type) |
| sPymbed | 0:1387ff3eed4a | 1581 | { |
| sPymbed | 0:1387ff3eed4a | 1582 | if (ptr != NULL) |
| sPymbed | 0:1387ff3eed4a | 1583 | NU_Deallocate_Memory(ptr); |
| sPymbed | 0:1387ff3eed4a | 1584 | } |
| sPymbed | 0:1387ff3eed4a | 1585 | |
| sPymbed | 0:1387ff3eed4a | 1586 | #endif /* WOLFSSL_NUCLEUS_1_2 */ |
| sPymbed | 0:1387ff3eed4a | 1587 | |
| sPymbed | 0:1387ff3eed4a | 1588 | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) |
| sPymbed | 0:1387ff3eed4a | 1589 | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */ |
| sPymbed | 0:1387ff3eed4a | 1590 | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */ |
| sPymbed | 0:1387ff3eed4a | 1591 | #endif |
| sPymbed | 0:1387ff3eed4a | 1592 |