-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtypes.h
More file actions
2383 lines (2227 loc) · 92.2 KB
/
types.h
File metadata and controls
2383 lines (2227 loc) · 92.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* types.h
*
* Copyright (C) 2006-2025 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
/*!
\file wolfssl/wolfcrypt/types.h
*/
/*
DESCRIPTION
This library defines the primitive data types and abstraction macros to
decouple library dependencies with standard string, memory and so on.
*/
#ifndef WOLF_CRYPT_TYPES_H
#define WOLF_CRYPT_TYPES_H
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/wc_port.h>
#include <wolfssl/wolfcrypt/oid_sum.h>
#if defined(EXTERNAL_OPTS_OPENVPN) && defined(BUILDING_WOLFSSL)
#error EXTERNAL_OPTS_OPENVPN should not be defined in compiled wolfssl \
library files.
#endif
#ifdef __APPLE__
#include <AvailabilityMacros.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/*
* This struct is used multiple time by other structs and
* needs to be defined somewhere that all structs can import
* (with minimal dependencies).
*/
#ifdef HAVE_EX_DATA
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
typedef void (*wolfSSL_ex_data_cleanup_routine_t)(void *data);
#endif
typedef struct WOLFSSL_CRYPTO_EX_DATA {
void* ex_data[MAX_EX_DATA];
#ifdef HAVE_EX_DATA_CLEANUP_HOOKS
wolfSSL_ex_data_cleanup_routine_t
ex_data_cleanup_routines[MAX_EX_DATA];
#endif
} WOLFSSL_CRYPTO_EX_DATA;
typedef void (WOLFSSL_CRYPTO_EX_new)(void* p, void* ptr,
WOLFSSL_CRYPTO_EX_DATA* a, int idx, long argValue, void* arg);
typedef int (WOLFSSL_CRYPTO_EX_dup)(WOLFSSL_CRYPTO_EX_DATA* out,
const WOLFSSL_CRYPTO_EX_DATA* in, void* inPtr, int idx,
long argV, void* arg);
typedef void (WOLFSSL_CRYPTO_EX_free)(void* p, void* ptr,
WOLFSSL_CRYPTO_EX_DATA* a, int idx, long argValue, void* arg);
#endif
#if defined(WORDS_BIGENDIAN)
#define BIG_ENDIAN_ORDER
#endif
#ifndef BIG_ENDIAN_ORDER
#define LITTLE_ENDIAN_ORDER
#endif
#ifndef WOLFSSL_TYPES
#define WOLFSSL_TYPES
#ifndef byte
/* If using C++ C17 or later and getting:
* "error: reference to 'byte' is ambiguous", this is caused by
* cstddef conflict with "std::byte" in
* "enum class byte : unsigned char {};".
* This can occur if the user application is using "std" as the
* default namespace before including wolfSSL headers.
* Workarounds: https://github.com/wolfSSL/wolfssl/issues/5400
*/
typedef unsigned char byte;
#endif
typedef signed char sword8;
typedef unsigned char word8;
#ifdef WC_16BIT_CPU
typedef int sword16;
typedef unsigned int word16;
typedef long sword32;
typedef unsigned long word32;
#else
typedef short sword16;
typedef unsigned short word16;
typedef int sword32;
typedef unsigned int word32;
#endif
typedef byte word24[3];
#endif
typedef const char wcchar[];
#ifndef WC_BITFIELD
#ifdef WOLF_C89
#define WC_BITFIELD unsigned
#else
#define WC_BITFIELD byte
#endif
#endif
#ifndef HAVE_ANONYMOUS_INLINE_AGGREGATES
/* if a version is available, pivot on the version, otherwise guess it's
* disallowed, subject to override.
*/
#if !defined(WOLF_C89) && (!defined(__STDC__) \
|| (!defined(__STDC_VERSION__) && !defined(__cplusplus)) \
|| (defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 201101L)) \
|| (defined(__cplusplus) && (__cplusplus >= 201103L)))
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
#endif
#elif ~(~HAVE_ANONYMOUS_INLINE_AGGREGATES + 1) == 1
/* forced on with empty value -- remap to 1 */
#undef HAVE_ANONYMOUS_INLINE_AGGREGATES
#define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
#elif HAVE_ANONYMOUS_INLINE_AGGREGATES
/* forced on with explicit nonzero value -- leave as-is. */
#else
/* forced off with explicit zero value -- remap to undef. */
#undef HAVE_ANONYMOUS_INLINE_AGGREGATES
#endif
#ifndef HAVE_EMPTY_AGGREGATES
/* The C standards don't define empty aggregates, but gcc and clang do.
* We need to accommodate them for one of the same reasons C++ does --
* conditionally empty aggregates, e.g. in hash.h.
*
* Nonetheless, in C++, empty aggregates wind up with size 1. If we use
* the [0] construct and the header is compiled by clang++, it warns
* "struct has size 0 in C, size 1 in C++ [-Wextern-c-compat]", despite
* the extern "C" wrapper. We sidestep this warning by recognizing
* here that C++ doesn't support truly empty aggregates. LLVM, for its
* part, deprecates compilation of C code as C++ using clang++.
*/
#if !defined(WOLF_C89) && defined(__GNUC__) && \
!defined(__STRICT_ANSI__) && \
!defined(__cplusplus) && \
defined(HAVE_ANONYMOUS_INLINE_AGGREGATES)
#define HAVE_EMPTY_AGGREGATES 1
#endif
#elif ~(~HAVE_EMPTY_AGGREGATES + 1) == 1
/* forced on with empty value -- remap to 1 */
#undef HAVE_EMPTY_AGGREGATES
#define HAVE_EMPTY_AGGREGATES 1
#elif HAVE_EMPTY_AGGREGATES
/* forced on with explicit nonzero value -- leave as-is. */
#else
/* forced off with explicit zero value -- remap to undef. */
#undef HAVE_EMPTY_AGGREGATES
#endif
#define _WOLF_AGG_DUMMY_MEMBER_HELPER2(a, b, c) a ## b ## c
#define _WOLF_AGG_DUMMY_MEMBER_HELPER(a, b, c) \
_WOLF_AGG_DUMMY_MEMBER_HELPER2(a, b, c)
#ifdef HAVE_EMPTY_AGGREGATES
/* swallow the semicolon with a zero-sized array (language extension
* specific to gcc/clang).
*/
#define WOLF_AGG_DUMMY_MEMBER \
struct { \
PRAGMA_GCC_DIAG_PUSH \
PRAGMA_GCC("GCC diagnostic ignored \"-Wpedantic\"") \
PRAGMA_CLANG_DIAG_PUSH \
PRAGMA_CLANG("clang diagnostic ignored \"-Wzero-length-array\"") \
byte _WOLF_AGG_DUMMY_MEMBER_HELPER(_wolf_L, __LINE__, \
_agg_dummy_member)[0]; \
PRAGMA_CLANG_DIAG_POP \
PRAGMA_GCC_DIAG_POP \
}
#else
/* Use a single byte with a constructed name as a dummy member -- these
* are the standard semantics of an empty structure in C++.
*/
#define WOLF_AGG_DUMMY_MEMBER char _WOLF_AGG_DUMMY_MEMBER_HELPER( \
_wolf_L, __LINE__, _agg_dummy_member)
#endif
/* helpers for stringifying the expanded value of a macro argument rather
* than its literal text:
*/
#define _WC_STRINGIFY_L2(str) #str
#define WC_STRINGIFY(str) _WC_STRINGIFY_L2(str)
/* With a true C89-dialect compiler (simulate with gcc -std=c89 -Wall
* -Wextra -pedantic), a trailing comma on the last value in an enum
* definition is a syntax error. We use this macro to accommodate that
* without disrupting clean flow/syntax when some enum values are
* preprocessor-gated.
*/
#if defined(WOLF_C89) || defined(WOLF_NO_TRAILING_ENUM_COMMAS)
#define _WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER2(a, b, c, d, e) \
a ## b ## c ## d ## e
#define _WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER(a, b, c, d, e) \
_WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER2(a, b, c, d, e)
#define WOLF_ENUM_DUMMY_LAST_ELEMENT(prefix) \
_WOLF_ENUM_DUMMY_LAST_ELEMENT_HELPER(_wolf_, prefix, _L, __LINE__, \
_enum_dummy_last_element)
#else
#define WOLF_ENUM_DUMMY_LAST_ELEMENT(prefix) /* null expansion */
#endif
/* try to set SIZEOF_LONG or SIZEOF_LONG_LONG if user didn't */
#if defined(_WIN32) || defined(HAVE_LIMITS_H)
#include <limits.h>
/* make sure both SIZEOF_LONG_LONG and SIZEOF_LONG are set,
* otherwise causes issues with CTC_SETTINGS */
#if !defined(SIZEOF_LONG_LONG) || !defined(SIZEOF_LONG)
#if !defined(SIZEOF_LONG) && defined(ULONG_MAX) && \
(ULONG_MAX == 0xffffffffUL)
#define SIZEOF_LONG 4
#endif
#if !defined(SIZEOF_LONG_LONG) && defined(ULLONG_MAX) && \
(ULLONG_MAX == 0xffffffffffffffffULL)
#define SIZEOF_LONG_LONG 8
#endif
#endif
#elif !defined(__BCPLUSPLUS__) && !defined(__EMSCRIPTEN__)
#if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
#if (defined(__alpha__) || defined(__ia64__) || \
defined(_ARCH_PPC64) || defined(__ppc64__) || \
defined(__x86_64__) || defined(__s390x__ ) || \
((defined(sun) || defined(__sun)) && \
(defined(LP64) || defined(_LP64))) || \
(defined(__riscv_xlen) && (__riscv_xlen == 64)) || \
defined(__aarch64__) || defined(__mips64) || \
(defined(__DCC__) && (defined(__LP64) || defined(__LP64__))))
/* long should be 64bit */
#define SIZEOF_LONG 8
#elif defined(__i386__) || defined(__CORTEX_M3__) || defined(__ppc__)
/* long long should be 64bit */
#define SIZEOF_LONG_LONG 8
#endif
#endif
#endif
#if (defined(_MSC_VER) && (_MSC_VER == 1200)) || /* MSVC6 */ \
(defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)) || \
defined(__BCPLUSPLUS__) || \
(defined(__WATCOMC__) && defined(__WATCOM_INT64__))
/* windows types */
#define WORD64_AVAILABLE
#define W64LIT(x) x##ui64
#define SW64LIT(x) x##i64
typedef __int64 sword64;
typedef unsigned __int64 word64;
#elif defined(__EMSCRIPTEN__)
#define WORD64_AVAILABLE
#define W64LIT(x) x##ull
#define SW64LIT(x) x##ll
typedef long long sword64;
typedef unsigned long long word64;
#elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
#define WORD64_AVAILABLE
#ifdef WOLF_C89
#define W64LIT(x) x##UL
#define SW64LIT(x) x##L
#else
#define W64LIT(x) x##ULL
#define SW64LIT(x) x##LL
#endif
typedef long sword64;
typedef unsigned long word64;
#elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8
#define WORD64_AVAILABLE
#ifdef WOLF_C89
#define W64LIT(x) x##UL
#define SW64LIT(x) x##L
#else
#define W64LIT(x) x##ULL
#define SW64LIT(x) x##LL
#endif
typedef long long sword64;
typedef unsigned long long word64;
#elif defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ == 8
#define WORD64_AVAILABLE
#ifdef WOLF_C89
#define W64LIT(x) x##UL
#define SW64LIT(x) x##L
#else
#define W64LIT(x) x##ULL
#define SW64LIT(x) x##LL
#endif
typedef long long sword64;
typedef unsigned long long word64;
#endif
#if defined(WORD64_AVAILABLE) && !defined(WC_16BIT_CPU)
/* These platforms have 64-bit CPU registers. */
#if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) || \
(defined(__mips64) && \
((defined(_ABI64) && (_MIPS_SIM == _ABI64)) || \
(defined(_ABIO64) && (_MIPS_SIM == _ABIO64)))) || \
defined(__x86_64__) || defined(_M_X64)) || \
defined(__aarch64__) || defined(__sparc64__) || defined(__s390x__ ) || \
(defined(__riscv_xlen) && (__riscv_xlen == 64)) || defined(_M_ARM64) || \
defined(__aarch64__) || defined(__ppc64__) || \
(defined(__DCC__) && (defined(__LP64) || defined(__LP64__)))
#define WC_64BIT_CPU
#elif (defined(sun) || defined(__sun)) && \
(defined(LP64) || defined(_LP64))
/* LP64 with GNU GCC compiler is reserved for when long int is 64 bits
* and int uses 32 bits. When using Solaris Studio sparc and __sparc are
* available for 32 bit detection but __sparc64__ could be missed. This
* uses LP64 for checking 64 bit CPU arch. */
#define WC_64BIT_CPU
#else
#define WC_32BIT_CPU
#endif
#if defined(NO_64BIT)
typedef word32 wolfssl_word;
#define WOLFSSL_WORD_SIZE_LOG2 2
#undef WORD64_AVAILABLE
#else
#ifdef WC_64BIT_CPU
typedef word64 wolfssl_word;
#define WOLFSSL_WORD_SIZE_LOG2 3
#else
typedef word32 wolfssl_word;
#define WOLFSSL_WORD_SIZE_LOG2 2
#ifdef WORD64_AVAILABLE
#define WOLFCRYPT_SLOW_WORD64
#endif
#endif
#endif
#elif defined(WC_16BIT_CPU)
#ifndef MICROCHIP_PIC24
#undef WORD64_AVAILABLE
#endif
typedef word16 wolfssl_word;
#define WOLFSSL_WORD_SIZE_LOG2 1
#define MP_16BIT /* for mp_int, mp_word needs to be twice as big as \
* mp_digit, no 64 bit type so make mp_digit 16 bit */
#else
#undef WORD64_AVAILABLE
typedef word32 wolfssl_word;
#define WOLFSSL_WORD_SIZE_LOG2 2
#define MP_16BIT /* for mp_int, mp_word needs to be twice as big as \
* mp_digit, no 64 bit type so make mp_digit 16 bit */
#endif
typedef struct w64wrapper {
#if defined(WORD64_AVAILABLE) && !defined(WOLFSSL_W64_WRAPPER_TEST)
word64 n;
#else
word32 n[2];
#endif /* WORD64_AVAILABLE && WOLFSSL_W64_WRAPPER_TEST */
} w64wrapper;
#ifdef WC_PTR_TYPE /* Allow user supplied type */
typedef WC_PTR_TYPE wc_ptr_t;
#elif defined(HAVE_UINTPTR_T)
#ifndef NO_STDINT_H
#include <stdint.h>
#endif
typedef uintptr_t wc_ptr_t;
#else /* fallback to architecture size_t for pointer size */
#include <stddef.h> /* included for getting size_t type */
typedef size_t wc_ptr_t;
#endif
enum {
WOLFSSL_WORD_SIZE = sizeof(wolfssl_word),
WOLFSSL_BIT_SIZE = 8,
WOLFSSL_WORD_BITS = WOLFSSL_WORD_SIZE * WOLFSSL_BIT_SIZE
};
#define WOLFSSL_MAX_8BIT 0xffU
#define WOLFSSL_MAX_16BIT 0xffffU
#define WOLFSSL_MAX_32BIT 0xffffffffU
#ifndef WC_DO_NOTHING
#define WC_DO_NOTHING do {} while (0)
#ifdef _MSC_VER
/* disable buggy MSC warning around while(0),
*"warning C4127: conditional expression is constant"
*/
#pragma warning(disable: 4127)
#endif
#endif
#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
#define INLINE WC_INLINE
#endif
/* set up rotate style */
#if ((defined(_MSC_VER) && !defined(WOLFSSL_NOT_WINDOWS_API)) || \
defined(__BCPLUSPLUS__)) && !defined(WOLFSSL_SGX) && \
!defined(INTIME_RTOS)
#define INTEL_INTRINSICS
#define FAST_ROTATE
#elif defined(__MWERKS__) && TARGET_CPU_PPC
#define PPC_INTRINSICS
#define FAST_ROTATE
#elif defined(__CCRX__)
#define FAST_ROTATE
#elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
/* GCC does peephole optimizations which should result in using rotate
instructions */
#define FAST_ROTATE
#endif
/* set up thread local storage if available */
#if defined(HAVE_THREAD_LS) && !defined(NO_THREAD_LS)
#if defined(_MSC_VER) || defined(__WATCOMC__)
#define THREAD_LS_T __declspec(thread)
/* Thread local storage only in FreeRTOS v8.2.1 and higher */
#elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
defined(WOLFSSL_ZEPHYR)
#define THREAD_LS_T
#else
#define THREAD_LS_T __thread
#endif
#else
#define THREAD_LS_T
#endif
#ifndef FALL_THROUGH
/* GCC 7 has new switch() fall-through detection */
#if defined(__GNUC__)
#if defined(fallthrough)
#define FALL_THROUGH fallthrough
#elif ((__GNUC__ > 7) || ((__GNUC__ == 7) && (__GNUC_MINOR__ >= 1)))
#define FALL_THROUGH ; __attribute__ ((fallthrough))
#elif defined(__clang__) && defined(__clang_major__) && \
(__clang_major__ >= 12)
#define FALL_THROUGH ; __attribute__ ((fallthrough))
#endif
#endif
#endif /* FALL_THROUGH */
#if !defined(FALL_THROUGH) || defined(__XC32)
/* use stub for fall through by default or for Microchip compiler */
#undef FALL_THROUGH
#define FALL_THROUGH
#endif
#define XSTR_SIZEOF(x) (sizeof(x) - 1) /* -1 to not count the null char */
#define XELEM_CNT(x) (sizeof((x))/sizeof(*(x)))
#ifdef NO_INLINE
#define WC_WUR_INT(x) (x)
#else
static WC_INLINE WARN_UNUSED_RESULT int WC_WUR_INT(int x) { return x; }
#endif
#ifdef WORD64_AVAILABLE
#define WC_MAX_UINT_OF(x) \
((x)((((word64)1 << ((sizeof(x) * (word64)CHAR_BIT) - \
(word64)1)) - (word64)1) \
| \
((word64)1 << \
((sizeof(x) * (word64)CHAR_BIT) - (word64)1))))
#define WC_MAX_SINT_OF(x) \
((x)((sword64)((((word64)1 << \
((sizeof(x) * (word64)CHAR_BIT) - \
(word64)2)) - (word64)1) \
| \
((word64)1 << \
((sizeof(x) * (word64)CHAR_BIT) - \
(word64)2)))))
#else
#define WC_MAX_UINT_OF(x) \
((x)((((word32)1 << ((sizeof(x) * (word32)CHAR_BIT) - \
(word32)1)) - (word32)1) \
| \
((word32)1 << \
((sizeof(x) * (word32)CHAR_BIT) - (word32)1))))
#define WC_MAX_SINT_OF(x) \
((x)((sword32)((((word32)1 << \
((sizeof(x) * (word32)CHAR_BIT) - \
(word32)2)) - (word32)1) \
| \
((word32)1 << \
((sizeof(x) * (word32)CHAR_BIT) - \
(word32)2)))))
#endif
#define WC_MIN_SINT_OF(x) (-WC_MAX_SINT_OF(x) - 1)
/* The _CLIP variants of the safe arithmetic macros always store a value to out,
* but if the result is too large to represent in the type, out is set to the
* largest representable value with same sign as the actual result ("clipped").
*
* The non-_CLIP variants do not store a value if the result can't be accurately
* represented, and their return values must be checked.
*
* Both _CLIP and non-_CLIP macros return 1 if the result could be represented
* by the type, and 0 if not.
*/
#define WC_SAFE_SUM_UNSIGNED_CLIP(type, in1, in2, out) \
((in2) <= (WC_MAX_UINT_OF(type) - (in1)) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
((out) = WC_MAX_UINT_OF(type), 0))
#define WC_SAFE_SUB_UNSIGNED_CLIP(type, in1, in2, out) \
((in2) <= (in1) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_UNDERFLOW] */ 1) : \
((out) = 0, 0))
#define WC_SAFE_SUM_UNSIGNED(type, in1, in2, out) WC_WUR_INT( \
((in2) <= (WC_MAX_UINT_OF(type) - (in1)) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
0))
#define WC_SAFE_SUB_UNSIGNED(type, in1, in2, out) WC_WUR_INT( \
((in2) <= (in1) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_UNDERFLOW] */ 1) : \
0))
#if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && FIPS_VERSION3_LE(6,0,0))
#define WC_SAFE_SUM_WORD32(in1, in2, out) \
WC_SAFE_SUM_UNSIGNED_CLIP(word32, in1, in2, out)
#else
#define WC_SAFE_SUM_WORD32(in1, in2, out) \
WC_SAFE_SUM_UNSIGNED(word32, in1, in2, out)
#endif
#define WC_SAFE_SUM_SIGNED_CLIP(type, in1, in2, out) \
((((in1) > 0) && ((in2) > 0)) ? \
((in2) <= WC_MAX_SINT_OF(type) - (in1) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
((out) = (type)WC_MAX_SINT_OF(type), 0)) \
: \
((((in1) < 0) && ((in2) < 0)) ? \
((in2) >= WC_MIN_SINT_OF(type) - (in1) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
((out) = (type)WC_MIN_SINT_OF(type), 0)) \
: \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1)))
#define WC_SAFE_SUB_SIGNED_CLIP(type, in1, in2, out) \
((((in1) > 0) && ((in2) < 0)) ? \
((in2) >= (in1) - WC_MAX_SINT_OF(type) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
((out) = (type)WC_MAX_SINT_OF(type), 0)) \
: \
((((in1) < 0) && ((in2) > 0)) ? \
((in2) <= (in1) - WC_MIN_SINT_OF(type) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
((out) = (type)WC_MIN_SINT_OF(type), 0)) \
: \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1)))
#define WC_SAFE_SUM_SIGNED(type, in1, in2, out) WC_WUR_INT( \
((((in1) > 0) && ((in2) > 0)) ? \
((in2) <= WC_MAX_SINT_OF(type) - (in1) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
0) \
: \
((((in1) < 0) && ((in2) < 0)) ? \
((in2) >= WC_MIN_SINT_OF(type) - (in1) ? \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
0) \
: \
((out) = (in1) + (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1))))
#define WC_SAFE_SUB_SIGNED(type, in1, in2, out) WC_WUR_INT( \
((((in1) > 0) && ((in2) < 0)) ? \
((in2) >= (in1) - WC_MAX_SINT_OF(type) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
0) \
: \
((((in1) < 0) && ((in2) > 0)) ? \
((in2) <= (in1) - WC_MIN_SINT_OF(type) ? \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1) : \
0) \
: \
((out) = (in1) - (in2), \
/* coverity[INTEGER_OVERFLOW] */ 1))))
#if defined(HAVE_IO_POOL)
WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);
WOLFSSL_API void* XREALLOC(void *p, size_t n, void* heap, int type);
WOLFSSL_API void XFREE(void *p, void* heap, int type);
#elif (defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_INTEL_QA)) || \
defined(HAVE_INTEL_QA_SYNC)
#ifndef HAVE_INTEL_QA_SYNC
#include <wolfssl/wolfcrypt/port/intel/quickassist_mem.h>
#undef USE_WOLFSSL_MEMORY
#ifdef WOLFSSL_DEBUG_MEMORY
#define XMALLOC(s, h, t) \
IntelQaMalloc((s), (h), (t), __func__, __LINE__)
#define XFREE(p, h, t) \
IntelQaFree((p), (h), (t), __func__, __LINE__)
#define XREALLOC(p, n, h, t) \
IntelQaRealloc((p), (n), (h), (t), __func__, __LINE__)
#else
#define XMALLOC(s, h, t) IntelQaMalloc((s), (h), (t))
#define XFREE(p, h, t) IntelQaFree((p), (h), (t))
#define XREALLOC(p, n, h, t) IntelQaRealloc((p), (n), (h), (t))
#endif /* WOLFSSL_DEBUG_MEMORY */
#else
#include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
#undef USE_WOLFSSL_MEMORY
#ifdef WOLFSSL_DEBUG_MEMORY
#define XMALLOC(s, h, t) \
wc_CryptoCb_IntelQaMalloc((s), (h), (t), __func__, __LINE__)
#define XFREE(p, h, t) \
wc_CryptoCb_IntelQaFree((p), (h), (t), __func__, __LINE__)
#define XREALLOC(p, n, h, t) \
wc_CryptoCb_IntelQaRealloc((p), (n), (h), (t), __func__, \
__LINE__)
#else
#define XMALLOC(s, h, t) \
wc_CryptoCb_IntelQaMalloc((s), (h), (t))
#define XFREE(p, h, t) \
wc_CryptoCb_IntelQaFree((p), (h), (t))
#define XREALLOC(p, n, h, t) \
wc_CryptoCb_IntelQaRealloc((p), (n), (h), (t))
#endif /* WOLFSSL_DEBUG_MEMORY */
#endif
#elif defined(XMALLOC_USER)
/* prototypes for user heap override functions */
#include <stddef.h> /* for size_t */
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(WOLFSSL_MEMORY_LOG)
#define XMALLOC(n, h, t) xmalloc(n, h, t, __func__, __FILE__, __LINE__)
#define XREALLOC(p, n, h, t) \
xrealloc(p, n, h, t, __func__, __FILE__, __LINE__)
#define XFREE(p, h, t) xfree(p, h, t, __func__, __FILE__, __LINE__)
/* prototypes for user heap override functions */
#include <stddef.h> /* for size_t */
#include <stdlib.h>
WOLFSSL_API void *xmalloc(size_t n, void* heap, int type,
const char* func, const char* file, unsigned int line);
WOLFSSL_API void *xrealloc(void *p, size_t n, void* heap, int type,
const char* func, const char* file, unsigned int line);
WOLFSSL_API void xfree(void *p, void* heap, int type, const char* func,
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)))
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) m2mb_os_free(xp)
#else
#define XFREE(p, h, t) do { void* xp = (p); if (xp) \
m2mb_os_free(xp); } while (0)
#endif
#define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
#elif defined(NO_WOLFSSL_MEMORY)
#ifdef WOLFSSL_NO_MALLOC
/* this platform does not support heap use */
#ifdef WOLFSSL_SMALL_STACK
#error WOLFSSL_SMALL_STACK requires a heap implementation.
#endif
#ifndef WC_NO_CONSTRUCTORS
#define WC_NO_CONSTRUCTORS
#endif
#ifdef WOLFSSL_MALLOC_CHECK
#ifndef NO_STDIO_FILESYSTEM
#include <stdio.h>
#endif
static inline void* malloc_check(size_t sz) {
fprintf(stderr, "wolfSSL_malloc failed");
return NULL;
};
#define XMALLOC(s, h, t) ((void)(h), (void)(t), malloc_check((s)))
#define XFREE(p, h, t) do { (void)(h); (void)(t); } while (0)
#define XREALLOC(p, n, h, t) ((void)(h), (void)(t), NULL)
#else
#define XMALLOC(s, h, t) ((void)(s), (void)(h), (void)(t), NULL)
#define XFREE(p, h, t) do { (void)(p); (void)(h); (void)(t); } while(0)
#define XREALLOC(p, n, h, t) ((void)(p), (void)(n), (void)(h), (void)(t), NULL)
#endif
#else
/* just use plain C stdlib stuff if desired */
#include <stdlib.h>
#define XMALLOC(s, h, t) ((void)(h), (void)(t), malloc((size_t)(s))) /* native heap */
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) do { (void)(h); (void)(t); free(p); } while (0) /* native heap */
#else
#define XFREE(p, h, t) do { void* xp = (p); (void)(h); if (xp) free(xp); } while (0) /* native heap */
#endif
#define XREALLOC(p, n, h, t) \
((void)(h), (void)(t), realloc((p), (size_t)(n))) /* native heap */
#endif
#elif defined(WOLFSSL_LINUXKM)
/* definitions are in linuxkm/linuxkm_wc_port.h */
#elif defined(WOLFSSL_BSDKM)
/* definitions are in bsdkm/bsdkm_wc_port.h */
#elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
&& !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
&& !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
&& !defined(WOLFSSL_LEANPSK) && !defined(WOLFSSL_uITRON4)
/* default C runtime, can install different routines at runtime via cbs */
#ifndef WOLFSSL_MEMORY_H
#include <wolfssl/wolfcrypt/memory.h>
#endif
#ifdef WOLFSSL_STATIC_MEMORY
#ifdef WOLFSSL_DEBUG_MEMORY
#define XMALLOC(s, h, t) \
wolfSSL_Malloc((s), (h), (t), __func__, __LINE__)
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) \
wolfSSL_Free(xp, h, t, __func__, __LINE__)
#else
#define XFREE(p, h, t) do { void* xp = (p); if (xp) \
wolfSSL_Free(xp, h, t, __func__, __LINE__); } while (0)
#endif
#define XREALLOC(p, n, h, t) \
wolfSSL_Realloc((p), (n), (h), (t), __func__, __LINE__)
#else
#define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) wolfSSL_Free(xp, h, t)
#else
#define XFREE(p, h, t) do { void* xp = (p); if (xp) \
wolfSSL_Free(xp, h, t); } while (0)
#endif
#define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
#endif /* WOLFSSL_DEBUG_MEMORY */
#elif defined(WOLFSSL_EMBOS) && !defined(XMALLOC_USER) \
&& !defined(NO_WOLFSSL_MEMORY) \
&& !defined(WOLFSSL_STATIC_MEMORY)
/* settings.h solve this case already. Avoid redefinition. */
#elif (!defined(FREERTOS) && !defined(FREERTOS_TCP)) || \
defined(WOLFSSL_TRACK_MEMORY)
#ifdef WOLFSSL_DEBUG_MEMORY
#define XMALLOC(s, h, t) ((void)(h), (void)(t), \
wolfSSL_Malloc((s), __func__, __LINE__))
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) do { (void)(h); (void)(t); \
wolfSSL_Free(xp, __func__, __LINE__); } while (0)
#else
#define XFREE(p, h, t) do { void* xp = (p); (void)(h); \
(void)(t); if (xp) wolfSSL_Free(xp, __func__, __LINE__); \
} while (0)
#endif
#define XREALLOC(p, n, h, t) ((void)(h), (void)(t), \
wolfSSL_Realloc((p), (n), __func__, __LINE__))
#else
#define XMALLOC(s, h, t) ((void)(h), (void)(t), \
wolfSSL_Malloc((s)))
#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
#define XFREE(p, h, t) do { (void)(h); (void)(t); \
wolfSSL_Free(p); } while (0)
#else
#define XFREE(p, h, t) do { void* xp = (p); (void)(h); \
(void)(t); if (xp) wolfSSL_Free(xp); } while (0)
#endif
#define XREALLOC(p, n, h, t) ((void)(h), (void)(t), \
wolfSSL_Realloc((p), (n)))
#endif /* WOLFSSL_DEBUG_MEMORY */
#endif /* WOLFSSL_STATIC_MEMORY */
#endif
#if defined(WOLFSSL_SMALL_STACK) && defined(WC_NO_CONSTRUCTORS)
#error WOLFSSL_SMALL_STACK requires constructors.
#endif
#include <wolfssl/wolfcrypt/memory.h>
/* declare/free variable handling for async and smallstack */
#ifndef WC_ALLOC_DO_ON_FAILURE
#define WC_ALLOC_DO_ON_FAILURE() WC_DO_NOTHING
#endif
#define WC_DECLARE_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
VAR_TYPE* VAR_NAME[VAR_ITEMS] = { NULL, }; \
int idx##VAR_NAME = 0, inner_idx_##VAR_NAME
#define WC_HEAP_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE) \
VAR_TYPE* VAR_NAME[VAR_ITEMS]
#define WC_ALLOC_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
for (idx##VAR_NAME=0; idx##VAR_NAME<(VAR_ITEMS); idx##VAR_NAME++) { \
(VAR_NAME)[idx##VAR_NAME] = (VAR_TYPE*)XMALLOC(VAR_SIZE, (HEAP), \
DYNAMIC_TYPE_TMP_BUFFER); \
if ((VAR_NAME)[idx##VAR_NAME] == NULL) { \
for (inner_idx_##VAR_NAME = 0; \
inner_idx_##VAR_NAME < idx##VAR_NAME; \
inner_idx_##VAR_NAME++) { \
XFREE((VAR_NAME)[inner_idx_##VAR_NAME], (HEAP), \
DYNAMIC_TYPE_TMP_BUFFER); \
(VAR_NAME)[inner_idx_##VAR_NAME] = NULL; \
} \
for (inner_idx_##VAR_NAME = idx##VAR_NAME + 1; \
inner_idx_##VAR_NAME < (VAR_ITEMS); \
inner_idx_##VAR_NAME++) { \
(VAR_NAME)[inner_idx_##VAR_NAME] = NULL; \
} \
idx##VAR_NAME = 0; \
WC_ALLOC_DO_ON_FAILURE(); \
break; \
} \
}
#define WC_CALLOC_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
do { \
WC_ALLOC_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP); \
if (idx##VAR_NAME != 0) { \
for (idx##VAR_NAME=0; \
idx##VAR_NAME<(VAR_ITEMS); \
idx##VAR_NAME++) { \
XMEMSET((VAR_NAME)[idx##VAR_NAME], 0, VAR_SIZE); \
} \
} \
} while (0)
#define WC_HEAP_ARRAY_OK(VAR_NAME) (idx##VAR_NAME != 0)
#define WC_FREE_HEAP_ARRAY(VAR_NAME, VAR_ITEMS, HEAP) \
if (WC_HEAP_ARRAY_OK(VAR_NAME)) { \
for (idx##VAR_NAME=0; idx##VAR_NAME<(VAR_ITEMS); idx##VAR_NAME++) { \
XFREE((VAR_NAME)[idx##VAR_NAME], (HEAP), DYNAMIC_TYPE_TMP_BUFFER); \
} \
idx##VAR_NAME = 0; \
}
#if defined(WOLFSSL_SMALL_STACK)
#define WC_DECLARE_VAR_IS_HEAP_ALLOC
#define WC_DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
VAR_TYPE* VAR_NAME = NULL
#define WC_VAR_OK(VAR_NAME) ((VAR_NAME) != NULL)
#define WC_ALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
do { \
(VAR_NAME) = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * (VAR_SIZE), \
(HEAP), DYNAMIC_TYPE_WOLF_BIGINT); \
if ((VAR_NAME) == NULL) { \
WC_ALLOC_DO_ON_FAILURE(); \
} \
} while (0)
#define WC_ALLOC_VAR_EX(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP, TY, ONFAIL)\
do { \
(VAR_NAME) = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * (VAR_SIZE), \
(HEAP), TY); \
if ((VAR_NAME) == NULL) { \
ONFAIL; \
} \
} while (0)
#define WC_CALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
do { \
WC_ALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP); \
XMEMSET(VAR_NAME, 0, sizeof(VAR_TYPE) * (VAR_SIZE)); \
} while (0)
#define WC_FREE_VAR(VAR_NAME, HEAP) \
XFREE(VAR_NAME, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT)
#define WC_FREE_VAR_EX(VAR_NAME, HEAP, TYPE) \
XFREE(VAR_NAME, (HEAP), TYPE)
#define WC_DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
WC_DECLARE_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP)
#define WC_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE) \
WC_HEAP_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE)
#define WC_ALLOC_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
WC_ALLOC_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP)
#define WC_CALLOC_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
WC_CALLOC_HEAP_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP)
#define WC_ARRAY_OK(VAR_NAME) WC_HEAP_ARRAY_OK(VAR_NAME)
#define WC_FREE_ARRAY(VAR_NAME, VAR_ITEMS, HEAP) \
WC_FREE_HEAP_ARRAY(VAR_NAME, VAR_ITEMS, HEAP)
#else
#undef WC_DECLARE_VAR_IS_HEAP_ALLOC
#define WC_DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
VAR_TYPE VAR_NAME[VAR_SIZE]
#define WC_ALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) WC_DO_NOTHING
#define WC_ALLOC_VAR_EX(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP, TYPE, ONFAIL)\
WC_DO_NOTHING
#define WC_VAR_OK(VAR_NAME) 1
#define WC_CALLOC_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
XMEMSET(VAR_NAME, 0, sizeof(var))
#define WC_FREE_VAR(VAR_NAME, HEAP) WC_DO_NOTHING \
/* nothing to free, its stack */
#define WC_FREE_VAR_EX(VAR_NAME, HEAP, TYPE) WC_DO_NOTHING
#define WC_DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
VAR_TYPE VAR_NAME[VAR_ITEMS][(VAR_SIZE) / sizeof(VAR_TYPE)] /* NOLINT(bugprone-sizeof-expression) */
#define WC_ARRAY_ARG(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE) \
VAR_TYPE VAR_NAME[VAR_ITEMS][(VAR_SIZE) / sizeof(VAR_TYPE)] /* NOLINT(bugprone-sizeof-expression) */
#define WC_ALLOC_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
WC_DO_NOTHING
#define WC_CALLOC_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
XMEMSET(VAR_NAME, 0, sizeof(VAR_NAME))
#define WC_ARRAY_OK(VAR_NAME) 1
#define WC_FREE_ARRAY(VAR_NAME, VAR_ITEMS, HEAP) WC_DO_NOTHING \
/* nothing to free, its stack */
#endif
#if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
/* These are here for the FIPS code that can't be changed.
* New definitions don't need to be added here. */
#define DECLARE_VAR WC_DECLARE_VAR
#define DECLARE_ARRAY WC_DECLARE_ARRAY
#define FREE_VAR WC_FREE_VAR
#define FREE_ARRAY WC_FREE_ARRAY
#define DECLARE_ARRAY_DYNAMIC_DEC WC_DECLARE_HEAP_ARRAY
#define DECLARE_ARRAY_DYNAMIC_EXE WC_ALLOC_HEAP_ARRAY
#define FREE_ARRAY_DYNAMIC WC_FREE_HEAP_ARRAY
#endif /* HAVE_FIPS */
#if !defined(USE_WOLF_STRTOK) && \
((defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) || \
defined(WOLFSSL_TIRTOS) || defined(WOLF_C99))
#define USE_WOLF_STRTOK
#endif
#if !defined(USE_WOLF_STRSEP) && (defined(WOLF_C89) || defined(WOLF_C99))
#define USE_WOLF_STRSEP
#endif
#if !defined(XSTRLCPY) && !defined(USE_WOLF_STRLCPY)
#define USE_WOLF_STRLCPY
#endif
#if !defined(XSTRLCAT) && !defined(USE_WOLF_STRLCAT)
#define USE_WOLF_STRLCAT
#endif
#ifndef STRING_USER
#ifndef NO_STRING_H
#include <string.h>
#endif
#define XMEMCPY(d,s,l) memcpy((d),(s),(l))
#define XMEMSET(b,c,l) memset((b),(c),(l))
#define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n))
#define XMEMMOVE(d,s,l) memmove((d),(s),(l))
#define XSTRLEN(s1) strlen((s1))
#define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n))
/* strstr, strncmp, strcmp, and strncat only used by wolfSSL proper,
* not required for wolfCrypt only */
#define XSTRSTR(s1,s2) strstr((s1),(s2))
#define XSTRNSTR(s1,s2,n) wolfSSL_strnstr((s1),(s2),(n))
#define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
#define XSTRCMP(s1,s2) strcmp((s1),(s2))
#define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n))
#ifdef USE_WOLF_STRSEP
#define XSTRSEP(s1,d) wc_strsep((s1),(d))
#else
#define XSTRSEP(s1,d) strsep((s1),(d))
#endif
#ifndef XSTRCASECMP
#if (defined(MICROCHIP_MPLAB_HARMONY) || defined(MICROCHIP_PIC32)) && \
(__XC32_VERSION >= 1000) && (__XC32_VERSION < 4000)
/* XC32 supports str[n]casecmp in version >= 1.0 through 4.0. */
#define XSTRCASECMP(s1,s2) strcasecmp((s1),(s2))
#elif defined(MICROCHIP_MPLAB_HARMONY) || defined(MICROCHIP_PIC32) || \
defined(WOLFSSL_TIRTOS) || defined(WOLFSSL_ZEPHYR) || \
defined(MICROCHIP_PIC24)
/* XC32 version < 1.0 does not support strcasecmp. */
#define USE_WOLF_STRCASECMP
#elif defined(USE_WINDOWS_API) || defined(FREERTOS_TCP_WINSIM)
#define XSTRCASECMP(s1,s2) _stricmp((s1),(s2))
#else
#if defined(HAVE_STRINGS_H) && defined(WOLF_C99) && \
!defined(WOLFSSL_SGX)
#include <strings.h>
#endif
#if defined(WOLFSSL_DEOS)
#define XSTRCASECMP(s1,s2) stricmp((s1),(s2))
#elif defined(WOLFSSL_CMSIS_RTOSv2) || defined(WOLFSSL_AZSPHERE) \
|| defined(WOLF_C89)
#define USE_WOLF_STRCASECMP
#elif defined(WOLF_C89)