crypto_key.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. wait_for_completion(&ecr.completion);
  68. res = ecr.res;
  69. }
  70. out:
  71. if (req)
  72. ablkcipher_request_free(req);
  73. if (tfm)
  74. crypto_free_ablkcipher(tfm);
  75. return res;
  76. }
  77. void ext4_free_crypt_info(struct ext4_crypt_info *ci)
  78. {
  79. if (!ci)
  80. return;
  81. if (ci->ci_keyring_key)
  82. key_put(ci->ci_keyring_key);
  83. crypto_free_ablkcipher(ci->ci_ctfm);
  84. kmem_cache_free(ext4_crypt_info_cachep, ci);
  85. }
  86. void ext4_free_encryption_info(struct inode *inode,
  87. struct ext4_crypt_info *ci)
  88. {
  89. struct ext4_inode_info *ei = EXT4_I(inode);
  90. struct ext4_crypt_info *prev;
  91. if (ci == NULL)
  92. ci = ACCESS_ONCE(ei->i_crypt_info);
  93. if (ci == NULL)
  94. return;
  95. prev = cmpxchg(&ei->i_crypt_info, ci, NULL);
  96. if (prev != ci)
  97. return;
  98. ext4_free_crypt_info(ci);
  99. }
  100. int _ext4_get_encryption_info(struct inode *inode)
  101. {
  102. struct ext4_inode_info *ei = EXT4_I(inode);
  103. struct ext4_crypt_info *crypt_info;
  104. char full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
  105. (EXT4_KEY_DESCRIPTOR_SIZE * 2) + 1];
  106. struct key *keyring_key = NULL;
  107. struct ext4_encryption_key *master_key;
  108. struct ext4_encryption_context ctx;
  109. const struct user_key_payload *ukp;
  110. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  111. struct crypto_ablkcipher *ctfm;
  112. const char *cipher_str;
  113. char raw_key[EXT4_MAX_KEY_SIZE];
  114. char mode;
  115. int res;
  116. if (!ext4_read_workqueue) {
  117. res = ext4_init_crypto();
  118. if (res)
  119. return res;
  120. }
  121. retry:
  122. crypt_info = ACCESS_ONCE(ei->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. ext4_free_encryption_info(inode, crypt_info);
  128. goto retry;
  129. }
  130. res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  131. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  132. &ctx, sizeof(ctx));
  133. if (res < 0) {
  134. if (!DUMMY_ENCRYPTION_ENABLED(sbi))
  135. return res;
  136. ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
  137. ctx.filenames_encryption_mode =
  138. EXT4_ENCRYPTION_MODE_AES_256_CTS;
  139. ctx.flags = 0;
  140. } else if (res != sizeof(ctx))
  141. return -EINVAL;
  142. res = 0;
  143. crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
  144. if (!crypt_info)
  145. return -ENOMEM;
  146. crypt_info->ci_flags = ctx.flags;
  147. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  148. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  149. crypt_info->ci_ctfm = NULL;
  150. crypt_info->ci_keyring_key = NULL;
  151. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  152. sizeof(crypt_info->ci_master_key));
  153. if (S_ISREG(inode->i_mode))
  154. mode = crypt_info->ci_data_mode;
  155. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  156. mode = crypt_info->ci_filename_mode;
  157. else
  158. BUG();
  159. switch (mode) {
  160. case EXT4_ENCRYPTION_MODE_AES_256_XTS:
  161. cipher_str = "xts(aes)";
  162. break;
  163. case EXT4_ENCRYPTION_MODE_AES_256_CTS:
  164. cipher_str = "cts(cbc(aes))";
  165. break;
  166. default:
  167. printk_once(KERN_WARNING
  168. "ext4: unsupported key mode %d (ino %u)\n",
  169. mode, (unsigned) inode->i_ino);
  170. res = -ENOKEY;
  171. goto out;
  172. }
  173. if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
  174. memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
  175. goto got_key;
  176. }
  177. memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
  178. EXT4_KEY_DESC_PREFIX_SIZE);
  179. sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
  180. "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
  181. ctx.master_key_descriptor);
  182. full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
  183. (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
  184. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  185. if (IS_ERR(keyring_key)) {
  186. res = PTR_ERR(keyring_key);
  187. keyring_key = NULL;
  188. goto out;
  189. }
  190. crypt_info->ci_keyring_key = keyring_key;
  191. if (keyring_key->type != &key_type_logon) {
  192. printk_once(KERN_WARNING
  193. "ext4: key type must be logon\n");
  194. res = -ENOKEY;
  195. goto out;
  196. }
  197. ukp = user_key_payload(keyring_key);
  198. if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
  199. res = -EINVAL;
  200. goto out;
  201. }
  202. master_key = (struct ext4_encryption_key *)ukp->data;
  203. BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
  204. EXT4_KEY_DERIVATION_NONCE_SIZE);
  205. if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) {
  206. printk_once(KERN_WARNING
  207. "ext4: key size incorrect: %d\n",
  208. master_key->size);
  209. res = -ENOKEY;
  210. goto out;
  211. }
  212. res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
  213. raw_key);
  214. if (res)
  215. goto out;
  216. got_key:
  217. ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
  218. if (!ctfm || IS_ERR(ctfm)) {
  219. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  220. printk(KERN_DEBUG
  221. "%s: error %d (inode %u) allocating crypto tfm\n",
  222. __func__, res, (unsigned) inode->i_ino);
  223. goto out;
  224. }
  225. crypt_info->ci_ctfm = ctfm;
  226. crypto_ablkcipher_clear_flags(ctfm, ~0);
  227. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
  228. CRYPTO_TFM_REQ_WEAK_KEY);
  229. res = crypto_ablkcipher_setkey(ctfm, raw_key,
  230. ext4_encryption_key_size(mode));
  231. if (res)
  232. goto out;
  233. memzero_explicit(raw_key, sizeof(raw_key));
  234. if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) != NULL) {
  235. ext4_free_crypt_info(crypt_info);
  236. goto retry;
  237. }
  238. return 0;
  239. out:
  240. if (res == -ENOKEY)
  241. res = 0;
  242. ext4_free_crypt_info(crypt_info);
  243. memzero_explicit(raw_key, sizeof(raw_key));
  244. return res;
  245. }
  246. int ext4_has_encryption_key(struct inode *inode)
  247. {
  248. struct ext4_inode_info *ei = EXT4_I(inode);
  249. return (ei->i_crypt_info != NULL);
  250. }