Hi Felix,
As you correctly noticed, crypto callbacks currently don't provide a generic copy/free callback, or any mechanism to track ownership. This must be implemented by the user on top of the cryptocb framework.
Is there a portable way (not device-specific) to get notified when wolfSSL copies or destroys a hash context, or to customize this process?
For SHA variants, have you looked at the WOLFSSL_HASH_FLAGS option? This should do just what you are looking for - it will add flags to the context indicating whether or not the struct is a copy or the original. I imagine that using this combined with adding a refcount to your custom context should be enough for your cryptocb to know when to free the custom context when a finalization operation is detected?
Also, rolling your own end-to-end HSM solution (or even just the client side) on TC3xx via crypto callbacks is a big endeavor. Have you considered using wolfHSM? wolfHSM would do all of this for you, and is tightly integrated with wolfCrypt/wolfSSL as well as wolfBooot. We created it because we were tired of seeing so many customers "reinvent the wheel" with proprietary solutions built on top of wolfCrypt, and so set out to develop a uniform end-to-end solution to do exactly what you are trying to do (and more!).
https://www.wolfssl.com/products/wolfhsm/
https://www.wolfssl.com/documentation/m … index.html
https://github.com/wolfSSL/wolfHSM
Please get in touch if you would like more information about wolfHSM!