Merge branch 'upstream' of git://git.infradead.org/users/pcmoore/selinux into for...
[deliverable/linux.git] / fs / f2fs / crypto_key.c
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
20 #include "f2fs.h"
21 #include "xattr.h"
22
23 static void derive_crypt_complete(struct crypto_async_request *req, int rc)
24 {
25 struct f2fs_completion_result *ecr = req->data;
26
27 if (rc == -EINPROGRESS)
28 return;
29
30 ecr->res = rc;
31 complete(&ecr->completion);
32 }
33
34 /**
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.
39 *
40 * Return: Zero on success; non-zero otherwise.
41 */
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])
45 {
46 int res = 0;
47 struct ablkcipher_request *req = NULL;
48 DECLARE_F2FS_COMPLETION_RESULT(ecr);
49 struct scatterlist src_sg, dst_sg;
50 struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
51 0);
52
53 if (IS_ERR(tfm)) {
54 res = PTR_ERR(tfm);
55 tfm = NULL;
56 goto out;
57 }
58 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
59 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
60 if (!req) {
61 res = -ENOMEM;
62 goto out;
63 }
64 ablkcipher_request_set_callback(req,
65 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
66 derive_crypt_complete, &ecr);
67 res = crypto_ablkcipher_setkey(tfm, deriving_key,
68 F2FS_AES_128_ECB_KEY_SIZE);
69 if (res < 0)
70 goto out;
71
72 sg_init_one(&src_sg, source_key, F2FS_AES_256_XTS_KEY_SIZE);
73 sg_init_one(&dst_sg, derived_key, F2FS_AES_256_XTS_KEY_SIZE);
74 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
75 F2FS_AES_256_XTS_KEY_SIZE, NULL);
76 res = crypto_ablkcipher_encrypt(req);
77 if (res == -EINPROGRESS || res == -EBUSY) {
78 BUG_ON(req->base.data != &ecr);
79 wait_for_completion(&ecr.completion);
80 res = ecr.res;
81 }
82 out:
83 if (req)
84 ablkcipher_request_free(req);
85 if (tfm)
86 crypto_free_ablkcipher(tfm);
87 return res;
88 }
89
90 static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
91 {
92 if (!ci)
93 return;
94
95 if (ci->ci_keyring_key)
96 key_put(ci->ci_keyring_key);
97 crypto_free_ablkcipher(ci->ci_ctfm);
98 kmem_cache_free(f2fs_crypt_info_cachep, ci);
99 }
100
101 void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
102 {
103 struct f2fs_inode_info *fi = F2FS_I(inode);
104 struct f2fs_crypt_info *prev;
105
106 if (ci == NULL)
107 ci = ACCESS_ONCE(fi->i_crypt_info);
108 if (ci == NULL)
109 return;
110 prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
111 if (prev != ci)
112 return;
113
114 f2fs_free_crypt_info(ci);
115 }
116
117 int _f2fs_get_encryption_info(struct inode *inode)
118 {
119 struct f2fs_inode_info *fi = F2FS_I(inode);
120 struct f2fs_crypt_info *crypt_info;
121 char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
122 (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
123 struct key *keyring_key = NULL;
124 struct f2fs_encryption_key *master_key;
125 struct f2fs_encryption_context ctx;
126 struct user_key_payload *ukp;
127 struct crypto_ablkcipher *ctfm;
128 const char *cipher_str;
129 char raw_key[F2FS_MAX_KEY_SIZE];
130 char mode;
131 int res;
132
133 res = f2fs_crypto_initialize();
134 if (res)
135 return res;
136 retry:
137 crypt_info = ACCESS_ONCE(fi->i_crypt_info);
138 if (crypt_info) {
139 if (!crypt_info->ci_keyring_key ||
140 key_validate(crypt_info->ci_keyring_key) == 0)
141 return 0;
142 f2fs_free_encryption_info(inode, crypt_info);
143 goto retry;
144 }
145
146 res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
147 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
148 &ctx, sizeof(ctx), NULL);
149 if (res < 0)
150 return res;
151 else if (res != sizeof(ctx))
152 return -EINVAL;
153 res = 0;
154
155 crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
156 if (!crypt_info)
157 return -ENOMEM;
158
159 crypt_info->ci_flags = ctx.flags;
160 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
161 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
162 crypt_info->ci_ctfm = NULL;
163 crypt_info->ci_keyring_key = NULL;
164 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
165 sizeof(crypt_info->ci_master_key));
166 if (S_ISREG(inode->i_mode))
167 mode = crypt_info->ci_data_mode;
168 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
169 mode = crypt_info->ci_filename_mode;
170 else
171 BUG();
172
173 switch (mode) {
174 case F2FS_ENCRYPTION_MODE_AES_256_XTS:
175 cipher_str = "xts(aes)";
176 break;
177 case F2FS_ENCRYPTION_MODE_AES_256_CTS:
178 cipher_str = "cts(cbc(aes))";
179 break;
180 default:
181 printk_once(KERN_WARNING
182 "f2fs: unsupported key mode %d (ino %u)\n",
183 mode, (unsigned) inode->i_ino);
184 res = -ENOKEY;
185 goto out;
186 }
187
188 memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
189 F2FS_KEY_DESC_PREFIX_SIZE);
190 sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
191 "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
192 ctx.master_key_descriptor);
193 full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
194 (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
195 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
196 if (IS_ERR(keyring_key)) {
197 res = PTR_ERR(keyring_key);
198 keyring_key = NULL;
199 goto out;
200 }
201 crypt_info->ci_keyring_key = keyring_key;
202 BUG_ON(keyring_key->type != &key_type_logon);
203 ukp = ((struct user_key_payload *)keyring_key->payload.data);
204 if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
205 res = -EINVAL;
206 goto out;
207 }
208 master_key = (struct f2fs_encryption_key *)ukp->data;
209 BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
210 F2FS_KEY_DERIVATION_NONCE_SIZE);
211 BUG_ON(master_key->size != F2FS_AES_256_XTS_KEY_SIZE);
212 res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
213 raw_key);
214 if (res)
215 goto out;
216
217 ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
218 if (!ctfm || IS_ERR(ctfm)) {
219 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
220 printk(KERN_DEBUG
221 "%s: error %d (inode %u) allocating crypto tfm\n",
222 __func__, res, (unsigned) inode->i_ino);
223 goto out;
224 }
225 crypt_info->ci_ctfm = ctfm;
226 crypto_ablkcipher_clear_flags(ctfm, ~0);
227 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
228 CRYPTO_TFM_REQ_WEAK_KEY);
229 res = crypto_ablkcipher_setkey(ctfm, raw_key,
230 f2fs_encryption_key_size(mode));
231 if (res)
232 goto out;
233
234 memzero_explicit(raw_key, sizeof(raw_key));
235 if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
236 f2fs_free_crypt_info(crypt_info);
237 goto retry;
238 }
239 return 0;
240
241 out:
242 if (res == -ENOKEY && !S_ISREG(inode->i_mode))
243 res = 0;
244
245 f2fs_free_crypt_info(crypt_info);
246 memzero_explicit(raw_key, sizeof(raw_key));
247 return res;
248 }
249
250 int f2fs_has_encryption_key(struct inode *inode)
251 {
252 struct f2fs_inode_info *fi = F2FS_I(inode);
253
254 return (fi->i_crypt_info != NULL);
255 }
This page took 0.045565 seconds and 5 git commands to generate.