1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2015, Google, Inc.
7 * Originally written by Michael Halcrow, Ildar Muslukhov, and Uday Savagaonkar.
8 * Heavily modified since then.
11 #ifndef _FSCRYPT_PRIVATE_H
12 #define _FSCRYPT_PRIVATE_H
14 #include <linux/fscrypt.h>
15 #include <crypto/hash.h>
17 #define CONST_STRLEN(str) (sizeof(str) - 1)
19 #define FS_KEY_DERIVATION_NONCE_SIZE 16
21 #define FSCRYPT_MIN_KEY_SIZE 16
24 * Encryption context for inode
27 * 1 byte: Protector format (1 = this version)
28 * 1 byte: File contents encryption mode
29 * 1 byte: File names encryption mode
31 * 8 bytes: Master Key descriptor
32 * 16 bytes: Encryption Key derivation nonce
34 struct fscrypt_context {
36 u8 contents_encryption_mode;
37 u8 filenames_encryption_mode;
39 u8 master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
40 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
43 #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
46 * For encrypted symlinks, the ciphertext length is stored at the beginning
47 * of the string in little-endian format.
49 struct fscrypt_symlink_data {
51 char encrypted_path[1];
55 * fscrypt_info - the "encryption key" for an inode
57 * When an encrypted file's key is made available, an instance of this struct is
58 * allocated and stored in ->i_crypt_info. Once created, it remains until the
63 /* The actual crypto transform used for encryption and decryption */
64 struct crypto_skcipher *ci_ctfm;
67 * Cipher for ESSIV IV generation. Only set for CBC contents
68 * encryption, otherwise is NULL.
70 struct crypto_cipher *ci_essiv_tfm;
73 * Encryption mode used for this inode. It corresponds to either
74 * ci_data_mode or ci_filename_mode, depending on the inode type.
76 struct fscrypt_mode *ci_mode;
78 /* Back-pointer to the inode */
79 struct inode *ci_inode;
82 * The master key with which this inode was unlocked (decrypted). This
83 * will be NULL if the master key was found in a process-subscribed
84 * keyring rather than in the filesystem-level keyring.
86 struct key *ci_master_key;
89 * Link in list of inodes that were unlocked with the master key.
90 * Only used when ->ci_master_key is set.
92 struct list_head ci_master_key_link;
95 * If non-NULL, then encryption is done using the master key directly
96 * and ci_ctfm will equal ci_direct_key->dk_ctfm.
98 struct fscrypt_direct_key *ci_direct_key;
100 /* fields from the fscrypt_context */
104 u8 ci_master_key_descriptor[FSCRYPT_KEY_DESCRIPTOR_SIZE];
105 u8 ci_nonce[FS_KEY_DERIVATION_NONCE_SIZE];
111 } fscrypt_direction_t;
113 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
115 static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
118 if (contents_mode == FSCRYPT_MODE_AES_128_CBC &&
119 filenames_mode == FSCRYPT_MODE_AES_128_CTS)
122 if (contents_mode == FSCRYPT_MODE_AES_256_XTS &&
123 filenames_mode == FSCRYPT_MODE_AES_256_CTS)
126 if (contents_mode == FSCRYPT_MODE_ADIANTUM &&
127 filenames_mode == FSCRYPT_MODE_ADIANTUM)
134 extern struct kmem_cache *fscrypt_info_cachep;
135 extern int fscrypt_initialize(unsigned int cop_flags);
136 extern int fscrypt_crypt_block(const struct inode *inode,
137 fscrypt_direction_t rw, u64 lblk_num,
138 struct page *src_page, struct page *dest_page,
139 unsigned int len, unsigned int offs,
141 extern struct page *fscrypt_alloc_bounce_page(gfp_t gfp_flags);
142 extern const struct dentry_operations fscrypt_d_ops;
144 extern void __printf(3, 4) __cold
145 fscrypt_msg(const struct inode *inode, const char *level, const char *fmt, ...);
147 #define fscrypt_warn(inode, fmt, ...) \
148 fscrypt_msg((inode), KERN_WARNING, fmt, ##__VA_ARGS__)
149 #define fscrypt_err(inode, fmt, ...) \
150 fscrypt_msg((inode), KERN_ERR, fmt, ##__VA_ARGS__)
152 #define FSCRYPT_MAX_IV_SIZE 32
156 /* logical block number within the file */
159 /* per-file nonce; only set in DIRECT_KEY mode */
160 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
162 u8 raw[FSCRYPT_MAX_IV_SIZE];
165 void fscrypt_generate_iv(union fscrypt_iv *iv, u64 lblk_num,
166 const struct fscrypt_info *ci);
169 extern int fname_encrypt(struct inode *inode, const struct qstr *iname,
170 u8 *out, unsigned int olen);
171 extern bool fscrypt_fname_encrypted_size(const struct inode *inode,
172 u32 orig_len, u32 max_len,
173 u32 *encrypted_len_ret);
177 struct fscrypt_hkdf {
178 struct crypto_shash *hmac_tfm;
181 extern int fscrypt_init_hkdf(struct fscrypt_hkdf *hkdf, const u8 *master_key,
182 unsigned int master_key_size);
184 extern int fscrypt_hkdf_expand(struct fscrypt_hkdf *hkdf, u8 context,
185 const u8 *info, unsigned int infolen,
186 u8 *okm, unsigned int okmlen);
188 extern void fscrypt_destroy_hkdf(struct fscrypt_hkdf *hkdf);
193 * fscrypt_master_key_secret - secret key material of an in-use master key
195 struct fscrypt_master_key_secret {
197 /* Size of the raw key in bytes */
201 u8 raw[FSCRYPT_MAX_KEY_SIZE];
203 } __randomize_layout;
206 * fscrypt_master_key - an in-use master key
208 * This represents a master encryption key which has been added to the
209 * filesystem and can be used to "unlock" the encrypted files which were
212 struct fscrypt_master_key {
215 * The secret key material. After FS_IOC_REMOVE_ENCRYPTION_KEY is
216 * executed, this is wiped and no new inodes can be unlocked with this
217 * key; however, there may still be inodes in ->mk_decrypted_inodes
218 * which could not be evicted. As long as some inodes still remain,
219 * FS_IOC_REMOVE_ENCRYPTION_KEY can be retried, or
220 * FS_IOC_ADD_ENCRYPTION_KEY can add the secret again.
222 * Locking: protected by key->sem.
224 struct fscrypt_master_key_secret mk_secret;
226 /* Arbitrary key descriptor which was assigned by userspace */
227 struct fscrypt_key_specifier mk_spec;
230 * Length of ->mk_decrypted_inodes, plus one if mk_secret is present.
231 * Once this goes to 0, the master key is removed from ->s_master_keys.
232 * The 'struct fscrypt_master_key' will continue to live as long as the
233 * 'struct key' whose payload it is, but we won't let this reference
236 refcount_t mk_refcount;
239 * List of inodes that were unlocked using this key. This allows the
240 * inodes to be evicted efficiently if the key is removed.
242 struct list_head mk_decrypted_inodes;
243 spinlock_t mk_decrypted_inodes_lock;
245 } __randomize_layout;
248 is_master_key_secret_present(const struct fscrypt_master_key_secret *secret)
251 * The READ_ONCE() is only necessary for fscrypt_drop_inode() and
252 * fscrypt_key_describe(). These run in atomic context, so they can't
253 * take key->sem and thus 'secret' can change concurrently which would
254 * be a data race. But they only need to know whether the secret *was*
255 * present at the time of check, so READ_ONCE() suffices.
257 return READ_ONCE(secret->size) != 0;
260 static inline const char *master_key_spec_type(
261 const struct fscrypt_key_specifier *spec)
263 switch (spec->type) {
264 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
270 static inline int master_key_spec_len(const struct fscrypt_key_specifier *spec)
272 switch (spec->type) {
273 case FSCRYPT_KEY_SPEC_TYPE_DESCRIPTOR:
274 return FSCRYPT_KEY_DESCRIPTOR_SIZE;
280 fscrypt_find_master_key(struct super_block *sb,
281 const struct fscrypt_key_specifier *mk_spec);
283 extern int __init fscrypt_init_keyring(void);
287 struct fscrypt_mode {
288 const char *friendly_name;
289 const char *cipher_str;
292 bool logged_impl_name;
297 fscrypt_mode_supports_direct_key(const struct fscrypt_mode *mode)
299 return mode->ivsize >= offsetofend(union fscrypt_iv, nonce);
302 extern struct crypto_skcipher *
303 fscrypt_allocate_skcipher(struct fscrypt_mode *mode, const u8 *raw_key,
304 const struct inode *inode);
306 extern int fscrypt_set_derived_key(struct fscrypt_info *ci,
307 const u8 *derived_key);
311 extern void fscrypt_put_direct_key(struct fscrypt_direct_key *dk);
313 extern int fscrypt_setup_v1_file_key(struct fscrypt_info *ci,
314 const u8 *raw_master_key);
316 extern int fscrypt_setup_v1_file_key_via_subscribed_keyrings(
317 struct fscrypt_info *ci);
319 #endif /* _FSCRYPT_PRIVATE_H */