Merge pull request #1686 from cconlon/nucleus-update

Nucleus port and PB changes
This commit is contained in:
JacobBarthelmeh
2018-07-25 09:17:40 -06:00
committed by GitHub
10 changed files with 227 additions and 3 deletions

View File

@ -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

View File

@ -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 <fcntl.h>
@ -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"

View File

@ -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);

View File

@ -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 <wolfcrypt/src/port/ti/ti-ccm.c> /* initialize and Mutex for TI Crypt Engine */
#include <wolfcrypt/src/port/ti/ti-hash.c> /* md5, sha1, sha224, sha256 */

View File

@ -197,6 +197,11 @@
void BSP_Ser_Printf (CPU_CHAR* format, ...);
#undef printf
#define printf BSP_Ser_Printf
#elif defined(WOLFSSL_PB)
#include <stdarg.h>
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

View File

@ -162,6 +162,8 @@
/* do nothing */
#elif defined(INTIME_RTOS)
#include <rt.h>
#elif defined(WOLFSSL_NUCLEUS_1_2)
/* do nothing */
#else
#ifndef SINGLE_THREADED
#define WOLFSSL_PTHREADS

View File

@ -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 */

View File

@ -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 <wolfssl/wolfcrypt/visibility.h>
#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

View File

@ -97,6 +97,11 @@
#elif defined(INTIME_RTOS)
#include <rt.h>
#include <io.h>
#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 <stdio.h>
@ -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 <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
@ -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;

View File

@ -80,6 +80,9 @@
#elif defined(WOLFSSL_VXWORKS)
#include <sockLib.h>
#include <errno.h>
#elif defined(WOLFSSL_NUCLEUS_1_2)
#include <externs.h>
#include <errno.h>
#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