2 * linux/fs/f2fs/crypto_key.c
4 * Copied from linux/fs/f2fs/crypto_key.c
6 * Copyright (C) 2015, Google, Inc.
8 * This contains encryption key functions for f2fs
10 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
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/skcipher.h>
18 #include <linux/f2fs_fs.h>
23 static void derive_crypt_complete(struct crypto_async_request *req, int rc)
25 struct f2fs_completion_result *ecr = req->data;
27 if (rc == -EINPROGRESS)
31 complete(&ecr->completion);
35 * f2fs_derive_key_aes() - Derive a key using AES-128-ECB
36 * @deriving_key: Encryption key used for derivatio.
37 * @source_key: Source key to which to apply derivation.
38 * @derived_key: Derived key.
40 * Return: Zero on success; non-zero otherwise.
42 static int f2fs_derive_key_aes(char deriving_key[F2FS_AES_128_ECB_KEY_SIZE],
43 char source_key[F2FS_AES_256_XTS_KEY_SIZE],
44 char derived_key[F2FS_AES_256_XTS_KEY_SIZE])
47 struct skcipher_request *req = NULL;
48 DECLARE_F2FS_COMPLETION_RESULT(ecr);
49 struct scatterlist src_sg, dst_sg;
50 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
57 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
58 req = skcipher_request_alloc(tfm, GFP_NOFS);
63 skcipher_request_set_callback(req,
64 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
65 derive_crypt_complete, &ecr);
66 res = crypto_skcipher_setkey(tfm, deriving_key,
67 F2FS_AES_128_ECB_KEY_SIZE);
71 sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE);
72 sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE);
73 skcipher_request_set_crypt(req, &src_sg, &dst_sg,
74 F2FS_AES_256_XTS_KEY_SIZE, NULL);
75 res = crypto_skcipher_encrypt(req);
76 if (res == -EINPROGRESS || res == -EBUSY) {
77 BUG_ON(req->base.data != &ecr);
78 wait_for_completion(&ecr.completion);
82 skcipher_request_free(req);
83 crypto_free_skcipher(tfm);
87 static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
92 key_put(ci->ci_keyring_key);
93 crypto_free_skcipher(ci->ci_ctfm);
94 kmem_cache_free(f2fs_crypt_info_cachep, ci);
97 void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
99 struct f2fs_inode_info *fi = F2FS_I(inode);
100 struct f2fs_crypt_info *prev;
103 ci = ACCESS_ONCE(fi->i_crypt_info);
106 prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
110 f2fs_free_crypt_info(ci);
113 int _f2fs_get_encryption_info(struct inode *inode)
115 struct f2fs_inode_info *fi = F2FS_I(inode);
116 struct f2fs_crypt_info *crypt_info;
117 char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
118 (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
119 struct key *keyring_key = NULL;
120 struct f2fs_encryption_key *master_key;
121 struct f2fs_encryption_context ctx;
122 const struct user_key_payload *ukp;
123 struct crypto_skcipher *ctfm;
124 const char *cipher_str;
125 char raw_key[F2FS_MAX_KEY_SIZE];
129 res = f2fs_crypto_initialize();
133 crypt_info = ACCESS_ONCE(fi->i_crypt_info);
135 if (!crypt_info->ci_keyring_key ||
136 key_validate(crypt_info->ci_keyring_key) == 0)
138 f2fs_free_encryption_info(inode, crypt_info);
142 res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
143 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
144 &ctx, sizeof(ctx), NULL);
147 else if (res != sizeof(ctx))
151 crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
155 crypt_info->ci_flags = ctx.flags;
156 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
157 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
158 crypt_info->ci_ctfm = NULL;
159 crypt_info->ci_keyring_key = NULL;
160 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
161 sizeof(crypt_info->ci_master_key));
162 if (S_ISREG(inode->i_mode))
163 mode = crypt_info->ci_data_mode;
164 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
165 mode = crypt_info->ci_filename_mode;
170 case F2FS_ENCRYPTION_MODE_AES_256_XTS:
171 cipher_str = "xts(aes)";
173 case F2FS_ENCRYPTION_MODE_AES_256_CTS:
174 cipher_str = "cts(cbc(aes))";
177 printk_once(KERN_WARNING
178 "f2fs: unsupported key mode %d (ino %u)\n",
179 mode, (unsigned) inode->i_ino);
184 memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
185 F2FS_KEY_DESC_PREFIX_SIZE);
186 sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
187 "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
188 ctx.master_key_descriptor);
189 full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
190 (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
191 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
192 if (IS_ERR(keyring_key)) {
193 res = PTR_ERR(keyring_key);
197 crypt_info->ci_keyring_key = keyring_key;
198 BUG_ON(keyring_key->type != &key_type_logon);
199 ukp = user_key_payload(keyring_key);
200 if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
204 master_key = (struct f2fs_encryption_key *)ukp->data;
205 BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
206 F2FS_KEY_DERIVATION_NONCE_SIZE);
207 BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
208 res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
213 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
214 if (!ctfm || IS_ERR(ctfm)) {
215 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
217 "%s: error %d (inode %u) allocating crypto tfm\n",
218 __func__, res, (unsigned) inode->i_ino);
221 crypt_info->ci_ctfm = ctfm;
222 crypto_skcipher_clear_flags(ctfm, ~0);
223 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
224 res = crypto_skcipher_setkey(ctfm, raw_key,
225 f2fs_encryption_key_size(mode));
229 memzero_explicit(raw_key, sizeof(raw_key));
230 if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
231 f2fs_free_crypt_info(crypt_info);
237 if (res == -ENOKEY && !S_ISREG(inode->i_mode))
240 f2fs_free_crypt_info(crypt_info);
241 memzero_explicit(raw_key, sizeof(raw_key));
245 int f2fs_has_encryption_key(struct inode *inode)
247 struct f2fs_inode_info *fi = F2FS_I(inode);
249 return (fi->i_crypt_info != NULL);