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 <uapi/linux/fs.h>
20 #define FS_KEY_DERIVATION_NONCE_SIZE 16
21 #define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
23 #define FS_POLICY_FLAGS_PAD_4 0x00
24 #define FS_POLICY_FLAGS_PAD_8 0x01
25 #define FS_POLICY_FLAGS_PAD_16 0x02
26 #define FS_POLICY_FLAGS_PAD_32 0x03
27 #define FS_POLICY_FLAGS_PAD_MASK 0x03
28 #define FS_POLICY_FLAGS_VALID 0x03
30 /* Encryption algorithms */
31 #define FS_ENCRYPTION_MODE_INVALID 0
32 #define FS_ENCRYPTION_MODE_AES_256_XTS 1
33 #define FS_ENCRYPTION_MODE_AES_256_GCM 2
34 #define FS_ENCRYPTION_MODE_AES_256_CBC 3
35 #define FS_ENCRYPTION_MODE_AES_256_CTS 4
38 * Encryption context for inode
41 * 1 byte: Protector format (1 = this version)
42 * 1 byte: File contents encryption mode
43 * 1 byte: File names encryption mode
45 * 8 bytes: Master Key descriptor
46 * 16 bytes: Encryption Key derivation nonce
48 struct fscrypt_context {
50 u8 contents_encryption_mode;
51 u8 filenames_encryption_mode;
53 u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
54 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
57 /* Encryption parameters */
58 #define FS_XTS_TWEAK_SIZE 16
59 #define FS_AES_128_ECB_KEY_SIZE 16
60 #define FS_AES_256_GCM_KEY_SIZE 32
61 #define FS_AES_256_CBC_KEY_SIZE 32
62 #define FS_AES_256_CTS_KEY_SIZE 32
63 #define FS_AES_256_XTS_KEY_SIZE 64
64 #define FS_MAX_KEY_SIZE 64
66 #define FS_KEY_DESC_PREFIX "fscrypt:"
67 #define FS_KEY_DESC_PREFIX_SIZE 8
69 /* This is passed in from userspace into the kernel keyring */
72 u8 raw[FS_MAX_KEY_SIZE];
80 struct crypto_ablkcipher *ci_ctfm;
81 struct key *ci_keyring_key;
82 u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
85 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL 0x00000001
86 #define FS_WRITE_PATH_FL 0x00000002
91 struct page *bounce_page; /* Ciphertext page */
92 struct page *control_page; /* Original page */
96 struct work_struct work;
98 struct list_head free_list; /* Free list */
100 u8 flags; /* Flags */
101 u8 mode; /* Encryption mode for tfm */
104 struct fscrypt_completion_result {
105 struct completion completion;
109 #define DECLARE_FS_COMPLETION_RESULT(ecr) \
110 struct fscrypt_completion_result ecr = { \
111 COMPLETION_INITIALIZER((ecr).completion), 0 }
113 static inline int fscrypt_key_size(int mode)
116 case FS_ENCRYPTION_MODE_AES_256_XTS:
117 return FS_AES_256_XTS_KEY_SIZE;
118 case FS_ENCRYPTION_MODE_AES_256_GCM:
119 return FS_AES_256_GCM_KEY_SIZE;
120 case FS_ENCRYPTION_MODE_AES_256_CBC:
121 return FS_AES_256_CBC_KEY_SIZE;
122 case FS_ENCRYPTION_MODE_AES_256_CTS:
123 return FS_AES_256_CTS_KEY_SIZE;
130 #define FS_FNAME_NUM_SCATTER_ENTRIES 4
131 #define FS_CRYPTO_BLOCK_SIZE 16
132 #define FS_FNAME_CRYPTO_DIGEST_SIZE 32
135 * For encrypted symlinks, the ciphertext length is stored at the beginning
136 * of the string in little-endian format.
138 struct fscrypt_symlink_data {
140 char encrypted_path[1];
144 * This function is used to calculate the disk space required to
145 * store a filename of length l in encrypted symlink format.
147 static inline u32 fscrypt_symlink_data_len(u32 l)
149 if (l < FS_CRYPTO_BLOCK_SIZE)
150 l = FS_CRYPTO_BLOCK_SIZE;
151 return (l + sizeof(struct fscrypt_symlink_data) - 1);
159 struct fscrypt_name {
160 const struct qstr *usr_fname;
161 struct fscrypt_str disk_name;
164 struct fscrypt_str crypto_buf;
167 #define FSTR_INIT(n, l) { .name = n, .len = l }
168 #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
169 #define fname_name(p) ((p)->disk_name.name)
170 #define fname_len(p) ((p)->disk_name.len)
173 * crypto opertions for filesystems
175 struct fscrypt_operations {
176 int (*get_context)(struct inode *, void *, size_t);
177 int (*prepare_context)(struct inode *);
178 int (*set_context)(struct inode *, const void *, size_t, void *);
179 int (*dummy_context)(struct inode *);
180 bool (*is_encrypted)(struct inode *);
181 bool (*empty_dir)(struct inode *);
182 unsigned (*max_namelen)(struct inode *);
185 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
187 if (inode->i_sb->s_cop->dummy_context &&
188 inode->i_sb->s_cop->dummy_context(inode))
193 static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
195 return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
198 static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
200 return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
203 static inline u32 fscrypt_validate_encryption_key_size(u32 mode, u32 size)
205 if (size == fscrypt_key_size(mode))
210 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
212 if (str->len == 1 && str->name[0] == '.')
215 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
221 static inline struct page *fscrypt_control_page(struct page *page)
223 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
224 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
227 return ERR_PTR(-EINVAL);
231 static inline int fscrypt_has_encryption_key(struct inode *inode)
233 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
234 return (inode->i_crypt_info != NULL);
240 static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
242 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
243 spin_lock(&dentry->d_lock);
244 dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
245 spin_unlock(&dentry->d_lock);
249 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
250 extern const struct dentry_operations fscrypt_d_ops;
253 static inline void fscrypt_set_d_op(struct dentry *dentry)
255 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
256 d_set_d_op(dentry, &fscrypt_d_ops);
260 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
262 extern struct kmem_cache *fscrypt_info_cachep;
263 int fscrypt_initialize(void);
265 extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *);
266 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
267 extern struct page *fscrypt_encrypt_page(struct inode *, struct page *);
268 extern int fscrypt_decrypt_page(struct page *);
269 extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
270 extern void fscrypt_pullback_bio_page(struct page **, bool);
271 extern void fscrypt_restore_control_page(struct page *);
272 extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
275 extern int fscrypt_process_policy(struct inode *,
276 const struct fscrypt_policy *);
277 extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *);
278 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
279 extern int fscrypt_inherit_context(struct inode *, struct inode *,
282 extern int get_crypt_info(struct inode *);
283 extern int fscrypt_get_encryption_info(struct inode *);
284 extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
287 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
288 int lookup, struct fscrypt_name *);
289 extern void fscrypt_free_filename(struct fscrypt_name *);
290 extern u32 fscrypt_fname_encrypted_size(struct inode *, u32);
291 extern int fscrypt_fname_alloc_buffer(struct inode *, u32,
292 struct fscrypt_str *);
293 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
294 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
295 const struct fscrypt_str *, struct fscrypt_str *);
296 extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
297 struct fscrypt_str *);
301 static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i)
303 return ERR_PTR(-EOPNOTSUPP);
306 static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
311 static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
314 return ERR_PTR(-EOPNOTSUPP);
317 static inline int fscrypt_notsupp_decrypt_page(struct page *p)
322 static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
328 static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
333 static inline void fscrypt_notsupp_restore_control_page(struct page *p)
338 static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
339 sector_t s, unsigned int f)
345 static inline int fscrypt_notsupp_process_policy(struct inode *i,
346 const struct fscrypt_policy *p)
351 static inline int fscrypt_notsupp_get_policy(struct inode *i,
352 struct fscrypt_policy *p)
357 static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
363 static inline int fscrypt_notsupp_inherit_context(struct inode *p,
364 struct inode *i, void *v, bool b)
370 static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
375 static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
376 struct fscrypt_info *f)
382 static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
383 const struct qstr *iname,
384 int lookup, struct fscrypt_name *fname)
386 if (dir->i_sb->s_cop->is_encrypted(dir))
389 memset(fname, 0, sizeof(struct fscrypt_name));
390 fname->usr_fname = iname;
391 fname->disk_name.name = (unsigned char *)iname->name;
392 fname->disk_name.len = iname->len;
396 static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
401 static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
408 static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
409 u32 ilen, struct fscrypt_str *crypto_str)
414 static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
419 static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
420 u32 hash, u32 minor_hash,
421 const struct fscrypt_str *iname,
422 struct fscrypt_str *oname)
427 static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
428 const struct qstr *iname,
429 struct fscrypt_str *oname)
433 #endif /* _LINUX_FSCRYPTO_H */