1
0

linuxkm_wc_port.h 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  1. /* linuxkm_wc_port.h
  2. *
  3. * Copyright (C) 2006-2024 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. /* included by wolfssl/wolfcrypt/wc_port.h */
  22. #ifndef LINUXKM_WC_PORT_H
  23. #define LINUXKM_WC_PORT_H
  24. #include <linux/version.h>
  25. #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 16, 0)
  26. #error Unsupported kernel.
  27. #endif
  28. #ifdef HAVE_CONFIG_H
  29. #ifndef PACKAGE_NAME
  30. #error wc_port.h included before config.h
  31. #endif
  32. /* config.h is autogenerated without gating, and is subject to repeat
  33. * inclusions, so gate it out here to keep autodetection masking
  34. * intact:
  35. */
  36. #undef HAVE_CONFIG_H
  37. #endif
  38. /* suppress inclusion of stdint-gcc.h to avoid conflicts with Linux native
  39. * include/linux/types.h:
  40. */
  41. #define _GCC_STDINT_H
  42. #define WC_PTR_TYPE uintptr_t
  43. /* needed to suppress inclusion of stdio.h in wolfssl/wolfcrypt/types.h */
  44. #define XSNPRINTF snprintf
  45. /* the rigmarole around kstrtoll() here is to accommodate its
  46. * warn-unused-result attribute.
  47. *
  48. * also needed to suppress inclusion of stdlib.h in
  49. * wolfssl/wolfcrypt/types.h.
  50. */
  51. #define XATOI(s) ({ \
  52. long long _xatoi_res = 0; \
  53. int _xatoi_ret = kstrtoll(s, 10, &_xatoi_res); \
  54. if (_xatoi_ret != 0) { \
  55. _xatoi_res = 0; \
  56. } \
  57. (int)_xatoi_res; \
  58. })
  59. /* Kbuild+gcc on x86 doesn't consistently honor the default ALIGN16 on stack
  60. * objects, but gives adequate alignment with "32".
  61. */
  62. #if defined(CONFIG_X86) && !defined(ALIGN16)
  63. #define ALIGN16 __attribute__ ( (aligned (32)))
  64. #endif
  65. /* kvmalloc()/kvfree() and friends added in linux commit a7c3e901 */
  66. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 12, 0)
  67. #define HAVE_KVMALLOC
  68. #endif
  69. /* kernel printf doesn't implement fp. */
  70. #ifndef WOLFSSL_NO_FLOAT_FMT
  71. #define WOLFSSL_NO_FLOAT_FMT
  72. #endif
  73. #ifdef BUILDING_WOLFSSL
  74. #if defined(CONFIG_MIPS) && defined(HAVE_LINUXKM_PIE_SUPPORT)
  75. /* __ZBOOT__ disables some unhelpful macros around the mem*() funcs in
  76. * legacy arch/mips/include/asm/string.h
  77. */
  78. #define __ZBOOT__
  79. #define memcmp __builtin_memcmp
  80. #define __ARCH_MEMCMP_NO_REDIRECT
  81. #define __ARCH_MEMCPY_NO_REDIRECT
  82. #define __builtin_memcpy memcpy
  83. extern void *memcpy(void *dest, const void *src, unsigned int n);
  84. #define __ARCH_MEMCPY_NO_REDIRECT
  85. #define __builtin_memset memset
  86. extern void *memset(void *dest, int c, unsigned int n);
  87. #endif
  88. _Pragma("GCC diagnostic push");
  89. /* we include all the needed kernel headers with these masked out. else
  90. * there are profuse warnings.
  91. */
  92. _Pragma("GCC diagnostic ignored \"-Wunused-parameter\"");
  93. _Pragma("GCC diagnostic ignored \"-Wpointer-arith\"");
  94. _Pragma("GCC diagnostic ignored \"-Wshadow\"");
  95. _Pragma("GCC diagnostic ignored \"-Wnested-externs\"");
  96. _Pragma("GCC diagnostic ignored \"-Wredundant-decls\"");
  97. _Pragma("GCC diagnostic ignored \"-Wsign-compare\"");
  98. _Pragma("GCC diagnostic ignored \"-Wpointer-sign\"");
  99. _Pragma("GCC diagnostic ignored \"-Wbad-function-cast\"");
  100. _Pragma("GCC diagnostic ignored \"-Wdiscarded-qualifiers\"");
  101. _Pragma("GCC diagnostic ignored \"-Wtype-limits\"");
  102. _Pragma("GCC diagnostic ignored \"-Wswitch-enum\"");
  103. _Pragma("GCC diagnostic ignored \"-Wcast-function-type\""); /* needed for kernel 4.14.336 */
  104. #include <linux/kconfig.h>
  105. #if defined(__PIE__) && defined(CONFIG_ARM64)
  106. #define alt_cb_patch_nops my__alt_cb_patch_nops
  107. #endif
  108. #include <linux/kernel.h>
  109. #include <linux/ctype.h>
  110. #if defined(CONFIG_FORTIFY_SOURCE) || defined(DEBUG_LINUXKM_FORTIFY_OVERLAY)
  111. #ifdef __PIE__
  112. /* the inline definitions in fortify-string.h use non-inline
  113. * fortify_panic().
  114. */
  115. extern void __my_fortify_panic(const char *name) __noreturn __cold;
  116. #define fortify_panic __my_fortify_panic
  117. #endif
  118. /* the _FORTIFY_SOURCE macros and implementations for several string
  119. * functions are incompatible with libwolfssl, so just reimplement with
  120. * inlines and remap with macros.
  121. */
  122. #define __ARCH_STRLEN_NO_REDIRECT
  123. #define __ARCH_MEMCPY_NO_REDIRECT
  124. #define __ARCH_MEMSET_NO_REDIRECT
  125. #define __ARCH_MEMMOVE_NO_REDIRECT
  126. /* the inline definitions in fortify-string.h use non-inline
  127. * strlen().
  128. */
  129. static inline size_t strlen(const char *s) {
  130. const char *s_start = s;
  131. while (*s)
  132. ++s;
  133. return (size_t)((uintptr_t)s - (uintptr_t)s_start);
  134. }
  135. #include <linux/string.h>
  136. #undef strlen
  137. #define strlen(s) \
  138. ((__builtin_constant_p(s) && __builtin_constant_p(*(s))) ? \
  139. (sizeof(s) - 1) : strlen(s))
  140. static inline void *my_memcpy(void *dest, const void *src, size_t n) {
  141. if (! (((uintptr_t)dest | (uintptr_t)src | (uintptr_t)n)
  142. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  143. {
  144. uintptr_t *src_longs = (uintptr_t *)src,
  145. *dest_longs = (uintptr_t *)dest,
  146. *endp = (uintptr_t *)((u8 *)src + n);
  147. while (src_longs < endp)
  148. *dest_longs++ = *src_longs++;
  149. } else {
  150. u8 *src_bytes = (u8 *)src,
  151. *dest_bytes = (u8 *)dest,
  152. *endp = src_bytes + n;
  153. while (src_bytes < endp)
  154. *dest_bytes++ = *src_bytes++;
  155. }
  156. return dest;
  157. }
  158. #undef memcpy
  159. #define memcpy my_memcpy
  160. static inline void *my_memset(void *dest, int c, size_t n) {
  161. if (! (((uintptr_t)dest | (uintptr_t)n)
  162. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  163. {
  164. uintptr_t c_long = __builtin_choose_expr(
  165. sizeof(uintptr_t) == 8,
  166. (uintptr_t)(u8)c * 0x0101010101010101UL,
  167. (uintptr_t)(u8)c * 0x01010101U
  168. );
  169. uintptr_t *dest_longs = (uintptr_t *)dest,
  170. *endp = (uintptr_t *)((u8 *)dest_longs + n);
  171. while (dest_longs < endp)
  172. *dest_longs++ = c_long;
  173. } else {
  174. u8 *dest_bytes = (u8 *)dest, *endp = dest_bytes + n;
  175. while (dest_bytes < endp)
  176. *dest_bytes++ = (u8)c;
  177. }
  178. return dest;
  179. }
  180. #undef memset
  181. #define memset my_memset
  182. static inline void *my_memmove(void *dest, const void *src, size_t n) {
  183. if (! (((uintptr_t)dest | (uintptr_t)src | (uintptr_t)n)
  184. & (uintptr_t)(sizeof(uintptr_t) - 1)))
  185. {
  186. uintptr_t *src_longs = (uintptr_t *)src,
  187. *dest_longs = (uintptr_t *)dest;
  188. n >>= __builtin_choose_expr(
  189. sizeof(uintptr_t) == 8,
  190. 3U,
  191. 2U);
  192. if (src_longs < dest_longs) {
  193. uintptr_t *startp = src_longs;
  194. src_longs += n - 1;
  195. dest_longs += n - 1;
  196. while (src_longs >= startp)
  197. *dest_longs-- = *src_longs--;
  198. } else if (src_longs > dest_longs) {
  199. uintptr_t *endp = src_longs + n;
  200. while (src_longs < endp)
  201. *dest_longs++ = *src_longs++;
  202. }
  203. } else {
  204. u8 *src_bytes = (u8 *)src, *dest_bytes = (u8 *)dest;
  205. if (src_bytes < dest_bytes) {
  206. u8 *startp = src_bytes;
  207. src_bytes += n - 1;
  208. dest_bytes += n - 1;
  209. while (src_bytes >= startp)
  210. *dest_bytes-- = *src_bytes--;
  211. } else if (src_bytes > dest_bytes) {
  212. u8 *endp = src_bytes + n;
  213. while (src_bytes < endp)
  214. *dest_bytes++ = *src_bytes++;
  215. }
  216. }
  217. return dest;
  218. }
  219. #undef memmove
  220. #define memmove my_memmove
  221. #endif /* CONFIG_FORTIFY_SOURCE */
  222. #include <linux/init.h>
  223. #include <linux/module.h>
  224. #include <linux/delay.h>
  225. #ifdef __PIE__
  226. /* without this, mm.h brings in static, but not inline, pmd_to_page(),
  227. * with direct references to global vmem variables.
  228. */
  229. #undef USE_SPLIT_PMD_PTLOCKS
  230. #define USE_SPLIT_PMD_PTLOCKS 0
  231. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  232. /* without this, static show_free_areas() mm.h brings in direct
  233. * reference to unexported __show_free_areas().
  234. */
  235. #define __show_free_areas my__show_free_areas
  236. void my__show_free_areas(
  237. unsigned int flags,
  238. nodemask_t *nodemask,
  239. int max_zone_idx);
  240. #endif
  241. #endif
  242. #include <linux/mm.h>
  243. #ifndef SINGLE_THREADED
  244. #include <linux/kthread.h>
  245. #endif
  246. #include <linux/net.h>
  247. #include <linux/slab.h>
  248. #ifdef LINUXKM_LKCAPI_REGISTER
  249. #include <linux/crypto.h>
  250. #include <linux/scatterlist.h>
  251. #include <crypto/scatterwalk.h>
  252. #include <crypto/internal/aead.h>
  253. #include <crypto/internal/skcipher.h>
  254. /* the LKCAPI assumes that expanded encrypt and decrypt keys will stay
  255. * loaded simultaneously, and the Linux in-tree implementations have two
  256. * AES key structs in each context, one for each direction. in
  257. * linuxkm/lkcapi_glue.c (used for CBC, CFB, and GCM), we do the same
  258. * thing with "struct km_AesCtx". however, wolfCrypt struct AesXts
  259. * already has two AES expanded keys, the main and tweak, and the tweak
  260. * is always used in the encrypt direction regardless of the main
  261. * direction. to avoid allocating and computing a duplicate second
  262. * tweak encrypt key, we set
  263. * WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS, which adds a second
  264. * Aes slot to wolfCrypt's struct AesXts, and activates support for
  265. * AES_ENCRYPTION_AND_DECRYPTION on AES-XTS.
  266. */
  267. #ifndef WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  268. #define WC_AES_XTS_SUPPORT_SIMULTANEOUS_ENC_AND_DEC_KEYS
  269. #endif
  270. #endif
  271. #if defined(WOLFSSL_AESNI) || defined(USE_INTEL_SPEEDUP) || \
  272. defined(WOLFSSL_SP_X86_64_ASM)
  273. #ifndef CONFIG_X86
  274. #error X86 SIMD extensions requested, but CONFIG_X86 is not set.
  275. #endif
  276. #define WOLFSSL_LINUXKM_SIMD
  277. #define WOLFSSL_LINUXKM_SIMD_X86
  278. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  279. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  280. #endif
  281. #elif defined(WOLFSSL_ARMASM) || defined(WOLFSSL_SP_ARM32_ASM) || \
  282. defined(WOLFSSL_SP_ARM64_ASM) || defined(WOLFSSL_SP_ARM_THUMB_ASM) ||\
  283. defined(WOLFSSL_SP_ARM_CORTEX_M_ASM)
  284. #if !defined(CONFIG_ARM) && !defined(CONFIG_ARM64)
  285. #error ARM SIMD extensions requested, but CONFIG_ARM* is not set.
  286. #endif
  287. #define WOLFSSL_LINUXKM_SIMD
  288. #define WOLFSSL_LINUXKM_SIMD_ARM
  289. #ifndef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  290. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  291. #endif
  292. #else
  293. #ifndef WOLFSSL_NO_ASM
  294. #define WOLFSSL_NO_ASM
  295. #endif
  296. #endif
  297. /* benchmarks.c uses floating point math, so needs a working
  298. * SAVE_VECTOR_REGISTERS().
  299. */
  300. #if defined(WOLFSSL_LINUXKM_BENCHMARKS) && \
  301. !defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  302. #define WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  303. #endif
  304. #if defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && \
  305. defined(CONFIG_X86)
  306. extern __must_check int allocate_wolfcrypt_linuxkm_fpu_states(void);
  307. extern void free_wolfcrypt_linuxkm_fpu_states(void);
  308. extern __must_check int can_save_vector_registers_x86(void);
  309. extern __must_check int save_vector_registers_x86(void);
  310. extern void restore_vector_registers_x86(void);
  311. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  312. #include <asm/i387.h>
  313. #else
  314. #include <asm/simd.h>
  315. #endif
  316. #ifndef CAN_SAVE_VECTOR_REGISTERS
  317. #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
  318. #define CAN_SAVE_VECTOR_REGISTERS() (can_save_vector_registers_x86() && (SAVE_VECTOR_REGISTERS2_fuzzer() == 0))
  319. #else
  320. #define CAN_SAVE_VECTOR_REGISTERS() can_save_vector_registers_x86()
  321. #endif
  322. #endif
  323. #ifndef SAVE_VECTOR_REGISTERS
  324. #define SAVE_VECTOR_REGISTERS(fail_clause) { \
  325. int _svr_ret = save_vector_registers_x86(); \
  326. if (_svr_ret != 0) { \
  327. fail_clause \
  328. } \
  329. }
  330. #endif
  331. #ifndef SAVE_VECTOR_REGISTERS2
  332. #ifdef DEBUG_VECTOR_REGISTER_ACCESS_FUZZING
  333. #define SAVE_VECTOR_REGISTERS2() ({ \
  334. int _fuzzer_ret = SAVE_VECTOR_REGISTERS2_fuzzer(); \
  335. (_fuzzer_ret == 0) ? \
  336. save_vector_registers_x86() : \
  337. _fuzzer_ret; \
  338. })
  339. #else
  340. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_x86()
  341. #endif
  342. #endif
  343. #ifndef RESTORE_VECTOR_REGISTERS
  344. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_x86()
  345. #endif
  346. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && (defined(CONFIG_ARM) || defined(CONFIG_ARM64))
  347. #error kernel module ARM SIMD is not yet tested or usable.
  348. #include <asm/fpsimd.h>
  349. static WARN_UNUSED_RESULT inline int save_vector_registers_arm(void)
  350. {
  351. preempt_disable();
  352. if (! may_use_simd()) {
  353. preempt_enable();
  354. return BAD_STATE_E;
  355. } else {
  356. fpsimd_preserve_current_state();
  357. return 0;
  358. }
  359. }
  360. static inline void restore_vector_registers_arm(void)
  361. {
  362. fpsimd_restore_current_state();
  363. preempt_enable();
  364. }
  365. #ifndef SAVE_VECTOR_REGISTERS
  366. #define SAVE_VECTOR_REGISTERS(fail_clause) { int _svr_ret = save_vector_registers_arm(); if (_svr_ret != 0) { fail_clause } }
  367. #endif
  368. #ifndef SAVE_VECTOR_REGISTERS2
  369. #define SAVE_VECTOR_REGISTERS2() save_vector_registers_arm()
  370. #endif
  371. #ifndef CAN_SAVE_VECTOR_REGISTERS
  372. #define CAN_SAVE_VECTOR_REGISTERS() can_save_vector_registers_arm()
  373. #endif
  374. #ifndef RESTORE_VECTOR_REGISTERS
  375. #define RESTORE_VECTOR_REGISTERS() restore_vector_registers_arm()
  376. #endif
  377. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  378. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  379. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  380. _Pragma("GCC diagnostic pop");
  381. /* avoid -Wpointer-arith, encountered when -DCONFIG_FORTIFY_SOURCE */
  382. #undef __is_constexpr
  383. #define __is_constexpr(x) __builtin_constant_p(x)
  384. /* the kernel uses -std=c89, but not -pedantic, and makes full use of anon
  385. * structs/unions, so we should too.
  386. */
  387. #define HAVE_ANONYMOUS_INLINE_AGGREGATES 1
  388. #define NO_THREAD_LS
  389. #define NO_ATTRIBUTE_CONSTRUCTOR
  390. #ifdef HAVE_FIPS
  391. extern int wolfCrypt_FIPS_first(void);
  392. extern int wolfCrypt_FIPS_last(void);
  393. #if FIPS_VERSION3_GE(6,0,0)
  394. extern int wolfCrypt_FIPS_AES_sanity(void);
  395. extern int wolfCrypt_FIPS_CMAC_sanity(void);
  396. extern int wolfCrypt_FIPS_DH_sanity(void);
  397. extern int wolfCrypt_FIPS_ECC_sanity(void);
  398. extern int wolfCrypt_FIPS_ED25519_sanity(void);
  399. extern int wolfCrypt_FIPS_ED448_sanity(void);
  400. extern int wolfCrypt_FIPS_HMAC_sanity(void);
  401. extern int wolfCrypt_FIPS_KDF_sanity(void);
  402. extern int wolfCrypt_FIPS_PBKDF_sanity(void);
  403. extern int wolfCrypt_FIPS_DRBG_sanity(void);
  404. extern int wolfCrypt_FIPS_RSA_sanity(void);
  405. extern int wolfCrypt_FIPS_SHA_sanity(void);
  406. extern int wolfCrypt_FIPS_SHA256_sanity(void);
  407. extern int wolfCrypt_FIPS_SHA512_sanity(void);
  408. extern int wolfCrypt_FIPS_SHA3_sanity(void);
  409. extern int wolfCrypt_FIPS_FT_sanity(void);
  410. extern int wc_RunAllCast_fips(void);
  411. #endif
  412. #endif
  413. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  414. /* work around backward dependency of asn.c on ssl.c. */
  415. struct Signer;
  416. struct Signer *GetCA(void *signers, unsigned char *hash);
  417. #ifndef NO_SKID
  418. struct Signer *GetCAByName(void* signers, unsigned char *hash);
  419. #ifdef HAVE_OCSP
  420. struct Signer* GetCAByKeyHash(void* vp, const unsigned char* keyHash);
  421. #endif /* HAVE_OCSP */
  422. #ifdef WOLFSSL_AKID_NAME
  423. struct Signer* GetCAByAKID(void* vp, const unsigned char* issuer,
  424. unsigned int issuerSz,
  425. const unsigned char* serial,
  426. unsigned int serialSz);
  427. #endif
  428. #endif /* NO_SKID */
  429. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  430. struct WOLFSSL_X509_NAME;
  431. extern int wolfSSL_X509_NAME_add_entry_by_NID(struct WOLFSSL_X509_NAME *name, int nid,
  432. int type, const unsigned char *bytes,
  433. int len, int loc, int set);
  434. extern void wolfSSL_X509_NAME_free(struct WOLFSSL_X509_NAME* name);
  435. extern struct WOLFSSL_X509_NAME* wolfSSL_X509_NAME_new_ex(void *heap);
  436. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  437. #endif /* !WOLFCRYPT_ONLY && !NO_CERTS */
  438. #if defined(__PIE__) && !defined(USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE)
  439. #error "compiling -fPIE requires PIE redirect table."
  440. #endif
  441. #if defined(HAVE_FIPS) && !defined(HAVE_LINUXKM_PIE_SUPPORT)
  442. #error "FIPS build requires PIE support."
  443. #endif
  444. #ifdef USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE
  445. #ifdef CONFIG_MIPS
  446. #undef __ARCH_MEMCMP_NO_REDIRECT
  447. #undef memcmp
  448. extern int memcmp(const void *s1, const void *s2, size_t n);
  449. #endif
  450. struct wolfssl_linuxkm_pie_redirect_table {
  451. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  452. typeof(memcmp) *memcmp;
  453. #endif
  454. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  455. typeof(memcpy) *memcpy;
  456. #endif
  457. #ifndef __ARCH_MEMSET_NO_REDIRECT
  458. typeof(memset) *memset;
  459. #endif
  460. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  461. typeof(memmove) *memmove;
  462. #endif
  463. #ifndef __ARCH_STRCMP_NO_REDIRECT
  464. typeof(strcmp) *strcmp;
  465. #endif
  466. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  467. typeof(strncmp) *strncmp;
  468. #endif
  469. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  470. typeof(strcasecmp) *strcasecmp;
  471. #endif
  472. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  473. typeof(strncasecmp) *strncasecmp;
  474. #endif
  475. #ifndef __ARCH_STRLEN_NO_REDIRECT
  476. typeof(strlen) *strlen;
  477. #endif
  478. #ifndef __ARCH_STRSTR_NO_REDIRECT
  479. typeof(strstr) *strstr;
  480. #endif
  481. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  482. typeof(strncpy) *strncpy;
  483. #endif
  484. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  485. typeof(strncat) *strncat;
  486. #endif
  487. typeof(kstrtoll) *kstrtoll;
  488. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  489. typeof(_printk) *_printk;
  490. #else
  491. typeof(printk) *printk;
  492. #endif
  493. #ifdef CONFIG_FORTIFY_SOURCE
  494. typeof(__warn_printk) *__warn_printk;
  495. #endif
  496. typeof(snprintf) *snprintf;
  497. const unsigned char *_ctype;
  498. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
  499. typeof(kmalloc_noprof) *kmalloc_noprof;
  500. typeof(krealloc_noprof) *krealloc_noprof;
  501. typeof(kzalloc_noprof) *kzalloc_noprof;
  502. typeof(__kvmalloc_node_noprof) *__kvmalloc_node_noprof;
  503. typeof(__kmalloc_cache_noprof) *__kmalloc_cache_noprof;
  504. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
  505. typeof(kmalloc_noprof) *kmalloc_noprof;
  506. typeof(krealloc_noprof) *krealloc_noprof;
  507. typeof(kzalloc_noprof) *kzalloc_noprof;
  508. typeof(kvmalloc_node_noprof) *kvmalloc_node_noprof;
  509. typeof(kmalloc_trace_noprof) *kmalloc_trace_noprof;
  510. #else /* <6.10.0 */
  511. typeof(kmalloc) *kmalloc;
  512. typeof(krealloc) *krealloc;
  513. #ifdef HAVE_KVMALLOC
  514. typeof(kvmalloc_node) *kvmalloc_node;
  515. #endif
  516. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  517. typeof(kmalloc_trace) *kmalloc_trace;
  518. #else
  519. typeof(kmem_cache_alloc_trace) *kmem_cache_alloc_trace;
  520. typeof(kmalloc_order_trace) *kmalloc_order_trace;
  521. #endif
  522. #endif /* <6.10.0 */
  523. #ifdef HAVE_KVMALLOC
  524. typeof(kvfree) *kvfree;
  525. #endif
  526. typeof(kfree) *kfree;
  527. typeof(ksize) *ksize;
  528. typeof(is_vmalloc_addr) *is_vmalloc_addr;
  529. typeof(get_random_bytes) *get_random_bytes;
  530. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  531. typeof(getnstimeofday) *getnstimeofday;
  532. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  533. typeof(current_kernel_time64) *current_kernel_time64;
  534. #else
  535. typeof(ktime_get_coarse_real_ts64) *ktime_get_coarse_real_ts64;
  536. #endif
  537. struct task_struct *(*get_current)(void);
  538. #ifdef WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS
  539. #ifdef CONFIG_X86
  540. typeof(allocate_wolfcrypt_linuxkm_fpu_states) *allocate_wolfcrypt_linuxkm_fpu_states;
  541. typeof(can_save_vector_registers_x86) *can_save_vector_registers_x86;
  542. typeof(free_wolfcrypt_linuxkm_fpu_states) *free_wolfcrypt_linuxkm_fpu_states;
  543. typeof(restore_vector_registers_x86) *restore_vector_registers_x86;
  544. typeof(save_vector_registers_x86) *save_vector_registers_x86;
  545. #else /* !CONFIG_X86 */
  546. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  547. #endif /* arch */
  548. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  549. typeof(__mutex_init) *__mutex_init;
  550. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  551. typeof(mutex_lock_nested) *mutex_lock_nested;
  552. #else
  553. typeof(mutex_lock) *mutex_lock;
  554. #endif
  555. typeof(mutex_unlock) *mutex_unlock;
  556. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  557. typeof(mutex_destroy) *mutex_destroy;
  558. #endif
  559. #ifdef HAVE_FIPS
  560. typeof(wolfCrypt_FIPS_first) *wolfCrypt_FIPS_first;
  561. typeof(wolfCrypt_FIPS_last) *wolfCrypt_FIPS_last;
  562. #if FIPS_VERSION3_GE(6,0,0)
  563. typeof(wolfCrypt_FIPS_AES_sanity) *wolfCrypt_FIPS_AES_sanity;
  564. typeof(wolfCrypt_FIPS_CMAC_sanity) *wolfCrypt_FIPS_CMAC_sanity;
  565. typeof(wolfCrypt_FIPS_DH_sanity) *wolfCrypt_FIPS_DH_sanity;
  566. typeof(wolfCrypt_FIPS_ECC_sanity) *wolfCrypt_FIPS_ECC_sanity;
  567. typeof(wolfCrypt_FIPS_ED25519_sanity) *wolfCrypt_FIPS_ED25519_sanity;
  568. typeof(wolfCrypt_FIPS_ED448_sanity) *wolfCrypt_FIPS_ED448_sanity;
  569. typeof(wolfCrypt_FIPS_HMAC_sanity) *wolfCrypt_FIPS_HMAC_sanity;
  570. typeof(wolfCrypt_FIPS_KDF_sanity) *wolfCrypt_FIPS_KDF_sanity;
  571. typeof(wolfCrypt_FIPS_PBKDF_sanity) *wolfCrypt_FIPS_PBKDF_sanity;
  572. typeof(wolfCrypt_FIPS_DRBG_sanity) *wolfCrypt_FIPS_DRBG_sanity;
  573. typeof(wolfCrypt_FIPS_RSA_sanity) *wolfCrypt_FIPS_RSA_sanity;
  574. typeof(wolfCrypt_FIPS_SHA_sanity) *wolfCrypt_FIPS_SHA_sanity;
  575. typeof(wolfCrypt_FIPS_SHA256_sanity) *wolfCrypt_FIPS_SHA256_sanity;
  576. typeof(wolfCrypt_FIPS_SHA512_sanity) *wolfCrypt_FIPS_SHA512_sanity;
  577. typeof(wolfCrypt_FIPS_SHA3_sanity) *wolfCrypt_FIPS_SHA3_sanity;
  578. typeof(wolfCrypt_FIPS_FT_sanity) *wolfCrypt_FIPS_FT_sanity;
  579. typeof(wc_RunAllCast_fips) *wc_RunAllCast_fips;
  580. #endif
  581. #endif
  582. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  583. typeof(GetCA) *GetCA;
  584. #ifndef NO_SKID
  585. typeof(GetCAByName) *GetCAByName;
  586. #ifdef HAVE_OCSP
  587. typeof(GetCAByKeyHash) *GetCAByKeyHash;
  588. #endif /* HAVE_OCSP */
  589. #endif /* NO_SKID */
  590. #ifdef WOLFSSL_AKID_NAME
  591. typeof(GetCAByAKID) *GetCAByAKID;
  592. #endif /* WOLFSSL_AKID_NAME */
  593. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  594. typeof(wolfSSL_X509_NAME_add_entry_by_NID) *wolfSSL_X509_NAME_add_entry_by_NID;
  595. typeof(wolfSSL_X509_NAME_free) *wolfSSL_X509_NAME_free;
  596. typeof(wolfSSL_X509_NAME_new_ex) *wolfSSL_X509_NAME_new_ex;
  597. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  598. #endif /* !WOLFCRYPT_ONLY && !NO_CERTS */
  599. #ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
  600. typeof(dump_stack) *dump_stack;
  601. #endif
  602. #ifdef CONFIG_ARM64
  603. #ifdef __PIE__
  604. /* alt_cb_patch_nops defined early to allow shimming in system
  605. * headers, but now we need the native one.
  606. */
  607. #undef alt_cb_patch_nops
  608. typeof(my__alt_cb_patch_nops) *alt_cb_patch_nops;
  609. #else
  610. typeof(alt_cb_patch_nops) *alt_cb_patch_nops;
  611. #endif
  612. #endif
  613. const void *_last_slot;
  614. };
  615. extern const struct wolfssl_linuxkm_pie_redirect_table *wolfssl_linuxkm_get_pie_redirect_table(void);
  616. #ifdef __PIE__
  617. #ifndef __ARCH_MEMCMP_NO_REDIRECT
  618. #define memcmp (wolfssl_linuxkm_get_pie_redirect_table()->memcmp)
  619. #endif
  620. #ifndef __ARCH_MEMCPY_NO_REDIRECT
  621. #define memcpy (wolfssl_linuxkm_get_pie_redirect_table()->memcpy)
  622. #endif
  623. #ifndef __ARCH_MEMSET_NO_REDIRECT
  624. #define memset (wolfssl_linuxkm_get_pie_redirect_table()->memset)
  625. #endif
  626. #ifndef __ARCH_MEMMOVE_NO_REDIRECT
  627. #define memmove (wolfssl_linuxkm_get_pie_redirect_table()->memmove)
  628. #endif
  629. #ifndef __ARCH_STRCMP_NO_REDIRECT
  630. #define strcmp (wolfssl_linuxkm_get_pie_redirect_table()->strcmp)
  631. #endif
  632. #ifndef __ARCH_STRNCMP_NO_REDIRECT
  633. #define strncmp (wolfssl_linuxkm_get_pie_redirect_table()->strncmp)
  634. #endif
  635. #ifndef __ARCH_STRCASECMP_NO_REDIRECT
  636. #define strcasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strcasecmp)
  637. #endif
  638. #ifndef __ARCH_STRNCASECMP_NO_REDIRECT
  639. #define strncasecmp (wolfssl_linuxkm_get_pie_redirect_table()->strncasecmp)
  640. #endif
  641. #ifndef __ARCH_STRLEN_NO_REDIRECT
  642. #define strlen (wolfssl_linuxkm_get_pie_redirect_table()->strlen)
  643. #endif
  644. #ifndef __ARCH_STRSTR_NO_REDIRECT
  645. #define strstr (wolfssl_linuxkm_get_pie_redirect_table()->strstr)
  646. #endif
  647. #ifndef __ARCH_STRNCPY_NO_REDIRECT
  648. #define strncpy (wolfssl_linuxkm_get_pie_redirect_table()->strncpy)
  649. #endif
  650. #ifndef __ARCH_STRNCAT_NO_REDIRECT
  651. #define strncat (wolfssl_linuxkm_get_pie_redirect_table()->strncat)
  652. #endif
  653. #define kstrtoll (wolfssl_linuxkm_get_pie_redirect_table()->kstrtoll)
  654. #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)
  655. #define _printk (wolfssl_linuxkm_get_pie_redirect_table()->_printk)
  656. #else
  657. #define printk (wolfssl_linuxkm_get_pie_redirect_table()->printk)
  658. #endif
  659. #ifdef CONFIG_FORTIFY_SOURCE
  660. #define __warn_printk (wolfssl_linuxkm_get_pie_redirect_table()->__warn_printk)
  661. #endif
  662. #define snprintf (wolfssl_linuxkm_get_pie_redirect_table()->snprintf)
  663. #define _ctype (wolfssl_linuxkm_get_pie_redirect_table()->_ctype)
  664. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 11, 0)
  665. /* see include/linux/alloc_tag.h and include/linux/slab.h */
  666. #define kmalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_noprof)
  667. #define krealloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->krealloc_noprof)
  668. #define kzalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kzalloc_noprof)
  669. #define __kvmalloc_node_noprof (wolfssl_linuxkm_get_pie_redirect_table()->__kvmalloc_node_noprof)
  670. #define __kmalloc_cache_noprof (wolfssl_linuxkm_get_pie_redirect_table()->__kmalloc_cache_noprof)
  671. #elif LINUX_VERSION_CODE >= KERNEL_VERSION(6, 10, 0)
  672. /* see include/linux/alloc_tag.h and include/linux/slab.h */
  673. #define kmalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_noprof)
  674. #define krealloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->krealloc_noprof)
  675. #define kzalloc_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kzalloc_noprof)
  676. #define kvmalloc_node_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node_noprof)
  677. #define kmalloc_trace_noprof (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace_noprof)
  678. #else /* <6.10.0 */
  679. #define kmalloc (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc)
  680. #define krealloc (wolfssl_linuxkm_get_pie_redirect_table()->krealloc)
  681. #define kzalloc(size, flags) kmalloc(size, (flags) | __GFP_ZERO)
  682. #ifdef HAVE_KVMALLOC
  683. #define kvmalloc_node (wolfssl_linuxkm_get_pie_redirect_table()->kvmalloc_node)
  684. #endif
  685. #if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 1, 0)
  686. #define kmalloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_trace)
  687. #else
  688. #define kmem_cache_alloc_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmem_cache_alloc_trace)
  689. #define kmalloc_order_trace (wolfssl_linuxkm_get_pie_redirect_table()->kmalloc_order_trace)
  690. #endif
  691. #endif /* <6.10.0 */
  692. #define kfree (wolfssl_linuxkm_get_pie_redirect_table()->kfree)
  693. #ifdef HAVE_KVMALLOC
  694. #define kvfree (wolfssl_linuxkm_get_pie_redirect_table()->kvfree)
  695. #endif
  696. #define ksize (wolfssl_linuxkm_get_pie_redirect_table()->ksize)
  697. #define is_vmalloc_addr (wolfssl_linuxkm_get_pie_redirect_table()->is_vmalloc_addr)
  698. #define get_random_bytes (wolfssl_linuxkm_get_pie_redirect_table()->get_random_bytes)
  699. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  700. #define getnstimeofday (wolfssl_linuxkm_get_pie_redirect_table()->getnstimeofday)
  701. #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 0, 0)
  702. #define current_kernel_time64 (wolfssl_linuxkm_get_pie_redirect_table()->current_kernel_time64)
  703. #else
  704. #define ktime_get_coarse_real_ts64 (wolfssl_linuxkm_get_pie_redirect_table()->ktime_get_coarse_real_ts64)
  705. #endif
  706. #undef get_current
  707. #define get_current (wolfssl_linuxkm_get_pie_redirect_table()->get_current)
  708. #if defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS) && defined(CONFIG_X86)
  709. #define allocate_wolfcrypt_linuxkm_fpu_states (wolfssl_linuxkm_get_pie_redirect_table()->allocate_wolfcrypt_linuxkm_fpu_states)
  710. #define can_save_vector_registers_x86 (wolfssl_linuxkm_get_pie_redirect_table()->can_save_vector_registers_x86)
  711. #define free_wolfcrypt_linuxkm_fpu_states (wolfssl_linuxkm_get_pie_redirect_table()->free_wolfcrypt_linuxkm_fpu_states)
  712. #define restore_vector_registers_x86 (wolfssl_linuxkm_get_pie_redirect_table()->restore_vector_registers_x86)
  713. #define save_vector_registers_x86 (wolfssl_linuxkm_get_pie_redirect_table()->save_vector_registers_x86)
  714. #elif defined(WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS)
  715. #error WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS is set for an unsupported architecture.
  716. #endif /* WOLFSSL_LINUXKM_USE_SAVE_VECTOR_REGISTERS */
  717. #define __mutex_init (wolfssl_linuxkm_get_pie_redirect_table()->__mutex_init)
  718. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  719. #define mutex_lock_nested (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock_nested)
  720. #else
  721. #define mutex_lock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_lock)
  722. #endif
  723. #define mutex_unlock (wolfssl_linuxkm_get_pie_redirect_table()->mutex_unlock)
  724. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  725. #define mutex_destroy (wolfssl_linuxkm_get_pie_redirect_table()->mutex_destroy)
  726. #endif
  727. /* per linux/ctype.h, tolower() and toupper() are macros bound to static inlines
  728. * that use macros that bring in the _ctype global. for __PIE__, this needs to
  729. * be masked out.
  730. */
  731. #undef tolower
  732. #undef toupper
  733. #define tolower(c) (islower(c) ? (c) : ((c) + ('a'-'A')))
  734. #define toupper(c) (isupper(c) ? (c) : ((c) - ('a'-'A')))
  735. #if !defined(WOLFCRYPT_ONLY) && !defined(NO_CERTS)
  736. #define GetCA (wolfssl_linuxkm_get_pie_redirect_table()->GetCA)
  737. #ifndef NO_SKID
  738. #define GetCAByName (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByName)
  739. #ifdef HAVE_OCSP
  740. #define GetCAByKeyHash (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByKeyHash)
  741. #endif /* HAVE_OCSP */
  742. #endif /* NO_SKID */
  743. #ifdef WOLFSSL_AKID_NAME
  744. #define GetCAByAKID (wolfssl_linuxkm_get_pie_redirect_table()->GetCAByAKID)
  745. #endif
  746. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  747. #define wolfSSL_X509_NAME_add_entry_by_NID (wolfssl_linuxkm_get_pie_redirect_table()->wolfSSL_X509_NAME_add_entry_by_NID)
  748. #define wolfSSL_X509_NAME_free (wolfssl_linuxkm_get_pie_redirect_table()->wolfSSL_X509_NAME_free)
  749. #define wolfSSL_X509_NAME_new_ex (wolfssl_linuxkm_get_pie_redirect_table()->wolfSSL_X509_NAME_new_ex)
  750. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  751. #endif /* !WOLFCRYPT_ONLY && !NO_CERTS */
  752. #ifdef WOLFSSL_DEBUG_BACKTRACE_ERROR_CODES
  753. #define dump_stack (wolfssl_linuxkm_get_pie_redirect_table()->dump_stack)
  754. #endif
  755. #endif /* __PIE__ */
  756. #endif /* USE_WOLFSSL_LINUXKM_PIE_REDIRECT_TABLE */
  757. /* remove this multifariously conflicting macro, picked up from
  758. * Linux arch/<arch>/include/asm/current.h.
  759. */
  760. #ifndef WOLFSSL_NEED_LINUX_CURRENT
  761. #undef current
  762. #endif
  763. /* min() and max() in linux/kernel.h over-aggressively type-check, producing
  764. * myriad spurious -Werrors throughout the codebase.
  765. */
  766. #undef min
  767. #undef max
  768. /* work around namespace conflict between wolfssl/internal.h (enum HandShakeType)
  769. * and linux/key.h (extern int()).
  770. */
  771. #define key_update wc_key_update
  772. #define lkm_printf(format, args...) printk(KERN_INFO "wolfssl: %s(): " format, __func__, ## args)
  773. #define printf(...) lkm_printf(__VA_ARGS__)
  774. #ifdef HAVE_FIPS
  775. extern void fipsEntry(void);
  776. #endif
  777. /* suppress false-positive "writing 1 byte into a region of size 0" warnings
  778. * building old kernels with new gcc:
  779. */
  780. #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 0, 0)
  781. _Pragma("GCC diagnostic ignored \"-Wstringop-overflow\"");
  782. #endif
  783. /* includes are all above, with incompatible warnings masked out. */
  784. #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 5, 0)
  785. typedef __kernel_time_t time_t;
  786. #else
  787. typedef __kernel_time64_t time_t;
  788. #endif
  789. extern time_t time(time_t * timer);
  790. #define XTIME time
  791. #define WOLFSSL_GMTIME
  792. #define XGMTIME(c, t) gmtime(c)
  793. #define NO_TIMEVAL 1
  794. #endif /* BUILDING_WOLFSSL */
  795. /* if BUILDING_WOLFSSL, mutex.h will have already been included recursively
  796. * above, with the bevy of warnings suppressed, and the below include will
  797. * be a redundant no-op.
  798. */
  799. #include <linux/mutex.h>
  800. typedef struct mutex wolfSSL_Mutex;
  801. #define WOLFSSL_MUTEX_INITIALIZER(lockname) __MUTEX_INITIALIZER(lockname)
  802. /* prevent gcc's mm_malloc.h from being included, since it unconditionally
  803. * includes stdlib.h, which is kernel-incompatible.
  804. */
  805. #define _MM_MALLOC_H_INCLUDED
  806. /* fun fact: since linux commit 59bb47985c, kmalloc with power-of-2 size is
  807. * aligned to the size.
  808. */
  809. #define WC_LINUXKM_ROUND_UP_P_OF_2(x) ( \
  810. { \
  811. size_t _alloc_sz = (x); \
  812. if (_alloc_sz < 8192) \
  813. _alloc_sz = 1UL << \
  814. ((sizeof(_alloc_sz) * 8UL) - __builtin_clzl(_alloc_sz - 1)); \
  815. _alloc_sz; \
  816. })
  817. #ifdef HAVE_KVMALLOC
  818. #define malloc(size) kvmalloc_node(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL, NUMA_NO_NODE)
  819. #define free(ptr) kvfree(ptr)
  820. void *lkm_realloc(void *ptr, size_t newsize);
  821. #define realloc(ptr, newsize) lkm_realloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize))
  822. #else
  823. #define malloc(size) kmalloc(WC_LINUXKM_ROUND_UP_P_OF_2(size), GFP_KERNEL)
  824. #define free(ptr) kfree(ptr)
  825. #define realloc(ptr, newsize) krealloc(ptr, WC_LINUXKM_ROUND_UP_P_OF_2(newsize), GFP_KERNEL)
  826. #endif
  827. #ifndef static_assert
  828. #define static_assert(expr, ...) __static_assert(expr, ##__VA_ARGS__, #expr)
  829. #define __static_assert(expr, msg, ...) _Static_assert(expr, msg)
  830. #endif
  831. #include <wolfssl/wolfcrypt/memory.h>
  832. #ifdef WOLFSSL_TRACK_MEMORY
  833. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); wolfSSL_Malloc(s);})
  834. #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
  835. #define XFREE(p, h, t) ({(void)(h); (void)(t); wolfSSL_Free(p);})
  836. #else
  837. #define XFREE(p, h, t) ({void* _xp; (void)(h); _xp = (p); if(_xp) wolfSSL_Free(_xp);})
  838. #endif
  839. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); wolfSSL_Realloc(p, n);})
  840. #else
  841. #define XMALLOC(s, h, t) ({(void)(h); (void)(t); malloc(s);})
  842. #ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK
  843. #define XFREE(p, h, t) ({(void)(h); (void)(t); free(p);})
  844. #else
  845. #define XFREE(p, h, t) ({void* _xp; (void)(h); (void)(t); _xp = (p); if(_xp) free(_xp);})
  846. #endif
  847. #define XREALLOC(p, n, h, t) ({(void)(h); (void)(t); realloc(p, n);})
  848. #endif
  849. #include <linux/limits.h>
  850. #ifndef INT32_MAX
  851. #define INT32_MAX INT_MAX
  852. #endif
  853. #ifndef UINT32_MAX
  854. #define UINT32_MAX UINT_MAX
  855. #endif
  856. /* Linux headers define these using C expressions, but we need
  857. * them to be evaluable by the preprocessor, for use in sp_int.h.
  858. */
  859. #if BITS_PER_LONG == 64
  860. static_assert(sizeof(ULONG_MAX) == 8,
  861. "BITS_PER_LONG is 64, but ULONG_MAX is not.");
  862. #undef UCHAR_MAX
  863. #define UCHAR_MAX 255
  864. #undef USHRT_MAX
  865. #define USHRT_MAX 65535
  866. #undef UINT_MAX
  867. #define UINT_MAX 4294967295U
  868. #undef ULONG_MAX
  869. #define ULONG_MAX 18446744073709551615UL
  870. #undef ULLONG_MAX
  871. #define ULLONG_MAX ULONG_MAX
  872. #undef INT_MAX
  873. #define INT_MAX 2147483647
  874. #undef LONG_MAX
  875. #define LONG_MAX 9223372036854775807L
  876. #undef LLONG_MAX
  877. #define LLONG_MAX LONG_MAX
  878. #elif BITS_PER_LONG == 32
  879. static_assert(sizeof(ULONG_MAX) == 4,
  880. "BITS_PER_LONG is 32, but ULONG_MAX is not.");
  881. #undef UCHAR_MAX
  882. #define UCHAR_MAX 255
  883. #undef USHRT_MAX
  884. #define USHRT_MAX 65535
  885. #undef UINT_MAX
  886. #define UINT_MAX 4294967295U
  887. #undef ULONG_MAX
  888. #define ULONG_MAX 4294967295UL
  889. #undef INT_MAX
  890. #define INT_MAX 2147483647
  891. #undef LONG_MAX
  892. #define LONG_MAX 2147483647L
  893. #undef ULLONG_MAX
  894. #undef LLONG_MAX
  895. #if BITS_PER_LONG_LONG == 64
  896. #define ULLONG_MAX 18446744073709551615UL
  897. #define LLONG_MAX 9223372036854775807L
  898. #else
  899. #undef NO_64BIT
  900. #define NO_64BIT
  901. #define ULLONG_MAX ULONG_MAX
  902. #define LLONG_MAX LONG_MAX
  903. #endif
  904. #else
  905. #error unexpected BITS_PER_LONG value.
  906. #endif
  907. #endif /* LINUXKM_WC_PORT_H */