|
| 1 | +/* bsdkm_wc_port.h |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2025 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 3 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 | +/* included by wolfssl/wolfcrypt/wc_port.h */ |
| 23 | + |
| 24 | +#ifndef BSDKM_WC_PORT_H |
| 25 | +#define BSDKM_WC_PORT_H |
| 26 | + |
| 27 | +#ifdef WOLFSSL_BSDKM |
| 28 | + |
| 29 | +#include <sys/ctype.h> |
| 30 | +#include <sys/types.h> |
| 31 | +#include <sys/malloc.h> |
| 32 | +#include <sys/systm.h> |
| 33 | +#if !defined(SINGLE_THREADED) |
| 34 | + #include <sys/mutex.h> |
| 35 | +#endif /* !SINGLE_THREADED */ |
| 36 | +#ifndef CHAR_BIT |
| 37 | + #include <sys/limits.h> |
| 38 | +#endif /* !CHAR_BIT*/ |
| 39 | + |
| 40 | +/* needed to prevent wolfcrypt/src/asn.c version shadowing |
| 41 | + * extern global version from /usr/src/sys/sys/systm.h */ |
| 42 | +#define version wc_version |
| 43 | + |
| 44 | +#define wc_km_printf printf |
| 45 | + |
| 46 | +/* str and char utility functions */ |
| 47 | +#define XATOI(s) ({ \ |
| 48 | + char * endptr = NULL; \ |
| 49 | + long _xatoi_ret = strtol(s, &endptr, 10); \ |
| 50 | + if ((s) == endptr || *endptr != '\0') { \ |
| 51 | + _xatoi_ret = 0; \ |
| 52 | + } \ |
| 53 | + (int)_xatoi_ret; \ |
| 54 | + }) |
| 55 | + |
| 56 | +#if !defined(XMALLOC_OVERRIDE) |
| 57 | + #error bsdkm requires XMALLOC_OVERRIDE |
| 58 | +#endif /* !XMALLOC_OVERRIDE */ |
| 59 | + |
| 60 | +/* use malloc and free from /usr/include/sys/malloc.h */ |
| 61 | +extern struct malloc_type M_WOLFSSL[1]; |
| 62 | + |
| 63 | +#define XMALLOC(s, h, t) \ |
| 64 | + ({(void)(h); (void)(t); malloc(s, M_WOLFSSL, M_WAITOK | M_ZERO);}) |
| 65 | + |
| 66 | +#ifdef WOLFSSL_XFREE_NO_NULLNESS_CHECK |
| 67 | + #define XFREE(p, h, t) \ |
| 68 | + ({(void)(h); (void)(t); free(p, M_WOLFSSL);}) |
| 69 | +#else |
| 70 | + #define XFREE(p, h, t) \ |
| 71 | + ({void* _xp; (void)(h); (void)(t); _xp = (p); \ |
| 72 | + if(_xp) free(_xp, M_WOLFSSL);}) |
| 73 | +#endif |
| 74 | + |
| 75 | +#if !defined(SINGLE_THREADED) |
| 76 | + #define WC_MUTEX_OPS_INLINE |
| 77 | + |
| 78 | + typedef struct wolfSSL_Mutex { |
| 79 | + struct mtx lock; |
| 80 | + } wolfSSL_Mutex; |
| 81 | + |
| 82 | + static __always_inline int wc_InitMutex(wolfSSL_Mutex * m) |
| 83 | + { |
| 84 | + mtx_init(&m->lock, "wolfssl spinlock", NULL, MTX_SPIN); |
| 85 | + return 0; |
| 86 | + } |
| 87 | + |
| 88 | + static __always_inline int wc_FreeMutex(wolfSSL_Mutex * m) |
| 89 | + { |
| 90 | + mtx_destroy(&m->lock); |
| 91 | + return 0; |
| 92 | + } |
| 93 | + |
| 94 | + static __always_inline int wc_LockMutex(wolfSSL_Mutex *m) |
| 95 | + { |
| 96 | + mtx_lock_spin(&m->lock); |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + static __always_inline int wc_UnLockMutex(wolfSSL_Mutex* m) |
| 101 | + { |
| 102 | + mtx_unlock_spin(&m->lock); |
| 103 | + return 0; |
| 104 | + } |
| 105 | +#endif /* !SINGLE_THREADED */ |
| 106 | + |
| 107 | +#if defined(WOLFSSL_HAVE_ATOMIC_H) && !defined(WOLFSSL_NO_ATOMICS) |
| 108 | + #include <machine/atomic.h> |
| 109 | + typedef volatile int wolfSSL_Atomic_Int; |
| 110 | + typedef volatile unsigned int wolfSSL_Atomic_Uint; |
| 111 | + #define WOLFSSL_ATOMIC_INITIALIZER(x) (x) |
| 112 | + #define WOLFSSL_ATOMIC_LOAD(x) (int)atomic_load_acq_int(&(x)) |
| 113 | + #define WOLFSSL_ATOMIC_STORE(x, v) atomic_store_rel_int(&(x), (v)) |
| 114 | + #define WOLFSSL_ATOMIC_OPS |
| 115 | +#endif /* WOLFSSL_HAVE_ATOMIC_H && !WOLFSSL_NO_ATOMICS */ |
| 116 | + |
| 117 | +#endif /* WOLFSSL_BSDKM */ |
| 118 | +#endif /* BSDKM_WC_PORT_H */ |
0 commit comments