encrypted.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. * Copyright (C) 2010 IBM Corporation
  3. *
  4. * Author:
  5. * Mimi Zohar <zohar@us.ibm.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * See Documentation/security/keys-trusted-encrypted.txt
  12. */
  13. #include <linux/uaccess.h>
  14. #include <linux/module.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/parser.h>
  18. #include <linux/string.h>
  19. #include <linux/err.h>
  20. #include <keys/user-type.h>
  21. #include <keys/trusted-type.h>
  22. #include <keys/encrypted-type.h>
  23. #include <linux/key-type.h>
  24. #include <linux/random.h>
  25. #include <linux/rcupdate.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/crypto.h>
  28. #include <crypto/hash.h>
  29. #include <crypto/sha.h>
  30. #include <crypto/aes.h>
  31. #include "encrypted.h"
  32. static const char KEY_TRUSTED_PREFIX[] = "trusted:";
  33. static const char KEY_USER_PREFIX[] = "user:";
  34. static const char hash_alg[] = "sha256";
  35. static const char hmac_alg[] = "hmac(sha256)";
  36. static const char blkcipher_alg[] = "cbc(aes)";
  37. static unsigned int ivsize;
  38. static int blksize;
  39. #define KEY_TRUSTED_PREFIX_LEN (sizeof (KEY_TRUSTED_PREFIX) - 1)
  40. #define KEY_USER_PREFIX_LEN (sizeof (KEY_USER_PREFIX) - 1)
  41. #define HASH_SIZE SHA256_DIGEST_SIZE
  42. #define MAX_DATA_SIZE 4096
  43. #define MIN_DATA_SIZE 20
  44. struct sdesc {
  45. struct shash_desc shash;
  46. char ctx[];
  47. };
  48. static struct crypto_shash *hashalg;
  49. static struct crypto_shash *hmacalg;
  50. enum {
  51. Opt_err = -1, Opt_new, Opt_load, Opt_update
  52. };
  53. static const match_table_t key_tokens = {
  54. {Opt_new, "new"},
  55. {Opt_load, "load"},
  56. {Opt_update, "update"},
  57. {Opt_err, NULL}
  58. };
  59. static int aes_get_sizes(void)
  60. {
  61. struct crypto_blkcipher *tfm;
  62. tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
  63. if (IS_ERR(tfm)) {
  64. pr_err("encrypted_key: failed to alloc_cipher (%ld)\n",
  65. PTR_ERR(tfm));
  66. return PTR_ERR(tfm);
  67. }
  68. ivsize = crypto_blkcipher_ivsize(tfm);
  69. blksize = crypto_blkcipher_blocksize(tfm);
  70. crypto_free_blkcipher(tfm);
  71. return 0;
  72. }
  73. /*
  74. * valid_master_desc - verify the 'key-type:desc' of a new/updated master-key
  75. *
  76. * key-type:= "trusted:" | "encrypted:"
  77. * desc:= master-key description
  78. *
  79. * Verify that 'key-type' is valid and that 'desc' exists. On key update,
  80. * only the master key description is permitted to change, not the key-type.
  81. * The key-type remains constant.
  82. *
  83. * On success returns 0, otherwise -EINVAL.
  84. */
  85. static int valid_master_desc(const char *new_desc, const char *orig_desc)
  86. {
  87. if (!memcmp(new_desc, KEY_TRUSTED_PREFIX, KEY_TRUSTED_PREFIX_LEN)) {
  88. if (strlen(new_desc) == KEY_TRUSTED_PREFIX_LEN)
  89. goto out;
  90. if (orig_desc)
  91. if (memcmp(new_desc, orig_desc, KEY_TRUSTED_PREFIX_LEN))
  92. goto out;
  93. } else if (!memcmp(new_desc, KEY_USER_PREFIX, KEY_USER_PREFIX_LEN)) {
  94. if (strlen(new_desc) == KEY_USER_PREFIX_LEN)
  95. goto out;
  96. if (orig_desc)
  97. if (memcmp(new_desc, orig_desc, KEY_USER_PREFIX_LEN))
  98. goto out;
  99. } else
  100. goto out;
  101. return 0;
  102. out:
  103. return -EINVAL;
  104. }
  105. /*
  106. * datablob_parse - parse the keyctl data
  107. *
  108. * datablob format:
  109. * new <master-key name> <decrypted data length>
  110. * load <master-key name> <decrypted data length> <encrypted iv + data>
  111. * update <new-master-key name>
  112. *
  113. * Tokenizes a copy of the keyctl data, returning a pointer to each token,
  114. * which is null terminated.
  115. *
  116. * On success returns 0, otherwise -EINVAL.
  117. */
  118. static int datablob_parse(char *datablob, char **master_desc,
  119. char **decrypted_datalen, char **hex_encoded_iv)
  120. {
  121. substring_t args[MAX_OPT_ARGS];
  122. int ret = -EINVAL;
  123. int key_cmd;
  124. char *p;
  125. p = strsep(&datablob, " \t");
  126. if (!p)
  127. return ret;
  128. key_cmd = match_token(p, key_tokens, args);
  129. *master_desc = strsep(&datablob, " \t");
  130. if (!*master_desc)
  131. goto out;
  132. if (valid_master_desc(*master_desc, NULL) < 0)
  133. goto out;
  134. if (decrypted_datalen) {
  135. *decrypted_datalen = strsep(&datablob, " \t");
  136. if (!*decrypted_datalen)
  137. goto out;
  138. }
  139. switch (key_cmd) {
  140. case Opt_new:
  141. if (!decrypted_datalen)
  142. break;
  143. ret = 0;
  144. break;
  145. case Opt_load:
  146. if (!decrypted_datalen)
  147. break;
  148. *hex_encoded_iv = strsep(&datablob, " \t");
  149. if (!*hex_encoded_iv)
  150. break;
  151. ret = 0;
  152. break;
  153. case Opt_update:
  154. if (decrypted_datalen)
  155. break;
  156. ret = 0;
  157. break;
  158. case Opt_err:
  159. break;
  160. }
  161. out:
  162. return ret;
  163. }
  164. /*
  165. * datablob_format - format as an ascii string, before copying to userspace
  166. */
  167. static char *datablob_format(struct encrypted_key_payload *epayload,
  168. size_t asciiblob_len)
  169. {
  170. char *ascii_buf, *bufp;
  171. u8 *iv = epayload->iv;
  172. int len;
  173. int i;
  174. ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
  175. if (!ascii_buf)
  176. goto out;
  177. ascii_buf[asciiblob_len] = '\0';
  178. /* copy datablob master_desc and datalen strings */
  179. len = sprintf(ascii_buf, "%s %s ", epayload->master_desc,
  180. epayload->datalen);
  181. /* convert the hex encoded iv, encrypted-data and HMAC to ascii */
  182. bufp = &ascii_buf[len];
  183. for (i = 0; i < (asciiblob_len - len) / 2; i++)
  184. bufp = pack_hex_byte(bufp, iv[i]);
  185. out:
  186. return ascii_buf;
  187. }
  188. /*
  189. * request_trusted_key - request the trusted key
  190. *
  191. * Trusted keys are sealed to PCRs and other metadata. Although userspace
  192. * manages both trusted/encrypted key-types, like the encrypted key type
  193. * data, trusted key type data is not visible decrypted from userspace.
  194. */
  195. static struct key *request_trusted_key(const char *trusted_desc,
  196. u8 **master_key, size_t *master_keylen)
  197. {
  198. struct trusted_key_payload *tpayload;
  199. struct key *tkey;
  200. tkey = request_key(&key_type_trusted, trusted_desc, NULL);
  201. if (IS_ERR(tkey))
  202. goto error;
  203. down_read(&tkey->sem);
  204. tpayload = rcu_dereference(tkey->payload.data);
  205. *master_key = tpayload->key;
  206. *master_keylen = tpayload->key_len;
  207. error:
  208. return tkey;
  209. }
  210. /*
  211. * request_user_key - request the user key
  212. *
  213. * Use a user provided key to encrypt/decrypt an encrypted-key.
  214. */
  215. static struct key *request_user_key(const char *master_desc, u8 **master_key,
  216. size_t *master_keylen)
  217. {
  218. struct user_key_payload *upayload;
  219. struct key *ukey;
  220. ukey = request_key(&key_type_user, master_desc, NULL);
  221. if (IS_ERR(ukey))
  222. goto error;
  223. down_read(&ukey->sem);
  224. upayload = rcu_dereference(ukey->payload.data);
  225. *master_key = upayload->data;
  226. *master_keylen = upayload->datalen;
  227. error:
  228. return ukey;
  229. }
  230. static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
  231. {
  232. struct sdesc *sdesc;
  233. int size;
  234. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  235. sdesc = kmalloc(size, GFP_KERNEL);
  236. if (!sdesc)
  237. return ERR_PTR(-ENOMEM);
  238. sdesc->shash.tfm = alg;
  239. sdesc->shash.flags = 0x0;
  240. return sdesc;
  241. }
  242. static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
  243. const u8 *buf, unsigned int buflen)
  244. {
  245. struct sdesc *sdesc;
  246. int ret;
  247. sdesc = alloc_sdesc(hmacalg);
  248. if (IS_ERR(sdesc)) {
  249. pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
  250. return PTR_ERR(sdesc);
  251. }
  252. ret = crypto_shash_setkey(hmacalg, key, keylen);
  253. if (!ret)
  254. ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
  255. kfree(sdesc);
  256. return ret;
  257. }
  258. static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
  259. {
  260. struct sdesc *sdesc;
  261. int ret;
  262. sdesc = alloc_sdesc(hashalg);
  263. if (IS_ERR(sdesc)) {
  264. pr_info("encrypted_key: can't alloc %s\n", hash_alg);
  265. return PTR_ERR(sdesc);
  266. }
  267. ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
  268. kfree(sdesc);
  269. return ret;
  270. }
  271. enum derived_key_type { ENC_KEY, AUTH_KEY };
  272. /* Derive authentication/encryption key from trusted key */
  273. static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
  274. const u8 *master_key, size_t master_keylen)
  275. {
  276. u8 *derived_buf;
  277. unsigned int derived_buf_len;
  278. int ret;
  279. derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
  280. if (derived_buf_len < HASH_SIZE)
  281. derived_buf_len = HASH_SIZE;
  282. derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
  283. if (!derived_buf) {
  284. pr_err("encrypted_key: out of memory\n");
  285. return -ENOMEM;
  286. }
  287. if (key_type)
  288. strcpy(derived_buf, "AUTH_KEY");
  289. else
  290. strcpy(derived_buf, "ENC_KEY");
  291. memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
  292. master_keylen);
  293. ret = calc_hash(derived_key, derived_buf, derived_buf_len);
  294. kfree(derived_buf);
  295. return ret;
  296. }
  297. static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
  298. unsigned int key_len, const u8 *iv,
  299. unsigned int ivsize)
  300. {
  301. int ret;
  302. desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
  303. if (IS_ERR(desc->tfm)) {
  304. pr_err("encrypted_key: failed to load %s transform (%ld)\n",
  305. blkcipher_alg, PTR_ERR(desc->tfm));
  306. return PTR_ERR(desc->tfm);
  307. }
  308. desc->flags = 0;
  309. ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
  310. if (ret < 0) {
  311. pr_err("encrypted_key: failed to setkey (%d)\n", ret);
  312. crypto_free_blkcipher(desc->tfm);
  313. return ret;
  314. }
  315. crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
  316. return 0;
  317. }
  318. static struct key *request_master_key(struct encrypted_key_payload *epayload,
  319. u8 **master_key, size_t *master_keylen)
  320. {
  321. struct key *mkey = NULL;
  322. if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
  323. KEY_TRUSTED_PREFIX_LEN)) {
  324. mkey = request_trusted_key(epayload->master_desc +
  325. KEY_TRUSTED_PREFIX_LEN,
  326. master_key, master_keylen);
  327. } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
  328. KEY_USER_PREFIX_LEN)) {
  329. mkey = request_user_key(epayload->master_desc +
  330. KEY_USER_PREFIX_LEN,
  331. master_key, master_keylen);
  332. } else
  333. goto out;
  334. if (IS_ERR(mkey))
  335. pr_info("encrypted_key: key %s not found",
  336. epayload->master_desc);
  337. if (mkey)
  338. dump_master_key(*master_key, *master_keylen);
  339. out:
  340. return mkey;
  341. }
  342. /* Before returning data to userspace, encrypt decrypted data. */
  343. static int derived_key_encrypt(struct encrypted_key_payload *epayload,
  344. const u8 *derived_key,
  345. unsigned int derived_keylen)
  346. {
  347. struct scatterlist sg_in[2];
  348. struct scatterlist sg_out[1];
  349. struct blkcipher_desc desc;
  350. unsigned int encrypted_datalen;
  351. unsigned int padlen;
  352. char pad[16];
  353. int ret;
  354. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  355. padlen = encrypted_datalen - epayload->decrypted_datalen;
  356. ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
  357. epayload->iv, ivsize);
  358. if (ret < 0)
  359. goto out;
  360. dump_decrypted_data(epayload);
  361. memset(pad, 0, sizeof pad);
  362. sg_init_table(sg_in, 2);
  363. sg_set_buf(&sg_in[0], epayload->decrypted_data,
  364. epayload->decrypted_datalen);
  365. sg_set_buf(&sg_in[1], pad, padlen);
  366. sg_init_table(sg_out, 1);
  367. sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
  368. ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
  369. crypto_free_blkcipher(desc.tfm);
  370. if (ret < 0)
  371. pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
  372. else
  373. dump_encrypted_data(epayload, encrypted_datalen);
  374. out:
  375. return ret;
  376. }
  377. static int datablob_hmac_append(struct encrypted_key_payload *epayload,
  378. const u8 *master_key, size_t master_keylen)
  379. {
  380. u8 derived_key[HASH_SIZE];
  381. u8 *digest;
  382. int ret;
  383. ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
  384. if (ret < 0)
  385. goto out;
  386. digest = epayload->master_desc + epayload->datablob_len;
  387. ret = calc_hmac(digest, derived_key, sizeof derived_key,
  388. epayload->master_desc, epayload->datablob_len);
  389. if (!ret)
  390. dump_hmac(NULL, digest, HASH_SIZE);
  391. out:
  392. return ret;
  393. }
  394. /* verify HMAC before decrypting encrypted key */
  395. static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
  396. const u8 *master_key, size_t master_keylen)
  397. {
  398. u8 derived_key[HASH_SIZE];
  399. u8 digest[HASH_SIZE];
  400. int ret;
  401. ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
  402. if (ret < 0)
  403. goto out;
  404. ret = calc_hmac(digest, derived_key, sizeof derived_key,
  405. epayload->master_desc, epayload->datablob_len);
  406. if (ret < 0)
  407. goto out;
  408. ret = memcmp(digest, epayload->master_desc + epayload->datablob_len,
  409. sizeof digest);
  410. if (ret) {
  411. ret = -EINVAL;
  412. dump_hmac("datablob",
  413. epayload->master_desc + epayload->datablob_len,
  414. HASH_SIZE);
  415. dump_hmac("calc", digest, HASH_SIZE);
  416. }
  417. out:
  418. return ret;
  419. }
  420. static int derived_key_decrypt(struct encrypted_key_payload *epayload,
  421. const u8 *derived_key,
  422. unsigned int derived_keylen)
  423. {
  424. struct scatterlist sg_in[1];
  425. struct scatterlist sg_out[2];
  426. struct blkcipher_desc desc;
  427. unsigned int encrypted_datalen;
  428. char pad[16];
  429. int ret;
  430. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  431. ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
  432. epayload->iv, ivsize);
  433. if (ret < 0)
  434. goto out;
  435. dump_encrypted_data(epayload, encrypted_datalen);
  436. memset(pad, 0, sizeof pad);
  437. sg_init_table(sg_in, 1);
  438. sg_init_table(sg_out, 2);
  439. sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
  440. sg_set_buf(&sg_out[0], epayload->decrypted_data,
  441. epayload->decrypted_datalen);
  442. sg_set_buf(&sg_out[1], pad, sizeof pad);
  443. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen);
  444. crypto_free_blkcipher(desc.tfm);
  445. if (ret < 0)
  446. goto out;
  447. dump_decrypted_data(epayload);
  448. out:
  449. return ret;
  450. }
  451. /* Allocate memory for decrypted key and datablob. */
  452. static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
  453. const char *master_desc,
  454. const char *datalen)
  455. {
  456. struct encrypted_key_payload *epayload = NULL;
  457. unsigned short datablob_len;
  458. unsigned short decrypted_datalen;
  459. unsigned int encrypted_datalen;
  460. long dlen;
  461. int ret;
  462. ret = strict_strtol(datalen, 10, &dlen);
  463. if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
  464. return ERR_PTR(-EINVAL);
  465. decrypted_datalen = dlen;
  466. encrypted_datalen = roundup(decrypted_datalen, blksize);
  467. datablob_len = strlen(master_desc) + 1 + strlen(datalen) + 1
  468. + ivsize + 1 + encrypted_datalen;
  469. ret = key_payload_reserve(key, decrypted_datalen + datablob_len
  470. + HASH_SIZE + 1);
  471. if (ret < 0)
  472. return ERR_PTR(ret);
  473. epayload = kzalloc(sizeof(*epayload) + decrypted_datalen +
  474. datablob_len + HASH_SIZE + 1, GFP_KERNEL);
  475. if (!epayload)
  476. return ERR_PTR(-ENOMEM);
  477. epayload->decrypted_datalen = decrypted_datalen;
  478. epayload->datablob_len = datablob_len;
  479. return epayload;
  480. }
  481. static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
  482. const char *hex_encoded_iv)
  483. {
  484. struct key *mkey;
  485. u8 derived_key[HASH_SIZE];
  486. u8 *master_key;
  487. u8 *hmac;
  488. const char *hex_encoded_data;
  489. unsigned int encrypted_datalen;
  490. size_t master_keylen;
  491. size_t asciilen;
  492. int ret;
  493. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  494. asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
  495. if (strlen(hex_encoded_iv) != asciilen)
  496. return -EINVAL;
  497. hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
  498. hex2bin(epayload->iv, hex_encoded_iv, ivsize);
  499. hex2bin(epayload->encrypted_data, hex_encoded_data, encrypted_datalen);
  500. hmac = epayload->master_desc + epayload->datablob_len;
  501. hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2), HASH_SIZE);
  502. mkey = request_master_key(epayload, &master_key, &master_keylen);
  503. if (IS_ERR(mkey))
  504. return PTR_ERR(mkey);
  505. ret = datablob_hmac_verify(epayload, master_key, master_keylen);
  506. if (ret < 0) {
  507. pr_err("encrypted_key: bad hmac (%d)\n", ret);
  508. goto out;
  509. }
  510. ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
  511. if (ret < 0)
  512. goto out;
  513. ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
  514. if (ret < 0)
  515. pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
  516. out:
  517. up_read(&mkey->sem);
  518. key_put(mkey);
  519. return ret;
  520. }
  521. static void __ekey_init(struct encrypted_key_payload *epayload,
  522. const char *master_desc, const char *datalen)
  523. {
  524. epayload->master_desc = epayload->decrypted_data
  525. + epayload->decrypted_datalen;
  526. epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
  527. epayload->iv = epayload->datalen + strlen(datalen) + 1;
  528. epayload->encrypted_data = epayload->iv + ivsize + 1;
  529. memcpy(epayload->master_desc, master_desc, strlen(master_desc));
  530. memcpy(epayload->datalen, datalen, strlen(datalen));
  531. }
  532. /*
  533. * encrypted_init - initialize an encrypted key
  534. *
  535. * For a new key, use a random number for both the iv and data
  536. * itself. For an old key, decrypt the hex encoded data.
  537. */
  538. static int encrypted_init(struct encrypted_key_payload *epayload,
  539. const char *master_desc, const char *datalen,
  540. const char *hex_encoded_iv)
  541. {
  542. int ret = 0;
  543. __ekey_init(epayload, master_desc, datalen);
  544. if (!hex_encoded_iv) {
  545. get_random_bytes(epayload->iv, ivsize);
  546. get_random_bytes(epayload->decrypted_data,
  547. epayload->decrypted_datalen);
  548. } else
  549. ret = encrypted_key_decrypt(epayload, hex_encoded_iv);
  550. return ret;
  551. }
  552. /*
  553. * encrypted_instantiate - instantiate an encrypted key
  554. *
  555. * Decrypt an existing encrypted datablob or create a new encrypted key
  556. * based on a kernel random number.
  557. *
  558. * On success, return 0. Otherwise return errno.
  559. */
  560. static int encrypted_instantiate(struct key *key, const void *data,
  561. size_t datalen)
  562. {
  563. struct encrypted_key_payload *epayload = NULL;
  564. char *datablob = NULL;
  565. char *master_desc = NULL;
  566. char *decrypted_datalen = NULL;
  567. char *hex_encoded_iv = NULL;
  568. int ret;
  569. if (datalen <= 0 || datalen > 32767 || !data)
  570. return -EINVAL;
  571. datablob = kmalloc(datalen + 1, GFP_KERNEL);
  572. if (!datablob)
  573. return -ENOMEM;
  574. datablob[datalen] = 0;
  575. memcpy(datablob, data, datalen);
  576. ret = datablob_parse(datablob, &master_desc, &decrypted_datalen,
  577. &hex_encoded_iv);
  578. if (ret < 0)
  579. goto out;
  580. epayload = encrypted_key_alloc(key, master_desc, decrypted_datalen);
  581. if (IS_ERR(epayload)) {
  582. ret = PTR_ERR(epayload);
  583. goto out;
  584. }
  585. ret = encrypted_init(epayload, master_desc, decrypted_datalen,
  586. hex_encoded_iv);
  587. if (ret < 0) {
  588. kfree(epayload);
  589. goto out;
  590. }
  591. rcu_assign_pointer(key->payload.data, epayload);
  592. out:
  593. kfree(datablob);
  594. return ret;
  595. }
  596. static void encrypted_rcu_free(struct rcu_head *rcu)
  597. {
  598. struct encrypted_key_payload *epayload;
  599. epayload = container_of(rcu, struct encrypted_key_payload, rcu);
  600. memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
  601. kfree(epayload);
  602. }
  603. /*
  604. * encrypted_update - update the master key description
  605. *
  606. * Change the master key description for an existing encrypted key.
  607. * The next read will return an encrypted datablob using the new
  608. * master key description.
  609. *
  610. * On success, return 0. Otherwise return errno.
  611. */
  612. static int encrypted_update(struct key *key, const void *data, size_t datalen)
  613. {
  614. struct encrypted_key_payload *epayload = key->payload.data;
  615. struct encrypted_key_payload *new_epayload;
  616. char *buf;
  617. char *new_master_desc = NULL;
  618. int ret = 0;
  619. if (datalen <= 0 || datalen > 32767 || !data)
  620. return -EINVAL;
  621. buf = kmalloc(datalen + 1, GFP_KERNEL);
  622. if (!buf)
  623. return -ENOMEM;
  624. buf[datalen] = 0;
  625. memcpy(buf, data, datalen);
  626. ret = datablob_parse(buf, &new_master_desc, NULL, NULL);
  627. if (ret < 0)
  628. goto out;
  629. ret = valid_master_desc(new_master_desc, epayload->master_desc);
  630. if (ret < 0)
  631. goto out;
  632. new_epayload = encrypted_key_alloc(key, new_master_desc,
  633. epayload->datalen);
  634. if (IS_ERR(new_epayload)) {
  635. ret = PTR_ERR(new_epayload);
  636. goto out;
  637. }
  638. __ekey_init(new_epayload, new_master_desc, epayload->datalen);
  639. memcpy(new_epayload->iv, epayload->iv, ivsize);
  640. memcpy(new_epayload->decrypted_data, epayload->decrypted_data,
  641. epayload->decrypted_datalen);
  642. rcu_assign_pointer(key->payload.data, new_epayload);
  643. call_rcu(&epayload->rcu, encrypted_rcu_free);
  644. out:
  645. kfree(buf);
  646. return ret;
  647. }
  648. /*
  649. * encrypted_read - format and copy the encrypted data to userspace
  650. *
  651. * The resulting datablob format is:
  652. * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
  653. *
  654. * On success, return to userspace the encrypted key datablob size.
  655. */
  656. static long encrypted_read(const struct key *key, char __user *buffer,
  657. size_t buflen)
  658. {
  659. struct encrypted_key_payload *epayload;
  660. struct key *mkey;
  661. u8 *master_key;
  662. size_t master_keylen;
  663. char derived_key[HASH_SIZE];
  664. char *ascii_buf;
  665. size_t asciiblob_len;
  666. int ret;
  667. epayload = rcu_dereference_key(key);
  668. /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
  669. asciiblob_len = epayload->datablob_len + ivsize + 1
  670. + roundup(epayload->decrypted_datalen, blksize)
  671. + (HASH_SIZE * 2);
  672. if (!buffer || buflen < asciiblob_len)
  673. return asciiblob_len;
  674. mkey = request_master_key(epayload, &master_key, &master_keylen);
  675. if (IS_ERR(mkey))
  676. return PTR_ERR(mkey);
  677. ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
  678. if (ret < 0)
  679. goto out;
  680. ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
  681. if (ret < 0)
  682. goto out;
  683. ret = datablob_hmac_append(epayload, master_key, master_keylen);
  684. if (ret < 0)
  685. goto out;
  686. ascii_buf = datablob_format(epayload, asciiblob_len);
  687. if (!ascii_buf) {
  688. ret = -ENOMEM;
  689. goto out;
  690. }
  691. up_read(&mkey->sem);
  692. key_put(mkey);
  693. if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
  694. ret = -EFAULT;
  695. kfree(ascii_buf);
  696. return asciiblob_len;
  697. out:
  698. up_read(&mkey->sem);
  699. key_put(mkey);
  700. return ret;
  701. }
  702. /*
  703. * encrypted_destroy - before freeing the key, clear the decrypted data
  704. *
  705. * Before freeing the key, clear the memory containing the decrypted
  706. * key data.
  707. */
  708. static void encrypted_destroy(struct key *key)
  709. {
  710. struct encrypted_key_payload *epayload = key->payload.data;
  711. if (!epayload)
  712. return;
  713. memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
  714. kfree(key->payload.data);
  715. }
  716. struct key_type key_type_encrypted = {
  717. .name = "encrypted",
  718. .instantiate = encrypted_instantiate,
  719. .update = encrypted_update,
  720. .match = user_match,
  721. .destroy = encrypted_destroy,
  722. .describe = user_describe,
  723. .read = encrypted_read,
  724. };
  725. EXPORT_SYMBOL_GPL(key_type_encrypted);
  726. static void encrypted_shash_release(void)
  727. {
  728. if (hashalg)
  729. crypto_free_shash(hashalg);
  730. if (hmacalg)
  731. crypto_free_shash(hmacalg);
  732. }
  733. static int __init encrypted_shash_alloc(void)
  734. {
  735. int ret;
  736. hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
  737. if (IS_ERR(hmacalg)) {
  738. pr_info("encrypted_key: could not allocate crypto %s\n",
  739. hmac_alg);
  740. return PTR_ERR(hmacalg);
  741. }
  742. hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
  743. if (IS_ERR(hashalg)) {
  744. pr_info("encrypted_key: could not allocate crypto %s\n",
  745. hash_alg);
  746. ret = PTR_ERR(hashalg);
  747. goto hashalg_fail;
  748. }
  749. return 0;
  750. hashalg_fail:
  751. crypto_free_shash(hmacalg);
  752. return ret;
  753. }
  754. static int __init init_encrypted(void)
  755. {
  756. int ret;
  757. ret = encrypted_shash_alloc();
  758. if (ret < 0)
  759. return ret;
  760. ret = register_key_type(&key_type_encrypted);
  761. if (ret < 0)
  762. goto out;
  763. return aes_get_sizes();
  764. out:
  765. encrypted_shash_release();
  766. return ret;
  767. }
  768. static void __exit cleanup_encrypted(void)
  769. {
  770. encrypted_shash_release();
  771. unregister_key_type(&key_type_encrypted);
  772. }
  773. late_initcall(init_encrypted);
  774. module_exit(cleanup_encrypted);
  775. MODULE_LICENSE("GPL");