wolfSSL 3.11.1 for TLS1.3 beta
Fork of wolfSSL by
wolfcrypt/src/wc_port.c@13:80fb167dafdf, 2017-05-30 (annotated)
- Committer:
- wolfSSL
- Date:
- Tue May 30 06:16:19 2017 +0000
- Revision:
- 13:80fb167dafdf
- Parent:
- 11:cee25a834751
wolfSSL 3.11.1: TLS1.3 Beta
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
wolfSSL | 11:cee25a834751 | 1 | /* port.c |
wolfSSL | 11:cee25a834751 | 2 | * |
wolfSSL | 11:cee25a834751 | 3 | * Copyright (C) 2006-2016 wolfSSL Inc. |
wolfSSL | 11:cee25a834751 | 4 | * |
wolfSSL | 11:cee25a834751 | 5 | * This file is part of wolfSSL. |
wolfSSL | 11:cee25a834751 | 6 | * |
wolfSSL | 11:cee25a834751 | 7 | * wolfSSL is free software; you can redistribute it and/or modify |
wolfSSL | 11:cee25a834751 | 8 | * it under the terms of the GNU General Public License as published by |
wolfSSL | 11:cee25a834751 | 9 | * the Free Software Foundation; either version 2 of the License, or |
wolfSSL | 11:cee25a834751 | 10 | * (at your option) any later version. |
wolfSSL | 11:cee25a834751 | 11 | * |
wolfSSL | 11:cee25a834751 | 12 | * wolfSSL is distributed in the hope that it will be useful, |
wolfSSL | 11:cee25a834751 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
wolfSSL | 11:cee25a834751 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
wolfSSL | 11:cee25a834751 | 15 | * GNU General Public License for more details. |
wolfSSL | 11:cee25a834751 | 16 | * |
wolfSSL | 11:cee25a834751 | 17 | * You should have received a copy of the GNU General Public License |
wolfSSL | 11:cee25a834751 | 18 | * along with this program; if not, write to the Free Software |
wolfSSL | 11:cee25a834751 | 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA |
wolfSSL | 11:cee25a834751 | 20 | */ |
wolfSSL | 11:cee25a834751 | 21 | |
wolfSSL | 11:cee25a834751 | 22 | |
wolfSSL | 11:cee25a834751 | 23 | #ifdef HAVE_CONFIG_H |
wolfSSL | 11:cee25a834751 | 24 | #include <config.h> |
wolfSSL | 11:cee25a834751 | 25 | #endif |
wolfSSL | 11:cee25a834751 | 26 | |
wolfSSL | 11:cee25a834751 | 27 | #include <wolfssl/wolfcrypt/settings.h> |
wolfSSL | 11:cee25a834751 | 28 | #include <wolfssl/wolfcrypt/types.h> |
wolfSSL | 11:cee25a834751 | 29 | #include <wolfssl/wolfcrypt/error-crypt.h> |
wolfSSL | 11:cee25a834751 | 30 | #include <wolfssl/wolfcrypt/logging.h> |
wolfSSL | 11:cee25a834751 | 31 | #include <wolfssl/wolfcrypt/wc_port.h> |
wolfSSL | 11:cee25a834751 | 32 | #ifdef HAVE_ECC |
wolfSSL | 11:cee25a834751 | 33 | #include <wolfssl/wolfcrypt/ecc.h> |
wolfSSL | 11:cee25a834751 | 34 | #endif |
wolfSSL | 11:cee25a834751 | 35 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 11:cee25a834751 | 36 | #include <wolfssl/wolfcrypt/async.h> |
wolfSSL | 11:cee25a834751 | 37 | #endif |
wolfSSL | 11:cee25a834751 | 38 | |
wolfSSL | 11:cee25a834751 | 39 | /* IPP header files for library initialization */ |
wolfSSL | 11:cee25a834751 | 40 | #ifdef HAVE_FAST_RSA |
wolfSSL | 11:cee25a834751 | 41 | #include <ipp.h> |
wolfSSL | 11:cee25a834751 | 42 | #include <ippcp.h> |
wolfSSL | 11:cee25a834751 | 43 | #endif |
wolfSSL | 11:cee25a834751 | 44 | |
wolfSSL | 11:cee25a834751 | 45 | #if defined(FREESCALE_LTC_TFM) |
wolfSSL | 11:cee25a834751 | 46 | #include <wolfssl/wolfcrypt/port/nxp/ksdk_port.h> |
wolfSSL | 11:cee25a834751 | 47 | #endif |
wolfSSL | 11:cee25a834751 | 48 | |
wolfSSL | 11:cee25a834751 | 49 | #ifdef WOLFSSL_ATMEL |
wolfSSL | 11:cee25a834751 | 50 | #include <wolfssl/wolfcrypt/port/atmel/atmel.h> |
wolfSSL | 11:cee25a834751 | 51 | #endif |
wolfSSL | 11:cee25a834751 | 52 | |
wolfSSL | 11:cee25a834751 | 53 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
wolfSSL | 11:cee25a834751 | 54 | #include <wolfssl/openssl/evp.h> |
wolfSSL | 11:cee25a834751 | 55 | #endif |
wolfSSL | 11:cee25a834751 | 56 | |
wolfSSL | 11:cee25a834751 | 57 | #if defined(USE_WOLFSSL_MEMORY) && defined(WOLFSSL_TRACK_MEMORY) |
wolfSSL | 11:cee25a834751 | 58 | #include <wolfssl/wolfcrypt/mem_track.h> |
wolfSSL | 11:cee25a834751 | 59 | #endif |
wolfSSL | 11:cee25a834751 | 60 | |
wolfSSL | 11:cee25a834751 | 61 | #ifdef _MSC_VER |
wolfSSL | 11:cee25a834751 | 62 | /* 4996 warning to use MS extensions e.g., strcpy_s instead of strncpy */ |
wolfSSL | 11:cee25a834751 | 63 | #pragma warning(disable: 4996) |
wolfSSL | 11:cee25a834751 | 64 | #endif |
wolfSSL | 11:cee25a834751 | 65 | |
wolfSSL | 11:cee25a834751 | 66 | /* prevent multiple mutex initializations */ |
wolfSSL | 11:cee25a834751 | 67 | static volatile int initRefCount = 0; |
wolfSSL | 11:cee25a834751 | 68 | |
wolfSSL | 11:cee25a834751 | 69 | /* Used to initialize state for wolfcrypt |
wolfSSL | 11:cee25a834751 | 70 | return 0 on success |
wolfSSL | 11:cee25a834751 | 71 | */ |
wolfSSL | 11:cee25a834751 | 72 | int wolfCrypt_Init(void) |
wolfSSL | 11:cee25a834751 | 73 | { |
wolfSSL | 11:cee25a834751 | 74 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 75 | |
wolfSSL | 11:cee25a834751 | 76 | if (initRefCount == 0) { |
wolfSSL | 11:cee25a834751 | 77 | WOLFSSL_ENTER("wolfCrypt_Init"); |
wolfSSL | 11:cee25a834751 | 78 | |
wolfSSL | 11:cee25a834751 | 79 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 11:cee25a834751 | 80 | ret = wolfAsync_HardwareStart(); |
wolfSSL | 11:cee25a834751 | 81 | if (ret != 0) { |
wolfSSL | 11:cee25a834751 | 82 | WOLFSSL_MSG("Async hardware start failed"); |
wolfSSL | 11:cee25a834751 | 83 | return ret; |
wolfSSL | 11:cee25a834751 | 84 | } |
wolfSSL | 11:cee25a834751 | 85 | #endif |
wolfSSL | 11:cee25a834751 | 86 | |
wolfSSL | 11:cee25a834751 | 87 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
wolfSSL | 11:cee25a834751 | 88 | ret = InitMemoryTracker(); |
wolfSSL | 11:cee25a834751 | 89 | if (ret != 0) { |
wolfSSL | 11:cee25a834751 | 90 | WOLFSSL_MSG("InitMemoryTracker failed"); |
wolfSSL | 11:cee25a834751 | 91 | return ret; |
wolfSSL | 11:cee25a834751 | 92 | } |
wolfSSL | 11:cee25a834751 | 93 | #endif |
wolfSSL | 11:cee25a834751 | 94 | |
wolfSSL | 11:cee25a834751 | 95 | #if WOLFSSL_CRYPT_HW_MUTEX |
wolfSSL | 11:cee25a834751 | 96 | /* If crypto hardware mutex protection is enabled, then initialize it */ |
wolfSSL | 11:cee25a834751 | 97 | ret = wolfSSL_CryptHwMutexInit(); |
wolfSSL | 11:cee25a834751 | 98 | if (ret != 0) { |
wolfSSL | 11:cee25a834751 | 99 | WOLFSSL_MSG("Hw crypt mutex init failed"); |
wolfSSL | 11:cee25a834751 | 100 | return ret; |
wolfSSL | 11:cee25a834751 | 101 | } |
wolfSSL | 11:cee25a834751 | 102 | #endif |
wolfSSL | 11:cee25a834751 | 103 | |
wolfSSL | 11:cee25a834751 | 104 | /* if defined have fast RSA then initialize Intel IPP */ |
wolfSSL | 11:cee25a834751 | 105 | #ifdef HAVE_FAST_RSA |
wolfSSL | 11:cee25a834751 | 106 | WOLFSSL_MSG("Attempting to use optimized IPP Library"); |
wolfSSL | 11:cee25a834751 | 107 | if ((ret = ippInit()) != ippStsNoErr) { |
wolfSSL | 11:cee25a834751 | 108 | /* possible to get a CPU feature support status on optimized IPP |
wolfSSL | 11:cee25a834751 | 109 | library but still use default library and see competitive speeds */ |
wolfSSL | 11:cee25a834751 | 110 | WOLFSSL_MSG("Warning when trying to set up optimization"); |
wolfSSL | 11:cee25a834751 | 111 | WOLFSSL_MSG(ippGetStatusString(ret)); |
wolfSSL | 11:cee25a834751 | 112 | WOLFSSL_MSG("Using default fast IPP library"); |
wolfSSL | 11:cee25a834751 | 113 | ret = 0; |
wolfSSL | 11:cee25a834751 | 114 | (void)ret; /* suppress not read warning */ |
wolfSSL | 11:cee25a834751 | 115 | } |
wolfSSL | 11:cee25a834751 | 116 | #endif |
wolfSSL | 11:cee25a834751 | 117 | |
wolfSSL | 11:cee25a834751 | 118 | #if defined(FREESCALE_LTC_TFM) || defined(FREESCALE_LTC_ECC) |
wolfSSL | 11:cee25a834751 | 119 | ret = ksdk_port_init(); |
wolfSSL | 11:cee25a834751 | 120 | if (ret != 0) { |
wolfSSL | 11:cee25a834751 | 121 | WOLFSSL_MSG("KSDK port init failed"); |
wolfSSL | 11:cee25a834751 | 122 | return ret; |
wolfSSL | 11:cee25a834751 | 123 | } |
wolfSSL | 11:cee25a834751 | 124 | #endif |
wolfSSL | 11:cee25a834751 | 125 | |
wolfSSL | 11:cee25a834751 | 126 | #ifdef WOLFSSL_ATMEL |
wolfSSL | 11:cee25a834751 | 127 | atmel_init(); |
wolfSSL | 11:cee25a834751 | 128 | #endif |
wolfSSL | 11:cee25a834751 | 129 | |
wolfSSL | 11:cee25a834751 | 130 | #ifdef WOLFSSL_ARMASM |
wolfSSL | 11:cee25a834751 | 131 | WOLFSSL_MSG("Using ARM hardware acceleration"); |
wolfSSL | 11:cee25a834751 | 132 | #endif |
wolfSSL | 11:cee25a834751 | 133 | |
wolfSSL | 11:cee25a834751 | 134 | #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER) |
wolfSSL | 11:cee25a834751 | 135 | wolfSSL_EVP_init(); |
wolfSSL | 11:cee25a834751 | 136 | #endif |
wolfSSL | 11:cee25a834751 | 137 | |
wolfSSL | 11:cee25a834751 | 138 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
wolfSSL | 11:cee25a834751 | 139 | if ((ret = wc_LoggingInit()) != 0) { |
wolfSSL | 11:cee25a834751 | 140 | WOLFSSL_MSG("Error creating logging mutex"); |
wolfSSL | 11:cee25a834751 | 141 | return ret; |
wolfSSL | 11:cee25a834751 | 142 | } |
wolfSSL | 11:cee25a834751 | 143 | #endif |
wolfSSL | 11:cee25a834751 | 144 | |
wolfSSL | 11:cee25a834751 | 145 | #ifdef HAVE_ECC |
wolfSSL | 11:cee25a834751 | 146 | #ifdef ECC_CACHE_CURVE |
wolfSSL | 11:cee25a834751 | 147 | if ((ret = wc_ecc_curve_cache_init()) != 0) { |
wolfSSL | 11:cee25a834751 | 148 | WOLFSSL_MSG("Error creating curve cache"); |
wolfSSL | 11:cee25a834751 | 149 | return ret; |
wolfSSL | 11:cee25a834751 | 150 | } |
wolfSSL | 11:cee25a834751 | 151 | #endif |
wolfSSL | 11:cee25a834751 | 152 | #endif |
wolfSSL | 11:cee25a834751 | 153 | |
wolfSSL | 11:cee25a834751 | 154 | initRefCount = 1; |
wolfSSL | 11:cee25a834751 | 155 | } |
wolfSSL | 11:cee25a834751 | 156 | |
wolfSSL | 11:cee25a834751 | 157 | return ret; |
wolfSSL | 11:cee25a834751 | 158 | } |
wolfSSL | 11:cee25a834751 | 159 | |
wolfSSL | 11:cee25a834751 | 160 | |
wolfSSL | 11:cee25a834751 | 161 | /* return success value is the same as wolfCrypt_Init */ |
wolfSSL | 11:cee25a834751 | 162 | int wolfCrypt_Cleanup(void) |
wolfSSL | 11:cee25a834751 | 163 | { |
wolfSSL | 11:cee25a834751 | 164 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 165 | |
wolfSSL | 11:cee25a834751 | 166 | if (initRefCount == 1) { |
wolfSSL | 11:cee25a834751 | 167 | WOLFSSL_ENTER("wolfCrypt_Cleanup"); |
wolfSSL | 11:cee25a834751 | 168 | |
wolfSSL | 11:cee25a834751 | 169 | #ifdef HAVE_ECC |
wolfSSL | 11:cee25a834751 | 170 | #ifdef FP_ECC |
wolfSSL | 11:cee25a834751 | 171 | wc_ecc_fp_free(); |
wolfSSL | 11:cee25a834751 | 172 | #endif |
wolfSSL | 11:cee25a834751 | 173 | #ifdef ECC_CACHE_CURVE |
wolfSSL | 11:cee25a834751 | 174 | wc_ecc_curve_cache_free(); |
wolfSSL | 11:cee25a834751 | 175 | #endif |
wolfSSL | 11:cee25a834751 | 176 | #endif /* HAVE_ECC */ |
wolfSSL | 11:cee25a834751 | 177 | |
wolfSSL | 11:cee25a834751 | 178 | #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE) |
wolfSSL | 11:cee25a834751 | 179 | ret = wc_LoggingCleanup(); |
wolfSSL | 11:cee25a834751 | 180 | #endif |
wolfSSL | 11:cee25a834751 | 181 | |
wolfSSL | 11:cee25a834751 | 182 | #if defined(WOLFSSL_TRACK_MEMORY) && !defined(WOLFSSL_STATIC_MEMORY) |
wolfSSL | 11:cee25a834751 | 183 | ShowMemoryTracker(); |
wolfSSL | 11:cee25a834751 | 184 | #endif |
wolfSSL | 11:cee25a834751 | 185 | |
wolfSSL | 11:cee25a834751 | 186 | #ifdef WOLFSSL_ASYNC_CRYPT |
wolfSSL | 11:cee25a834751 | 187 | wolfAsync_HardwareStop(); |
wolfSSL | 11:cee25a834751 | 188 | #endif |
wolfSSL | 11:cee25a834751 | 189 | |
wolfSSL | 11:cee25a834751 | 190 | initRefCount = 0; /* allow re-init */ |
wolfSSL | 11:cee25a834751 | 191 | } |
wolfSSL | 11:cee25a834751 | 192 | |
wolfSSL | 11:cee25a834751 | 193 | return ret; |
wolfSSL | 11:cee25a834751 | 194 | } |
wolfSSL | 11:cee25a834751 | 195 | |
wolfSSL | 11:cee25a834751 | 196 | #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) |
wolfSSL | 11:cee25a834751 | 197 | |
wolfSSL | 11:cee25a834751 | 198 | /* File Handling Helpers */ |
wolfSSL | 11:cee25a834751 | 199 | int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name) |
wolfSSL | 11:cee25a834751 | 200 | { |
wolfSSL | 11:cee25a834751 | 201 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 202 | |
wolfSSL | 11:cee25a834751 | 203 | if (name) |
wolfSSL | 11:cee25a834751 | 204 | *name = NULL; |
wolfSSL | 11:cee25a834751 | 205 | |
wolfSSL | 11:cee25a834751 | 206 | if (ctx == NULL || path == NULL) { |
wolfSSL | 11:cee25a834751 | 207 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 208 | } |
wolfSSL | 11:cee25a834751 | 209 | |
wolfSSL | 11:cee25a834751 | 210 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
wolfSSL | 11:cee25a834751 | 211 | |
wolfSSL | 11:cee25a834751 | 212 | #ifdef USE_WINDOWS_API |
wolfSSL | 11:cee25a834751 | 213 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ - 4); |
wolfSSL | 11:cee25a834751 | 214 | XSTRNCAT(ctx->name, "\\*", 3); |
wolfSSL | 11:cee25a834751 | 215 | |
wolfSSL | 11:cee25a834751 | 216 | ctx->hFind = FindFirstFileA(ctx->name, &ctx->FindFileData); |
wolfSSL | 11:cee25a834751 | 217 | if (ctx->hFind == INVALID_HANDLE_VALUE) { |
wolfSSL | 11:cee25a834751 | 218 | WOLFSSL_MSG("FindFirstFile for path verify locations failed"); |
wolfSSL | 11:cee25a834751 | 219 | return BAD_PATH_ERROR; |
wolfSSL | 11:cee25a834751 | 220 | } |
wolfSSL | 11:cee25a834751 | 221 | |
wolfSSL | 11:cee25a834751 | 222 | do { |
wolfSSL | 11:cee25a834751 | 223 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { |
wolfSSL | 11:cee25a834751 | 224 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3); |
wolfSSL | 11:cee25a834751 | 225 | XSTRNCAT(ctx->name, "\\", 2); |
wolfSSL | 11:cee25a834751 | 226 | XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2); |
wolfSSL | 11:cee25a834751 | 227 | if (name) |
wolfSSL | 11:cee25a834751 | 228 | *name = ctx->name; |
wolfSSL | 11:cee25a834751 | 229 | return 0; |
wolfSSL | 11:cee25a834751 | 230 | } |
wolfSSL | 11:cee25a834751 | 231 | } while (FindNextFileA(ctx->hFind, &ctx->FindFileData)); |
wolfSSL | 11:cee25a834751 | 232 | #else |
wolfSSL | 11:cee25a834751 | 233 | ctx->dir = opendir(path); |
wolfSSL | 11:cee25a834751 | 234 | if (ctx->dir == NULL) { |
wolfSSL | 11:cee25a834751 | 235 | WOLFSSL_MSG("opendir path verify locations failed"); |
wolfSSL | 11:cee25a834751 | 236 | return BAD_PATH_ERROR; |
wolfSSL | 11:cee25a834751 | 237 | } |
wolfSSL | 11:cee25a834751 | 238 | |
wolfSSL | 11:cee25a834751 | 239 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
wolfSSL | 11:cee25a834751 | 240 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2); |
wolfSSL | 11:cee25a834751 | 241 | XSTRNCAT(ctx->name, "/", 1); |
wolfSSL | 11:cee25a834751 | 242 | XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2); |
wolfSSL | 11:cee25a834751 | 243 | |
wolfSSL | 11:cee25a834751 | 244 | if (stat(ctx->name, &ctx->s) != 0) { |
wolfSSL | 11:cee25a834751 | 245 | WOLFSSL_MSG("stat on name failed"); |
wolfSSL | 11:cee25a834751 | 246 | ret = BAD_PATH_ERROR; |
wolfSSL | 11:cee25a834751 | 247 | break; |
wolfSSL | 11:cee25a834751 | 248 | } else if (ctx->s.st_mode & S_IFREG) { |
wolfSSL | 11:cee25a834751 | 249 | if (name) |
wolfSSL | 11:cee25a834751 | 250 | *name = ctx->name; |
wolfSSL | 11:cee25a834751 | 251 | return 0; |
wolfSSL | 11:cee25a834751 | 252 | } |
wolfSSL | 11:cee25a834751 | 253 | } |
wolfSSL | 11:cee25a834751 | 254 | #endif |
wolfSSL | 11:cee25a834751 | 255 | wc_ReadDirClose(ctx); |
wolfSSL | 11:cee25a834751 | 256 | |
wolfSSL | 11:cee25a834751 | 257 | return ret; |
wolfSSL | 11:cee25a834751 | 258 | } |
wolfSSL | 11:cee25a834751 | 259 | |
wolfSSL | 11:cee25a834751 | 260 | int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name) |
wolfSSL | 11:cee25a834751 | 261 | { |
wolfSSL | 11:cee25a834751 | 262 | int ret = -1; |
wolfSSL | 11:cee25a834751 | 263 | |
wolfSSL | 11:cee25a834751 | 264 | if (name) |
wolfSSL | 11:cee25a834751 | 265 | *name = NULL; |
wolfSSL | 11:cee25a834751 | 266 | |
wolfSSL | 11:cee25a834751 | 267 | if (ctx == NULL || path == NULL) { |
wolfSSL | 11:cee25a834751 | 268 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 269 | } |
wolfSSL | 11:cee25a834751 | 270 | |
wolfSSL | 11:cee25a834751 | 271 | XMEMSET(ctx->name, 0, MAX_FILENAME_SZ); |
wolfSSL | 11:cee25a834751 | 272 | |
wolfSSL | 11:cee25a834751 | 273 | #ifdef USE_WINDOWS_API |
wolfSSL | 11:cee25a834751 | 274 | while (FindNextFileA(ctx->hFind, &ctx->FindFileData)) { |
wolfSSL | 11:cee25a834751 | 275 | if (ctx->FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) { |
wolfSSL | 11:cee25a834751 | 276 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 3); |
wolfSSL | 11:cee25a834751 | 277 | XSTRNCAT(ctx->name, "\\", 2); |
wolfSSL | 11:cee25a834751 | 278 | XSTRNCAT(ctx->name, ctx->FindFileData.cFileName, MAX_FILENAME_SZ/2); |
wolfSSL | 11:cee25a834751 | 279 | if (name) |
wolfSSL | 11:cee25a834751 | 280 | *name = ctx->name; |
wolfSSL | 11:cee25a834751 | 281 | return 0; |
wolfSSL | 11:cee25a834751 | 282 | } |
wolfSSL | 11:cee25a834751 | 283 | } |
wolfSSL | 11:cee25a834751 | 284 | #else |
wolfSSL | 11:cee25a834751 | 285 | while ((ctx->entry = readdir(ctx->dir)) != NULL) { |
wolfSSL | 11:cee25a834751 | 286 | XSTRNCPY(ctx->name, path, MAX_FILENAME_SZ/2 - 2); |
wolfSSL | 11:cee25a834751 | 287 | XSTRNCAT(ctx->name, "/", 1); |
wolfSSL | 11:cee25a834751 | 288 | XSTRNCAT(ctx->name, ctx->entry->d_name, MAX_FILENAME_SZ/2); |
wolfSSL | 11:cee25a834751 | 289 | |
wolfSSL | 11:cee25a834751 | 290 | if (stat(ctx->name, &ctx->s) != 0) { |
wolfSSL | 11:cee25a834751 | 291 | WOLFSSL_MSG("stat on name failed"); |
wolfSSL | 11:cee25a834751 | 292 | ret = BAD_PATH_ERROR; |
wolfSSL | 11:cee25a834751 | 293 | break; |
wolfSSL | 11:cee25a834751 | 294 | } else if (ctx->s.st_mode & S_IFREG) { |
wolfSSL | 11:cee25a834751 | 295 | if (name) |
wolfSSL | 11:cee25a834751 | 296 | *name = ctx->name; |
wolfSSL | 11:cee25a834751 | 297 | return 0; |
wolfSSL | 11:cee25a834751 | 298 | } |
wolfSSL | 11:cee25a834751 | 299 | } |
wolfSSL | 11:cee25a834751 | 300 | #endif |
wolfSSL | 11:cee25a834751 | 301 | |
wolfSSL | 11:cee25a834751 | 302 | wc_ReadDirClose(ctx); |
wolfSSL | 11:cee25a834751 | 303 | |
wolfSSL | 11:cee25a834751 | 304 | return ret; |
wolfSSL | 11:cee25a834751 | 305 | } |
wolfSSL | 11:cee25a834751 | 306 | |
wolfSSL | 11:cee25a834751 | 307 | void wc_ReadDirClose(ReadDirCtx* ctx) |
wolfSSL | 11:cee25a834751 | 308 | { |
wolfSSL | 11:cee25a834751 | 309 | if (ctx == NULL) { |
wolfSSL | 11:cee25a834751 | 310 | return; |
wolfSSL | 11:cee25a834751 | 311 | } |
wolfSSL | 11:cee25a834751 | 312 | |
wolfSSL | 11:cee25a834751 | 313 | #ifdef USE_WINDOWS_API |
wolfSSL | 11:cee25a834751 | 314 | if (ctx->hFind != INVALID_HANDLE_VALUE) { |
wolfSSL | 11:cee25a834751 | 315 | FindClose(ctx->hFind); |
wolfSSL | 11:cee25a834751 | 316 | ctx->hFind = INVALID_HANDLE_VALUE; |
wolfSSL | 11:cee25a834751 | 317 | } |
wolfSSL | 11:cee25a834751 | 318 | #else |
wolfSSL | 11:cee25a834751 | 319 | if (ctx->dir) { |
wolfSSL | 11:cee25a834751 | 320 | closedir(ctx->dir); |
wolfSSL | 11:cee25a834751 | 321 | ctx->dir = NULL; |
wolfSSL | 11:cee25a834751 | 322 | } |
wolfSSL | 11:cee25a834751 | 323 | #endif |
wolfSSL | 11:cee25a834751 | 324 | } |
wolfSSL | 11:cee25a834751 | 325 | |
wolfSSL | 11:cee25a834751 | 326 | #endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */ |
wolfSSL | 11:cee25a834751 | 327 | |
wolfSSL | 11:cee25a834751 | 328 | |
wolfSSL | 11:cee25a834751 | 329 | wolfSSL_Mutex* wc_InitAndAllocMutex() |
wolfSSL | 11:cee25a834751 | 330 | { |
wolfSSL | 11:cee25a834751 | 331 | wolfSSL_Mutex* m = (wolfSSL_Mutex*) XMALLOC(sizeof(wolfSSL_Mutex), NULL, |
wolfSSL | 11:cee25a834751 | 332 | DYNAMIC_TYPE_MUTEX); |
wolfSSL | 11:cee25a834751 | 333 | if (m != NULL) { |
wolfSSL | 11:cee25a834751 | 334 | if (wc_InitMutex(m) != 0) { |
wolfSSL | 11:cee25a834751 | 335 | WOLFSSL_MSG("Init Mutex failed"); |
wolfSSL | 11:cee25a834751 | 336 | XFREE(m, NULL, DYNAMIC_TYPE_MUTEX); |
wolfSSL | 11:cee25a834751 | 337 | m = NULL; |
wolfSSL | 11:cee25a834751 | 338 | } |
wolfSSL | 11:cee25a834751 | 339 | } |
wolfSSL | 11:cee25a834751 | 340 | else { |
wolfSSL | 11:cee25a834751 | 341 | WOLFSSL_MSG("Memory error with Mutex allocation"); |
wolfSSL | 11:cee25a834751 | 342 | } |
wolfSSL | 11:cee25a834751 | 343 | |
wolfSSL | 11:cee25a834751 | 344 | return m; |
wolfSSL | 11:cee25a834751 | 345 | } |
wolfSSL | 11:cee25a834751 | 346 | |
wolfSSL | 11:cee25a834751 | 347 | |
wolfSSL | 11:cee25a834751 | 348 | #if WOLFSSL_CRYPT_HW_MUTEX |
wolfSSL | 11:cee25a834751 | 349 | /* Mutex for protection of cryptography hardware */ |
wolfSSL | 11:cee25a834751 | 350 | static wolfSSL_Mutex wcCryptHwMutex; |
wolfSSL | 11:cee25a834751 | 351 | static int wcCryptHwMutexInit = 0; |
wolfSSL | 11:cee25a834751 | 352 | |
wolfSSL | 11:cee25a834751 | 353 | int wolfSSL_CryptHwMutexInit(void) { |
wolfSSL | 11:cee25a834751 | 354 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 355 | if(wcCryptHwMutexInit == 0) { |
wolfSSL | 11:cee25a834751 | 356 | ret = wc_InitMutex(&wcCryptHwMutex); |
wolfSSL | 11:cee25a834751 | 357 | if(ret == 0) { |
wolfSSL | 11:cee25a834751 | 358 | wcCryptHwMutexInit = 1; |
wolfSSL | 11:cee25a834751 | 359 | } |
wolfSSL | 11:cee25a834751 | 360 | } |
wolfSSL | 11:cee25a834751 | 361 | return ret; |
wolfSSL | 11:cee25a834751 | 362 | } |
wolfSSL | 11:cee25a834751 | 363 | |
wolfSSL | 11:cee25a834751 | 364 | int wolfSSL_CryptHwMutexLock(void) { |
wolfSSL | 11:cee25a834751 | 365 | int ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 366 | |
wolfSSL | 11:cee25a834751 | 367 | /* Make sure HW Mutex has been initialized */ |
wolfSSL | 11:cee25a834751 | 368 | wolfSSL_CryptHwMutexInit(); |
wolfSSL | 11:cee25a834751 | 369 | |
wolfSSL | 11:cee25a834751 | 370 | if(wcCryptHwMutexInit) { |
wolfSSL | 11:cee25a834751 | 371 | ret = wc_LockMutex(&wcCryptHwMutex); |
wolfSSL | 11:cee25a834751 | 372 | } |
wolfSSL | 11:cee25a834751 | 373 | return ret; |
wolfSSL | 11:cee25a834751 | 374 | } |
wolfSSL | 11:cee25a834751 | 375 | |
wolfSSL | 11:cee25a834751 | 376 | int wolfSSL_CryptHwMutexUnLock(void) { |
wolfSSL | 11:cee25a834751 | 377 | int ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 378 | |
wolfSSL | 11:cee25a834751 | 379 | if(wcCryptHwMutexInit) { |
wolfSSL | 11:cee25a834751 | 380 | ret = wc_UnLockMutex(&wcCryptHwMutex); |
wolfSSL | 11:cee25a834751 | 381 | } |
wolfSSL | 11:cee25a834751 | 382 | return ret; |
wolfSSL | 11:cee25a834751 | 383 | } |
wolfSSL | 11:cee25a834751 | 384 | #endif /* WOLFSSL_CRYPT_HW_MUTEX */ |
wolfSSL | 11:cee25a834751 | 385 | |
wolfSSL | 11:cee25a834751 | 386 | |
wolfSSL | 11:cee25a834751 | 387 | /* ---------------------------------------------------------------------------*/ |
wolfSSL | 11:cee25a834751 | 388 | /* Mutex Ports */ |
wolfSSL | 11:cee25a834751 | 389 | /* ---------------------------------------------------------------------------*/ |
wolfSSL | 11:cee25a834751 | 390 | #ifdef SINGLE_THREADED |
wolfSSL | 11:cee25a834751 | 391 | |
wolfSSL | 11:cee25a834751 | 392 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 393 | { |
wolfSSL | 11:cee25a834751 | 394 | (void)m; |
wolfSSL | 11:cee25a834751 | 395 | return 0; |
wolfSSL | 11:cee25a834751 | 396 | } |
wolfSSL | 11:cee25a834751 | 397 | |
wolfSSL | 11:cee25a834751 | 398 | int wc_FreeMutex(wolfSSL_Mutex *m) |
wolfSSL | 11:cee25a834751 | 399 | { |
wolfSSL | 11:cee25a834751 | 400 | (void)m; |
wolfSSL | 11:cee25a834751 | 401 | return 0; |
wolfSSL | 11:cee25a834751 | 402 | } |
wolfSSL | 11:cee25a834751 | 403 | |
wolfSSL | 11:cee25a834751 | 404 | |
wolfSSL | 11:cee25a834751 | 405 | int wc_LockMutex(wolfSSL_Mutex *m) |
wolfSSL | 11:cee25a834751 | 406 | { |
wolfSSL | 11:cee25a834751 | 407 | (void)m; |
wolfSSL | 11:cee25a834751 | 408 | return 0; |
wolfSSL | 11:cee25a834751 | 409 | } |
wolfSSL | 11:cee25a834751 | 410 | |
wolfSSL | 11:cee25a834751 | 411 | |
wolfSSL | 11:cee25a834751 | 412 | int wc_UnLockMutex(wolfSSL_Mutex *m) |
wolfSSL | 11:cee25a834751 | 413 | { |
wolfSSL | 11:cee25a834751 | 414 | (void)m; |
wolfSSL | 11:cee25a834751 | 415 | return 0; |
wolfSSL | 11:cee25a834751 | 416 | } |
wolfSSL | 11:cee25a834751 | 417 | |
wolfSSL | 11:cee25a834751 | 418 | #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ |
wolfSSL | 11:cee25a834751 | 419 | defined(FREESCALE_FREE_RTOS) |
wolfSSL | 11:cee25a834751 | 420 | |
wolfSSL | 11:cee25a834751 | 421 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 422 | { |
wolfSSL | 11:cee25a834751 | 423 | int iReturn; |
wolfSSL | 11:cee25a834751 | 424 | |
wolfSSL | 11:cee25a834751 | 425 | *m = ( wolfSSL_Mutex ) xSemaphoreCreateMutex(); |
wolfSSL | 11:cee25a834751 | 426 | if( *m != NULL ) |
wolfSSL | 11:cee25a834751 | 427 | iReturn = 0; |
wolfSSL | 11:cee25a834751 | 428 | else |
wolfSSL | 11:cee25a834751 | 429 | iReturn = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 430 | |
wolfSSL | 11:cee25a834751 | 431 | return iReturn; |
wolfSSL | 11:cee25a834751 | 432 | } |
wolfSSL | 11:cee25a834751 | 433 | |
wolfSSL | 11:cee25a834751 | 434 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 435 | { |
wolfSSL | 11:cee25a834751 | 436 | vSemaphoreDelete( *m ); |
wolfSSL | 11:cee25a834751 | 437 | return 0; |
wolfSSL | 11:cee25a834751 | 438 | } |
wolfSSL | 11:cee25a834751 | 439 | |
wolfSSL | 11:cee25a834751 | 440 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 441 | { |
wolfSSL | 11:cee25a834751 | 442 | /* Assume an infinite block, or should there be zero block? */ |
wolfSSL | 11:cee25a834751 | 443 | xSemaphoreTake( *m, portMAX_DELAY ); |
wolfSSL | 11:cee25a834751 | 444 | return 0; |
wolfSSL | 11:cee25a834751 | 445 | } |
wolfSSL | 11:cee25a834751 | 446 | |
wolfSSL | 11:cee25a834751 | 447 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 448 | { |
wolfSSL | 11:cee25a834751 | 449 | xSemaphoreGive( *m ); |
wolfSSL | 11:cee25a834751 | 450 | return 0; |
wolfSSL | 11:cee25a834751 | 451 | } |
wolfSSL | 11:cee25a834751 | 452 | |
wolfSSL | 11:cee25a834751 | 453 | #elif defined(WOLFSSL_SAFERTOS) |
wolfSSL | 11:cee25a834751 | 454 | |
wolfSSL | 11:cee25a834751 | 455 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 456 | { |
wolfSSL | 11:cee25a834751 | 457 | vSemaphoreCreateBinary(m->mutexBuffer, m->mutex); |
wolfSSL | 11:cee25a834751 | 458 | if (m->mutex == NULL) |
wolfSSL | 11:cee25a834751 | 459 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 460 | |
wolfSSL | 11:cee25a834751 | 461 | return 0; |
wolfSSL | 11:cee25a834751 | 462 | } |
wolfSSL | 11:cee25a834751 | 463 | |
wolfSSL | 11:cee25a834751 | 464 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 465 | { |
wolfSSL | 11:cee25a834751 | 466 | (void)m; |
wolfSSL | 11:cee25a834751 | 467 | return 0; |
wolfSSL | 11:cee25a834751 | 468 | } |
wolfSSL | 11:cee25a834751 | 469 | |
wolfSSL | 11:cee25a834751 | 470 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 471 | { |
wolfSSL | 11:cee25a834751 | 472 | /* Assume an infinite block */ |
wolfSSL | 11:cee25a834751 | 473 | xSemaphoreTake(m->mutex, portMAX_DELAY); |
wolfSSL | 11:cee25a834751 | 474 | return 0; |
wolfSSL | 11:cee25a834751 | 475 | } |
wolfSSL | 11:cee25a834751 | 476 | |
wolfSSL | 11:cee25a834751 | 477 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 478 | { |
wolfSSL | 11:cee25a834751 | 479 | xSemaphoreGive(m->mutex); |
wolfSSL | 11:cee25a834751 | 480 | return 0; |
wolfSSL | 11:cee25a834751 | 481 | } |
wolfSSL | 11:cee25a834751 | 482 | |
wolfSSL | 11:cee25a834751 | 483 | #elif defined(USE_WINDOWS_API) |
wolfSSL | 11:cee25a834751 | 484 | |
wolfSSL | 11:cee25a834751 | 485 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 486 | { |
wolfSSL | 11:cee25a834751 | 487 | InitializeCriticalSection(m); |
wolfSSL | 11:cee25a834751 | 488 | return 0; |
wolfSSL | 11:cee25a834751 | 489 | } |
wolfSSL | 11:cee25a834751 | 490 | |
wolfSSL | 11:cee25a834751 | 491 | |
wolfSSL | 11:cee25a834751 | 492 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 493 | { |
wolfSSL | 11:cee25a834751 | 494 | DeleteCriticalSection(m); |
wolfSSL | 11:cee25a834751 | 495 | return 0; |
wolfSSL | 11:cee25a834751 | 496 | } |
wolfSSL | 11:cee25a834751 | 497 | |
wolfSSL | 11:cee25a834751 | 498 | |
wolfSSL | 11:cee25a834751 | 499 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 500 | { |
wolfSSL | 11:cee25a834751 | 501 | EnterCriticalSection(m); |
wolfSSL | 11:cee25a834751 | 502 | return 0; |
wolfSSL | 11:cee25a834751 | 503 | } |
wolfSSL | 11:cee25a834751 | 504 | |
wolfSSL | 11:cee25a834751 | 505 | |
wolfSSL | 11:cee25a834751 | 506 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 507 | { |
wolfSSL | 11:cee25a834751 | 508 | LeaveCriticalSection(m); |
wolfSSL | 11:cee25a834751 | 509 | return 0; |
wolfSSL | 11:cee25a834751 | 510 | } |
wolfSSL | 11:cee25a834751 | 511 | |
wolfSSL | 11:cee25a834751 | 512 | #elif defined(WOLFSSL_PTHREADS) |
wolfSSL | 11:cee25a834751 | 513 | |
wolfSSL | 11:cee25a834751 | 514 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 515 | { |
wolfSSL | 11:cee25a834751 | 516 | if (pthread_mutex_init(m, 0) == 0) |
wolfSSL | 11:cee25a834751 | 517 | return 0; |
wolfSSL | 11:cee25a834751 | 518 | else |
wolfSSL | 11:cee25a834751 | 519 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 520 | } |
wolfSSL | 11:cee25a834751 | 521 | |
wolfSSL | 11:cee25a834751 | 522 | |
wolfSSL | 11:cee25a834751 | 523 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 524 | { |
wolfSSL | 11:cee25a834751 | 525 | if (pthread_mutex_destroy(m) == 0) |
wolfSSL | 11:cee25a834751 | 526 | return 0; |
wolfSSL | 11:cee25a834751 | 527 | else |
wolfSSL | 11:cee25a834751 | 528 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 529 | } |
wolfSSL | 11:cee25a834751 | 530 | |
wolfSSL | 11:cee25a834751 | 531 | |
wolfSSL | 11:cee25a834751 | 532 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 533 | { |
wolfSSL | 11:cee25a834751 | 534 | if (pthread_mutex_lock(m) == 0) |
wolfSSL | 11:cee25a834751 | 535 | return 0; |
wolfSSL | 11:cee25a834751 | 536 | else |
wolfSSL | 11:cee25a834751 | 537 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 538 | } |
wolfSSL | 11:cee25a834751 | 539 | |
wolfSSL | 11:cee25a834751 | 540 | |
wolfSSL | 11:cee25a834751 | 541 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 542 | { |
wolfSSL | 11:cee25a834751 | 543 | if (pthread_mutex_unlock(m) == 0) |
wolfSSL | 11:cee25a834751 | 544 | return 0; |
wolfSSL | 11:cee25a834751 | 545 | else |
wolfSSL | 11:cee25a834751 | 546 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 547 | } |
wolfSSL | 11:cee25a834751 | 548 | |
wolfSSL | 11:cee25a834751 | 549 | #elif defined(THREADX) |
wolfSSL | 11:cee25a834751 | 550 | |
wolfSSL | 11:cee25a834751 | 551 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 552 | { |
wolfSSL | 11:cee25a834751 | 553 | if (tx_mutex_create(m, "wolfSSL Mutex", TX_NO_INHERIT) == 0) |
wolfSSL | 11:cee25a834751 | 554 | return 0; |
wolfSSL | 11:cee25a834751 | 555 | else |
wolfSSL | 11:cee25a834751 | 556 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 557 | } |
wolfSSL | 11:cee25a834751 | 558 | |
wolfSSL | 11:cee25a834751 | 559 | |
wolfSSL | 11:cee25a834751 | 560 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 561 | { |
wolfSSL | 11:cee25a834751 | 562 | if (tx_mutex_delete(m) == 0) |
wolfSSL | 11:cee25a834751 | 563 | return 0; |
wolfSSL | 11:cee25a834751 | 564 | else |
wolfSSL | 11:cee25a834751 | 565 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 566 | } |
wolfSSL | 11:cee25a834751 | 567 | |
wolfSSL | 11:cee25a834751 | 568 | |
wolfSSL | 11:cee25a834751 | 569 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 570 | { |
wolfSSL | 11:cee25a834751 | 571 | if (tx_mutex_get(m, TX_WAIT_FOREVER) == 0) |
wolfSSL | 11:cee25a834751 | 572 | return 0; |
wolfSSL | 11:cee25a834751 | 573 | else |
wolfSSL | 11:cee25a834751 | 574 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 575 | } |
wolfSSL | 11:cee25a834751 | 576 | |
wolfSSL | 11:cee25a834751 | 577 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 578 | { |
wolfSSL | 11:cee25a834751 | 579 | if (tx_mutex_put(m) == 0) |
wolfSSL | 11:cee25a834751 | 580 | return 0; |
wolfSSL | 11:cee25a834751 | 581 | else |
wolfSSL | 11:cee25a834751 | 582 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 583 | } |
wolfSSL | 11:cee25a834751 | 584 | |
wolfSSL | 11:cee25a834751 | 585 | #elif defined(MICRIUM) |
wolfSSL | 11:cee25a834751 | 586 | |
wolfSSL | 11:cee25a834751 | 587 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 588 | { |
wolfSSL | 11:cee25a834751 | 589 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
wolfSSL | 11:cee25a834751 | 590 | if (NetSecure_OS_MutexCreate(m) == 0) |
wolfSSL | 11:cee25a834751 | 591 | return 0; |
wolfSSL | 11:cee25a834751 | 592 | else |
wolfSSL | 11:cee25a834751 | 593 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 594 | #else |
wolfSSL | 11:cee25a834751 | 595 | return 0; |
wolfSSL | 11:cee25a834751 | 596 | #endif |
wolfSSL | 11:cee25a834751 | 597 | } |
wolfSSL | 11:cee25a834751 | 598 | |
wolfSSL | 11:cee25a834751 | 599 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 600 | { |
wolfSSL | 11:cee25a834751 | 601 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
wolfSSL | 11:cee25a834751 | 602 | if (NetSecure_OS_wc_FreeMutex(m) == 0) |
wolfSSL | 11:cee25a834751 | 603 | return 0; |
wolfSSL | 11:cee25a834751 | 604 | else |
wolfSSL | 11:cee25a834751 | 605 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 606 | #else |
wolfSSL | 11:cee25a834751 | 607 | return 0; |
wolfSSL | 11:cee25a834751 | 608 | #endif |
wolfSSL | 11:cee25a834751 | 609 | } |
wolfSSL | 11:cee25a834751 | 610 | |
wolfSSL | 11:cee25a834751 | 611 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 612 | { |
wolfSSL | 11:cee25a834751 | 613 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
wolfSSL | 11:cee25a834751 | 614 | if (NetSecure_OS_wc_LockMutex(m) == 0) |
wolfSSL | 11:cee25a834751 | 615 | return 0; |
wolfSSL | 11:cee25a834751 | 616 | else |
wolfSSL | 11:cee25a834751 | 617 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 618 | #else |
wolfSSL | 11:cee25a834751 | 619 | return 0; |
wolfSSL | 11:cee25a834751 | 620 | #endif |
wolfSSL | 11:cee25a834751 | 621 | } |
wolfSSL | 11:cee25a834751 | 622 | |
wolfSSL | 11:cee25a834751 | 623 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 624 | { |
wolfSSL | 11:cee25a834751 | 625 | #if (NET_SECURE_MGR_CFG_EN == DEF_ENABLED) |
wolfSSL | 11:cee25a834751 | 626 | if (NetSecure_OS_wc_UnLockMutex(m) == 0) |
wolfSSL | 11:cee25a834751 | 627 | return 0; |
wolfSSL | 11:cee25a834751 | 628 | else |
wolfSSL | 11:cee25a834751 | 629 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 630 | #else |
wolfSSL | 11:cee25a834751 | 631 | return 0; |
wolfSSL | 11:cee25a834751 | 632 | #endif |
wolfSSL | 11:cee25a834751 | 633 | |
wolfSSL | 11:cee25a834751 | 634 | } |
wolfSSL | 11:cee25a834751 | 635 | |
wolfSSL | 11:cee25a834751 | 636 | #elif defined(EBSNET) |
wolfSSL | 11:cee25a834751 | 637 | |
wolfSSL | 11:cee25a834751 | 638 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 639 | { |
wolfSSL | 11:cee25a834751 | 640 | if (rtp_sig_mutex_alloc(m, "wolfSSL Mutex") == -1) |
wolfSSL | 11:cee25a834751 | 641 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 642 | else |
wolfSSL | 11:cee25a834751 | 643 | return 0; |
wolfSSL | 11:cee25a834751 | 644 | } |
wolfSSL | 11:cee25a834751 | 645 | |
wolfSSL | 11:cee25a834751 | 646 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 647 | { |
wolfSSL | 11:cee25a834751 | 648 | rtp_sig_mutex_free(*m); |
wolfSSL | 11:cee25a834751 | 649 | return 0; |
wolfSSL | 11:cee25a834751 | 650 | } |
wolfSSL | 11:cee25a834751 | 651 | |
wolfSSL | 11:cee25a834751 | 652 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 653 | { |
wolfSSL | 11:cee25a834751 | 654 | if (rtp_sig_mutex_claim_timed(*m, RTIP_INF) == 0) |
wolfSSL | 11:cee25a834751 | 655 | return 0; |
wolfSSL | 11:cee25a834751 | 656 | else |
wolfSSL | 11:cee25a834751 | 657 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 658 | } |
wolfSSL | 11:cee25a834751 | 659 | |
wolfSSL | 11:cee25a834751 | 660 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 661 | { |
wolfSSL | 11:cee25a834751 | 662 | rtp_sig_mutex_release(*m); |
wolfSSL | 11:cee25a834751 | 663 | return 0; |
wolfSSL | 11:cee25a834751 | 664 | } |
wolfSSL | 11:cee25a834751 | 665 | |
wolfSSL | 11:cee25a834751 | 666 | #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX) |
wolfSSL | 11:cee25a834751 | 667 | |
wolfSSL | 11:cee25a834751 | 668 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 669 | { |
wolfSSL | 11:cee25a834751 | 670 | if (_mutex_init(m, NULL) == MQX_EOK) |
wolfSSL | 11:cee25a834751 | 671 | return 0; |
wolfSSL | 11:cee25a834751 | 672 | else |
wolfSSL | 11:cee25a834751 | 673 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 674 | } |
wolfSSL | 11:cee25a834751 | 675 | |
wolfSSL | 11:cee25a834751 | 676 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 677 | { |
wolfSSL | 11:cee25a834751 | 678 | if (_mutex_destroy(m) == MQX_EOK) |
wolfSSL | 11:cee25a834751 | 679 | return 0; |
wolfSSL | 11:cee25a834751 | 680 | else |
wolfSSL | 11:cee25a834751 | 681 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 682 | } |
wolfSSL | 11:cee25a834751 | 683 | |
wolfSSL | 11:cee25a834751 | 684 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 685 | { |
wolfSSL | 11:cee25a834751 | 686 | if (_mutex_lock(m) == MQX_EOK) |
wolfSSL | 11:cee25a834751 | 687 | return 0; |
wolfSSL | 11:cee25a834751 | 688 | else |
wolfSSL | 11:cee25a834751 | 689 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 690 | } |
wolfSSL | 11:cee25a834751 | 691 | |
wolfSSL | 11:cee25a834751 | 692 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 693 | { |
wolfSSL | 11:cee25a834751 | 694 | if (_mutex_unlock(m) == MQX_EOK) |
wolfSSL | 11:cee25a834751 | 695 | return 0; |
wolfSSL | 11:cee25a834751 | 696 | else |
wolfSSL | 11:cee25a834751 | 697 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 698 | } |
wolfSSL | 11:cee25a834751 | 699 | |
wolfSSL | 11:cee25a834751 | 700 | #elif defined(WOLFSSL_TIRTOS) |
wolfSSL | 11:cee25a834751 | 701 | #include <xdc/runtime/Error.h> |
wolfSSL | 11:cee25a834751 | 702 | |
wolfSSL | 11:cee25a834751 | 703 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 704 | { |
wolfSSL | 11:cee25a834751 | 705 | Semaphore_Params params; |
wolfSSL | 11:cee25a834751 | 706 | Error_Block eb; |
wolfSSL | 11:cee25a834751 | 707 | |
wolfSSL | 11:cee25a834751 | 708 | Error_init(&eb); |
wolfSSL | 11:cee25a834751 | 709 | Semaphore_Params_init(¶ms); |
wolfSSL | 11:cee25a834751 | 710 | params.mode = Semaphore_Mode_BINARY; |
wolfSSL | 11:cee25a834751 | 711 | |
wolfSSL | 11:cee25a834751 | 712 | *m = Semaphore_create(1, ¶ms, &eb); |
wolfSSL | 11:cee25a834751 | 713 | if (Error_check(&eb)) { |
wolfSSL | 11:cee25a834751 | 714 | Error_raise(&eb, Error_E_generic, "Failed to Create the semaphore.", |
wolfSSL | 11:cee25a834751 | 715 | NULL); |
wolfSSL | 11:cee25a834751 | 716 | return BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 717 | } |
wolfSSL | 11:cee25a834751 | 718 | else |
wolfSSL | 11:cee25a834751 | 719 | return 0; |
wolfSSL | 11:cee25a834751 | 720 | } |
wolfSSL | 11:cee25a834751 | 721 | |
wolfSSL | 11:cee25a834751 | 722 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 723 | { |
wolfSSL | 11:cee25a834751 | 724 | Semaphore_delete(m); |
wolfSSL | 11:cee25a834751 | 725 | |
wolfSSL | 11:cee25a834751 | 726 | return 0; |
wolfSSL | 11:cee25a834751 | 727 | } |
wolfSSL | 11:cee25a834751 | 728 | |
wolfSSL | 11:cee25a834751 | 729 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 730 | { |
wolfSSL | 11:cee25a834751 | 731 | Semaphore_pend(*m, BIOS_WAIT_FOREVER); |
wolfSSL | 11:cee25a834751 | 732 | |
wolfSSL | 11:cee25a834751 | 733 | return 0; |
wolfSSL | 11:cee25a834751 | 734 | } |
wolfSSL | 11:cee25a834751 | 735 | |
wolfSSL | 11:cee25a834751 | 736 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 737 | { |
wolfSSL | 11:cee25a834751 | 738 | Semaphore_post(*m); |
wolfSSL | 11:cee25a834751 | 739 | |
wolfSSL | 11:cee25a834751 | 740 | return 0; |
wolfSSL | 11:cee25a834751 | 741 | } |
wolfSSL | 11:cee25a834751 | 742 | |
wolfSSL | 11:cee25a834751 | 743 | #elif defined(WOLFSSL_uITRON4) |
wolfSSL | 11:cee25a834751 | 744 | |
wolfSSL | 11:cee25a834751 | 745 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 746 | { |
wolfSSL | 11:cee25a834751 | 747 | int iReturn; |
wolfSSL | 11:cee25a834751 | 748 | m->sem.sematr = TA_TFIFO; |
wolfSSL | 11:cee25a834751 | 749 | m->sem.isemcnt = 1; |
wolfSSL | 11:cee25a834751 | 750 | m->sem.maxsem = 1; |
wolfSSL | 11:cee25a834751 | 751 | m->sem.name = NULL; |
wolfSSL | 11:cee25a834751 | 752 | |
wolfSSL | 11:cee25a834751 | 753 | m->id = acre_sem(&m->sem); |
wolfSSL | 11:cee25a834751 | 754 | if( m->id != E_OK ) |
wolfSSL | 11:cee25a834751 | 755 | iReturn = 0; |
wolfSSL | 11:cee25a834751 | 756 | else |
wolfSSL | 11:cee25a834751 | 757 | iReturn = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 758 | |
wolfSSL | 11:cee25a834751 | 759 | return iReturn; |
wolfSSL | 11:cee25a834751 | 760 | } |
wolfSSL | 11:cee25a834751 | 761 | |
wolfSSL | 11:cee25a834751 | 762 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 763 | { |
wolfSSL | 11:cee25a834751 | 764 | del_sem( m->id ); |
wolfSSL | 11:cee25a834751 | 765 | return 0; |
wolfSSL | 11:cee25a834751 | 766 | } |
wolfSSL | 11:cee25a834751 | 767 | |
wolfSSL | 11:cee25a834751 | 768 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 769 | { |
wolfSSL | 11:cee25a834751 | 770 | wai_sem(m->id); |
wolfSSL | 11:cee25a834751 | 771 | return 0; |
wolfSSL | 11:cee25a834751 | 772 | } |
wolfSSL | 11:cee25a834751 | 773 | |
wolfSSL | 11:cee25a834751 | 774 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 775 | { |
wolfSSL | 11:cee25a834751 | 776 | sig_sem(m->id); |
wolfSSL | 11:cee25a834751 | 777 | return 0; |
wolfSSL | 11:cee25a834751 | 778 | } |
wolfSSL | 11:cee25a834751 | 779 | |
wolfSSL | 11:cee25a834751 | 780 | /**** uITRON malloc/free ***/ |
wolfSSL | 11:cee25a834751 | 781 | static ID ID_wolfssl_MPOOL = 0; |
wolfSSL | 11:cee25a834751 | 782 | static T_CMPL wolfssl_MPOOL = {TA_TFIFO, 0, NULL, "wolfSSL_MPOOL"}; |
wolfSSL | 11:cee25a834751 | 783 | |
wolfSSL | 11:cee25a834751 | 784 | int uITRON4_minit(size_t poolsz) { |
wolfSSL | 11:cee25a834751 | 785 | ER ercd; |
wolfSSL | 11:cee25a834751 | 786 | wolfssl_MPOOL.mplsz = poolsz; |
wolfSSL | 11:cee25a834751 | 787 | ercd = acre_mpl(&wolfssl_MPOOL); |
wolfSSL | 11:cee25a834751 | 788 | if (ercd > 0) { |
wolfSSL | 11:cee25a834751 | 789 | ID_wolfssl_MPOOL = ercd; |
wolfSSL | 11:cee25a834751 | 790 | return 0; |
wolfSSL | 11:cee25a834751 | 791 | } else { |
wolfSSL | 11:cee25a834751 | 792 | return -1; |
wolfSSL | 11:cee25a834751 | 793 | } |
wolfSSL | 11:cee25a834751 | 794 | } |
wolfSSL | 11:cee25a834751 | 795 | |
wolfSSL | 11:cee25a834751 | 796 | void *uITRON4_malloc(size_t sz) { |
wolfSSL | 11:cee25a834751 | 797 | ER ercd; |
wolfSSL | 11:cee25a834751 | 798 | void *p; |
wolfSSL | 11:cee25a834751 | 799 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p); |
wolfSSL | 11:cee25a834751 | 800 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 801 | return p; |
wolfSSL | 11:cee25a834751 | 802 | } else { |
wolfSSL | 11:cee25a834751 | 803 | return 0; |
wolfSSL | 11:cee25a834751 | 804 | } |
wolfSSL | 11:cee25a834751 | 805 | } |
wolfSSL | 11:cee25a834751 | 806 | |
wolfSSL | 11:cee25a834751 | 807 | void *uITRON4_realloc(void *p, size_t sz) { |
wolfSSL | 11:cee25a834751 | 808 | ER ercd; |
wolfSSL | 11:cee25a834751 | 809 | void *newp; |
wolfSSL | 11:cee25a834751 | 810 | if(p) { |
wolfSSL | 11:cee25a834751 | 811 | ercd = get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp); |
wolfSSL | 11:cee25a834751 | 812 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 813 | XMEMCPY(newp, p, sz); |
wolfSSL | 11:cee25a834751 | 814 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
wolfSSL | 11:cee25a834751 | 815 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 816 | return newp; |
wolfSSL | 11:cee25a834751 | 817 | } |
wolfSSL | 11:cee25a834751 | 818 | } |
wolfSSL | 11:cee25a834751 | 819 | } |
wolfSSL | 11:cee25a834751 | 820 | return 0; |
wolfSSL | 11:cee25a834751 | 821 | } |
wolfSSL | 11:cee25a834751 | 822 | |
wolfSSL | 11:cee25a834751 | 823 | void uITRON4_free(void *p) { |
wolfSSL | 11:cee25a834751 | 824 | ER ercd; |
wolfSSL | 11:cee25a834751 | 825 | ercd = rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
wolfSSL | 11:cee25a834751 | 826 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 827 | return; |
wolfSSL | 11:cee25a834751 | 828 | } else { |
wolfSSL | 11:cee25a834751 | 829 | return; |
wolfSSL | 11:cee25a834751 | 830 | } |
wolfSSL | 11:cee25a834751 | 831 | } |
wolfSSL | 11:cee25a834751 | 832 | |
wolfSSL | 11:cee25a834751 | 833 | #elif defined(WOLFSSL_uTKERNEL2) |
wolfSSL | 11:cee25a834751 | 834 | |
wolfSSL | 11:cee25a834751 | 835 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 836 | { |
wolfSSL | 11:cee25a834751 | 837 | int iReturn; |
wolfSSL | 11:cee25a834751 | 838 | m->sem.sematr = TA_TFIFO; |
wolfSSL | 11:cee25a834751 | 839 | m->sem.isemcnt = 1; |
wolfSSL | 11:cee25a834751 | 840 | m->sem.maxsem = 1; |
wolfSSL | 11:cee25a834751 | 841 | |
wolfSSL | 11:cee25a834751 | 842 | m->id = tk_cre_sem(&m->sem); |
wolfSSL | 11:cee25a834751 | 843 | if( m->id != NULL ) |
wolfSSL | 11:cee25a834751 | 844 | iReturn = 0; |
wolfSSL | 11:cee25a834751 | 845 | else |
wolfSSL | 11:cee25a834751 | 846 | iReturn = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 847 | |
wolfSSL | 11:cee25a834751 | 848 | return iReturn; |
wolfSSL | 11:cee25a834751 | 849 | } |
wolfSSL | 11:cee25a834751 | 850 | |
wolfSSL | 11:cee25a834751 | 851 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 852 | { |
wolfSSL | 11:cee25a834751 | 853 | tk_del_sem(m->id); |
wolfSSL | 11:cee25a834751 | 854 | return 0; |
wolfSSL | 11:cee25a834751 | 855 | } |
wolfSSL | 11:cee25a834751 | 856 | |
wolfSSL | 11:cee25a834751 | 857 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 858 | { |
wolfSSL | 11:cee25a834751 | 859 | tk_wai_sem(m->id, 1, TMO_FEVR); |
wolfSSL | 11:cee25a834751 | 860 | return 0; |
wolfSSL | 11:cee25a834751 | 861 | } |
wolfSSL | 11:cee25a834751 | 862 | |
wolfSSL | 11:cee25a834751 | 863 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 864 | { |
wolfSSL | 11:cee25a834751 | 865 | tk_sig_sem(m->id, 1); |
wolfSSL | 11:cee25a834751 | 866 | return 0; |
wolfSSL | 11:cee25a834751 | 867 | } |
wolfSSL | 11:cee25a834751 | 868 | |
wolfSSL | 11:cee25a834751 | 869 | /**** uT-Kernel malloc/free ***/ |
wolfSSL | 11:cee25a834751 | 870 | static ID ID_wolfssl_MPOOL = 0; |
wolfSSL | 11:cee25a834751 | 871 | static T_CMPL wolfssl_MPOOL = { |
wolfSSL | 11:cee25a834751 | 872 | NULL, /* Extended information */ |
wolfSSL | 11:cee25a834751 | 873 | TA_TFIFO, /* Memory pool attribute */ |
wolfSSL | 11:cee25a834751 | 874 | 0, /* Size of whole memory pool (byte) */ |
wolfSSL | 11:cee25a834751 | 875 | "wolfSSL" /* Object name (max 8-char) */ |
wolfSSL | 11:cee25a834751 | 876 | }; |
wolfSSL | 11:cee25a834751 | 877 | |
wolfSSL | 11:cee25a834751 | 878 | int uTKernel_init_mpool(unsigned int sz) { |
wolfSSL | 11:cee25a834751 | 879 | ER ercd; |
wolfSSL | 11:cee25a834751 | 880 | wolfssl_MPOOL.mplsz = sz; |
wolfSSL | 11:cee25a834751 | 881 | ercd = tk_cre_mpl(&wolfssl_MPOOL); |
wolfSSL | 11:cee25a834751 | 882 | if (ercd > 0) { |
wolfSSL | 11:cee25a834751 | 883 | ID_wolfssl_MPOOL = ercd; |
wolfSSL | 11:cee25a834751 | 884 | return 0; |
wolfSSL | 11:cee25a834751 | 885 | } else { |
wolfSSL | 11:cee25a834751 | 886 | return (int)ercd; |
wolfSSL | 11:cee25a834751 | 887 | } |
wolfSSL | 11:cee25a834751 | 888 | } |
wolfSSL | 11:cee25a834751 | 889 | |
wolfSSL | 11:cee25a834751 | 890 | void *uTKernel_malloc(unsigned int sz) { |
wolfSSL | 11:cee25a834751 | 891 | ER ercd; |
wolfSSL | 11:cee25a834751 | 892 | void *p; |
wolfSSL | 11:cee25a834751 | 893 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&p, TMO_FEVR); |
wolfSSL | 11:cee25a834751 | 894 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 895 | return p; |
wolfSSL | 11:cee25a834751 | 896 | } else { |
wolfSSL | 11:cee25a834751 | 897 | return 0; |
wolfSSL | 11:cee25a834751 | 898 | } |
wolfSSL | 11:cee25a834751 | 899 | } |
wolfSSL | 11:cee25a834751 | 900 | |
wolfSSL | 11:cee25a834751 | 901 | void *uTKernel_realloc(void *p, unsigned int sz) { |
wolfSSL | 11:cee25a834751 | 902 | ER ercd; |
wolfSSL | 11:cee25a834751 | 903 | void *newp; |
wolfSSL | 11:cee25a834751 | 904 | if (p) { |
wolfSSL | 11:cee25a834751 | 905 | ercd = tk_get_mpl(ID_wolfssl_MPOOL, sz, (VP)&newp, TMO_FEVR); |
wolfSSL | 11:cee25a834751 | 906 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 907 | XMEMCPY(newp, p, sz); |
wolfSSL | 11:cee25a834751 | 908 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
wolfSSL | 11:cee25a834751 | 909 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 910 | return newp; |
wolfSSL | 11:cee25a834751 | 911 | } |
wolfSSL | 11:cee25a834751 | 912 | } |
wolfSSL | 11:cee25a834751 | 913 | } |
wolfSSL | 11:cee25a834751 | 914 | return 0; |
wolfSSL | 11:cee25a834751 | 915 | } |
wolfSSL | 11:cee25a834751 | 916 | |
wolfSSL | 11:cee25a834751 | 917 | void uTKernel_free(void *p) { |
wolfSSL | 11:cee25a834751 | 918 | ER ercd; |
wolfSSL | 11:cee25a834751 | 919 | ercd = tk_rel_mpl(ID_wolfssl_MPOOL, (VP)p); |
wolfSSL | 11:cee25a834751 | 920 | if (ercd == E_OK) { |
wolfSSL | 11:cee25a834751 | 921 | return; |
wolfSSL | 11:cee25a834751 | 922 | } else { |
wolfSSL | 11:cee25a834751 | 923 | return; |
wolfSSL | 11:cee25a834751 | 924 | } |
wolfSSL | 11:cee25a834751 | 925 | } |
wolfSSL | 11:cee25a834751 | 926 | |
wolfSSL | 11:cee25a834751 | 927 | #elif defined (WOLFSSL_FROSTED) |
wolfSSL | 11:cee25a834751 | 928 | |
wolfSSL | 11:cee25a834751 | 929 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 930 | { |
wolfSSL | 11:cee25a834751 | 931 | *m = mutex_init(); |
wolfSSL | 11:cee25a834751 | 932 | if (*m) |
wolfSSL | 11:cee25a834751 | 933 | return 0; |
wolfSSL | 11:cee25a834751 | 934 | else |
wolfSSL | 11:cee25a834751 | 935 | return -1; |
wolfSSL | 11:cee25a834751 | 936 | } |
wolfSSL | 11:cee25a834751 | 937 | |
wolfSSL | 11:cee25a834751 | 938 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 939 | { |
wolfSSL | 11:cee25a834751 | 940 | mutex_destroy(*m); |
wolfSSL | 11:cee25a834751 | 941 | return(0); |
wolfSSL | 11:cee25a834751 | 942 | } |
wolfSSL | 11:cee25a834751 | 943 | |
wolfSSL | 11:cee25a834751 | 944 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 945 | { |
wolfSSL | 11:cee25a834751 | 946 | mutex_lock(*m); |
wolfSSL | 11:cee25a834751 | 947 | return 0; |
wolfSSL | 11:cee25a834751 | 948 | } |
wolfSSL | 11:cee25a834751 | 949 | |
wolfSSL | 11:cee25a834751 | 950 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 951 | { |
wolfSSL | 11:cee25a834751 | 952 | mutex_unlock(*m); |
wolfSSL | 11:cee25a834751 | 953 | return 0; |
wolfSSL | 11:cee25a834751 | 954 | } |
wolfSSL | 11:cee25a834751 | 955 | |
wolfSSL | 11:cee25a834751 | 956 | #elif defined(WOLFSSL_CMSIS_RTOS) |
wolfSSL | 11:cee25a834751 | 957 | |
wolfSSL | 11:cee25a834751 | 958 | #define CMSIS_NMUTEX 10 |
wolfSSL | 11:cee25a834751 | 959 | osMutexDef(wolfSSL_mt0); osMutexDef(wolfSSL_mt1); osMutexDef(wolfSSL_mt2); |
wolfSSL | 11:cee25a834751 | 960 | osMutexDef(wolfSSL_mt3); osMutexDef(wolfSSL_mt4); osMutexDef(wolfSSL_mt5); |
wolfSSL | 11:cee25a834751 | 961 | osMutexDef(wolfSSL_mt6); osMutexDef(wolfSSL_mt7); osMutexDef(wolfSSL_mt8); |
wolfSSL | 11:cee25a834751 | 962 | osMutexDef(wolfSSL_mt9); |
wolfSSL | 11:cee25a834751 | 963 | |
wolfSSL | 11:cee25a834751 | 964 | static const osMutexDef_t *CMSIS_mutex[] = { osMutex(wolfSSL_mt0), |
wolfSSL | 11:cee25a834751 | 965 | osMutex(wolfSSL_mt1), osMutex(wolfSSL_mt2), osMutex(wolfSSL_mt3), |
wolfSSL | 11:cee25a834751 | 966 | osMutex(wolfSSL_mt4), osMutex(wolfSSL_mt5), osMutex(wolfSSL_mt6), |
wolfSSL | 11:cee25a834751 | 967 | osMutex(wolfSSL_mt7), osMutex(wolfSSL_mt8), osMutex(wolfSSL_mt9) }; |
wolfSSL | 11:cee25a834751 | 968 | |
wolfSSL | 11:cee25a834751 | 969 | static osMutexId CMSIS_mutexID[CMSIS_NMUTEX] = {0}; |
wolfSSL | 11:cee25a834751 | 970 | |
wolfSSL | 11:cee25a834751 | 971 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 972 | { |
wolfSSL | 11:cee25a834751 | 973 | int i; |
wolfSSL | 11:cee25a834751 | 974 | for (i=0; i<CMSIS_NMUTEX; i++) { |
wolfSSL | 11:cee25a834751 | 975 | if(CMSIS_mutexID[i] == 0) { |
wolfSSL | 11:cee25a834751 | 976 | CMSIS_mutexID[i] = osMutexCreate(CMSIS_mutex[i]); |
wolfSSL | 11:cee25a834751 | 977 | (*m) = CMSIS_mutexID[i]; |
wolfSSL | 11:cee25a834751 | 978 | return 0; |
wolfSSL | 11:cee25a834751 | 979 | } |
wolfSSL | 11:cee25a834751 | 980 | } |
wolfSSL | 11:cee25a834751 | 981 | return -1; |
wolfSSL | 11:cee25a834751 | 982 | } |
wolfSSL | 11:cee25a834751 | 983 | |
wolfSSL | 11:cee25a834751 | 984 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 985 | { |
wolfSSL | 11:cee25a834751 | 986 | int i; |
wolfSSL | 11:cee25a834751 | 987 | osMutexDelete (*m); |
wolfSSL | 11:cee25a834751 | 988 | for (i=0; i<CMSIS_NMUTEX; i++) { |
wolfSSL | 11:cee25a834751 | 989 | if(CMSIS_mutexID[i] == (*m)) { |
wolfSSL | 11:cee25a834751 | 990 | CMSIS_mutexID[i] = 0; |
wolfSSL | 11:cee25a834751 | 991 | return(0); |
wolfSSL | 11:cee25a834751 | 992 | } |
wolfSSL | 11:cee25a834751 | 993 | } |
wolfSSL | 11:cee25a834751 | 994 | return(-1); |
wolfSSL | 11:cee25a834751 | 995 | } |
wolfSSL | 11:cee25a834751 | 996 | |
wolfSSL | 11:cee25a834751 | 997 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 998 | { |
wolfSSL | 11:cee25a834751 | 999 | osMutexWait(*m, osWaitForever); |
wolfSSL | 11:cee25a834751 | 1000 | return(0); |
wolfSSL | 11:cee25a834751 | 1001 | } |
wolfSSL | 11:cee25a834751 | 1002 | |
wolfSSL | 11:cee25a834751 | 1003 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1004 | { |
wolfSSL | 11:cee25a834751 | 1005 | osMutexRelease (*m); |
wolfSSL | 11:cee25a834751 | 1006 | return 0; |
wolfSSL | 11:cee25a834751 | 1007 | } |
wolfSSL | 11:cee25a834751 | 1008 | |
wolfSSL | 11:cee25a834751 | 1009 | #elif defined(WOLFSSL_MDK_ARM) |
wolfSSL | 11:cee25a834751 | 1010 | |
wolfSSL | 11:cee25a834751 | 1011 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1012 | { |
wolfSSL | 11:cee25a834751 | 1013 | os_mut_init (m); |
wolfSSL | 11:cee25a834751 | 1014 | return 0; |
wolfSSL | 11:cee25a834751 | 1015 | } |
wolfSSL | 11:cee25a834751 | 1016 | |
wolfSSL | 11:cee25a834751 | 1017 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1018 | { |
wolfSSL | 11:cee25a834751 | 1019 | return(0); |
wolfSSL | 11:cee25a834751 | 1020 | } |
wolfSSL | 11:cee25a834751 | 1021 | |
wolfSSL | 11:cee25a834751 | 1022 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1023 | { |
wolfSSL | 11:cee25a834751 | 1024 | os_mut_wait (m, 0xffff); |
wolfSSL | 11:cee25a834751 | 1025 | return(0); |
wolfSSL | 11:cee25a834751 | 1026 | } |
wolfSSL | 11:cee25a834751 | 1027 | |
wolfSSL | 11:cee25a834751 | 1028 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1029 | { |
wolfSSL | 11:cee25a834751 | 1030 | os_mut_release (m); |
wolfSSL | 11:cee25a834751 | 1031 | return 0; |
wolfSSL | 11:cee25a834751 | 1032 | } |
wolfSSL | 11:cee25a834751 | 1033 | |
wolfSSL | 11:cee25a834751 | 1034 | #elif defined(INTIME_RTOS) |
wolfSSL | 11:cee25a834751 | 1035 | |
wolfSSL | 11:cee25a834751 | 1036 | int wc_InitMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1037 | { |
wolfSSL | 11:cee25a834751 | 1038 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 1039 | |
wolfSSL | 11:cee25a834751 | 1040 | if (m == NULL) |
wolfSSL | 11:cee25a834751 | 1041 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 1042 | |
wolfSSL | 11:cee25a834751 | 1043 | *m = CreateRtSemaphore( |
wolfSSL | 11:cee25a834751 | 1044 | 1, /* initial unit count */ |
wolfSSL | 11:cee25a834751 | 1045 | 1, /* maximum unit count */ |
wolfSSL | 11:cee25a834751 | 1046 | PRIORITY_QUEUING /* creation flags: FIFO_QUEUING or PRIORITY_QUEUING */ |
wolfSSL | 11:cee25a834751 | 1047 | ); |
wolfSSL | 11:cee25a834751 | 1048 | if (*m == BAD_RTHANDLE) { |
wolfSSL | 11:cee25a834751 | 1049 | ret = GetLastRtError(); |
wolfSSL | 11:cee25a834751 | 1050 | if (ret != E_OK) |
wolfSSL | 11:cee25a834751 | 1051 | ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 1052 | } |
wolfSSL | 11:cee25a834751 | 1053 | return ret; |
wolfSSL | 11:cee25a834751 | 1054 | } |
wolfSSL | 11:cee25a834751 | 1055 | |
wolfSSL | 11:cee25a834751 | 1056 | int wc_FreeMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1057 | { |
wolfSSL | 11:cee25a834751 | 1058 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 1059 | BOOLEAN del; |
wolfSSL | 11:cee25a834751 | 1060 | |
wolfSSL | 11:cee25a834751 | 1061 | if (m == NULL) |
wolfSSL | 11:cee25a834751 | 1062 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 1063 | |
wolfSSL | 11:cee25a834751 | 1064 | del = DeleteRtSemaphore( |
wolfSSL | 11:cee25a834751 | 1065 | *m /* handle for RT semaphore */ |
wolfSSL | 11:cee25a834751 | 1066 | ); |
wolfSSL | 11:cee25a834751 | 1067 | if (del != TRUE) |
wolfSSL | 11:cee25a834751 | 1068 | ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 1069 | |
wolfSSL | 11:cee25a834751 | 1070 | return ret; |
wolfSSL | 11:cee25a834751 | 1071 | } |
wolfSSL | 11:cee25a834751 | 1072 | |
wolfSSL | 11:cee25a834751 | 1073 | int wc_LockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1074 | { |
wolfSSL | 11:cee25a834751 | 1075 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 1076 | DWORD lck; |
wolfSSL | 11:cee25a834751 | 1077 | |
wolfSSL | 11:cee25a834751 | 1078 | if (m == NULL) |
wolfSSL | 11:cee25a834751 | 1079 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 1080 | |
wolfSSL | 11:cee25a834751 | 1081 | lck = WaitForRtSemaphore( |
wolfSSL | 11:cee25a834751 | 1082 | *m, /* handle for RT semaphore */ |
wolfSSL | 11:cee25a834751 | 1083 | 1, /* number of units to wait for */ |
wolfSSL | 11:cee25a834751 | 1084 | WAIT_FOREVER /* number of milliseconds to wait for units */ |
wolfSSL | 11:cee25a834751 | 1085 | ); |
wolfSSL | 11:cee25a834751 | 1086 | if (lck == WAIT_FAILED) { |
wolfSSL | 11:cee25a834751 | 1087 | ret = GetLastRtError(); |
wolfSSL | 11:cee25a834751 | 1088 | if (ret != E_OK) |
wolfSSL | 11:cee25a834751 | 1089 | ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 1090 | } |
wolfSSL | 11:cee25a834751 | 1091 | return ret; |
wolfSSL | 11:cee25a834751 | 1092 | } |
wolfSSL | 11:cee25a834751 | 1093 | |
wolfSSL | 11:cee25a834751 | 1094 | int wc_UnLockMutex(wolfSSL_Mutex* m) |
wolfSSL | 11:cee25a834751 | 1095 | { |
wolfSSL | 11:cee25a834751 | 1096 | int ret = 0; |
wolfSSL | 11:cee25a834751 | 1097 | BOOLEAN rel; |
wolfSSL | 11:cee25a834751 | 1098 | |
wolfSSL | 11:cee25a834751 | 1099 | if (m == NULL) |
wolfSSL | 11:cee25a834751 | 1100 | return BAD_FUNC_ARG; |
wolfSSL | 11:cee25a834751 | 1101 | |
wolfSSL | 11:cee25a834751 | 1102 | rel = ReleaseRtSemaphore( |
wolfSSL | 11:cee25a834751 | 1103 | *m, /* handle for RT semaphore */ |
wolfSSL | 11:cee25a834751 | 1104 | 1 /* number of units to release to semaphore */ |
wolfSSL | 11:cee25a834751 | 1105 | ); |
wolfSSL | 11:cee25a834751 | 1106 | if (rel != TRUE) |
wolfSSL | 11:cee25a834751 | 1107 | ret = BAD_MUTEX_E; |
wolfSSL | 11:cee25a834751 | 1108 | |
wolfSSL | 11:cee25a834751 | 1109 | return ret; |
wolfSSL | 11:cee25a834751 | 1110 | } |
wolfSSL | 11:cee25a834751 | 1111 | |
wolfSSL | 11:cee25a834751 | 1112 | #else |
wolfSSL | 11:cee25a834751 | 1113 | #warning No mutex handling defined |
wolfSSL | 11:cee25a834751 | 1114 | |
wolfSSL | 11:cee25a834751 | 1115 | #endif |
wolfSSL | 11:cee25a834751 | 1116 | |
wolfSSL | 11:cee25a834751 | 1117 | |
wolfSSL | 11:cee25a834751 | 1118 | #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) |
wolfSSL | 11:cee25a834751 | 1119 | #include <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */ |
wolfSSL | 11:cee25a834751 | 1120 | #include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */ |
wolfSSL | 11:cee25a834751 | 1121 | #endif |
wolfSSL | 11:cee25a834751 | 1122 |