trusted_tpm1.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2010 IBM Corporation
  4. * Copyright (c) 2019-2021, Linaro Limited
  5. *
  6. * See Documentation/security/keys/trusted-encrypted.rst
  7. */
  8. #include <crypto/hash_info.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/parser.h>
  12. #include <linux/string.h>
  13. #include <linux/err.h>
  14. #include <keys/trusted-type.h>
  15. #include <linux/key-type.h>
  16. #include <linux/crypto.h>
  17. #include <crypto/hash.h>
  18. #include <crypto/sha1.h>
  19. #include <linux/tpm.h>
  20. #include <linux/tpm_command.h>
  21. #include <keys/trusted_tpm.h>
  22. static const char hmac_alg[] = "hmac(sha1)";
  23. static const char hash_alg[] = "sha1";
  24. static struct tpm_chip *chip;
  25. static struct tpm_digest *digests;
  26. struct sdesc {
  27. struct shash_desc shash;
  28. char ctx[];
  29. };
  30. static struct crypto_shash *hashalg;
  31. static struct crypto_shash *hmacalg;
  32. static struct sdesc *init_sdesc(struct crypto_shash *alg)
  33. {
  34. struct sdesc *sdesc;
  35. int size;
  36. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  37. sdesc = kmalloc(size, GFP_KERNEL);
  38. if (!sdesc)
  39. return ERR_PTR(-ENOMEM);
  40. sdesc->shash.tfm = alg;
  41. return sdesc;
  42. }
  43. static int TSS_sha1(const unsigned char *data, unsigned int datalen,
  44. unsigned char *digest)
  45. {
  46. struct sdesc *sdesc;
  47. int ret;
  48. sdesc = init_sdesc(hashalg);
  49. if (IS_ERR(sdesc)) {
  50. pr_info("can't alloc %s\n", hash_alg);
  51. return PTR_ERR(sdesc);
  52. }
  53. ret = crypto_shash_digest(&sdesc->shash, data, datalen, digest);
  54. kfree_sensitive(sdesc);
  55. return ret;
  56. }
  57. static int TSS_rawhmac(unsigned char *digest, const unsigned char *key,
  58. unsigned int keylen, ...)
  59. {
  60. struct sdesc *sdesc;
  61. va_list argp;
  62. unsigned int dlen;
  63. unsigned char *data;
  64. int ret;
  65. sdesc = init_sdesc(hmacalg);
  66. if (IS_ERR(sdesc)) {
  67. pr_info("can't alloc %s\n", hmac_alg);
  68. return PTR_ERR(sdesc);
  69. }
  70. ret = crypto_shash_setkey(hmacalg, key, keylen);
  71. if (ret < 0)
  72. goto out;
  73. ret = crypto_shash_init(&sdesc->shash);
  74. if (ret < 0)
  75. goto out;
  76. va_start(argp, keylen);
  77. for (;;) {
  78. dlen = va_arg(argp, unsigned int);
  79. if (dlen == 0)
  80. break;
  81. data = va_arg(argp, unsigned char *);
  82. if (data == NULL) {
  83. ret = -EINVAL;
  84. break;
  85. }
  86. ret = crypto_shash_update(&sdesc->shash, data, dlen);
  87. if (ret < 0)
  88. break;
  89. }
  90. va_end(argp);
  91. if (!ret)
  92. ret = crypto_shash_final(&sdesc->shash, digest);
  93. out:
  94. kfree_sensitive(sdesc);
  95. return ret;
  96. }
  97. /*
  98. * calculate authorization info fields to send to TPM
  99. */
  100. int TSS_authhmac(unsigned char *digest, const unsigned char *key,
  101. unsigned int keylen, unsigned char *h1,
  102. unsigned char *h2, unsigned int h3, ...)
  103. {
  104. unsigned char paramdigest[SHA1_DIGEST_SIZE];
  105. struct sdesc *sdesc;
  106. unsigned int dlen;
  107. unsigned char *data;
  108. unsigned char c;
  109. int ret;
  110. va_list argp;
  111. if (!chip)
  112. return -ENODEV;
  113. sdesc = init_sdesc(hashalg);
  114. if (IS_ERR(sdesc)) {
  115. pr_info("can't alloc %s\n", hash_alg);
  116. return PTR_ERR(sdesc);
  117. }
  118. c = !!h3;
  119. ret = crypto_shash_init(&sdesc->shash);
  120. if (ret < 0)
  121. goto out;
  122. va_start(argp, h3);
  123. for (;;) {
  124. dlen = va_arg(argp, unsigned int);
  125. if (dlen == 0)
  126. break;
  127. data = va_arg(argp, unsigned char *);
  128. if (!data) {
  129. ret = -EINVAL;
  130. break;
  131. }
  132. ret = crypto_shash_update(&sdesc->shash, data, dlen);
  133. if (ret < 0)
  134. break;
  135. }
  136. va_end(argp);
  137. if (!ret)
  138. ret = crypto_shash_final(&sdesc->shash, paramdigest);
  139. if (!ret)
  140. ret = TSS_rawhmac(digest, key, keylen, SHA1_DIGEST_SIZE,
  141. paramdigest, TPM_NONCE_SIZE, h1,
  142. TPM_NONCE_SIZE, h2, 1, &c, 0, 0);
  143. out:
  144. kfree_sensitive(sdesc);
  145. return ret;
  146. }
  147. EXPORT_SYMBOL_GPL(TSS_authhmac);
  148. /*
  149. * verify the AUTH1_COMMAND (Seal) result from TPM
  150. */
  151. int TSS_checkhmac1(unsigned char *buffer,
  152. const uint32_t command,
  153. const unsigned char *ononce,
  154. const unsigned char *key,
  155. unsigned int keylen, ...)
  156. {
  157. uint32_t bufsize;
  158. uint16_t tag;
  159. uint32_t ordinal;
  160. uint32_t result;
  161. unsigned char *enonce;
  162. unsigned char *continueflag;
  163. unsigned char *authdata;
  164. unsigned char testhmac[SHA1_DIGEST_SIZE];
  165. unsigned char paramdigest[SHA1_DIGEST_SIZE];
  166. struct sdesc *sdesc;
  167. unsigned int dlen;
  168. unsigned int dpos;
  169. va_list argp;
  170. int ret;
  171. if (!chip)
  172. return -ENODEV;
  173. bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
  174. tag = LOAD16(buffer, 0);
  175. ordinal = command;
  176. result = LOAD32N(buffer, TPM_RETURN_OFFSET);
  177. if (tag == TPM_TAG_RSP_COMMAND)
  178. return 0;
  179. if (tag != TPM_TAG_RSP_AUTH1_COMMAND)
  180. return -EINVAL;
  181. authdata = buffer + bufsize - SHA1_DIGEST_SIZE;
  182. continueflag = authdata - 1;
  183. enonce = continueflag - TPM_NONCE_SIZE;
  184. sdesc = init_sdesc(hashalg);
  185. if (IS_ERR(sdesc)) {
  186. pr_info("can't alloc %s\n", hash_alg);
  187. return PTR_ERR(sdesc);
  188. }
  189. ret = crypto_shash_init(&sdesc->shash);
  190. if (ret < 0)
  191. goto out;
  192. ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
  193. sizeof result);
  194. if (ret < 0)
  195. goto out;
  196. ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
  197. sizeof ordinal);
  198. if (ret < 0)
  199. goto out;
  200. va_start(argp, keylen);
  201. for (;;) {
  202. dlen = va_arg(argp, unsigned int);
  203. if (dlen == 0)
  204. break;
  205. dpos = va_arg(argp, unsigned int);
  206. ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
  207. if (ret < 0)
  208. break;
  209. }
  210. va_end(argp);
  211. if (!ret)
  212. ret = crypto_shash_final(&sdesc->shash, paramdigest);
  213. if (ret < 0)
  214. goto out;
  215. ret = TSS_rawhmac(testhmac, key, keylen, SHA1_DIGEST_SIZE, paramdigest,
  216. TPM_NONCE_SIZE, enonce, TPM_NONCE_SIZE, ononce,
  217. 1, continueflag, 0, 0);
  218. if (ret < 0)
  219. goto out;
  220. if (memcmp(testhmac, authdata, SHA1_DIGEST_SIZE))
  221. ret = -EINVAL;
  222. out:
  223. kfree_sensitive(sdesc);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(TSS_checkhmac1);
  227. /*
  228. * verify the AUTH2_COMMAND (unseal) result from TPM
  229. */
  230. static int TSS_checkhmac2(unsigned char *buffer,
  231. const uint32_t command,
  232. const unsigned char *ononce,
  233. const unsigned char *key1,
  234. unsigned int keylen1,
  235. const unsigned char *key2,
  236. unsigned int keylen2, ...)
  237. {
  238. uint32_t bufsize;
  239. uint16_t tag;
  240. uint32_t ordinal;
  241. uint32_t result;
  242. unsigned char *enonce1;
  243. unsigned char *continueflag1;
  244. unsigned char *authdata1;
  245. unsigned char *enonce2;
  246. unsigned char *continueflag2;
  247. unsigned char *authdata2;
  248. unsigned char testhmac1[SHA1_DIGEST_SIZE];
  249. unsigned char testhmac2[SHA1_DIGEST_SIZE];
  250. unsigned char paramdigest[SHA1_DIGEST_SIZE];
  251. struct sdesc *sdesc;
  252. unsigned int dlen;
  253. unsigned int dpos;
  254. va_list argp;
  255. int ret;
  256. bufsize = LOAD32(buffer, TPM_SIZE_OFFSET);
  257. tag = LOAD16(buffer, 0);
  258. ordinal = command;
  259. result = LOAD32N(buffer, TPM_RETURN_OFFSET);
  260. if (tag == TPM_TAG_RSP_COMMAND)
  261. return 0;
  262. if (tag != TPM_TAG_RSP_AUTH2_COMMAND)
  263. return -EINVAL;
  264. authdata1 = buffer + bufsize - (SHA1_DIGEST_SIZE + 1
  265. + SHA1_DIGEST_SIZE + SHA1_DIGEST_SIZE);
  266. authdata2 = buffer + bufsize - (SHA1_DIGEST_SIZE);
  267. continueflag1 = authdata1 - 1;
  268. continueflag2 = authdata2 - 1;
  269. enonce1 = continueflag1 - TPM_NONCE_SIZE;
  270. enonce2 = continueflag2 - TPM_NONCE_SIZE;
  271. sdesc = init_sdesc(hashalg);
  272. if (IS_ERR(sdesc)) {
  273. pr_info("can't alloc %s\n", hash_alg);
  274. return PTR_ERR(sdesc);
  275. }
  276. ret = crypto_shash_init(&sdesc->shash);
  277. if (ret < 0)
  278. goto out;
  279. ret = crypto_shash_update(&sdesc->shash, (const u8 *)&result,
  280. sizeof result);
  281. if (ret < 0)
  282. goto out;
  283. ret = crypto_shash_update(&sdesc->shash, (const u8 *)&ordinal,
  284. sizeof ordinal);
  285. if (ret < 0)
  286. goto out;
  287. va_start(argp, keylen2);
  288. for (;;) {
  289. dlen = va_arg(argp, unsigned int);
  290. if (dlen == 0)
  291. break;
  292. dpos = va_arg(argp, unsigned int);
  293. ret = crypto_shash_update(&sdesc->shash, buffer + dpos, dlen);
  294. if (ret < 0)
  295. break;
  296. }
  297. va_end(argp);
  298. if (!ret)
  299. ret = crypto_shash_final(&sdesc->shash, paramdigest);
  300. if (ret < 0)
  301. goto out;
  302. ret = TSS_rawhmac(testhmac1, key1, keylen1, SHA1_DIGEST_SIZE,
  303. paramdigest, TPM_NONCE_SIZE, enonce1,
  304. TPM_NONCE_SIZE, ononce, 1, continueflag1, 0, 0);
  305. if (ret < 0)
  306. goto out;
  307. if (memcmp(testhmac1, authdata1, SHA1_DIGEST_SIZE)) {
  308. ret = -EINVAL;
  309. goto out;
  310. }
  311. ret = TSS_rawhmac(testhmac2, key2, keylen2, SHA1_DIGEST_SIZE,
  312. paramdigest, TPM_NONCE_SIZE, enonce2,
  313. TPM_NONCE_SIZE, ononce, 1, continueflag2, 0, 0);
  314. if (ret < 0)
  315. goto out;
  316. if (memcmp(testhmac2, authdata2, SHA1_DIGEST_SIZE))
  317. ret = -EINVAL;
  318. out:
  319. kfree_sensitive(sdesc);
  320. return ret;
  321. }
  322. /*
  323. * For key specific tpm requests, we will generate and send our
  324. * own TPM command packets using the drivers send function.
  325. */
  326. int trusted_tpm_send(unsigned char *cmd, size_t buflen)
  327. {
  328. int rc;
  329. if (!chip)
  330. return -ENODEV;
  331. dump_tpm_buf(cmd);
  332. rc = tpm_send(chip, cmd, buflen);
  333. dump_tpm_buf(cmd);
  334. if (rc > 0)
  335. /* Can't return positive return codes values to keyctl */
  336. rc = -EPERM;
  337. return rc;
  338. }
  339. EXPORT_SYMBOL_GPL(trusted_tpm_send);
  340. /*
  341. * Lock a trusted key, by extending a selected PCR.
  342. *
  343. * Prevents a trusted key that is sealed to PCRs from being accessed.
  344. * This uses the tpm driver's extend function.
  345. */
  346. static int pcrlock(const int pcrnum)
  347. {
  348. if (!capable(CAP_SYS_ADMIN))
  349. return -EPERM;
  350. return tpm_pcr_extend(chip, pcrnum, digests) ? -EINVAL : 0;
  351. }
  352. /*
  353. * Create an object specific authorisation protocol (OSAP) session
  354. */
  355. static int osap(struct tpm_buf *tb, struct osapsess *s,
  356. const unsigned char *key, uint16_t type, uint32_t handle)
  357. {
  358. unsigned char enonce[TPM_NONCE_SIZE];
  359. unsigned char ononce[TPM_NONCE_SIZE];
  360. int ret;
  361. ret = tpm_get_random(chip, ononce, TPM_NONCE_SIZE);
  362. if (ret < 0)
  363. return ret;
  364. if (ret != TPM_NONCE_SIZE)
  365. return -EIO;
  366. tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OSAP);
  367. tpm_buf_append_u16(tb, type);
  368. tpm_buf_append_u32(tb, handle);
  369. tpm_buf_append(tb, ononce, TPM_NONCE_SIZE);
  370. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  371. if (ret < 0)
  372. return ret;
  373. s->handle = LOAD32(tb->data, TPM_DATA_OFFSET);
  374. memcpy(s->enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)]),
  375. TPM_NONCE_SIZE);
  376. memcpy(enonce, &(tb->data[TPM_DATA_OFFSET + sizeof(uint32_t) +
  377. TPM_NONCE_SIZE]), TPM_NONCE_SIZE);
  378. return TSS_rawhmac(s->secret, key, SHA1_DIGEST_SIZE, TPM_NONCE_SIZE,
  379. enonce, TPM_NONCE_SIZE, ononce, 0, 0);
  380. }
  381. /*
  382. * Create an object independent authorisation protocol (oiap) session
  383. */
  384. int oiap(struct tpm_buf *tb, uint32_t *handle, unsigned char *nonce)
  385. {
  386. int ret;
  387. if (!chip)
  388. return -ENODEV;
  389. tpm_buf_reset(tb, TPM_TAG_RQU_COMMAND, TPM_ORD_OIAP);
  390. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  391. if (ret < 0)
  392. return ret;
  393. *handle = LOAD32(tb->data, TPM_DATA_OFFSET);
  394. memcpy(nonce, &tb->data[TPM_DATA_OFFSET + sizeof(uint32_t)],
  395. TPM_NONCE_SIZE);
  396. return 0;
  397. }
  398. EXPORT_SYMBOL_GPL(oiap);
  399. struct tpm_digests {
  400. unsigned char encauth[SHA1_DIGEST_SIZE];
  401. unsigned char pubauth[SHA1_DIGEST_SIZE];
  402. unsigned char xorwork[SHA1_DIGEST_SIZE * 2];
  403. unsigned char xorhash[SHA1_DIGEST_SIZE];
  404. unsigned char nonceodd[TPM_NONCE_SIZE];
  405. };
  406. /*
  407. * Have the TPM seal(encrypt) the trusted key, possibly based on
  408. * Platform Configuration Registers (PCRs). AUTH1 for sealing key.
  409. */
  410. static int tpm_seal(struct tpm_buf *tb, uint16_t keytype,
  411. uint32_t keyhandle, const unsigned char *keyauth,
  412. const unsigned char *data, uint32_t datalen,
  413. unsigned char *blob, uint32_t *bloblen,
  414. const unsigned char *blobauth,
  415. const unsigned char *pcrinfo, uint32_t pcrinfosize)
  416. {
  417. struct osapsess sess;
  418. struct tpm_digests *td;
  419. unsigned char cont;
  420. uint32_t ordinal;
  421. uint32_t pcrsize;
  422. uint32_t datsize;
  423. int sealinfosize;
  424. int encdatasize;
  425. int storedsize;
  426. int ret;
  427. int i;
  428. /* alloc some work space for all the hashes */
  429. td = kmalloc(sizeof *td, GFP_KERNEL);
  430. if (!td)
  431. return -ENOMEM;
  432. /* get session for sealing key */
  433. ret = osap(tb, &sess, keyauth, keytype, keyhandle);
  434. if (ret < 0)
  435. goto out;
  436. dump_sess(&sess);
  437. /* calculate encrypted authorization value */
  438. memcpy(td->xorwork, sess.secret, SHA1_DIGEST_SIZE);
  439. memcpy(td->xorwork + SHA1_DIGEST_SIZE, sess.enonce, SHA1_DIGEST_SIZE);
  440. ret = TSS_sha1(td->xorwork, SHA1_DIGEST_SIZE * 2, td->xorhash);
  441. if (ret < 0)
  442. goto out;
  443. ret = tpm_get_random(chip, td->nonceodd, TPM_NONCE_SIZE);
  444. if (ret < 0)
  445. goto out;
  446. if (ret != TPM_NONCE_SIZE) {
  447. ret = -EIO;
  448. goto out;
  449. }
  450. ordinal = htonl(TPM_ORD_SEAL);
  451. datsize = htonl(datalen);
  452. pcrsize = htonl(pcrinfosize);
  453. cont = 0;
  454. /* encrypt data authorization key */
  455. for (i = 0; i < SHA1_DIGEST_SIZE; ++i)
  456. td->encauth[i] = td->xorhash[i] ^ blobauth[i];
  457. /* calculate authorization HMAC value */
  458. if (pcrinfosize == 0) {
  459. /* no pcr info specified */
  460. ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
  461. sess.enonce, td->nonceodd, cont,
  462. sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
  463. td->encauth, sizeof(uint32_t), &pcrsize,
  464. sizeof(uint32_t), &datsize, datalen, data, 0,
  465. 0);
  466. } else {
  467. /* pcr info specified */
  468. ret = TSS_authhmac(td->pubauth, sess.secret, SHA1_DIGEST_SIZE,
  469. sess.enonce, td->nonceodd, cont,
  470. sizeof(uint32_t), &ordinal, SHA1_DIGEST_SIZE,
  471. td->encauth, sizeof(uint32_t), &pcrsize,
  472. pcrinfosize, pcrinfo, sizeof(uint32_t),
  473. &datsize, datalen, data, 0, 0);
  474. }
  475. if (ret < 0)
  476. goto out;
  477. /* build and send the TPM request packet */
  478. tpm_buf_reset(tb, TPM_TAG_RQU_AUTH1_COMMAND, TPM_ORD_SEAL);
  479. tpm_buf_append_u32(tb, keyhandle);
  480. tpm_buf_append(tb, td->encauth, SHA1_DIGEST_SIZE);
  481. tpm_buf_append_u32(tb, pcrinfosize);
  482. tpm_buf_append(tb, pcrinfo, pcrinfosize);
  483. tpm_buf_append_u32(tb, datalen);
  484. tpm_buf_append(tb, data, datalen);
  485. tpm_buf_append_u32(tb, sess.handle);
  486. tpm_buf_append(tb, td->nonceodd, TPM_NONCE_SIZE);
  487. tpm_buf_append_u8(tb, cont);
  488. tpm_buf_append(tb, td->pubauth, SHA1_DIGEST_SIZE);
  489. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  490. if (ret < 0)
  491. goto out;
  492. /* calculate the size of the returned Blob */
  493. sealinfosize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t));
  494. encdatasize = LOAD32(tb->data, TPM_DATA_OFFSET + sizeof(uint32_t) +
  495. sizeof(uint32_t) + sealinfosize);
  496. storedsize = sizeof(uint32_t) + sizeof(uint32_t) + sealinfosize +
  497. sizeof(uint32_t) + encdatasize;
  498. /* check the HMAC in the response */
  499. ret = TSS_checkhmac1(tb->data, ordinal, td->nonceodd, sess.secret,
  500. SHA1_DIGEST_SIZE, storedsize, TPM_DATA_OFFSET, 0,
  501. 0);
  502. /* copy the returned blob to caller */
  503. if (!ret) {
  504. memcpy(blob, tb->data + TPM_DATA_OFFSET, storedsize);
  505. *bloblen = storedsize;
  506. }
  507. out:
  508. kfree_sensitive(td);
  509. return ret;
  510. }
  511. /*
  512. * use the AUTH2_COMMAND form of unseal, to authorize both key and blob
  513. */
  514. static int tpm_unseal(struct tpm_buf *tb,
  515. uint32_t keyhandle, const unsigned char *keyauth,
  516. const unsigned char *blob, int bloblen,
  517. const unsigned char *blobauth,
  518. unsigned char *data, unsigned int *datalen)
  519. {
  520. unsigned char nonceodd[TPM_NONCE_SIZE];
  521. unsigned char enonce1[TPM_NONCE_SIZE];
  522. unsigned char enonce2[TPM_NONCE_SIZE];
  523. unsigned char authdata1[SHA1_DIGEST_SIZE];
  524. unsigned char authdata2[SHA1_DIGEST_SIZE];
  525. uint32_t authhandle1 = 0;
  526. uint32_t authhandle2 = 0;
  527. unsigned char cont = 0;
  528. uint32_t ordinal;
  529. int ret;
  530. /* sessions for unsealing key and data */
  531. ret = oiap(tb, &authhandle1, enonce1);
  532. if (ret < 0) {
  533. pr_info("oiap failed (%d)\n", ret);
  534. return ret;
  535. }
  536. ret = oiap(tb, &authhandle2, enonce2);
  537. if (ret < 0) {
  538. pr_info("oiap failed (%d)\n", ret);
  539. return ret;
  540. }
  541. ordinal = htonl(TPM_ORD_UNSEAL);
  542. ret = tpm_get_random(chip, nonceodd, TPM_NONCE_SIZE);
  543. if (ret < 0)
  544. return ret;
  545. if (ret != TPM_NONCE_SIZE) {
  546. pr_info("tpm_get_random failed (%d)\n", ret);
  547. return -EIO;
  548. }
  549. ret = TSS_authhmac(authdata1, keyauth, TPM_NONCE_SIZE,
  550. enonce1, nonceodd, cont, sizeof(uint32_t),
  551. &ordinal, bloblen, blob, 0, 0);
  552. if (ret < 0)
  553. return ret;
  554. ret = TSS_authhmac(authdata2, blobauth, TPM_NONCE_SIZE,
  555. enonce2, nonceodd, cont, sizeof(uint32_t),
  556. &ordinal, bloblen, blob, 0, 0);
  557. if (ret < 0)
  558. return ret;
  559. /* build and send TPM request packet */
  560. tpm_buf_reset(tb, TPM_TAG_RQU_AUTH2_COMMAND, TPM_ORD_UNSEAL);
  561. tpm_buf_append_u32(tb, keyhandle);
  562. tpm_buf_append(tb, blob, bloblen);
  563. tpm_buf_append_u32(tb, authhandle1);
  564. tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
  565. tpm_buf_append_u8(tb, cont);
  566. tpm_buf_append(tb, authdata1, SHA1_DIGEST_SIZE);
  567. tpm_buf_append_u32(tb, authhandle2);
  568. tpm_buf_append(tb, nonceodd, TPM_NONCE_SIZE);
  569. tpm_buf_append_u8(tb, cont);
  570. tpm_buf_append(tb, authdata2, SHA1_DIGEST_SIZE);
  571. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  572. if (ret < 0) {
  573. pr_info("authhmac failed (%d)\n", ret);
  574. return ret;
  575. }
  576. *datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
  577. ret = TSS_checkhmac2(tb->data, ordinal, nonceodd,
  578. keyauth, SHA1_DIGEST_SIZE,
  579. blobauth, SHA1_DIGEST_SIZE,
  580. sizeof(uint32_t), TPM_DATA_OFFSET,
  581. *datalen, TPM_DATA_OFFSET + sizeof(uint32_t), 0,
  582. 0);
  583. if (ret < 0) {
  584. pr_info("TSS_checkhmac2 failed (%d)\n", ret);
  585. return ret;
  586. }
  587. memcpy(data, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t), *datalen);
  588. return 0;
  589. }
  590. /*
  591. * Have the TPM seal(encrypt) the symmetric key
  592. */
  593. static int key_seal(struct trusted_key_payload *p,
  594. struct trusted_key_options *o)
  595. {
  596. struct tpm_buf tb;
  597. int ret;
  598. ret = tpm_buf_init(&tb, 0, 0);
  599. if (ret)
  600. return ret;
  601. /* include migratable flag at end of sealed key */
  602. p->key[p->key_len] = p->migratable;
  603. ret = tpm_seal(&tb, o->keytype, o->keyhandle, o->keyauth,
  604. p->key, p->key_len + 1, p->blob, &p->blob_len,
  605. o->blobauth, o->pcrinfo, o->pcrinfo_len);
  606. if (ret < 0)
  607. pr_info("srkseal failed (%d)\n", ret);
  608. tpm_buf_destroy(&tb);
  609. return ret;
  610. }
  611. /*
  612. * Have the TPM unseal(decrypt) the symmetric key
  613. */
  614. static int key_unseal(struct trusted_key_payload *p,
  615. struct trusted_key_options *o)
  616. {
  617. struct tpm_buf tb;
  618. int ret;
  619. ret = tpm_buf_init(&tb, 0, 0);
  620. if (ret)
  621. return ret;
  622. ret = tpm_unseal(&tb, o->keyhandle, o->keyauth, p->blob, p->blob_len,
  623. o->blobauth, p->key, &p->key_len);
  624. if (ret < 0)
  625. pr_info("srkunseal failed (%d)\n", ret);
  626. else
  627. /* pull migratable flag out of sealed key */
  628. p->migratable = p->key[--p->key_len];
  629. tpm_buf_destroy(&tb);
  630. return ret;
  631. }
  632. enum {
  633. Opt_err,
  634. Opt_keyhandle, Opt_keyauth, Opt_blobauth,
  635. Opt_pcrinfo, Opt_pcrlock, Opt_migratable,
  636. Opt_hash,
  637. Opt_policydigest,
  638. Opt_policyhandle,
  639. };
  640. static const match_table_t key_tokens = {
  641. {Opt_keyhandle, "keyhandle=%s"},
  642. {Opt_keyauth, "keyauth=%s"},
  643. {Opt_blobauth, "blobauth=%s"},
  644. {Opt_pcrinfo, "pcrinfo=%s"},
  645. {Opt_pcrlock, "pcrlock=%s"},
  646. {Opt_migratable, "migratable=%s"},
  647. {Opt_hash, "hash=%s"},
  648. {Opt_policydigest, "policydigest=%s"},
  649. {Opt_policyhandle, "policyhandle=%s"},
  650. {Opt_err, NULL}
  651. };
  652. /* can have zero or more token= options */
  653. static int getoptions(char *c, struct trusted_key_payload *pay,
  654. struct trusted_key_options *opt)
  655. {
  656. substring_t args[MAX_OPT_ARGS];
  657. char *p = c;
  658. int token;
  659. int res;
  660. unsigned long handle;
  661. unsigned long lock;
  662. unsigned long token_mask = 0;
  663. unsigned int digest_len;
  664. int i;
  665. int tpm2;
  666. tpm2 = tpm_is_tpm2(chip);
  667. if (tpm2 < 0)
  668. return tpm2;
  669. opt->hash = tpm2 ? HASH_ALGO_SHA256 : HASH_ALGO_SHA1;
  670. if (!c)
  671. return 0;
  672. while ((p = strsep(&c, " \t"))) {
  673. if (*p == '\0' || *p == ' ' || *p == '\t')
  674. continue;
  675. token = match_token(p, key_tokens, args);
  676. if (test_and_set_bit(token, &token_mask))
  677. return -EINVAL;
  678. switch (token) {
  679. case Opt_pcrinfo:
  680. opt->pcrinfo_len = strlen(args[0].from) / 2;
  681. if (opt->pcrinfo_len > MAX_PCRINFO_SIZE)
  682. return -EINVAL;
  683. res = hex2bin(opt->pcrinfo, args[0].from,
  684. opt->pcrinfo_len);
  685. if (res < 0)
  686. return -EINVAL;
  687. break;
  688. case Opt_keyhandle:
  689. res = kstrtoul(args[0].from, 16, &handle);
  690. if (res < 0)
  691. return -EINVAL;
  692. opt->keytype = SEAL_keytype;
  693. opt->keyhandle = handle;
  694. break;
  695. case Opt_keyauth:
  696. if (strlen(args[0].from) != 2 * SHA1_DIGEST_SIZE)
  697. return -EINVAL;
  698. res = hex2bin(opt->keyauth, args[0].from,
  699. SHA1_DIGEST_SIZE);
  700. if (res < 0)
  701. return -EINVAL;
  702. break;
  703. case Opt_blobauth:
  704. /*
  705. * TPM 1.2 authorizations are sha1 hashes passed in as
  706. * hex strings. TPM 2.0 authorizations are simple
  707. * passwords (although it can take a hash as well)
  708. */
  709. opt->blobauth_len = strlen(args[0].from);
  710. if (opt->blobauth_len == 2 * TPM_DIGEST_SIZE) {
  711. res = hex2bin(opt->blobauth, args[0].from,
  712. TPM_DIGEST_SIZE);
  713. if (res < 0)
  714. return -EINVAL;
  715. opt->blobauth_len = TPM_DIGEST_SIZE;
  716. break;
  717. }
  718. if (tpm2 && opt->blobauth_len <= sizeof(opt->blobauth)) {
  719. memcpy(opt->blobauth, args[0].from,
  720. opt->blobauth_len);
  721. break;
  722. }
  723. return -EINVAL;
  724. break;
  725. case Opt_migratable:
  726. if (*args[0].from == '0')
  727. pay->migratable = 0;
  728. else if (*args[0].from != '1')
  729. return -EINVAL;
  730. break;
  731. case Opt_pcrlock:
  732. res = kstrtoul(args[0].from, 10, &lock);
  733. if (res < 0)
  734. return -EINVAL;
  735. opt->pcrlock = lock;
  736. break;
  737. case Opt_hash:
  738. if (test_bit(Opt_policydigest, &token_mask))
  739. return -EINVAL;
  740. for (i = 0; i < HASH_ALGO__LAST; i++) {
  741. if (!strcmp(args[0].from, hash_algo_name[i])) {
  742. opt->hash = i;
  743. break;
  744. }
  745. }
  746. if (i == HASH_ALGO__LAST)
  747. return -EINVAL;
  748. if (!tpm2 && i != HASH_ALGO_SHA1) {
  749. pr_info("TPM 1.x only supports SHA-1.\n");
  750. return -EINVAL;
  751. }
  752. break;
  753. case Opt_policydigest:
  754. digest_len = hash_digest_size[opt->hash];
  755. if (!tpm2 || strlen(args[0].from) != (2 * digest_len))
  756. return -EINVAL;
  757. res = hex2bin(opt->policydigest, args[0].from,
  758. digest_len);
  759. if (res < 0)
  760. return -EINVAL;
  761. opt->policydigest_len = digest_len;
  762. break;
  763. case Opt_policyhandle:
  764. if (!tpm2)
  765. return -EINVAL;
  766. res = kstrtoul(args[0].from, 16, &handle);
  767. if (res < 0)
  768. return -EINVAL;
  769. opt->policyhandle = handle;
  770. break;
  771. default:
  772. return -EINVAL;
  773. }
  774. }
  775. return 0;
  776. }
  777. static struct trusted_key_options *trusted_options_alloc(void)
  778. {
  779. struct trusted_key_options *options;
  780. int tpm2;
  781. tpm2 = tpm_is_tpm2(chip);
  782. if (tpm2 < 0)
  783. return NULL;
  784. options = kzalloc(sizeof *options, GFP_KERNEL);
  785. if (options) {
  786. /* set any non-zero defaults */
  787. options->keytype = SRK_keytype;
  788. if (!tpm2)
  789. options->keyhandle = SRKHANDLE;
  790. }
  791. return options;
  792. }
  793. static int trusted_tpm_seal(struct trusted_key_payload *p, char *datablob)
  794. {
  795. struct trusted_key_options *options = NULL;
  796. int ret = 0;
  797. int tpm2;
  798. tpm2 = tpm_is_tpm2(chip);
  799. if (tpm2 < 0)
  800. return tpm2;
  801. options = trusted_options_alloc();
  802. if (!options)
  803. return -ENOMEM;
  804. ret = getoptions(datablob, p, options);
  805. if (ret < 0)
  806. goto out;
  807. dump_options(options);
  808. if (!options->keyhandle && !tpm2) {
  809. ret = -EINVAL;
  810. goto out;
  811. }
  812. if (tpm2)
  813. ret = tpm2_seal_trusted(chip, p, options);
  814. else
  815. ret = key_seal(p, options);
  816. if (ret < 0) {
  817. pr_info("key_seal failed (%d)\n", ret);
  818. goto out;
  819. }
  820. if (options->pcrlock) {
  821. ret = pcrlock(options->pcrlock);
  822. if (ret < 0) {
  823. pr_info("pcrlock failed (%d)\n", ret);
  824. goto out;
  825. }
  826. }
  827. out:
  828. kfree_sensitive(options);
  829. return ret;
  830. }
  831. static int trusted_tpm_unseal(struct trusted_key_payload *p, char *datablob)
  832. {
  833. struct trusted_key_options *options = NULL;
  834. int ret = 0;
  835. int tpm2;
  836. tpm2 = tpm_is_tpm2(chip);
  837. if (tpm2 < 0)
  838. return tpm2;
  839. options = trusted_options_alloc();
  840. if (!options)
  841. return -ENOMEM;
  842. ret = getoptions(datablob, p, options);
  843. if (ret < 0)
  844. goto out;
  845. dump_options(options);
  846. if (!options->keyhandle && !tpm2) {
  847. ret = -EINVAL;
  848. goto out;
  849. }
  850. if (tpm2)
  851. ret = tpm2_unseal_trusted(chip, p, options);
  852. else
  853. ret = key_unseal(p, options);
  854. if (ret < 0)
  855. pr_info("key_unseal failed (%d)\n", ret);
  856. if (options->pcrlock) {
  857. ret = pcrlock(options->pcrlock);
  858. if (ret < 0) {
  859. pr_info("pcrlock failed (%d)\n", ret);
  860. goto out;
  861. }
  862. }
  863. out:
  864. kfree_sensitive(options);
  865. return ret;
  866. }
  867. static int trusted_tpm_get_random(unsigned char *key, size_t key_len)
  868. {
  869. return tpm_get_random(chip, key, key_len);
  870. }
  871. static void trusted_shash_release(void)
  872. {
  873. if (hashalg)
  874. crypto_free_shash(hashalg);
  875. if (hmacalg)
  876. crypto_free_shash(hmacalg);
  877. }
  878. static int __init trusted_shash_alloc(void)
  879. {
  880. int ret;
  881. hmacalg = crypto_alloc_shash(hmac_alg, 0, 0);
  882. if (IS_ERR(hmacalg)) {
  883. pr_info("could not allocate crypto %s\n",
  884. hmac_alg);
  885. return PTR_ERR(hmacalg);
  886. }
  887. hashalg = crypto_alloc_shash(hash_alg, 0, 0);
  888. if (IS_ERR(hashalg)) {
  889. pr_info("could not allocate crypto %s\n",
  890. hash_alg);
  891. ret = PTR_ERR(hashalg);
  892. goto hashalg_fail;
  893. }
  894. return 0;
  895. hashalg_fail:
  896. crypto_free_shash(hmacalg);
  897. return ret;
  898. }
  899. static int __init init_digests(void)
  900. {
  901. int i;
  902. digests = kcalloc(chip->nr_allocated_banks, sizeof(*digests),
  903. GFP_KERNEL);
  904. if (!digests)
  905. return -ENOMEM;
  906. for (i = 0; i < chip->nr_allocated_banks; i++)
  907. digests[i].alg_id = chip->allocated_banks[i].alg_id;
  908. return 0;
  909. }
  910. static int __init trusted_tpm_init(void)
  911. {
  912. int ret;
  913. chip = tpm_default_chip();
  914. if (!chip)
  915. return -ENODEV;
  916. ret = init_digests();
  917. if (ret < 0)
  918. goto err_put;
  919. ret = trusted_shash_alloc();
  920. if (ret < 0)
  921. goto err_free;
  922. ret = register_key_type(&key_type_trusted);
  923. if (ret < 0)
  924. goto err_release;
  925. return 0;
  926. err_release:
  927. trusted_shash_release();
  928. err_free:
  929. kfree(digests);
  930. err_put:
  931. put_device(&chip->dev);
  932. return ret;
  933. }
  934. static void trusted_tpm_exit(void)
  935. {
  936. if (chip) {
  937. put_device(&chip->dev);
  938. kfree(digests);
  939. trusted_shash_release();
  940. unregister_key_type(&key_type_trusted);
  941. }
  942. }
  943. struct trusted_key_ops trusted_key_tpm_ops = {
  944. .migratable = 1, /* migratable by default */
  945. .init = trusted_tpm_init,
  946. .seal = trusted_tpm_seal,
  947. .unseal = trusted_tpm_unseal,
  948. .get_random = trusted_tpm_get_random,
  949. .exit = trusted_tpm_exit,
  950. };