Whamcloud - gitweb
LU-14677 sec: migrate/extend/split on encrypted file
[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 struct ll_sb_info;
33 int ll_set_encflags(struct inode *inode, void *encctx, __u32 encctxlen,
34                     bool preload);
35 void llcrypt_free_ctx(void *encctx, __u32 size);
36 bool ll_sbi_has_test_dummy_encryption(struct ll_sb_info *sbi);
37 bool ll_sbi_has_encrypt(struct ll_sb_info *sbi);
38 void ll_sbi_set_encrypt(struct ll_sb_info *sbi, bool set);
39 /* sizeof(struct fscrypt_context_v2) = 40 */
40 #define LLCRYPT_ENC_CTX_SIZE 40
41
42
43 /* Encoding/decoding routines inspired from yEnc principles.
44  * We just take care of a few critical characters:
45  * NULL, LF, CR, /, DEL and =.
46  * If such a char is found, it is replaced with '=' followed by
47  * the char value + 64.
48  * All other chars are left untouched.
49  * Efficiency of this encoding depends on the occurences of the
50  * critical chars, but statistically on binary data it can be much higher
51  * than base64 for instance.
52  */
53 static inline int critical_encode(const u8 *src, int len, char *dst)
54 {
55         u8 *p = (u8 *)src, *q = dst;
56
57         while (p - src < len) {
58                 /* escape NULL, LF, CR, /, DEL and = */
59                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
60                              *p == '/' || *p == 0x7F || *p == '=')) {
61                         *(q++) = '=';
62                         *(q++) = *(p++) + 64;
63                 } else {
64                         *(q++) = *(p++);
65                 }
66         }
67
68         return (char *)q - dst;
69 }
70
71 /* returns the number of chars encoding would produce */
72 static inline int critical_chars(const u8 *src, int len)
73 {
74         u8 *p = (u8 *)src;
75         int newlen = len;
76
77         while (p - src < len) {
78                 /* NULL, LF, CR, /, DEL and = cost an additional '=' */
79                 if (unlikely(*p == 0x0 || *p == 0xA || *p == 0xD ||
80                              *p == '/' || *p == 0x7F || *p == '='))
81                         newlen++;
82                 p++;
83         }
84
85         return newlen;
86 }
87
88 /* decoding routine - returns the number of chars in output */
89 static inline int critical_decode(const u8 *src, int len, char *dst)
90 {
91         u8 *p = (u8 *)src, *q = dst;
92
93         while (p - src < len) {
94                 if (unlikely(*p == '=')) {
95                         *(q++) = *(++p) - 64;
96                         p++;
97                 } else {
98                         *(q++) = *(p++);
99                 }
100         }
101
102         return (char *)q - dst;
103 }
104
105 #ifdef CONFIG_LL_ENCRYPTION
106 #include <libcfs/crypto/llcrypt.h>
107 #else /* !CONFIG_LL_ENCRYPTION */
108 #ifdef HAVE_LUSTRE_CRYPTO
109 #define __FS_HAS_ENCRYPTION 1
110 #include <linux/fscrypt.h>
111
112 #define llcrypt_operations      fscrypt_operations
113 #define llcrypt_symlink_data    fscrypt_symlink_data
114 #define llcrypt_dummy_context_enabled(inode) \
115         fscrypt_dummy_context_enabled(inode)
116 #define llcrypt_has_encryption_key(inode) fscrypt_has_encryption_key(inode)
117 #define llcrypt_encrypt_pagecache_blocks(page, len, offs, gfp_flags)    \
118         fscrypt_encrypt_pagecache_blocks(page, len, offs, gfp_flags)
119 #define llcrypt_encrypt_block_inplace(inode, page, len, offs, lblk, gfp_flags) \
120         fscrypt_encrypt_block_inplace(inode, page, len, offs, lblk, gfp_flags)
121 #define llcrypt_decrypt_pagecache_blocks(page, len, offs)       \
122         fscrypt_decrypt_pagecache_blocks(page, len, offs)
123 #define llcrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num) \
124         fscrypt_decrypt_block_inplace(inode, page, len, offs, lblk_num)
125 #define llcrypt_inherit_context(parent, child, fs_data, preload)        \
126         fscrypt_inherit_context(parent, child, fs_data, preload)
127 #define llcrypt_get_encryption_info(inode) fscrypt_get_encryption_info(inode)
128 #define llcrypt_put_encryption_info(inode) fscrypt_put_encryption_info(inode)
129 #define llcrypt_free_inode(inode)          fscrypt_free_inode(inode)
130 #define llcrypt_finalize_bounce_page(pagep)  fscrypt_finalize_bounce_page(pagep)
131 #define llcrypt_file_open(inode, filp)  fscrypt_file_open(inode, filp)
132 #define llcrypt_ioctl_set_policy(filp, arg)  fscrypt_ioctl_set_policy(filp, arg)
133 #define llcrypt_ioctl_get_policy_ex(filp, arg)  \
134         fscrypt_ioctl_get_policy_ex(filp, arg)
135 #define llcrypt_ioctl_add_key(filp, arg)        fscrypt_ioctl_add_key(filp, arg)
136 #define llcrypt_ioctl_remove_key(filp, arg)  fscrypt_ioctl_remove_key(filp, arg)
137 #define llcrypt_ioctl_remove_key_all_users(filp, arg)   \
138         fscrypt_ioctl_remove_key_all_users(filp, arg)
139 #define llcrypt_ioctl_get_key_status(filp, arg) \
140         fscrypt_ioctl_get_key_status(filp, arg)
141 #define llcrypt_drop_inode(inode)       fscrypt_drop_inode(inode)
142 #define llcrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags) \
143         fscrypt_prepare_rename(olddir, olddentry, newdir, newdentry, flags)
144 #define llcrypt_prepare_link(old_dentry, dir, dentry)   \
145         fscrypt_prepare_link(old_dentry, dir, dentry)
146 #define llcrypt_prepare_setattr(dentry, attr)           \
147         fscrypt_prepare_setattr(dentry, attr)
148 #define llcrypt_set_ops(sb, cop)        fscrypt_set_ops(sb, cop)
149 #define llcrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str) \
150         fscrypt_fname_alloc_buffer(inode, max_encrypted_len, crypto_str)
151 #define llcrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname) \
152         fscrypt_fname_disk_to_usr(inode, hash, minor_hash, iname, oname)
153 #define llcrypt_fname_free_buffer(crypto_str) \
154         fscrypt_fname_free_buffer(crypto_str)
155 #define llcrypt_setup_filename(dir, iname, lookup, fname) \
156         fscrypt_setup_filename(dir, iname, lookup, fname)
157 #define llcrypt_free_filename(fname) \
158         fscrypt_free_filename(fname)
159 #define llcrypt_prepare_lookup(dir, dentry, fname) \
160         fscrypt_prepare_lookup(dir, dentry, fname)
161 #define llcrypt_encrypt_symlink(inode, target, len, disk_link) \
162         fscrypt_encrypt_symlink(inode, target, len, disk_link)
163 #define llcrypt_prepare_symlink(dir, target, len, max_len, disk_link)   \
164         fscrypt_prepare_symlink(dir, target, len, max_len, disk_link)
165 #define llcrypt_get_symlink(inode, caddr, max_size, done) \
166         fscrypt_get_symlink(inode, caddr, max_size, done)
167 #define llcrypt_handle_d_move(dentry) \
168         fscrypt_handle_d_move(dentry)
169 #else /* !HAVE_LUSTRE_CRYPTO */
170 /* Extracts the second-to-last ciphertext block */
171 #define LLCRYPT_FNAME_DIGEST(name, len)                                \
172         ((name) + round_down((len) - LL_CRYPTO_BLOCK_SIZE - 1,         \
173                             LL_CRYPTO_BLOCK_SIZE))
174 #define LLCRYPT_FNAME_DIGEST_SIZE      LL_CRYPTO_BLOCK_SIZE
175 #include <libcfs/crypto/llcrypt.h>
176 #endif /* HAVE_LUSTRE_CRYPTO */
177 #endif /* !CONFIG_LL_ENCRYPTION */
178
179 #endif /* _LUSTRE_CRYPTO_H_ */