crypto_key.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * linux/fs/f2fs/crypto_key.c
  3. *
  4. * Copied from linux/fs/f2fs/crypto_key.c
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. *
  8. * This contains encryption key functions for f2fs
  9. *
  10. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  11. */
  12. #include <keys/encrypted-type.h>
  13. #include <keys/user-type.h>
  14. #include <linux/random.h>
  15. #include <linux/scatterlist.h>
  16. #include <uapi/linux/keyctl.h>
  17. #include <crypto/hash.h>
  18. #include <linux/f2fs_fs.h>
  19. #include "f2fs.h"
  20. #include "xattr.h"
  21. static void derive_crypt_complete(struct crypto_async_request *req, int rc)
  22. {
  23. struct f2fs_completion_result *ecr = req->data;
  24. if (rc == -EINPROGRESS)
  25. return;
  26. ecr->res = rc;
  27. complete(&ecr->completion);
  28. }
  29. /**
  30. * f2fs_derive_key_aes() - Derive a key using AES-128-ECB
  31. * @deriving_key: Encryption key used for derivatio.
  32. * @source_key: Source key to which to apply derivation.
  33. * @derived_key: Derived key.
  34. *
  35. * Return: Zero on success; non-zero otherwise.
  36. */
  37. static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE],
  38. char source_key[F2FS_AES_256_XTS_KEY_SIZE],
  39. char derived_key[F2FS_AES_256_XTS_KEY_SIZE])
  40. {
  41. int res = 0;
  42. struct ablkcipher_request *req = NULL;
  43. DECLARE_F2FS_COMPLETION_RESULT(ecr);
  44. struct scatterlist src_sg, dst_sg;
  45. struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
  46. 0);
  47. if (IS_ERR(tfm)) {
  48. res = PTR_ERR(tfm);
  49. tfm = NULL;
  50. goto out;
  51. }
  52. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  53. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  54. if (!req) {
  55. res = -ENOMEM;
  56. goto out;
  57. }
  58. ablkcipher_request_set_callback(req,
  59. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  60. derive_crypt_complete, &ecr);
  61. res = crypto_ablkcipher_setkey(tfm, deriving_key,
  62. F2FS_AES_128_ECB_KEY_SIZE);
  63. if (res < 0)
  64. goto out;
  65. sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE);
  66. sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE);
  67. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
  68. F2FS_AES_256_XTS_KEY_SIZE, NULL);
  69. res = crypto_ablkcipher_encrypt(req);
  70. if (res == -EINPROGRESS || res == -EBUSY) {
  71. BUG_ON(req->base.data != &ecr);
  72. wait_for_completion(&ecr.completion);
  73. res = ecr.res;
  74. }
  75. out:
  76. if (req)
  77. ablkcipher_request_free(req);
  78. if (tfm)
  79. crypto_free_ablkcipher(tfm);
  80. return res;
  81. }
  82. static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
  83. {
  84. if (!ci)
  85. return;
  86. key_put(ci->ci_keyring_key);
  87. crypto_free_ablkcipher(ci->ci_ctfm);
  88. kmem_cache_free(f2fs_crypt_info_cachep, ci);
  89. }
  90. void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
  91. {
  92. struct f2fs_inode_info *fi = F2FS_I(inode);
  93. struct f2fs_crypt_info *prev;
  94. if (ci == NULL)
  95. ci = ACCESS_ONCE(fi->i_crypt_info);
  96. if (ci == NULL)
  97. return;
  98. prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
  99. if (prev != ci)
  100. return;
  101. f2fs_free_crypt_info(ci);
  102. }
  103. int _f2fs_get_encryption_info(struct inode *inode)
  104. {
  105. struct f2fs_inode_info *fi = F2FS_I(inode);
  106. struct f2fs_crypt_info *crypt_info;
  107. char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  108. (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
  109. struct key *keyring_key = NULL;
  110. struct f2fs_encryption_key *master_key;
  111. struct f2fs_encryption_context ctx;
  112. struct user_key_payload *ukp;
  113. struct crypto_ablkcipher *ctfm;
  114. const char *cipher_str;
  115. char raw_key[F2FS_MAX_KEY_SIZE];
  116. char mode;
  117. int res;
  118. res = f2fs_crypto_initialize();
  119. if (res)
  120. return res;
  121. retry:
  122. crypt_info = ACCESS_ONCE(fi->i_crypt_info);
  123. if (crypt_info) {
  124. if (!crypt_info->ci_keyring_key ||
  125. key_validate(crypt_info->ci_keyring_key) == 0)
  126. return 0;
  127. f2fs_free_encryption_info(inode, crypt_info);
  128. goto retry;
  129. }
  130. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  131. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  132. &ctx, sizeof(ctx), NULL);
  133. if (res < 0)
  134. return res;
  135. else if (res != sizeof(ctx))
  136. return -EINVAL;
  137. res = 0;
  138. crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
  139. if (!crypt_info)
  140. return -ENOMEM;
  141. crypt_info->ci_flags = ctx.flags;
  142. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  143. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  144. crypt_info->ci_ctfm = NULL;
  145. crypt_info->ci_keyring_key = NULL;
  146. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  147. sizeof(crypt_info->ci_master_key));
  148. if (S_ISREG(inode->i_mode))
  149. mode = crypt_info->ci_data_mode;
  150. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  151. mode = crypt_info->ci_filename_mode;
  152. else
  153. BUG();
  154. switch (mode) {
  155. case F2FS_ENCRYPTION_MODE_AES_256_XTS:
  156. cipher_str = "xts(aes)";
  157. break;
  158. case F2FS_ENCRYPTION_MODE_AES_256_CTS:
  159. cipher_str = "cts(cbc(aes))";
  160. break;
  161. default:
  162. printk_once(KERN_WARNING
  163. "f2fs: unsupported key mode %d (ino %u)\n",
  164. mode, (unsigned) inode->i_ino);
  165. res = -ENOKEY;
  166. goto out;
  167. }
  168. memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
  169. F2FS_KEY_DESC_PREFIX_SIZE);
  170. sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
  171. "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
  172. ctx.master_key_descriptor);
  173. full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  174. (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
  175. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  176. if (IS_ERR(keyring_key)) {
  177. res = PTR_ERR(keyring_key);
  178. keyring_key = NULL;
  179. goto out;
  180. }
  181. crypt_info->ci_keyring_key = keyring_key;
  182. BUG_ON(keyring_key->type != &key_type_logon);
  183. ukp = ((struct user_key_payload *)keyring_key->payload.data);
  184. if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
  185. res = -EINVAL;
  186. goto out;
  187. }
  188. master_key = (struct f2fs_encryption_key *)ukp->data;
  189. BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
  190. F2FS_KEY_DERIVATION_NONCE_SIZE);
  191. BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
  192. res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
  193. raw_key);
  194. if (res)
  195. goto out;
  196. ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
  197. if (!ctfm || IS_ERR(ctfm)) {
  198. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  199. printk(KERN_DEBUG
  200. "%s: error %d (inode %u) allocating crypto tfm\n",
  201. __func__, res, (unsigned) inode->i_ino);
  202. goto out;
  203. }
  204. crypt_info->ci_ctfm = ctfm;
  205. crypto_ablkcipher_clear_flags(ctfm, ~0);
  206. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
  207. CRYPTO_TFM_REQ_WEAK_KEY);
  208. res = crypto_ablkcipher_setkey(ctfm, raw_key,
  209. f2fs_encryption_key_size(mode));
  210. if (res)
  211. goto out;
  212. memzero_explicit(raw_key, sizeof(raw_key));
  213. if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
  214. f2fs_free_crypt_info(crypt_info);
  215. goto retry;
  216. }
  217. return 0;
  218. out:
  219. if (res == -ENOKEY && !S_ISREG(inode->i_mode))
  220. res = 0;
  221. f2fs_free_crypt_info(crypt_info);
  222. memzero_explicit(raw_key, sizeof(raw_key));
  223. return res;
  224. }
  225. int f2fs_has_encryption_key(struct inode *inode)
  226. {
  227. struct f2fs_inode_info *fi = F2FS_I(inode);
  228. return (fi->i_crypt_info != NULL);
  229. }