From f6304ae37af55e1bdbc767f8723d90b18cb56964 Mon Sep 17 00:00:00 2001 From: Chris Conlon Date: Thu, 1 Nov 2012 11:23:42 -0600 Subject: [PATCH] add support for Freescale MQX --- ctaocrypt/src/asn.c | 13 +++++++--- ctaocrypt/src/logging.c | 6 ++++- ctaocrypt/src/random.c | 13 ++++++++++ ctaocrypt/src/rsa.c | 6 ++++- cyassl/ctaocrypt/des3.h | 2 +- cyassl/ctaocrypt/settings.h | 24 +++++++++++++++++++ cyassl/ctaocrypt/tfm.h | 2 +- cyassl/ctaocrypt/types.h | 3 ++- cyassl/internal.h | 6 ++++- cyassl/ssl.h | 6 ++++- src/internal.c | 48 ++++++++++++++++++++++++++++++++++--- src/io.c | 38 +++++++++++++++++++++++++++++ src/keys.c | 6 ++++- src/ssl.c | 10 ++++++++ 14 files changed, 169 insertions(+), 14 deletions(-) diff --git a/ctaocrypt/src/asn.c b/ctaocrypt/src/asn.c index 6db28fd21..c282225a8 100644 --- a/ctaocrypt/src/asn.c +++ b/ctaocrypt/src/asn.c @@ -53,7 +53,11 @@ #endif #ifdef CYASSL_DEBUG_ENCODING - #include + #ifdef FREESCALE_MQX + #include + #else + #include + #endif #endif #ifdef _MSC_VER @@ -497,8 +501,11 @@ static int GetAlgoId(const byte* input, word32* inOutIdx, word32* oid, if (GetLength(input, &i, &length, maxIdx) < 0) return ASN_PARSE_E; - while(length--) - *oid += input[i++]; + while(length--) { + /* odd HC08 compiler behavior here when input[i++] */ + *oid += input[i]; + i++; + } /* just sum it up for now */ /* could have NULL tag and 0 terminator, but may not */ diff --git a/ctaocrypt/src/logging.c b/ctaocrypt/src/logging.c index 584d1dda9..790b0db94 100644 --- a/ctaocrypt/src/logging.c +++ b/ctaocrypt/src/logging.c @@ -88,7 +88,11 @@ void CyaSSL_Debugging_OFF(void) #ifdef DEBUG_CYASSL -#include /* for default printf stuff */ +#ifdef FREESCALE_MQX + #include +#else + #include /* for default printf stuff */ +#endif #ifdef THREADX int dc_log_printf(char*, ...); diff --git a/ctaocrypt/src/random.c b/ctaocrypt/src/random.c index a4565db8b..43bfbbb97 100644 --- a/ctaocrypt/src/random.c +++ b/ctaocrypt/src/random.c @@ -159,6 +159,19 @@ int GenerateSeed(OS_Seed* os, byte* output, word32 sz) return 0; } +#elif defined(FREESCALE_MQX) + +#warning "write a real random seed!!!!, just for testing now" + +int GenerateSeed(OS_Seed* os, byte* output, word32 sz) +{ + int i; + for (i = 0; i < sz; i++ ) + output[i] = i; + + return 0; +} + #elif defined(NO_DEV_RANDOM) #error "you need to write an os specific GenerateSeed() here" diff --git a/ctaocrypt/src/rsa.c b/ctaocrypt/src/rsa.c index a84b559c7..06ad74351 100644 --- a/ctaocrypt/src/rsa.c +++ b/ctaocrypt/src/rsa.c @@ -32,7 +32,11 @@ #include #ifdef SHOW_GEN - #include + #ifdef FREESCALE_MQX + #include + #else + #include + #endif #endif diff --git a/cyassl/ctaocrypt/des3.h b/cyassl/ctaocrypt/des3.h index adae9a955..c6c864074 100644 --- a/cyassl/ctaocrypt/des3.h +++ b/cyassl/ctaocrypt/des3.h @@ -40,7 +40,7 @@ enum { DES_KS_SIZE = 32, DES_ENCRYPTION = 0, - DES_DECRYPTION = 1, + DES_DECRYPTION = 1 }; diff --git a/cyassl/ctaocrypt/settings.h b/cyassl/ctaocrypt/settings.h index de09ed07d..ac8042cb2 100644 --- a/cyassl/ctaocrypt/settings.h +++ b/cyassl/ctaocrypt/settings.h @@ -63,6 +63,9 @@ /* Uncomment next line if building CyaSSL for LSR */ /* #define CYASSL_LSR */ +/* Uncomment next line if building CyaSSL for Freescale MQX/RTCS/MFS */ +/* #define FREESCALE_MQX */ + #include @@ -192,6 +195,27 @@ #define TFM_TIMING_RESISTANT #endif +#ifdef FREESCALE_MQX + #define SIZEOF_LONG_LONG 8 + #define NO_WRITEV + #define NO_DEV_RANDOM + #define NO_RABBIT + #define NO_CYASSL_DIR + #define USE_FAST_MATH + #define TFM_TIMING_RESISTANT + #ifndef NO_FILESYSTEM + #include "mfs.h" + #include "fio.h" + #endif + #ifndef SINGLE_THREADED + #include "mutex.h" + #endif + + #define XMALLOC(s, h, type) (void *)_mem_alloc_system((s)) + #define XFREE(p, h, type) _mem_free(p) + /* Note: MQX has no realloc, using fastmath above */ +#endif + #ifdef MICRIUM #include "stdlib.h" diff --git a/cyassl/ctaocrypt/tfm.h b/cyassl/ctaocrypt/tfm.h index af74a9034..320fb594e 100644 --- a/cyassl/ctaocrypt/tfm.h +++ b/cyassl/ctaocrypt/tfm.h @@ -357,7 +357,7 @@ typedef struct { void fp_set(fp_int *a, fp_digit b); /* copy from a to b */ -#define fp_copy(a, b) (void)(((a) != (b)) ? (XMEMCPY((b), (a), sizeof(fp_int))) : (void)0) +#define fp_copy(a, b) (void)(((a) != (b)) ? ((void)XMEMCPY((b), (a), sizeof(fp_int))) : (void)0) #define fp_init_copy(a, b) fp_copy(b, a) /* clamp digits */ diff --git a/cyassl/ctaocrypt/types.h b/cyassl/ctaocrypt/types.h index 3c171ec51..897d941ae 100644 --- a/cyassl/ctaocrypt/types.h +++ b/cyassl/ctaocrypt/types.h @@ -150,7 +150,8 @@ enum { extern void *XMALLOC(size_t n, void* heap, int type); extern void *XREALLOC(void *p, size_t n, void* heap, int type); extern void XFREE(void *p, void* heap, int type); -#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) && !defined(CYASSL_SAFERTOS) +#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \ + && !defined(CYASSL_SAFERTOS) && !defined(FREESCALE_MQX) /* default C runtime, can install different routines at runtime */ #include #define XMALLOC(s, h, t) ((void)h, (void)t, CyaSSL_Malloc((s))) diff --git a/cyassl/internal.h b/cyassl/internal.h index 3eff23e3d..a06ccb9fb 100644 --- a/cyassl/internal.h +++ b/cyassl/internal.h @@ -73,6 +73,8 @@ /* do nothing */ #elif defined(EBSNET) /* do nothing */ +#elif defined(FREESCALE_MQX) + /* do nothing */ #else #ifndef SINGLE_THREADED #define CYASSL_PTHREADS @@ -724,6 +726,8 @@ struct CYASSL_CIPHER { typedef OS_MUTEX CyaSSL_Mutex; #elif defined(EBSNET) typedef RTP_MUTEX CyaSSL_Mutex; + #elif defined(FREESCALE_MQX) + typedef MUTEX_STRUCT CyaSSL_Mutex; #else #error Need a mutex type in multithreaded mode #endif /* USE_WINDOWS_API */ @@ -1359,7 +1363,7 @@ CYASSL_API void SSL_ResourceFree(CYASSL*); /* Micrium uses */ enum { IV_SZ = 32, /* max iv sz */ - NAME_SZ = 80, /* max one line */ + NAME_SZ = 80 /* max one line */ }; diff --git a/cyassl/ssl.h b/cyassl/ssl.h index e29bcc79f..f29c4af34 100644 --- a/cyassl/ssl.h +++ b/cyassl/ssl.h @@ -32,7 +32,11 @@ #ifndef NO_FILESYSTEM - #include /* ERR_printf */ + #ifdef FREESCALE_MQX + #include + #else + #include /* ERR_printf */ + #endif #endif #ifdef YASSL_PREFIX diff --git a/src/internal.c b/src/internal.c index 786916c89..430f42953 100644 --- a/src/internal.c +++ b/src/internal.c @@ -37,15 +37,23 @@ #endif #if defined(DEBUG_CYASSL) || defined(SHOW_SECRETS) - #include + #ifdef FREESCALE_MQX + #include + #else + #include + #endif #endif #ifdef __sun #include #endif -#define TRUE 1 -#define FALSE 0 +#ifndef TRUE + #define TRUE 1 +#endif +#ifndef FALSE + #define FALSE 0 +#endif #if defined(OPENSSL_EXTRA) && defined(NO_DH) @@ -8011,5 +8019,39 @@ int UnLockMutex(CyaSSL_Mutex *m) return 0; } + #elif defined(FREESCALE_MQX) + + int InitMutex(CyaSSL_Mutex* m) + { + if (_mutex_init(m, NULL) == MQX_EOK) + return 0; + else + return BAD_MUTEX_ERROR; + } + + int FreeMutex(CyaSSL_Mutex* m) + { + if (_mutex_destroy(m) == MQX_EOK) + return 0; + else + return BAD_MUTEX_ERROR; + } + + int LockMutex(CyaSSL_Mutex* m) + { + if (_mutex_lock(m) == MQX_EOK) + return 0; + else + return BAD_MUTEX_ERROR; + } + + int UnLockMutex(CyaSSL_Mutex* m) + { + if (_mutex_unlock(m) == MQX_EOK) + return 0; + else + return BAD_MUTEX_ERROR; + } + #endif /* USE_WINDOWS_API */ #endif /* SINGLE_THREADED */ diff --git a/src/io.c b/src/io.c index b9f2293b6..aaa78c724 100644 --- a/src/io.c +++ b/src/io.c @@ -48,6 +48,9 @@ #ifndef LWIP_PROVIDE_ERRNO #define LWIP_PROVIDE_ERRNO 1 #endif + #elif defined(FREESCALE_MQX) + #include + #include #else #include #include @@ -98,6 +101,14 @@ #define SOCKET_EINTR SYS_NET_EINTR #define SOCKET_EPIPE SYS_NET_EPIPE #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED +#elif defined(FREESCALE_MQX) + /* RTCS doesn't have an EWOULDBLOCK error */ + #define SOCKET_EWOULDBLOCK EAGAIN + #define SOCKET_EAGAIN EAGAIN + #define SOCKET_ECONNRESET RTCSERR_TCP_CONN_RESET + #define SOCKET_EINTR EINTR + #define SOCKET_EPIPE EPIPE + #define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED #else #define SOCKET_EWOULDBLOCK EWOULDBLOCK #define SOCKET_EAGAIN EAGAIN @@ -133,6 +144,29 @@ #endif +/* Translates return codes returned from + * send() and recv() if need be. + */ +static INLINE int TranslateReturnCode(int old, int sd) +{ + (void)sd; + +#ifdef FREESCALE_MQX + if (old == 0) { + errno = SOCKET_EWOULDBLOCK; + return -1; /* convert to BSD style wouldblock as error */ + } + + if (old < 0) { + errno = RTCS_geterror(sd); + if (errno == RTCSERR_TCP_CONN_CLOSING) + return 0; /* convert to BSD style closing */ + } +#endif + + return old; +} + static INLINE int LastError(void) { #ifdef USE_WINDOWS_API @@ -172,6 +206,8 @@ int EmbedReceive(CYASSL *ssl, char *buf, int sz, void *ctx) recvd = (int)RECV_FUNCTION(sd, buf, sz, ssl->rflags); + recvd = TranslateReturnCode(recvd, sd); + if (recvd < 0) { err = LastError(); CYASSL_MSG("Embed Receive error"); @@ -294,6 +330,8 @@ int EmbedReceiveFrom(CYASSL *ssl, char *buf, int sz, void *ctx) recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags, (struct sockaddr*)&peer, &peerSz); + recvd = TranslateReturnCode(recvd, sd); + if (recvd < 0) { err = LastError(); CYASSL_MSG("Embed Receive From error"); diff --git a/src/keys.c b/src/keys.c index f266b9ee6..ece9c9fa3 100644 --- a/src/keys.c +++ b/src/keys.c @@ -27,7 +27,11 @@ #include #include #ifdef SHOW_SECRETS - #include + #ifdef FREESCALE_MQX + #include + #else + #include + #endif #endif diff --git a/src/ssl.c b/src/ssl.c index 86fce8767..109c1cfff 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -1392,6 +1392,16 @@ static int ProcessChainBuffer(CYASSL_CTX* ctx, const unsigned char* buff, #define XFCLOSE fs_close #define XSEEK_END 0 #define XBADFILE NULL +#elif defined(FREESCALE_MQX) + #define XFILE MQX_FILE_PTR + #define XFOPEN fopen + #define XFSEEK fseek + #define XFTELL ftell + #define XREWIND(F) fseek(F, 0, IO_SEEK_SET) + #define XFREAD fread + #define XFCLOSE fclose + #define XSEEK_END IO_SEEK_END + #define XBADFILE NULL #elif defined(MICRIUM) #include #define XFILE FS_FILE*