Skip to content

Commit 6b4a5f4

Browse files
authored
Merge pull request #214 from AlexLanzano/keywrap-test-fix
Clean up key wrap internal functions and tests
2 parents 784fc33 + 604e3f3 commit 6b4a5f4

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/wh_server_keystore.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -572,9 +572,12 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
572572
uint8_t iv[WOLFHSM_KEYWRAP_AES_GCM_IV_SIZE];
573573
uint8_t serverKey[AES_MAX_KEY_SIZE];
574574
uint32_t serverKeySz = sizeof(serverKey);
575+
uint8_t plainBlob[sizeof(*metadataIn) + WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE];
576+
uint32_t plainBlobSz = sizeof(*metadataIn) + keySz;
577+
uint8_t* encBlob;
575578

576579
if (server == NULL || keyIn == NULL || metadataIn == NULL ||
577-
wrappedKeyOut == NULL) {
580+
wrappedKeyOut == NULL || plainBlobSz > sizeof(plainBlob)) {
578581
return WH_ERROR_BADARGS;
579582
}
580583

@@ -613,15 +616,14 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
613616
}
614617

615618
/* Combine key and metadata into one blob */
616-
uint8_t plainBlob[sizeof(*metadataIn) + keySz];
617619
memcpy(plainBlob, metadataIn, sizeof(*metadataIn));
618620
memcpy(plainBlob + sizeof(*metadataIn), keyIn, keySz);
619621

620-
/* Place the encrypted blob after the IV and Auth Tag*/
621-
uint8_t* encBlob = (uint8_t*)wrappedKeyOut + sizeof(iv) + sizeof(authTag);
622+
/* Place the encrypted blob after the IV and Auth Tag */
623+
encBlob = (uint8_t*)wrappedKeyOut + sizeof(iv) + sizeof(authTag);
622624

623625
/* Encrypt the blob */
624-
ret = wc_AesGcmEncrypt(aes, encBlob, plainBlob, sizeof(plainBlob), iv,
626+
ret = wc_AesGcmEncrypt(aes, encBlob, plainBlob, plainBlobSz, iv,
625627
sizeof(iv), authTag, sizeof(authTag), NULL, 0);
626628
if (ret != 0) {
627629
wc_AesFree(aes);
@@ -650,10 +652,10 @@ static int _AesGcmUnwrapKey(whServerContext* server, uint16_t serverKeyId,
650652
uint32_t serverKeySz = sizeof(serverKey);
651653
uint8_t* encBlob = (uint8_t*)wrappedKeyIn + sizeof(iv) + sizeof(authTag);
652654
uint16_t encBlobSz = wrappedKeySz - sizeof(iv) - sizeof(authTag);
653-
uint8_t plainBlob[sizeof(*metadataOut) + keySz];
655+
uint8_t plainBlob[sizeof(*metadataOut) + WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE];
654656

655657
if (server == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
656-
keyOut == NULL) {
658+
keyOut == NULL || keySz > WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE) {
657659
return WH_ERROR_BADARGS;
658660
}
659661

test/wh_test_keywrap.c

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,6 @@
5454

5555
#endif /* HAVE_AESGCM */
5656

57-
/* RSA Specific defines */
58-
#ifndef NO_RSA
59-
60-
#define WH_TEST_RSA_KEY_OFFSET 0x2000
61-
#define WH_TEST_RSA_KEYID 3
62-
#define WH_TEST_RSA_MAX_DER_SIZE 2000
63-
64-
/* We need the extra 4 bytes at the start to store the actual wrapped key size
65-
*/
66-
#define WH_TEST_RSA_MAX_WRAPPED_KEYSIZE \
67-
(sizeof(uint32_t) + WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + \
68-
WH_TEST_RSA_MAX_DER_SIZE + sizeof(whNvmMetadata))
69-
#endif /* !NO_RSA */
70-
7157
static int _InitServerKek(whClientContext* client)
7258
{
7359
/* IMPORTANT NOTE: Server KEK is typically intrinsic or set during
@@ -101,7 +87,7 @@ static int _AesGcm_KeyWrap(whClientContext* client, WC_RNG* rng)
10187
uint8_t wrappedKey[WH_TEST_AES_WRAPPED_KEYSIZE];
10288
whKeyId wrappedKeyId;
10389
whNvmMetadata metadata = {
104-
.id = WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, 0, WH_TEST_AESGCM_KEYID),
90+
.id = WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO, client->comm->client_id, WH_TEST_AESGCM_KEYID),
10591
.label = "AES Key Label",
10692
.len = WH_TEST_AES_KEYSIZE,
10793
.flags = WH_NVM_FLAGS_NONE,

0 commit comments

Comments
 (0)