OSDN Git Service

f6d6acd37b97a738474f8bcff851eadd591363f2
[uclinux-h8/linux.git] / fs / crypto / keyinfo.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * key management facility for FS encryption support.
4  *
5  * Copyright (C) 2015, Google, Inc.
6  *
7  * This contains encryption key functions.
8  *
9  * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
10  */
11
12 #include <keys/user-type.h>
13 #include <linux/scatterlist.h>
14 #include <linux/ratelimit.h>
15 #include <crypto/aes.h>
16 #include <crypto/sha.h>
17 #include <crypto/skcipher.h>
18 #include "fscrypt_private.h"
19
20 static struct crypto_shash *essiv_hash_tfm;
21
22 /**
23  * derive_key_aes() - Derive a key using AES-128-ECB
24  * @deriving_key: Encryption key used for derivation.
25  * @source_key:   Source key to which to apply derivation.
26  * @derived_raw_key:  Derived raw key.
27  *
28  * Return: Zero on success; non-zero otherwise.
29  */
30 static int derive_key_aes(u8 deriving_key[FS_KEY_DERIVATION_NONCE_SIZE],
31                                 const struct fscrypt_key *source_key,
32                                 u8 derived_raw_key[FS_MAX_KEY_SIZE])
33 {
34         int res = 0;
35         struct skcipher_request *req = NULL;
36         DECLARE_CRYPTO_WAIT(wait);
37         struct scatterlist src_sg, dst_sg;
38         struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
39
40         if (IS_ERR(tfm)) {
41                 res = PTR_ERR(tfm);
42                 tfm = NULL;
43                 goto out;
44         }
45         crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
46         req = skcipher_request_alloc(tfm, GFP_NOFS);
47         if (!req) {
48                 res = -ENOMEM;
49                 goto out;
50         }
51         skcipher_request_set_callback(req,
52                         CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
53                         crypto_req_done, &wait);
54         res = crypto_skcipher_setkey(tfm, deriving_key,
55                                      FS_KEY_DERIVATION_NONCE_SIZE);
56         if (res < 0)
57                 goto out;
58
59         sg_init_one(&src_sg, source_key->raw, source_key->size);
60         sg_init_one(&dst_sg, derived_raw_key, source_key->size);
61         skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
62                                    NULL);
63         res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
64 out:
65         skcipher_request_free(req);
66         crypto_free_skcipher(tfm);
67         return res;
68 }
69
70 static int validate_user_key(struct fscrypt_info *crypt_info,
71                         struct fscrypt_context *ctx, u8 *raw_key,
72                         const char *prefix, int min_keysize)
73 {
74         char *description;
75         struct key *keyring_key;
76         struct fscrypt_key *master_key;
77         const struct user_key_payload *ukp;
78         int res;
79
80         description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
81                                 FS_KEY_DESCRIPTOR_SIZE,
82                                 ctx->master_key_descriptor);
83         if (!description)
84                 return -ENOMEM;
85
86         keyring_key = request_key(&key_type_logon, description, NULL);
87         kfree(description);
88         if (IS_ERR(keyring_key))
89                 return PTR_ERR(keyring_key);
90         down_read(&keyring_key->sem);
91
92         ukp = user_key_payload_locked(keyring_key);
93         if (!ukp) {
94                 /* key was revoked before we acquired its semaphore */
95                 res = -EKEYREVOKED;
96                 goto out;
97         }
98         if (ukp->datalen != sizeof(struct fscrypt_key)) {
99                 res = -EINVAL;
100                 goto out;
101         }
102         master_key = (struct fscrypt_key *)ukp->data;
103
104         if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
105             || master_key->size % AES_BLOCK_SIZE != 0) {
106                 printk_once(KERN_WARNING
107                                 "%s: key size incorrect: %d\n",
108                                 __func__, master_key->size);
109                 res = -ENOKEY;
110                 goto out;
111         }
112         res = derive_key_aes(ctx->nonce, master_key, raw_key);
113 out:
114         up_read(&keyring_key->sem);
115         key_put(keyring_key);
116         return res;
117 }
118
119 static const struct {
120         const char *cipher_str;
121         int keysize;
122 } available_modes[] = {
123         [FS_ENCRYPTION_MODE_AES_256_XTS]      = { "xts(aes)",           64 },
124         [FS_ENCRYPTION_MODE_AES_256_CTS]      = { "cts(cbc(aes))",      32 },
125         [FS_ENCRYPTION_MODE_AES_128_CBC]      = { "cbc(aes)",           16 },
126         [FS_ENCRYPTION_MODE_AES_128_CTS]      = { "cts(cbc(aes))",      16 },
127 };
128
129 static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
130                                  const char **cipher_str_ret, int *keysize_ret)
131 {
132         u32 mode;
133
134         if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
135                 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
136                                     inode->i_ino,
137                                     ci->ci_data_mode, ci->ci_filename_mode);
138                 return -EINVAL;
139         }
140
141         if (S_ISREG(inode->i_mode)) {
142                 mode = ci->ci_data_mode;
143         } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
144                 mode = ci->ci_filename_mode;
145         } else {
146                 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
147                           inode->i_ino, (inode->i_mode & S_IFMT));
148                 return -EINVAL;
149         }
150
151         *cipher_str_ret = available_modes[mode].cipher_str;
152         *keysize_ret = available_modes[mode].keysize;
153         return 0;
154 }
155
156 static void put_crypt_info(struct fscrypt_info *ci)
157 {
158         if (!ci)
159                 return;
160
161         crypto_free_skcipher(ci->ci_ctfm);
162         crypto_free_cipher(ci->ci_essiv_tfm);
163         kmem_cache_free(fscrypt_info_cachep, ci);
164 }
165
166 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
167 {
168         struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
169
170         /* init hash transform on demand */
171         if (unlikely(!tfm)) {
172                 struct crypto_shash *prev_tfm;
173
174                 tfm = crypto_alloc_shash("sha256", 0, 0);
175                 if (IS_ERR(tfm)) {
176                         pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
177                                             PTR_ERR(tfm));
178                         return PTR_ERR(tfm);
179                 }
180                 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
181                 if (prev_tfm) {
182                         crypto_free_shash(tfm);
183                         tfm = prev_tfm;
184                 }
185         }
186
187         {
188                 SHASH_DESC_ON_STACK(desc, tfm);
189                 desc->tfm = tfm;
190                 desc->flags = 0;
191
192                 return crypto_shash_digest(desc, key, keysize, salt);
193         }
194 }
195
196 static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
197                                 int keysize)
198 {
199         int err;
200         struct crypto_cipher *essiv_tfm;
201         u8 salt[SHA256_DIGEST_SIZE];
202
203         essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
204         if (IS_ERR(essiv_tfm))
205                 return PTR_ERR(essiv_tfm);
206
207         ci->ci_essiv_tfm = essiv_tfm;
208
209         err = derive_essiv_salt(raw_key, keysize, salt);
210         if (err)
211                 goto out;
212
213         /*
214          * Using SHA256 to derive the salt/key will result in AES-256 being
215          * used for IV generation. File contents encryption will still use the
216          * configured keysize (AES-128) nevertheless.
217          */
218         err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
219         if (err)
220                 goto out;
221
222 out:
223         memzero_explicit(salt, sizeof(salt));
224         return err;
225 }
226
227 void __exit fscrypt_essiv_cleanup(void)
228 {
229         crypto_free_shash(essiv_hash_tfm);
230 }
231
232 int fscrypt_get_encryption_info(struct inode *inode)
233 {
234         struct fscrypt_info *crypt_info;
235         struct fscrypt_context ctx;
236         struct crypto_skcipher *ctfm;
237         const char *cipher_str;
238         int keysize;
239         u8 *raw_key = NULL;
240         int res;
241
242         if (inode->i_crypt_info)
243                 return 0;
244
245         res = fscrypt_initialize(inode->i_sb->s_cop->flags);
246         if (res)
247                 return res;
248
249         res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
250         if (res < 0) {
251                 if (!fscrypt_dummy_context_enabled(inode) ||
252                     IS_ENCRYPTED(inode))
253                         return res;
254                 /* Fake up a context for an unencrypted directory */
255                 memset(&ctx, 0, sizeof(ctx));
256                 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
257                 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
258                 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
259                 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
260         } else if (res != sizeof(ctx)) {
261                 return -EINVAL;
262         }
263
264         if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
265                 return -EINVAL;
266
267         if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
268                 return -EINVAL;
269
270         crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
271         if (!crypt_info)
272                 return -ENOMEM;
273
274         crypt_info->ci_flags = ctx.flags;
275         crypt_info->ci_data_mode = ctx.contents_encryption_mode;
276         crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
277         crypt_info->ci_ctfm = NULL;
278         crypt_info->ci_essiv_tfm = NULL;
279         memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
280                                 sizeof(crypt_info->ci_master_key));
281
282         res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
283         if (res)
284                 goto out;
285
286         /*
287          * This cannot be a stack buffer because it is passed to the scatterlist
288          * crypto API as part of key derivation.
289          */
290         res = -ENOMEM;
291         raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
292         if (!raw_key)
293                 goto out;
294
295         res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
296                                 keysize);
297         if (res && inode->i_sb->s_cop->key_prefix) {
298                 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
299                                              inode->i_sb->s_cop->key_prefix,
300                                              keysize);
301                 if (res2) {
302                         if (res2 == -ENOKEY)
303                                 res = -ENOKEY;
304                         goto out;
305                 }
306         } else if (res) {
307                 goto out;
308         }
309         ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
310         if (IS_ERR(ctfm)) {
311                 res = PTR_ERR(ctfm);
312                 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
313                          __func__, res, inode->i_ino);
314                 goto out;
315         }
316         crypt_info->ci_ctfm = ctfm;
317         crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
318         /*
319          * if the provided key is longer than keysize, we use the first
320          * keysize bytes of the derived key only
321          */
322         res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
323         if (res)
324                 goto out;
325
326         if (S_ISREG(inode->i_mode) &&
327             crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
328                 res = init_essiv_generator(crypt_info, raw_key, keysize);
329                 if (res) {
330                         pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
331                                  __func__, res, inode->i_ino);
332                         goto out;
333                 }
334         }
335         if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
336                 crypt_info = NULL;
337 out:
338         if (res == -ENOKEY)
339                 res = 0;
340         put_crypt_info(crypt_info);
341         kzfree(raw_key);
342         return res;
343 }
344 EXPORT_SYMBOL(fscrypt_get_encryption_info);
345
346 void fscrypt_put_encryption_info(struct inode *inode)
347 {
348         put_crypt_info(inode->i_crypt_info);
349         inode->i_crypt_info = NULL;
350 }
351 EXPORT_SYMBOL(fscrypt_put_encryption_info);