Skip to content

Commit c97e7be

Browse files
committed
Clarify new InOut arg on wrapping API. Add missing null check
1 parent 1730587 commit c97e7be

File tree

2 files changed

+44
-41
lines changed

2 files changed

+44
-41
lines changed

src/wh_client_keywrap.c

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int wh_Client_KeyWrapRequest(whClientContext* ctx,
5050

5151
int wh_Client_KeyWrapResponse(whClientContext* ctx,
5252
enum wc_CipherType cipherType,
53-
void* wrappedKeyOut, uint16_t* wrappedKeySz)
53+
void* wrappedKeyOut, uint16_t* wrappedKeyInOutSz)
5454
{
5555
int ret;
5656
uint16_t group;
@@ -59,7 +59,7 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
5959
whMessageKeystore_KeyWrapResponse* resp = NULL;
6060
uint8_t* respData;
6161

62-
if (ctx == NULL || wrappedKeyOut == NULL || wrappedKeySz == NULL) {
62+
if (ctx == NULL || wrappedKeyOut == NULL || wrappedKeyInOutSz == NULL) {
6363
return WH_ERROR_BADARGS;
6464
}
6565

@@ -85,14 +85,14 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
8585
if (resp->rc != 0) {
8686
return resp->rc;
8787
}
88-
else if (resp->wrappedKeySz > *wrappedKeySz) {
88+
else if (resp->wrappedKeySz > *wrappedKeyInOutSz) {
8989
return WH_ERROR_BUFFER_SIZE;
9090
}
9191

9292
/* Copy the wrapped key from the response data into wrappedKeyOut */
9393
respData = (uint8_t*)(resp + 1);
9494
memcpy(wrappedKeyOut, respData, resp->wrappedKeySz);
95-
*wrappedKeySz = resp->wrappedKeySz;
95+
*wrappedKeyInOutSz = resp->wrappedKeySz;
9696

9797
return WH_ERROR_OK;
9898
}
@@ -163,7 +163,7 @@ int wh_Client_KeyUnwrapAndExportRequest(whClientContext* ctx,
163163
int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
164164
enum wc_CipherType cipherType,
165165
whNvmMetadata* metadataOut,
166-
void* keyOut, uint16_t* keySz)
166+
void* keyOut, uint16_t* keyInOutSz)
167167
{
168168
int ret;
169169
uint16_t group;
@@ -172,7 +172,8 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
172172
whMessageKeystore_KeyUnwrapAndExportResponse* resp = NULL;
173173
uint8_t* respData;
174174

175-
if (ctx == NULL || metadataOut == NULL || keyOut == NULL || keySz == NULL) {
175+
if (ctx == NULL || metadataOut == NULL || keyOut == NULL ||
176+
keyInOutSz == NULL) {
176177
return WH_ERROR_BADARGS;
177178
}
178179

@@ -200,7 +201,7 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
200201
if (resp->rc != WH_ERROR_OK) {
201202
return resp->rc;
202203
}
203-
else if (resp->keySz > *keySz) {
204+
else if (resp->keySz > *keyInOutSz) {
204205
return WH_ERROR_BUFFER_SIZE;
205206
}
206207

@@ -209,7 +210,7 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
209210
respData = (uint8_t*)(resp + 1);
210211
memcpy(metadataOut, respData, sizeof(*metadataOut));
211212
memcpy(keyOut, respData + sizeof(*metadataOut), resp->keySz);
212-
*keySz = resp->keySz;
213+
*keyInOutSz = resp->keySz;
213214

214215
return WH_ERROR_OK;
215216
}
@@ -219,12 +220,12 @@ int wh_Client_KeyUnwrapAndExport(whClientContext* ctx,
219220
uint16_t serverKeyId, void* wrappedKeyIn,
220221
uint16_t wrappedKeySz,
221222
whNvmMetadata* metadataOut, void* keyOut,
222-
uint16_t* keySz)
223+
uint16_t* keyInOutSz)
223224
{
224225
int ret = WH_ERROR_OK;
225226

226227
if (ctx == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
227-
keyOut == NULL || keySz == NULL)
228+
keyOut == NULL || keyInOutSz == NULL)
228229
return WH_ERROR_BADARGS;
229230

230231
ret = wh_Client_KeyUnwrapAndExportRequest(ctx, cipherType, serverKeyId,
@@ -235,7 +236,7 @@ int wh_Client_KeyUnwrapAndExport(whClientContext* ctx,
235236

236237
do {
237238
ret = wh_Client_KeyUnwrapAndExportResponse(ctx, cipherType, metadataOut,
238-
keyOut, keySz);
239+
keyOut, keyInOutSz);
239240
} while (ret == WH_ERROR_NOTREADY);
240241

241242
return ret;
@@ -426,11 +427,11 @@ int wh_Client_DataWrapResponse(whClientContext* ctx,
426427

427428
int wh_Client_DataWrap(whClientContext* ctx, enum wc_CipherType cipherType,
428429
uint16_t serverKeyId, void* dataIn, uint32_t dataInSz,
429-
void* wrappedDataOut, uint32_t* wrappedDataOutSz)
430+
void* wrappedDataOut, uint32_t* wrappedDataInOutSz)
430431
{
431432
int ret;
432433
if (ctx == NULL || wrappedDataOut == NULL || dataIn == NULL ||
433-
wrappedDataOutSz == NULL) {
434+
wrappedDataInOutSz == NULL) {
434435
return WH_ERROR_BADARGS;
435436
}
436437

@@ -442,7 +443,7 @@ int wh_Client_DataWrap(whClientContext* ctx, enum wc_CipherType cipherType,
442443

443444
do {
444445
ret = wh_Client_DataWrapResponse(ctx, cipherType, wrappedDataOut,
445-
wrappedDataOutSz);
446+
wrappedDataInOutSz);
446447

447448
} while (ret == WH_ERROR_NOTREADY);
448449

@@ -494,7 +495,7 @@ int wh_Client_DataUnwrapResponse(whClientContext* ctx,
494495
whMessageKeystore_DataUnwrapResponse* resp = NULL;
495496
uint8_t* respData;
496497

497-
if (ctx == NULL || dataOut == NULL) {
498+
if (ctx == NULL || dataOut == NULL || dataSz == NULL) {
498499
return WH_ERROR_BADARGS;
499500
}
500501

@@ -534,11 +535,11 @@ int wh_Client_DataUnwrapResponse(whClientContext* ctx,
534535
int wh_Client_DataUnwrap(whClientContext* ctx, enum wc_CipherType cipherType,
535536
uint16_t serverKeyId, void* wrappedDataIn,
536537
uint32_t wrappedDataInSz, void* dataOut,
537-
uint32_t* dataOutSz)
538+
uint32_t* dataInOutSz)
538539
{
539540
int ret;
540541
if (ctx == NULL || wrappedDataIn == NULL || dataOut == NULL ||
541-
dataOutSz == NULL) {
542+
dataInOutSz == NULL) {
542543
return WH_ERROR_BADARGS;
543544
}
544545

@@ -549,7 +550,8 @@ int wh_Client_DataUnwrap(whClientContext* ctx, enum wc_CipherType cipherType,
549550
}
550551

551552
do {
552-
ret = wh_Client_DataUnwrapResponse(ctx, cipherType, dataOut, dataOutSz);
553+
ret =
554+
wh_Client_DataUnwrapResponse(ctx, cipherType, dataOut, dataInOutSz);
553555

554556
} while (ret == WH_ERROR_NOTREADY);
555557

wolfhsm/wh_client.h

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -884,15 +884,15 @@ int wh_Client_KeyExportDma(whClientContext* c, uint16_t keyId,
884884
* @param[in] keySz The size in bytes of the key material to wrap.
885885
* @param[in] metadataIn Pointer to the metadata for the wrapped key.
886886
* @param[out] wrappedKeyOut Pointer to store the wrapped key.
887-
* @param[in/out] wrappedKeySz IN: Size of wrappedKeyOut in bytes.
888-
* OUT: Size of the total wrapped key object.
889-
* OUT may be less than IN.
887+
* @param[in/out] wrappedKeyInOutSz IN: Size of wrappedKeyOut in bytes.
888+
* OUT: Size of the total wrapped key object
889+
* returned by the server. OUT may be less than IN.
890890
* @return int Returns 0 on success, or a negative error code on failure.
891891
*/
892892
int wh_Client_KeyWrap(whClientContext* ctx, enum wc_CipherType cipherType,
893893
uint16_t serverKeyId, void* keyIn, uint16_t keySz,
894894
whNvmMetadata* metadataIn, void* wrappedKeyOut,
895-
uint16_t* wrappedKeySz);
895+
uint16_t* wrappedKeyInOutSz);
896896

897897
/**
898898
* @brief Sends a key wrap request to the server
@@ -926,14 +926,14 @@ int wh_Client_KeyWrapRequest(whClientContext* ctx,
926926
* @param[in] ctx Pointer to the client context.
927927
* @param[in] cipherType Cipher used to wrap the key.
928928
* @param[out] wrappedKeyOut Pointer to store the wrapped key.
929-
* @param[in/out] wrappedKeySz IN: Size of the wrappedKeyOut buffer.
930-
* OUT: Size of the wrapped key object.
931-
* OUT may be less than IN
929+
* @param[in/out] wrappedKeyInOutSz IN: Size of the wrappedKeyOut buffer.
930+
* OUT: Size of the wrapped key object.
931+
* OUT may be less than IN
932932
* @return int Returns 0 on success, or a negative error code on failure.
933933
*/
934934
int wh_Client_KeyWrapResponse(whClientContext* ctx,
935935
enum wc_CipherType cipherType,
936-
void* wrappedKeyOut, uint16_t* wrappedKeySz);
936+
void* wrappedKeyOut, uint16_t* wrappedKeyInOutSz);
937937

938938
/**
939939
* @brief Requests the server to unwrap and export a wrapped key and receives
@@ -952,17 +952,17 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
952952
* @param[in] wrappedKeySz The size in bytes of the wrapped key data.
953953
* @param[out] metadataOut Pointer to store the unwrapped key metadata.
954954
* @param[out] keyOut Pointer to store the unwrapped key.
955-
* @param[in/out] keySz IN: Size of the keyOut buffer.
956-
* OUT: Size of the exported key.
957-
* OUT may be less than IN.
955+
* @param[in/out] keyInOutSz IN: Size of the keyOut buffer.
956+
* OUT: Size of the exported key returned by the
957+
* server. OUT may be less than IN.
958958
* @return int Returns 0 on success, or a negative error code on failure.
959959
*/
960960
int wh_Client_KeyUnwrapAndExport(whClientContext* ctx,
961961
enum wc_CipherType cipherType,
962962
uint16_t serverKeyId, void* wrappedKeyIn,
963963
uint16_t wrappedKeySz,
964964
whNvmMetadata* metadataOut, void* keyOut,
965-
uint16_t* keySz);
965+
uint16_t* keyInOutSz);
966966

967967
/**
968968
* @brief Requests the server to unwrap-and-export a wrapped key
@@ -998,15 +998,15 @@ int wh_Client_KeyUnwrapAndExportRequest(whClientContext* ctx,
998998
* @param[in] cipherType Cipher used when for unwrapping the key.
999999
* @param[out] metadataOut Pointer to store the unwrapped key metadata.
10001000
* @param[out] keyOut Pointer to store the unwrapped key.
1001-
* @param[in/out] keySz IN: Size of the keyOut buffer.
1002-
* OUT: Size of the exported key.
1003-
* OUT may be less than IN.
1001+
* @param[in/out] keyInOutSz IN: Size of the keyOut buffer.
1002+
* OUT: Size of the exported key returned by the
1003+
* server. OUT may be less than IN.
10041004
* @return int Returns 0 on success, or a negative error code on failure.
10051005
*/
10061006
int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
10071007
enum wc_CipherType cipherType,
10081008
whNvmMetadata* metadataOut,
1009-
void* keyOut, uint16_t* keySz);
1009+
void* keyOut, uint16_t* keyInOutSz);
10101010

10111011
/**
10121012
* @brief Requests the server to unwrap and cache a wrapped key and receives the
@@ -1081,13 +1081,14 @@ int wh_Client_KeyUnwrapAndCacheResponse(whClientContext* ctx,
10811081
* @param[in] dataInSz The size in bytes of the plaintext data.
10821082
* @param[out] wrappedDataOut The pointer to the buffer that stores the
10831083
* resulting wrapped data.
1084-
* @param[in/out] wrappedDataOutSz IN: The size in bytes of the wrapped data
1085-
* buffer. OUT: The size of the wrapped data object. OUT may be less than IN.
1084+
* @param[in/out] wrappedDataInOutSz IN: The size in bytes of wrappedDataOut
1085+
* buffer. OUT: The size of the wrapped data object returned from the server.
1086+
* OUT may be less than IN.
10861087
* @return int Returns 0 on success, or a negative error code on failure.
10871088
*/
10881089
int wh_Client_DataWrap(whClientContext* ctx, enum wc_CipherType cipherType,
10891090
uint16_t serverKeyId, void* dataIn, uint32_t dataInSz,
1090-
void* wrappedDataOut, uint32_t* wrappedDataOutSz);
1091+
void* wrappedDataOut, uint32_t* wrappedDataInOutSz);
10911092

10921093
/**
10931094
* @brief Helper function to unwrap a wrapped data object using a specified key
@@ -1103,15 +1104,15 @@ int wh_Client_DataWrap(whClientContext* ctx, enum wc_CipherType cipherType,
11031104
* @param[in] wrappedDataInSz The size in bytes of the wrapped data object.
11041105
* @param[out] dataOut The pointer to the buffer that stores the
11051106
* resulting unwrapped data.
1106-
* @param[in/out] dataOutSz IN: The size in bytes of the unwrapped data buffer.
1107-
* OUT: The size of the unwrapped data object.
1108-
* OUT may be less than IN.
1107+
* @param[in/out] dataInOutSz IN: The size in bytes of dataOut.
1108+
* OUT: The size of the unwrapped data object return
1109+
* by the server. OUT may be less than IN.
11091110
* @return int Returns 0 on success, or a negative error code on failure.
11101111
*/
11111112
int wh_Client_DataUnwrap(whClientContext* ctx, enum wc_CipherType cipherType,
11121113
uint16_t serverKeyId, void* wrappedDataIn,
11131114
uint32_t wrappedDataInSz, void* dataOut,
1114-
uint32_t* dataOutSz);
1115+
uint32_t* dataInOutSz);
11151116

11161117
/* Counter functions */
11171118
int wh_Client_CounterInitRequest(whClientContext* c, whNvmId counterId,

0 commit comments

Comments
 (0)