Skip to content

Commit 76aeedd

Browse files
committed
Change wrap object size argument to be in/out instead of in
1 parent bcb4ae3 commit 76aeedd

File tree

8 files changed

+172
-121
lines changed

8 files changed

+172
-121
lines changed

examples/demo/client/wh_demo_client_keywrap.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
7979
WC_RNG rng[1];
8080
uint8_t key[WH_DEMO_KEYWRAP_AES_KEYSIZE];
8181
uint8_t exportedKey[WH_DEMO_KEYWRAP_AES_KEYSIZE];
82+
uint16_t exportedKeySz = sizeof(exportedKey);
8283
whNvmMetadata metadata = {
8384
.id = WH_CLIENT_KEYID_MAKE_WRAPPED_META(
8485
client->comm->client_id, WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID),
@@ -88,6 +89,7 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
8889
.len = WH_DEMO_KEYWRAP_AES_KEYSIZE};
8990
whNvmMetadata exportedMetadata;
9091
uint8_t wrappedKey[WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE];
92+
uint16_t wrappedKeySz = sizeof(wrappedKey);
9193
whKeyId wrappedKeyId;
9294

9395
const uint8_t plaintext[] = "hello, wolfSSL AES-GCM!";
@@ -131,9 +133,9 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
131133

132134
/* Now we request the server to wrap the key using the KEK we
133135
* establish above in the first step. */
134-
ret = wh_Client_KeyWrap(client, WC_CIPHER_AES_GCM, WH_DEMO_KEYWRAP_KEKID,
135-
key, sizeof(key), &metadata, wrappedKey,
136-
sizeof(wrappedKey));
136+
ret =
137+
wh_Client_KeyWrap(client, WC_CIPHER_AES_GCM, WH_DEMO_KEYWRAP_KEKID, key,
138+
sizeof(key), &metadata, wrappedKey, &wrappedKeySz);
137139
if (ret != 0) {
138140
WOLFHSM_CFG_PRINTF("Failed to wh_Client_KeyWrap %d\n", ret);
139141
goto cleanup_rng;
@@ -212,10 +214,9 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
212214
/* Exporting a wrapped key */
213215

214216
/* Request the server to unwrap and export the wrapped key we created */
215-
ret = wh_Client_KeyUnwrapAndExport(client, WC_CIPHER_AES_GCM,
216-
WH_DEMO_KEYWRAP_KEKID, wrappedKey,
217-
sizeof(wrappedKey), &exportedMetadata,
218-
exportedKey, sizeof(exportedKey));
217+
ret = wh_Client_KeyUnwrapAndExport(
218+
client, WC_CIPHER_AES_GCM, WH_DEMO_KEYWRAP_KEKID, wrappedKey,
219+
sizeof(wrappedKey), &exportedMetadata, exportedKey, &exportedKeySz);
219220
if (ret != 0) {
220221
WOLFHSM_CFG_PRINTF("Failed to wh_Client_KeyUnwrapAndCache %d\n", ret);
221222
goto cleanup_aes;

src/wh_client_keywrap.c

Lines changed: 40 additions & 26 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* wrappedKeySz)
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) {
62+
if (ctx == NULL || wrappedKeyOut == NULL || wrappedKeySz == NULL) {
6363
return WH_ERROR_BADARGS;
6464
}
6565

@@ -77,31 +77,35 @@ int wh_Client_KeyWrapResponse(whClientContext* ctx,
7777
}
7878

7979
if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_KEYWRAP ||
80-
size < sizeof(*resp) || size > sizeof(*resp) + wrappedKeySz ||
80+
size < sizeof(*resp) || size > sizeof(*resp) + resp->wrappedKeySz ||
8181
resp->cipherType != cipherType) {
8282
return WH_ERROR_ABORTED;
8383
}
8484

8585
if (resp->rc != 0) {
8686
return resp->rc;
8787
}
88+
else if (resp->wrappedKeySz > *wrappedKeySz) {
89+
return WH_ERROR_BUFFER_SIZE;
90+
}
8891

8992
/* Copy the wrapped key from the response data into wrappedKeyOut */
9093
respData = (uint8_t*)(resp + 1);
91-
memcpy(wrappedKeyOut, respData, wrappedKeySz);
94+
memcpy(wrappedKeyOut, respData, resp->wrappedKeySz);
95+
*wrappedKeySz = resp->wrappedKeySz;
9296

9397
return WH_ERROR_OK;
9498
}
9599

96100
int wh_Client_KeyWrap(whClientContext* ctx, enum wc_CipherType cipherType,
97101
uint16_t serverKeyId, void* keyIn, uint16_t keySz,
98102
whNvmMetadata* metadataIn, void* wrappedKeyOut,
99-
uint16_t wrappedKeySz)
103+
uint16_t* wrappedKeySz)
100104
{
101105
int ret = WH_ERROR_OK;
102106

103107
if (ctx == NULL || keyIn == NULL || metadataIn == NULL ||
104-
wrappedKeyOut == NULL) {
108+
wrappedKeyOut == NULL || wrappedKeySz == NULL) {
105109
return WH_ERROR_BADARGS;
106110
}
107111

@@ -159,7 +163,7 @@ int wh_Client_KeyUnwrapAndExportRequest(whClientContext* ctx,
159163
int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
160164
enum wc_CipherType cipherType,
161165
whNvmMetadata* metadataOut,
162-
void* keyOut, uint16_t keySz)
166+
void* keyOut, uint16_t* keySz)
163167
{
164168
int ret;
165169
uint16_t group;
@@ -168,7 +172,7 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
168172
whMessageKeystore_KeyUnwrapAndExportResponse* resp = NULL;
169173
uint8_t* respData;
170174

171-
if (ctx == NULL || metadataOut == NULL || keyOut == NULL) {
175+
if (ctx == NULL || metadataOut == NULL || keyOut == NULL || keySz == NULL) {
172176
return WH_ERROR_BADARGS;
173177
}
174178

@@ -188,23 +192,24 @@ int wh_Client_KeyUnwrapAndExportResponse(whClientContext* ctx,
188192

189193
if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_KEYUNWRAPEXPORT ||
190194
size < sizeof(*resp) ||
191-
size > sizeof(*resp) + sizeof(*metadataOut) + keySz ||
195+
size > sizeof(*resp) + sizeof(*metadataOut) + resp->keySz ||
192196
resp->cipherType != cipherType) {
193197
return WH_ERROR_ABORTED;
194198
}
195199

196200
if (resp->rc != WH_ERROR_OK) {
197201
return resp->rc;
198202
}
199-
else if (resp->keySz != keySz) {
203+
else if (resp->keySz > *keySz) {
200204
return WH_ERROR_BUFFER_SIZE;
201205
}
202206

203207
/* Copy the metadata and key from the response data into metadataOut and
204208
* keyOut */
205209
respData = (uint8_t*)(resp + 1);
206210
memcpy(metadataOut, respData, sizeof(*metadataOut));
207-
memcpy(keyOut, respData + sizeof(*metadataOut), keySz);
211+
memcpy(keyOut, respData + sizeof(*metadataOut), resp->keySz);
212+
*keySz = resp->keySz;
208213

209214
return WH_ERROR_OK;
210215
}
@@ -214,12 +219,12 @@ int wh_Client_KeyUnwrapAndExport(whClientContext* ctx,
214219
uint16_t serverKeyId, void* wrappedKeyIn,
215220
uint16_t wrappedKeySz,
216221
whNvmMetadata* metadataOut, void* keyOut,
217-
uint16_t keySz)
222+
uint16_t* keySz)
218223
{
219224
int ret = WH_ERROR_OK;
220225

221226
if (ctx == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
222-
keyOut == NULL)
227+
keyOut == NULL || keySz == NULL)
223228
return WH_ERROR_BADARGS;
224229

225230
ret = wh_Client_KeyUnwrapAndExportRequest(ctx, cipherType, serverKeyId,
@@ -372,7 +377,7 @@ int wh_Client_DataWrapRequest(whClientContext* ctx,
372377

373378
int wh_Client_DataWrapResponse(whClientContext* ctx,
374379
enum wc_CipherType cipherType,
375-
void* wrappedDataOut, uint32_t wrappedDataSz)
380+
void* wrappedDataOut, uint32_t* wrappedDataSz)
376381
{
377382
int ret;
378383
uint16_t group;
@@ -381,7 +386,7 @@ int wh_Client_DataWrapResponse(whClientContext* ctx,
381386
whMessageKeystore_DataWrapResponse* resp = NULL;
382387
uint8_t* respData;
383388

384-
if (ctx == NULL || wrappedDataOut == NULL) {
389+
if (ctx == NULL || wrappedDataOut == NULL || wrappedDataSz == NULL) {
385390
return WH_ERROR_BADARGS;
386391
}
387392

@@ -399,29 +404,33 @@ int wh_Client_DataWrapResponse(whClientContext* ctx,
399404
}
400405

401406
if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_DATAWRAP ||
402-
size < sizeof(*resp) || size > sizeof(*resp) + wrappedDataSz ||
403-
resp->wrappedDataSz != wrappedDataSz ||
407+
size < sizeof(*resp) || size > sizeof(*resp) + resp->wrappedDataSz ||
404408
resp->cipherType != cipherType) {
405409
return WH_ERROR_ABORTED;
406410
}
407411

408412
if (resp->rc != 0) {
409413
return resp->rc;
410414
}
415+
else if (resp->wrappedDataSz > *wrappedDataSz) {
416+
return WH_ERROR_BUFFER_SIZE;
417+
}
411418

412419
/* Copy the wrapped key from the response data into wrappedKeyOut */
413420
respData = (uint8_t*)(resp + 1);
414-
memcpy(wrappedDataOut, respData, wrappedDataSz);
421+
memcpy(wrappedDataOut, respData, resp->wrappedDataSz);
422+
*wrappedDataSz = resp->wrappedDataSz;
415423

416424
return WH_ERROR_OK;
417425
}
418426

419427
int wh_Client_DataWrap(whClientContext* ctx, enum wc_CipherType cipherType,
420428
uint16_t serverKeyId, void* dataIn, uint32_t dataInSz,
421-
void* wrappedDataOut, uint32_t wrappedDataOutSz)
429+
void* wrappedDataOut, uint32_t* wrappedDataOutSz)
422430
{
423431
int ret;
424-
if (ctx == NULL || wrappedDataOut == NULL || dataIn == NULL) {
432+
if (ctx == NULL || wrappedDataOut == NULL || dataIn == NULL ||
433+
wrappedDataOutSz == NULL) {
425434
return WH_ERROR_BADARGS;
426435
}
427436

@@ -476,7 +485,7 @@ int wh_Client_DataUnwrapRequest(whClientContext* ctx,
476485

477486
int wh_Client_DataUnwrapResponse(whClientContext* ctx,
478487
enum wc_CipherType cipherType, void* dataOut,
479-
uint32_t dataSz)
488+
uint32_t* dataSz)
480489
{
481490
int ret;
482491
uint16_t group;
@@ -503,28 +512,33 @@ int wh_Client_DataUnwrapResponse(whClientContext* ctx,
503512
}
504513

505514
if (group != WH_MESSAGE_GROUP_KEY || action != WH_KEY_DATAUNWRAP ||
506-
size < sizeof(*resp) || size > sizeof(*resp) + dataSz ||
507-
resp->dataSz != dataSz || resp->cipherType != cipherType) {
515+
size < sizeof(*resp) || size > sizeof(*resp) + resp->dataSz ||
516+
resp->cipherType != cipherType) {
508517
return WH_ERROR_ABORTED;
509518
}
510519

511520
if (resp->rc != 0) {
512521
return resp->rc;
513522
}
523+
else if (resp->dataSz > *dataSz) {
524+
return WH_ERROR_BUFFER_SIZE;
525+
}
514526

515527
/* Copy the wrapped key from the response data into wrappedKeyOut */
516528
respData = (uint8_t*)(resp + 1);
517-
memcpy(dataOut, respData, dataSz);
529+
memcpy(dataOut, respData, resp->dataSz);
530+
*dataSz = resp->dataSz;
518531

519532
return WH_ERROR_OK;
520533
}
521534
int wh_Client_DataUnwrap(whClientContext* ctx, enum wc_CipherType cipherType,
522535
uint16_t serverKeyId, void* wrappedDataIn,
523536
uint32_t wrappedDataInSz, void* dataOut,
524-
uint32_t dataOutSz)
537+
uint32_t* dataOutSz)
525538
{
526539
int ret;
527-
if (ctx == NULL || wrappedDataIn == NULL || dataOut == NULL) {
540+
if (ctx == NULL || wrappedDataIn == NULL || dataOut == NULL ||
541+
dataOutSz == NULL) {
528542
return WH_ERROR_BADARGS;
529543
}
530544

0 commit comments

Comments
 (0)