Add SHE (Secure Hardware Extension) support to wolfCrypt#10009
Add SHE (Secure Hardware Extension) support to wolfCrypt#10009night1rider wants to merge 6 commits intowolfSSL:masterfrom
Conversation
39d4163 to
54b673a
Compare
bigbrett
left a comment
There was a problem hiding this comment.
Absolutely fantastic work @night1rider. A few issues to address but overall looks great.
.wolfssl_known_macro_extras
Outdated
| WOLFSSL_SECURE_RENEGOTIATION_ON_BY_DEFAULT | ||
| WOLFSSL_SERVER_EXAMPLE | ||
| WOLFSSL_SETTINGS_FILE | ||
| WOLFSSL_SH224 |
There was a problem hiding this comment.
This was a copy paste mistake I introduced, removed.
There was a problem hiding this comment.
I'm wondering if she.{c,h} is bound to cause some annoying conflicts in automotive codebases and vendor libs. Perhaps we could namespace it to wc_she.{c,h} just to get ahead of that?
There was a problem hiding this comment.
This makes sense since we already have files following this convention (wc_encrypt, wc_lms, wc_mlkem, wc_port, etc.) there's no issue doing it now. Names should be updated now
wolfssl/wolfcrypt/she.h
Outdated
| /* Generate M1/M2/M3 for the SHE key update protocol and write to | ||
| * caller-provided buffers. Callback optional -- if a callback is | ||
| * registered it is tried first; if it returns CRYPTOCB_UNAVAILABLE | ||
| * the software path runs. This allows a secure element to generate | ||
| * M1/M2/M3 when it holds the auth key internally. |
There was a problem hiding this comment.
I think it is a little confusing to mention the "Callback optional" field here, since the dispatch to cryptoCb is internal and not exposed in this module. For example, we don't say "Callback optional" in our AES API.
Same comment for other functions in this file
There was a problem hiding this comment.
I wanted to mention usefulness of the callbacks somewhere, especially for generating M1/M2/M3 or M4/M5 when they need to access a secure element/hardware. This can be covered in the source comments and docs instead of the public header. I removed the callback language from all five API comments in wc_she.h.
There was a problem hiding this comment.
make sure to update the out-of-tree doxygen with your lovely API comments too, so it renders in the website docs!
There was a problem hiding this comment.
I will link a PR shortly with the doc update in the main body of the pr description
wolfssl/wolfcrypt/she.h
Outdated
| * in - input data (e.g. BaseKey || KDF_Constant, 32 bytes) | ||
| * inSz - length of input in bytes (zero-padded to block boundary) | ||
| * out - output buffer for 16-byte compressed result */ | ||
| WOLFSSL_TEST_VIS int wc_She_AesMp16(Aes* aes, const byte* in, word32 inSz, |
There was a problem hiding this comment.
All the other APIs use wc_SHE but this has wc_She. Unify for consistency
There was a problem hiding this comment.
I overlooked this due to it being an internal function, updated the name.
wolfcrypt/src/she.c
Outdated
| return BAD_FUNC_ARG; | ||
| } | ||
|
|
||
| return wc_CryptoCb_SheSetUid(she, uid, uidSz, ctx); |
There was a problem hiding this comment.
shouldn't this be "get" UID based on calling function? Though the crypto callbacks do only have a set function. What is the intent here?
There was a problem hiding this comment.
This was overlooked. Renamed wc_CryptoCb_SheSetUid to wc_CryptoCb_SheGetUid, WC_SHE_SET_UID to WC_SHE_GET_UID, and the union field setUid to getUid in cryptocb.h and cryptocb.c. The intent is to allow someone to use the same API after implementing the callback across different platforms. This is the same intent for GetCounter as well.
| /* Returns CRYPTOCB_UNAVAILABLE if no callback. */ | ||
| /* -------------------------------------------------------------------------- */ | ||
| #if defined(WOLF_CRYPTO_CB) && !defined(NO_WC_SHE_GETCOUNTER) | ||
| int wc_SHE_GetCounter(wc_SHE* she, word32* counter, const void* ctx) |
There was a problem hiding this comment.
Same comment here - I wonder if a default software impl with dumb global counter would be worthwhile just to enable testing? Could have a build macro enabling this to prevent inclusion in production builds maybe?
There was a problem hiding this comment.
Addressed in my previous comment regarding the "default global "wolfCrypt" UID".
wolfcrypt/src/cryptocb.c
Outdated
| #endif /* WOLFSSL_CMAC */ | ||
|
|
||
| #ifdef WOLFSSL_SHE | ||
| int wc_CryptoCb_SheSetUid(wc_SHE* she, const byte* uid, word32 uidSz, |
There was a problem hiding this comment.
I'm confused, can you set the UID? Shouldn't this be "get" UID? Same comment is made in she.c.
Are we setting ID in local wolfCrypt struct or are we getting it from HW?
There was a problem hiding this comment.
I just overlooked this, addressed. We are getting the UID from hardware and some hardware supports a challenge to get the UID to prove that the hardware is real, hence the void context parameter. This is just an API to get the UID to then pass to the generate API calls. There is no setting the UID in the structure. The struct is now very basic and holds only M1/M2/M3 if they are imported and the import function is on, plus devId for callbacks, KDF constants, etc.
| } | ||
| /* fall-through to software path */ | ||
| } | ||
| #endif |
There was a problem hiding this comment.
Need to reset ret=0 in fallback case for good measure
There was a problem hiding this comment.
Updated, overlooked this.
| int ret = wc_CryptoCb_Free(she->devId, WC_ALGO_TYPE_SHE, | ||
| 0, 0, she); | ||
| if (ret != WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)) { | ||
| return; |
There was a problem hiding this comment.
Ever a case where we want to ForceZero here?
There was a problem hiding this comment.
We already have a ForceZero on the fall-through path. The person who implements their callback should be allowed to control how free works. If they want our software cleanup, they can clean up what they want and then return CRYPTOCB_UNAVAILABLE to continue to the ForceZero. If they handle everything themselves, they return success and we skip it and the callback implementer assumes the responsibility.
wolfSSL-Fenrir-bot
left a comment
There was a problem hiding this comment.
Fenrir Automated Review — PR #10009
Scan targets checked: wolfcrypt-bugs, wolfcrypt-src
Findings: 1
Low (1)
Missing NULL check on id parameter in wc_SHE_Init_Id
File: wolfcrypt/src/she.c:155-157
Function: wc_SHE_Init_Id
Category: NULL pointer dereference
The function wc_SHE_Init_Id validates she == NULL and checks that len is within bounds (0 to WC_SHE_MAX_ID_LEN), but does not check whether id is NULL before calling XMEMCPY(she->id, id, (size_t)len). If a caller passes id = NULL with len > 0, this results in a NULL pointer dereference. By contrast, the sibling function wc_SHE_Init_Label correctly validates label == NULL before use. The impact is limited because this requires caller misuse and would crash immediately (no memory corruption beyond the dereference), but it is inconsistent with the defensive validation pattern used throughout the rest of the SHE API.
if (len < 0 || len > WC_SHE_MAX_ID_LEN) {
return BUFFER_E;
}
XMEMCPY(she->id, id, (size_t)len);Recommendation: Add a NULL check for id when len > 0, consistent with the pattern in wc_SHE_Init_Label: if (id == NULL && len > 0) { return BAD_FUNC_ARG; } or simply if (id == NULL) { return BAD_FUNC_ARG; } before the length check.
This review was generated automatically by Fenrir. Findings are non-blocking.
…_she.{c,h}, fix naming consistency, auto-enable CMAC/AES dependencies, add WC_SHE_SW_DEFAULT opt-inAddress PR wolfSSL#10009 review comments from bigbrett and Fenrir
…_she.{c,h}, fix naming consistency, auto-enable CMAC/AES dependencies, add WC_SHE_SW_DEFAULT opt-inAddress PR wolfSSL#10009 review comments from bigbrett and Fenrir
|
Rebased off current master |
Implements software-only SHE CMD_LOAD_KEY message generation (M1/M2/M3)
and verification message computation (M4/M5) with optional crypto
callback support for hardware offload.
New files:
verification computation, and crypto callback integration
API:
M1/M2/M3, callback optional)
Crypto callback integration:
WC_SHE_GENERATE_M1M2M3, WC_SHE_GENERATE_M4M5, WC_SHE_EXPORT_KEY
NO_WC_SHE_EXPORTKEY, NO_WC_SHE_IMPORT_M123
Build system:
and NO_* disable flag combinations
Ported from wolfHSM wh_she_crypto.c.