fs crypto: move per-file encryption from f2fs tree to fs/crypto
[deliverable/linux.git] / include / linux / fscrypto.h
CommitLineData
0b81d077
JK
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 <uapi/linux/fs.h>
19
20#define FS_KEY_DERIVATION_NONCE_SIZE 16
21#define FS_ENCRYPTION_CONTEXT_FORMAT_V1 1
22
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
29
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
36
37/**
38 * Encryption context for inode
39 *
40 * Protector format:
41 * 1 byte: Protector format (1 = this version)
42 * 1 byte: File contents encryption mode
43 * 1 byte: File names encryption mode
44 * 1 byte: Flags
45 * 8 bytes: Master Key descriptor
46 * 16 bytes: Encryption Key derivation nonce
47 */
48struct fscrypt_context {
49 u8 format;
50 u8 contents_encryption_mode;
51 u8 filenames_encryption_mode;
52 u8 flags;
53 u8 master_key_descriptor[FS_KEY_DESCRIPTOR_SIZE];
54 u8 nonce[FS_KEY_DERIVATION_NONCE_SIZE];
55} __packed;
56
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
65
66#define FS_KEY_DESC_PREFIX "fscrypt:"
67#define FS_KEY_DESC_PREFIX_SIZE 8
68
69/* This is passed in from userspace into the kernel keyring */
70struct fscrypt_key {
71 u32 mode;
72 u8 raw[FS_MAX_KEY_SIZE];
73 u32 size;
74} __packed;
75
76struct fscrypt_info {
77 u8 ci_data_mode;
78 u8 ci_filename_mode;
79 u8 ci_flags;
80 struct crypto_ablkcipher *ci_ctfm;
81 struct key *ci_keyring_key;
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
88struct 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
104struct 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
113static inline int fscrypt_key_size(int mode)
114{
115 switch (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;
124 default:
125 BUG();
126 }
127 return 0;
128}
129
130#define FS_FNAME_NUM_SCATTER_ENTRIES 4
131#define FS_CRYPTO_BLOCK_SIZE 16
132#define FS_FNAME_CRYPTO_DIGEST_SIZE 32
133
134/**
135 * For encrypted symlinks, the ciphertext length is stored at the beginning
136 * of the string in little-endian format.
137 */
138struct fscrypt_symlink_data {
139 __le16 len;
140 char encrypted_path[1];
141} __packed;
142
143/**
144 * This function is used to calculate the disk space required to
145 * store a filename of length l in encrypted symlink format.
146 */
147static inline u32 fscrypt_symlink_data_len(u32 l)
148{
149 if (l < FS_CRYPTO_BLOCK_SIZE)
150 l = FS_CRYPTO_BLOCK_SIZE;
151 return (l + sizeof(struct fscrypt_symlink_data) - 1);
152}
153
154struct fscrypt_str {
155 unsigned char *name;
156 u32 len;
157};
158
159struct fscrypt_name {
160 const struct qstr *usr_fname;
161 struct fscrypt_str disk_name;
162 u32 hash;
163 u32 minor_hash;
164 struct fscrypt_str crypto_buf;
165};
166
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 (*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 *);
183};
184
185static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
186{
187 if (inode->i_sb->s_cop->dummy_context &&
188 inode->i_sb->s_cop->dummy_context(inode))
189 return true;
190 return false;
191}
192
193static inline bool fscrypt_valid_contents_enc_mode(u32 mode)
194{
195 return (mode == FS_ENCRYPTION_MODE_AES_256_XTS);
196}
197
198static inline bool fscrypt_valid_filenames_enc_mode(u32 mode)
199{
200 return (mode == FS_ENCRYPTION_MODE_AES_256_CTS);
201}
202
203static inline u32 fscrypt_validate_encryption_key_size(u32 mode, u32 size)
204{
205 if (size == fscrypt_key_size(mode))
206 return size;
207 return 0;
208}
209
210static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
211{
212 if (str->len == 1 && str->name[0] == '.')
213 return true;
214
215 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
216 return true;
217
218 return false;
219}
220
221static inline struct page *fscrypt_control_page(struct page *page)
222{
223#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
224 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
225#else
226 WARN_ON_ONCE(1);
227 return ERR_PTR(-EINVAL);
228#endif
229}
230
231static inline int fscrypt_has_encryption_key(struct inode *inode)
232{
233#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
234 return (inode->i_crypt_info != NULL);
235#else
236 return 0;
237#endif
238}
239
240static inline void fscrypt_set_encrypted_dentry(struct dentry *dentry)
241{
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);
246#endif
247}
248
249#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
250extern const struct dentry_operations fscrypt_d_ops;
251#endif
252
253static inline void fscrypt_set_d_op(struct dentry *dentry)
254{
255#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
256 d_set_d_op(dentry, &fscrypt_d_ops);
257#endif
258}
259
260#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
261/* crypto.c */
262extern struct kmem_cache *fscrypt_info_cachep;
263int fscrypt_initialize(void);
264
265extern struct fscrypt_ctx *fscrypt_get_ctx(struct inode *);
266extern void fscrypt_release_ctx(struct fscrypt_ctx *);
267extern struct page *fscrypt_encrypt_page(struct inode *, struct page *);
268extern int fscrypt_decrypt_page(struct page *);
269extern void fscrypt_decrypt_bio_pages(struct fscrypt_ctx *, struct bio *);
270extern void fscrypt_pullback_bio_page(struct page **, bool);
271extern void fscrypt_restore_control_page(struct page *);
272extern int fscrypt_zeroout_range(struct inode *, pgoff_t, sector_t,
273 unsigned int);
274/* policy.c */
275extern int fscrypt_process_policy(struct inode *,
276 const struct fscrypt_policy *);
277extern int fscrypt_get_policy(struct inode *, struct fscrypt_policy *);
278extern int fscrypt_has_permitted_context(struct inode *, struct inode *);
279extern int fscrypt_inherit_context(struct inode *, struct inode *,
280 void *, bool);
281/* keyinfo.c */
282extern int get_crypt_info(struct inode *);
283extern int fscrypt_get_encryption_info(struct inode *);
284extern void fscrypt_put_encryption_info(struct inode *, struct fscrypt_info *);
285
286/* fname.c */
287extern int fscrypt_setup_filename(struct inode *, const struct qstr *,
288 int lookup, struct fscrypt_name *);
289extern void fscrypt_free_filename(struct fscrypt_name *);
290extern u32 fscrypt_fname_encrypted_size(struct inode *, u32);
291extern int fscrypt_fname_alloc_buffer(struct inode *, u32,
292 struct fscrypt_str *);
293extern void fscrypt_fname_free_buffer(struct fscrypt_str *);
294extern int fscrypt_fname_disk_to_usr(struct inode *, u32, u32,
295 const struct fscrypt_str *, struct fscrypt_str *);
296extern int fscrypt_fname_usr_to_disk(struct inode *, const struct qstr *,
297 struct fscrypt_str *);
298#endif
299
300/* crypto.c */
301static inline struct fscrypt_ctx *fscrypt_notsupp_get_ctx(struct inode *i)
302{
303 return ERR_PTR(-EOPNOTSUPP);
304}
305
306static inline void fscrypt_notsupp_release_ctx(struct fscrypt_ctx *c)
307{
308 return;
309}
310
311static inline struct page *fscrypt_notsupp_encrypt_page(struct inode *i,
312 struct page *p)
313{
314 return ERR_PTR(-EOPNOTSUPP);
315}
316
317static inline int fscrypt_notsupp_decrypt_page(struct page *p)
318{
319 return -EOPNOTSUPP;
320}
321
322static inline void fscrypt_notsupp_decrypt_bio_pages(struct fscrypt_ctx *c,
323 struct bio *b)
324{
325 return;
326}
327
328static inline void fscrypt_notsupp_pullback_bio_page(struct page **p, bool b)
329{
330 return;
331}
332
333static inline void fscrypt_notsupp_restore_control_page(struct page *p)
334{
335 return;
336}
337
338static inline int fscrypt_notsupp_zeroout_range(struct inode *i, pgoff_t p,
339 sector_t s, unsigned int f)
340{
341 return -EOPNOTSUPP;
342}
343
344/* policy.c */
345static inline int fscrypt_notsupp_process_policy(struct inode *i,
346 const struct fscrypt_policy *p)
347{
348 return -EOPNOTSUPP;
349}
350
351static inline int fscrypt_notsupp_get_policy(struct inode *i,
352 struct fscrypt_policy *p)
353{
354 return -EOPNOTSUPP;
355}
356
357static inline int fscrypt_notsupp_has_permitted_context(struct inode *p,
358 struct inode *i)
359{
360 return 0;
361}
362
363static inline int fscrypt_notsupp_inherit_context(struct inode *p,
364 struct inode *i, void *v, bool b)
365{
366 return -EOPNOTSUPP;
367}
368
369/* keyinfo.c */
370static inline int fscrypt_notsupp_get_encryption_info(struct inode *i)
371{
372 return -EOPNOTSUPP;
373}
374
375static inline void fscrypt_notsupp_put_encryption_info(struct inode *i,
376 struct fscrypt_info *f)
377{
378 return;
379}
380
381 /* fname.c */
382static inline int fscrypt_notsupp_setup_filename(struct inode *dir,
383 const struct qstr *iname,
384 int lookup, struct fscrypt_name *fname)
385{
386 if (dir->i_sb->s_cop->is_encrypted(dir))
387 return -EOPNOTSUPP;
388
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;
393 return 0;
394}
395
396static inline void fscrypt_notsupp_free_filename(struct fscrypt_name *fname)
397{
398 return;
399}
400
401static inline u32 fscrypt_notsupp_fname_encrypted_size(struct inode *i, u32 s)
402{
403 /* never happens */
404 WARN_ON(1);
405 return 0;
406}
407
408static inline int fscrypt_notsupp_fname_alloc_buffer(struct inode *inode,
409 u32 ilen, struct fscrypt_str *crypto_str)
410{
411 return -EOPNOTSUPP;
412}
413
414static inline void fscrypt_notsupp_fname_free_buffer(struct fscrypt_str *c)
415{
416 return;
417}
418
419static 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)
423{
424 return -EOPNOTSUPP;
425}
426
427static inline int fscrypt_notsupp_fname_usr_to_disk(struct inode *inode,
428 const struct qstr *iname,
429 struct fscrypt_str *oname)
430{
431 return -EOPNOTSUPP;
432}
433#endif /* _LINUX_FSCRYPTO_H */
This page took 0.039054 seconds and 5 git commands to generate.