Whamcloud - gitweb
LU-15003 sec: use enc pool for bounce pages
[fs/lustre-release.git] / lustre / include / lustre_crypto.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2019, 2020, Whamcloud.
24  */
25 /*
26  * This file is part of Lustre, http://www.lustre.org/
27  */
28
29 #ifndef _LUSTRE_CRYPTO_H_
30 #define _LUSTRE_CRYPTO_H_
31
32 #if defined(HAVE_LUSTRE_CRYPTO) && !defined(CONFIG_LL_ENCRYPTION)
33 #define __FS_HAS_ENCRYPTION 1
34 #include <linux/fscrypt.h>
35
36 #define LL_CRYPTO_BLOCK_SIZE            FS_CRYPTO_BLOCK_SIZE
37 #define llcrypt_name                    fscrypt_name
38 #define llcrypt_str                     fscrypt_str
39 #define LLTR_INIT                       FSTR_INIT
40 #define llcrypt_operations              fscrypt_operations
41 #define llcrypt_symlink_data            fscrypt_symlink_data
42 #define llcrypt_init()                  0
43 #define llcrypt_exit()                  {}
44 #ifndef HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED
45 #define llcrypt_context                 fscrypt_context
46 #define llcrypt_dummy_context           fscrypt_dummy_context
47 #endif
48 #define llcrypt_require_key(inode)     \
49         fscrypt_require_key(inode)
50 #define llcrypt_has_encryption_key(inode) fscrypt_has_encryption_key(inode)
51 #define llcrypt_encrypt_pagecache_blocks(page, len, offs, gfp_flags)    \
52         fscrypt_encrypt_pagecache_blocks(page, len, offs, gfp_flags)
53 #define llcrypt_decrypt_pagecache_blocks(page, len, offs)       \
54         fscrypt_decrypt_pagecache_blocks(page, len, offs)
55 #define llcrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num) \
56         fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num)
57 #define llcrypt_inherit_context(parent, child, fs_data, preload)        \
58         fscrypt_inherit_context(parent, child, fs_data, preload)
59 #define llcrypt_get_encryption_info(inode) fscrypt_get_encryption_info(inode)
60 #define llcrypt_put_encryption_info(inode) fscrypt_put_encryption_info(inode)
61 #define llcrypt_free_inode(inode)          fscrypt_free_inode(inode)
62 #define llcrypt_finalize_bounce_page(pagep)  fscrypt_finalize_bounce_page(pagep)
63 #define llcrypt_file_open(inode, filp)  fscrypt_file_open(inode, filp)
64 #define llcrypt_ioctl_set_policy(filp, arg)  fscrypt_ioctl_set_policy(filp, arg)
65 #define llcrypt_ioctl_get_policy_ex(filp, arg)  \
66         fscrypt_ioctl_get_policy_ex(filp, arg)
67 #define llcrypt_policy_has_filename_enc(inode) true
68 #define llcrypt_ioctl_add_key(filp, arg)        fscrypt_ioctl_add_key(filp, arg)
69 #define llcrypt_ioctl_remove_key(filp, arg)  fscrypt_ioctl_remove_key(filp, arg)
70 #define llcrypt_ioctl_remove_key_all_users(filp, arg)   \
71         fscrypt_ioctl_remove_key_all_users(filp, arg)
72 #define llcrypt_ioctl_get_key_status(filp, arg) \
73         fscrypt_ioctl_get_key_status(filp, arg)
74 #define llcrypt_drop_inode(inode)       fscrypt_drop_inode(inode)
75 #define llcrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags) \
76         fscrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags)
77 #define llcrypt_prepare_link(old_dentry, dir, dentry)   \
78         fscrypt_prepare_link(old_dentry, dir, dentry)
79 #define llcrypt_prepare_setattr(dentry, attr)           \
80         fscrypt_prepare_setattr(dentry, attr)
81 #define __llcrypt_prepare_lookup(inode, dentry, fname)  \
82         __fscrypt_prepare_lookup(inode, dentry, fname)
83 #define llcrypt_set_ops(sb, cop)        fscrypt_set_ops(sb, cop)
84 #define llcrypt_sb_free(sb)             {}
85 #define llcrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str) \
86         fscrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str)
87 #define llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname) \
88         fscrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname)
89 #define llcrypt_fname_free_buffer(crypto_str) \
90         fscrypt_fname_free_buffer(crypto_str)
91 #define llcrypt_setup_filename(dir, iname, lookup, fname) \
92         fscrypt_setup_filename(dir, iname, lookup, fname)
93 #define llcrypt_free_filename(fname) \
94         fscrypt_free_filename(fname)
95 #define llcrypt_match_name(fname, de_name, name_len)            \
96         fscrypt_match_name(fname, de_name, name_len)
97 #define llcrypt_prepare_lookup(dir, dentry, fname) \
98         fscrypt_prepare_lookup(dir, dentry, fname)
99 #define llcrypt_encrypt_symlink(inode, target, len, disk_link) \
100         fscrypt_encrypt_symlink(inode, target, len, disk_link)
101 #define __llcrypt_encrypt_symlink(inode, target, len, disk_link) \
102         __fscrypt_encrypt_symlink(inode, target, len, disk_link)
103 #define llcrypt_prepare_symlink(dir, target, len, max_len, disk_link)   \
104         fscrypt_prepare_symlink(dir, target, len, max_len, disk_link)
105 #define llcrypt_get_symlink(inode, caddr, max_size, done) \
106         fscrypt_get_symlink(inode, caddr, max_size, done)
107
108 #define LL_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
109 #define LL_IOC_GET_ENCRYPTION_POLICY_EX FS_IOC_GET_ENCRYPTION_POLICY_EX
110 #define LL_IOC_ADD_ENCRYPTION_KEY FS_IOC_ADD_ENCRYPTION_KEY
111 #define LL_IOC_REMOVE_ENCRYPTION_KEY FS_IOC_REMOVE_ENCRYPTION_KEY
112 #define LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS \
113         FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
114 #define LL_IOC_GET_ENCRYPTION_KEY_STATUS FS_IOC_GET_ENCRYPTION_KEY_STATUS
115
116 #else /* HAVE_LUSTRE_CRYPTO && !CONFIG_LL_ENCRYPTION */
117 #include <libcfs/crypto/llcrypt.h>
118 #endif /* !HAVE_LUSTRE_CRYPTO || CONFIG_LL_ENCRYPTION */
119
120 #ifndef DCACHE_NOKEY_NAME
121 #define DCACHE_NOKEY_NAME               0x02000000 /* Enc name without key */
122 #endif
123
124 #if !defined(HAVE_FSCRYPT_IS_NOKEY_NAME) || defined(CONFIG_LL_ENCRYPTION)
125
126 static inline bool llcrypt_is_nokey_name(const struct dentry *dentry)
127 {
128         return dentry->d_flags & DCACHE_NOKEY_NAME;
129 }
130 #else
131 #define llcrypt_is_nokey_name(dentry)           \
132         fscrypt_is_nokey_name(dentry)
133 #endif
134
135 #if defined(HAVE_LUSTRE_CRYPTO) && !defined(HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED)
136 #define llcrypt_show_test_dummy_encryption(seq, sep, sb)        \
137         fscrypt_show_test_dummy_encryption(seq, sep, sb)
138 #define llcrypt_set_test_dummy_encryption(sb, arg, ctx)         \
139         fscrypt_set_test_dummy_encryption(sb, arg, ctx)
140 #define llcrypt_free_dummy_context(ctx)                         \
141         fscrypt_free_dummy_context(ctx)
142 #else
143 #define llcrypt_show_test_dummy_encryption(seq, sep, sb)        {}
144 #define llcrypt_free_dummy_context(ctx)                         {}
145 #endif
146
147 /* Macro to extract digest from Lustre specific structures */
148 #if defined(HAVE_FSCRYPT_DIGESTED_NAME) && !defined(CONFIG_LL_ENCRYPTION)
149 #define LLCRYPT_EXTRACT_DIGEST          FSCRYPT_FNAME_DIGEST
150 #else
151 #define LLCRYPT_EXTRACT_DIGEST(name, len)                       \
152         ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1,  \
153                              LL_CRYPTO_BLOCK_SIZE))
154 #endif
155
156 struct ll_sb_info;
157 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
158                     bool preload);
159 void llcrypt_free_ctx(void *encctx, __u32 size);
160 bool ll_sb_has_test_dummy_encryption(struct super_block *sb);
161 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi);
162 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set);
163 bool ll_sbi_has_name_encrypt(struct ll_sb_info *sbi);
164 void ll_sbi_set_name_encrypt(struct ll_sb_info *sbi, bool set);
165 /* sizeof(struct fscrypt_context_v2) = 40 */
166 #define LLCRYPT_ENC_CTX_SIZE 40
167
168 /* Encoding/decoding routines inspired from yEnc principles.
169  * We just take care of a few critical characters:
170  * NULL, LF, CR, /, DEL and =.
171  * If such a char is found, it is replaced with '=' followed by
172  * the char value + 64.
173  * All other chars are left untouched.
174  * Efficiency of this encoding depends on the occurences of the
175  * critical chars, but statistically on binary data it can be much higher
176  * than base64 for instance.
177  */
178 static inline int critical_encode(const u8 *src, int len, char *dst)
179 {
180         u8 *p = (u8 *)src, *q = dst;
181
182         while (p - src < len) {
183                 /* escape NULL, LF, CR, /, DEL and = */
184                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
185                              *p == '/' || *p == 0x7F || *p == '=')) {
186                         *(q++) = '=';
187                         *(q++) = *(p++) + 64;
188                 } else {
189                         *(q++) = *(p++);
190                 }
191         }
192
193         return (char *)q - dst;
194 }
195
196 /* returns the number of chars encoding would produce */
197 static inline int critical_chars(const u8 *src, int len)
198 {
199         u8 *p = (u8 *)src;
200         int newlen = len;
201
202         while (p - src < len) {
203                 /* NULL, LF, CR, /, DEL and = cost an additional '=' */
204                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
205                              *p == '/' || *p == 0x7F || *p == '='))
206                         newlen++;
207                 p++;
208         }
209
210         return newlen;
211 }
212
213 /* decoding routine - returns the number of chars in output */
214 static inline int critical_decode(const u8 *src, int len, char *dst)
215 {
216         u8 *p = (u8 *)src, *q = dst;
217
218         while (p - src < len) {
219                 if (unlikely(*p == '=')) {
220                         *(q++) = *(++p) - 64;
221                         p++;
222                 } else {
223                         *(q++) = *(p++);
224                 }
225         }
226
227         return (char *)q - dst;
228 }
229
230 #endif /* _LUSTRE_CRYPTO_H_ */