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 | |
| 7 | #include <common.h> |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 8 | #include <dm.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 9 | #include <tpm.h> |
| 10 | #include <asm/unaligned.h> |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 11 | #include <u-boot/sha1.h> |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 12 | |
| 13 | /* Internal error of TPM command library */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 14 | #define TPM_LIB_ERROR ((u32)~0u) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 15 | |
| 16 | /* Useful constants */ |
| 17 | enum { |
| 18 | COMMAND_BUFFER_SIZE = 256, |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 19 | TPM_REQUEST_HEADER_LENGTH = 10, |
| 20 | TPM_RESPONSE_HEADER_LENGTH = 10, |
| 21 | PCR_DIGEST_LENGTH = 20, |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 22 | DIGEST_LENGTH = 20, |
| 23 | TPM_REQUEST_AUTH_LENGTH = 45, |
| 24 | TPM_RESPONSE_AUTH_LENGTH = 41, |
| 25 | /* some max lengths, valid for RSA keys <= 2048 bits */ |
| 26 | TPM_KEY12_MAX_LENGTH = 618, |
| 27 | TPM_PUBKEY_MAX_LENGTH = 288, |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 30 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 31 | |
| 32 | #ifndef CONFIG_SHA1 |
| 33 | #error "TPM_AUTH_SESSIONS require SHA1 to be configured, too" |
| 34 | #endif /* !CONFIG_SHA1 */ |
| 35 | |
| 36 | struct session_data { |
| 37 | int valid; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 38 | u32 handle; |
| 39 | u8 nonce_even[DIGEST_LENGTH]; |
| 40 | u8 nonce_odd[DIGEST_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static struct session_data oiap_session = {0, }; |
| 44 | |
| 45 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
| 46 | |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 47 | /** |
| 48 | * Pack data into a byte string. The data types are specified in |
| 49 | * the format string: 'b' means unsigned byte, 'w' unsigned word, |
| 50 | * 'd' unsigned double word, and 's' byte string. The data are a |
| 51 | * series of offsets and values (for type byte string there are also |
| 52 | * lengths). The data values are packed into the byte string |
| 53 | * sequentially, and so a latter value could over-write a former |
| 54 | * value. |
| 55 | * |
| 56 | * @param str output string |
| 57 | * @param size size of output string |
| 58 | * @param format format string |
| 59 | * @param ... data points |
| 60 | * @return 0 on success, non-0 on error |
| 61 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 62 | int pack_byte_string(u8 *str, size_t size, const char *format, ...) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 63 | { |
| 64 | va_list args; |
| 65 | size_t offset = 0, length = 0; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 66 | u8 *data = NULL; |
| 67 | u32 value = 0; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 68 | |
| 69 | va_start(args, format); |
| 70 | for (; *format; format++) { |
| 71 | switch (*format) { |
| 72 | case 'b': |
| 73 | offset = va_arg(args, size_t); |
| 74 | value = va_arg(args, int); |
| 75 | length = 1; |
| 76 | break; |
| 77 | case 'w': |
| 78 | offset = va_arg(args, size_t); |
| 79 | value = va_arg(args, int); |
| 80 | length = 2; |
| 81 | break; |
| 82 | case 'd': |
| 83 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 84 | value = va_arg(args, u32); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 85 | length = 4; |
| 86 | break; |
| 87 | case 's': |
| 88 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 89 | data = va_arg(args, u8 *); |
| 90 | length = va_arg(args, u32); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 91 | break; |
| 92 | default: |
| 93 | debug("Couldn't recognize format string\n"); |
André Draszik | 3e490f6 | 2017-10-03 16:55:54 +0100 | [diff] [blame] | 94 | va_end(args); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 95 | return -1; |
| 96 | } |
| 97 | |
xypron.glpk@gmx.de | b7af157 | 2017-07-30 21:40:37 +0200 | [diff] [blame] | 98 | if (offset + length > size) { |
| 99 | va_end(args); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 100 | return -1; |
xypron.glpk@gmx.de | b7af157 | 2017-07-30 21:40:37 +0200 | [diff] [blame] | 101 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 102 | |
| 103 | switch (*format) { |
| 104 | case 'b': |
| 105 | str[offset] = value; |
| 106 | break; |
| 107 | case 'w': |
| 108 | put_unaligned_be16(value, str + offset); |
| 109 | break; |
| 110 | case 'd': |
| 111 | put_unaligned_be32(value, str + offset); |
| 112 | break; |
| 113 | case 's': |
| 114 | memcpy(str + offset, data, length); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | va_end(args); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Unpack data from a byte string. The data types are specified in |
| 125 | * the format string: 'b' means unsigned byte, 'w' unsigned word, |
| 126 | * 'd' unsigned double word, and 's' byte string. The data are a |
| 127 | * series of offsets and pointers (for type byte string there are also |
| 128 | * lengths). |
| 129 | * |
| 130 | * @param str output string |
| 131 | * @param size size of output string |
| 132 | * @param format format string |
| 133 | * @param ... data points |
| 134 | * @return 0 on success, non-0 on error |
| 135 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 136 | int unpack_byte_string(const u8 *str, size_t size, const char *format, ...) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 137 | { |
| 138 | va_list args; |
| 139 | size_t offset = 0, length = 0; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 140 | u8 *ptr8 = NULL; |
| 141 | u16 *ptr16 = NULL; |
| 142 | u32 *ptr32 = NULL; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 143 | |
| 144 | va_start(args, format); |
| 145 | for (; *format; format++) { |
| 146 | switch (*format) { |
| 147 | case 'b': |
| 148 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 149 | ptr8 = va_arg(args, u8 *); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 150 | length = 1; |
| 151 | break; |
| 152 | case 'w': |
| 153 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 154 | ptr16 = va_arg(args, u16 *); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 155 | length = 2; |
| 156 | break; |
| 157 | case 'd': |
| 158 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 159 | ptr32 = va_arg(args, u32 *); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 160 | length = 4; |
| 161 | break; |
| 162 | case 's': |
| 163 | offset = va_arg(args, size_t); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 164 | ptr8 = va_arg(args, u8 *); |
| 165 | length = va_arg(args, u32); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 166 | break; |
| 167 | default: |
xypron.glpk@gmx.de | b7af157 | 2017-07-30 21:40:37 +0200 | [diff] [blame] | 168 | va_end(args); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 169 | debug("Couldn't recognize format string\n"); |
| 170 | return -1; |
| 171 | } |
| 172 | |
André Draszik | 3e490f6 | 2017-10-03 16:55:54 +0100 | [diff] [blame] | 173 | if (offset + length > size) { |
| 174 | va_end(args); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 175 | return -1; |
André Draszik | 3e490f6 | 2017-10-03 16:55:54 +0100 | [diff] [blame] | 176 | } |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 177 | |
| 178 | switch (*format) { |
| 179 | case 'b': |
| 180 | *ptr8 = str[offset]; |
| 181 | break; |
| 182 | case 'w': |
| 183 | *ptr16 = get_unaligned_be16(str + offset); |
| 184 | break; |
| 185 | case 'd': |
| 186 | *ptr32 = get_unaligned_be32(str + offset); |
| 187 | break; |
| 188 | case 's': |
| 189 | memcpy(ptr8, str + offset, length); |
| 190 | break; |
| 191 | } |
| 192 | } |
| 193 | va_end(args); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get TPM command size. |
| 200 | * |
| 201 | * @param command byte string of TPM command |
| 202 | * @return command size of the TPM command |
| 203 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 204 | static u32 tpm_command_size(const void *command) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 205 | { |
| 206 | const size_t command_size_offset = 2; |
| 207 | return get_unaligned_be32(command + command_size_offset); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Get TPM response return code, which is one of TPM_RESULT values. |
| 212 | * |
| 213 | * @param response byte string of TPM response |
| 214 | * @return return code of the TPM response |
| 215 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 216 | static u32 tpm_return_code(const void *response) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 217 | { |
| 218 | const size_t return_code_offset = 6; |
| 219 | return get_unaligned_be32(response + return_code_offset); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Send a TPM command and return response's return code, and optionally |
| 224 | * return response to caller. |
| 225 | * |
| 226 | * @param command byte string of TPM command |
| 227 | * @param response output buffer for TPM response, or NULL if the |
| 228 | * caller does not care about it |
| 229 | * @param size_ptr output buffer size (input parameter) and TPM |
| 230 | * response length (output parameter); this parameter |
| 231 | * is a bidirectional |
| 232 | * @return return code of the TPM response |
| 233 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 234 | static u32 tpm_sendrecv_command(const void *command, void *response, |
| 235 | size_t *size_ptr) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 236 | { |
Christophe Ricard | 52d309b | 2015-10-06 22:54:43 +0200 | [diff] [blame] | 237 | struct udevice *dev; |
Tom Rini | 6b2d40c | 2017-05-10 15:20:18 -0400 | [diff] [blame] | 238 | int err, ret; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 239 | u8 response_buffer[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 240 | size_t response_length; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 241 | |
| 242 | if (response) { |
| 243 | response_length = *size_ptr; |
| 244 | } else { |
| 245 | response = response_buffer; |
| 246 | response_length = sizeof(response_buffer); |
| 247 | } |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 248 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 249 | ret = uclass_first_device_err(UCLASS_TPM, &dev); |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 250 | if (ret) |
| 251 | return ret; |
| 252 | err = tpm_xfer(dev, command, tpm_command_size(command), |
| 253 | response, &response_length); |
Christophe Ricard | 52d309b | 2015-10-06 22:54:43 +0200 | [diff] [blame] | 254 | |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 255 | if (err < 0) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 256 | return TPM_LIB_ERROR; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 257 | if (size_ptr) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 258 | *size_ptr = response_length; |
| 259 | |
| 260 | return tpm_return_code(response); |
| 261 | } |
| 262 | |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 263 | int tpm_init(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 264 | { |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 265 | int err; |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 266 | struct udevice *dev; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 267 | |
Simon Glass | c7298e7 | 2016-02-11 13:23:26 -0700 | [diff] [blame] | 268 | err = uclass_first_device_err(UCLASS_TPM, &dev); |
| 269 | if (err) |
Simon Glass | 3e4f2fd | 2015-08-22 18:31:32 -0600 | [diff] [blame] | 270 | return err; |
| 271 | return tpm_open(dev); |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 274 | u32 tpm_startup(enum tpm_startup_type mode) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 275 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 276 | const u8 command[12] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 277 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x0, 0x0, 0x0, 0x99, 0x0, 0x0, |
| 278 | }; |
| 279 | const size_t mode_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 280 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 281 | |
| 282 | if (pack_byte_string(buf, sizeof(buf), "sw", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 283 | 0, command, sizeof(command), |
| 284 | mode_offset, mode)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 285 | return TPM_LIB_ERROR; |
| 286 | |
| 287 | return tpm_sendrecv_command(buf, NULL, NULL); |
| 288 | } |
| 289 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 290 | u32 tpm_self_test_full(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 291 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 292 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 293 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x50, |
| 294 | }; |
| 295 | return tpm_sendrecv_command(command, NULL, NULL); |
| 296 | } |
| 297 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 298 | u32 tpm_continue_self_test(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 299 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 300 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 301 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x53, |
| 302 | }; |
| 303 | return tpm_sendrecv_command(command, NULL, NULL); |
| 304 | } |
| 305 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 306 | u32 tpm_nv_define_space(u32 index, u32 perm, u32 size) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 307 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 308 | const u8 command[101] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 309 | 0x0, 0xc1, /* TPM_TAG */ |
| 310 | 0x0, 0x0, 0x0, 0x65, /* parameter size */ |
| 311 | 0x0, 0x0, 0x0, 0xcc, /* TPM_COMMAND_CODE */ |
| 312 | /* TPM_NV_DATA_PUBLIC->... */ |
| 313 | 0x0, 0x18, /* ...->TPM_STRUCTURE_TAG */ |
| 314 | 0, 0, 0, 0, /* ...->TPM_NV_INDEX */ |
| 315 | /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ |
| 316 | 0x0, 0x3, |
| 317 | 0, 0, 0, |
| 318 | 0x1f, |
| 319 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 320 | /* TPM_NV_DATA_PUBLIC->TPM_PCR_INFO_SHORT */ |
| 321 | 0x0, 0x3, |
| 322 | 0, 0, 0, |
| 323 | 0x1f, |
| 324 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 325 | /* TPM_NV_ATTRIBUTES->... */ |
| 326 | 0x0, 0x17, /* ...->TPM_STRUCTURE_TAG */ |
| 327 | 0, 0, 0, 0, /* ...->attributes */ |
| 328 | /* End of TPM_NV_ATTRIBUTES */ |
| 329 | 0, /* bReadSTClear */ |
| 330 | 0, /* bWriteSTClear */ |
| 331 | 0, /* bWriteDefine */ |
| 332 | 0, 0, 0, 0, /* size */ |
| 333 | }; |
| 334 | const size_t index_offset = 12; |
| 335 | const size_t perm_offset = 70; |
| 336 | const size_t size_offset = 77; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 337 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 338 | |
| 339 | if (pack_byte_string(buf, sizeof(buf), "sddd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 340 | 0, command, sizeof(command), |
| 341 | index_offset, index, |
| 342 | perm_offset, perm, |
| 343 | size_offset, size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 344 | return TPM_LIB_ERROR; |
| 345 | |
| 346 | return tpm_sendrecv_command(buf, NULL, NULL); |
| 347 | } |
| 348 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 349 | u32 tpm_nv_read_value(u32 index, void *data, u32 count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 350 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 351 | const u8 command[22] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 352 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x16, 0x0, 0x0, 0x0, 0xcf, |
| 353 | }; |
| 354 | const size_t index_offset = 10; |
| 355 | const size_t length_offset = 18; |
| 356 | const size_t data_size_offset = 10; |
| 357 | const size_t data_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 358 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 359 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 360 | u32 data_size; |
| 361 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 362 | |
| 363 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 364 | 0, command, sizeof(command), |
| 365 | index_offset, index, |
| 366 | length_offset, count)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 367 | return TPM_LIB_ERROR; |
| 368 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 369 | if (err) |
| 370 | return err; |
| 371 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 372 | data_size_offset, &data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 373 | return TPM_LIB_ERROR; |
| 374 | if (data_size > count) |
| 375 | return TPM_LIB_ERROR; |
| 376 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 377 | data_offset, data, data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 378 | return TPM_LIB_ERROR; |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 383 | u32 tpm_nv_write_value(u32 index, const void *data, u32 length) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 384 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 385 | const u8 command[256] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 386 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xcd, |
| 387 | }; |
| 388 | const size_t command_size_offset = 2; |
| 389 | const size_t index_offset = 10; |
| 390 | const size_t length_offset = 18; |
| 391 | const size_t data_offset = 22; |
| 392 | const size_t write_info_size = 12; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 393 | const u32 total_length = |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 394 | TPM_REQUEST_HEADER_LENGTH + write_info_size + length; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 395 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 396 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 397 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 398 | |
| 399 | if (pack_byte_string(buf, sizeof(buf), "sddds", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 400 | 0, command, sizeof(command), |
| 401 | command_size_offset, total_length, |
| 402 | index_offset, index, |
| 403 | length_offset, length, |
| 404 | data_offset, data, length)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 405 | return TPM_LIB_ERROR; |
| 406 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 407 | if (err) |
| 408 | return err; |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 413 | u32 tpm_extend(u32 index, const void *in_digest, void *out_digest) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 414 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 415 | const u8 command[34] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 416 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x22, 0x0, 0x0, 0x0, 0x14, |
| 417 | }; |
| 418 | const size_t index_offset = 10; |
| 419 | const size_t in_digest_offset = 14; |
| 420 | const size_t out_digest_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 421 | u8 buf[COMMAND_BUFFER_SIZE]; |
| 422 | u8 response[TPM_RESPONSE_HEADER_LENGTH + PCR_DIGEST_LENGTH]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 423 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 424 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 425 | |
| 426 | if (pack_byte_string(buf, sizeof(buf), "sds", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 427 | 0, command, sizeof(command), |
| 428 | index_offset, index, |
| 429 | in_digest_offset, in_digest, |
| 430 | PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 431 | return TPM_LIB_ERROR; |
| 432 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 433 | if (err) |
| 434 | return err; |
| 435 | |
| 436 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 437 | out_digest_offset, out_digest, |
| 438 | PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 439 | return TPM_LIB_ERROR; |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 444 | u32 tpm_pcr_read(u32 index, void *data, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 445 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 446 | const u8 command[14] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 447 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xe, 0x0, 0x0, 0x0, 0x15, |
| 448 | }; |
| 449 | const size_t index_offset = 10; |
| 450 | const size_t out_digest_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 451 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 452 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 453 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 454 | |
| 455 | if (count < PCR_DIGEST_LENGTH) |
| 456 | return TPM_LIB_ERROR; |
| 457 | |
| 458 | if (pack_byte_string(buf, sizeof(buf), "sd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 459 | 0, command, sizeof(command), |
| 460 | index_offset, index)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 461 | return TPM_LIB_ERROR; |
| 462 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 463 | if (err) |
| 464 | return err; |
| 465 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 466 | out_digest_offset, data, PCR_DIGEST_LENGTH)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 467 | return TPM_LIB_ERROR; |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 472 | u32 tpm_tsc_physical_presence(u16 presence) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 473 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 474 | const u8 command[12] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 475 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xc, 0x40, 0x0, 0x0, 0xa, 0x0, 0x0, |
| 476 | }; |
| 477 | const size_t presence_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 478 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 479 | |
| 480 | if (pack_byte_string(buf, sizeof(buf), "sw", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 481 | 0, command, sizeof(command), |
| 482 | presence_offset, presence)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 483 | return TPM_LIB_ERROR; |
| 484 | |
| 485 | return tpm_sendrecv_command(buf, NULL, NULL); |
| 486 | } |
| 487 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 488 | u32 tpm_read_pubek(void *data, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 489 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 490 | const u8 command[30] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 491 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0x1e, 0x0, 0x0, 0x0, 0x7c, |
| 492 | }; |
| 493 | const size_t response_size_offset = 2; |
| 494 | const size_t data_offset = 10; |
| 495 | const size_t header_and_checksum_size = TPM_RESPONSE_HEADER_LENGTH + 20; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 496 | u8 response[COMMAND_BUFFER_SIZE + TPM_PUBEK_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 497 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 498 | u32 data_size; |
| 499 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 500 | |
| 501 | err = tpm_sendrecv_command(command, response, &response_length); |
| 502 | if (err) |
| 503 | return err; |
| 504 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 505 | response_size_offset, &data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 506 | return TPM_LIB_ERROR; |
| 507 | if (data_size < header_and_checksum_size) |
| 508 | return TPM_LIB_ERROR; |
| 509 | data_size -= header_and_checksum_size; |
| 510 | if (data_size > count) |
| 511 | return TPM_LIB_ERROR; |
| 512 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 513 | data_offset, data, data_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 514 | return TPM_LIB_ERROR; |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 519 | u32 tpm_force_clear(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 520 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 521 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 522 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x5d, |
| 523 | }; |
| 524 | |
| 525 | return tpm_sendrecv_command(command, NULL, NULL); |
| 526 | } |
| 527 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 528 | u32 tpm_physical_enable(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 529 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 530 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 531 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x6f, |
| 532 | }; |
| 533 | |
| 534 | return tpm_sendrecv_command(command, NULL, NULL); |
| 535 | } |
| 536 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 537 | u32 tpm_physical_disable(void) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 538 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 539 | const u8 command[10] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 540 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xa, 0x0, 0x0, 0x0, 0x70, |
| 541 | }; |
| 542 | |
| 543 | return tpm_sendrecv_command(command, NULL, NULL); |
| 544 | } |
| 545 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 546 | u32 tpm_physical_set_deactivated(u8 state) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 547 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 548 | const u8 command[11] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 549 | 0x0, 0xc1, 0x0, 0x0, 0x0, 0xb, 0x0, 0x0, 0x0, 0x72, |
| 550 | }; |
| 551 | const size_t state_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 552 | u8 buf[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 553 | |
| 554 | if (pack_byte_string(buf, sizeof(buf), "sb", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 555 | 0, command, sizeof(command), |
| 556 | state_offset, state)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 557 | return TPM_LIB_ERROR; |
| 558 | |
| 559 | return tpm_sendrecv_command(buf, NULL, NULL); |
| 560 | } |
| 561 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 562 | u32 tpm_get_capability(u32 cap_area, u32 sub_cap, void *cap, size_t count) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 563 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 564 | const u8 command[22] = { |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 565 | 0x0, 0xc1, /* TPM_TAG */ |
| 566 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 567 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 568 | 0x0, 0x0, 0x0, 0x0, /* TPM_CAPABILITY_AREA */ |
| 569 | 0x0, 0x0, 0x0, 0x4, /* subcap size */ |
| 570 | 0x0, 0x0, 0x0, 0x0, /* subcap value */ |
| 571 | }; |
| 572 | const size_t cap_area_offset = 10; |
| 573 | const size_t sub_cap_offset = 18; |
| 574 | const size_t cap_offset = 14; |
| 575 | const size_t cap_size_offset = 10; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 576 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 577 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 578 | u32 cap_size; |
| 579 | u32 err; |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 580 | |
| 581 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 582 | 0, command, sizeof(command), |
| 583 | cap_area_offset, cap_area, |
| 584 | sub_cap_offset, sub_cap)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 585 | return TPM_LIB_ERROR; |
| 586 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 587 | if (err) |
| 588 | return err; |
| 589 | if (unpack_byte_string(response, response_length, "d", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 590 | cap_size_offset, &cap_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 591 | return TPM_LIB_ERROR; |
| 592 | if (cap_size > response_length || cap_size > count) |
| 593 | return TPM_LIB_ERROR; |
| 594 | if (unpack_byte_string(response, response_length, "s", |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 595 | cap_offset, cap, cap_size)) |
Che-liang Chiou | c18f901 | 2013-02-28 09:34:57 +0000 | [diff] [blame] | 596 | return TPM_LIB_ERROR; |
| 597 | |
| 598 | return 0; |
| 599 | } |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 600 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 601 | u32 tpm_get_permanent_flags(struct tpm_permanent_flags *pflags) |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 602 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 603 | const u8 command[22] = { |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 604 | 0x0, 0xc1, /* TPM_TAG */ |
| 605 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 606 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 607 | 0x0, 0x0, 0x0, 0x4, /* TPM_CAP_FLAG_PERM */ |
| 608 | 0x0, 0x0, 0x0, 0x4, /* subcap size */ |
| 609 | 0x0, 0x0, 0x1, 0x8, /* subcap value */ |
| 610 | }; |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 611 | const size_t data_size_offset = TPM_HEADER_SIZE; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 612 | const size_t data_offset = TPM_HEADER_SIZE + sizeof(u32); |
| 613 | u8 response[COMMAND_BUFFER_SIZE]; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 614 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 615 | u32 err; |
| 616 | u32 data_size; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 617 | |
| 618 | err = tpm_sendrecv_command(command, response, &response_length); |
| 619 | if (err) |
| 620 | return err; |
André Draszik | 1361fbc | 2017-10-03 16:55:51 +0100 | [diff] [blame] | 621 | if (unpack_byte_string(response, response_length, "d", |
| 622 | data_size_offset, &data_size)) |
| 623 | return TPM_LIB_ERROR; |
| 624 | if (data_size < sizeof(*pflags)) |
| 625 | return TPM_LIB_ERROR; |
| 626 | if (unpack_byte_string(response, response_length, "s", |
| 627 | data_offset, pflags, sizeof(*pflags))) |
| 628 | return TPM_LIB_ERROR; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 629 | |
| 630 | return 0; |
| 631 | } |
| 632 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 633 | u32 tpm_get_permissions(u32 index, u32 *perm) |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 634 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 635 | const u8 command[22] = { |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 636 | 0x0, 0xc1, /* TPM_TAG */ |
| 637 | 0x0, 0x0, 0x0, 0x16, /* parameter size */ |
| 638 | 0x0, 0x0, 0x0, 0x65, /* TPM_COMMAND_CODE */ |
| 639 | 0x0, 0x0, 0x0, 0x11, |
| 640 | 0x0, 0x0, 0x0, 0x4, |
| 641 | }; |
| 642 | const size_t index_offset = 18; |
| 643 | const size_t perm_offset = 60; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 644 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 645 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 646 | u32 err; |
Simon Glass | ff9f04a | 2015-08-22 18:31:41 -0600 | [diff] [blame] | 647 | |
| 648 | if (pack_byte_string(buf, sizeof(buf), "d", 0, command, sizeof(command), |
| 649 | index_offset, index)) |
| 650 | return TPM_LIB_ERROR; |
| 651 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 652 | if (err) |
| 653 | return err; |
| 654 | if (unpack_byte_string(response, response_length, "d", |
| 655 | perm_offset, perm)) |
| 656 | return TPM_LIB_ERROR; |
| 657 | |
| 658 | return 0; |
| 659 | } |
| 660 | |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 661 | #ifdef CONFIG_TPM_FLUSH_RESOURCES |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 662 | u32 tpm_flush_specific(u32 key_handle, u32 resource_type) |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 663 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 664 | const u8 command[18] = { |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 665 | 0x00, 0xc1, /* TPM_TAG */ |
| 666 | 0x00, 0x00, 0x00, 0x12, /* parameter size */ |
| 667 | 0x00, 0x00, 0x00, 0xba, /* TPM_COMMAND_CODE */ |
| 668 | 0x00, 0x00, 0x00, 0x00, /* key handle */ |
| 669 | 0x00, 0x00, 0x00, 0x00, /* resource type */ |
| 670 | }; |
| 671 | const size_t key_handle_offset = 10; |
| 672 | const size_t resource_type_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 673 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 674 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 675 | u32 err; |
Mario Six | 4eceb6c | 2017-01-11 16:00:50 +0100 | [diff] [blame] | 676 | |
| 677 | if (pack_byte_string(buf, sizeof(buf), "sdd", |
| 678 | 0, command, sizeof(command), |
| 679 | key_handle_offset, key_handle, |
| 680 | resource_type_offset, resource_type)) |
| 681 | return TPM_LIB_ERROR; |
| 682 | |
| 683 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 684 | if (err) |
| 685 | return err; |
| 686 | return 0; |
| 687 | } |
| 688 | #endif /* CONFIG_TPM_FLUSH_RESOURCES */ |
| 689 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 690 | #ifdef CONFIG_TPM_AUTH_SESSIONS |
| 691 | |
| 692 | /** |
| 693 | * Fill an authentication block in a request. |
| 694 | * This func can create the first as well as the second auth block (for |
| 695 | * double authorized commands). |
| 696 | * |
| 697 | * @param request pointer to the request (w/ uninitialised auth data) |
| 698 | * @param request_len0 length of the request without auth data |
| 699 | * @param handles_len length of the handles area in request |
| 700 | * @param auth_session pointer to the (valid) auth session to be used |
| 701 | * @param request_auth pointer to the auth block of the request to be filled |
| 702 | * @param auth authentication data (HMAC key) |
| 703 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 704 | static u32 create_request_auth(const void *request, size_t request_len0, |
| 705 | size_t handles_len, |
| 706 | struct session_data *auth_session, |
| 707 | void *request_auth, const void *auth) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 708 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 709 | u8 hmac_data[DIGEST_LENGTH * 3 + 1]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 710 | sha1_context hash_ctx; |
| 711 | const size_t command_code_offset = 6; |
| 712 | const size_t auth_nonce_odd_offset = 4; |
| 713 | const size_t auth_continue_offset = 24; |
| 714 | const size_t auth_auth_offset = 25; |
| 715 | |
| 716 | if (!auth_session || !auth_session->valid) |
| 717 | return TPM_LIB_ERROR; |
| 718 | |
| 719 | sha1_starts(&hash_ctx); |
| 720 | sha1_update(&hash_ctx, request + command_code_offset, 4); |
| 721 | if (request_len0 > TPM_REQUEST_HEADER_LENGTH + handles_len) |
| 722 | sha1_update(&hash_ctx, |
| 723 | request + TPM_REQUEST_HEADER_LENGTH + handles_len, |
| 724 | request_len0 - TPM_REQUEST_HEADER_LENGTH |
| 725 | - handles_len); |
| 726 | sha1_finish(&hash_ctx, hmac_data); |
| 727 | |
| 728 | sha1_starts(&hash_ctx); |
| 729 | sha1_update(&hash_ctx, auth_session->nonce_odd, DIGEST_LENGTH); |
| 730 | sha1_update(&hash_ctx, hmac_data, sizeof(hmac_data)); |
| 731 | sha1_finish(&hash_ctx, auth_session->nonce_odd); |
| 732 | |
| 733 | if (pack_byte_string(request_auth, TPM_REQUEST_AUTH_LENGTH, "dsb", |
| 734 | 0, auth_session->handle, |
| 735 | auth_nonce_odd_offset, auth_session->nonce_odd, |
| 736 | DIGEST_LENGTH, |
| 737 | auth_continue_offset, 1)) |
| 738 | return TPM_LIB_ERROR; |
| 739 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "ss", |
| 740 | DIGEST_LENGTH, |
| 741 | auth_session->nonce_even, |
| 742 | DIGEST_LENGTH, |
| 743 | 2 * DIGEST_LENGTH, |
| 744 | request_auth + auth_nonce_odd_offset, |
| 745 | DIGEST_LENGTH + 1)) |
| 746 | return TPM_LIB_ERROR; |
| 747 | sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data), |
| 748 | request_auth + auth_auth_offset); |
| 749 | |
| 750 | return TPM_SUCCESS; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Verify an authentication block in a response. |
| 755 | * Since this func updates the nonce_even in the session data it has to be |
| 756 | * called when receiving a succesfull AUTH response. |
| 757 | * This func can verify the first as well as the second auth block (for |
| 758 | * double authorized commands). |
| 759 | * |
| 760 | * @param command_code command code of the request |
| 761 | * @param response pointer to the request (w/ uninitialised auth data) |
| 762 | * @param handles_len length of the handles area in response |
| 763 | * @param auth_session pointer to the (valid) auth session to be used |
| 764 | * @param response_auth pointer to the auth block of the response to be verified |
| 765 | * @param auth authentication data (HMAC key) |
| 766 | */ |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 767 | static u32 verify_response_auth(u32 command_code, const void *response, |
| 768 | size_t response_len0, size_t handles_len, |
| 769 | struct session_data *auth_session, |
| 770 | const void *response_auth, const void *auth) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 771 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 772 | u8 hmac_data[DIGEST_LENGTH * 3 + 1]; |
| 773 | u8 computed_auth[DIGEST_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 774 | sha1_context hash_ctx; |
| 775 | const size_t return_code_offset = 6; |
| 776 | const size_t auth_continue_offset = 20; |
| 777 | const size_t auth_auth_offset = 21; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 778 | u8 auth_continue; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 779 | |
| 780 | if (!auth_session || !auth_session->valid) |
| 781 | return TPM_AUTHFAIL; |
| 782 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "d", |
| 783 | 0, command_code)) |
| 784 | return TPM_LIB_ERROR; |
| 785 | if (response_len0 < TPM_RESPONSE_HEADER_LENGTH) |
| 786 | return TPM_LIB_ERROR; |
| 787 | |
| 788 | sha1_starts(&hash_ctx); |
| 789 | sha1_update(&hash_ctx, response + return_code_offset, 4); |
| 790 | sha1_update(&hash_ctx, hmac_data, 4); |
| 791 | if (response_len0 > TPM_RESPONSE_HEADER_LENGTH + handles_len) |
| 792 | sha1_update(&hash_ctx, |
| 793 | response + TPM_RESPONSE_HEADER_LENGTH + handles_len, |
| 794 | response_len0 - TPM_RESPONSE_HEADER_LENGTH |
| 795 | - handles_len); |
| 796 | sha1_finish(&hash_ctx, hmac_data); |
| 797 | |
| 798 | memcpy(auth_session->nonce_even, response_auth, DIGEST_LENGTH); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 799 | auth_continue = ((u8 *)response_auth)[auth_continue_offset]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 800 | if (pack_byte_string(hmac_data, sizeof(hmac_data), "ssb", |
| 801 | DIGEST_LENGTH, |
| 802 | response_auth, |
| 803 | DIGEST_LENGTH, |
| 804 | 2 * DIGEST_LENGTH, |
| 805 | auth_session->nonce_odd, |
| 806 | DIGEST_LENGTH, |
| 807 | 3 * DIGEST_LENGTH, |
| 808 | auth_continue)) |
| 809 | return TPM_LIB_ERROR; |
| 810 | |
| 811 | sha1_hmac(auth, DIGEST_LENGTH, hmac_data, sizeof(hmac_data), |
| 812 | computed_auth); |
| 813 | |
| 814 | if (memcmp(computed_auth, response_auth + auth_auth_offset, |
| 815 | DIGEST_LENGTH)) |
| 816 | return TPM_AUTHFAIL; |
| 817 | |
| 818 | return TPM_SUCCESS; |
| 819 | } |
| 820 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 821 | u32 tpm_terminate_auth_session(u32 auth_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 822 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 823 | const u8 command[18] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 824 | 0x00, 0xc1, /* TPM_TAG */ |
| 825 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 826 | 0x00, 0x00, 0x00, 0xba, /* TPM_COMMAND_CODE */ |
| 827 | 0x00, 0x00, 0x00, 0x00, /* TPM_HANDLE */ |
Miquel Raynal | a59aa34 | 2018-05-15 11:57:02 +0200 | [diff] [blame^] | 828 | 0x00, 0x00, 0x00, 0x02, /* TPM_RESOURCE_TYPE */ |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 829 | }; |
| 830 | const size_t req_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 831 | u8 request[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 832 | |
| 833 | if (pack_byte_string(request, sizeof(request), "sd", |
| 834 | 0, command, sizeof(command), |
| 835 | req_handle_offset, auth_handle)) |
| 836 | return TPM_LIB_ERROR; |
| 837 | if (oiap_session.valid && oiap_session.handle == auth_handle) |
| 838 | oiap_session.valid = 0; |
| 839 | |
| 840 | return tpm_sendrecv_command(request, NULL, NULL); |
| 841 | } |
| 842 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 843 | u32 tpm_end_oiap(void) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 844 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 845 | u32 err = TPM_SUCCESS; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 846 | if (oiap_session.valid) |
| 847 | err = tpm_terminate_auth_session(oiap_session.handle); |
| 848 | return err; |
| 849 | } |
| 850 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 851 | u32 tpm_oiap(u32 *auth_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 852 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 853 | const u8 command[10] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 854 | 0x00, 0xc1, /* TPM_TAG */ |
| 855 | 0x00, 0x00, 0x00, 0x0a, /* parameter size */ |
| 856 | 0x00, 0x00, 0x00, 0x0a, /* TPM_COMMAND_CODE */ |
| 857 | }; |
| 858 | const size_t res_auth_handle_offset = TPM_RESPONSE_HEADER_LENGTH; |
| 859 | const size_t res_nonce_even_offset = TPM_RESPONSE_HEADER_LENGTH + 4; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 860 | u8 response[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 861 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 862 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 863 | |
| 864 | if (oiap_session.valid) |
| 865 | tpm_terminate_auth_session(oiap_session.handle); |
| 866 | |
| 867 | err = tpm_sendrecv_command(command, response, &response_length); |
| 868 | if (err) |
| 869 | return err; |
| 870 | if (unpack_byte_string(response, response_length, "ds", |
| 871 | res_auth_handle_offset, &oiap_session.handle, |
| 872 | res_nonce_even_offset, &oiap_session.nonce_even, |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 873 | (u32)DIGEST_LENGTH)) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 874 | return TPM_LIB_ERROR; |
| 875 | oiap_session.valid = 1; |
| 876 | if (auth_handle) |
| 877 | *auth_handle = oiap_session.handle; |
| 878 | return 0; |
| 879 | } |
| 880 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 881 | u32 tpm_load_key2_oiap(u32 parent_handle, const void *key, size_t key_length, |
| 882 | const void *parent_key_usage_auth, u32 *key_handle) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 883 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 884 | const u8 command[14] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 885 | 0x00, 0xc2, /* TPM_TAG */ |
| 886 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 887 | 0x00, 0x00, 0x00, 0x41, /* TPM_COMMAND_CODE */ |
| 888 | 0x00, 0x00, 0x00, 0x00, /* parent handle */ |
| 889 | }; |
| 890 | const size_t req_size_offset = 2; |
| 891 | const size_t req_parent_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
| 892 | const size_t req_key_offset = TPM_REQUEST_HEADER_LENGTH + 4; |
| 893 | const size_t res_handle_offset = TPM_RESPONSE_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 894 | u8 request[sizeof(command) + TPM_KEY12_MAX_LENGTH + |
| 895 | TPM_REQUEST_AUTH_LENGTH]; |
| 896 | u8 response[COMMAND_BUFFER_SIZE]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 897 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 898 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 899 | |
| 900 | if (!oiap_session.valid) { |
| 901 | err = tpm_oiap(NULL); |
| 902 | if (err) |
| 903 | return err; |
| 904 | } |
| 905 | if (pack_byte_string(request, sizeof(request), "sdds", |
| 906 | 0, command, sizeof(command), |
| 907 | req_size_offset, |
| 908 | sizeof(command) + key_length |
| 909 | + TPM_REQUEST_AUTH_LENGTH, |
| 910 | req_parent_handle_offset, parent_handle, |
| 911 | req_key_offset, key, key_length |
| 912 | )) |
| 913 | return TPM_LIB_ERROR; |
| 914 | |
| 915 | err = create_request_auth(request, sizeof(command) + key_length, 4, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 916 | &oiap_session, |
| 917 | request + sizeof(command) + key_length, |
| 918 | parent_key_usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 919 | if (err) |
| 920 | return err; |
| 921 | err = tpm_sendrecv_command(request, response, &response_length); |
| 922 | if (err) { |
| 923 | if (err == TPM_AUTHFAIL) |
| 924 | oiap_session.valid = 0; |
| 925 | return err; |
| 926 | } |
| 927 | |
| 928 | err = verify_response_auth(0x00000041, response, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 929 | response_length - TPM_RESPONSE_AUTH_LENGTH, |
| 930 | 4, &oiap_session, |
| 931 | response + response_length - |
| 932 | TPM_RESPONSE_AUTH_LENGTH, |
| 933 | parent_key_usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 934 | if (err) |
| 935 | return err; |
| 936 | |
| 937 | if (key_handle) { |
| 938 | if (unpack_byte_string(response, response_length, "d", |
| 939 | res_handle_offset, key_handle)) |
| 940 | return TPM_LIB_ERROR; |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 946 | u32 tpm_get_pub_key_oiap(u32 key_handle, const void *usage_auth, void *pubkey, |
| 947 | size_t *pubkey_len) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 948 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 949 | const u8 command[14] = { |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 950 | 0x00, 0xc2, /* TPM_TAG */ |
| 951 | 0x00, 0x00, 0x00, 0x00, /* parameter size */ |
| 952 | 0x00, 0x00, 0x00, 0x21, /* TPM_COMMAND_CODE */ |
| 953 | 0x00, 0x00, 0x00, 0x00, /* key handle */ |
| 954 | }; |
| 955 | const size_t req_size_offset = 2; |
| 956 | const size_t req_key_handle_offset = TPM_REQUEST_HEADER_LENGTH; |
| 957 | const size_t res_pubkey_offset = TPM_RESPONSE_HEADER_LENGTH; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 958 | u8 request[sizeof(command) + TPM_REQUEST_AUTH_LENGTH]; |
| 959 | u8 response[TPM_RESPONSE_HEADER_LENGTH + TPM_PUBKEY_MAX_LENGTH + |
| 960 | TPM_RESPONSE_AUTH_LENGTH]; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 961 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 962 | u32 err; |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 963 | |
| 964 | if (!oiap_session.valid) { |
| 965 | err = tpm_oiap(NULL); |
| 966 | if (err) |
| 967 | return err; |
| 968 | } |
| 969 | if (pack_byte_string(request, sizeof(request), "sdd", |
| 970 | 0, command, sizeof(command), |
| 971 | req_size_offset, |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 972 | (u32)(sizeof(command) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 973 | + TPM_REQUEST_AUTH_LENGTH), |
| 974 | req_key_handle_offset, key_handle |
| 975 | )) |
| 976 | return TPM_LIB_ERROR; |
| 977 | err = create_request_auth(request, sizeof(command), 4, &oiap_session, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 978 | request + sizeof(command), usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 979 | if (err) |
| 980 | return err; |
| 981 | err = tpm_sendrecv_command(request, response, &response_length); |
| 982 | if (err) { |
| 983 | if (err == TPM_AUTHFAIL) |
| 984 | oiap_session.valid = 0; |
| 985 | return err; |
| 986 | } |
| 987 | err = verify_response_auth(0x00000021, response, |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 988 | response_length - TPM_RESPONSE_AUTH_LENGTH, |
| 989 | 0, &oiap_session, |
| 990 | response + response_length - |
| 991 | TPM_RESPONSE_AUTH_LENGTH, |
| 992 | usage_auth); |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 993 | if (err) |
| 994 | return err; |
| 995 | |
| 996 | if (pubkey) { |
| 997 | if ((response_length - TPM_RESPONSE_HEADER_LENGTH |
Miquel Raynal | 886ccc0 | 2018-05-15 11:57:00 +0200 | [diff] [blame] | 998 | - TPM_RESPONSE_AUTH_LENGTH) > *pubkey_len) |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 999 | return TPM_LIB_ERROR; |
| 1000 | *pubkey_len = response_length - TPM_RESPONSE_HEADER_LENGTH |
| 1001 | - TPM_RESPONSE_AUTH_LENGTH; |
| 1002 | memcpy(pubkey, response + res_pubkey_offset, |
| 1003 | response_length - TPM_RESPONSE_HEADER_LENGTH |
| 1004 | - TPM_RESPONSE_AUTH_LENGTH); |
| 1005 | } |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 1010 | #ifdef CONFIG_TPM_LOAD_KEY_BY_SHA1 |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1011 | u32 tpm_find_key_sha1(const u8 auth[20], const u8 pubkey_digest[20], |
| 1012 | u32 *handle) |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 1013 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1014 | u16 key_count; |
| 1015 | u32 key_handles[10]; |
| 1016 | u8 buf[288]; |
| 1017 | u8 *ptr; |
| 1018 | u32 err; |
| 1019 | u8 digest[20]; |
mario.six@gdsys.cc | a5a7ea2 | 2017-03-20 10:28:28 +0100 | [diff] [blame] | 1020 | size_t buf_len; |
| 1021 | unsigned int i; |
| 1022 | |
| 1023 | /* fetch list of already loaded keys in the TPM */ |
| 1024 | err = tpm_get_capability(TPM_CAP_HANDLE, TPM_RT_KEY, buf, sizeof(buf)); |
| 1025 | if (err) |
| 1026 | return -1; |
| 1027 | key_count = get_unaligned_be16(buf); |
| 1028 | ptr = buf + 2; |
| 1029 | for (i = 0; i < key_count; ++i, ptr += 4) |
| 1030 | key_handles[i] = get_unaligned_be32(ptr); |
| 1031 | |
| 1032 | /* now search a(/ the) key which we can access with the given auth */ |
| 1033 | for (i = 0; i < key_count; ++i) { |
| 1034 | buf_len = sizeof(buf); |
| 1035 | err = tpm_get_pub_key_oiap(key_handles[i], auth, buf, &buf_len); |
| 1036 | if (err && err != TPM_AUTHFAIL) |
| 1037 | return -1; |
| 1038 | if (err) |
| 1039 | continue; |
| 1040 | sha1_csum(buf, buf_len, digest); |
| 1041 | if (!memcmp(digest, pubkey_digest, 20)) { |
| 1042 | *handle = key_handles[i]; |
| 1043 | return 0; |
| 1044 | } |
| 1045 | } |
| 1046 | return 1; |
| 1047 | } |
| 1048 | #endif /* CONFIG_TPM_LOAD_KEY_BY_SHA1 */ |
| 1049 | |
Reinhard Pfau | 4fece43 | 2013-06-26 15:55:13 +0200 | [diff] [blame] | 1050 | #endif /* CONFIG_TPM_AUTH_SESSIONS */ |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1051 | |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1052 | u32 tpm_get_random(void *data, u32 count) |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1053 | { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1054 | const u8 command[14] = { |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1055 | 0x0, 0xc1, /* TPM_TAG */ |
| 1056 | 0x0, 0x0, 0x0, 0xe, /* parameter size */ |
| 1057 | 0x0, 0x0, 0x0, 0x46, /* TPM_COMMAND_CODE */ |
| 1058 | }; |
| 1059 | const size_t length_offset = 10; |
| 1060 | const size_t data_size_offset = 10; |
| 1061 | const size_t data_offset = 14; |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1062 | u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE]; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1063 | size_t response_length = sizeof(response); |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1064 | u32 data_size; |
| 1065 | u8 *out = data; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1066 | |
| 1067 | while (count > 0) { |
Miquel Raynal | d790f55 | 2018-05-15 11:56:59 +0200 | [diff] [blame] | 1068 | u32 this_bytes = min((size_t)count, |
| 1069 | sizeof(response) - data_offset); |
| 1070 | u32 err; |
André Draszik | 2c6e5ff | 2017-10-03 16:55:52 +0100 | [diff] [blame] | 1071 | |
| 1072 | if (pack_byte_string(buf, sizeof(buf), "sd", |
| 1073 | 0, command, sizeof(command), |
| 1074 | length_offset, this_bytes)) |
| 1075 | return TPM_LIB_ERROR; |
| 1076 | err = tpm_sendrecv_command(buf, response, &response_length); |
| 1077 | if (err) |
| 1078 | return err; |
| 1079 | if (unpack_byte_string(response, response_length, "d", |
| 1080 | data_size_offset, &data_size)) |
| 1081 | return TPM_LIB_ERROR; |
| 1082 | if (data_size > count) |
| 1083 | return TPM_LIB_ERROR; |
| 1084 | if (unpack_byte_string(response, response_length, "s", |
| 1085 | data_offset, out, data_size)) |
| 1086 | return TPM_LIB_ERROR; |
| 1087 | |
| 1088 | count -= data_size; |
| 1089 | out += data_size; |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |