@@ -1835,9 +1835,12 @@ TPM_RC FwImportVerifyAndDecrypt(
18351835 }
18361836 }
18371837 if (rc == 0 ) {
1838- /* Always run TPM2_ConstantCompare so timing doesn't leak size match */
1838+ /* Always run TPM2_ConstantCompare over min(sizes) so timing doesn't
1839+ * leak size match and we don't read past integrity[integritySize] */
1840+ word32 cmpSz = (integritySize < (UINT16 )digestSz ) ?
1841+ (word32 )integritySize : (word32 )digestSz ;
18391842 sizeMismatch = (integritySize != (UINT16 )digestSz );
1840- hmacDiff = TPM2_ConstantCompare (integrity , hmacCalc , ( word32 ) digestSz );
1843+ hmacDiff = TPM2_ConstantCompare (integrity , hmacCalc , cmpSz );
18411844 if (sizeMismatch | hmacDiff ) {
18421845 rc = TPM_RC_INTEGRITY ;
18431846 }
@@ -2543,18 +2546,29 @@ TPM_RC FwVerifySignatureCore(FWTPM_Object* obj,
25432546 (enum wc_HashType )wcHash );
25442547 int expSz = wc_EncodeSignature (expDI ,
25452548 digest , digestSz , oid );
2549+ int sizeMismatch ;
2550+ int sigDiff ;
2551+ word32 cmpSz ;
25462552
25472553 FWTPM_ALLOC_BUF (decSig , FWTPM_MAX_PUB_BUF );
25482554 wcRc = wc_RsaSSL_Verify (
25492555 sig -> signature .rsassa .sig .buffer ,
25502556 sig -> signature .rsassa .sig .size ,
25512557 decSig , (word32 )FWTPM_MAX_PUB_BUF , rsaKey );
2552- if (wcRc >= 0 ) {
2553- if (wcRc != expSz || expSz <= 0 ||
2554- TPM2_ConstantCompare (decSig , expDI , expSz ) != 0 ) {
2558+ if (wcRc >= 0 && expSz > 0 ) {
2559+ /* Always run TPM2_ConstantCompare so timing doesn't
2560+ * leak decoded-length vs expected-length match */
2561+ sizeMismatch = (wcRc != expSz );
2562+ cmpSz = (wcRc < expSz ) ? (word32 )wcRc :
2563+ (word32 )expSz ;
2564+ sigDiff = TPM2_ConstantCompare (decSig , expDI , cmpSz );
2565+ if (sizeMismatch | sigDiff ) {
25552566 wcRc = -1 ;
25562567 }
25572568 }
2569+ else if (wcRc >= 0 ) {
2570+ wcRc = -1 ;
2571+ }
25582572 FWTPM_FREE_BUF (decSig );
25592573 }
25602574 if (wcRc < 0 )
@@ -2876,6 +2890,9 @@ TPM_RC FwCredentialUnwrap(
28762890 FWTPM_DECLARE_VAR (hmac , Hmac );
28772891 FWTPM_DECLARE_BUF (decBuf , FWTPM_MAX_NV_DATA + 2 );
28782892
2893+ /* Zero-init so tail bytes are deterministic when integrityHmacSz < 32 */
2894+ TPM2_ForceZero (integrityHmac , sizeof (integrityHmac ));
2895+
28792896 FWTPM_ALLOC_VAR (aes , Aes );
28802897 FWTPM_ALLOC_VAR (hmac , Hmac );
28812898 FWTPM_ALLOC_BUF (decBuf , FWTPM_MAX_NV_DATA + 2 );
0 commit comments