blob: bb4606368eb12ce559e62aa96776668a22105dd8 [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>
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070013#include <linux/fscrypto.h>
14
15static void derive_crypt_complete(struct crypto_async_request *req, int rc)
16{
17 struct fscrypt_completion_result *ecr = req->data;
18
19 if (rc == -EINPROGRESS)
20 return;
21
22 ecr->res = rc;
23 complete(&ecr->completion);
24}
25
26/**
27 * derive_key_aes() - Derive a key using AES-128-ECB
28 * @deriving_key: Encryption key used for derivation.
29 * @source_key: Source key to which to apply derivation.
30 * @derived_key: Derived key.
31 *
32 * Return: Zero on success; non-zero otherwise.
33 */
34static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
35 u8 source_key[FS_AES_256_XTS_KEY_SIZE],
36 u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
37{
38 int res = 0;
Linus Torvaldsd4075742016-03-21 11:03:02 -070039 struct skcipher_request *req = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070040 DECLARE_FS_COMPLETION_RESULT(ecr);
41 struct scatterlist src_sg, dst_sg;
Linus Torvaldsd4075742016-03-21 11:03:02 -070042 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070043
44 if (IS_ERR(tfm)) {
45 res = PTR_ERR(tfm);
46 tfm = NULL;
47 goto out;
48 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070049 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
50 req = skcipher_request_alloc(tfm, GFP_NOFS);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070051 if (!req) {
52 res = -ENOMEM;
53 goto out;
54 }
Linus Torvaldsd4075742016-03-21 11:03:02 -070055 skcipher_request_set_callback(req,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070056 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
57 derive_crypt_complete, &ecr);
Linus Torvaldsd4075742016-03-21 11:03:02 -070058 res = crypto_skcipher_setkey(tfm, deriving_key,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070059 FS_AES_128_ECB_KEY_SIZE);
60 if (res < 0)
61 goto out;
62
63 sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
64 sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
Linus Torvaldsd4075742016-03-21 11:03:02 -070065 skcipher_request_set_crypt(req, &src_sg, &dst_sg,
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070066 FS_AES_256_XTS_KEY_SIZE, NULL);
Linus Torvaldsd4075742016-03-21 11:03:02 -070067 res = crypto_skcipher_encrypt(req);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070068 if (res == -EINPROGRESS || res == -EBUSY) {
69 wait_for_completion(&ecr.completion);
70 res = ecr.res;
71 }
72out:
Linus Torvaldsd4075742016-03-21 11:03:02 -070073 skcipher_request_free(req);
74 crypto_free_skcipher(tfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -070075 return res;
76}
77
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -070078static int validate_user_key(struct fscrypt_info *crypt_info,
79 struct fscrypt_context *ctx, u8 *raw_key,
80 u8 *prefix, int prefix_size)
81{
82 u8 *full_key_descriptor;
83 struct key *keyring_key;
84 struct fscrypt_key *master_key;
85 const struct user_key_payload *ukp;
86 int full_key_len = prefix_size + (FS_KEY_DESCRIPTOR_SIZE * 2) + 1;
87 int res;
88
89 full_key_descriptor = kmalloc(full_key_len, GFP_NOFS);
90 if (!full_key_descriptor)
91 return -ENOMEM;
92
93 memcpy(full_key_descriptor, prefix, prefix_size);
94 sprintf(full_key_descriptor + prefix_size,
95 "%*phN", FS_KEY_DESCRIPTOR_SIZE,
96 ctx->master_key_descriptor);
97 full_key_descriptor[full_key_len - 1] = '\0';
98 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
99 kfree(full_key_descriptor);
100 if (IS_ERR(keyring_key))
101 return PTR_ERR(keyring_key);
Eric Biggers2984e522017-02-21 15:07:11 -0800102 down_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700103
104 if (keyring_key->type != &key_type_logon) {
105 printk_once(KERN_WARNING
106 "%s: key type must be logon\n", __func__);
107 res = -ENOKEY;
108 goto out;
109 }
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700110 ukp = user_key_payload(keyring_key);
111 if (ukp->datalen != sizeof(struct fscrypt_key)) {
112 res = -EINVAL;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700113 goto out;
114 }
115 master_key = (struct fscrypt_key *)ukp->data;
116 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
117
118 if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
119 printk_once(KERN_WARNING
120 "%s: key size incorrect: %d\n",
121 __func__, master_key->size);
122 res = -ENOKEY;
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700123 goto out;
124 }
125 res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700126out:
Eric Biggers2984e522017-02-21 15:07:11 -0800127 up_read(&keyring_key->sem);
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700128 key_put(keyring_key);
129 return res;
130}
131
Eric Biggers8f398502016-09-15 13:32:11 -0400132static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
133 const char **cipher_str_ret, int *keysize_ret)
134{
135 if (S_ISREG(inode->i_mode)) {
136 if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
137 *cipher_str_ret = "xts(aes)";
138 *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
139 return 0;
140 }
141 pr_warn_once("fscrypto: unsupported contents encryption mode "
142 "%d for inode %lu\n",
143 ci->ci_data_mode, inode->i_ino);
144 return -ENOKEY;
145 }
146
147 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
148 if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
149 *cipher_str_ret = "cts(cbc(aes))";
150 *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
151 return 0;
152 }
153 pr_warn_once("fscrypto: unsupported filenames encryption mode "
154 "%d for inode %lu\n",
155 ci->ci_filename_mode, inode->i_ino);
156 return -ENOKEY;
157 }
158
159 pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
160 (inode->i_mode & S_IFMT), inode->i_ino);
161 return -ENOKEY;
162}
163
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700164static void put_crypt_info(struct fscrypt_info *ci)
165{
166 if (!ci)
167 return;
168
Linus Torvaldsd4075742016-03-21 11:03:02 -0700169 crypto_free_skcipher(ci->ci_ctfm);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700170 kmem_cache_free(fscrypt_info_cachep, ci);
171}
172
Eric Biggers2984e522017-02-21 15:07:11 -0800173int fscrypt_get_encryption_info(struct inode *inode)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700174{
175 struct fscrypt_info *crypt_info;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700176 struct fscrypt_context ctx;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700177 struct crypto_skcipher *ctfm;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700178 const char *cipher_str;
Eric Biggers8f398502016-09-15 13:32:11 -0400179 int keysize;
Eric Biggers0f0909e2016-11-13 20:41:09 -0500180 u8 *raw_key = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700181 int res;
182
Eric Biggers2984e522017-02-21 15:07:11 -0800183 if (inode->i_crypt_info)
184 return 0;
185
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700186 res = fscrypt_initialize();
187 if (res)
188 return res;
189
190 if (!inode->i_sb->s_cop->get_context)
191 return -EOPNOTSUPP;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700192
193 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
194 if (res < 0) {
195 if (!fscrypt_dummy_context_enabled(inode))
196 return res;
Eric Biggers8f398502016-09-15 13:32:11 -0400197 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700198 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
199 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
200 ctx.flags = 0;
201 } else if (res != sizeof(ctx)) {
202 return -EINVAL;
203 }
Eric Biggers8f398502016-09-15 13:32:11 -0400204
205 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
206 return -EINVAL;
207
208 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
209 return -EINVAL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700210
211 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
212 if (!crypt_info)
213 return -ENOMEM;
214
215 crypt_info->ci_flags = ctx.flags;
216 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
217 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
218 crypt_info->ci_ctfm = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700219 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
220 sizeof(crypt_info->ci_master_key));
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700221
Eric Biggers8f398502016-09-15 13:32:11 -0400222 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
223 if (res)
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700224 goto out;
Eric Biggers8f398502016-09-15 13:32:11 -0400225
Eric Biggers0f0909e2016-11-13 20:41:09 -0500226 /*
227 * This cannot be a stack buffer because it is passed to the scatterlist
228 * crypto API as part of key derivation.
229 */
230 res = -ENOMEM;
231 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
232 if (!raw_key)
233 goto out;
234
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700235 if (fscrypt_dummy_context_enabled(inode)) {
236 memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
237 goto got_key;
238 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700239
Jaegeuk Kimb5a7aef2016-05-04 22:05:01 -0700240 res = validate_user_key(crypt_info, &ctx, raw_key,
241 FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
242 if (res && inode->i_sb->s_cop->key_prefix) {
243 u8 *prefix = NULL;
244 int prefix_size, res2;
245
246 prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
247 res2 = validate_user_key(crypt_info, &ctx, raw_key,
248 prefix, prefix_size);
249 if (res2) {
250 if (res2 == -ENOKEY)
251 res = -ENOKEY;
252 goto out;
253 }
254 } else if (res) {
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700255 goto out;
256 }
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700257got_key:
Linus Torvaldsd4075742016-03-21 11:03:02 -0700258 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700259 if (!ctfm || IS_ERR(ctfm)) {
260 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
261 printk(KERN_DEBUG
262 "%s: error %d (inode %u) allocating crypto tfm\n",
263 __func__, res, (unsigned) inode->i_ino);
264 goto out;
265 }
266 crypt_info->ci_ctfm = ctfm;
Linus Torvaldsd4075742016-03-21 11:03:02 -0700267 crypto_skcipher_clear_flags(ctfm, ~0);
268 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
Eric Biggers8f398502016-09-15 13:32:11 -0400269 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700270 if (res)
271 goto out;
272
Eric Biggers2984e522017-02-21 15:07:11 -0800273 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
274 crypt_info = NULL;
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700275out:
276 if (res == -ENOKEY)
277 res = 0;
278 put_crypt_info(crypt_info);
Eric Biggers0f0909e2016-11-13 20:41:09 -0500279 kzfree(raw_key);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700280 return res;
281}
Eric Biggers2984e522017-02-21 15:07:11 -0800282EXPORT_SYMBOL(fscrypt_get_encryption_info);
Jaegeuk Kim0b81d072015-05-15 16:26:10 -0700283
284void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
285{
286 struct fscrypt_info *prev;
287
288 if (ci == NULL)
289 ci = ACCESS_ONCE(inode->i_crypt_info);
290 if (ci == NULL)
291 return;
292
293 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
294 if (prev != ci)
295 return;
296
297 put_crypt_info(ci);
298}
299EXPORT_SYMBOL(fscrypt_put_encryption_info);