@@ -2127,11 +2127,11 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21272127 int integritySz = 0 ;
21282128 int ivSz = 0 ;
21292129 int sensSz = 0 ;
2130- BYTE * sensitiveData = NULL ;
2131- TPM2B_SYM_KEY symKey ;
21322130 TPM2B_IV ivField ;
21332131 TPM2_Packet packet ;
21342132#ifdef WOLFTPM2_PRIVATE_IMPORT
2133+ BYTE * sensitiveData = NULL ;
2134+ TPM2B_SYM_KEY symKey ;
21352135 TPM2B_DIGEST hmacKey ;
21362136 Aes enc ;
21372137 Hmac hmac_ctx ;
@@ -2144,12 +2144,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21442144 /* if using a parent then use it's integrity algorithm */
21452145 if (parentKey != NULL ) {
21462146 nameAlg = parentKey -> pub .publicArea .nameAlg ;
2147- symKey .size = parentKey -> handle .symmetric .keyBits .sym ;
21482147 }
2149- else {
2150- symKey .size = sym -> keyBits .sym ;
2151- }
2152-
21532148 digestSz = TPM2_GetHashDigestSize (nameAlg );
21542149 if (digestSz == 0 ) {
21552150 #ifdef DEBUG_WOLFTPM
@@ -2188,9 +2183,23 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21882183 TPM2_Packet_AppendSensitive (& packet , sens );
21892184 sensSz = packet .pos ;
21902185 priv -> size = integritySz + ivSz + sensSz ;
2186+ sensSz = ivSz + sensSz ;
21912187
2188+ #ifdef WOLFTPM2_PRIVATE_IMPORT
21922189 sensitiveData = & priv -> buffer [integritySz ];
2193- sensSz = ivSz + sensSz ;
2190+ if (parentKey != NULL ) {
2191+ symKey .size = parentKey -> handle .symmetric .keyBits .sym ;
2192+ }
2193+ else {
2194+ symKey .size = sym -> keyBits .sym ;
2195+ }
2196+ /* convert from bit to byte and round up */
2197+ symKey .size = (symKey .size + 7 ) / 8 ;
2198+ /* check for invalid value */
2199+ if (symKey .size > sizeof (symKey .buffer )) {
2200+ return BUFFER_E ;
2201+ }
2202+ #endif
21942203
21952204 if (innerWrap ) {
21962205 /* TODO: Inner wrap support */
@@ -2199,7 +2208,6 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
21992208 if (outerWrap ) {
22002209 #ifdef WOLFTPM2_PRIVATE_IMPORT
22012210 /* Generate symmetric key for encryption of inner values */
2202- symKey .size = (symKey .size + 7 ) / 8 ; /* convert to byte and round up */
22032211 rc = TPM2_KDFa (nameAlg , symSeed , "STORAGE" , (TPM2B_NONCE * )name ,
22042212 NULL , symKey .buffer , symKey .size );
22052213 if (rc != symKey .size ) {
@@ -2213,7 +2221,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
22132221 rc = wc_AesInit (& enc , NULL , INVALID_DEVID );
22142222 if (rc == 0 ) {
22152223 rc = wc_AesSetKey (& enc , symKey .buffer , symKey .size ,
2216- ivField .size == 0 ? NULL : ivField . buffer , AES_ENCRYPTION );
2224+ ivField .buffer , AES_ENCRYPTION );
22172225 if (rc == 0 ) {
22182226 /* use inline encryption for both IV and sensitive */
22192227 rc = wc_AesCfbEncrypt (& enc , sensitiveData , sensitiveData ,
@@ -2270,9 +2278,7 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
22702278 digestSz = TPM2_Packet_SwapU16 (digestSz );
22712279 XMEMCPY (& priv -> buffer [0 ], & digestSz , sizeof (word16 ));
22722280 #else
2273- (void )sensitiveData ;
22742281 (void )name ;
2275- (void )symKey ;
22762282 (void )sensSz ;
22772283 rc = NOT_COMPILED_IN ;
22782284 #endif
@@ -5258,7 +5264,7 @@ int wolfTPM2_LoadSymmetricKey(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key, int alg,
52585264 return BUFFER_E ;
52595265 }
52605266
5261- hashAlg = ( keySz == 32 ) ? TPM_ALG_SHA256 : TPM_ALG_SHA1 ;
5267+ hashAlg = WOLFTPM2_WRAP_DIGEST ;
52625268 hashAlgDigSz = TPM2_GetHashDigestSize (hashAlg );
52635269
52645270 /* Setup load command */
0 commit comments