Skip to content

Commit d44efb6

Browse files
committed
Update rsa key wrap test to handle a dynamic wrapped key length
1 parent 0dae633 commit d44efb6

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

test/wh_test_keywrap.c

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,13 @@
6060

6161
#define WH_TEST_RSA_KEY_OFFSET 0x2000
6262
#define WH_TEST_RSA_KEYID 3
63-
#define WH_TEST_RSA_DER_SIZE 1766
64-
#define WH_TEST_RSA_WRAPPED_KEYSIZE \
65-
(WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_RSA_DER_SIZE + \
66-
sizeof(whNvmMetadata))
63+
#define WH_TEST_RSA_MAX_DER_SIZE 2000
64+
65+
/* We need the extra 4 bytes at the start to store the actual wrapped key size
66+
*/
67+
#define WH_TEST_RSA_MAX_WRAPPED_KEYSIZE \
68+
(sizeof(uint32_t) + WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + \
69+
WH_TEST_RSA_MAX_DER_SIZE + sizeof(whNvmMetadata))
6770
#endif /* !NO_RSA */
6871

6972
static int _InitServerKek(whClientContext* ctx)
@@ -356,59 +359,64 @@ static int _AesGcm_UseWrappedKeyFromNvm(whClientContext* client, void* flashCtx,
356359
#ifndef NO_RSA
357360

358361
static int _Rsa_WriteWrappedKeyToNvm(whClientContext* client, void* flashCtx,
359-
whFlashCb* flashCb, WC_RNG* rng)
362+
whFlashCb* flashCb)
360363
{
361364
int ret;
362365
whKeyId rsaKeyId = WH_TEST_RSA_KEYID;
363366
RsaKey rsaKey[1];
364-
uint8_t rsaKeyDer[WH_TEST_RSA_DER_SIZE];
367+
uint8_t rsaKeyDer[WH_TEST_RSA_MAX_DER_SIZE];
365368
int rsaKeyDerSz;
366-
uint8_t rsaWrappedKey[WH_TEST_RSA_WRAPPED_KEYSIZE];
369+
uint8_t rsaWrappedKey[WH_TEST_RSA_MAX_WRAPPED_KEYSIZE];
370+
uint32_t rsaWrappedKeySz;
367371
whNvmMetadata rsaKeyMetadata = {
368372
.label = "RSA 3072 Key",
369373
.access = WH_NVM_ACCESS_ANY,
370374
.flags = WH_NVM_FLAGS_NONE,
371375
.id = WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, 0, rsaKeyId),
372-
.len = WH_TEST_RSA_DER_SIZE};
376+
.len = 0};
373377

374378
/* Initialize the RSA key */
375-
ret = wc_InitRsaKey(rsaKey, NULL);
379+
ret = wc_InitRsaKey_ex(rsaKey, NULL, WH_DEV_ID);
376380
if (ret != WH_ERROR_OK) {
377381
WH_ERROR_PRINT("Failed to wc_InitRsaKey %d\n", ret);
378382
return ret;
379383
}
380384

381385
/* Generate the RSA key */
382-
ret = wc_MakeRsaKey(rsaKey, 3072, 65537, rng);
386+
ret = wh_Client_RsaMakeExportKey(client, 3072, 65537, rsaKey);
383387
if (ret != WH_ERROR_OK) {
384-
WH_ERROR_PRINT("Failed to wc_MakeRsaKey %d\n", ret);
388+
WH_ERROR_PRINT("Failed to wh_Client_RsaMakeExportKey %d\n", ret);
385389
return ret;
386390
}
387391

388392
/* Convert the RSA key to DER format so it can be stored in flash */
389-
rsaKeyDerSz = wc_RsaKeyToDer(rsaKey, rsaKeyDer, WH_TEST_RSA_DER_SIZE);
393+
rsaKeyDerSz = wc_RsaKeyToDer(rsaKey, rsaKeyDer, WH_TEST_RSA_MAX_DER_SIZE);
390394
if (rsaKeyDerSz < 0) {
391395
ret = rsaKeyDerSz;
392396
WH_ERROR_PRINT("Failed to wc_RsaKeyToDer %d\n", ret);
393397
return ret;
394398
}
395399

396-
/* Validate the DER size */
397-
if (rsaKeyDerSz != WH_TEST_RSA_DER_SIZE) {
398-
WH_ERROR_PRINT("Unexpected RSA DER size\n");
399-
return WH_ERROR_ABORTED;
400-
}
400+
rsaKeyMetadata.len = rsaKeyDerSz;
401+
402+
/* Since the size of the DER can change depending on the server
403+
* configuration we need to store the size of the wrapped key in flash as
404+
* well */
405+
rsaWrappedKeySz = WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE +
406+
sizeof(whNvmMetadata) + rsaKeyDerSz;
407+
memcpy(rsaWrappedKey, &rsaWrappedKeySz, sizeof(rsaWrappedKeySz));
401408

402-
/* Request the server to wrap the RSA key using the server KEK */
409+
/* Request the server to wrap the RSA key using the server KEK.
410+
* Leave the beginning 4 bytes to hold the wrapped key size. */
403411
ret = wh_Client_KeyWrap(client, WC_CIPHER_AES_GCM, WH_TEST_KEKID, rsaKeyDer,
404-
rsaKeyDerSz, &rsaKeyMetadata, rsaWrappedKey,
405-
sizeof(rsaWrappedKey));
412+
rsaKeyDerSz, &rsaKeyMetadata, &rsaWrappedKey[4],
413+
rsaWrappedKeySz);
406414
if (ret != 0) {
407415
WH_ERROR_PRINT("Failed to wh_Client_KeyWrap %d\n", ret);
408416
return ret;
409417
}
410418

411-
/* Write the wrapped RSA key to a specified location in flash */
419+
/* Write the wrapped RSA key to a specified location in flash. */
412420
ret = flashCb->Program(flashCtx, WH_TEST_RSA_KEY_OFFSET,
413421
sizeof(rsaWrappedKey), rsaWrappedKey);
414422
if (ret != 0) {
@@ -433,9 +441,10 @@ static int _Rsa_UseWrappedKeyFromNvm(whClientContext* client, void* flashCtx,
433441
int ret;
434442
whKeyId serverKekId = WH_TEST_KEKID;
435443

436-
RsaKey rsa[1];
437-
whKeyId rsaKeyId = WH_TEST_RSA_KEYID;
438-
uint8_t rsaWrappedKey[WH_TEST_RSA_WRAPPED_KEYSIZE];
444+
RsaKey rsa[1];
445+
whKeyId rsaKeyId = WH_TEST_RSA_KEYID;
446+
uint8_t rsaWrappedKey[WH_TEST_RSA_MAX_WRAPPED_KEYSIZE];
447+
uint32_t rsaWrappedKeySz;
439448

440449
const uint8_t plaintext[] = "Hello with RSA-3072!";
441450
uint8_t ciphertext[384];
@@ -449,9 +458,13 @@ static int _Rsa_UseWrappedKeyFromNvm(whClientContext* client, void* flashCtx,
449458
return ret;
450459
}
451460

461+
/* Get the size of the wrapped key by reading the first 4 bytes of the
462+
* wrapped key */
463+
memcpy(&rsaWrappedKeySz, rsaWrappedKey, sizeof(rsaWrappedKeySz));
464+
452465
/* Request the server to unwrap and cache the key for us */
453466
ret = wh_Client_KeyUnwrapAndCache(client, WC_CIPHER_AES_GCM, serverKekId,
454-
rsaWrappedKey, sizeof(rsaWrappedKey),
467+
&rsaWrappedKey[4], rsaWrappedKeySz,
455468
&rsaKeyId);
456469
if (ret != 0) {
457470
WH_ERROR_PRINT("Failed to wh_Client_KeyUnwrapAndCache %d\n", ret);
@@ -532,7 +545,7 @@ int whTest_Client_WriteWrappedKeysToNvm(whClientContext* client, void* flashCtx,
532545
#endif /* HAVE_AESGCM */
533546

534547
#ifndef NO_RSA
535-
ret = _Rsa_WriteWrappedKeyToNvm(client, flashCtx, flashCb, rng);
548+
ret = _Rsa_WriteWrappedKeyToNvm(client, flashCtx, flashCb);
536549
if (ret != WH_ERROR_OK) {
537550
WH_ERROR_PRINT("Failed to _Rsa_WriteWrappedKeyToNvm %d\n", ret);
538551
goto cleanup_and_exit;

0 commit comments

Comments
 (0)