Skip to content

Commit 3f6f6b5

Browse files
committed
Added H5 test app support for pre-provisioned TPM (WOLFTPM_MFG_IDENTITY) "quote" and "signed_timestamp" using IAK
1 parent 7689374 commit 3f6f6b5

File tree

4 files changed

+260
-13
lines changed

4 files changed

+260
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ CFLAGS+= \
105105
# Setup default optimizations (for GCC)
106106
ifeq ($(USE_GCC_HEADLESS),1)
107107
CFLAGS+=-Wall -Wextra -Wno-main -ffreestanding -Wno-unused -nostartfiles
108-
CFLAGS+=-ffunction-sections -fdata-sections -fomit-frame-pointer
108+
CFLAGS+=-ffunction-sections -fdata-sections -fomit-frame-pointer -Wno-unused-variable
109109
LDFLAGS+=-Wl,-gc-sections -Wl,-Map=wolfboot.map -ffreestanding -nostartfiles
110110
# Not setting LDFLAGS directly since it is passed to the test-app
111111
LSCRIPT_FLAGS+=-T $(LSCRIPT)

include/tpm.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,23 +48,32 @@ extern WOLFTPM2_KEY wolftpm_srk;
4848

4949
#define WOLFBOOT_MAX_SEAL_SZ MAX_SYM_DATA
5050

51-
52-
int wolfBoot_tpm2_init(void);
53-
void wolfBoot_tpm2_deinit(void);
54-
55-
int wolfBoot_tpm2_clear(void);
56-
5751
/* API's that are callable from non-secure code */
5852
int CSME_NSE_API wolfBoot_tpm2_caps(WOLFTPM2_CAPS* caps);
5953
int CSME_NSE_API wolfBoot_tpm2_get_handles(TPM_HANDLE handle, TPML_HANDLE* handles);
6054
const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg,
6155
char* name, int name_sz);
6256
const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc,
6357
char* error, int error_sz);
64-
TPM_RC CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out);
58+
int CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out);
6559
int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int* digestSz);
6660
int CSME_NSE_API wolfBoot_tpm2_read_cert(uint32_t handle, uint8_t* cert, uint32_t* certSz);
6761

62+
#ifdef WOLFTPM_MFG_IDENTITY
63+
int CSME_NSE_API wolfBoot_tpm2_get_aik(WOLFTPM2_KEY* aik,
64+
uint8_t* masterPassword, uint16_t masterPasswordSz);
65+
int CSME_NSE_API wolfBoot_tpm2_get_timestamp(WOLFTPM2_KEY* aik, GetTime_Out* getTime);
66+
int CSME_NSE_API wolfBoot_tpm2_quote(WOLFTPM2_KEY* aik,
67+
byte* pcrArray, word32 pcrArraySz, Quote_Out* quoteResult);
68+
int CSME_NSE_API wolfBoot_tpm2_parse_attest(const TPM2B_ATTEST* in, TPMS_ATTEST* out);
69+
#endif
70+
71+
/* Internal wolfBoot TPM API's */
72+
int wolfBoot_tpm2_init(void);
73+
void wolfBoot_tpm2_deinit(void);
74+
75+
int wolfBoot_tpm2_clear(void);
76+
6877
#if defined(WOLFBOOT_TPM_VERIFY) || defined(WOLFBOOT_TPM_SEAL)
6978
int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey,
7079
TPM_ALG_ID* pAlg);

src/tpm.c

Lines changed: 104 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,9 +1182,9 @@ const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc, char* error, int er
11821182
return (const char*)error;
11831183
}
11841184

1185-
TPM_RC CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out)
1185+
int CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out)
11861186
{
1187-
return TPM2_GetCapability(in, out);
1187+
return (int)TPM2_GetCapability(in, out);
11881188
}
11891189

11901190
int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int* digestSz)
@@ -1195,9 +1195,111 @@ int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int*
11951195

11961196
int CSME_NSE_API wolfBoot_tpm2_read_cert(uint32_t handle, uint8_t* cert, uint32_t* certSz)
11971197
{
1198+
wolfTPM2_SetAuthPassword(&wolftpm_dev, 0, NULL);
11981199
return wolfTPM2_NVReadCert(&wolftpm_dev, handle, cert, certSz);
11991200
}
12001201

1202+
#ifdef WOLFTPM_MFG_IDENTITY
1203+
int CSME_NSE_API wolfBoot_tpm2_get_aik(WOLFTPM2_KEY* aik,
1204+
uint8_t* masterPassword, uint16_t masterPasswordSz)
1205+
{
1206+
int rc;
1207+
if (aik == NULL) {
1208+
return BAD_FUNC_ARG;
1209+
}
1210+
1211+
/* Load existing AIK and set auth */
1212+
rc = wolfTPM2_ReadPublicKey(&wolftpm_dev, aik, TPM2_IAK_KEY_HANDLE);
1213+
if (rc == 0) {
1214+
/* Custom should supply their own custom master password used during
1215+
* device provisioning. If using a sample TPM supply NULL to use the
1216+
* default password. */
1217+
rc = wolfTPM2_SetIdentityAuth(&wolftpm_dev, &aik->handle,
1218+
masterPassword, masterPasswordSz);
1219+
}
1220+
return rc;
1221+
}
1222+
1223+
int CSME_NSE_API wolfBoot_tpm2_get_timestamp(WOLFTPM2_KEY* aik, GetTime_Out* getTime)
1224+
{
1225+
int rc;
1226+
WOLFTPM2_HANDLE eh_handle;
1227+
/* sample master password for EH */
1228+
uint8_t Master_EH_AuthValue[] = {
1229+
0xDE, 0xEF, 0x8C, 0xDF, 0x1B, 0x77, 0xBD, 0x00,
1230+
0x30, 0x58, 0x5E, 0x47, 0xB8, 0x21, 0x46, 0x0B
1231+
};
1232+
1233+
if (aik == NULL || getTime == NULL) {
1234+
return BAD_FUNC_ARG;
1235+
}
1236+
1237+
memset(getTime, 0, sizeof(*getTime));
1238+
memset(&eh_handle, 0, sizeof(eh_handle));
1239+
1240+
eh_handle.hndl = TPM_RH_ENDORSEMENT;
1241+
1242+
/* Calculate EH auth value */
1243+
rc = wolfTPM2_SetIdentityAuth(&wolftpm_dev, &eh_handle,
1244+
Master_EH_AuthValue, (uint16_t)sizeof(Master_EH_AuthValue));
1245+
if (rc == 0) {
1246+
/* Set EH auth */
1247+
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &eh_handle);
1248+
1249+
/* set auth for using the AIK */
1250+
wolfTPM2_SetAuthHandle(&wolftpm_dev, 1, &aik->handle);
1251+
}
1252+
if (rc == 0) {
1253+
rc = wolfTPM2_GetTime(aik, getTime);
1254+
}
1255+
1256+
wolfTPM2_UnsetAuth(&wolftpm_dev, 1);
1257+
wolfTPM2_UnsetAuth(&wolftpm_dev, 0);
1258+
1259+
return rc;
1260+
}
1261+
1262+
int CSME_NSE_API wolfBoot_tpm2_parse_attest(const TPM2B_ATTEST* in, TPMS_ATTEST* out)
1263+
{
1264+
return TPM2_ParseAttest(in, out);
1265+
}
1266+
1267+
int CSME_NSE_API wolfBoot_tpm2_quote(WOLFTPM2_KEY* aik,
1268+
byte* pcrArray, word32 pcrArraySz, Quote_Out* quoteResult)
1269+
{
1270+
int rc;
1271+
Quote_In quoteAsk;
1272+
TPMT_ASYM_SCHEME* scheme;
1273+
1274+
if (aik == NULL || pcrArray == NULL || pcrArraySz == 0 ||
1275+
quoteResult == NULL) {
1276+
return BAD_FUNC_ARG;
1277+
}
1278+
1279+
/* set auth for using the AIK */
1280+
wolfTPM2_SetAuthHandle(&wolftpm_dev, 0, &aik->handle);
1281+
1282+
/* Prepare Quote request */
1283+
XMEMSET(&quoteAsk, 0, sizeof(quoteAsk));
1284+
XMEMSET(quoteResult, 0, sizeof(*quoteResult));
1285+
1286+
scheme = &aik->pub.publicArea.parameters.asymDetail.scheme;
1287+
quoteAsk.signHandle = aik->handle.hndl;
1288+
quoteAsk.inScheme.scheme = scheme->scheme;
1289+
quoteAsk.inScheme.details.any.hashAlg = scheme->details.anySig.hashAlg;
1290+
quoteAsk.qualifyingData.size = 0; /* optional */
1291+
/* Choose PCR(s) for signing */
1292+
TPM2_SetupPCRSelArray(&quoteAsk.PCRselect, scheme->details.anySig.hashAlg,
1293+
pcrArray, pcrArraySz);
1294+
1295+
/* Get AIK signed attestation of PCR(s) */
1296+
rc = TPM2_Quote(&quoteAsk, quoteResult);
1297+
1298+
wolfTPM2_UnsetAuth(&wolftpm_dev, 0);
1299+
1300+
return rc;
1301+
}
1302+
#endif /* WOLFTPM_MFG_IDENTITY */
12011303

12021304

12031305
/**

test-app/app_stm32h5.c

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,12 @@ static int cmd_update_xmodem(const char *args);
166166
static int cmd_reboot(const char *args);
167167
#ifdef WOLFBOOT_TPM
168168
static int cmd_tpm_info(const char *args);
169+
#ifdef WOLFTPM_MFG_IDENTITY
169170
static int cmd_tpm_idevid(const char *args);
170171
static int cmd_tpm_iak(const char *args);
172+
static int cmd_tpm_signed_timestamp(const char *args);
173+
static int cmd_tpm_quote(const char *args);
174+
#endif
171175
#endif
172176

173177

@@ -197,8 +201,12 @@ struct console_command COMMANDS[] =
197201
{cmd_reboot, "reboot", "reboot the system"},
198202
#ifdef WOLFBOOT_TPM
199203
{cmd_tpm_info, "tpm", "get TPM capabilities"},
204+
#ifdef WOLFTPM_MFG_IDENTITY
200205
{cmd_tpm_idevid, "idevid", "show Initial Device Identification (IDevID) certificate"},
201206
{cmd_tpm_iak, "iak", "show Initial Attestation Identification (IAK) certificate"},
207+
{cmd_tpm_signed_timestamp, "signed_timestamp", "TPM IAK signed timestamp attestation report"},
208+
{cmd_tpm_quote, "quote", "TPM IAK signed PCR(s) attestation report"},
209+
#endif
202210
#endif
203211
{NULL, "", ""}
204212
};
@@ -782,9 +790,8 @@ static int cmd_tpm_info(const char *args)
782790
/* Read measured boot PCR */
783791
if (rc == 0) {
784792
char algName[24];
785-
printf("Measured boot: PCR %s %d\r\n",
786-
wolfBoot_tpm2_get_alg_name(WOLFBOOT_TPM_PCR_ALG, algName, sizeof(algName)),
787-
WOLFBOOT_MEASURED_PCR_A);
793+
printf("Measured boot: PCR %d - %s\r\n", WOLFBOOT_MEASURED_PCR_A,
794+
wolfBoot_tpm2_get_alg_name(WOLFBOOT_TPM_PCR_ALG, algName, sizeof(algName)));
788795
hashSz = 0;
789796
rc = wolfBoot_tpm2_read_pcr(WOLFBOOT_MEASURED_PCR_A, hashBuf, &hashSz);
790797
if (rc == 0) {
@@ -807,6 +814,7 @@ static int cmd_tpm_info(const char *args)
807814
return rc;
808815
}
809816

817+
#ifdef WOLFTPM_MFG_IDENTITY
810818
static int cmd_tpm_idevid(const char *args)
811819
{
812820
int rc;
@@ -819,6 +827,11 @@ static int cmd_tpm_idevid(const char *args)
819827
printf("IDevID Handle 0x%x\r\n", (unsigned int)handle);
820828
print_hex(cert, certSz, 1);
821829
}
830+
else {
831+
char error[100];
832+
printf("TPM error 0x%x: %s\r\n",
833+
rc, wolfBoot_tpm2_get_rc_string(rc, error, sizeof(error)));
834+
}
822835
return rc;
823836
}
824837

@@ -834,8 +847,126 @@ static int cmd_tpm_iak(const char *args)
834847
printf("IAK Handle 0x%x\r\n", (unsigned int)handle);
835848
print_hex(cert, certSz, 1);
836849
}
850+
else {
851+
char error[100];
852+
printf("TPM error 0x%x: %s\r\n",
853+
rc, wolfBoot_tpm2_get_rc_string(rc, error, sizeof(error)));
854+
}
837855
return rc;
838856
}
857+
858+
static int cmd_tpm_signed_timestamp(const char *args)
859+
{
860+
int rc;
861+
WOLFTPM2_KEY aik;
862+
GetTime_Out getTime;
863+
TPMS_ATTEST timeAttest;
864+
865+
rc = wolfBoot_tpm2_get_aik(&aik, NULL, 0);
866+
if (rc == 0) {
867+
rc = wolfBoot_tpm2_get_timestamp(&aik, &getTime);
868+
}
869+
if (rc == 0) {
870+
rc = wolfBoot_tpm2_parse_attest(&getTime.timeInfo, &timeAttest);
871+
}
872+
if (rc == 0) {
873+
if (timeAttest.magic != TPM_GENERATED_VALUE) {
874+
printf("\tError, attested data not generated by the TPM = 0x%X\n",
875+
(unsigned int)timeAttest.magic);
876+
}
877+
878+
printf("TPM with signature attests (type 0x%x):\n", timeAttest.type);
879+
/* time value in milliseconds that advances while the TPM is powered */
880+
printf("\tTPM uptime since last power-up (in ms): %lu\n",
881+
(unsigned long)timeAttest.attested.time.time.time);
882+
/* time value in milliseconds that advances while the TPM is powered */
883+
printf("\tTPM clock, total time the TPM has been on (in ms): %lu\n",
884+
(unsigned long)timeAttest.attested.time.time.clockInfo.clock);
885+
/* number of occurrences of TPM Reset since the last TPM2_Clear() */
886+
printf("\tReset Count: %u\n",
887+
(unsigned int)timeAttest.attested.time.time.clockInfo.resetCount);
888+
/* number of times that TPM2_Shutdown() or _TPM_Hash_Start have occurred since the last TPM Reset or TPM2_Clear(). */
889+
printf("\tRestart Count: %u\n",
890+
(unsigned int)timeAttest.attested.time.time.clockInfo.restartCount);
891+
/* This parameter is set to YES when the value reported in Clock is guaranteed to be unique for the current Owner */
892+
printf("\tClock Safe: %u\n",
893+
timeAttest.attested.time.time.clockInfo.safe);
894+
/* a TPM vendor-specific value indicating the version number of the firmware */
895+
printf("\tFirmware Version (vendor specific): 0x%lX\n",
896+
(unsigned long)timeAttest.attested.time.firmwareVersion);
897+
}
898+
899+
if (rc != 0) {
900+
char error[100];
901+
printf("TPM get timestamp error 0x%x: %s\r\n",
902+
rc, wolfBoot_tpm2_get_rc_string(rc, error, sizeof(error)));
903+
}
904+
905+
return rc;
906+
}
907+
908+
static int cmd_tpm_quote(const char *args)
909+
{
910+
int rc;
911+
WOLFTPM2_KEY aik;
912+
Quote_Out quoteResult;
913+
TPMS_ATTEST quoteAttest;
914+
uint8_t pcrArray[1];
915+
uint32_t pcrArraySz = 0;
916+
char algName[24];
917+
918+
#ifdef WOLFBOOT_MEASURED_PCR_A
919+
pcrArray[0] = WOLFBOOT_MEASURED_PCR_A;
920+
pcrArraySz++;
921+
#else
922+
pcrArray[0] = 16; /* test PCR */
923+
pcrArraySz++;
924+
#endif
925+
926+
rc = wolfBoot_tpm2_get_aik(&aik, NULL, 0);
927+
if (rc == 0) {
928+
rc = wolfBoot_tpm2_quote(&aik, pcrArray, pcrArraySz, &quoteResult);
929+
}
930+
if (rc == 0) {
931+
rc = wolfBoot_tpm2_parse_attest(&quoteResult.quoted, &quoteAttest);
932+
}
933+
if (rc == 0) {
934+
TPMT_SIGNATURE* sig = &quoteResult.signature;
935+
printf("TPM with signature attests (type 0x%x):\n", quoteAttest.type);
936+
printf("\tTPM signed %lu count of PCRs\n",
937+
(unsigned long)quoteAttest.attested.quote.pcrSelect.count);
938+
939+
printf("\tPCR digest:\n");
940+
print_hex(quoteAttest.attested.quote.pcrDigest.buffer,
941+
quoteAttest.attested.quote.pcrDigest.size, 0);
942+
943+
printf("\tTPM generated %s signature:\n",
944+
wolfBoot_tpm2_get_alg_name(sig->sigAlg, algName, sizeof(algName)));
945+
printf("\tHash algorithm: %s\n",
946+
wolfBoot_tpm2_get_alg_name(sig->signature.any.hashAlg, algName, sizeof(algName)));
947+
switch (sig->sigAlg) {
948+
case TPM_ALG_ECDSA:
949+
case TPM_ALG_ECDAA:
950+
printf("\tR size: %d\n", sig->signature.ecdsa.signatureR.size);
951+
print_hex(sig->signature.ecdsa.signatureR.buffer, sig->signature.ecdsa.signatureR.size, 0);
952+
printf("\tS size: %d\n", sig->signature.ecdsa.signatureS.size);
953+
print_hex(sig->signature.ecdsa.signatureS.buffer, sig->signature.ecdsa.signatureS.size, 0);
954+
break;
955+
case TPM_ALG_RSASSA:
956+
case TPM_ALG_RSAPSS:
957+
printf("\tSignature size: %d\n", sig->signature.rsassa.sig.size);
958+
print_hex(sig->signature.rsassa.sig.buffer, sig->signature.rsassa.sig.size, 0);
959+
break;
960+
};
961+
}
962+
else {
963+
char error[100];
964+
printf("TPM quote error 0x%x: %s\r\n", rc,
965+
wolfBoot_tpm2_get_rc_string(rc, error, sizeof(error)));
966+
}
967+
return rc;
968+
}
969+
#endif /* WOLFTPM_MFG_IDENTITY */
839970
#endif /* WOLFBOOT_TPM */
840971

841972

@@ -965,6 +1096,11 @@ void main(void)
9651096
printf("Version : 0x%lx\r\n", app_version);
9661097
printf("========================\r\n");
9671098

1099+
cmd_info(NULL);
1100+
#ifdef WOLFBOOT_TPM
1101+
cmd_tpm_info(NULL);
1102+
#endif
1103+
9681104
console_loop();
9691105

9701106
while(1)

0 commit comments

Comments
 (0)