Skip to content

Commit

Permalink
Helper to get wolfCrypt hash type. `TPMI_ALG_HASH TPM2_GetTpmHashType…
Browse files Browse the repository at this point in the history
…(int hashType)`.
  • Loading branch information
dgarske committed Oct 24, 2024
1 parent a5f6c91 commit fa1cd52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/tpm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -5525,6 +5525,26 @@ int TPM2_GetHashDigestSize(TPMI_ALG_HASH hashAlg)
return 0;
}

TPMI_ALG_HASH TPM2_GetTpmHashType(int hashType)
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
switch (hashType) {
case (int)WC_HASH_TYPE_SHA:
return TPM_ALG_SHA1;
case (int)WC_HASH_TYPE_SHA256:
return TPM_ALG_SHA256;
case (int)WC_HASH_TYPE_SHA384:
return TPM_ALG_SHA384;
case (int)WC_HASH_TYPE_SHA512:
return TPM_ALG_SHA512;
default:
break;
}
#endif
(void)hashType;
return TPM_ALG_ERROR;
}

int TPM2_GetHashType(TPMI_ALG_HASH hashAlg)
{
#ifndef WOLFTPM2_NO_WOLFCRYPT
Expand Down
22 changes: 22 additions & 0 deletions wolftpm/tpm2.h
Original file line number Diff line number Diff line change
Expand Up @@ -3394,6 +3394,28 @@ WOLFTPM_API int TPM2_GetHashDigestSize(TPMI_ALG_HASH hashAlg);
*/
WOLFTPM_API int TPM2_GetHashType(TPMI_ALG_HASH hashAlg);

/*!
\ingroup TPM2_Proprietary
\brief Translate a wolfCrypt hash type to TPM2 hash type
\return a TPM2 hash type (TPM_ALG_*)
\return TPM_ALG_ERROR when wolfCrypt hash type is invalid or not found
\param hashType a wolfCrypt hash type
_Example_
\code
int wc_hashType = WC_HASH_TYPE_SHA256;
TPMI_ALG_HASH hashAlg;
hashAlg = TPM2_GetHashDigestSize(wc_hashType);
if (hashAlg != TPM_ALG_ERROR) {
//hashAlg contains a valid TPM2 hash type
}
\endcode
*/
WOLFTPM_API TPMI_ALG_HASH TPM2_GetTpmHashType(int hashType);

/*!
\ingroup TPM2_Proprietary
\brief Generate a fresh nonce of random numbers
Expand Down

0 comments on commit fa1cd52

Please sign in to comment.