crypto_key.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. crypto_free_ablkcipher(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_ablkcipher *ctfm;
  110. const char *cipher_str;
  111. char raw_key[EXT4_MAX_KEY_SIZE];
  112. char mode;
  113. int res;
  114. if (ei->i_crypt_info)
  115. return 0;
  116. res = ext4_init_crypto();
  117. if (res)
  118. return res;
  119. res = ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  120. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  121. &ctx, sizeof(ctx));
  122. if (res < 0) {
  123. if (!DUMMY_ENCRYPTION_ENABLED(sbi))
  124. return res;
  125. ctx.contents_encryption_mode = EXT4_ENCRYPTION_MODE_AES_256_XTS;
  126. ctx.filenames_encryption_mode =
  127. EXT4_ENCRYPTION_MODE_AES_256_CTS;
  128. ctx.flags = 0;
  129. } else if (res != sizeof(ctx))
  130. return -EINVAL;
  131. res = 0;
  132. crypt_info = kmem_cache_alloc(ext4_crypt_info_cachep, GFP_KERNEL);
  133. if (!crypt_info)
  134. return -ENOMEM;
  135. crypt_info->ci_flags = ctx.flags;
  136. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  137. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  138. crypt_info->ci_ctfm = NULL;
  139. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  140. sizeof(crypt_info->ci_master_key));
  141. if (S_ISREG(inode->i_mode))
  142. mode = crypt_info->ci_data_mode;
  143. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  144. mode = crypt_info->ci_filename_mode;
  145. else
  146. BUG();
  147. switch (mode) {
  148. case EXT4_ENCRYPTION_MODE_AES_256_XTS:
  149. cipher_str = "xts(aes)";
  150. break;
  151. case EXT4_ENCRYPTION_MODE_AES_256_CTS:
  152. cipher_str = "cts(cbc(aes))";
  153. break;
  154. default:
  155. printk_once(KERN_WARNING
  156. "ext4: unsupported key mode %d (ino %u)\n",
  157. mode, (unsigned) inode->i_ino);
  158. res = -ENOKEY;
  159. goto out;
  160. }
  161. if (DUMMY_ENCRYPTION_ENABLED(sbi)) {
  162. memset(raw_key, 0x42, EXT4_AES_256_XTS_KEY_SIZE);
  163. goto got_key;
  164. }
  165. memcpy(full_key_descriptor, EXT4_KEY_DESC_PREFIX,
  166. EXT4_KEY_DESC_PREFIX_SIZE);
  167. sprintf(full_key_descriptor + EXT4_KEY_DESC_PREFIX_SIZE,
  168. "%*phN", EXT4_KEY_DESCRIPTOR_SIZE,
  169. ctx.master_key_descriptor);
  170. full_key_descriptor[EXT4_KEY_DESC_PREFIX_SIZE +
  171. (2 * EXT4_KEY_DESCRIPTOR_SIZE)] = '\0';
  172. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  173. if (IS_ERR(keyring_key)) {
  174. res = PTR_ERR(keyring_key);
  175. keyring_key = NULL;
  176. goto out;
  177. }
  178. if (keyring_key->type != &key_type_logon) {
  179. printk_once(KERN_WARNING
  180. "ext4: key type must be logon\n");
  181. res = -ENOKEY;
  182. goto out;
  183. }
  184. down_read(&keyring_key->sem);
  185. ukp = user_key_payload(keyring_key);
  186. if (!ukp) {
  187. /* key was revoked before we acquired its semaphore */
  188. res = -EKEYREVOKED;
  189. up_read(&keyring_key->sem);
  190. goto out;
  191. }
  192. if (ukp->datalen != sizeof(struct ext4_encryption_key)) {
  193. res = -EINVAL;
  194. up_read(&keyring_key->sem);
  195. goto out;
  196. }
  197. master_key = (struct ext4_encryption_key *)ukp->data;
  198. BUILD_BUG_ON(EXT4_AES_128_ECB_KEY_SIZE !=
  199. EXT4_KEY_DERIVATION_NONCE_SIZE);
  200. if (master_key->size != EXT4_AES_256_XTS_KEY_SIZE) {
  201. printk_once(KERN_WARNING
  202. "ext4: key size incorrect: %d\n",
  203. master_key->size);
  204. res = -ENOKEY;
  205. up_read(&keyring_key->sem);
  206. goto out;
  207. }
  208. res = ext4_derive_key_aes(ctx.nonce, master_key->raw,
  209. raw_key);
  210. up_read(&keyring_key->sem);
  211. if (res)
  212. goto out;
  213. got_key:
  214. ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
  215. if (!ctfm || IS_ERR(ctfm)) {
  216. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  217. printk(KERN_DEBUG
  218. "%s: error %d (inode %u) allocating crypto tfm\n",
  219. __func__, res, (unsigned) inode->i_ino);
  220. goto out;
  221. }
  222. crypt_info->ci_ctfm = ctfm;
  223. crypto_ablkcipher_clear_flags(ctfm, ~0);
  224. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
  225. CRYPTO_TFM_REQ_WEAK_KEY);
  226. res = crypto_ablkcipher_setkey(ctfm, raw_key,
  227. ext4_encryption_key_size(mode));
  228. if (res)
  229. goto out;
  230. if (cmpxchg(&ei->i_crypt_info, NULL, crypt_info) == NULL)
  231. crypt_info = NULL;
  232. out:
  233. if (res == -ENOKEY)
  234. res = 0;
  235. key_put(keyring_key);
  236. ext4_free_crypt_info(crypt_info);
  237. memzero_explicit(raw_key, sizeof(raw_key));
  238. return res;
  239. }
  240. int ext4_has_encryption_key(struct inode *inode)
  241. {
  242. struct ext4_inode_info *ei = EXT4_I(inode);
  243. return (ei->i_crypt_info != NULL);
  244. }