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 <u-boot/crc.h>
16 TPM2_HIERARCHY_LOCKOUT = 0,
17 TPM2_HIERARCHY_ENDORSEMENT,
18 TPM2_HIERARCHY_PLATFORM,
22 /* Subset of supported capabilities */
23 enum tpm2_capability {
24 TPM_CAP_TPM_PROPERTIES = 0x6,
27 /* Subset of supported properties */
28 #define TPM2_PROPERTIES_OFFSET 0x0000020E
30 enum tpm2_cap_tpm_property {
31 TPM2_FAIL_COUNTER = 0,
34 TPM2_LOCKOUT_RECOVERY,
38 #define SANDBOX_TPM_PCR_NB 1
40 static const u8 sandbox_extended_once_pcr[] = {
41 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
42 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
43 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
44 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
48 /* TPM internal states */
52 /* TPM password per hierarchy */
53 char pw[TPM2_HIERARCHY_NB][TPM2_DIGEST_LEN + 1];
54 int pw_sz[TPM2_HIERARCHY_NB];
56 u32 properties[TPM2_PROPERTY_NB];
58 u8 pcr[SANDBOX_TPM_PCR_NB][TPM2_DIGEST_LEN];
59 /* TPM PCR extensions */
60 u32 pcr_extensions[SANDBOX_TPM_PCR_NB];
64 * Check the tag validity depending on the command (authentication required or
65 * not). If authentication is required, check it is valid. Update the auth
66 * pointer to point to the next chunk of data to process if needed.
68 static int sandbox_tpm2_check_session(struct udevice *dev, u32 command, u16 tag,
70 enum tpm2_hierarchy *hierarchy)
72 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
73 u32 handle, auth_sz, session_handle;
79 case TPM2_CC_SELF_TEST:
80 case TPM2_CC_GET_CAPABILITY:
81 case TPM2_CC_PCR_READ:
82 if (tag != TPM2_ST_NO_SESSIONS) {
83 printf("No session required for command 0x%x\n",
85 return TPM2_RC_BAD_TAG;
91 case TPM2_CC_HIERCHANGEAUTH:
92 case TPM2_CC_DAM_RESET:
93 case TPM2_CC_DAM_PARAMETERS:
94 case TPM2_CC_PCR_EXTEND:
95 if (tag != TPM2_ST_SESSIONS) {
96 printf("Session required for command 0x%x\n", command);
97 return TPM2_RC_AUTH_CONTEXT;
100 handle = get_unaligned_be32(*auth);
101 *auth += sizeof(handle);
104 * PCR_Extend had a different protection mechanism and does not
105 * use the same standards as other commands.
107 if (command == TPM2_CC_PCR_EXTEND)
111 case TPM2_RH_LOCKOUT:
112 *hierarchy = TPM2_HIERARCHY_LOCKOUT;
114 case TPM2_RH_ENDORSEMENT:
115 if (command == TPM2_CC_CLEAR) {
116 printf("Endorsement hierarchy unsupported\n");
117 return TPM2_RC_AUTH_MISSING;
119 *hierarchy = TPM2_HIERARCHY_ENDORSEMENT;
121 case TPM2_RH_PLATFORM:
122 *hierarchy = TPM2_HIERARCHY_PLATFORM;
125 printf("Wrong handle 0x%x\n", handle);
126 return TPM2_RC_VALUE;
132 printf("Command code not recognized: 0x%x\n", command);
133 return TPM2_RC_COMMAND_CODE;
136 auth_sz = get_unaligned_be32(*auth);
137 *auth += sizeof(auth_sz);
139 session_handle = get_unaligned_be32(*auth);
140 *auth += sizeof(session_handle);
141 if (session_handle != TPM2_RS_PW) {
142 printf("Wrong session handle 0x%x\n", session_handle);
143 return TPM2_RC_VALUE;
146 nonce_sz = get_unaligned_be16(*auth);
147 *auth += sizeof(nonce_sz);
149 printf("Nonces not supported in Sandbox, aborting\n");
150 return TPM2_RC_HANDLE;
153 /* Ignore attributes */
156 pw_sz = get_unaligned_be16(*auth);
157 *auth += sizeof(pw_sz);
158 if (auth_sz != (9 + nonce_sz + pw_sz)) {
159 printf("Authentication size (%d) do not match %d\n",
160 auth_sz, 9 + nonce_sz + pw_sz);
164 /* No passwork is acceptable */
165 if (!pw_sz && !tpm->pw_sz[*hierarchy])
166 return TPM2_RC_SUCCESS;
168 /* Password is too long */
169 if (pw_sz > TPM2_DIGEST_LEN) {
170 printf("Password should not be more than %dB\n",
172 return TPM2_RC_AUTHSIZE;
175 pw = (const char *)*auth;
178 /* Password is wrong */
179 if (pw_sz != tpm->pw_sz[*hierarchy] ||
180 strncmp(pw, tpm->pw[*hierarchy], tpm->pw_sz[*hierarchy])) {
181 printf("Authentication failed: wrong password.\n");
182 return TPM2_RC_BAD_AUTH;
185 return TPM2_RC_SUCCESS;
188 static int sandbox_tpm2_check_readyness(struct udevice *dev, int command)
190 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
193 case TPM2_CC_STARTUP:
194 if (!tpm->init_done || tpm->startup_done)
195 return TPM2_RC_INITIALIZE;
198 case TPM2_CC_GET_CAPABILITY:
199 if (!tpm->init_done || !tpm->startup_done)
200 return TPM2_RC_INITIALIZE;
203 case TPM2_CC_SELF_TEST:
204 if (!tpm->startup_done)
205 return TPM2_RC_INITIALIZE;
209 if (!tpm->tests_done)
210 return TPM2_RC_NEEDS_TEST;
218 static int sandbox_tpm2_fill_buf(u8 *recv, size_t *recv_len, u16 tag, u32 rc)
220 *recv_len = sizeof(tag) + sizeof(u32) + sizeof(rc);
223 put_unaligned_be16(tag, recv);
227 put_unaligned_be32(*recv_len, recv);
230 /* Write return code */
231 put_unaligned_be32(rc, recv);
234 /* Add trailing \0 */
240 static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
243 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
246 /* Only simulate the first extensions from all '0' with only '0' */
247 for (i = 0; i < TPM2_DIGEST_LEN; i++)
248 if (tpm->pcr[pcr_index][i] || extension[i])
249 return TPM2_RC_FAILURE;
251 memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
253 tpm->pcr_extensions[pcr_index]++;
258 static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
259 size_t send_size, u8 *recvbuf,
262 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
263 enum tpm2_hierarchy hierarchy = 0;
264 const u8 *sent = sendbuf;
266 u32 length, command, rc = 0;
267 u16 tag, mode, new_pw_sz;
271 /* TPM2_GetProperty */
272 u32 capability, property, property_count;
274 /* TPM2_PCR_Read/Extend variables */
277 u32 selections, pcr_nb;
281 tag = get_unaligned_be16(sent);
284 length = get_unaligned_be32(sent);
285 sent += sizeof(length);
286 if (length != send_size) {
287 printf("TPM2: Unmatching length, received: %ld, expected: %d\n",
290 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
294 command = get_unaligned_be32(sent);
295 sent += sizeof(command);
296 rc = sandbox_tpm2_check_readyness(dev, command);
298 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
302 rc = sandbox_tpm2_check_session(dev, command, tag, &sent, &hierarchy);
304 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
309 case TPM2_CC_STARTUP:
310 mode = get_unaligned_be16(sent);
311 sent += sizeof(mode);
320 tpm->startup_done = true;
322 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
325 case TPM2_CC_SELF_TEST:
327 sent += sizeof(yes_no);
336 tpm->tests_done = true;
338 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
342 /* Reset this hierarchy password */
343 tpm->pw_sz[hierarchy] = 0;
345 /* Reset all password if thisis the PLATFORM hierarchy */
346 if (hierarchy == TPM2_HIERARCHY_PLATFORM)
347 for (i = 0; i < TPM2_HIERARCHY_NB; i++)
350 /* Reset the properties */
351 for (i = 0; i < TPM2_PROPERTY_NB; i++)
352 tpm->properties[i] = 0;
354 /* Reset the PCRs and their number of extensions */
355 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++) {
356 tpm->pcr_extensions[i] = 0;
357 for (j = 0; j < TPM2_DIGEST_LEN; j++)
361 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
364 case TPM2_CC_HIERCHANGEAUTH:
365 new_pw_sz = get_unaligned_be16(sent);
366 sent += sizeof(new_pw_sz);
367 if (new_pw_sz > TPM2_DIGEST_LEN) {
369 } else if (new_pw_sz) {
370 tpm->pw_sz[hierarchy] = new_pw_sz;
371 memcpy(tpm->pw[hierarchy], sent, new_pw_sz);
375 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
378 case TPM2_CC_GET_CAPABILITY:
379 capability = get_unaligned_be32(sent);
380 sent += sizeof(capability);
381 if (capability != TPM_CAP_TPM_PROPERTIES) {
382 printf("Sandbox TPM only support TPM_CAPABILITIES\n");
383 return TPM2_RC_HANDLE;
386 property = get_unaligned_be32(sent);
387 sent += sizeof(property);
388 property -= TPM2_PROPERTIES_OFFSET;
390 property_count = get_unaligned_be32(sent);
391 sent += sizeof(property_count);
392 if (!property_count ||
393 property + property_count > TPM2_PROPERTY_NB) {
395 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
399 put_unaligned_be16(tag, recv);
402 /* Ignore length for now */
405 /* Write return code */
406 put_unaligned_be32(rc, recv);
409 /* Tell there is more data to read */
411 recv += sizeof(yes_no);
413 /* Repeat the capability */
414 put_unaligned_be32(capability, recv);
415 recv += sizeof(capability);
417 /* Give the number of properties that follow */
418 put_unaligned_be32(property_count, recv);
419 recv += sizeof(property_count);
421 /* Fill with the properties */
422 for (i = 0; i < property_count; i++) {
423 put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
425 recv += sizeof(property);
426 put_unaligned_be32(tpm->properties[property + i],
428 recv += sizeof(property);
431 /* Add trailing \0 */
434 /* Write response length */
435 *recv_len = recv - recvbuf;
436 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
440 case TPM2_CC_DAM_PARAMETERS:
441 tpm->properties[TPM2_PROP_MAX_TRIES] = get_unaligned_be32(sent);
442 sent += sizeof(*tpm->properties);
443 tpm->properties[TPM2_RECOVERY_TIME] = get_unaligned_be32(sent);
444 sent += sizeof(*tpm->properties);
445 tpm->properties[TPM2_LOCKOUT_RECOVERY] = get_unaligned_be32(sent);
446 sent += sizeof(*tpm->properties);
448 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
451 case TPM2_CC_PCR_READ:
452 selections = get_unaligned_be32(sent);
453 sent += sizeof(selections);
454 if (selections != 1) {
455 printf("Sandbox cannot handle more than one PCR\n");
457 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
460 alg = get_unaligned_be16(sent);
462 if (alg != TPM2_ALG_SHA256) {
463 printf("Sandbox TPM only handle SHA256 algorithm\n");
465 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
468 pcr_array_sz = *sent;
469 sent += sizeof(pcr_array_sz);
470 if (!pcr_array_sz || pcr_array_sz > 8) {
471 printf("Sandbox TPM cannot handle so much PCRs\n");
473 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
476 for (i = 0; i < pcr_array_sz; i++)
477 pcr_map += (u64)sent[i] << (i * 8);
479 if (pcr_map >> SANDBOX_TPM_PCR_NB) {
480 printf("Sandbox TPM handles up to %d PCR(s)\n",
483 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
487 printf("Empty PCR map.\n");
489 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
492 for (i = 0; i < SANDBOX_TPM_PCR_NB; i++)
493 if (pcr_map & BIT(i))
497 put_unaligned_be16(tag, recv);
500 /* Ignore length for now */
503 /* Write return code */
504 put_unaligned_be32(rc, recv);
507 /* Number of extensions */
508 put_unaligned_be32(tpm->pcr_extensions[pcr_index], recv);
512 memcpy(recv, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
513 recv += TPM2_DIGEST_LEN;
515 /* Add trailing \0 */
518 /* Write response length */
519 *recv_len = recv - recvbuf;
520 put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
524 case TPM2_CC_PCR_EXTEND:
525 /* Get the PCR index */
526 pcr_index = get_unaligned_be32(sendbuf + sizeof(tag) +
529 if (pcr_index > SANDBOX_TPM_PCR_NB) {
530 printf("Sandbox TPM handles up to %d PCR(s)\n",
535 /* Check the number of hashes */
536 pcr_nb = get_unaligned_be32(sent);
537 sent += sizeof(pcr_nb);
539 printf("Sandbox cannot handle more than one PCR\n");
541 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
544 /* Check the hash algorithm */
545 alg = get_unaligned_be16(sent);
547 if (alg != TPM2_ALG_SHA256) {
548 printf("Sandbox TPM only handle SHA256 algorithm\n");
550 return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
554 rc = sandbox_tpm2_extend(dev, pcr_index, sent);
556 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
560 printf("TPM2 command %02x unknown in Sandbox\n", command);
561 rc = TPM2_RC_COMMAND_CODE;
562 sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
568 static int sandbox_tpm2_get_desc(struct udevice *dev, char *buf, int size)
573 return snprintf(buf, size, "Sandbox TPM2.x");
576 static int sandbox_tpm2_open(struct udevice *dev)
578 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
583 tpm->init_done = true;
588 static int sandbox_tpm2_probe(struct udevice *dev)
590 struct sandbox_tpm2 *tpm = dev_get_priv(dev);
591 struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
593 /* Use the TPM v2 stack */
594 priv->version = TPM_V2;
596 memset(tpm, 0, sizeof(*tpm));
598 priv->pcr_count = 32;
599 priv->pcr_select_min = 2;
604 static int sandbox_tpm2_close(struct udevice *dev)
609 static const struct tpm_ops sandbox_tpm2_ops = {
610 .open = sandbox_tpm2_open,
611 .close = sandbox_tpm2_close,
612 .get_desc = sandbox_tpm2_get_desc,
613 .xfer = sandbox_tpm2_xfer,
616 static const struct udevice_id sandbox_tpm2_ids[] = {
617 { .compatible = "sandbox,tpm2" },
621 U_BOOT_DRIVER(sandbox_tpm2) = {
622 .name = "sandbox_tpm2",
624 .of_match = sandbox_tpm2_ids,
625 .ops = &sandbox_tpm2_ops,
626 .probe = sandbox_tpm2_probe,
627 .priv_auto_alloc_size = sizeof(struct sandbox_tpm2),