tpm-v2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  44. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  45. tpm_u32(27 + pw_sz), /* Length */
  46. tpm_u32(TPM2_CC_CLEAR), /* Command code */
  47. /* HANDLE */
  48. tpm_u32(handle), /* TPM resource handle */
  49. /* AUTH_SESSION */
  50. tpm_u32(9 + pw_sz), /* Authorization size */
  51. tpm_u32(TPM2_RS_PW), /* Session handle */
  52. tpm_u16(0), /* Size of <nonce> */
  53. /* <nonce> (if any) */
  54. 0, /* Attributes: Cont/Excl/Rst */
  55. tpm_u16(pw_sz), /* Size of <hmac/password> */
  56. /* STRING(pw) <hmac/password> (if any) */
  57. };
  58. unsigned int offset = 27;
  59. int ret;
  60. /*
  61. * Fill the command structure starting from the first buffer:
  62. * - the password (if any)
  63. */
  64. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  65. offset, pw, pw_sz);
  66. offset += pw_sz;
  67. if (ret)
  68. return TPM_LIB_ERROR;
  69. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  70. }
  71. u32 tpm2_pcr_extend(struct udevice *dev, u32 index, u32 algorithm,
  72. const u8 *digest, u32 digest_len)
  73. {
  74. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  75. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  76. tpm_u32(33 + digest_len), /* Length */
  77. tpm_u32(TPM2_CC_PCR_EXTEND), /* Command code */
  78. /* HANDLE */
  79. tpm_u32(index), /* Handle (PCR Index) */
  80. /* AUTH_SESSION */
  81. tpm_u32(9), /* Authorization size */
  82. tpm_u32(TPM2_RS_PW), /* Session handle */
  83. tpm_u16(0), /* Size of <nonce> */
  84. /* <nonce> (if any) */
  85. 0, /* Attributes: Cont/Excl/Rst */
  86. tpm_u16(0), /* Size of <hmac/password> */
  87. /* <hmac/password> (if any) */
  88. tpm_u32(1), /* Count (number of hashes) */
  89. tpm_u16(algorithm), /* Algorithm of the hash */
  90. /* STRING(digest) Digest */
  91. };
  92. unsigned int offset = 33;
  93. int ret;
  94. /*
  95. * Fill the command structure starting from the first buffer:
  96. * - the digest
  97. */
  98. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  99. offset, digest, digest_len);
  100. offset += digest_len;
  101. if (ret)
  102. return TPM_LIB_ERROR;
  103. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  104. }
  105. u32 tpm2_pcr_read(struct udevice *dev, u32 idx, unsigned int idx_min_sz,
  106. void *data, unsigned int *updates)
  107. {
  108. u8 idx_array_sz = max(idx_min_sz, DIV_ROUND_UP(idx, 8));
  109. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  110. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  111. tpm_u32(17 + idx_array_sz), /* Length */
  112. tpm_u32(TPM2_CC_PCR_READ), /* Command code */
  113. /* TPML_PCR_SELECTION */
  114. tpm_u32(1), /* Number of selections */
  115. tpm_u16(TPM2_ALG_SHA256), /* Algorithm of the hash */
  116. idx_array_sz, /* Array size for selection */
  117. /* bitmap(idx) Selected PCR bitmap */
  118. };
  119. size_t response_len = COMMAND_BUFFER_SIZE;
  120. u8 response[COMMAND_BUFFER_SIZE];
  121. unsigned int pcr_sel_idx = idx / 8;
  122. u8 pcr_sel_bit = BIT(idx % 8);
  123. unsigned int counter = 0;
  124. int ret;
  125. if (pack_byte_string(command_v2, COMMAND_BUFFER_SIZE, "b",
  126. 17 + pcr_sel_idx, pcr_sel_bit))
  127. return TPM_LIB_ERROR;
  128. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  129. if (ret)
  130. return ret;
  131. if (unpack_byte_string(response, response_len, "ds",
  132. 10, &counter,
  133. response_len - TPM2_DIGEST_LEN, data,
  134. TPM2_DIGEST_LEN))
  135. return TPM_LIB_ERROR;
  136. if (updates)
  137. *updates = counter;
  138. return 0;
  139. }
  140. u32 tpm2_get_capability(struct udevice *dev, u32 capability, u32 property,
  141. void *buf, size_t prop_count)
  142. {
  143. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  144. tpm_u16(TPM2_ST_NO_SESSIONS), /* TAG */
  145. tpm_u32(22), /* Length */
  146. tpm_u32(TPM2_CC_GET_CAPABILITY), /* Command code */
  147. tpm_u32(capability), /* Capability */
  148. tpm_u32(property), /* Property */
  149. tpm_u32(prop_count), /* Property count */
  150. };
  151. u8 response[COMMAND_BUFFER_SIZE];
  152. size_t response_len = COMMAND_BUFFER_SIZE;
  153. unsigned int properties_off;
  154. int ret;
  155. ret = tpm_sendrecv_command(dev, command_v2, response, &response_len);
  156. if (ret)
  157. return ret;
  158. /*
  159. * In the response buffer, the properties are located after the:
  160. * tag (u16), response size (u32), response code (u32),
  161. * YES/NO flag (u8), TPM_CAP (u32).
  162. */
  163. properties_off = sizeof(u16) + sizeof(u32) + sizeof(u32) +
  164. sizeof(u8) + sizeof(u32);
  165. memcpy(buf, &response[properties_off], response_len - properties_off);
  166. return 0;
  167. }
  168. u32 tpm2_dam_reset(struct udevice *dev, const char *pw, const ssize_t pw_sz)
  169. {
  170. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  171. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  172. tpm_u32(27 + pw_sz), /* Length */
  173. tpm_u32(TPM2_CC_DAM_RESET), /* Command code */
  174. /* HANDLE */
  175. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  176. /* AUTH_SESSION */
  177. tpm_u32(9 + pw_sz), /* Authorization size */
  178. tpm_u32(TPM2_RS_PW), /* Session handle */
  179. tpm_u16(0), /* Size of <nonce> */
  180. /* <nonce> (if any) */
  181. 0, /* Attributes: Cont/Excl/Rst */
  182. tpm_u16(pw_sz), /* Size of <hmac/password> */
  183. /* STRING(pw) <hmac/password> (if any) */
  184. };
  185. unsigned int offset = 27;
  186. int ret;
  187. /*
  188. * Fill the command structure starting from the first buffer:
  189. * - the password (if any)
  190. */
  191. ret = pack_byte_string(command_v2, sizeof(command_v2), "s",
  192. offset, pw, pw_sz);
  193. offset += pw_sz;
  194. if (ret)
  195. return TPM_LIB_ERROR;
  196. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  197. }
  198. u32 tpm2_dam_parameters(struct udevice *dev, const char *pw,
  199. const ssize_t pw_sz, unsigned int max_tries,
  200. unsigned int recovery_time,
  201. unsigned int lockout_recovery)
  202. {
  203. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  204. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  205. tpm_u32(27 + pw_sz + 12), /* Length */
  206. tpm_u32(TPM2_CC_DAM_PARAMETERS), /* Command code */
  207. /* HANDLE */
  208. tpm_u32(TPM2_RH_LOCKOUT), /* TPM resource handle */
  209. /* AUTH_SESSION */
  210. tpm_u32(9 + pw_sz), /* Authorization size */
  211. tpm_u32(TPM2_RS_PW), /* Session handle */
  212. tpm_u16(0), /* Size of <nonce> */
  213. /* <nonce> (if any) */
  214. 0, /* Attributes: Cont/Excl/Rst */
  215. tpm_u16(pw_sz), /* Size of <hmac/password> */
  216. /* STRING(pw) <hmac/password> (if any) */
  217. /* LOCKOUT PARAMETERS */
  218. /* tpm_u32(max_tries) Max tries (0, always lock) */
  219. /* tpm_u32(recovery_time) Recovery time (0, no lock) */
  220. /* tpm_u32(lockout_recovery) Lockout recovery */
  221. };
  222. unsigned int offset = 27;
  223. int ret;
  224. /*
  225. * Fill the command structure starting from the first buffer:
  226. * - the password (if any)
  227. * - max tries
  228. * - recovery time
  229. * - lockout recovery
  230. */
  231. ret = pack_byte_string(command_v2, sizeof(command_v2), "sddd",
  232. offset, pw, pw_sz,
  233. offset + pw_sz, max_tries,
  234. offset + pw_sz + 4, recovery_time,
  235. offset + pw_sz + 8, lockout_recovery);
  236. offset += pw_sz + 12;
  237. if (ret)
  238. return TPM_LIB_ERROR;
  239. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  240. }
  241. int tpm2_change_auth(struct udevice *dev, u32 handle, const char *newpw,
  242. const ssize_t newpw_sz, const char *oldpw,
  243. const ssize_t oldpw_sz)
  244. {
  245. unsigned int offset = 27;
  246. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  247. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  248. tpm_u32(offset + oldpw_sz + 2 + newpw_sz), /* Length */
  249. tpm_u32(TPM2_CC_HIERCHANGEAUTH), /* Command code */
  250. /* HANDLE */
  251. tpm_u32(handle), /* TPM resource handle */
  252. /* AUTH_SESSION */
  253. tpm_u32(9 + oldpw_sz), /* Authorization size */
  254. tpm_u32(TPM2_RS_PW), /* Session handle */
  255. tpm_u16(0), /* Size of <nonce> */
  256. /* <nonce> (if any) */
  257. 0, /* Attributes: Cont/Excl/Rst */
  258. tpm_u16(oldpw_sz) /* Size of <hmac/password> */
  259. /* STRING(oldpw) <hmac/password> (if any) */
  260. /* TPM2B_AUTH (TPM2B_DIGEST) */
  261. /* tpm_u16(newpw_sz) Digest size, new pw length */
  262. /* STRING(newpw) Digest buffer, new pw */
  263. };
  264. int ret;
  265. /*
  266. * Fill the command structure starting from the first buffer:
  267. * - the old password (if any)
  268. * - size of the new password
  269. * - new password
  270. */
  271. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  272. offset, oldpw, oldpw_sz,
  273. offset + oldpw_sz, newpw_sz,
  274. offset + oldpw_sz + 2, newpw, newpw_sz);
  275. offset += oldpw_sz + 2 + newpw_sz;
  276. if (ret)
  277. return TPM_LIB_ERROR;
  278. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  279. }
  280. u32 tpm2_pcr_setauthpolicy(struct udevice *dev, const char *pw,
  281. const ssize_t pw_sz, u32 index, const char *key)
  282. {
  283. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  284. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  285. tpm_u32(35 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  286. tpm_u32(TPM2_CC_PCR_SETAUTHPOL), /* Command code */
  287. /* HANDLE */
  288. tpm_u32(TPM2_RH_PLATFORM), /* TPM resource handle */
  289. /* AUTH_SESSION */
  290. tpm_u32(9 + pw_sz), /* Authorization size */
  291. tpm_u32(TPM2_RS_PW), /* session handle */
  292. tpm_u16(0), /* Size of <nonce> */
  293. /* <nonce> (if any) */
  294. 0, /* Attributes: Cont/Excl/Rst */
  295. tpm_u16(pw_sz) /* Size of <hmac/password> */
  296. /* STRING(pw) <hmac/password> (if any) */
  297. /* TPM2B_AUTH (TPM2B_DIGEST) */
  298. /* tpm_u16(TPM2_DIGEST_LEN) Digest size length */
  299. /* STRING(key) Digest buffer (PCR key) */
  300. /* TPMI_ALG_HASH */
  301. /* tpm_u16(TPM2_ALG_SHA256) Algorithm of the hash */
  302. /* TPMI_DH_PCR */
  303. /* tpm_u32(index), PCR Index */
  304. };
  305. unsigned int offset = 27;
  306. int ret;
  307. /*
  308. * Fill the command structure starting from the first buffer:
  309. * - the password (if any)
  310. * - the PCR key length
  311. * - the PCR key
  312. * - the hash algorithm
  313. * - the PCR index
  314. */
  315. ret = pack_byte_string(command_v2, sizeof(command_v2), "swswd",
  316. offset, pw, pw_sz,
  317. offset + pw_sz, TPM2_DIGEST_LEN,
  318. offset + pw_sz + 2, key, TPM2_DIGEST_LEN,
  319. offset + pw_sz + 2 + TPM2_DIGEST_LEN,
  320. TPM2_ALG_SHA256,
  321. offset + pw_sz + 4 + TPM2_DIGEST_LEN, index);
  322. offset += pw_sz + 2 + TPM2_DIGEST_LEN + 2 + 4;
  323. if (ret)
  324. return TPM_LIB_ERROR;
  325. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  326. }
  327. u32 tpm2_pcr_setauthvalue(struct udevice *dev, const char *pw,
  328. const ssize_t pw_sz, u32 index, const char *key,
  329. const ssize_t key_sz)
  330. {
  331. u8 command_v2[COMMAND_BUFFER_SIZE] = {
  332. tpm_u16(TPM2_ST_SESSIONS), /* TAG */
  333. tpm_u32(33 + pw_sz + TPM2_DIGEST_LEN), /* Length */
  334. tpm_u32(TPM2_CC_PCR_SETAUTHVAL), /* Command code */
  335. /* HANDLE */
  336. tpm_u32(index), /* Handle (PCR Index) */
  337. /* AUTH_SESSION */
  338. tpm_u32(9 + pw_sz), /* Authorization size */
  339. tpm_u32(TPM2_RS_PW), /* session handle */
  340. tpm_u16(0), /* Size of <nonce> */
  341. /* <nonce> (if any) */
  342. 0, /* Attributes: Cont/Excl/Rst */
  343. tpm_u16(pw_sz), /* Size of <hmac/password> */
  344. /* STRING(pw) <hmac/password> (if any) */
  345. /* TPM2B_DIGEST */
  346. /* tpm_u16(key_sz) Key length */
  347. /* STRING(key) Key */
  348. };
  349. unsigned int offset = 27;
  350. int ret;
  351. /*
  352. * Fill the command structure starting from the first buffer:
  353. * - the password (if any)
  354. * - the number of digests, 1 in our case
  355. * - the algorithm, sha256 in our case
  356. * - the digest (64 bytes)
  357. */
  358. ret = pack_byte_string(command_v2, sizeof(command_v2), "sws",
  359. offset, pw, pw_sz,
  360. offset + pw_sz, key_sz,
  361. offset + pw_sz + 2, key, key_sz);
  362. offset += pw_sz + 2 + key_sz;
  363. if (ret)
  364. return TPM_LIB_ERROR;
  365. return tpm_sendrecv_command(dev, command_v2, NULL, NULL);
  366. }
  367. u32 tpm2_get_random(struct udevice *dev, void *data, u32 count)
  368. {
  369. const u8 command_v2[10] = {
  370. tpm_u16(TPM2_ST_NO_SESSIONS),
  371. tpm_u32(12),
  372. tpm_u32(TPM2_CC_GET_RANDOM),
  373. };
  374. u8 buf[COMMAND_BUFFER_SIZE], response[COMMAND_BUFFER_SIZE];
  375. const size_t data_size_offset = 10;
  376. const size_t data_offset = 12;
  377. size_t response_length = sizeof(response);
  378. u32 data_size;
  379. u8 *out = data;
  380. while (count > 0) {
  381. u32 this_bytes = min((size_t)count,
  382. sizeof(response) - data_offset);
  383. u32 err;
  384. if (pack_byte_string(buf, sizeof(buf), "sw",
  385. 0, command_v2, sizeof(command_v2),
  386. sizeof(command_v2), this_bytes))
  387. return TPM_LIB_ERROR;
  388. err = tpm_sendrecv_command(dev, buf, response,
  389. &response_length);
  390. if (err)
  391. return err;
  392. if (unpack_byte_string(response, response_length, "w",
  393. data_size_offset, &data_size))
  394. return TPM_LIB_ERROR;
  395. if (data_size > this_bytes)
  396. return TPM_LIB_ERROR;
  397. if (unpack_byte_string(response, response_length, "s",
  398. data_offset, out, data_size))
  399. return TPM_LIB_ERROR;
  400. count -= data_size;
  401. out += data_size;
  402. }
  403. return 0;
  404. }