crypto_key.c 7.0 KB

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