encrypted.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  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, u8 **master_key,
  271. size_t *master_keylen)
  272. {
  273. 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 = ukey->payload.data;
  280. *master_key = upayload->data;
  281. *master_keylen = upayload->datalen;
  282. error:
  283. return ukey;
  284. }
  285. static struct sdesc *alloc_sdesc(struct crypto_shash *alg)
  286. {
  287. struct sdesc *sdesc;
  288. int size;
  289. size = sizeof(struct shash_desc) + crypto_shash_descsize(alg);
  290. sdesc = kmalloc(size, GFP_KERNEL);
  291. if (!sdesc)
  292. return ERR_PTR(-ENOMEM);
  293. sdesc->shash.tfm = alg;
  294. sdesc->shash.flags = 0x0;
  295. return sdesc;
  296. }
  297. static int calc_hmac(u8 *digest, const u8 *key, unsigned int keylen,
  298. const u8 *buf, unsigned int buflen)
  299. {
  300. struct sdesc *sdesc;
  301. int ret;
  302. sdesc = alloc_sdesc(hmacalg);
  303. if (IS_ERR(sdesc)) {
  304. pr_info("encrypted_key: can't alloc %s\n", hmac_alg);
  305. return PTR_ERR(sdesc);
  306. }
  307. ret = crypto_shash_setkey(hmacalg, key, keylen);
  308. if (!ret)
  309. ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
  310. kfree(sdesc);
  311. return ret;
  312. }
  313. static int calc_hash(u8 *digest, const u8 *buf, unsigned int buflen)
  314. {
  315. struct sdesc *sdesc;
  316. int ret;
  317. sdesc = alloc_sdesc(hashalg);
  318. if (IS_ERR(sdesc)) {
  319. pr_info("encrypted_key: can't alloc %s\n", hash_alg);
  320. return PTR_ERR(sdesc);
  321. }
  322. ret = crypto_shash_digest(&sdesc->shash, buf, buflen, digest);
  323. kfree(sdesc);
  324. return ret;
  325. }
  326. enum derived_key_type { ENC_KEY, AUTH_KEY };
  327. /* Derive authentication/encryption key from trusted key */
  328. static int get_derived_key(u8 *derived_key, enum derived_key_type key_type,
  329. const u8 *master_key, size_t master_keylen)
  330. {
  331. u8 *derived_buf;
  332. unsigned int derived_buf_len;
  333. int ret;
  334. derived_buf_len = strlen("AUTH_KEY") + 1 + master_keylen;
  335. if (derived_buf_len < HASH_SIZE)
  336. derived_buf_len = HASH_SIZE;
  337. derived_buf = kzalloc(derived_buf_len, GFP_KERNEL);
  338. if (!derived_buf) {
  339. pr_err("encrypted_key: out of memory\n");
  340. return -ENOMEM;
  341. }
  342. if (key_type)
  343. strcpy(derived_buf, "AUTH_KEY");
  344. else
  345. strcpy(derived_buf, "ENC_KEY");
  346. memcpy(derived_buf + strlen(derived_buf) + 1, master_key,
  347. master_keylen);
  348. ret = calc_hash(derived_key, derived_buf, derived_buf_len);
  349. kfree(derived_buf);
  350. return ret;
  351. }
  352. static int init_blkcipher_desc(struct blkcipher_desc *desc, const u8 *key,
  353. unsigned int key_len, const u8 *iv,
  354. unsigned int ivsize)
  355. {
  356. int ret;
  357. desc->tfm = crypto_alloc_blkcipher(blkcipher_alg, 0, CRYPTO_ALG_ASYNC);
  358. if (IS_ERR(desc->tfm)) {
  359. pr_err("encrypted_key: failed to load %s transform (%ld)\n",
  360. blkcipher_alg, PTR_ERR(desc->tfm));
  361. return PTR_ERR(desc->tfm);
  362. }
  363. desc->flags = 0;
  364. ret = crypto_blkcipher_setkey(desc->tfm, key, key_len);
  365. if (ret < 0) {
  366. pr_err("encrypted_key: failed to setkey (%d)\n", ret);
  367. crypto_free_blkcipher(desc->tfm);
  368. return ret;
  369. }
  370. crypto_blkcipher_set_iv(desc->tfm, iv, ivsize);
  371. return 0;
  372. }
  373. static struct key *request_master_key(struct encrypted_key_payload *epayload,
  374. u8 **master_key, size_t *master_keylen)
  375. {
  376. struct key *mkey = NULL;
  377. if (!strncmp(epayload->master_desc, KEY_TRUSTED_PREFIX,
  378. KEY_TRUSTED_PREFIX_LEN)) {
  379. mkey = request_trusted_key(epayload->master_desc +
  380. KEY_TRUSTED_PREFIX_LEN,
  381. master_key, master_keylen);
  382. } else if (!strncmp(epayload->master_desc, KEY_USER_PREFIX,
  383. KEY_USER_PREFIX_LEN)) {
  384. mkey = request_user_key(epayload->master_desc +
  385. KEY_USER_PREFIX_LEN,
  386. master_key, master_keylen);
  387. } else
  388. goto out;
  389. if (IS_ERR(mkey)) {
  390. int ret = PTR_ERR(mkey);
  391. if (ret == -ENOTSUPP)
  392. pr_info("encrypted_key: key %s not supported",
  393. epayload->master_desc);
  394. else
  395. pr_info("encrypted_key: key %s not found",
  396. epayload->master_desc);
  397. goto out;
  398. }
  399. dump_master_key(*master_key, *master_keylen);
  400. out:
  401. return mkey;
  402. }
  403. /* Before returning data to userspace, encrypt decrypted data. */
  404. static int derived_key_encrypt(struct encrypted_key_payload *epayload,
  405. const u8 *derived_key,
  406. unsigned int derived_keylen)
  407. {
  408. struct scatterlist sg_in[2];
  409. struct scatterlist sg_out[1];
  410. struct blkcipher_desc desc;
  411. unsigned int encrypted_datalen;
  412. unsigned int padlen;
  413. char pad[16];
  414. int ret;
  415. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  416. padlen = encrypted_datalen - epayload->decrypted_datalen;
  417. ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
  418. epayload->iv, ivsize);
  419. if (ret < 0)
  420. goto out;
  421. dump_decrypted_data(epayload);
  422. memset(pad, 0, sizeof pad);
  423. sg_init_table(sg_in, 2);
  424. sg_set_buf(&sg_in[0], epayload->decrypted_data,
  425. epayload->decrypted_datalen);
  426. sg_set_buf(&sg_in[1], pad, padlen);
  427. sg_init_table(sg_out, 1);
  428. sg_set_buf(sg_out, epayload->encrypted_data, encrypted_datalen);
  429. ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, encrypted_datalen);
  430. crypto_free_blkcipher(desc.tfm);
  431. if (ret < 0)
  432. pr_err("encrypted_key: failed to encrypt (%d)\n", ret);
  433. else
  434. dump_encrypted_data(epayload, encrypted_datalen);
  435. out:
  436. return ret;
  437. }
  438. static int datablob_hmac_append(struct encrypted_key_payload *epayload,
  439. const u8 *master_key, size_t master_keylen)
  440. {
  441. u8 derived_key[HASH_SIZE];
  442. u8 *digest;
  443. int ret;
  444. ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
  445. if (ret < 0)
  446. goto out;
  447. digest = epayload->format + epayload->datablob_len;
  448. ret = calc_hmac(digest, derived_key, sizeof derived_key,
  449. epayload->format, epayload->datablob_len);
  450. if (!ret)
  451. dump_hmac(NULL, digest, HASH_SIZE);
  452. out:
  453. return ret;
  454. }
  455. /* verify HMAC before decrypting encrypted key */
  456. static int datablob_hmac_verify(struct encrypted_key_payload *epayload,
  457. const u8 *format, const u8 *master_key,
  458. size_t master_keylen)
  459. {
  460. u8 derived_key[HASH_SIZE];
  461. u8 digest[HASH_SIZE];
  462. int ret;
  463. char *p;
  464. unsigned short len;
  465. ret = get_derived_key(derived_key, AUTH_KEY, master_key, master_keylen);
  466. if (ret < 0)
  467. goto out;
  468. len = epayload->datablob_len;
  469. if (!format) {
  470. p = epayload->master_desc;
  471. len -= strlen(epayload->format) + 1;
  472. } else
  473. p = epayload->format;
  474. ret = calc_hmac(digest, derived_key, sizeof derived_key, p, len);
  475. if (ret < 0)
  476. goto out;
  477. ret = memcmp(digest, epayload->format + epayload->datablob_len,
  478. sizeof digest);
  479. if (ret) {
  480. ret = -EINVAL;
  481. dump_hmac("datablob",
  482. epayload->format + epayload->datablob_len,
  483. HASH_SIZE);
  484. dump_hmac("calc", digest, HASH_SIZE);
  485. }
  486. out:
  487. return ret;
  488. }
  489. static int derived_key_decrypt(struct encrypted_key_payload *epayload,
  490. const u8 *derived_key,
  491. unsigned int derived_keylen)
  492. {
  493. struct scatterlist sg_in[1];
  494. struct scatterlist sg_out[2];
  495. struct blkcipher_desc desc;
  496. unsigned int encrypted_datalen;
  497. char pad[16];
  498. int ret;
  499. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  500. ret = init_blkcipher_desc(&desc, derived_key, derived_keylen,
  501. epayload->iv, ivsize);
  502. if (ret < 0)
  503. goto out;
  504. dump_encrypted_data(epayload, encrypted_datalen);
  505. memset(pad, 0, sizeof pad);
  506. sg_init_table(sg_in, 1);
  507. sg_init_table(sg_out, 2);
  508. sg_set_buf(sg_in, epayload->encrypted_data, encrypted_datalen);
  509. sg_set_buf(&sg_out[0], epayload->decrypted_data,
  510. epayload->decrypted_datalen);
  511. sg_set_buf(&sg_out[1], pad, sizeof pad);
  512. ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, encrypted_datalen);
  513. crypto_free_blkcipher(desc.tfm);
  514. if (ret < 0)
  515. goto out;
  516. dump_decrypted_data(epayload);
  517. out:
  518. return ret;
  519. }
  520. /* Allocate memory for decrypted key and datablob. */
  521. static struct encrypted_key_payload *encrypted_key_alloc(struct key *key,
  522. const char *format,
  523. const char *master_desc,
  524. const char *datalen)
  525. {
  526. struct encrypted_key_payload *epayload = NULL;
  527. unsigned short datablob_len;
  528. unsigned short decrypted_datalen;
  529. unsigned short payload_datalen;
  530. unsigned int encrypted_datalen;
  531. unsigned int format_len;
  532. long dlen;
  533. int ret;
  534. ret = strict_strtol(datalen, 10, &dlen);
  535. if (ret < 0 || dlen < MIN_DATA_SIZE || dlen > MAX_DATA_SIZE)
  536. return ERR_PTR(-EINVAL);
  537. format_len = (!format) ? strlen(key_format_default) : strlen(format);
  538. decrypted_datalen = dlen;
  539. payload_datalen = decrypted_datalen;
  540. if (format && !strcmp(format, key_format_ecryptfs)) {
  541. if (dlen != ECRYPTFS_MAX_KEY_BYTES) {
  542. pr_err("encrypted_key: keylen for the ecryptfs format "
  543. "must be equal to %d bytes\n",
  544. ECRYPTFS_MAX_KEY_BYTES);
  545. return ERR_PTR(-EINVAL);
  546. }
  547. decrypted_datalen = ECRYPTFS_MAX_KEY_BYTES;
  548. payload_datalen = sizeof(struct ecryptfs_auth_tok);
  549. }
  550. encrypted_datalen = roundup(decrypted_datalen, blksize);
  551. datablob_len = format_len + 1 + strlen(master_desc) + 1
  552. + strlen(datalen) + 1 + ivsize + 1 + encrypted_datalen;
  553. ret = key_payload_reserve(key, payload_datalen + datablob_len
  554. + HASH_SIZE + 1);
  555. if (ret < 0)
  556. return ERR_PTR(ret);
  557. epayload = kzalloc(sizeof(*epayload) + payload_datalen +
  558. datablob_len + HASH_SIZE + 1, GFP_KERNEL);
  559. if (!epayload)
  560. return ERR_PTR(-ENOMEM);
  561. epayload->payload_datalen = payload_datalen;
  562. epayload->decrypted_datalen = decrypted_datalen;
  563. epayload->datablob_len = datablob_len;
  564. return epayload;
  565. }
  566. static int encrypted_key_decrypt(struct encrypted_key_payload *epayload,
  567. const char *format, const char *hex_encoded_iv)
  568. {
  569. struct key *mkey;
  570. u8 derived_key[HASH_SIZE];
  571. u8 *master_key;
  572. u8 *hmac;
  573. const char *hex_encoded_data;
  574. unsigned int encrypted_datalen;
  575. size_t master_keylen;
  576. size_t asciilen;
  577. int ret;
  578. encrypted_datalen = roundup(epayload->decrypted_datalen, blksize);
  579. asciilen = (ivsize + 1 + encrypted_datalen + HASH_SIZE) * 2;
  580. if (strlen(hex_encoded_iv) != asciilen)
  581. return -EINVAL;
  582. hex_encoded_data = hex_encoded_iv + (2 * ivsize) + 2;
  583. ret = hex2bin(epayload->iv, hex_encoded_iv, ivsize);
  584. if (ret < 0)
  585. return -EINVAL;
  586. ret = hex2bin(epayload->encrypted_data, hex_encoded_data,
  587. encrypted_datalen);
  588. if (ret < 0)
  589. return -EINVAL;
  590. hmac = epayload->format + epayload->datablob_len;
  591. ret = hex2bin(hmac, hex_encoded_data + (encrypted_datalen * 2),
  592. HASH_SIZE);
  593. if (ret < 0)
  594. return -EINVAL;
  595. mkey = request_master_key(epayload, &master_key, &master_keylen);
  596. if (IS_ERR(mkey))
  597. return PTR_ERR(mkey);
  598. ret = datablob_hmac_verify(epayload, format, master_key, master_keylen);
  599. if (ret < 0) {
  600. pr_err("encrypted_key: bad hmac (%d)\n", ret);
  601. goto out;
  602. }
  603. ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
  604. if (ret < 0)
  605. goto out;
  606. ret = derived_key_decrypt(epayload, derived_key, sizeof derived_key);
  607. if (ret < 0)
  608. pr_err("encrypted_key: failed to decrypt key (%d)\n", ret);
  609. out:
  610. up_read(&mkey->sem);
  611. key_put(mkey);
  612. return ret;
  613. }
  614. static void __ekey_init(struct encrypted_key_payload *epayload,
  615. const char *format, const char *master_desc,
  616. const char *datalen)
  617. {
  618. unsigned int format_len;
  619. format_len = (!format) ? strlen(key_format_default) : strlen(format);
  620. epayload->format = epayload->payload_data + epayload->payload_datalen;
  621. epayload->master_desc = epayload->format + format_len + 1;
  622. epayload->datalen = epayload->master_desc + strlen(master_desc) + 1;
  623. epayload->iv = epayload->datalen + strlen(datalen) + 1;
  624. epayload->encrypted_data = epayload->iv + ivsize + 1;
  625. epayload->decrypted_data = epayload->payload_data;
  626. if (!format)
  627. memcpy(epayload->format, key_format_default, format_len);
  628. else {
  629. if (!strcmp(format, key_format_ecryptfs))
  630. epayload->decrypted_data =
  631. ecryptfs_get_auth_tok_key((struct ecryptfs_auth_tok *)epayload->payload_data);
  632. memcpy(epayload->format, format, format_len);
  633. }
  634. memcpy(epayload->master_desc, master_desc, strlen(master_desc));
  635. memcpy(epayload->datalen, datalen, strlen(datalen));
  636. }
  637. /*
  638. * encrypted_init - initialize an encrypted key
  639. *
  640. * For a new key, use a random number for both the iv and data
  641. * itself. For an old key, decrypt the hex encoded data.
  642. */
  643. static int encrypted_init(struct encrypted_key_payload *epayload,
  644. const char *key_desc, const char *format,
  645. const char *master_desc, const char *datalen,
  646. const char *hex_encoded_iv)
  647. {
  648. int ret = 0;
  649. if (format && !strcmp(format, key_format_ecryptfs)) {
  650. ret = valid_ecryptfs_desc(key_desc);
  651. if (ret < 0)
  652. return ret;
  653. ecryptfs_fill_auth_tok((struct ecryptfs_auth_tok *)epayload->payload_data,
  654. key_desc);
  655. }
  656. __ekey_init(epayload, format, master_desc, datalen);
  657. if (!hex_encoded_iv) {
  658. get_random_bytes(epayload->iv, ivsize);
  659. get_random_bytes(epayload->decrypted_data,
  660. epayload->decrypted_datalen);
  661. } else
  662. ret = encrypted_key_decrypt(epayload, format, hex_encoded_iv);
  663. return ret;
  664. }
  665. /*
  666. * encrypted_instantiate - instantiate an encrypted key
  667. *
  668. * Decrypt an existing encrypted datablob or create a new encrypted key
  669. * based on a kernel random number.
  670. *
  671. * On success, return 0. Otherwise return errno.
  672. */
  673. static int encrypted_instantiate(struct key *key, const void *data,
  674. size_t datalen)
  675. {
  676. struct encrypted_key_payload *epayload = NULL;
  677. char *datablob = NULL;
  678. const char *format = NULL;
  679. char *master_desc = NULL;
  680. char *decrypted_datalen = NULL;
  681. char *hex_encoded_iv = NULL;
  682. int ret;
  683. if (datalen <= 0 || datalen > 32767 || !data)
  684. return -EINVAL;
  685. datablob = kmalloc(datalen + 1, GFP_KERNEL);
  686. if (!datablob)
  687. return -ENOMEM;
  688. datablob[datalen] = 0;
  689. memcpy(datablob, data, datalen);
  690. ret = datablob_parse(datablob, &format, &master_desc,
  691. &decrypted_datalen, &hex_encoded_iv);
  692. if (ret < 0)
  693. goto out;
  694. epayload = encrypted_key_alloc(key, format, master_desc,
  695. decrypted_datalen);
  696. if (IS_ERR(epayload)) {
  697. ret = PTR_ERR(epayload);
  698. goto out;
  699. }
  700. ret = encrypted_init(epayload, key->description, format, master_desc,
  701. decrypted_datalen, hex_encoded_iv);
  702. if (ret < 0) {
  703. kfree(epayload);
  704. goto out;
  705. }
  706. rcu_assign_keypointer(key, epayload);
  707. out:
  708. kfree(datablob);
  709. return ret;
  710. }
  711. static void encrypted_rcu_free(struct rcu_head *rcu)
  712. {
  713. struct encrypted_key_payload *epayload;
  714. epayload = container_of(rcu, struct encrypted_key_payload, rcu);
  715. memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
  716. kfree(epayload);
  717. }
  718. /*
  719. * encrypted_update - update the master key description
  720. *
  721. * Change the master key description for an existing encrypted key.
  722. * The next read will return an encrypted datablob using the new
  723. * master key description.
  724. *
  725. * On success, return 0. Otherwise return errno.
  726. */
  727. static int encrypted_update(struct key *key, const void *data, size_t datalen)
  728. {
  729. struct encrypted_key_payload *epayload = key->payload.data;
  730. struct encrypted_key_payload *new_epayload;
  731. char *buf;
  732. char *new_master_desc = NULL;
  733. const char *format = NULL;
  734. int ret = 0;
  735. if (test_bit(KEY_FLAG_NEGATIVE, &key->flags))
  736. return -ENOKEY;
  737. if (datalen <= 0 || datalen > 32767 || !data)
  738. return -EINVAL;
  739. buf = kmalloc(datalen + 1, GFP_KERNEL);
  740. if (!buf)
  741. return -ENOMEM;
  742. buf[datalen] = 0;
  743. memcpy(buf, data, datalen);
  744. ret = datablob_parse(buf, &format, &new_master_desc, NULL, NULL);
  745. if (ret < 0)
  746. goto out;
  747. ret = valid_master_desc(new_master_desc, epayload->master_desc);
  748. if (ret < 0)
  749. goto out;
  750. new_epayload = encrypted_key_alloc(key, epayload->format,
  751. new_master_desc, epayload->datalen);
  752. if (IS_ERR(new_epayload)) {
  753. ret = PTR_ERR(new_epayload);
  754. goto out;
  755. }
  756. __ekey_init(new_epayload, epayload->format, new_master_desc,
  757. epayload->datalen);
  758. memcpy(new_epayload->iv, epayload->iv, ivsize);
  759. memcpy(new_epayload->payload_data, epayload->payload_data,
  760. epayload->payload_datalen);
  761. rcu_assign_keypointer(key, new_epayload);
  762. call_rcu(&epayload->rcu, encrypted_rcu_free);
  763. out:
  764. kfree(buf);
  765. return ret;
  766. }
  767. /*
  768. * encrypted_read - format and copy the encrypted data to userspace
  769. *
  770. * The resulting datablob format is:
  771. * <master-key name> <decrypted data length> <encrypted iv> <encrypted data>
  772. *
  773. * On success, return to userspace the encrypted key datablob size.
  774. */
  775. static long encrypted_read(const struct key *key, char __user *buffer,
  776. size_t buflen)
  777. {
  778. struct encrypted_key_payload *epayload;
  779. struct key *mkey;
  780. u8 *master_key;
  781. size_t master_keylen;
  782. char derived_key[HASH_SIZE];
  783. char *ascii_buf;
  784. size_t asciiblob_len;
  785. int ret;
  786. epayload = rcu_dereference_key(key);
  787. /* returns the hex encoded iv, encrypted-data, and hmac as ascii */
  788. asciiblob_len = epayload->datablob_len + ivsize + 1
  789. + roundup(epayload->decrypted_datalen, blksize)
  790. + (HASH_SIZE * 2);
  791. if (!buffer || buflen < asciiblob_len)
  792. return asciiblob_len;
  793. mkey = request_master_key(epayload, &master_key, &master_keylen);
  794. if (IS_ERR(mkey))
  795. return PTR_ERR(mkey);
  796. ret = get_derived_key(derived_key, ENC_KEY, master_key, master_keylen);
  797. if (ret < 0)
  798. goto out;
  799. ret = derived_key_encrypt(epayload, derived_key, sizeof derived_key);
  800. if (ret < 0)
  801. goto out;
  802. ret = datablob_hmac_append(epayload, master_key, master_keylen);
  803. if (ret < 0)
  804. goto out;
  805. ascii_buf = datablob_format(epayload, asciiblob_len);
  806. if (!ascii_buf) {
  807. ret = -ENOMEM;
  808. goto out;
  809. }
  810. up_read(&mkey->sem);
  811. key_put(mkey);
  812. if (copy_to_user(buffer, ascii_buf, asciiblob_len) != 0)
  813. ret = -EFAULT;
  814. kfree(ascii_buf);
  815. return asciiblob_len;
  816. out:
  817. up_read(&mkey->sem);
  818. key_put(mkey);
  819. return ret;
  820. }
  821. /*
  822. * encrypted_destroy - before freeing the key, clear the decrypted data
  823. *
  824. * Before freeing the key, clear the memory containing the decrypted
  825. * key data.
  826. */
  827. static void encrypted_destroy(struct key *key)
  828. {
  829. struct encrypted_key_payload *epayload = key->payload.data;
  830. if (!epayload)
  831. return;
  832. memset(epayload->decrypted_data, 0, epayload->decrypted_datalen);
  833. kfree(key->payload.data);
  834. }
  835. struct key_type key_type_encrypted = {
  836. .name = "encrypted",
  837. .instantiate = encrypted_instantiate,
  838. .update = encrypted_update,
  839. .match = user_match,
  840. .destroy = encrypted_destroy,
  841. .describe = user_describe,
  842. .read = encrypted_read,
  843. };
  844. EXPORT_SYMBOL_GPL(key_type_encrypted);
  845. static void encrypted_shash_release(void)
  846. {
  847. if (hashalg)
  848. crypto_free_shash(hashalg);
  849. if (hmacalg)
  850. crypto_free_shash(hmacalg);
  851. }
  852. static int __init encrypted_shash_alloc(void)
  853. {
  854. int ret;
  855. hmacalg = crypto_alloc_shash(hmac_alg, 0, CRYPTO_ALG_ASYNC);
  856. if (IS_ERR(hmacalg)) {
  857. pr_info("encrypted_key: could not allocate crypto %s\n",
  858. hmac_alg);
  859. return PTR_ERR(hmacalg);
  860. }
  861. hashalg = crypto_alloc_shash(hash_alg, 0, CRYPTO_ALG_ASYNC);
  862. if (IS_ERR(hashalg)) {
  863. pr_info("encrypted_key: could not allocate crypto %s\n",
  864. hash_alg);
  865. ret = PTR_ERR(hashalg);
  866. goto hashalg_fail;
  867. }
  868. return 0;
  869. hashalg_fail:
  870. crypto_free_shash(hmacalg);
  871. return ret;
  872. }
  873. static int __init init_encrypted(void)
  874. {
  875. int ret;
  876. ret = encrypted_shash_alloc();
  877. if (ret < 0)
  878. return ret;
  879. ret = aes_get_sizes();
  880. if (ret < 0)
  881. goto out;
  882. ret = register_key_type(&key_type_encrypted);
  883. if (ret < 0)
  884. goto out;
  885. return 0;
  886. out:
  887. encrypted_shash_release();
  888. return ret;
  889. }
  890. static void __exit cleanup_encrypted(void)
  891. {
  892. encrypted_shash_release();
  893. unregister_key_type(&key_type_encrypted);
  894. }
  895. late_initcall(init_encrypted);
  896. module_exit(cleanup_encrypted);
  897. MODULE_LICENSE("GPL");