Skip to content

Commit 185ab61

Browse files
committed
TPM based root of trust using NV index.
1 parent f2012c3 commit 185ab61

File tree

2 files changed

+73
-22
lines changed

2 files changed

+73
-22
lines changed

src/image.c

+69-18
Original file line numberDiff line numberDiff line change
@@ -121,20 +121,21 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
121121
{
122122
int ret, verify_res = 0;
123123
uint8_t *pubkey = keystore_get_buffer(key_slot);
124-
int point_sz = keystore_get_size(key_slot)/2;
125-
#ifdef WOLFBOOT_TPM
124+
int pubkey_sz = keystore_get_size(key_slot);
125+
int point_sz = pubkey_sz/2;
126+
#if defined(WOLFBOOT_TPM) && !defined(WOLFBOOT_TPM_KEYSTORE)
126127
WOLFTPM2_KEY tpmKey;
127128
#else
128129
ecc_key ecc;
129130
mp_int r, s;
130131
#endif
131132

132-
if (pubkey == NULL || point_sz <= 0)
133+
if (pubkey == NULL || pubkey_sz <= 0) {
133134
return;
135+
}
134136

135-
#ifdef WOLFBOOT_TPM
136-
/* TODO: Check ECC Root of Trust in TPM */
137-
137+
#if defined(WOLFBOOT_TPM) && !defined(WOLFBOOT_TPM_KEYSTORE)
138+
/* Use TPM for ECC verify */
138139
/* Load public key into TPM */
139140
memset(&tpmKey, 0, sizeof(tpmKey));
140141
ret = wolfTPM2_LoadEccPublicKey(&wolftpm_dev, &tpmKey,
@@ -276,25 +277,24 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
276277
{
277278
int ret;
278279
uint8_t output[IMAGE_SIGNATURE_SIZE];
279-
int output_sz = sizeof(output);
280+
int output_sz = (int)sizeof(output);
280281
uint8_t* digest_out = NULL;
281282
uint8_t *pubkey = keystore_get_buffer(key_slot);
282283
int pubkey_sz = keystore_get_size(key_slot);
283284
word32 inOutIdx = 0;
284-
#ifdef WOLFBOOT_TPM
285+
#if defined(WOLFBOOT_TPM) && !defined(WOLFBOOT_TPM_KEYSTORE)
285286
WOLFTPM2_KEY tpmKey;
286287
const byte *n = NULL, *e = NULL;
287288
word32 nSz = 0, eSz = 0;
288289
#else
289290
struct RsaKey rsa;
290291
#endif
291292

292-
if ((pubkey_sz < 0) || (pubkey == NULL))
293+
if (pubkey == NULL || pubkey_sz < 0) {
293294
return;
295+
}
294296

295-
#ifdef WOLFBOOT_TPM
296-
/* TODO: Check RSA Root of Trust in TPM */
297-
297+
#if defined(WOLFBOOT_TPM) && !defined(WOLFBOOT_TPM_KEYSTORE)
298298
/* Extract DER RSA key struct */
299299
memset(&tpmKey, 0, sizeof(tpmKey));
300300
ret = wc_RsaPublicKeyDecode_ex(pubkey, &inOutIdx, pubkey_sz,
@@ -365,8 +365,9 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
365365
ret = RsaDecodeSignature(&digest_out, ret);
366366
}
367367
#endif
368-
if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out)
368+
if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out) {
369369
RSA_VERIFY_HASH(img, digest_out);
370+
}
370371
}
371372
#endif /* WOLFBOOT_SIGN_RSA2048 || WOLFBOOT_SIGN_3072 || \
372373
* WOLFBOOT_SIGN_RSA4096 */
@@ -642,7 +643,8 @@ static void key_sha3_384(uint8_t key_slot, uint8_t *hash)
642643
#endif /* SHA3-384 */
643644

644645
#ifdef WOLFBOOT_TPM
645-
#if defined(WOLFTPM_DEBUG_IO) || defined(WOLFBOOT_DEBUG_TPM)
646+
#if defined(DEBUG_WOLFTPM) || defined(WOLFTPM_DEBUG_IO) || \
647+
defined(WOLFBOOT_DEBUG_TPM)
646648
#define LINE_LEN 16
647649
static void wolfBoot_PrintBin(const byte* buffer, word32 length)
648650
{
@@ -698,6 +700,14 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
698700
byte rxBuf[MAX_SPI_FRAMESIZE+TPM_TIS_HEADER_SZ];
699701
int xferSz = TPM_TIS_HEADER_SZ + size;
700702

703+
#ifdef WOLFTPM_DEBUG_IO
704+
wolfBoot_printf("TPM2_IoCb (Adv): Read %d, Addr %x, Size %d\n",
705+
isRead ? 1 : 0, addr, size);
706+
if (!isRead) {
707+
wolfBoot_PrintBin(buf, size);
708+
}
709+
#endif
710+
701711
/* Build TPM header */
702712
txBuf[1] = (addr>>16) & 0xFF;
703713
txBuf[2] = (addr>>8) & 0xFF;
@@ -711,7 +721,7 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
711721
memcpy(&txBuf[TPM_TIS_HEADER_SZ], buf, size);
712722
}
713723
memset(rxBuf, 0, sizeof(rxBuf));
714-
#endif
724+
#endif /* WOLFTPM_ADV_IO */
715725

716726
#ifdef WOLFTPM_CHECK_WAIT_STATE /* Handle TIS wait states */
717727
/* Send header - leave CS asserted */
@@ -751,11 +761,20 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
751761
}
752762
#else /* Send Entire Message - no wait states */
753763
ret = spi_xfer(SPI_CS_TPM, txBuf, rxBuf, xferSz, 0);
754-
#endif
764+
765+
#ifdef WOLFTPM_DEBUG_IO
766+
wolfBoot_printf("TPM2_IoCb: Ret %d, Sz %d\n", ret, xferSz);
767+
wolfBoot_PrintBin(txBuf, xferSz);
768+
wolfBoot_PrintBin(rxBuf, xferSz);
769+
#endif
770+
#endif /* !WOLFTPM_CHECK_WAIT_STATE */
755771

756772
#ifdef WOLFTPM_ADV_IO
757773
if (isRead) {
758774
memcpy(buf, &rxBuf[TPM_TIS_HEADER_SZ], size);
775+
#ifdef WOLFTPM_DEBUG_IO
776+
wolfBoot_PrintBin(buf, size);
777+
#endif
759778
}
760779
#endif
761780

@@ -764,8 +783,8 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
764783
#endif /* !ARCH_SIM */
765784

766785
#if defined(WOLFBOOT_TPM) && defined(WOLFBOOT_MEASURED_BOOT)
767-
#define measure_boot(hash) measure_boot_at((hash), __LINE__)
768-
static int measure_boot_at(uint8_t* hash, int line)
786+
#define measure_boot(hash) wolfBoot_tpm2_extend((hash), __LINE__)
787+
static int wolfBoot_tpm2_extend(uint8_t* hash, int line)
769788
{
770789
int rc;
771790
PCR_Extend_In pcrExtend;
@@ -1178,12 +1197,44 @@ static int keyslot_id_by_sha(const uint8_t *hint)
11781197
/* Override global */
11791198
uint8_t digest[WOLFBOOT_SHA_DIGEST_SIZE];
11801199
#endif
1200+
1201+
#if defined(WOLFBOOT_TPM) && defined(WOLFBOOT_TPM_KEYSTORE)
1202+
/* use public key hash (hint) */
1203+
int rc;
1204+
WOLFTPM2_NV nv;
1205+
word32 digestSz = (word32)TPM2_GetHashDigestSize(WOLFBOOT_TPM_HASH_ALG);
1206+
XMEMSET(&nv, 0, sizeof(nv));
1207+
nv.handle.hndl = WOLFBOOT_TPM_KEYSTORE_NV_INDEX;
1208+
1209+
#if 0 /* TODO: Add auth */
1210+
nv.handle.auth.size = sizeof(authBuf);
1211+
XMEMCPY(nv.handle.auth.buffer, authBuf, sizeof(authBuf));
1212+
#endif
1213+
1214+
rc = wolfTPM2_NVReadAuth(&wolftpm_dev, &nv, WOLFBOOT_TPM_KEYSTORE_NV_INDEX,
1215+
digest, &digestSz, 0);
1216+
if (rc == 0 && memcmp(digest, hint, WOLFBOOT_SHA_DIGEST_SIZE) == 0) {
1217+
#ifdef DEBUG_WOLFTPM
1218+
wolfBoot_printf("TPM Root of Trust valid\n");
1219+
#endif
1220+
return 0;
1221+
}
1222+
else {
1223+
#ifdef DEBUG_WOLFTPM
1224+
wolfBoot_printf("TPM Root of Trust failed! %d (%s)\n",
1225+
rc, wolfTPM2_GetRCString(rc));
1226+
wolfBoot_printf("Expected Hash %d\n", WOLFBOOT_SHA_DIGEST_SIZE);
1227+
wolfBoot_PrintBin(hint, WOLFBOOT_SHA_DIGEST_SIZE);
1228+
#endif
1229+
}
1230+
#else
11811231
int id = 0;
11821232
for (id = 0; id < keystore_num_pubkeys(); id++) {
11831233
key_hash(id, digest);
11841234
if (memcmp(digest, hint, WOLFBOOT_SHA_DIGEST_SIZE) == 0)
11851235
return id;
11861236
}
1237+
#endif
11871238
return -1;
11881239
}
11891240
#endif

tools/test.mk

+4-4
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,9 @@ test-size-all:
924924
make keysclean
925925
make test-size SIGN=ED25519 LIMIT=11398
926926
make keysclean
927-
make test-size SIGN=ECC256 LIMIT=22174
927+
make test-size SIGN=ECC256 LIMIT=22266
928928
make keysclean
929-
make test-size SIGN=ECC256 NO_ASM=1 LIMIT=13650
929+
make test-size SIGN=ECC256 NO_ASM=1 LIMIT=13702
930930
make keysclean
931931
make test-size SIGN=RSA2048 LIMIT=11182
932932
make keysclean
@@ -936,9 +936,9 @@ test-size-all:
936936
make keysclean
937937
make test-size SIGN=RSA4096 NO_ASM=1 LIMIT=11462
938938
make keysclean
939-
make test-size SIGN=ECC384 LIMIT=17470
939+
make test-size SIGN=ECC384 LIMIT=17562
940940
make keysclean
941-
make test-size SIGN=ECC384 NO_ASM=1 LIMIT=15082
941+
make test-size SIGN=ECC384 NO_ASM=1 LIMIT=15172
942942
make keysclean
943943
make test-size SIGN=ED448 LIMIT=13414
944944
make keysclean

0 commit comments

Comments
 (0)