f2fs: introduce f2fs_flush_merged_bios for cleanup
[cascardo/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 derivation.
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                 wait_for_completion(&ecr.completion);
79                 res = ecr.res;
80         }
81 out:
82         if (req)
83                 ablkcipher_request_free(req);
84         if (tfm)
85                 crypto_free_ablkcipher(tfm);
86         return res;
87 }
88
89 static void f2fs_free_crypt_info(struct f2fs_crypt_info *ci)
90 {
91         if (!ci)
92                 return;
93
94         key_put(ci->ci_keyring_key);
95         crypto_free_ablkcipher(ci->ci_ctfm);
96         kmem_cache_free(f2fs_crypt_info_cachep, ci);
97 }
98
99 void f2fs_free_encryption_info(struct inode *inode, struct f2fs_crypt_info *ci)
100 {
101         struct f2fs_inode_info *fi = F2FS_I(inode);
102         struct f2fs_crypt_info *prev;
103
104         if (ci == NULL)
105                 ci = ACCESS_ONCE(fi->i_crypt_info);
106         if (ci == NULL)
107                 return;
108         prev = cmpxchg(&fi->i_crypt_info, ci, NULL);
109         if (prev != ci)
110                 return;
111
112         f2fs_free_crypt_info(ci);
113 }
114
115 int _f2fs_get_encryption_info(struct inode *inode)
116 {
117         struct f2fs_inode_info *fi = F2FS_I(inode);
118         struct f2fs_crypt_info *crypt_info;
119         char full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
120                                 (F2FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
121         struct key *keyring_key = NULL;
122         struct f2fs_encryption_key *master_key;
123         struct f2fs_encryption_context ctx;
124         const struct user_key_payload *ukp;
125         struct crypto_ablkcipher *ctfm;
126         const char *cipher_str;
127         char raw_key[F2FS_MAX_KEY_SIZE];
128         char mode;
129         int res;
130
131         res = f2fs_crypto_initialize();
132         if (res)
133                 return res;
134 retry:
135         crypt_info = ACCESS_ONCE(fi->i_crypt_info);
136         if (crypt_info) {
137                 if (!crypt_info->ci_keyring_key ||
138                                 key_validate(crypt_info->ci_keyring_key) == 0)
139                         return 0;
140                 f2fs_free_encryption_info(inode, crypt_info);
141                 goto retry;
142         }
143
144         res = f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
145                                 F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
146                                 &ctx, sizeof(ctx), NULL);
147         if (res < 0)
148                 return res;
149         else if (res != sizeof(ctx))
150                 return -EINVAL;
151         res = 0;
152
153         crypt_info = kmem_cache_alloc(f2fs_crypt_info_cachep, GFP_NOFS);
154         if (!crypt_info)
155                 return -ENOMEM;
156
157         crypt_info->ci_flags = ctx.flags;
158         crypt_info->ci_data_mode = ctx.contents_encryption_mode;
159         crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
160         crypt_info->ci_ctfm = NULL;
161         crypt_info->ci_keyring_key = NULL;
162         memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
163                                 sizeof(crypt_info->ci_master_key));
164         if (S_ISREG(inode->i_mode))
165                 mode = crypt_info->ci_data_mode;
166         else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
167                 mode = crypt_info->ci_filename_mode;
168         else
169                 BUG();
170
171         switch (mode) {
172         case F2FS_ENCRYPTION_MODE_AES_256_XTS:
173                 cipher_str = "xts(aes)";
174                 break;
175         case F2FS_ENCRYPTION_MODE_AES_256_CTS:
176                 cipher_str = "cts(cbc(aes))";
177                 break;
178         default:
179                 printk_once(KERN_WARNING
180                             "f2fs: unsupported key mode %d (ino %u)\n",
181                             mode, (unsigned) inode->i_ino);
182                 res = -ENOKEY;
183                 goto out;
184         }
185
186         memcpy(full_key_descriptor, F2FS_KEY_DESC_PREFIX,
187                                         F2FS_KEY_DESC_PREFIX_SIZE);
188         sprintf(full_key_descriptor + F2FS_KEY_DESC_PREFIX_SIZE,
189                                         "%*phN", F2FS_KEY_DESCRIPTOR_SIZE,
190                                         ctx.master_key_descriptor);
191         full_key_descriptor[F2FS_KEY_DESC_PREFIX_SIZE +
192                                         (2 * F2FS_KEY_DESCRIPTOR_SIZE)] = '\0';
193         keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
194         if (IS_ERR(keyring_key)) {
195                 res = PTR_ERR(keyring_key);
196                 keyring_key = NULL;
197                 goto out;
198         }
199         crypt_info->ci_keyring_key = keyring_key;
200         if (keyring_key->type != &key_type_logon) {
201                 printk_once(KERN_WARNING "f2fs: key type must be logon\n");
202                 res = -ENOKEY;
203                 goto out;
204         }
205         down_read(&keyring_key->sem);
206         ukp = user_key_payload(keyring_key);
207         if (ukp->datalen != sizeof(struct f2fs_encryption_key)) {
208                 res = -EINVAL;
209                 up_read(&keyring_key->sem);
210                 goto out;
211         }
212         master_key = (struct f2fs_encryption_key *)ukp->data;
213         BUILD_BUG_ON(F2FS_AES_128_ECB_KEY_SIZE !=
214                                 F2FS_KEY_DERIVATION_NONCE_SIZE);
215         if (master_key->size != F2FS_AES_256_XTS_KEY_SIZE) {
216                 printk_once(KERN_WARNING
217                                 "f2fs: key size incorrect: %d\n",
218                                 master_key->size);
219                 res = -ENOKEY;
220                 up_read(&keyring_key->sem);
221                 goto out;
222         }
223         res = f2fs_derive_key_aes(ctx.nonce, master_key->raw,
224                                   raw_key);
225         up_read(&keyring_key->sem);
226         if (res)
227                 goto out;
228
229         ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
230         if (!ctfm || IS_ERR(ctfm)) {
231                 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
232                 printk(KERN_DEBUG
233                        "%s: error %d (inode %u) allocating crypto tfm\n",
234                        __func__, res, (unsigned) inode->i_ino);
235                 goto out;
236         }
237         crypt_info->ci_ctfm = ctfm;
238         crypto_ablkcipher_clear_flags(ctfm, ~0);
239         crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
240                              CRYPTO_TFM_REQ_WEAK_KEY);
241         res = crypto_ablkcipher_setkey(ctfm, raw_key,
242                                         f2fs_encryption_key_size(mode));
243         if (res)
244                 goto out;
245
246         memzero_explicit(raw_key, sizeof(raw_key));
247         if (cmpxchg(&fi->i_crypt_info, NULL, crypt_info) != NULL) {
248                 f2fs_free_crypt_info(crypt_info);
249                 goto retry;
250         }
251         return 0;
252
253 out:
254         if (res == -ENOKEY && !S_ISREG(inode->i_mode))
255                 res = 0;
256
257         f2fs_free_crypt_info(crypt_info);
258         memzero_explicit(raw_key, sizeof(raw_key));
259         return res;
260 }
261
262 int f2fs_has_encryption_key(struct inode *inode)
263 {
264         struct f2fs_inode_info *fi = F2FS_I(inode);
265
266         return (fi->i_crypt_info != NULL);
267 }