2 * key management facility for FS encryption support.
4 * Copyright (C) 2015, Google, Inc.
6 * This contains encryption key functions.
8 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
11 #include <keys/encrypted-type.h>
12 #include <keys/user-type.h>
13 #include <linux/random.h>
14 #include <linux/scatterlist.h>
15 #include <uapi/linux/keyctl.h>
16 #include <crypto/hash.h>
17 #include <linux/fscrypto.h>
19 static void derive_crypt_complete(struct crypto_async_request *req, int rc)
21 struct fscrypt_completion_result *ecr = req->data;
23 if (rc == -EINPROGRESS)
27 complete(&ecr->completion);
31 * derive_key_aes() - Derive a key using AES-128-ECB
32 * @deriving_key: Encryption key used for derivation.
33 * @source_key: Source key to which to apply derivation.
34 * @derived_key: Derived key.
36 * Return: Zero on success; non-zero otherwise.
38 static int derive_key_aes(u8 deriving_key[FS_AES_128_ECB_KEY_SIZE],
39 u8 source_key[FS_AES_256_XTS_KEY_SIZE],
40 u8 derived_key[FS_AES_256_XTS_KEY_SIZE])
43 struct ablkcipher_request *req = NULL;
44 DECLARE_FS_COMPLETION_RESULT(ecr);
45 struct scatterlist src_sg, dst_sg;
46 struct crypto_ablkcipher *tfm = crypto_alloc_ablkcipher("ecb(aes)", 0,
54 crypto_ablkcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
55 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
60 ablkcipher_request_set_callback(req,
61 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
62 derive_crypt_complete, &ecr);
63 res = crypto_ablkcipher_setkey(tfm, deriving_key,
64 FS_AES_128_ECB_KEY_SIZE);
68 sg_init_one(&src_sg, source_key, FS_AES_256_XTS_KEY_SIZE);
69 sg_init_one(&dst_sg, derived_key, FS_AES_256_XTS_KEY_SIZE);
70 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg,
71 FS_AES_256_XTS_KEY_SIZE, NULL);
72 res = crypto_ablkcipher_encrypt(req);
73 if (res == -EINPROGRESS || res == -EBUSY) {
74 wait_for_completion(&ecr.completion);
79 ablkcipher_request_free(req);
81 crypto_free_ablkcipher(tfm);
85 static void put_crypt_info(struct fscrypt_info *ci)
90 if (ci->ci_keyring_key)
91 key_put(ci->ci_keyring_key);
92 crypto_free_ablkcipher(ci->ci_ctfm);
93 kmem_cache_free(fscrypt_info_cachep, ci);
96 int get_crypt_info(struct inode *inode)
98 struct fscrypt_info *crypt_info;
99 u8 full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
100 (FS_KEY_DESCRIPTOR_SIZE * 2) + 1];
101 struct key *keyring_key = NULL;
102 struct fscrypt_key *master_key;
103 struct fscrypt_context ctx;
104 const struct user_key_payload *ukp;
105 struct crypto_ablkcipher *ctfm;
106 const char *cipher_str;
107 u8 raw_key[FS_MAX_KEY_SIZE];
111 res = fscrypt_initialize();
115 if (!inode->i_sb->s_cop->get_context)
118 crypt_info = ACCESS_ONCE(inode->i_crypt_info);
120 if (!crypt_info->ci_keyring_key ||
121 key_validate(crypt_info->ci_keyring_key) == 0)
123 fscrypt_put_encryption_info(inode, crypt_info);
127 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
129 if (!fscrypt_dummy_context_enabled(inode))
131 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
132 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
134 } else if (res != sizeof(ctx)) {
139 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
143 crypt_info->ci_flags = ctx.flags;
144 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
145 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
146 crypt_info->ci_ctfm = NULL;
147 crypt_info->ci_keyring_key = NULL;
148 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
149 sizeof(crypt_info->ci_master_key));
150 if (S_ISREG(inode->i_mode))
151 mode = crypt_info->ci_data_mode;
152 else if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
153 mode = crypt_info->ci_filename_mode;
158 case FS_ENCRYPTION_MODE_AES_256_XTS:
159 cipher_str = "xts(aes)";
161 case FS_ENCRYPTION_MODE_AES_256_CTS:
162 cipher_str = "cts(cbc(aes))";
165 printk_once(KERN_WARNING
166 "%s: unsupported key mode %d (ino %u)\n",
167 __func__, mode, (unsigned) inode->i_ino);
171 if (fscrypt_dummy_context_enabled(inode)) {
172 memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
175 memcpy(full_key_descriptor, FS_KEY_DESC_PREFIX,
176 FS_KEY_DESC_PREFIX_SIZE);
177 sprintf(full_key_descriptor + FS_KEY_DESC_PREFIX_SIZE,
178 "%*phN", FS_KEY_DESCRIPTOR_SIZE,
179 ctx.master_key_descriptor);
180 full_key_descriptor[FS_KEY_DESC_PREFIX_SIZE +
181 (2 * FS_KEY_DESCRIPTOR_SIZE)] = '\0';
182 keyring_key = request_key(&key_type_logon, full_key_descriptor, NULL);
183 if (IS_ERR(keyring_key)) {
184 res = PTR_ERR(keyring_key);
188 crypt_info->ci_keyring_key = keyring_key;
189 if (keyring_key->type != &key_type_logon) {
190 printk_once(KERN_WARNING
191 "%s: key type must be logon\n", __func__);
195 down_read(&keyring_key->sem);
196 ukp = user_key_payload(keyring_key);
197 if (ukp->datalen != sizeof(struct fscrypt_key)) {
199 up_read(&keyring_key->sem);
202 master_key = (struct fscrypt_key *)ukp->data;
203 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
205 if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
206 printk_once(KERN_WARNING
207 "%s: key size incorrect: %d\n",
208 __func__, master_key->size);
210 up_read(&keyring_key->sem);
213 res = derive_key_aes(ctx.nonce, master_key->raw, raw_key);
214 up_read(&keyring_key->sem);
218 ctfm = crypto_alloc_ablkcipher(cipher_str, 0, 0);
219 if (!ctfm || IS_ERR(ctfm)) {
220 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
222 "%s: error %d (inode %u) allocating crypto tfm\n",
223 __func__, res, (unsigned) inode->i_ino);
226 crypt_info->ci_ctfm = ctfm;
227 crypto_ablkcipher_clear_flags(ctfm, ~0);
228 crypto_tfm_set_flags(crypto_ablkcipher_tfm(ctfm),
229 CRYPTO_TFM_REQ_WEAK_KEY);
230 res = crypto_ablkcipher_setkey(ctfm, raw_key, fscrypt_key_size(mode));
234 memzero_explicit(raw_key, sizeof(raw_key));
235 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) != NULL) {
236 put_crypt_info(crypt_info);
244 put_crypt_info(crypt_info);
245 memzero_explicit(raw_key, sizeof(raw_key));
249 void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
251 struct fscrypt_info *prev;
254 ci = ACCESS_ONCE(inode->i_crypt_info);
258 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
264 EXPORT_SYMBOL(fscrypt_put_encryption_info);
266 int fscrypt_get_encryption_info(struct inode *inode)
268 struct fscrypt_info *ci = inode->i_crypt_info;
271 (ci->ci_keyring_key &&
272 (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
273 (1 << KEY_FLAG_REVOKED) |
274 (1 << KEY_FLAG_DEAD)))))
275 return get_crypt_info(inode);
278 EXPORT_SYMBOL(fscrypt_get_encryption_info);