1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2018, Bootlin
4 * Author: Miquel Raynal <miquel.raynal@bootlin.com>
10 #include <asm/state.h>
11 #include <asm/unaligned.h>
12 #include <linux/bitops.h>
13 #include <u-boot/crc.h>
17 TPM2_HIERARCHY_LOCKOUT = 0,
18 TPM2_HIERARCHY_ENDORSEMENT,
19 TPM2_HIERARCHY_PLATFORM,
23 /* Subset of supported capabilities */
24 enum tpm2_capability {
25 TPM_CAP_TPM_PROPERTIES = 0x6,
28 /* Subset of supported properties */
29 #define TPM2_PROPERTIES_OFFSET 0x0000020E
31 enum tpm2_cap_tpm_property {
32 TPM2_FAIL_COUNTER = 0,
35 TPM2_LOCKOUT_RECOVERY,
39 #define SANDBOX_TPM_PCR_NB 1
41 static const u8 sandbox_extended_once_pcr[] = {
42 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
43 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
44 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
45 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
49 /* TPM internal states */
53 /* TPM password per hierarchy */
54 char pw[TPM2_HIERARCHY_NB][TPM2_DIGEST_LEN + 1];
55 int pw_sz[TPM2_HIERARCHY_NB];
57 u32 properties[TPM2_PROPERTY_NB];
59 u8 pcr[SANDBOX_TPM_PCR_NB][TPM2_DIGEST_LEN];
60 /* TPM PCR extensions */
61 u32 pcr_extensions[SANDBOX_TPM_PCR_NB];
65 * Check the tag validity depending on the command (authentication required or
66 * not). If authentication is required, check it is valid. Update the auth
67 * pointer to point to the next chunk of data to process if needed.
69 static int sandbox_tpm2_check_session(struct udevice *dev, u32 command, u16 tag,
71 enum tpm2_hierarchy *hierarchy)
73 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
74 u32 handle, auth_sz, session_handle;
80 case TPM2_CC_SELF_TEST:
81 case TPM2_CC_GET_CAPABILITY:
82 case TPM2_CC_PCR_READ:
83 if (tag != TPM2_ST_NO_SESSIONS) {
84 printf("No session required for command 0x%x\n",
86 return TPM2_RC_BAD_TAG;
92 case TPM2_CC_HIERCHANGEAUTH:
93 case TPM2_CC_DAM_RESET:
94 case TPM2_CC_DAM_PARAMETERS:
95 case TPM2_CC_PCR_EXTEND:
96 if (tag != TPM2_ST_SESSIONS) {
97 printf("Session required for command 0x%x\n", command);
98 return TPM2_RC_AUTH_CONTEXT;
101 handle = get_unaligned_be32(*auth);
102 *auth += sizeof(handle);
105 * PCR_Extend had a different protection mechanism and does not
106 * use the same standards as other commands.
108 if (command == TPM2_CC_PCR_EXTEND)
112 case TPM2_RH_LOCKOUT:
113 *hierarchy = TPM2_HIERARCHY_LOCKOUT;
115 case TPM2_RH_ENDORSEMENT:
116 if (command == TPM2_CC_CLEAR) {
117 printf("Endorsement hierarchy unsupported\n");
118 return TPM2_RC_AUTH_MISSING;
120 *hierarchy = TPM2_HIERARCHY_ENDORSEMENT;
122 case TPM2_RH_PLATFORM:
123 *hierarchy = TPM2_HIERARCHY_PLATFORM;
126 printf("Wrong handle 0x%x\n", handle);
127 return TPM2_RC_VALUE;
133 printf("Command code not recognized: 0x%x\n", command);
134 return TPM2_RC_COMMAND_CODE;
137 auth_sz = get_unaligned_be32(*auth);
138 *auth += sizeof(auth_sz);
140 session_handle = get_unaligned_be32(*auth);
141 *auth += sizeof(session_handle);
142 if (session_handle != TPM2_RS_PW) {
143 printf("Wrong session handle 0x%x\n", session_handle);
144 return TPM2_RC_VALUE;
147 nonce_sz = get_unaligned_be16(*auth);
148 *auth += sizeof(nonce_sz);
150 printf("Nonces not supported in Sandbox, aborting\n");
151 return TPM2_RC_HANDLE;
154 /* Ignore attributes */
157 pw_sz = get_unaligned_be16(*auth);
158 *auth += sizeof(pw_sz);
159 if (auth_sz != (9 + nonce_sz + pw_sz)) {
160 printf("Authentication size (%d) do not match %d\n",
161 auth_sz, 9 + nonce_sz + pw_sz);
165 /* No passwork is acceptable */
166 if (!pw_sz && !tpm->pw_sz[*hierarchy])
167 return TPM2_RC_SUCCESS;
169 /* Password is too long */
170 if (pw_sz > TPM2_DIGEST_LEN) {
171 printf("Password should not be more than %dB\n",
173 return TPM2_RC_AUTHSIZE;
176 pw = (const char *)*auth;
179 /* Password is wrong */
180 if (pw_sz != tpm->pw_sz[*hierarchy] ||
181 strncmp(pw, tpm->pw[*hierarchy], tpm->pw_sz[*hierarchy])) {
182 printf("Authentication failed: wrong password.\n");
183 return TPM2_RC_BAD_AUTH;
186 return TPM2_RC_SUCCESS;
189 static int sandbox_tpm2_check_readyness(struct udevice *dev, int command)
191 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
194 case TPM2_CC_STARTUP:
195 if (!tpm->init_done || tpm->startup_done)
196 return TPM2_RC_INITIALIZE;
199 case TPM2_CC_GET_CAPABILITY:
200 if (!tpm->init_done || !tpm->startup_done)
201 return TPM2_RC_INITIALIZE;
204 case TPM2_CC_SELF_TEST:
205 if (!tpm->startup_done)
206 return TPM2_RC_INITIALIZE;
210 if (!tpm->tests_done)
211 return TPM2_RC_NEEDS_TEST;
219 static int sandbox_tpm2_fill_buf(u8 *recv, size_t *recv_len, u16 tag, u32 rc)
221 *recv_len = sizeof(tag) + sizeof(u32) + sizeof(rc);
224 put_unaligned_be16(tag, recv);
228 put_unaligned_be32(*recv_len, recv);
231 /* Write return code */
232 put_unaligned_be32(rc, recv);
235 /* Add trailing \0 */
241 static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
244 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
247 /* Only simulate the first extensions from all '0' with only '0' */
248 for (i = 0; i < TPM2_DIGEST_LEN; i++)
249 if (tpm->pcr[pcr_index][i] || extension[i])
250 return TPM2_RC_FAILURE;
252 memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
254 tpm->pcr_extensions[pcr_index]++;
259 static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
260 size_t send_size, u8 *recvbuf,
263 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
264 enum tpm2_hierarchy hierarchy = 0;
265 const u8 *sent = sendbuf;
267 u32 length, command, rc = 0;
268 u16 tag, mode, new_pw_sz;
272 /* TPM2_GetProperty */
273 u32 capability, property, property_count;
275 /* TPM2_PCR_Read/Extend variables */
278 u32 selections, pcr_nb;
282 tag = get_unaligned_be16(sent);
285 length = get_unaligned_be32(sent);
286 sent += sizeof(length);
287 if (length != send_size) {
288 printf("TPM2: Unmatching length, received: %ld, expected: %d\n",
291 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
295 command = get_unaligned_be32(sent);
296 sent += sizeof(command);
297 rc = sandbox_tpm2_check_readyness(dev, command);
299 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
303 rc = sandbox_tpm2_check_session(dev, command, tag, &sent, &hierarchy);
305 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
310 case TPM2_CC_STARTUP:
311 mode = get_unaligned_be16(sent);
312 sent += sizeof(mode);
321 tpm->startup_done = true;
323 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
326 case TPM2_CC_SELF_TEST:
328 sent += sizeof(yes_no);
337 tpm->tests_done = true;
339 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
343 /* Reset this hierarchy password */
344 tpm->pw_sz[hierarchy] = 0;
346 /* Reset all password if thisis the PLATFORM hierarchy */
347 if (hierarchy == TPM2_HIERARCHY_PLATFORM)
348 for (i = 0; i < TPM2_HIERARCHY_NB; i++)
351 /* Reset the properties */
352 for (i = 0; i < TPM2_PROPERTY_NB; i++)
353 tpm->properties[i] = 0;
355 /* Reset the PCRs and their number of extensions */
356 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++) {
357 tpm->pcr_extensions[i] = 0;
358 for (j = 0; j < TPM2_DIGEST_LEN; j++)
362 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
365 case TPM2_CC_HIERCHANGEAUTH:
366 new_pw_sz = get_unaligned_be16(sent);
367 sent += sizeof(new_pw_sz);
368 if (new_pw_sz > TPM2_DIGEST_LEN) {
370 } else if (new_pw_sz) {
371 tpm->pw_sz[hierarchy] = new_pw_sz;
372 memcpy(tpm->pw[hierarchy], sent, new_pw_sz);
376 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
379 case TPM2_CC_GET_CAPABILITY:
380 capability = get_unaligned_be32(sent);
381 sent += sizeof(capability);
382 if (capability != TPM_CAP_TPM_PROPERTIES) {
383 printf("Sandbox TPM only support TPM_CAPABILITIES\n");
384 return TPM2_RC_HANDLE;
387 property = get_unaligned_be32(sent);
388 sent += sizeof(property);
389 property -= TPM2_PROPERTIES_OFFSET;
391 property_count = get_unaligned_be32(sent);
392 sent += sizeof(property_count);
393 if (!property_count ||
394 property + property_count > TPM2_PROPERTY_NB) {
396 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
400 put_unaligned_be16(tag, recv);
403 /* Ignore length for now */
406 /* Write return code */
407 put_unaligned_be32(rc, recv);
410 /* Tell there is more data to read */
412 recv += sizeof(yes_no);
414 /* Repeat the capability */
415 put_unaligned_be32(capability, recv);
416 recv += sizeof(capability);
418 /* Give the number of properties that follow */
419 put_unaligned_be32(property_count, recv);
420 recv += sizeof(property_count);
422 /* Fill with the properties */
423 for (i = 0; i < property_count; i++) {
424 put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
426 recv += sizeof(property);
427 put_unaligned_be32(tpm->properties[property + i],
429 recv += sizeof(property);
432 /* Add trailing \0 */
435 /* Write response length */
436 *recv_len = recv - recvbuf;
437 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
441 case TPM2_CC_DAM_PARAMETERS:
442 tpm->properties[TPM2_PROP_MAX_TRIES] = get_unaligned_be32(sent);
443 sent += sizeof(*tpm->properties);
444 tpm->properties[TPM2_RECOVERY_TIME] = get_unaligned_be32(sent);
445 sent += sizeof(*tpm->properties);
446 tpm->properties[TPM2_LOCKOUT_RECOVERY] = get_unaligned_be32(sent);
447 sent += sizeof(*tpm->properties);
449 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
452 case TPM2_CC_PCR_READ:
453 selections = get_unaligned_be32(sent);
454 sent += sizeof(selections);
455 if (selections != 1) {
456 printf("Sandbox cannot handle more than one PCR\n");
458 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
461 alg = get_unaligned_be16(sent);
463 if (alg != TPM2_ALG_SHA256) {
464 printf("Sandbox TPM only handle SHA256 algorithm\n");
466 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
469 pcr_array_sz = *sent;
470 sent += sizeof(pcr_array_sz);
471 if (!pcr_array_sz || pcr_array_sz > 8) {
472 printf("Sandbox TPM cannot handle so much PCRs\n");
474 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
477 for (i = 0; i < pcr_array_sz; i++)
478 pcr_map += (u64)sent[i] << (i * 8);
480 if (pcr_map >> SANDBOX_TPM_PCR_NB) {
481 printf("Sandbox TPM handles up to %d PCR(s)\n",
484 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
488 printf("Empty PCR map.\n");
490 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
493 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++)
494 if (pcr_map & BIT(i))
498 put_unaligned_be16(tag, recv);
501 /* Ignore length for now */
504 /* Write return code */
505 put_unaligned_be32(rc, recv);
508 /* Number of extensions */
509 put_unaligned_be32(tpm->pcr_extensions[pcr_index], recv);
513 memcpy(recv, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
514 recv += TPM2_DIGEST_LEN;
516 /* Add trailing \0 */
519 /* Write response length */
520 *recv_len = recv - recvbuf;
521 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
525 case TPM2_CC_PCR_EXTEND:
526 /* Get the PCR index */
527 pcr_index = get_unaligned_be32(sendbuf + sizeof(tag) +
530 if (pcr_index > SANDBOX_TPM_PCR_NB) {
531 printf("Sandbox TPM handles up to %d PCR(s)\n",
536 /* Check the number of hashes */
537 pcr_nb = get_unaligned_be32(sent);
538 sent += sizeof(pcr_nb);
540 printf("Sandbox cannot handle more than one PCR\n");
542 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
545 /* Check the hash algorithm */
546 alg = get_unaligned_be16(sent);
548 if (alg != TPM2_ALG_SHA256) {
549 printf("Sandbox TPM only handle SHA256 algorithm\n");
551 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
555 rc = sandbox_tpm2_extend(dev, pcr_index, sent);
557 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
561 printf("TPM2 command %02x unknown in Sandbox\n", command);
562 rc = TPM2_RC_COMMAND_CODE;
563 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
569 static int sandbox_tpm2_get_desc(struct udevice *dev, char *buf, int size)
574 return snprintf(buf, size, "Sandbox TPM2.x");
577 static int sandbox_tpm2_open(struct udevice *dev)
579 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
584 tpm->init_done = true;
589 static int sandbox_tpm2_probe(struct udevice *dev)
591 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
592 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
594 /* Use the TPM v2 stack */
595 priv->version = TPM_V2;
597 memset(tpm, 0, sizeof(*tpm));
599 priv->pcr_count = 32;
600 priv->pcr_select_min = 2;
605 static int sandbox_tpm2_close(struct udevice *dev)
610 static const struct tpm_ops sandbox_tpm2_ops = {
611 .open = sandbox_tpm2_open,
612 .close = sandbox_tpm2_close,
613 .get_desc = sandbox_tpm2_get_desc,
614 .xfer = sandbox_tpm2_xfer,
617 static const struct udevice_id sandbox_tpm2_ids[] = {
618 { .compatible = "sandbox,tpm2" },
622 U_BOOT_DRIVER(sandbox_tpm2) = {
623 .name = "sandbox_tpm2",
625 .of_match = sandbox_tpm2_ids,
626 .ops = &sandbox_tpm2_ops,
627 .probe = sandbox_tpm2_probe,
628 .priv_auto_alloc_size = sizeof(struct sandbox_tpm2),