Skip to content

Commit 7ed5e0b

Browse files
committed
zephyr no malloc
- cert gen - csr gen - pkcs12 - Compiles for Zephyr 3.4.0 and 2.7.4 - Add support for CONFIG_POSIX_API
1 parent 4e6a345 commit 7ed5e0b

18 files changed

Lines changed: 401 additions & 51 deletions

File tree

wolfcrypt/src/memory.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,10 +991,17 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
991991
break;
992992
}
993993
#ifdef WOLFSSL_DEBUG_STATIC_MEMORY
994+
#ifdef WOLFSSL_ZEPHYR
995+
else {
996+
fprintf(stderr, "Size: %zu, Empty: %d\n", size,
997+
mem->sizeList[i]);
998+
}
999+
#else
9941000
else {
9951001
fprintf(stderr, "Size: %ld, Empty: %d\n", size,
9961002
mem->sizeList[i]);
9971003
}
1004+
#endif
9981005
#endif
9991006
}
10001007
}
@@ -1029,7 +1036,13 @@ void* wolfSSL_Malloc(size_t size, void* heap, int type)
10291036
else {
10301037
WOLFSSL_MSG("ERROR ran out of static memory");
10311038
#ifdef WOLFSSL_DEBUG_MEMORY
1032-
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", size, func, line);
1039+
#ifdef WOLFSSL_ZEPHYR
1040+
fprintf(stderr, "Looking for %zu bytes at %s:%d\n", size, func,
1041+
line);
1042+
#else
1043+
fprintf(stderr, "Looking for %lu bytes at %s:%d\n", size, func,
1044+
line);
1045+
#endif
10331046
#endif
10341047
}
10351048

wolfcrypt/src/pkcs12.c

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,22 @@ typedef struct WC_PKCS12_ATTRIBUTE {
130130

131131

132132
WC_PKCS12* wc_PKCS12_new(void)
133+
{
134+
return wc_PKCS12_new_ex(NULL);
135+
}
136+
137+
138+
WC_PKCS12* wc_PKCS12_new_ex(void* heap)
133139
{
134140
WC_PKCS12* pkcs12 = (WC_PKCS12*)XMALLOC(sizeof(WC_PKCS12),
135-
NULL, DYNAMIC_TYPE_PKCS);
141+
heap, DYNAMIC_TYPE_PKCS);
136142
if (pkcs12 == NULL) {
137143
WOLFSSL_MSG("Memory issue when creating WC_PKCS12 struct");
138144
return NULL;
139145
}
140146

141147
XMEMSET(pkcs12, 0, sizeof(WC_PKCS12));
148+
pkcs12->heap = heap;
142149

143150
return pkcs12;
144151
}
@@ -202,7 +209,7 @@ void wc_PKCS12_free(WC_PKCS12* pkcs12)
202209
}
203210
#endif
204211

205-
XFREE(pkcs12, NULL, DYNAMIC_TYPE_PKCS);
212+
XFREE(pkcs12, heap, DYNAMIC_TYPE_PKCS);
206213
}
207214

208215

@@ -2604,20 +2611,12 @@ WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz, char* name,
26042611
return NULL;
26052612
}
26062613

2607-
if ((pkcs12 = wc_PKCS12_new()) == NULL) {
2614+
if ((pkcs12 = wc_PKCS12_new_ex(heap)) == NULL) {
26082615
wc_FreeRng(&rng);
26092616
WOLFSSL_LEAVE("wc_PKCS12_create", MEMORY_E);
26102617
return NULL;
26112618
}
26122619

2613-
if ((ret = wc_PKCS12_SetHeap(pkcs12, heap)) != 0) {
2614-
wc_PKCS12_free(pkcs12);
2615-
wc_FreeRng(&rng);
2616-
WOLFSSL_LEAVE("wc_PKCS12_create", ret);
2617-
(void)ret;
2618-
return NULL;
2619-
}
2620-
26212620
if (iter <= 0) {
26222621
iter = WC_PKCS12_ITT_DEFAULT;
26232622
}

wolfcrypt/src/random.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3731,25 +3731,33 @@ int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
37313731

37323732
#elif defined(WOLFSSL_ZEPHYR)
37333733

3734-
#include <version.h>
3734+
#include <version.h>
37353735

37363736
#if KERNEL_VERSION_NUMBER >= 0x30500
37373737
#include <zephyr/random/random.h>
37383738
#else
3739-
#include <zephyr/random/rand32.h>
3739+
#if KERNEL_VERSION_NUMBER >= 0x30100
3740+
#include <zephyr/random/rand32.h>
3741+
#else
3742+
#include <random/rand32.h>
3743+
#endif
37403744
#endif
37413745

37423746
#ifndef _POSIX_C_SOURCE
3743-
#include <zephyr/posix/time.h>
3747+
#if KERNEL_VERSION_NUMBER >= 0x30100
3748+
#include <zephyr/posix/time.h>
3749+
#else
3750+
#include <posix/time.h>
3751+
#endif
37443752
#else
37453753
#include <time.h>
37463754
#endif
37473755

3748-
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3749-
{
3750-
sys_rand_get(output, sz);
3751-
return 0;
3752-
}
3756+
int wc_GenerateSeed(OS_Seed* os, byte* output, word32 sz)
3757+
{
3758+
sys_rand_get(output, sz);
3759+
return 0;
3760+
}
37533761

37543762
#elif defined(WOLFSSL_TELIT_M2MB)
37553763

wolfcrypt/src/wc_port.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,11 +3668,13 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
36683668

36693669
#elif defined(WOLFSSL_ZEPHYR)
36703670

3671+
void* wolfsslThreadHeapHint = NULL;
3672+
36713673
int wolfSSL_NewThread(THREAD_TYPE* thread,
36723674
THREAD_CB cb, void* arg)
36733675
{
36743676
#ifndef WOLFSSL_ZEPHYR_STACK_SZ
3675-
#define WOLFSSL_ZEPHYR_STACK_SZ (24*1024)
3677+
#define WOLFSSL_ZEPHYR_STACK_SZ (48*1024)
36763678
#endif
36773679

36783680
if (thread == NULL || cb == NULL)
@@ -3685,11 +3687,14 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
36853687
* thread->threadStack = k_thread_stack_alloc(WOLFSSL_ZEPHYR_STACK_SZ,
36863688
* 0);
36873689
*/
3690+
printf("thread stack size is %ld\n", Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ));
36883691
thread->threadStack = (void*)XMALLOC(
3689-
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ), 0,
3690-
DYNAMIC_TYPE_TMP_BUFFER);
3691-
if (thread->threadStack == NULL)
3692+
Z_KERNEL_STACK_SIZE_ADJUST(WOLFSSL_ZEPHYR_STACK_SZ),
3693+
wolfsslThreadHeapHint, DYNAMIC_TYPE_TMP_BUFFER);
3694+
if (thread->threadStack == NULL) {
3695+
WOLFSSL_MSG("error: XMALLOC failed");
36923696
return MEMORY_E;
3697+
}
36933698

36943699
/* k_thread_create does not return any error codes */
36953700
/* Casting to k_thread_entry_t should be fine since we just ignore the
@@ -3716,7 +3721,8 @@ char* mystrnstr(const char* s1, const char* s2, unsigned int n)
37163721
* if (err != 0)
37173722
* ret = MEMORY_E;
37183723
*/
3719-
XFREE(thread.threadStack, NULL, DYNAMIC_TYPE_TMP_BUFFER);
3724+
XFREE(thread.threadStack, wolfsslThreadHeapHint,
3725+
DYNAMIC_TYPE_TMP_BUFFER);
37203726
thread.threadStack = NULL;
37213727

37223728
/* No thread resources to free. Everything is stored in thread.tid */

wolfcrypt/test/test.c

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@ const byte const_byte_array[] = "A+Gd\0\0\0";
354354
#ifdef HAVE_PKCS7
355355
#include <wolfssl/wolfcrypt/pkcs7.h>
356356
#endif
357+
#ifdef HAVE_PKCS12
358+
#include <wolfssl/wolfcrypt/pkcs12.h>
359+
#endif
357360
#ifdef HAVE_FIPS
358361
#include <wolfssl/wolfcrypt/fips_test.h>
359362
#endif
@@ -584,6 +587,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t srp_test(void);
584587
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t random_test(void);
585588
#endif /* WC_NO_RNG */
586589
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void);
590+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
587591
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t ripemd_test(void);
588592
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
589593
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_test(void); /* test mini api */
@@ -595,7 +599,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t openssl_evpSig_test(void);
595599
#endif
596600

597601
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf1_test(void);
598-
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void);
602+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void);
599603
#if defined(HAVE_PBKDF2) && !defined(NO_SHA256) && !defined(NO_HMAC)
600604
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pbkdf2_test(void);
601605
#endif
@@ -1672,6 +1676,13 @@ options: [-s max_relative_stack_bytes] [-m max_relative_heap_memory_bytes]\n\
16721676
PRIVATE_KEY_LOCK();
16731677
#endif
16741678

1679+
#if defined(HAVE_PKCS12) && defined(USE_CERT_BUFFERS_2048)
1680+
if ( (ret = pkcs12_test()) != 0)
1681+
TEST_FAIL("PKCS12 test failed!\n", ret);
1682+
else
1683+
TEST_PASS("PKCS12 test passed!\n");
1684+
#endif
1685+
16751686
#if defined(OPENSSL_EXTRA) && !defined(WOLFCRYPT_ONLY)
16761687
if ( (ret = openssl_test()) != 0)
16771688
TEST_FAIL("OPENSSL test failed!\n", ret);
@@ -24707,7 +24718,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t scrypt_test(void)
2470724718
#endif
2470824719

2470924720
#ifdef HAVE_PKCS12
24710-
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
24721+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_pbkdf_test(void)
2471124722
{
2471224723
WOLFSSL_SMALL_STACK_STATIC const byte passwd[] = { 0x00, 0x73, 0x00, 0x6d, 0x00, 0x65, 0x00, 0x67,
2471324724
0x00, 0x00 };
@@ -24734,7 +24745,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
2473424745
int kLen = 24;
2473524746
int iterations = 1;
2473624747
wc_test_ret_t ret;
24737-
WOLFSSL_ENTER("pkcs12_test");
24748+
WOLFSSL_ENTER("pkcs12_pbkdf_test");
2473824749

2473924750
ret = wc_PKCS12_PBKDF(derived, passwd, sizeof(passwd), salt, 8,
2474024751
iterations, kLen, WC_SHA256, id);
@@ -24839,7 +24850,7 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
2483924850
return ret;
2484024851
#endif
2484124852
#ifdef HAVE_PKCS12
24842-
ret = pkcs12_test();
24853+
ret = pkcs12_pbkdf_test();
2484324854
if (ret != 0)
2484424855
return ret;
2484524856
#endif
@@ -24853,6 +24864,76 @@ WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pwdbased_test(void)
2485324864

2485424865
#endif /* NO_PWDBASED */
2485524866

24867+
#if defined(HAVE_PKCS12) && defined(USE_CERT_BUFFERS_2048)
24868+
WOLFSSL_TEST_SUBROUTINE wc_test_ret_t pkcs12_test(void)
24869+
{
24870+
wc_test_ret_t ret = 0;
24871+
WC_PKCS12* pkcs12 = NULL;
24872+
/* Gen vars */
24873+
byte* pkcs12der = NULL;
24874+
int pkcs12derSz = 0;
24875+
WC_DerCertList derCaList = {
24876+
(byte*)ca_cert_der_2048, sizeof_ca_cert_der_2048, NULL
24877+
};
24878+
char* pass = (char*)"wolfSSL test";
24879+
/* Parsing vars */
24880+
WC_DerCertList* derCaListOut = NULL;
24881+
byte* keyDer = NULL;
24882+
byte* certDer = NULL;
24883+
word32 keySz;
24884+
word32 certSz;
24885+
24886+
WOLFSSL_ENTER("pkcs12_test");
24887+
24888+
pkcs12 = wc_PKCS12_create(pass, XSTRLEN(pass),
24889+
(char*)"friendlyName" /* not used currently */,
24890+
(byte*)server_key_der_2048, sizeof_server_key_der_2048,
24891+
(byte*)server_cert_der_2048, sizeof_server_cert_der_2048,
24892+
&derCaList, PBE_SHA1_DES3, PBE_SHA1_DES3, 100, 100,
24893+
0 /* not used currently */, HEAP_HINT);
24894+
if (pkcs12 == NULL)
24895+
return MEMORY_E;
24896+
24897+
ret = wc_i2d_PKCS12(pkcs12, NULL, &pkcs12derSz);
24898+
if (ret != LENGTH_ONLY_E)
24899+
return ret == 0 ? -1 : ret;
24900+
24901+
pkcs12der = (byte*)XMALLOC(pkcs12derSz, HEAP_HINT, DYNAMIC_TYPE_PKCS);
24902+
if (pkcs12der == NULL)
24903+
return MEMORY_E;
24904+
24905+
{
24906+
/* Use tmp pointer to avoid advancing pkcs12der */
24907+
byte* tmp = pkcs12der;
24908+
ret = wc_i2d_PKCS12(pkcs12, &tmp, &pkcs12derSz);
24909+
if (ret <= 0)
24910+
return ret == 0 ? -1 : ret;
24911+
}
24912+
24913+
wc_PKCS12_free(pkcs12);
24914+
pkcs12 = wc_PKCS12_new_ex(HEAP_HINT);
24915+
if (pkcs12 == NULL)
24916+
return MEMORY_E;
24917+
24918+
/* convert the DER file into an internal structure */
24919+
ret = wc_d2i_PKCS12(pkcs12der, pkcs12derSz, pkcs12);
24920+
if (ret != 0)
24921+
return ret;
24922+
24923+
/* parse the internal structure into its parts */
24924+
ret = wc_PKCS12_parse(pkcs12, "wolfSSL test", &keyDer, &keySz,
24925+
&certDer, &certSz, &derCaListOut);
24926+
if (ret != 0 || keyDer == NULL || certDer == NULL || derCaListOut == NULL)
24927+
return ret == 0 ? -1 : ret;
24928+
24929+
wc_FreeCertList(derCaListOut, HEAP_HINT);
24930+
XFREE(keyDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
24931+
XFREE(certDer, HEAP_HINT, DYNAMIC_TYPE_PKCS);
24932+
wc_PKCS12_free(pkcs12);
24933+
return ret;
24934+
}
24935+
#endif
24936+
2485624937
#if defined(HAVE_HKDF) && !defined(NO_HMAC)
2485724938

2485824939
#if defined(WOLFSSL_AFALG_XILINX) || defined(WOLFSSL_AFALG_XILINX_AES) || \
@@ -50257,7 +50338,7 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
5025750338
return BAD_FUNC_ARG;
5025850339

5025950340
#ifdef DEBUG_WOLFSSL
50260-
printf("CryptoDevCb: Algo Type %d\n", info->algo_type);
50341+
WOLFSSL_MSG_EX("CryptoDevCb: Algo Type %d\n", info->algo_type);
5026150342
#endif
5026250343

5026350344
if (info->algo_type == WC_ALGO_TYPE_RNG) {
@@ -50299,7 +50380,7 @@ static int myCryptoDevCb(int devIdArg, wc_CryptoInfo* info, void* ctx)
5029950380
}
5030050381
else if (info->algo_type == WC_ALGO_TYPE_PK) {
5030150382
#ifdef DEBUG_WOLFSSL
50302-
printf("CryptoDevCb: Pk Type %d\n", info->pk.type);
50383+
WOLFSSL_MSG_EX("CryptoDevCb: Pk Type %d\n", info->pk.type);
5030350384
#endif
5030450385

5030550386
#ifndef NO_RSA

wolfssl/internal.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,12 @@
206206
#endif
207207
#elif defined(WOLFSSL_ZEPHYR)
208208
#ifndef SINGLE_THREADED
209-
#include <zephyr/kernel.h>
209+
#include <version.h>
210+
#if KERNEL_VERSION_NUMBER >= 0x30100
211+
#include <zephyr/kernel.h>
212+
#else
213+
#include <kernel.h>
214+
#endif
210215
#endif
211216
#elif defined(WOLFSSL_TELIT_M2MB)
212217
/* do nothing */

wolfssl/test.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,26 @@
143143
#include <pthread.h>
144144
#define SOCKET_T int
145145
#elif defined(WOLFSSL_ZEPHYR)
146+
#include <version.h>
146147
#include <string.h>
147148
#include <sys/types.h>
148-
#include <zephyr/net/socket.h>
149+
#if KERNEL_VERSION_NUMBER >= 0x30100
150+
#include <zephyr/net/socket.h>
151+
#ifdef CONFIG_POSIX_API
152+
#include <zephyr/posix/poll.h>
153+
#include <zephyr/posix/netdb.h>
154+
#include <zephyr/posix/sys/socket.h>
155+
#include <zephyr/posix/sys/select.h>
156+
#endif
157+
#else
158+
#include <net/socket.h>
159+
#ifdef CONFIG_POSIX_API
160+
#include <posix/poll.h>
161+
#include <posix/netdb.h>
162+
#include <posix/sys/socket.h>
163+
#include <posix/sys/select.h>
164+
#endif
165+
#endif
149166
#define SOCKET_T int
150167
#define SOL_SOCKET 1
151168
#define WOLFSSL_USE_GETADDRINFO

wolfssl/wolfcrypt/pkcs12.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ enum {
4747
};
4848

4949
WOLFSSL_API WC_PKCS12* wc_PKCS12_new(void);
50+
WOLFSSL_API WC_PKCS12* wc_PKCS12_new_ex(void* heap);
5051
WOLFSSL_API void wc_PKCS12_free(WC_PKCS12* pkcs12);
5152
WOLFSSL_API int wc_d2i_PKCS12(const byte* der, word32 derSz, WC_PKCS12* pkcs12);
5253
#ifndef NO_FILESYSTEM
@@ -67,7 +68,7 @@ WOLFSSL_API WC_PKCS12* wc_PKCS12_create(char* pass, word32 passSz,
6768
WOLFSSL_LOCAL int wc_PKCS12_SetHeap(WC_PKCS12* pkcs12, void* heap);
6869
WOLFSSL_LOCAL void* wc_PKCS12_GetHeap(WC_PKCS12* pkcs12);
6970

70-
WOLFSSL_LOCAL void wc_FreeCertList(WC_DerCertList* list, void* heap);
71+
WOLFSSL_API void wc_FreeCertList(WC_DerCertList* list, void* heap);
7172

7273
#ifdef __cplusplus
7374
} /* extern "C" */

0 commit comments

Comments
 (0)