1 // SPDX-License-Identifier: GPL-2.0
3 * Key setup facility for FS encryption support.
5 * Copyright (C) 2015, Google, Inc.
7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
11 #include <crypto/aes.h>
12 #include <crypto/sha.h>
13 #include <crypto/skcipher.h>
14 #include <linux/key.h>
16 #include "fscrypt_private.h"
18 static struct crypto_shash *essiv_hash_tfm;
20 static struct fscrypt_mode available_modes[] = {
21 [FSCRYPT_MODE_AES_256_XTS] = {
22 .friendly_name = "AES-256-XTS",
23 .cipher_str = "xts(aes)",
27 [FSCRYPT_MODE_AES_256_CTS] = {
28 .friendly_name = "AES-256-CTS-CBC",
29 .cipher_str = "cts(cbc(aes))",
33 [FSCRYPT_MODE_AES_128_CBC] = {
34 .friendly_name = "AES-128-CBC",
35 .cipher_str = "cbc(aes)",
40 [FSCRYPT_MODE_AES_128_CTS] = {
41 .friendly_name = "AES-128-CTS-CBC",
42 .cipher_str = "cts(cbc(aes))",
46 [FSCRYPT_MODE_ADIANTUM] = {
47 .friendly_name = "Adiantum",
48 .cipher_str = "adiantum(xchacha12,aes)",
54 static struct fscrypt_mode *
55 select_encryption_mode(const struct fscrypt_info *ci, const struct inode *inode)
57 if (!fscrypt_valid_enc_modes(ci->ci_data_mode, ci->ci_filename_mode)) {
59 "Unsupported encryption modes (contents mode %d, filenames mode %d)",
60 ci->ci_data_mode, ci->ci_filename_mode);
61 return ERR_PTR(-EINVAL);
64 if (S_ISREG(inode->i_mode))
65 return &available_modes[ci->ci_data_mode];
67 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
68 return &available_modes[ci->ci_filename_mode];
70 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
71 inode->i_ino, (inode->i_mode & S_IFMT));
72 return ERR_PTR(-EINVAL);
75 /* Create a symmetric cipher object for the given encryption mode and key */
76 struct crypto_skcipher *fscrypt_allocate_skcipher(struct fscrypt_mode *mode,
78 const struct inode *inode)
80 struct crypto_skcipher *tfm;
83 tfm = crypto_alloc_skcipher(mode->cipher_str, 0, 0);
85 if (PTR_ERR(tfm) == -ENOENT) {
87 "Missing crypto API support for %s (API name: \"%s\")",
88 mode->friendly_name, mode->cipher_str);
89 return ERR_PTR(-ENOPKG);
91 fscrypt_err(inode, "Error allocating '%s' transform: %ld",
92 mode->cipher_str, PTR_ERR(tfm));
95 if (unlikely(!mode->logged_impl_name)) {
97 * fscrypt performance can vary greatly depending on which
98 * crypto algorithm implementation is used. Help people debug
99 * performance problems by logging the ->cra_driver_name the
100 * first time a mode is used. Note that multiple threads can
101 * race here, but it doesn't really matter.
103 mode->logged_impl_name = true;
104 pr_info("fscrypt: %s using implementation \"%s\"\n",
106 crypto_skcipher_alg(tfm)->base.cra_driver_name);
108 crypto_skcipher_set_flags(tfm, CRYPTO_TFM_REQ_FORBID_WEAK_KEYS);
109 err = crypto_skcipher_setkey(tfm, raw_key, mode->keysize);
116 crypto_free_skcipher(tfm);
120 static int derive_essiv_salt(const u8 *key, int keysize, u8 *salt)
122 struct crypto_shash *tfm = READ_ONCE(essiv_hash_tfm);
124 /* init hash transform on demand */
125 if (unlikely(!tfm)) {
126 struct crypto_shash *prev_tfm;
128 tfm = crypto_alloc_shash("sha256", 0, 0);
130 if (PTR_ERR(tfm) == -ENOENT) {
132 "Missing crypto API support for SHA-256");
136 "Error allocating SHA-256 transform: %ld",
140 prev_tfm = cmpxchg(&essiv_hash_tfm, NULL, tfm);
142 crypto_free_shash(tfm);
148 SHASH_DESC_ON_STACK(desc, tfm);
151 return crypto_shash_digest(desc, key, keysize, salt);
155 static int init_essiv_generator(struct fscrypt_info *ci, const u8 *raw_key,
159 struct crypto_cipher *essiv_tfm;
160 u8 salt[SHA256_DIGEST_SIZE];
162 if (WARN_ON(ci->ci_mode->ivsize != AES_BLOCK_SIZE))
165 essiv_tfm = crypto_alloc_cipher("aes", 0, 0);
166 if (IS_ERR(essiv_tfm))
167 return PTR_ERR(essiv_tfm);
169 ci->ci_essiv_tfm = essiv_tfm;
171 err = derive_essiv_salt(raw_key, keysize, salt);
176 * Using SHA256 to derive the salt/key will result in AES-256 being
177 * used for IV generation. File contents encryption will still use the
178 * configured keysize (AES-128) nevertheless.
180 err = crypto_cipher_setkey(essiv_tfm, salt, sizeof(salt));
185 memzero_explicit(salt, sizeof(salt));
189 /* Given the per-file key, set up the file's crypto transform object(s) */
190 int fscrypt_set_derived_key(struct fscrypt_info *ci, const u8 *derived_key)
192 struct fscrypt_mode *mode = ci->ci_mode;
193 struct crypto_skcipher *ctfm;
196 ctfm = fscrypt_allocate_skcipher(mode, derived_key, ci->ci_inode);
198 return PTR_ERR(ctfm);
202 if (mode->needs_essiv) {
203 err = init_essiv_generator(ci, derived_key, mode->keysize);
205 fscrypt_warn(ci->ci_inode,
206 "Error initializing ESSIV generator: %d",
215 * Find the master key, then set up the inode's actual encryption key.
217 static int setup_file_encryption_key(struct fscrypt_info *ci)
219 return fscrypt_setup_v1_file_key_via_subscribed_keyrings(ci);
222 static void put_crypt_info(struct fscrypt_info *ci)
227 if (ci->ci_direct_key) {
228 fscrypt_put_direct_key(ci->ci_direct_key);
230 crypto_free_skcipher(ci->ci_ctfm);
231 crypto_free_cipher(ci->ci_essiv_tfm);
233 kmem_cache_free(fscrypt_info_cachep, ci);
236 int fscrypt_get_encryption_info(struct inode *inode)
238 struct fscrypt_info *crypt_info;
239 struct fscrypt_context ctx;
240 struct fscrypt_mode *mode;
243 if (fscrypt_has_encryption_key(inode))
246 res = fscrypt_initialize(inode->i_sb->s_cop->flags);
250 res = inode->i_sb->s_cop->get_context(inode, &ctx, sizeof(ctx));
252 if (!fscrypt_dummy_context_enabled(inode) ||
253 IS_ENCRYPTED(inode)) {
255 "Error %d getting encryption context",
259 /* Fake up a context for an unencrypted directory */
260 memset(&ctx, 0, sizeof(ctx));
261 ctx.format = FS_ENCRYPTION_CONTEXT_FORMAT_V1;
262 ctx.contents_encryption_mode = FSCRYPT_MODE_AES_256_XTS;
263 ctx.filenames_encryption_mode = FSCRYPT_MODE_AES_256_CTS;
264 memset(ctx.master_key_descriptor, 0x42,
265 FSCRYPT_KEY_DESCRIPTOR_SIZE);
266 } else if (res != sizeof(ctx)) {
268 "Unknown encryption context size (%d bytes)", res);
272 if (ctx.format != FS_ENCRYPTION_CONTEXT_FORMAT_V1) {
273 fscrypt_warn(inode, "Unknown encryption context version (%d)",
278 if (ctx.flags & ~FSCRYPT_POLICY_FLAGS_VALID) {
279 fscrypt_warn(inode, "Unknown encryption context flags (0x%02x)",
284 crypt_info = kmem_cache_zalloc(fscrypt_info_cachep, GFP_NOFS);
288 crypt_info->ci_inode = inode;
290 crypt_info->ci_flags = ctx.flags;
291 crypt_info->ci_data_mode = ctx.contents_encryption_mode;
292 crypt_info->ci_filename_mode = ctx.filenames_encryption_mode;
293 memcpy(crypt_info->ci_master_key_descriptor, ctx.master_key_descriptor,
294 FSCRYPT_KEY_DESCRIPTOR_SIZE);
295 memcpy(crypt_info->ci_nonce, ctx.nonce, FS_KEY_DERIVATION_NONCE_SIZE);
297 mode = select_encryption_mode(crypt_info, inode);
302 WARN_ON(mode->ivsize > FSCRYPT_MAX_IV_SIZE);
303 crypt_info->ci_mode = mode;
305 res = setup_file_encryption_key(crypt_info);
309 if (cmpxchg_release(&inode->i_crypt_info, NULL, crypt_info) == NULL)
314 put_crypt_info(crypt_info);
317 EXPORT_SYMBOL(fscrypt_get_encryption_info);
320 * fscrypt_put_encryption_info - free most of an inode's fscrypt data
322 * Free the inode's fscrypt_info. Filesystems must call this when the inode is
323 * being evicted. An RCU grace period need not have elapsed yet.
325 void fscrypt_put_encryption_info(struct inode *inode)
327 put_crypt_info(inode->i_crypt_info);
328 inode->i_crypt_info = NULL;
330 EXPORT_SYMBOL(fscrypt_put_encryption_info);
333 * fscrypt_free_inode - free an inode's fscrypt data requiring RCU delay
335 * Free the inode's cached decrypted symlink target, if any. Filesystems must
336 * call this after an RCU grace period, just before they free the inode.
338 void fscrypt_free_inode(struct inode *inode)
340 if (IS_ENCRYPTED(inode) && S_ISLNK(inode->i_mode)) {
341 kfree(inode->i_link);
342 inode->i_link = NULL;
345 EXPORT_SYMBOL(fscrypt_free_inode);