Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 The Chromium OS Authors. |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 4 | * Coypright (c) 2013 Guntermann & Drunck GmbH |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 7 | #define LOG_CATEGORY UCLASS_TPM |
| 8 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 12 | #include <asm/unaligned.h> |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 13 | #include <u-boot/sha1.h> |
Miquel Raynal | 4c6759e | 2018-05-15 11:57:06 +0200 | [diff] [blame] | 14 | #include <tpm-common.h> |
| 15 | #include <tpm-v1.h> |
| 16 | #include "tpm-utils.h" |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 17 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 18 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 19 | |
| 20 | #ifndef CONFIG_SHA1 |
| 21 | #error "TPM_AUTH_SESSIONS require SHA1 to be configured, too" |
| 22 | #endif /* !CONFIG_SHA1 */ |
| 23 | |
| 24 | struct session_data { |
| 25 | int valid; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 26 | u32 handle; |
| 27 | u8 nonce_even[DIGEST_LENGTH]; |
| 28 | u8 nonce_odd[DIGEST_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | static struct session_data oiap_session = {0, }; |
| 32 | |
| 33 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
| 34 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 35 | u32 tpm1_startup(struct udevice *dev, enum tpm_startup_type mode) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 36 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 37 | const u8 command[12] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 38 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0, |
| 39 | }; |
| 40 | const size_t mode_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 41 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 42 | |
| 43 | if (pack_byte_string(buf, sizeof(buf), "sw", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 44 | 0, command, sizeof(command), |
| 45 | mode_offset, mode)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 46 | return TPM_LIB_ERROR; |
| 47 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 48 | return tpm_sendrecv_command(dev, buf, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 51 | u32 tpm1_resume(struct udevice *dev) |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 52 | { |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 53 | return tpm1_startup(dev, TPM_ST_STATE); |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 54 | } |
| 55 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 56 | u32 tpm1_self_test_full(struct udevice *dev) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 57 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 58 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 59 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50, |
| 60 | }; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 61 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 64 | u32 tpm1_continue_self_test(struct udevice *dev) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 65 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 66 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 67 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53, |
| 68 | }; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 69 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 72 | u32 tpm1_clear_and_reenable(struct udevice *dev) |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 73 | { |
| 74 | u32 ret; |
| 75 | |
| 76 | log_info("TPM: Clear and re-enable\n"); |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 77 | ret = tpm1_force_clear(dev); |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 78 | if (ret != TPM_SUCCESS) { |
| 79 | log_err("Can't initiate a force clear\n"); |
| 80 | return ret; |
| 81 | } |
| 82 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 83 | ret = tpm1_physical_enable(dev); |
| 84 | if (ret != TPM_SUCCESS) { |
| 85 | log_err("TPM: Can't set enabled state\n"); |
| 86 | return ret; |
| 87 | } |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 88 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 89 | ret = tpm1_physical_set_deactivated(dev, 0); |
| 90 | if (ret != TPM_SUCCESS) { |
| 91 | log_err("TPM: Can't set deactivated state\n"); |
| 92 | return ret; |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 93 | } |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 94 | |
| 95 | return TPM_SUCCESS; |
| 96 | } |
| 97 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 98 | u32 tpm1_nv_define_space(struct udevice *dev, u32 index, u32 perm, u32 size) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 99 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 100 | const u8 command[101] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 101 | 0x0, 0xc1, /* TPM_TAG */ |
| 102 | 0x0, 0x0, 0x0, 0x65, /* parameter size */ |
| 103 | 0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */ |
| 104 | /* TPM_NV_DATA_PUBLIC->... */ |
| 105 | 0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */ |
| 106 | 0, 0, 0, 0, /* ...->TPM_NV_INDEX */ |
| 107 | /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ |
| 108 | 0x0, 0x3, |
| 109 | 0, 0, 0, |
| 110 | 0x1f, |
| 111 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 112 | /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ |
| 113 | 0x0, 0x3, |
| 114 | 0, 0, 0, |
| 115 | 0x1f, |
| 116 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 117 | /* TPM_NV_ATTRIBUTES->... */ |
| 118 | 0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */ |
| 119 | 0, 0, 0, 0, /* ...->attributes */ |
| 120 | /* End of TPM_NV_ATTRIBUTES */ |
| 121 | 0, /* bReadSTClear */ |
| 122 | 0, /* bWriteSTClear */ |
| 123 | 0, /* bWriteDefine */ |
| 124 | 0, 0, 0, 0, /* size */ |
| 125 | }; |
| 126 | const size_t index_offset = 12; |
| 127 | const size_t perm_offset = 70; |
| 128 | const size_t size_offset = 77; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 129 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 130 | |
| 131 | if (pack_byte_string(buf, sizeof(buf), "sddd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 132 | 0, command, sizeof(command), |
| 133 | index_offset, index, |
| 134 | perm_offset, perm, |
| 135 | size_offset, size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 136 | return TPM_LIB_ERROR; |
| 137 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 138 | return tpm_sendrecv_command(dev, buf, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 141 | u32 tpm1_nv_set_locked(struct udevice *dev) |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 142 | { |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 143 | return tpm1_nv_define_space(dev, TPM_NV_INDEX_LOCK, 0, 0); |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 144 | } |
| 145 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 146 | u32 tpm1_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 147 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 148 | const u8 command[22] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 149 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf, |
| 150 | }; |
| 151 | const size_t index_offset = 10; |
| 152 | const size_t length_offset = 18; |
| 153 | const size_t data_size_offset = 10; |
| 154 | const size_t data_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 155 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 156 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 157 | u32 data_size; |
| 158 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 159 | |
| 160 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 161 | 0, command, sizeof(command), |
| 162 | index_offset, index, |
| 163 | length_offset, count)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 164 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 165 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 166 | if (err) |
| 167 | return err; |
| 168 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 169 | data_size_offset, &data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 170 | return TPM_LIB_ERROR; |
| 171 | if (data_size > count) |
| 172 | return TPM_LIB_ERROR; |
| 173 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 174 | data_offset, data, data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 175 | return TPM_LIB_ERROR; |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 180 | u32 tpm1_nv_write_value(struct udevice *dev, u32 index, const void *data, |
| 181 | u32 length) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 182 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 183 | const u8 command[256] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 184 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd, |
| 185 | }; |
| 186 | const size_t command_size_offset = 2; |
| 187 | const size_t index_offset = 10; |
| 188 | const size_t length_offset = 18; |
| 189 | const size_t data_offset = 22; |
| 190 | const size_t write_info_size = 12; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 191 | const u32 total_length = |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 192 | TPM_REQUEST_HEADER_LENGTH + write_info_size + length; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 193 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 194 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 195 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 196 | |
| 197 | if (pack_byte_string(buf, sizeof(buf), "sddds", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 198 | 0, command, sizeof(command), |
| 199 | command_size_offset, total_length, |
| 200 | index_offset, index, |
| 201 | length_offset, length, |
| 202 | data_offset, data, length)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 203 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 204 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 205 | if (err) |
| 206 | return err; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 211 | u32 tpm1_extend(struct udevice *dev, u32 index, const void *in_digest, |
| 212 | void *out_digest) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 213 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 214 | const u8 command[34] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 215 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x14, |
| 216 | }; |
| 217 | const size_t index_offset = 10; |
| 218 | const size_t in_digest_offset = 14; |
| 219 | const size_t out_digest_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 220 | u8 buf[COMMAND_BUFFER_SIZE]; |
| 221 | u8 response[TPM_RESPONSE_HEADER_LENGTH + PCR_DIGEST_LENGTH]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 222 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 223 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 224 | |
| 225 | if (pack_byte_string(buf, sizeof(buf), "sds", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 226 | 0, command, sizeof(command), |
| 227 | index_offset, index, |
| 228 | in_digest_offset, in_digest, |
| 229 | PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 230 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 231 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 232 | if (err) |
| 233 | return err; |
| 234 | |
| 235 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 236 | out_digest_offset, out_digest, |
| 237 | PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 238 | return TPM_LIB_ERROR; |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 243 | u32 tpm1_pcr_read(struct udevice *dev, u32 index, void *data, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 244 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 245 | const u8 command[14] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 246 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x15, |
| 247 | }; |
| 248 | const size_t index_offset = 10; |
| 249 | const size_t out_digest_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 250 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 251 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 252 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 253 | |
| 254 | if (count < PCR_DIGEST_LENGTH) |
| 255 | return TPM_LIB_ERROR; |
| 256 | |
| 257 | if (pack_byte_string(buf, sizeof(buf), "sd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 258 | 0, command, sizeof(command), |
| 259 | index_offset, index)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 260 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 261 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 262 | if (err) |
| 263 | return err; |
| 264 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 265 | out_digest_offset, data, PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 266 | return TPM_LIB_ERROR; |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 271 | u32 tpm1_tsc_physical_presence(struct udevice *dev, u16 presence) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 272 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 273 | const u8 command[12] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 274 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x0, 0x0, |
| 275 | }; |
| 276 | const size_t presence_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 277 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 278 | |
| 279 | if (pack_byte_string(buf, sizeof(buf), "sw", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 280 | 0, command, sizeof(command), |
| 281 | presence_offset, presence)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 282 | return TPM_LIB_ERROR; |
| 283 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 284 | return tpm_sendrecv_command(dev, buf, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 287 | u32 tpm1_finalise_physical_presence(struct udevice *dev) |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 288 | { |
| 289 | const u8 command[12] = { |
| 290 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x2, 0xa0, |
| 291 | }; |
| 292 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 293 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 294 | } |
| 295 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 296 | u32 tpm1_read_pubek(struct udevice *dev, void *data, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 297 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 298 | const u8 command[30] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 299 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x7c, |
| 300 | }; |
| 301 | const size_t response_size_offset = 2; |
| 302 | const size_t data_offset = 10; |
| 303 | const size_t header_and_checksum_size = TPM_RESPONSE_HEADER_LENGTH + 20; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 304 | u8 response[COMMAND_BUFFER_SIZE + TPM_PUBEK_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 305 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 306 | u32 data_size; |
| 307 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 308 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 309 | err = tpm_sendrecv_command(dev, command, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 310 | if (err) |
| 311 | return err; |
| 312 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 313 | response_size_offset, &data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 314 | return TPM_LIB_ERROR; |
| 315 | if (data_size < header_and_checksum_size) |
| 316 | return TPM_LIB_ERROR; |
| 317 | data_size -= header_and_checksum_size; |
| 318 | if (data_size > count) |
| 319 | return TPM_LIB_ERROR; |
| 320 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 321 | data_offset, data, data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 322 | return TPM_LIB_ERROR; |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 327 | u32 tpm1_force_clear(struct udevice *dev) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 328 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 329 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 330 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x5d, |
| 331 | }; |
| 332 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 333 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 336 | u32 tpm1_physical_enable(struct udevice *dev) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 337 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 338 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 339 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x6f, |
| 340 | }; |
| 341 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 342 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 345 | u32 tpm1_physical_disable(struct udevice *dev) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 346 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 347 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 348 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x70, |
| 349 | }; |
| 350 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 351 | return tpm_sendrecv_command(dev, command, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 354 | u32 tpm1_physical_set_deactivated(struct udevice *dev, u8 state) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 355 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 356 | const u8 command[11] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 357 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x72, |
| 358 | }; |
| 359 | const size_t state_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 360 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 361 | |
| 362 | if (pack_byte_string(buf, sizeof(buf), "sb", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 363 | 0, command, sizeof(command), |
| 364 | state_offset, state)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 365 | return TPM_LIB_ERROR; |
| 366 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 367 | return tpm_sendrecv_command(dev, buf, NULL, NULL); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 370 | u32 tpm1_get_capability(struct udevice *dev, u32 cap_area, u32 sub_cap, |
| 371 | void *cap, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 372 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 373 | const u8 command[22] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 374 | 0x0, 0xc1, /* TPM_TAG */ |
| 375 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 376 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 377 | 0x0, 0x0, 0x0, 0x0, /* TPM_CAPABILITY_AREA */ |
| 378 | 0x0, 0x0, 0x0, 0x4, /* subcap size */ |
| 379 | 0x0, 0x0, 0x0, 0x0, /* subcap value */ |
| 380 | }; |
| 381 | const size_t cap_area_offset = 10; |
| 382 | const size_t sub_cap_offset = 18; |
| 383 | const size_t cap_offset = 14; |
| 384 | const size_t cap_size_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 385 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 386 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 387 | u32 cap_size; |
| 388 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 389 | |
| 390 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 391 | 0, command, sizeof(command), |
| 392 | cap_area_offset, cap_area, |
| 393 | sub_cap_offset, sub_cap)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 394 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 395 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 396 | if (err) |
| 397 | return err; |
| 398 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 399 | cap_size_offset, &cap_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 400 | return TPM_LIB_ERROR; |
| 401 | if (cap_size > response_length || cap_size > count) |
| 402 | return TPM_LIB_ERROR; |
| 403 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 404 | cap_offset, cap, cap_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 405 | return TPM_LIB_ERROR; |
| 406 | |
| 407 | return 0; |
| 408 | } |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 409 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 410 | u32 tpm1_get_permanent_flags(struct udevice *dev, |
| 411 | struct tpm_permanent_flags *pflags) |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 412 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 413 | const u8 command[22] = { |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 414 | 0x0, 0xc1, /* TPM_TAG */ |
| 415 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 416 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 417 | 0x0, 0x0, 0x0, 0x4, /* TPM_CAP_FLAG_PERM */ |
| 418 | 0x0, 0x0, 0x0, 0x4, /* subcap size */ |
| 419 | 0x0, 0x0, 0x1, 0x8, /* subcap value */ |
| 420 | }; |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 421 | const size_t data_size_offset = TPM_HEADER_SIZE; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 422 | const size_t data_offset = TPM_HEADER_SIZE + sizeof(u32); |
| 423 | u8 response[COMMAND_BUFFER_SIZE]; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 424 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 425 | u32 err; |
| 426 | u32 data_size; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 427 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 428 | err = tpm_sendrecv_command(dev, command, response, &response_length); |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 429 | if (err) |
| 430 | return err; |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 431 | if (unpack_byte_string(response, response_length, "d", |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 432 | data_size_offset, &data_size)) { |
| 433 | log_err("Cannot unpack data size\n"); |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 434 | return TPM_LIB_ERROR; |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 435 | } |
| 436 | if (data_size < sizeof(*pflags)) { |
| 437 | log_err("Data size too small\n"); |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 438 | return TPM_LIB_ERROR; |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 439 | } |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 440 | if (unpack_byte_string(response, response_length, "s", |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 441 | data_offset, pflags, sizeof(*pflags))) { |
| 442 | log_err("Cannot unpack pflags\n"); |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 443 | return TPM_LIB_ERROR; |
Simon Glass | bccabbe | 2018-10-01 12:22:29 -0600 | [diff] [blame] | 444 | } |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 449 | u32 tpm1_get_permissions(struct udevice *dev, u32 index, u32 *perm) |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 450 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 451 | const u8 command[22] = { |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 452 | 0x0, 0xc1, /* TPM_TAG */ |
| 453 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 454 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 455 | 0x0, 0x0, 0x0, 0x11, |
| 456 | 0x0, 0x0, 0x0, 0x4, |
| 457 | }; |
| 458 | const size_t index_offset = 18; |
Simon Glass | 9523a4e | 2022-08-30 21:05:33 -0600 | [diff] [blame^] | 459 | const size_t perm_offset = 74; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 460 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 461 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 462 | u32 err; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 463 | |
Simon Glass | 9523a4e | 2022-08-30 21:05:33 -0600 | [diff] [blame^] | 464 | if (pack_byte_string(buf, sizeof(buf), "sd", |
| 465 | 0, command, sizeof(command), |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 466 | index_offset, index)) |
| 467 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 468 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 469 | if (err) |
| 470 | return err; |
| 471 | if (unpack_byte_string(response, response_length, "d", |
| 472 | perm_offset, perm)) |
| 473 | return TPM_LIB_ERROR; |
| 474 | |
| 475 | return 0; |
| 476 | } |
| 477 | |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 478 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 479 | u32 tpm1_flush_specific(struct udevice *dev, u32 key_handle, u32 resource_type) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 480 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 481 | const u8 command[18] = { |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 482 | 0x00, 0xc1, /* TPM_TAG */ |
| 483 | 0x00, 0x00, 0x00, 0x12, /* parameter size */ |
| 484 | 0x00, 0x00, 0x00, 0xba, /* TPM_COMMAND_CODE */ |
| 485 | 0x00, 0x00, 0x00, 0x00, /* key handle */ |
| 486 | 0x00, 0x00, 0x00, 0x00, /* resource type */ |
| 487 | }; |
| 488 | const size_t key_handle_offset = 10; |
| 489 | const size_t resource_type_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 490 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 491 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 492 | u32 err; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 493 | |
| 494 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
| 495 | 0, command, sizeof(command), |
| 496 | key_handle_offset, key_handle, |
| 497 | resource_type_offset, resource_type)) |
| 498 | return TPM_LIB_ERROR; |
| 499 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 500 | err = tpm_sendrecv_command(dev, buf, response, &response_length); |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 501 | if (err) |
| 502 | return err; |
| 503 | return 0; |
| 504 | } |
| 505 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
| 506 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 507 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 508 | |
| 509 | /** |
| 510 | * Fill an authentication block in a request. |
| 511 | * This func can create the first as well as the second auth block (for |
| 512 | * double authorized commands). |
| 513 | * |
| 514 | * @param request pointer to the request (w/ uninitialised auth data) |
| 515 | * @param request_len0 length of the request without auth data |
| 516 | * @param handles_len length of the handles area in request |
| 517 | * @param auth_session pointer to the (valid) auth session to be used |
| 518 | * @param request_auth pointer to the auth block of the request to be filled |
| 519 | * @param auth authentication data (HMAC key) |
| 520 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 521 | static u32 create_request_auth(const void *request, size_t request_len0, |
| 522 | size_t handles_len, |
| 523 | struct session_data *auth_session, |
| 524 | void *request_auth, const void *auth) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 525 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 526 | u8 hmac_data[DIGEST_LENGTH * 3 + 1]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 527 | sha1_context hash_ctx; |
| 528 | const size_t command_code_offset = 6; |
| 529 | const size_t auth_nonce_odd_offset = 4; |
| 530 | const size_t auth_continue_offset = 24; |
| 531 | const size_t auth_auth_offset = 25; |
| 532 | |
| 533 | if (!auth_session || !auth_session->valid) |
| 534 | return TPM_LIB_ERROR; |
| 535 | |
| 536 | sha1_starts(&hash_ctx); |
| 537 | sha1_update(&hash_ctx, request + command_code_offset, 4); |
| 538 | if (request_len0 > TPM_REQUEST_HEADER_LENGTH + handles_len) |
| 539 | sha1_update(&hash_ctx, |
| 540 | request + TPM_REQUEST_HEADER_LENGTH + handles_len, |
| 541 | request_len0 - TPM_REQUEST_HEADER_LENGTH |
| 542 | - handles_len); |
| 543 | sha1_finish(&hash_ctx, hmac_data); |
| 544 | |
| 545 | sha1_starts(&hash_ctx); |
| 546 | sha1_update(&hash_ctx, auth_session->nonce_odd, DIGEST_LENGTH); |
| 547 | sha1_update(&hash_ctx, hmac_data, sizeof(hmac_data)); |
| 548 | sha1_finish(&hash_ctx, auth_session->nonce_odd); |
| 549 | |
| 550 | if (pack_byte_string(request_auth, TPM_REQUEST_AUTH_LENGTH, "dsb", |
| 551 | 0, auth_session->handle, |
| 552 | auth_nonce_odd_offset, auth_session->nonce_odd, |
| 553 | DIGEST_LENGTH, |
| 554 | auth_continue_offset, 1)) |
| 555 | return TPM_LIB_ERROR; |
| 556 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "ss", |
| 557 | DIGEST_LENGTH, |
| 558 | auth_session->nonce_even, |
| 559 | DIGEST_LENGTH, |
| 560 | 2 * DIGEST_LENGTH, |
| 561 | request_auth + auth_nonce_odd_offset, |
| 562 | DIGEST_LENGTH + 1)) |
| 563 | return TPM_LIB_ERROR; |
| 564 | sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data), |
| 565 | request_auth + auth_auth_offset); |
| 566 | |
| 567 | return TPM_SUCCESS; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Verify an authentication block in a response. |
| 572 | * Since this func updates the nonce_even in the session data it has to be |
| 573 | * called when receiving a succesfull AUTH response. |
| 574 | * This func can verify the first as well as the second auth block (for |
| 575 | * double authorized commands). |
| 576 | * |
| 577 | * @param command_code command code of the request |
| 578 | * @param response pointer to the request (w/ uninitialised auth data) |
| 579 | * @param handles_len length of the handles area in response |
| 580 | * @param auth_session pointer to the (valid) auth session to be used |
| 581 | * @param response_auth pointer to the auth block of the response to be verified |
| 582 | * @param auth authentication data (HMAC key) |
| 583 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 584 | static u32 verify_response_auth(u32 command_code, const void *response, |
| 585 | size_t response_len0, size_t handles_len, |
| 586 | struct session_data *auth_session, |
| 587 | const void *response_auth, const void *auth) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 588 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 589 | u8 hmac_data[DIGEST_LENGTH * 3 + 1]; |
| 590 | u8 computed_auth[DIGEST_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 591 | sha1_context hash_ctx; |
| 592 | const size_t return_code_offset = 6; |
| 593 | const size_t auth_continue_offset = 20; |
| 594 | const size_t auth_auth_offset = 21; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 595 | u8 auth_continue; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 596 | |
| 597 | if (!auth_session || !auth_session->valid) |
| 598 | return TPM_AUTHFAIL; |
| 599 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "d", |
| 600 | 0, command_code)) |
| 601 | return TPM_LIB_ERROR; |
| 602 | if (response_len0 < TPM_RESPONSE_HEADER_LENGTH) |
| 603 | return TPM_LIB_ERROR; |
| 604 | |
| 605 | sha1_starts(&hash_ctx); |
| 606 | sha1_update(&hash_ctx, response + return_code_offset, 4); |
| 607 | sha1_update(&hash_ctx, hmac_data, 4); |
| 608 | if (response_len0 > TPM_RESPONSE_HEADER_LENGTH + handles_len) |
| 609 | sha1_update(&hash_ctx, |
| 610 | response + TPM_RESPONSE_HEADER_LENGTH + handles_len, |
| 611 | response_len0 - TPM_RESPONSE_HEADER_LENGTH |
| 612 | - handles_len); |
| 613 | sha1_finish(&hash_ctx, hmac_data); |
| 614 | |
| 615 | memcpy(auth_session->nonce_even, response_auth, DIGEST_LENGTH); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 616 | auth_continue = ((u8 *)response_auth)[auth_continue_offset]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 617 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "ssb", |
| 618 | DIGEST_LENGTH, |
| 619 | response_auth, |
| 620 | DIGEST_LENGTH, |
| 621 | 2 * DIGEST_LENGTH, |
| 622 | auth_session->nonce_odd, |
| 623 | DIGEST_LENGTH, |
| 624 | 3 * DIGEST_LENGTH, |
| 625 | auth_continue)) |
| 626 | return TPM_LIB_ERROR; |
| 627 | |
| 628 | sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data), |
| 629 | computed_auth); |
| 630 | |
| 631 | if (memcmp(computed_auth, response_auth + auth_auth_offset, |
| 632 | DIGEST_LENGTH)) |
| 633 | return TPM_AUTHFAIL; |
| 634 | |
| 635 | return TPM_SUCCESS; |
| 636 | } |
| 637 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 638 | u32 tpm1_terminate_auth_session(struct udevice *dev, u32 auth_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 639 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 640 | const u8 command[18] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 641 | 0x00, 0xc1, /* TPM_TAG */ |
| 642 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 643 | 0x00, 0x00, 0x00, 0xba, /* TPM_COMMAND_CODE */ |
| 644 | 0x00, 0x00, 0x00, 0x00, /* TPM_HANDLE */ |
Miquel Raynal | a59aa34 | 2018-05-15 11:57:02 +0200 | [diff] [blame] | 645 | 0x00, 0x00, 0x00, 0x02, /* TPM_RESOURCE_TYPE */ |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 646 | }; |
| 647 | const size_t req_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 648 | u8 request[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 649 | |
| 650 | if (pack_byte_string(request, sizeof(request), "sd", |
| 651 | 0, command, sizeof(command), |
| 652 | req_handle_offset, auth_handle)) |
| 653 | return TPM_LIB_ERROR; |
| 654 | if (oiap_session.valid && oiap_session.handle == auth_handle) |
| 655 | oiap_session.valid = 0; |
| 656 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 657 | return tpm_sendrecv_command(dev, request, NULL, NULL); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 658 | } |
| 659 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 660 | u32 tpm1_end_oiap(struct udevice *dev) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 661 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 662 | u32 err = TPM_SUCCESS; |
Miquel Raynal | 82b0566 | 2018-05-15 11:57:03 +0200 | [diff] [blame] | 663 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 664 | if (oiap_session.valid) |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 665 | err = tpm1_terminate_auth_session(dev, oiap_session.handle); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 666 | return err; |
| 667 | } |
| 668 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 669 | u32 tpm1_oiap(struct udevice *dev, u32 *auth_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 670 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 671 | const u8 command[10] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 672 | 0x00, 0xc1, /* TPM_TAG */ |
| 673 | 0x00, 0x00, 0x00, 0x0a, /* parameter size */ |
| 674 | 0x00, 0x00, 0x00, 0x0a, /* TPM_COMMAND_CODE */ |
| 675 | }; |
| 676 | const size_t res_auth_handle_offset = TPM_RESPONSE_HEADER_LENGTH; |
| 677 | const size_t res_nonce_even_offset = TPM_RESPONSE_HEADER_LENGTH + 4; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 678 | u8 response[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 679 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 680 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 681 | |
| 682 | if (oiap_session.valid) |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 683 | tpm1_terminate_auth_session(dev, oiap_session.handle); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 684 | |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 685 | err = tpm_sendrecv_command(dev, command, response, &response_length); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 686 | if (err) |
| 687 | return err; |
| 688 | if (unpack_byte_string(response, response_length, "ds", |
| 689 | res_auth_handle_offset, &oiap_session.handle, |
| 690 | res_nonce_even_offset, &oiap_session.nonce_even, |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 691 | (u32)DIGEST_LENGTH)) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 692 | return TPM_LIB_ERROR; |
| 693 | oiap_session.valid = 1; |
| 694 | if (auth_handle) |
| 695 | *auth_handle = oiap_session.handle; |
| 696 | return 0; |
| 697 | } |
| 698 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 699 | u32 tpm1_load_key2_oiap(struct udevice *dev, u32 parent_handle, const void *key, |
| 700 | size_t key_length, const void *parent_key_usage_auth, |
| 701 | u32 *key_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 702 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 703 | const u8 command[14] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 704 | 0x00, 0xc2, /* TPM_TAG */ |
| 705 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 706 | 0x00, 0x00, 0x00, 0x41, /* TPM_COMMAND_CODE */ |
| 707 | 0x00, 0x00, 0x00, 0x00, /* parent handle */ |
| 708 | }; |
| 709 | const size_t req_size_offset = 2; |
| 710 | const size_t req_parent_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
| 711 | const size_t req_key_offset = TPM_REQUEST_HEADER_LENGTH + 4; |
| 712 | const size_t res_handle_offset = TPM_RESPONSE_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 713 | u8 request[sizeof(command) + TPM_KEY12_MAX_LENGTH + |
| 714 | TPM_REQUEST_AUTH_LENGTH]; |
| 715 | u8 response[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 716 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 717 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 718 | |
| 719 | if (!oiap_session.valid) { |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 720 | err = tpm1_oiap(dev, NULL); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 721 | if (err) |
| 722 | return err; |
| 723 | } |
| 724 | if (pack_byte_string(request, sizeof(request), "sdds", |
| 725 | 0, command, sizeof(command), |
| 726 | req_size_offset, |
| 727 | sizeof(command) + key_length |
| 728 | + TPM_REQUEST_AUTH_LENGTH, |
| 729 | req_parent_handle_offset, parent_handle, |
| 730 | req_key_offset, key, key_length |
| 731 | )) |
| 732 | return TPM_LIB_ERROR; |
| 733 | |
| 734 | err = create_request_auth(request, sizeof(command) + key_length, 4, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 735 | &oiap_session, |
| 736 | request + sizeof(command) + key_length, |
| 737 | parent_key_usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 738 | if (err) |
| 739 | return err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 740 | err = tpm_sendrecv_command(dev, request, response, &response_length); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 741 | if (err) { |
| 742 | if (err == TPM_AUTHFAIL) |
| 743 | oiap_session.valid = 0; |
| 744 | return err; |
| 745 | } |
| 746 | |
| 747 | err = verify_response_auth(0x00000041, response, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 748 | response_length - TPM_RESPONSE_AUTH_LENGTH, |
| 749 | 4, &oiap_session, |
| 750 | response + response_length - |
| 751 | TPM_RESPONSE_AUTH_LENGTH, |
| 752 | parent_key_usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 753 | if (err) |
| 754 | return err; |
| 755 | |
| 756 | if (key_handle) { |
| 757 | if (unpack_byte_string(response, response_length, "d", |
| 758 | res_handle_offset, key_handle)) |
| 759 | return TPM_LIB_ERROR; |
| 760 | } |
| 761 | |
| 762 | return 0; |
| 763 | } |
| 764 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 765 | u32 tpm1_get_pub_key_oiap(struct udevice *dev, u32 key_handle, |
| 766 | const void *usage_auth, void *pubkey, |
| 767 | size_t *pubkey_len) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 768 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 769 | const u8 command[14] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 770 | 0x00, 0xc2, /* TPM_TAG */ |
| 771 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 772 | 0x00, 0x00, 0x00, 0x21, /* TPM_COMMAND_CODE */ |
| 773 | 0x00, 0x00, 0x00, 0x00, /* key handle */ |
| 774 | }; |
| 775 | const size_t req_size_offset = 2; |
| 776 | const size_t req_key_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
| 777 | const size_t res_pubkey_offset = TPM_RESPONSE_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 778 | u8 request[sizeof(command) + TPM_REQUEST_AUTH_LENGTH]; |
| 779 | u8 response[TPM_RESPONSE_HEADER_LENGTH + TPM_PUBKEY_MAX_LENGTH + |
| 780 | TPM_RESPONSE_AUTH_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 781 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 782 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 783 | |
| 784 | if (!oiap_session.valid) { |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 785 | err = tpm1_oiap(dev, NULL); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 786 | if (err) |
| 787 | return err; |
| 788 | } |
| 789 | if (pack_byte_string(request, sizeof(request), "sdd", |
| 790 | 0, command, sizeof(command), |
| 791 | req_size_offset, |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 792 | (u32)(sizeof(command) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 793 | + TPM_REQUEST_AUTH_LENGTH), |
| 794 | req_key_handle_offset, key_handle |
| 795 | )) |
| 796 | return TPM_LIB_ERROR; |
| 797 | err = create_request_auth(request, sizeof(command), 4, &oiap_session, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 798 | request + sizeof(command), usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 799 | if (err) |
| 800 | return err; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 801 | err = tpm_sendrecv_command(dev, request, response, &response_length); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 802 | if (err) { |
| 803 | if (err == TPM_AUTHFAIL) |
| 804 | oiap_session.valid = 0; |
| 805 | return err; |
| 806 | } |
| 807 | err = verify_response_auth(0x00000021, response, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 808 | response_length - TPM_RESPONSE_AUTH_LENGTH, |
| 809 | 0, &oiap_session, |
| 810 | response + response_length - |
| 811 | TPM_RESPONSE_AUTH_LENGTH, |
| 812 | usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 813 | if (err) |
| 814 | return err; |
| 815 | |
| 816 | if (pubkey) { |
| 817 | if ((response_length - TPM_RESPONSE_HEADER_LENGTH |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 818 | - TPM_RESPONSE_AUTH_LENGTH) > *pubkey_len) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 819 | return TPM_LIB_ERROR; |
| 820 | *pubkey_len = response_length - TPM_RESPONSE_HEADER_LENGTH |
| 821 | - TPM_RESPONSE_AUTH_LENGTH; |
| 822 | memcpy(pubkey, response + res_pubkey_offset, |
| 823 | response_length - TPM_RESPONSE_HEADER_LENGTH |
| 824 | - TPM_RESPONSE_AUTH_LENGTH); |
| 825 | } |
| 826 | |
| 827 | return 0; |
| 828 | } |
| 829 | |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 830 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 831 | u32 tpm1_find_key_sha1(struct udevice *dev, const u8 auth[20], |
| 832 | const u8 pubkey_digest[20], u32 *handle) |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 833 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 834 | u16 key_count; |
| 835 | u32 key_handles[10]; |
| 836 | u8 buf[288]; |
| 837 | u8 *ptr; |
| 838 | u32 err; |
| 839 | u8 digest[20]; |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 840 | size_t buf_len; |
| 841 | unsigned int i; |
| 842 | |
| 843 | /* fetch list of already loaded keys in the TPM */ |
Mathew McBride | 10f24eb | 2021-11-11 04:06:27 +0000 | [diff] [blame] | 844 | err = tpm1_get_capability(dev, TPM_CAP_HANDLE, TPM_RT_KEY, buf, |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 845 | sizeof(buf)); |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 846 | if (err) |
| 847 | return -1; |
| 848 | key_count = get_unaligned_be16(buf); |
| 849 | ptr = buf + 2; |
| 850 | for (i = 0; i < key_count; ++i, ptr += 4) |
| 851 | key_handles[i] = get_unaligned_be32(ptr); |
| 852 | |
| 853 | /* now search a(/ the) key which we can access with the given auth */ |
| 854 | for (i = 0; i < key_count; ++i) { |
| 855 | buf_len = sizeof(buf); |
Mathew McBride | 10f24eb | 2021-11-11 04:06:27 +0000 | [diff] [blame] | 856 | err = tpm1_get_pub_key_oiap(dev, key_handles[i], auth, buf, &buf_len); |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 857 | if (err && err != TPM_AUTHFAIL) |
| 858 | return -1; |
| 859 | if (err) |
| 860 | continue; |
| 861 | sha1_csum(buf, buf_len, digest); |
| 862 | if (!memcmp(digest, pubkey_digest, 20)) { |
| 863 | *handle = key_handles[i]; |
| 864 | return 0; |
| 865 | } |
| 866 | } |
| 867 | return 1; |
| 868 | } |
| 869 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
| 870 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 871 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 872 | |
Simon Glass | 3b8692a | 2021-02-06 14:23:36 -0700 | [diff] [blame] | 873 | u32 tpm1_get_random(struct udevice *dev, void *data, u32 count) |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 874 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 875 | const u8 command[14] = { |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 876 | 0x0, 0xc1, /* TPM_TAG */ |
| 877 | 0x0, 0x0, 0x0, 0xe, /* parameter size */ |
| 878 | 0x0, 0x0, 0x0, 0x46, /* TPM_COMMAND_CODE */ |
| 879 | }; |
| 880 | const size_t length_offset = 10; |
| 881 | const size_t data_size_offset = 10; |
| 882 | const size_t data_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 883 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 884 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 885 | u32 data_size; |
| 886 | u8 *out = data; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 887 | |
| 888 | while (count > 0) { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 889 | u32 this_bytes = min((size_t)count, |
| 890 | sizeof(response) - data_offset); |
| 891 | u32 err; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 892 | |
| 893 | if (pack_byte_string(buf, sizeof(buf), "sd", |
| 894 | 0, command, sizeof(command), |
| 895 | length_offset, this_bytes)) |
| 896 | return TPM_LIB_ERROR; |
Simon Glass | 8ceca1d | 2018-11-18 14:22:27 -0700 | [diff] [blame] | 897 | err = tpm_sendrecv_command(dev, buf, response, |
| 898 | &response_length); |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 899 | if (err) |
| 900 | return err; |
| 901 | if (unpack_byte_string(response, response_length, "d", |
| 902 | data_size_offset, &data_size)) |
| 903 | return TPM_LIB_ERROR; |
| 904 | if (data_size > count) |
| 905 | return TPM_LIB_ERROR; |
| 906 | if (unpack_byte_string(response, response_length, "s", |
| 907 | data_offset, out, data_size)) |
| 908 | return TPM_LIB_ERROR; |
| 909 | |
| 910 | count -= data_size; |
| 911 | out += data_size; |
| 912 | } |
| 913 | |
| 914 | return 0; |
| 915 | } |