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