Skip to content

Commit 4050983

Browse files
committed
Fix for crypto callback to return CRYPTOCB_UNAVAILABLE when a TPM key is not set. Fix to use curve type to determine hash type not digest size.
1 parent bce6cf7 commit 4050983

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/tpm2_cryptocb.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,15 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
182182
int curve_id;
183183
WOLFTPM2_KEY* key;
184184

185-
#ifdef WOLFTPM2_USE_SW_ECDHE
186-
if (tlsCtx->ecdhKey == NULL) {
187-
return exit_rc;
188-
}
189-
#endif
190-
191185
if ( tlsCtx->eccKey == NULL
192186
&& tlsCtx->ecdsaKey == NULL
193187
&& tlsCtx->ecdhKey == NULL
194188
) {
195189
#ifdef DEBUG_WOLFTPM
196-
printf("No crypto callback key pointer set!\n");
190+
printf("No crypto callback TPM key set, "
191+
"fallback to software crypto\n");
197192
#endif
198-
return BAD_FUNC_ARG;
193+
return exit_rc;
199194
}
200195

201196
/* Make sure an ECDH key has been set and curve is supported */
@@ -205,6 +200,7 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
205200
}
206201
rc = TPM2_GetTpmCurve(curve_id);
207202
if (rc < 0) {
203+
/* curve not available, so fallback to sw crypto */
208204
return exit_rc;
209205
}
210206
curve_id = rc;
@@ -215,9 +211,14 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
215211
if (tlsCtx->ecdhKey == NULL)
216212
#endif
217213
{
218-
/* Create an ECC key for ECDSA - if one isn't already created */
219214
key = (tlsCtx->ecdsaKey != NULL) ?
220215
(WOLFTPM2_KEY*)tlsCtx->ecdsaKey : tlsCtx->eccKey;
216+
if (key == NULL) {
217+
/* fallback to software crypto */
218+
return exit_rc;
219+
}
220+
221+
/* Create an ECC key for ECDSA - if one isn't already created */
221222
if (key->handle.hndl == 0 ||
222223
key->handle.hndl == TPM_RH_NULL
223224
) {
@@ -261,8 +262,13 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
261262
}
262263
#ifndef WOLFTPM2_USE_SW_ECDHE
263264
else {
264-
/* Generate ephemeral key - if one isn't already created */
265265
key = tlsCtx->ecdhKey;
266+
if (key == NULL) {
267+
/* fallback to software crypto */
268+
return exit_rc;
269+
}
270+
271+
/* Generate ephemeral key - if one isn't already created */
266272
if (key->handle.hndl == 0 ||
267273
key->handle.hndl == TPM_RH_NULL) {
268274
rc = wolfTPM2_ECDHGenKey(tlsCtx->dev, tlsCtx->ecdhKey,

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 cuve */
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)