crypto_key.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * linux/fs/f2fs/crypto_key.c
  3. *
  4. * Copied from linux/fs/f2fs/crypto_key.c
  5. *
  6. * Copyright (C) 2015, Google, Inc.
  7. *
  8. * This contains encryption key functions for f2fs
  9. *
  10. * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
  11. */
  12. #include <keys/encrypted-type.h>
  13. #include <keys/user-type.h>
  14. #include <linux/random.h>
  15. #include <linux/scatterlist.h>
  16. #include <uapi/linux/keyctl.h>
  17. #include <crypto/hash.h>
  18. #include <linux/f2fs_fs.h>
  19. #include "f2fs.h"
  20. #include "xattr.h"
  21. static void derive_crypt_complete(struct crypto_async_request *req, int rc)
  22. {
  23. struct f2fs_completion_result *ecr = req->data;
  24. if (rc == -EINPROGRESS)
  25. return;
  26. ecr->res = rc;
  27. complete(&ecr->completion);
  28. }
  29. /**
  30. * f2fs_derive_key_aes() - Derive a key using AES-128-ECB
  31. * @deriving_key: Encryption key used for derivatio.
  32. * @source_key: Source key to which to apply derivation.
  33. * @derived_key: Derived key.
  34. *
  35. * Return: Zero on success; non-zero otherwise.
  36. */
  37. static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE],
  38. char source_key[F2FS_AES_256_XTS_KEY_SIZE],
  39. char derived_key[F2FS_AES_256_XTS_KEY_SIZE])
  40. {
  41. int res = 0;
  42. struct ablkcipher_request *req = NULL;
  43. DECLARE_F2FS_COMPLETION_RESULT(ecr);
  44. struct scatterlist src_sg, dst_sg;
  45. struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
  46. 0);
  47. if (IS_ERR(tfm)) {
  48. res = PTR_ERR(tfm);
  49. tfm = NULL;
  50. goto out;
  51. }
  52. crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
  53. req = ablkcipher_request_alloc(tfm, GFP_NOFS);
  54. if (!req) {
  55. res = -ENOMEM;
  56. goto out;
  57. }
  58. ablkcipher_request_set_callback(req,
  59. CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
  60. derive_crypt_complete, &ecr);
  61. res = crypto_ablkcipher_setkey(tfm, deriving_key,
  62. F2FS_AES_128_ECB_KEY_SIZE);
  63. if (res < 0)
  64. goto out;
  65. sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE);
  66. sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE);
  67. ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
  68. F2FS_AES_256_XTS_KEY_SIZE, NULL);
  69. res = crypto_ablkcipher_encrypt(req);
  70. if (res == -EINPROGRESS || res == -EBUSY) {
  71. BUG_ON(req->base.data != &ecr);
  72. wait_for_completion(&ecr.completion);
  73. res = ecr.res;
  74. }
  75. out:
  76. if (req)
  77. ablkcipher_request_free(req);
  78. if (tfm)
  79. crypto_free_ablkcipher(tfm);
  80. return res;
  81. }
  82. void f2fs_free_encryption_info(struct inode *inode)
  83. {
  84. struct f2fs_inode_info *fi = F2FS_I(inode);
  85. struct f2fs_crypt_info *ci = fi->i_crypt_info;
  86. if (!ci)
  87. return;
  88. if (ci->ci_keyring_key)
  89. key_put(ci->ci_keyring_key);
  90. crypto_free_ablkcipher(ci->ci_ctfm);
  91. memzero_explicit(&ci->ci_raw, sizeof(ci->ci_raw));
  92. kfree(ci);
  93. fi->i_crypt_info = NULL;
  94. }
  95. int _f2fs_get_encryption_info(struct inode *inode)
  96. {
  97. struct f2fs_inode_info *fi = F2FS_I(inode);
  98. struct f2fs_crypt_info *crypt_info;
  99. char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  100. (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
  101. struct key *keyring_key = NULL;
  102. struct f2fs_encryption_key *master_key;
  103. struct f2fs_encryption_context ctx;
  104. struct user_key_payload *ukp;
  105. int res;
  106. if (!f2fs_read_workqueue) {
  107. res = f2fs_init_crypto();
  108. if (res)
  109. return res;
  110. }
  111. if (fi->i_crypt_info) {
  112. if (!fi->i_crypt_info->ci_keyring_key ||
  113. key_validate(fi->i_crypt_info->ci_keyring_key) == 0)
  114. return 0;
  115. f2fs_free_encryption_info(inode);
  116. }
  117. res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  118. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  119. &ctx, sizeof(ctx), NULL);
  120. if (res < 0)
  121. return res;
  122. else if (res != sizeof(ctx))
  123. return -EINVAL;
  124. res = 0;
  125. crypt_info = kmalloc(sizeof(struct f2fs_crypt_info), GFP_NOFS);
  126. if (!crypt_info)
  127. return -ENOMEM;
  128. crypt_info->ci_flags = ctx.flags;
  129. crypt_info->ci_data_mode = ctx.contents_encryption_mode;
  130. crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
  131. crypt_info->ci_ctfm = NULL;
  132. memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
  133. sizeof(crypt_info->ci_master_key));
  134. if (S_ISREG(inode->i_mode))
  135. crypt_info->ci_mode = ctx.contents_encryption_mode;
  136. else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  137. crypt_info->ci_mode = ctx.filenames_encryption_mode;
  138. else {
  139. printk(KERN_ERR "f2fs crypto: Unsupported inode type.\n");
  140. BUG();
  141. }
  142. crypt_info->ci_size = f2fs_encryption_key_size(crypt_info->ci_mode);
  143. BUG_ON(!crypt_info->ci_size);
  144. memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
  145. F2FS_KEY_DESC_PREFIX_SIZE);
  146. sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
  147. "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
  148. ctx.master_key_descriptor);
  149. full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
  150. (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
  151. keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
  152. if (IS_ERR(keyring_key)) {
  153. res = PTR_ERR(keyring_key);
  154. keyring_key = NULL;
  155. goto out;
  156. }
  157. BUG_ON(keyring_key->type != &key_type_logon);
  158. ukp = ((struct user_key_payload *)keyring_key->payload.data);
  159. if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
  160. res = -EINVAL;
  161. goto out;
  162. }
  163. master_key = (struct f2fs_encryption_key *)ukp->data;
  164. BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
  165. F2FS_KEY_DERIVATION_NONCE_SIZE);
  166. BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
  167. res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
  168. crypt_info->ci_raw);
  169. out:
  170. if (res < 0) {
  171. if (res == -ENOKEY)
  172. res = 0;
  173. kfree(crypt_info);
  174. } else {
  175. fi->i_crypt_info = crypt_info;
  176. crypt_info->ci_keyring_key = keyring_key;
  177. keyring_key = NULL;
  178. }
  179. if (keyring_key)
  180. key_put(keyring_key);
  181. return res;
  182. }
  183. int f2fs_has_encryption_key(struct inode *inode)
  184. {
  185. struct f2fs_inode_info *fi = F2FS_I(inode);
  186. return (fi->i_crypt_info != NULL);
  187. }