types.h
Go to the documentation of this file.
1 /* types.h
2  *
3  * Copyright (C) 2006-2020 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 
26 #ifndef WOLF_CRYPT_TYPES_H
27 #define WOLF_CRYPT_TYPES_H
28 
29  #include <wolfssl/wolfcrypt/settings.h>
31 
32  #ifdef __cplusplus
33  extern "C" {
34  #endif
35 
36 
37  #define WOLFSSL_ABI
38  /* Tag for all the APIs that are a part of the fixed ABI. */
39 
40  /*
41  * This struct is used multiple time by other structs and
42  * needs to be defined somwhere that all structs can import
43  * (with minimal depencencies).
44  */
45  #if defined(HAVE_EX_DATA) || defined(FORTRESS)
46  typedef struct WOLFSSL_CRYPTO_EX_DATA {
47  void* ex_data[MAX_EX_DATA];
48  } WOLFSSL_CRYPTO_EX_DATA;
49  #endif
50 
51  #if defined(WORDS_BIGENDIAN)
52  #define BIG_ENDIAN_ORDER
53  #endif
54 
55  #ifndef BIG_ENDIAN_ORDER
56  #define LITTLE_ENDIAN_ORDER
57  #endif
58 
59  #ifndef WOLFSSL_TYPES
60  #ifndef byte
61  typedef unsigned char byte;
62  #endif
63  #ifdef WC_16BIT_CPU
64  typedef unsigned int word16;
65  typedef unsigned long word32;
66  #else
67  typedef unsigned short word16;
68  typedef unsigned int word32;
69  #endif
70  typedef byte word24[3];
71  #endif
72 
73 
74  /* constant pointer to a constant char */
75  #ifdef WOLFSSL_NO_CONSTCHARCONST
76  typedef const char* wcchar;
77  #else
78  typedef const char* const wcchar;
79  #endif
80 
81 
82  /* try to set SIZEOF_LONG or SIZEOF_LONG_LONG if user didn't */
83  #if defined(_MSC_VER) || defined(HAVE_LIMITS_H)
84  #if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
85  #include <limits.h>
86  #if defined(ULONG_MAX) && (ULONG_MAX == 0xffffffffUL)
87  #define SIZEOF_LONG 4
88  #endif
89  #if defined(ULLONG_MAX) && (ULLONG_MAX == 0xffffffffffffffffULL)
90  #define SIZEOF_LONG_LONG 8
91  #endif
92  #endif
93  #elif !defined(__BCPLUSPLUS__) && !defined(__EMSCRIPTEN__)
94  #if !defined(SIZEOF_LONG_LONG) && !defined(SIZEOF_LONG)
95  #if (defined(__alpha__) || defined(__ia64__) || \
96  defined(_ARCH_PPC64) || defined(__mips64) || \
97  defined(__x86_64__) || defined(__s390x__ ) || \
98  ((defined(sun) || defined(__sun)) && \
99  (defined(LP64) || defined(_LP64))))
100  /* long should be 64bit */
101  #define SIZEOF_LONG 8
102  #elif defined(__i386__) || defined(__CORTEX_M3__)
103  /* long long should be 64bit */
104  #define SIZEOF_LONG_LONG 8
105  #endif
106  #endif
107  #endif
108 
109  #if defined(_MSC_VER) || defined(__BCPLUSPLUS__)
110  #define WORD64_AVAILABLE
111  #define W64LIT(x) x##ui64
112  typedef unsigned __int64 word64;
113  #elif defined(__EMSCRIPTEN__)
114  #define WORD64_AVAILABLE
115  #define W64LIT(x) x##ull
116  typedef unsigned long long word64;
117  #elif defined(SIZEOF_LONG) && SIZEOF_LONG == 8
118  #define WORD64_AVAILABLE
119  #define W64LIT(x) x##LL
120  typedef unsigned long word64;
121  #elif defined(SIZEOF_LONG_LONG) && SIZEOF_LONG_LONG == 8
122  #define WORD64_AVAILABLE
123  #define W64LIT(x) x##LL
124  typedef unsigned long long word64;
125  #elif defined(__SIZEOF_LONG_LONG__) && __SIZEOF_LONG_LONG__ == 8
126  #define WORD64_AVAILABLE
127  #define W64LIT(x) x##LL
128  typedef unsigned long long word64;
129  #endif
130 
131 #if !defined(NO_64BIT) && defined(WORD64_AVAILABLE) && !defined(WC_16BIT_CPU)
132  /* These platforms have 64-bit CPU registers. */
133  #if (defined(__alpha__) || defined(__ia64__) || defined(_ARCH_PPC64) || \
134  defined(__mips64) || defined(__x86_64__) || defined(_M_X64)) || \
135  defined(__aarch64__) || defined(__sparc64__) || defined(__s390x__ ) || \
136  (defined(__riscv_xlen) && (__riscv_xlen == 64))
137  typedef word64 wolfssl_word;
138  #define WC_64BIT_CPU
139  #elif (defined(sun) || defined(__sun)) && \
140  (defined(LP64) || defined(_LP64))
141  /* LP64 with GNU GCC compiler is reserved for when long int is 64 bits
142  * and int uses 32 bits. When using Solaris Studio sparc and __sparc are
143  * available for 32 bit detection but __sparc64__ could be missed. This
144  * uses LP64 for checking 64 bit CPU arch. */
145  typedef word64 wolfssl_word;
146  #define WC_64BIT_CPU
147  #else
148  typedef word32 wolfssl_word;
149  #ifdef WORD64_AVAILABLE
150  #define WOLFCRYPT_SLOW_WORD64
151  #endif
152  #endif
153 
154 #elif defined(WC_16BIT_CPU)
155  #undef WORD64_AVAILABLE
156  typedef word16 wolfssl_word;
157  #define MP_16BIT /* for mp_int, mp_word needs to be twice as big as
158  mp_digit, no 64 bit type so make mp_digit 16 bit */
159 
160 #else
161  #undef WORD64_AVAILABLE
162  typedef word32 wolfssl_word;
163  #define MP_16BIT /* for mp_int, mp_word needs to be twice as big as
164  mp_digit, no 64 bit type so make mp_digit 16 bit */
165 #endif
166 
167  enum {
168  WOLFSSL_WORD_SIZE = sizeof(wolfssl_word),
169  WOLFSSL_BIT_SIZE = 8,
170  WOLFSSL_WORD_BITS = WOLFSSL_WORD_SIZE * WOLFSSL_BIT_SIZE
171  };
172 
173  #define WOLFSSL_MAX_16BIT 0xffffU
174 
175  /* use inlining if compiler allows */
176  #ifndef WC_INLINE
177  #ifndef NO_INLINE
178  #ifdef _MSC_VER
179  #define WC_INLINE __inline
180  #elif defined(__GNUC__)
181  #ifdef WOLFSSL_VXWORKS
182  #define WC_INLINE __inline__
183  #else
184  #define WC_INLINE inline
185  #endif
186  #elif defined(__IAR_SYSTEMS_ICC__)
187  #define WC_INLINE inline
188  #elif defined(THREADX)
189  #define WC_INLINE _Inline
190  #elif defined(__ghc__)
191  #ifndef __cplusplus
192  #define WC_INLINE __inline
193  #else
194  #define WC_INLINE inline
195  #endif
196  #else
197  #define WC_INLINE
198  #endif
199  #else
200  #define WC_INLINE
201  #endif
202  #endif
203 
204  #if defined(HAVE_FIPS) || defined(HAVE_SELFTEST)
205  #define INLINE WC_INLINE
206  #endif
207 
208 
209  /* set up rotate style */
210  #if (defined(_MSC_VER) || defined(__BCPLUSPLUS__)) && \
211  !defined(WOLFSSL_SGX) && !defined(INTIME_RTOS)
212  #define INTEL_INTRINSICS
213  #define FAST_ROTATE
214  #elif defined(__MWERKS__) && TARGET_CPU_PPC
215  #define PPC_INTRINSICS
216  #define FAST_ROTATE
217  #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
218  /* GCC does peephole optimizations which should result in using rotate
219  instructions */
220  #define FAST_ROTATE
221  #endif
222 
223 
224  /* set up thread local storage if available */
225  #ifdef HAVE_THREAD_LS
226  #if defined(_MSC_VER)
227  #define THREAD_LS_T __declspec(thread)
228  /* Thread local storage only in FreeRTOS v8.2.1 and higher */
229  #elif defined(FREERTOS) || defined(FREERTOS_TCP) || \
230  defined(WOLFSSL_ZEPHYR)
231  #define THREAD_LS_T
232  #else
233  #define THREAD_LS_T __thread
234  #endif
235  #else
236  #define THREAD_LS_T
237  #endif
238 
239  /* GCC 7 has new switch() fall-through detection */
240  /* default to FALL_THROUGH stub */
241  #ifndef FALL_THROUGH
242  #define FALL_THROUGH
243 
244  #if defined(__GNUC__)
245  #if ((__GNUC__ > 7) || ((__GNUC__ == 7) && (__GNUC_MINOR__ >= 1)))
246  #undef FALL_THROUGH
247  #define FALL_THROUGH __attribute__ ((fallthrough));
248  #endif
249  #endif
250  #endif /* FALL_THROUGH */
251 
252  /* Micrium will use Visual Studio for compilation but not the Win32 API */
253  #if defined(_WIN32) && !defined(MICRIUM) && !defined(FREERTOS) && \
254  !defined(FREERTOS_TCP) && !defined(EBSNET) && \
255  !defined(WOLFSSL_UTASKER) && !defined(INTIME_RTOS)
256  #define USE_WINDOWS_API
257  #endif
258 
259  #define XSTR_SIZEOF(x) (sizeof(x) - 1) /* -1 to not count the null char */
260 
261  /* idea to add global alloc override by Moises Guimaraes */
262  /* default to libc stuff */
263  /* XREALLOC is used once in normal math lib, not in fast math lib */
264  /* XFREE on some embedded systems doesn't like free(0) so test */
265  #if defined(HAVE_IO_POOL)
266  WOLFSSL_API void* XMALLOC(size_t n, void* heap, int type);
267  WOLFSSL_API void* XREALLOC(void *p, size_t n, void* heap, int type);
268  WOLFSSL_API void XFREE(void *p, void* heap, int type);
269  #elif (defined(WOLFSSL_ASYNC_CRYPT) && defined(HAVE_INTEL_QA)) || \
270  defined(HAVE_INTEL_QA_SYNC)
271  #ifndef HAVE_INTEL_QA_SYNC
272  #include <wolfssl/wolfcrypt/port/intel/quickassist_mem.h>
273  #undef USE_WOLFSSL_MEMORY
274  #ifdef WOLFSSL_DEBUG_MEMORY
275  #define XMALLOC(s, h, t) IntelQaMalloc((s), (h), (t), __func__, __LINE__)
276  #define XFREE(p, h, t) IntelQaFree((p), (h), (t), __func__, __LINE__)
277  #define XREALLOC(p, n, h, t) IntelQaRealloc((p), (n), (h), (t), __func__, __LINE__)
278  #else
279  #define XMALLOC(s, h, t) IntelQaMalloc((s), (h), (t))
280  #define XFREE(p, h, t) IntelQaFree((p), (h), (t))
281  #define XREALLOC(p, n, h, t) IntelQaRealloc((p), (n), (h), (t))
282  #endif /* WOLFSSL_DEBUG_MEMORY */
283  #else
284  #include <wolfssl/wolfcrypt/port/intel/quickassist_sync.h>
285  #undef USE_WOLFSSL_MEMORY
286  #ifdef WOLFSSL_DEBUG_MEMORY
287  #define XMALLOC(s, h, t) wc_CryptoCb_IntelQaMalloc((s), (h), (t), __func__, __LINE__)
288  #define XFREE(p, h, t) wc_CryptoCb_IntelQaFree((p), (h), (t), __func__, __LINE__)
289  #define XREALLOC(p, n, h, t) wc_CryptoCb_IntelQaRealloc((p), (n), (h), (t), __func__, __LINE__)
290  #else
291  #define XMALLOC(s, h, t) wc_CryptoCb_IntelQaMalloc((s), (h), (t))
292  #define XFREE(p, h, t) wc_CryptoCb_IntelQaFree((p), (h), (t))
293  #define XREALLOC(p, n, h, t) wc_CryptoCb_IntelQaRealloc((p), (n), (h), (t))
294  #endif /* WOLFSSL_DEBUG_MEMORY */
295  #endif
296  #elif defined(XMALLOC_USER)
297  /* prototypes for user heap override functions */
298  #include <stddef.h> /* for size_t */
299  extern void *XMALLOC(size_t n, void* heap, int type);
300  extern void *XREALLOC(void *p, size_t n, void* heap, int type);
301  extern void XFREE(void *p, void* heap, int type);
302  #elif defined(WOLFSSL_MEMORY_LOG)
303  #define XMALLOC(n, h, t) xmalloc(n, h, t, __func__, __FILE__, __LINE__)
304  #define XREALLOC(p, n, h, t) xrealloc(p, n, h, t, __func__, __FILE__, __LINE__)
305  #define XFREE(p, h, t) xfree(p, h, t, __func__, __FILE__, __LINE__)
306 
307  /* prototypes for user heap override functions */
308  #include <stddef.h> /* for size_t */
309  #include <stdlib.h>
310  WOLFSSL_API void *xmalloc(size_t n, void* heap, int type,
311  const char* func, const char* file, unsigned int line);
312  WOLFSSL_API void *xrealloc(void *p, size_t n, void* heap, int type,
313  const char* func, const char* file, unsigned int line);
314  WOLFSSL_API void xfree(void *p, void* heap, int type, const char* func,
315  const char* file, unsigned int line);
316  #elif defined(XMALLOC_OVERRIDE)
317  /* override the XMALLOC, XFREE and XREALLOC macros */
318  #elif defined(WOLFSSL_TELIT_M2MB)
319  /* Telit M2MB SDK requires use m2mb_os API's, not std malloc/free */
320  /* Use of malloc/free will cause CPU reboot */
321  #define XMALLOC(s, h, t) ((void)h, (void)t, m2mb_os_malloc((s)))
322  #define XFREE(p, h, t) {void* xp = (p); if((xp)) m2mb_os_free((xp));}
323  #define XREALLOC(p, n, h, t) m2mb_os_realloc((p), (n))
324 
325  #elif defined(NO_WOLFSSL_MEMORY)
326  #ifdef WOLFSSL_NO_MALLOC
327  /* this platform does not support heap use */
328  #ifdef WOLFSSL_MALLOC_CHECK
329  #include <stdio.h>
330  static inline void* malloc_check(size_t sz) {
331  printf("wolfSSL_malloc failed");
332  return NULL;
333  };
334  #define XMALLOC(s, h, t) malloc_check((s))
335  #define XFREE(p, h, t)
336  #define XREALLOC(p, n, h, t) (NULL)
337  #else
338  #define XMALLOC(s, h, t) (NULL)
339  #define XFREE(p, h, t)
340  #define XREALLOC(p, n, h, t) (NULL)
341  #endif
342  #else
343  /* just use plain C stdlib stuff if desired */
344  #include <stdlib.h>
345  #define XMALLOC(s, h, t) malloc((s))
346  #define XFREE(p, h, t) {void* xp = (p); if((xp)) free((xp));}
347  #define XREALLOC(p, n, h, t) realloc((p), (n))
348  #endif
349  #elif !defined(MICRIUM_MALLOC) && !defined(EBSNET) \
350  && !defined(WOLFSSL_SAFERTOS) && !defined(FREESCALE_MQX) \
351  && !defined(FREESCALE_KSDK_MQX) && !defined(FREESCALE_FREE_RTOS) \
352  && !defined(WOLFSSL_LEANPSK) && !defined(WOLFSSL_uITRON4)
353  /* default C runtime, can install different routines at runtime via cbs */
354  #include <wolfssl/wolfcrypt/memory.h>
355  #ifdef WOLFSSL_STATIC_MEMORY
356  #ifdef WOLFSSL_DEBUG_MEMORY
357  #define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t), __func__, __LINE__)
358  #define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t), __func__, __LINE__);}
359  #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t), __func__, __LINE__)
360  #else
361  #define XMALLOC(s, h, t) wolfSSL_Malloc((s), (h), (t))
362  #define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), (h), (t));}
363  #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), (h), (t))
364  #endif /* WOLFSSL_DEBUG_MEMORY */
365  #elif !defined(FREERTOS) && !defined(FREERTOS_TCP)
366  #ifdef WOLFSSL_DEBUG_MEMORY
367  #define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s), __func__, __LINE__))
368  #define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp), __func__, __LINE__);}
369  #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n), __func__, __LINE__)
370  #else
371  #define XMALLOC(s, h, t) ((void)h, (void)t, wolfSSL_Malloc((s)))
372  #define XFREE(p, h, t) {void* xp = (p); if((xp)) wolfSSL_Free((xp));}
373  #define XREALLOC(p, n, h, t) wolfSSL_Realloc((p), (n))
374  #endif /* WOLFSSL_DEBUG_MEMORY */
375  #endif /* WOLFSSL_STATIC_MEMORY */
376  #endif
377 
378  /* declare/free variable handling for async */
379  #ifdef WOLFSSL_ASYNC_CRYPT
380  #define DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
381  VAR_TYPE* VAR_NAME = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT);
382  #define DECLARE_VAR_INIT(VAR_NAME, VAR_TYPE, VAR_SIZE, INIT_VALUE, HEAP) \
383  VAR_TYPE* VAR_NAME = ({ \
384  VAR_TYPE* ptr = (VAR_TYPE*)XMALLOC(sizeof(VAR_TYPE) * VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT); \
385  if (ptr && INIT_VALUE) { \
386  XMEMCPY(ptr, INIT_VALUE, sizeof(VAR_TYPE) * VAR_SIZE); \
387  } \
388  ptr; \
389  })
390  #define DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
391  VAR_TYPE* VAR_NAME[VAR_ITEMS]; \
392  int idx##VAR_NAME; \
393  for (idx##VAR_NAME=0; idx##VAR_NAME<VAR_ITEMS; idx##VAR_NAME++) { \
394  VAR_NAME[idx##VAR_NAME] = (VAR_TYPE*)XMALLOC(VAR_SIZE, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT); \
395  }
396  #define FREE_VAR(VAR_NAME, HEAP) \
397  XFREE(VAR_NAME, (HEAP), DYNAMIC_TYPE_WOLF_BIGINT);
398  #define FREE_ARRAY(VAR_NAME, VAR_ITEMS, HEAP) \
399  for (idx##VAR_NAME=0; idx##VAR_NAME<VAR_ITEMS; idx##VAR_NAME++) { \
400  XFREE(VAR_NAME[idx##VAR_NAME], (HEAP), DYNAMIC_TYPE_WOLF_BIGINT); \
401  }
402 
403  #define DECLARE_ARRAY_DYNAMIC_DEC(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
404  DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP)
405  #define DECLARE_ARRAY_DYNAMIC_EXE(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP)
406  #define FREE_ARRAY_DYNAMIC(VAR_NAME, VAR_ITEMS, HEAP) \
407  FREE_ARRAY(VAR_NAME, VAR_ITEMS, HEAP)
408  #else
409  #define DECLARE_VAR(VAR_NAME, VAR_TYPE, VAR_SIZE, HEAP) \
410  VAR_TYPE VAR_NAME[VAR_SIZE]
411  #define DECLARE_VAR_INIT(VAR_NAME, VAR_TYPE, VAR_SIZE, INIT_VALUE, HEAP) \
412  VAR_TYPE* VAR_NAME = (VAR_TYPE*)INIT_VALUE
413  #define DECLARE_ARRAY(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
414  VAR_TYPE VAR_NAME[VAR_ITEMS][VAR_SIZE]
415  #define FREE_VAR(VAR_NAME, HEAP) /* nothing to free, its stack */
416  #define FREE_ARRAY(VAR_NAME, VAR_ITEMS, HEAP) /* nothing to free, its stack */
417 
418  #define DECLARE_ARRAY_DYNAMIC_DEC(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
419  VAR_TYPE* VAR_NAME[VAR_ITEMS]; \
420  int idx##VAR_NAME;
421  #define DECLARE_ARRAY_DYNAMIC_EXE(VAR_NAME, VAR_TYPE, VAR_ITEMS, VAR_SIZE, HEAP) \
422  for (idx##VAR_NAME=0; idx##VAR_NAME<VAR_ITEMS; idx##VAR_NAME++) { \
423  VAR_NAME[idx##VAR_NAME] = (VAR_TYPE*)XMALLOC(VAR_SIZE, (HEAP), DYNAMIC_TYPE_TMP_BUFFER); \
424  }
425  #define FREE_ARRAY_DYNAMIC(VAR_NAME, VAR_ITEMS, HEAP) \
426  for (idx##VAR_NAME=0; idx##VAR_NAME<VAR_ITEMS; idx##VAR_NAME++) { \
427  XFREE(VAR_NAME[idx##VAR_NAME], (HEAP), DYNAMIC_TYPE_TMP_BUFFER); \
428  }
429  #endif
430 
431  #if !defined(USE_WOLF_STRTOK) && \
432  ((defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)) || \
433  defined(WOLFSSL_TIRTOS) || defined(WOLF_C99))
434  #define USE_WOLF_STRTOK
435  #endif
436  #if !defined(USE_WOLF_STRSEP) && (defined(WOLF_C99))
437  #define USE_WOLF_STRSEP
438  #endif
439 
440  #ifndef STRING_USER
441  #include <string.h>
442  #define XMEMCPY(d,s,l) memcpy((d),(s),(l))
443  #define XMEMSET(b,c,l) memset((b),(c),(l))
444  #define XMEMCMP(s1,s2,n) memcmp((s1),(s2),(n))
445  #define XMEMMOVE(d,s,l) memmove((d),(s),(l))
446 
447  #define XSTRLEN(s1) strlen((s1))
448  #define XSTRNCPY(s1,s2,n) strncpy((s1),(s2),(n))
449  /* strstr, strncmp, and strncat only used by wolfSSL proper,
450  * not required for wolfCrypt only */
451  #define XSTRSTR(s1,s2) strstr((s1),(s2))
452  #define XSTRNSTR(s1,s2,n) mystrnstr((s1),(s2),(n))
453  #define XSTRNCMP(s1,s2,n) strncmp((s1),(s2),(n))
454  #define XSTRNCAT(s1,s2,n) strncat((s1),(s2),(n))
455 
456  #ifdef USE_WOLF_STRSEP
457  #define XSTRSEP(s1,d) wc_strsep((s1),(d))
458  #else
459  #define XSTRSEP(s1,d) strsep((s1),(d))
460  #endif
461 
462  #ifndef XSTRNCASECMP
463  #if defined(MICROCHIP_PIC32) || defined(WOLFSSL_TIRTOS) || \
464  defined(WOLFSSL_ZEPHYR)
465  /* XC32 does not support strncasecmp, so use case sensitive one */
466  #define XSTRNCASECMP(s1,s2,n) strncmp((s1),(s2),(n))
467  #elif defined(USE_WINDOWS_API) || defined(FREERTOS_TCP_WINSIM)
468  #define XSTRNCASECMP(s1,s2,n) _strnicmp((s1),(s2),(n))
469  #else
470  #if defined(HAVE_STRINGS_H) && defined(WOLF_C99) && \
471  !defined(WOLFSSL_SGX)
472  #include <strings.h>
473  #endif
474  #if defined(WOLFSSL_DEOS)
475  #define XSTRNCASECMP(s1,s2,n) strnicmp((s1),(s2),(n))
476  #else
477  #define XSTRNCASECMP(s1,s2,n) strncasecmp((s1),(s2),(n))
478  #endif
479  #endif
480  #endif /* !XSTRNCASECMP */
481 
482  /* snprintf is used in asn.c for GetTimeString, PKCS7 test, and when
483  debugging is turned on */
484  #ifndef USE_WINDOWS_API
485  #ifndef XSNPRINTF
486  #if defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \
487  defined(HAVE_PKCS7)) && !defined(NO_STDIO_FILESYSTEM)
488  /* case where stdio is not included else where but is needed
489  for snprintf */
490  #include <stdio.h>
491  #endif
492  #define XSNPRINTF snprintf
493  #endif
494  #else
495  #if defined(_MSC_VER) || defined(__CYGWIN__) || defined(__MINGW32__)
496  #if defined(_MSC_VER) && (_MSC_VER >= 1900)
497  /* Beginning with the UCRT in Visual Studio 2015 and
498  Windows 10, snprintf is no longer identical to
499  _snprintf. The snprintf function behavior is now
500  C99 standard compliant. */
501  #include <stdio.h>
502  #define XSNPRINTF snprintf
503  #else
504  /* 4996 warning to use MS extensions e.g., _sprintf_s
505  instead of _snprintf */
506  #if !defined(__MINGW32__)
507  #pragma warning(disable: 4996)
508  #endif
509  static WC_INLINE
510  int xsnprintf(char *buffer, size_t bufsize,
511  const char *format, ...) {
512  va_list ap;
513  int ret;
514 
515  if ((int)bufsize <= 0) return -1;
516  va_start(ap, format);
517  ret = vsnprintf(buffer, bufsize, format, ap);
518  if (ret >= (int)bufsize)
519  ret = -1;
520  va_end(ap);
521  return ret;
522  }
523  #define XSNPRINTF xsnprintf
524  #endif /* (_MSC_VER >= 1900) */
525  #else
526  #define XSNPRINTF snprintf
527  #endif /* _MSC_VER */
528  #endif /* USE_WINDOWS_API */
529 
530  #if defined(WOLFSSL_CERT_EXT) || defined(OPENSSL_EXTRA) \
531  || defined(HAVE_ALPN)
532  /* use only Thread Safe version of strtok */
533  #if defined(USE_WOLF_STRTOK)
534  #define XSTRTOK(s1,d,ptr) wc_strtok((s1),(d),(ptr))
535  #elif defined(USE_WINDOWS_API) || defined(INTIME_RTOS)
536  #define XSTRTOK(s1,d,ptr) strtok_s((s1),(d),(ptr))
537  #else
538  #define XSTRTOK(s1,d,ptr) strtok_r((s1),(d),(ptr))
539  #endif
540  #endif
541 
542  #if defined(WOLFSSL_CERT_EXT) || defined(HAVE_OCSP) || \
543  defined(HAVE_CRL_IO) || defined(HAVE_HTTP_CLIENT) || \
544  !defined(NO_CRYPT_BENCHMARK)
545 
546  #ifndef XATOI /* if custom XATOI is not already defined */
547  #include <stdlib.h>
548  #define XATOI(s) atoi((s))
549  #endif
550  #endif
551  #endif
552 
553  #ifdef USE_WOLF_STRTOK
554  WOLFSSL_API char* wc_strtok(char *str, const char *delim, char **nextp);
555  #endif
556  #ifdef USE_WOLF_STRSEP
557  WOLFSSL_API char* wc_strsep(char **stringp, const char *delim);
558  #endif
559 
560  #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
561  !defined(NO_STDIO_FILESYSTEM)
562  #ifndef XGETENV
563  #include <stdlib.h>
564  #define XGETENV getenv
565  #endif
566  #endif /* OPENSSL_EXTRA */
567 
568  #ifndef CTYPE_USER
569  #include <ctype.h>
570  #if defined(HAVE_ECC) || defined(HAVE_OCSP) || \
571  defined(WOLFSSL_KEY_GEN) || !defined(NO_DSA)
572  #define XTOUPPER(c) toupper((c))
573  #define XISALPHA(c) isalpha((c))
574  #endif
575  /* needed by wolfSSL_check_domain_name() */
576  #define XTOLOWER(c) tolower((c))
577  #endif
578 
579 
580  /* memory allocation types for user hints */
581  enum {
582  DYNAMIC_TYPE_CA = 1,
583  DYNAMIC_TYPE_CERT = 2,
584  DYNAMIC_TYPE_KEY = 3,
585  DYNAMIC_TYPE_FILE = 4,
586  DYNAMIC_TYPE_SUBJECT_CN = 5,
587  DYNAMIC_TYPE_PUBLIC_KEY = 6,
588  DYNAMIC_TYPE_SIGNER = 7,
589  DYNAMIC_TYPE_NONE = 8,
590  DYNAMIC_TYPE_BIGINT = 9,
591  DYNAMIC_TYPE_RSA = 10,
592  DYNAMIC_TYPE_METHOD = 11,
593  DYNAMIC_TYPE_OUT_BUFFER = 12,
594  DYNAMIC_TYPE_IN_BUFFER = 13,
595  DYNAMIC_TYPE_INFO = 14,
596  DYNAMIC_TYPE_DH = 15,
597  DYNAMIC_TYPE_DOMAIN = 16,
598  DYNAMIC_TYPE_SSL = 17,
599  DYNAMIC_TYPE_CTX = 18,
600  DYNAMIC_TYPE_WRITEV = 19,
601  DYNAMIC_TYPE_OPENSSL = 20,
602  DYNAMIC_TYPE_DSA = 21,
603  DYNAMIC_TYPE_CRL = 22,
604  DYNAMIC_TYPE_REVOKED = 23,
605  DYNAMIC_TYPE_CRL_ENTRY = 24,
606  DYNAMIC_TYPE_CERT_MANAGER = 25,
607  DYNAMIC_TYPE_CRL_MONITOR = 26,
608  DYNAMIC_TYPE_OCSP_STATUS = 27,
609  DYNAMIC_TYPE_OCSP_ENTRY = 28,
610  DYNAMIC_TYPE_ALTNAME = 29,
611  DYNAMIC_TYPE_SUITES = 30,
612  DYNAMIC_TYPE_CIPHER = 31,
613  DYNAMIC_TYPE_RNG = 32,
614  DYNAMIC_TYPE_ARRAYS = 33,
615  DYNAMIC_TYPE_DTLS_POOL = 34,
616  DYNAMIC_TYPE_SOCKADDR = 35,
617  DYNAMIC_TYPE_LIBZ = 36,
618  DYNAMIC_TYPE_ECC = 37,
619  DYNAMIC_TYPE_TMP_BUFFER = 38,
620  DYNAMIC_TYPE_DTLS_MSG = 39,
621  DYNAMIC_TYPE_X509 = 40,
622  DYNAMIC_TYPE_TLSX = 41,
623  DYNAMIC_TYPE_OCSP = 42,
624  DYNAMIC_TYPE_SIGNATURE = 43,
625  DYNAMIC_TYPE_HASHES = 44,
626  DYNAMIC_TYPE_SRP = 45,
627  DYNAMIC_TYPE_COOKIE_PWD = 46,
628  DYNAMIC_TYPE_USER_CRYPTO = 47,
629  DYNAMIC_TYPE_OCSP_REQUEST = 48,
630  DYNAMIC_TYPE_X509_EXT = 49,
631  DYNAMIC_TYPE_X509_STORE = 50,
632  DYNAMIC_TYPE_X509_CTX = 51,
633  DYNAMIC_TYPE_URL = 52,
634  DYNAMIC_TYPE_DTLS_FRAG = 53,
635  DYNAMIC_TYPE_DTLS_BUFFER = 54,
636  DYNAMIC_TYPE_SESSION_TICK = 55,
637  DYNAMIC_TYPE_PKCS = 56,
638  DYNAMIC_TYPE_MUTEX = 57,
639  DYNAMIC_TYPE_PKCS7 = 58,
640  DYNAMIC_TYPE_AES_BUFFER = 59,
641  DYNAMIC_TYPE_WOLF_BIGINT = 60,
642  DYNAMIC_TYPE_ASN1 = 61,
643  DYNAMIC_TYPE_LOG = 62,
644  DYNAMIC_TYPE_WRITEDUP = 63,
645  DYNAMIC_TYPE_PRIVATE_KEY = 64,
646  DYNAMIC_TYPE_HMAC = 65,
647  DYNAMIC_TYPE_ASYNC = 66,
648  DYNAMIC_TYPE_ASYNC_NUMA = 67,
649  DYNAMIC_TYPE_ASYNC_NUMA64 = 68,
650  DYNAMIC_TYPE_CURVE25519 = 69,
651  DYNAMIC_TYPE_ED25519 = 70,
652  DYNAMIC_TYPE_SECRET = 71,
653  DYNAMIC_TYPE_DIGEST = 72,
654  DYNAMIC_TYPE_RSA_BUFFER = 73,
655  DYNAMIC_TYPE_DCERT = 74,
656  DYNAMIC_TYPE_STRING = 75,
657  DYNAMIC_TYPE_PEM = 76,
658  DYNAMIC_TYPE_DER = 77,
659  DYNAMIC_TYPE_CERT_EXT = 78,
660  DYNAMIC_TYPE_ALPN = 79,
661  DYNAMIC_TYPE_ENCRYPTEDINFO= 80,
662  DYNAMIC_TYPE_DIRCTX = 81,
663  DYNAMIC_TYPE_HASHCTX = 82,
664  DYNAMIC_TYPE_SEED = 83,
665  DYNAMIC_TYPE_SYMMETRIC_KEY= 84,
666  DYNAMIC_TYPE_ECC_BUFFER = 85,
667  DYNAMIC_TYPE_QSH = 86,
668  DYNAMIC_TYPE_SALT = 87,
669  DYNAMIC_TYPE_HASH_TMP = 88,
670  DYNAMIC_TYPE_BLOB = 89,
671  DYNAMIC_TYPE_NAME_ENTRY = 90,
672  DYNAMIC_TYPE_CURVE448 = 91,
673  DYNAMIC_TYPE_ED448 = 92,
674  DYNAMIC_TYPE_SNIFFER_SERVER = 1000,
675  DYNAMIC_TYPE_SNIFFER_SESSION = 1001,
676  DYNAMIC_TYPE_SNIFFER_PB = 1002,
677  DYNAMIC_TYPE_SNIFFER_PB_BUFFER = 1003,
678  DYNAMIC_TYPE_SNIFFER_TICKET_ID = 1004,
679  DYNAMIC_TYPE_SNIFFER_NAMED_KEY = 1005,
680  };
681 
682  /* max error buffer string size */
683  #ifndef WOLFSSL_MAX_ERROR_SZ
684  #define WOLFSSL_MAX_ERROR_SZ 80
685  #endif
686 
687  /* stack protection */
688  enum {
689  MIN_STACK_BUFFER = 8
690  };
691 
692 
693  /* Algorithm Types */
694  enum wc_AlgoType {
695  WC_ALGO_TYPE_NONE = 0,
696  WC_ALGO_TYPE_HASH = 1,
697  WC_ALGO_TYPE_CIPHER = 2,
698  WC_ALGO_TYPE_PK = 3,
699  WC_ALGO_TYPE_RNG = 4,
700  WC_ALGO_TYPE_SEED = 5,
701  WC_ALGO_TYPE_HMAC = 6,
702 
703  WC_ALGO_TYPE_MAX = WC_ALGO_TYPE_HMAC
704  };
705 
706  /* hash types */
707  enum wc_HashType {
708  #if defined(HAVE_SELFTEST) || defined(HAVE_FIPS)
709  /* In selftest build, WC_* types are not mapped to WC_HASH_TYPE types.
710  * Values here are based on old selftest hmac.h enum, with additions.
711  * These values are fixed for backwards FIPS compatibility */
712  WC_HASH_TYPE_NONE = 15,
713  WC_HASH_TYPE_MD2 = 16,
714  WC_HASH_TYPE_MD4 = 17,
715  WC_HASH_TYPE_MD5 = 0,
716  WC_HASH_TYPE_SHA = 1, /* SHA-1 (not old SHA-0) */
717  WC_HASH_TYPE_SHA224 = 8,
718  WC_HASH_TYPE_SHA256 = 2,
719  WC_HASH_TYPE_SHA384 = 5,
720  WC_HASH_TYPE_SHA512 = 4,
721  WC_HASH_TYPE_MD5_SHA = 18,
722  WC_HASH_TYPE_SHA3_224 = 10,
723  WC_HASH_TYPE_SHA3_256 = 11,
724  WC_HASH_TYPE_SHA3_384 = 12,
725  WC_HASH_TYPE_SHA3_512 = 13,
726  WC_HASH_TYPE_BLAKE2B = 14,
727  WC_HASH_TYPE_BLAKE2S = 19,
728 
729  WC_HASH_TYPE_MAX = WC_HASH_TYPE_BLAKE2S
730  #else
731  WC_HASH_TYPE_NONE = 0,
732  WC_HASH_TYPE_MD2 = 1,
733  WC_HASH_TYPE_MD4 = 2,
734  WC_HASH_TYPE_MD5 = 3,
735  WC_HASH_TYPE_SHA = 4, /* SHA-1 (not old SHA-0) */
736  WC_HASH_TYPE_SHA224 = 5,
737  WC_HASH_TYPE_SHA256 = 6,
738  WC_HASH_TYPE_SHA384 = 7,
739  WC_HASH_TYPE_SHA512 = 8,
740  WC_HASH_TYPE_MD5_SHA = 9,
741  WC_HASH_TYPE_SHA3_224 = 10,
742  WC_HASH_TYPE_SHA3_256 = 11,
743  WC_HASH_TYPE_SHA3_384 = 12,
744  WC_HASH_TYPE_SHA3_512 = 13,
745  WC_HASH_TYPE_BLAKE2B = 14,
746  WC_HASH_TYPE_BLAKE2S = 15,
747 
748  WC_HASH_TYPE_MAX = WC_HASH_TYPE_BLAKE2S
749  #endif /* HAVE_SELFTEST */
750  };
751 
752  /* cipher types */
753  enum wc_CipherType {
754  WC_CIPHER_NONE = 0,
755  WC_CIPHER_AES = 1,
756  WC_CIPHER_AES_CBC = 2,
757  WC_CIPHER_AES_GCM = 3,
758  WC_CIPHER_AES_CTR = 4,
759  WC_CIPHER_AES_XTS = 5,
760  WC_CIPHER_AES_CFB = 6,
761  WC_CIPHER_DES3 = 7,
762  WC_CIPHER_DES = 8,
763  WC_CIPHER_CHACHA = 9,
764  WC_CIPHER_HC128 = 10,
765  WC_CIPHER_IDEA = 11,
766 
767  WC_CIPHER_MAX = WC_CIPHER_HC128
768  };
769 
770  /* PK=public key (asymmetric) based algorithms */
771  enum wc_PkType {
772  WC_PK_TYPE_NONE = 0,
773  WC_PK_TYPE_RSA = 1,
774  WC_PK_TYPE_DH = 2,
775  WC_PK_TYPE_ECDH = 3,
776  WC_PK_TYPE_ECDSA_SIGN = 4,
777  WC_PK_TYPE_ECDSA_VERIFY = 5,
778  WC_PK_TYPE_ED25519 = 6,
779  WC_PK_TYPE_CURVE25519 = 7,
780  WC_PK_TYPE_RSA_KEYGEN = 8,
781  WC_PK_TYPE_EC_KEYGEN = 9,
782 
783  WC_PK_TYPE_MAX = WC_PK_TYPE_EC_KEYGEN
784  };
785 
786 
787  /* settings detection for compile vs runtime math incompatibilities */
788  enum {
789  #if !defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
790  CTC_SETTINGS = 0x0
791  #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
792  CTC_SETTINGS = 0x1
793  #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
794  CTC_SETTINGS = 0x2
795  #elif !defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
796  CTC_SETTINGS = 0x4
797  #elif defined(USE_FAST_MATH) && !defined(SIZEOF_LONG) && !defined(SIZEOF_LONG_LONG)
798  CTC_SETTINGS = 0x8
799  #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG) && (SIZEOF_LONG == 8)
800  CTC_SETTINGS = 0x10
801  #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 8)
802  CTC_SETTINGS = 0x20
803  #elif defined(USE_FAST_MATH) && defined(SIZEOF_LONG_LONG) && (SIZEOF_LONG_LONG == 4)
804  CTC_SETTINGS = 0x40
805  #else
806  #error "bad math long / long long settings"
807  #endif
808  };
809 
810 
811  WOLFSSL_API word32 CheckRunTimeSettings(void);
812 
813  /* If user uses RSA, DH, DSA, or ECC math lib directly then fast math and long
814  types need to match at compile time and run time, CheckCtcSettings will
815  return 1 if a match otherwise 0 */
816  #define CheckCtcSettings() (CTC_SETTINGS == CheckRunTimeSettings())
817 
818  /* invalid device id */
819  #define INVALID_DEVID -2
820 
821 
822  /* AESNI requires alignment and ARMASM gains some performance from it
823  * Xilinx RSA operations require alignment */
824  #if defined(WOLFSSL_AESNI) || defined(WOLFSSL_ARMASM) || \
825  defined(USE_INTEL_SPEEDUP) || defined(WOLFSSL_AFALG_XILINX)
826  #if !defined(ALIGN16)
827  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
828  #define ALIGN16 __attribute__ ( (aligned (16)))
829  #elif defined(_MSC_VER)
830  /* disable align warning, we want alignment ! */
831  #pragma warning(disable: 4324)
832  #define ALIGN16 __declspec (align (16))
833  #else
834  #define ALIGN16
835  #endif
836  #endif /* !ALIGN16 */
837 
838  #if !defined (ALIGN32)
839  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
840  #define ALIGN32 __attribute__ ( (aligned (32)))
841  #elif defined(_MSC_VER)
842  /* disable align warning, we want alignment ! */
843  #pragma warning(disable: 4324)
844  #define ALIGN32 __declspec (align (32))
845  #else
846  #define ALIGN32
847  #endif
848  #endif /* !ALIGN32 */
849 
850  #if !defined(ALIGN64)
851  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
852  #define ALIGN64 __attribute__ ( (aligned (64)))
853  #elif defined(_MSC_VER)
854  /* disable align warning, we want alignment ! */
855  #pragma warning(disable: 4324)
856  #define ALIGN64 __declspec (align (64))
857  #else
858  #define ALIGN64
859  #endif
860  #endif /* !ALIGN64 */
861 
862  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
863  #define ALIGN128 __attribute__ ( (aligned (128)))
864  #elif defined(_MSC_VER)
865  /* disable align warning, we want alignment ! */
866  #pragma warning(disable: 4324)
867  #define ALIGN128 __declspec (align (128))
868  #else
869  #define ALIGN128
870  #endif
871 
872  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
873  #define ALIGN256 __attribute__ ( (aligned (256)))
874  #elif defined(_MSC_VER)
875  /* disable align warning, we want alignment ! */
876  #pragma warning(disable: 4324)
877  #define ALIGN256 __declspec (align (256))
878  #else
879  #define ALIGN256
880  #endif
881 
882  #else
883  #ifndef ALIGN16
884  #define ALIGN16
885  #endif
886  #ifndef ALIGN32
887  #define ALIGN32
888  #endif
889  #ifndef ALIGN64
890  #define ALIGN64
891  #endif
892  #ifndef ALIGN128
893  #define ALIGN128
894  #endif
895  #ifndef ALIGN256
896  #define ALIGN256
897  #endif
898  #endif /* WOLFSSL_AESNI || WOLFSSL_ARMASM */
899 
900 
901  #ifndef TRUE
902  #define TRUE 1
903  #endif
904  #ifndef FALSE
905  #define FALSE 0
906  #endif
907 
908 
909  #if defined(HAVE_STACK_SIZE)
910  #define EXIT_TEST(ret) return (void*)((size_t)(ret))
911  #else
912  #define EXIT_TEST(ret) return ret
913  #endif
914 
915 
916  #if (defined(__IAR_SYSTEMS_ICC__) && (__IAR_SYSTEMS_ICC__ > 8)) || \
917  defined(__GNUC__)
918  #define WOLFSSL_PACK __attribute__ ((packed))
919  #else
920  #define WOLFSSL_PACK
921  #endif
922 
923  #ifndef __GNUC_PREREQ
924  #if defined(__GNUC__) && defined(__GNUC_MINOR__)
925  #define __GNUC_PREREQ(maj, min) \
926  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
927  #else
928  #define __GNUC_PREREQ(maj, min) (0) /* not GNUC */
929  #endif
930  #endif
931 
932  #if defined(__IAR_SYSTEMS_ICC__) || defined(__GNUC__)
933  #define WC_NORETURN __attribute__((noreturn))
934  #else
935  #define WC_NORETURN
936  #endif
937 
938  #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY) || \
939  defined(WOLFSSL_DEBUG_MATH) || defined(DEBUG_WOLFSSL) || \
940  defined(WOLFSSL_PUBLIC_MP) || defined(OPENSSL_EXTRA) || \
941  (defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT))
942  #undef WC_MP_TO_RADIX
943  #define WC_MP_TO_RADIX
944  #endif
945 
946  #ifdef __cplusplus
947  } /* extern "C" */
948  #endif
949 
950 #endif /* WOLF_CRYPT_TYPES_H */
WOLFSSL_API void * XREALLOC(void *p, size_t n, void *heap, int type)
This is not actually a function, but rather a preprocessor macro, which allows the user to substitute...
Definition: memory.c:994
WOLFSSL_API void * XMALLOC(size_t n, void *heap, int type)
This is not actually a function, but rather a preprocessor macro, which allows the user to substitute...
Definition: memory.c:973
WOLFSSL_API void XFREE(void *p, void *heap, int type)
This is not actually a function, but rather a preprocessor macro, which allows the user to substitute...
Definition: memory.c:1015
WOLFSSL_API word32 CheckRunTimeSettings(void)
This function checks the compile time class settings. It is important when a user is using a wolfCryp...
Definition: integer.c:103