diff --git a/src/internal.c b/src/internal.c index 2c36b0355..c4bcc7914 100644 --- a/src/internal.c +++ b/src/internal.c @@ -6097,6 +6097,16 @@ ProtocolVersion MakeDTLSv1_2(void) return (word32)(uTaskerSystemTick / TICK_RESOLUTION); } +#elif defined(WOLFSSL_NUCLEUS_1_2) + + #define NU_TICKS_PER_SECOND 100 + + word32 LowResTimer(void) + { + /* returns number of 10ms ticks, so 100 ticks/sec */ + return NU_Retrieve_Clock() / NU_TICKS_PER_SECOND; + } + #else /* Posix style time */ #ifndef USER_TIME diff --git a/wolfcrypt/src/random.c b/wolfcrypt/src/random.c index c1aef714d..b60abfa91 100644 --- a/wolfcrypt/src/random.c +++ b/wolfcrypt/src/random.c @@ -142,6 +142,8 @@ int wc_RNG_GenerateByte(WC_RNG* rng, byte* b) #elif defined(WOLFSSL_ROWLEY_ARM) #elif defined(WOLFSSL_EMBOS) #elif defined(MICRIUM) +#elif defined(WOLFSSL_NUCLEUS) +#elif defined(WOLFSSL_PB) #else /* include headers that may be needed to get good seed */ #include @@ -1690,6 +1692,20 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } + +#elif defined(WOLFSSL_PB) + + int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz) + { + word32 i; + for (i = 0; i < sz; i++) + output[i] = UTL_Rand(); + + (void)os; + + return 0; + } + #elif defined(WOLFSSL_NUCLEUS) #include "nucleus.h" #include "kernel/plus_common.h" diff --git a/wolfcrypt/src/sha.c b/wolfcrypt/src/sha.c index 65e8a375c..38b14f557 100644 --- a/wolfcrypt/src/sha.c +++ b/wolfcrypt/src/sha.c @@ -287,6 +287,14 @@ static WC_INLINE void AddLength(wc_Sha* sha, word32 len) #define f3(x,y,z) (((x)&(y))|((z)&((x)|(y)))) #define f4(x,y,z) ((x)^(y)^(z)) + #ifdef WOLFSSL_NUCLEUS_1_2 + /* nucleus.h also defines R1-R4 */ + #undef R1 + #undef R2 + #undef R3 + #undef R4 + #endif + /* (R0+R1), R2, R3, R4 are the different operations used in SHA1 */ #define R0(v,w,x,y,z,i) (z)+= f1((w),(x),(y)) + blk0((i)) + 0x5A827999+ \ rotlFixed((v),5); (w) = rotlFixed((w),30); diff --git a/wolfcrypt/src/wc_port.c b/wolfcrypt/src/wc_port.c index ab1d1e73d..f89ca07ac 100644 --- a/wolfcrypt/src/wc_port.c +++ b/wolfcrypt/src/wc_port.c @@ -221,7 +221,7 @@ int wolfCrypt_Cleanup(void) } #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \ - !defined(WOLFSSL_NUCLEUS) + !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) /* File Handling Helpers */ /* returns 0 if file found, -1 if no files or negative error */ @@ -1255,6 +1255,44 @@ int wolfSSL_CryptHwMutexUnLock(void) { return ret; } +#elif defined(WOLFSSL_NUCLEUS_1_2) + + int wc_InitMutex(wolfSSL_Mutex* m) + { + /* Call the Nucleus function to create the semaphore */ + if (NU_Create_Semaphore(m, "WOLFSSL_MTX", 1, + NU_PRIORITY) == NU_SUCCESS) { + return 0; + } + + return BAD_MUTEX_E; + } + + int wc_FreeMutex(wolfSSL_Mutex* m) + { + if (NU_Delete_Semaphore(m) == NU_SUCCESS) + return 0; + + return BAD_MUTEX_E; + } + + int wc_LockMutex(wolfSSL_Mutex* m) + { + /* passing suspend task option */ + if (NU_Obtain_Semaphore(m, NU_SUSPEND) == NU_SUCCESS) + return 0; + + return BAD_MUTEX_E; + } + + int wc_UnLockMutex(wolfSSL_Mutex* m) + { + if (NU_Release_Semaphore(m) == NU_SUCCESS) + return 0; + + return BAD_MUTEX_E; + } + #else #warning No mutex handling defined @@ -1487,6 +1525,66 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n) } #endif +/* custom memory wrappers */ +#ifdef WOLFSSL_NUCLEUS_1_2 + + /* system memory pool */ + extern NU_MEMORY_POOL System_Memory; + + void* nucleus_malloc(unsigned long size, void* heap, int type) + { + STATUS status; + void* stack_ptr; + + status = NU_Allocate_Memory(&System_Memory, &stack_ptr, size, + NU_NO_SUSPEND); + if (status == NU_SUCCESS) { + return 0; + } else { + return stack_ptr; + } + } + + void* nucleus_realloc(void* ptr, unsigned long size, void* heap, int type) + { + STATUS status; + DM_HEADER* old_header; + word32 old_size, copy_size; + void* new_mem; + + /* if ptr is NULL, behave like malloc */ + new_mem = nucleus_malloc(size, NULL, 0); + if (new_mem == 0 || ptr == 0) { + return new_mem; + } + + /* calculate old memory block size */ + /* mem pointers stored in block headers (ref dm_defs.h) */ + old_header = (DM_HEADER*) ((byte*)ptr - DM_OVERHEAD); + old_size = (byte*)old_header->dm_next_memory - (byte*)ptr; + + /* copy old to new */ + if (old_size < size) { + copy_size = old_size; + } else { + copy_size = size; + } + XMEMCPY(new_mem, ptr, copy_size); + + /* free old */ + nucleus_free(ptr, NULL, 0); + + return new_mem; + } + + void nucleus_free(void* ptr, void* heap, int type) + { + if (ptr != NULL) + NU_Deallocate_Memory(ptr); + } + +#endif /* WOLFSSL_NUCLEUS_1_2 */ + #if defined(WOLFSSL_TI_CRYPT) || defined(WOLFSSL_TI_HASH) #include /* initialize and Mutex for TI Crypt Engine */ #include /* md5, sha1, sha224, sha256 */ diff --git a/wolfcrypt/test/test.c b/wolfcrypt/test/test.c index 52981a203..5fdeda243 100644 --- a/wolfcrypt/test/test.c +++ b/wolfcrypt/test/test.c @@ -197,6 +197,11 @@ void BSP_Ser_Printf (CPU_CHAR* format, ...); #undef printf #define printf BSP_Ser_Printf +#elif defined(WOLFSSL_PB) + #include + int wolfssl_pb_print(const char*, ...); + #undef printf + #define printf wolfssl_pb_print #endif #include "wolfcrypt/test/test.h" @@ -395,6 +400,23 @@ static void myFipsCb(int ok, int err, const char* hash) #endif #endif +#ifdef WOLFSSL_PB +int wolfssl_pb_print(const char* msg, ...) +{ + int ret; + va_list args; + char tmpBuf[80]; + + va_start(args, msg); + ret = vsprint(tmpBuf, msg, args); + va_end(args); + + fnDumpStringToSystemLog(tmpBuf); + + return ret; +} +#endif /* WOLFSSL_PB */ + #ifdef HAVE_STACK_SIZE THREAD_RETURN WOLFSSL_THREAD wolfcrypt_test(void* args) #else diff --git a/wolfssl/internal.h b/wolfssl/internal.h index 5660065bf..719802952 100644 --- a/wolfssl/internal.h +++ b/wolfssl/internal.h @@ -162,6 +162,8 @@ /* do nothing */ #elif defined(INTIME_RTOS) #include +#elif defined(WOLFSSL_NUCLEUS_1_2) + /* do nothing */ #else #ifndef SINGLE_THREADED #define WOLFSSL_PTHREADS diff --git a/wolfssl/wolfcrypt/integer.h b/wolfssl/wolfcrypt/integer.h index 14767b9ab..0f9c925b2 100644 --- a/wolfssl/wolfcrypt/integer.h +++ b/wolfssl/wolfcrypt/integer.h @@ -71,6 +71,11 @@ extern "C" { /* C++ compilers don't like assigning void * to mp_digit * */ #define OPT_CAST(x) (x *) +#elif defined(_SH3) + +/* SuperH SH3 compiler doesn't like assigning voi* to mp_digit* */ +#define OPT_CAST(x) (x *) + #else /* C on the other hand doesn't care */ diff --git a/wolfssl/wolfcrypt/settings.h b/wolfssl/wolfcrypt/settings.h index 8b531dcd4..9309c0e54 100644 --- a/wolfssl/wolfcrypt/settings.h +++ b/wolfssl/wolfcrypt/settings.h @@ -166,6 +166,9 @@ /* Uncomment next line if building for using XILINX */ /* #define WOLFSSL_XILINX */ +/* Uncomment next line if building for Nucleus 1.2 */ +/* #define WOLFSSL_NUCLEUS_1_2 */ + #include #ifdef WOLFSSL_USER_SETTINGS @@ -451,6 +454,32 @@ #include "wolfssl_chibios.h" #endif +#ifdef WOLFSSL_PB + /* PB is using older 1.2 version of Nucleus */ + #undef WOLFSSL_NUCLEUS + #define WOLFSSL_NUCLEUS_1_2 +#endif + +#ifdef WOLFSSL_NUCLEUS_1_2 + #define NO_WRITEV + #define NO_WOLFSSL_DIR + + #if !defined(NO_ASN_TIME) && !defined(USER_TIME) + #error User must define XTIME, see manual + #endif + + #if !defined(XMALLOC_OVERRIDE) && !defined(XMALLOC_USER) + extern void* nucleus_malloc(unsigned long size, void* heap, int type); + extern void* nucleus_realloc(void* ptr, unsigned long size, void* heap, + int type); + extern void nucleus_free(void* ptr, void* heap, int type); + + #define XMALLOC(s, h, type) nucleus_malloc + #define XREALLOC(p, n, h, t) nucleus_realloc + #define XFREE(p, h, type) nucleus_free + #endif +#endif + #ifdef WOLFSSL_NRF5x #define SIZEOF_LONG 4 #define SIZEOF_LONG_LONG 8 diff --git a/wolfssl/wolfcrypt/wc_port.h b/wolfssl/wolfcrypt/wc_port.h index 78700d643..0ac0e2514 100644 --- a/wolfssl/wolfcrypt/wc_port.h +++ b/wolfssl/wolfcrypt/wc_port.h @@ -97,6 +97,11 @@ #elif defined(INTIME_RTOS) #include #include +#elif defined(WOLFSSL_NUCLEUS_1_2) + /* NU_DEBUG needed struct access in nucleus_realloc */ + #define NU_DEBUG + #include "plus/nucleus.h" + #include "nucleus.h" #else #ifndef SINGLE_THREADED #define WOLFSSL_PTHREADS @@ -168,6 +173,8 @@ typedef mutex_t * wolfSSL_Mutex; #elif defined(INTIME_RTOS) typedef RTHANDLE wolfSSL_Mutex; + #elif defined(WOLFSSL_NUCLEUS_1_2) + typedef NU_SEMAPHORE wolfSSL_Mutex; #else #error Need a mutex type in multithreaded mode #endif /* USE_WINDOWS_API */ @@ -273,6 +280,18 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #define XSEEK_END FS_SEEK_END #define XBADFILE NULL #define XFGETS(b,s,f) -2 /* Not ported yet */ +#elif defined(WOLFSSL_NUCLEUS_1_2) + #include "fal/inc/fal.h" + #define XFILE FILE* + #define XFOPEN fopen + #define XFSEEK fseek + #define XFTELL ftell + #define XREWIND rewind + #define XFREAD fread + #define XFWRITE fwrite + #define XFCLOSE fclose + #define XSEEK_END PSEEK_END + #define XBADFILE NULL #else /* stdio, default case */ #include @@ -294,7 +313,7 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #define XFGETS fgets #if !defined(USE_WINDOWS_API) && !defined(NO_WOLFSSL_DIR)\ - && !defined(WOLFSSL_NUCLEUS) + && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2) #include #include #include @@ -308,7 +327,8 @@ WOLFSSL_API int wolfCrypt_Cleanup(void); #define MAX_PATH 256 #endif -#if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) +#if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \ + !defined(WOLFSSL_NUCLEUS_1_2) typedef struct ReadDirCtx { #ifdef USE_WINDOWS_API WIN32_FIND_DATAA FindFileData; diff --git a/wolfssl/wolfio.h b/wolfssl/wolfio.h index d60bdbe7a..cafaaad5e 100644 --- a/wolfssl/wolfio.h +++ b/wolfssl/wolfio.h @@ -80,6 +80,9 @@ #elif defined(WOLFSSL_VXWORKS) #include #include + #elif defined(WOLFSSL_NUCLEUS_1_2) + #include + #include #elif defined(WOLFSSL_ATMEL) #include "socket/include/socket.h" #elif defined(INTIME_RTOS) @@ -198,6 +201,14 @@ #define SOCKET_EPIPE FREERTOS_SOCKET_ERROR #define SOCKET_ECONNREFUSED FREERTOS_SOCKET_ERROR #define SOCKET_ECONNABORTED FREERTOS_SOCKET_ERROR +#elif defined(WOLFSSL_NUCLEUS_1_2) + #define SOCKET_EWOULDBLOCK NU_WOULD_BLOCK + #define SOCKET_EAGAIN NU_WOULD_BLOCK + #define SOCKET_ECONNRESET NU_NOT_CONNECTED + #define SOCKET_EINTR NU_NOT_CONNECTED + #define SOCKET_EPIPE NU_NOT_CONNECTED + #define SOCKET_ECONNREFUSED NU_CONNECTION_REFUSED + #define SOCKET_ECONNABORTED NU_NOT_CONNECTED #else #define SOCKET_EWOULDBLOCK EWOULDBLOCK #define SOCKET_EAGAIN EAGAIN @@ -244,6 +255,9 @@ #elif defined(WOLFSSL_VXWORKS) #define SEND_FUNCTION send #define RECV_FUNCTION recv +#elif defined(WOLFSSL_NUCLEUS_1_2) + #define SEND_FUNCTION NU_Send + #define RECV_FUNCTION NU_Recv #else #define SEND_FUNCTION send #define RECV_FUNCTION recv