@@ -572,9 +572,11 @@ 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+ uint8_t * encBlob ;
575577
576578 if (server == NULL || keyIn == NULL || metadataIn == NULL ||
577- wrappedKeyOut == NULL ) {
579+ wrappedKeyOut == NULL || keySz > WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE ) {
578580 return WH_ERROR_BADARGS ;
579581 }
580582
@@ -613,12 +615,11 @@ static int _AesGcmWrapKey(whServerContext* server, whKeyId serverKeyId,
613615 }
614616
615617 /* Combine key and metadata into one blob */
616- uint8_t plainBlob [sizeof (* metadataIn ) + keySz ];
617618 memcpy (plainBlob , metadataIn , sizeof (* metadataIn ));
618619 memcpy (plainBlob + sizeof (* metadataIn ), keyIn , keySz );
619620
620621 /* Place the encrypted blob after the IV and Auth Tag*/
621- uint8_t * encBlob = (uint8_t * )wrappedKeyOut + sizeof (iv ) + sizeof (authTag );
622+ encBlob = (uint8_t * )wrappedKeyOut + sizeof (iv ) + sizeof (authTag );
622623
623624 /* Encrypt the blob */
624625 ret = wc_AesGcmEncrypt (aes , encBlob , plainBlob , sizeof (plainBlob ), iv ,
@@ -650,10 +651,10 @@ static int _AesGcmUnwrapKey(whServerContext* server, uint16_t serverKeyId,
650651 uint32_t serverKeySz = sizeof (serverKey );
651652 uint8_t * encBlob = (uint8_t * )wrappedKeyIn + sizeof (iv ) + sizeof (authTag );
652653 uint16_t encBlobSz = wrappedKeySz - sizeof (iv ) - sizeof (authTag );
653- uint8_t plainBlob [sizeof (* metadataOut ) + keySz ];
654+ uint8_t plainBlob [sizeof (* metadataOut ) + WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE ];
654655
655656 if (server == NULL || wrappedKeyIn == NULL || metadataOut == NULL ||
656- keyOut == NULL ) {
657+ keyOut == NULL || keySz > WOLFHSM_CFG_KEYWRAP_MAX_KEY_SIZE ) {
657658 return WH_ERROR_BADARGS ;
658659 }
659660
0 commit comments