tpm-v2.c 13 KB

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