Merge pull request #2190 from dgarske/telit

Port for Telit IoT AppZone SDK
This commit is contained in:
toddouska
2019-04-09 14:26:59 -07:00
committed by GitHub
13 changed files with 716 additions and 221 deletions

View File

@@ -6510,7 +6510,7 @@ ProtocolVersion MakeDTLSv1_2(void)
#else
/* Posix style time */
#ifndef USER_TIME
#if !defined(USER_TIME) && !defined(USE_WOLF_TM)
#include <time.h>
#endif

View File

@@ -3772,6 +3772,10 @@ static void TLSX_PointFormat_ValidateRequest(WOLFSSL* ssl, byte* semaphore)
static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
{
#if defined(HAVE_FFDHE) || defined(HAVE_ECC) || defined(HAVE_CURVE25519)
(void)semaphore;
#endif
if (ssl->options.cipherSuite0 == TLS13_BYTE)
return;
if (ssl->options.cipherSuite0 == ECC_BYTE ||
@@ -3786,8 +3790,10 @@ static void TLSX_PointFormat_ValidateResponse(WOLFSSL* ssl, byte* semaphore)
#endif
}
#if !defined(HAVE_FFDHE) || (!defined(HAVE_ECC) && !defined(HAVE_CURVE25519))
/* turns semaphore on to avoid sending this extension. */
TURN_ON(semaphore, TLSX_ToSemaphore(TLSX_EC_POINT_FORMATS));
#endif
}
#endif

View File

@@ -1164,6 +1164,13 @@ end:
{
return (word32) XTIME(0) * 1000;
}
#elif defined(XTIME_MS)
word32 TimeNowInMilliseconds(void)
{
return (word32)XTIME_MS(0);
}
#elif defined(USE_WINDOWS_API)
/* The time in milliseconds.
* Used for tickets to represent difference between when first seen and when

View File

@@ -33,20 +33,19 @@
/* Macro to disable benchmark */
#ifndef NO_CRYPT_BENCHMARK
#if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
/* MQX classic needs for EXIT_FAILURE */
#include <stdlib.h> /* we're using malloc / free direct here */
/* only for stack size check */
#ifdef HAVE_STACK_SIZE
#include <wolfssl/ssl.h>
#include <wolfssl/test.h>
#endif
#ifdef WOLFSSL_STATIC_MEMORY
#include <wolfssl/wolfcrypt/memory.h>
static WOLFSSL_HEAP_HINT* HEAP_HINT;
#ifdef USE_FLAT_BENCHMARK_H
#include "benchmark.h"
#else
#define HEAP_HINT NULL
#endif /* WOLFSSL_STATIC_MEMORY */
#include <string.h>
#include "wolfcrypt/benchmark/benchmark.h"
#endif
/* printf mappings */
#ifdef FREESCALE_MQX
#include <mqx.h>
#if MQX_USE_IO_OLD
@@ -71,11 +70,8 @@
#define printf BSP_Ser_Printf
#elif defined(WOLFSSL_ZEPHYR)
#include <stdio.h>
#define BENCH_EMBEDDED
#define printf printfk
static int printfk(const char *fmt, ...)
{
int ret;
@@ -92,10 +88,36 @@
return ret;
}
#elif defined(WOLFSSL_TELIT_M2MB)
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "wolfssl/wolfcrypt/wc_port.h" /* for m2mb headers */
#include "m2m_log.h" /* for M2M_LOG_INFO - not standard API */
/* remap printf */
#undef printf
#define printf M2M_LOG_INFO
/* OS requires occasional sleep() */
#ifndef TEST_SLEEP_MS
#define TEST_SLEEP_MS 50
#endif
#define TEST_SLEEP() m2mb_os_taskSleep(M2MB_OS_MS2TICKS(TEST_SLEEP_MS))
/* don't use file system for these tests, since ./certs dir isn't loaded */
#undef NO_FILESYSTEM
#define NO_FILESYSTEM
#else
#if defined(XMALLOC_USER) || defined(FREESCALE_MQX)
/* MQX classic needs for EXIT_FAILURE */
#include <stdlib.h> /* we're using malloc / free direct here */
#endif
#include <string.h>
#include <stdio.h>
#endif
#include <wolfssl/wolfcrypt/memory.h>
#include <wolfssl/wolfcrypt/random.h>
#include <wolfssl/wolfcrypt/des3.h>
#include <wolfssl/wolfcrypt/arc4.h>
@@ -142,18 +164,25 @@
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/types.h>
#ifdef WOLFSSL_ASYNC_CRYPT
#include <wolfssl/wolfcrypt/async.h>
#endif
#ifdef WOLFSSL_STATIC_MEMORY
static WOLFSSL_HEAP_HINT* HEAP_HINT;
#else
#define HEAP_HINT NULL
#endif /* WOLFSSL_STATIC_MEMORY */
#ifndef EXIT_FAILURE
#define EXIT_FAILURE 1
#endif
/* only for stack size check */
#ifdef HAVE_STACK_SIZE
#include <wolfssl/ssl.h>
#include <wolfssl/test.h>
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
#include <wolfssl/wolfcrypt/async.h>
/* optional macro to add sleep between tests */
#ifndef TEST_SLEEP
/* stub the sleep macro */
#define TEST_SLEEP()
#endif
@@ -582,11 +611,6 @@ static const char* bench_desc_words[][9] = {
#pragma warning(disable: 4996)
#endif
#ifdef USE_FLAT_BENCHMARK_H
#include "benchmark.h"
#else
#include "wolfcrypt/benchmark/benchmark.h"
#endif
#ifdef WOLFSSL_CURRTIME_REMAP
#define current_time WOLFSSL_CURRTIME_REMAP
@@ -1052,6 +1076,8 @@ static void bench_stats_sym_finish(const char* desc, int doAsync, int count,
(void)doAsync;
(void)ret;
TEST_SLEEP();
}
#ifdef BENCH_ASYM
@@ -1095,6 +1121,8 @@ static void bench_stats_asym_finish(const char* algo, int strength,
(void)doAsync;
(void)ret;
TEST_SLEEP();
}
#endif
#endif /* BENCH_ASYM */
@@ -4067,7 +4095,7 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING],
#endif
#if !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
!defined(WOLFSSL_RSA_PUBLIC_ONLY)
#if !defined(WOLFSSL_MDK5_COMPLv5)
#if !defined(WOLFSSL_MDK5_COMPLv5)
/* MDK5 compiler regard this as a executable statement, and does not allow declarations after the line. */
DECLARE_ARRAY_DYNAMIC_DEC(out, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
#else
@@ -4083,7 +4111,7 @@ static void bench_rsa_helper(int doAsync, RsaKey rsaKey[BENCH_MAX_PENDING],
!defined(WOLFSSL_RSA_PUBLIC_ONLY)
DECLARE_ARRAY_DYNAMIC_EXE(out, byte, BENCH_MAX_PENDING, rsaKeySz, HEAP_HINT);
#endif
if (!rsa_sign_verify) {
#ifndef WOLFSSL_RSA_VERIFY_ONLY
/* begin public RSA */

View File

@@ -75,6 +75,9 @@
#endif
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#ifdef __cplusplus
extern "C" {
#endif
WOLFSSL_LOCAL int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod,
@@ -83,6 +86,9 @@ WOLFSSL_LOCAL int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif
/* reverse an array, used for radix code */

View File

@@ -26,8 +26,6 @@
#include <wolfssl/wolfcrypt/settings.h>
/* submitted by eof */
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
@@ -221,6 +219,9 @@ void WOLFSSL_TIME(int count)
#elif defined(WOLFSSL_ESPIDF)
#include "esp_types.h"
#include "esp_log.h"
#elif defined(WOLFSSL_TELIT_M2MB)
#include <stdio.h>
#include "m2m_log.h"
#else
#include <stdio.h> /* for default printf stuff */
#endif
@@ -261,6 +262,8 @@ static void wolfssl_log(const int logLevel, const char *const logMessage)
ESP_LOGI("wolfssl", "%s", logMessage);
#elif defined(WOLFSSL_ZEPHYR)
printk("%s\n", logMessage);
#elif defined(WOLFSSL_TELIT_M2MB)
M2M_LOG_INFO("%s\n", logMessage);
#else
fprintf(stderr, "%s\n", logMessage);
#endif

View File

@@ -150,6 +150,7 @@ int wc_RNG_GenerateByte(WC_RNG* rng, byte* b)
#elif defined(WOLFSSL_NUCLEUS)
#elif defined(WOLFSSL_PB)
#elif defined(WOLFSSL_ZEPHYR)
#elif defined(WOLFSSL_TELIT_M2MB)
#else
/* include headers that may be needed to get good seed */
#include <fcntl.h>
@@ -2216,6 +2217,33 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
return ret;
}
#elif defined(WOLFSSL_TELIT_M2MB)
#include "stdlib.h"
static long get_timestamp(void) {
long myTime = 0;
INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
if (fd >= 0) {
M2MB_RTC_TIMEVAL_T timeval;
m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
myTime = timeval.msec;
m2mb_rtc_close(fd);
}
return myTime;
}
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
{
int i;
srand(get_timestamp());
for (i = 0; i < sz; i++ ) {
output[i] = rand() % 256;
if ((i % 8) == 7) {
srand(get_timestamp());
}
}
return 0;
}
#elif defined(NO_DEV_RANDOM)
#error "you need to write an os specific wc_GenerateSeed() here"

View File

@@ -67,6 +67,9 @@
#endif
#if defined(WOLFSSL_HAVE_SP_RSA) || defined(WOLFSSL_HAVE_SP_DH)
#ifdef __cplusplus
extern "C" {
#endif
WOLFSSL_LOCAL int sp_ModExp_1024(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_1536(mp_int* base, mp_int* exp, mp_int* mod,
@@ -75,6 +78,9 @@ WOLFSSL_LOCAL int sp_ModExp_2048(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
WOLFSSL_LOCAL int sp_ModExp_3072(mp_int* base, mp_int* exp, mp_int* mod,
mp_int* res);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif

View File

@@ -329,6 +329,39 @@ int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name)
return 0;
}
}
#elif defined(WOLFSSL_TELIT_M2MB)
ctx->dir = m2mb_fs_opendir((const CHAR*)path);
if (ctx->dir == NULL) {
WOLFSSL_MSG("opendir path verify locations failed");
return BAD_PATH_ERROR;
}
while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
dnameLen = (int)XSTRLEN(ctx->entry->d_name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '/';
/* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
* of earlier check it is known that dnameLen is less than
* MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
WOLFSSL_MSG("stat on name failed");
ret = BAD_PATH_ERROR;
break;
}
else if (ctx->s.st_mode & M2MB_S_IFREG) {
if (name)
*name = ctx->name;
return 0;
}
}
#else
ctx->dir = opendir(path);
if (ctx->dir == NULL) {
@@ -426,6 +459,33 @@ int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name)
return 0;
}
}
#elif defined(WOLFSSL_TELIT_M2MB)
while ((ctx->entry = m2mb_fs_readdir(ctx->dir)) != NULL) {
dnameLen = (int)XSTRLEN(ctx->entry->d_name);
if (pathLen + dnameLen + 2 >= MAX_FILENAME_SZ) {
ret = BAD_PATH_ERROR;
break;
}
XSTRNCPY(ctx->name, path, pathLen + 1);
ctx->name[pathLen] = '/';
/* Use dnameLen + 1 for GCC 8 warnings of truncating d_name. Because
* of earlier check it is known that dnameLen is less than
* MAX_FILENAME_SZ - (pathLen + 2) so dnameLen +1 will fit */
XSTRNCPY(ctx->name + pathLen + 1, ctx->entry->d_name, dnameLen + 1);
if (m2mb_fs_stat(ctx->name, &ctx->s) != 0) {
WOLFSSL_MSG("stat on name failed");
ret = BAD_PATH_ERROR;
break;
}
else if (ctx->s.st_mode & M2MB_S_IFREG) {
if (name)
*name = ctx->name;
return 0;
}
}
#else
while ((ctx->entry = readdir(ctx->dir)) != NULL) {
dnameLen = (int)XSTRLEN(ctx->entry->d_name);
@@ -474,6 +534,11 @@ void wc_ReadDirClose(ReadDirCtx* ctx)
fs_closedir(ctx->dirp);
ctx->dirp = NULL;
}
#elif defined(WOLFSSL_TELIT_M2MB)
if (ctx->dir) {
m2mb_fs_closedir(ctx->dir);
ctx->dir = NULL;
}
#else
if (ctx->dir) {
closedir(ctx->dir);
@@ -513,7 +578,7 @@ int z_fs_close(XFILE file)
return ret;
}
#endif /* !NO_FILESYSTEM && !NO_WOLFSSL_DIR */
#endif /* !NO_FILESYSTEM && !WOLFSSL_ZEPHYR */
wolfSSL_Mutex* wc_InitAndAllocMutex(void)
@@ -1628,6 +1693,78 @@ int wolfSSL_CryptHwMutexUnLock(void) {
return 0;
}
#elif defined(WOLFSSL_TELIT_M2MB)
int wc_InitMutex(wolfSSL_Mutex* m)
{
M2MB_OS_RESULT_E osRes;
M2MB_OS_MTX_ATTR_HANDLE mtxAttrHandle;
UINT32 inheritVal = 1;
osRes = m2mb_os_mtx_setAttrItem(&mtxAttrHandle,
CMDS_ARGS(
M2MB_OS_MTX_SEL_CMD_CREATE_ATTR, NULL,
M2MB_OS_MTX_SEL_CMD_NAME, "wolfMtx",
M2MB_OS_MTX_SEL_CMD_INHERIT, inheritVal
)
);
if (osRes != M2MB_OS_SUCCESS) {
return BAD_MUTEX_E;
}
osRes = m2mb_os_mtx_init(m, &mtxAttrHandle);
if (osRes != M2MB_OS_SUCCESS) {
return BAD_MUTEX_E;
}
return 0;
}
int wc_FreeMutex(wolfSSL_Mutex* m)
{
M2MB_OS_RESULT_E osRes;
if (m == NULL)
return BAD_MUTEX_E;
osRes = m2mb_os_mtx_deinit(*m);
if (osRes != M2MB_OS_SUCCESS) {
return BAD_MUTEX_E;
}
return 0;
}
int wc_LockMutex(wolfSSL_Mutex* m)
{
M2MB_OS_RESULT_E osRes;
if (m == NULL)
return BAD_MUTEX_E;
osRes = m2mb_os_mtx_get(*m, M2MB_OS_WAIT_FOREVER);
if (osRes != M2MB_OS_SUCCESS) {
return BAD_MUTEX_E;
}
return 0;
}
int wc_UnLockMutex(wolfSSL_Mutex* m)
{
M2MB_OS_RESULT_E osRes;
if (m == NULL)
return BAD_MUTEX_E;
osRes = m2mb_os_mtx_put(*m);
if (osRes != M2MB_OS_SUCCESS) {
return BAD_MUTEX_E;
}
return 0;
}
#else
#warning No mutex handling defined
@@ -1910,6 +2047,59 @@ time_t wiced_pseudo_unix_epoch_time(time_t * timer)
return epoch_time;
}
#endif /* WOLFSSL_WICED */
#ifdef WOLFSSL_TELIT_M2MB
time_t m2mb_xtime(time_t * timer)
{
time_t myTime = 0;
INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
if (fd >= 0) {
M2MB_RTC_TIMEVAL_T timeval;
m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
myTime = timeval.sec;
m2mb_rtc_close(fd);
}
return myTime;
}
#ifdef WOLFSSL_TLS13
time_t m2mb_xtime_ms(time_t * timer)
{
time_t myTime = 0;
INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
if (fd >= 0) {
M2MB_RTC_TIMEVAL_T timeval;
m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
myTime = timeval.sec + timeval.msec;
m2mb_rtc_close(fd);
}
return myTime;
}
#endif /* WOLFSSL_TLS13 */
#ifndef NO_CRYPT_BENCHMARK
double m2mb_xtime_bench(int reset)
{
double myTime = 0;
INT32 fd = m2mb_rtc_open("/dev/rtc0", 0);
if (fd >= 0) {
M2MB_RTC_TIMEVAL_T timeval;
m2mb_rtc_ioctl(fd, M2MB_RTC_IOCTL_GET_TIMEVAL, &timeval);
myTime = (double)timeval.sec + ((double)timeval.msec / 1000);
m2mb_rtc_close(fd);
}
return myTime;
}
#endif /* !NO_CRYPT_BENCHMARK */
#endif /* WOLFSSL_TELIT_M2MB */
#endif /* !NO_ASN_TIME */
#ifndef WOLFSSL_LEANPSK

File diff suppressed because it is too large Load Diff

View File

@@ -173,6 +173,8 @@
#ifndef SINGLE_THREADED
#include <kernel.h>
#endif
#elif defined(WOLFSSL_TELIT_M2MB)
/* do nothing */
#else
#ifndef SINGLE_THREADED
#define WOLFSSL_PTHREADS

View File

@@ -257,6 +257,13 @@
const char* file, unsigned int line);
#elif defined(XMALLOC_OVERRIDE)
/* override the XMALLOC, XFREE and XREALLOC macros */
#elif defined(WOLFSSL_TELIT_M2MB)
/* Telit M2MB SDK requires use m2mb_os API's, not std malloc/free */
/* Use of malloc/free will cause CPU reboot */
#define XMALLOC(s, h, t) ((void)h, (void)t, m2mb_os_malloc((s)))
#define XFREE(p, h, t) {void* xp = (p); if((xp)) m2mb_os_free((xp));}
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
#elif defined(NO_WOLFSSL_MEMORY)
/* just use plain C stdlib stuff if desired */
#include <stdlib.h>

View File

@@ -114,6 +114,32 @@
#ifndef SINGLE_THREADED
#include <kernel.h>
#endif
#elif defined(WOLFSSL_TELIT_M2MB)
/* Telit SDK uses C++ compile option (--cpp), which causes link issue
to API's if wrapped in extern "C" */
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "m2mb_types.h"
#include "m2mb_os_types.h"
#include "m2mb_os_api.h"
#include "m2mb_os.h"
#include "m2mb_os_mtx.h"
#ifndef NO_ASN_TIME
#include "m2mb_rtc.h"
#endif
#ifndef NO_FILESYSTEM
#include "m2mb_fs_posix.h"
#endif
#undef kB /* eliminate conflict in asn.h */
#ifdef __cplusplus
extern "C" {
#endif
#else
#ifndef SINGLE_THREADED
#define WOLFSSL_PTHREADS
@@ -195,6 +221,8 @@
typedef NU_SEMAPHORE wolfSSL_Mutex;
#elif defined(WOLFSSL_ZEPHYR)
typedef struct k_mutex wolfSSL_Mutex;
#elif defined(WOLFSSL_TELIT_M2MB)
typedef M2MB_OS_MTX_HANDLE wolfSSL_Mutex;
#else
#error Need a mutex type in multithreaded mode
#endif /* USE_WINDOWS_API */
@@ -352,6 +380,19 @@ WOLFSSL_API int wolfCrypt_Cleanup(void);
#define XBADFILE NULL
#define XFGETS(b,s,f) -2 /* Not ported yet */
#elif defined(WOLFSSL_TELIT_M2MB)
#define XFILE INT32
#define XFOPEN(NAME, MODE) m2mb_fs_open((NAME), 0, (MODE))
#define XFSEEK(F, O, W) m2mb_fs_lseek((F), (O), (W))
#define XFTELL(F) m2mb_fs_lseek((F), 0, M2MB_SEEK_END)
#define XREWIND(F) (void)F
#define XFREAD(BUF, SZ, AMT, F) m2mb_fs_read((F), (BUF), (SZ)*(AMT))
#define XFWRITE(BUF, SZ, AMT, F) m2mb_fs_write((F), (BUF), (SZ)*(AMT))
#define XFCLOSE m2mb_fs_close
#define XSEEK_END M2MB_SEEK_END
#define XBADFILE -1
#define XFGETS(b,s,f) -2 /* Not ported yet */
#elif defined(WOLFSSL_USER_FILESYSTEM)
/* To be defined in user_settings.h */
#else
@@ -400,6 +441,11 @@ WOLFSSL_API int wolfCrypt_Cleanup(void);
struct fs_dir_t dir;
struct fs_dirent s;
struct fs_dir_t* dirp;
#elif defined(WOLFSSL_TELIT_M2MB)
M2MB_DIR_T* dir;
struct M2MB_DIRENT* entry;
struct M2MB_STAT s;
#else
struct dirent* entry;
DIR* dir;
@@ -546,6 +592,22 @@ WOLFSSL_API int wolfCrypt_Cleanup(void);
#define USE_WOLF_TM
#elif defined(WOLFSSL_TELIT_M2MB)
typedef long time_t;
extern time_t m2mb_xtime(time_t * timer);
#define XTIME(tl) m2mb_xtime((tl))
#ifdef WOLFSSL_TLS13
extern time_t m2mb_xtime_ms(time_t * timer);
#define XTIME_MS(tl) m2mb_xtime_ms((tl))
#endif
#ifndef NO_CRYPT_BENCHMARK
extern double m2mb_xtime_bench(int reset);
#define WOLFSSL_CURRTIME_REMAP m2mb_xtime_bench
#endif
#define XGMTIME(c, t) gmtime((c))
#define WOLFSSL_GMTIME
#define USE_WOLF_TM
#else
/* default */
/* uses complete <time.h> facility */