linuxkm_wc_port.h 40 KB

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