1 // SPDX-License-Identifier: GPL-2.0
3 * key management facility for FS encryption support.
5 * Copyright (C) 2015, Google, Inc.
7 * This contains encryption key functions.
9 * Written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar, 2015.
12 #include <keys/user-type.h>
13 #include <linux/scatterlist.h>
14 #include <linux/ratelimit.h>
15 #include <crypto/aes.h>
16 #include <crypto/sha.h>
17 #include "fscrypt_private.h"
19 static struct crypto_shash
*essiv_hash_tfm
;
21 static void derive_crypt_complete(struct crypto_async_request
*req
, int rc
)
23 struct fscrypt_completion_result
*ecr
= req
->data
;
25 if (rc
== -EINPROGRESS
)
29 complete(&ecr
->completion
);
33 * derive_key_aes() - Derive a key using AES-128-ECB
34 * @deriving_key: Encryption key used for derivation.
35 * @source_key: Source key to which to apply derivation.
36 * @derived_raw_key: Derived raw key.
38 * Return: Zero on success; non-zero otherwise.
40 static int derive_key_aes(u8 deriving_key
[FS_AES_128_ECB_KEY_SIZE
],
41 const struct fscrypt_key
*source_key
,
42 u8 derived_raw_key
[FS_MAX_KEY_SIZE
])
45 struct skcipher_request
*req
= NULL
;
46 DECLARE_FS_COMPLETION_RESULT(ecr
);
47 struct scatterlist src_sg
, dst_sg
;
48 struct crypto_skcipher
*tfm
= crypto_alloc_skcipher("ecb(aes)", 0, 0);
55 crypto_skcipher_set_flags(tfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
56 req
= skcipher_request_alloc(tfm
, GFP_NOFS
);
61 skcipher_request_set_callback(req
,
62 CRYPTO_TFM_REQ_MAY_BACKLOG
| CRYPTO_TFM_REQ_MAY_SLEEP
,
63 derive_crypt_complete
, &ecr
);
64 res
= crypto_skcipher_setkey(tfm
, deriving_key
,
65 FS_AES_128_ECB_KEY_SIZE
);
69 sg_init_one(&src_sg
, source_key
->raw
, source_key
->size
);
70 sg_init_one(&dst_sg
, derived_raw_key
, source_key
->size
);
71 skcipher_request_set_crypt(req
, &src_sg
, &dst_sg
, source_key
->size
,
73 res
= crypto_skcipher_encrypt(req
);
74 if (res
== -EINPROGRESS
|| res
== -EBUSY
) {
75 wait_for_completion(&ecr
.completion
);
79 skcipher_request_free(req
);
80 crypto_free_skcipher(tfm
);
84 static int validate_user_key(struct fscrypt_info
*crypt_info
,
85 struct fscrypt_context
*ctx
, u8
*raw_key
,
86 const char *prefix
, int min_keysize
)
89 struct key
*keyring_key
;
90 struct fscrypt_key
*master_key
;
91 const struct user_key_payload
*ukp
;
94 description
= kasprintf(GFP_NOFS
, "%s%*phN", prefix
,
95 FS_KEY_DESCRIPTOR_SIZE
,
96 ctx
->master_key_descriptor
);
100 keyring_key
= request_key(&key_type_logon
, description
, NULL
);
102 if (IS_ERR(keyring_key
))
103 return PTR_ERR(keyring_key
);
104 down_read(&keyring_key
->sem
);
106 if (keyring_key
->type
!= &key_type_logon
) {
107 printk_once(KERN_WARNING
108 "%s: key type must be logon\n", __func__
);
112 ukp
= user_key_payload_locked(keyring_key
);
114 /* key was revoked before we acquired its semaphore */
118 if (ukp
->datalen
!= sizeof(struct fscrypt_key
)) {
122 master_key
= (struct fscrypt_key
*)ukp
->data
;
123 BUILD_BUG_ON(FS_AES_128_ECB_KEY_SIZE
!= FS_KEY_DERIVATION_NONCE_SIZE
);
125 if (master_key
->size
< min_keysize
|| master_key
->size
> FS_MAX_KEY_SIZE
126 || master_key
->size
% AES_BLOCK_SIZE
!= 0) {
127 printk_once(KERN_WARNING
128 "%s: key size incorrect: %d\n",
129 __func__
, master_key
->size
);
133 res
= derive_key_aes(ctx
->nonce
, master_key
, raw_key
);
135 up_read(&keyring_key
->sem
);
136 key_put(keyring_key
);
140 static const struct {
141 const char *cipher_str
;
143 } available_modes
[] = {
144 [FS_ENCRYPTION_MODE_AES_256_XTS
] = { "xts(aes)",
145 FS_AES_256_XTS_KEY_SIZE
},
146 [FS_ENCRYPTION_MODE_AES_256_CTS
] = { "cts(cbc(aes))",
147 FS_AES_256_CTS_KEY_SIZE
},
148 [FS_ENCRYPTION_MODE_AES_128_CBC
] = { "cbc(aes)",
149 FS_AES_128_CBC_KEY_SIZE
},
150 [FS_ENCRYPTION_MODE_AES_128_CTS
] = { "cts(cbc(aes))",
151 FS_AES_128_CTS_KEY_SIZE
},
154 static int determine_cipher_type(struct fscrypt_info
*ci
, struct inode
*inode
,
155 const char **cipher_str_ret
, int *keysize_ret
)
159 if (!fscrypt_valid_enc_modes(ci
->ci_data_mode
, ci
->ci_filename_mode
)) {
160 pr_warn_ratelimited("fscrypt: inode %lu uses unsupported encryption modes (contents mode %d, filenames mode %d)\n",
162 ci
->ci_data_mode
, ci
->ci_filename_mode
);
166 if (S_ISREG(inode
->i_mode
)) {
167 mode
= ci
->ci_data_mode
;
168 } else if (S_ISDIR(inode
->i_mode
) || S_ISLNK(inode
->i_mode
)) {
169 mode
= ci
->ci_filename_mode
;
171 WARN_ONCE(1, "fscrypt: filesystem tried to load encryption info for inode %lu, which is not encryptable (file type %d)\n",
172 inode
->i_ino
, (inode
->i_mode
& S_IFMT
));
176 *cipher_str_ret
= available_modes
[mode
].cipher_str
;
177 *keysize_ret
= available_modes
[mode
].keysize
;
181 static void put_crypt_info(struct fscrypt_info
*ci
)
186 crypto_free_skcipher(ci
->ci_ctfm
);
187 crypto_free_cipher(ci
->ci_essiv_tfm
);
188 kmem_cache_free(fscrypt_info_cachep
, ci
);
191 static int derive_essiv_salt(const u8
*key
, int keysize
, u8
*salt
)
193 struct crypto_shash
*tfm
= READ_ONCE(essiv_hash_tfm
);
195 /* init hash transform on demand */
196 if (unlikely(!tfm
)) {
197 struct crypto_shash
*prev_tfm
;
199 tfm
= crypto_alloc_shash("sha256", 0, 0);
201 pr_warn_ratelimited("fscrypt: error allocating SHA-256 transform: %ld\n",
205 prev_tfm
= cmpxchg(&essiv_hash_tfm
, NULL
, tfm
);
207 crypto_free_shash(tfm
);
213 SHASH_DESC_ON_STACK(desc
, tfm
);
217 return crypto_shash_digest(desc
, key
, keysize
, salt
);
221 static int init_essiv_generator(struct fscrypt_info
*ci
, const u8
*raw_key
,
225 struct crypto_cipher
*essiv_tfm
;
226 u8 salt
[SHA256_DIGEST_SIZE
];
228 essiv_tfm
= crypto_alloc_cipher("aes", 0, 0);
229 if (IS_ERR(essiv_tfm
))
230 return PTR_ERR(essiv_tfm
);
232 ci
->ci_essiv_tfm
= essiv_tfm
;
234 err
= derive_essiv_salt(raw_key
, keysize
, salt
);
239 * Using SHA256 to derive the salt/key will result in AES-256 being
240 * used for IV generation. File contents encryption will still use the
241 * configured keysize (AES-128) nevertheless.
243 err
= crypto_cipher_setkey(essiv_tfm
, salt
, sizeof(salt
));
248 memzero_explicit(salt
, sizeof(salt
));
252 void __exit
fscrypt_essiv_cleanup(void)
254 crypto_free_shash(essiv_hash_tfm
);
257 int fscrypt_get_encryption_info(struct inode
*inode
)
259 struct fscrypt_info
*crypt_info
;
260 struct fscrypt_context ctx
;
261 struct crypto_skcipher
*ctfm
;
262 const char *cipher_str
;
267 if (inode
->i_crypt_info
)
270 res
= fscrypt_initialize(inode
->i_sb
->s_cop
->flags
);
274 res
= inode
->i_sb
->s_cop
->get_context(inode
, &ctx
, sizeof(ctx
));
276 if (!fscrypt_dummy_context_enabled(inode
) ||
277 inode
->i_sb
->s_cop
->is_encrypted(inode
))
279 /* Fake up a context for an unencrypted directory */
280 memset(&ctx
, 0, sizeof(ctx
));
281 ctx
.format
= FS_ENCRYPTION_CONTEXT_FORMAT_V1
;
282 ctx
.contents_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_XTS
;
283 ctx
.filenames_encryption_mode
= FS_ENCRYPTION_MODE_AES_256_CTS
;
284 memset(ctx
.master_key_descriptor
, 0x42, FS_KEY_DESCRIPTOR_SIZE
);
285 } else if (res
!= sizeof(ctx
)) {
289 if (ctx
.format
!= FS_ENCRYPTION_CONTEXT_FORMAT_V1
)
292 if (ctx
.flags
& ~FS_POLICY_FLAGS_VALID
)
295 crypt_info
= kmem_cache_alloc(fscrypt_info_cachep
, GFP_NOFS
);
299 crypt_info
->ci_flags
= ctx
.flags
;
300 crypt_info
->ci_data_mode
= ctx
.contents_encryption_mode
;
301 crypt_info
->ci_filename_mode
= ctx
.filenames_encryption_mode
;
302 crypt_info
->ci_ctfm
= NULL
;
303 crypt_info
->ci_essiv_tfm
= NULL
;
304 memcpy(crypt_info
->ci_master_key
, ctx
.master_key_descriptor
,
305 sizeof(crypt_info
->ci_master_key
));
307 res
= determine_cipher_type(crypt_info
, inode
, &cipher_str
, &keysize
);
312 * This cannot be a stack buffer because it is passed to the scatterlist
313 * crypto API as part of key derivation.
316 raw_key
= kmalloc(FS_MAX_KEY_SIZE
, GFP_NOFS
);
320 res
= validate_user_key(crypt_info
, &ctx
, raw_key
, FS_KEY_DESC_PREFIX
,
322 if (res
&& inode
->i_sb
->s_cop
->key_prefix
) {
323 int res2
= validate_user_key(crypt_info
, &ctx
, raw_key
,
324 inode
->i_sb
->s_cop
->key_prefix
,
334 ctfm
= crypto_alloc_skcipher(cipher_str
, 0, 0);
335 if (!ctfm
|| IS_ERR(ctfm
)) {
336 res
= ctfm
? PTR_ERR(ctfm
) : -ENOMEM
;
337 pr_debug("%s: error %d (inode %lu) allocating crypto tfm\n",
338 __func__
, res
, inode
->i_ino
);
341 crypt_info
->ci_ctfm
= ctfm
;
342 crypto_skcipher_clear_flags(ctfm
, ~0);
343 crypto_skcipher_set_flags(ctfm
, CRYPTO_TFM_REQ_WEAK_KEY
);
345 * if the provided key is longer than keysize, we use the first
346 * keysize bytes of the derived key only
348 res
= crypto_skcipher_setkey(ctfm
, raw_key
, keysize
);
352 if (S_ISREG(inode
->i_mode
) &&
353 crypt_info
->ci_data_mode
== FS_ENCRYPTION_MODE_AES_128_CBC
) {
354 res
= init_essiv_generator(crypt_info
, raw_key
, keysize
);
356 pr_debug("%s: error %d (inode %lu) allocating essiv tfm\n",
357 __func__
, res
, inode
->i_ino
);
361 if (cmpxchg(&inode
->i_crypt_info
, NULL
, crypt_info
) == NULL
)
366 put_crypt_info(crypt_info
);
370 EXPORT_SYMBOL(fscrypt_get_encryption_info
);
372 void fscrypt_put_encryption_info(struct inode
*inode
, struct fscrypt_info
*ci
)
374 struct fscrypt_info
*prev
;
377 ci
= ACCESS_ONCE(inode
->i_crypt_info
);
381 prev
= cmpxchg(&inode
->i_crypt_info
, ci
, NULL
);
387 EXPORT_SYMBOL(fscrypt_put_encryption_info
);