tpm-v2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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-common.h>
  9. #include <tpm-v2.h>
  10. #include <linux/bitops.h>
  11. #include "tpm-utils.h"
  12. u32 tpm2_startup(struct udevice *dev, enum tpm2_startup_types mode)
  13. {
  14. const u8 command_v2[12] = {
  15. tpm_u16(TPM2_ST_NO_SESSIONS),
  16. tpm_u32(12),
  17. tpm_u32(TPM2_CC_STARTUP),
  18. tpm_u16(mode),
  19. };
  20. int ret;
  21. /*
  22. * Note TPM2_Startup command will return RC_SUCCESS the first time,
  23. * but will return RC_INITIALIZE otherwise.
  24. */
  25. ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  26. if (ret && ret != TPM2_RC_INITIALIZE)
  27. return ret;
  28. return 0;
  29. }
  30. u32 tpm2_self_test(struct udevice *dev, enum tpm2_yes_no full_test)
  31. {
  32. const u8 command_v2[12] = {
  33. tpm_u16(TPM2_ST_NO_SESSIONS),
  34. tpm_u32(11),
  35. tpm_u32(TPM2_CC_SELF_TEST),
  36. full_test,
  37. };
  38. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  39. }
  40. u32 tpm2_clear(struct udevice *dev, u32 handle, const char *pw,
  41. const ssize_t pw_sz)
  42. {
  43. /* Length of the message header, up to start of password */
  44. uint offset = 27;
  45. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  46. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  47. tpm_u32(offset + pw_sz), /* Length */
  48. tpm_u32(TPM2_CC_CLEAR), /* Command code */
  49. /* HANDLE */
  50. tpm_u32(handle), /* TPM resource handle */
  51. /* AUTH_SESSION */
  52. tpm_u32(9 + pw_sz), /* Authorization size */
  53. tpm_u32(TPM2_RS_PW), /* Session handle */
  54. tpm_u16(0), /* Size of <nonce> */
  55. /* <nonce> (if any) */
  56. 0, /* Attributes: Cont/Excl/Rst */
  57. tpm_u16(pw_sz), /* Size of <hmac/password> */
  58. /* STRING(pw) <hmac/password> (if any) */
  59. };
  60. int ret;
  61. /*
  62. * Fill the command structure starting from the first buffer:
  63. * - the password (if any)
  64. */
  65. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  66. offset, pw, pw_sz);
  67. offset += pw_sz;
  68. if (ret)
  69. return TPM_LIB_ERROR;
  70. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  71. }
  72. u32 tpm2_nv_define_space(struct udevice *dev, u32 space_index,
  73. size_t space_size, u32 nv_attributes,
  74. const u8 *nv_policy, size_t nv_policy_size)
  75. {
  76. /*
  77. * Calculate the offset of the nv_policy piece by adding each of the
  78. * chunks below.
  79. */
  80. uint offset = 10 + 8 + 13 + 14;
  81. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  82. /* header 10 bytes */
  83. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  84. tpm_u32(offset + nv_policy_size),/* Length */
  85. tpm_u32(TPM2_CC_NV_DEFINE_SPACE),/* Command code */
  86. /* handles 8 bytes */
  87. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  88. /* session header 13 bytes */
  89. tpm_u32(9), /* Header size */
  90. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  91. tpm_u16(0), /* nonce_size */
  92. 0, /* session_attrs */
  93. tpm_u16(0), /* auth_size */
  94. /* message 14 bytes + policy */
  95. tpm_u16(12 + nv_policy_size), /* size */
  96. tpm_u32(space_index),
  97. tpm_u16(TPM2_ALG_SHA256),
  98. tpm_u32(nv_attributes),
  99. tpm_u16(nv_policy_size),
  100. /* nv_policy */
  101. };
  102. int ret;
  103. /*
  104. * Fill the command structure starting from the first buffer:
  105. * - the password (if any)
  106. */
  107. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  108. offset, nv_policy, nv_policy_size);
  109. if (ret)
  110. return TPM_LIB_ERROR;
  111. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  112. }
  113. u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
  114. const u8 *digest, u32 digest_len)
  115. {
  116. /* Length of the message header, up to start of digest */
  117. uint offset = 33;
  118. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  119. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  120. tpm_u32(offset + digest_len), /* Length */
  121. tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
  122. /* HANDLE */
  123. tpm_u32(index), /* Handle (PCR Index) */
  124. /* AUTH_SESSION */
  125. tpm_u32(9), /* Authorization size */
  126. tpm_u32(TPM2_RS_PW), /* Session handle */
  127. tpm_u16(0), /* Size of <nonce> */
  128. /* <nonce> (if any) */
  129. 0, /* Attributes: Cont/Excl/Rst */
  130. tpm_u16(0), /* Size of <hmac/password> */
  131. /* <hmac/password> (if any) */
  132. /* hashes */
  133. tpm_u32(1), /* Count (number of hashes) */
  134. tpm_u16(algorithm), /* Algorithm of the hash */
  135. /* STRING(digest) Digest */
  136. };
  137. int ret;
  138. /*
  139. * Fill the command structure starting from the first buffer:
  140. * - the digest
  141. */
  142. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  143. offset, digest, digest_len);
  144. if (ret)
  145. return TPM_LIB_ERROR;
  146. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  147. }
  148. u32 tpm2_nv_read_value(struct udevice *dev, u32 index, void *data, u32 count)
  149. {
  150. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  151. /* header 10 bytes */
  152. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  153. tpm_u32(10 + 8 + 4 + 9 + 4), /* Length */
  154. tpm_u32(TPM2_CC_NV_READ), /* Command code */
  155. /* handles 8 bytes */
  156. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  157. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  158. /* AUTH_SESSION */
  159. tpm_u32(9), /* Authorization size */
  160. tpm_u32(TPM2_RS_PW), /* Session handle */
  161. tpm_u16(0), /* Size of <nonce> */
  162. /* <nonce> (if any) */
  163. 0, /* Attributes: Cont/Excl/Rst */
  164. tpm_u16(0), /* Size of <hmac/password> */
  165. /* <hmac/password> (if any) */
  166. tpm_u16(count), /* Number of bytes */
  167. tpm_u16(0), /* Offset */
  168. };
  169. size_t response_len = COMMAND_BUFFER_SIZE;
  170. u8 response[COMMAND_BUFFER_SIZE];
  171. int ret;
  172. u16 tag;
  173. u32 size, code;
  174. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  175. if (ret)
  176. return log_msg_ret("read", ret);
  177. if (unpack_byte_string(response, response_len, "wdds",
  178. 0, &tag, 2, &size, 6, &code,
  179. 16, data, count))
  180. return TPM_LIB_ERROR;
  181. return 0;
  182. }
  183. u32 tpm2_nv_write_value(struct udevice *dev, u32 index, const void *data,
  184. u32 count)
  185. {
  186. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  187. uint offset = 10 + 8 + 4 + 9 + 2;
  188. uint len = offset + count + 2;
  189. /* Use empty password auth if platform hierarchy is disabled */
  190. u32 auth = priv->plat_hier_disabled ? HR_NV_INDEX + index :
  191. TPM2_RH_PLATFORM;
  192. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  193. /* header 10 bytes */
  194. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  195. tpm_u32(len), /* Length */
  196. tpm_u32(TPM2_CC_NV_WRITE), /* Command code */
  197. /* handles 8 bytes */
  198. tpm_u32(auth), /* Primary platform seed */
  199. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  200. /* AUTH_SESSION */
  201. tpm_u32(9), /* Authorization size */
  202. tpm_u32(TPM2_RS_PW), /* Session handle */
  203. tpm_u16(0), /* Size of <nonce> */
  204. /* <nonce> (if any) */
  205. 0, /* Attributes: Cont/Excl/Rst */
  206. tpm_u16(0), /* Size of <hmac/password> */
  207. /* <hmac/password> (if any) */
  208. tpm_u16(count),
  209. };
  210. size_t response_len = COMMAND_BUFFER_SIZE;
  211. u8 response[COMMAND_BUFFER_SIZE];
  212. int ret;
  213. ret = pack_byte_string(command_v2, sizeof(command_v2), "sw",
  214. offset, data, count,
  215. offset + count, 0);
  216. if (ret)
  217. return TPM_LIB_ERROR;
  218. return tpm_sendrecv_command(dev, command_v2, response, &response_len);
  219. }
  220. u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
  221. u16 algorithm, void *data, u32 digest_len,
  222. unsigned int *updates)
  223. {
  224. u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
  225. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  226. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  227. tpm_u32(17 + idx_array_sz), /* Length */
  228. tpm_u32(TPM2_CC_PCR_READ), /* Command code */
  229. /* TPML_PCR_SELECTION */
  230. tpm_u32(1), /* Number of selections */
  231. tpm_u16(algorithm), /* Algorithm of the hash */
  232. idx_array_sz, /* Array size for selection */
  233. /* bitmap(idx) Selected PCR bitmap */
  234. };
  235. size_t response_len = COMMAND_BUFFER_SIZE;
  236. u8 response[COMMAND_BUFFER_SIZE];
  237. unsigned int pcr_sel_idx = idx / 8;
  238. u8 pcr_sel_bit = BIT(idx % 8);
  239. unsigned int counter = 0;
  240. int ret;
  241. if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
  242. 17 + pcr_sel_idx, pcr_sel_bit))
  243. return TPM_LIB_ERROR;
  244. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  245. if (ret)
  246. return ret;
  247. if (digest_len > response_len)
  248. return TPM_LIB_ERROR;
  249. if (unpack_byte_string(response, response_len, "ds",
  250. 10, &counter,
  251. response_len - digest_len, data,
  252. digest_len))
  253. return TPM_LIB_ERROR;
  254. if (updates)
  255. *updates = counter;
  256. return 0;
  257. }
  258. u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
  259. void *buf, size_t prop_count)
  260. {
  261. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  262. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  263. tpm_u32(22), /* Length */
  264. tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
  265. tpm_u32(capability), /* Capability */
  266. tpm_u32(property), /* Property */
  267. tpm_u32(prop_count), /* Property count */
  268. };
  269. u8 response[COMMAND_BUFFER_SIZE];
  270. size_t response_len = COMMAND_BUFFER_SIZE;
  271. unsigned int properties_off;
  272. int ret;
  273. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  274. if (ret)
  275. return ret;
  276. /*
  277. * In the response buffer, the properties are located after the:
  278. * tag (u16), response size (u32), response code (u32),
  279. * YES/NO flag (u8), TPM_CAP (u32).
  280. */
  281. properties_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
  282. sizeof(u8) + sizeof(u32);
  283. memcpy(buf, &response[properties_off], response_len - properties_off);
  284. return 0;
  285. }
  286. u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz)
  287. {
  288. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  289. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  290. tpm_u32(27 + pw_sz), /* Length */
  291. tpm_u32(TPM2_CC_DAM_RESET), /* Command code */
  292. /* HANDLE */
  293. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  294. /* AUTH_SESSION */
  295. tpm_u32(9 + pw_sz), /* Authorization size */
  296. tpm_u32(TPM2_RS_PW), /* Session handle */
  297. tpm_u16(0), /* Size of <nonce> */
  298. /* <nonce> (if any) */
  299. 0, /* Attributes: Cont/Excl/Rst */
  300. tpm_u16(pw_sz), /* Size of <hmac/password> */
  301. /* STRING(pw) <hmac/password> (if any) */
  302. };
  303. unsigned int offset = 27;
  304. int ret;
  305. /*
  306. * Fill the command structure starting from the first buffer:
  307. * - the password (if any)
  308. */
  309. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  310. offset, pw, pw_sz);
  311. offset += pw_sz;
  312. if (ret)
  313. return TPM_LIB_ERROR;
  314. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  315. }
  316. u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
  317. const ssize_t pw_sz, unsigned int max_tries,
  318. unsigned int recovery_time,
  319. unsigned int lockout_recovery)
  320. {
  321. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  322. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  323. tpm_u32(27 + pw_sz + 12), /* Length */
  324. tpm_u32(TPM2_CC_DAM_PARAMETERS), /* Command code */
  325. /* HANDLE */
  326. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  327. /* AUTH_SESSION */
  328. tpm_u32(9 + pw_sz), /* Authorization size */
  329. tpm_u32(TPM2_RS_PW), /* Session handle */
  330. tpm_u16(0), /* Size of <nonce> */
  331. /* <nonce> (if any) */
  332. 0, /* Attributes: Cont/Excl/Rst */
  333. tpm_u16(pw_sz), /* Size of <hmac/password> */
  334. /* STRING(pw) <hmac/password> (if any) */
  335. /* LOCKOUT PARAMETERS */
  336. /* tpm_u32(max_tries) Max tries (0, always lock) */
  337. /* tpm_u32(recovery_time) Recovery time (0, no lock) */
  338. /* tpm_u32(lockout_recovery) Lockout recovery */
  339. };
  340. unsigned int offset = 27;
  341. int ret;
  342. /*
  343. * Fill the command structure starting from the first buffer:
  344. * - the password (if any)
  345. * - max tries
  346. * - recovery time
  347. * - lockout recovery
  348. */
  349. ret = pack_byte_string(command_v2, sizeof(command_v2), "sddd",
  350. offset, pw, pw_sz,
  351. offset + pw_sz, max_tries,
  352. offset + pw_sz + 4, recovery_time,
  353. offset + pw_sz + 8, lockout_recovery);
  354. offset += pw_sz + 12;
  355. if (ret)
  356. return TPM_LIB_ERROR;
  357. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  358. }
  359. int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
  360. const ssize_t newpw_sz, const char *oldpw,
  361. const ssize_t oldpw_sz)
  362. {
  363. unsigned int offset = 27;
  364. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  365. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  366. tpm_u32(offset + oldpw_sz + 2 + newpw_sz), /* Length */
  367. tpm_u32(TPM2_CC_HIERCHANGEAUTH), /* Command code */
  368. /* HANDLE */
  369. tpm_u32(handle), /* TPM resource handle */
  370. /* AUTH_SESSION */
  371. tpm_u32(9 + oldpw_sz), /* Authorization size */
  372. tpm_u32(TPM2_RS_PW), /* Session handle */
  373. tpm_u16(0), /* Size of <nonce> */
  374. /* <nonce> (if any) */
  375. 0, /* Attributes: Cont/Excl/Rst */
  376. tpm_u16(oldpw_sz) /* Size of <hmac/password> */
  377. /* STRING(oldpw) <hmac/password> (if any) */
  378. /* TPM2B_AUTH (TPM2B_DIGEST) */
  379. /* tpm_u16(newpw_sz) Digest size, new pw length */
  380. /* STRING(newpw) Digest buffer, new pw */
  381. };
  382. int ret;
  383. /*
  384. * Fill the command structure starting from the first buffer:
  385. * - the old password (if any)
  386. * - size of the new password
  387. * - new password
  388. */
  389. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  390. offset, oldpw, oldpw_sz,
  391. offset + oldpw_sz, newpw_sz,
  392. offset + oldpw_sz + 2, newpw, newpw_sz);
  393. offset += oldpw_sz + 2 + newpw_sz;
  394. if (ret)
  395. return TPM_LIB_ERROR;
  396. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  397. }
  398. u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
  399. const ssize_t pw_sz, u32 index, const char *key)
  400. {
  401. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  402. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  403. tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  404. tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
  405. /* HANDLE */
  406. tpm_u32(TPM2_RH_PLATFORM), /* TPM resource handle */
  407. /* AUTH_SESSION */
  408. tpm_u32(9 + pw_sz), /* Authorization size */
  409. tpm_u32(TPM2_RS_PW), /* session handle */
  410. tpm_u16(0), /* Size of <nonce> */
  411. /* <nonce> (if any) */
  412. 0, /* Attributes: Cont/Excl/Rst */
  413. tpm_u16(pw_sz) /* Size of <hmac/password> */
  414. /* STRING(pw) <hmac/password> (if any) */
  415. /* TPM2B_AUTH (TPM2B_DIGEST) */
  416. /* tpm_u16(TPM2_DIGEST_LEN) Digest size length */
  417. /* STRING(key) Digest buffer (PCR key) */
  418. /* TPMI_ALG_HASH */
  419. /* tpm_u16(TPM2_ALG_SHA256) Algorithm of the hash */
  420. /* TPMI_DH_PCR */
  421. /* tpm_u32(index), PCR Index */
  422. };
  423. unsigned int offset = 27;
  424. int ret;
  425. /*
  426. * Fill the command structure starting from the first buffer:
  427. * - the password (if any)
  428. * - the PCR key length
  429. * - the PCR key
  430. * - the hash algorithm
  431. * - the PCR index
  432. */
  433. ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
  434. offset, pw, pw_sz,
  435. offset + pw_sz, TPM2_DIGEST_LEN,
  436. offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
  437. offset + pw_sz + 2 + TPM2_DIGEST_LEN,
  438. TPM2_ALG_SHA256,
  439. offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
  440. offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
  441. if (ret)
  442. return TPM_LIB_ERROR;
  443. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  444. }
  445. u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
  446. const ssize_t pw_sz, u32 index, const char *key,
  447. const ssize_t key_sz)
  448. {
  449. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  450. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  451. tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  452. tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
  453. /* HANDLE */
  454. tpm_u32(index), /* Handle (PCR Index) */
  455. /* AUTH_SESSION */
  456. tpm_u32(9 + pw_sz), /* Authorization size */
  457. tpm_u32(TPM2_RS_PW), /* session handle */
  458. tpm_u16(0), /* Size of <nonce> */
  459. /* <nonce> (if any) */
  460. 0, /* Attributes: Cont/Excl/Rst */
  461. tpm_u16(pw_sz), /* Size of <hmac/password> */
  462. /* STRING(pw) <hmac/password> (if any) */
  463. /* TPM2B_DIGEST */
  464. /* tpm_u16(key_sz) Key length */
  465. /* STRING(key) Key */
  466. };
  467. unsigned int offset = 27;
  468. int ret;
  469. /*
  470. * Fill the command structure starting from the first buffer:
  471. * - the password (if any)
  472. * - the number of digests, 1 in our case
  473. * - the algorithm, sha256 in our case
  474. * - the digest (64 bytes)
  475. */
  476. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  477. offset, pw, pw_sz,
  478. offset + pw_sz, key_sz,
  479. offset + pw_sz + 2, key, key_sz);
  480. offset += pw_sz + 2 + key_sz;
  481. if (ret)
  482. return TPM_LIB_ERROR;
  483. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  484. }
  485. u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
  486. {
  487. const u8 command_v2[10] = {
  488. tpm_u16(TPM2_ST_NO_SESSIONS),
  489. tpm_u32(12),
  490. tpm_u32(TPM2_CC_GET_RANDOM),
  491. };
  492. u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  493. const size_t data_size_offset = 10;
  494. const size_t data_offset = 12;
  495. size_t response_length = sizeof(response);
  496. u32 data_size;
  497. u8 *out = data;
  498. while (count > 0) {
  499. u32 this_bytes = min((size_t)count,
  500. sizeof(response) - data_offset);
  501. u32 err;
  502. if (pack_byte_string(buf, sizeof(buf), "sw",
  503. 0, command_v2, sizeof(command_v2),
  504. sizeof(command_v2), this_bytes))
  505. return TPM_LIB_ERROR;
  506. err = tpm_sendrecv_command(dev, buf, response,
  507. &response_length);
  508. if (err)
  509. return err;
  510. if (unpack_byte_string(response, response_length, "w",
  511. data_size_offset, &data_size))
  512. return TPM_LIB_ERROR;
  513. if (data_size > this_bytes)
  514. return TPM_LIB_ERROR;
  515. if (unpack_byte_string(response, response_length, "s",
  516. data_offset, out, data_size))
  517. return TPM_LIB_ERROR;
  518. count -= data_size;
  519. out += data_size;
  520. }
  521. return 0;
  522. }
  523. u32 tpm2_write_lock(struct udevice *dev, u32 index)
  524. {
  525. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  526. /* header 10 bytes */
  527. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  528. tpm_u32(10 + 8 + 13), /* Length */
  529. tpm_u32(TPM2_CC_NV_WRITELOCK), /* Command code */
  530. /* handles 8 bytes */
  531. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  532. tpm_u32(HR_NV_INDEX + index), /* Password authorisation */
  533. /* session header 9 bytes */
  534. tpm_u32(9), /* Header size */
  535. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  536. tpm_u16(0), /* nonce_size */
  537. 0, /* session_attrs */
  538. tpm_u16(0), /* auth_size */
  539. };
  540. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  541. }
  542. u32 tpm2_disable_platform_hierarchy(struct udevice *dev)
  543. {
  544. struct tpm_chip_priv *priv = dev_get_uclass_priv(dev);
  545. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  546. /* header 10 bytes */
  547. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  548. tpm_u32(10 + 4 + 13 + 5), /* Length */
  549. tpm_u32(TPM2_CC_HIER_CONTROL), /* Command code */
  550. /* 4 bytes */
  551. tpm_u32(TPM2_RH_PLATFORM), /* Primary platform seed */
  552. /* session header 9 bytes */
  553. tpm_u32(9), /* Header size */
  554. tpm_u32(TPM2_RS_PW), /* Password authorisation */
  555. tpm_u16(0), /* nonce_size */
  556. 0, /* session_attrs */
  557. tpm_u16(0), /* auth_size */
  558. /* payload 5 bytes */
  559. tpm_u32(TPM2_RH_PLATFORM), /* Hierarchy to disable */
  560. 0, /* 0=disable */
  561. };
  562. int ret;
  563. ret = tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  564. log_info("ret=%s, %x\n", dev->name, ret);
  565. if (ret)
  566. return ret;
  567. priv->plat_hier_disabled = true;
  568. return 0;
  569. }
  570. u32 tpm2_submit_command(struct udevice *dev, const u8 *sendbuf,
  571. u8 *recvbuf, size_t *recv_size)
  572. {
  573. return tpm_sendrecv_command(dev, sendbuf, recvbuf, recv_size);
  574. }