From b6473792c0158e1b7d1eb489e8f1b37e34d8dcb5 Mon Sep 17 00:00:00 2001 From: David Garske Date: Tue, 28 Nov 2023 09:59:27 -0800 Subject: [PATCH] Cleanup KDF function return code checking to avoid scan-build warning. --- src/tpm2_param_enc.c | 41 +++++++++++++++++------------------------ src/tpm2_wrap.c | 27 +++++++++++---------------- 2 files changed, 28 insertions(+), 40 deletions(-) diff --git a/src/tpm2_param_enc.c b/src/tpm2_param_enc.c index 8d315b9..f72a2e7 100644 --- a/src/tpm2_param_enc.c +++ b/src/tpm2_param_enc.c @@ -103,7 +103,7 @@ int TPM2_KDFa( return NOT_COMPILED_IN; hLen = TPM2_GetHashDigestSize(hashAlg); - if ( (hLen <= 0) || (hLen > WC_MAX_DIGEST_SIZE)) + if ((hLen <= 0) || (hLen > WC_MAX_DIGEST_SIZE)) return NOT_COMPILED_IN; /* get label length if provided, including null termination */ @@ -128,46 +128,39 @@ int TPM2_KDFa( else { ret = wc_HmacSetKey(&hmac_ctx, hashType, NULL, 0); } - if (ret != 0) - goto exit; - /* add counter - KDFa i2 */ - TPM2_Packet_U32ToByteArray(counter, uint32Buf); - ret = wc_HmacUpdate(&hmac_ctx, uint32Buf, (word32)sizeof(uint32Buf)); - if (ret != 0) - goto exit; - + if (ret == 0) { + TPM2_Packet_U32ToByteArray(counter, uint32Buf); + ret = wc_HmacUpdate(&hmac_ctx, uint32Buf, (word32)sizeof(uint32Buf)); + } /* add label - KDFa label */ - if (label != NULL) { + if (ret == 0 && label != NULL) { ret = wc_HmacUpdate(&hmac_ctx, (byte*)label, lLen); - if (ret != 0) - goto exit; } /* add contextU */ - if (contextU != NULL && contextU->size > 0) { + if (ret == 0 && contextU != NULL && contextU->size > 0) { ret = wc_HmacUpdate(&hmac_ctx, contextU->buffer, contextU->size); - if (ret != 0) - goto exit; } /* add contextV */ - if (contextV != NULL && contextV->size > 0) { + if (ret == 0 && contextV != NULL && contextV->size > 0) { ret = wc_HmacUpdate(&hmac_ctx, contextV->buffer, contextV->size); - if (ret != 0) - goto exit; } /* add size in bits */ - TPM2_Packet_U32ToByteArray(sizeInBits, uint32Buf); - ret = wc_HmacUpdate(&hmac_ctx, uint32Buf, (word32)sizeof(uint32Buf)); - if (ret != 0) - goto exit; + if (ret == 0) { + TPM2_Packet_U32ToByteArray(sizeInBits, uint32Buf); + ret = wc_HmacUpdate(&hmac_ctx, uint32Buf, (word32)sizeof(uint32Buf)); + } /* get result */ - ret = wc_HmacFinal(&hmac_ctx, hash); - if (ret != 0) + if (ret == 0) { + ret = wc_HmacFinal(&hmac_ctx, hash); + } + if (ret != 0) { goto exit; + } if ((UINT32)hLen > keySz - pos) { copyLen = keySz - pos; diff --git a/src/tpm2_wrap.c b/src/tpm2_wrap.c index b64f998..2b3efa0 100644 --- a/src/tpm2_wrap.c +++ b/src/tpm2_wrap.c @@ -999,7 +999,7 @@ static int TPM2_KDFe( hashType = (enum wc_HashType)ret; hLen = TPM2_GetHashDigestSize(hashAlg); - if ( (hLen <= 0) || (hLen > WC_MAX_DIGEST_SIZE)) + if ((hLen <= 0) || (hLen > WC_MAX_DIGEST_SIZE)) return NOT_COMPILED_IN; /* get label length if provided, including null termination */ @@ -1021,37 +1021,32 @@ static int TPM2_KDFe( TPM2_Packet_U32ToByteArray(counter, uint32Buf); ret = wc_HashUpdate(&hash_ctx, hashType, uint32Buf, (word32)sizeof(uint32Buf)); - if (ret != 0) - goto exit; - /* add Z */ - ret = wc_HashUpdate(&hash_ctx, hashType, Z->buffer, Z->size); - + if (ret == 0) { + ret = wc_HashUpdate(&hash_ctx, hashType, Z->buffer, Z->size); + } /* add label */ - if (label != NULL) { + if (ret == 0 && label != NULL) { ret = wc_HashUpdate(&hash_ctx, hashType, (byte*)label, lLen); - if (ret != 0) - goto exit; } /* add partyUInfo */ - if (partyUInfo != NULL && partyUInfo->size > 0) { + if (ret == 0 && partyUInfo != NULL && partyUInfo->size > 0) { ret = wc_HashUpdate(&hash_ctx, hashType, partyUInfo->buffer, partyUInfo->size); - if (ret != 0) - goto exit; } /* add partyVInfo */ - if (partyVInfo != NULL && partyVInfo->size > 0) { + if (ret == 0 && partyVInfo != NULL && partyVInfo->size > 0) { ret = wc_HashUpdate(&hash_ctx, hashType, partyVInfo->buffer, partyVInfo->size); - if (ret != 0) - goto exit; } /* get result */ - ret = wc_HashFinal(&hash_ctx, hashType, hash); + if (ret == 0) { + ret = wc_HashFinal(&hash_ctx, hashType, hash); + } + if (ret != 0) goto exit;