GNU Linux-libre 4.9.287-gnu1
[releases.git] / include / linux / fscrypto.h
1 /*
2  * General per-file encryption definition
3  *
4  * Copyright (C) 2015, Google, Inc.
5  *
6  * Written by Michael Halcrow, 2015.
7  * Modified by Jaegeuk Kim, 2015.
8  */
9
10 #ifndef _LINUX_FSCRYPTO_H
11 #define _LINUX_FSCRYPTO_H
12
13 #include <linux/key.h>
14 #include <linux/fs.h>
15 #include <linux/mm.h>
16 #include <linux/bio.h>
17 #include <linux/dcache.h>
18 #include <crypto/skcipher.h>
19 #include <uapi/linux/fs.h>
20
21 #define FS_KEY_DERIVATION_NONCE_SIZE            16
22 #define FS_ENCRYPTION_CONTEXT_FORMAT_V1         1
23
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
30
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
37
38 /**
39  * Encryption context for inode
40  *
41  * Protector format:
42  *  1 byte: Protector format (1 = this version)
43  *  1 byte: File contents encryption mode
44  *  1 byte: File names encryption mode
45  *  1 byte: Flags
46  *  8 bytes: Master Key descriptor
47  *  16 bytes: Encryption Key derivation nonce
48  */
49 struct fscrypt_context {
50         u8 format;
51         u8 contents_encryption_mode;
52         u8 filenames_encryption_mode;
53         u8 flags;
54         u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
55         u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
56 } __packed;
57
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
66
67 #define FS_KEY_DESC_PREFIX              "fscrypt:"
68 #define FS_KEY_DESC_PREFIX_SIZE         8
69
70 /* This is passed in from userspace into the kernel keyring */
71 struct fscrypt_key {
72         u32 mode;
73         u8 raw[FS_MAX_KEY_SIZE];
74         u32 size;
75 } __packed;
76
77 struct fscrypt_info {
78         u8 ci_data_mode;
79         u8 ci_filename_mode;
80         u8 ci_flags;
81         struct crypto_skcipher *ci_ctfm;
82         u8 ci_master_key[FS_KEY_DESCRIPTOR_SIZE];
83 };
84
85 #define FS_CTX_REQUIRES_FREE_ENCRYPT_FL         0x00000001
86 #define FS_WRITE_PATH_FL                        0x00000002
87
88 struct fscrypt_ctx {
89         union {
90                 struct {
91                         struct page *bounce_page;       /* Ciphertext page */
92                         struct page *control_page;      /* Original page  */
93                 } w;
94                 struct {
95                         struct bio *bio;
96                         struct work_struct work;
97                 } r;
98                 struct list_head free_list;     /* Free list */
99         };
100         u8 flags;                               /* Flags */
101         u8 mode;                                /* Encryption mode for tfm */
102 };
103
104 struct fscrypt_completion_result {
105         struct completion completion;
106         int res;
107 };
108
109 #define DECLARE_FS_COMPLETION_RESULT(ecr) \
110         struct fscrypt_completion_result ecr = { \
111                 COMPLETION_INITIALIZER((ecr).completion), 0 }
112
113 #define FS_FNAME_NUM_SCATTER_ENTRIES    4
114 #define FS_CRYPTO_BLOCK_SIZE            16
115 #define FS_FNAME_CRYPTO_DIGEST_SIZE     32
116
117 /**
118  * For encrypted symlinks, the ciphertext length is stored at the beginning
119  * of the string in little-endian format.
120  */
121 struct fscrypt_symlink_data {
122         __le16 len;
123         char encrypted_path[1];
124 } __packed;
125
126 /**
127  * This function is used to calculate the disk space required to
128  * store a filename of length l in encrypted symlink format.
129  */
130 static inline u32 fscrypt_symlink_data_len(u32 l)
131 {
132         if (l < FS_CRYPTO_BLOCK_SIZE)
133                 l = FS_CRYPTO_BLOCK_SIZE;
134         return (l + sizeof(struct fscrypt_symlink_data) - 1);
135 }
136
137 struct fscrypt_str {
138         unsigned char *name;
139         u32 len;
140 };
141
142 struct fscrypt_name {
143         const struct qstr *usr_fname;
144         struct fscrypt_str disk_name;
145         u32 hash;
146         u32 minor_hash;
147         struct fscrypt_str crypto_buf;
148 };
149
150 #define FSTR_INIT(n, l)         { .name = n, .len = l }
151 #define FSTR_TO_QSTR(f)         QSTR_INIT((f)->name, (f)->len)
152 #define fname_name(p)           ((p)->disk_name.name)
153 #define fname_len(p)            ((p)->disk_name.len)
154
155 /*
156  * crypto opertions for filesystems
157  */
158 struct fscrypt_operations {
159         int (*get_context)(struct inode *, void *, size_t);
160         int (*key_prefix)(struct inode *, u8 **);
161         int (*prepare_context)(struct inode *);
162         int (*set_context)(struct inode *, const void *, size_t, void *);
163         int (*dummy_context)(struct inode *);
164         bool (*is_encrypted)(struct inode *);
165         bool (*empty_dir)(struct inode *);
166         unsigned (*max_namelen)(struct inode *);
167 };
168
169 static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
170 {
171         if (inode->i_sb->s_cop->dummy_context &&
172                                 inode->i_sb->s_cop->dummy_context(inode))
173                 return true;
174         return false;
175 }
176
177 static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
178 {
179         return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
180 }
181
182 static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
183 {
184         return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
185 }
186
187 static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
188 {
189         if (str->len == 1 && str->name[0] == '.')
190                 return true;
191
192         if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
193                 return true;
194
195         return false;
196 }
197
198 static inline struct page *fscrypt_control_page(struct page *page)
199 {
200 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
201         return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
202 #else
203         WARN_ON_ONCE(1);
204         return ERR_PTR(-EINVAL);
205 #endif
206 }
207
208 static inline int fscrypt_has_encryption_key(struct inode *inode)
209 {
210 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
211         return (inode->i_crypt_info != NULL);
212 #else
213         return 0;
214 #endif
215 }
216
217 static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
218 {
219 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
220         spin_lock(&dentry->d_lock);
221         dentry->d_flags |= DCACHE_ENCRYPTED_WITH_KEY;
222         spin_unlock(&dentry->d_lock);
223 #endif
224 }
225
226 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
227 extern const struct dentry_operations fscrypt_d_ops;
228 #endif
229
230 static inline void fscrypt_set_d_op(struct dentry *dentry)
231 {
232 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
233         d_set_d_op(dentry, &fscrypt_d_ops);
234 #endif
235 }
236
237 #if IS_ENABLED(CONFIG_FS_ENCRYPTION)
238 /* crypto.c */
239 extern struct kmem_cache *fscrypt_info_cachep;
240 int fscrypt_initialize(void);
241
242 extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *, gfp_t);
243 extern void fscrypt_release_ctx(struct fscrypt_ctx *);
244 extern struct page *fscrypt_encrypt_page(struct inode *, struct page *, gfp_t);
245 extern int fscrypt_decrypt_page(struct page *);
246 extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
247 extern void fscrypt_pullback_bio_page(struct page **, bool);
248 extern void fscrypt_restore_control_page(struct page *);
249 extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
250                                                 unsigned int);
251 /* policy.c */
252 extern int fscrypt_ioctl_set_policy(struct file *, const void __user *);
253 extern int fscrypt_ioctl_get_policy(struct file *, void __user *);
254 extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
255 extern int fscrypt_inherit_context(struct inode *, struct inode *,
256                                         void *, bool);
257 /* keyinfo.c */
258 extern int fscrypt_get_encryption_info(struct inode *);
259 extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
260
261 /* fname.c */
262 extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
263                                 int lookup, struct fscrypt_name *);
264 extern void fscrypt_free_filename(struct fscrypt_name *);
265 extern u32 fscrypt_fname_encrypted_size(struct inode *, u32);
266 extern int fscrypt_fname_alloc_buffer(struct inode *, u32,
267                                 struct fscrypt_str *);
268 extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
269 extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
270                         const struct fscrypt_str *, struct fscrypt_str *);
271 extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
272                         struct fscrypt_str *);
273 #endif
274
275 /* crypto.c */
276 static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i,
277                                                         gfp_t f)
278 {
279         return ERR_PTR(-EOPNOTSUPP);
280 }
281
282 static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
283 {
284         return;
285 }
286
287 static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
288                                                 struct page *p, gfp_t f)
289 {
290         return ERR_PTR(-EOPNOTSUPP);
291 }
292
293 static inline int fscrypt_notsupp_decrypt_page(struct page *p)
294 {
295         return -EOPNOTSUPP;
296 }
297
298 static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
299                                                 struct bio *b)
300 {
301         return;
302 }
303
304 static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
305 {
306         return;
307 }
308
309 static inline void fscrypt_notsupp_restore_control_page(struct page *p)
310 {
311         return;
312 }
313
314 static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
315                                         sector_t s, unsigned int f)
316 {
317         return -EOPNOTSUPP;
318 }
319
320 /* policy.c */
321 static inline int fscrypt_notsupp_ioctl_set_policy(struct file *f,
322                                 const void __user *arg)
323 {
324         return -EOPNOTSUPP;
325 }
326
327 static inline int fscrypt_notsupp_ioctl_get_policy(struct file *f,
328                                 void __user *arg)
329 {
330         return -EOPNOTSUPP;
331 }
332
333 static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
334                                 struct inode *i)
335 {
336         return 0;
337 }
338
339 static inline int fscrypt_notsupp_inherit_context(struct inode *p,
340                                 struct inode *i, void *v, bool b)
341 {
342         return -EOPNOTSUPP;
343 }
344
345 /* keyinfo.c */
346 static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
347 {
348         return -EOPNOTSUPP;
349 }
350
351 static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
352                                         struct fscrypt_info *f)
353 {
354         return;
355 }
356
357  /* fname.c */
358 static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
359                         const struct qstr *iname,
360                         int lookup, struct fscrypt_name *fname)
361 {
362         if (dir->i_sb->s_cop->is_encrypted(dir))
363                 return -EOPNOTSUPP;
364
365         memset(fname, 0, sizeof(struct fscrypt_name));
366         fname->usr_fname = iname;
367         fname->disk_name.name = (unsigned char *)iname->name;
368         fname->disk_name.len = iname->len;
369         return 0;
370 }
371
372 static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
373 {
374         return;
375 }
376
377 static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
378 {
379         /* never happens */
380         WARN_ON(1);
381         return 0;
382 }
383
384 static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
385                                 u32 ilen, struct fscrypt_str *crypto_str)
386 {
387         return -EOPNOTSUPP;
388 }
389
390 static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
391 {
392         return;
393 }
394
395 static inline int fscrypt_notsupp_fname_disk_to_usr(struct inode *inode,
396                         u32 hash, u32 minor_hash,
397                         const struct fscrypt_str *iname,
398                         struct fscrypt_str *oname)
399 {
400         return -EOPNOTSUPP;
401 }
402
403 static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
404                         const struct qstr *iname,
405                         struct fscrypt_str *oname)
406 {
407         return -EOPNOTSUPP;
408 }
409 #endif  /* _LINUX_FSCRYPTO_H */