tpm2_tis_sandbox.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018, Bootlin
  4. * Author: Miquel Raynal <miquel.raynal@bootlin.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <tpm-v2.h>
  9. #include <asm/state.h>
  10. #include <asm/unaligned.h>
  11. #include <u-boot/crc.h>
  12. /* Hierarchies */
  13. enum tpm2_hierarchy {
  14. TPM2_HIERARCHY_LOCKOUT = 0,
  15. TPM2_HIERARCHY_ENDORSEMENT,
  16. TPM2_HIERARCHY_PLATFORM,
  17. TPM2_HIERARCHY_NB,
  18. };
  19. /* Subset of supported capabilities */
  20. enum tpm2_capability {
  21. TPM_CAP_TPM_PROPERTIES = 0x6,
  22. };
  23. /* Subset of supported properties */
  24. #define TPM2_PROPERTIES_OFFSET 0x0000020E
  25. enum tpm2_cap_tpm_property {
  26. TPM2_FAIL_COUNTER = 0,
  27. TPM2_PROP_MAX_TRIES,
  28. TPM2_RECOVERY_TIME,
  29. TPM2_LOCKOUT_RECOVERY,
  30. TPM2_PROPERTY_NB,
  31. };
  32. #define SANDBOX_TPM_PCR_NB 1
  33. static const u8 sandbox_extended_once_pcr[] = {
  34. 0xf5, 0xa5, 0xfd, 0x42, 0xd1, 0x6a, 0x20, 0x30,
  35. 0x27, 0x98, 0xef, 0x6e, 0xd3, 0x09, 0x97, 0x9b,
  36. 0x43, 0x00, 0x3d, 0x23, 0x20, 0xd9, 0xf0, 0xe8,
  37. 0xea, 0x98, 0x31, 0xa9, 0x27, 0x59, 0xfb, 0x4b,
  38. };
  39. struct sandbox_tpm2 {
  40. /* TPM internal states */
  41. bool init_done;
  42. bool startup_done;
  43. bool tests_done;
  44. /* TPM password per hierarchy */
  45. char pw[TPM2_HIERARCHY_NB][TPM2_DIGEST_LEN + 1];
  46. int pw_sz[TPM2_HIERARCHY_NB];
  47. /* TPM properties */
  48. u32 properties[TPM2_PROPERTY_NB];
  49. /* TPM PCRs */
  50. u8 pcr[SANDBOX_TPM_PCR_NB][TPM2_DIGEST_LEN];
  51. /* TPM PCR extensions */
  52. u32 pcr_extensions[SANDBOX_TPM_PCR_NB];
  53. };
  54. /*
  55. * Check the tag validity depending on the command (authentication required or
  56. * not). If authentication is required, check it is valid. Update the auth
  57. * pointer to point to the next chunk of data to process if needed.
  58. */
  59. static int sandbox_tpm2_check_session(struct udevice *dev, u32 command, u16 tag,
  60. const u8 **auth,
  61. enum tpm2_hierarchy *hierarchy)
  62. {
  63. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  64. u32 handle, auth_sz, session_handle;
  65. u16 nonce_sz, pw_sz;
  66. const char *pw;
  67. switch (command) {
  68. case TPM2_CC_STARTUP:
  69. case TPM2_CC_SELF_TEST:
  70. case TPM2_CC_GET_CAPABILITY:
  71. case TPM2_CC_PCR_READ:
  72. if (tag != TPM2_ST_NO_SESSIONS) {
  73. printf("No session required for command 0x%x\n",
  74. command);
  75. return TPM2_RC_BAD_TAG;
  76. }
  77. return 0;
  78. case TPM2_CC_CLEAR:
  79. case TPM2_CC_HIERCHANGEAUTH:
  80. case TPM2_CC_DAM_RESET:
  81. case TPM2_CC_DAM_PARAMETERS:
  82. case TPM2_CC_PCR_EXTEND:
  83. if (tag != TPM2_ST_SESSIONS) {
  84. printf("Session required for command 0x%x\n", command);
  85. return TPM2_RC_AUTH_CONTEXT;
  86. }
  87. handle = get_unaligned_be32(*auth);
  88. *auth += sizeof(handle);
  89. /*
  90. * PCR_Extend had a different protection mechanism and does not
  91. * use the same standards as other commands.
  92. */
  93. if (command == TPM2_CC_PCR_EXTEND)
  94. break;
  95. switch (handle) {
  96. case TPM2_RH_LOCKOUT:
  97. *hierarchy = TPM2_HIERARCHY_LOCKOUT;
  98. break;
  99. case TPM2_RH_ENDORSEMENT:
  100. if (command == TPM2_CC_CLEAR) {
  101. printf("Endorsement hierarchy unsupported\n");
  102. return TPM2_RC_AUTH_MISSING;
  103. }
  104. *hierarchy = TPM2_HIERARCHY_ENDORSEMENT;
  105. break;
  106. case TPM2_RH_PLATFORM:
  107. *hierarchy = TPM2_HIERARCHY_PLATFORM;
  108. break;
  109. default:
  110. printf("Wrong handle 0x%x\n", handle);
  111. return TPM2_RC_VALUE;
  112. }
  113. break;
  114. default:
  115. printf("Command code not recognized: 0x%x\n", command);
  116. return TPM2_RC_COMMAND_CODE;
  117. }
  118. auth_sz = get_unaligned_be32(*auth);
  119. *auth += sizeof(auth_sz);
  120. session_handle = get_unaligned_be32(*auth);
  121. *auth += sizeof(session_handle);
  122. if (session_handle != TPM2_RS_PW) {
  123. printf("Wrong session handle 0x%x\n", session_handle);
  124. return TPM2_RC_VALUE;
  125. }
  126. nonce_sz = get_unaligned_be16(*auth);
  127. *auth += sizeof(nonce_sz);
  128. if (nonce_sz) {
  129. printf("Nonces not supported in Sandbox, aborting\n");
  130. return TPM2_RC_HANDLE;
  131. }
  132. /* Ignore attributes */
  133. *auth += sizeof(u8);
  134. pw_sz = get_unaligned_be16(*auth);
  135. *auth += sizeof(pw_sz);
  136. if (auth_sz != (9 + nonce_sz + pw_sz)) {
  137. printf("Authentication size (%d) do not match %d\n",
  138. auth_sz, 9 + nonce_sz + pw_sz);
  139. return TPM2_RC_SIZE;
  140. }
  141. /* No passwork is acceptable */
  142. if (!pw_sz && !tpm->pw_sz[*hierarchy])
  143. return TPM2_RC_SUCCESS;
  144. /* Password is too long */
  145. if (pw_sz > TPM2_DIGEST_LEN) {
  146. printf("Password should not be more than %dB\n",
  147. TPM2_DIGEST_LEN);
  148. return TPM2_RC_AUTHSIZE;
  149. }
  150. pw = (const char *)*auth;
  151. *auth += pw_sz;
  152. /* Password is wrong */
  153. if (pw_sz != tpm->pw_sz[*hierarchy] ||
  154. strncmp(pw, tpm->pw[*hierarchy], tpm->pw_sz[*hierarchy])) {
  155. printf("Authentication failed: wrong password.\n");
  156. return TPM2_RC_BAD_AUTH;
  157. }
  158. return TPM2_RC_SUCCESS;
  159. }
  160. static int sandbox_tpm2_check_readyness(struct udevice *dev, int command)
  161. {
  162. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  163. switch (command) {
  164. case TPM2_CC_STARTUP:
  165. if (!tpm->init_done || tpm->startup_done)
  166. return TPM2_RC_INITIALIZE;
  167. break;
  168. case TPM2_CC_GET_CAPABILITY:
  169. if (!tpm->init_done || !tpm->startup_done)
  170. return TPM2_RC_INITIALIZE;
  171. break;
  172. case TPM2_CC_SELF_TEST:
  173. if (!tpm->startup_done)
  174. return TPM2_RC_INITIALIZE;
  175. break;
  176. default:
  177. if (!tpm->tests_done)
  178. return TPM2_RC_NEEDS_TEST;
  179. break;
  180. }
  181. return 0;
  182. }
  183. static int sandbox_tpm2_fill_buf(u8 *recv, size_t *recv_len, u16 tag, u32 rc)
  184. {
  185. *recv_len = sizeof(tag) + sizeof(u32) + sizeof(rc);
  186. /* Write tag */
  187. put_unaligned_be16(tag, recv);
  188. recv += sizeof(tag);
  189. /* Write length */
  190. put_unaligned_be32(*recv_len, recv);
  191. recv += sizeof(u32);
  192. /* Write return code */
  193. put_unaligned_be32(rc, recv);
  194. recv += sizeof(rc);
  195. /* Add trailing \0 */
  196. *recv = '\0';
  197. return 0;
  198. }
  199. static int sandbox_tpm2_extend(struct udevice *dev, int pcr_index,
  200. const u8 *extension)
  201. {
  202. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  203. int i;
  204. /* Only simulate the first extensions from all '0' with only '0' */
  205. for (i = 0; i < TPM2_DIGEST_LEN; i++)
  206. if (tpm->pcr[pcr_index][i] || extension[i])
  207. return TPM2_RC_FAILURE;
  208. memcpy(tpm->pcr[pcr_index], sandbox_extended_once_pcr,
  209. TPM2_DIGEST_LEN);
  210. tpm->pcr_extensions[pcr_index]++;
  211. return 0;
  212. };
  213. static int sandbox_tpm2_xfer(struct udevice *dev, const u8 *sendbuf,
  214. size_t send_size, u8 *recvbuf,
  215. size_t *recv_len)
  216. {
  217. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  218. enum tpm2_hierarchy hierarchy = 0;
  219. const u8 *sent = sendbuf;
  220. u8 *recv = recvbuf;
  221. u32 length, command, rc = 0;
  222. u16 tag, mode, new_pw_sz;
  223. u8 yes_no;
  224. int i, j;
  225. /* TPM2_GetProperty */
  226. u32 capability, property, property_count;
  227. /* TPM2_PCR_Read/Extend variables */
  228. int pcr_index = 0;
  229. u64 pcr_map = 0;
  230. u32 selections, pcr_nb;
  231. u16 alg;
  232. u8 pcr_array_sz;
  233. tag = get_unaligned_be16(sent);
  234. sent += sizeof(tag);
  235. length = get_unaligned_be32(sent);
  236. sent += sizeof(length);
  237. if (length != send_size) {
  238. printf("TPM2: Unmatching length, received: %ld, expected: %d\n",
  239. send_size, length);
  240. rc = TPM2_RC_SIZE;
  241. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  242. return 0;
  243. }
  244. command = get_unaligned_be32(sent);
  245. sent += sizeof(command);
  246. rc = sandbox_tpm2_check_readyness(dev, command);
  247. if (rc) {
  248. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  249. return 0;
  250. }
  251. rc = sandbox_tpm2_check_session(dev, command, tag, &sent, &hierarchy);
  252. if (rc) {
  253. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  254. return 0;
  255. }
  256. switch (command) {
  257. case TPM2_CC_STARTUP:
  258. mode = get_unaligned_be16(sent);
  259. sent += sizeof(mode);
  260. switch (mode) {
  261. case TPM2_SU_CLEAR:
  262. case TPM2_SU_STATE:
  263. break;
  264. default:
  265. rc = TPM2_RC_VALUE;
  266. }
  267. tpm->startup_done = true;
  268. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  269. break;
  270. case TPM2_CC_SELF_TEST:
  271. yes_no = *sent;
  272. sent += sizeof(yes_no);
  273. switch (yes_no) {
  274. case TPMI_YES:
  275. case TPMI_NO:
  276. break;
  277. default:
  278. rc = TPM2_RC_VALUE;
  279. }
  280. tpm->tests_done = true;
  281. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  282. break;
  283. case TPM2_CC_CLEAR:
  284. /* Reset this hierarchy password */
  285. tpm->pw_sz[hierarchy] = 0;
  286. /* Reset all password if thisis the PLATFORM hierarchy */
  287. if (hierarchy == TPM2_HIERARCHY_PLATFORM)
  288. for (i = 0; i < TPM2_HIERARCHY_NB; i++)
  289. tpm->pw_sz[i] = 0;
  290. /* Reset the properties */
  291. for (i = 0; i < TPM2_PROPERTY_NB; i++)
  292. tpm->properties[i] = 0;
  293. /* Reset the PCRs and their number of extensions */
  294. for (i = 0; i < SANDBOX_TPM_PCR_NB; i++) {
  295. tpm->pcr_extensions[i] = 0;
  296. for (j = 0; j < TPM2_DIGEST_LEN; j++)
  297. tpm->pcr[i][j] = 0;
  298. }
  299. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  300. break;
  301. case TPM2_CC_HIERCHANGEAUTH:
  302. new_pw_sz = get_unaligned_be16(sent);
  303. sent += sizeof(new_pw_sz);
  304. if (new_pw_sz > TPM2_DIGEST_LEN) {
  305. rc = TPM2_RC_SIZE;
  306. } else if (new_pw_sz) {
  307. tpm->pw_sz[hierarchy] = new_pw_sz;
  308. memcpy(tpm->pw[hierarchy], sent, new_pw_sz);
  309. sent += new_pw_sz;
  310. }
  311. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  312. break;
  313. case TPM2_CC_GET_CAPABILITY:
  314. capability = get_unaligned_be32(sent);
  315. sent += sizeof(capability);
  316. if (capability != TPM_CAP_TPM_PROPERTIES) {
  317. printf("Sandbox TPM only support TPM_CAPABILITIES\n");
  318. return TPM2_RC_HANDLE;
  319. }
  320. property = get_unaligned_be32(sent);
  321. sent += sizeof(property);
  322. property -= TPM2_PROPERTIES_OFFSET;
  323. property_count = get_unaligned_be32(sent);
  324. sent += sizeof(property_count);
  325. if (!property_count ||
  326. property + property_count > TPM2_PROPERTY_NB) {
  327. rc = TPM2_RC_HANDLE;
  328. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  329. }
  330. /* Write tag */
  331. put_unaligned_be16(tag, recv);
  332. recv += sizeof(tag);
  333. /* Ignore length for now */
  334. recv += sizeof(u32);
  335. /* Write return code */
  336. put_unaligned_be32(rc, recv);
  337. recv += sizeof(rc);
  338. /* Tell there is more data to read */
  339. *recv = TPMI_YES;
  340. recv += sizeof(yes_no);
  341. /* Repeat the capability */
  342. put_unaligned_be32(capability, recv);
  343. recv += sizeof(capability);
  344. /* Give the number of properties that follow */
  345. put_unaligned_be32(property_count, recv);
  346. recv += sizeof(property_count);
  347. /* Fill with the properties */
  348. for (i = 0; i < property_count; i++) {
  349. put_unaligned_be32(TPM2_PROPERTIES_OFFSET + property +
  350. i, recv);
  351. recv += sizeof(property);
  352. put_unaligned_be32(tpm->properties[property + i],
  353. recv);
  354. recv += sizeof(property);
  355. }
  356. /* Add trailing \0 */
  357. *recv = '\0';
  358. /* Write response length */
  359. *recv_len = recv - recvbuf;
  360. put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
  361. break;
  362. case TPM2_CC_DAM_PARAMETERS:
  363. tpm->properties[TPM2_PROP_MAX_TRIES] = get_unaligned_be32(sent);
  364. sent += sizeof(*tpm->properties);
  365. tpm->properties[TPM2_RECOVERY_TIME] = get_unaligned_be32(sent);
  366. sent += sizeof(*tpm->properties);
  367. tpm->properties[TPM2_LOCKOUT_RECOVERY] = get_unaligned_be32(sent);
  368. sent += sizeof(*tpm->properties);
  369. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  370. break;
  371. case TPM2_CC_PCR_READ:
  372. selections = get_unaligned_be32(sent);
  373. sent += sizeof(selections);
  374. if (selections != 1) {
  375. printf("Sandbox cannot handle more than one PCR\n");
  376. rc = TPM2_RC_VALUE;
  377. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  378. }
  379. alg = get_unaligned_be16(sent);
  380. sent += sizeof(alg);
  381. if (alg != TPM2_ALG_SHA256) {
  382. printf("Sandbox TPM only handle SHA256 algorithm\n");
  383. rc = TPM2_RC_VALUE;
  384. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  385. }
  386. pcr_array_sz = *sent;
  387. sent += sizeof(pcr_array_sz);
  388. if (!pcr_array_sz || pcr_array_sz > 8) {
  389. printf("Sandbox TPM cannot handle so much PCRs\n");
  390. rc = TPM2_RC_VALUE;
  391. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  392. }
  393. for (i = 0; i < pcr_array_sz; i++)
  394. pcr_map += (u64)sent[i] << (i * 8);
  395. if (pcr_map >> SANDBOX_TPM_PCR_NB) {
  396. printf("Sandbox TPM handles up to %d PCR(s)\n",
  397. SANDBOX_TPM_PCR_NB);
  398. rc = TPM2_RC_VALUE;
  399. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  400. }
  401. if (!pcr_map) {
  402. printf("Empty PCR map.\n");
  403. rc = TPM2_RC_VALUE;
  404. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  405. }
  406. for (i = 0; i < SANDBOX_TPM_PCR_NB; i++)
  407. if (pcr_map & BIT(i))
  408. pcr_index = i;
  409. /* Write tag */
  410. put_unaligned_be16(tag, recv);
  411. recv += sizeof(tag);
  412. /* Ignore length for now */
  413. recv += sizeof(u32);
  414. /* Write return code */
  415. put_unaligned_be32(rc, recv);
  416. recv += sizeof(rc);
  417. /* Number of extensions */
  418. put_unaligned_be32(tpm->pcr_extensions[pcr_index], recv);
  419. recv += sizeof(u32);
  420. /* Copy the PCR */
  421. memcpy(recv, tpm->pcr[pcr_index], TPM2_DIGEST_LEN);
  422. recv += TPM2_DIGEST_LEN;
  423. /* Add trailing \0 */
  424. *recv = '\0';
  425. /* Write response length */
  426. *recv_len = recv - recvbuf;
  427. put_unaligned_be32(*recv_len, recvbuf + sizeof(tag));
  428. break;
  429. case TPM2_CC_PCR_EXTEND:
  430. /* Get the PCR index */
  431. pcr_index = get_unaligned_be32(sendbuf + sizeof(tag) +
  432. sizeof(length) +
  433. sizeof(command));
  434. if (pcr_index > SANDBOX_TPM_PCR_NB) {
  435. printf("Sandbox TPM handles up to %d PCR(s)\n",
  436. SANDBOX_TPM_PCR_NB);
  437. rc = TPM2_RC_VALUE;
  438. }
  439. /* Check the number of hashes */
  440. pcr_nb = get_unaligned_be32(sent);
  441. sent += sizeof(pcr_nb);
  442. if (pcr_nb != 1) {
  443. printf("Sandbox cannot handle more than one PCR\n");
  444. rc = TPM2_RC_VALUE;
  445. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  446. }
  447. /* Check the hash algorithm */
  448. alg = get_unaligned_be16(sent);
  449. sent += sizeof(alg);
  450. if (alg != TPM2_ALG_SHA256) {
  451. printf("Sandbox TPM only handle SHA256 algorithm\n");
  452. rc = TPM2_RC_VALUE;
  453. return sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  454. }
  455. /* Extend the PCR */
  456. rc = sandbox_tpm2_extend(dev, pcr_index, sent);
  457. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  458. break;
  459. default:
  460. printf("TPM2 command %02x unknown in Sandbox\n", command);
  461. rc = TPM2_RC_COMMAND_CODE;
  462. sandbox_tpm2_fill_buf(recv, recv_len, tag, rc);
  463. }
  464. return 0;
  465. }
  466. static int sandbox_tpm2_get_desc(struct udevice *dev, char *buf, int size)
  467. {
  468. if (size < 15)
  469. return -ENOSPC;
  470. return snprintf(buf, size, "Sandbox TPM2.x");
  471. }
  472. static int sandbox_tpm2_open(struct udevice *dev)
  473. {
  474. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  475. if (tpm->init_done)
  476. return -EIO;
  477. tpm->init_done = true;
  478. return 0;
  479. }
  480. static int sandbox_tpm2_probe(struct udevice *dev)
  481. {
  482. struct sandbox_tpm2 *tpm = dev_get_priv(dev);
  483. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  484. /* Use the TPM v2 stack */
  485. priv->version = TPM_V2;
  486. memset(tpm, 0, sizeof(*tpm));
  487. priv->pcr_count = 32;
  488. priv->pcr_select_min = 2;
  489. return 0;
  490. }
  491. static int sandbox_tpm2_close(struct udevice *dev)
  492. {
  493. return 0;
  494. }
  495. static const struct tpm_ops sandbox_tpm2_ops = {
  496. .open = sandbox_tpm2_open,
  497. .close = sandbox_tpm2_close,
  498. .get_desc = sandbox_tpm2_get_desc,
  499. .xfer = sandbox_tpm2_xfer,
  500. };
  501. static const struct udevice_id sandbox_tpm2_ids[] = {
  502. { .compatible = "sandbox,tpm2" },
  503. { }
  504. };
  505. U_BOOT_DRIVER(sandbox_tpm2) = {
  506. .name = "sandbox_tpm2",
  507. .id = UCLASS_TPM,
  508. .of_match = sandbox_tpm2_ids,
  509. .ops = &sandbox_tpm2_ops,
  510. .probe = sandbox_tpm2_probe,
  511. .priv_auto_alloc_size = sizeof(struct sandbox_tpm2),
  512. };