types.h 41 KB

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