fs crypto: move per-file encryption from f2fs tree to fs/crypto
[deliverable/linux.git] / fs / crypto / fname.c
1 /*
2 * This contains functions for filename crypto management
3 *
4 * Copyright (C) 2015, Google, Inc.
5 * Copyright (C) 2015, Motorola Mobility
6 *
7 * Written by Uday Savagaonkar, 2014.
8 * Modified by Jaegeuk Kim, 2015.
9 *
10 * This has not yet undergone a rigorous security audit.
11 */
12
13 #include <crypto/hash.h>
14 #include <crypto/sha.h>
15 #include <keys/encrypted-type.h>
16 #include <keys/user-type.h>
17 #include <linux/crypto.h>
18 #include <linux/scatterlist.h>
19 #include <linux/ratelimit.h>
20 #include <linux/fscrypto.h>
21
22 static u32 size_round_up(size_t size, size_t blksize)
23 {
24 return ((size + blksize - 1) / blksize) * blksize;
25 }
26
27 /**
28 * dir_crypt_complete() -
29 */
30 static void dir_crypt_complete(struct crypto_async_request *req, int res)
31 {
32 struct fscrypt_completion_result *ecr = req->data;
33
34 if (res == -EINPROGRESS)
35 return;
36 ecr->res = res;
37 complete(&ecr->completion);
38 }
39
40 /**
41 * fname_encrypt() -
42 *
43 * This function encrypts the input filename, and returns the length of the
44 * ciphertext. Errors are returned as negative numbers. We trust the caller to
45 * allocate sufficient memory to oname string.
46 */
47 static int fname_encrypt(struct inode *inode,
48 const struct qstr *iname, struct fscrypt_str *oname)
49 {
50 u32 ciphertext_len;
51 struct ablkcipher_request *req = NULL;
52 DECLARE_FS_COMPLETION_RESULT(ecr);
53 struct fscrypt_info *ci = inode->i_crypt_info;
54 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
55 int res = 0;
56 char iv[FS_CRYPTO_BLOCK_SIZE];
57 struct scatterlist src_sg, dst_sg;
58 int padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
59 char *workbuf, buf[32], *alloc_buf = NULL;
60 unsigned lim;
61
62 lim = inode->i_sb->s_cop->max_namelen(inode);
63 if (iname->len <= 0 || iname->len > lim)
64 return -EIO;
65
66 ciphertext_len = (iname->len < FS_CRYPTO_BLOCK_SIZE) ?
67 FS_CRYPTO_BLOCK_SIZE : iname->len;
68 ciphertext_len = size_round_up(ciphertext_len, padding);
69 ciphertext_len = (ciphertext_len > lim) ? lim : ciphertext_len;
70
71 if (ciphertext_len <= sizeof(buf)) {
72 workbuf = buf;
73 } else {
74 alloc_buf = kmalloc(ciphertext_len, GFP_NOFS);
75 if (!alloc_buf)
76 return -ENOMEM;
77 workbuf = alloc_buf;
78 }
79
80 /* Allocate request */
81 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
82 if (!req) {
83 printk_ratelimited(KERN_ERR
84 "%s: crypto_request_alloc() failed\n", __func__);
85 kfree(alloc_buf);
86 return -ENOMEM;
87 }
88 ablkcipher_request_set_callback(req,
89 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
90 dir_crypt_complete, &ecr);
91
92 /* Copy the input */
93 memcpy(workbuf, iname->name, iname->len);
94 if (iname->len < ciphertext_len)
95 memset(workbuf + iname->len, 0, ciphertext_len - iname->len);
96
97 /* Initialize IV */
98 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
99
100 /* Create encryption request */
101 sg_init_one(&src_sg, workbuf, ciphertext_len);
102 sg_init_one(&dst_sg, oname->name, ciphertext_len);
103 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, ciphertext_len, iv);
104 res = crypto_ablkcipher_encrypt(req);
105 if (res == -EINPROGRESS || res == -EBUSY) {
106 wait_for_completion(&ecr.completion);
107 res = ecr.res;
108 }
109 kfree(alloc_buf);
110 ablkcipher_request_free(req);
111 if (res < 0)
112 printk_ratelimited(KERN_ERR
113 "%s: Error (error code %d)\n", __func__, res);
114
115 oname->len = ciphertext_len;
116 return res;
117 }
118
119 /*
120 * fname_decrypt()
121 * This function decrypts the input filename, and returns
122 * the length of the plaintext.
123 * Errors are returned as negative numbers.
124 * We trust the caller to allocate sufficient memory to oname string.
125 */
126 static int fname_decrypt(struct inode *inode,
127 const struct fscrypt_str *iname,
128 struct fscrypt_str *oname)
129 {
130 struct ablkcipher_request *req = NULL;
131 DECLARE_FS_COMPLETION_RESULT(ecr);
132 struct scatterlist src_sg, dst_sg;
133 struct fscrypt_info *ci = inode->i_crypt_info;
134 struct crypto_ablkcipher *tfm = ci->ci_ctfm;
135 int res = 0;
136 char iv[FS_CRYPTO_BLOCK_SIZE];
137 unsigned lim;
138
139 lim = inode->i_sb->s_cop->max_namelen(inode);
140 if (iname->len <= 0 || iname->len > lim)
141 return -EIO;
142
143 /* Allocate request */
144 req = ablkcipher_request_alloc(tfm, GFP_NOFS);
145 if (!req) {
146 printk_ratelimited(KERN_ERR
147 "%s: crypto_request_alloc() failed\n", __func__);
148 return -ENOMEM;
149 }
150 ablkcipher_request_set_callback(req,
151 CRYPTO_TFM_REQ_MAY_BACKLOG | CRYPTO_TFM_REQ_MAY_SLEEP,
152 dir_crypt_complete, &ecr);
153
154 /* Initialize IV */
155 memset(iv, 0, FS_CRYPTO_BLOCK_SIZE);
156
157 /* Create decryption request */
158 sg_init_one(&src_sg, iname->name, iname->len);
159 sg_init_one(&dst_sg, oname->name, oname->len);
160 ablkcipher_request_set_crypt(req, &src_sg, &dst_sg, iname->len, iv);
161 res = crypto_ablkcipher_decrypt(req);
162 if (res == -EINPROGRESS || res == -EBUSY) {
163 wait_for_completion(&ecr.completion);
164 res = ecr.res;
165 }
166 ablkcipher_request_free(req);
167 if (res < 0) {
168 printk_ratelimited(KERN_ERR
169 "%s: Error (error code %d)\n", __func__, res);
170 return res;
171 }
172
173 oname->len = strnlen(oname->name, iname->len);
174 return oname->len;
175 }
176
177 static const char *lookup_table =
178 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,";
179
180 /**
181 * digest_encode() -
182 *
183 * Encodes the input digest using characters from the set [a-zA-Z0-9_+].
184 * The encoded string is roughly 4/3 times the size of the input string.
185 */
186 static int digest_encode(const char *src, int len, char *dst)
187 {
188 int i = 0, bits = 0, ac = 0;
189 char *cp = dst;
190
191 while (i < len) {
192 ac += (((unsigned char) src[i]) << bits);
193 bits += 8;
194 do {
195 *cp++ = lookup_table[ac & 0x3f];
196 ac >>= 6;
197 bits -= 6;
198 } while (bits >= 6);
199 i++;
200 }
201 if (bits)
202 *cp++ = lookup_table[ac & 0x3f];
203 return cp - dst;
204 }
205
206 static int digest_decode(const char *src, int len, char *dst)
207 {
208 int i = 0, bits = 0, ac = 0;
209 const char *p;
210 char *cp = dst;
211
212 while (i < len) {
213 p = strchr(lookup_table, src[i]);
214 if (p == NULL || src[i] == 0)
215 return -2;
216 ac += (p - lookup_table) << bits;
217 bits += 6;
218 if (bits >= 8) {
219 *cp++ = ac & 0xff;
220 ac >>= 8;
221 bits -= 8;
222 }
223 i++;
224 }
225 if (ac)
226 return -1;
227 return cp - dst;
228 }
229
230 u32 fscrypt_fname_encrypted_size(struct inode *inode, u32 ilen)
231 {
232 int padding = 32;
233 struct fscrypt_info *ci = inode->i_crypt_info;
234
235 if (ci)
236 padding = 4 << (ci->ci_flags & FS_POLICY_FLAGS_PAD_MASK);
237 if (ilen < FS_CRYPTO_BLOCK_SIZE)
238 ilen = FS_CRYPTO_BLOCK_SIZE;
239 return size_round_up(ilen, padding);
240 }
241 EXPORT_SYMBOL(fscrypt_fname_encrypted_size);
242
243 /**
244 * fscrypt_fname_crypto_alloc_obuff() -
245 *
246 * Allocates an output buffer that is sufficient for the crypto operation
247 * specified by the context and the direction.
248 */
249 int fscrypt_fname_alloc_buffer(struct inode *inode,
250 u32 ilen, struct fscrypt_str *crypto_str)
251 {
252 unsigned int olen = fscrypt_fname_encrypted_size(inode, ilen);
253
254 crypto_str->len = olen;
255 if (olen < FS_FNAME_CRYPTO_DIGEST_SIZE * 2)
256 olen = FS_FNAME_CRYPTO_DIGEST_SIZE * 2;
257 /*
258 * Allocated buffer can hold one more character to null-terminate the
259 * string
260 */
261 crypto_str->name = kmalloc(olen + 1, GFP_NOFS);
262 if (!(crypto_str->name))
263 return -ENOMEM;
264 return 0;
265 }
266 EXPORT_SYMBOL(fscrypt_fname_alloc_buffer);
267
268 /**
269 * fscrypt_fname_crypto_free_buffer() -
270 *
271 * Frees the buffer allocated for crypto operation.
272 */
273 void fscrypt_fname_free_buffer(struct fscrypt_str *crypto_str)
274 {
275 if (!crypto_str)
276 return;
277 kfree(crypto_str->name);
278 crypto_str->name = NULL;
279 }
280 EXPORT_SYMBOL(fscrypt_fname_free_buffer);
281
282 /**
283 * fscrypt_fname_disk_to_usr() - converts a filename from disk space to user
284 * space
285 */
286 int fscrypt_fname_disk_to_usr(struct inode *inode,
287 u32 hash, u32 minor_hash,
288 const struct fscrypt_str *iname,
289 struct fscrypt_str *oname)
290 {
291 const struct qstr qname = FSTR_TO_QSTR(iname);
292 char buf[24];
293 int ret;
294
295 if (fscrypt_is_dot_dotdot(&qname)) {
296 oname->name[0] = '.';
297 oname->name[iname->len - 1] = '.';
298 oname->len = iname->len;
299 return oname->len;
300 }
301
302 if (iname->len < FS_CRYPTO_BLOCK_SIZE)
303 return -EUCLEAN;
304
305 if (inode->i_crypt_info)
306 return fname_decrypt(inode, iname, oname);
307
308 if (iname->len <= FS_FNAME_CRYPTO_DIGEST_SIZE) {
309 ret = digest_encode(iname->name, iname->len, oname->name);
310 oname->len = ret;
311 return ret;
312 }
313 if (hash) {
314 memcpy(buf, &hash, 4);
315 memcpy(buf + 4, &minor_hash, 4);
316 } else {
317 memset(buf, 0, 8);
318 }
319 memcpy(buf + 8, iname->name + iname->len - 16, 16);
320 oname->name[0] = '_';
321 ret = digest_encode(buf, 24, oname->name + 1);
322 oname->len = ret + 1;
323 return ret + 1;
324 }
325 EXPORT_SYMBOL(fscrypt_fname_disk_to_usr);
326
327 /**
328 * fscrypt_fname_usr_to_disk() - converts a filename from user space to disk
329 * space
330 */
331 int fscrypt_fname_usr_to_disk(struct inode *inode,
332 const struct qstr *iname,
333 struct fscrypt_str *oname)
334 {
335 if (fscrypt_is_dot_dotdot(iname)) {
336 oname->name[0] = '.';
337 oname->name[iname->len - 1] = '.';
338 oname->len = iname->len;
339 return oname->len;
340 }
341 if (inode->i_crypt_info)
342 return fname_encrypt(inode, iname, oname);
343 /*
344 * Without a proper key, a user is not allowed to modify the filenames
345 * in a directory. Consequently, a user space name cannot be mapped to
346 * a disk-space name
347 */
348 return -EACCES;
349 }
350 EXPORT_SYMBOL(fscrypt_fname_usr_to_disk);
351
352 int fscrypt_setup_filename(struct inode *dir, const struct qstr *iname,
353 int lookup, struct fscrypt_name *fname)
354 {
355 int ret = 0, bigname = 0;
356
357 memset(fname, 0, sizeof(struct fscrypt_name));
358 fname->usr_fname = iname;
359
360 if (!dir->i_sb->s_cop->is_encrypted(dir) ||
361 fscrypt_is_dot_dotdot(iname)) {
362 fname->disk_name.name = (unsigned char *)iname->name;
363 fname->disk_name.len = iname->len;
364 return 0;
365 }
366 ret = get_crypt_info(dir);
367 if (ret && ret != -EOPNOTSUPP)
368 return ret;
369
370 if (dir->i_crypt_info) {
371 ret = fscrypt_fname_alloc_buffer(dir, iname->len,
372 &fname->crypto_buf);
373 if (ret < 0)
374 return ret;
375 ret = fname_encrypt(dir, iname, &fname->crypto_buf);
376 if (ret < 0)
377 goto errout;
378 fname->disk_name.name = fname->crypto_buf.name;
379 fname->disk_name.len = fname->crypto_buf.len;
380 return 0;
381 }
382 if (!lookup)
383 return -EACCES;
384
385 /*
386 * We don't have the key and we are doing a lookup; decode the
387 * user-supplied name
388 */
389 if (iname->name[0] == '_')
390 bigname = 1;
391 if ((bigname && (iname->len != 33)) || (!bigname && (iname->len > 43)))
392 return -ENOENT;
393
394 fname->crypto_buf.name = kmalloc(32, GFP_KERNEL);
395 if (fname->crypto_buf.name == NULL)
396 return -ENOMEM;
397
398 ret = digest_decode(iname->name + bigname, iname->len - bigname,
399 fname->crypto_buf.name);
400 if (ret < 0) {
401 ret = -ENOENT;
402 goto errout;
403 }
404 fname->crypto_buf.len = ret;
405 if (bigname) {
406 memcpy(&fname->hash, fname->crypto_buf.name, 4);
407 memcpy(&fname->minor_hash, fname->crypto_buf.name + 4, 4);
408 } else {
409 fname->disk_name.name = fname->crypto_buf.name;
410 fname->disk_name.len = fname->crypto_buf.len;
411 }
412 return 0;
413
414 errout:
415 fscrypt_fname_free_buffer(&fname->crypto_buf);
416 return ret;
417 }
418 EXPORT_SYMBOL(fscrypt_setup_filename);
419
420 void fscrypt_free_filename(struct fscrypt_name *fname)
421 {
422 kfree(fname->crypto_buf.name);
423 fname->crypto_buf.name = NULL;
424 fname->usr_fname = NULL;
425 fname->disk_name.name = NULL;
426 }
427 EXPORT_SYMBOL(fscrypt_free_filename);
This page took 0.074653 seconds and 5 git commands to generate.