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/user-type.h>
12 #include <linux/scatterlist.h>
13 #include <linux/fscrypto.h>
15 static void derive_crypt_complete(struct crypto_async_request *req, int rc)
17 struct fscrypt_completion_result *ecr = req->data;
19 if (rc == -EINPROGRESS)
23 complete(&ecr->completion);
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.
32 * Return: Zero on success; non-zero otherwise.
34 static 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])
39 struct skcipher_request *req = NULL;
40 DECLARE_FS_COMPLETION_RESULT(ecr);
41 struct scatterlist src_sg, dst_sg;
42 struct crypto_skcipher *tfm = crypto_alloc_skcipher("ecb(aes)", 0, 0);
49 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_WEAK_KEY);
50 req = skcipher_request_alloc(tfm, GFP_NOFS);
55 skcipher_request_set_callback(req,
56 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
57 derive_crypt_complete, &ecr);
58 res = crypto_skcipher_setkey(tfm, deriving_key,
59 FS_AES_128_ECB_KEY_SIZE);
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);
65 skcipher_request_set_crypt(req, &src_sg, &dst_sg,
66 FS_AES_256_XTS_KEY_SIZE, NULL);
67 res = crypto_skcipher_encrypt(req);
68 if (res == -EINPROGRESS || res == -EBUSY) {
69 wait_for_completion(&ecr.completion);
73 skcipher_request_free(req);
74 crypto_free_skcipher(tfm);
78 static int validate_user_key(struct fscrypt_info *crypt_info,
79 struct fscrypt_context *ctx, u8 *raw_key,
80 u8 *prefix, int prefix_size)
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;
89 full_key_descriptor = kmalloc(full_key_len, GFP_NOFS);
90 if (!full_key_descriptor)
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);
102 down_read(&keyring_key->sem);
104 if (keyring_key->type != &key_type_logon) {
105 printk_once(KERN_WARNING
106 "%s: key type must be logon\n", __func__);
110 ukp = user_key_payload(keyring_key);
112 /* key was revoked before we acquired its semaphore */
116 if (ukp->datalen != sizeof(struct fscrypt_key)) {
120 master_key = (struct fscrypt_key *)ukp->data;
121 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE != FS_KEY_DERIVATION_NONCE_SIZE);
123 if (master_key->size != FS_AES_256_XTS_KEY_SIZE) {
124 printk_once(KERN_WARNING
125 "%s: key size incorrect: %d\n",
126 __func__, master_key->size);
130 res = derive_key_aes(ctx->nonce, master_key->raw, raw_key);
132 up_read(&keyring_key->sem);
133 key_put(keyring_key);
137 static int determine_cipher_type(struct fscrypt_info *ci, struct inode *inode,
138 const char **cipher_str_ret, int *keysize_ret)
140 if (S_ISREG(inode->i_mode)) {
141 if (ci->ci_data_mode == FS_ENCRYPTION_MODE_AES_256_XTS) {
142 *cipher_str_ret = "xts(aes)";
143 *keysize_ret = FS_AES_256_XTS_KEY_SIZE;
146 pr_warn_once("fscrypto: unsupported contents encryption mode "
147 "%d for inode %lu\n",
148 ci->ci_data_mode, inode->i_ino);
152 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) {
153 if (ci->ci_filename_mode == FS_ENCRYPTION_MODE_AES_256_CTS) {
154 *cipher_str_ret = "cts(cbc(aes))";
155 *keysize_ret = FS_AES_256_CTS_KEY_SIZE;
158 pr_warn_once("fscrypto: unsupported filenames encryption mode "
159 "%d for inode %lu\n",
160 ci->ci_filename_mode, inode->i_ino);
164 pr_warn_once("fscrypto: unsupported file type %d for inode %lu\n",
165 (inode->i_mode & S_IFMT), inode->i_ino);
169 static void put_crypt_info(struct fscrypt_info *ci)
174 crypto_free_skcipher(ci->ci_ctfm);
175 kmem_cache_free(fscrypt_info_cachep, ci);
178 int fscrypt_get_encryption_info(struct inode *inode)
180 struct fscrypt_info *crypt_info;
181 struct fscrypt_context ctx;
182 struct crypto_skcipher *ctfm;
183 const char *cipher_str;
188 if (inode->i_crypt_info)
191 res = fscrypt_initialize();
195 if (!inode->i_sb->s_cop->get_context)
198 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
200 if (!fscrypt_dummy_context_enabled(inode))
202 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
203 ctx.contents_encryption_mode = FS_ENCRYPTION_MODE_AES_256_XTS;
204 ctx.filenames_encryption_mode = FS_ENCRYPTION_MODE_AES_256_CTS;
206 } else if (res != sizeof(ctx)) {
210 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1)
213 if (ctx.flags & ~FS_POLICY_FLAGS_VALID)
216 crypt_info = kmem_cache_alloc(fscrypt_info_cachep, GFP_NOFS);
220 crypt_info->ci_flags = ctx.flags;
221 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
222 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
223 crypt_info->ci_ctfm = NULL;
224 memcpy(crypt_info->ci_master_key, ctx.master_key_descriptor,
225 sizeof(crypt_info->ci_master_key));
227 res = determine_cipher_type(crypt_info, inode, &cipher_str, &keysize);
232 * This cannot be a stack buffer because it is passed to the scatterlist
233 * crypto API as part of key derivation.
236 raw_key = kmalloc(FS_MAX_KEY_SIZE, GFP_NOFS);
240 if (fscrypt_dummy_context_enabled(inode)) {
241 memset(raw_key, 0x42, FS_AES_256_XTS_KEY_SIZE);
245 res = validate_user_key(crypt_info, &ctx, raw_key,
246 FS_KEY_DESC_PREFIX, FS_KEY_DESC_PREFIX_SIZE);
247 if (res && inode->i_sb->s_cop->key_prefix) {
249 int prefix_size, res2;
251 prefix_size = inode->i_sb->s_cop->key_prefix(inode, &prefix);
252 res2 = validate_user_key(crypt_info, &ctx, raw_key,
253 prefix, prefix_size);
263 ctfm = crypto_alloc_skcipher(cipher_str, 0, 0);
264 if (!ctfm || IS_ERR(ctfm)) {
265 res = ctfm ? PTR_ERR(ctfm) : -ENOMEM;
267 "%s: error %d (inode %u) allocating crypto tfm\n",
268 __func__, res, (unsigned) inode->i_ino);
271 crypt_info->ci_ctfm = ctfm;
272 crypto_skcipher_clear_flags(ctfm, ~0);
273 crypto_skcipher_set_flags(ctfm, CRYPTO_TFM_REQ_WEAK_KEY);
274 res = crypto_skcipher_setkey(ctfm, raw_key, keysize);
278 if (cmpxchg(&inode->i_crypt_info, NULL, crypt_info) == NULL)
283 put_crypt_info(crypt_info);
287 EXPORT_SYMBOL(fscrypt_get_encryption_info);
289 void fscrypt_put_encryption_info(struct inode *inode, struct fscrypt_info *ci)
291 struct fscrypt_info *prev;
294 ci = ACCESS_ONCE(inode->i_crypt_info);
298 prev = cmpxchg(&inode->i_crypt_info, ci, NULL);
304 EXPORT_SYMBOL(fscrypt_put_encryption_info);