-
Notifications
You must be signed in to change notification settings - Fork 38
add support for ECC_MAKE_PUB and ECC_CHECK_PUB crypto callbacks #451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rizlik
wants to merge
2
commits into
wolfSSL:main
Choose a base branch
from
rizlik:ecc_new_cbs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,844
−60
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2790,57 +2790,282 @@ int wh_Client_EccVerify(whClientContext* ctx, ecc_key* key, const uint8_t* sig, | |
| return ret; | ||
| } | ||
|
|
||
| #if 0 | ||
| /* Response half for ECC make-public. Single-shot receive: returns | ||
| * WH_ERROR_NOTREADY if the reply has not arrived yet. */ | ||
| static int _EccMakePubResponse(whClientContext* ctx, uint8_t* pubOut, | ||
| uint16_t* inout_pubOutSz) | ||
| { | ||
| int ret; | ||
| uint16_t group; | ||
| uint16_t action; | ||
| uint16_t res_len = 0; | ||
| uint8_t* dataPtr; | ||
| whMessageCrypto_EccMakePubResponse* res = NULL; | ||
|
|
||
| dataPtr = wh_CommClient_GetDataPtr(ctx->comm); | ||
| if (dataPtr == NULL) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); | ||
| if (ret != WH_ERROR_OK) { | ||
| return ret; | ||
| } | ||
|
|
||
| ret = _getCryptoResponse(dataPtr, WC_PK_TYPE_EC_MAKE_PUB, (uint8_t**)&res); | ||
| if (ret >= 0) { | ||
| const size_t hdr_sz = | ||
| sizeof(whMessageCrypto_GenericResponseHeader) + sizeof(*res); | ||
| /* Defensive bound: res->pubSz must fit within the actual received | ||
| * frame */ | ||
| if (res_len < hdr_sz || res->pubSz > (res_len - hdr_sz)) { | ||
| return WH_ERROR_ABORTED; | ||
| } | ||
| if (res->pubSz > (uint32_t)(*inout_pubOutSz)) { | ||
| /* Report the size the caller needs so it can re-call */ | ||
| *inout_pubOutSz = (uint16_t)res->pubSz; | ||
| return WH_ERROR_BUFFER_SIZE; | ||
| } | ||
| memcpy(pubOut, (uint8_t*)(res + 1), res->pubSz); | ||
| *inout_pubOutSz = (uint16_t)res->pubSz; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| int wh_Client_EccMakePub(whClientContext* ctx, ecc_key* key, uint8_t* pubOut, | ||
| uint16_t* inout_pubOutSz) | ||
| { | ||
| int ret = WH_ERROR_OK; | ||
| whMessageCrypto_EccMakePubRequest* req = NULL; | ||
| uint8_t* dataPtr = NULL; | ||
|
|
||
| /* Transaction state */ | ||
| whKeyId key_id; | ||
| int evict = 0; | ||
|
|
||
| WH_DEBUG_CLIENT_VERBOSE("ctx:%p key:%p pubOut:%p inout_pubOutSz:%p\n", ctx, | ||
| key, pubOut, inout_pubOutSz); | ||
|
|
||
| /* Validate response-side args upfront so we never send a request that the | ||
| * matching *Response would then reject without consuming the reply. */ | ||
| if ((ctx == NULL) || (key == NULL) || (pubOut == NULL) || | ||
| (inout_pubOutSz == NULL)) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| key_id = WH_DEVCTX_TO_KEYID(key->devCtx); | ||
| /* Import key if necessary */ | ||
| if (WH_KEYID_ISERASED(key_id)) { | ||
| /* Must import the key to the server and evict it afterwards */ | ||
| uint8_t keyLabel[] = "TempEccMakePub"; | ||
| whNvmFlags flags = WH_NVM_FLAGS_USAGE_ANY; | ||
|
|
||
| ret = wh_Client_EccImportKey(ctx, key, &key_id, flags, sizeof(keyLabel), | ||
| keyLabel); | ||
| if (ret == 0) { | ||
| evict = 1; | ||
| } | ||
| } | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| dataPtr = wh_CommClient_GetDataPtr(ctx->comm); | ||
| if (dataPtr == NULL) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| } | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| /* Request Message */ | ||
| uint16_t group = WH_MESSAGE_GROUP_CRYPTO; | ||
| uint16_t action = WC_ALGO_TYPE_PK; | ||
| uint32_t options = 0; | ||
|
|
||
| uint16_t req_len = sizeof(whMessageCrypto_GenericRequestHeader) + | ||
| sizeof(whMessageCrypto_EccMakePubRequest); | ||
|
|
||
| if (req_len <= WOLFHSM_CFG_COMM_DATA_LEN) { | ||
| /* Setup generic header and get pointer to request data */ | ||
| req = (whMessageCrypto_EccMakePubRequest*)_createCryptoRequest( | ||
| dataPtr, WC_PK_TYPE_EC_MAKE_PUB, ctx->cryptoAffinity); | ||
|
|
||
| if (evict != 0) { | ||
| options |= WH_MESSAGE_CRYPTO_ECCMAKEPUB_OPTIONS_EVICT; | ||
| } | ||
|
|
||
| memset(req, 0, sizeof(*req)); | ||
| req->options = options; | ||
| req->keyId = key_id; | ||
|
|
||
| WH_DEBUG_CLIENT_VERBOSE("EccMakePub req: key_id=%x options=%u\n", | ||
| key_id, (unsigned int)options); | ||
|
|
||
| /* write request */ | ||
| ret = wh_Client_SendRequest(ctx, group, action, req_len, dataPtr); | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| /* Server will evict at this point. Reset evict */ | ||
| evict = 0; | ||
|
|
||
| do { | ||
| ret = _EccMakePubResponse(ctx, pubOut, inout_pubOutSz); | ||
| } while (ret == WH_ERROR_NOTREADY); | ||
| } | ||
| } | ||
| else { | ||
| /* Request length is too long */ | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| } | ||
| /* Evict the key manually on error */ | ||
| if (evict != 0) { | ||
| (void)wh_Client_KeyEvict(ctx, key_id); | ||
| } | ||
| WH_DEBUG_CLIENT_VERBOSE("ret:%d\n", ret); | ||
| return ret; | ||
| } | ||
|
|
||
| #ifdef HAVE_ECC_CHECK_KEY | ||
| /* Response half for ECC key validation. The verdict is the response code | ||
| * itself: 0 when the key is valid, a wolfCrypt error when it is not. */ | ||
| static int _EccCheckPubKeyResponse(whClientContext* ctx) | ||
| { | ||
| int ret; | ||
| uint16_t group; | ||
| uint16_t action; | ||
| uint16_t res_len = 0; | ||
| uint8_t* dataPtr; | ||
| whMessageCrypto_EccCheckResponse* res = NULL; | ||
|
|
||
| dataPtr = wh_CommClient_GetDataPtr(ctx->comm); | ||
| if (dataPtr == NULL) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
|
|
||
| ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr); | ||
| if (ret != WH_ERROR_OK) { | ||
| return ret; | ||
| } | ||
|
|
||
| /* A negative rc here is the verdict on an invalid key rather than a | ||
| * transport failure, and is handed back to wolfCrypt verbatim. */ | ||
| ret = _getCryptoResponse(dataPtr, WC_PK_TYPE_EC_CHECK_PUB_KEY, | ||
| (uint8_t**)&res); | ||
| if (ret >= 0) { | ||
| const size_t hdr_sz = | ||
| sizeof(whMessageCrypto_GenericResponseHeader) + sizeof(*res); | ||
| /* A success rc must be accompanied by an affirmative body */ | ||
| if ((res_len < hdr_sz) || (res->ok == 0)) { | ||
| return WH_ERROR_ABORTED; | ||
| } | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| int wh_Client_EccCheckPubKey(whClientContext* ctx, ecc_key* key, | ||
| const uint8_t* pub_key, uint16_t pub_key_len) | ||
| const uint8_t* pub_key, uint16_t pub_key_len, | ||
| int check_order, int check_priv) | ||
| { | ||
| /* TODO: Check if keyid is set on incoming key. | ||
| * if not, import private key to server | ||
| * send request with pub key der | ||
| * server creates new key with private and public. check | ||
| * evict temp key | ||
| */ | ||
| int ret; | ||
| int ret = WH_ERROR_OK; | ||
| whMessageCrypto_EccCheckRequest* req = NULL; | ||
| uint8_t* dataPtr = NULL; | ||
|
|
||
| /* Transaction state */ | ||
| whKeyId key_id; | ||
| int evict = 0; | ||
|
|
||
| WH_DEBUG_CLIENT_VERBOSE("ctx:%p key:%p pub_key:%p pub_key_len:%u\n", ctx, | ||
| key, pub_key, pub_key_len); | ||
|
|
||
| if ( (ctx == NULL) || | ||
| (key == NULL) || | ||
| (pub_key == NULL) || | ||
| (pub_key_len == 0) ) { | ||
| if ((ctx == NULL) || (key == NULL) || | ||
| ((pub_key == NULL) && (pub_key_len > 0))) { | ||
| return WH_ERROR_BADARGS; | ||
| } | ||
| int curve_id = wc_ecc_get_curve_id(key->idx); | ||
| whKeyId key_id = WH_DEVCTX_TO_KEYID(key->devCtx); | ||
|
|
||
| /* Request packet */ | ||
| wh_Packet_pk_ecc_check_req* req = &packet->pkEccCheckReq; | ||
| uint8_t* req_pub_key = (uint8_t*)(req + 1); | ||
| key_id = WH_DEVCTX_TO_KEYID(key->devCtx); | ||
| /* Import key if necessary */ | ||
| if (WH_KEYID_ISERASED(key_id)) { | ||
| /* Must import the key to the server and evict it afterwards */ | ||
| uint8_t keyLabel[] = "TempEccCheck"; | ||
| whNvmFlags flags = WH_NVM_FLAGS_USAGE_ANY; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comment on flags as above |
||
|
|
||
| req->type = WC_PK_TYPE_EC_CHECK_PRIV_KEY; | ||
| req->keyId = key_id; | ||
| req->curveId = curve_id; | ||
| ret = wh_Client_EccImportKey(ctx, key, &key_id, flags, sizeof(keyLabel), | ||
| keyLabel); | ||
| if (ret == 0) { | ||
| evict = 1; | ||
| } | ||
| } | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| dataPtr = wh_CommClient_GetDataPtr(ctx->comm); | ||
| if (dataPtr == NULL) { | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| } | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| /* Request Message */ | ||
| uint16_t group = WH_MESSAGE_GROUP_CRYPTO; | ||
| uint16_t action = WC_ALGO_TYPE_PK; | ||
| uint32_t options = 0; | ||
|
|
||
| /* Response packet */ | ||
| wh_Packet_pk_ecc_check_res* res = &packet->pkEccCheckRes; | ||
| /* Compute the total length in a wide type so a large pub_key_len | ||
| * cannot wrap the sum before the bounds check below. Narrowing to | ||
| * the uint16_t wire length is safe once the check has passed. */ | ||
| uint32_t req_len = (uint32_t)sizeof(whMessageCrypto_GenericRequestHeader) + | ||
| (uint32_t)sizeof(whMessageCrypto_EccCheckRequest) + | ||
| pub_key_len; | ||
|
|
||
| if (req_len <= WOLFHSM_CFG_COMM_DATA_LEN) { | ||
| /* Setup generic header and get pointer to request data */ | ||
| req = (whMessageCrypto_EccCheckRequest*)_createCryptoRequest( | ||
| dataPtr, WC_PK_TYPE_EC_CHECK_PUB_KEY, ctx->cryptoAffinity); | ||
|
|
||
| /* write request */ | ||
| ret = wh_Client_SendRequest(ctx, group, | ||
| WC_ALGO_TYPE_PK, | ||
| WH_PACKET_STUB_SIZE + sizeof(packet->pkEccCheckReq), | ||
| (uint8_t*)packet); | ||
| /* read response */ | ||
| if (ret == 0) { | ||
| do { | ||
| ret = wh_Client_RecvResponse(ctx, &group, &action, &dataSz, | ||
| (uint8_t*)packet); | ||
| } while (ret == WH_ERROR_NOTREADY); | ||
| if (evict != 0) { | ||
| options |= WH_MESSAGE_CRYPTO_ECCCHECK_OPTIONS_EVICT; | ||
| } | ||
|
|
||
| memset(req, 0, sizeof(*req)); | ||
| req->options = options; | ||
| req->keyId = key_id; | ||
| req->curveId = (uint32_t)wc_ecc_get_curve_id(key->idx); | ||
| req->checkOrder = (uint32_t)(check_order != 0); | ||
| req->checkPriv = (uint32_t)(check_priv != 0); | ||
| req->pubSz = pub_key_len; | ||
| if ((pub_key != NULL) && (pub_key_len > 0)) { | ||
| memcpy((uint8_t*)(req + 1), pub_key, pub_key_len); | ||
| } | ||
|
|
||
| WH_DEBUG_CLIENT_VERBOSE("EccCheckPubKey req: key_id=%x, " | ||
| "pub_key_len=%u, options=%u\n", | ||
| key_id, (unsigned int)pub_key_len, | ||
| (unsigned int)options); | ||
|
|
||
| /* write request */ | ||
| ret = wh_Client_SendRequest(ctx, group, action, (uint16_t)req_len, | ||
| dataPtr); | ||
|
|
||
| if (ret == WH_ERROR_OK) { | ||
| /* Server will evict at this point. Reset evict */ | ||
| evict = 0; | ||
|
|
||
| do { | ||
| ret = _EccCheckPubKeyResponse(ctx); | ||
| } while (ret == WH_ERROR_NOTREADY); | ||
| } | ||
| } | ||
| else { | ||
| /* Request length is too long */ | ||
| ret = WH_ERROR_BADARGS; | ||
| } | ||
| } | ||
| if (ret == 0) { | ||
| if (packet->rc != 0) | ||
| ret = packet->rc; | ||
| /* Evict the key manually on error */ | ||
| if (evict != 0) { | ||
| (void)wh_Client_KeyEvict(ctx, key_id); | ||
| } | ||
| WH_DEBUG_CLIENT_VERBOSE("ret:%d\n", ret); | ||
| return ret; | ||
| } | ||
| #endif /* 0 */ | ||
| #endif /* HAVE_ECC_CHECK_KEY */ | ||
|
|
||
| #endif /* HAVE_ECC */ | ||
|
|
||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should probably restrict usage flags to only what is necessary here. Also do you think we should set ephemeral flag?