Skip to content

Commit a2e05f5

Browse files
committed
Address copilot comments
1 parent dcbe3db commit a2e05f5

5 files changed

Lines changed: 42 additions & 31 deletions

File tree

src/wh_client_crypto.c

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,23 +1040,33 @@ int wh_Client_AesCbcRequest(whClientContext* ctx, Aes* aes, int enc,
10401040
{
10411041
whMessageCrypto_AesCbcRequest* req;
10421042
uint8_t* dataPtr;
1043+
uint16_t blocks;
1044+
uint32_t key_len;
1045+
const uint8_t* key;
1046+
whKeyId key_id;
1047+
uint8_t* iv;
1048+
uint32_t iv_len;
1049+
uint8_t* req_in;
1050+
uint8_t* req_key;
1051+
uint8_t* req_iv;
1052+
uint32_t req_len;
10431053

10441054
if ((ctx == NULL) || (aes == NULL) || ((len % AES_BLOCK_SIZE) != 0) ||
10451055
(in == NULL)) {
10461056
return WH_ERROR_BADARGS;
10471057
}
10481058

1049-
uint16_t blocks = len / AES_BLOCK_SIZE;
1059+
blocks = len / AES_BLOCK_SIZE;
10501060

10511061
if (blocks == 0) {
10521062
return WH_ERROR_OK;
10531063
}
10541064

1055-
uint32_t key_len = aes->keylen;
1056-
const uint8_t* key = (const uint8_t*)(aes->devKey);
1057-
whKeyId key_id = WH_DEVCTX_TO_KEYID(aes->devCtx);
1058-
uint8_t* iv = (uint8_t*)aes->reg;
1059-
uint32_t iv_len = AES_IV_SIZE;
1065+
key_len = aes->keylen;
1066+
key = (const uint8_t*)(aes->devKey);
1067+
key_id = WH_DEVCTX_TO_KEYID(aes->devCtx);
1068+
iv = (uint8_t*)aes->reg;
1069+
iv_len = AES_IV_SIZE;
10601070

10611071
dataPtr = wh_CommClient_GetDataPtr(ctx->comm);
10621072
if (dataPtr == NULL) {
@@ -1065,11 +1075,11 @@ int wh_Client_AesCbcRequest(whClientContext* ctx, Aes* aes, int enc,
10651075

10661076
req = (whMessageCrypto_AesCbcRequest*)_createCryptoRequest(
10671077
dataPtr, WC_CIPHER_AES_CBC, ctx->cryptoAffinity);
1068-
uint8_t* req_in = (uint8_t*)(req + 1);
1069-
uint8_t* req_key = req_in + len;
1070-
uint8_t* req_iv = req_key + key_len;
1071-
uint32_t req_len = sizeof(whMessageCrypto_GenericRequestHeader) +
1072-
sizeof(*req) + len + key_len + iv_len;
1078+
req_in = (uint8_t*)(req + 1);
1079+
req_key = req_in + len;
1080+
req_iv = req_key + key_len;
1081+
req_len = sizeof(whMessageCrypto_GenericRequestHeader) +
1082+
sizeof(*req) + len + key_len + iv_len;
10731083

10741084
if (req_len > WOLFHSM_CFG_COMM_DATA_LEN) {
10751085
return WH_ERROR_BADARGS;
@@ -1110,7 +1120,7 @@ int wh_Client_AesCbcResponse(whClientContext* ctx, Aes* aes, uint8_t* out,
11101120
uint16_t res_len = 0;
11111121
whMessageCrypto_AesCbcResponse* res;
11121122

1113-
if ((ctx == NULL) || (aes == NULL)) {
1123+
if ((ctx == NULL) || (aes == NULL) || (out == NULL)) {
11141124
return WH_ERROR_BADARGS;
11151125
}
11161126

@@ -1124,15 +1134,13 @@ int wh_Client_AesCbcResponse(whClientContext* ctx, Aes* aes, uint8_t* out,
11241134
ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_CBC, (uint8_t**)&res);
11251135
if (ret == WH_ERROR_OK) {
11261136
uint8_t* res_out = (uint8_t*)(res + 1);
1127-
if (out != NULL) {
1128-
memcpy(out, res_out, res->sz);
1129-
/* For encryption, update the IV with the last ciphertext
1130-
* block for CBC chaining */
1131-
if (res->sz >= AES_BLOCK_SIZE) {
1132-
uint32_t last_offset =
1133-
((res->sz / AES_BLOCK_SIZE) - 1) * AES_BLOCK_SIZE;
1134-
memcpy((uint8_t*)aes->reg, out + last_offset, AES_IV_SIZE);
1135-
}
1137+
memcpy(out, res_out, res->sz);
1138+
/* For encryption, update the IV with the last ciphertext
1139+
* block for CBC chaining */
1140+
if (res->sz >= AES_BLOCK_SIZE) {
1141+
uint32_t last_offset =
1142+
((res->sz / AES_BLOCK_SIZE) - 1) * AES_BLOCK_SIZE;
1143+
memcpy((uint8_t*)aes->reg, out + last_offset, AES_IV_SIZE);
11361144
}
11371145
if (out_size != NULL) {
11381146
*out_size = res->sz;

src/wh_server_crypto.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4401,6 +4401,7 @@ int wh_Server_HandleCryptoRequest(whServerContext* ctx, uint16_t magic,
44014401
uint16_t* out_resp_size, void* resp_packet)
44024402
{
44034403
int ret = 0;
4404+
int devId = INVALID_DEVID;
44044405
whMessageCrypto_GenericRequestHeader rqstHeader = {0};
44054406
whMessageCrypto_GenericResponseHeader respHeader = {0};
44064407

@@ -4430,10 +4431,10 @@ int wh_Server_HandleCryptoRequest(whServerContext* ctx, uint16_t magic,
44304431
magic, (whMessageCrypto_GenericRequestHeader*)req_packet, &rqstHeader);
44314432

44324433
/* Compute devId from the per-message affinity field */
4433-
int devId = (rqstHeader.affinity == WH_CRYPTO_AFFINITY_HW &&
4434-
ctx->devId != INVALID_DEVID)
4435-
? ctx->devId
4436-
: INVALID_DEVID;
4434+
devId = (rqstHeader.affinity == WH_CRYPTO_AFFINITY_HW &&
4435+
ctx->devId != INVALID_DEVID)
4436+
? ctx->devId
4437+
: INVALID_DEVID;
44374438

44384439
WH_DEBUG_SERVER_VERBOSE("HandleCryptoRequest. Action:%u\n", action);
44394440
WH_DEBUG_VERBOSE_HEXDUMP("[server] Crypto Request:\n", (const uint8_t*)req_packet,
@@ -5864,6 +5865,7 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
58645865
uint16_t* out_resp_size, void* resp_packet)
58655866
{
58665867
int ret = 0;
5868+
int devId = INVALID_DEVID;
58675869
whMessageCrypto_GenericRequestHeader rqstHeader = {0};
58685870
whMessageCrypto_GenericResponseHeader respHeader = {0};
58695871

@@ -5893,10 +5895,10 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
58935895
magic, (whMessageCrypto_GenericRequestHeader*)req_packet, &rqstHeader);
58945896

58955897
/* Compute devId from the per-message affinity field */
5896-
int devId = (rqstHeader.affinity == WH_CRYPTO_AFFINITY_HW &&
5897-
ctx->devId != INVALID_DEVID)
5898-
? ctx->devId
5899-
: INVALID_DEVID;
5898+
devId = (rqstHeader.affinity == WH_CRYPTO_AFFINITY_HW &&
5899+
ctx->devId != INVALID_DEVID)
5900+
? ctx->devId
5901+
: INVALID_DEVID;
59005902

59015903
switch (action) {
59025904
case WC_ALGO_TYPE_HASH:

test/wh_test_crypto_affinity.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ static int whTest_CryptoAffinityWithCb(void)
210210
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_OK);
211211
WH_TEST_ASSERT_RETURN(affinity == WH_CRYPTO_AFFINITY_HW);
212212

213-
/* Test 2: Set HW affinity - local only, no round-trip */
213+
/* Test 2: Set SW affinity - local only, no round-trip */
214214
rc = wh_Client_SetCryptoAffinity(client, WH_CRYPTO_AFFINITY_SW);
215215
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_OK);
216216

wolfhsm/wh_client_crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ int wh_Client_AesCbcRequest(whClientContext* ctx, Aes* aes, int enc,
739739
*
740740
* @param[in] ctx Pointer to the client context
741741
* @param[in,out] aes Pointer to the AES structure (IV updated on encrypt)
742-
* @param[out] out Pointer to where the output data is placed. May be NULL.
742+
* @param[out] out Pointer to where the output data is placed. Must not be NULL.
743743
* @param[out] out_size Set to the number of bytes produced. May be NULL.
744744
* @return int Returns 0 on success, WH_ERROR_NOTREADY if the response is not
745745
* yet available, or a negative error code on failure.

wolfhsm/wh_server.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct whServerCryptoContext {
6969
#ifndef WC_NO_RNG
7070
WC_RNG rng[1];
7171
#else
72+
/* Placeholder to prevent empty struct in C90 */
7273
uint8_t WH_PAD[1];
7374
#endif
7475
} whServerCryptoContext;

0 commit comments

Comments
 (0)