blob: 3c84caca27ebacede5783f74bc54069c35e8be29 [file] [log] [blame]
Jaegeuk Kim0b81d072015-05-15 16:26:10 -07001/*
2 * key management facility for FS encryption support.
3 *
4 * Copyright (C) 2015, Google, Inc.
5 *
6 * This contains encryption key functions.
7 *
8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
9 */
10
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070011#include <keys/user-type.h>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070012#include <linux/scatterlist.h>
Daniel Walterb7e7cf72017-06-19 09:27:58 +020013#include <linux/ratelimit.h>
14#include <crypto/aes.h>
15#include <crypto/sha.h>
Theodore Ts'o3325bea2016-11-26 20:32:46 -050016#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070017
Daniel Walterb7e7cf72017-06-19 09:27:58 +020018static struct crypto_shash *essiv_hash_tfm;
19
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070020/**
21 * derive_key_aes() - Derive a key using AES-128-ECB
22 * @deriving_key: Encryption key used for derivation.
23 * @source_key: Source key to which to apply derivation.
Daniel Walterb7e7cf72017-06-19 09:27:58 +020024 * @derived_raw_key: Derived raw key.
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070025 *
26 * Return: Zero on success; non-zero otherwise.
27 */
28static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
Daniel Walterb7e7cf72017-06-19 09:27:58 +020029 const struct fscrypt_key *source_key,
30 u8 derived_raw_key[FS_MAX_KEY_SIZE])
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070031{
32 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070033 struct skcipher_request *req = NULL;
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010034 DECLARE_CRYPTO_WAIT(wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070035 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070036 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070037
38 if (IS_ERR(tfm)) {
39 res = PTR_ERR(tfm);
40 tfm = NULL;
41 goto out;
42 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070043 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
44 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070045 if (!req) {
46 res = -ENOMEM;
47 goto out;
48 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070049 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070050 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010051 crypto_req_done, &wait);
Linus Torvaldsd4075742016-03-21 11:03:02 -070052 res = crypto_skcipher_setkey(tfm, deriving_key,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070053 FS_AES_128_ECB_KEY_SIZE);
54 if (res < 0)
55 goto out;
56
Daniel Walterb7e7cf72017-06-19 09:27:58 +020057 sg_init_one(&src_sg, source_key->raw, source_key->size);
58 sg_init_one(&dst_sg, derived_raw_key, source_key->size);
59 skcipher_request_set_crypt(req, &src_sg, &dst_sg, source_key->size,
60 NULL);
Gilad Ben-Yossefd0082e12017-10-18 08:00:44 +010061 res = crypto_wait_req(crypto_skcipher_encrypt(req), &wait);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070062out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070063 skcipher_request_free(req);
64 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070065 return res;
66}
67
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070068static int validate_user_key(struct fscrypt_info *crypt_info,
69 struct fscrypt_context *ctx, u8 *raw_key,
Daniel Walterb7e7cf72017-06-19 09:27:58 +020070 const char *prefix, int min_keysize)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070071{
Eric Biggersa5d431e2017-01-05 13:51:18 -080072 char *description;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070073 struct key *keyring_key;
74 struct fscrypt_key *master_key;
75 const struct user_key_payload *ukp;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070076 int res;
77
Eric Biggersa5d431e2017-01-05 13:51:18 -080078 description = kasprintf(GFP_NOFS, "%s%*phN", prefix,
79 FS_KEY_DESCRIPTOR_SIZE,
80 ctx->master_key_descriptor);
81 if (!description)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070082 return -ENOMEM;
83
Eric Biggersa5d431e2017-01-05 13:51:18 -080084 keyring_key = request_key(&key_type_logon, description, NULL);
85 kfree(description);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070086 if (IS_ERR(keyring_key))
87 return PTR_ERR(keyring_key);
Eric Biggers1b53cf92017-02-21 15:07:11 -080088 down_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070089
90 if (keyring_key->type != &key_type_logon) {
91 printk_once(KERN_WARNING
92 "%s: key type must be logon\n", __func__);
93 res = -ENOKEY;
94 goto out;
95 }
David Howells0837e492017-03-01 15:11:23 +000096 ukp = user_key_payload_locked(keyring_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070097 if (ukp->datalen != sizeof(struct fscrypt_key)) {
98 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070099 goto out;
100 }
101 master_key = (struct fscrypt_key *)ukp->data;
102 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
103
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200104 if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
105 || master_key->size % AES_BLOCK_SIZE != 0) {
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700106 printk_once(KERN_WARNING
107 "%s: key size incorrect: %d\n",
108 __func__, master_key->size);
109 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700110 goto out;
111 }
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200112 res = derive_key_aes(ctx->nonce, master_key, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700113out:
Eric Biggers1b53cf92017-02-21 15:07:11 -0800114 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700115 key_put(keyring_key);
116 return res;
117}
118
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200119static const struct {
120 const char *cipher_str;
121 int keysize;
122} available_modes[] = {
123 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
124 FS_AES_256_XTS_KEY_SIZE },
125 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
126 FS_AES_256_CTS_KEY_SIZE },
127 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
128 FS_AES_128_CBC_KEY_SIZE },
129 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
130 FS_AES_128_CTS_KEY_SIZE },
131};
132
Eric Biggers8f398502016-09-15 13:32:11 -0400133static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
134 const char **cipher_str_ret, int *keysize_ret)
135{
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200136 u32 mode;
137
138 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
139 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
140 inode->i_ino,
141 ci->ci_data_mode, ci->ci_filename_mode);
142 return -EINVAL;
143 }
144
Eric Biggers8f398502016-09-15 13:32:11 -0400145 if (S_ISREG(inode->i_mode)) {
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200146 mode = ci->ci_data_mode;
147 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
148 mode = ci->ci_filename_mode;
149 } else {
150 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
151 inode->i_ino, (inode->i_mode & S_IFMT));
152 return -EINVAL;
Eric Biggers8f398502016-09-15 13:32:11 -0400153 }
154
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200155 *cipher_str_ret = available_modes[mode].cipher_str;
156 *keysize_ret = available_modes[mode].keysize;
157 return 0;
Eric Biggers8f398502016-09-15 13:32:11 -0400158}
159
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700160static void put_crypt_info(struct fscrypt_info *ci)
161{
162 if (!ci)
163 return;
164
Linus Torvaldsd4075742016-03-21 11:03:02 -0700165 crypto_free_skcipher(ci->ci_ctfm);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200166 crypto_free_cipher(ci->ci_essiv_tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700167 kmem_cache_free(fscrypt_info_cachep, ci);
168}
169
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200170static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
171{
172 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
173
174 /* init hash transform on demand */
175 if (unlikely(!tfm)) {
176 struct crypto_shash *prev_tfm;
177
178 tfm = crypto_alloc_shash("sha256", 0, 0);
179 if (IS_ERR(tfm)) {
180 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
181 PTR_ERR(tfm));
182 return PTR_ERR(tfm);
183 }
184 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
185 if (prev_tfm) {
186 crypto_free_shash(tfm);
187 tfm = prev_tfm;
188 }
189 }
190
191 {
192 SHASH_DESC_ON_STACK(desc, tfm);
193 desc->tfm = tfm;
194 desc->flags = 0;
195
196 return crypto_shash_digest(desc, key, keysize, salt);
197 }
198}
199
200static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
201 int keysize)
202{
203 int err;
204 struct crypto_cipher *essiv_tfm;
205 u8 salt[SHA256_DIGEST_SIZE];
206
207 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
208 if (IS_ERR(essiv_tfm))
209 return PTR_ERR(essiv_tfm);
210
211 ci->ci_essiv_tfm = essiv_tfm;
212
213 err = derive_essiv_salt(raw_key, keysize, salt);
214 if (err)
215 goto out;
216
217 /*
218 * Using SHA256 to derive the salt/key will result in AES-256 being
219 * used for IV generation. File contents encryption will still use the
220 * configured keysize (AES-128) nevertheless.
221 */
222 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
223 if (err)
224 goto out;
225
226out:
227 memzero_explicit(salt, sizeof(salt));
228 return err;
229}
230
231void __exit fscrypt_essiv_cleanup(void)
232{
233 crypto_free_shash(essiv_hash_tfm);
234}
235
Eric Biggers1b53cf92017-02-21 15:07:11 -0800236int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700237{
238 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700239 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700240 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700241 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400242 int keysize;
Eric Biggersa6e08912016-11-13 20:41:09 -0500243 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700244 int res;
245
Eric Biggers1b53cf92017-02-21 15:07:11 -0800246 if (inode->i_crypt_info)
247 return 0;
248
David Gstirf32d7ac2016-12-06 23:53:57 +0100249 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700250 if (res)
251 return res;
252
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700253 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
254 if (res < 0) {
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500255 if (!fscrypt_dummy_context_enabled(inode) ||
256 inode->i_sb->s_cop->is_encrypted(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700257 return res;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500258 /* Fake up a context for an unencrypted directory */
259 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400260 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700261 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
262 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Theodore Ts'o5bbdcbb2017-01-02 15:12:17 -0500263 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700264 } else if (res != sizeof(ctx)) {
265 return -EINVAL;
266 }
Eric Biggers8f398502016-09-15 13:32:11 -0400267
268 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
269 return -EINVAL;
270
271 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
272 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700273
274 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
275 if (!crypt_info)
276 return -ENOMEM;
277
278 crypt_info->ci_flags = ctx.flags;
279 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
280 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
281 crypt_info->ci_ctfm = NULL;
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200282 crypt_info->ci_essiv_tfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700283 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
284 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700285
Eric Biggers8f398502016-09-15 13:32:11 -0400286 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
287 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700288 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400289
Eric Biggersa6e08912016-11-13 20:41:09 -0500290 /*
291 * This cannot be a stack buffer because it is passed to the scatterlist
292 * crypto API as part of key derivation.
293 */
294 res = -ENOMEM;
295 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
296 if (!raw_key)
297 goto out;
298
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200299 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
300 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700301 if (res && inode->i_sb->s_cop->key_prefix) {
Eric Biggersa5d431e2017-01-05 13:51:18 -0800302 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200303 inode->i_sb->s_cop->key_prefix,
304 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700305 if (res2) {
306 if (res2 == -ENOKEY)
307 res = -ENOKEY;
308 goto out;
309 }
310 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700311 goto out;
312 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700313 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700314 if (!ctfm || IS_ERR(ctfm)) {
315 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200316 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
317 __func__, res, inode->i_ino);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700318 goto out;
319 }
320 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700321 crypto_skcipher_clear_flags(ctfm, ~0);
322 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200323 /*
324 * if the provided key is longer than keysize, we use the first
325 * keysize bytes of the derived key only
326 */
Eric Biggers8f398502016-09-15 13:32:11 -0400327 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700328 if (res)
329 goto out;
330
Daniel Walterb7e7cf72017-06-19 09:27:58 +0200331 if (S_ISREG(inode->i_mode) &&
332 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
333 res = init_essiv_generator(crypt_info, raw_key, keysize);
334 if (res) {
335 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
336 __func__, res, inode->i_ino);
337 goto out;
338 }
339 }
Eric Biggers1b53cf92017-02-21 15:07:11 -0800340 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
341 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700342out:
343 if (res == -ENOKEY)
344 res = 0;
345 put_crypt_info(crypt_info);
Eric Biggersa6e08912016-11-13 20:41:09 -0500346 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700347 return res;
348}
Eric Biggers1b53cf92017-02-21 15:07:11 -0800349EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700350
351void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
352{
353 struct fscrypt_info *prev;
354
355 if (ci == NULL)
356 ci = ACCESS_ONCE(inode->i_crypt_info);
357 if (ci == NULL)
358 return;
359
360 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
361 if (prev != ci)
362 return;
363
364 put_crypt_info(ci);
365}
366EXPORT_SYMBOL(fscrypt_put_encryption_info);