crypto_key.c 7.2 KB

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