keyinfo.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. * key management facility for FS encryption support.
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption key functions.
  7. *
  8. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  9. */
  10. #include <keys/user-type.h>
  11. #include <linux/hashtable.h>
  12. #include <linux/scatterlist.h>
  13. #include <linux/ratelimit.h>
  14. #include <crypto/aes.h>
  15. #include <crypto/algapi.h>
  16. #include <crypto/sha.h>
  17. #include <crypto/skcipher.h>
  18. #include "fscrypt_private.h"
  19. #include "fscrypt_ice.h"
  20. static struct crypto_shash *essiv_hash_tfm;
  21. /* Table of keys referenced by FS_POLICY_FLAG_DIRECT_KEY policies */
  22. static DEFINE_HASHTABLE(fscrypt_master_keys, 6); /* 6 bits = 64 buckets */
  23. static DEFINE_SPINLOCK(fscrypt_master_keys_lock);
  24. /*
  25. * Key derivation function. This generates the derived key by encrypting the
  26. * master key with AES-128-ECB using the inode's nonce as the AES key.
  27. *
  28. * The master key must be at least as long as the derived key. If the master
  29. * key is longer, then only the first 'derived_keysize' bytes are used.
  30. */
  31. static int derive_key_aes(const u8 *master_key,
  32. const struct fscrypt_context *ctx,
  33. u8 *derived_key, unsigned int derived_keysize)
  34. {
  35. int res = 0;
  36. struct skcipher_request *req = NULL;
  37. DECLARE_CRYPTO_WAIT(wait);
  38. struct scatterlist src_sg, dst_sg;
  39. struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
  40. if (IS_ERR(tfm)) {
  41. res = PTR_ERR(tfm);
  42. tfm = NULL;
  43. goto out;
  44. }
  45. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  46. req = skcipher_request_alloc(tfm, GFP_NOFS);
  47. if (!req) {
  48. res = -ENOMEM;
  49. goto out;
  50. }
  51. skcipher_request_set_callback(req,
  52. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  53. crypto_req_done, &wait);
  54. res = crypto_skcipher_setkey(tfm, ctx->nonce, sizeof(ctx->nonce));
  55. if (res < 0)
  56. goto out;
  57. sg_init_one(&src_sg, master_key, derived_keysize);
  58. sg_init_one(&dst_sg, derived_key, derived_keysize);
  59. skcipher_request_set_crypt(req, &src_sg, &dst_sg, derived_keysize,
  60. NULL);
  61. res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
  62. out:
  63. skcipher_request_free(req);
  64. crypto_free_skcipher(tfm);
  65. return res;
  66. }
  67. /*
  68. * Search the current task's subscribed keyrings for a "logon" key with
  69. * description prefix:descriptor, and if found acquire a read lock on it and
  70. * return a pointer to its validated payload in *payload_ret.
  71. */
  72. static struct key *
  73. find_and_lock_process_key(const char *prefix,
  74. const u8 descriptor[FS_KEY_DESCRIPTOR_SIZE],
  75. unsigned int min_keysize,
  76. const struct fscrypt_key **payload_ret)
  77. {
  78. char *description;
  79. struct key *key;
  80. const struct user_key_payload *ukp;
  81. const struct fscrypt_key *payload;
  82. description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
  83. FS_KEY_DESCRIPTOR_SIZE, descriptor);
  84. if (!description)
  85. return ERR_PTR(-ENOMEM);
  86. key = request_key(&key_type_logon, description, NULL);
  87. kfree(description);
  88. if (IS_ERR(key))
  89. return key;
  90. down_read(&key->sem);
  91. ukp = user_key_payload_locked(key);
  92. if (!ukp) /* was the key revoked before we acquired its semaphore? */
  93. goto invalid;
  94. payload = (const struct fscrypt_key *)ukp->data;
  95. if (ukp->datalen != sizeof(struct fscrypt_key) ||
  96. payload->size < 1 || payload->size > FS_MAX_KEY_SIZE) {
  97. fscrypt_warn(NULL,
  98. "key with description '%s' has invalid payload",
  99. key->description);
  100. goto invalid;
  101. }
  102. if (payload->size < min_keysize) {
  103. fscrypt_warn(NULL,
  104. "key with description '%s' is too short (got %u bytes, need %u+ bytes)",
  105. key->description, payload->size, min_keysize);
  106. goto invalid;
  107. }
  108. *payload_ret = payload;
  109. return key;
  110. invalid:
  111. up_read(&key->sem);
  112. key_put(key);
  113. return ERR_PTR(-ENOKEY);
  114. }
  115. static struct fscrypt_mode available_modes[] = {
  116. [FS_ENCRYPTION_MODE_AES_256_XTS] = {
  117. .friendly_name = "AES-256-XTS",
  118. .cipher_str = "xts(aes)",
  119. .keysize = 64,
  120. .ivsize = 16,
  121. },
  122. [FS_ENCRYPTION_MODE_AES_256_CTS] = {
  123. .friendly_name = "AES-256-CTS-CBC",
  124. .cipher_str = "cts(cbc(aes))",
  125. .keysize = 32,
  126. .ivsize = 16,
  127. },
  128. [FS_ENCRYPTION_MODE_AES_128_CBC] = {
  129. .friendly_name = "AES-128-CBC",
  130. .cipher_str = "cbc(aes)",
  131. .keysize = 16,
  132. .ivsize = 16,
  133. .needs_essiv = true,
  134. },
  135. [FS_ENCRYPTION_MODE_AES_128_CTS] = {
  136. .friendly_name = "AES-128-CTS-CBC",
  137. .cipher_str = "cts(cbc(aes))",
  138. .keysize = 16,
  139. .ivsize = 16,
  140. },
  141. [FS_ENCRYPTION_MODE_ADIANTUM] = {
  142. .friendly_name = "Adiantum",
  143. .cipher_str = "adiantum(xchacha12,aes)",
  144. .keysize = 32,
  145. .ivsize = 32,
  146. },
  147. [FS_ENCRYPTION_MODE_PRIVATE] = {
  148. .friendly_name = "Inline encryption (AES-256-XTS)",
  149. .cipher_str = NULL,
  150. .keysize = 64,
  151. .ivsize = 16,
  152. },
  153. };
  154. static struct fscrypt_mode *
  155. select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
  156. {
  157. if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
  158. fscrypt_warn(inode->i_sb,
  159. "inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)",
  160. inode->i_ino, ci->ci_data_mode,
  161. ci->ci_filename_mode);
  162. return ERR_PTR(-EINVAL);
  163. }
  164. if (S_ISREG(inode->i_mode))
  165. return &available_modes[ci->ci_data_mode];
  166. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  167. return &available_modes[ci->ci_filename_mode];
  168. WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
  169. inode->i_ino, (inode->i_mode & S_IFMT));
  170. return ERR_PTR(-EINVAL);
  171. }
  172. /* Find the master key, then derive the inode's actual encryption key */
  173. static int find_and_derive_key(const struct inode *inode,
  174. const struct fscrypt_context *ctx,
  175. u8 *derived_key, const struct fscrypt_mode *mode)
  176. {
  177. struct key *key;
  178. const struct fscrypt_key *payload;
  179. int err;
  180. key = find_and_lock_process_key(FS_KEY_DESC_PREFIX,
  181. ctx->master_key_descriptor,
  182. mode->keysize, &payload);
  183. if (key == ERR_PTR(-ENOKEY) && inode->i_sb->s_cop->key_prefix) {
  184. key = find_and_lock_process_key(inode->i_sb->s_cop->key_prefix,
  185. ctx->master_key_descriptor,
  186. mode->keysize, &payload);
  187. }
  188. if (IS_ERR(key))
  189. return PTR_ERR(key);
  190. if (is_private_mode(mode)) {
  191. /*
  192. * Inline encryption: no key derivation required because IVs are
  193. * assigned based on iv_sector.
  194. */
  195. memcpy(derived_key, payload->raw, mode->keysize);
  196. err = 0;
  197. } else if (ctx->flags & FS_POLICY_FLAG_DIRECT_KEY) {
  198. if (mode->ivsize < offsetofend(union fscrypt_iv, nonce)) {
  199. fscrypt_warn(inode->i_sb,
  200. "direct key mode not allowed with %s",
  201. mode->friendly_name);
  202. err = -EINVAL;
  203. } else if (ctx->contents_encryption_mode !=
  204. ctx->filenames_encryption_mode) {
  205. fscrypt_warn(inode->i_sb,
  206. "direct key mode not allowed with different contents and filenames modes");
  207. err = -EINVAL;
  208. } else {
  209. memcpy(derived_key, payload->raw, mode->keysize);
  210. err = 0;
  211. }
  212. } else {
  213. err = derive_key_aes(payload->raw, ctx, derived_key,
  214. mode->keysize);
  215. }
  216. up_read(&key->sem);
  217. key_put(key);
  218. return err;
  219. }
  220. /* Allocate and key a symmetric cipher object for the given encryption mode */
  221. static struct crypto_skcipher *
  222. allocate_skcipher_for_mode(struct fscrypt_mode *mode, const u8 *raw_key,
  223. const struct inode *inode)
  224. {
  225. struct crypto_skcipher *tfm;
  226. int err;
  227. tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
  228. if (IS_ERR(tfm)) {
  229. fscrypt_warn(inode->i_sb,
  230. "error allocating '%s' transform for inode %lu: %ld",
  231. mode->cipher_str, inode->i_ino, PTR_ERR(tfm));
  232. return tfm;
  233. }
  234. if (unlikely(!mode->logged_impl_name)) {
  235. /*
  236. * fscrypt performance can vary greatly depending on which
  237. * crypto algorithm implementation is used. Help people debug
  238. * performance problems by logging the ->cra_driver_name the
  239. * first time a mode is used. Note that multiple threads can
  240. * race here, but it doesn't really matter.
  241. */
  242. mode->logged_impl_name = true;
  243. pr_info("fscrypt: %s using implementation \"%s\"\n",
  244. mode->friendly_name,
  245. crypto_skcipher_alg(tfm)->base.cra_driver_name);
  246. }
  247. crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  248. err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
  249. if (err)
  250. goto err_free_tfm;
  251. return tfm;
  252. err_free_tfm:
  253. crypto_free_skcipher(tfm);
  254. return ERR_PTR(err);
  255. }
  256. /* Master key referenced by FS_POLICY_FLAG_DIRECT_KEY policy */
  257. struct fscrypt_master_key {
  258. struct hlist_node mk_node;
  259. atomic_t mk_refcount;
  260. const struct fscrypt_mode *mk_mode;
  261. struct crypto_skcipher *mk_ctfm;
  262. u8 mk_descriptor[FS_KEY_DESCRIPTOR_SIZE];
  263. u8 mk_raw[FS_MAX_KEY_SIZE];
  264. };
  265. static void free_master_key(struct fscrypt_master_key *mk)
  266. {
  267. if (mk) {
  268. crypto_free_skcipher(mk->mk_ctfm);
  269. kzfree(mk);
  270. }
  271. }
  272. static void put_master_key(struct fscrypt_master_key *mk)
  273. {
  274. if (!atomic_dec_and_lock(&mk->mk_refcount, &fscrypt_master_keys_lock))
  275. return;
  276. hash_del(&mk->mk_node);
  277. spin_unlock(&fscrypt_master_keys_lock);
  278. free_master_key(mk);
  279. }
  280. /*
  281. * Find/insert the given master key into the fscrypt_master_keys table. If
  282. * found, it is returned with elevated refcount, and 'to_insert' is freed if
  283. * non-NULL. If not found, 'to_insert' is inserted and returned if it's
  284. * non-NULL; otherwise NULL is returned.
  285. */
  286. static struct fscrypt_master_key *
  287. find_or_insert_master_key(struct fscrypt_master_key *to_insert,
  288. const u8 *raw_key, const struct fscrypt_mode *mode,
  289. const struct fscrypt_info *ci)
  290. {
  291. unsigned long hash_key;
  292. struct fscrypt_master_key *mk;
  293. /*
  294. * Careful: to avoid potentially leaking secret key bytes via timing
  295. * information, we must key the hash table by descriptor rather than by
  296. * raw key, and use crypto_memneq() when comparing raw keys.
  297. */
  298. BUILD_BUG_ON(sizeof(hash_key) > FS_KEY_DESCRIPTOR_SIZE);
  299. memcpy(&hash_key, ci->ci_master_key_descriptor, sizeof(hash_key));
  300. spin_lock(&fscrypt_master_keys_lock);
  301. hash_for_each_possible(fscrypt_master_keys, mk, mk_node, hash_key) {
  302. if (memcmp(ci->ci_master_key_descriptor, mk->mk_descriptor,
  303. FS_KEY_DESCRIPTOR_SIZE) != 0)
  304. continue;
  305. if (mode != mk->mk_mode)
  306. continue;
  307. if (crypto_memneq(raw_key, mk->mk_raw, mode->keysize))
  308. continue;
  309. /* using existing tfm with same (descriptor, mode, raw_key) */
  310. atomic_inc(&mk->mk_refcount);
  311. spin_unlock(&fscrypt_master_keys_lock);
  312. free_master_key(to_insert);
  313. return mk;
  314. }
  315. if (to_insert)
  316. hash_add(fscrypt_master_keys, &to_insert->mk_node, hash_key);
  317. spin_unlock(&fscrypt_master_keys_lock);
  318. return to_insert;
  319. }
  320. /* Prepare to encrypt directly using the master key in the given mode */
  321. static struct fscrypt_master_key *
  322. fscrypt_get_master_key(const struct fscrypt_info *ci, struct fscrypt_mode *mode,
  323. const u8 *raw_key, const struct inode *inode)
  324. {
  325. struct fscrypt_master_key *mk;
  326. int err;
  327. /* Is there already a tfm for this key? */
  328. mk = find_or_insert_master_key(NULL, raw_key, mode, ci);
  329. if (mk)
  330. return mk;
  331. /* Nope, allocate one. */
  332. mk = kzalloc(sizeof(*mk), GFP_NOFS);
  333. if (!mk)
  334. return ERR_PTR(-ENOMEM);
  335. atomic_set(&mk->mk_refcount, 1);
  336. mk->mk_mode = mode;
  337. mk->mk_ctfm = allocate_skcipher_for_mode(mode, raw_key, inode);
  338. if (IS_ERR(mk->mk_ctfm)) {
  339. err = PTR_ERR(mk->mk_ctfm);
  340. mk->mk_ctfm = NULL;
  341. goto err_free_mk;
  342. }
  343. memcpy(mk->mk_descriptor, ci->ci_master_key_descriptor,
  344. FS_KEY_DESCRIPTOR_SIZE);
  345. memcpy(mk->mk_raw, raw_key, mode->keysize);
  346. return find_or_insert_master_key(mk, raw_key, mode, ci);
  347. err_free_mk:
  348. free_master_key(mk);
  349. return ERR_PTR(err);
  350. }
  351. static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
  352. {
  353. struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
  354. /* init hash transform on demand */
  355. if (unlikely(!tfm)) {
  356. struct crypto_shash *prev_tfm;
  357. tfm = crypto_alloc_shash("sha256", 0, 0);
  358. if (IS_ERR(tfm)) {
  359. fscrypt_warn(NULL,
  360. "error allocating SHA-256 transform: %ld",
  361. PTR_ERR(tfm));
  362. return PTR_ERR(tfm);
  363. }
  364. prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
  365. if (prev_tfm) {
  366. crypto_free_shash(tfm);
  367. tfm = prev_tfm;
  368. }
  369. }
  370. {
  371. SHASH_DESC_ON_STACK(desc, tfm);
  372. desc->tfm = tfm;
  373. desc->flags = 0;
  374. return crypto_shash_digest(desc, key, keysize, salt);
  375. }
  376. }
  377. static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
  378. int keysize)
  379. {
  380. int err;
  381. struct crypto_cipher *essiv_tfm;
  382. u8 salt[SHA256_DIGEST_SIZE];
  383. essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
  384. if (IS_ERR(essiv_tfm))
  385. return PTR_ERR(essiv_tfm);
  386. ci->ci_essiv_tfm = essiv_tfm;
  387. err = derive_essiv_salt(raw_key, keysize, salt);
  388. if (err)
  389. goto out;
  390. /*
  391. * Using SHA256 to derive the salt/key will result in AES-256 being
  392. * used for IV generation. File contents encryption will still use the
  393. * configured keysize (AES-128) nevertheless.
  394. */
  395. err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
  396. if (err)
  397. goto out;
  398. out:
  399. memzero_explicit(salt, sizeof(salt));
  400. return err;
  401. }
  402. void __exit fscrypt_essiv_cleanup(void)
  403. {
  404. crypto_free_shash(essiv_hash_tfm);
  405. }
  406. /*
  407. * Given the encryption mode and key (normally the derived key, but for
  408. * FS_POLICY_FLAG_DIRECT_KEY mode it's the master key), set up the inode's
  409. * symmetric cipher transform object(s).
  410. */
  411. static int setup_crypto_transform(struct fscrypt_info *ci,
  412. struct fscrypt_mode *mode,
  413. const u8 *raw_key, const struct inode *inode)
  414. {
  415. struct fscrypt_master_key *mk;
  416. struct crypto_skcipher *ctfm;
  417. int err;
  418. if (ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY) {
  419. mk = fscrypt_get_master_key(ci, mode, raw_key, inode);
  420. if (IS_ERR(mk))
  421. return PTR_ERR(mk);
  422. ctfm = mk->mk_ctfm;
  423. } else {
  424. mk = NULL;
  425. ctfm = allocate_skcipher_for_mode(mode, raw_key, inode);
  426. if (IS_ERR(ctfm))
  427. return PTR_ERR(ctfm);
  428. }
  429. ci->ci_master_key = mk;
  430. ci->ci_ctfm = ctfm;
  431. if (mode->needs_essiv) {
  432. /* ESSIV implies 16-byte IVs which implies !DIRECT_KEY */
  433. WARN_ON(mode->ivsize != AES_BLOCK_SIZE);
  434. WARN_ON(ci->ci_flags & FS_POLICY_FLAG_DIRECT_KEY);
  435. err = init_essiv_generator(ci, raw_key, mode->keysize);
  436. if (err) {
  437. fscrypt_warn(inode->i_sb,
  438. "error initializing ESSIV generator for inode %lu: %d",
  439. inode->i_ino, err);
  440. return err;
  441. }
  442. }
  443. return 0;
  444. }
  445. static void put_crypt_info(struct fscrypt_info *ci)
  446. {
  447. if (!ci)
  448. return;
  449. if (ci->ci_master_key) {
  450. put_master_key(ci->ci_master_key);
  451. } else {
  452. crypto_free_skcipher(ci->ci_ctfm);
  453. crypto_free_cipher(ci->ci_essiv_tfm);
  454. }
  455. memset(ci->ci_raw_key, 0, FS_MAX_KEY_SIZE);
  456. kmem_cache_free(fscrypt_info_cachep, ci);
  457. }
  458. int fscrypt_get_encryption_info(struct inode *inode)
  459. {
  460. struct fscrypt_info *crypt_info;
  461. struct fscrypt_context ctx;
  462. struct fscrypt_mode *mode;
  463. u8 *raw_key = NULL;
  464. int res;
  465. if (inode->i_crypt_info)
  466. return 0;
  467. res = fscrypt_initialize(inode->i_sb->s_cop->flags);
  468. if (res)
  469. return res;
  470. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  471. if (res < 0) {
  472. if (!fscrypt_dummy_context_enabled(inode) ||
  473. IS_ENCRYPTED(inode))
  474. return res;
  475. /* Fake up a context for an unencrypted directory */
  476. memset(&ctx, 0, sizeof(ctx));
  477. ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
  478. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  479. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  480. memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
  481. } else if (res != sizeof(ctx)) {
  482. return -EINVAL;
  483. }
  484. if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
  485. return -EINVAL;
  486. if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
  487. return -EINVAL;
  488. crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
  489. if (!crypt_info)
  490. return -ENOMEM;
  491. crypt_info->ci_flags = ctx.flags;
  492. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  493. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  494. memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor,
  495. FS_KEY_DESCRIPTOR_SIZE);
  496. memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
  497. mode = select_encryption_mode(crypt_info, inode);
  498. if (IS_ERR(mode)) {
  499. res = PTR_ERR(mode);
  500. goto out;
  501. }
  502. WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
  503. crypt_info->ci_mode = mode;
  504. /*
  505. * This cannot be a stack buffer because it may be passed to the
  506. * scatterlist crypto API as part of key derivation.
  507. */
  508. res = -ENOMEM;
  509. raw_key = kmalloc(mode->keysize, GFP_NOFS);
  510. if (!raw_key)
  511. goto out;
  512. res = find_and_derive_key(inode, &ctx, raw_key, mode);
  513. if (res)
  514. goto out;
  515. if (is_private_mode(crypt_info->ci_mode)) {
  516. if (!fscrypt_is_ice_capable(inode->i_sb)) {
  517. fscrypt_warn(inode->i_sb, "ICE support not available");
  518. res = -EINVAL;
  519. goto out;
  520. }
  521. /* Let's encrypt/decrypt by ICE */
  522. memcpy(crypt_info->ci_raw_key, raw_key, mode->keysize);
  523. goto done;
  524. }
  525. res = setup_crypto_transform(crypt_info, mode, raw_key, inode);
  526. if (res)
  527. goto out;
  528. done:
  529. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  530. crypt_info = NULL;
  531. out:
  532. if (res == -ENOKEY)
  533. res = 0;
  534. put_crypt_info(crypt_info);
  535. kzfree(raw_key);
  536. return res;
  537. }
  538. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  539. void fscrypt_put_encryption_info(struct inode *inode)
  540. {
  541. put_crypt_info(inode->i_crypt_info);
  542. inode->i_crypt_info = NULL;
  543. }
  544. EXPORT_SYMBOL(fscrypt_put_encryption_info);