Skip to content

Commit 2c298c1

Browse files
committed
Fix finding 344
1 parent ccda6fe commit 2c298c1

1 file changed

Lines changed: 61 additions & 46 deletions

File tree

src/tpm2_wrap.c

Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -2386,86 +2386,101 @@ static int SensitiveToPrivate(TPM2B_SENSITIVE* sens, TPM2B_PRIVATE* priv,
23862386
symKey.size = (symKey.size + 7) / 8;
23872387
/* check for invalid value */
23882388
if (symKey.size > sizeof(symKey.buffer)) {
2389-
return BUFFER_E;
2389+
rc = BUFFER_E;
23902390
}
23912391
#endif
23922392

23932393
if (innerWrap) {
23942394
/* TODO: Inner wrap support */
23952395
}
23962396

2397-
if (outerWrap) {
2397+
if (rc == 0 && outerWrap) {
23982398
#ifdef WOLFTPM2_PRIVATE_IMPORT
23992399
/* Generate symmetric key for encryption of inner values */
24002400
rc = TPM2_KDFa(nameAlg, symSeed, "STORAGE", (TPM2B_NONCE*)name,
24012401
NULL, symKey.buffer, symKey.size);
2402-
if (rc != symKey.size) {
2402+
if (rc == symKey.size) {
2403+
rc = 0;
2404+
}
2405+
else {
24032406
#ifdef DEBUG_WOLFTPM
24042407
printf("KDFa STORAGE Gen Error %d\n", rc);
24052408
#endif
2406-
return TPM_RC_FAILURE;
2409+
rc = TPM_RC_FAILURE;
24072410
}
24082411

24092412
/* Encrypt the Sensitive Area using the generated symmetric key */
2410-
rc = wc_AesInit(&enc, NULL, INVALID_DEVID);
24112413
if (rc == 0) {
2412-
rc = wc_AesSetKey(&enc, symKey.buffer, symKey.size,
2413-
ivField.buffer, AES_ENCRYPTION);
2414+
rc = wc_AesInit(&enc, NULL, INVALID_DEVID);
24142415
if (rc == 0) {
2415-
/* use inline encryption for both IV and sensitive */
2416-
rc = wc_AesCfbEncrypt(&enc, sensitiveData, sensitiveData,
2417-
sensSz);
2416+
rc = wc_AesSetKey(&enc, symKey.buffer, symKey.size,
2417+
ivField.buffer, AES_ENCRYPTION);
2418+
if (rc == 0) {
2419+
/* use inline encryption for both IV and sensitive */
2420+
rc = wc_AesCfbEncrypt(&enc, sensitiveData, sensitiveData,
2421+
sensSz);
2422+
}
2423+
wc_AesFree(&enc);
2424+
}
2425+
if (rc != 0) {
2426+
#ifdef DEBUG_WOLFTPM
2427+
printf("SensitiveToPrivate AES error %d!\n", rc);
2428+
#endif
24182429
}
2419-
wc_AesFree(&enc);
2420-
}
2421-
if (rc != 0) {
2422-
#ifdef DEBUG_WOLFTPM
2423-
printf("SensitiveToPrivate AES error %d!\n", rc);
2424-
#endif
2425-
return rc;
24262430
}
24272431

24282432
/* Generate HMAC key for generation of the integrity value */
2429-
hmacKey.size = digestSz;
2430-
rc = TPM2_KDFa(nameAlg, symSeed, "INTEGRITY", NULL, NULL,
2431-
hmacKey.buffer, hmacKey.size);
2432-
if (rc != hmacKey.size) {
2433-
#ifdef DEBUG_WOLFTPM
2434-
printf("KDFa INTEGRITY Gen Error %d\n", rc);
2435-
#endif
2436-
return rc;
2433+
if (rc == 0) {
2434+
hmacKey.size = digestSz;
2435+
rc = TPM2_KDFa(nameAlg, symSeed, "INTEGRITY", NULL, NULL,
2436+
hmacKey.buffer, hmacKey.size);
2437+
if (rc == hmacKey.size) {
2438+
rc = 0;
2439+
}
2440+
else {
2441+
#ifdef DEBUG_WOLFTPM
2442+
printf("KDFa INTEGRITY Gen Error %d\n", rc);
2443+
#endif
2444+
rc = TPM_RC_FAILURE;
2445+
}
24372446
}
24382447

24392448
/* setup HMAC */
2440-
rc = wc_HmacInit(&hmac_ctx, NULL, INVALID_DEVID);
24412449
if (rc == 0) {
2442-
/* start HMAC */
2443-
rc = wc_HmacSetKey(&hmac_ctx, TPM2_GetHashType(nameAlg),
2444-
hmacKey.buffer, hmacKey.size);
2450+
rc = wc_HmacInit(&hmac_ctx, NULL, INVALID_DEVID);
2451+
if (rc == 0) {
2452+
/* start HMAC */
2453+
rc = wc_HmacSetKey(&hmac_ctx, TPM2_GetHashType(nameAlg),
2454+
hmacKey.buffer, hmacKey.size);
24452455

2446-
/* consume IV and sensitive area */
2447-
if (rc == 0)
2448-
rc = wc_HmacUpdate(&hmac_ctx, sensitiveData, sensSz);
2456+
/* consume IV and sensitive area */
2457+
if (rc == 0)
2458+
rc = wc_HmacUpdate(&hmac_ctx, sensitiveData, sensSz);
24492459

2450-
/* consume name field */
2451-
if (rc == 0)
2452-
rc = wc_HmacUpdate(&hmac_ctx, name->name, name->size);
2460+
/* consume name field */
2461+
if (rc == 0)
2462+
rc = wc_HmacUpdate(&hmac_ctx, name->name, name->size);
24532463

2454-
if (rc == 0)
2455-
rc = wc_HmacFinal(&hmac_ctx, &priv->buffer[sizeof(word16)]);
2464+
if (rc == 0)
2465+
rc = wc_HmacFinal(&hmac_ctx, &priv->buffer[sizeof(word16)]);
24562466

2457-
wc_HmacFree(&hmac_ctx);
2458-
}
2459-
if (rc != 0) {
2460-
#ifdef DEBUG_WOLFTPM
2461-
printf("SensitiveToPrivate HMAC error %d!\n", rc);
2462-
#endif
2463-
return rc;
2467+
wc_HmacFree(&hmac_ctx);
2468+
}
2469+
if (rc != 0) {
2470+
#ifdef DEBUG_WOLFTPM
2471+
printf("SensitiveToPrivate HMAC error %d!\n", rc);
2472+
#endif
2473+
}
24642474
}
24652475

24662476
/* store the size of the integrity */
2467-
digestSz = TPM2_Packet_SwapU16(digestSz);
2468-
XMEMCPY(&priv->buffer[0], &digestSz, sizeof(word16));
2477+
if (rc == 0) {
2478+
digestSz = TPM2_Packet_SwapU16(digestSz);
2479+
XMEMCPY(&priv->buffer[0], &digestSz, sizeof(word16));
2480+
}
2481+
2482+
TPM2_ForceZero(&symKey, sizeof(symKey));
2483+
TPM2_ForceZero(&hmacKey, sizeof(hmacKey));
24692484
#else
24702485
(void)name;
24712486
(void)sensSz;

0 commit comments

Comments
 (0)