blob: 4022c61f7e9b4d6186b0e7bfd9faf364c708ee17 [file] [log] [blame]
Hyojun Kim63da4202017-10-06 17:10:08 -07001/*
2 * fscrypt_common.h: common declarations for per-file encryption
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_FSCRYPT_COMMON_H
11#define _LINUX_FSCRYPT_COMMON_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_CRYPTO_BLOCK_SIZE 16
22
23struct fscrypt_info;
24
25struct fscrypt_ctx {
26 union {
27 struct {
28 struct page *bounce_page; /* Ciphertext page */
29 struct page *control_page; /* Original page */
30 } w;
31 struct {
32 struct bio *bio;
33 struct work_struct work;
34 } r;
35 struct list_head free_list; /* Free list */
36 };
37 u8 flags; /* Flags */
38};
39
40/**
41 * For encrypted symlinks, the ciphertext length is stored at the beginning
42 * of the string in little-endian format.
43 */
44struct fscrypt_symlink_data {
45 __le16 len;
46 char encrypted_path[1];
47} __packed;
48
49struct fscrypt_str {
50 unsigned char *name;
51 u32 len;
52};
53
54struct fscrypt_name {
55 const struct qstr *usr_fname;
56 struct fscrypt_str disk_name;
57 u32 hash;
58 u32 minor_hash;
59 struct fscrypt_str crypto_buf;
60};
61
62#define FSTR_INIT(n, l) { .name = n, .len = l }
63#define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
64#define fname_name(p) ((p)->disk_name.name)
65#define fname_len(p) ((p)->disk_name.len)
66
67/*
68 * fscrypt superblock flags
69 */
70#define FS_CFLG_OWN_PAGES (1U << 1)
71
72/*
73 * crypto opertions for filesystems
74 */
75struct fscrypt_operations {
76 unsigned int flags;
77 const char *key_prefix;
78 int (*get_context)(struct inode *, void *, size_t);
79 int (*set_context)(struct inode *, const void *, size_t, void *);
80 int (*dummy_context)(struct inode *);
81 bool (*is_encrypted)(struct inode *);
82 bool (*empty_dir)(struct inode *);
83 unsigned (*max_namelen)(struct inode *);
84};
85
86static inline bool fscrypt_dummy_context_enabled(struct inode *inode)
87{
88 if (inode->i_sb->s_cop->dummy_context &&
89 inode->i_sb->s_cop->dummy_context(inode))
90 return true;
91 return false;
92}
93
94static inline bool fscrypt_valid_enc_modes(u32 contents_mode,
95 u32 filenames_mode)
96{
97 if (contents_mode == FS_ENCRYPTION_MODE_AES_128_CBC &&
98 filenames_mode == FS_ENCRYPTION_MODE_AES_128_CTS)
99 return true;
100
101 if (contents_mode == FS_ENCRYPTION_MODE_AES_256_XTS &&
102 filenames_mode == FS_ENCRYPTION_MODE_AES_256_CTS)
103 return true;
104
105 return false;
106}
107
108static inline bool fscrypt_is_dot_dotdot(const struct qstr *str)
109{
110 if (str->len == 1 && str->name[0] == '.')
111 return true;
112
113 if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
114 return true;
115
116 return false;
117}
118
119static inline struct page *fscrypt_control_page(struct page *page)
120{
121#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
122 return ((struct fscrypt_ctx *)page_private(page))->w.control_page;
123#else
124 WARN_ON_ONCE(1);
125 return ERR_PTR(-EINVAL);
126#endif
127}
128
129static inline int fscrypt_has_encryption_key(const struct inode *inode)
130{
131#if IS_ENABLED(CONFIG_FS_ENCRYPTION)
132 return (inode->i_crypt_info != NULL);
133#else
134 return 0;
135#endif
136}
137
138#endif /* _LINUX_FSCRYPT_COMMON_H */