encrypted.c 26 KB

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