Skip to content

Commit 8c5c6cb

Browse files
committed
Fix AddressSanitizer warning with overlapping memcpy (need to use memmove). Fix test case with WOLFTPM2_USE_SW_ECDHE.
1 parent ce22962 commit 8c5c6cb

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/tpm2_cryptocb.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,9 +353,9 @@ int wolfTPM2_CryptoDevCb(int devId, wc_CryptoInfo* info, void* ctx)
353353
if (rc == 0) {
354354
/* combine R and S at key size (zero pad leading) */
355355
word32 keySz = wc_ecc_size(info->pk.eccverify.key);
356-
XMEMCPY(&sigRS[keySz-rLen], r, rLen);
356+
XMEMMOVE(&sigRS[keySz-rLen], r, rLen);
357357
XMEMSET(&sigRS[0], 0, keySz-rLen);
358-
XMEMCPY(&sigRS[keySz + (keySz-sLen)], s, sLen);
358+
XMEMMOVE(&sigRS[keySz + (keySz-sLen)], s, sLen);
359359
XMEMSET(&sigRS[keySz], 0, keySz-sLen);
360360
rc = wolfTPM2_VerifyHash(tlsCtx->dev, &eccPub,
361361
sigRS, keySz*2,

src/tpm2_wrap.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4084,7 +4084,9 @@ int wolfTPM2_SignHashScheme(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* key,
40844084

40854085
/* Assemble R and S into signature (R then S) */
40864086
sigOutSz = curveSize * 2;
4087-
if (sigOutSz > *sigSz) {
4087+
if (sigOutSz > *sigSz ||
4088+
curveSize > ecdsa->signatureR.size ||
4089+
curveSize > ecdsa->signatureS.size) {
40884090
#ifdef DEBUG_WOLFTPM
40894091
printf("TPM2_Sign: ECC result buffer too small %d -> %d\n",
40904092
sigOutSz, *sigSz);

tests/unit_tests.c

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
418418
{
419419
int rc;
420420
int verifyRes = 0;
421-
WOLFTPM2_KEY eccKey;
421+
WOLFTPM2_KEYBLOB eccKey;
422422
TPMT_PUBLIC publicTemplate;
423423
byte sigRs[MAX_ECC_BYTES*2];
424424
word32 sigRsSz = (word32)sizeof(sigRs);
@@ -434,7 +434,7 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
434434

435435
XMEMSET(&tpmCtx, 0, sizeof(tpmCtx));
436436
tpmCtx.dev = dev;
437-
tpmCtx.eccKey = &eccKey;
437+
tpmCtx.ecdsaKey = &eccKey;
438438
tpmCtx.storageKey = storageKey;
439439

440440
if (flags & FLAGS_USE_CRYPTO_CB) {
@@ -451,8 +451,14 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
451451
TPMA_OBJECT_sign | TPMA_OBJECT_noDA),
452452
curve, TPM_ALG_ECDSA, hashAlg);
453453
AssertIntEQ(rc, 0);
454-
rc = wolfTPM2_CreateAndLoadKey(dev, &eccKey, &storageKey->handle,
454+
455+
/* Use create key and load key directly instead to make
456+
* sure the private portion is populated */
457+
rc = wolfTPM2_CreateKey(dev, &eccKey, &storageKey->handle,
455458
&publicTemplate, (byte*)gKeyAuth, sizeof(gKeyAuth)-1);
459+
if (rc == TPM_RC_SUCCESS) {
460+
rc = wolfTPM2_LoadKey(dev, &eccKey, &storageKey->handle);
461+
}
456462
if ((rc & TPM_RC_HASH) == TPM_RC_HASH) {
457463
printf("Hash type not supported... Skipping\n");
458464
return;
@@ -464,7 +470,7 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
464470
AssertIntEQ(rc, 0);
465471

466472
/* Sign with TPM */
467-
rc = wolfTPM2_SignHashScheme(dev, &eccKey, digest, digestSz,
473+
rc = wolfTPM2_SignHashScheme(dev, (WOLFTPM2_KEY*)&eccKey, digest, digestSz,
468474
sigRs, (int*)&sigRsSz, TPM_ALG_ECDSA, hashAlg);
469475
AssertIntEQ(rc, 0);
470476

@@ -483,7 +489,7 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
483489
AssertIntEQ(rc, 0);
484490

485491
/* Convert TPM key to wolfCrypt key for verification */
486-
rc = wolfTPM2_EccKey_TpmToWolf(dev, &eccKey, &wolfKey);
492+
rc = wolfTPM2_EccKey_TpmToWolf(dev, (WOLFTPM2_KEY*)&eccKey, &wolfKey);
487493
AssertIntEQ(rc, 0);
488494

489495
/* Verify TPM signature with wolfCrypt */
@@ -494,7 +500,9 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
494500
/* Cleanup first wolfCrypt key */
495501
wc_ecc_free(&wolfKey);
496502
wolfTPM2_UnloadHandle(dev, &eccKey.handle);
497-
503+
#ifdef WOLF_CRYPTO_CB
504+
tpmCtx.ecdsaKey = NULL; /* create new one */
505+
#endif
498506

499507
/* -- Use wolfCrypt key to sign and verify with TPM -- */
500508
/* Initialize new wolfCrypt ECC key */
@@ -516,12 +524,11 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
516524
r = sigRs;
517525
s = &sigRs[MAX_ECC_BYTES];
518526
rLen = sLen = MAX_ECC_BYTES;
519-
rc = wc_ecc_sig_to_rs(sig,
520-
sigSz, r, &rLen, s, &sLen);
527+
rc = wc_ecc_sig_to_rs(sig, sigSz, r, &rLen, s, &sLen);
521528
AssertIntEQ(rc, 0);
522529

523530
/* Convert wolfCrypt key to TPM key for verification */
524-
rc = wolfTPM2_EccKey_WolfToTpm(dev, &wolfKey, &eccKey);
531+
rc = wolfTPM2_EccKey_WolfToTpm(dev, &wolfKey, (WOLFTPM2_KEY*)&eccKey);
525532
AssertIntEQ(rc, 0);
526533

527534
/* combine R and S at key size (zero pad leading) */
@@ -531,8 +538,8 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
531538
XMEMSET(&sigRs[curveSize], 0, curveSize-sLen);
532539

533540
/* Verify wolfCrypt signature with TPM */
534-
rc = wolfTPM2_VerifyHashScheme(dev, &eccKey, sigRs, curveSize*2,
535-
digest, digestSz, TPM_ALG_ECDSA, hashAlg);
541+
rc = wolfTPM2_VerifyHashScheme(dev, (WOLFTPM2_KEY*)&eccKey, sigRs,
542+
curveSize*2, digest, digestSz, TPM_ALG_ECDSA, hashAlg);
536543
AssertIntEQ(rc, 0);
537544

538545
/* Cleanup */
@@ -551,7 +558,8 @@ static void test_wolfTPM2_EccSignVerifyDig(WOLFTPM2_DEV* dev,
551558
}
552559
}
553560

554-
static void test_wolfTPM2_EccSignVerify_All(WOLFTPM2_DEV* dev, WOLFTPM2_KEY* storageKey, int flags)
561+
static void test_wolfTPM2_EccSignVerify_All(WOLFTPM2_DEV* dev,
562+
WOLFTPM2_KEY* storageKey, int flags)
555563
{
556564
int i;
557565
byte digest[TPM_MAX_DIGEST_SIZE];

0 commit comments

Comments
 (0)