1 /* wc_port.h
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 /*!
23     \file wolfssl/wolfcrypt/wc_port.h
24 */
25 
26 #ifndef WOLF_CRYPT_PORT_H
27 #define WOLF_CRYPT_PORT_H
28 
29 #include <wolfssl/wolfcrypt/settings.h>
30 #include <wolfssl/wolfcrypt/visibility.h>
31 
32 #ifdef __cplusplus
33     extern "C" {
34 #endif
35 
36 /* Detect if compiler supports C99. "NO_WOLF_C99" can be defined in
37  * user_settings.h to disable checking for C99 support. */
38 #if !defined(WOLF_C99) && defined(__STDC_VERSION__) && \
39     !defined(WOLFSSL_ARDUINO) && !defined(NO_WOLF_C99)
40     #if __STDC_VERSION__ >= 199901L
41         #define WOLF_C99
42     #endif
43 #endif
44 
45 
46 /* GENERIC INCLUDE SECTION */
47 #if defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
48     #include <mqx.h>
49     #if (defined(MQX_USE_IO_OLD) && MQX_USE_IO_OLD) || \
50         defined(FREESCALE_MQX_5_0)
51         #include <fio.h>
52     #else
53         #include <nio.h>
54     #endif
55 #endif
56 
57 #ifdef WOLFSSL_LINUXKM
58     #ifdef HAVE_CONFIG_H
59         #ifndef PACKAGE_NAME
60             #error wc_port.h included before config.h
61         #endif
62         /* config.h is autogenerated without gating, and is subject to repeat
63          * inclusions, so gate it out here to keep autodetection masking
64          * intact:
65          */
66         #undef HAVE_CONFIG_H
67     #endif
68 
69     #ifdef BUILDING_WOLFSSL
70 
71     #if defined(CONFIG_MIPS) && defined(HAVE_LINUXKM_PIE_SUPPORT)
72         /* __ZBOOT__ disables some unhelpful macros around the mem*() funcs in
73          * legacy arch/mips/include/asm/string.h
74          */
75         #define __ZBOOT__
76         #define memcmp __builtin_memcmp
77         #define __ARCH_MEMCMP_NO_REDIRECT
78         #define __ARCH_MEMCPY_NO_REDIRECT
79         #define __builtin_memcpy memcpy
80         extern void *memcpy(void *dest, const void *src, unsigned int n);
81         #define __ARCH_MEMCPY_NO_REDIRECT
82         #define __builtin_memset memset
83         extern void *memset(void *dest, int c, unsigned int n);
84     #endif
85 
86     _Pragma("GCC diagnostic push");
87 
88     /* we include all the needed kernel headers with these masked out. else
89      * there are profuse warnings.
90      */
91     _Pragma("GCC diagnostic ignored \"-Wunused-parameter\"");
92     _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"");
93     _Pragma("GCC diagnostic ignored \"-Wshadow\"");
94     _Pragma("GCC diagnostic ignored \"-Wnested-externs\"");
95     _Pragma("GCC diagnostic ignored \"-Wredundant-decls\"");
96     _Pragma("GCC diagnostic ignored \"-Wsign-compare\"");
97     _Pragma("GCC diagnostic ignored \"-Wpointer-sign\"");
98     _Pragma("GCC diagnostic ignored \"-Wbad-function-cast\"");
99     _Pragma("GCC diagnostic ignored \"-Wdiscarded-qualifiers\"");
100     _Pragma("GCC diagnostic ignored \"-Wtype-limits\"");
101     _Pragma("GCC diagnostic ignored \"-Wswitch-enum\"");
102 
103     /* suppress inclusion of stdint-gcc.h to avoid conflicts with Linux native include/linux/types.h: */
104     #define _GCC_STDINT_H
105 
106     #include <linux/kconfig.h>
107     #include <linux/kernel.h>
108     #include <linux/version.h>
109     #include <linux/ctype.h>
110     #include <linux/init.h>
111     #include <linux/module.h>
112     #ifdef __PIE__
113         /* without this, mm.h brings in static, but not inline, pmd_to_page(),
114          * with direct references to global vmem variables.
115          */
116         #undef USE_SPLIT_PMD_PTLOCKS
117         #define USE_SPLIT_PMD_PTLOCKS 0
118     #endif
119     #include <linux/mm.h>
120     #ifndef SINGLE_THREADED
121         #include <linux/kthread.h>
122     #endif
123     #include <linux/net.h>
124     #include <linux/slab.h>
125     #if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || defined(WOLFSSL_SP_X86_64_ASM)
126         #ifndef CONFIG_X86
127             #error X86 SIMD extensions requested, but CONFIG_X86 is not set.
128         #endif
129         #define WOLFSSL_LINUXKM_SIMD
130         #define WOLFSSL_LINUXKM_SIMD_X86
131         #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
132             #include <asm/i387.h>
133         #else
134             #include <asm/simd.h>
135         #endif
136         #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
137             #include <asm/fpu/internal.h>
138         #endif
139         #ifndef SAVE_VECTOR_REGISTERS
140             #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_x86(); if (_svr_ret != 0) { fail_clause } }
141         #endif
142         #ifndef RESTORE_VECTOR_REGISTERS
143             #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_x86()
144         #endif
145     #elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_SP_ARM32_ASM) || \
146           defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM_THUMB_ASM) ||\
147           defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)
148         #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
149             #error ARM SIMD extensions requested, but CONFIG_ARM* is not set.
150         #endif
151         #define WOLFSSL_LINUXKM_SIMD
152         #define WOLFSSL_LINUXKM_SIMD_ARM
153         #include <asm/fpsimd.h>
154         #ifndef SAVE_VECTOR_REGISTERS
155             #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_arm(); if (_svr_ret != 0) { fail_clause } }
156         #endif
157         #ifndef RESTORE_VECTOR_REGISTERS
158             #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_arm()
159         #endif
160     #else
161         #ifndef WOLFSSL_NO_ASM
162             #define WOLFSSL_NO_ASM
163         #endif
164     #endif
165 
166     _Pragma("GCC diagnostic pop");
167 
168     /* the kernel uses -std=c89, but not -pedantic, and makes full use of anon
169      * structs/unions, so we should too.
170      */
171     #define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
172 
173     #define NO_THREAD_LS
174     #define NO_ATTRIBUTE_CONSTRUCTOR
175 
176     /* kvmalloc()/kvfree() and friends added in linux commit a7c3e901 */
177     #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
178         #define HAVE_KVMALLOC
179     #endif
180 
181     #ifdef HAVE_FIPS
182         extern int wolfCrypt_FIPS_first(void);
183         extern int wolfCrypt_FIPS_last(void);
184     #endif
185 
186     #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
187         /* work around backward dependency of asn.c on ssl.c. */
188         struct Signer;
189         struct Signer *GetCA(void *signers, unsigned char *hash);
190         #ifndef NO_SKID
191             struct Signer *GetCAByName(void* signers, unsigned char *hash);
192         #endif
193     #endif
194 
195     #if defined(__PIE__) && !defined(USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE)
196         #error "compiling -fPIE without PIE support."
197     #endif
198 
199     #if defined(HAVE_FIPS) && !defined(HAVE_LINUXKM_PIE_SUPPORT)
200         #error "FIPS build requires PIE support."
201     #endif
202 
203     #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
204 
205 #ifdef CONFIG_MIPS
206     #undef __ARCH_MEMCMP_NO_REDIRECT
207     #undef memcmp
208     extern int memcmp(const void *s1, const void *s2, size_t n);
209 #endif
210 
211     struct wolfssl_linuxkm_pie_redirect_table {
212     #ifndef __ARCH_MEMCMP_NO_REDIRECT
213         typeof(memcmp) *memcmp;
214     #endif
215     #ifndef __ARCH_MEMCPY_NO_REDIRECT
216         typeof(memcpy) *memcpy;
217     #endif
218     #ifndef __ARCH_MEMSET_NO_REDIRECT
219         typeof(memset) *memset;
220     #endif
221     #ifndef __ARCH_MEMMOVE_NO_REDIRECT
222         typeof(memmove) *memmove;
223     #endif
224     #ifndef __ARCH_STRNCMP_NO_REDIRECT
225         typeof(strncmp) *strncmp;
226     #endif
227     #ifndef __ARCH_STRLEN_NO_REDIRECT
228         typeof(strlen) *strlen;
229     #endif
230     #ifndef __ARCH_STRSTR_NO_REDIRECT
231         typeof(strstr) *strstr;
232     #endif
233     #ifndef __ARCH_STRNCPY_NO_REDIRECT
234         typeof(strncpy) *strncpy;
235     #endif
236     #ifndef __ARCH_STRNCAT_NO_REDIRECT
237         typeof(strncat) *strncat;
238     #endif
239     #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
240         typeof(strncasecmp) *strncasecmp;
241     #endif
242         typeof(kstrtoll) *kstrtoll;
243 
244         #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
245             typeof(_printk) *_printk;
246         #else
247             typeof(printk) *printk;
248         #endif
249         typeof(snprintf) *snprintf;
250 
251         const unsigned char *_ctype;
252 
253         typeof(kmalloc) *kmalloc;
254         typeof(kfree) *kfree;
255         typeof(ksize) *ksize;
256         typeof(krealloc) *krealloc;
257         #ifdef HAVE_KVMALLOC
258         typeof(kvmalloc_node) *kvmalloc_node;
259         typeof(kvfree) *kvfree;
260         #endif
261         typeof(is_vmalloc_addr) *is_vmalloc_addr;
262         typeof(kmem_cache_alloc_trace) *kmem_cache_alloc_trace;
263         typeof(kmalloc_order_trace) *kmalloc_order_trace;
264 
265         typeof(get_random_bytes) *get_random_bytes;
266         #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
267             typeof(getnstimeofday) *getnstimeofday;
268         #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
269             typeof(current_kernel_time64) *current_kernel_time64;
270         #else
271             typeof(ktime_get_coarse_real_ts64) *ktime_get_coarse_real_ts64;
272         #endif
273 
274         struct task_struct *(*get_current)(void);
275         int (*preempt_count)(void);
276 
277         #ifdef WOLFSSL_LINUXKM_SIMD_X86
278         typeof(irq_fpu_usable) *irq_fpu_usable;
279         /* kernel_fpu_begin() replaced by kernel_fpu_begin_mask() in commit e4512289,
280          * released in kernel 5.11, backported to 5.4.93
281          */
282         #ifdef kernel_fpu_begin
283             typeof(kernel_fpu_begin_mask) *kernel_fpu_begin_mask;
284         #else
285             typeof(kernel_fpu_begin) *kernel_fpu_begin;
286         #endif
287         typeof(kernel_fpu_end) *kernel_fpu_end;
288 
289         #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0)
290             typeof(copy_fpregs_to_fpstate) *copy_fpregs_to_fpstate;
291             typeof(copy_kernel_to_fpregs) *copy_kernel_to_fpregs;
292         #else
293             typeof(save_fpregs_to_fpstate) *save_fpregs_to_fpstate;
294             typeof(__restore_fpregs_from_fpstate) *__restore_fpregs_from_fpstate;
295             typeof(xfeatures_mask_all) *xfeatures_mask_all;
296         #endif
297         typeof(cpu_number) *cpu_number;
298         typeof(nr_cpu_ids) *nr_cpu_ids;
299 
300         #endif /* WOLFSSL_LINUXKM_SIMD_X86 */
301 
302         typeof(__mutex_init) *__mutex_init;
303         #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
304             typeof(mutex_lock_nested) *mutex_lock_nested;
305         #else
306             typeof(mutex_lock) *mutex_lock;
307         #endif
308         typeof(mutex_unlock) *mutex_unlock;
309         #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
310             typeof(mutex_destroy) *mutex_destroy;
311         #endif
312 
313         #ifdef HAVE_FIPS
314         typeof(wolfCrypt_FIPS_first) *wolfCrypt_FIPS_first;
315         typeof(wolfCrypt_FIPS_last) *wolfCrypt_FIPS_last;
316         #endif
317 
318         #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
319         typeof(GetCA) *GetCA;
320         #ifndef NO_SKID
321         typeof(GetCAByName) *GetCAByName;
322         #endif
323         #endif
324 
325         const void *_last_slot;
326     };
327 
328     extern const struct wolfssl_linuxkm_pie_redirect_table *wolfssl_linuxkm_get_pie_redirect_table(void);
329 
330     #ifdef __PIE__
331 
332     #ifndef __ARCH_MEMCMP_NO_REDIRECT
333         #define memcmp (wolfssl_linuxkm_get_pie_redirect_table()->memcmp)
334     #endif
335     #ifndef __ARCH_MEMCPY_NO_REDIRECT
336         #define memcpy (wolfssl_linuxkm_get_pie_redirect_table()->memcpy)
337     #endif
338     #ifndef __ARCH_MEMSET_NO_REDIRECT
339         #define memset (wolfssl_linuxkm_get_pie_redirect_table()->memset)
340     #endif
341     #ifndef __ARCH_MEMMOVE_NO_REDIRECT
342         #define memmove (wolfssl_linuxkm_get_pie_redirect_table()->memmove)
343     #endif
344     #ifndef __ARCH_STRNCMP_NO_REDIRECT
345         #define strncmp (wolfssl_linuxkm_get_pie_redirect_table()->strncmp)
346     #endif
347     #ifndef __ARCH_STRLEN_NO_REDIRECT
348         #define strlen (wolfssl_linuxkm_get_pie_redirect_table()->strlen)
349     #endif
350     #ifndef __ARCH_STRSTR_NO_REDIRECT
351         #define strstr (wolfssl_linuxkm_get_pie_redirect_table()->strstr)
352     #endif
353     #ifndef __ARCH_STRNCPY_NO_REDIRECT
354         #define strncpy (wolfssl_linuxkm_get_pie_redirect_table()->strncpy)
355     #endif
356     #ifndef __ARCH_STRNCAT_NO_REDIRECT
357         #define strncat (wolfssl_linuxkm_get_pie_redirect_table()->strncat)
358     #endif
359     #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
360         #define strncasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strncasecmp)
361     #endif
362     #define kstrtoll (wolfssl_linuxkm_get_pie_redirect_table()->kstrtoll)
363 
364     #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
365         #define _printk (wolfssl_linuxkm_get_pie_redirect_table()->_printk)
366     #else
367         #define printk (wolfssl_linuxkm_get_pie_redirect_table()->printk)
368     #endif
369     #define snprintf (wolfssl_linuxkm_get_pie_redirect_table()->snprintf)
370 
371     #define _ctype (wolfssl_linuxkm_get_pie_redirect_table()->_ctype)
372 
373     #define kmalloc (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc)
374     #define kfree (wolfssl_linuxkm_get_pie_redirect_table()->kfree)
375     #define ksize (wolfssl_linuxkm_get_pie_redirect_table()->ksize)
376     #define krealloc (wolfssl_linuxkm_get_pie_redirect_table()->krealloc)
377     #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO)
378     #ifdef HAVE_KVMALLOC
379         #define kvmalloc_node (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node)
380         #define kvfree (wolfssl_linuxkm_get_pie_redirect_table()->kvfree)
381     #endif
382     #define is_vmalloc_addr (wolfssl_linuxkm_get_pie_redirect_table()->is_vmalloc_addr)
383     #define kmem_cache_alloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmem_cache_alloc_trace)
384     #define kmalloc_order_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_order_trace)
385 
386     #define get_random_bytes (wolfssl_linuxkm_get_pie_redirect_table()->get_random_bytes)
387     #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
388         #define getnstimeofday (wolfssl_linuxkm_get_pie_redirect_table()->getnstimeofday)
389     #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
390         #define current_kernel_time64 (wolfssl_linuxkm_get_pie_redirect_table()->current_kernel_time64)
391     #else
392         #define ktime_get_coarse_real_ts64 (wolfssl_linuxkm_get_pie_redirect_table()->ktime_get_coarse_real_ts64)
393     #endif
394 
395     #undef get_current
396     #define get_current (wolfssl_linuxkm_get_pie_redirect_table()->get_current)
397     #undef preempt_count
398     #define preempt_count (wolfssl_linuxkm_get_pie_redirect_table()->preempt_count)
399 
400     #ifdef WOLFSSL_LINUXKM_SIMD_X86
401         #define irq_fpu_usable (wolfssl_linuxkm_get_pie_redirect_table()->irq_fpu_usable)
402         #ifdef kernel_fpu_begin
403             #define kernel_fpu_begin_mask (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin_mask)
404         #else
405             #define kernel_fpu_begin (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_begin)
406         #endif
407         #define kernel_fpu_end (wolfssl_linuxkm_get_pie_redirect_table()->kernel_fpu_end)
408         #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 14, 0)
409             #define copy_fpregs_to_fpstate (wolfssl_linuxkm_get_pie_redirect_table()->copy_fpregs_to_fpstate)
410             #define copy_kernel_to_fpregs (wolfssl_linuxkm_get_pie_redirect_table()->copy_kernel_to_fpregs)
411         #else
412             #define save_fpregs_to_fpstate (wolfssl_linuxkm_get_pie_redirect_table()->save_fpregs_to_fpstate)
413             #define __restore_fpregs_from_fpstate (wolfssl_linuxkm_get_pie_redirect_table()->__restore_fpregs_from_fpstate)
414             #define xfeatures_mask_all (*(wolfssl_linuxkm_get_pie_redirect_table()->xfeatures_mask_all))
415         #endif
416         #define cpu_number (*(wolfssl_linuxkm_get_pie_redirect_table()->cpu_number))
417         #define nr_cpu_ids (*(wolfssl_linuxkm_get_pie_redirect_table()->nr_cpu_ids))
418     #endif
419 
420     #define __mutex_init (wolfssl_linuxkm_get_pie_redirect_table()->__mutex_init)
421     #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
422         #define mutex_lock_nested (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock_nested)
423     #else
424         #define mutex_lock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock)
425     #endif
426     #define mutex_unlock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_unlock)
427     #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
428         #define mutex_destroy (wolfssl_linuxkm_get_pie_redirect_table()->mutex_destroy)
429     #endif
430 
431     /* per linux/ctype.h, tolower() and toupper() are macros bound to static inlines
432      * that use macros that bring in the _ctype global.  for __PIE__, this needs to
433      * be masked out.
434      */
435     #undef tolower
436     #undef toupper
437     #define tolower(c) (islower(c) ? (c) : ((c) + ('a'-'A')))
438     #define toupper(c) (isupper(c) ? (c) : ((c) - ('a'-'A')))
439 
440     #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
441         #define GetCA (wolfssl_linuxkm_get_pie_redirect_table()->GetCA)
442         #ifndef NO_SKID
443             #define GetCAByName (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByName)
444         #endif
445     #endif
446 
447     #endif /* __PIE__ */
448 
449     #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
450 
451 #ifdef WOLFSSL_LINUXKM_SIMD
452 
453 #ifdef WOLFSSL_LINUXKM_SIMD_X86
454 
455     extern __must_check int allocate_wolfcrypt_linuxkm_fpu_states(void);
456     extern void free_wolfcrypt_linuxkm_fpu_states(void);
457     extern __must_check int save_vector_registers_x86(void);
458     extern void restore_vector_registers_x86(void);
459 
460 #elif defined(CONFIG_ARM) || defined(CONFIG_ARM64)
461 
462     #error kernel module ARM SIMD is not yet tested or usable.
463 
save_vector_registers_arm(void)464     static WARN_UNUSED_RESULT inline int save_vector_registers_arm(void)
465     {
466         preempt_disable();
467         if (! may_use_simd()) {
468             preempt_enable();
469             return BAD_STATE_E;
470         } else {
471             fpsimd_preserve_current_state();
472             return 0;
473         }
474     }
restore_vector_registers_arm(void)475     static inline void restore_vector_registers_arm(void)
476     {
477         fpsimd_restore_current_state();
478         preempt_enable();
479     }
480 
481 #endif
482 
483 #endif /* WOLFSSL_LINUXKM_SIMD */
484 
485     /* Linux headers define these using C expressions, but we need
486      * them to be evaluable by the preprocessor, for use in sp_int.h.
487      */
488     #if BITS_PER_LONG == 64
489         _Static_assert(sizeof(ULONG_MAX) == 8, "BITS_PER_LONG is 64, but ULONG_MAX is not.");
490 
491         #undef UCHAR_MAX
492         #define UCHAR_MAX 255
493         #undef USHRT_MAX
494         #define USHRT_MAX 65535
495         #undef UINT_MAX
496         #define UINT_MAX 4294967295U
497         #undef ULONG_MAX
498         #define ULONG_MAX 18446744073709551615UL
499         #undef ULLONG_MAX
500         #define ULLONG_MAX ULONG_MAX
501         #undef INT_MAX
502         #define INT_MAX 2147483647
503         #undef LONG_MAX
504         #define LONG_MAX 9223372036854775807L
505         #undef LLONG_MAX
506         #define LLONG_MAX LONG_MAX
507 
508     #elif BITS_PER_LONG == 32
509 
510         _Static_assert(sizeof(ULONG_MAX) == 4, "BITS_PER_LONG is 32, but ULONG_MAX is not.");
511 
512         #undef UCHAR_MAX
513         #define UCHAR_MAX 255
514         #undef USHRT_MAX
515         #define USHRT_MAX 65535
516         #undef UINT_MAX
517         #define UINT_MAX 4294967295U
518         #undef ULONG_MAX
519         #define ULONG_MAX 4294967295UL
520         #undef INT_MAX
521         #define INT_MAX 2147483647
522         #undef LONG_MAX
523         #define LONG_MAX 2147483647L
524 
525         #undef ULLONG_MAX
526         #undef LLONG_MAX
527         #if BITS_PER_LONG_LONG == 64
528             #define ULLONG_MAX 18446744073709551615UL
529             #define LLONG_MAX 9223372036854775807L
530         #else
531             #undef NO_64BIT
532             #define NO_64BIT
533             #define ULLONG_MAX ULONG_MAX
534             #define LLONG_MAX LONG_MAX
535         #endif
536 
537 #else
538         #error unexpected BITS_PER_LONG value.
539 #endif
540 
541     /* remove this multifariously conflicting macro, picked up from
542      * Linux arch/<arch>/include/asm/current.h.
543      */
544     #ifndef WOLFSSL_NEED_LINUX_CURRENT
545         #undef current
546     #endif
547 
548     /* prevent gcc's mm_malloc.h from being included, since it unconditionally
549      * includes stdlib.h, which is kernel-incompatible.
550      */
551     #define _MM_MALLOC_H_INCLUDED
552 
553     #ifdef HAVE_KVMALLOC
554         #define malloc(x) kvmalloc_node(x, GFP_KERNEL, NUMA_NO_NODE)
555         #define free(x) kvfree(x)
556         void *lkm_realloc(void *ptr, size_t newsize);
557         #define realloc(x, y) lkm_realloc(x, y)
558     #else
559         #define malloc(x) kmalloc(x, GFP_KERNEL)
560         #define free(x) kfree(x)
561         #define realloc(x,y) krealloc(x, y, GFP_KERNEL)
562     #endif
563 
564     /* min() and max() in linux/kernel.h over-aggressively type-check, producing
565      * myriad spurious -Werrors throughout the codebase.
566      */
567     #undef min
568     #undef max
569 
570     /* work around namespace conflict between wolfssl/internal.h (enum HandShakeType)
571      * and linux/key.h (extern int()).
572      */
573     #define key_update wc_key_update
574 
575     #define lkm_printf(format, args...) printk(KERN_INFO "wolfssl: %s(): " format, __func__, ## args)
576     #define printf(...) lkm_printf(__VA_ARGS__)
577 
578     #ifdef HAVE_FIPS
579         extern void fipsEntry(void);
580     #endif
581 
582     /* suppress false-positive "writing 1 byte into a region of size 0" warnings
583      * building old kernels with new gcc:
584      */
585     #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
586     _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"");
587     #endif
588 
589     #endif /* BUILDING_WOLFSSL */
590 
591     /* needed to suppress inclusion of stdio.h in wolfssl/wolfcrypt/types.h */
592     #define XSNPRINTF snprintf
593 
594     /* the rigmarole around kstrtoll() here is to accommodate its warn-unused-result attribute. */
595     /* also needed to suppress inclusion of stdlib.h in wolfssl/wolfcrypt/types.h */
596     #define XATOI(s) ({                                 \
597           long long _xatoi_res = 0;                     \
598           int _xatoi_ret = kstrtoll(s, 10, &_xatoi_res); \
599           if (_xatoi_ret != 0) {                        \
600             _xatoi_res = 0;                             \
601           }                                             \
602           (int)_xatoi_res;                              \
603         })
604 
605 #endif /* WOLFSSL_LINUXKM */
606 
607 /* THREADING/MUTEX SECTION */
608 #ifdef USE_WINDOWS_API
609     #ifdef WOLFSSL_GAME_BUILD
610         #include "system/xtl.h"
611     #else
612         #ifndef WIN32_LEAN_AND_MEAN
613             #define WIN32_LEAN_AND_MEAN
614         #endif
615         #ifndef WOLFSSL_SGX
616             #if defined(_WIN32_WCE) || defined(WIN32_LEAN_AND_MEAN)
617                 /* On WinCE winsock2.h must be included before windows.h */
618                 #include <winsock2.h>
619             #endif
620             #include <windows.h>
621             #ifndef WOLFSSL_USER_IO
622                 #include <ws2tcpip.h> /* required for InetPton */
623             #endif
624         #endif /* WOLFSSL_SGX */
625     #endif
626 #elif defined(THREADX)
627     #ifndef SINGLE_THREADED
628         #ifdef NEED_THREADX_TYPES
629             #include <types.h>
630         #endif
631         #include <tx_api.h>
632     #endif
633 #elif defined(WOLFSSL_DEOS)
634     #include "mutexapi.h"
635 #elif defined(MICRIUM)
636     /* do nothing, just don't pick Unix */
637 #elif defined(FREERTOS) || defined(FREERTOS_TCP) || defined(WOLFSSL_SAFERTOS)
638     /* do nothing */
639 #elif defined(RTTHREAD)
640     /* do nothing */
641 #elif defined(EBSNET)
642     /* do nothing */
643 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
644     /* do nothing */
645 #elif defined(FREESCALE_FREE_RTOS)
646     #include "fsl_os_abstraction.h"
647 #elif defined(WOLFSSL_VXWORKS)
648     #include <semLib.h>
649 #elif defined(WOLFSSL_uITRON4)
650     #include "stddef.h"
651     #include "kernel.h"
652 #elif  defined(WOLFSSL_uTKERNEL2)
653     #include "tk/tkernel.h"
654 #elif defined(WOLFSSL_CMSIS_RTOS)
655     #include "cmsis_os.h"
656 #elif defined(WOLFSSL_CMSIS_RTOSv2)
657     #include "cmsis_os2.h"
658 #elif defined(WOLFSSL_MDK_ARM)
659     #if defined(WOLFSSL_MDK5)
660         #include "cmsis_os.h"
661     #else
662         #include <rtl.h>
663     #endif
664 #elif defined(WOLFSSL_CMSIS_RTOS)
665     #include "cmsis_os.h"
666 #elif defined(WOLFSSL_TIRTOS)
667     #include <ti/sysbios/BIOS.h>
668     #include <ti/sysbios/knl/Semaphore.h>
669 #elif defined(WOLFSSL_FROSTED)
670     #include <semaphore.h>
671 #elif defined(INTIME_RTOS)
672     #include <rt.h>
673     #include <io.h>
674 #elif defined(WOLFSSL_NUCLEUS_1_2)
675     /* NU_DEBUG needed struct access in nucleus_realloc */
676     #define NU_DEBUG
677     #include "plus/nucleus.h"
678     #include "nucleus.h"
679 #elif defined(WOLFSSL_APACHE_MYNEWT)
680     /* do nothing */
681 #elif defined(WOLFSSL_ZEPHYR)
682     #ifndef SINGLE_THREADED
683         #include <kernel.h>
684     #endif
685 #elif defined(WOLFSSL_TELIT_M2MB)
686 
687     /* Telit SDK uses C++ compile option (--cpp), which causes link issue
688         to API's if wrapped in extern "C" */
689     #ifdef __cplusplus
690         }  /* extern "C" */
691     #endif
692 
693     #include "m2mb_types.h"
694     #include "m2mb_os_types.h"
695     #include "m2mb_os_api.h"
696     #include "m2mb_os.h"
697     #include "m2mb_os_mtx.h"
698     #ifndef NO_ASN_TIME
699     #include "m2mb_rtc.h"
700     #endif
701     #ifndef NO_FILESYSTEM
702     #include "m2mb_fs_posix.h"
703     #endif
704 
705     #undef kB /* eliminate conflict in asn.h */
706 
707     #ifdef __cplusplus
708         extern "C" {
709     #endif
710 
711 #else
712     #ifndef SINGLE_THREADED
713         #ifndef WOLFSSL_USER_MUTEX
714             #if defined(WOLFSSL_LINUXKM)
715                 #define WOLFSSL_KTHREADS
716             #else
717                 #define WOLFSSL_PTHREADS
718                 #include <pthread.h>
719             #endif
720         #endif
721     #endif
722     #if (defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)) && \
723         !defined(NO_FILESYSTEM)
724         #ifdef FUSION_RTOS
725             #include <fclunistd.h>
726         #else
727             #include <unistd.h>      /* for close of BIO */
728         #endif
729     #endif
730 #endif
731 
732 /* For FIPS keep the function names the same */
733 #ifdef HAVE_FIPS
734 #define wc_InitMutex   InitMutex
735 #define wc_FreeMutex   FreeMutex
736 #define wc_LockMutex   LockMutex
737 #define wc_UnLockMutex UnLockMutex
738 #endif /* HAVE_FIPS */
739 
740 #ifdef SINGLE_THREADED
741     typedef int wolfSSL_Mutex;
742 #else /* MULTI_THREADED */
743     /* FREERTOS comes first to enable use of FreeRTOS Windows simulator only */
744     #if defined(FREERTOS)
745         typedef xSemaphoreHandle wolfSSL_Mutex;
746     #elif defined(FREERTOS_TCP)
747         #include "FreeRTOS.h"
748         #include "semphr.h"
749         typedef SemaphoreHandle_t  wolfSSL_Mutex;
750     #elif defined (RTTHREAD)
751         #include "rtthread.h"
752         typedef rt_mutex_t wolfSSL_Mutex;
753     #elif defined(WOLFSSL_SAFERTOS)
754         typedef struct wolfSSL_Mutex {
755             signed char mutexBuffer[portQUEUE_OVERHEAD_BYTES];
756             xSemaphoreHandle mutex;
757         } wolfSSL_Mutex;
758     #elif defined(USE_WINDOWS_API)
759         typedef CRITICAL_SECTION wolfSSL_Mutex;
760     #elif defined(WOLFSSL_PTHREADS)
761         typedef pthread_mutex_t wolfSSL_Mutex;
762     #elif defined(WOLFSSL_KTHREADS)
763         typedef struct mutex wolfSSL_Mutex;
764     #elif defined(THREADX)
765         typedef TX_MUTEX wolfSSL_Mutex;
766     #elif defined(WOLFSSL_DEOS)
767         typedef mutex_handle_t wolfSSL_Mutex;
768     #elif defined(MICRIUM)
769         typedef OS_MUTEX wolfSSL_Mutex;
770     #elif defined(EBSNET)
771         typedef RTP_MUTEX wolfSSL_Mutex;
772     #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
773         typedef MUTEX_STRUCT wolfSSL_Mutex;
774     #elif defined(FREESCALE_FREE_RTOS)
775         typedef mutex_t wolfSSL_Mutex;
776     #elif defined(WOLFSSL_VXWORKS)
777         typedef SEM_ID wolfSSL_Mutex;
778     #elif defined(WOLFSSL_uITRON4)
779         typedef struct wolfSSL_Mutex {
780             T_CSEM sem ;
781             ID     id ;
782         } wolfSSL_Mutex;
783     #elif defined(WOLFSSL_uTKERNEL2)
784         typedef struct wolfSSL_Mutex {
785             T_CSEM sem ;
786             ID     id ;
787         } wolfSSL_Mutex;
788     #elif defined(WOLFSSL_MDK_ARM)
789         #if defined(WOLFSSL_CMSIS_RTOS)
790             typedef osMutexId wolfSSL_Mutex;
791         #else
792             typedef OS_MUT wolfSSL_Mutex;
793         #endif
794     #elif defined(WOLFSSL_CMSIS_RTOS)
795         typedef osMutexId wolfSSL_Mutex;
796     #elif defined(WOLFSSL_CMSIS_RTOSv2)
797         typedef osMutexId_t wolfSSL_Mutex;
798     #elif defined(WOLFSSL_TIRTOS)
799         typedef ti_sysbios_knl_Semaphore_Handle wolfSSL_Mutex;
800     #elif defined(WOLFSSL_FROSTED)
801         typedef mutex_t * wolfSSL_Mutex;
802     #elif defined(INTIME_RTOS)
803         typedef RTHANDLE wolfSSL_Mutex;
804     #elif defined(WOLFSSL_NUCLEUS_1_2)
805         typedef NU_SEMAPHORE wolfSSL_Mutex;
806     #elif defined(WOLFSSL_ZEPHYR)
807         typedef struct k_mutex wolfSSL_Mutex;
808     #elif defined(WOLFSSL_TELIT_M2MB)
809         typedef M2MB_OS_MTX_HANDLE wolfSSL_Mutex;
810     #elif defined(WOLFSSL_USER_MUTEX)
811         /* typedef User_Mutex wolfSSL_Mutex; */
812     #elif defined(WOLFSSL_LINUXKM)
813         typedef struct mutex wolfSSL_Mutex;
814     #else
815         #error Need a mutex type in multithreaded mode
816     #endif /* USE_WINDOWS_API */
817 #endif /* SINGLE_THREADED */
818 
819 /* Enable crypt HW mutex for Freescale MMCAU, PIC32MZ or STM32 */
820 #if defined(FREESCALE_MMCAU) || defined(WOLFSSL_MICROCHIP_PIC32MZ) || \
821     defined(STM32_CRYPTO) || defined(STM32_HASH) || defined(STM32_RNG)
822     #ifndef WOLFSSL_CRYPT_HW_MUTEX
823         #define WOLFSSL_CRYPT_HW_MUTEX  1
824     #endif
825 #endif /* FREESCALE_MMCAU */
826 
827 #ifndef WOLFSSL_CRYPT_HW_MUTEX
828     #define WOLFSSL_CRYPT_HW_MUTEX  0
829 #endif
830 
831 #if WOLFSSL_CRYPT_HW_MUTEX
832     /* wolfSSL_CryptHwMutexInit is called on first wolfSSL_CryptHwMutexLock,
833        however it's recommended to call this directly on Hw init to avoid possible
834        race condition where two calls to wolfSSL_CryptHwMutexLock are made at
835        the same time. */
836     int wolfSSL_CryptHwMutexInit(void);
837     int wolfSSL_CryptHwMutexLock(void);
838     int wolfSSL_CryptHwMutexUnLock(void);
839 #else
840     /* Define stubs, since HW mutex is disabled */
841     #define wolfSSL_CryptHwMutexInit()      0 /* Success */
842     #define wolfSSL_CryptHwMutexLock()      0 /* Success */
843     #define wolfSSL_CryptHwMutexUnLock()    (void)0 /* Success */
844 #endif /* WOLFSSL_CRYPT_HW_MUTEX */
845 
846 /* Mutex functions */
847 WOLFSSL_API int wc_InitMutex(wolfSSL_Mutex*);
848 WOLFSSL_API wolfSSL_Mutex* wc_InitAndAllocMutex(void);
849 WOLFSSL_API int wc_FreeMutex(wolfSSL_Mutex*);
850 WOLFSSL_API int wc_LockMutex(wolfSSL_Mutex*);
851 WOLFSSL_API int wc_UnLockMutex(wolfSSL_Mutex*);
852 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
853 /* dynamically set which mutex to use. unlock / lock is controlled by flag */
854 typedef void (mutex_cb)(int flag, int type, const char* file, int line);
855 
856 WOLFSSL_API int wc_LockMutex_ex(int flag, int type, const char* file, int line);
857 WOLFSSL_API int wc_SetMutexCb(mutex_cb* cb);
858 #endif
859 
860 /* main crypto initialization function */
861 WOLFSSL_API int wolfCrypt_Init(void);
862 WOLFSSL_API int wolfCrypt_Cleanup(void);
863 
864 #ifdef WOLFSSL_TRACK_MEMORY_VERBOSE
865     WOLFSSL_API long wolfCrypt_heap_peakAllocs_checkpoint(void);
866     WOLFSSL_API long wolfCrypt_heap_peakBytes_checkpoint(void);
867 #endif
868 
869 
870 /* FILESYSTEM SECTION */
871 /* filesystem abstraction layer, used by ssl.c */
872 #ifndef NO_FILESYSTEM
873 
874 #if defined(EBSNET)
875     #include "vfapi.h"
876     #include "vfile.h"
877 
878     int ebsnet_fseek(int a, long b, int c); /* Not prototyped in vfile.h per
879                                              * EBSnet feedback */
880 
881     #define XFILE                    int
882     #define XFOPEN(NAME, MODE)       vf_open((const char *)NAME, VO_RDONLY, 0)
883     #define XFSEEK                   ebsnet_fseek
884     #define XFTELL                   vf_tell
885     #define XREWIND                  vf_rewind
886     #define XFREAD(BUF, SZ, AMT, FD) vf_read(FD, BUF, SZ*AMT)
887     #define XFWRITE(BUF, SZ, AMT, FD) vf_write(FD, BUF, SZ*AMT)
888     #define XFCLOSE                  vf_close
889     #define XSEEK_END                VSEEK_END
890     #define XBADFILE                 -1
891     #define XFGETS(b,s,f)            -2 /* Not ported yet */
892 
893 #elif defined(LSR_FS)
894     #include <fs.h>
895     #define XFILE                   struct fs_file*
896     #define XFOPEN(NAME, MODE)      fs_open((char*)NAME)
897     #define XFSEEK(F, O, W)         (void)F
898     #define XFTELL(F)               (F)->len
899     #define XREWIND(F)              (void)F
900     #define XFREAD(BUF, SZ, AMT, F) fs_read(F, (char*)BUF, SZ*AMT)
901     #define XFWRITE(BUF, SZ, AMT, F) fs_write(F, (char*)BUF, SZ*AMT)
902     #define XFCLOSE                 fs_close
903     #define XSEEK_END               0
904     #define XBADFILE                NULL
905     #define XFGETS(b,s,f)           -2 /* Not ported yet */
906 
907 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
908     #define XFILE                   MQX_FILE_PTR
909     #define XFOPEN                  fopen
910     #define XFSEEK                  fseek
911     #define XFTELL                  ftell
912     #define XREWIND(F)              fseek(F, 0, IO_SEEK_SET)
913     #define XFREAD                  fread
914     #define XFWRITE                 fwrite
915     #define XFCLOSE                 fclose
916     #define XSEEK_END               IO_SEEK_END
917     #define XBADFILE                NULL
918     #define XFGETS                  fgets
919 
920 #elif defined(WOLFSSL_DEOS)
921     #define NO_FILESYSTEM
922     #warning "TODO - DDC-I Certifiable Fast File System for Deos is not integrated"
923 #elif defined(MICRIUM)
924     #include <fs_api.h>
925     #define XFILE      FS_FILE*
926     #define XFOPEN     fs_fopen
927     #define XFSEEK     fs_fseek
928     #define XFTELL     fs_ftell
929     #define XREWIND    fs_rewind
930     #define XFREAD     fs_fread
931     #define XFWRITE    fs_fwrite
932     #define XFCLOSE    fs_fclose
933     #define XSEEK_END  FS_SEEK_END
934     #define XBADFILE   NULL
935     #define XFGETS(b,s,f) -2 /* Not ported yet */
936 
937 #elif defined(WOLFSSL_NUCLEUS_1_2)
938     #include "fal/inc/fal.h"
939     #define XFILE      FILE*
940     #define XFOPEN     fopen
941     #define XFSEEK     fseek
942     #define XFTELL     ftell
943     #define XREWIND    rewind
944     #define XFREAD     fread
945     #define XFWRITE    fwrite
946     #define XFCLOSE    fclose
947     #define XSEEK_END  PSEEK_END
948     #define XBADFILE   NULL
949 
950 #elif defined(WOLFSSL_APACHE_MYNEWT)
951     #include <fs/fs.h>
952     #define XFILE  struct fs_file*
953 
954     #define XFOPEN     mynewt_fopen
955     #define XFSEEK     mynewt_fseek
956     #define XFTELL     mynewt_ftell
957     #define XREWIND    mynewt_rewind
958     #define XFREAD     mynewt_fread
959     #define XFWRITE    mynewt_fwrite
960     #define XFCLOSE    mynewt_fclose
961     #define XSEEK_END  2
962     #define XBADFILE   NULL
963     #define XFGETS(b,s,f) -2 /* Not ported yet */
964 
965 #elif defined(WOLFSSL_ZEPHYR)
966     #include <fs.h>
967 
968     #define XFILE      struct fs_file_t*
969     #define STAT       struct fs_dirent
970 
971     XFILE z_fs_open(const char* filename, const char* perm);
972     int z_fs_close(XFILE file);
973 
974     #define XFOPEN              z_fs_open
975     #define XFCLOSE             z_fs_close
976     #define XFSEEK              fs_seek
977     #define XFTELL              fs_tell
978     #define XFREWIND            fs_rewind
979     #define XREWIND(F)          fs_seek(F, 0, FS_SEEK_SET)
980     #define XFREAD(P,S,N,F)     fs_read(F, P, S*N)
981     #define XFWRITE(P,S,N,F)    fs_write(F, P, S*N)
982     #define XSEEK_END           FS_SEEK_END
983     #define XBADFILE            NULL
984     #define XFGETS(b,s,f)       -2 /* Not ported yet */
985 
986 #elif defined(WOLFSSL_TELIT_M2MB)
987     #define XFILE                    INT32
988     #define XFOPEN(NAME, MODE)       m2mb_fs_open((NAME), 0, (MODE))
989     #define XFSEEK(F, O, W)          m2mb_fs_lseek((F), (O), (W))
990     #define XFTELL(F)                m2mb_fs_lseek((F), 0, M2MB_SEEK_END)
991     #define XREWIND(F)               (void)F
992     #define XFREAD(BUF, SZ, AMT, F)  m2mb_fs_read((F), (BUF), (SZ)*(AMT))
993     #define XFWRITE(BUF, SZ, AMT, F) m2mb_fs_write((F), (BUF), (SZ)*(AMT))
994     #define XFCLOSE                  m2mb_fs_close
995     #define XSEEK_END                M2MB_SEEK_END
996     #define XBADFILE                 -1
997     #define XFGETS(b,s,f)            -2 /* Not ported yet */
998 
999 #elif defined (WOLFSSL_XILINX)
1000     #include "xsdps.h"
1001     #include "ff.h"
1002 
1003     /* workaround to declare variable and provide type */
1004     #define XFILE                    FIL curFile; FIL*
1005     #define XFOPEN(NAME, MODE)       ({ FRESULT res; res = f_open(&curFile, (NAME), (FA_OPEN_ALWAYS | FA_WRITE | FA_READ)); (res == FR_OK) ? &curFile : NULL; })
1006     #define XFSEEK(F, O, W)          f_lseek((F), (O))
1007     #define XFTELL(F)                f_tell((F))
1008     #define XREWIND(F)               f_rewind((F))
1009     #define XFREAD(BUF, SZ, AMT, F)  ({ FRESULT res; UINT br; res = f_read((F), (BUF), (SZ)*(AMT), &br); (void)br; res; })
1010     #define XFWRITE(BUF, SZ, AMT, F) ({ FRESULT res; UINT written; res = f_write((F), (BUF), (SZ)*(AMT), &written); (void)written; res; })
1011     #define XFCLOSE(F)               f_close((F))
1012     #define XSEEK_END                0
1013     #define XBADFILE                 NULL
1014     #define XFGETS(b,s,f)            f_gets((b), (s), (f))
1015 #elif defined (_WIN32_WCE)
1016     /* stdio, WINCE case */
1017     #include <stdio.h>
1018     #define XFILE      FILE*
1019     #define XFOPEN     fopen
1020     #define XFDOPEN    fdopen
1021     #define XFSEEK     fseek
1022     #define XFTELL     ftell
1023     #define XREWIND(F) XFSEEK(F, 0, SEEK_SET)
1024     #define XFREAD     fread
1025     #define XFWRITE    fwrite
1026     #define XFCLOSE    fclose
1027     #define XSEEK_END  SEEK_END
1028     #define XBADFILE   NULL
1029     #define XFGETS     fgets
1030     #define XVSNPRINTF _vsnprintf
1031 
1032 #elif defined(FUSION_RTOS)
1033     #include <fclstdio.h>
1034     #include <fclunistd.h>
1035     #include <fcldirent.h>
1036     #include <sys/fclstat.h>
1037     #include <fclstring.h>
1038     #include <fcl_os.h>
1039     #define XFILE     FCL_FILE*
1040     #define XFOPEN    FCL_FOPEN
1041     #define XFSEEK    FCL_FSEEK
1042     #define XFTELL    FCL_FTELL
1043     #define XREWIND   FCL_REWIND
1044     #define XFREAD    FCL_FREAD
1045     #define XFWRITE   FCL_FWRITE
1046     #define XFCLOSE   FCL_FCLOSE
1047     #define XSEEK_END SEEK_END
1048     #define XBADFILE  NULL
1049     #define XFGETS    FCL_FGETS
1050     #define XFPUTS    FCL_FPUTS
1051     #define XFPRINTF  FCL_FPRINTF
1052     #define XVFPRINTF FCL_VFPRINTF
1053     #define XVSNPRINTF  FCL_VSNPRINTF
1054     #define XSNPRINTF  FCL_SNPRINTF
1055     #define XSPRINTF  FCL_SPRINTF
1056     #define DIR       FCL_DIR
1057     #define stat      FCL_STAT
1058     #define opendir   FCL_OPENDIR
1059     #define closedir  FCL_CLOSEDIR
1060     #define readdir   FCL_READDIR
1061     #define dirent    fclDirent
1062     #define strncasecmp FCL_STRNCASECMP
1063 
1064     /* FUSION SPECIFIC ERROR CODE */
1065     #define FUSION_IO_SEND_E FCL_EWOULDBLOCK
1066 
1067 #elif defined(WOLFSSL_USER_FILESYSTEM)
1068     /* To be defined in user_settings.h */
1069 #else
1070     /* stdio, default case */
1071     #include <stdio.h>
1072     #define XFILE      FILE*
1073     #if defined(WOLFSSL_MDK_ARM)
1074         extern FILE * wolfSSL_fopen(const char *name, const char *mode) ;
1075         #define XFOPEN     wolfSSL_fopen
1076     #else
1077         #define XFOPEN     fopen
1078     #endif
1079     #define XFDOPEN    fdopen
1080     #define XFSEEK     fseek
1081     #define XFTELL     ftell
1082     #define XREWIND    rewind
1083     #define XFREAD     fread
1084     #define XFWRITE    fwrite
1085     #define XFCLOSE    fclose
1086     #define XSEEK_END  SEEK_END
1087     #define XBADFILE   NULL
1088     #define XFGETS     fgets
1089     #define XFPRINTF   fprintf
1090     #define XFFLUSH    fflush
1091 
1092     #if !defined(NO_WOLFSSL_DIR)\
1093         && !defined(WOLFSSL_NUCLEUS) && !defined(WOLFSSL_NUCLEUS_1_2)
1094     #if defined(USE_WINDOWS_API)
1095         #include <sys/stat.h>
1096         #define XSTAT       _stat
1097         #define XS_ISREG(s) (s & _S_IFREG)
1098         #define SEPARATOR_CHAR ';'
1099 
1100     #elif defined(INTIME_RTOS)
1101         #include <sys/stat.h>
1102         #define XSTAT _stat64
1103         #define XS_ISREG(s) S_ISREG(s)
1104         #define SEPARATOR_CHAR ';'
1105         #define XWRITE      write
1106         #define XREAD       read
1107         #define XCLOSE      close
1108 
1109     #elif defined(WOLFSSL_ZEPHYR)
1110         #define XSTAT       fs_stat
1111         #define XS_ISREG(s) (s == FS_DIR_ENTRY_FILE)
1112         #define SEPARATOR_CHAR ':'
1113     #elif defined(WOLFSSL_TELIT_M2MB)
1114         #define XSTAT       m2mb_fs_stat
1115         #define XS_ISREG(s) (s & M2MB_S_IFREG)
1116         #define SEPARATOR_CHAR ':'
1117     #else
1118         #include <dirent.h>
1119         #include <unistd.h>
1120         #include <sys/stat.h>
1121         #define XWRITE      write
1122         #define XREAD       read
1123         #define XCLOSE      close
1124         #define XSTAT       stat
1125         #define XS_ISREG(s) S_ISREG(s)
1126         #define SEPARATOR_CHAR ':'
1127     #endif
1128     #endif
1129 #endif
1130 
1131     #ifndef MAX_FILENAME_SZ
1132         #define MAX_FILENAME_SZ  256 /* max file name length */
1133     #endif
1134     #ifndef MAX_PATH
1135         #define MAX_PATH 256
1136     #endif
1137 
1138     WOLFSSL_LOCAL int wc_FileLoad(const char* fname, unsigned char** buf,
1139         size_t* bufLen, void* heap);
1140 
1141 #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_NUCLEUS) && \
1142     !defined(WOLFSSL_NUCLEUS_1_2)
1143     typedef struct ReadDirCtx {
1144     #ifdef USE_WINDOWS_API
1145         WIN32_FIND_DATAA FindFileData;
1146         HANDLE hFind;
1147         struct XSTAT s;
1148     #elif defined(WOLFSSL_ZEPHYR)
1149         struct fs_dirent entry;
1150         struct fs_dir_t  dir;
1151         struct fs_dirent s;
1152         struct fs_dir_t* dirp;
1153 
1154     #elif defined(WOLFSSL_TELIT_M2MB)
1155         M2MB_DIR_T* dir;
1156         struct M2MB_DIRENT* entry;
1157         struct M2MB_STAT s;
1158     #elif defined(INTIME_RTOS)
1159         struct stat64 s;
1160         struct _find64 FindFileData;
1161         #define IntimeFindFirst(name, data) (0 == _findfirst64(name, data))
1162         #define IntimeFindNext(data)  (0 == _findnext64(data))
1163         #define IntimeFindClose(data) (0 == _findclose64(data))
1164         #define IntimeFilename(ctx)   ctx->FindFileData.f_filename
1165     #else
1166         struct dirent* entry;
1167         DIR*   dir;
1168         struct XSTAT s;
1169     #endif
1170         char name[MAX_FILENAME_SZ];
1171     } ReadDirCtx;
1172 
1173     #define WC_READDIR_NOFILE -1
1174 
1175     WOLFSSL_API int wc_ReadDirFirst(ReadDirCtx* ctx, const char* path, char** name);
1176     WOLFSSL_API int wc_ReadDirNext(ReadDirCtx* ctx, const char* path, char** name);
1177     WOLFSSL_API void wc_ReadDirClose(ReadDirCtx* ctx);
1178 #endif /* !NO_WOLFSSL_DIR */
1179     #define WC_ISFILEEXIST_NOFILE -1
1180 
1181     WOLFSSL_API int wc_FileExists(const char* fname);
1182 
1183 #endif /* !NO_FILESYSTEM */
1184 
1185 /* Defaults, user may over-ride with user_settings.h or in a porting section
1186  * above
1187  */
1188 #ifndef XVFPRINTF
1189     #define XVFPRINTF  vfprintf
1190 #endif
1191 #ifndef XVSNPRINTF
1192     #define XVSNPRINTF vsnprintf
1193 #endif
1194 #ifndef XFPUTS
1195     #define XFPUTS     fputs
1196 #endif
1197 #ifndef XSPRINTF
1198     #define XSPRINTF   sprintf
1199 #endif
1200 
1201 
1202 /* MIN/MAX MACRO SECTION */
1203 /* Windows API defines its own min() macro. */
1204 #if defined(USE_WINDOWS_API)
1205     #if defined(min) || defined(WOLFSSL_MYSQL_COMPATIBLE)
1206         #define WOLFSSL_HAVE_MIN
1207     #endif /* min */
1208     #if defined(max) || defined(WOLFSSL_MYSQL_COMPATIBLE)
1209         #define WOLFSSL_HAVE_MAX
1210     #endif /* max */
1211 #endif /* USE_WINDOWS_API */
1212 
1213 #ifdef __QNXNTO__
1214     #define WOLFSSL_HAVE_MIN
1215     #define WOLFSSL_HAVE_MAX
1216 #endif
1217 
1218 /* TIME SECTION */
1219 /* Time functions */
1220 #ifndef NO_ASN_TIME
1221 #if defined(USER_TIME)
1222     /* Use our gmtime and time_t/struct tm types.
1223        Only needs seconds since EPOCH using XTIME function.
1224        time_t XTIME(time_t * timer) {}
1225     */
1226     #define WOLFSSL_GMTIME
1227     #ifndef HAVE_TM_TYPE
1228         #define USE_WOLF_TM
1229     #endif
1230     #ifndef HAVE_TIME_T_TYPE
1231         #define USE_WOLF_TIME_T
1232     #endif
1233 
1234 #elif defined(TIME_OVERRIDES)
1235     /* Override XTIME() and XGMTIME() functionality.
1236        Requires user to provide these functions:
1237         time_t XTIME(time_t * timer) {}
1238         struct tm* XGMTIME(const time_t* timer, struct tm* tmp) {}
1239     */
1240     #ifndef HAVE_TIME_T_TYPE
1241         #define USE_WOLF_TIME_T
1242     #endif
1243     #ifndef HAVE_TM_TYPE
1244         #define USE_WOLF_TM
1245     #endif
1246     #define NEED_TMP_TIME
1247 
1248 #elif defined(WOLFSSL_XILINX)
1249     #ifndef XTIME
1250         #define XTIME(t1)       xilinx_time((t1))
1251     #endif
1252     #include <time.h>
1253 
1254 #elif defined(HAVE_RTP_SYS)
1255     #include "os.h"           /* dc_rtc_api needs    */
1256     #include "dc_rtc_api.h"   /* to get current time */
1257 
1258     /* uses parital <time.h> structures */
1259     #define XTIME(tl)       (0)
1260     #define XGMTIME(c, t)   rtpsys_gmtime((c))
1261 
1262 #elif defined(WOLFSSL_DEOS)
1263     #include <time.h>
1264 
1265 #elif defined(MICRIUM)
1266     #include <clk.h>
1267     #include <time.h>
1268     #define XTIME(t1)       micrium_time((t1))
1269     #define WOLFSSL_GMTIME
1270 
1271 #elif defined(MICROCHIP_TCPIP_V5) || defined(MICROCHIP_TCPIP)
1272     #include <time.h>
1273     extern time_t pic32_time(time_t* timer);
1274     #define XTIME(t1)       pic32_time((t1))
1275     #define XGMTIME(c, t)   gmtime((c))
1276 
1277 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
1278     #ifdef FREESCALE_MQX_4_0
1279         #include <time.h>
1280         extern time_t mqx_time(time_t* timer);
1281     #else
1282         #define HAVE_GMTIME_R
1283     #endif
1284     #define XTIME(t1)       mqx_time((t1))
1285 
1286 #elif defined(FREESCALE_KSDK_BM) || defined(FREESCALE_FREE_RTOS) || defined(FREESCALE_KSDK_FREERTOS)
1287     #include <time.h>
1288     #ifndef XTIME
1289         /*extern time_t ksdk_time(time_t* timer);*/
1290         #define XTIME(t1)   ksdk_time((t1))
1291     #endif
1292     #define XGMTIME(c, t)   gmtime((c))
1293 
1294 #elif defined(WOLFSSL_ATMEL) && defined(WOLFSSL_ATMEL_TIME)
1295     #define XTIME(t1)       atmel_get_curr_time_and_date((t1))
1296     #define WOLFSSL_GMTIME
1297     #define USE_WOLF_TM
1298     #define USE_WOLF_TIME_T
1299 
1300 #elif defined(WOLFSSL_WICED)
1301     #include <time.h>
1302     time_t wiced_pseudo_unix_epoch_time(time_t * timer);
1303     #define XTIME(t1)       wiced_pseudo_unix_epoch_time((t1))
1304     #define HAVE_GMTIME_R
1305 
1306 #elif defined(IDIRECT_DEV_TIME)
1307     /*Gets the timestamp from cloak software owned by VT iDirect
1308     in place of time() from <time.h> */
1309     #include <time.h>
1310     #define XTIME(t1)       idirect_time((t1))
1311     #define XGMTIME(c, t)   gmtime((c))
1312 
1313 #elif defined(_WIN32_WCE)
1314     #include <windows.h>
1315     #include <stdlib.h> /* For file system */
1316 
1317     time_t windows_time(time_t* timer);
1318 
1319     #define FindNextFileA(h, d) FindNextFile(h, (LPWIN32_FIND_DATAW) d)
1320     #define FindFirstFileA(fn, d) FindFirstFile((LPCWSTR) fn, \
1321                                                 (LPWIN32_FIND_DATAW) d)
1322     #define XTIME(t1)       windows_time((t1))
1323     #define WOLFSSL_GMTIME
1324 
1325     /* if struct tm is not defined in WINCE SDK */
1326     #ifndef _TM_DEFINED
1327         struct tm {
1328             int tm_sec;     /* seconds */
1329             int tm_min;     /* minutes */
1330             int tm_hour;    /* hours */
1331             int tm_mday;    /* day of month (month specific) */
1332             int tm_mon;     /* month */
1333             int tm_year;    /* year */
1334             int tm_wday;    /* day of week (out of 1-7)*/
1335             int tm_yday;    /* day of year (out of 365) */
1336             int tm_isdst;   /* is it daylight savings */
1337             };
1338             #define _TM_DEFINED
1339     #endif
1340 
1341 #elif defined(WOLFSSL_APACHE_MYNEWT)
1342     #include "os/os_time.h"
1343     #define XTIME(t1)       mynewt_time((t1))
1344     #define WOLFSSL_GMTIME
1345     #define USE_WOLF_TM
1346     #define USE_WOLF_TIME_T
1347 
1348 #elif defined(WOLFSSL_ZEPHYR)
1349     #ifndef _POSIX_C_SOURCE
1350         #include <posix/time.h>
1351     #else
1352         #include <sys/time.h>
1353     #endif
1354 
1355     time_t z_time(time_t *timer);
1356 
1357     #define XTIME(tl)       z_time((tl))
1358     #define XGMTIME(c, t)   gmtime((c))
1359 
1360 #elif defined(WOLFSSL_TELIT_M2MB)
1361     typedef long time_t;
1362     extern time_t m2mb_xtime(time_t * timer);
1363     #define XTIME(tl)       m2mb_xtime((tl))
1364     #ifdef WOLFSSL_TLS13
1365         extern time_t m2mb_xtime_ms(time_t * timer);
1366         #define XTIME_MS(tl)    m2mb_xtime_ms((tl))
1367     #endif
1368     #ifndef NO_CRYPT_BENCHMARK
1369         extern double m2mb_xtime_bench(int reset);
1370         #define WOLFSSL_CURRTIME_REMAP m2mb_xtime_bench
1371     #endif
1372     #define XGMTIME(c, t)   gmtime((c))
1373     #define WOLFSSL_GMTIME
1374     #define USE_WOLF_TM
1375 
1376 
1377 #elif defined(WOLFSSL_LINUXKM)
1378     #ifdef BUILDING_WOLFSSL
1379 
1380     /* includes are all above, with incompatible warnings masked out. */
1381     #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
1382     typedef __kernel_time_t time_t;
1383     #else
1384     typedef __kernel_time64_t time_t;
1385     #endif
1386     extern time_t time(time_t * timer);
1387     #define XTIME time
1388     #define WOLFSSL_GMTIME
1389     #define XGMTIME(c, t) gmtime(c)
1390     #define NO_TIMEVAL 1
1391 
1392     #endif /* BUILDING_WOLFSSL */
1393 
1394 #elif defined(HAL_RTC_MODULE_ENABLED)
1395     #include <time.h>
1396     WOLFSSL_LOCAL time_t* stm32_hal_time(time_t* t1);
1397     #define XTIME(t1) stm32_hal_time(t1)
1398     #define WOLFSSL_GMTIME
1399 #else
1400     /* default */
1401     /* uses complete <time.h> facility */
1402     #include <time.h>
1403     #if defined(HAVE_SYS_TIME_H)
1404         #include <sys/time.h>
1405     #endif
1406 
1407     /* PowerPC time_t is int */
1408     #ifdef __PPC__
1409         #define TIME_T_NOT_64BIT
1410     #endif
1411 
1412     #define XMKTIME(tm) mktime(tm)
1413     #define XDIFFTIME(to, from) difftime(to, from)
1414 #endif
1415 
1416 #ifdef SIZEOF_TIME_T
1417     /* check if size of time_t from autoconf is less than 8 bytes (64bits) */
1418     #if SIZEOF_TIME_T < 8
1419         #undef  TIME_T_NOT_64BIT
1420         #define TIME_T_NOT_64BIT
1421     #endif
1422 #endif
1423 #ifdef TIME_T_NOT_LONG
1424     /* one old reference to TIME_T_NOT_LONG in GCC-ARM example README
1425      * this keeps support for the old macro name */
1426     #undef  TIME_T_NOT_64BIT
1427     #define TIME_T_NOT_64BIT
1428 #endif
1429 
1430 /* Map default time functions */
1431 #if !defined(XTIME) && !defined(TIME_OVERRIDES) && !defined(USER_TIME)
1432     #ifdef TEST_BEFORE_DATE
1433     #define XTIME(tl)       (946681200UL) /* Jan 1, 2000 */
1434     #else
1435     #define XTIME(tl)       time((tl))
1436     #endif
1437 #endif
1438 #if !defined(XGMTIME) && !defined(TIME_OVERRIDES)
1439     /* Always use gmtime_r if available. */
1440     #if defined(HAVE_GMTIME_R)
1441         #define XGMTIME(c, t)   gmtime_r((c), (t))
1442         #define NEED_TMP_TIME
1443     #else
1444         #define XGMTIME(c, t)   gmtime((c))
1445     #endif
1446 #endif
1447 #if !defined(XVALIDATE_DATE) && !defined(HAVE_VALIDATE_DATE)
1448     #define USE_WOLF_VALIDDATE
1449     #define XVALIDATE_DATE(d, f, t) wc_ValidateDate((d), (f), (t))
1450 #endif
1451 
1452 /* wolf struct tm and time_t */
1453 #if defined(USE_WOLF_TM)
1454     struct tm {
1455         int  tm_sec;     /* seconds after the minute [0-60] */
1456         int  tm_min;     /* minutes after the hour [0-59] */
1457         int  tm_hour;    /* hours since midnight [0-23] */
1458         int  tm_mday;    /* day of the month [1-31] */
1459         int  tm_mon;     /* months since January [0-11] */
1460         int  tm_year;    /* years since 1900 */
1461         int  tm_wday;    /* days since Sunday [0-6] */
1462         int  tm_yday;    /* days since January 1 [0-365] */
1463         int  tm_isdst;   /* Daylight Savings Time flag */
1464         long tm_gmtoff;  /* offset from CUT in seconds */
1465         char *tm_zone;   /* timezone abbreviation */
1466     };
1467 #endif /* USE_WOLF_TM */
1468 #if defined(USE_WOLF_TIME_T)
1469     typedef long time_t;
1470 #endif
1471 #if defined(USE_WOLF_SUSECONDS_T)
1472     typedef long suseconds_t;
1473 #endif
1474 #if defined(USE_WOLF_TIMEVAL_T)
1475     struct timeval
1476     {
1477         time_t tv_sec;
1478         suseconds_t tv_usec;
1479     };
1480 #endif
1481 
1482     /* forward declarations */
1483 #if defined(USER_TIME)
1484     struct tm* gmtime(const time_t* timer);
1485     extern time_t XTIME(time_t * timer);
1486 
1487     #ifdef STACK_TRAP
1488         /* for stack trap tracking, don't call os gmtime on OS X/linux,
1489            uses a lot of stack spce */
1490         extern time_t time(time_t * timer);
1491         #define XTIME(tl)  time((tl))
1492     #endif /* STACK_TRAP */
1493 
1494 #elif defined(TIME_OVERRIDES)
1495     extern time_t XTIME(time_t * timer);
1496     extern struct tm* XGMTIME(const time_t* timer, struct tm* tmp);
1497 #elif defined(WOLFSSL_GMTIME)
1498     struct tm* gmtime(const time_t* timer);
1499 #endif
1500 #endif /* NO_ASN_TIME */
1501 
1502 
1503 #ifndef WOLFSSL_LEANPSK
1504     char* mystrnstr(const char* s1, const char* s2, unsigned int n);
1505 #endif
1506 
1507 #ifndef FILE_BUFFER_SIZE
1508     /* default static file buffer size for input, will use dynamic buffer if
1509      * not big enough */
1510     #ifdef WOLFSSL_CERT_EXT
1511     #define FILE_BUFFER_SIZE (3*1024)
1512     #else
1513     #define FILE_BUFFER_SIZE (1*1024)
1514     #endif
1515 #endif
1516 
1517 #ifdef HAVE_CAVIUM_OCTEON_SYNC
1518     /* By default, the OCTEON's global variables are all thread local. This
1519      * tag allows them to be shared between threads. */
1520     #include "cvmx-platform.h"
1521     #define WOLFSSL_GLOBAL CVMX_SHARED
1522 #else
1523     #define WOLFSSL_GLOBAL
1524 #endif
1525 
1526 #ifdef WOLFSSL_DSP
1527     #include "wolfssl_dsp.h"
1528 
1529     /* callbacks for setting handle */
1530     typedef int (*wolfSSL_DSP_Handle_cb)(remote_handle64 *handle, int finished,
1531                                          void *ctx);
1532     WOLFSSL_API int wolfSSL_GetHandleCbSet();
1533     WOLFSSL_API int wolfSSL_SetHandleCb(wolfSSL_DSP_Handle_cb in);
1534     WOLFSSL_LOCAL int wolfSSL_InitHandle();
1535     WOLFSSL_LOCAL void wolfSSL_CleanupHandle();
1536 #endif
1537 
1538 #ifdef WOLFSSL_SCE
1539     #ifndef WOLFSSL_SCE_GSCE_HANDLE
1540         #define WOLFSSL_SCE_GSCE_HANDLE g_sce
1541     #endif
1542 #endif
1543 
1544 #ifdef __cplusplus
1545     }  /* extern "C" */
1546 #endif
1547 
1548 #endif /* WOLF_CRYPT_PORT_H */
1549