Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
| 5 | * Copyright 2017 Facebook Inc. |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 6 | * Copyright 2018 Siemens AG |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <console/cbmem_console.h> |
| 19 | #include <console/console.h> |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 20 | #include <security/tpm/tspi.h> |
| 21 | #include <security/tpm/tss.h> |
| 22 | #include <stdlib.h> |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 23 | #if CONFIG(VBOOT) |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 24 | #include <vb2_api.h> |
Joel Kitching | 2eb89c8 | 2019-04-25 17:45:12 +0800 | [diff] [blame] | 25 | #include <vb2_sha.h> |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 26 | #include <assert.h> |
| 27 | #endif |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 28 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 29 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 30 | static uint32_t tpm1_invoke_state_machine(void) |
| 31 | { |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 32 | uint8_t disabled; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 33 | uint8_t deactivated; |
| 34 | uint32_t result = TPM_SUCCESS; |
| 35 | |
| 36 | /* Check that the TPM is enabled and activated. */ |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 37 | result = tlcl_get_flags(&disabled, &deactivated, NULL); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 38 | if (result != TPM_SUCCESS) { |
| 39 | printk(BIOS_ERR, "TPM: Can't read capabilities.\n"); |
| 40 | return result; |
| 41 | } |
| 42 | |
Philipp Deppenwiese | 4d2af9d | 2018-08-14 09:46:55 -0700 | [diff] [blame] | 43 | if (disabled) { |
| 44 | printk(BIOS_INFO, "TPM: is disabled. Enabling...\n"); |
| 45 | |
| 46 | result = tlcl_set_enable(); |
| 47 | if (result != TPM_SUCCESS) { |
| 48 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 49 | return result; |
| 50 | } |
| 51 | } |
| 52 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 53 | if (!!deactivated != CONFIG(TPM_DEACTIVATE)) { |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 54 | printk(BIOS_INFO, |
| 55 | "TPM: Unexpected TPM deactivated state. Toggling...\n"); |
| 56 | result = tlcl_set_deactivated(!deactivated); |
| 57 | if (result != TPM_SUCCESS) { |
| 58 | printk(BIOS_ERR, |
| 59 | "TPM: Can't toggle deactivated state.\n"); |
| 60 | return result; |
| 61 | } |
| 62 | |
| 63 | deactivated = !deactivated; |
| 64 | result = TPM_E_MUST_REBOOT; |
| 65 | } |
| 66 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 67 | return result; |
| 68 | } |
| 69 | #endif |
| 70 | |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 71 | static uint32_t tpm_setup_s3_helper(void) |
| 72 | { |
| 73 | uint32_t result; |
| 74 | |
| 75 | result = tlcl_resume(); |
| 76 | switch (result) { |
| 77 | case TPM_SUCCESS: |
| 78 | break; |
| 79 | |
| 80 | case TPM_E_INVALID_POSTINIT: |
| 81 | /* |
| 82 | * We're on a platform where the TPM maintains power |
| 83 | * in S3, so it's already initialized. |
| 84 | */ |
| 85 | printk(BIOS_INFO, "TPM: Already initialized.\n"); |
| 86 | result = TPM_SUCCESS; |
| 87 | break; |
| 88 | |
| 89 | default: |
| 90 | printk(BIOS_ERR, "TPM: Resume failed (%#x).\n", result); |
| 91 | break; |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | return result; |
| 95 | } |
| 96 | |
| 97 | static uint32_t tpm_setup_epilogue(uint32_t result) |
| 98 | { |
| 99 | if (result != TPM_SUCCESS) |
| 100 | post_code(POST_TPM_FAILURE); |
| 101 | else |
| 102 | printk(BIOS_INFO, "TPM: setup succeeded\n"); |
| 103 | |
| 104 | return result; |
| 105 | } |
| 106 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 107 | /* |
| 108 | * tpm_setup starts the TPM and establishes the root of trust for the |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 109 | * anti-rollback mechanism. tpm_setup can fail for three reasons. 1 A bug. |
| 110 | * 2 a TPM hardware failure. 3 An unexpected TPM state due to some attack. In |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 111 | * general we cannot easily distinguish the kind of failure, so our strategy is |
Jonathan Neuschäfer | 61322d7 | 2018-10-29 22:52:42 +0100 | [diff] [blame] | 112 | * to reboot in recovery mode in all cases. The recovery mode calls tpm_setup |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 113 | * again, which executes (almost) the same sequence of operations. There is a |
| 114 | * good chance that, if recovery mode was entered because of a TPM failure, the |
| 115 | * failure will repeat itself. (In general this is impossible to guarantee |
| 116 | * because we have no way of creating the exact TPM initial state at the |
| 117 | * previous boot.) In recovery mode, we ignore the failure and continue, thus |
| 118 | * giving the recovery kernel a chance to fix things (that's why we don't set |
| 119 | * bGlobalLock). The choice is between a knowingly insecure device and a |
| 120 | * bricked device. |
| 121 | * |
| 122 | * As a side note, observe that we go through considerable hoops to avoid using |
| 123 | * the STCLEAR permissions for the index spaces. We do this to avoid writing |
| 124 | * to the TPM flashram at every reboot or wake-up, because of concerns about |
| 125 | * the durability of the NVRAM. |
| 126 | */ |
| 127 | uint32_t tpm_setup(int s3flag) |
| 128 | { |
| 129 | uint32_t result; |
| 130 | |
| 131 | result = tlcl_lib_init(); |
| 132 | if (result != TPM_SUCCESS) { |
| 133 | printk(BIOS_ERR, "TPM: Can't initialize.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 134 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* Handle special init for S3 resume path */ |
| 138 | if (s3flag) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 139 | printk(BIOS_INFO, "TPM: Handle S3 resume.\n"); |
| 140 | return tpm_setup_epilogue(tpm_setup_s3_helper()); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | result = tlcl_startup(); |
| 144 | if (result != TPM_SUCCESS) { |
| 145 | printk(BIOS_ERR, "TPM: Can't run startup command.\n"); |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 146 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | result = tlcl_assert_physical_presence(); |
| 150 | if (result != TPM_SUCCESS) { |
| 151 | /* |
| 152 | * It is possible that the TPM was delivered with the physical |
| 153 | * presence command disabled. This tries enabling it, then |
| 154 | * tries asserting PP again. |
| 155 | */ |
| 156 | result = tlcl_physical_presence_cmd_enable(); |
| 157 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 158 | printk(BIOS_ERR, "TPM: Can't enable physical presence command.\n"); |
| 159 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | result = tlcl_assert_physical_presence(); |
| 163 | if (result != TPM_SUCCESS) { |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 164 | printk(BIOS_ERR, "TPM: Can't assert physical presence.\n"); |
| 165 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 169 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 170 | result = tpm1_invoke_state_machine(); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 171 | #endif |
| 172 | |
Joel Kitching | 9937a06 | 2018-10-11 18:16:59 +0800 | [diff] [blame] | 173 | return tpm_setup_epilogue(result); |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | uint32_t tpm_clear_and_reenable(void) |
| 177 | { |
| 178 | uint32_t result; |
| 179 | |
| 180 | printk(BIOS_INFO, "TPM: Clear and re-enable\n"); |
| 181 | result = tlcl_force_clear(); |
| 182 | if (result != TPM_SUCCESS) { |
| 183 | printk(BIOS_ERR, "TPM: Can't initiate a force clear.\n"); |
| 184 | return result; |
| 185 | } |
| 186 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 187 | #if CONFIG(TPM1) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 188 | result = tlcl_set_enable(); |
| 189 | if (result != TPM_SUCCESS) { |
| 190 | printk(BIOS_ERR, "TPM: Can't set enabled state.\n"); |
| 191 | return result; |
| 192 | } |
| 193 | |
| 194 | result = tlcl_set_deactivated(0); |
| 195 | if (result != TPM_SUCCESS) { |
| 196 | printk(BIOS_ERR, "TPM: Can't set deactivated state.\n"); |
| 197 | return result; |
| 198 | } |
| 199 | #endif |
| 200 | |
| 201 | return TPM_SUCCESS; |
| 202 | } |
| 203 | |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 204 | uint32_t tpm_extend_pcr(int pcr, enum vb2_hash_algorithm digest_algo, |
| 205 | uint8_t *digest, size_t digest_len, const char *name) |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 206 | { |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 207 | uint32_t result; |
| 208 | |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 209 | if (!digest) |
| 210 | return TPM_E_IOERROR; |
| 211 | |
Philipp Deppenwiese | f849972 | 2018-07-30 01:27:47 +0200 | [diff] [blame] | 212 | result = tlcl_extend(pcr, digest, NULL); |
| 213 | if (result != TPM_SUCCESS) |
| 214 | return result; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 215 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 216 | if (CONFIG(VBOOT_MEASURED_BOOT)) |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 217 | tcpa_log_add_table_entry(name, pcr, digest_algo, |
| 218 | digest, digest_len); |
| 219 | |
Furquan Shaikh | 38f3ffa | 2018-07-31 14:26:39 -0700 | [diff] [blame] | 220 | return TPM_SUCCESS; |
Philipp Deppenwiese | c07f8fb | 2018-02-27 19:40:52 +0100 | [diff] [blame] | 221 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 222 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 223 | #if CONFIG(VBOOT) |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 224 | uint32_t tpm_measure_region(const struct region_device *rdev, uint8_t pcr, |
| 225 | const char *rname) |
| 226 | { |
| 227 | uint8_t digest[TPM_PCR_MAX_LEN], digest_len; |
| 228 | uint8_t buf[HASH_DATA_CHUNK_SIZE]; |
| 229 | uint32_t result, offset; |
| 230 | size_t len; |
| 231 | struct vb2_digest_context ctx; |
| 232 | enum vb2_hash_algorithm hash_alg; |
| 233 | |
| 234 | if (!rdev || !rname) |
| 235 | return TPM_E_INVALID_ARG; |
| 236 | result = tlcl_lib_init(); |
| 237 | if (result != TPM_SUCCESS) { |
| 238 | printk(BIOS_ERR, "TPM: Can't initialize library.\n"); |
| 239 | return result; |
| 240 | } |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 241 | if (CONFIG(TPM1)) { |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 242 | hash_alg = VB2_HASH_SHA1; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 243 | } else { /* CONFIG_TPM2 */ |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 244 | hash_alg = VB2_HASH_SHA256; |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 245 | } |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 246 | |
| 247 | digest_len = vb2_digest_size(hash_alg); |
| 248 | assert(digest_len <= sizeof(digest)); |
| 249 | if (vb2_digest_init(&ctx, hash_alg)) { |
| 250 | printk(BIOS_ERR, "TPM: Error initializing hash.\n"); |
| 251 | return TPM_E_HASH_ERROR; |
| 252 | } |
| 253 | /* |
| 254 | * Though one can mmap the full needed region on x86 this is not the |
| 255 | * case for e.g. ARM. In order to make this code as universal as |
| 256 | * possible across different platforms read the data to hash in chunks. |
| 257 | */ |
| 258 | for (offset = 0; offset < region_device_sz(rdev); offset += len) { |
| 259 | len = MIN(sizeof(buf), region_device_sz(rdev) - offset); |
| 260 | if (rdev_readat(rdev, buf, offset, len) < 0) { |
| 261 | printk(BIOS_ERR, "TPM: Not able to read region %s.\n", |
Philipp Deppenwiese | 66f9a09 | 2018-11-08 10:59:40 +0100 | [diff] [blame] | 262 | rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 263 | return TPM_E_READ_FAILURE; |
| 264 | } |
| 265 | if (vb2_digest_extend(&ctx, buf, len)) { |
| 266 | printk(BIOS_ERR, "TPM: Error extending hash.\n"); |
| 267 | return TPM_E_HASH_ERROR; |
| 268 | } |
| 269 | } |
| 270 | if (vb2_digest_finalize(&ctx, digest, digest_len)) { |
| 271 | printk(BIOS_ERR, "TPM: Error finalizing hash.\n"); |
| 272 | return TPM_E_HASH_ERROR; |
| 273 | } |
Philipp Deppenwiese | c9b7d1f | 2018-11-10 00:35:02 +0100 | [diff] [blame] | 274 | result = tpm_extend_pcr(pcr, hash_alg, digest, digest_len, rname); |
Werner Zeh | 30cf14f | 2018-10-23 07:40:08 +0200 | [diff] [blame] | 275 | if (result != TPM_SUCCESS) { |
| 276 | printk(BIOS_ERR, "TPM: Extending hash into PCR failed.\n"); |
| 277 | return result; |
| 278 | } |
| 279 | printk(BIOS_DEBUG, "TPM: Measured %s into PCR %d\n", rname, pcr); |
| 280 | return TPM_SUCCESS; |
| 281 | } |
| 282 | #endif /* VBOOT */ |