Jacob Garber | fa8f567 | 2020-05-18 13:18:19 -0600 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 2 | |
| 3 | /* |
| 4 | * TPM Lightweight Command Library. |
| 5 | * |
| 6 | * A low-level library for interfacing to TPM hardware or an emulator. |
| 7 | */ |
| 8 | |
Philipp Deppenwiese | d88fb36 | 2017-10-18 20:26:18 +0200 | [diff] [blame] | 9 | #ifndef TSS_H_ |
| 10 | #define TSS_H_ |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 11 | |
Vadim Bendebury | 245d457 | 2016-04-05 16:01:57 -0700 | [diff] [blame] | 12 | #include <types.h> |
Sergii Dmytruk | 7221a6c | 2022-10-22 20:11:35 +0300 | [diff] [blame] | 13 | #include <vb2_sha.h> |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 14 | |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 15 | #include <security/tpm/tis.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 16 | #include <security/tpm/tss_errors.h> |
| 17 | #include <security/tpm/tss/vendor/cr50/cr50.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 18 | #include <security/tpm/tss/tcg-1.2/tss_structures.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 19 | #include <security/tpm/tss/tcg-2.0/tss_structures.h> |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 20 | #include <security/tpm/tss1.h> |
| 21 | #include <security/tpm/tss2.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 22 | |
| 23 | /* |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 24 | * Operations that are applicable to both TPM versions have wrappers which |
| 25 | * pick the implementation based on version determined during initialization via |
| 26 | * tlcl_lib_init(). |
| 27 | * |
| 28 | * Other operations are defined in tss1.h and tss2.h. |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 29 | */ |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 30 | |
| 31 | /** |
| 32 | * Call this first. Returns 0 if success, nonzero if error. |
| 33 | */ |
Jon Murphy | d7b8dc9 | 2023-09-05 11:36:43 -0600 | [diff] [blame] | 34 | tpm_result_t tlcl_lib_init(void); |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 35 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame^] | 36 | /** |
| 37 | * Query active TPM family. Returns TPM_UNKNOWN if uninitialized and TPM_1 or TPM_2 otherwise. |
| 38 | */ |
| 39 | static inline enum tpm_family tlcl_get_family(void) |
| 40 | { |
| 41 | /* Defined in tss/tss.c */ |
| 42 | extern enum tpm_family tlcl_tpm_family; |
| 43 | |
| 44 | if (CONFIG(TPM1) && CONFIG(TPM2)) |
| 45 | return tlcl_tpm_family; |
| 46 | if (CONFIG(TPM1)) |
| 47 | return TPM_1; |
| 48 | if (CONFIG(TPM2)) |
| 49 | return TPM_2; |
| 50 | return TPM_UNKNOWN; |
| 51 | } |
| 52 | |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 53 | /* Commands */ |
| 54 | |
Sergii Dmytruk | 47e9e8c | 2022-11-02 00:50:03 +0200 | [diff] [blame^] | 55 | #define TLCL_CALL(name, ...) do { \ |
| 56 | if (tlcl_get_family() == TPM_1) \ |
| 57 | return tlcl1_##name(__VA_ARGS__); \ |
| 58 | if (tlcl_get_family() == TPM_2) \ |
| 59 | return tlcl2_##name(__VA_ARGS__); \ |
| 60 | return TPM_CB_INTERNAL_INCONSISTENCY; \ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 61 | } while (0) |
| 62 | |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 63 | /** |
| 64 | * Send a TPM_Startup(ST_CLEAR). The TPM error code is returned (0 for |
| 65 | * success). |
| 66 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 67 | static inline tpm_result_t tlcl_startup(void) |
| 68 | { |
| 69 | TLCL_CALL(startup); |
| 70 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 71 | |
| 72 | /** |
| 73 | * Resume by sending a TPM_Startup(ST_STATE). The TPM error code is returned |
| 74 | * (0 for success). |
| 75 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 76 | static inline tpm_result_t tlcl_resume(void) |
| 77 | { |
| 78 | TLCL_CALL(resume); |
| 79 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 80 | |
| 81 | /** |
Joel Kitching | 2e690ee | 2018-11-15 16:48:53 +0800 | [diff] [blame] | 82 | * Save TPM state by sending either TPM_SaveState() (TPM1.2) or |
| 83 | * TPM_Shutdown(ST_STATE) (TPM2.0). The TPM error code is returned (0 for |
| 84 | * success). |
| 85 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 86 | static inline tpm_result_t tlcl_save_state(void) |
| 87 | { |
| 88 | TLCL_CALL(save_state); |
| 89 | } |
Joel Kitching | 2e690ee | 2018-11-15 16:48:53 +0800 | [diff] [blame] | 90 | |
| 91 | /** |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 92 | * Run the self test. |
| 93 | * |
| 94 | * Note---this is synchronous. To run this in parallel with other firmware, |
| 95 | * use ContinueSelfTest(). The TPM error code is returned. |
| 96 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 97 | static inline tpm_result_t tlcl_self_test_full(void) |
| 98 | { |
| 99 | TLCL_CALL(self_test_full); |
| 100 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 101 | |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 102 | /** |
| 103 | * Write [length] bytes of [data] to space at [index]. The TPM error code is |
| 104 | * returned. |
| 105 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 106 | static inline tpm_result_t tlcl_write(uint32_t index, const void *data, uint32_t length) |
| 107 | { |
| 108 | TLCL_CALL(write, index, data, length); |
| 109 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 110 | |
| 111 | /** |
| 112 | * Read [length] bytes from space at [index] into [data]. The TPM error code |
| 113 | * is returned. |
| 114 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 115 | static inline tpm_result_t tlcl_read(uint32_t index, void *data, uint32_t length) |
| 116 | { |
| 117 | TLCL_CALL(read, index, data, length); |
| 118 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Assert physical presence in software. The TPM error code is returned. |
| 122 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 123 | static inline tpm_result_t tlcl_assert_physical_presence(void) |
| 124 | { |
| 125 | TLCL_CALL(assert_physical_presence); |
| 126 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
| 129 | * Enable the physical presence command. The TPM error code is returned. |
| 130 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 131 | static inline tpm_result_t tlcl_physical_presence_cmd_enable(void) |
| 132 | { |
| 133 | TLCL_CALL(physical_presence_cmd_enable); |
| 134 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 135 | |
| 136 | /** |
Frans Hendriks | 8bd5c99 | 2018-10-29 10:47:52 +0100 | [diff] [blame] | 137 | * Finalize the physical presence settings: software PP is enabled, hardware PP |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 138 | * is disabled, and the lifetime lock is set. The TPM error code is returned. |
| 139 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 140 | static inline tpm_result_t tlcl_finalize_physical_presence(void) |
| 141 | { |
| 142 | TLCL_CALL(finalize_physical_presence); |
| 143 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Issue a ForceClear. The TPM error code is returned. |
| 147 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 148 | static inline tpm_result_t tlcl_force_clear(void) |
| 149 | { |
| 150 | TLCL_CALL(force_clear); |
| 151 | } |
Vadim Bendebury | 4c0851c | 2016-07-03 17:08:10 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 154 | * Perform a TPM_Extend. |
| 155 | */ |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 156 | static inline tpm_result_t tlcl_extend(int pcr_num, const uint8_t *digest_data, |
| 157 | enum vb2_hash_algorithm digest_algo) |
| 158 | { |
| 159 | TLCL_CALL(extend, pcr_num, digest_data, digest_algo); |
| 160 | } |
Daisuke Nojiri | efb5cde | 2014-07-02 08:37:23 -0700 | [diff] [blame] | 161 | |
Sergii Dmytruk | 094a051 | 2022-10-31 18:41:52 +0200 | [diff] [blame] | 162 | extern tis_sendrecv_fn tlcl_tis_sendrecv; |
Daisuke Nojiri | d9f26ed | 2020-04-21 15:13:07 -0700 | [diff] [blame] | 163 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 164 | #endif /* TSS_H_ */ |