keyinfo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * key management facility for FS encryption support.
  3. *
  4. * Copyright (C) 2015, Google, Inc.
  5. *
  6. * This contains encryption key functions.
  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 <crypto/hash.h>
  16. #include <linux/fscrypto.h>
  17. static void derive_crypt_complete(struct crypto_async_request *req, int rc)
  18. {
  19. struct fscrypt_completion_result *ecr = req->data;
  20. if (rc == -EINPROGRESS)
  21. return;
  22. ecr->res = rc;
  23. complete(&ecr->completion);
  24. }
  25. /**
  26. * 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 derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
  34. u8 source_key[FS_AES_256_XTS_KEY_SIZE],
  35. u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
  36. {
  37. int res = 0;
  38. struct ablkcipher_request *req = NULL;
  39. DECLARE_FS_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. FS_AES_128_ECB_KEY_SIZE);
  59. if (res < 0)
  60. goto out;
  61. sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
  62. sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
  63. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
  64. FS_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. static int validate_user_key(struct fscrypt_info *crypt_info,
  78. struct fscrypt_context *ctx, u8 *raw_key,
  79. u8 *prefix, int prefix_size)
  80. {
  81. u8 *full_key_descriptor;
  82. struct key *keyring_key;
  83. struct fscrypt_key *master_key;
  84. const struct user_key_payload *ukp;
  85. int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1;
  86. int res;
  87. full_key_descriptor = kmalloc(full_key_len, GFP_NOFS);
  88. if (!full_key_descriptor)
  89. return -ENOMEM;
  90. memcpy(full_key_descriptor, prefix, prefix_size);
  91. sprintf(full_key_descriptor + prefix_size,
  92. "%*phN", FS_KEY_DESCRIPTOR_SIZE,
  93. ctx->master_key_descriptor);
  94. full_key_descriptor[full_key_len - 1] = '\0';
  95. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  96. kfree(full_key_descriptor);
  97. if (IS_ERR(keyring_key))
  98. return PTR_ERR(keyring_key);
  99. down_read(&keyring_key->sem);
  100. if (keyring_key->type != &key_type_logon) {
  101. printk_once(KERN_WARNING
  102. "%s: key type must be logon\n", __func__);
  103. res = -ENOKEY;
  104. goto out;
  105. }
  106. ukp = ((struct user_key_payload *)keyring_key->payload.data);
  107. if (ukp->datalen != sizeof(struct fscrypt_key)) {
  108. res = -EINVAL;
  109. goto out;
  110. }
  111. master_key = (struct fscrypt_key *)ukp->data;
  112. BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
  113. if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
  114. printk_once(KERN_WARNING
  115. "%s: key size incorrect: %d\n",
  116. __func__, master_key->size);
  117. res = -ENOKEY;
  118. goto out;
  119. }
  120. res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
  121. out:
  122. up_read(&keyring_key->sem);
  123. key_put(keyring_key);
  124. return res;
  125. }
  126. static void put_crypt_info(struct fscrypt_info *ci)
  127. {
  128. if (!ci)
  129. return;
  130. crypto_free_ablkcipher(ci->ci_ctfm);
  131. kmem_cache_free(fscrypt_info_cachep, ci);
  132. }
  133. int fscrypt_get_encryption_info(struct inode *inode)
  134. {
  135. struct fscrypt_info *crypt_info;
  136. struct fscrypt_context ctx;
  137. struct crypto_ablkcipher *ctfm;
  138. const char *cipher_str;
  139. u8 raw_key[FS_MAX_KEY_SIZE];
  140. u8 mode;
  141. int res;
  142. if (inode->i_crypt_info)
  143. return 0;
  144. res = fscrypt_initialize();
  145. if (res)
  146. return res;
  147. if (!inode->i_sb->s_cop->get_context)
  148. return -EOPNOTSUPP;
  149. res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
  150. if (res < 0) {
  151. if (!fscrypt_dummy_context_enabled(inode))
  152. return res;
  153. ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
  154. ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
  155. ctx.flags = 0;
  156. } else if (res != sizeof(ctx)) {
  157. return -EINVAL;
  158. }
  159. res = 0;
  160. crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
  161. if (!crypt_info)
  162. return -ENOMEM;
  163. crypt_info->ci_flags = ctx.flags;
  164. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  165. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  166. crypt_info->ci_ctfm = NULL;
  167. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  168. sizeof(crypt_info->ci_master_key));
  169. if (S_ISREG(inode->i_mode))
  170. mode = crypt_info->ci_data_mode;
  171. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  172. mode = crypt_info->ci_filename_mode;
  173. else
  174. BUG();
  175. switch (mode) {
  176. case FS_ENCRYPTION_MODE_AES_256_XTS:
  177. cipher_str = "xts(aes)";
  178. break;
  179. case FS_ENCRYPTION_MODE_AES_256_CTS:
  180. cipher_str = "cts(cbc(aes))";
  181. break;
  182. default:
  183. printk_once(KERN_WARNING
  184. "%s: unsupported key mode %d (ino %u)\n",
  185. __func__, mode, (unsigned) inode->i_ino);
  186. res = -ENOKEY;
  187. goto out;
  188. }
  189. if (fscrypt_dummy_context_enabled(inode)) {
  190. memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
  191. goto got_key;
  192. }
  193. res = validate_user_key(crypt_info, &ctx, raw_key,
  194. FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
  195. if (res && inode->i_sb->s_cop->key_prefix) {
  196. u8 *prefix = NULL;
  197. int prefix_size, res2;
  198. prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
  199. res2 = validate_user_key(crypt_info, &ctx, raw_key,
  200. prefix, prefix_size);
  201. if (res2) {
  202. if (res2 == -ENOKEY)
  203. res = -ENOKEY;
  204. goto out;
  205. }
  206. } else if (res) {
  207. goto out;
  208. }
  209. got_key:
  210. ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
  211. if (!ctfm || IS_ERR(ctfm)) {
  212. res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
  213. printk(KERN_DEBUG
  214. "%s: error %d (inode %u) allocating crypto tfm\n",
  215. __func__, res, (unsigned) inode->i_ino);
  216. goto out;
  217. }
  218. crypt_info->ci_ctfm = ctfm;
  219. crypto_ablkcipher_clear_flags(ctfm, ~0);
  220. crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
  221. CRYPTO_TFM_REQ_WEAK_KEY);
  222. res = crypto_ablkcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
  223. if (res)
  224. goto out;
  225. if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
  226. crypt_info = NULL;
  227. out:
  228. if (res == -ENOKEY)
  229. res = 0;
  230. put_crypt_info(crypt_info);
  231. memzero_explicit(raw_key, sizeof(raw_key));
  232. return res;
  233. }
  234. EXPORT_SYMBOL(fscrypt_get_encryption_info);
  235. void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
  236. {
  237. struct fscrypt_info *prev;
  238. if (ci == NULL)
  239. ci = ACCESS_ONCE(inode->i_crypt_info);
  240. if (ci == NULL)
  241. return;
  242. prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
  243. if (prev != ci)
  244. return;
  245. put_crypt_info(ci);
  246. }
  247. EXPORT_SYMBOL(fscrypt_put_encryption_info);