crypto_key.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. if (ci->ci_keyring_key)
  87. key_put(ci->ci_keyring_key);
  88. crypto_free_ablkcipher(ci->ci_ctfm);
  89. kmem_cache_free(f2fs_crypt_info_cachep, ci);
  90. }
  91. void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
  92. {
  93. struct f2fs_inode_info *fi = F2FS_I(inode);
  94. struct f2fs_crypt_info *prev;
  95. if (ci == NULL)
  96. ci = ACCESS_ONCE(fi->i_crypt_info);
  97. if (ci == NULL)
  98. return;
  99. prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
  100. if (prev != ci)
  101. return;
  102. f2fs_free_crypt_info(ci);
  103. }
  104. int _f2fs_get_encryption_info(struct inode *inode)
  105. {
  106. struct f2fs_inode_info *fi = F2FS_I(inode);
  107. struct f2fs_crypt_info *crypt_info;
  108. char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  109. (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
  110. struct key *keyring_key = NULL;
  111. struct f2fs_encryption_key *master_key;
  112. struct f2fs_encryption_context ctx;
  113. struct user_key_payload *ukp;
  114. struct crypto_ablkcipher *ctfm;
  115. const char *cipher_str;
  116. char raw_key[F2FS_MAX_KEY_SIZE];
  117. char mode;
  118. int res;
  119. res = f2fs_crypto_initialize();
  120. if (res)
  121. return res;
  122. retry:
  123. crypt_info = ACCESS_ONCE(fi->i_crypt_info);
  124. if (crypt_info) {
  125. if (!crypt_info->ci_keyring_key ||
  126. key_validate(crypt_info->ci_keyring_key) == 0)
  127. return 0;
  128. f2fs_free_encryption_info(inode, crypt_info);
  129. goto retry;
  130. }
  131. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  132. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  133. &ctx, sizeof(ctx), NULL);
  134. if (res < 0)
  135. return res;
  136. else if (res != sizeof(ctx))
  137. return -EINVAL;
  138. res = 0;
  139. crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
  140. if (!crypt_info)
  141. return -ENOMEM;
  142. crypt_info->ci_flags = ctx.flags;
  143. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  144. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  145. crypt_info->ci_ctfm = NULL;
  146. crypt_info->ci_keyring_key = NULL;
  147. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  148. sizeof(crypt_info->ci_master_key));
  149. if (S_ISREG(inode->i_mode))
  150. mode = crypt_info->ci_data_mode;
  151. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  152. mode = crypt_info->ci_filename_mode;
  153. else
  154. BUG();
  155. switch (mode) {
  156. case F2FS_ENCRYPTION_MODE_AES_256_XTS:
  157. cipher_str = "xts(aes)";
  158. break;
  159. case F2FS_ENCRYPTION_MODE_AES_256_CTS:
  160. cipher_str = "cts(cbc(aes))";
  161. break;
  162. default:
  163. printk_once(KERN_WARNING
  164. "f2fs: unsupported key mode %d (ino %u)\n",
  165. mode, (unsigned) inode->i_ino);
  166. res = -ENOKEY;
  167. goto out;
  168. }
  169. memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
  170. F2FS_KEY_DESC_PREFIX_SIZE);
  171. sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
  172. "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
  173. ctx.master_key_descriptor);
  174. full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  175. (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
  176. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  177. if (IS_ERR(keyring_key)) {
  178. res = PTR_ERR(keyring_key);
  179. keyring_key = NULL;
  180. goto out;
  181. }
  182. crypt_info->ci_keyring_key = keyring_key;
  183. BUG_ON(keyring_key->type != &key_type_logon);
  184. ukp = ((struct user_key_payload *)keyring_key->payload.data);
  185. if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
  186. res = -EINVAL;
  187. goto out;
  188. }
  189. master_key = (struct f2fs_encryption_key *)ukp->data;
  190. BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
  191. F2FS_KEY_DERIVATION_NONCE_SIZE);
  192. BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
  193. res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
  194. raw_key);
  195. if (res)
  196. goto out;
  197. ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
  198. if (!ctfm || IS_ERR(ctfm)) {
  199. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  200. printk(KERN_DEBUG
  201. "%s: error %d (inode %u) allocating crypto tfm\n",
  202. __func__, res, (unsigned) inode->i_ino);
  203. goto out;
  204. }
  205. crypt_info->ci_ctfm = ctfm;
  206. crypto_ablkcipher_clear_flags(ctfm, ~0);
  207. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
  208. CRYPTO_TFM_REQ_WEAK_KEY);
  209. res = crypto_ablkcipher_setkey(ctfm, raw_key,
  210. f2fs_encryption_key_size(mode));
  211. if (res)
  212. goto out;
  213. memzero_explicit(raw_key, sizeof(raw_key));
  214. if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
  215. f2fs_free_crypt_info(crypt_info);
  216. goto retry;
  217. }
  218. return 0;
  219. out:
  220. if (res == -ENOKEY && !S_ISREG(inode->i_mode))
  221. res = 0;
  222. f2fs_free_crypt_info(crypt_info);
  223. memzero_explicit(raw_key, sizeof(raw_key));
  224. return res;
  225. }
  226. int f2fs_has_encryption_key(struct inode *inode)
  227. {
  228. struct f2fs_inode_info *fi = F2FS_I(inode);
  229. return (fi->i_crypt_info != NULL);
  230. }