/* tpm.h * * Copyright (C) 2026 wolfSSL Inc. * * This file is part of wolfBoot. * * wolfBoot is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * wolfBoot is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA */ #ifndef _WOLFBOOT_TPM_H_ #define _WOLFBOOT_TPM_H_ #ifdef WOLFBOOT_TPM #include #include "wolftpm/tpm2.h" #include "wolftpm/tpm2_wrap.h" extern WOLFTPM2_DEV wolftpm_dev; #if defined(WOLFBOOT_TPM_KEYSTORE) || defined(WOLFBOOT_TPM_SEAL) extern WOLFTPM2_SESSION wolftpm_session; extern WOLFTPM2_KEY wolftpm_srk; #endif #ifndef WOLFBOOT_TPM_KEYSTORE_NV_BASE #define WOLFBOOT_TPM_KEYSTORE_NV_BASE 0x01400200 #endif #ifndef WOLFBOOT_TPM_SEAL_NV_BASE #define WOLFBOOT_TPM_SEAL_NV_BASE 0x01400300 #endif #ifndef WOLFBOOT_TPM_PCR_ALG /* Prefer SHA2-256 for PCR's, and all TPM 2.0 devices support it */ #define WOLFBOOT_TPM_PCR_ALG TPM_ALG_SHA256 #define WOLFBOOT_TPM_PCR_DIG_SZ 32 #endif #define WOLFBOOT_MAX_SEAL_SZ MAX_SYM_DATA /* API's that are callable from non-secure code */ int CSME_NSE_API wolfBoot_tpm2_caps(WOLFTPM2_CAPS* caps); int CSME_NSE_API wolfBoot_tpm2_get_handles(TPM_HANDLE handle, TPML_HANDLE* handles); const char* CSME_NSE_API wolfBoot_tpm2_get_alg_name(TPM_ALG_ID alg, char* name, int name_sz); const char* CSME_NSE_API wolfBoot_tpm2_get_rc_string(int rc, char* error, int error_sz); int CSME_NSE_API wolfBoot_tpm2_get_capability(GetCapability_In* in, GetCapability_Out* out); int CSME_NSE_API wolfBoot_tpm2_read_pcr(uint8_t pcrIndex, uint8_t* digest, int* digestSz); int CSME_NSE_API wolfBoot_tpm2_read_cert(uint32_t handle, uint8_t* cert, uint32_t* certSz); #ifdef WOLFTPM_MFG_IDENTITY /* MFG identity auth provisioning. * Precomputed mode (default): the final per-device authValue is set directly, * no master secret on the device. In this mode, wolfBoot_tpm2_get_aik() treats * the authOverride argument as an optional *authValue* override. * Derive mode (WOLFBOOT_TPM_MFG_AUTH_DERIVE): authValue = low 16 bytes of * SHA-256(TPM serial || master); the master is shared across the reel. * For wolfBoot_tpm2_get_aik() the master secret is provided via the * authOverride argument (NULL = sample). */ #ifdef WOLFBOOT_TPM_MFG_AUTH_DERIVE /* EH master for derive mode (sample - override in production) */ #ifndef WOLFBOOT_TPM_MFG_EH_MASTER #define WOLFBOOT_TPM_MFG_EH_MASTER { \ 0xDE, 0xEF, 0x8C, 0xDF, 0x1B, 0x77, 0xBD, 0x00, \ 0x30, 0x58, 0x5E, 0x47, 0xB8, 0x21, 0x46, 0x0B } #endif #else /* 16-byte per-device authValues. Placeholder defaults (all 0xFF) fail TPM auth * until overwritten per-device by the provisioning tool. */ #ifndef WOLFBOOT_TPM_MFG_AIK_AUTH #define WOLFBOOT_TPM_MFG_AIK_AUTH { \ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } #endif #ifndef WOLFBOOT_TPM_MFG_EH_AUTH #define WOLFBOOT_TPM_MFG_EH_AUTH { \ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, \ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF } #endif #endif /* authOverride meaning depends on WOLFBOOT_TPM_MFG_AUTH_DERIVE: * derive mode -> master secret hashed into the authValue (NULL = sample) * precomputed mode -> optional literal authValue override (NULL = built-in) */ int CSME_NSE_API wolfBoot_tpm2_get_aik(WOLFTPM2_KEY* aik, uint8_t* authOverride, uint16_t authOverrideSz); int CSME_NSE_API wolfBoot_tpm2_get_timestamp(WOLFTPM2_KEY* aik, GetTime_Out* getTime); int CSME_NSE_API wolfBoot_tpm2_quote(WOLFTPM2_KEY* aik, byte* pcrArray, word32 pcrArraySz, Quote_Out* quoteResult); int CSME_NSE_API wolfBoot_tpm2_parse_attest(const TPM2B_ATTEST* in, TPMS_ATTEST* out); #endif /* Internal wolfBoot TPM API's */ int wolfBoot_tpm2_init(void); void wolfBoot_tpm2_deinit(void); int wolfBoot_tpm2_clear(void); #if defined(WOLFBOOT_TPM_VERIFY) || defined(WOLFBOOT_TPM_SEAL) int wolfBoot_load_pubkey(const uint8_t* pubkey_hint, WOLFTPM2_KEY* pubKey, TPM_ALG_ID* pAlg); #endif #if defined(WOLFBOOT_TPM_KEYSTORE) || defined(WOLFBOOT_TPM_SEAL) int wolfBoot_constant_compare(const uint8_t* a, const uint8_t* b, uint32_t len); #endif #ifdef WOLFBOOT_TPM_KEYSTORE int wolfBoot_check_rot(int key_slot, uint8_t* pubkey_hint); #endif #ifdef WOLFBOOT_TPM_SEAL int wolfBoot_get_random(uint8_t* buf, int sz); int wolfBoot_get_pcr_active(uint8_t pcrAlg, uint32_t* pcrMask, uint8_t pcrMax); int wolfBoot_build_policy(uint8_t pcrAlg, uint32_t pcrMask, uint8_t* policy, uint32_t* policySz, uint8_t* policyRef, uint32_t policyRefSz); int wolfBoot_get_policy(struct wolfBoot_image* img, uint8_t** policy, uint16_t* policySz); int wolfBoot_seal(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, int index, const uint8_t* secret, int secret_sz); int wolfBoot_seal_auth(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, int index, const uint8_t* secret, int secret_sz, const uint8_t* auth, int authSz); int wolfBoot_seal_blob(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, WOLFTPM2_KEYBLOB* seal_blob, const uint8_t* secret, int secret_sz, const uint8_t* auth, int authSz); int wolfBoot_unseal(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, int index, uint8_t* secret, int* secret_sz); int wolfBoot_unseal_auth(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, int index, uint8_t* secret, int* secret_sz, const uint8_t* auth, int authSz); int wolfBoot_unseal_blob(const uint8_t* pubkey_hint, const uint8_t* policy, uint16_t policySz, WOLFTPM2_KEYBLOB* seal_blob, uint8_t* secret, int* secret_sz, const uint8_t* auth, int authSz); int wolfBoot_delete_seal(int index); int wolfBoot_read_blob(uint32_t nvIndex, WOLFTPM2_KEYBLOB* blob, const uint8_t* auth, uint32_t authSz); int wolfBoot_store_blob(TPMI_RH_NV_AUTH authHandle, uint32_t nvIndex, word32 nvAttributes, WOLFTPM2_KEYBLOB* blob, const uint8_t* auth, uint32_t authSz); int wolfBoot_delete_blob(TPMI_RH_NV_AUTH authHandle, uint32_t nvIndex, const uint8_t* auth, uint32_t authSz); uint32_t wolfBoot_tpm_pcrmask_sel(uint32_t pcrMask, uint8_t* pcrArray, uint32_t pcrArraySz); #endif #ifdef WOLFBOOT_MEASURED_BOOT int wolfBoot_tpm2_extend(uint8_t pcrIndex, uint8_t* hash, int line); /* helper for measuring boot at line */ #define measure_boot(hash) \ wolfBoot_tpm2_extend(WOLFBOOT_MEASURED_PCR_A, (hash), __LINE__) #endif /* WOLFBOOT_MEASURED_BOOT */ int wolfBoot_tpm_self_test(void); /* debugging */ void wolfBoot_print_hexstr(const unsigned char* bin, unsigned long sz, unsigned long maxLine); void wolfBoot_print_bin(const uint8_t* buffer, uint32_t length); #else /* stubs */ #define measure_boot(hash) #endif /* WOLFBOOT_TPM */ #endif /* !_WOLFBOOT_TPM_H_ */