Skip to content

Commit 02c9e8b

Browse files
authored
Merge pull request #433 from dgarske/cryptocb_hashtype
Fix for crypto callback unavailable and ECDSA hash type
2 parents bce6cf7 + 29dcfa7 commit 02c9e8b

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/tpm2_cryptocb.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,10 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
193193
&& tlsCtx->ecdhKey == NULL
194194
) {
195195
#ifdef DEBUG_WOLFTPM
196-
printf("No crypto callback key pointer set!\n");
196+
printf("No crypto callback TPM key set, "
197+
"fallback to software crypto\n");
197198
#endif
198-
return BAD_FUNC_ARG;
199+
return exit_rc;
199200
}
200201

201202
/* Make sure an ECDH key has been set and curve is supported */
@@ -205,6 +206,7 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
205206
}
206207
rc = TPM2_GetTpmCurve(curve_id);
207208
if (rc < 0) {
209+
/* curve not available, so fallback to sw crypto */
208210
return exit_rc;
209211
}
210212
curve_id = rc;
@@ -215,9 +217,14 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
215217
if (tlsCtx->ecdhKey == NULL)
216218
#endif
217219
{
218-
/* Create an ECC key for ECDSA - if one isn't already created */
219220
key = (tlsCtx->ecdsaKey != NULL) ?
220221
(WOLFTPM2_KEY*)tlsCtx->ecdsaKey : tlsCtx->eccKey;
222+
if (key == NULL) {
223+
/* fallback to software crypto */
224+
return exit_rc;
225+
}
226+
227+
/* Create an ECC key for ECDSA - if one isn't already created */
221228
if (key->handle.hndl == 0 ||
222229
key->handle.hndl == TPM_RH_NULL
223230
) {

src/tpm2_wrap.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4132,11 +4132,13 @@ int wolfTPM2_SignHash(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
41324132
sigAlg = TPM_ALG_ECDSA;
41334133
}
41344134
if (hashAlg == 0 || hashAlg == TPM_ALG_NULL) {
4135-
if (digestSz == 64)
4135+
/* determine hash type based on curve */
4136+
int curve_id = pub->parameters.eccDetail.curveID;
4137+
if (curve_id == TPM_ECC_NIST_P521)
41364138
hashAlg = TPM_ALG_SHA512;
4137-
else if (digestSz == 48)
4139+
else if (curve_id == TPM_ECC_NIST_P384)
41384140
hashAlg = TPM_ALG_SHA384;
4139-
else if (digestSz == 32)
4141+
else
41404142
hashAlg = TPM_ALG_SHA256;
41414143
}
41424144
}
@@ -4273,12 +4275,16 @@ int wolfTPM2_VerifyHash_ex(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
42734275
int wolfTPM2_VerifyHash(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
42744276
const byte* sig, int sigSz, const byte* digest, int digestSz)
42754277
{
4278+
int curve_id = 0;
42764279
int hashAlg = TPM_ALG_NULL;
42774280

4278-
/* detect hash algorithm based on digest size */
4279-
if (digestSz >= TPM_SHA512_DIGEST_SIZE)
4281+
/* detect hash algorithm based on key curve */
4282+
if (key != NULL) {
4283+
curve_id = key->pub.publicArea.parameters.eccDetail.curveID;
4284+
}
4285+
if (curve_id == TPM_ECC_NIST_P521)
42804286
hashAlg = TPM_ALG_SHA512;
4281-
else if (digestSz >= TPM_SHA384_DIGEST_SIZE)
4287+
else if (curve_id == TPM_ECC_NIST_P384)
42824288
hashAlg = TPM_ALG_SHA384;
42834289
else
42844290
hashAlg = TPM_ALG_SHA256;

0 commit comments

Comments
 (0)