]> asedeno.scripts.mit.edu Git - linux.git/blob - fs/crypto/keyinfo.c
fscrypt: move v1 policy key setup to keysetup_v1.c
[linux.git] / fs / crypto / keyinfo.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Key setup facility for FS encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  *
7  * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8  * Heavily modified since then.
9  */
10
11 #include <crypto/aes.h>
12 #include <crypto/sha.h>
13 #include <crypto/skcipher.h>
14 #include <linux/key.h>
15
16 #include "fscrypt_private.h"
17
18 static struct crypto_shash *essiv_hash_tfm;
19
20 static struct fscrypt_mode available_modes[] = {
21         [FSCRYPT_MODE_AES_256_XTS] = {
22                 .friendly_name = "AES-256-XTS",
23                 .cipher_str = "xts(aes)",
24                 .keysize = 64,
25                 .ivsize = 16,
26         },
27         [FSCRYPT_MODE_AES_256_CTS] = {
28                 .friendly_name = "AES-256-CTS-CBC",
29                 .cipher_str = "cts(cbc(aes))",
30                 .keysize = 32,
31                 .ivsize = 16,
32         },
33         [FSCRYPT_MODE_AES_128_CBC] = {
34                 .friendly_name = "AES-128-CBC",
35                 .cipher_str = "cbc(aes)",
36                 .keysize = 16,
37                 .ivsize = 16,
38                 .needs_essiv = true,
39         },
40         [FSCRYPT_MODE_AES_128_CTS] = {
41                 .friendly_name = "AES-128-CTS-CBC",
42                 .cipher_str = "cts(cbc(aes))",
43                 .keysize = 16,
44                 .ivsize = 16,
45         },
46         [FSCRYPT_MODE_ADIANTUM] = {
47                 .friendly_name = "Adiantum",
48                 .cipher_str = "adiantum(xchacha12,aes)",
49                 .keysize = 32,
50                 .ivsize = 32,
51         },
52 };
53
54 static struct fscrypt_mode *
55 select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
56 {
57         if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
58                 fscrypt_warn(inode,
59                              "Unsupported encryption modes (contents mode %d, filenames mode %d)",
60                              ci->ci_data_mode, ci->ci_filename_mode);
61                 return ERR_PTR(-EINVAL);
62         }
63
64         if (S_ISREG(inode->i_mode))
65                 return &available_modes[ci->ci_data_mode];
66
67         if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
68                 return &available_modes[ci->ci_filename_mode];
69
70         WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
71                   inode->i_ino, (inode->i_mode & S_IFMT));
72         return ERR_PTR(-EINVAL);
73 }
74
75 /* Create a symmetric cipher object for the given encryption mode and key */
76 struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
77                                                   const u8 *raw_key,
78                                                   const struct inode *inode)
79 {
80         struct crypto_skcipher *tfm;
81         int err;
82
83         tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
84         if (IS_ERR(tfm)) {
85                 if (PTR_ERR(tfm) == -ENOENT) {
86                         fscrypt_warn(inode,
87                                      "Missing crypto API support for %s (API name: \"%s\")",
88                                      mode->friendly_name, mode->cipher_str);
89                         return ERR_PTR(-ENOPKG);
90                 }
91                 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
92                             mode->cipher_str, PTR_ERR(tfm));
93                 return tfm;
94         }
95         if (unlikely(!mode->logged_impl_name)) {
96                 /*
97                  * fscrypt performance can vary greatly depending on which
98                  * crypto algorithm implementation is used.  Help people debug
99                  * performance problems by logging the ->cra_driver_name the
100                  * first time a mode is used.  Note that multiple threads can
101                  * race here, but it doesn't really matter.
102                  */
103                 mode->logged_impl_name = true;
104                 pr_info("fscrypt: %s using implementation \"%s\"\n",
105                         mode->friendly_name,
106                         crypto_skcipher_alg(tfm)->base.cra_driver_name);
107         }
108         crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
109         err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
110         if (err)
111                 goto err_free_tfm;
112
113         return tfm;
114
115 err_free_tfm:
116         crypto_free_skcipher(tfm);
117         return ERR_PTR(err);
118 }
119
120 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
121 {
122         struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
123
124         /* init hash transform on demand */
125         if (unlikely(!tfm)) {
126                 struct crypto_shash *prev_tfm;
127
128                 tfm = crypto_alloc_shash("sha256", 0, 0);
129                 if (IS_ERR(tfm)) {
130                         if (PTR_ERR(tfm) == -ENOENT) {
131                                 fscrypt_warn(NULL,
132                                              "Missing crypto API support for SHA-256");
133                                 return -ENOPKG;
134                         }
135                         fscrypt_err(NULL,
136                                     "Error allocating SHA-256 transform: %ld",
137                                     PTR_ERR(tfm));
138                         return PTR_ERR(tfm);
139                 }
140                 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
141                 if (prev_tfm) {
142                         crypto_free_shash(tfm);
143                         tfm = prev_tfm;
144                 }
145         }
146
147         {
148                 SHASH_DESC_ON_STACK(desc, tfm);
149                 desc->tfm = tfm;
150
151                 return crypto_shash_digest(desc, key, keysize, salt);
152         }
153 }
154
155 static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
156                                 int keysize)
157 {
158         int err;
159         struct crypto_cipher *essiv_tfm;
160         u8 salt[SHA256_DIGEST_SIZE];
161
162         if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
163                 return -EINVAL;
164
165         essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
166         if (IS_ERR(essiv_tfm))
167                 return PTR_ERR(essiv_tfm);
168
169         ci->ci_essiv_tfm = essiv_tfm;
170
171         err = derive_essiv_salt(raw_key, keysize, salt);
172         if (err)
173                 goto out;
174
175         /*
176          * Using SHA256 to derive the salt/key will result in AES-256 being
177          * used for IV generation. File contents encryption will still use the
178          * configured keysize (AES-128) nevertheless.
179          */
180         err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
181         if (err)
182                 goto out;
183
184 out:
185         memzero_explicit(salt, sizeof(salt));
186         return err;
187 }
188
189 /* Given the per-file key, set up the file's crypto transform object(s) */
190 int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
191 {
192         struct fscrypt_mode *mode = ci->ci_mode;
193         struct crypto_skcipher *ctfm;
194         int err;
195
196         ctfm = fscrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
197         if (IS_ERR(ctfm))
198                 return PTR_ERR(ctfm);
199
200         ci->ci_ctfm = ctfm;
201
202         if (mode->needs_essiv) {
203                 err = init_essiv_generator(ci, derived_key, mode->keysize);
204                 if (err) {
205                         fscrypt_warn(ci->ci_inode,
206                                      "Error initializing ESSIV generator: %d",
207                                      err);
208                         return err;
209                 }
210         }
211         return 0;
212 }
213
214 /*
215  * Find the master key, then set up the inode's actual encryption key.
216  */
217 static int setup_file_encryption_key(struct fscrypt_info *ci)
218 {
219         return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
220 }
221
222 static void put_crypt_info(struct fscrypt_info *ci)
223 {
224         if (!ci)
225                 return;
226
227         if (ci->ci_direct_key) {
228                 fscrypt_put_direct_key(ci->ci_direct_key);
229         } else {
230                 crypto_free_skcipher(ci->ci_ctfm);
231                 crypto_free_cipher(ci->ci_essiv_tfm);
232         }
233         kmem_cache_free(fscrypt_info_cachep, ci);
234 }
235
236 int fscrypt_get_encryption_info(struct inode *inode)
237 {
238         struct fscrypt_info *crypt_info;
239         struct fscrypt_context ctx;
240         struct fscrypt_mode *mode;
241         int res;
242
243         if (fscrypt_has_encryption_key(inode))
244                 return 0;
245
246         res = fscrypt_initialize(inode->i_sb->s_cop->flags);
247         if (res)
248                 return res;
249
250         res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
251         if (res < 0) {
252                 if (!fscrypt_dummy_context_enabled(inode) ||
253                     IS_ENCRYPTED(inode)) {
254                         fscrypt_warn(inode,
255                                      "Error %d getting encryption context",
256                                      res);
257                         return res;
258                 }
259                 /* Fake up a context for an unencrypted directory */
260                 memset(&ctx, 0, sizeof(ctx));
261                 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
262                 ctx.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
263                 ctx.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
264                 memset(ctx.master_key_descriptor, 0x42,
265                        FSCRYPT_KEY_DESCRIPTOR_SIZE);
266         } else if (res != sizeof(ctx)) {
267                 fscrypt_warn(inode,
268                              "Unknown encryption context size (%d bytes)", res);
269                 return -EINVAL;
270         }
271
272         if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) {
273                 fscrypt_warn(inode, "Unknown encryption context version (%d)",
274                              ctx.format);
275                 return -EINVAL;
276         }
277
278         if (ctx.flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
279                 fscrypt_warn(inode, "Unknown encryption context flags (0x%02x)",
280                              ctx.flags);
281                 return -EINVAL;
282         }
283
284         crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
285         if (!crypt_info)
286                 return -ENOMEM;
287
288         crypt_info->ci_inode = inode;
289
290         crypt_info->ci_flags = ctx.flags;
291         crypt_info->ci_data_mode = ctx.contents_encryption_mode;
292         crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
293         memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor,
294                FSCRYPT_KEY_DESCRIPTOR_SIZE);
295         memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
296
297         mode = select_encryption_mode(crypt_info, inode);
298         if (IS_ERR(mode)) {
299                 res = PTR_ERR(mode);
300                 goto out;
301         }
302         WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
303         crypt_info->ci_mode = mode;
304
305         res = setup_file_encryption_key(crypt_info);
306         if (res)
307                 goto out;
308
309         if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL)
310                 crypt_info = NULL;
311 out:
312         if (res == -ENOKEY)
313                 res = 0;
314         put_crypt_info(crypt_info);
315         return res;
316 }
317 EXPORT_SYMBOL(fscrypt_get_encryption_info);
318
319 /**
320  * fscrypt_put_encryption_info - free most of an inode's fscrypt data
321  *
322  * Free the inode's fscrypt_info.  Filesystems must call this when the inode is
323  * being evicted.  An RCU grace period need not have elapsed yet.
324  */
325 void fscrypt_put_encryption_info(struct inode *inode)
326 {
327         put_crypt_info(inode->i_crypt_info);
328         inode->i_crypt_info = NULL;
329 }
330 EXPORT_SYMBOL(fscrypt_put_encryption_info);
331
332 /**
333  * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
334  *
335  * Free the inode's cached decrypted symlink target, if any.  Filesystems must
336  * call this after an RCU grace period, just before they free the inode.
337  */
338 void fscrypt_free_inode(struct inode *inode)
339 {
340         if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
341                 kfree(inode->i_link);
342                 inode->i_link = NULL;
343         }
344 }
345 EXPORT_SYMBOL(fscrypt_free_inode);