asym_tpm.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "ASYM-TPM: "fmt
  3. #include <linux/slab.h>
  4. #include <linux/module.h>
  5. #include <linux/export.h>
  6. #include <linux/kernel.h>
  7. #include <linux/seq_file.h>
  8. #include <linux/scatterlist.h>
  9. #include <linux/tpm.h>
  10. #include <linux/tpm_command.h>
  11. #include <crypto/akcipher.h>
  12. #include <crypto/hash.h>
  13. #include <crypto/sha.h>
  14. #include <asm/unaligned.h>
  15. #include <keys/asymmetric-subtype.h>
  16. #include <keys/trusted.h>
  17. #include <crypto/asym_tpm_subtype.h>
  18. #include <crypto/public_key.h>
  19. #define TPM_ORD_FLUSHSPECIFIC 186
  20. #define TPM_ORD_LOADKEY2 65
  21. #define TPM_ORD_UNBIND 30
  22. #define TPM_ORD_SIGN 60
  23. #define TPM_LOADKEY2_SIZE 59
  24. #define TPM_FLUSHSPECIFIC_SIZE 18
  25. #define TPM_UNBIND_SIZE 63
  26. #define TPM_SIGN_SIZE 63
  27. #define TPM_RT_KEY 0x00000001
  28. /*
  29. * Load a TPM key from the blob provided by userspace
  30. */
  31. static int tpm_loadkey2(struct tpm_buf *tb,
  32. uint32_t keyhandle, unsigned char *keyauth,
  33. const unsigned char *keyblob, int keybloblen,
  34. uint32_t *newhandle)
  35. {
  36. unsigned char nonceodd[TPM_NONCE_SIZE];
  37. unsigned char enonce[TPM_NONCE_SIZE];
  38. unsigned char authdata[SHA1_DIGEST_SIZE];
  39. uint32_t authhandle = 0;
  40. unsigned char cont = 0;
  41. uint32_t ordinal;
  42. int ret;
  43. ordinal = htonl(TPM_ORD_LOADKEY2);
  44. /* session for loading the key */
  45. ret = oiap(tb, &authhandle, enonce);
  46. if (ret < 0) {
  47. pr_info("oiap failed (%d)\n", ret);
  48. return ret;
  49. }
  50. /* generate odd nonce */
  51. ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
  52. if (ret < 0) {
  53. pr_info("tpm_get_random failed (%d)\n", ret);
  54. return ret;
  55. }
  56. /* calculate authorization HMAC value */
  57. ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
  58. nonceodd, cont, sizeof(uint32_t), &ordinal,
  59. keybloblen, keyblob, 0, 0);
  60. if (ret < 0)
  61. return ret;
  62. /* build the request buffer */
  63. INIT_BUF(tb);
  64. store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
  65. store32(tb, TPM_LOADKEY2_SIZE + keybloblen);
  66. store32(tb, TPM_ORD_LOADKEY2);
  67. store32(tb, keyhandle);
  68. storebytes(tb, keyblob, keybloblen);
  69. store32(tb, authhandle);
  70. storebytes(tb, nonceodd, TPM_NONCE_SIZE);
  71. store8(tb, cont);
  72. storebytes(tb, authdata, SHA1_DIGEST_SIZE);
  73. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  74. if (ret < 0) {
  75. pr_info("authhmac failed (%d)\n", ret);
  76. return ret;
  77. }
  78. ret = TSS_checkhmac1(tb->data, ordinal, nonceodd, keyauth,
  79. SHA1_DIGEST_SIZE, 0, 0);
  80. if (ret < 0) {
  81. pr_info("TSS_checkhmac1 failed (%d)\n", ret);
  82. return ret;
  83. }
  84. *newhandle = LOAD32(tb->data, TPM_DATA_OFFSET);
  85. return 0;
  86. }
  87. /*
  88. * Execute the FlushSpecific TPM command
  89. */
  90. static int tpm_flushspecific(struct tpm_buf *tb, uint32_t handle)
  91. {
  92. INIT_BUF(tb);
  93. store16(tb, TPM_TAG_RQU_COMMAND);
  94. store32(tb, TPM_FLUSHSPECIFIC_SIZE);
  95. store32(tb, TPM_ORD_FLUSHSPECIFIC);
  96. store32(tb, handle);
  97. store32(tb, TPM_RT_KEY);
  98. return trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  99. }
  100. /*
  101. * Decrypt a blob provided by userspace using a specific key handle.
  102. * The handle is a well known handle or previously loaded by e.g. LoadKey2
  103. */
  104. static int tpm_unbind(struct tpm_buf *tb,
  105. uint32_t keyhandle, unsigned char *keyauth,
  106. const unsigned char *blob, uint32_t bloblen,
  107. void *out, uint32_t outlen)
  108. {
  109. unsigned char nonceodd[TPM_NONCE_SIZE];
  110. unsigned char enonce[TPM_NONCE_SIZE];
  111. unsigned char authdata[SHA1_DIGEST_SIZE];
  112. uint32_t authhandle = 0;
  113. unsigned char cont = 0;
  114. uint32_t ordinal;
  115. uint32_t datalen;
  116. int ret;
  117. ordinal = htonl(TPM_ORD_UNBIND);
  118. datalen = htonl(bloblen);
  119. /* session for loading the key */
  120. ret = oiap(tb, &authhandle, enonce);
  121. if (ret < 0) {
  122. pr_info("oiap failed (%d)\n", ret);
  123. return ret;
  124. }
  125. /* generate odd nonce */
  126. ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
  127. if (ret < 0) {
  128. pr_info("tpm_get_random failed (%d)\n", ret);
  129. return ret;
  130. }
  131. /* calculate authorization HMAC value */
  132. ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
  133. nonceodd, cont, sizeof(uint32_t), &ordinal,
  134. sizeof(uint32_t), &datalen,
  135. bloblen, blob, 0, 0);
  136. if (ret < 0)
  137. return ret;
  138. /* build the request buffer */
  139. INIT_BUF(tb);
  140. store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
  141. store32(tb, TPM_UNBIND_SIZE + bloblen);
  142. store32(tb, TPM_ORD_UNBIND);
  143. store32(tb, keyhandle);
  144. store32(tb, bloblen);
  145. storebytes(tb, blob, bloblen);
  146. store32(tb, authhandle);
  147. storebytes(tb, nonceodd, TPM_NONCE_SIZE);
  148. store8(tb, cont);
  149. storebytes(tb, authdata, SHA1_DIGEST_SIZE);
  150. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  151. if (ret < 0) {
  152. pr_info("authhmac failed (%d)\n", ret);
  153. return ret;
  154. }
  155. datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
  156. ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
  157. keyauth, SHA1_DIGEST_SIZE,
  158. sizeof(uint32_t), TPM_DATA_OFFSET,
  159. datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
  160. 0, 0);
  161. if (ret < 0) {
  162. pr_info("TSS_checkhmac1 failed (%d)\n", ret);
  163. return ret;
  164. }
  165. memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
  166. min(outlen, datalen));
  167. return datalen;
  168. }
  169. /*
  170. * Sign a blob provided by userspace (that has had the hash function applied)
  171. * using a specific key handle. The handle is assumed to have been previously
  172. * loaded by e.g. LoadKey2.
  173. *
  174. * Note that the key signature scheme of the used key should be set to
  175. * TPM_SS_RSASSAPKCS1v15_DER. This allows the hashed input to be of any size
  176. * up to key_length_in_bytes - 11 and not be limited to size 20 like the
  177. * TPM_SS_RSASSAPKCS1v15_SHA1 signature scheme.
  178. */
  179. static int tpm_sign(struct tpm_buf *tb,
  180. uint32_t keyhandle, unsigned char *keyauth,
  181. const unsigned char *blob, uint32_t bloblen,
  182. void *out, uint32_t outlen)
  183. {
  184. unsigned char nonceodd[TPM_NONCE_SIZE];
  185. unsigned char enonce[TPM_NONCE_SIZE];
  186. unsigned char authdata[SHA1_DIGEST_SIZE];
  187. uint32_t authhandle = 0;
  188. unsigned char cont = 0;
  189. uint32_t ordinal;
  190. uint32_t datalen;
  191. int ret;
  192. ordinal = htonl(TPM_ORD_SIGN);
  193. datalen = htonl(bloblen);
  194. /* session for loading the key */
  195. ret = oiap(tb, &authhandle, enonce);
  196. if (ret < 0) {
  197. pr_info("oiap failed (%d)\n", ret);
  198. return ret;
  199. }
  200. /* generate odd nonce */
  201. ret = tpm_get_random(NULL, nonceodd, TPM_NONCE_SIZE);
  202. if (ret < 0) {
  203. pr_info("tpm_get_random failed (%d)\n", ret);
  204. return ret;
  205. }
  206. /* calculate authorization HMAC value */
  207. ret = TSS_authhmac(authdata, keyauth, SHA1_DIGEST_SIZE, enonce,
  208. nonceodd, cont, sizeof(uint32_t), &ordinal,
  209. sizeof(uint32_t), &datalen,
  210. bloblen, blob, 0, 0);
  211. if (ret < 0)
  212. return ret;
  213. /* build the request buffer */
  214. INIT_BUF(tb);
  215. store16(tb, TPM_TAG_RQU_AUTH1_COMMAND);
  216. store32(tb, TPM_SIGN_SIZE + bloblen);
  217. store32(tb, TPM_ORD_SIGN);
  218. store32(tb, keyhandle);
  219. store32(tb, bloblen);
  220. storebytes(tb, blob, bloblen);
  221. store32(tb, authhandle);
  222. storebytes(tb, nonceodd, TPM_NONCE_SIZE);
  223. store8(tb, cont);
  224. storebytes(tb, authdata, SHA1_DIGEST_SIZE);
  225. ret = trusted_tpm_send(tb->data, MAX_BUF_SIZE);
  226. if (ret < 0) {
  227. pr_info("authhmac failed (%d)\n", ret);
  228. return ret;
  229. }
  230. datalen = LOAD32(tb->data, TPM_DATA_OFFSET);
  231. ret = TSS_checkhmac1(tb->data, ordinal, nonceodd,
  232. keyauth, SHA1_DIGEST_SIZE,
  233. sizeof(uint32_t), TPM_DATA_OFFSET,
  234. datalen, TPM_DATA_OFFSET + sizeof(uint32_t),
  235. 0, 0);
  236. if (ret < 0) {
  237. pr_info("TSS_checkhmac1 failed (%d)\n", ret);
  238. return ret;
  239. }
  240. memcpy(out, tb->data + TPM_DATA_OFFSET + sizeof(uint32_t),
  241. min(datalen, outlen));
  242. return datalen;
  243. }
  244. /*
  245. * Maximum buffer size for the BER/DER encoded public key. The public key
  246. * is of the form SEQUENCE { INTEGER n, INTEGER e } where n is a maximum 2048
  247. * bit key and e is usually 65537
  248. * The encoding overhead is:
  249. * - max 4 bytes for SEQUENCE
  250. * - max 4 bytes for INTEGER n type/length
  251. * - 257 bytes of n
  252. * - max 2 bytes for INTEGER e type/length
  253. * - 3 bytes of e
  254. */
  255. #define PUB_KEY_BUF_SIZE (4 + 4 + 257 + 2 + 3)
  256. /*
  257. * Provide a part of a description of the key for /proc/keys.
  258. */
  259. static void asym_tpm_describe(const struct key *asymmetric_key,
  260. struct seq_file *m)
  261. {
  262. struct tpm_key *tk = asymmetric_key->payload.data[asym_crypto];
  263. if (!tk)
  264. return;
  265. seq_printf(m, "TPM1.2/Blob");
  266. }
  267. static void asym_tpm_destroy(void *payload0, void *payload3)
  268. {
  269. struct tpm_key *tk = payload0;
  270. if (!tk)
  271. return;
  272. kfree(tk->blob);
  273. tk->blob_len = 0;
  274. kfree(tk);
  275. }
  276. /* How many bytes will it take to encode the length */
  277. static inline uint32_t definite_length(uint32_t len)
  278. {
  279. if (len <= 127)
  280. return 1;
  281. if (len <= 255)
  282. return 2;
  283. return 3;
  284. }
  285. static inline uint8_t *encode_tag_length(uint8_t *buf, uint8_t tag,
  286. uint32_t len)
  287. {
  288. *buf++ = tag;
  289. if (len <= 127) {
  290. buf[0] = len;
  291. return buf + 1;
  292. }
  293. if (len <= 255) {
  294. buf[0] = 0x81;
  295. buf[1] = len;
  296. return buf + 2;
  297. }
  298. buf[0] = 0x82;
  299. put_unaligned_be16(len, buf + 1);
  300. return buf + 3;
  301. }
  302. static uint32_t derive_pub_key(const void *pub_key, uint32_t len, uint8_t *buf)
  303. {
  304. uint8_t *cur = buf;
  305. uint32_t n_len = definite_length(len) + 1 + len + 1;
  306. uint32_t e_len = definite_length(3) + 1 + 3;
  307. uint8_t e[3] = { 0x01, 0x00, 0x01 };
  308. /* SEQUENCE */
  309. cur = encode_tag_length(cur, 0x30, n_len + e_len);
  310. /* INTEGER n */
  311. cur = encode_tag_length(cur, 0x02, len + 1);
  312. cur[0] = 0x00;
  313. memcpy(cur + 1, pub_key, len);
  314. cur += len + 1;
  315. cur = encode_tag_length(cur, 0x02, sizeof(e));
  316. memcpy(cur, e, sizeof(e));
  317. cur += sizeof(e);
  318. return cur - buf;
  319. }
  320. /*
  321. * Determine the crypto algorithm name.
  322. */
  323. static int determine_akcipher(const char *encoding, const char *hash_algo,
  324. char alg_name[CRYPTO_MAX_ALG_NAME])
  325. {
  326. if (strcmp(encoding, "pkcs1") == 0) {
  327. if (!hash_algo) {
  328. strcpy(alg_name, "pkcs1pad(rsa)");
  329. return 0;
  330. }
  331. if (snprintf(alg_name, CRYPTO_MAX_ALG_NAME, "pkcs1pad(rsa,%s)",
  332. hash_algo) >= CRYPTO_MAX_ALG_NAME)
  333. return -EINVAL;
  334. return 0;
  335. }
  336. if (strcmp(encoding, "raw") == 0) {
  337. strcpy(alg_name, "rsa");
  338. return 0;
  339. }
  340. return -ENOPKG;
  341. }
  342. /*
  343. * Query information about a key.
  344. */
  345. static int tpm_key_query(const struct kernel_pkey_params *params,
  346. struct kernel_pkey_query *info)
  347. {
  348. struct tpm_key *tk = params->key->payload.data[asym_crypto];
  349. int ret;
  350. char alg_name[CRYPTO_MAX_ALG_NAME];
  351. struct crypto_akcipher *tfm;
  352. uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
  353. uint32_t der_pub_key_len;
  354. int len;
  355. /* TPM only works on private keys, public keys still done in software */
  356. ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
  357. if (ret < 0)
  358. return ret;
  359. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  360. if (IS_ERR(tfm))
  361. return PTR_ERR(tfm);
  362. der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
  363. der_pub_key);
  364. ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
  365. if (ret < 0)
  366. goto error_free_tfm;
  367. len = crypto_akcipher_maxsize(tfm);
  368. info->key_size = tk->key_len;
  369. info->max_data_size = tk->key_len / 8;
  370. info->max_sig_size = len;
  371. info->max_enc_size = len;
  372. info->max_dec_size = tk->key_len / 8;
  373. info->supported_ops = KEYCTL_SUPPORTS_ENCRYPT |
  374. KEYCTL_SUPPORTS_DECRYPT |
  375. KEYCTL_SUPPORTS_VERIFY |
  376. KEYCTL_SUPPORTS_SIGN;
  377. ret = 0;
  378. error_free_tfm:
  379. crypto_free_akcipher(tfm);
  380. pr_devel("<==%s() = %d\n", __func__, ret);
  381. return ret;
  382. }
  383. /*
  384. * Encryption operation is performed with the public key. Hence it is done
  385. * in software
  386. */
  387. static int tpm_key_encrypt(struct tpm_key *tk,
  388. struct kernel_pkey_params *params,
  389. const void *in, void *out)
  390. {
  391. char alg_name[CRYPTO_MAX_ALG_NAME];
  392. struct crypto_akcipher *tfm;
  393. struct akcipher_request *req;
  394. struct crypto_wait cwait;
  395. struct scatterlist in_sg, out_sg;
  396. uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
  397. uint32_t der_pub_key_len;
  398. int ret;
  399. pr_devel("==>%s()\n", __func__);
  400. ret = determine_akcipher(params->encoding, params->hash_algo, alg_name);
  401. if (ret < 0)
  402. return ret;
  403. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  404. if (IS_ERR(tfm))
  405. return PTR_ERR(tfm);
  406. der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
  407. der_pub_key);
  408. ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
  409. if (ret < 0)
  410. goto error_free_tfm;
  411. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  412. if (!req)
  413. goto error_free_tfm;
  414. sg_init_one(&in_sg, in, params->in_len);
  415. sg_init_one(&out_sg, out, params->out_len);
  416. akcipher_request_set_crypt(req, &in_sg, &out_sg, params->in_len,
  417. params->out_len);
  418. crypto_init_wait(&cwait);
  419. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  420. CRYPTO_TFM_REQ_MAY_SLEEP,
  421. crypto_req_done, &cwait);
  422. ret = crypto_akcipher_encrypt(req);
  423. ret = crypto_wait_req(ret, &cwait);
  424. if (ret == 0)
  425. ret = req->dst_len;
  426. akcipher_request_free(req);
  427. error_free_tfm:
  428. crypto_free_akcipher(tfm);
  429. pr_devel("<==%s() = %d\n", __func__, ret);
  430. return ret;
  431. }
  432. /*
  433. * Decryption operation is performed with the private key in the TPM.
  434. */
  435. static int tpm_key_decrypt(struct tpm_key *tk,
  436. struct kernel_pkey_params *params,
  437. const void *in, void *out)
  438. {
  439. struct tpm_buf *tb;
  440. uint32_t keyhandle;
  441. uint8_t srkauth[SHA1_DIGEST_SIZE];
  442. uint8_t keyauth[SHA1_DIGEST_SIZE];
  443. int r;
  444. pr_devel("==>%s()\n", __func__);
  445. if (params->hash_algo)
  446. return -ENOPKG;
  447. if (strcmp(params->encoding, "pkcs1"))
  448. return -ENOPKG;
  449. tb = kzalloc(sizeof(*tb), GFP_KERNEL);
  450. if (!tb)
  451. return -ENOMEM;
  452. /* TODO: Handle a non-all zero SRK authorization */
  453. memset(srkauth, 0, sizeof(srkauth));
  454. r = tpm_loadkey2(tb, SRKHANDLE, srkauth,
  455. tk->blob, tk->blob_len, &keyhandle);
  456. if (r < 0) {
  457. pr_devel("loadkey2 failed (%d)\n", r);
  458. goto error;
  459. }
  460. /* TODO: Handle a non-all zero key authorization */
  461. memset(keyauth, 0, sizeof(keyauth));
  462. r = tpm_unbind(tb, keyhandle, keyauth,
  463. in, params->in_len, out, params->out_len);
  464. if (r < 0)
  465. pr_devel("tpm_unbind failed (%d)\n", r);
  466. if (tpm_flushspecific(tb, keyhandle) < 0)
  467. pr_devel("flushspecific failed (%d)\n", r);
  468. error:
  469. kzfree(tb);
  470. pr_devel("<==%s() = %d\n", __func__, r);
  471. return r;
  472. }
  473. /*
  474. * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
  475. */
  476. static const u8 digest_info_md5[] = {
  477. 0x30, 0x20, 0x30, 0x0c, 0x06, 0x08,
  478. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, /* OID */
  479. 0x05, 0x00, 0x04, 0x10
  480. };
  481. static const u8 digest_info_sha1[] = {
  482. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  483. 0x2b, 0x0e, 0x03, 0x02, 0x1a,
  484. 0x05, 0x00, 0x04, 0x14
  485. };
  486. static const u8 digest_info_rmd160[] = {
  487. 0x30, 0x21, 0x30, 0x09, 0x06, 0x05,
  488. 0x2b, 0x24, 0x03, 0x02, 0x01,
  489. 0x05, 0x00, 0x04, 0x14
  490. };
  491. static const u8 digest_info_sha224[] = {
  492. 0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09,
  493. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04,
  494. 0x05, 0x00, 0x04, 0x1c
  495. };
  496. static const u8 digest_info_sha256[] = {
  497. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09,
  498. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
  499. 0x05, 0x00, 0x04, 0x20
  500. };
  501. static const u8 digest_info_sha384[] = {
  502. 0x30, 0x41, 0x30, 0x0d, 0x06, 0x09,
  503. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02,
  504. 0x05, 0x00, 0x04, 0x30
  505. };
  506. static const u8 digest_info_sha512[] = {
  507. 0x30, 0x51, 0x30, 0x0d, 0x06, 0x09,
  508. 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03,
  509. 0x05, 0x00, 0x04, 0x40
  510. };
  511. static const struct asn1_template {
  512. const char *name;
  513. const u8 *data;
  514. size_t size;
  515. } asn1_templates[] = {
  516. #define _(X) { #X, digest_info_##X, sizeof(digest_info_##X) }
  517. _(md5),
  518. _(sha1),
  519. _(rmd160),
  520. _(sha256),
  521. _(sha384),
  522. _(sha512),
  523. _(sha224),
  524. { NULL }
  525. #undef _
  526. };
  527. static const struct asn1_template *lookup_asn1(const char *name)
  528. {
  529. const struct asn1_template *p;
  530. for (p = asn1_templates; p->name; p++)
  531. if (strcmp(name, p->name) == 0)
  532. return p;
  533. return NULL;
  534. }
  535. /*
  536. * Sign operation is performed with the private key in the TPM.
  537. */
  538. static int tpm_key_sign(struct tpm_key *tk,
  539. struct kernel_pkey_params *params,
  540. const void *in, void *out)
  541. {
  542. struct tpm_buf *tb;
  543. uint32_t keyhandle;
  544. uint8_t srkauth[SHA1_DIGEST_SIZE];
  545. uint8_t keyauth[SHA1_DIGEST_SIZE];
  546. void *asn1_wrapped = NULL;
  547. uint32_t in_len = params->in_len;
  548. int r;
  549. pr_devel("==>%s()\n", __func__);
  550. if (strcmp(params->encoding, "pkcs1"))
  551. return -ENOPKG;
  552. if (params->hash_algo) {
  553. const struct asn1_template *asn1 =
  554. lookup_asn1(params->hash_algo);
  555. if (!asn1)
  556. return -ENOPKG;
  557. /* request enough space for the ASN.1 template + input hash */
  558. asn1_wrapped = kzalloc(in_len + asn1->size, GFP_KERNEL);
  559. if (!asn1_wrapped)
  560. return -ENOMEM;
  561. /* Copy ASN.1 template, then the input */
  562. memcpy(asn1_wrapped, asn1->data, asn1->size);
  563. memcpy(asn1_wrapped + asn1->size, in, in_len);
  564. in = asn1_wrapped;
  565. in_len += asn1->size;
  566. }
  567. if (in_len > tk->key_len / 8 - 11) {
  568. r = -EOVERFLOW;
  569. goto error_free_asn1_wrapped;
  570. }
  571. r = -ENOMEM;
  572. tb = kzalloc(sizeof(*tb), GFP_KERNEL);
  573. if (!tb)
  574. goto error_free_asn1_wrapped;
  575. /* TODO: Handle a non-all zero SRK authorization */
  576. memset(srkauth, 0, sizeof(srkauth));
  577. r = tpm_loadkey2(tb, SRKHANDLE, srkauth,
  578. tk->blob, tk->blob_len, &keyhandle);
  579. if (r < 0) {
  580. pr_devel("loadkey2 failed (%d)\n", r);
  581. goto error_free_tb;
  582. }
  583. /* TODO: Handle a non-all zero key authorization */
  584. memset(keyauth, 0, sizeof(keyauth));
  585. r = tpm_sign(tb, keyhandle, keyauth, in, in_len, out, params->out_len);
  586. if (r < 0)
  587. pr_devel("tpm_sign failed (%d)\n", r);
  588. if (tpm_flushspecific(tb, keyhandle) < 0)
  589. pr_devel("flushspecific failed (%d)\n", r);
  590. error_free_tb:
  591. kzfree(tb);
  592. error_free_asn1_wrapped:
  593. kfree(asn1_wrapped);
  594. pr_devel("<==%s() = %d\n", __func__, r);
  595. return r;
  596. }
  597. /*
  598. * Do encryption, decryption and signing ops.
  599. */
  600. static int tpm_key_eds_op(struct kernel_pkey_params *params,
  601. const void *in, void *out)
  602. {
  603. struct tpm_key *tk = params->key->payload.data[asym_crypto];
  604. int ret = -EOPNOTSUPP;
  605. /* Perform the encryption calculation. */
  606. switch (params->op) {
  607. case kernel_pkey_encrypt:
  608. ret = tpm_key_encrypt(tk, params, in, out);
  609. break;
  610. case kernel_pkey_decrypt:
  611. ret = tpm_key_decrypt(tk, params, in, out);
  612. break;
  613. case kernel_pkey_sign:
  614. ret = tpm_key_sign(tk, params, in, out);
  615. break;
  616. default:
  617. BUG();
  618. }
  619. return ret;
  620. }
  621. /*
  622. * Verify a signature using a public key.
  623. */
  624. static int tpm_key_verify_signature(const struct key *key,
  625. const struct public_key_signature *sig)
  626. {
  627. const struct tpm_key *tk = key->payload.data[asym_crypto];
  628. struct crypto_wait cwait;
  629. struct crypto_akcipher *tfm;
  630. struct akcipher_request *req;
  631. struct scatterlist sig_sg, digest_sg;
  632. char alg_name[CRYPTO_MAX_ALG_NAME];
  633. uint8_t der_pub_key[PUB_KEY_BUF_SIZE];
  634. uint32_t der_pub_key_len;
  635. void *output;
  636. unsigned int outlen;
  637. int ret;
  638. pr_devel("==>%s()\n", __func__);
  639. BUG_ON(!tk);
  640. BUG_ON(!sig);
  641. BUG_ON(!sig->s);
  642. if (!sig->digest)
  643. return -ENOPKG;
  644. ret = determine_akcipher(sig->encoding, sig->hash_algo, alg_name);
  645. if (ret < 0)
  646. return ret;
  647. tfm = crypto_alloc_akcipher(alg_name, 0, 0);
  648. if (IS_ERR(tfm))
  649. return PTR_ERR(tfm);
  650. der_pub_key_len = derive_pub_key(tk->pub_key, tk->pub_key_len,
  651. der_pub_key);
  652. ret = crypto_akcipher_set_pub_key(tfm, der_pub_key, der_pub_key_len);
  653. if (ret < 0)
  654. goto error_free_tfm;
  655. ret = -ENOMEM;
  656. req = akcipher_request_alloc(tfm, GFP_KERNEL);
  657. if (!req)
  658. goto error_free_tfm;
  659. ret = -ENOMEM;
  660. outlen = crypto_akcipher_maxsize(tfm);
  661. output = kmalloc(outlen, GFP_KERNEL);
  662. if (!output)
  663. goto error_free_req;
  664. sg_init_one(&sig_sg, sig->s, sig->s_size);
  665. sg_init_one(&digest_sg, output, outlen);
  666. akcipher_request_set_crypt(req, &sig_sg, &digest_sg, sig->s_size,
  667. outlen);
  668. crypto_init_wait(&cwait);
  669. akcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG |
  670. CRYPTO_TFM_REQ_MAY_SLEEP,
  671. crypto_req_done, &cwait);
  672. /* Perform the verification calculation. This doesn't actually do the
  673. * verification, but rather calculates the hash expected by the
  674. * signature and returns that to us.
  675. */
  676. ret = crypto_wait_req(crypto_akcipher_verify(req), &cwait);
  677. if (ret)
  678. goto out_free_output;
  679. /* Do the actual verification step. */
  680. if (req->dst_len != sig->digest_size ||
  681. memcmp(sig->digest, output, sig->digest_size) != 0)
  682. ret = -EKEYREJECTED;
  683. out_free_output:
  684. kfree(output);
  685. error_free_req:
  686. akcipher_request_free(req);
  687. error_free_tfm:
  688. crypto_free_akcipher(tfm);
  689. pr_devel("<==%s() = %d\n", __func__, ret);
  690. if (WARN_ON_ONCE(ret > 0))
  691. ret = -EINVAL;
  692. return ret;
  693. }
  694. /*
  695. * Parse enough information out of TPM_KEY structure:
  696. * TPM_STRUCT_VER -> 4 bytes
  697. * TPM_KEY_USAGE -> 2 bytes
  698. * TPM_KEY_FLAGS -> 4 bytes
  699. * TPM_AUTH_DATA_USAGE -> 1 byte
  700. * TPM_KEY_PARMS -> variable
  701. * UINT32 PCRInfoSize -> 4 bytes
  702. * BYTE* -> PCRInfoSize bytes
  703. * TPM_STORE_PUBKEY
  704. * UINT32 encDataSize;
  705. * BYTE* -> encDataSize;
  706. *
  707. * TPM_KEY_PARMS:
  708. * TPM_ALGORITHM_ID -> 4 bytes
  709. * TPM_ENC_SCHEME -> 2 bytes
  710. * TPM_SIG_SCHEME -> 2 bytes
  711. * UINT32 parmSize -> 4 bytes
  712. * BYTE* -> variable
  713. */
  714. static int extract_key_parameters(struct tpm_key *tk)
  715. {
  716. const void *cur = tk->blob;
  717. uint32_t len = tk->blob_len;
  718. const void *pub_key;
  719. uint32_t sz;
  720. uint32_t key_len;
  721. if (len < 11)
  722. return -EBADMSG;
  723. /* Ensure this is a legacy key */
  724. if (get_unaligned_be16(cur + 4) != 0x0015)
  725. return -EBADMSG;
  726. /* Skip to TPM_KEY_PARMS */
  727. cur += 11;
  728. len -= 11;
  729. if (len < 12)
  730. return -EBADMSG;
  731. /* Make sure this is an RSA key */
  732. if (get_unaligned_be32(cur) != 0x00000001)
  733. return -EBADMSG;
  734. /* Make sure this is TPM_ES_RSAESPKCSv15 encoding scheme */
  735. if (get_unaligned_be16(cur + 4) != 0x0002)
  736. return -EBADMSG;
  737. /* Make sure this is TPM_SS_RSASSAPKCS1v15_DER signature scheme */
  738. if (get_unaligned_be16(cur + 6) != 0x0003)
  739. return -EBADMSG;
  740. sz = get_unaligned_be32(cur + 8);
  741. if (len < sz + 12)
  742. return -EBADMSG;
  743. /* Move to TPM_RSA_KEY_PARMS */
  744. len -= 12;
  745. cur += 12;
  746. /* Grab the RSA key length */
  747. key_len = get_unaligned_be32(cur);
  748. switch (key_len) {
  749. case 512:
  750. case 1024:
  751. case 1536:
  752. case 2048:
  753. break;
  754. default:
  755. return -EINVAL;
  756. }
  757. /* Move just past TPM_KEY_PARMS */
  758. cur += sz;
  759. len -= sz;
  760. if (len < 4)
  761. return -EBADMSG;
  762. sz = get_unaligned_be32(cur);
  763. if (len < 4 + sz)
  764. return -EBADMSG;
  765. /* Move to TPM_STORE_PUBKEY */
  766. cur += 4 + sz;
  767. len -= 4 + sz;
  768. /* Grab the size of the public key, it should jive with the key size */
  769. sz = get_unaligned_be32(cur);
  770. if (sz > 256)
  771. return -EINVAL;
  772. pub_key = cur + 4;
  773. tk->key_len = key_len;
  774. tk->pub_key = pub_key;
  775. tk->pub_key_len = sz;
  776. return 0;
  777. }
  778. /* Given the blob, parse it and load it into the TPM */
  779. struct tpm_key *tpm_key_create(const void *blob, uint32_t blob_len)
  780. {
  781. int r;
  782. struct tpm_key *tk;
  783. r = tpm_is_tpm2(NULL);
  784. if (r < 0)
  785. goto error;
  786. /* We don't support TPM2 yet */
  787. if (r > 0) {
  788. r = -ENODEV;
  789. goto error;
  790. }
  791. r = -ENOMEM;
  792. tk = kzalloc(sizeof(struct tpm_key), GFP_KERNEL);
  793. if (!tk)
  794. goto error;
  795. tk->blob = kmemdup(blob, blob_len, GFP_KERNEL);
  796. if (!tk->blob)
  797. goto error_memdup;
  798. tk->blob_len = blob_len;
  799. r = extract_key_parameters(tk);
  800. if (r < 0)
  801. goto error_extract;
  802. return tk;
  803. error_extract:
  804. kfree(tk->blob);
  805. tk->blob_len = 0;
  806. error_memdup:
  807. kfree(tk);
  808. error:
  809. return ERR_PTR(r);
  810. }
  811. EXPORT_SYMBOL_GPL(tpm_key_create);
  812. /*
  813. * TPM-based asymmetric key subtype
  814. */
  815. struct asymmetric_key_subtype asym_tpm_subtype = {
  816. .owner = THIS_MODULE,
  817. .name = "asym_tpm",
  818. .name_len = sizeof("asym_tpm") - 1,
  819. .describe = asym_tpm_describe,
  820. .destroy = asym_tpm_destroy,
  821. .query = tpm_key_query,
  822. .eds_op = tpm_key_eds_op,
  823. .verify_signature = tpm_key_verify_signature,
  824. };
  825. EXPORT_SYMBOL_GPL(asym_tpm_subtype);
  826. MODULE_DESCRIPTION("TPM based asymmetric key subtype");
  827. MODULE_AUTHOR("Intel Corporation");
  828. MODULE_LICENSE("GPL v2");