2 * General per-file encryption definition
4 * Copyright (C) 2015, Google, Inc.
6 * Written by Michael Halcrow, 2015.
7 * Modified by Jaegeuk Kim, 2015.
10 #ifndef _LINUX_FSCRYPTO_H
11 #define _LINUX_FSCRYPTO_H
13 #include <linux/key.h>
16 #include <linux/bio.h>
17 #include <linux/dcache.h>
18 #include <crypto/skcipher.h>
19 #include <uapi/linux/fs.h>
21 #define FS_KEY_DERIVATION_NONCE_SIZE 16
22 #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
24 #define FS_POLICY_FLAGS_PAD_4 0x00
25 #define FS_POLICY_FLAGS_PAD_8 0x01
26 #define FS_POLICY_FLAGS_PAD_16 0x02
27 #define FS_POLICY_FLAGS_PAD_32 0x03
28 #define FS_POLICY_FLAGS_PAD_MASK 0x03
29 #define FS_POLICY_FLAGS_VALID 0x03
31 /* Encryption algorithms */
32 #define FS_ENCRYPTION_MODE_INVALID 0
33 #define FS_ENCRYPTION_MODE_AES_256_XTS 1
34 #define FS_ENCRYPTION_MODE_AES_256_GCM 2
35 #define FS_ENCRYPTION_MODE_AES_256_CBC 3
36 #define FS_ENCRYPTION_MODE_AES_256_CTS 4
39 * Encryption context for inode
42 * 1 byte: Protector format (1 = this version)
43 * 1 byte: File contents encryption mode
44 * 1 byte: File names encryption mode
46 * 8 bytes: Master Key descriptor
47 * 16 bytes: Encryption Key derivation nonce
49 struct fscrypt_context {
51 u8 contents_encryption_mode;
52 u8 filenames_encryption_mode;
54 u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
55 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
58 /* Encryption parameters */
59 #define FS_XTS_TWEAK_SIZE 16
60 #define FS_AES_128_ECB_KEY_SIZE 16
61 #define FS_AES_256_GCM_KEY_SIZE 32
62 #define FS_AES_256_CBC_KEY_SIZE 32
63 #define FS_AES_256_CTS_KEY_SIZE 32
64 #define FS_AES_256_XTS_KEY_SIZE 64
65 #define FS_MAX_KEY_SIZE 64
67 #define FS_KEY_DESC_PREFIX "fscrypt:"
68 #define FS_KEY_DESC_PREFIX_SIZE 8
70 /* This is passed in from userspace into the kernel keyring */
73 u8 raw[FS_MAX_KEY_SIZE];
81 struct crypto_skcipher *ci_ctfm;
82 struct key *ci_keyring_key;
83 u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
86 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
87 #define FS_WRITE_PATH_FL 0x00000002
92 struct page *bounce_page; /* Ciphertext page */
93 struct page *control_page; /* Original page */
97 struct work_struct work;
99 struct list_head free_list; /* Free list */
101 u8 flags; /* Flags */
102 u8 mode; /* Encryption mode for tfm */
105 struct fscrypt_completion_result {
106 struct completion completion;
110 #define DECLARE_FS_COMPLETION_RESULT(ecr) \
111 struct fscrypt_completion_result ecr = { \
112 COMPLETION_INITIALIZER((ecr).completion), 0 }
114 #define FS_FNAME_NUM_SCATTER_ENTRIES 4
115 #define FS_CRYPTO_BLOCK_SIZE 16
116 #define FS_FNAME_CRYPTO_DIGEST_SIZE 32
119 * For encrypted symlinks, the ciphertext length is stored at the beginning
120 * of the string in little-endian format.
122 struct fscrypt_symlink_data {
124 char encrypted_path[1];
128 * This function is used to calculate the disk space required to
129 * store a filename of length l in encrypted symlink format.
131 static inline u32 fscrypt_symlink_data_len(u32 l)
133 if (l < FS_CRYPTO_BLOCK_SIZE)
134 l = FS_CRYPTO_BLOCK_SIZE;
135 return (l + sizeof(struct fscrypt_symlink_data) - 1);
143 struct fscrypt_name {
144 const struct qstr *usr_fname;
145 struct fscrypt_str disk_name;
148 struct fscrypt_str crypto_buf;
151 #define FSTR_INIT(n, l) { .name = n, .len = l }
152 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
153 #define fname_name(p) ((p)->disk_name.name)
154 #define fname_len(p) ((p)->disk_name.len)
157 * crypto opertions for filesystems
159 struct fscrypt_operations {
160 int (*get_context)(struct inode *, void *, size_t);
161 int (*key_prefix)(struct inode *, u8 **);
162 int (*prepare_context)(struct inode *);
163 int (*set_context)(struct inode *, const void *, size_t, void *);
164 int (*dummy_context)(struct inode *);
165 bool (*is_encrypted)(struct inode *);
166 bool (*empty_dir)(struct inode *);
167 unsigned (*max_namelen)(struct inode *);
170 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
172 if (inode->i_sb->s_cop->dummy_context &&
173 inode->i_sb->s_cop->dummy_context(inode))
178 static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
180 return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
183 static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
185 return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
188 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
190 if (str->len == 1 && str->name[0] == '.')
193 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
199 static inline struct page *fscrypt_control_page(struct page *page)
201 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
202 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
205 return ERR_PTR(-EINVAL);
209 static inline int fscrypt_has_encryption_key(struct inode *inode)
211 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
212 return (inode->i_crypt_info != NULL);
218 static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
220 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
221 spin_lock(&dentry->d_lock);
222 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
223 spin_unlock(&dentry->d_lock);
227 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
228 extern const struct dentry_operations fscrypt_d_ops;
231 static inline void fscrypt_set_d_op(struct dentry *dentry)
233 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
234 d_set_d_op(dentry, &fscrypt_d_ops);
238 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
240 extern struct kmem_cache *fscrypt_info_cachep;
241 int fscrypt_initialize(void);
243 extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *, gfp_t);
244 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
245 extern struct page *fscrypt_encrypt_page(struct inode *, struct page *, gfp_t);
246 extern int fscrypt_decrypt_page(struct page *);
247 extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
248 extern void fscrypt_pullback_bio_page(struct page **, bool);
249 extern void fscrypt_restore_control_page(struct page *);
250 extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
253 extern int fscrypt_process_policy(struct file *, const struct fscrypt_policy *);
254 extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *);
255 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
256 extern int fscrypt_inherit_context(struct inode *, struct inode *,
259 extern int get_crypt_info(struct inode *);
260 extern int fscrypt_get_encryption_info(struct inode *);
261 extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
264 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
265 int lookup, struct fscrypt_name *);
266 extern void fscrypt_free_filename(struct fscrypt_name *);
267 extern u32 fscrypt_fname_encrypted_size(struct inode *, u32);
268 extern int fscrypt_fname_alloc_buffer(struct inode *, u32,
269 struct fscrypt_str *);
270 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
271 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
272 const struct fscrypt_str *, struct fscrypt_str *);
273 extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
274 struct fscrypt_str *);
278 static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i,
281 return ERR_PTR(-EOPNOTSUPP);
284 static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
289 static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
290 struct page *p, gfp_t f)
292 return ERR_PTR(-EOPNOTSUPP);
295 static inline int fscrypt_notsupp_decrypt_page(struct page *p)
300 static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
306 static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
311 static inline void fscrypt_notsupp_restore_control_page(struct page *p)
316 static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
317 sector_t s, unsigned int f)
323 static inline int fscrypt_notsupp_process_policy(struct file *f,
324 const struct fscrypt_policy *p)
329 static inline int fscrypt_notsupp_get_policy(struct inode *i,
330 struct fscrypt_policy *p)
335 static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
341 static inline int fscrypt_notsupp_inherit_context(struct inode *p,
342 struct inode *i, void *v, bool b)
348 static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
353 static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
354 struct fscrypt_info *f)
360 static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
361 const struct qstr *iname,
362 int lookup, struct fscrypt_name *fname)
364 if (dir->i_sb->s_cop->is_encrypted(dir))
367 memset(fname, 0, sizeof(struct fscrypt_name));
368 fname->usr_fname = iname;
369 fname->disk_name.name = (unsigned char *)iname->name;
370 fname->disk_name.len = iname->len;
374 static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
379 static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
386 static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
387 u32 ilen, struct fscrypt_str *crypto_str)
392 static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
397 static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
398 u32 hash, u32 minor_hash,
399 const struct fscrypt_str *iname,
400 struct fscrypt_str *oname)
405 static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
406 const struct qstr *iname,
407 struct fscrypt_str *oname)
411 #endif /* _LINUX_FSCRYPTO_H */