blob: c891d2ea9d2484d242c87a5d775bd1c6467c5276 [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 Walter8e989de2017-06-19 09:27:58 +020013#include <linux/ratelimit.h>
14#include <crypto/aes.h>
15#include <crypto/sha.h>
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070016#include "fscrypt_private.h"
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070017
Daniel Walter8e989de2017-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 Walter8e989de2017-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 Walter8e989de2017-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-Yossef743205f2017-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-Yossef743205f2017-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 Walter8e989de2017-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-Yossef743205f2017-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 Walter8e989de2017-06-19 09:27:58 +020070 const char *prefix, int min_keysize)
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070071{
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070072 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
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070078 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
Jaegeuk Kime6b120d2017-07-10 12:55:09 -070084 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 Biggers2984e522017-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 Howells4d7931d2017-03-01 15:11:23 +000096 ukp = user_key_payload_locked(keyring_key);
Eric Biggersa0471ef2017-10-09 12:46:18 -070097 if (!ukp) {
98 /* key was revoked before we acquired its semaphore */
99 res = -EKEYREVOKED;
100 goto out;
101 }
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700102 if (ukp->datalen != sizeof(struct fscrypt_key)) {
103 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700104 goto out;
105 }
106 master_key = (struct fscrypt_key *)ukp->data;
107 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
108
Daniel Walter8e989de2017-06-19 09:27:58 +0200109 if (master_key->size < min_keysize || master_key->size > FS_MAX_KEY_SIZE
110 || master_key->size % AES_BLOCK_SIZE != 0) {
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700111 printk_once(KERN_WARNING
112 "%s: key size incorrect: %d\n",
113 __func__, master_key->size);
114 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700115 goto out;
116 }
Daniel Walter8e989de2017-06-19 09:27:58 +0200117 res = derive_key_aes(ctx->nonce, master_key, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700118out:
Eric Biggers2984e522017-02-21 15:07:11 -0800119 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700120 key_put(keyring_key);
121 return res;
122}
123
Daniel Walter8e989de2017-06-19 09:27:58 +0200124static const struct {
125 const char *cipher_str;
126 int keysize;
127} available_modes[] = {
128 [FS_ENCRYPTION_MODE_AES_256_XTS] = { "xts(aes)",
129 FS_AES_256_XTS_KEY_SIZE },
130 [FS_ENCRYPTION_MODE_AES_256_CTS] = { "cts(cbc(aes))",
131 FS_AES_256_CTS_KEY_SIZE },
132 [FS_ENCRYPTION_MODE_AES_128_CBC] = { "cbc(aes)",
133 FS_AES_128_CBC_KEY_SIZE },
134 [FS_ENCRYPTION_MODE_AES_128_CTS] = { "cts(cbc(aes))",
135 FS_AES_128_CTS_KEY_SIZE },
136};
137
Eric Biggers8f398502016-09-15 13:32:11 -0400138static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
139 const char **cipher_str_ret, int *keysize_ret)
140{
Daniel Walter8e989de2017-06-19 09:27:58 +0200141 u32 mode;
142
143 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
144 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
145 inode->i_ino,
146 ci->ci_data_mode, ci->ci_filename_mode);
147 return -EINVAL;
148 }
149
Eric Biggers8f398502016-09-15 13:32:11 -0400150 if (S_ISREG(inode->i_mode)) {
Daniel Walter8e989de2017-06-19 09:27:58 +0200151 mode = ci->ci_data_mode;
152 } else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
153 mode = ci->ci_filename_mode;
154 } else {
155 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
156 inode->i_ino, (inode->i_mode & S_IFMT));
157 return -EINVAL;
Eric Biggers8f398502016-09-15 13:32:11 -0400158 }
159
Daniel Walter8e989de2017-06-19 09:27:58 +0200160 *cipher_str_ret = available_modes[mode].cipher_str;
161 *keysize_ret = available_modes[mode].keysize;
162 return 0;
Eric Biggers8f398502016-09-15 13:32:11 -0400163}
164
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700165static void put_crypt_info(struct fscrypt_info *ci)
166{
167 if (!ci)
168 return;
169
Linus Torvaldsd4075742016-03-21 11:03:02 -0700170 crypto_free_skcipher(ci->ci_ctfm);
Daniel Walter8e989de2017-06-19 09:27:58 +0200171 crypto_free_cipher(ci->ci_essiv_tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700172 kmem_cache_free(fscrypt_info_cachep, ci);
173}
174
Daniel Walter8e989de2017-06-19 09:27:58 +0200175static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
176{
177 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
178
179 /* init hash transform on demand */
180 if (unlikely(!tfm)) {
181 struct crypto_shash *prev_tfm;
182
183 tfm = crypto_alloc_shash("sha256", 0, 0);
184 if (IS_ERR(tfm)) {
185 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
186 PTR_ERR(tfm));
187 return PTR_ERR(tfm);
188 }
189 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
190 if (prev_tfm) {
191 crypto_free_shash(tfm);
192 tfm = prev_tfm;
193 }
194 }
195
196 {
197 SHASH_DESC_ON_STACK(desc, tfm);
198 desc->tfm = tfm;
199 desc->flags = 0;
200
201 return crypto_shash_digest(desc, key, keysize, salt);
202 }
203}
204
205static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
206 int keysize)
207{
208 int err;
209 struct crypto_cipher *essiv_tfm;
210 u8 salt[SHA256_DIGEST_SIZE];
211
212 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
213 if (IS_ERR(essiv_tfm))
214 return PTR_ERR(essiv_tfm);
215
216 ci->ci_essiv_tfm = essiv_tfm;
217
218 err = derive_essiv_salt(raw_key, keysize, salt);
219 if (err)
220 goto out;
221
222 /*
223 * Using SHA256 to derive the salt/key will result in AES-256 being
224 * used for IV generation. File contents encryption will still use the
225 * configured keysize (AES-128) nevertheless.
226 */
227 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
228 if (err)
229 goto out;
230
231out:
232 memzero_explicit(salt, sizeof(salt));
233 return err;
234}
235
236void __exit fscrypt_essiv_cleanup(void)
237{
238 crypto_free_shash(essiv_hash_tfm);
239}
240
Eric Biggers2984e522017-02-21 15:07:11 -0800241int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700242{
243 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700244 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700245 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700246 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400247 int keysize;
Eric Biggers0f0909e2016-11-13 20:41:09 -0500248 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700249 int res;
250
Eric Biggers2984e522017-02-21 15:07:11 -0800251 if (inode->i_crypt_info)
252 return 0;
253
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700254 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700255 if (res)
256 return res;
257
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700258 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
259 if (res < 0) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700260 if (!fscrypt_dummy_context_enabled(inode) ||
Eric Biggersd750ec72017-10-09 12:15:36 -0700261 IS_ENCRYPTED(inode))
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700262 return res;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700263 /* Fake up a context for an unencrypted directory */
264 memset(&ctx, 0, sizeof(ctx));
Eric Biggers8f398502016-09-15 13:32:11 -0400265 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700266 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
267 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700268 memset(ctx.master_key_descriptor, 0x42, FS_KEY_DESCRIPTOR_SIZE);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700269 } else if (res != sizeof(ctx)) {
270 return -EINVAL;
271 }
Eric Biggers8f398502016-09-15 13:32:11 -0400272
273 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
274 return -EINVAL;
275
276 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
277 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700278
279 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
280 if (!crypt_info)
281 return -ENOMEM;
282
283 crypt_info->ci_flags = ctx.flags;
284 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
285 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
286 crypt_info->ci_ctfm = NULL;
Daniel Walter8e989de2017-06-19 09:27:58 +0200287 crypt_info->ci_essiv_tfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700288 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
289 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700290
Eric Biggers8f398502016-09-15 13:32:11 -0400291 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
292 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700293 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400294
Eric Biggers0f0909e2016-11-13 20:41:09 -0500295 /*
296 * This cannot be a stack buffer because it is passed to the scatterlist
297 * crypto API as part of key derivation.
298 */
299 res = -ENOMEM;
300 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
301 if (!raw_key)
302 goto out;
303
Daniel Walter8e989de2017-06-19 09:27:58 +0200304 res = validate_user_key(crypt_info, &ctx, raw_key, FS_KEY_DESC_PREFIX,
305 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700306 if (res && inode->i_sb->s_cop->key_prefix) {
Jaegeuk Kime6b120d2017-07-10 12:55:09 -0700307 int res2 = validate_user_key(crypt_info, &ctx, raw_key,
Daniel Walter8e989de2017-06-19 09:27:58 +0200308 inode->i_sb->s_cop->key_prefix,
309 keysize);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700310 if (res2) {
311 if (res2 == -ENOKEY)
312 res = -ENOKEY;
313 goto out;
314 }
315 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700316 goto out;
317 }
Linus Torvaldsd4075742016-03-21 11:03:02 -0700318 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700319 if (!ctfm || IS_ERR(ctfm)) {
320 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
Daniel Walter8e989de2017-06-19 09:27:58 +0200321 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
322 __func__, res, inode->i_ino);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700323 goto out;
324 }
325 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700326 crypto_skcipher_clear_flags(ctfm, ~0);
327 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Daniel Walter8e989de2017-06-19 09:27:58 +0200328 /*
329 * if the provided key is longer than keysize, we use the first
330 * keysize bytes of the derived key only
331 */
Eric Biggers8f398502016-09-15 13:32:11 -0400332 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700333 if (res)
334 goto out;
335
Daniel Walter8e989de2017-06-19 09:27:58 +0200336 if (S_ISREG(inode->i_mode) &&
337 crypt_info->ci_data_mode == FS_ENCRYPTION_MODE_AES_128_CBC) {
338 res = init_essiv_generator(crypt_info, raw_key, keysize);
339 if (res) {
340 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
341 __func__, res, inode->i_ino);
342 goto out;
343 }
344 }
Eric Biggers2984e522017-02-21 15:07:11 -0800345 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
346 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700347out:
348 if (res == -ENOKEY)
349 res = 0;
350 put_crypt_info(crypt_info);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500351 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700352 return res;
353}
Eric Biggers2984e522017-02-21 15:07:11 -0800354EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700355
356void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
357{
358 struct fscrypt_info *prev;
359
360 if (ci == NULL)
361 ci = ACCESS_ONCE(inode->i_crypt_info);
362 if (ci == NULL)
363 return;
364
365 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
366 if (prev != ci)
367 return;
368
369 put_crypt_info(ci);
370}
371EXPORT_SYMBOL(fscrypt_put_encryption_info);