Merge pull request #11119 from philljj/bsdkm_cleanup

Bsdkm cleanup
pull/11169/head
JacobBarthelmeh 2026-08-13 10:49:17 -06:00 committed by GitHub
commit 66d49df095
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 111 additions and 34 deletions

View File

@ -63,10 +63,11 @@ static inline time_t wolfkmod_time(time_t * tloc) {
/* str and char utility functions */ /* str and char utility functions */
#define XATOI(s) ({ \ #define XATOI(s) ({ \
char * endptr = NULL; \ const char * _str = (s); \
long _xatoi_ret = strtol(s, &endptr, 10); \ char * _endptr = NULL; \
if ((s) == endptr || *endptr != '\0') { \ long _xatoi_ret = strtol(_str, &_endptr, 10); \
_xatoi_ret = 0; \ if ((_str) == _endptr || *_endptr != '\0') { \
_xatoi_ret = 0; \
} \ } \
(int)_xatoi_ret; \ (int)_xatoi_ret; \
}) })
@ -79,27 +80,53 @@ static inline time_t wolfkmod_time(time_t * tloc) {
extern struct malloc_type M_WOLFSSL[1]; extern struct malloc_type M_WOLFSSL[1];
#if defined(WOLFSSL_BSDKM_MEMORY_DEBUG) #if defined(WOLFSSL_BSDKM_MEMORY_DEBUG)
#define XMALLOC(s, h, t) ({ \ #if defined(BSDKM_CRYPTO_REGISTER)
(void)(h); (void)(t); \ /* cryptodev work functions must not sleep.see man CRYPTO_DRIVER(9) */
size_t _sz = (size_t)(s); \ #define XMALLOC(s, h, t) ({ \
int _wait_flag = curthread->td_critnest == 0 ? M_WAITOK : M_NOWAIT; \ (void)(h); (void)(t); \
void * _ptr = malloc(_sz, M_WOLFSSL, _wait_flag | M_ZERO); \ size_t _sz; void * _ptr; \
printf("info: malloc: %p, M_WOLFSSL, %zu\n", _ptr, _sz); \ _sz = (size_t)(s); \
(void *)_ptr; \ _ptr = malloc(_sz, M_WOLFSSL, M_NOWAIT | M_ZERO); \
}) printf("info: malloc: %p, M_WOLFSSL, %zu, 0x%02x\n", _ptr, _sz, \
M_NOWAIT | M_ZERO); \
(void *)_ptr; \
})
#else
#define XMALLOC(s, h, t) ({ \
(void)(h); (void)(t); \
size_t _sz; int _wait_flag; void * _ptr; \
_sz = (size_t)(s); \
_wait_flag = curthread->td_critnest == 0 ? M_WAITOK : M_NOWAIT; \
_ptr = malloc(_sz, M_WOLFSSL, _wait_flag | M_ZERO); \
printf("info: malloc: %p, M_WOLFSSL, %zu, 0x%02x\n", _ptr, _sz, \
_wait_flag); \
(void *)_ptr; \
})
#endif /* BSDKM_CRYPTO_REGISTER */
#define XFREE(p, h, t) ({ \ #define XFREE(p, h, t) ({ \
void* _xp; (void)(h); (void)(t); _xp = (p); \ void* _xp; (void)(h); (void)(t); _xp = (p); \
printf("info: free: %p, M_WOLFSSL\n", _xp); \ printf("info: free: %p, M_WOLFSSL\n", _xp); \
if(_xp) free(_xp, M_WOLFSSL); \ if(_xp) free(_xp, M_WOLFSSL); \
}) })
#else #else
#define XMALLOC(s, h, t) ({ \ #if defined(BSDKM_CRYPTO_REGISTER)
(void)(h); (void)(t); \ /* cryptodev work functions must not sleep.see man CRYPTO_DRIVER(9) */
int _wait_flag = curthread->td_critnest == 0 ? M_WAITOK : M_NOWAIT; \ #define XMALLOC(s, h, t) ({ \
void * _ptr = malloc((s), M_WOLFSSL, _wait_flag | M_ZERO); \ (void)(h); (void)(t); \
(void *)_ptr; \ void * _ptr; \
}) _ptr = malloc((s), M_WOLFSSL, M_NOWAIT | M_ZERO); \
(void *)_ptr; \
})
#else
#define XMALLOC(s, h, t) ({ \
(void)(h); (void)(t); \
int _wait_flag; void * _ptr; \
_wait_flag = curthread->td_critnest == 0 ? M_WAITOK : M_NOWAIT; \
_ptr = malloc((s), M_WOLFSSL, _wait_flag | M_ZERO); \
(void *)_ptr; \
})
#endif /* BSDKM_CRYPTO_REGISTER */
#define XFREE(p, h, t) ({ \ #define XFREE(p, h, t) ({ \
void* _xp; (void)(h); (void)(t); _xp = (p); \ void* _xp; (void)(h); (void)(t); _xp = (p); \

View File

@ -383,16 +383,19 @@ static void km_AesFree(Aes * aes) {
#endif #endif
} }
static void wolfkdriv_aes_ctx_clear(km_aes_ctx * ctx) /* clean up allocated km_aes_ctx struct.
* - cbc allocates both encrypt and decrypt, and frees both.
* - gcm uses only aes_encrypt.
* */
static void wolfkdriv_aes_ctx_clear(km_aes_ctx * ctx, int free_decrypt)
{ {
if (ctx != NULL) { if (ctx != NULL) {
km_AesFree(&ctx->aes_encrypt); km_AesFree(&ctx->aes_encrypt);
km_AesFree(&ctx->aes_decrypt);
}
#ifdef WOLFKM_DEBUG_AES if (free_decrypt) {
printf("info: exiting km_AesExitCommon\n"); km_AesFree(&ctx->aes_decrypt);
#endif /* WOLFKM_DEBUG_AES */ }
}
} }
static void wolfkdriv_identify(driver_t * driver, device_t parent) static void wolfkdriv_identify(driver_t * driver, device_t parent)
@ -671,7 +674,16 @@ static int wolfkdriv_newsession_aes(device_t dev,
newsession_cipher_out: newsession_cipher_out:
if (error != 0) { if (error != 0) {
wolfkdriv_aes_ctx_clear(&session->aes_ctx); switch (csp->csp_cipher_alg) {
case CRYPTO_AES_NIST_GCM_16:
wolfkdriv_aes_ctx_clear(&session->aes_ctx, 0);
break;
case CRYPTO_AES_CBC:
wolfkdriv_aes_ctx_clear(&session->aes_ctx, 1);
default:
break;
}
return (EINVAL); return (EINVAL);
} }
@ -713,13 +725,27 @@ static void
wolfkdriv_freesession(device_t dev, crypto_session_t cses) wolfkdriv_freesession(device_t dev, crypto_session_t cses)
{ {
wolfkdriv_session_t * session = NULL; wolfkdriv_session_t * session = NULL;
const struct crypto_session_params * csp = NULL;
(void)dev; (void)dev;
/* get the wolfkdriv_session_t context */ /* get the wolfkdriv_session_t context */
session = crypto_get_driver_session(cses); session = crypto_get_driver_session(cses);
csp = crypto_get_params(cses);
/* clean it up */ /* clean it up */
wolfkdriv_aes_ctx_clear(&session->aes_ctx); switch (csp->csp_mode) {
case CSP_MODE_CIPHER:
wolfkdriv_aes_ctx_clear(&session->aes_ctx, 1);
break;
case CSP_MODE_DIGEST:
case CSP_MODE_ETA:
break;
case CSP_MODE_AEAD:
wolfkdriv_aes_ctx_clear(&session->aes_ctx, 0);
break;
default:
__assert_unreachable();
}
#if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG) #if defined(WOLFSSL_BSDKM_VERBOSE_DEBUG)
device_printf(dev, "info: exiting freesession\n"); device_printf(dev, "info: exiting freesession\n");
@ -763,6 +789,19 @@ static int wolfkdriv_cbc_work(device_t dev, wolfkdriv_session_t * session,
is_encrypt = 0; is_encrypt = 0;
memcpy(&aes, &session->aes_ctx.aes_decrypt, sizeof(aes)); memcpy(&aes, &session->aes_ctx.aes_decrypt, sizeof(aes));
} }
#if defined(WOLFSSL_AESGCM_STREAM) && defined(WOLFSSL_SMALL_STACK) && \
!defined(WOLFSSL_AESNI)
aes.streamData = NULL;
#endif
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
{
error = wc_debug_CipherLifecycleInit(&aes.CipherLifecycleTag, NULL);
if (error) {
error = EINVAL;
goto cbc_work_out;
}
}
#endif
/* must be multiple of block size */ /* must be multiple of block size */
if (data_len % WC_AES_BLOCK_SIZE) { if (data_len % WC_AES_BLOCK_SIZE) {
@ -911,6 +950,15 @@ static int wolfkdriv_gcm_work(device_t dev, wolfkdriv_session_t * session,
!defined(WOLFSSL_AESNI) !defined(WOLFSSL_AESNI)
aes.streamData = NULL; aes.streamData = NULL;
#endif #endif
#ifdef WC_DEBUG_CIPHER_LIFECYCLE
{
error = wc_debug_CipherLifecycleInit(&aes.CipherLifecycleTag, NULL);
if (error) {
error = EINVAL;
goto gcm_work_out;
}
}
#endif
data_len = crp->crp_payload_length; data_len = crp->crp_payload_length;
if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) { if (CRYPTO_OP_IS_ENCRYPT(crp->crp_op)) {

View File

@ -129,11 +129,13 @@ void wolfkmod_vecreg_exit(void)
fpu_kern_leave(curthread, NULL); \ fpu_kern_leave(curthread, NULL); \
} while (0) } while (0)
#else #else
#define wolfkmod_fpu_kern_enter() \ #define wolfkmod_fpu_kern_enter() do { \
fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); fpu_kern_enter(curthread, NULL, FPU_KERN_NOCTX); \
} while (0)
#define wolfkmod_fpu_kern_leave() \ #define wolfkmod_fpu_kern_leave() do { \
fpu_kern_leave(curthread, NULL); fpu_kern_leave(curthread, NULL); \
} while (0)
#endif /* WOLFSSL_BSDKM_FPU_DEBUG */ #endif /* WOLFSSL_BSDKM_FPU_DEBUG */
int wolfkmod_vecreg_save(int flags_unused) int wolfkmod_vecreg_save(int flags_unused)