Whamcloud - gitweb
LU-15922 sec: new connect flag for name encryption
[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_encrypt_block_inplace(inode, page, len, offs, lblk, gfp_flags) \
54         fscrypt_encrypt_block_inplace(inode, page, len, offs, lblk, gfp_flags)
55 #define llcrypt_decrypt_pagecache_blocks(page, len, offs)       \
56         fscrypt_decrypt_pagecache_blocks(page, len, offs)
57 #define llcrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num) \
58         fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num)
59 #define llcrypt_inherit_context(parent, child, fs_data, preload)        \
60         fscrypt_inherit_context(parent, child, fs_data, preload)
61 #define llcrypt_get_encryption_info(inode) fscrypt_get_encryption_info(inode)
62 #define llcrypt_put_encryption_info(inode) fscrypt_put_encryption_info(inode)
63 #define llcrypt_free_inode(inode)          fscrypt_free_inode(inode)
64 #define llcrypt_finalize_bounce_page(pagep)  fscrypt_finalize_bounce_page(pagep)
65 #define llcrypt_file_open(inode, filp)  fscrypt_file_open(inode, filp)
66 #define llcrypt_ioctl_set_policy(filp, arg)  fscrypt_ioctl_set_policy(filp, arg)
67 #define llcrypt_ioctl_get_policy_ex(filp, arg)  \
68         fscrypt_ioctl_get_policy_ex(filp, arg)
69 #define llcrypt_policy_has_filename_enc(inode) true
70 #define llcrypt_ioctl_add_key(filp, arg)        fscrypt_ioctl_add_key(filp, arg)
71 #define llcrypt_ioctl_remove_key(filp, arg)  fscrypt_ioctl_remove_key(filp, arg)
72 #define llcrypt_ioctl_remove_key_all_users(filp, arg)   \
73         fscrypt_ioctl_remove_key_all_users(filp, arg)
74 #define llcrypt_ioctl_get_key_status(filp, arg) \
75         fscrypt_ioctl_get_key_status(filp, arg)
76 #define llcrypt_drop_inode(inode)       fscrypt_drop_inode(inode)
77 #define llcrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags) \
78         fscrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags)
79 #define llcrypt_prepare_link(old_dentry, dir, dentry)   \
80         fscrypt_prepare_link(old_dentry, dir, dentry)
81 #define llcrypt_prepare_setattr(dentry, attr)           \
82         fscrypt_prepare_setattr(dentry, attr)
83 #define __llcrypt_prepare_lookup(inode, dentry, fname)  \
84         __fscrypt_prepare_lookup(inode, dentry, fname)
85 #define llcrypt_set_ops(sb, cop)        fscrypt_set_ops(sb, cop)
86 #define llcrypt_sb_free(sb)             {}
87 #define llcrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str) \
88         fscrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str)
89 #define llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname) \
90         fscrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname)
91 #define llcrypt_fname_free_buffer(crypto_str) \
92         fscrypt_fname_free_buffer(crypto_str)
93 #define llcrypt_setup_filename(dir, iname, lookup, fname) \
94         fscrypt_setup_filename(dir, iname, lookup, fname)
95 #define llcrypt_free_filename(fname) \
96         fscrypt_free_filename(fname)
97 #define llcrypt_match_name(fname, de_name, name_len)            \
98         fscrypt_match_name(fname, de_name, name_len)
99 #define llcrypt_prepare_lookup(dir, dentry, fname) \
100         fscrypt_prepare_lookup(dir, dentry, fname)
101 #define llcrypt_encrypt_symlink(inode, target, len, disk_link) \
102         fscrypt_encrypt_symlink(inode, target, len, disk_link)
103 #define __llcrypt_encrypt_symlink(inode, target, len, disk_link) \
104         __fscrypt_encrypt_symlink(inode, target, len, disk_link)
105 #define llcrypt_prepare_symlink(dir, target, len, max_len, disk_link)   \
106         fscrypt_prepare_symlink(dir, target, len, max_len, disk_link)
107 #define llcrypt_get_symlink(inode, caddr, max_size, done) \
108         fscrypt_get_symlink(inode, caddr, max_size, done)
109
110 #define LL_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
111 #define LL_IOC_GET_ENCRYPTION_POLICY_EX FS_IOC_GET_ENCRYPTION_POLICY_EX
112 #define LL_IOC_ADD_ENCRYPTION_KEY FS_IOC_ADD_ENCRYPTION_KEY
113 #define LL_IOC_REMOVE_ENCRYPTION_KEY FS_IOC_REMOVE_ENCRYPTION_KEY
114 #define LL_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS \
115         FS_IOC_REMOVE_ENCRYPTION_KEY_ALL_USERS
116 #define LL_IOC_GET_ENCRYPTION_KEY_STATUS FS_IOC_GET_ENCRYPTION_KEY_STATUS
117
118 #else /* HAVE_LUSTRE_CRYPTO && !CONFIG_LL_ENCRYPTION */
119 #include <libcfs/crypto/llcrypt.h>
120 #endif /* !HAVE_LUSTRE_CRYPTO || CONFIG_LL_ENCRYPTION */
121
122 #ifndef DCACHE_NOKEY_NAME
123 #define DCACHE_NOKEY_NAME               0x02000000 /* Enc name without key */
124 #endif
125
126 #if !defined(HAVE_FSCRYPT_IS_NOKEY_NAME) || defined(CONFIG_LL_ENCRYPTION)
127
128 static inline bool llcrypt_is_nokey_name(const struct dentry *dentry)
129 {
130         return dentry->d_flags & DCACHE_NOKEY_NAME;
131 }
132 #else
133 #define llcrypt_is_nokey_name(dentry)           \
134         fscrypt_is_nokey_name(dentry)
135 #endif
136
137 #if defined(HAVE_LUSTRE_CRYPTO) && !defined(HAVE_FSCRYPT_DUMMY_CONTEXT_ENABLED)
138 #define llcrypt_show_test_dummy_encryption(seq, sep, sb)        \
139         fscrypt_show_test_dummy_encryption(seq, sep, sb)
140 #define llcrypt_set_test_dummy_encryption(sb, arg, ctx)         \
141         fscrypt_set_test_dummy_encryption(sb, arg, ctx)
142 #define llcrypt_free_dummy_context(ctx)                         \
143         fscrypt_free_dummy_context(ctx)
144 #else
145 #define llcrypt_show_test_dummy_encryption(seq, sep, sb)        {}
146 #define llcrypt_free_dummy_context(ctx)                         {}
147 #endif
148
149 /* Macro to extract digest from Lustre specific structures */
150 #if defined(HAVE_FSCRYPT_DIGESTED_NAME) && !defined(CONFIG_LL_ENCRYPTION)
151 #define LLCRYPT_EXTRACT_DIGEST          FSCRYPT_FNAME_DIGEST
152 #else
153 #define LLCRYPT_EXTRACT_DIGEST(name, len)                       \
154         ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1,  \
155                              LL_CRYPTO_BLOCK_SIZE))
156 #endif
157
158 struct ll_sb_info;
159 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
160                     bool preload);
161 void llcrypt_free_ctx(void *encctx, __u32 size);
162 bool ll_sb_has_test_dummy_encryption(struct super_block *sb);
163 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi);
164 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set);
165 bool ll_sbi_has_name_encrypt(struct ll_sb_info *sbi);
166 void ll_sbi_set_name_encrypt(struct ll_sb_info *sbi, bool set);
167 /* sizeof(struct fscrypt_context_v2) = 40 */
168 #define LLCRYPT_ENC_CTX_SIZE 40
169
170 /* Encoding/decoding routines inspired from yEnc principles.
171  * We just take care of a few critical characters:
172  * NULL, LF, CR, /, DEL and =.
173  * If such a char is found, it is replaced with '=' followed by
174  * the char value + 64.
175  * All other chars are left untouched.
176  * Efficiency of this encoding depends on the occurences of the
177  * critical chars, but statistically on binary data it can be much higher
178  * than base64 for instance.
179  */
180 static inline int critical_encode(const u8 *src, int len, char *dst)
181 {
182         u8 *p = (u8 *)src, *q = dst;
183
184         while (p - src < len) {
185                 /* escape NULL, LF, CR, /, DEL and = */
186                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
187                              *p == '/' || *p == 0x7F || *p == '=')) {
188                         *(q++) = '=';
189                         *(q++) = *(p++) + 64;
190                 } else {
191                         *(q++) = *(p++);
192                 }
193         }
194
195         return (char *)q - dst;
196 }
197
198 /* returns the number of chars encoding would produce */
199 static inline int critical_chars(const u8 *src, int len)
200 {
201         u8 *p = (u8 *)src;
202         int newlen = len;
203
204         while (p - src < len) {
205                 /* NULL, LF, CR, /, DEL and = cost an additional '=' */
206                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
207                              *p == '/' || *p == 0x7F || *p == '='))
208                         newlen++;
209                 p++;
210         }
211
212         return newlen;
213 }
214
215 /* decoding routine - returns the number of chars in output */
216 static inline int critical_decode(const u8 *src, int len, char *dst)
217 {
218         u8 *p = (u8 *)src, *q = dst;
219
220         while (p - src < len) {
221                 if (unlikely(*p == '=')) {
222                         *(q++) = *(++p) - 64;
223                         p++;
224                 } else {
225                         *(q++) = *(p++);
226                 }
227         }
228
229         return (char *)q - dst;
230 }
231
232 #endif /* _LUSTRE_CRYPTO_H_ */