diff --git a/.github/workflows/watcomc.yml b/.github/workflows/watcomc.yml new file mode 100644 index 0000000000..ea1af57042 --- /dev/null +++ b/.github/workflows/watcomc.yml @@ -0,0 +1,84 @@ +name: Build Watcom C + +# START OF COMMON SECTION +on: + push: + branches: [ 'master', 'main', 'release/**' ] + pull_request: + branches: [ '*' ] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true +# END OF COMMON SECTION + +jobs: + wolfssl_watcomc_windows: + if: github.repository_owner == 'wolfssl' + strategy: + fail-fast: false + matrix: + common: + - cmake: '-G "Watcom WMake" -DCMAKE_VERBOSE_MAKEFILE=TRUE -DWOLFSSL_ASM=no -DWOLFSSL_EXAMPLES=no -DWOLFSSL_CRYPT_TESTS=no' + platform: + - title: 'Windows OW 2.0' + system: 'Windows' + image: 'windows-latest' + owimage: '2.0' + id: 'win32ow20' + cmake: '-DCMAKE_SYSTEM_NAME=Windows -DCMAKE_SYSTEM_PROCESSOR=x86' + - title: 'Linux OW 2.0' + system: 'Linux' + image: 'ubuntu-latest' + owimage: '2.0' + id: 'linuxow20' + cmake: '-DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86' + - title: 'OS/2 OW 2.0' + system: 'OS2' + image: 'windows-latest' + owimage: '2.0' + id: 'os2ow20' + cmake: '-DCMAKE_SYSTEM_NAME=OS2 -DCMAKE_SYSTEM_PROCESSOR=x86' + thread: + - id: 'multi' + cmake: '' + owcmake: '-DCMAKE_POLICY_DEFAULT_CMP0136=NEW -DCMAKE_WATCOM_RUNTIME_LIBRARY=MultiThreaded' + - id: 'single' + cmake: '-DWOLFSSL_SINGLE_THREADED=yes' + owcmake: '-DCMAKE_POLICY_DEFAULT_CMP0136=NEW -DCMAKE_WATCOM_RUNTIME_LIBRARY=SingleThreaded' + library: + - id: 'dll' + cmake: '' + owcmake: 'DLL' + - id: 'static' + cmake: '-DBUILD_SHARED_LIBS=no' + owcmake: '' + exclude: + - { platform: { system: 'Linux' }, library: { id: 'dll' } } + runs-on: ${{ matrix.platform.image }} + name: ${{ matrix.platform.title }} (${{ matrix.thread.id }} ${{ matrix.library.id }}) + steps: + - name: Setup Open Watcom ${{ matrix.platform.owimage }} + uses: open-watcom/setup-watcom@v0 + with: + version: ${{ matrix.platform.owimage }} + + - name: Checkout wolfSSL + uses: actions/checkout@v4 + with: + path: wolfssl + + - name: Build wolfSSL + working-directory: wolfssl + shell: bash + run: | + cmake -B build ${{matrix.common.cmake}} ${{ matrix.platform.cmake }} ${{ matrix.thread.cmake }} ${{ matrix.library.cmake }} ${{ matrix.thread.owcmake }}${{ matrix.library.owcmake }} + cmake --build build + + - name: Upload build errors + if: failure() + uses: actions/upload-artifact@v4 + with: + name: ${{ matrix.platform.id }}-${{ matrix.thread.id }}-${{ matrix.library.id }} + path: | + build/** diff --git a/.wolfssl_known_macro_extras b/.wolfssl_known_macro_extras index a29ead2abe..15e4330905 100644 --- a/.wolfssl_known_macro_extras +++ b/.wolfssl_known_macro_extras @@ -900,6 +900,7 @@ __INTEGRITY __INTEL_COMPILER __KEIL__ __KEY_DATA_H__ +__LINUX__ __LP64 __LP64__ __MACH__ @@ -908,7 +909,9 @@ __MINGW32__ __MINGW64_VERSION_MAJOR __MINGW64__ __MWERKS__ +__NT__ __OpenBSD__ +__OS2__ __PIE__ __POWERPC__ __PPC__ diff --git a/CMakeLists.txt b/CMakeLists.txt index 19f3819642..40661019d8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -153,9 +153,14 @@ endif() # Thread local storage include(CheckCSourceCompiles) -set(TLS_KEYWORDS "__thread" "__declspec(thread)") -foreach(TLS_KEYWORD IN LISTS TLS_KEYWORDS) - set(TLS_CODE "#include +if(CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") + if(CMAKE_SYSTEM_NAME STREQUAL "Windows") + list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_THREAD_LS") + endif() +else() + set(TLS_KEYWORDS "__thread" "__declspec(thread)") + foreach(TLS_KEYWORD IN LISTS TLS_KEYWORDS) + set(TLS_CODE "#include static void foo(void) { static ${TLS_KEYWORD} int bar\; exit(1)\; @@ -164,21 +169,22 @@ foreach(TLS_KEYWORD IN LISTS TLS_KEYWORDS) int main() { return 0\; }" - ) - check_c_source_compiles(${TLS_CODE} THREAD_LS_ON) + ) + check_c_source_compiles(${TLS_CODE} THREAD_LS_ON) - if(THREAD_LS_ON) - list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_THREAD_LS") - break() - else() - # THREAD_LS_ON is cached after each call to - # check_c_source_compiles, and the function - # won't run subsequent times if the variable - # is in the cache. To make it run again, we - # need to remove the variable from the cache. - unset(THREAD_LS_ON CACHE) - endif() -endforeach() + if(THREAD_LS_ON) + list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_THREAD_LS") + break() + else() + # THREAD_LS_ON is cached after each call to + # check_c_source_compiles, and the function + # won't run subsequent times if the variable + # is in the cache. To make it run again, we + # need to remove the variable from the cache. + unset(THREAD_LS_ON CACHE) + endif() + endforeach() +endif() # TODO: AX_PTHREAD does a lot. Need to implement the # rest of its logic. @@ -198,13 +204,20 @@ find_package(Threads) # Example for map file and custom linker script #set(CMAKE_EXE_LINKER_FLAGS " -Xlinker -Map=output.map -T\"${CMAKE_CURRENT_SOURCE_DIR}/linker.ld\"") +message(STATUS "C Compiler ID: ${CMAKE_C_COMPILER_ID}") + if(DEFINED WARNING_C_FLAGS) -set(CMAKE_C_FLAGS "${WARNING_C_FLAGS} ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS "${WARNING_C_FLAGS} ${CMAKE_C_FLAGS}") +endif() + +if(CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -wx -wcd=202") + list(APPEND WOLFSSL_DEFINITIONS "-DWOLFSSL_HAVE_MIN -DWOLFSSL_HAVE_MAX -DNO_WRITEV") elseif(WIN32) -# Windows cl.exe does not support the -Wextra, -Wno-unused and -Werror flags. -set(CMAKE_C_FLAGS "-Wall ${CMAKE_C_FLAGS}") + # Windows cl.exe does not support the -Wextra, -Wno-unused and -Werror flags. + set(CMAKE_C_FLAGS "-Wall ${CMAKE_C_FLAGS}") else() -set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused -Werror ${CMAKE_C_FLAGS}") + set(CMAKE_C_FLAGS "-Wall -Wextra -Wno-unused -Werror ${CMAKE_C_FLAGS}") endif() #################################################### @@ -281,9 +294,7 @@ if(NOT WOLFSSL_SINGLE_THREADED) if(CMAKE_USE_PTHREADS_INIT) list(APPEND WOLFSSL_LINK_LIBS Threads::Threads) set(HAVE_PTHREAD 1) - list(APPEND WOLFSSL_DEFINITIONS - "-DHAVE_PTHREAD" - "-D_POSIX_THREADS") + list(APPEND WOLFSSL_DEFINITIONS "-DHAVE_PTHREAD") endif() else() list(APPEND WOLFSSL_DEFINITIONS "-DSINGLE_THREADED") @@ -2414,6 +2425,7 @@ if (WIN32 OR ${CMAKE_SYSTEM_NAME} MATCHES "^MSYS" OR ${CMAKE_SYSTEM_NAME} MATCHE message("Building on Windows/MSYS/MINGW") target_link_libraries(wolfssl PUBLIC ws2_32 crypt32 advapi32) +elseif(CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") elseif(APPLE) message("Building on Apple") if(WOLFSSL_SYS_CA_CERTS) @@ -2479,7 +2491,9 @@ if(WOLFSSL_EXAMPLES) add_executable(tls_bench ${CMAKE_CURRENT_SOURCE_DIR}/examples/benchmark/tls_bench.c) target_link_libraries(tls_bench wolfssl) - target_link_libraries(tls_bench Threads::Threads) + if(NOT CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") + target_link_libraries(tls_bench Threads::Threads) + endif() set_property(TARGET tls_bench PROPERTY RUNTIME_OUTPUT_DIRECTORY ${WOLFSSL_OUTPUT_BASE}/examples/benchmark) @@ -2501,7 +2515,9 @@ if(WOLFSSL_EXAMPLES) ${CMAKE_CURRENT_BINARY_DIR}) target_compile_options(unit_test PUBLIC "-DNO_MAIN_DRIVER") target_link_libraries(unit_test wolfssl) - target_link_libraries(unit_test Threads::Threads) + if(NOT CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") + target_link_libraries(unit_test Threads::Threads) + endif() set_property(TARGET unit_test PROPERTY RUNTIME_OUTPUT_DIRECTORY ${WOLFSSL_OUTPUT_BASE}/tests/) @@ -2751,19 +2767,22 @@ if(WOLFSSL_INSTALL) set(includedir "\${prefix}/include") set(VERSION ${PROJECT_VERSION}) - # Setting libm in Libs.private of wolfssl.pc. - # See "Link Libraries" in above about `m` insertion to LINK_LIBRARIES - get_target_property(_wolfssl_dep_libs wolfssl LINK_LIBRARIES) - list(FIND _wolfssl_dep_libs m _dep_libm) - if ("${_dep_libm}" GREATER -1) - set(LIBM -lm) + if(CMAKE_C_COMPILER_ID STREQUAL "OpenWatcom") else() - set(LIBM) - endif() + # Setting libm in Libs.private of wolfssl.pc. + # See "Link Libraries" in above about `m` insertion to LINK_LIBRARIES + get_target_property(_wolfssl_dep_libs wolfssl LINK_LIBRARIES) + list(FIND _wolfssl_dep_libs m _dep_libm) + if ("${_dep_libm}" GREATER -1) + set(LIBM -lm) + else() + set(LIBM) + endif() - configure_file(support/wolfssl.pc.in ${CMAKE_CURRENT_BINARY_DIR}/support/wolfssl.pc @ONLY) - install(FILES ${CMAKE_CURRENT_BINARY_DIR}/support/wolfssl.pc - DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) + configure_file(support/wolfssl.pc.in ${CMAKE_CURRENT_BINARY_DIR}/support/wolfssl.pc @ONLY) + install(FILES ${CMAKE_CURRENT_BINARY_DIR}/support/wolfssl.pc + DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) + endif() include(CMakePackageConfigHelpers) configure_package_config_file(${CMAKE_CURRENT_SOURCE_DIR}/cmake/Config.cmake.in diff --git a/examples/benchmark/tls_bench.c b/examples/benchmark/tls_bench.c index c22404260b..15e053dfdb 100644 --- a/examples/benchmark/tls_bench.c +++ b/examples/benchmark/tls_bench.c @@ -1230,7 +1230,7 @@ static int bench_tls_client(info_t* info) } #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN) -static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args) +static THREAD_RETURN_NOJOIN WOLFSSL_THREAD_NO_JOIN client_thread(void* args) { int ret; info_t* info = (info_t*)args; @@ -1243,7 +1243,7 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN client_thread(void* args) THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_server.cond)); THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_server.cond)); - WOLFSSL_RETURN_FROM_THREAD(0); + RETURN_FROM_THREAD_NOJOIN(0); } #endif /* !SINGLE_THREADED */ #endif /* !NO_WOLFSSL_CLIENT */ @@ -1675,7 +1675,7 @@ static int bench_tls_server(info_t* info) } #if !defined(SINGLE_THREADED) && defined(WOLFSSL_THREAD_NO_JOIN) -static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args) +static THREAD_RETURN_NOJOIN WOLFSSL_THREAD_NO_JOIN server_thread(void* args) { int ret = 0; info_t* info = (info_t*)args; @@ -1703,7 +1703,7 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN server_thread(void* args) THREAD_CHECK_RET(wolfSSL_CondSignal(&info->to_client.cond)); THREAD_CHECK_RET(wolfSSL_CondEnd(&info->to_client.cond)); - WOLFSSL_RETURN_FROM_THREAD(0); + RETURN_FROM_THREAD_NOJOIN(0); } #endif /* !SINGLE_THREADED */ #endif /* !NO_WOLFSSL_SERVER */ diff --git a/src/internal.c b/src/internal.c index 0c8f347433..288571c146 100644 --- a/src/internal.c +++ b/src/internal.c @@ -11916,7 +11916,7 @@ static int GetRecordHeader(WOLFSSL* ssl, word32* inOutIdx, (!ssl->options.dtls && rh->pvMinor < ssl->version.minor)) #else - rh->pvMinor < ssl->version.minor + (rh->pvMinor < ssl->version.minor) #endif )) { WOLFSSL_MSG("SSL version error"); diff --git a/src/ssl_load.c b/src/ssl_load.c index 127d8f0ebb..084fd2cdf9 100644 --- a/src/ssl_load.c +++ b/src/ssl_load.c @@ -35,6 +35,7 @@ #ifdef WOLFSSL_SYS_CA_CERTS #ifdef _WIN32 + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include #include diff --git a/src/wolfio.c b/src/wolfio.c index 806d447561..8e6aabbc69 100644 --- a/src/wolfio.c +++ b/src/wolfio.c @@ -41,11 +41,6 @@ #include #endif -#ifdef _WIN32_WCE - /* On WinCE winsock2.h must be included before windows.h for socket stuff */ - #include -#endif - #include #include #include @@ -56,7 +51,9 @@ int Nucleus_Net_Errno; #endif #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT) - #ifndef USE_WINDOWS_API + #ifdef USE_WINDOWS_API + #include + #else #if defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT) #elif defined(ARDUINO) #elif defined(FREESCALE_MQX) @@ -248,7 +245,7 @@ static int TranslateIoReturnCode(int err, SOCKET_T sd, int direction) return WOLFSSL_CBIO_ERR_CONN_CLOSE; } -#if defined(_WIN32) +#if defined(_WIN32) && !defined(__WATCOMC__) strcpy_s(errstr, sizeof(errstr), "\tGeneral error: "); errstr_offset = strlen(errstr); FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, diff --git a/tests/api.c b/tests/api.c index 01e810abe8..410ea272af 100644 --- a/tests/api.c +++ b/tests/api.c @@ -79305,19 +79305,22 @@ static int test_wc_ParseCert_Error(void) const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00}; /* Test data */ - const struct testStruct { + struct testStruct { const byte* c; word32 cSz; - const int expRet; - } t[] = { - {c0, sizeof(c0), WC_NO_ERR_TRACE(ASN_PARSE_E)}, /* Invalid bit-string length */ - {c1, sizeof(c1), WC_NO_ERR_TRACE(ASN_PARSE_E)}, /* Invalid bit-string length */ - {c2, sizeof(c2), WC_NO_ERR_TRACE(ASN_PARSE_E)}, /* Invalid integer length (zero) */ - {c3, sizeof(c3), WC_NO_ERR_TRACE(ASN_PARSE_E)}, /* Valid INTEGER, but buffer too short */ - {c4, sizeof(c4), WC_NO_ERR_TRACE(ASN_PARSE_E)}, /* Valid INTEGER, but not in bit-string */ - }; + int expRet; + } t[5]; const int tSz = (int)(sizeof(t) / sizeof(struct testStruct)); + #define INIT_TEST_DATA(i,x,y) \ + t[i].c = x; t[i].cSz = sizeof(x); t[i].expRet = y + INIT_TEST_DATA(0, c0, WC_NO_ERR_TRACE(ASN_PARSE_E) ); + INIT_TEST_DATA(1, c1, WC_NO_ERR_TRACE(ASN_PARSE_E) ); + INIT_TEST_DATA(2, c2, WC_NO_ERR_TRACE(ASN_PARSE_E) ); + INIT_TEST_DATA(3, c3, WC_NO_ERR_TRACE(ASN_PARSE_E) ); + INIT_TEST_DATA(4, c4, WC_NO_ERR_TRACE(ASN_PARSE_E) ); + #undef INIT_TEST_DATA + for (i = 0; i < tSz; i++) { WOLFSSL_MSG_EX("i == %d", i); wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL); @@ -81887,7 +81890,10 @@ static char earlyDataBuffer[1]; static int test_tls13_apis(void) { EXPECT_DECLS; - int ret; +#if defined(HAVE_SUPPORTED_CURVES) && defined(HAVE_ECC) && \ + (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)) + int ret; +#endif #ifndef WOLFSSL_NO_TLS12 #ifndef NO_WOLFSSL_CLIENT WOLFSSL_CTX* clientTls12Ctx = NULL; @@ -82011,8 +82017,6 @@ static int test_tls13_apis(void) int kyberLevel; #endif - (void)ret; - #ifndef WOLFSSL_NO_TLS12 #ifndef NO_WOLFSSL_CLIENT clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()); @@ -93043,10 +93047,10 @@ static int test_wolfSSL_CTX_set_timeout(void) EXPECT_DECLS; #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_TLS) && \ !defined(NO_SESSION_CACHE) - int timeout; WOLFSSL_CTX* ctx = NULL; - - (void)timeout; +#if defined(WOLFSSL_ERROR_CODE_OPENSSL) + int timeout; +#endif ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())); @@ -102892,14 +102896,19 @@ static const char* apitest_res_string(int res) #ifndef WOLFSSL_UNIT_TEST_NO_TIMING static double gettime_secs(void) - #if defined(_MSC_VER) && defined(_WIN32) + #if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__)) { /* there's no gettimeofday for Windows, so we'll use system time */ #define EPOCH_DIFF 11644473600LL FILETIME currentFileTime; + ULARGE_INTEGER uli = { 0, 0 }; + + #if defined(__WATCOMC__) + GetSystemTimeAsFileTime(¤tFileTime); + #else GetSystemTimePreciseAsFileTime(¤tFileTime); + #endif - ULARGE_INTEGER uli = { 0, 0 }; uli.LowPart = currentFileTime.dwLowDateTime; uli.HighPart = currentFileTime.dwHighDateTime; diff --git a/tests/suites.c b/tests/suites.c index e900c5d0a9..e55f03c105 100644 --- a/tests/suites.c +++ b/tests/suites.c @@ -361,8 +361,8 @@ static int execute_test_case(int svr_argc, char** svr_argv, func_args cliArgs = {0, NULL, 0, NULL, NULL, NULL}; func_args svrArgs = {0, NULL, 0, NULL, NULL, NULL}; #else - func_args cliArgs = {cli_argc, cli_argv, 0, NULL, NULL}; - func_args svrArgs = {svr_argc, svr_argv, 0, NULL, NULL}; + func_args cliArgs = {0, NULL, 0, NULL, NULL}; + func_args svrArgs = {0, NULL, 0, NULL, NULL}; #endif tcp_ready ready; @@ -379,17 +379,14 @@ static int execute_test_case(int svr_argc, char** svr_argv, #ifdef WOLFSSL_NO_CLIENT_AUTH int reqClientCert; #endif - #if defined(WOLFSSL_SRTP) && defined(WOLFSSL_COND) srtp_test_helper srtp_helper; #endif -#if defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_SRTP) cliArgs.argc = cli_argc; cliArgs.argv = cli_argv; svrArgs.argc = svr_argc; svrArgs.argv = svr_argv; -#endif /* Is Valid Cipher and Version Checks */ /* build command list for the Is checks below */ diff --git a/tests/utils.h b/tests/utils.h index c12f87d6f2..38a93cf8db 100644 --- a/tests/utils.h +++ b/tests/utils.h @@ -27,9 +27,15 @@ #ifndef NO_FILESYSTEM -#ifdef _MSC_VER +#if defined(_MSC_VER) +#include +#elif defined(__WATCOMC__) +#ifdef __LINUX__ +#include +#else #include #endif +#endif #define TMP_DIR_PREFIX "tmpDir-" /* len is length of tmpDir name, assuming @@ -50,6 +56,9 @@ char* create_tmp_dir(char *tmpDir, int len) #elif defined(__MINGW32__) if (mkdir(tmpDir) != 0) return NULL; +#elif defined(__WATCOMC__) && !defined(__LINUX__) + if (mkdir(tmpDir) != 0) + return NULL; #else if (mkdir(tmpDir, 0700) != 0) return NULL; diff --git a/wolfcrypt/benchmark/benchmark.c b/wolfcrypt/benchmark/benchmark.c index 3e535d7d92..e63359c7b4 100644 --- a/wolfcrypt/benchmark/benchmark.c +++ b/wolfcrypt/benchmark/benchmark.c @@ -14463,6 +14463,7 @@ void bench_sphincsKeySign(byte level, byte optim) #if defined(_WIN32) && !defined(INTIME_RTOS) #define WIN32_LEAN_AND_MEAN + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include double current_time(int reset) diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index e0d2daebf3..05178206d1 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -87,6 +87,7 @@ This library contains implementation for the random number generator. #ifndef _WIN32_WINNT #define _WIN32_WINNT 0x0400 #endif + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include #include #elif defined(HAVE_WNR) @@ -913,7 +914,8 @@ static WC_INLINE word64 Entropy_TimeHiRes(void) * @param [in,out] args Entropy data including: counter and stop flag. * @return NULL always. */ -static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN Entropy_IncCounter(void* args) +static THREAD_RETURN_NOJOIN WOLFSSL_THREAD_NO_JOIN + Entropy_IncCounter(void* args) { (void)args; @@ -926,8 +928,9 @@ static THREAD_RETURN WOLFSSL_THREAD_NO_JOIN Entropy_IncCounter(void* args) #ifdef WOLFSSL_DEBUG_ENTROPY_MEMUSE fprintf(stderr, "EXITING ENTROPY COUNTER THREAD\n"); #endif + /* Exit from thread. */ - WOLFSSL_RETURN_FROM_THREAD(0); + RETURN_FROM_THREAD_NOJOIN(0); } /* Start a thread that increments counter if not one already. diff --git a/wolfcrypt/src/sp_int.c b/wolfcrypt/src/sp_int.c index efdcdb0cb6..b0dfa25b6a 100644 --- a/wolfcrypt/src/sp_int.c +++ b/wolfcrypt/src/sp_int.c @@ -5136,7 +5136,7 @@ int sp_init_size(sp_int* a, unsigned int size) int err = MP_OKAY; /* Validate parameters. Don't use size more than max compiled. */ - if ((a == NULL) || ((size <= 0) || (size > SP_INT_DIGITS))) { + if ((a == NULL) || ((size == 0) || (size > SP_INT_DIGITS))) { err = MP_VAL; } diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index 35afb1e4b3..19af7b79f7 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -1657,6 +1657,99 @@ int wolfSSL_HwPkMutexUnLock(void) return 0; } +#elif defined(__WATCOMC__) + + int wc_InitMutex(wolfSSL_Mutex* m) + { + #ifdef ___OS2__ + DosCreateMutexSem( NULL, m, 0, FALSE ); + #elif defined(__NT__) + InitializeCriticalSection(m); + #elif defined(__LINUX__) + if (pthread_mutex_init(m, NULL) ) + return BAD_MUTEX_E; + #endif + return 0; + } + + int wc_FreeMutex(wolfSSL_Mutex* m) + { + #ifdef ___OS2__ + DosCloseMutexSem(*m); + #elif defined(__NT__) + DeleteCriticalSection(m); + #elif defined(__LINUX__) + if (pthread_mutex_destroy(m) ) + return BAD_MUTEX_E; + #endif + return 0; + } + + int wc_LockMutex(wolfSSL_Mutex* m) + { + #ifdef ___OS2__ + DosRequestMutexSem(*m, SEM_INDEFINITE_WAIT); + #elif defined(__NT__) + EnterCriticalSection(m); + #elif defined(__LINUX__) + if (pthread_mutex_lock(m) ) + return BAD_MUTEX_E; + #endif + return 0; + } + + int wc_UnLockMutex(wolfSSL_Mutex* m) + { + #ifdef ___OS2__ + DosReleaseMutexSem(*m); + #elif defined(__NT__) + LeaveCriticalSection(m); + #elif defined(__LINUX__) + if (pthread_mutex_unlock(m) ) + return BAD_MUTEX_E; + #endif + return 0; + } + + #if defined(WOLFSSL_USE_RWLOCK) && defined(__LINUX__) + + int wc_InitRwLock(wolfSSL_RwLock* m) + { + if (pthread_rwlock_init(m, NULL) ) + return BAD_MUTEX_E; + return 0; + } + + int wc_FreeRwLock(wolfSSL_RwLock* m) + { + if (pthread_rwlock_destroy(m) ) + return BAD_MUTEX_E; + return 0; + } + + int wc_LockRwLock_Wr(wolfSSL_RwLock* m) + { + if (pthread_rwlock_wrlock(m) ) + return BAD_MUTEX_E; + return 0; + } + + int wc_LockRwLock_Rd(wolfSSL_RwLock* m) + { + if (pthread_rwlock_rdlock(m) ) + return BAD_MUTEX_E; + return 0; + } + + int wc_UnLockRwLock(wolfSSL_RwLock* m) + { + if (pthread_rwlock_unlock(m) == 0) + return BAD_MUTEX_E; + return 0; + } + + #endif + #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ defined(FREESCALE_FREE_RTOS) @@ -3734,7 +3827,213 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) #ifndef SINGLE_THREADED /* Environment-specific multi-thread implementation check */ -#if defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS) && \ +#if defined(__WATCOMC__) + + int wolfSSL_NewThread(THREAD_TYPE* thread, + THREAD_CB cb, void* arg) + { + if (thread == NULL || cb == NULL) + return BAD_FUNC_ARG; + #if defined(___OS2__) + *thread = _beginthread(cb, NULL, 0, arg); + if (*thread == INVALID_THREAD_VAL) { + return MEMORY_E; + } + #elif defined(__NT__) + /* Use _beginthreadex instead of _beginthread because of: + * _beginthreadex is safer to use than _beginthread. If the thread + * that's generated by _beginthread exits quickly, the handle that's + * returned to the caller of _beginthread might be invalid or point + * to another thread. However, the handle that's returned by + * _beginthreadex has to be closed by the caller of _beginthreadex, + * so it's guaranteed to be a valid handle if _beginthreadex didn't + * return an error.*/ + *thread = _beginthreadex(NULL, 0, cb, arg, 0, NULL); + if (*thread == 0) { + *thread = INVALID_THREAD_VAL; + return MEMORY_E; + } + #elif defined(__LINUX__) + if (pthread_create(thread, NULL, cb, arg)) + return MEMORY_E; + #endif + return 0; + } + + int wolfSSL_JoinThread(THREAD_TYPE thread) + { + int ret = 0; + + if (thread == INVALID_THREAD_VAL) + return BAD_FUNC_ARG; + #if defined(___OS2__) + DosWaitThread(&thread, DCWW_WAIT); + #elif defined(__NT__) + /* We still want to attempt to close the thread handle even on error */ + if (WaitForSingleObject((HANDLE)thread, INFINITE) == WAIT_FAILED) + ret = MEMORY_E; + if (CloseHandle((HANDLE)thread) == 0) + ret = MEMORY_E; + #elif defined(__LINUX__) + if (pthread_join(thread, NULL) != 0) + ret = MEMORY_E; + #endif + return ret; + } + + #if defined(WOLFSSL_THREAD_NO_JOIN) + int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg) + { + THREAD_TYPE thread; + int ret = 0; + + if (cb == NULL) + return BAD_FUNC_ARG; + #if defined(___OS2__) + thread = _beginthread(cb, NULL, 0, arg); + if (thread == INVALID_THREAD_VAL) + ret = MEMORY_E; + #elif defined(__NT__) + thread = _beginthread(cb, 0, arg); + if (thread == -1L) + ret = MEMORY_E; + #elif defined(__LINUX__) + XMEMSET(&thread, 0, sizeof(thread)); + ret = wolfSSL_NewThread(&thread, cb, arg); + if (ret == 0) + ret = pthread_detach(thread); + #endif + return ret; + } + #endif + + #ifdef WOLFSSL_COND + int wolfSSL_CondInit(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + DosCreateMutexSem( NULL, &cond->mutex, 0, FALSE ); + DosCreateEventSem( NULL, &cond->cond, DCE_POSTONE, FALSE ); + #elif defined(__NT__) + cond->cond = CreateEventA(NULL, FALSE, FALSE, NULL); + if (cond->cond == NULL) + return MEMORY_E; + + if (wc_InitMutex(&cond->mutex) != 0) { + if (CloseHandle(cond->cond) == 0) + return MEMORY_E; + return MEMORY_E; + } + #elif defined(__LINUX__) + if (pthread_mutex_init(&cond->mutex, NULL) != 0) + return MEMORY_E; + + if (pthread_cond_init(&cond->cond, NULL) != 0) { + /* Keep compilers happy that we are using the return code */ + if (pthread_mutex_destroy(&cond->mutex) != 0) + return MEMORY_E; + return MEMORY_E; + } + #endif + return 0; + } + + int wolfSSL_CondFree(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + DosCloseMutexSem(cond->mutex); + DosCloseEventSem(cond->cond); + #elif defined(__NT__) + if (CloseHandle(cond->cond) == 0) + return MEMORY_E; + #elif defined(__LINUX__) + if (pthread_mutex_destroy(&cond->mutex) != 0) + return MEMORY_E; + + if (pthread_cond_destroy(&cond->cond) != 0) + return MEMORY_E; + #endif + return 0; + } + + int wolfSSL_CondStart(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + #elif defined(__NT__) + if (wc_LockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + #elif defined(__LINUX__) + if (pthread_mutex_lock(&cond->mutex) != 0) + return BAD_MUTEX_E; + #endif + return 0; + } + + int wolfSSL_CondSignal(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + #elif defined(__NT__) + if (wc_UnLockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + + if (SetEvent(cond->cond) == 0) + return MEMORY_E; + + if (wc_LockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + #elif defined(__LINUX__) + if (pthread_cond_signal(&cond->cond) != 0) + return MEMORY_E; + #endif + return 0; + } + + int wolfSSL_CondWait(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + #elif defined(__NT__) + if (wc_UnLockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + + if (WaitForSingleObject(cond->cond, INFINITE) == WAIT_FAILED) + return MEMORY_E; + + if (wc_LockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + #elif defined(__LINUX__) + if (pthread_cond_wait(&cond->cond, &cond->mutex) != 0) + return MEMORY_E; + #endif + return 0; + } + + int wolfSSL_CondEnd(COND_TYPE* cond) + { + if (cond == NULL) + return BAD_FUNC_ARG; + #if defined(__OS2__) + #elif defined(__NT__) + if (wc_UnLockMutex(&cond->mutex) != 0) + return BAD_MUTEX_E; + #elif defined(__LINUX__) + if (pthread_mutex_unlock(&cond->mutex) != 0) + return BAD_MUTEX_E; + #endif + return 0; + } + #endif /* WOLFSSL_COND */ + + +#elif defined(USE_WINDOWS_API) && !defined(WOLFSSL_PTHREADS) && \ !defined(_WIN32_WCE) int wolfSSL_NewThread(THREAD_TYPE* thread, THREAD_CB cb, void* arg) diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 53897c9a0f..a5825bde95 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -7696,17 +7696,11 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void) 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; - - const byte* keys[] = {key1, key2, key3, key4}; - WOLFSSL_SMALL_STACK_STATIC const byte ivs1[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; WOLFSSL_SMALL_STACK_STATIC const byte ivs2[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; WOLFSSL_SMALL_STACK_STATIC const byte ivs3[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00}; WOLFSSL_SMALL_STACK_STATIC const byte ivs4[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; - - const byte* ivs[] = {ivs1, ivs2, ivs3, ivs4}; - #ifndef BENCH_EMBEDDED WOLFSSL_SMALL_STACK_STATIC const byte cipher_big_result[] = { 0x06, 0xa6, 0x5d, 0x31, 0x21, 0x6c, 0xdb, 0x37, 0x48, 0x7c, 0x01, 0x9d, @@ -7832,18 +7826,30 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t chacha_test(void) int block_size; #endif /* BENCH_EMBEDDED */ - byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90}; - byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96}; - byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5}; - byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd}; + const byte a[] = {0x76,0xb8,0xe0,0xad,0xa0,0xf1,0x3d,0x90}; + const byte b[] = {0x45,0x40,0xf0,0x5a,0x9f,0x1f,0xb2,0x96}; + const byte c[] = {0xde,0x9c,0xba,0x7b,0xf3,0xd6,0x9e,0xf5}; + const byte d[] = {0x89,0x67,0x09,0x52,0x60,0x83,0x64,0xfd}; - byte* test_chacha[4]; + const byte* test_chacha[4]; + const byte* keys[4]; + const byte* ivs[4]; test_chacha[0] = a; test_chacha[1] = b; test_chacha[2] = c; test_chacha[3] = d; + keys[0] = key1; + keys[1] = key2; + keys[2] = key3; + keys[3] = key4; + + ivs[0] = ivs1; + ivs[1] = ivs2; + ivs[2] = ivs3; + ivs[3] = ivs4; + WOLFSSL_ENTER("chacha_test"); #ifndef BENCH_EMBEDDED @@ -8203,17 +8209,40 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t poly1305_test(void) 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff }; - const byte* msgs[] = {NULL, msg1, msg2, msg3, msg5, msg6, msg7}; - word32 szm[] = {0, sizeof(msg1), sizeof(msg2), + const byte* msgs[7]; + const word32 szm[7] = {0, sizeof(msg1), sizeof(msg2), sizeof(msg3), sizeof(msg5), sizeof(msg6), sizeof(msg7)}; - const byte* keys[] = {key, key, key2, key2, key5, key, key7}; - const byte* tests[] = {correct0, correct1, correct2, correct3, correct5, - correct6, correct7}; + const byte* keys[7]; + const byte* tests[7]; int i; wc_test_ret_t ret = 0; WOLFSSL_ENTER("poly1305_test"); + msgs[0] = NULL; + msgs[1] = msg1; + msgs[2] = msg2; + msgs[3] = msg3; + msgs[4] = msg5; + msgs[5] = msg6; + msgs[6] = msg7; + + keys[0] = key; + keys[1] = key; + keys[2] = key2; + keys[3] = key2; + keys[4] = key5; + keys[5] = key; + keys[6] = key7; + + tests[0] = correct0; + tests[1] = correct1; + tests[2] = correct2; + tests[3] = correct3; + tests[4] = correct5; + tests[5] = correct6; + tests[6] = correct7; + for (i = 0; i < 7; i++) { ret = wc_Poly1305SetKey(&enc, keys[i], 32); if (ret != 0) @@ -22899,31 +22928,37 @@ static wc_test_ret_t dh_test_check_pubvalue(void) WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig0[] = { 0x02, 0x00, 0x01 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValTooBig1[] = { 0x01, 0x01, 0x01 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValTooLong[] = { 0x01, 0x00, 0x00, 0x01 }; - const dh_pubvalue_test dh_pubval_fail[] = { - { prime, sizeof(prime) }, - { pubValZero, sizeof(pubValZero) }, - { pubValZeroLong, sizeof(pubValZeroLong) }, - { pubValOne, sizeof(pubValOne) }, - { pubValOneLong, sizeof(pubValOneLong) }, - { pubValPrimeMinusOne, sizeof(pubValPrimeMinusOne) }, - { pubValPrimeLong, sizeof(pubValPrimeLong) }, - { pubValPrimePlusOne, sizeof(pubValPrimePlusOne) }, - { pubValTooBig0, sizeof(pubValTooBig0) }, - { pubValTooBig1, sizeof(pubValTooBig1) }, - { pubValTooLong, sizeof(pubValTooLong) }, - }; WOLFSSL_SMALL_STACK_STATIC const byte pubValTwo[] = { 0x02 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValTwoLong[] = { 0x00, 0x00, 0x02 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValGood[] = { 0x12, 0x34 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLen[] = { 0x00, 0x12, 0x34 }; WOLFSSL_SMALL_STACK_STATIC const byte pubValGoodLong[] = { 0x00, 0x00, 0x12, 0x34 }; - const dh_pubvalue_test dh_pubval_pass[] = { - { pubValTwo, sizeof(pubValTwo) }, - { pubValTwoLong, sizeof(pubValTwoLong) }, - { pubValGood, sizeof(pubValGood) }, - { pubValGoodLen, sizeof(pubValGoodLen) }, - { pubValGoodLong, sizeof(pubValGoodLong) }, - }; + dh_pubvalue_test dh_pubval_fail[11]; + dh_pubvalue_test dh_pubval_pass[5]; + + #define INIT_PUBVAL_FAIL(i,y) dh_pubval_fail[i].data = y; dh_pubval_fail[i].len = sizeof(y) + #define INIT_PUBVAL_PASS(i,y) dh_pubval_pass[i].data = y; dh_pubval_pass[i].len = sizeof(y) + + INIT_PUBVAL_FAIL(0, prime); + INIT_PUBVAL_FAIL(1, pubValZero); + INIT_PUBVAL_FAIL(2, pubValZeroLong); + INIT_PUBVAL_FAIL(3, pubValOne); + INIT_PUBVAL_FAIL(4, pubValOneLong); + INIT_PUBVAL_FAIL(5, pubValPrimeMinusOne); + INIT_PUBVAL_FAIL(6, pubValPrimeLong); + INIT_PUBVAL_FAIL(7, pubValPrimePlusOne); + INIT_PUBVAL_FAIL(8, pubValTooBig0); + INIT_PUBVAL_FAIL(9, pubValTooBig1); + INIT_PUBVAL_FAIL(10, pubValTooLong); + + INIT_PUBVAL_PASS(0, pubValTwo); + INIT_PUBVAL_PASS(1, pubValTwoLong); + INIT_PUBVAL_PASS(2, pubValGood); + INIT_PUBVAL_PASS(3, pubValGoodLen); + INIT_PUBVAL_PASS(4, pubValGoodLong); + + #undef INIT_PUBVAL_FAIL + #undef INIT_PUBVAL_PASS for (i = 0; i < sizeof(dh_pubval_fail) / sizeof(*dh_pubval_fail); i++) { ret = wc_DhCheckPubValue(prime, sizeof(prime), dh_pubval_fail[i].data, diff --git a/wolfssl/internal.h b/wolfssl/internal.h index c229299710..cedc04870e 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -151,14 +151,22 @@ #include #endif -#ifdef USE_WINDOWS_API +#ifdef __WATCOMC__ + #if defined(__OS2__) + #elif defined(__NT__) + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ + #include + #elif defined(__LINUX__) + #ifndef SINGLE_THREADED + #define WOLFSSL_PTHREADS + #include + #endif + #endif +#elif defined(USE_WINDOWS_API) #ifdef WOLFSSL_GAME_BUILD #include "system/xtl.h" #else - #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN) - /* On WinCE winsock2.h must be included before windows.h */ - #include - #endif + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include #endif #elif defined(THREADX) @@ -232,7 +240,7 @@ #endif #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) #ifdef FUSION_RTOS - #include + #include #else #include /* for close of BIO */ #endif diff --git a/wolfssl/openssl/ssl.h b/wolfssl/openssl/ssl.h index 30cd988bfd..da1616845d 100644 --- a/wolfssl/openssl/ssl.h +++ b/wolfssl/openssl/ssl.h @@ -1116,7 +1116,7 @@ wolfSSL_X509_STORE_set_verify_cb((WOLFSSL_X509_STORE *)(s), (WOLFSSL_X509_STORE_ #define SSL_CTX_set_psk_server_callback wolfSSL_CTX_set_psk_server_callback #define SSL_set_psk_server_callback wolfSSL_set_psk_server_callback -#ifndef INVALID_SOCKET +#if !defined(USE_WINDOWS_API) && !defined(INVALID_SOCKET) #define INVALID_SOCKET (-1) #endif diff --git a/wolfssl/test.h b/wolfssl/test.h index 33b86404eb..87f31f5d96 100644 --- a/wolfssl/test.h +++ b/wolfssl/test.h @@ -1449,7 +1449,7 @@ static WC_INLINE void tcp_socket(SOCKET_T* sockfd, int udp, int sctp) err_sys_with_errno("setsockopt SO_NOSIGPIPE failed\n"); } #elif defined(WOLFSSL_MDK_ARM) || defined (WOLFSSL_TIRTOS) ||\ - defined(WOLFSSL_KEIL_TCP_NET) || defined(WOLFSSL_ZEPHYR) + defined(WOLFSSL_KEIL_TCP_NET) || defined(WOLFSSL_ZEPHYR) /* nothing to define */ #elif defined(NETOS) /* TODO: signal(SIGPIPE, SIG_IGN); */ @@ -1521,7 +1521,7 @@ static WC_INLINE int tcp_select_ex(SOCKET_T socketfd, int to_sec, int rx) fd_set* recvfds = NULL; fd_set* sendfds = NULL; SOCKET_T nfds = socketfd + 1; -#if !defined(__INTEGRITY) +#if !defined(__INTEGRITY) && !defined(__WATCOMC__) struct timeval timeout = {(to_sec > 0) ? to_sec : 0, 0}; #else struct timeval timeout; @@ -1538,8 +1538,9 @@ static WC_INLINE int tcp_select_ex(SOCKET_T socketfd, int to_sec, int rx) else sendfds = &fds; -#if defined(__INTEGRITY) - timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0, 0; +#if defined(__INTEGRITY) || defined(__WATCOMC__) + timeout.tv_sec = (long long)(to_sec > 0) ? to_sec : 0; + timeout.tv_usec = 0; #endif result = select(nfds, recvfds, sendfds, &errfds, &timeout); @@ -2131,6 +2132,7 @@ static WC_INLINE unsigned int my_psk_client_cs_cb(WOLFSSL* ssl, #elif defined(USE_WINDOWS_API) #define WIN32_LEAN_AND_MEAN + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include static WC_INLINE double current_time(int reset) @@ -4508,7 +4510,7 @@ static WC_INLINE int SimulateWantWriteIOSendCb(WOLFSSL *ssl, char *buf, int sz, #endif /* USE_WOLFSSL_IO */ #if defined(__hpux__) || defined(__MINGW32__) || defined (WOLFSSL_TIRTOS) \ - || defined(_MSC_VER) + || defined(_MSC_VER) || defined(__WATCOMC__) /* HP/UX doesn't have strsep, needed by test/suites.c */ static WC_INLINE char* strsep(char **stringp, const char *delim) diff --git a/wolfssl/wolfcrypt/types.h b/wolfssl/wolfcrypt/types.h index 6431dd4ceb..f6a7b2cd1e 100644 --- a/wolfssl/wolfcrypt/types.h +++ b/wolfssl/wolfcrypt/types.h @@ -406,7 +406,7 @@ typedef struct w64wrapper { /* set up thread local storage if available */ #ifdef HAVE_THREAD_LS - #if defined(_MSC_VER) + #if defined(_MSC_VER) || defined(__WATCOMC__) #define THREAD_LS_T __declspec(thread) /* Thread local storage only in FreeRTOS v8.2.1 and higher */ #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \ @@ -996,6 +996,10 @@ typedef struct w64wrapper { #define XTOLOWER(c) tolower((c)) #endif + #ifdef __WATCOMC__ + /* avoid OFFSETOF conflict in os2def.h */ + #undef OFFSETOF + #endif #ifndef OFFSETOF #if defined(__clang__) || (defined(__GNUC__) && (__GNUC__ >= 4)) #define OFFSETOF(type, field) __builtin_offsetof(type, field) @@ -1476,6 +1480,46 @@ typedef struct w64wrapper { * wolfSSL_JoinThread() and wolfSSL_Cond signaling if they want. * Otherwise, those functions are omitted. */ + #elif defined(__WATCOMC__) + #if __WATCOMC__ < 1300 + #define _WCCALLBACK + #endif + #if defined(__NT__) + typedef unsigned THREAD_RETURN; + typedef uintptr_t THREAD_TYPE; + typedef struct COND_TYPE { + wolfSSL_Mutex mutex; + HANDLE cond; + } COND_TYPE; + #define WOLFSSL_COND + #define INVALID_THREAD_VAL ((THREAD_TYPE)(INVALID_HANDLE_VALUE)) + #define WOLFSSL_THREAD __stdcall + #define WOLFSSL_THREAD_NO_JOIN _WCCALLBACK + #elif defined(__OS2__) + typedef void THREAD_RETURN; + typedef TID THREAD_TYPE; + typedef struct COND_TYPE { + wolfSSL_Mutex mutex; + LHANDLE cond; + } COND_TYPE; + #define WOLFSSL_COND + #define INVALID_THREAD_VAL ((THREAD_TYPE)(-1)) + #define WOLFSSL_THREAD _WCCALLBACK + #define WOLFSSL_THREAD_NO_JOIN _WCCALLBACK + #elif defined(__LINUX__) + #include + typedef struct COND_TYPE { + pthread_mutex_t mutex; + pthread_cond_t cond; + } COND_TYPE; + typedef void* THREAD_RETURN; + typedef pthread_t THREAD_TYPE; + #define WOLFSSL_COND + #define WOLFSSL_THREAD + #ifndef HAVE_SELFTEST + #define WOLFSSL_THREAD_NO_JOIN + #endif + #endif #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET) || \ defined(FREESCALE_MQX) typedef unsigned int THREAD_RETURN; @@ -1588,8 +1632,6 @@ typedef struct w64wrapper { * to check if the value is an invalid thread * WOLFSSL_THREAD - attribute that should be used to declare thread * callbacks - * WOLFSSL_THREAD_NO_JOIN - attribute that should be used to declare - * thread callbacks that don't require cleanup * WOLFSSL_COND - defined if this system supports signaling * COND_TYPE - type that should be passed into the signaling API * WOLFSSL_THREAD_VOID_RETURN - defined if the thread callback has a @@ -1597,8 +1639,16 @@ typedef struct w64wrapper { * WOLFSSL_RETURN_FROM_THREAD - define used to correctly return from a * thread callback * THREAD_CB - thread callback type for regular threading API - * THREAD_CB_NOJOIN - thread callback type for threading API that don't + * + * WOLFSSL_THREAD_NO_JOIN - attribute used to declare thread callbacks + * that do not require cleanup + * THREAD_CB_NOJOIN - thread callback type for thread APIs that do not * require cleanup + * THREAD_RETURN_NOJOIN - return type used to declare thread callbacks + * that do not require cleanup + * RETURN_FROM_THREAD_NOJOIN - define used to correctly return from + * a thread callback that do not require + * cleanup * * Other defines/types are specific for the threading implementation */ @@ -1621,8 +1671,17 @@ typedef struct w64wrapper { /* Create a thread that will be automatically cleaned up. We can't * return a handle/pointer to the new thread because there are no * guarantees for how long it will be valid. */ - typedef THREAD_RETURN (WOLFSSL_THREAD_NO_JOIN *THREAD_CB_NOJOIN) - (void* arg); + #if defined(WOLFSSL_PTHREADS) + #define THREAD_CB_NOJOIN THREAD_CB + #define THREAD_RETURN_NOJOIN THREAD_RETURN + #define RETURN_FROM_THREAD_NOJOIN(x) \ + WOLFSSL_RETURN_FROM_THREAD(x) + #else + #define THREAD_RETURN_NOJOIN void + typedef THREAD_RETURN_NOJOIN + (WOLFSSL_THREAD_NO_JOIN *THREAD_CB_NOJOIN)(void* arg); + #define RETURN_FROM_THREAD_NOJOIN(x) return + #endif WOLFSSL_API int wolfSSL_NewThreadNoJoin(THREAD_CB_NOJOIN cb, void* arg); #endif diff --git a/wolfssl/wolfcrypt/visibility.h b/wolfssl/wolfcrypt/visibility.h index 01d4bb4afd..b8eb035e4a 100644 --- a/wolfssl/wolfcrypt/visibility.h +++ b/wolfssl/wolfcrypt/visibility.h @@ -33,7 +33,7 @@ #if defined(BUILDING_WOLFSSL) #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) || \ - defined(_WIN32_WCE) + defined(_WIN32_WCE) || defined(__WATCOMC__) #if defined(WOLFSSL_DLL) #define WOLFSSL_API __declspec(dllexport) #else @@ -52,7 +52,7 @@ #endif /* HAVE_VISIBILITY */ #else /* BUILDING_WOLFSSL */ #if defined(_MSC_VER) || defined(__MINGW32__) || defined(__CYGWIN__) || \ - defined(_WIN32_WCE) + defined(_WIN32_WCE) || defined(__WATCOMC__) #if defined(WOLFSSL_DLL) #define WOLFSSL_API __declspec(dllimport) #else diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 0ef6c8637d..db8ec6a4de 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -120,7 +120,34 @@ #endif /* THREADING/MUTEX SECTION */ -#if defined(SINGLE_THREADED) && defined(NO_FILESYSTEM) +#if defined(__WATCOMC__) + #if !defined(SINGLE_THREADED) + #if defined(USE_WINDOWS_API) + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ + #include + #include + #elif defined(__OS2__) + #define INCL_DOSSEMAPHORES + #define INCL_DOSPROCESS + #include + #include + #else + #ifndef WOLFSSL_USER_MUTEX + #define WOLFSSL_PTHREADS + #endif + #if defined(WOLFSSL_PTHREADS) + #include + #endif + #endif + #else + #if defined(USE_WINDOWS_API) + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ + #include + #elif defined(__OS2__) + #include + #endif + #endif +#elif defined(SINGLE_THREADED) && defined(NO_FILESYSTEM) /* No system headers required for build. */ #elif defined(USE_WINDOWS_API) #if defined(WOLFSSL_PTHREADS) @@ -133,10 +160,7 @@ #define WIN32_LEAN_AND_MEAN #endif #if !defined(WOLFSSL_SGX) && !defined(WOLFSSL_NOT_WINDOWS_API) - #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN) - /* On WinCE winsock2.h must be included before windows.h */ - #include - #endif + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include #ifndef WOLFSSL_USER_IO #include /* required for InetPton */ @@ -371,6 +395,9 @@ /* typedef User_Mutex wolfSSL_Mutex; */ #elif defined(WOLFSSL_LINUXKM) /* definitions are in linuxkm/linuxkm_wc_port.h */ + #elif defined(__WATCOMC__) + /* OS/2 */ + typedef ULONG wolfSSL_Mutex; #else #error Need a mutex type in multithreaded mode #endif /* USE_WINDOWS_API */ @@ -1178,6 +1205,7 @@ WOLFSSL_ABI WOLFSSL_API int wolfCrypt_Cleanup(void); #define XGMTIME(c, t) gmtime((c)) #elif defined(_WIN32_WCE) + #define _WINSOCKAPI_ /* Force winsock (workaround for WinCE) */ #include #include /* For file system */ diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index 804267d315..2e460f5f4f 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -152,6 +152,26 @@ #include #elif defined(WOLFSSL_EMNET) #include + #elif defined(__WATCOMC__) + #if defined(__OS2__) + #include + #include + #include + #include + #include + #include + #include + #include + + typedef int socklen_t; + #elif defined(__LINUX__) + #include + #include + #include + #include + #include + #include + #endif #elif !defined(WOLFSSL_NO_SOCK) #include #include