@@ -108,7 +108,7 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
108108 BYTE * param , * encParam = NULL ;
109109 int paramSz , encParamSz = 0 ;
110110 int i , authPos ;
111- int tmpSz = 0 ; /* Used to calculate the new total size of the Auth Area */
111+ int authTotalSzPos = 0 ;
112112#ifndef WOLFTPM2_NO_WOLFCRYPT
113113 UINT32 handleValue1 , handleValue2 , handleValue3 ;
114114 int handlePos ;
@@ -120,8 +120,8 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
120120 /* Parse Auth */
121121 TPM2_Packet_ParseU32 (packet , & authSz );
122122 packet -> pos -= sizeof (authSz );
123- /* Later Auth Area size is updated */
124- TPM2_Packet_MarkU32 (packet , & tmpSz );
123+ /* Get position for total auth size to be updated later */
124+ TPM2_Packet_MarkU32 (packet , & authTotalSzPos );
125125 /* Mark the position of the Auth Area data */
126126 authPos = packet -> pos ;
127127 packet -> pos += authSz ;
@@ -174,17 +174,32 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
174174 }
175175 }
176176
177- /* Note: Copy between TPM2_AUTH_SESSION and TPMS_AUTH_COMMAND is allowed */
178- XMEMCPY (& authCmd , session , sizeof (TPMS_AUTH_COMMAND ));
179-
180- if (TPM2_IS_HMAC_SESSION (session -> sessionHandle ) ||
181- TPM2_IS_POLICY_SESSION (session -> sessionHandle ))
177+ /* Build auth */
178+ XMEMSET (& authCmd , 0 , sizeof (authCmd ));
179+ authCmd .sessionHandle = session -> sessionHandle ;
180+ authCmd .sessionAttributes = session -> sessionAttributes ;
181+ authCmd .nonce .size = session -> nonceCaller .size ;
182+ XMEMCPY (authCmd .nonce .buffer , session -> nonceCaller .buffer ,
183+ authCmd .nonce .size );
184+
185+ /* Password Auth */
186+ if (session -> sessionHandle == TPM_RS_PW ) {
187+ authCmd .hmac .size = session -> auth .size ;
188+ XMEMCPY (authCmd .hmac .buffer , session -> auth .buffer ,
189+ session -> auth .size );
190+ }
191+ /* HMAC or Policy Session */
192+ else if (TPM2_IS_HMAC_SESSION (session -> sessionHandle ) ||
193+ TPM2_IS_POLICY_SESSION (session -> sessionHandle ))
182194 {
183195 #ifndef WOLFTPM2_NO_WOLFCRYPT
184196 TPM2B_NAME name1 , name2 , name3 ;
185197 TPM2B_DIGEST hash ;
186198 #endif
187199
200+ /* default is a HMAC output (using alg authHash) */
201+ authCmd .hmac .size = TPM2_GetHashDigestSize (session -> authHash );
202+
188203 /* if param enc is not supported for this command then clear flag */
189204 /* session attribute flags are from TPM perspective */
190205 if ((info -> flags & (CMD_FLAG_ENC2 | CMD_FLAG_ENC4 )) == 0 ) {
@@ -240,16 +255,28 @@ static int TPM2_CommandProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
240255 #endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_HMAC */
241256 }
242257
243- /* Replace auth in session */
258+ /* Place session auth */
244259 packet -> pos = authPos ;
245260 TPM2_Packet_AppendAuthCmd (packet , & authCmd );
246261 authPos = packet -> pos ; /* update auth position */
247262 }
248263
249- /* Update the Auth Area size in the command packet */
250- TPM2_Packet_PlaceU32 (packet , tmpSz );
264+ /* Update the Auth Area total size in the command packet */
265+ i = TPM2_Packet_PlaceU32 (packet , authTotalSzPos );
266+
267+ #ifdef DEBUG_WOLFTPM
268+ if ((int )authSz != i ) {
269+ /* actual auth size did not match estimated size from
270+ * TPM2_Packet_AppendAuth */
271+ printf ("Error: Calculated auth size %d did not match actual %d!\n" ,
272+ authSz , i );
273+ return BUFFER_E ;
274+ }
275+ #endif
251276
252277 (void )cmdCode ;
278+ (void )i ;
279+
253280 return rc ;
254281}
255282
@@ -343,6 +370,11 @@ static int TPM2_ResponseProcess(TPM2_CTX* ctx, TPM2_Packet* packet,
343370 return TPM_RC_HMAC ;
344371 }
345372 }
373+
374+ /* Save off last known HMAC */
375+ session -> hmac .size = authRsp .hmac .size ;
376+ XMEMCMP (session -> hmac .buffer , authRsp .hmac .buffer ,
377+ authRsp .hmac .size );
346378 #else
347379 (void )cmdCode ;
348380 #endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_HMAC */
0 commit comments